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