(read_braced_string): Check for EOF. If encountered issue an error message.
[official-gcc.git] / gcc / cp / init.c
blob710f61744131367a15a7f628b55b86b99f98f2c4
1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* High-level class interface. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "rtl.h"
31 #include "expr.h"
32 #include "cp-tree.h"
33 #include "flags.h"
34 #include "output.h"
35 #include "except.h"
36 #include "toplev.h"
38 static void construct_virtual_base (tree, tree);
39 static void expand_aggr_init_1 (tree, tree, tree, tree, int);
40 static void expand_default_init (tree, tree, tree, tree, int);
41 static tree build_vec_delete_1 (tree, tree, tree, special_function_kind, int);
42 static void perform_member_init (tree, tree);
43 static tree build_builtin_delete_call (tree);
44 static int member_init_ok_or_else (tree, tree, tree);
45 static void expand_virtual_init (tree, tree);
46 static tree sort_mem_initializers (tree, tree);
47 static tree initializing_context (tree);
48 static void expand_cleanup_for_base (tree, tree);
49 static tree get_temp_regvar (tree, tree);
50 static tree dfs_initialize_vtbl_ptrs (tree, void *);
51 static tree build_default_init (tree, tree);
52 static tree build_new_1 (tree);
53 static tree get_cookie_size (tree);
54 static tree build_dtor_call (tree, special_function_kind, int);
55 static tree build_field_list (tree, tree, int *);
56 static tree build_vtbl_address (tree);
58 /* We are about to generate some complex initialization code.
59 Conceptually, it is all a single expression. However, we may want
60 to include conditionals, loops, and other such statement-level
61 constructs. Therefore, we build the initialization code inside a
62 statement-expression. This function starts such an expression.
63 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
64 pass them back to finish_init_stmts when the expression is
65 complete. */
67 void
68 begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
70 if (building_stmt_tree ())
71 *stmt_expr_p = begin_stmt_expr ();
72 else
73 *stmt_expr_p = begin_global_stmt_expr ();
75 if (building_stmt_tree ())
76 *compound_stmt_p = begin_compound_stmt (/*has_no_scope=*/1);
79 /* Finish out the statement-expression begun by the previous call to
80 begin_init_stmts. Returns the statement-expression itself. */
82 tree
83 finish_init_stmts (tree stmt_expr, tree compound_stmt)
85 if (building_stmt_tree ())
86 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
88 if (building_stmt_tree ())
90 stmt_expr = finish_stmt_expr (stmt_expr);
91 STMT_EXPR_NO_SCOPE (stmt_expr) = true;
93 else
94 stmt_expr = finish_global_stmt_expr (stmt_expr);
96 /* To avoid spurious warnings about unused values, we set
97 TREE_USED. */
98 if (stmt_expr)
99 TREE_USED (stmt_expr) = 1;
101 return stmt_expr;
104 /* Constructors */
106 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
107 which we want to initialize the vtable pointer for, DATA is
108 TREE_LIST whose TREE_VALUE is the this ptr expression. */
110 static tree
111 dfs_initialize_vtbl_ptrs (tree binfo, void *data)
113 if ((!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
114 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
116 tree base_ptr = TREE_VALUE ((tree) data);
118 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1);
120 expand_virtual_init (binfo, base_ptr);
123 BINFO_MARKED (binfo) = 1;
125 return NULL_TREE;
128 /* Initialize all the vtable pointers in the object pointed to by
129 ADDR. */
131 void
132 initialize_vtbl_ptrs (tree addr)
134 tree list;
135 tree type;
137 type = TREE_TYPE (TREE_TYPE (addr));
138 list = build_tree_list (type, addr);
140 /* Walk through the hierarchy, initializing the vptr in each base
141 class. We do these in pre-order because we can't find the virtual
142 bases for a class until we've initialized the vtbl for that
143 class. */
144 dfs_walk_real (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs,
145 NULL, unmarkedp, list);
146 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, type);
149 /* Return an expression for the zero-initialization of an object with
150 type T. This expression will either be a constant (in the case
151 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
152 aggregate). In either case, the value can be used as DECL_INITIAL
153 for a decl of the indicated TYPE; it is a valid static initializer.
154 If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS is the
155 number of elements in the array. If STATIC_STORAGE_P is TRUE,
156 initializers are only generated for entities for which
157 zero-initialization does not simply mean filling the storage with
158 zero bytes. */
160 tree
161 build_zero_init (tree type, tree nelts, bool static_storage_p)
163 tree init = NULL_TREE;
165 /* [dcl.init]
167 To zero-initialization storage for an object of type T means:
169 -- if T is a scalar type, the storage is set to the value of zero
170 converted to T.
172 -- if T is a non-union class type, the storage for each nonstatic
173 data member and each base-class subobject is zero-initialized.
175 -- if T is a union type, the storage for its first data member is
176 zero-initialized.
178 -- if T is an array type, the storage for each element is
179 zero-initialized.
181 -- if T is a reference type, no initialization is performed. */
183 my_friendly_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST,
184 20030618);
186 if (type == error_mark_node)
188 else if (static_storage_p && zero_init_p (type))
189 /* In order to save space, we do not explicitly build initializers
190 for items that do not need them. GCC's semantics are that
191 items with static storage duration that are not otherwise
192 initialized are initialized to zero. */
194 else if (SCALAR_TYPE_P (type))
195 init = convert (type, integer_zero_node);
196 else if (CLASS_TYPE_P (type))
198 tree field;
199 tree inits;
201 /* Build a constructor to contain the initializations. */
202 init = build_constructor (type, NULL_TREE);
203 /* Iterate over the fields, building initializations. */
204 inits = NULL_TREE;
205 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
207 if (TREE_CODE (field) != FIELD_DECL)
208 continue;
210 /* Note that for class types there will be FIELD_DECLs
211 corresponding to base classes as well. Thus, iterating
212 over TYPE_FIELDs will result in correct initialization of
213 all of the subobjects. */
214 if (static_storage_p && !zero_init_p (TREE_TYPE (field)))
215 inits = tree_cons (field,
216 build_zero_init (TREE_TYPE (field),
217 /*nelts=*/NULL_TREE,
218 static_storage_p),
219 inits);
221 /* For unions, only the first field is initialized. */
222 if (TREE_CODE (type) == UNION_TYPE)
223 break;
225 CONSTRUCTOR_ELTS (init) = nreverse (inits);
227 else if (TREE_CODE (type) == ARRAY_TYPE)
229 tree index;
230 tree max_index;
231 tree inits;
233 /* Build a constructor to contain the initializations. */
234 init = build_constructor (type, NULL_TREE);
235 /* Iterate over the array elements, building initializations. */
236 inits = NULL_TREE;
237 max_index = nelts ? nelts : array_type_nelts (type);
238 my_friendly_assert (TREE_CODE (max_index) == INTEGER_CST, 20030618);
240 for (index = size_zero_node;
241 !tree_int_cst_lt (max_index, index);
242 index = size_binop (PLUS_EXPR, index, size_one_node))
243 inits = tree_cons (index,
244 build_zero_init (TREE_TYPE (type),
245 /*nelts=*/NULL_TREE,
246 static_storage_p),
247 inits);
248 CONSTRUCTOR_ELTS (init) = nreverse (inits);
250 else if (TREE_CODE (type) == REFERENCE_TYPE)
252 else
253 abort ();
255 /* In all cases, the initializer is a constant. */
256 if (init)
257 TREE_CONSTANT (init) = 1;
259 return init;
262 /* Build an expression for the default-initialization of an object of
263 the indicated TYPE. If NELTS is non-NULL, and TYPE is an
264 ARRAY_TYPE, NELTS is the number of elements in the array. If
265 initialization of TYPE requires calling constructors, this function
266 returns NULL_TREE; the caller is responsible for arranging for the
267 constructors to be called. */
269 static tree
270 build_default_init (tree type, tree nelts)
272 /* [dcl.init]:
274 To default-initialize an object of type T means:
276 --if T is a non-POD class type (clause _class_), the default construc-
277 tor for T is called (and the initialization is ill-formed if T has
278 no accessible default constructor);
280 --if T is an array type, each element is default-initialized;
282 --otherwise, the storage for the object is zero-initialized.
284 A program that calls for default-initialization of an entity of refer-
285 ence type is ill-formed. */
287 /* If TYPE_NEEDS_CONSTRUCTING is true, the caller is responsible for
288 performing the initialization. This is confusing in that some
289 non-PODs do not have TYPE_NEEDS_CONSTRUCTING set. (For example,
290 a class with a pointer-to-data member as a non-static data member
291 does not have TYPE_NEEDS_CONSTRUCTING set.) Therefore, we end up
292 passing non-PODs to build_zero_init below, which is contrary to
293 the semantics quoted above from [dcl.init].
295 It happens, however, that the behavior of the constructor the
296 standard says we should have generated would be precisely the
297 same as that obtained by calling build_zero_init below, so things
298 work out OK. */
299 if (TYPE_NEEDS_CONSTRUCTING (type)
300 || (nelts && TREE_CODE (nelts) != INTEGER_CST))
301 return NULL_TREE;
303 /* At this point, TYPE is either a POD class type, an array of POD
304 classes, or something even more inoccuous. */
305 return build_zero_init (type, nelts, /*static_storage_p=*/false);
308 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
309 arguments. If TREE_LIST is void_type_node, an empty initializer
310 list was given; if NULL_TREE no initializer was given. */
312 static void
313 perform_member_init (tree member, tree init)
315 tree decl;
316 tree type = TREE_TYPE (member);
317 bool explicit;
319 explicit = (init != NULL_TREE);
321 /* Effective C++ rule 12 requires that all data members be
322 initialized. */
323 if (warn_ecpp && !explicit && TREE_CODE (type) != ARRAY_TYPE)
324 warning ("`%D' should be initialized in the member initialization "
325 "list",
326 member);
328 if (init == void_type_node)
329 init = NULL_TREE;
331 /* Get an lvalue for the data member. */
332 decl = build_class_member_access_expr (current_class_ref, member,
333 /*access_path=*/NULL_TREE,
334 /*preserve_reference=*/true);
335 if (decl == error_mark_node)
336 return;
338 /* Deal with this here, as we will get confused if we try to call the
339 assignment op for an anonymous union. This can happen in a
340 synthesized copy constructor. */
341 if (ANON_AGGR_TYPE_P (type))
343 if (init)
345 init = build (INIT_EXPR, type, decl, TREE_VALUE (init));
346 finish_expr_stmt (init);
349 else if (TYPE_NEEDS_CONSTRUCTING (type)
350 || (init && TYPE_HAS_CONSTRUCTOR (type)))
352 if (explicit
353 && TREE_CODE (type) == ARRAY_TYPE
354 && init != NULL_TREE
355 && TREE_CHAIN (init) == NULL_TREE
356 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
358 /* Initialization of one array from another. */
359 finish_expr_stmt (build_vec_init (decl, NULL_TREE, TREE_VALUE (init),
360 /* from_array=*/1));
362 else
363 finish_expr_stmt (build_aggr_init (decl, init, 0));
365 else
367 if (init == NULL_TREE)
369 if (explicit)
371 init = build_default_init (type, /*nelts=*/NULL_TREE);
372 if (TREE_CODE (type) == REFERENCE_TYPE)
373 warning
374 ("default-initialization of `%#D', which has reference type",
375 member);
377 /* member traversal: note it leaves init NULL */
378 else if (TREE_CODE (type) == REFERENCE_TYPE)
379 pedwarn ("uninitialized reference member `%D'", member);
381 else if (TREE_CODE (init) == TREE_LIST)
383 /* There was an explicit member initialization. Do some
384 work in that case. */
385 if (TREE_CHAIN (init))
387 warning ("initializer list treated as compound expression");
388 init = build_compound_expr (init);
390 else
391 init = TREE_VALUE (init);
394 if (init)
395 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
398 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
400 tree expr;
402 expr = build_class_member_access_expr (current_class_ref, member,
403 /*access_path=*/NULL_TREE,
404 /*preserve_reference=*/false);
405 expr = build_delete (type, expr, sfk_complete_destructor,
406 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
408 if (expr != error_mark_node)
409 finish_eh_cleanup (expr);
413 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
414 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
416 static tree
417 build_field_list (tree t, tree list, int *uses_unions_p)
419 tree fields;
421 *uses_unions_p = 0;
423 /* Note whether or not T is a union. */
424 if (TREE_CODE (t) == UNION_TYPE)
425 *uses_unions_p = 1;
427 for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
429 /* Skip CONST_DECLs for enumeration constants and so forth. */
430 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
431 continue;
433 /* Keep track of whether or not any fields are unions. */
434 if (TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
435 *uses_unions_p = 1;
437 /* For an anonymous struct or union, we must recursively
438 consider the fields of the anonymous type. They can be
439 directly initialized from the constructor. */
440 if (ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
442 /* Add this field itself. Synthesized copy constructors
443 initialize the entire aggregate. */
444 list = tree_cons (fields, NULL_TREE, list);
445 /* And now add the fields in the anonymous aggregate. */
446 list = build_field_list (TREE_TYPE (fields), list,
447 uses_unions_p);
449 /* Add this field. */
450 else if (DECL_NAME (fields))
451 list = tree_cons (fields, NULL_TREE, list);
454 return list;
457 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
458 a FIELD_DECL or BINFO in T that needs initialization. The
459 TREE_VALUE gives the initializer, or list of initializer arguments.
461 Return a TREE_LIST containing all of the initializations required
462 for T, in the order in which they should be performed. The output
463 list has the same format as the input. */
465 static tree
466 sort_mem_initializers (tree t, tree mem_inits)
468 tree init;
469 tree base;
470 tree sorted_inits;
471 tree next_subobject;
472 int i;
473 int uses_unions_p;
475 /* Build up a list of initializations. The TREE_PURPOSE of entry
476 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
477 TREE_VALUE will be the constructor arguments, or NULL if no
478 explicit initialization was provided. */
479 sorted_inits = NULL_TREE;
480 /* Process the virtual bases. */
481 for (base = CLASSTYPE_VBASECLASSES (t); base; base = TREE_CHAIN (base))
482 sorted_inits = tree_cons (TREE_VALUE (base), NULL_TREE, sorted_inits);
483 /* Process the direct bases. */
484 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
486 base = BINFO_BASETYPE (TYPE_BINFO (t), i);
487 if (!TREE_VIA_VIRTUAL (base))
488 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
490 /* Process the non-static data members. */
491 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
492 /* Reverse the entire list of initializations, so that they are in
493 the order that they will actually be performed. */
494 sorted_inits = nreverse (sorted_inits);
496 /* If the user presented the initializers in an order different from
497 that in which they will actually occur, we issue a warning. Keep
498 track of the next subobject which can be explicitly initialized
499 without issuing a warning. */
500 next_subobject = sorted_inits;
502 /* Go through the explicit initializers, filling in TREE_PURPOSE in
503 the SORTED_INITS. */
504 for (init = mem_inits; init; init = TREE_CHAIN (init))
506 tree subobject;
507 tree subobject_init;
509 subobject = TREE_PURPOSE (init);
511 /* If the explicit initializers are in sorted order, then
512 SUBOBJECT will be NEXT_SUBOBJECT, or something following
513 it. */
514 for (subobject_init = next_subobject;
515 subobject_init;
516 subobject_init = TREE_CHAIN (subobject_init))
517 if (TREE_PURPOSE (subobject_init) == subobject)
518 break;
520 /* Issue a warning if the explicit initializer order does not
521 match that which will actually occur. */
522 if (warn_reorder && !subobject_init)
524 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
525 cp_warning_at ("`%D' will be initialized after",
526 TREE_PURPOSE (next_subobject));
527 else
528 warning ("base `%T' will be initialized after",
529 TREE_PURPOSE (next_subobject));
530 if (TREE_CODE (subobject) == FIELD_DECL)
531 cp_warning_at (" `%#D'", subobject);
532 else
533 warning (" base `%T'", subobject);
536 /* Look again, from the beginning of the list. */
537 if (!subobject_init)
539 subobject_init = sorted_inits;
540 while (TREE_PURPOSE (subobject_init) != subobject)
541 subobject_init = TREE_CHAIN (subobject_init);
544 /* It is invalid to initialize the same subobject more than
545 once. */
546 if (TREE_VALUE (subobject_init))
548 if (TREE_CODE (subobject) == FIELD_DECL)
549 error ("multiple initializations given for `%D'", subobject);
550 else
551 error ("multiple initializations given for base `%T'",
552 subobject);
555 /* Record the initialization. */
556 TREE_VALUE (subobject_init) = TREE_VALUE (init);
557 next_subobject = subobject_init;
560 /* [class.base.init]
562 If a ctor-initializer specifies more than one mem-initializer for
563 multiple members of the same union (including members of
564 anonymous unions), the ctor-initializer is ill-formed. */
565 if (uses_unions_p)
567 tree last_field = NULL_TREE;
568 for (init = sorted_inits; init; init = TREE_CHAIN (init))
570 tree field;
571 tree field_type;
572 int done;
574 /* Skip uninitialized members and base classes. */
575 if (!TREE_VALUE (init)
576 || TREE_CODE (TREE_PURPOSE (init)) != FIELD_DECL)
577 continue;
578 /* See if this field is a member of a union, or a member of a
579 structure contained in a union, etc. */
580 field = TREE_PURPOSE (init);
581 for (field_type = DECL_CONTEXT (field);
582 !same_type_p (field_type, t);
583 field_type = TYPE_CONTEXT (field_type))
584 if (TREE_CODE (field_type) == UNION_TYPE)
585 break;
586 /* If this field is not a member of a union, skip it. */
587 if (TREE_CODE (field_type) != UNION_TYPE)
588 continue;
590 /* It's only an error if we have two initializers for the same
591 union type. */
592 if (!last_field)
594 last_field = field;
595 continue;
598 /* See if LAST_FIELD and the field initialized by INIT are
599 members of the same union. If so, there's a problem,
600 unless they're actually members of the same structure
601 which is itself a member of a union. For example, given:
603 union { struct { int i; int j; }; };
605 initializing both `i' and `j' makes sense. */
606 field_type = DECL_CONTEXT (field);
607 done = 0;
610 tree last_field_type;
612 last_field_type = DECL_CONTEXT (last_field);
613 while (1)
615 if (same_type_p (last_field_type, field_type))
617 if (TREE_CODE (field_type) == UNION_TYPE)
618 error ("initializations for multiple members of `%T'",
619 last_field_type);
620 done = 1;
621 break;
624 if (same_type_p (last_field_type, t))
625 break;
627 last_field_type = TYPE_CONTEXT (last_field_type);
630 /* If we've reached the outermost class, then we're
631 done. */
632 if (same_type_p (field_type, t))
633 break;
635 field_type = TYPE_CONTEXT (field_type);
637 while (!done);
639 last_field = field;
643 return sorted_inits;
646 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
647 is a TREE_LIST giving the explicit mem-initializer-list for the
648 constructor. The TREE_PURPOSE of each entry is a subobject (a
649 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
650 is a TREE_LIST giving the arguments to the constructor or
651 void_type_node for an empty list of arguments. */
653 void
654 emit_mem_initializers (tree mem_inits)
656 /* Sort the mem-initializers into the order in which the
657 initializations should be performed. */
658 mem_inits = sort_mem_initializers (current_class_type, mem_inits);
660 in_base_initializer = 1;
662 /* Initialize base classes. */
663 while (mem_inits
664 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL)
666 tree subobject = TREE_PURPOSE (mem_inits);
667 tree arguments = TREE_VALUE (mem_inits);
669 /* If these initializations are taking place in a copy
670 constructor, the base class should probably be explicitly
671 initialized. */
672 if (extra_warnings && !arguments
673 && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
674 && TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (subobject)))
675 warning ("base class `%#T' should be explicitly initialized in the "
676 "copy constructor",
677 BINFO_TYPE (subobject));
679 /* If an explicit -- but empty -- initializer list was present,
680 treat it just like default initialization at this point. */
681 if (arguments == void_type_node)
682 arguments = NULL_TREE;
684 /* Initialize the base. */
685 if (TREE_VIA_VIRTUAL (subobject))
686 construct_virtual_base (subobject, arguments);
687 else
689 tree base_addr;
691 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
692 subobject, 1);
693 expand_aggr_init_1 (subobject, NULL_TREE,
694 build_indirect_ref (base_addr, NULL),
695 arguments,
696 LOOKUP_NORMAL);
697 expand_cleanup_for_base (subobject, NULL_TREE);
700 mem_inits = TREE_CHAIN (mem_inits);
702 in_base_initializer = 0;
704 /* Initialize the vptrs. */
705 initialize_vtbl_ptrs (current_class_ptr);
707 /* Initialize the data members. */
708 while (mem_inits)
710 perform_member_init (TREE_PURPOSE (mem_inits),
711 TREE_VALUE (mem_inits));
712 mem_inits = TREE_CHAIN (mem_inits);
716 /* Returns the address of the vtable (i.e., the value that should be
717 assigned to the vptr) for BINFO. */
719 static tree
720 build_vtbl_address (tree binfo)
722 tree binfo_for = binfo;
723 tree vtbl;
725 if (BINFO_VPTR_INDEX (binfo) && TREE_VIA_VIRTUAL (binfo)
726 && BINFO_PRIMARY_P (binfo))
727 /* If this is a virtual primary base, then the vtable we want to store
728 is that for the base this is being used as the primary base of. We
729 can't simply skip the initialization, because we may be expanding the
730 inits of a subobject constructor where the virtual base layout
731 can be different. */
732 while (BINFO_PRIMARY_BASE_OF (binfo_for))
733 binfo_for = BINFO_PRIMARY_BASE_OF (binfo_for);
735 /* Figure out what vtable BINFO's vtable is based on, and mark it as
736 used. */
737 vtbl = get_vtbl_decl_for_binfo (binfo_for);
738 assemble_external (vtbl);
739 TREE_USED (vtbl) = 1;
741 /* Now compute the address to use when initializing the vptr. */
742 vtbl = BINFO_VTABLE (binfo_for);
743 if (TREE_CODE (vtbl) == VAR_DECL)
745 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
746 TREE_CONSTANT (vtbl) = 1;
749 return vtbl;
752 /* This code sets up the virtual function tables appropriate for
753 the pointer DECL. It is a one-ply initialization.
755 BINFO is the exact type that DECL is supposed to be. In
756 multiple inheritance, this might mean "C's A" if C : A, B. */
758 static void
759 expand_virtual_init (tree binfo, tree decl)
761 tree vtbl, vtbl_ptr;
762 tree vtt_index;
764 /* Compute the initializer for vptr. */
765 vtbl = build_vtbl_address (binfo);
767 /* We may get this vptr from a VTT, if this is a subobject
768 constructor or subobject destructor. */
769 vtt_index = BINFO_VPTR_INDEX (binfo);
770 if (vtt_index)
772 tree vtbl2;
773 tree vtt_parm;
775 /* Compute the value to use, when there's a VTT. */
776 vtt_parm = current_vtt_parm;
777 vtbl2 = build (PLUS_EXPR,
778 TREE_TYPE (vtt_parm),
779 vtt_parm,
780 vtt_index);
781 vtbl2 = build1 (INDIRECT_REF, TREE_TYPE (vtbl), vtbl2);
783 /* The actual initializer is the VTT value only in the subobject
784 constructor. In maybe_clone_body we'll substitute NULL for
785 the vtt_parm in the case of the non-subobject constructor. */
786 vtbl = build (COND_EXPR,
787 TREE_TYPE (vtbl),
788 build (EQ_EXPR, boolean_type_node,
789 current_in_charge_parm, integer_zero_node),
790 vtbl2,
791 vtbl);
794 /* Compute the location of the vtpr. */
795 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL),
796 TREE_TYPE (binfo));
797 my_friendly_assert (vtbl_ptr != error_mark_node, 20010730);
799 /* Assign the vtable to the vptr. */
800 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
801 finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
804 /* If an exception is thrown in a constructor, those base classes already
805 constructed must be destroyed. This function creates the cleanup
806 for BINFO, which has just been constructed. If FLAG is non-NULL,
807 it is a DECL which is nonzero when this base needs to be
808 destroyed. */
810 static void
811 expand_cleanup_for_base (tree binfo, tree flag)
813 tree expr;
815 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
816 return;
818 /* Call the destructor. */
819 expr = build_special_member_call (current_class_ref,
820 base_dtor_identifier,
821 NULL_TREE,
822 binfo,
823 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL);
824 if (flag)
825 expr = fold (build (COND_EXPR, void_type_node,
826 c_common_truthvalue_conversion (flag),
827 expr, integer_zero_node));
829 finish_eh_cleanup (expr);
832 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
833 constructor. */
835 static void
836 construct_virtual_base (tree vbase, tree arguments)
838 tree inner_if_stmt;
839 tree compound_stmt;
840 tree exp;
841 tree flag;
843 /* If there are virtual base classes with destructors, we need to
844 emit cleanups to destroy them if an exception is thrown during
845 the construction process. These exception regions (i.e., the
846 period during which the cleanups must occur) begin from the time
847 the construction is complete to the end of the function. If we
848 create a conditional block in which to initialize the
849 base-classes, then the cleanup region for the virtual base begins
850 inside a block, and ends outside of that block. This situation
851 confuses the sjlj exception-handling code. Therefore, we do not
852 create a single conditional block, but one for each
853 initialization. (That way the cleanup regions always begin
854 in the outer block.) We trust the back-end to figure out
855 that the FLAG will not change across initializations, and
856 avoid doing multiple tests. */
857 flag = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
858 inner_if_stmt = begin_if_stmt ();
859 finish_if_stmt_cond (flag, inner_if_stmt);
860 compound_stmt = begin_compound_stmt (/*has_no_scope=*/1);
862 /* Compute the location of the virtual base. If we're
863 constructing virtual bases, then we must be the most derived
864 class. Therefore, we don't have to look up the virtual base;
865 we already know where it is. */
866 exp = build (PLUS_EXPR,
867 TREE_TYPE (current_class_ptr),
868 current_class_ptr,
869 fold (build1 (NOP_EXPR, TREE_TYPE (current_class_ptr),
870 BINFO_OFFSET (vbase))));
871 exp = build1 (NOP_EXPR,
872 build_pointer_type (BINFO_TYPE (vbase)),
873 exp);
874 exp = build1 (INDIRECT_REF, BINFO_TYPE (vbase), exp);
876 expand_aggr_init_1 (vbase, current_class_ref, exp,
877 arguments, LOOKUP_COMPLAIN);
878 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
879 finish_then_clause (inner_if_stmt);
880 finish_if_stmt ();
882 expand_cleanup_for_base (vbase, flag);
885 /* Find the context in which this FIELD can be initialized. */
887 static tree
888 initializing_context (tree field)
890 tree t = DECL_CONTEXT (field);
892 /* Anonymous union members can be initialized in the first enclosing
893 non-anonymous union context. */
894 while (t && ANON_AGGR_TYPE_P (t))
895 t = TYPE_CONTEXT (t);
896 return t;
899 /* Function to give error message if member initialization specification
900 is erroneous. FIELD is the member we decided to initialize.
901 TYPE is the type for which the initialization is being performed.
902 FIELD must be a member of TYPE.
904 MEMBER_NAME is the name of the member. */
906 static int
907 member_init_ok_or_else (tree field, tree type, tree member_name)
909 if (field == error_mark_node)
910 return 0;
911 if (!field)
913 error ("class `%T' does not have any field named `%D'", type,
914 member_name);
915 return 0;
917 if (TREE_CODE (field) == VAR_DECL)
919 error ("`%#D' is a static data member; it can only be "
920 "initialized at its definition",
921 field);
922 return 0;
924 if (TREE_CODE (field) != FIELD_DECL)
926 error ("`%#D' is not a non-static data member of `%T'",
927 field, type);
928 return 0;
930 if (initializing_context (field) != type)
932 error ("class `%T' does not have any field named `%D'", type,
933 member_name);
934 return 0;
937 return 1;
940 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
941 is a _TYPE node or TYPE_DECL which names a base for that type.
942 Check the validity of NAME, and return either the base _TYPE, base
943 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
944 NULL_TREE and issue a diagnostic.
946 An old style unnamed direct single base construction is permitted,
947 where NAME is NULL. */
949 tree
950 expand_member_init (tree name)
952 tree basetype;
953 tree field;
955 if (!current_class_ref)
956 return NULL_TREE;
958 if (!name)
960 /* This is an obsolete unnamed base class initializer. The
961 parser will already have warned about its use. */
962 switch (CLASSTYPE_N_BASECLASSES (current_class_type))
964 case 0:
965 error ("unnamed initializer for `%T', which has no base classes",
966 current_class_type);
967 return NULL_TREE;
968 case 1:
969 basetype = TYPE_BINFO_BASETYPE (current_class_type, 0);
970 break;
971 default:
972 error ("unnamed initializer for `%T', which uses multiple inheritance",
973 current_class_type);
974 return NULL_TREE;
977 else if (TYPE_P (name))
979 basetype = TYPE_MAIN_VARIANT (name);
980 name = TYPE_NAME (name);
982 else if (TREE_CODE (name) == TYPE_DECL)
983 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
984 else
985 basetype = NULL_TREE;
987 if (basetype)
989 tree binfo;
991 if (current_template_parms)
992 return basetype;
994 binfo = lookup_base (current_class_type, basetype,
995 ba_ignore, NULL);
996 if (!binfo || (!TREE_VIA_VIRTUAL (binfo)
997 && (BINFO_INHERITANCE_CHAIN (binfo)
998 != TYPE_BINFO (current_class_type))))
1000 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1001 error ("type `%D' is not a direct or virtual base of `%T'",
1002 name, current_class_type);
1003 else
1004 error ("type `%D' is not a direct base of `%T'",
1005 name, current_class_type);
1006 return NULL_TREE;
1008 return binfo;
1010 else
1012 if (TREE_CODE (name) == IDENTIFIER_NODE)
1013 field = lookup_field (current_class_type, name, 1, false);
1014 else
1015 field = name;
1017 if (member_init_ok_or_else (field, current_class_type, name))
1018 return field;
1021 return NULL_TREE;
1024 /* This is like `expand_member_init', only it stores one aggregate
1025 value into another.
1027 INIT comes in two flavors: it is either a value which
1028 is to be stored in EXP, or it is a parameter list
1029 to go to a constructor, which will operate on EXP.
1030 If INIT is not a parameter list for a constructor, then set
1031 LOOKUP_ONLYCONVERTING.
1032 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1033 the initializer, if FLAGS is 0, then it is the (init) form.
1034 If `init' is a CONSTRUCTOR, then we emit a warning message,
1035 explaining that such initializations are invalid.
1037 If INIT resolves to a CALL_EXPR which happens to return
1038 something of the type we are looking for, then we know
1039 that we can safely use that call to perform the
1040 initialization.
1042 The virtual function table pointer cannot be set up here, because
1043 we do not really know its type.
1045 This never calls operator=().
1047 When initializing, nothing is CONST.
1049 A default copy constructor may have to be used to perform the
1050 initialization.
1052 A constructor or a conversion operator may have to be used to
1053 perform the initialization, but not both, as it would be ambiguous. */
1055 tree
1056 build_aggr_init (tree exp, tree init, int flags)
1058 tree stmt_expr;
1059 tree compound_stmt;
1060 int destroy_temps;
1061 tree type = TREE_TYPE (exp);
1062 int was_const = TREE_READONLY (exp);
1063 int was_volatile = TREE_THIS_VOLATILE (exp);
1065 if (init == error_mark_node)
1066 return error_mark_node;
1068 TREE_READONLY (exp) = 0;
1069 TREE_THIS_VOLATILE (exp) = 0;
1071 if (init && TREE_CODE (init) != TREE_LIST)
1072 flags |= LOOKUP_ONLYCONVERTING;
1074 if (TREE_CODE (type) == ARRAY_TYPE)
1076 /* Must arrange to initialize each element of EXP
1077 from elements of INIT. */
1078 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1080 if (init && !itype)
1082 /* Handle bad initializers like:
1083 class COMPLEX {
1084 public:
1085 double re, im;
1086 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1087 ~COMPLEX() {};
1090 int main(int argc, char **argv) {
1091 COMPLEX zees(1.0, 0.0)[10];
1094 error ("bad array initializer");
1095 return error_mark_node;
1097 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
1098 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1099 if (itype && cp_type_quals (itype) != TYPE_UNQUALIFIED)
1100 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1101 stmt_expr = build_vec_init (exp, NULL_TREE, init,
1102 init && same_type_p (TREE_TYPE (init),
1103 TREE_TYPE (exp)));
1104 TREE_READONLY (exp) = was_const;
1105 TREE_THIS_VOLATILE (exp) = was_volatile;
1106 TREE_TYPE (exp) = type;
1107 if (init)
1108 TREE_TYPE (init) = itype;
1109 return stmt_expr;
1112 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1113 /* just know that we've seen something for this node */
1114 TREE_USED (exp) = 1;
1116 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1117 begin_init_stmts (&stmt_expr, &compound_stmt);
1118 destroy_temps = stmts_are_full_exprs_p ();
1119 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1120 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1121 init, LOOKUP_NORMAL|flags);
1122 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
1123 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1124 TREE_TYPE (exp) = type;
1125 TREE_READONLY (exp) = was_const;
1126 TREE_THIS_VOLATILE (exp) = was_volatile;
1128 return stmt_expr;
1131 /* Like build_aggr_init, but not just for aggregates. */
1133 tree
1134 build_init (tree decl, tree init, int flags)
1136 tree expr;
1138 if (IS_AGGR_TYPE (TREE_TYPE (decl))
1139 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1140 expr = build_aggr_init (decl, init, flags);
1141 else
1142 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
1144 return expr;
1147 static void
1148 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags)
1150 tree type = TREE_TYPE (exp);
1151 tree ctor_name;
1153 /* It fails because there may not be a constructor which takes
1154 its own type as the first (or only parameter), but which does
1155 take other types via a conversion. So, if the thing initializing
1156 the expression is a unit element of type X, first try X(X&),
1157 followed by initialization by X. If neither of these work
1158 out, then look hard. */
1159 tree rval;
1160 tree parms;
1162 if (init && TREE_CODE (init) != TREE_LIST
1163 && (flags & LOOKUP_ONLYCONVERTING))
1165 /* Base subobjects should only get direct-initialization. */
1166 if (true_exp != exp)
1167 abort ();
1169 if (flags & DIRECT_BIND)
1170 /* Do nothing. We hit this in two cases: Reference initialization,
1171 where we aren't initializing a real variable, so we don't want
1172 to run a new constructor; and catching an exception, where we
1173 have already built up the constructor call so we could wrap it
1174 in an exception region. */;
1175 else if (TREE_CODE (init) == CONSTRUCTOR
1176 && TREE_HAS_CONSTRUCTOR (init))
1178 /* A brace-enclosed initializer for an aggregate. */
1179 my_friendly_assert (CP_AGGREGATE_TYPE_P (type), 20021016);
1180 init = digest_init (type, init, (tree *)NULL);
1182 else
1183 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1185 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
1186 /* We need to protect the initialization of a catch parm with a
1187 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
1188 around the TARGET_EXPR for the copy constructor. See
1189 initialize_handler_parm. */
1191 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp,
1192 TREE_OPERAND (init, 0));
1193 TREE_TYPE (init) = void_type_node;
1195 else
1196 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init);
1197 TREE_SIDE_EFFECTS (init) = 1;
1198 finish_expr_stmt (init);
1199 return;
1202 if (init == NULL_TREE
1203 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1205 parms = init;
1206 if (parms)
1207 init = TREE_VALUE (parms);
1209 else
1210 parms = build_tree_list (NULL_TREE, init);
1212 if (true_exp == exp)
1213 ctor_name = complete_ctor_identifier;
1214 else
1215 ctor_name = base_ctor_identifier;
1217 rval = build_special_member_call (exp, ctor_name, parms, binfo, flags);
1218 if (TREE_SIDE_EFFECTS (rval))
1220 if (building_stmt_tree ())
1221 finish_expr_stmt (rval);
1222 else
1223 genrtl_expr_stmt (rval);
1227 /* This function is responsible for initializing EXP with INIT
1228 (if any).
1230 BINFO is the binfo of the type for who we are performing the
1231 initialization. For example, if W is a virtual base class of A and B,
1232 and C : A, B.
1233 If we are initializing B, then W must contain B's W vtable, whereas
1234 were we initializing C, W must contain C's W vtable.
1236 TRUE_EXP is nonzero if it is the true expression being initialized.
1237 In this case, it may be EXP, or may just contain EXP. The reason we
1238 need this is because if EXP is a base element of TRUE_EXP, we
1239 don't necessarily know by looking at EXP where its virtual
1240 baseclass fields should really be pointing. But we do know
1241 from TRUE_EXP. In constructors, we don't know anything about
1242 the value being initialized.
1244 FLAGS is just passes to `build_method_call'. See that function for
1245 its description. */
1247 static void
1248 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags)
1250 tree type = TREE_TYPE (exp);
1252 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1253 my_friendly_assert (building_stmt_tree (), 20021010);
1255 /* Use a function returning the desired type to initialize EXP for us.
1256 If the function is a constructor, and its first argument is
1257 NULL_TREE, know that it was meant for us--just slide exp on
1258 in and expand the constructor. Constructors now come
1259 as TARGET_EXPRs. */
1261 if (init && TREE_CODE (exp) == VAR_DECL
1262 && TREE_CODE (init) == CONSTRUCTOR
1263 && TREE_HAS_CONSTRUCTOR (init))
1265 /* If store_init_value returns NULL_TREE, the INIT has been
1266 record in the DECL_INITIAL for EXP. That means there's
1267 nothing more we have to do. */
1268 if (store_init_value (exp, init))
1269 finish_expr_stmt (build (INIT_EXPR, type, exp, init));
1270 return;
1273 /* We know that expand_default_init can handle everything we want
1274 at this point. */
1275 expand_default_init (binfo, true_exp, exp, init, flags);
1278 /* Report an error if TYPE is not a user-defined, aggregate type. If
1279 OR_ELSE is nonzero, give an error message. */
1282 is_aggr_type (tree type, int or_else)
1284 if (type == error_mark_node)
1285 return 0;
1287 if (! IS_AGGR_TYPE (type)
1288 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1289 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
1291 if (or_else)
1292 error ("`%T' is not an aggregate type", type);
1293 return 0;
1295 return 1;
1298 /* Like is_aggr_typedef, but returns typedef if successful. */
1300 tree
1301 get_aggr_from_typedef (tree name, int or_else)
1303 tree type;
1305 if (name == error_mark_node)
1306 return NULL_TREE;
1308 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1309 type = IDENTIFIER_TYPE_VALUE (name);
1310 else
1312 if (or_else)
1313 error ("`%T' fails to be an aggregate typedef", name);
1314 return NULL_TREE;
1317 if (! IS_AGGR_TYPE (type)
1318 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1319 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
1321 if (or_else)
1322 error ("type `%T' is of non-aggregate type", type);
1323 return NULL_TREE;
1325 return type;
1328 tree
1329 get_type_value (tree name)
1331 if (name == error_mark_node)
1332 return NULL_TREE;
1334 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1335 return IDENTIFIER_TYPE_VALUE (name);
1336 else
1337 return NULL_TREE;
1341 /* This code could just as well go in `class.c', but is placed here for
1342 modularity. */
1344 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1345 the appropriate function call. */
1347 tree
1348 build_member_call (tree type, tree name, tree parmlist)
1350 tree t;
1351 tree method_name;
1352 tree fns;
1353 int dtor = 0;
1354 tree basetype_path, decl;
1356 if (TREE_CODE (name) == TEMPLATE_ID_EXPR
1357 && TREE_CODE (type) == NAMESPACE_DECL)
1359 /* 'name' already refers to the decls from the namespace, since we
1360 hit do_identifier for template_ids. */
1361 method_name = TREE_OPERAND (name, 0);
1362 /* FIXME: Since we don't do independent names right yet, the
1363 name might also be a LOOKUP_EXPR. Once we resolve this to a
1364 real decl earlier, this can go. This may happen during
1365 tsubst'ing. */
1366 if (TREE_CODE (method_name) == LOOKUP_EXPR)
1368 method_name = lookup_namespace_name
1369 (type, TREE_OPERAND (method_name, 0));
1370 TREE_OPERAND (name, 0) = method_name;
1372 my_friendly_assert (is_overloaded_fn (method_name), 980519);
1373 return finish_call_expr (name, parmlist, /*disallow_virtual=*/true);
1376 if (DECL_P (name))
1377 name = DECL_NAME (name);
1379 if (TREE_CODE (type) == NAMESPACE_DECL)
1380 return finish_call_expr (lookup_namespace_name (type, name),
1381 parmlist,
1382 /*disallow_virtual=*/true);
1384 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1386 method_name = TREE_OPERAND (name, 0);
1387 if (TREE_CODE (method_name) == COMPONENT_REF)
1388 method_name = TREE_OPERAND (method_name, 1);
1389 if (is_overloaded_fn (method_name))
1390 method_name = DECL_NAME (OVL_CURRENT (method_name));
1391 TREE_OPERAND (name, 0) = method_name;
1393 else
1394 method_name = name;
1396 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1398 method_name = TREE_OPERAND (method_name, 0);
1399 dtor = 1;
1402 /* This shouldn't be here, and build_member_call shouldn't appear in
1403 parse.y! (mrs) */
1404 if (type && TREE_CODE (type) == IDENTIFIER_NODE
1405 && get_aggr_from_typedef (type, 0) == 0)
1407 tree ns = lookup_name (type, 0);
1408 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1409 return finish_call_expr (lookup_namespace_name (ns, name),
1410 parmlist,
1411 /*disallow_virtual=*/true);
1414 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1415 return error_mark_node;
1417 /* An operator we did not like. */
1418 if (name == NULL_TREE)
1419 return error_mark_node;
1421 if (dtor)
1423 error ("cannot call destructor `%T::~%T' without object", type,
1424 method_name);
1425 return error_mark_node;
1428 decl = maybe_dummy_object (type, &basetype_path);
1430 fns = lookup_fnfields (basetype_path, method_name, 0);
1431 if (fns)
1433 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1434 BASELINK_FUNCTIONS (fns) = build_nt (TEMPLATE_ID_EXPR,
1435 BASELINK_FUNCTIONS (fns),
1436 TREE_OPERAND (name, 1));
1437 return build_new_method_call (decl, fns, parmlist,
1438 /*conversion_path=*/NULL_TREE,
1439 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1442 /* Convert 'this' to the specified type to disambiguate conversion
1443 to the function's context. */
1444 if (decl == current_class_ref
1445 /* ??? this is wrong, but if this conversion is invalid we need to
1446 defer it until we know whether we are calling a static or
1447 non-static member function. Be conservative for now. */
1448 && ACCESSIBLY_UNIQUELY_DERIVED_P (type, current_class_type))
1450 basetype_path = NULL_TREE;
1451 decl = build_scoped_ref (decl, type, &basetype_path);
1452 if (decl == error_mark_node)
1453 return error_mark_node;
1456 if (constructor_name_p (method_name, type))
1457 return build_functional_cast (type, parmlist);
1458 if (TREE_CODE (name) == IDENTIFIER_NODE
1459 && ((t = lookup_field (TYPE_BINFO (type), name, 1, false))))
1461 if (t == error_mark_node)
1462 return error_mark_node;
1463 if (TREE_CODE (t) == FIELD_DECL)
1465 if (is_dummy_object (decl))
1467 error ("invalid use of non-static field `%D'", t);
1468 return error_mark_node;
1470 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1472 else if (TREE_CODE (t) == VAR_DECL)
1473 decl = t;
1474 else
1476 error ("invalid use of member `%D'", t);
1477 return error_mark_node;
1479 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl)))
1480 return build_new_op (CALL_EXPR, LOOKUP_NORMAL, decl,
1481 parmlist, NULL_TREE);
1482 return build_function_call (decl, parmlist);
1484 else
1486 error ("no method `%T::%D'", type, name);
1487 return error_mark_node;
1491 /* Build a reference to a member of an aggregate. This is not a
1492 C++ `&', but really something which can have its address taken,
1493 and then act as a pointer to member, for example TYPE :: FIELD
1494 can have its address taken by saying & TYPE :: FIELD.
1496 @@ Prints out lousy diagnostics for operator <typename>
1497 @@ fields.
1499 @@ This function should be rewritten and placed in search.c. */
1501 tree
1502 build_offset_ref (tree type, tree name)
1504 tree decl, t = error_mark_node;
1505 tree member;
1506 tree basebinfo = NULL_TREE;
1507 tree orig_name = name;
1509 /* class templates can come in as TEMPLATE_DECLs here. */
1510 if (TREE_CODE (name) == TEMPLATE_DECL)
1511 return name;
1513 if (processing_template_decl || uses_template_parms (type))
1514 return build_min_nt (SCOPE_REF, type, name);
1516 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1518 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1519 something like `a.template f<int>' or the like. For the most
1520 part, we treat this just like a.f. We do remember, however,
1521 the template-id that was used. */
1522 name = TREE_OPERAND (orig_name, 0);
1524 if (DECL_P (name))
1525 name = DECL_NAME (name);
1526 else
1528 if (TREE_CODE (name) == LOOKUP_EXPR)
1529 /* This can happen during tsubst'ing. */
1530 name = TREE_OPERAND (name, 0);
1531 else
1533 if (TREE_CODE (name) == COMPONENT_REF)
1534 name = TREE_OPERAND (name, 1);
1535 if (TREE_CODE (name) == OVERLOAD)
1536 name = DECL_NAME (OVL_CURRENT (name));
1540 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1543 if (type == NULL_TREE)
1544 return error_mark_node;
1546 /* Handle namespace names fully here. */
1547 if (TREE_CODE (type) == NAMESPACE_DECL)
1549 t = lookup_namespace_name (type, name);
1550 if (t == error_mark_node)
1551 return t;
1552 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1553 /* Reconstruct the TEMPLATE_ID_EXPR. */
1554 t = build (TEMPLATE_ID_EXPR, TREE_TYPE (t),
1555 t, TREE_OPERAND (orig_name, 1));
1556 if (! type_unknown_p (t))
1558 mark_used (t);
1559 t = convert_from_reference (t);
1561 return t;
1564 if (! is_aggr_type (type, 1))
1565 return error_mark_node;
1567 if (TREE_CODE (name) == BIT_NOT_EXPR)
1569 if (! check_dtor_name (type, name))
1570 error ("qualified type `%T' does not match destructor name `~%T'",
1571 type, TREE_OPERAND (name, 0));
1572 name = dtor_identifier;
1575 if (!COMPLETE_TYPE_P (complete_type (type))
1576 && !TYPE_BEING_DEFINED (type))
1578 error ("incomplete type `%T' does not have member `%D'", type,
1579 name);
1580 return error_mark_node;
1583 decl = maybe_dummy_object (type, &basebinfo);
1585 if (BASELINK_P (name) || DECL_P (name))
1586 member = name;
1587 else
1589 member = lookup_member (basebinfo, name, 1, 0);
1591 if (member == error_mark_node)
1592 return error_mark_node;
1595 /* A lot of this logic is now handled in lookup_member. */
1596 if (member && BASELINK_P (member))
1598 /* Go from the TREE_BASELINK to the member function info. */
1599 tree fnfields = member;
1600 t = BASELINK_FUNCTIONS (fnfields);
1602 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1604 /* The FNFIELDS are going to contain functions that aren't
1605 necessarily templates, and templates that don't
1606 necessarily match the explicit template parameters. We
1607 save all the functions, and the explicit parameters, and
1608 then figure out exactly what to instantiate with what
1609 arguments in instantiate_type. */
1611 if (TREE_CODE (t) != OVERLOAD)
1612 /* The code in instantiate_type which will process this
1613 expects to encounter OVERLOADs, not raw functions. */
1614 t = ovl_cons (t, NULL_TREE);
1616 t = build (TEMPLATE_ID_EXPR, TREE_TYPE (t), t,
1617 TREE_OPERAND (orig_name, 1));
1618 t = build (OFFSET_REF, unknown_type_node, decl, t);
1620 PTRMEM_OK_P (t) = 1;
1622 return t;
1625 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
1627 /* Get rid of a potential OVERLOAD around it */
1628 t = OVL_CURRENT (t);
1630 /* unique functions are handled easily. */
1631 perform_or_defer_access_check (basebinfo, t);
1632 mark_used (t);
1633 if (DECL_STATIC_FUNCTION_P (t))
1634 return t;
1635 t = build (OFFSET_REF, TREE_TYPE (t), decl, t);
1636 PTRMEM_OK_P (t) = 1;
1637 return t;
1640 TREE_TYPE (fnfields) = unknown_type_node;
1642 t = build (OFFSET_REF, unknown_type_node, decl, fnfields);
1643 PTRMEM_OK_P (t) = 1;
1644 return t;
1647 t = member;
1649 if (t == NULL_TREE)
1651 error ("`%D' is not a member of type `%T'", name, type);
1652 return error_mark_node;
1655 if (TREE_CODE (t) == TYPE_DECL)
1657 TREE_USED (t) = 1;
1658 return t;
1660 /* static class members and class-specific enum
1661 values can be returned without further ado. */
1662 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1664 mark_used (t);
1665 return convert_from_reference (t);
1668 if (TREE_CODE (t) == FIELD_DECL && DECL_C_BIT_FIELD (t))
1670 error ("invalid pointer to bit-field `%D'", t);
1671 return error_mark_node;
1674 /* static class functions too. */
1675 if (TREE_CODE (t) == FUNCTION_DECL
1676 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1677 abort ();
1679 /* In member functions, the form `type::name' is no longer
1680 equivalent to `this->type::name', at least not until
1681 resolve_offset_ref. */
1682 t = build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
1683 PTRMEM_OK_P (t) = 1;
1684 return t;
1687 /* If a OFFSET_REF made it through to here, then it did
1688 not have its address taken. */
1690 tree
1691 resolve_offset_ref (tree exp)
1693 tree type = TREE_TYPE (exp);
1694 tree base = NULL_TREE;
1695 tree member;
1696 tree basetype, addr;
1698 if (TREE_CODE (exp) == OFFSET_REF)
1700 member = TREE_OPERAND (exp, 1);
1701 base = TREE_OPERAND (exp, 0);
1703 else
1705 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
1706 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
1708 error ("object missing in use of pointer-to-member construct");
1709 return error_mark_node;
1711 member = exp;
1712 type = TREE_TYPE (type);
1713 base = current_class_ref;
1716 if (BASELINK_P (member) || TREE_CODE (member) == TEMPLATE_ID_EXPR)
1717 return build_unary_op (ADDR_EXPR, exp, 0);
1719 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
1721 if (!flag_ms_extensions)
1722 /* A single non-static member, make sure we don't allow a
1723 pointer-to-member. */
1724 exp = ovl_cons (member, NULL_TREE);
1726 return build_unary_op (ADDR_EXPR, exp, 0);
1729 if ((TREE_CODE (member) == VAR_DECL
1730 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))
1731 && ! TYPE_PTRMEM_P (TREE_TYPE (member)))
1732 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
1734 /* These were static members. */
1735 if (!cxx_mark_addressable (member))
1736 return error_mark_node;
1737 return member;
1740 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE
1741 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE)
1742 return member;
1744 /* Syntax error can cause a member which should
1745 have been seen as static to be grok'd as non-static. */
1746 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE)
1748 cp_error_at ("member `%D' is non-static but referenced as a static member",
1749 member);
1750 error ("at this point in file");
1751 return error_mark_node;
1754 /* The first case is really just a reference to a member of `this'. */
1755 if (TREE_CODE (member) == FIELD_DECL
1756 && (base == current_class_ref || is_dummy_object (base)))
1758 tree binfo = NULL_TREE;
1760 /* Try to get to basetype from 'this'; if that doesn't work,
1761 nothing will. */
1762 base = current_class_ref;
1764 /* First convert to the intermediate base specified, if appropriate. */
1765 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
1766 base = build_scoped_ref (base, TYPE_OFFSET_BASETYPE (type), &binfo);
1768 return build_class_member_access_expr (base, member,
1769 /*access_path=*/NULL_TREE,
1770 /*preserve_reference=*/false);
1773 /* Ensure that we have an object. */
1774 if (is_dummy_object (base))
1775 addr = error_mark_node;
1776 else
1777 /* If this is a reference to a member function, then return the
1778 address of the member function (which may involve going
1779 through the object's vtable), otherwise, return an expression
1780 for the dereferenced pointer-to-member construct. */
1781 addr = build_unary_op (ADDR_EXPR, base, 0);
1783 if (TYPE_PTRMEM_P (TREE_TYPE (member)))
1785 if (addr == error_mark_node)
1787 error ("object missing in `%E'", exp);
1788 return error_mark_node;
1791 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member)));
1792 basetype = lookup_base (TREE_TYPE (TREE_TYPE (addr)),
1793 basetype, ba_check, NULL);
1794 addr = build_base_path (PLUS_EXPR, addr, basetype, 1);
1796 member = cp_convert (ptrdiff_type_node, member);
1798 addr = build (PLUS_EXPR, build_pointer_type (type), addr, member);
1799 return build_indirect_ref (addr, 0);
1801 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
1803 return get_member_function_from_ptrfunc (&addr, member);
1805 abort ();
1806 /* NOTREACHED */
1807 return NULL_TREE;
1810 /* If DECL is a `const' declaration, and its value is a known
1811 constant, then return that value. */
1813 tree
1814 decl_constant_value (tree decl)
1816 if (TREE_READONLY_DECL_P (decl)
1817 && ! TREE_THIS_VOLATILE (decl)
1818 && DECL_INITIAL (decl)
1819 && DECL_INITIAL (decl) != error_mark_node
1820 /* This is invalid if initial value is not constant.
1821 If it has either a function call, a memory reference,
1822 or a variable, then re-evaluating it could give different results. */
1823 && TREE_CONSTANT (DECL_INITIAL (decl))
1824 /* Check for cases where this is sub-optimal, even though valid. */
1825 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1826 return DECL_INITIAL (decl);
1827 return decl;
1830 /* Common subroutines of build_new and build_vec_delete. */
1832 /* Call the global __builtin_delete to delete ADDR. */
1834 static tree
1835 build_builtin_delete_call (tree addr)
1837 mark_used (global_delete_fndecl);
1838 return build_call (global_delete_fndecl, build_tree_list (NULL_TREE, addr));
1841 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1842 (which needs to go through some sort of groktypename) or it
1843 is the name of the class we are newing. INIT is an initialization value.
1844 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1845 If INIT is void_type_node, it means do *not* call a constructor
1846 for this instance.
1848 For types with constructors, the data returned is initialized
1849 by the appropriate constructor.
1851 Whether the type has a constructor or not, if it has a pointer
1852 to a virtual function table, then that pointer is set up
1853 here.
1855 Unless I am mistaken, a call to new () will return initialized
1856 data regardless of whether the constructor itself is private or
1857 not. NOPE; new fails if the constructor is private (jcm).
1859 Note that build_new does nothing to assure that any special
1860 alignment requirements of the type are met. Rather, it leaves
1861 it up to malloc to do the right thing. Otherwise, folding to
1862 the right alignment cal cause problems if the user tries to later
1863 free the memory returned by `new'.
1865 PLACEMENT is the `placement' list for user-defined operator new (). */
1867 tree
1868 build_new (tree placement, tree decl, tree init, int use_global_new)
1870 tree type, rval;
1871 tree nelts = NULL_TREE, t;
1872 int has_array = 0;
1874 if (decl == error_mark_node)
1875 return error_mark_node;
1877 if (TREE_CODE (decl) == TREE_LIST)
1879 tree absdcl = TREE_VALUE (decl);
1880 tree last_absdcl = NULL_TREE;
1882 if (current_function_decl
1883 && DECL_CONSTRUCTOR_P (current_function_decl))
1884 my_friendly_assert (immediate_size_expand == 0, 19990926);
1886 nelts = integer_one_node;
1888 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
1889 abort ();
1890 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
1892 last_absdcl = absdcl;
1893 absdcl = TREE_OPERAND (absdcl, 0);
1896 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
1898 /* probably meant to be a vec new */
1899 tree this_nelts;
1901 while (TREE_OPERAND (absdcl, 0)
1902 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
1904 last_absdcl = absdcl;
1905 absdcl = TREE_OPERAND (absdcl, 0);
1908 has_array = 1;
1909 this_nelts = TREE_OPERAND (absdcl, 1);
1910 if (this_nelts != error_mark_node)
1912 if (this_nelts == NULL_TREE)
1913 error ("new of array type fails to specify size");
1914 else if (processing_template_decl)
1916 nelts = this_nelts;
1917 absdcl = TREE_OPERAND (absdcl, 0);
1919 else
1921 if (build_expr_type_conversion (WANT_INT | WANT_ENUM,
1922 this_nelts, false)
1923 == NULL_TREE)
1924 pedwarn ("size in array new must have integral type");
1926 this_nelts = save_expr (cp_convert (sizetype, this_nelts));
1927 absdcl = TREE_OPERAND (absdcl, 0);
1928 if (this_nelts == integer_zero_node)
1930 warning ("zero size array reserves no space");
1931 nelts = integer_zero_node;
1933 else
1934 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
1937 else
1938 nelts = integer_zero_node;
1941 if (last_absdcl)
1942 TREE_OPERAND (last_absdcl, 0) = absdcl;
1943 else
1944 TREE_VALUE (decl) = absdcl;
1946 type = groktypename (decl);
1947 if (! type || type == error_mark_node)
1948 return error_mark_node;
1950 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
1952 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
1954 /* An aggregate type. */
1955 type = IDENTIFIER_TYPE_VALUE (decl);
1956 decl = TYPE_MAIN_DECL (type);
1958 else
1960 /* A builtin type. */
1961 decl = lookup_name (decl, 1);
1962 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
1963 type = TREE_TYPE (decl);
1966 else if (TREE_CODE (decl) == TYPE_DECL)
1968 type = TREE_TYPE (decl);
1970 else
1972 type = decl;
1973 decl = TYPE_MAIN_DECL (type);
1976 if (processing_template_decl)
1978 if (has_array)
1979 t = tree_cons (tree_cons (NULL_TREE, type, NULL_TREE),
1980 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
1981 NULL_TREE);
1982 else
1983 t = type;
1985 rval = build_min (NEW_EXPR, build_pointer_type (type),
1986 placement, t, init);
1987 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
1988 return rval;
1991 /* ``A reference cannot be created by the new operator. A reference
1992 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
1993 returned by new.'' ARM 5.3.3 */
1994 if (TREE_CODE (type) == REFERENCE_TYPE)
1996 error ("new cannot be applied to a reference type");
1997 type = TREE_TYPE (type);
2000 if (TREE_CODE (type) == FUNCTION_TYPE)
2002 error ("new cannot be applied to a function type");
2003 return error_mark_node;
2006 /* When the object being created is an array, the new-expression yields a
2007 pointer to the initial element (if any) of the array. For example,
2008 both new int and new int[10] return an int*. 5.3.4. */
2009 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
2011 nelts = array_type_nelts_top (type);
2012 has_array = 1;
2013 type = TREE_TYPE (type);
2016 if (has_array)
2017 t = build_nt (ARRAY_REF, type, nelts);
2018 else
2019 t = type;
2021 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
2022 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2023 TREE_SIDE_EFFECTS (rval) = 1;
2024 rval = build_new_1 (rval);
2025 if (rval == error_mark_node)
2026 return error_mark_node;
2028 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2029 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2030 TREE_NO_UNUSED_WARNING (rval) = 1;
2032 return rval;
2035 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2037 tree
2038 build_java_class_ref (tree type)
2040 tree name = NULL_TREE, class_decl;
2041 static tree CL_suffix = NULL_TREE;
2042 if (CL_suffix == NULL_TREE)
2043 CL_suffix = get_identifier("class$");
2044 if (jclass_node == NULL_TREE)
2046 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
2047 if (jclass_node == NULL_TREE)
2048 fatal_error ("call to Java constructor, while `jclass' undefined");
2050 jclass_node = TREE_TYPE (jclass_node);
2053 /* Mangle the class$ field */
2055 tree field;
2056 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2057 if (DECL_NAME (field) == CL_suffix)
2059 mangle_decl (field);
2060 name = DECL_ASSEMBLER_NAME (field);
2061 break;
2063 if (!field)
2064 internal_error ("can't find class$");
2067 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2068 if (class_decl == NULL_TREE)
2070 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
2071 TREE_STATIC (class_decl) = 1;
2072 DECL_EXTERNAL (class_decl) = 1;
2073 TREE_PUBLIC (class_decl) = 1;
2074 DECL_ARTIFICIAL (class_decl) = 1;
2075 DECL_IGNORED_P (class_decl) = 1;
2076 pushdecl_top_level (class_decl);
2077 make_decl_rtl (class_decl, NULL);
2079 return class_decl;
2082 /* Returns the size of the cookie to use when allocating an array
2083 whose elements have the indicated TYPE. Assumes that it is already
2084 known that a cookie is needed. */
2086 static tree
2087 get_cookie_size (tree type)
2089 tree cookie_size;
2091 /* We need to allocate an additional max (sizeof (size_t), alignof
2092 (true_type)) bytes. */
2093 tree sizetype_size;
2094 tree type_align;
2096 sizetype_size = size_in_bytes (sizetype);
2097 type_align = size_int (TYPE_ALIGN_UNIT (type));
2098 if (INT_CST_LT_UNSIGNED (type_align, sizetype_size))
2099 cookie_size = sizetype_size;
2100 else
2101 cookie_size = type_align;
2103 return cookie_size;
2106 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2107 value is immediately handed to expand_expr. */
2109 static tree
2110 build_new_1 (tree exp)
2112 tree placement, init;
2113 tree true_type, size, rval, t;
2114 /* The type of the new-expression. (This type is always a pointer
2115 type.) */
2116 tree pointer_type;
2117 /* The type pointed to by POINTER_TYPE. */
2118 tree type;
2119 /* The type being allocated. For "new T[...]" this will be an
2120 ARRAY_TYPE. */
2121 tree full_type;
2122 /* A pointer type pointing to to the FULL_TYPE. */
2123 tree full_pointer_type;
2124 tree outer_nelts = NULL_TREE;
2125 tree nelts = NULL_TREE;
2126 tree alloc_call, alloc_expr;
2127 /* The address returned by the call to "operator new". This node is
2128 a VAR_DECL and is therefore reusable. */
2129 tree alloc_node;
2130 tree alloc_fn;
2131 tree cookie_expr, init_expr;
2132 int has_array = 0;
2133 enum tree_code code;
2134 int nothrow, check_new;
2135 /* Nonzero if the user wrote `::new' rather than just `new'. */
2136 int globally_qualified_p;
2137 int use_java_new = 0;
2138 /* If non-NULL, the number of extra bytes to allocate at the
2139 beginning of the storage allocated for an array-new expression in
2140 order to store the number of elements. */
2141 tree cookie_size = NULL_TREE;
2142 /* True if the function we are calling is a placement allocation
2143 function. */
2144 bool placement_allocation_fn_p;
2145 tree args = NULL_TREE;
2146 /* True if the storage must be initialized, either by a constructor
2147 or due to an explicit new-intiailizer. */
2148 bool is_initialized;
2149 /* The address of the thing allocated, not including any cookie. In
2150 particular, if an array cookie is in use, DATA_ADDR is the
2151 address of the first array element. This node is a VAR_DECL, and
2152 is therefore reusable. */
2153 tree data_addr;
2155 placement = TREE_OPERAND (exp, 0);
2156 type = TREE_OPERAND (exp, 1);
2157 init = TREE_OPERAND (exp, 2);
2158 globally_qualified_p = NEW_EXPR_USE_GLOBAL (exp);
2160 if (TREE_CODE (type) == ARRAY_REF)
2162 has_array = 1;
2163 nelts = outer_nelts = TREE_OPERAND (type, 1);
2164 type = TREE_OPERAND (type, 0);
2166 /* Use an incomplete array type to avoid VLA headaches. */
2167 full_type = build_cplus_array_type (type, NULL_TREE);
2169 else
2170 full_type = type;
2172 true_type = type;
2174 code = has_array ? VEC_NEW_EXPR : NEW_EXPR;
2176 /* If our base type is an array, then make sure we know how many elements
2177 it has. */
2178 while (TREE_CODE (true_type) == ARRAY_TYPE)
2180 tree this_nelts = array_type_nelts_top (true_type);
2181 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
2182 true_type = TREE_TYPE (true_type);
2185 if (!complete_type_or_else (true_type, exp))
2186 return error_mark_node;
2188 if (TREE_CODE (true_type) == VOID_TYPE)
2190 error ("invalid type `void' for new");
2191 return error_mark_node;
2194 if (abstract_virtuals_error (NULL_TREE, true_type))
2195 return error_mark_node;
2197 is_initialized = (TYPE_NEEDS_CONSTRUCTING (type) || init);
2198 if (CP_TYPE_CONST_P (true_type) && !is_initialized)
2200 error ("uninitialized const in `new' of `%#T'", true_type);
2201 return error_mark_node;
2204 size = size_in_bytes (true_type);
2205 if (has_array)
2206 size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
2208 /* Allocate the object. */
2209 if (! placement && TYPE_FOR_JAVA (true_type))
2211 tree class_addr, alloc_decl;
2212 tree class_decl = build_java_class_ref (true_type);
2213 tree class_size = size_in_bytes (true_type);
2214 static const char alloc_name[] = "_Jv_AllocObject";
2215 use_java_new = 1;
2216 alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name));
2217 if (alloc_decl == NULL_TREE)
2218 fatal_error ("call to Java constructor with `%s' undefined",
2219 alloc_name);
2221 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2222 alloc_call = (build_function_call
2223 (alloc_decl,
2224 tree_cons (NULL_TREE, class_addr,
2225 build_tree_list (NULL_TREE, class_size))));
2227 else
2229 tree fnname;
2231 fnname = ansi_opname (code);
2233 if (!globally_qualified_p
2234 && CLASS_TYPE_P (true_type)
2235 && (has_array
2236 ? TYPE_HAS_ARRAY_NEW_OPERATOR (true_type)
2237 : TYPE_HAS_NEW_OPERATOR (true_type)))
2239 /* Use a class-specific operator new. */
2240 /* If a cookie is required, add some extra space. */
2241 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type))
2243 cookie_size = get_cookie_size (true_type);
2244 size = size_binop (PLUS_EXPR, size, cookie_size);
2246 /* Create the argument list. */
2247 args = tree_cons (NULL_TREE, size, placement);
2248 /* Call the function. */
2249 alloc_call = build_method_call (build_dummy_object (true_type),
2250 fnname, args,
2251 TYPE_BINFO (true_type),
2252 LOOKUP_NORMAL);
2254 else
2256 /* Use a global operator new. */
2257 /* See if a cookie might be required. */
2258 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type))
2259 cookie_size = get_cookie_size (true_type);
2260 else
2261 cookie_size = NULL_TREE;
2263 alloc_call = build_operator_new_call (fnname, placement,
2264 &size, &cookie_size);
2268 if (alloc_call == error_mark_node)
2269 return error_mark_node;
2271 /* The ALLOC_CALL should be a CALL_EXPR -- or a COMPOUND_EXPR whose
2272 right-hand-side is ultimately a CALL_EXPR -- and the first
2273 operand should be the address of a known FUNCTION_DECL. */
2274 t = alloc_call;
2275 while (TREE_CODE (t) == COMPOUND_EXPR)
2276 t = TREE_OPERAND (t, 1);
2277 alloc_fn = get_callee_fndecl (t);
2278 my_friendly_assert (alloc_fn != NULL_TREE, 20020325);
2280 /* Now, check to see if this function is actually a placement
2281 allocation function. This can happen even when PLACEMENT is NULL
2282 because we might have something like:
2284 struct S { void* operator new (size_t, int i = 0); };
2286 A call to `new S' will get this allocation function, even though
2287 there is no explicit placement argument. If there is more than
2288 one argument, or there are variable arguments, then this is a
2289 placement allocation function. */
2290 placement_allocation_fn_p
2291 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
2292 || varargs_function_p (alloc_fn));
2294 /* unless an allocation function is declared with an empty excep-
2295 tion-specification (_except.spec_), throw(), it indicates failure to
2296 allocate storage by throwing a bad_alloc exception (clause _except_,
2297 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2298 cation function is declared with an empty exception-specification,
2299 throw(), it returns null to indicate failure to allocate storage and a
2300 non-null pointer otherwise.
2302 So check for a null exception spec on the op new we just called. */
2304 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
2305 check_new = (flag_check_new || nothrow) && ! use_java_new;
2307 /* In the simple case, we can stop now. */
2308 pointer_type = build_pointer_type (type);
2309 if (!cookie_size && !is_initialized)
2310 return build_nop (pointer_type, alloc_call);
2312 /* While we're working, use a pointer to the type we've actually
2313 allocated. Store the result of the call in a variable so that we
2314 can use it more than once. */
2315 full_pointer_type = build_pointer_type (full_type);
2316 alloc_expr = get_target_expr (build_nop (full_pointer_type, alloc_call));
2317 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
2319 if (cookie_size)
2321 tree cookie;
2323 /* Adjust so we're pointing to the start of the object. */
2324 data_addr = get_target_expr (build (PLUS_EXPR, full_pointer_type,
2325 alloc_node, cookie_size));
2327 /* Store the number of bytes allocated so that we can know how
2328 many elements to destroy later. We use the last sizeof
2329 (size_t) bytes to store the number of elements. */
2330 cookie = build (MINUS_EXPR, build_pointer_type (sizetype),
2331 data_addr, size_in_bytes (sizetype));
2332 cookie = build_indirect_ref (cookie, NULL);
2334 cookie_expr = build (MODIFY_EXPR, sizetype, cookie, nelts);
2335 data_addr = TARGET_EXPR_SLOT (data_addr);
2337 else
2339 cookie_expr = NULL_TREE;
2340 data_addr = alloc_node;
2343 /* Now initialize the allocated object. */
2344 if (is_initialized)
2346 init_expr = build_indirect_ref (data_addr, NULL);
2348 if (init == void_zero_node)
2349 init = build_default_init (full_type, nelts);
2350 else if (init && pedantic && has_array)
2351 pedwarn ("ISO C++ forbids initialization in array new");
2353 if (has_array)
2354 init_expr
2355 = build_vec_init (init_expr,
2356 cp_build_binary_op (MINUS_EXPR, outer_nelts,
2357 integer_one_node),
2358 init, /*from_array=*/0);
2359 else if (TYPE_NEEDS_CONSTRUCTING (type))
2360 init_expr = build_special_member_call (init_expr,
2361 complete_ctor_identifier,
2362 init, TYPE_BINFO (true_type),
2363 LOOKUP_NORMAL);
2364 else
2366 /* We are processing something like `new int (10)', which
2367 means allocate an int, and initialize it with 10. */
2369 if (TREE_CODE (init) == TREE_LIST)
2371 if (TREE_CHAIN (init) != NULL_TREE)
2372 pedwarn
2373 ("initializer list being treated as compound expression");
2374 init = build_compound_expr (init);
2376 else if (TREE_CODE (init) == CONSTRUCTOR
2377 && TREE_TYPE (init) == NULL_TREE)
2379 pedwarn ("ISO C++ forbids aggregate initializer to new");
2380 init = digest_init (type, init, 0);
2383 init_expr = build_modify_expr (init_expr, INIT_EXPR, init);
2386 if (init_expr == error_mark_node)
2387 return error_mark_node;
2389 /* If any part of the object initialization terminates by throwing an
2390 exception and a suitable deallocation function can be found, the
2391 deallocation function is called to free the memory in which the
2392 object was being constructed, after which the exception continues
2393 to propagate in the context of the new-expression. If no
2394 unambiguous matching deallocation function can be found,
2395 propagating the exception does not cause the object's memory to be
2396 freed. */
2397 if (flag_exceptions && ! use_java_new)
2399 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
2400 tree cleanup;
2401 int flags = (LOOKUP_NORMAL
2402 | (globally_qualified_p * LOOKUP_GLOBAL));
2404 /* The Standard is unclear here, but the right thing to do
2405 is to use the same method for finding deallocation
2406 functions that we use for finding allocation functions. */
2407 flags |= LOOKUP_SPECULATIVELY;
2409 cleanup = build_op_delete_call (dcode, alloc_node, size, flags,
2410 (placement_allocation_fn_p
2411 ? alloc_call : NULL_TREE));
2413 /* Ack! First we allocate the memory. Then we set our sentry
2414 variable to true, and expand a cleanup that deletes the memory
2415 if sentry is true. Then we run the constructor, and finally
2416 clear the sentry.
2418 It would be nice to be able to handle this without the sentry
2419 variable, perhaps with a TRY_CATCH_EXPR, but this doesn't
2420 work. We allocate the space first, so if there are any
2421 temporaries with cleanups in the constructor args we need this
2422 EH region to extend until end of full-expression to preserve
2423 nesting.
2425 If the backend had some mechanism so that we could force the
2426 allocation to be expanded after all the other args to the
2427 constructor, that would fix the nesting problem and we could
2428 do away with this complexity. But that would complicate other
2429 things; in particular, it would make it difficult to bail out
2430 if the allocation function returns null. Er, no, it wouldn't;
2431 we just don't run the constructor. The standard says it's
2432 unspecified whether or not the args are evaluated.
2434 FIXME FIXME FIXME inline invisible refs as refs. That way we
2435 can preevaluate value parameters. */
2437 if (cleanup)
2439 tree end, sentry, begin;
2441 begin = get_target_expr (boolean_true_node);
2442 CLEANUP_EH_ONLY (begin) = 1;
2444 sentry = TARGET_EXPR_SLOT (begin);
2446 TARGET_EXPR_CLEANUP (begin)
2447 = build (COND_EXPR, void_type_node, sentry,
2448 cleanup, void_zero_node);
2450 end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2451 sentry, boolean_false_node);
2453 init_expr
2454 = build (COMPOUND_EXPR, void_type_node, begin,
2455 build (COMPOUND_EXPR, void_type_node, init_expr,
2456 end));
2460 else
2461 init_expr = NULL_TREE;
2463 /* Now build up the return value in reverse order. */
2465 rval = data_addr;
2467 if (init_expr)
2468 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
2469 if (cookie_expr)
2470 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
2472 if (rval == alloc_node)
2473 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
2474 and return the call (which doesn't need to be adjusted). */
2475 rval = TARGET_EXPR_INITIAL (alloc_expr);
2476 else
2478 if (check_new)
2480 tree ifexp = cp_build_binary_op (NE_EXPR, alloc_node,
2481 integer_zero_node);
2482 rval = build_conditional_expr (ifexp, rval, alloc_node);
2485 /* Perform the allocation before anything else, so that ALLOC_NODE
2486 has been initialized before we start using it. */
2487 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2490 /* Convert to the final type. */
2491 return build_nop (pointer_type, rval);
2494 static tree
2495 build_vec_delete_1 (tree base, tree maxindex, tree type,
2496 special_function_kind auto_delete_vec, int use_global_delete)
2498 tree virtual_size;
2499 tree ptype = build_pointer_type (type = complete_type (type));
2500 tree size_exp = size_in_bytes (type);
2502 /* Temporary variables used by the loop. */
2503 tree tbase, tbase_init;
2505 /* This is the body of the loop that implements the deletion of a
2506 single element, and moves temp variables to next elements. */
2507 tree body;
2509 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2510 tree loop;
2512 /* This is the thing that governs what to do after the loop has run. */
2513 tree deallocate_expr = 0;
2515 /* This is the BIND_EXPR which holds the outermost iterator of the
2516 loop. It is convenient to set this variable up and test it before
2517 executing any other code in the loop.
2518 This is also the containing expression returned by this function. */
2519 tree controller = NULL_TREE;
2521 /* We should only have 1-D arrays here. */
2522 if (TREE_CODE (type) == ARRAY_TYPE)
2523 abort ();
2525 if (! IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
2527 loop = integer_zero_node;
2528 goto no_destructor;
2531 /* The below is short by the cookie size. */
2532 virtual_size = size_binop (MULT_EXPR, size_exp,
2533 convert (sizetype, maxindex));
2535 tbase = create_temporary_var (ptype);
2536 tbase_init = build_modify_expr (tbase, NOP_EXPR,
2537 fold (build (PLUS_EXPR, ptype,
2538 base,
2539 virtual_size)));
2540 DECL_REGISTER (tbase) = 1;
2541 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
2542 TREE_SIDE_EFFECTS (controller) = 1;
2544 body = NULL_TREE;
2546 body = tree_cons (NULL_TREE,
2547 build_delete (ptype, tbase, sfk_complete_destructor,
2548 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
2549 body);
2551 body = tree_cons (NULL_TREE,
2552 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
2553 body);
2555 body = tree_cons (NULL_TREE,
2556 build (EXIT_EXPR, void_type_node,
2557 build (EQ_EXPR, boolean_type_node, base, tbase)),
2558 body);
2560 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
2562 loop = tree_cons (NULL_TREE, tbase_init,
2563 tree_cons (NULL_TREE, loop, NULL_TREE));
2564 loop = build_compound_expr (loop);
2566 no_destructor:
2567 /* If the delete flag is one, or anything else with the low bit set,
2568 delete the storage. */
2569 deallocate_expr = integer_zero_node;
2570 if (auto_delete_vec != sfk_base_destructor)
2572 tree base_tbd;
2574 /* The below is short by the cookie size. */
2575 virtual_size = size_binop (MULT_EXPR, size_exp,
2576 convert (sizetype, maxindex));
2578 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2579 /* no header */
2580 base_tbd = base;
2581 else
2583 tree cookie_size;
2585 cookie_size = get_cookie_size (type);
2586 base_tbd
2587 = cp_convert (ptype,
2588 cp_build_binary_op (MINUS_EXPR,
2589 cp_convert (string_type_node,
2590 base),
2591 cookie_size));
2592 /* True size with header. */
2593 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
2596 if (auto_delete_vec == sfk_deleting_destructor)
2597 deallocate_expr = build_x_delete (base_tbd,
2598 2 | use_global_delete,
2599 virtual_size);
2602 if (loop && deallocate_expr != integer_zero_node)
2604 body = tree_cons (NULL_TREE, loop,
2605 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
2606 body = build_compound_expr (body);
2608 else
2609 body = loop;
2611 /* Outermost wrapper: If pointer is null, punt. */
2612 body = fold (build (COND_EXPR, void_type_node,
2613 fold (build (NE_EXPR, boolean_type_node, base,
2614 integer_zero_node)),
2615 body, integer_zero_node));
2616 body = build1 (NOP_EXPR, void_type_node, body);
2618 if (controller)
2620 TREE_OPERAND (controller, 1) = body;
2621 body = controller;
2624 if (TREE_CODE (base) == SAVE_EXPR)
2625 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
2626 body = build (COMPOUND_EXPR, void_type_node, base, body);
2628 return cp_convert (void_type_node, body);
2631 /* Create an unnamed variable of the indicated TYPE. */
2633 tree
2634 create_temporary_var (tree type)
2636 tree decl;
2638 decl = build_decl (VAR_DECL, NULL_TREE, type);
2639 TREE_USED (decl) = 1;
2640 DECL_ARTIFICIAL (decl) = 1;
2641 DECL_SOURCE_LOCATION (decl) = input_location;
2642 DECL_IGNORED_P (decl) = 1;
2643 DECL_CONTEXT (decl) = current_function_decl;
2645 return decl;
2648 /* Create a new temporary variable of the indicated TYPE, initialized
2649 to INIT.
2651 It is not entered into current_binding_level, because that breaks
2652 things when it comes time to do final cleanups (which take place
2653 "outside" the binding contour of the function). */
2655 static tree
2656 get_temp_regvar (tree type, tree init)
2658 tree decl;
2660 decl = create_temporary_var (type);
2661 if (building_stmt_tree ())
2662 add_decl_stmt (decl);
2663 else
2664 SET_DECL_RTL (decl, assign_temp (type, 2, 0, 1));
2665 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
2667 return decl;
2670 /* `build_vec_init' returns tree structure that performs
2671 initialization of a vector of aggregate types.
2673 BASE is a reference to the vector, of ARRAY_TYPE.
2674 MAXINDEX is the maximum index of the array (one less than the
2675 number of elements). It is only used if
2676 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
2677 INIT is the (possibly NULL) initializer.
2679 FROM_ARRAY is 0 if we should init everything with INIT
2680 (i.e., every element initialized from INIT).
2681 FROM_ARRAY is 1 if we should index into INIT in parallel
2682 with initialization of DECL.
2683 FROM_ARRAY is 2 if we should index into INIT in parallel,
2684 but use assignment instead of initialization. */
2686 tree
2687 build_vec_init (tree base, tree maxindex, tree init, int from_array)
2689 tree rval;
2690 tree base2 = NULL_TREE;
2691 tree size;
2692 tree itype = NULL_TREE;
2693 tree iterator;
2694 /* The type of the array. */
2695 tree atype = TREE_TYPE (base);
2696 /* The type of an element in the array. */
2697 tree type = TREE_TYPE (atype);
2698 /* The type of a pointer to an element in the array. */
2699 tree ptype;
2700 tree stmt_expr;
2701 tree compound_stmt;
2702 int destroy_temps;
2703 tree try_block = NULL_TREE;
2704 tree try_body = NULL_TREE;
2705 int num_initialized_elts = 0;
2707 if (TYPE_DOMAIN (atype))
2708 maxindex = array_type_nelts (atype);
2710 if (maxindex == NULL_TREE || maxindex == error_mark_node)
2711 return error_mark_node;
2713 if (init
2714 && (from_array == 2
2715 ? (!CLASS_TYPE_P (type) || !TYPE_HAS_COMPLEX_ASSIGN_REF (type))
2716 : !TYPE_NEEDS_CONSTRUCTING (type))
2717 && ((TREE_CODE (init) == CONSTRUCTOR
2718 /* Don't do this if the CONSTRUCTOR might contain something
2719 that might throw and require us to clean up. */
2720 && (CONSTRUCTOR_ELTS (init) == NULL_TREE
2721 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (target_type (type))))
2722 || from_array))
2724 /* Do non-default initialization of POD arrays resulting from
2725 brace-enclosed initializers. In this case, digest_init and
2726 store_constructor will handle the semantics for us. */
2728 stmt_expr = build (INIT_EXPR, atype, base, init);
2729 return stmt_expr;
2732 maxindex = cp_convert (ptrdiff_type_node, maxindex);
2733 ptype = build_pointer_type (type);
2734 size = size_in_bytes (type);
2735 if (TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE)
2736 base = cp_convert (ptype, default_conversion (base));
2738 /* The code we are generating looks like:
2740 T* t1 = (T*) base;
2741 T* rval = t1;
2742 ptrdiff_t iterator = maxindex;
2743 try {
2744 for (; iterator != -1; --iterator) {
2745 ... initialize *t1 ...
2746 ++t1;
2748 } catch (...) {
2749 ... destroy elements that were constructed ...
2751 return rval;
2753 We can omit the try and catch blocks if we know that the
2754 initialization will never throw an exception, or if the array
2755 elements do not have destructors. We can omit the loop completely if
2756 the elements of the array do not have constructors.
2758 We actually wrap the entire body of the above in a STMT_EXPR, for
2759 tidiness.
2761 When copying from array to another, when the array elements have
2762 only trivial copy constructors, we should use __builtin_memcpy
2763 rather than generating a loop. That way, we could take advantage
2764 of whatever cleverness the back-end has for dealing with copies
2765 of blocks of memory. */
2767 begin_init_stmts (&stmt_expr, &compound_stmt);
2768 destroy_temps = stmts_are_full_exprs_p ();
2769 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2770 rval = get_temp_regvar (ptype, base);
2771 base = get_temp_regvar (ptype, rval);
2772 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2774 /* Protect the entire array initialization so that we can destroy
2775 the partially constructed array if an exception is thrown.
2776 But don't do this if we're assigning. */
2777 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2778 && from_array != 2)
2780 try_block = begin_try_block ();
2781 try_body = begin_compound_stmt (/*has_no_scope=*/1);
2784 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
2786 /* Do non-default initialization of non-POD arrays resulting from
2787 brace-enclosed initializers. */
2789 tree elts;
2790 from_array = 0;
2792 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
2794 tree elt = TREE_VALUE (elts);
2795 tree baseref = build1 (INDIRECT_REF, type, base);
2797 num_initialized_elts++;
2799 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
2800 finish_expr_stmt (build_aggr_init (baseref, elt, 0));
2801 else
2802 finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
2803 elt));
2805 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
2806 finish_expr_stmt (build_unary_op (PREDECREMENT_EXPR, iterator, 0));
2809 /* Clear out INIT so that we don't get confused below. */
2810 init = NULL_TREE;
2812 else if (from_array)
2814 /* If initializing one array from another, initialize element by
2815 element. We rely upon the below calls the do argument
2816 checking. */
2817 if (init)
2819 base2 = default_conversion (init);
2820 itype = TREE_TYPE (base2);
2821 base2 = get_temp_regvar (itype, base2);
2822 itype = TREE_TYPE (itype);
2824 else if (TYPE_LANG_SPECIFIC (type)
2825 && TYPE_NEEDS_CONSTRUCTING (type)
2826 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2828 error ("initializer ends prematurely");
2829 return error_mark_node;
2833 /* Now, default-initialize any remaining elements. We don't need to
2834 do that if a) the type does not need constructing, or b) we've
2835 already initialized all the elements.
2837 We do need to keep going if we're copying an array. */
2839 if (from_array
2840 || (TYPE_NEEDS_CONSTRUCTING (type)
2841 && ! (host_integerp (maxindex, 0)
2842 && (num_initialized_elts
2843 == tree_low_cst (maxindex, 0) + 1))))
2845 /* If the ITERATOR is equal to -1, then we don't have to loop;
2846 we've already initialized all the elements. */
2847 tree for_stmt;
2848 tree for_body;
2849 tree elt_init;
2851 for_stmt = begin_for_stmt ();
2852 finish_for_init_stmt (for_stmt);
2853 finish_for_cond (build (NE_EXPR, boolean_type_node,
2854 iterator, integer_minus_one_node),
2855 for_stmt);
2856 finish_for_expr (build_unary_op (PREDECREMENT_EXPR, iterator, 0),
2857 for_stmt);
2859 /* Otherwise, loop through the elements. */
2860 for_body = begin_compound_stmt (/*has_no_scope=*/1);
2862 /* When we're not building a statement-tree, things are a little
2863 complicated. If, when we recursively call build_aggr_init,
2864 an expression containing a TARGET_EXPR is expanded, then it
2865 may get a cleanup. Then, the result of that expression is
2866 passed to finish_expr_stmt, which will call
2867 expand_start_target_temps/expand_end_target_temps. However,
2868 the latter call will not cause the cleanup to run because
2869 that block will still be on the block stack. So, we call
2870 expand_start_target_temps here manually; the corresponding
2871 call to expand_end_target_temps below will cause the cleanup
2872 to be performed. */
2873 if (!building_stmt_tree ())
2874 expand_start_target_temps ();
2876 if (from_array)
2878 tree to = build1 (INDIRECT_REF, type, base);
2879 tree from;
2881 if (base2)
2882 from = build1 (INDIRECT_REF, itype, base2);
2883 else
2884 from = NULL_TREE;
2886 if (from_array == 2)
2887 elt_init = build_modify_expr (to, NOP_EXPR, from);
2888 else if (TYPE_NEEDS_CONSTRUCTING (type))
2889 elt_init = build_aggr_init (to, from, 0);
2890 else if (from)
2891 elt_init = build_modify_expr (to, NOP_EXPR, from);
2892 else
2893 abort ();
2895 else if (TREE_CODE (type) == ARRAY_TYPE)
2897 if (init != 0)
2898 sorry
2899 ("cannot initialize multi-dimensional array with initializer");
2900 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
2901 0, 0, 0);
2903 else
2904 elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base),
2905 init, 0);
2907 /* The initialization of each array element is a
2908 full-expression, as per core issue 124. */
2909 if (!building_stmt_tree ())
2911 genrtl_expr_stmt (elt_init);
2912 expand_end_target_temps ();
2914 else
2916 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
2917 finish_expr_stmt (elt_init);
2918 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2921 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
2922 if (base2)
2923 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base2, 0));
2925 finish_compound_stmt (/*has_no_scope=*/1, for_body);
2926 finish_for_stmt (for_stmt);
2929 /* Make sure to cleanup any partially constructed elements. */
2930 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2931 && from_array != 2)
2933 tree e;
2934 tree m = cp_build_binary_op (MINUS_EXPR, maxindex, iterator);
2936 /* Flatten multi-dimensional array since build_vec_delete only
2937 expects one-dimensional array. */
2938 if (TREE_CODE (type) == ARRAY_TYPE)
2940 m = cp_build_binary_op (MULT_EXPR, m,
2941 array_type_nelts_total (type));
2942 type = strip_array_types (type);
2945 finish_compound_stmt (/*has_no_scope=*/1, try_body);
2946 finish_cleanup_try_block (try_block);
2947 e = build_vec_delete_1 (rval, m,
2948 type,
2949 sfk_base_destructor,
2950 /*use_global_delete=*/0);
2951 finish_cleanup (e, try_block);
2954 /* The value of the array initialization is the address of the
2955 first element in the array. */
2956 finish_expr_stmt (rval);
2958 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
2959 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
2960 return stmt_expr;
2963 /* Free up storage of type TYPE, at address ADDR.
2965 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
2966 of pointer.
2968 VIRTUAL_SIZE is the amount of storage that was allocated, and is
2969 used as the second argument to operator delete. It can include
2970 things like padding and magic size cookies. It has virtual in it,
2971 because if you have a base pointer and you delete through a virtual
2972 destructor, it should be the size of the dynamic object, not the
2973 static object, see Free Store 12.5 ISO C++.
2975 This does not call any destructors. */
2977 tree
2978 build_x_delete (tree addr, int which_delete, tree virtual_size)
2980 int use_global_delete = which_delete & 1;
2981 int use_vec_delete = !!(which_delete & 2);
2982 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
2983 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
2985 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
2988 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
2989 build_delete. */
2991 static tree
2992 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags)
2994 tree name;
2996 switch (dtor_kind)
2998 case sfk_complete_destructor:
2999 name = complete_dtor_identifier;
3000 break;
3002 case sfk_base_destructor:
3003 name = base_dtor_identifier;
3004 break;
3006 case sfk_deleting_destructor:
3007 name = deleting_dtor_identifier;
3008 break;
3010 default:
3011 abort ();
3013 return build_method_call (exp, name, NULL_TREE,
3014 TYPE_BINFO (TREE_TYPE (exp)), flags);
3017 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3018 ADDR is an expression which yields the store to be destroyed.
3019 AUTO_DELETE is the name of the destructor to call, i.e., either
3020 sfk_complete_destructor, sfk_base_destructor, or
3021 sfk_deleting_destructor.
3023 FLAGS is the logical disjunction of zero or more LOOKUP_
3024 flags. See cp-tree.h for more info. */
3026 tree
3027 build_delete (tree type, tree addr, special_function_kind auto_delete,
3028 int flags, int use_global_delete)
3030 tree expr;
3032 if (addr == error_mark_node)
3033 return error_mark_node;
3035 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3036 set to `error_mark_node' before it gets properly cleaned up. */
3037 if (type == error_mark_node)
3038 return error_mark_node;
3040 type = TYPE_MAIN_VARIANT (type);
3042 if (TREE_CODE (type) == POINTER_TYPE)
3044 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3045 if (TREE_CODE (type) == ARRAY_TYPE)
3046 goto handle_array;
3048 if (VOID_TYPE_P (type)
3049 /* We don't want to warn about delete of void*, only other
3050 incomplete types. Deleting other incomplete types
3051 invokes undefined behavior, but it is not ill-formed, so
3052 compile to something that would even do The Right Thing
3053 (TM) should the type have a trivial dtor and no delete
3054 operator. */
3055 || !complete_type_or_diagnostic (type, addr, 1)
3056 || !IS_AGGR_TYPE (type))
3058 /* Call the builtin operator delete. */
3059 return build_builtin_delete_call (addr);
3061 if (TREE_SIDE_EFFECTS (addr))
3062 addr = save_expr (addr);
3064 /* throw away const and volatile on target type of addr */
3065 addr = convert_force (build_pointer_type (type), addr, 0);
3067 else if (TREE_CODE (type) == ARRAY_TYPE)
3069 handle_array:
3071 if (TYPE_DOMAIN (type) == NULL_TREE)
3073 error ("unknown array size in delete");
3074 return error_mark_node;
3076 return build_vec_delete (addr, array_type_nelts (type),
3077 auto_delete, use_global_delete);
3079 else
3081 /* Don't check PROTECT here; leave that decision to the
3082 destructor. If the destructor is accessible, call it,
3083 else report error. */
3084 addr = build_unary_op (ADDR_EXPR, addr, 0);
3085 if (TREE_SIDE_EFFECTS (addr))
3086 addr = save_expr (addr);
3088 addr = convert_force (build_pointer_type (type), addr, 0);
3091 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3093 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3095 if (auto_delete != sfk_deleting_destructor)
3096 return void_zero_node;
3098 return build_op_delete_call
3099 (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
3100 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
3101 NULL_TREE);
3103 else
3105 tree do_delete = NULL_TREE;
3106 tree ifexp;
3108 my_friendly_assert (TYPE_HAS_DESTRUCTOR (type), 20011213);
3110 /* For `::delete x', we must not use the deleting destructor
3111 since then we would not be sure to get the global `operator
3112 delete'. */
3113 if (use_global_delete && auto_delete == sfk_deleting_destructor)
3115 /* We will use ADDR multiple times so we must save it. */
3116 addr = save_expr (addr);
3117 /* Delete the object. */
3118 do_delete = build_builtin_delete_call (addr);
3119 /* Otherwise, treat this like a complete object destructor
3120 call. */
3121 auto_delete = sfk_complete_destructor;
3123 /* If the destructor is non-virtual, there is no deleting
3124 variant. Instead, we must explicitly call the appropriate
3125 `operator delete' here. */
3126 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
3127 && auto_delete == sfk_deleting_destructor)
3129 /* We will use ADDR multiple times so we must save it. */
3130 addr = save_expr (addr);
3131 /* Build the call. */
3132 do_delete = build_op_delete_call (DELETE_EXPR,
3133 addr,
3134 cxx_sizeof_nowarn (type),
3135 LOOKUP_NORMAL,
3136 NULL_TREE);
3137 /* Call the complete object destructor. */
3138 auto_delete = sfk_complete_destructor;
3140 else if (auto_delete == sfk_deleting_destructor
3141 && TYPE_GETS_REG_DELETE (type))
3143 /* Make sure we have access to the member op delete, even though
3144 we'll actually be calling it from the destructor. */
3145 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
3146 LOOKUP_NORMAL, NULL_TREE);
3149 expr = build_dtor_call (build_indirect_ref (addr, NULL),
3150 auto_delete, flags);
3151 if (do_delete)
3152 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3154 if (flags & LOOKUP_DESTRUCTOR)
3155 /* Explicit destructor call; don't check for null pointer. */
3156 ifexp = integer_one_node;
3157 else
3158 /* Handle deleting a null pointer. */
3159 ifexp = fold (cp_build_binary_op (NE_EXPR, addr, integer_zero_node));
3161 if (ifexp != integer_one_node)
3162 expr = build (COND_EXPR, void_type_node,
3163 ifexp, expr, void_zero_node);
3165 return expr;
3169 /* At the beginning of a destructor, push cleanups that will call the
3170 destructors for our base classes and members.
3172 Called from begin_destructor_body. */
3174 void
3175 push_base_cleanups ()
3177 tree binfos;
3178 int i, n_baseclasses;
3179 tree member;
3180 tree expr;
3182 /* Run destructors for all virtual baseclasses. */
3183 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
3185 tree vbases;
3186 tree cond = (condition_conversion
3187 (build (BIT_AND_EXPR, integer_type_node,
3188 current_in_charge_parm,
3189 integer_two_node)));
3191 vbases = CLASSTYPE_VBASECLASSES (current_class_type);
3192 /* The CLASSTYPE_VBASECLASSES list is in initialization
3193 order, which is also the right order for pushing cleanups. */
3194 for (; vbases;
3195 vbases = TREE_CHAIN (vbases))
3197 tree vbase = TREE_VALUE (vbases);
3198 tree base_type = BINFO_TYPE (vbase);
3200 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (base_type))
3202 expr = build_special_member_call (current_class_ref,
3203 base_dtor_identifier,
3204 NULL_TREE,
3205 vbase,
3206 (LOOKUP_NORMAL
3207 | LOOKUP_NONVIRTUAL));
3208 expr = build (COND_EXPR, void_type_node, cond,
3209 expr, void_zero_node);
3210 finish_decl_cleanup (NULL_TREE, expr);
3215 binfos = BINFO_BASETYPES (TYPE_BINFO (current_class_type));
3216 n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
3218 /* Take care of the remaining baseclasses. */
3219 for (i = 0; i < n_baseclasses; i++)
3221 tree base_binfo = TREE_VEC_ELT (binfos, i);
3222 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
3223 || TREE_VIA_VIRTUAL (base_binfo))
3224 continue;
3226 expr = build_special_member_call (current_class_ref,
3227 base_dtor_identifier,
3228 NULL_TREE, base_binfo,
3229 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL);
3230 finish_decl_cleanup (NULL_TREE, expr);
3233 for (member = TYPE_FIELDS (current_class_type); member;
3234 member = TREE_CHAIN (member))
3236 if (TREE_CODE (member) != FIELD_DECL || DECL_ARTIFICIAL (member))
3237 continue;
3238 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member)))
3240 tree this_member = (build_class_member_access_expr
3241 (current_class_ref, member,
3242 /*access_path=*/NULL_TREE,
3243 /*preserve_reference=*/false));
3244 tree this_type = TREE_TYPE (member);
3245 expr = build_delete (this_type, this_member,
3246 sfk_complete_destructor,
3247 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
3249 finish_decl_cleanup (NULL_TREE, expr);
3254 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3256 tree
3257 build_vbase_delete (tree type, tree decl)
3259 tree vbases = CLASSTYPE_VBASECLASSES (type);
3260 tree result = NULL_TREE;
3261 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3263 my_friendly_assert (addr != error_mark_node, 222);
3265 while (vbases)
3267 tree this_addr
3268 = convert_force (build_pointer_type (BINFO_TYPE (TREE_VALUE (vbases))),
3269 addr, 0);
3270 result = tree_cons (NULL_TREE,
3271 build_delete (TREE_TYPE (this_addr), this_addr,
3272 sfk_base_destructor,
3273 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3274 result);
3275 vbases = TREE_CHAIN (vbases);
3277 return build_compound_expr (nreverse (result));
3280 /* Build a C++ vector delete expression.
3281 MAXINDEX is the number of elements to be deleted.
3282 ELT_SIZE is the nominal size of each element in the vector.
3283 BASE is the expression that should yield the store to be deleted.
3284 This function expands (or synthesizes) these calls itself.
3285 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3287 This also calls delete for virtual baseclasses of elements of the vector.
3289 Update: MAXINDEX is no longer needed. The size can be extracted from the
3290 start of the vector for pointers, and from the type for arrays. We still
3291 use MAXINDEX for arrays because it happens to already have one of the
3292 values we'd have to extract. (We could use MAXINDEX with pointers to
3293 confirm the size, and trap if the numbers differ; not clear that it'd
3294 be worth bothering.) */
3296 tree
3297 build_vec_delete (tree base, tree maxindex,
3298 special_function_kind auto_delete_vec, int use_global_delete)
3300 tree type;
3301 tree rval;
3302 tree base_init = NULL_TREE;
3304 if (TREE_CODE (base) == OFFSET_REF)
3305 base = resolve_offset_ref (base);
3307 type = TREE_TYPE (base);
3309 if (TREE_CODE (type) == POINTER_TYPE)
3311 /* Step back one from start of vector, and read dimension. */
3312 tree cookie_addr;
3314 if (TREE_SIDE_EFFECTS (base))
3316 base_init = get_target_expr (base);
3317 base = TARGET_EXPR_SLOT (base_init);
3319 type = strip_array_types (TREE_TYPE (type));
3320 cookie_addr = build (MINUS_EXPR,
3321 build_pointer_type (sizetype),
3322 base,
3323 TYPE_SIZE_UNIT (sizetype));
3324 maxindex = build_indirect_ref (cookie_addr, NULL);
3326 else if (TREE_CODE (type) == ARRAY_TYPE)
3328 /* get the total number of things in the array, maxindex is a bad name */
3329 maxindex = array_type_nelts_total (type);
3330 type = strip_array_types (type);
3331 base = build_unary_op (ADDR_EXPR, base, 1);
3332 if (TREE_SIDE_EFFECTS (base))
3334 base_init = get_target_expr (base);
3335 base = TARGET_EXPR_SLOT (base_init);
3338 else
3340 if (base != error_mark_node)
3341 error ("type to vector delete is neither pointer or array type");
3342 return error_mark_node;
3345 rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
3346 use_global_delete);
3347 if (base_init)
3348 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
3350 return rval;