]>
Commit | Line | Data |
---|---|---|
71c5b8ba | 1 | /* Handle initialization things in -*- C++ -*- |
a945c346 | 2 | Copyright (C) 1987-2024 Free Software Foundation, Inc. |
8d08fdba MS |
3 | Contributed by Michael Tiemann (tiemann@cygnus.com) |
4 | ||
f5adbb8d | 5 | This file is part of GCC. |
8d08fdba | 6 | |
f5adbb8d | 7 | GCC is free software; you can redistribute it and/or modify |
8d08fdba | 8 | it under the terms of the GNU General Public License as published by |
e77f031d | 9 | the Free Software Foundation; either version 3, or (at your option) |
8d08fdba MS |
10 | any later version. |
11 | ||
f5adbb8d | 12 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
e77f031d NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
8d08fdba | 20 | |
e92cc029 | 21 | /* High-level class interface. */ |
8d08fdba MS |
22 | |
23 | #include "config.h" | |
8d052bc7 | 24 | #include "system.h" |
4977bab6 | 25 | #include "coretypes.h" |
2adfab87 | 26 | #include "target.h" |
2adfab87 | 27 | #include "cp-tree.h" |
d8a2d370 DN |
28 | #include "stringpool.h" |
29 | #include "varasm.h" | |
45b0be94 | 30 | #include "gimplify.h" |
46621807 | 31 | #include "c-family/c-ubsan.h" |
24f12823 | 32 | #include "intl.h" |
314e6352 ML |
33 | #include "stringpool.h" |
34 | #include "attribs.h" | |
45b2222a | 35 | #include "asan.h" |
8e007055 | 36 | #include "stor-layout.h" |
2a837de2 | 37 | #include "pointer-query.h" |
8d08fdba | 38 | |
2a3398e1 NS |
39 | static bool begin_init_stmts (tree *, tree *); |
40 | static tree finish_init_stmts (bool, tree, tree); | |
2282d28d | 41 | static void construct_virtual_base (tree, tree); |
b9119edc PP |
42 | static bool expand_aggr_init_1 (tree, tree, tree, tree, int, tsubst_flags_t); |
43 | static bool expand_default_init (tree, tree, tree, tree, int, tsubst_flags_t); | |
362efdc1 NN |
44 | static int member_init_ok_or_else (tree, tree, tree); |
45 | static void expand_virtual_init (tree, tree); | |
2282d28d | 46 | static tree sort_mem_initializers (tree, tree); |
362efdc1 NN |
47 | static tree initializing_context (tree); |
48 | static void expand_cleanup_for_base (tree, tree); | |
362efdc1 | 49 | static tree dfs_initialize_vtbl_ptrs (tree, void *); |
362efdc1 | 50 | static tree build_field_list (tree, tree, int *); |
40bb78ad | 51 | static int diagnose_uninitialized_cst_or_ref_member_1 (tree, tree, bool, bool); |
8d08fdba | 52 | |
ff502317 BE |
53 | static GTY(()) tree fn; |
54 | ||
3dbc07b6 MM |
55 | /* We are about to generate some complex initialization code. |
56 | Conceptually, it is all a single expression. However, we may want | |
57 | to include conditionals, loops, and other such statement-level | |
58 | constructs. Therefore, we build the initialization code inside a | |
59 | statement-expression. This function starts such an expression. | |
60 | STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function; | |
61 | pass them back to finish_init_stmts when the expression is | |
62 | complete. */ | |
63 | ||
2a3398e1 | 64 | static bool |
362efdc1 | 65 | begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p) |
3dbc07b6 | 66 | { |
38e01f9e | 67 | bool is_global = !building_stmt_list_p (); |
c8094d83 | 68 | |
2a3398e1 | 69 | *stmt_expr_p = begin_stmt_expr (); |
325c3691 | 70 | *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE); |
2a3398e1 NS |
71 | |
72 | return is_global; | |
3dbc07b6 MM |
73 | } |
74 | ||
75 | /* Finish out the statement-expression begun by the previous call to | |
76 | begin_init_stmts. Returns the statement-expression itself. */ | |
77 | ||
2a3398e1 NS |
78 | static tree |
79 | finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt) | |
c8094d83 | 80 | { |
7a3397c7 | 81 | finish_compound_stmt (compound_stmt); |
c8094d83 | 82 | |
303b7406 | 83 | stmt_expr = finish_stmt_expr (stmt_expr, true); |
3dbc07b6 | 84 | |
38e01f9e | 85 | gcc_assert (!building_stmt_list_p () == is_global); |
c8094d83 | 86 | |
3dbc07b6 MM |
87 | return stmt_expr; |
88 | } | |
89 | ||
90 | /* Constructors */ | |
91 | ||
338d90b8 NS |
92 | /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base |
93 | which we want to initialize the vtable pointer for, DATA is | |
94 | TREE_LIST whose TREE_VALUE is the this ptr expression. */ | |
7177d104 | 95 | |
d569399b | 96 | static tree |
362efdc1 | 97 | dfs_initialize_vtbl_ptrs (tree binfo, void *data) |
d569399b | 98 | { |
5d5a519f NS |
99 | if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) |
100 | return dfs_skip_bases; | |
c8094d83 | 101 | |
5d5a519f | 102 | if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo)) |
d569399b MM |
103 | { |
104 | tree base_ptr = TREE_VALUE ((tree) data); | |
7177d104 | 105 | |
a271590a PC |
106 | base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1, |
107 | tf_warning_or_error); | |
d569399b MM |
108 | |
109 | expand_virtual_init (binfo, base_ptr); | |
110 | } | |
7177d104 | 111 | |
d569399b MM |
112 | return NULL_TREE; |
113 | } | |
114 | ||
cf2e003b MM |
115 | /* Initialize all the vtable pointers in the object pointed to by |
116 | ADDR. */ | |
e92cc029 | 117 | |
8d08fdba | 118 | void |
362efdc1 | 119 | initialize_vtbl_ptrs (tree addr) |
8d08fdba | 120 | { |
cf2e003b MM |
121 | tree list; |
122 | tree type; | |
123 | ||
124 | type = TREE_TYPE (TREE_TYPE (addr)); | |
125 | list = build_tree_list (type, addr); | |
d569399b | 126 | |
bbd15aac | 127 | /* Walk through the hierarchy, initializing the vptr in each base |
1f5a253a | 128 | class. We do these in pre-order because we can't find the virtual |
3461fba7 NS |
129 | bases for a class until we've initialized the vtbl for that |
130 | class. */ | |
5d5a519f | 131 | dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list); |
8d08fdba | 132 | } |
d569399b | 133 | |
17bbb839 MM |
134 | /* Return an expression for the zero-initialization of an object with |
135 | type T. This expression will either be a constant (in the case | |
136 | that T is a scalar), or a CONSTRUCTOR (in the case that T is an | |
b43d1bde PC |
137 | aggregate), or NULL (in the case that T does not require |
138 | initialization). In either case, the value can be used as | |
139 | DECL_INITIAL for a decl of the indicated TYPE; it is a valid static | |
140 | initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS | |
141 | is the number of elements in the array. If STATIC_STORAGE_P is | |
142 | TRUE, initializers are only generated for entities for which | |
1cb8292f | 143 | zero-initialization does not simply mean filling the storage with |
e33eba75 JJ |
144 | zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field, |
145 | subfields with bit positions at or above that bit size shouldn't | |
a8c1d899 JM |
146 | be added. Note that this only works when the result is assigned |
147 | to a base COMPONENT_REF; if we only have a pointer to the base subobject, | |
148 | expand_assignment will end up clearing the full size of TYPE. */ | |
94e6e4c4 | 149 | |
e33eba75 JJ |
150 | static tree |
151 | build_zero_init_1 (tree type, tree nelts, bool static_storage_p, | |
152 | tree field_size) | |
94e6e4c4 | 153 | { |
17bbb839 MM |
154 | tree init = NULL_TREE; |
155 | ||
156 | /* [dcl.init] | |
157 | ||
0fcedd9c | 158 | To zero-initialize an object of type T means: |
17bbb839 MM |
159 | |
160 | -- if T is a scalar type, the storage is set to the value of zero | |
0cbd7506 | 161 | converted to T. |
17bbb839 | 162 | |
d20b7173 | 163 | -- if T is a non-union class type, the storage for each non-static |
0cbd7506 | 164 | data member and each base-class subobject is zero-initialized. |
17bbb839 MM |
165 | |
166 | -- if T is a union type, the storage for its first data member is | |
0cbd7506 | 167 | zero-initialized. |
17bbb839 MM |
168 | |
169 | -- if T is an array type, the storage for each element is | |
0cbd7506 | 170 | zero-initialized. |
17bbb839 MM |
171 | |
172 | -- if T is a reference type, no initialization is performed. */ | |
94e6e4c4 | 173 | |
50bc768d | 174 | gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST); |
7a1d37e9 | 175 | |
17bbb839 MM |
176 | if (type == error_mark_node) |
177 | ; | |
178 | else if (static_storage_p && zero_init_p (type)) | |
179 | /* In order to save space, we do not explicitly build initializers | |
180 | for items that do not need them. GCC's semantics are that | |
181 | items with static storage duration that are not otherwise | |
182 | initialized are initialized to zero. */ | |
183 | ; | |
b2b1ea34 | 184 | else if (TYPE_PTR_OR_PTRMEM_P (type)) |
cda0a029 | 185 | init = fold (convert (type, nullptr_node)); |
b2b1ea34 JJ |
186 | else if (NULLPTR_TYPE_P (type)) |
187 | init = build_int_cst (type, 0); | |
b8063b29 | 188 | else if (SCALAR_TYPE_P (type)) |
550880a3 | 189 | init = build_zero_cst (type); |
5621a5d7 | 190 | else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type))) |
17bbb839 | 191 | { |
78327cf0 | 192 | tree field, next; |
9771b263 | 193 | vec<constructor_elt, va_gc> *v = NULL; |
17bbb839 | 194 | |
17bbb839 | 195 | /* Iterate over the fields, building initializations. */ |
78327cf0 | 196 | for (field = TYPE_FIELDS (type); field; field = next) |
17bbb839 | 197 | { |
78327cf0 JJ |
198 | next = DECL_CHAIN (field); |
199 | ||
17bbb839 MM |
200 | if (TREE_CODE (field) != FIELD_DECL) |
201 | continue; | |
202 | ||
78327cf0 JJ |
203 | /* For unions, only the first field is initialized. */ |
204 | if (TREE_CODE (type) == UNION_TYPE) | |
205 | next = NULL_TREE; | |
206 | ||
7614d42c JJ |
207 | if (TREE_TYPE (field) == error_mark_node) |
208 | continue; | |
209 | ||
e33eba75 JJ |
210 | /* Don't add virtual bases for base classes if they are beyond |
211 | the size of the current field, that means it is present | |
212 | somewhere else in the object. */ | |
213 | if (field_size) | |
214 | { | |
215 | tree bitpos = bit_position (field); | |
216 | if (TREE_CODE (bitpos) == INTEGER_CST | |
217 | && !tree_int_cst_lt (bitpos, field_size)) | |
218 | continue; | |
219 | } | |
220 | ||
78327cf0 JJ |
221 | /* Don't add zero width bitfields. */ |
222 | if (DECL_C_BIT_FIELD (field) | |
223 | && integer_zerop (DECL_SIZE (field))) | |
224 | continue; | |
225 | ||
17bbb839 MM |
226 | /* Note that for class types there will be FIELD_DECLs |
227 | corresponding to base classes as well. Thus, iterating | |
228 | over TYPE_FIELDs will result in correct initialization of | |
229 | all of the subobjects. */ | |
32a11c08 | 230 | if (!static_storage_p || !zero_init_p (TREE_TYPE (field))) |
4038c495 | 231 | { |
e33eba75 JJ |
232 | tree new_field_size |
233 | = (DECL_FIELD_IS_BASE (field) | |
234 | && DECL_SIZE (field) | |
235 | && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST) | |
236 | ? DECL_SIZE (field) : NULL_TREE; | |
237 | tree value = build_zero_init_1 (TREE_TYPE (field), | |
238 | /*nelts=*/NULL_TREE, | |
239 | static_storage_p, | |
240 | new_field_size); | |
b43d1bde PC |
241 | if (value) |
242 | CONSTRUCTOR_APPEND_ELT(v, field, value); | |
4038c495 | 243 | } |
17bbb839 | 244 | } |
4038c495 | 245 | |
0fcedd9c JM |
246 | /* Build a constructor to contain the initializations. */ |
247 | init = build_constructor (type, v); | |
17bbb839 MM |
248 | } |
249 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
94e6e4c4 | 250 | { |
17bbb839 | 251 | tree max_index; |
9771b263 | 252 | vec<constructor_elt, va_gc> *v = NULL; |
17bbb839 | 253 | |
17bbb839 | 254 | /* Iterate over the array elements, building initializations. */ |
6b6c8106 | 255 | if (nelts) |
2dcdd15d JJ |
256 | max_index = fold_build2_loc (input_location, MINUS_EXPR, |
257 | TREE_TYPE (nelts), nelts, | |
258 | build_one_cst (TREE_TYPE (nelts))); | |
259 | /* Treat flexible array members like [0] arrays. */ | |
260 | else if (TYPE_DOMAIN (type) == NULL_TREE) | |
7768cadb | 261 | return NULL_TREE; |
6b6c8106 SB |
262 | else |
263 | max_index = array_type_nelts (type); | |
9bdb04a2 AP |
264 | |
265 | /* If we have an error_mark here, we should just return error mark | |
266 | as we don't know the size of the array yet. */ | |
267 | if (max_index == error_mark_node) | |
268 | return error_mark_node; | |
50bc768d | 269 | gcc_assert (TREE_CODE (max_index) == INTEGER_CST); |
7a1d37e9 | 270 | |
a8e6c82a MM |
271 | /* A zero-sized array, which is accepted as an extension, will |
272 | have an upper bound of -1. */ | |
2dcdd15d | 273 | if (!integer_minus_onep (max_index)) |
94763647 | 274 | { |
f32682ca | 275 | constructor_elt ce; |
4038c495 | 276 | |
b01f0d13 | 277 | /* If this is a one element array, we just use a regular init. */ |
2dcdd15d | 278 | if (integer_zerop (max_index)) |
f32682ca | 279 | ce.index = size_zero_node; |
b01f0d13 | 280 | else |
f32682ca | 281 | ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, |
2dcdd15d | 282 | max_index); |
c8094d83 | 283 | |
2dcdd15d JJ |
284 | ce.value = build_zero_init_1 (TREE_TYPE (type), /*nelts=*/NULL_TREE, |
285 | static_storage_p, NULL_TREE); | |
f11c7048 JJ |
286 | if (ce.value) |
287 | { | |
288 | vec_alloc (v, 1); | |
289 | v->quick_push (ce); | |
290 | } | |
94763647 | 291 | } |
c8094d83 | 292 | |
4038c495 GB |
293 | /* Build a constructor to contain the initializations. */ |
294 | init = build_constructor (type, v); | |
94e6e4c4 | 295 | } |
b55b02ea | 296 | else if (VECTOR_TYPE_P (type)) |
e8160c9a | 297 | init = build_zero_cst (type); |
94e6e4c4 | 298 | else |
a9ed1829 | 299 | gcc_assert (TYPE_REF_P (type)); |
94e6e4c4 | 300 | |
17bbb839 MM |
301 | /* In all cases, the initializer is a constant. */ |
302 | if (init) | |
51eed280 | 303 | TREE_CONSTANT (init) = 1; |
94e6e4c4 AO |
304 | |
305 | return init; | |
306 | } | |
307 | ||
e33eba75 JJ |
308 | /* Return an expression for the zero-initialization of an object with |
309 | type T. This expression will either be a constant (in the case | |
310 | that T is a scalar), or a CONSTRUCTOR (in the case that T is an | |
311 | aggregate), or NULL (in the case that T does not require | |
312 | initialization). In either case, the value can be used as | |
313 | DECL_INITIAL for a decl of the indicated TYPE; it is a valid static | |
314 | initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS | |
315 | is the number of elements in the array. If STATIC_STORAGE_P is | |
316 | TRUE, initializers are only generated for entities for which | |
317 | zero-initialization does not simply mean filling the storage with | |
318 | zero bytes. */ | |
319 | ||
320 | tree | |
321 | build_zero_init (tree type, tree nelts, bool static_storage_p) | |
322 | { | |
323 | return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE); | |
324 | } | |
325 | ||
0fcedd9c | 326 | /* Return a suitable initializer for value-initializing an object of type |
8f540f06 | 327 | TYPE, as described in [dcl.init]. */ |
0fcedd9c | 328 | |
8f540f06 | 329 | tree |
309714d4 | 330 | build_value_init (tree type, tsubst_flags_t complain) |
0fcedd9c JM |
331 | { |
332 | /* [dcl.init] | |
333 | ||
334 | To value-initialize an object of type T means: | |
335 | ||
eca7fc57 JM |
336 | - if T is a class type (clause 9) with either no default constructor |
337 | (12.1) or a default constructor that is user-provided or deleted, | |
026c3cfd | 338 | then the object is default-initialized; |
0fcedd9c | 339 | |
eca7fc57 JM |
340 | - if T is a (possibly cv-qualified) class type without a user-provided |
341 | or deleted default constructor, then the object is zero-initialized | |
342 | and the semantic constraints for default-initialization are checked, | |
343 | and if T has a non-trivial default constructor, the object is | |
344 | default-initialized; | |
0fcedd9c JM |
345 | |
346 | - if T is an array type, then each element is value-initialized; | |
347 | ||
348 | - otherwise, the object is zero-initialized. | |
349 | ||
350 | A program that calls for default-initialization or | |
eca7fc57 | 351 | value-initialization of an entity of reference type is ill-formed. */ |
0fcedd9c | 352 | |
f968ef9b | 353 | if (CLASS_TYPE_P (type) && type_build_ctor_call (type)) |
0fcedd9c | 354 | { |
f968ef9b JJ |
355 | tree ctor |
356 | = build_special_member_call (NULL_TREE, complete_ctor_identifier, | |
357 | NULL, type, LOOKUP_NORMAL, complain); | |
358 | if (ctor == error_mark_node || TREE_CONSTANT (ctor)) | |
a710f1f8 | 359 | return ctor; |
ce46d604 JM |
360 | if (processing_template_decl) |
361 | /* The AGGR_INIT_EXPR tweaking below breaks in templates. */ | |
362 | return build_min (CAST_EXPR, type, NULL_TREE); | |
a710f1f8 JM |
363 | tree fn = NULL_TREE; |
364 | if (TREE_CODE (ctor) == CALL_EXPR) | |
365 | fn = get_callee_fndecl (ctor); | |
366 | ctor = build_aggr_init_expr (type, ctor); | |
367 | if (fn && user_provided_p (fn)) | |
eca7fc57 | 368 | return ctor; |
7b37a0c5 | 369 | else if (TYPE_HAS_COMPLEX_DFLT (type)) |
8f540f06 JM |
370 | { |
371 | /* This is a class that needs constructing, but doesn't have | |
372 | a user-provided constructor. So we need to zero-initialize | |
373 | the object and then call the implicitly defined ctor. | |
450a927a | 374 | This will be handled in simplify_aggr_init_expr. */ |
eca7fc57 | 375 | AGGR_INIT_ZERO_FIRST (ctor) = 1; |
8f540f06 JM |
376 | return ctor; |
377 | } | |
fd97a96a | 378 | } |
eca7fc57 JM |
379 | |
380 | /* Discard any access checking during subobject initialization; | |
381 | the checks are implied by the call to the ctor which we have | |
382 | verified is OK (cpp0x/defaulted46.C). */ | |
383 | push_deferring_access_checks (dk_deferred); | |
384 | tree r = build_value_init_noctor (type, complain); | |
385 | pop_deferring_access_checks (); | |
386 | return r; | |
fd97a96a JM |
387 | } |
388 | ||
389 | /* Like build_value_init, but don't call the constructor for TYPE. Used | |
390 | for base initializers. */ | |
391 | ||
392 | tree | |
309714d4 | 393 | build_value_init_noctor (tree type, tsubst_flags_t complain) |
fd97a96a | 394 | { |
46a76d4b JM |
395 | if (!COMPLETE_TYPE_P (type)) |
396 | { | |
397 | if (complain & tf_error) | |
398 | error ("value-initialization of incomplete type %qT", type); | |
399 | return error_mark_node; | |
400 | } | |
4ddd8a74 JM |
401 | /* FIXME the class and array cases should just use digest_init once it is |
402 | SFINAE-enabled. */ | |
fd97a96a JM |
403 | if (CLASS_TYPE_P (type)) |
404 | { | |
12185846 PC |
405 | gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type) |
406 | || errorcount != 0); | |
fd97a96a JM |
407 | |
408 | if (TREE_CODE (type) != UNION_TYPE) | |
0fcedd9c | 409 | { |
8f540f06 | 410 | tree field; |
9771b263 | 411 | vec<constructor_elt, va_gc> *v = NULL; |
0fcedd9c JM |
412 | |
413 | /* Iterate over the fields, building initializations. */ | |
910ad8de | 414 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
0fcedd9c JM |
415 | { |
416 | tree ftype, value; | |
417 | ||
418 | if (TREE_CODE (field) != FIELD_DECL) | |
419 | continue; | |
420 | ||
421 | ftype = TREE_TYPE (field); | |
422 | ||
a95aef3c PC |
423 | if (ftype == error_mark_node) |
424 | continue; | |
425 | ||
71b6cb2b JJ |
426 | /* Ignore flexible array members for value initialization. */ |
427 | if (TREE_CODE (ftype) == ARRAY_TYPE | |
428 | && !COMPLETE_TYPE_P (ftype) | |
429 | && !TYPE_DOMAIN (ftype) | |
430 | && COMPLETE_TYPE_P (TREE_TYPE (ftype)) | |
0c7bce0a | 431 | && (next_aggregate_field (DECL_CHAIN (field)) |
71b6cb2b JJ |
432 | == NULL_TREE)) |
433 | continue; | |
434 | ||
1ab84eda JJ |
435 | /* Ignore unnamed zero-width bitfields. */ |
436 | if (DECL_UNNAMED_BIT_FIELD (field) | |
437 | && integer_zerop (DECL_SIZE (field))) | |
438 | continue; | |
439 | ||
0fcedd9c JM |
440 | /* We could skip vfields and fields of types with |
441 | user-defined constructors, but I think that won't improve | |
442 | performance at all; it should be simpler in general just | |
443 | to zero out the entire object than try to only zero the | |
444 | bits that actually need it. */ | |
445 | ||
446 | /* Note that for class types there will be FIELD_DECLs | |
447 | corresponding to base classes as well. Thus, iterating | |
448 | over TYPE_FIELDs will result in correct initialization of | |
449 | all of the subobjects. */ | |
309714d4 | 450 | value = build_value_init (ftype, complain); |
c0014b07 | 451 | value = maybe_constant_init (value); |
0fcedd9c | 452 | |
351ccf20 JM |
453 | if (value == error_mark_node) |
454 | return error_mark_node; | |
455 | ||
c0014b07 JM |
456 | CONSTRUCTOR_APPEND_ELT(v, field, value); |
457 | ||
458 | /* We shouldn't have gotten here for anything that would need | |
459 | non-trivial initialization, and gimplify_init_ctor_preeval | |
460 | would need to be fixed to allow it. */ | |
461 | gcc_assert (TREE_CODE (value) != TARGET_EXPR | |
462 | && TREE_CODE (value) != AGGR_INIT_EXPR); | |
0fcedd9c JM |
463 | } |
464 | ||
465 | /* Build a constructor to contain the zero- initializations. */ | |
8f540f06 | 466 | return build_constructor (type, v); |
0fcedd9c JM |
467 | } |
468 | } | |
469 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
470 | { | |
9771b263 | 471 | vec<constructor_elt, va_gc> *v = NULL; |
0fcedd9c JM |
472 | |
473 | /* Iterate over the array elements, building initializations. */ | |
474 | tree max_index = array_type_nelts (type); | |
475 | ||
476 | /* If we have an error_mark here, we should just return error mark | |
477 | as we don't know the size of the array yet. */ | |
478 | if (max_index == error_mark_node) | |
462aa169 | 479 | { |
e2a009c7 JM |
480 | if (complain & tf_error) |
481 | error ("cannot value-initialize array of unknown bound %qT", | |
482 | type); | |
462aa169 JM |
483 | return error_mark_node; |
484 | } | |
0fcedd9c JM |
485 | gcc_assert (TREE_CODE (max_index) == INTEGER_CST); |
486 | ||
487 | /* A zero-sized array, which is accepted as an extension, will | |
488 | have an upper bound of -1. */ | |
489 | if (!tree_int_cst_equal (max_index, integer_minus_one_node)) | |
490 | { | |
f32682ca | 491 | constructor_elt ce; |
0fcedd9c | 492 | |
0fcedd9c JM |
493 | /* If this is a one element array, we just use a regular init. */ |
494 | if (tree_int_cst_equal (size_zero_node, max_index)) | |
f32682ca | 495 | ce.index = size_zero_node; |
0fcedd9c | 496 | else |
f32682ca | 497 | ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, max_index); |
0fcedd9c | 498 | |
f32682ca | 499 | ce.value = build_value_init (TREE_TYPE (type), complain); |
c0014b07 JM |
500 | ce.value = maybe_constant_init (ce.value); |
501 | if (ce.value == error_mark_node) | |
502 | return error_mark_node; | |
9dbd4406 | 503 | |
c0014b07 JM |
504 | vec_alloc (v, 1); |
505 | v->quick_push (ce); | |
351ccf20 | 506 | |
c0014b07 JM |
507 | /* We shouldn't have gotten here for anything that would need |
508 | non-trivial initialization, and gimplify_init_ctor_preeval | |
509 | would need to be fixed to allow it. */ | |
510 | gcc_assert (TREE_CODE (ce.value) != TARGET_EXPR | |
511 | && TREE_CODE (ce.value) != AGGR_INIT_EXPR); | |
0fcedd9c JM |
512 | } |
513 | ||
514 | /* Build a constructor to contain the initializations. */ | |
515 | return build_constructor (type, v); | |
516 | } | |
2b8497cd JM |
517 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
518 | { | |
519 | if (complain & tf_error) | |
520 | error ("value-initialization of function type %qT", type); | |
521 | return error_mark_node; | |
522 | } | |
9f613f06 | 523 | else if (TYPE_REF_P (type)) |
351ccf20 JM |
524 | { |
525 | if (complain & tf_error) | |
526 | error ("value-initialization of reference type %qT", type); | |
527 | return error_mark_node; | |
528 | } | |
0fcedd9c JM |
529 | |
530 | return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false); | |
531 | } | |
532 | ||
0790c8aa MP |
533 | /* Initialize current class with INIT, a TREE_LIST of arguments for |
534 | a target constructor. If TREE_LIST is void_type_node, an empty | |
535 | initializer list was given. Return the target constructor. */ | |
238e471c | 536 | |
0790c8aa | 537 | static tree |
238e471c VV |
538 | perform_target_ctor (tree init) |
539 | { | |
540 | tree decl = current_class_ref; | |
541 | tree type = current_class_type; | |
542 | ||
0790c8aa MP |
543 | init = build_aggr_init (decl, init, LOOKUP_NORMAL|LOOKUP_DELEGATING_CONS, |
544 | tf_warning_or_error); | |
545 | finish_expr_stmt (init); | |
eca7fc57 | 546 | if (type_build_dtor_call (type)) |
238e471c | 547 | { |
04e4997a PC |
548 | tree expr = build_delete (input_location, |
549 | type, decl, sfk_complete_destructor, | |
238e471c VV |
550 | LOOKUP_NORMAL |
551 | |LOOKUP_NONVIRTUAL | |
552 | |LOOKUP_DESTRUCTOR, | |
553 | 0, tf_warning_or_error); | |
32d8ff73 JM |
554 | if (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl)) |
555 | { | |
556 | tree base = build_delete (input_location, | |
557 | type, decl, sfk_base_destructor, | |
558 | LOOKUP_NORMAL | |
559 | |LOOKUP_NONVIRTUAL | |
560 | |LOOKUP_DESTRUCTOR, | |
561 | 0, tf_warning_or_error); | |
562 | expr = build_if_in_charge (expr, base); | |
563 | } | |
eca7fc57 JM |
564 | if (expr != error_mark_node |
565 | && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) | |
238e471c VV |
566 | finish_eh_cleanup (expr); |
567 | } | |
0790c8aa | 568 | return init; |
238e471c VV |
569 | } |
570 | ||
78b3bf0e JM |
571 | /* Instantiate the default member initializer of MEMBER, if needed. |
572 | Only get_nsdmi should use the return value of this function. */ | |
b15ea309 JM |
573 | |
574 | tree | |
78b3bf0e | 575 | maybe_instantiate_nsdmi_init (tree member, tsubst_flags_t complain) |
b15ea309 | 576 | { |
78b3bf0e | 577 | tree init = DECL_INITIAL (member); |
9b40ca25 JM |
578 | |
579 | /* tsubst_decl uses void_node to indicate an uninstantiated DMI. */ | |
580 | if (init == void_node) | |
f4cd9c51 | 581 | { |
9479da45 PP |
582 | /* Clear any special tsubst flags; the result of NSDMI instantiation |
583 | should be independent of the substitution context. */ | |
584 | complain &= tf_warning_or_error; | |
585 | ||
04eca83e | 586 | init = DECL_INITIAL (DECL_TI_TEMPLATE (member)); |
12659e10 | 587 | location_t expr_loc |
6bdfada4 | 588 | = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (member)); |
7b49e3da | 589 | if (TREE_CODE (init) == DEFERRED_PARSE) |
9fb82e65 | 590 | /* Unparsed. */; |
04eca83e | 591 | /* Check recursive instantiation. */ |
9fb82e65 | 592 | else if (DECL_INSTANTIATING_NSDMI_P (member)) |
04eca83e | 593 | { |
9fb82e65 | 594 | if (complain & tf_error) |
12659e10 JM |
595 | error_at (expr_loc, "recursive instantiation of default member " |
596 | "initializer for %qD", member); | |
04eca83e NS |
597 | init = error_mark_node; |
598 | } | |
599 | else | |
600 | { | |
16e723e6 | 601 | cp_evaluated ev; |
12659e10 JM |
602 | |
603 | location_t sloc = input_location; | |
604 | input_location = expr_loc; | |
605 | ||
04eca83e | 606 | DECL_INSTANTIATING_NSDMI_P (member) = 1; |
9fb82e65 | 607 | |
298434c9 | 608 | bool pushed = false; |
58df5350 | 609 | tree ctx = type_context_for_name_lookup (member); |
6173f713 | 610 | |
07c52d1e | 611 | bool push_to_top = maybe_push_to_top_level (member); |
6173f713 | 612 | if (!currently_open_class (ctx)) |
298434c9 | 613 | { |
82d5dece | 614 | push_nested_class (ctx); |
de03b82f | 615 | push_deferring_access_checks (dk_no_deferred); |
298434c9 JM |
616 | pushed = true; |
617 | } | |
618 | ||
82d5dece | 619 | inject_this_parameter (ctx, TYPE_UNQUALIFIED); |
12659e10 | 620 | |
f44a8dd5 JM |
621 | start_lambda_scope (member); |
622 | ||
04eca83e | 623 | /* Do deferred instantiation of the NSDMI. */ |
c29d5523 | 624 | init = tsubst_expr (init, DECL_TI_ARGS (member), complain, member); |
9fb82e65 | 625 | init = digest_nsdmi_init (member, init, complain); |
f44a8dd5 JM |
626 | |
627 | finish_lambda_scope (); | |
628 | ||
04eca83e | 629 | DECL_INSTANTIATING_NSDMI_P (member) = 0; |
12659e10 JM |
630 | |
631 | if (init != error_mark_node) | |
9b40ca25 | 632 | DECL_INITIAL (member) = init; |
12659e10 | 633 | |
298434c9 JM |
634 | if (pushed) |
635 | { | |
de03b82f | 636 | pop_deferring_access_checks (); |
298434c9 | 637 | pop_nested_class (); |
298434c9 | 638 | } |
07c52d1e | 639 | maybe_pop_from_top_level (push_to_top); |
298434c9 | 640 | |
12659e10 | 641 | input_location = sloc; |
04eca83e | 642 | } |
f4cd9c51 | 643 | } |
78b3bf0e JM |
644 | |
645 | return init; | |
646 | } | |
647 | ||
648 | /* Return the non-static data initializer for FIELD_DECL MEMBER. */ | |
649 | ||
650 | tree | |
651 | get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain) | |
652 | { | |
653 | tree save_ccp = current_class_ptr; | |
654 | tree save_ccr = current_class_ref; | |
655 | ||
656 | tree init = maybe_instantiate_nsdmi_init (member, complain); | |
9fb82e65 | 657 | |
7b49e3da | 658 | if (init && TREE_CODE (init) == DEFERRED_PARSE) |
b15ea309 | 659 | { |
9fb82e65 | 660 | if (complain & tf_error) |
b15ea309 | 661 | { |
9fb82e65 JM |
662 | error ("default member initializer for %qD required before the end " |
663 | "of its enclosing class", member); | |
664 | inform (location_of (init), "defined here"); | |
b15ea309 | 665 | DECL_INITIAL (member) = error_mark_node; |
b15ea309 | 666 | } |
9fb82e65 | 667 | init = error_mark_node; |
b15ea309 | 668 | } |
9fb82e65 | 669 | |
12659e10 JM |
670 | if (in_ctor) |
671 | { | |
672 | current_class_ptr = save_ccp; | |
673 | current_class_ref = save_ccr; | |
674 | } | |
675 | else | |
676 | { | |
677 | /* Use a PLACEHOLDER_EXPR when we don't have a 'this' parameter to | |
678 | refer to; constexpr evaluation knows what to do with it. */ | |
679 | current_class_ref = build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (member)); | |
680 | current_class_ptr = build_address (current_class_ref); | |
681 | } | |
682 | ||
cf59c898 PP |
683 | /* Clear processing_template_decl for sake of break_out_target_exprs; |
684 | INIT is always non-templated. */ | |
685 | processing_template_decl_sentinel ptds; | |
686 | ||
9fb82e65 JM |
687 | /* Strip redundant TARGET_EXPR so we don't need to remap it, and |
688 | so the aggregate init code below will see a CONSTRUCTOR. */ | |
689 | bool simple_target = (init && SIMPLE_TARGET_EXPR_P (init)); | |
690 | if (simple_target) | |
691 | init = TARGET_EXPR_INITIAL (init); | |
45d14461 | 692 | init = break_out_target_exprs (init, /*loc*/true); |
f8c1f29a MP |
693 | if (init && TREE_CODE (init) == TARGET_EXPR) |
694 | /* In a constructor, this expresses the full initialization, prevent | |
695 | perform_member_init from calling another constructor (58162). */ | |
696 | TARGET_EXPR_DIRECT_INIT_P (init) = in_ctor; | |
9fb82e65 JM |
697 | if (simple_target && TREE_CODE (init) != CONSTRUCTOR) |
698 | /* Now put it back so C++17 copy elision works. */ | |
699 | init = get_target_expr (init); | |
700 | ||
6ffbf87c JM |
701 | set_target_expr_eliding (init); |
702 | ||
b15ea309 JM |
703 | current_class_ptr = save_ccp; |
704 | current_class_ref = save_ccr; | |
705 | return init; | |
706 | } | |
707 | ||
945c17d8 MS |
708 | /* Diagnose the flexible array MEMBER if its INITializer is non-null |
709 | and return true if so. Otherwise return false. */ | |
710 | ||
a232a1cb | 711 | bool |
945c17d8 MS |
712 | maybe_reject_flexarray_init (tree member, tree init) |
713 | { | |
714 | tree type = TREE_TYPE (member); | |
715 | ||
716 | if (!init | |
717 | || TREE_CODE (type) != ARRAY_TYPE | |
718 | || TYPE_DOMAIN (type)) | |
719 | return false; | |
720 | ||
721 | /* Point at the flexible array member declaration if it's initialized | |
722 | in-class, and at the ctor if it's initialized in a ctor member | |
723 | initializer list. */ | |
724 | location_t loc; | |
725 | if (DECL_INITIAL (member) == init | |
a232a1cb | 726 | || !current_function_decl |
945c17d8 MS |
727 | || DECL_DEFAULTED_FN (current_function_decl)) |
728 | loc = DECL_SOURCE_LOCATION (member); | |
729 | else | |
730 | loc = DECL_SOURCE_LOCATION (current_function_decl); | |
731 | ||
732 | error_at (loc, "initializer for flexible array member %q#D", member); | |
733 | return true; | |
734 | } | |
735 | ||
04eb9c55 JM |
736 | /* If INIT's value can come from a call to std::initializer_list<T>::begin, |
737 | return that function. Otherwise, NULL_TREE. */ | |
738 | ||
739 | static tree | |
740 | find_list_begin (tree init) | |
741 | { | |
742 | STRIP_NOPS (init); | |
743 | while (TREE_CODE (init) == COMPOUND_EXPR) | |
744 | init = TREE_OPERAND (init, 1); | |
745 | STRIP_NOPS (init); | |
746 | if (TREE_CODE (init) == COND_EXPR) | |
747 | { | |
748 | tree left = TREE_OPERAND (init, 1); | |
749 | if (!left) | |
750 | left = TREE_OPERAND (init, 0); | |
751 | left = find_list_begin (left); | |
752 | if (left) | |
753 | return left; | |
754 | return find_list_begin (TREE_OPERAND (init, 2)); | |
755 | } | |
756 | if (TREE_CODE (init) == CALL_EXPR) | |
757 | if (tree fn = get_callee_fndecl (init)) | |
758 | if (id_equal (DECL_NAME (fn), "begin") | |
759 | && is_std_init_list (DECL_CONTEXT (fn))) | |
760 | return fn; | |
761 | return NULL_TREE; | |
762 | } | |
763 | ||
764 | /* If INIT initializing MEMBER is copying the address of the underlying array | |
765 | of an initializer_list, warn. */ | |
766 | ||
767 | static void | |
768 | maybe_warn_list_ctor (tree member, tree init) | |
769 | { | |
770 | tree memtype = TREE_TYPE (member); | |
771 | if (!init || !TYPE_PTR_P (memtype) | |
772 | || !is_list_ctor (current_function_decl)) | |
773 | return; | |
774 | ||
9b239d05 JW |
775 | tree parm = FUNCTION_FIRST_USER_PARMTYPE (current_function_decl); |
776 | parm = TREE_VALUE (parm); | |
777 | tree initlist = non_reference (parm); | |
778 | ||
779 | /* Do not warn if the parameter is an lvalue reference to non-const. */ | |
780 | if (TYPE_REF_P (parm) && !TYPE_REF_IS_RVALUE (parm) | |
781 | && !CP_TYPE_CONST_P (initlist)) | |
782 | return; | |
783 | ||
04eb9c55 JM |
784 | tree targs = CLASSTYPE_TI_ARGS (initlist); |
785 | tree elttype = TREE_VEC_ELT (targs, 0); | |
786 | ||
787 | if (!same_type_ignoring_top_level_qualifiers_p | |
788 | (TREE_TYPE (memtype), elttype)) | |
789 | return; | |
790 | ||
791 | tree begin = find_list_begin (init); | |
792 | if (!begin) | |
793 | return; | |
794 | ||
f9d0ca40 | 795 | location_t loc = cp_expr_loc_or_input_loc (init); |
04eb9c55 JM |
796 | warning_at (loc, OPT_Winit_list_lifetime, |
797 | "initializing %qD from %qE does not extend the lifetime " | |
798 | "of the underlying array", member, begin); | |
799 | } | |
800 | ||
0790c8aa MP |
801 | /* Data structure for find_uninit_fields_r, below. */ |
802 | ||
803 | struct find_uninit_data { | |
804 | /* The set tracking the yet-uninitialized members. */ | |
805 | hash_set<tree> *uninitialized; | |
806 | /* The data member we are currently initializing. It can be either | |
807 | a type (initializing a base class/delegating constructors), or | |
808 | a COMPONENT_REF. */ | |
809 | tree member; | |
810 | }; | |
811 | ||
812 | /* walk_tree callback that warns about using uninitialized data in | |
813 | a member-initializer-list. */ | |
814 | ||
815 | static tree | |
816 | find_uninit_fields_r (tree *tp, int *walk_subtrees, void *data) | |
817 | { | |
818 | find_uninit_data *d = static_cast<find_uninit_data *>(data); | |
819 | hash_set<tree> *uninitialized = d->uninitialized; | |
820 | tree init = *tp; | |
821 | const tree_code code = TREE_CODE (init); | |
822 | ||
823 | /* No need to look into types or unevaluated operands. */ | |
824 | if (TYPE_P (init) || unevaluated_p (code)) | |
825 | { | |
826 | *walk_subtrees = false; | |
827 | return NULL_TREE; | |
828 | } | |
829 | ||
830 | switch (code) | |
831 | { | |
832 | /* We'd need data flow info to avoid false positives. */ | |
833 | case COND_EXPR: | |
834 | case VEC_COND_EXPR: | |
835 | case BIND_EXPR: | |
836 | /* We might see a MODIFY_EXPR in cases like S() : a((b = 42)), c(b) { } | |
837 | where the initializer for 'a' surreptitiously initializes 'b'. Let's | |
838 | not bother with these complicated scenarios in the front end. */ | |
839 | case MODIFY_EXPR: | |
840 | /* Don't attempt to handle statement-expressions, either. */ | |
841 | case STATEMENT_LIST: | |
842 | uninitialized->empty (); | |
843 | gcc_fallthrough (); | |
844 | /* If we're just taking the address of an object, it doesn't matter | |
845 | whether it's been initialized. */ | |
846 | case ADDR_EXPR: | |
847 | *walk_subtrees = false; | |
848 | return NULL_TREE; | |
849 | default: | |
850 | break; | |
851 | } | |
852 | ||
853 | /* We'd need data flow info to avoid false positives. */ | |
854 | if (truth_value_p (code)) | |
855 | goto give_up; | |
856 | /* Attempt to handle a simple a{b}, but no more. */ | |
857 | else if (BRACE_ENCLOSED_INITIALIZER_P (init)) | |
858 | { | |
859 | if (CONSTRUCTOR_NELTS (init) == 1 | |
860 | && !BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (init, 0)->value)) | |
861 | init = CONSTRUCTOR_ELT (init, 0)->value; | |
862 | else | |
863 | goto give_up; | |
864 | } | |
865 | /* Warn about uninitialized 'this'. */ | |
866 | else if (code == CALL_EXPR) | |
867 | { | |
868 | tree fn = get_callee_fndecl (init); | |
f9fbf93d | 869 | if (fn && DECL_IOBJ_MEMBER_FUNCTION_P (fn)) |
0790c8aa MP |
870 | { |
871 | tree op = CALL_EXPR_ARG (init, 0); | |
872 | if (TREE_CODE (op) == ADDR_EXPR) | |
873 | op = TREE_OPERAND (op, 0); | |
874 | temp_override<tree> ovr (d->member, DECL_ARGUMENTS (fn)); | |
875 | cp_walk_tree_without_duplicates (&op, find_uninit_fields_r, data); | |
876 | } | |
877 | /* Functions (whether static or nonstatic member) may have side effects | |
878 | and initialize other members; it's not the front end's job to try to | |
879 | figure it out. But don't give up for constructors: we still want to | |
880 | warn when initializing base classes: | |
881 | ||
882 | struct D : public B { | |
883 | int x; | |
884 | D() : B(x) {} | |
885 | }; | |
886 | ||
887 | so carry on to detect that 'x' is used uninitialized. */ | |
888 | if (!fn || !DECL_CONSTRUCTOR_P (fn)) | |
889 | goto give_up; | |
890 | } | |
891 | ||
892 | /* If we find FIELD in the uninitialized set, we warn. */ | |
893 | if (code == COMPONENT_REF) | |
894 | { | |
895 | tree field = TREE_OPERAND (init, 1); | |
896 | tree type = TYPE_P (d->member) ? d->member : TREE_TYPE (d->member); | |
897 | ||
898 | /* We're initializing a reference member with itself. */ | |
899 | if (TYPE_REF_P (type) && cp_tree_equal (d->member, init)) | |
900 | warning_at (EXPR_LOCATION (init), OPT_Winit_self, | |
901 | "%qD is initialized with itself", field); | |
902 | else if (cp_tree_equal (TREE_OPERAND (init, 0), current_class_ref) | |
903 | && uninitialized->contains (field)) | |
904 | { | |
905 | if (TYPE_REF_P (TREE_TYPE (field))) | |
906 | warning_at (EXPR_LOCATION (init), OPT_Wuninitialized, | |
907 | "reference %qD is not yet bound to a value when used " | |
908 | "here", field); | |
b83f3cd3 MP |
909 | else if ((!INDIRECT_TYPE_P (type) || is_this_parameter (d->member)) |
910 | && !conv_binds_to_reference_parm_p (type, init)) | |
0790c8aa MP |
911 | warning_at (EXPR_LOCATION (init), OPT_Wuninitialized, |
912 | "member %qD is used uninitialized", field); | |
913 | *walk_subtrees = false; | |
914 | } | |
915 | } | |
916 | ||
917 | return NULL_TREE; | |
918 | ||
919 | give_up: | |
920 | *walk_subtrees = false; | |
921 | uninitialized->empty (); | |
922 | return integer_zero_node; | |
923 | } | |
924 | ||
925 | /* Wrapper around find_uninit_fields_r above. */ | |
926 | ||
927 | static void | |
928 | find_uninit_fields (tree *t, hash_set<tree> *uninitialized, tree member) | |
929 | { | |
930 | if (!uninitialized->is_empty ()) | |
931 | { | |
932 | find_uninit_data data = { uninitialized, member }; | |
933 | cp_walk_tree_without_duplicates (t, find_uninit_fields_r, &data); | |
934 | } | |
935 | } | |
936 | ||
d1d8fd28 MP |
937 | /* Return true if it's OK to initialize an array TYPE from INIT. Mere mortals |
938 | can't copy arrays, but the compiler can do so with a VEC_INIT_EXPR in | |
939 | certain cases. */ | |
940 | ||
941 | static bool | |
942 | can_init_array_with_p (tree type, tree init) | |
943 | { | |
944 | if (!init) | |
945 | /* Value-init, OK. */ | |
946 | return true; | |
947 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))) | |
948 | return false; | |
949 | /* We're called from synthesize_method, and we're processing the | |
950 | mem-initializers of a constructor. */ | |
951 | if (DECL_DEFAULTED_FN (current_function_decl)) | |
952 | return true; | |
d1d8fd28 MP |
953 | if (TREE_CODE (init) == TARGET_EXPR) |
954 | { | |
955 | init = TARGET_EXPR_INITIAL (init); | |
de0886d4 | 956 | /* As an extension, we allow copying from a compound literal. */ |
d1d8fd28 MP |
957 | if (TREE_CODE (init) == CONSTRUCTOR) |
958 | return CONSTRUCTOR_C99_COMPOUND_LITERAL (init); | |
de0886d4 MP |
959 | /* VEC_INIT_EXPR is used for non-constant initialization of trailing |
960 | elements with no explicit initializers. */ | |
961 | else if (TREE_CODE (init) == VEC_INIT_EXPR) | |
962 | return true; | |
d1d8fd28 MP |
963 | } |
964 | ||
965 | return false; | |
966 | } | |
967 | ||
2282d28d MM |
968 | /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of |
969 | arguments. If TREE_LIST is void_type_node, an empty initializer | |
0790c8aa MP |
970 | list was given; if NULL_TREE no initializer was given. UNINITIALIZED |
971 | is the hash set that tracks uninitialized fields. */ | |
e92cc029 | 972 | |
8d08fdba | 973 | static void |
0790c8aa | 974 | perform_member_init (tree member, tree init, hash_set<tree> &uninitialized) |
8d08fdba MS |
975 | { |
976 | tree decl; | |
977 | tree type = TREE_TYPE (member); | |
2282d28d | 978 | |
0e5f8a59 JM |
979 | /* Use the non-static data member initializer if there was no |
980 | mem-initializer for this field. */ | |
981 | if (init == NULL_TREE) | |
9fb82e65 | 982 | init = get_nsdmi (member, /*ctor*/true, tf_warning_or_error); |
0e5f8a59 | 983 | |
a9eba00e PC |
984 | if (init == error_mark_node) |
985 | return; | |
986 | ||
2282d28d MM |
987 | /* Effective C++ rule 12 requires that all data members be |
988 | initialized. */ | |
9dbd4406 | 989 | if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE) |
c5d75364 MLI |
990 | warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__, |
991 | "%qD should be initialized in the member initialization list", | |
992 | member); | |
2282d28d | 993 | |
2282d28d | 994 | /* Get an lvalue for the data member. */ |
50ad9642 MM |
995 | decl = build_class_member_access_expr (current_class_ref, member, |
996 | /*access_path=*/NULL_TREE, | |
5ade1ed2 DG |
997 | /*preserve_reference=*/true, |
998 | tf_warning_or_error); | |
2fbfe9b8 MS |
999 | if (decl == error_mark_node) |
1000 | return; | |
1001 | ||
efaaae49 | 1002 | if ((warn_init_self || warn_uninitialized || warn_self_move) |
0790c8aa MP |
1003 | && init |
1004 | && TREE_CODE (init) == TREE_LIST | |
c11e39b0 JW |
1005 | && TREE_CHAIN (init) == NULL_TREE) |
1006 | { | |
1007 | tree val = TREE_VALUE (init); | |
0aa359c1 PC |
1008 | /* Handle references. */ |
1009 | if (REFERENCE_REF_P (val)) | |
1010 | val = TREE_OPERAND (val, 0); | |
c11e39b0 JW |
1011 | if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member |
1012 | && TREE_OPERAND (val, 0) == current_class_ref) | |
1013 | warning_at (DECL_SOURCE_LOCATION (current_function_decl), | |
0ccb505d | 1014 | OPT_Winit_self, "%qD is initialized with itself", |
c11e39b0 | 1015 | member); |
efaaae49 MP |
1016 | else if (!maybe_warn_self_move (input_location, member, |
1017 | TREE_VALUE (init))) | |
0790c8aa | 1018 | find_uninit_fields (&val, &uninitialized, decl); |
c11e39b0 JW |
1019 | } |
1020 | ||
59dbb04d JM |
1021 | if (array_of_unknown_bound_p (type)) |
1022 | { | |
1023 | maybe_reject_flexarray_init (member, init); | |
1024 | return; | |
1025 | } | |
a1c9c9ff | 1026 | |
753b4679 MP |
1027 | if (init && TREE_CODE (init) == TREE_LIST) |
1028 | { | |
1029 | /* A(): a{e} */ | |
1030 | if (DIRECT_LIST_INIT_P (TREE_VALUE (init))) | |
1031 | init = build_x_compound_expr_from_list (init, ELK_MEM_INIT, | |
1032 | tf_warning_or_error); | |
1033 | /* We are trying to initialize an array from a ()-list. If we | |
1034 | should attempt to do so, conjure up a CONSTRUCTOR. */ | |
1035 | else if (TREE_CODE (type) == ARRAY_TYPE | |
1036 | /* P0960 is a C++20 feature. */ | |
1037 | && cxx_dialect >= cxx20) | |
1038 | init = do_aggregate_paren_init (init, type); | |
1039 | else if (!CLASS_TYPE_P (type)) | |
1040 | init = build_x_compound_expr_from_list (init, ELK_MEM_INIT, | |
1041 | tf_warning_or_error); | |
1042 | /* If we're initializing a class from a ()-list, leave the TREE_LIST | |
1043 | alone: we might call an appropriate constructor, or (in C++20) | |
1044 | do aggregate-initialization. */ | |
1045 | } | |
a1c9c9ff | 1046 | |
0790c8aa MP |
1047 | /* Assume we are initializing the member. */ |
1048 | bool member_initialized_p = true; | |
1049 | ||
9dbd4406 JM |
1050 | if (init == void_type_node) |
1051 | { | |
1052 | /* mem() means value-initialization. */ | |
1053 | if (TREE_CODE (type) == ARRAY_TYPE) | |
0f737a30 | 1054 | { |
9c69dcea | 1055 | init = build_vec_init_expr (type, init, tf_warning_or_error); |
6ffbf87c | 1056 | init = cp_build_init_expr (decl, init); |
0f737a30 JJ |
1057 | finish_expr_stmt (init); |
1058 | } | |
9dbd4406 JM |
1059 | else |
1060 | { | |
48e5d119 PC |
1061 | tree value = build_value_init (type, tf_warning_or_error); |
1062 | if (value == error_mark_node) | |
1063 | return; | |
6ffbf87c | 1064 | init = cp_build_init_expr (decl, value); |
351ccf20 | 1065 | finish_expr_stmt (init); |
9dbd4406 | 1066 | } |
9dbd4406 | 1067 | } |
6bdb8141 JM |
1068 | /* Deal with this here, as we will get confused if we try to call the |
1069 | assignment op for an anonymous union. This can happen in a | |
1070 | synthesized copy constructor. */ | |
9dbd4406 | 1071 | else if (ANON_AGGR_TYPE_P (type)) |
6bdb8141 | 1072 | { |
ff9f1a5d MM |
1073 | if (init) |
1074 | { | |
6ffbf87c | 1075 | init = cp_build_init_expr (decl, TREE_VALUE (init)); |
ff9f1a5d MM |
1076 | finish_expr_stmt (init); |
1077 | } | |
6bdb8141 | 1078 | } |
e2df21bf | 1079 | else if (init |
9f613f06 | 1080 | && (TYPE_REF_P (type) |
a1c9c9ff | 1081 | || (TREE_CODE (init) == CONSTRUCTOR |
e2df21bf JM |
1082 | && (CP_AGGREGATE_TYPE_P (type) |
1083 | || is_std_init_list (type))))) | |
1084 | { | |
1085 | /* With references and list-initialization, we need to deal with | |
1086 | extending temporary lifetimes. 12.2p5: "A temporary bound to a | |
1087 | reference member in a constructorâs ctor-initializer (12.6.2) | |
1088 | persists until the constructor exits." */ | |
1089 | unsigned i; tree t; | |
cd9cf97b | 1090 | releasing_vec cleanups; |
a1c9c9ff | 1091 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type)) |
4794d4b5 JM |
1092 | { |
1093 | if (BRACE_ENCLOSED_INITIALIZER_P (init) | |
1094 | && CP_AGGREGATE_TYPE_P (type)) | |
1095 | init = reshape_init (type, init, tf_warning_or_error); | |
1096 | init = digest_init (type, init, tf_warning_or_error); | |
1097 | } | |
e2df21bf JM |
1098 | if (init == error_mark_node) |
1099 | return; | |
a4dfd0f0 | 1100 | if (is_empty_field (member) |
6876b269 JM |
1101 | && !TREE_SIDE_EFFECTS (init)) |
1102 | /* Don't add trivial initialization of an empty base/field, as they | |
1103 | might not be ordered the way the back-end expects. */ | |
1104 | return; | |
f3fae478 JM |
1105 | /* A FIELD_DECL doesn't really have a suitable lifetime, but |
1106 | make_temporary_var_for_ref_to_temp will treat it as automatic and | |
1107 | set_up_extended_ref_temp wants to use the decl in a warning. */ | |
2c6f7927 | 1108 | init = extend_ref_init_temps (member, init, &cleanups); |
e2df21bf JM |
1109 | if (TREE_CODE (type) == ARRAY_TYPE |
1110 | && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type))) | |
1111 | init = build_vec_init_expr (type, init, tf_warning_or_error); | |
6ffbf87c | 1112 | init = cp_build_init_expr (decl, init); |
e2df21bf | 1113 | finish_expr_stmt (init); |
9771b263 | 1114 | FOR_EACH_VEC_ELT (*cleanups, i, t) |
ad8161e6 | 1115 | push_cleanup (NULL_TREE, t, false); |
e2df21bf | 1116 | } |
a0348261 JM |
1117 | else if (type_build_ctor_call (type) |
1118 | || (init && CLASS_TYPE_P (strip_array_types (type)))) | |
8d08fdba | 1119 | { |
534ecb17 | 1120 | if (TREE_CODE (type) == ARRAY_TYPE) |
8d08fdba | 1121 | { |
d1d8fd28 | 1122 | if (can_init_array_with_p (type, init)) |
534ecb17 | 1123 | { |
7e9a3ad3 MS |
1124 | if (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type))) |
1125 | { | |
1126 | /* Initialize the array only if it's not a flexible | |
1127 | array member (i.e., if it has an upper bound). */ | |
1128 | init = build_vec_init_expr (type, init, tf_warning_or_error); | |
6ffbf87c | 1129 | init = cp_build_init_expr (decl, init); |
7e9a3ad3 MS |
1130 | finish_expr_stmt (init); |
1131 | } | |
534ecb17 JM |
1132 | } |
1133 | else | |
1134 | error ("invalid initializer for array member %q#D", member); | |
8d08fdba MS |
1135 | } |
1136 | else | |
b87d79e6 | 1137 | { |
b8bf6ad9 JM |
1138 | int flags = LOOKUP_NORMAL; |
1139 | if (DECL_DEFAULTED_FN (current_function_decl)) | |
1140 | flags |= LOOKUP_DEFAULTED; | |
b87d79e6 JM |
1141 | if (CP_TYPE_CONST_P (type) |
1142 | && init == NULL_TREE | |
6132bdd7 | 1143 | && default_init_uninitialized_part (type)) |
0df9da03 FC |
1144 | { |
1145 | /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a | |
1146 | vtable; still give this diagnostic. */ | |
097f82ec | 1147 | auto_diagnostic_group d; |
0df9da03 FC |
1148 | if (permerror (DECL_SOURCE_LOCATION (current_function_decl), |
1149 | "uninitialized const member in %q#T", type)) | |
1150 | inform (DECL_SOURCE_LOCATION (member), | |
1151 | "%q#D should be initialized", member ); | |
1152 | } | |
b8bf6ad9 | 1153 | finish_expr_stmt (build_aggr_init (decl, init, flags, |
b87d79e6 JM |
1154 | tf_warning_or_error)); |
1155 | } | |
8d08fdba MS |
1156 | } |
1157 | else | |
1158 | { | |
1159 | if (init == NULL_TREE) | |
1160 | { | |
31d1acec | 1161 | tree core_type; |
8d08fdba | 1162 | /* member traversal: note it leaves init NULL */ |
9f613f06 | 1163 | if (TYPE_REF_P (type)) |
0df9da03 | 1164 | { |
097f82ec | 1165 | auto_diagnostic_group d; |
0df9da03 FC |
1166 | if (permerror (DECL_SOURCE_LOCATION (current_function_decl), |
1167 | "uninitialized reference member in %q#T", type)) | |
1168 | inform (DECL_SOURCE_LOCATION (member), | |
1169 | "%q#D should be initialized", member); | |
1170 | } | |
58ec3cc5 | 1171 | else if (CP_TYPE_CONST_P (type)) |
0df9da03 | 1172 | { |
097f82ec | 1173 | auto_diagnostic_group d; |
0df9da03 FC |
1174 | if (permerror (DECL_SOURCE_LOCATION (current_function_decl), |
1175 | "uninitialized const member in %q#T", type)) | |
1176 | inform (DECL_SOURCE_LOCATION (member), | |
1177 | "%q#D should be initialized", member ); | |
1178 | } | |
31d1acec | 1179 | |
69f36ba6 JM |
1180 | core_type = strip_array_types (type); |
1181 | ||
012e6a1e JM |
1182 | if (CLASS_TYPE_P (core_type) |
1183 | && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type) | |
1184 | || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))) | |
1185 | diagnose_uninitialized_cst_or_ref_member (core_type, | |
40bb78ad FC |
1186 | /*using_new=*/false, |
1187 | /*complain=*/true); | |
0790c8aa MP |
1188 | |
1189 | /* We left the member uninitialized. */ | |
1190 | member_initialized_p = false; | |
8d08fdba | 1191 | } |
8d08fdba | 1192 | |
04eb9c55 JM |
1193 | maybe_warn_list_ctor (member, init); |
1194 | ||
a1c9c9ff | 1195 | if (init) |
4f2e1536 MP |
1196 | finish_expr_stmt (cp_build_modify_expr (input_location, decl, |
1197 | INIT_EXPR, init, | |
5ade1ed2 | 1198 | tf_warning_or_error)); |
8d08fdba | 1199 | } |
eb66be0e | 1200 | |
0790c8aa MP |
1201 | if (member_initialized_p && warn_uninitialized) |
1202 | /* This member is now initialized, remove it from the uninitialized | |
1203 | set. */ | |
1204 | uninitialized.remove (member); | |
1205 | ||
eca7fc57 | 1206 | if (type_build_dtor_call (type)) |
b7484fbe | 1207 | { |
de22184b MS |
1208 | tree expr; |
1209 | ||
50ad9642 MM |
1210 | expr = build_class_member_access_expr (current_class_ref, member, |
1211 | /*access_path=*/NULL_TREE, | |
5ade1ed2 DG |
1212 | /*preserve_reference=*/false, |
1213 | tf_warning_or_error); | |
04e4997a PC |
1214 | expr = build_delete (input_location, |
1215 | type, expr, sfk_complete_destructor, | |
574cfaa4 JM |
1216 | LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0, |
1217 | tf_warning_or_error); | |
b7484fbe | 1218 | |
eca7fc57 JM |
1219 | if (expr != error_mark_node |
1220 | && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) | |
659e5a7a | 1221 | finish_eh_cleanup (expr); |
b7484fbe | 1222 | } |
8d08fdba MS |
1223 | } |
1224 | ||
ff9f1a5d MM |
1225 | /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all |
1226 | the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */ | |
1227 | ||
c8094d83 | 1228 | static tree |
a81072c7 | 1229 | build_field_list (tree t, tree list, int *uses_unions_or_anon_p) |
ff9f1a5d MM |
1230 | { |
1231 | tree fields; | |
1232 | ||
1233 | /* Note whether or not T is a union. */ | |
1234 | if (TREE_CODE (t) == UNION_TYPE) | |
a81072c7 | 1235 | *uses_unions_or_anon_p = 1; |
ff9f1a5d | 1236 | |
910ad8de | 1237 | for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields)) |
ff9f1a5d | 1238 | { |
535335bf JM |
1239 | tree fieldtype; |
1240 | ||
ff9f1a5d | 1241 | /* Skip CONST_DECLs for enumeration constants and so forth. */ |
17bbb839 | 1242 | if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields)) |
ff9f1a5d | 1243 | continue; |
c8094d83 | 1244 | |
535335bf | 1245 | fieldtype = TREE_TYPE (fields); |
ff9f1a5d MM |
1246 | |
1247 | /* For an anonymous struct or union, we must recursively | |
1248 | consider the fields of the anonymous type. They can be | |
1249 | directly initialized from the constructor. */ | |
535335bf | 1250 | if (ANON_AGGR_TYPE_P (fieldtype)) |
ff9f1a5d MM |
1251 | { |
1252 | /* Add this field itself. Synthesized copy constructors | |
1253 | initialize the entire aggregate. */ | |
1254 | list = tree_cons (fields, NULL_TREE, list); | |
1255 | /* And now add the fields in the anonymous aggregate. */ | |
a81072c7 JM |
1256 | list = build_field_list (fieldtype, list, uses_unions_or_anon_p); |
1257 | *uses_unions_or_anon_p = 1; | |
ff9f1a5d MM |
1258 | } |
1259 | /* Add this field. */ | |
1260 | else if (DECL_NAME (fields)) | |
1261 | list = tree_cons (fields, NULL_TREE, list); | |
1262 | } | |
1263 | ||
1264 | return list; | |
1265 | } | |
1266 | ||
a81072c7 JM |
1267 | /* Return the innermost aggregate scope for FIELD, whether that is |
1268 | the enclosing class or an anonymous aggregate within it. */ | |
1269 | ||
1270 | static tree | |
1271 | innermost_aggr_scope (tree field) | |
1272 | { | |
1273 | if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
1274 | return TREE_TYPE (field); | |
1275 | else | |
1276 | return DECL_CONTEXT (field); | |
1277 | } | |
1278 | ||
2282d28d MM |
1279 | /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives |
1280 | a FIELD_DECL or BINFO in T that needs initialization. The | |
1281 | TREE_VALUE gives the initializer, or list of initializer arguments. | |
1282 | ||
1283 | Return a TREE_LIST containing all of the initializations required | |
1284 | for T, in the order in which they should be performed. The output | |
1285 | list has the same format as the input. */ | |
e92cc029 | 1286 | |
8d08fdba | 1287 | static tree |
2282d28d | 1288 | sort_mem_initializers (tree t, tree mem_inits) |
8d08fdba | 1289 | { |
ff9f1a5d | 1290 | tree init; |
fa743e8c | 1291 | tree base, binfo, base_binfo; |
2282d28d MM |
1292 | tree sorted_inits; |
1293 | tree next_subobject; | |
9771b263 | 1294 | vec<tree, va_gc> *vbases; |
2282d28d | 1295 | int i; |
a81072c7 | 1296 | int uses_unions_or_anon_p = 0; |
ff9f1a5d | 1297 | |
2282d28d MM |
1298 | /* Build up a list of initializations. The TREE_PURPOSE of entry |
1299 | will be the subobject (a FIELD_DECL or BINFO) to initialize. The | |
1300 | TREE_VALUE will be the constructor arguments, or NULL if no | |
1301 | explicit initialization was provided. */ | |
1302 | sorted_inits = NULL_TREE; | |
c8094d83 | 1303 | |
2282d28d | 1304 | /* Process the virtual bases. */ |
9ba5ff0f | 1305 | for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; |
9771b263 | 1306 | vec_safe_iterate (vbases, i, &base); i++) |
58c42dc2 | 1307 | sorted_inits = tree_cons (base, NULL_TREE, sorted_inits); |
c8094d83 | 1308 | |
2282d28d | 1309 | /* Process the direct bases. */ |
fa743e8c NS |
1310 | for (binfo = TYPE_BINFO (t), i = 0; |
1311 | BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) | |
1312 | if (!BINFO_VIRTUAL_P (base_binfo)) | |
1313 | sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits); | |
1314 | ||
2282d28d | 1315 | /* Process the non-static data members. */ |
a81072c7 | 1316 | sorted_inits = build_field_list (t, sorted_inits, &uses_unions_or_anon_p); |
2282d28d MM |
1317 | /* Reverse the entire list of initializations, so that they are in |
1318 | the order that they will actually be performed. */ | |
1319 | sorted_inits = nreverse (sorted_inits); | |
1320 | ||
1321 | /* If the user presented the initializers in an order different from | |
1322 | that in which they will actually occur, we issue a warning. Keep | |
1323 | track of the next subobject which can be explicitly initialized | |
1324 | without issuing a warning. */ | |
1325 | next_subobject = sorted_inits; | |
1326 | ||
1327 | /* Go through the explicit initializers, filling in TREE_PURPOSE in | |
1328 | the SORTED_INITS. */ | |
1329 | for (init = mem_inits; init; init = TREE_CHAIN (init)) | |
1330 | { | |
1331 | tree subobject; | |
1332 | tree subobject_init; | |
1333 | ||
1334 | subobject = TREE_PURPOSE (init); | |
1335 | ||
1336 | /* If the explicit initializers are in sorted order, then | |
c8094d83 | 1337 | SUBOBJECT will be NEXT_SUBOBJECT, or something following |
2282d28d | 1338 | it. */ |
c8094d83 MS |
1339 | for (subobject_init = next_subobject; |
1340 | subobject_init; | |
2282d28d MM |
1341 | subobject_init = TREE_CHAIN (subobject_init)) |
1342 | if (TREE_PURPOSE (subobject_init) == subobject) | |
ff9f1a5d MM |
1343 | break; |
1344 | ||
2282d28d | 1345 | /* Issue a warning if the explicit initializer order does not |
2cfe82fe | 1346 | match that which will actually occur. |
0cbd7506 | 1347 | ??? Are all these on the correct lines? */ |
2282d28d | 1348 | if (warn_reorder && !subobject_init) |
ff9f1a5d | 1349 | { |
2282d28d | 1350 | if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL) |
af718670 PC |
1351 | warning_at (DECL_SOURCE_LOCATION (TREE_PURPOSE (next_subobject)), |
1352 | OPT_Wreorder, "%qD will be initialized after", | |
1353 | TREE_PURPOSE (next_subobject)); | |
2282d28d | 1354 | else |
b323323f | 1355 | warning (OPT_Wreorder, "base %qT will be initialized after", |
2282d28d MM |
1356 | TREE_PURPOSE (next_subobject)); |
1357 | if (TREE_CODE (subobject) == FIELD_DECL) | |
af718670 PC |
1358 | warning_at (DECL_SOURCE_LOCATION (subobject), |
1359 | OPT_Wreorder, " %q#D", subobject); | |
2282d28d | 1360 | else |
b323323f | 1361 | warning (OPT_Wreorder, " base %qT", subobject); |
c5d75364 MLI |
1362 | warning_at (DECL_SOURCE_LOCATION (current_function_decl), |
1363 | OPT_Wreorder, " when initialized here"); | |
ff9f1a5d | 1364 | } |
b7484fbe | 1365 | |
2282d28d MM |
1366 | /* Look again, from the beginning of the list. */ |
1367 | if (!subobject_init) | |
ff9f1a5d | 1368 | { |
2282d28d MM |
1369 | subobject_init = sorted_inits; |
1370 | while (TREE_PURPOSE (subobject_init) != subobject) | |
1371 | subobject_init = TREE_CHAIN (subobject_init); | |
ff9f1a5d | 1372 | } |
c8094d83 | 1373 | |
2282d28d MM |
1374 | /* It is invalid to initialize the same subobject more than |
1375 | once. */ | |
1376 | if (TREE_VALUE (subobject_init)) | |
ff9f1a5d | 1377 | { |
2282d28d | 1378 | if (TREE_CODE (subobject) == FIELD_DECL) |
c5d75364 MLI |
1379 | error_at (DECL_SOURCE_LOCATION (current_function_decl), |
1380 | "multiple initializations given for %qD", | |
1381 | subobject); | |
2282d28d | 1382 | else |
c5d75364 MLI |
1383 | error_at (DECL_SOURCE_LOCATION (current_function_decl), |
1384 | "multiple initializations given for base %qT", | |
1385 | subobject); | |
ff9f1a5d MM |
1386 | } |
1387 | ||
2282d28d MM |
1388 | /* Record the initialization. */ |
1389 | TREE_VALUE (subobject_init) = TREE_VALUE (init); | |
843710c0 PP |
1390 | /* Carry over the dummy TREE_TYPE node containing the source location. */ |
1391 | TREE_TYPE (subobject_init) = TREE_TYPE (init); | |
2282d28d | 1392 | next_subobject = subobject_init; |
ff9f1a5d MM |
1393 | } |
1394 | ||
1395 | /* [class.base.init] | |
b7484fbe | 1396 | |
ff9f1a5d MM |
1397 | If a ctor-initializer specifies more than one mem-initializer for |
1398 | multiple members of the same union (including members of | |
57ece258 JM |
1399 | anonymous unions), the ctor-initializer is ill-formed. |
1400 | ||
1401 | Here we also splice out uninitialized union members. */ | |
a81072c7 | 1402 | if (uses_unions_or_anon_p) |
ff9f1a5d | 1403 | { |
3a6a88c8 | 1404 | tree *last_p = NULL; |
57ece258 JM |
1405 | tree *p; |
1406 | for (p = &sorted_inits; *p; ) | |
8d08fdba | 1407 | { |
ff9f1a5d | 1408 | tree field; |
535335bf | 1409 | tree ctx; |
ff9f1a5d | 1410 | |
57ece258 JM |
1411 | init = *p; |
1412 | ||
1413 | field = TREE_PURPOSE (init); | |
1414 | ||
1415 | /* Skip base classes. */ | |
1416 | if (TREE_CODE (field) != FIELD_DECL) | |
1417 | goto next; | |
1418 | ||
a81072c7 | 1419 | /* If this is an anonymous aggregate with no explicit initializer, |
57ece258 | 1420 | splice it out. */ |
a81072c7 | 1421 | if (!TREE_VALUE (init) && ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
57ece258 JM |
1422 | goto splice; |
1423 | ||
ff9f1a5d MM |
1424 | /* See if this field is a member of a union, or a member of a |
1425 | structure contained in a union, etc. */ | |
a81072c7 JM |
1426 | ctx = innermost_aggr_scope (field); |
1427 | ||
ff9f1a5d | 1428 | /* If this field is not a member of a union, skip it. */ |
a81072c7 JM |
1429 | if (TREE_CODE (ctx) != UNION_TYPE |
1430 | && !ANON_AGGR_TYPE_P (ctx)) | |
57ece258 JM |
1431 | goto next; |
1432 | ||
3a6a88c8 JM |
1433 | /* If this union member has no explicit initializer and no NSDMI, |
1434 | splice it out. */ | |
1435 | if (TREE_VALUE (init) || DECL_INITIAL (field)) | |
1436 | /* OK. */; | |
1437 | else | |
57ece258 | 1438 | goto splice; |
8d08fdba | 1439 | |
ff9f1a5d MM |
1440 | /* It's only an error if we have two initializers for the same |
1441 | union type. */ | |
3a6a88c8 | 1442 | if (!last_p) |
6bdb8141 | 1443 | { |
3a6a88c8 | 1444 | last_p = p; |
57ece258 | 1445 | goto next; |
6bdb8141 | 1446 | } |
8d08fdba | 1447 | |
ff9f1a5d | 1448 | /* See if LAST_FIELD and the field initialized by INIT are |
a81072c7 JM |
1449 | members of the same union (or the union itself). If so, there's |
1450 | a problem, unless they're actually members of the same structure | |
ff9f1a5d | 1451 | which is itself a member of a union. For example, given: |
8d08fdba | 1452 | |
ff9f1a5d MM |
1453 | union { struct { int i; int j; }; }; |
1454 | ||
1455 | initializing both `i' and `j' makes sense. */ | |
a81072c7 JM |
1456 | ctx = common_enclosing_class |
1457 | (innermost_aggr_scope (field), | |
1458 | innermost_aggr_scope (TREE_PURPOSE (*last_p))); | |
3a6a88c8 | 1459 | |
a81072c7 JM |
1460 | if (ctx && (TREE_CODE (ctx) == UNION_TYPE |
1461 | || ctx == TREE_TYPE (TREE_PURPOSE (*last_p)))) | |
8d08fdba | 1462 | { |
3a6a88c8 JM |
1463 | /* A mem-initializer hides an NSDMI. */ |
1464 | if (TREE_VALUE (init) && !TREE_VALUE (*last_p)) | |
1465 | *last_p = TREE_CHAIN (*last_p); | |
1466 | else if (TREE_VALUE (*last_p) && !TREE_VALUE (init)) | |
1467 | goto splice; | |
1468 | else | |
2ef7251f MP |
1469 | { |
1470 | error_at (DECL_SOURCE_LOCATION (current_function_decl), | |
1471 | "initializations for multiple members of %qT", | |
1472 | ctx); | |
1473 | goto splice; | |
1474 | } | |
8d08fdba | 1475 | } |
ff9f1a5d | 1476 | |
3a6a88c8 | 1477 | last_p = p; |
57ece258 JM |
1478 | |
1479 | next: | |
1480 | p = &TREE_CHAIN (*p); | |
1481 | continue; | |
1482 | splice: | |
1483 | *p = TREE_CHAIN (*p); | |
b7484fbe MS |
1484 | } |
1485 | } | |
8d08fdba | 1486 | |
2282d28d | 1487 | return sorted_inits; |
b7484fbe MS |
1488 | } |
1489 | ||
50bea0c5 JJ |
1490 | /* Callback for cp_walk_tree to mark all PARM_DECLs in a tree as read. */ |
1491 | ||
1492 | static tree | |
1493 | mark_exp_read_r (tree *tp, int *, void *) | |
1494 | { | |
1495 | tree t = *tp; | |
1496 | if (TREE_CODE (t) == PARM_DECL) | |
1497 | mark_exp_read (t); | |
1498 | return NULL_TREE; | |
1499 | } | |
1500 | ||
2282d28d MM |
1501 | /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS |
1502 | is a TREE_LIST giving the explicit mem-initializer-list for the | |
1503 | constructor. The TREE_PURPOSE of each entry is a subobject (a | |
1504 | FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE | |
1505 | is a TREE_LIST giving the arguments to the constructor or | |
1506 | void_type_node for an empty list of arguments. */ | |
a9aedbc2 | 1507 | |
3dbc07b6 | 1508 | void |
2282d28d | 1509 | emit_mem_initializers (tree mem_inits) |
8d08fdba | 1510 | { |
b8bf6ad9 JM |
1511 | int flags = LOOKUP_NORMAL; |
1512 | ||
72e4661a PC |
1513 | /* We will already have issued an error message about the fact that |
1514 | the type is incomplete. */ | |
1515 | if (!COMPLETE_TYPE_P (current_class_type)) | |
1516 | return; | |
c8094d83 | 1517 | |
0790c8aa MP |
1518 | /* Keep a set holding fields that are not initialized. */ |
1519 | hash_set<tree> uninitialized; | |
1520 | ||
1521 | /* Initially that is all of them. */ | |
1522 | if (warn_uninitialized) | |
0c7bce0a | 1523 | for (tree f = next_aggregate_field (TYPE_FIELDS (current_class_type)); |
0790c8aa | 1524 | f != NULL_TREE; |
0c7bce0a | 1525 | f = next_aggregate_field (DECL_CHAIN (f))) |
4b1d3d8d MP |
1526 | if (!DECL_ARTIFICIAL (f) |
1527 | && !is_really_empty_class (TREE_TYPE (f), /*ignore_vptr*/false)) | |
0790c8aa MP |
1528 | uninitialized.add (f); |
1529 | ||
238e471c VV |
1530 | if (mem_inits |
1531 | && TYPE_P (TREE_PURPOSE (mem_inits)) | |
1532 | && same_type_p (TREE_PURPOSE (mem_inits), current_class_type)) | |
1533 | { | |
1534 | /* Delegating constructor. */ | |
1535 | gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE); | |
0790c8aa MP |
1536 | tree ctor = perform_target_ctor (TREE_VALUE (mem_inits)); |
1537 | find_uninit_fields (&ctor, &uninitialized, current_class_type); | |
238e471c VV |
1538 | return; |
1539 | } | |
1540 | ||
85b5d65a | 1541 | if (DECL_DEFAULTED_FN (current_function_decl) |
31f7f784 | 1542 | && ! DECL_INHERITED_CTOR (current_function_decl)) |
b8bf6ad9 JM |
1543 | flags |= LOOKUP_DEFAULTED; |
1544 | ||
2282d28d MM |
1545 | /* Sort the mem-initializers into the order in which the |
1546 | initializations should be performed. */ | |
1547 | mem_inits = sort_mem_initializers (current_class_type, mem_inits); | |
8d08fdba | 1548 | |
1f5a253a | 1549 | in_base_initializer = 1; |
c8094d83 | 1550 | |
2282d28d | 1551 | /* Initialize base classes. */ |
3b616f08 PC |
1552 | for (; (mem_inits |
1553 | && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL); | |
1554 | mem_inits = TREE_CHAIN (mem_inits)) | |
8d08fdba | 1555 | { |
2282d28d MM |
1556 | tree subobject = TREE_PURPOSE (mem_inits); |
1557 | tree arguments = TREE_VALUE (mem_inits); | |
1558 | ||
3b616f08 PC |
1559 | /* We already have issued an error message. */ |
1560 | if (arguments == error_mark_node) | |
1561 | continue; | |
1562 | ||
31f7f784 JM |
1563 | /* Suppress access control when calling the inherited ctor. */ |
1564 | bool inherited_base = (DECL_INHERITED_CTOR (current_function_decl) | |
1565 | && flag_new_inheriting_ctors | |
1566 | && arguments); | |
1567 | if (inherited_base) | |
1568 | push_deferring_access_checks (dk_deferred); | |
1569 | ||
91ea6df3 GDR |
1570 | if (arguments == NULL_TREE) |
1571 | { | |
1572 | /* If these initializations are taking place in a copy constructor, | |
1573 | the base class should probably be explicitly initialized if there | |
1574 | is a user-defined constructor in the base class (other than the | |
1575 | default constructor, which will be called anyway). */ | |
1576 | if (extra_warnings | |
1577 | && DECL_COPY_CONSTRUCTOR_P (current_function_decl) | |
1578 | && type_has_user_nondefault_constructor (BINFO_TYPE (subobject))) | |
1579 | warning_at (DECL_SOURCE_LOCATION (current_function_decl), | |
1580 | OPT_Wextra, "base class %q#T should be explicitly " | |
1581 | "initialized in the copy constructor", | |
1582 | BINFO_TYPE (subobject)); | |
91ea6df3 | 1583 | } |
2282d28d | 1584 | |
2282d28d | 1585 | /* Initialize the base. */ |
45e2bf2e | 1586 | if (!BINFO_VIRTUAL_P (subobject)) |
b7484fbe | 1587 | { |
2282d28d | 1588 | tree base_addr; |
c8094d83 | 1589 | |
2282d28d | 1590 | base_addr = build_base_path (PLUS_EXPR, current_class_ptr, |
a271590a | 1591 | subobject, 1, tf_warning_or_error); |
2282d28d | 1592 | expand_aggr_init_1 (subobject, NULL_TREE, |
04757a2a | 1593 | cp_build_fold_indirect_ref (base_addr), |
2282d28d | 1594 | arguments, |
b8bf6ad9 | 1595 | flags, |
5ade1ed2 | 1596 | tf_warning_or_error); |
2282d28d | 1597 | expand_cleanup_for_base (subobject, NULL_TREE); |
0790c8aa MP |
1598 | if (STATEMENT_LIST_TAIL (cur_stmt_list)) |
1599 | find_uninit_fields (&STATEMENT_LIST_TAIL (cur_stmt_list)->stmt, | |
1600 | &uninitialized, BINFO_TYPE (subobject)); | |
8d08fdba | 1601 | } |
45e2bf2e NS |
1602 | else if (!ABSTRACT_CLASS_TYPE_P (current_class_type)) |
1603 | /* C++14 DR1658 Means we do not have to construct vbases of | |
1604 | abstract classes. */ | |
1605 | construct_virtual_base (subobject, arguments); | |
e3e9e8ca JJ |
1606 | else |
1607 | /* When not constructing vbases of abstract classes, at least mark | |
1608 | the arguments expressions as read to avoid | |
1609 | -Wunused-but-set-parameter false positives. */ | |
50bea0c5 | 1610 | cp_walk_tree (&arguments, mark_exp_read_r, NULL, NULL); |
31f7f784 JM |
1611 | |
1612 | if (inherited_base) | |
1613 | pop_deferring_access_checks (); | |
8d08fdba | 1614 | } |
1f5a253a | 1615 | in_base_initializer = 0; |
8d08fdba | 1616 | |
2282d28d | 1617 | /* Initialize the vptrs. */ |
cf2e003b | 1618 | initialize_vtbl_ptrs (current_class_ptr); |
c8094d83 | 1619 | |
2282d28d MM |
1620 | /* Initialize the data members. */ |
1621 | while (mem_inits) | |
8d08fdba | 1622 | { |
843710c0 PP |
1623 | /* If this initializer was explicitly provided, then the dummy TREE_TYPE |
1624 | node contains the source location. */ | |
1625 | iloc_sentinel ils (EXPR_LOCATION (TREE_TYPE (mem_inits))); | |
1626 | ||
2282d28d | 1627 | perform_member_init (TREE_PURPOSE (mem_inits), |
0790c8aa MP |
1628 | TREE_VALUE (mem_inits), |
1629 | uninitialized); | |
1630 | ||
2282d28d | 1631 | mem_inits = TREE_CHAIN (mem_inits); |
b7484fbe | 1632 | } |
8d08fdba MS |
1633 | } |
1634 | ||
3ec6bad3 MM |
1635 | /* Returns the address of the vtable (i.e., the value that should be |
1636 | assigned to the vptr) for BINFO. */ | |
1637 | ||
2077db1b | 1638 | tree |
362efdc1 | 1639 | build_vtbl_address (tree binfo) |
3ec6bad3 | 1640 | { |
9965d119 | 1641 | tree binfo_for = binfo; |
3ec6bad3 MM |
1642 | tree vtbl; |
1643 | ||
fc6633e0 | 1644 | if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo)) |
9965d119 NS |
1645 | /* If this is a virtual primary base, then the vtable we want to store |
1646 | is that for the base this is being used as the primary base of. We | |
1647 | can't simply skip the initialization, because we may be expanding the | |
1648 | inits of a subobject constructor where the virtual base layout | |
1649 | can be different. */ | |
fc6633e0 NS |
1650 | while (BINFO_PRIMARY_P (binfo_for)) |
1651 | binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for); | |
9965d119 | 1652 | |
3ec6bad3 MM |
1653 | /* Figure out what vtable BINFO's vtable is based on, and mark it as |
1654 | used. */ | |
9965d119 | 1655 | vtbl = get_vtbl_decl_for_binfo (binfo_for); |
c3439626 | 1656 | TREE_USED (vtbl) = true; |
3ec6bad3 MM |
1657 | |
1658 | /* Now compute the address to use when initializing the vptr. */ | |
6de9cd9a | 1659 | vtbl = unshare_expr (BINFO_VTABLE (binfo_for)); |
5a6ccc94 | 1660 | if (VAR_P (vtbl)) |
6de9cd9a | 1661 | vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl); |
3ec6bad3 MM |
1662 | |
1663 | return vtbl; | |
1664 | } | |
1665 | ||
8d08fdba MS |
1666 | /* This code sets up the virtual function tables appropriate for |
1667 | the pointer DECL. It is a one-ply initialization. | |
1668 | ||
1669 | BINFO is the exact type that DECL is supposed to be. In | |
1670 | multiple inheritance, this might mean "C's A" if C : A, B. */ | |
e92cc029 | 1671 | |
8926095f | 1672 | static void |
362efdc1 | 1673 | expand_virtual_init (tree binfo, tree decl) |
8d08fdba | 1674 | { |
8d08fdba | 1675 | tree vtbl, vtbl_ptr; |
3ec6bad3 | 1676 | tree vtt_index; |
8d08fdba | 1677 | |
3ec6bad3 MM |
1678 | /* Compute the initializer for vptr. */ |
1679 | vtbl = build_vtbl_address (binfo); | |
1680 | ||
3461fba7 NS |
1681 | /* We may get this vptr from a VTT, if this is a subobject |
1682 | constructor or subobject destructor. */ | |
3ec6bad3 MM |
1683 | vtt_index = BINFO_VPTR_INDEX (binfo); |
1684 | if (vtt_index) | |
1685 | { | |
1686 | tree vtbl2; | |
1687 | tree vtt_parm; | |
1688 | ||
1689 | /* Compute the value to use, when there's a VTT. */ | |
e0fff4b3 | 1690 | vtt_parm = current_vtt_parm; |
5d49b6a7 | 1691 | vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index); |
04757a2a | 1692 | vtbl2 = cp_build_fold_indirect_ref (vtbl2); |
6de9cd9a | 1693 | vtbl2 = convert (TREE_TYPE (vtbl), vtbl2); |
3ec6bad3 MM |
1694 | |
1695 | /* The actual initializer is the VTT value only in the subobject | |
1696 | constructor. In maybe_clone_body we'll substitute NULL for | |
1697 | the vtt_parm in the case of the non-subobject constructor. */ | |
eb0dbdc7 | 1698 | vtbl = build_if_in_charge (vtbl, vtbl2); |
3ec6bad3 | 1699 | } |
70ae3201 MM |
1700 | |
1701 | /* Compute the location of the vtpr. */ | |
04757a2a | 1702 | vtbl_ptr = build_vfield_ref (cp_build_fold_indirect_ref (decl), |
338d90b8 | 1703 | TREE_TYPE (binfo)); |
50bc768d | 1704 | gcc_assert (vtbl_ptr != error_mark_node); |
8d08fdba | 1705 | |
70ae3201 | 1706 | /* Assign the vtable to the vptr. */ |
4b978f96 | 1707 | vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error); |
4f2e1536 MP |
1708 | finish_expr_stmt (cp_build_modify_expr (input_location, vtbl_ptr, NOP_EXPR, |
1709 | vtbl, tf_warning_or_error)); | |
8d08fdba MS |
1710 | } |
1711 | ||
f33e32a8 MM |
1712 | /* If an exception is thrown in a constructor, those base classes already |
1713 | constructed must be destroyed. This function creates the cleanup | |
0b8a1e58 | 1714 | for BINFO, which has just been constructed. If FLAG is non-NULL, |
838dfd8a | 1715 | it is a DECL which is nonzero when this base needs to be |
0b8a1e58 | 1716 | destroyed. */ |
f33e32a8 MM |
1717 | |
1718 | static void | |
362efdc1 | 1719 | expand_cleanup_for_base (tree binfo, tree flag) |
f33e32a8 MM |
1720 | { |
1721 | tree expr; | |
1722 | ||
eca7fc57 | 1723 | if (!type_build_dtor_call (BINFO_TYPE (binfo))) |
f33e32a8 MM |
1724 | return; |
1725 | ||
0b8a1e58 | 1726 | /* Call the destructor. */ |
c8094d83 | 1727 | expr = build_special_member_call (current_class_ref, |
4ba126e4 | 1728 | base_dtor_identifier, |
c166b898 | 1729 | NULL, |
4ba126e4 | 1730 | binfo, |
5ade1ed2 DG |
1731 | LOOKUP_NORMAL | LOOKUP_NONVIRTUAL, |
1732 | tf_warning_or_error); | |
eca7fc57 JM |
1733 | |
1734 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo))) | |
1735 | return; | |
1736 | ||
0b8a1e58 | 1737 | if (flag) |
db3927fb AH |
1738 | expr = fold_build3_loc (input_location, |
1739 | COND_EXPR, void_type_node, | |
ba47d38d | 1740 | c_common_truthvalue_conversion (input_location, flag), |
7866705a | 1741 | expr, integer_zero_node); |
0b8a1e58 | 1742 | |
659e5a7a | 1743 | finish_eh_cleanup (expr); |
f33e32a8 MM |
1744 | } |
1745 | ||
2282d28d MM |
1746 | /* Construct the virtual base-class VBASE passing the ARGUMENTS to its |
1747 | constructor. */ | |
e92cc029 | 1748 | |
8d08fdba | 1749 | static void |
2282d28d | 1750 | construct_virtual_base (tree vbase, tree arguments) |
8d08fdba | 1751 | { |
2282d28d | 1752 | tree inner_if_stmt; |
2282d28d | 1753 | tree exp; |
c8094d83 | 1754 | tree flag; |
2282d28d MM |
1755 | |
1756 | /* If there are virtual base classes with destructors, we need to | |
1757 | emit cleanups to destroy them if an exception is thrown during | |
1758 | the construction process. These exception regions (i.e., the | |
1759 | period during which the cleanups must occur) begin from the time | |
1760 | the construction is complete to the end of the function. If we | |
1761 | create a conditional block in which to initialize the | |
1762 | base-classes, then the cleanup region for the virtual base begins | |
1763 | inside a block, and ends outside of that block. This situation | |
1764 | confuses the sjlj exception-handling code. Therefore, we do not | |
1765 | create a single conditional block, but one for each | |
1766 | initialization. (That way the cleanup regions always begin | |
3b426391 | 1767 | in the outer block.) We trust the back end to figure out |
2282d28d MM |
1768 | that the FLAG will not change across initializations, and |
1769 | avoid doing multiple tests. */ | |
910ad8de | 1770 | flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl)); |
2282d28d MM |
1771 | inner_if_stmt = begin_if_stmt (); |
1772 | finish_if_stmt_cond (flag, inner_if_stmt); | |
2282d28d MM |
1773 | |
1774 | /* Compute the location of the virtual base. If we're | |
1775 | constructing virtual bases, then we must be the most derived | |
1776 | class. Therefore, we don't have to look up the virtual base; | |
1777 | we already know where it is. */ | |
22ed7e5f MM |
1778 | exp = convert_to_base_statically (current_class_ref, vbase); |
1779 | ||
c8094d83 | 1780 | expand_aggr_init_1 (vbase, current_class_ref, exp, arguments, |
4b978f96 | 1781 | 0, tf_warning_or_error); |
2282d28d | 1782 | finish_then_clause (inner_if_stmt); |
325c3691 | 1783 | finish_if_stmt (inner_if_stmt); |
2282d28d MM |
1784 | |
1785 | expand_cleanup_for_base (vbase, flag); | |
8d08fdba MS |
1786 | } |
1787 | ||
2ee887f2 | 1788 | /* Find the context in which this FIELD can be initialized. */ |
e92cc029 | 1789 | |
2ee887f2 | 1790 | static tree |
362efdc1 | 1791 | initializing_context (tree field) |
2ee887f2 MS |
1792 | { |
1793 | tree t = DECL_CONTEXT (field); | |
1794 | ||
1795 | /* Anonymous union members can be initialized in the first enclosing | |
1796 | non-anonymous union context. */ | |
6bdb8141 | 1797 | while (t && ANON_AGGR_TYPE_P (t)) |
2ee887f2 MS |
1798 | t = TYPE_CONTEXT (t); |
1799 | return t; | |
1800 | } | |
1801 | ||
8d08fdba MS |
1802 | /* Function to give error message if member initialization specification |
1803 | is erroneous. FIELD is the member we decided to initialize. | |
1804 | TYPE is the type for which the initialization is being performed. | |
72b7eeff | 1805 | FIELD must be a member of TYPE. |
c8094d83 | 1806 | |
8d08fdba MS |
1807 | MEMBER_NAME is the name of the member. */ |
1808 | ||
1809 | static int | |
362efdc1 | 1810 | member_init_ok_or_else (tree field, tree type, tree member_name) |
8d08fdba MS |
1811 | { |
1812 | if (field == error_mark_node) | |
1813 | return 0; | |
a723baf1 | 1814 | if (!field) |
8d08fdba | 1815 | { |
15a7ee29 | 1816 | error ("class %qT does not have any field named %qD", type, |
a723baf1 | 1817 | member_name); |
8d08fdba MS |
1818 | return 0; |
1819 | } | |
5a6ccc94 | 1820 | if (VAR_P (field)) |
b7484fbe | 1821 | { |
15a7ee29 | 1822 | error ("%q#D is a static data member; it can only be " |
a723baf1 MM |
1823 | "initialized at its definition", |
1824 | field); | |
1825 | return 0; | |
1826 | } | |
1827 | if (TREE_CODE (field) != FIELD_DECL) | |
1828 | { | |
15a7ee29 | 1829 | error ("%q#D is not a non-static data member of %qT", |
a723baf1 MM |
1830 | field, type); |
1831 | return 0; | |
1832 | } | |
1833 | if (initializing_context (field) != type) | |
1834 | { | |
15a7ee29 | 1835 | error ("class %qT does not have any field named %qD", type, |
a723baf1 | 1836 | member_name); |
b7484fbe MS |
1837 | return 0; |
1838 | } | |
1839 | ||
8d08fdba MS |
1840 | return 1; |
1841 | } | |
1842 | ||
2282d28d MM |
1843 | /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it |
1844 | is a _TYPE node or TYPE_DECL which names a base for that type. | |
1f5a253a NS |
1845 | Check the validity of NAME, and return either the base _TYPE, base |
1846 | binfo, or the FIELD_DECL of the member. If NAME is invalid, return | |
2282d28d | 1847 | NULL_TREE and issue a diagnostic. |
8d08fdba | 1848 | |
36a68fe7 NS |
1849 | An old style unnamed direct single base construction is permitted, |
1850 | where NAME is NULL. */ | |
8d08fdba | 1851 | |
fd74ca0b | 1852 | tree |
1f5a253a | 1853 | expand_member_init (tree name) |
8d08fdba | 1854 | { |
2282d28d MM |
1855 | tree basetype; |
1856 | tree field; | |
8d08fdba | 1857 | |
2282d28d | 1858 | if (!current_class_ref) |
fd74ca0b | 1859 | return NULL_TREE; |
8d08fdba | 1860 | |
36a68fe7 | 1861 | if (!name) |
90418208 | 1862 | { |
36a68fe7 NS |
1863 | /* This is an obsolete unnamed base class initializer. The |
1864 | parser will already have warned about its use. */ | |
604a3205 | 1865 | switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type))) |
36a68fe7 NS |
1866 | { |
1867 | case 0: | |
15a7ee29 | 1868 | error ("unnamed initializer for %qT, which has no base classes", |
2282d28d | 1869 | current_class_type); |
36a68fe7 NS |
1870 | return NULL_TREE; |
1871 | case 1: | |
604a3205 NS |
1872 | basetype = BINFO_TYPE |
1873 | (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0)); | |
36a68fe7 NS |
1874 | break; |
1875 | default: | |
15a7ee29 | 1876 | error ("unnamed initializer for %qT, which uses multiple inheritance", |
2282d28d | 1877 | current_class_type); |
36a68fe7 NS |
1878 | return NULL_TREE; |
1879 | } | |
90418208 | 1880 | } |
36a68fe7 | 1881 | else if (TYPE_P (name)) |
be99da77 | 1882 | { |
a82d6da5 | 1883 | basetype = TYPE_MAIN_VARIANT (name); |
36a68fe7 | 1884 | name = TYPE_NAME (name); |
be99da77 | 1885 | } |
36a68fe7 NS |
1886 | else if (TREE_CODE (name) == TYPE_DECL) |
1887 | basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name)); | |
2282d28d MM |
1888 | else |
1889 | basetype = NULL_TREE; | |
8d08fdba | 1890 | |
36a68fe7 | 1891 | if (basetype) |
41efda8f | 1892 | { |
d9148cf4 MM |
1893 | tree class_binfo; |
1894 | tree direct_binfo; | |
1895 | tree virtual_binfo; | |
1896 | int i; | |
2282d28d | 1897 | |
5cd5a78c JM |
1898 | if (current_template_parms |
1899 | || same_type_p (basetype, current_class_type)) | |
238e471c | 1900 | return basetype; |
2282d28d | 1901 | |
d9148cf4 MM |
1902 | class_binfo = TYPE_BINFO (current_class_type); |
1903 | direct_binfo = NULL_TREE; | |
1904 | virtual_binfo = NULL_TREE; | |
1905 | ||
1906 | /* Look for a direct base. */ | |
fa743e8c | 1907 | for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i) |
539ed333 | 1908 | if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype)) |
fa743e8c NS |
1909 | break; |
1910 | ||
d9148cf4 MM |
1911 | /* Look for a virtual base -- unless the direct base is itself |
1912 | virtual. */ | |
809e3e7f | 1913 | if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo)) |
58c42dc2 | 1914 | virtual_binfo = binfo_for_vbase (basetype, current_class_type); |
d9148cf4 MM |
1915 | |
1916 | /* [class.base.init] | |
c8094d83 | 1917 | |
0cbd7506 | 1918 | If a mem-initializer-id is ambiguous because it designates |
d9148cf4 MM |
1919 | both a direct non-virtual base class and an inherited virtual |
1920 | base class, the mem-initializer is ill-formed. */ | |
1921 | if (direct_binfo && virtual_binfo) | |
1922 | { | |
15a7ee29 | 1923 | error ("%qD is both a direct base and an indirect virtual base", |
d9148cf4 MM |
1924 | basetype); |
1925 | return NULL_TREE; | |
1926 | } | |
1927 | ||
1928 | if (!direct_binfo && !virtual_binfo) | |
8d08fdba | 1929 | { |
5775a06a | 1930 | if (CLASSTYPE_VBASECLASSES (current_class_type)) |
c3115fd2 MM |
1931 | error ("type %qT is not a direct or virtual base of %qT", |
1932 | basetype, current_class_type); | |
41efda8f | 1933 | else |
c3115fd2 MM |
1934 | error ("type %qT is not a direct base of %qT", |
1935 | basetype, current_class_type); | |
fd74ca0b | 1936 | return NULL_TREE; |
41efda8f | 1937 | } |
d9148cf4 MM |
1938 | |
1939 | return direct_binfo ? direct_binfo : virtual_binfo; | |
41efda8f MM |
1940 | } |
1941 | else | |
1942 | { | |
9dc6f476 | 1943 | if (identifier_p (name)) |
86ac0575 | 1944 | field = lookup_field (current_class_type, name, 1, false); |
2282d28d MM |
1945 | else |
1946 | field = name; | |
8d08fdba | 1947 | |
2282d28d | 1948 | if (member_init_ok_or_else (field, current_class_type, name)) |
1f5a253a | 1949 | return field; |
41efda8f | 1950 | } |
fd74ca0b | 1951 | |
2282d28d | 1952 | return NULL_TREE; |
8d08fdba MS |
1953 | } |
1954 | ||
1955 | /* This is like `expand_member_init', only it stores one aggregate | |
1956 | value into another. | |
1957 | ||
1958 | INIT comes in two flavors: it is either a value which | |
1959 | is to be stored in EXP, or it is a parameter list | |
1960 | to go to a constructor, which will operate on EXP. | |
f30432d7 MS |
1961 | If INIT is not a parameter list for a constructor, then set |
1962 | LOOKUP_ONLYCONVERTING. | |
6060a796 MS |
1963 | If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of |
1964 | the initializer, if FLAGS is 0, then it is the (init) form. | |
8d08fdba | 1965 | If `init' is a CONSTRUCTOR, then we emit a warning message, |
59be0cdd | 1966 | explaining that such initializations are invalid. |
8d08fdba | 1967 | |
8d08fdba MS |
1968 | If INIT resolves to a CALL_EXPR which happens to return |
1969 | something of the type we are looking for, then we know | |
1970 | that we can safely use that call to perform the | |
1971 | initialization. | |
1972 | ||
1973 | The virtual function table pointer cannot be set up here, because | |
1974 | we do not really know its type. | |
1975 | ||
8d08fdba MS |
1976 | This never calls operator=(). |
1977 | ||
1978 | When initializing, nothing is CONST. | |
1979 | ||
1980 | A default copy constructor may have to be used to perform the | |
1981 | initialization. | |
1982 | ||
1983 | A constructor or a conversion operator may have to be used to | |
e92cc029 | 1984 | perform the initialization, but not both, as it would be ambiguous. */ |
8d08fdba | 1985 | |
f1dedc31 | 1986 | tree |
5ade1ed2 | 1987 | build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain) |
8d08fdba | 1988 | { |
f1dedc31 MM |
1989 | tree stmt_expr; |
1990 | tree compound_stmt; | |
1991 | int destroy_temps; | |
8d08fdba MS |
1992 | tree type = TREE_TYPE (exp); |
1993 | int was_const = TREE_READONLY (exp); | |
f30432d7 | 1994 | int was_volatile = TREE_THIS_VOLATILE (exp); |
2a3398e1 | 1995 | int is_global; |
8d08fdba MS |
1996 | |
1997 | if (init == error_mark_node) | |
f1dedc31 | 1998 | return error_mark_node; |
8d08fdba | 1999 | |
d1a73b0b | 2000 | location_t init_loc = (init |
f9d0ca40 | 2001 | ? cp_expr_loc_or_input_loc (init) |
d1a73b0b JM |
2002 | : location_of (exp)); |
2003 | ||
8d08fdba | 2004 | TREE_READONLY (exp) = 0; |
f30432d7 MS |
2005 | TREE_THIS_VOLATILE (exp) = 0; |
2006 | ||
8d08fdba MS |
2007 | if (TREE_CODE (type) == ARRAY_TYPE) |
2008 | { | |
70f40fea JJ |
2009 | tree itype = init ? TREE_TYPE (init) : NULL_TREE; |
2010 | int from_array = 0; | |
671cb993 | 2011 | |
7887d808 | 2012 | if (DECL_DECOMPOSITION_P (exp)) |
0655c6d5 JM |
2013 | { |
2014 | from_array = 1; | |
69ce0c8c | 2015 | init = mark_rvalue_use (init); |
dfd7fdca DM |
2016 | if (init |
2017 | && DECL_P (tree_strip_any_location_wrapper (init)) | |
0655c6d5 JM |
2018 | && !(flags & LOOKUP_ONLYCONVERTING)) |
2019 | { | |
2020 | /* Wrap the initializer in a CONSTRUCTOR so that build_vec_init | |
2021 | recognizes it as direct-initialization. */ | |
2022 | init = build_constructor_single (init_list_type_node, | |
2023 | NULL_TREE, init); | |
2024 | CONSTRUCTOR_IS_DIRECT_INIT (init) = true; | |
2025 | } | |
2026 | } | |
70f40fea | 2027 | else |
8d08fdba | 2028 | { |
70f40fea JJ |
2029 | /* Must arrange to initialize each element of EXP |
2030 | from elements of INIT. */ | |
2031 | if (cv_qualified_p (type)) | |
2032 | TREE_TYPE (exp) = cv_unqualified (type); | |
2033 | if (itype && cv_qualified_p (itype)) | |
2034 | TREE_TYPE (init) = cv_unqualified (itype); | |
2035 | from_array = (itype && same_type_p (TREE_TYPE (init), | |
2036 | TREE_TYPE (exp))); | |
d1a73b0b | 2037 | |
e278212e PC |
2038 | if (init && !BRACE_ENCLOSED_INITIALIZER_P (init) |
2039 | && (!from_array | |
2040 | || (TREE_CODE (init) != CONSTRUCTOR | |
2041 | /* Can happen, eg, handling the compound-literals | |
2042 | extension (ext/complit12.C). */ | |
2043 | && TREE_CODE (init) != TARGET_EXPR))) | |
d1a73b0b JM |
2044 | { |
2045 | if (complain & tf_error) | |
e278212e PC |
2046 | error_at (init_loc, "array must be initialized " |
2047 | "with a brace-enclosed initializer"); | |
2048 | return error_mark_node; | |
d1a73b0b | 2049 | } |
8d08fdba | 2050 | } |
70f40fea | 2051 | |
a48cccea | 2052 | stmt_expr = build_vec_init (exp, NULL_TREE, init, |
844ae01d | 2053 | /*explicit_value_init_p=*/false, |
70f40fea | 2054 | from_array, |
5ade1ed2 | 2055 | complain); |
8d08fdba | 2056 | TREE_READONLY (exp) = was_const; |
f30432d7 | 2057 | TREE_THIS_VOLATILE (exp) = was_volatile; |
8d08fdba | 2058 | TREE_TYPE (exp) = type; |
a47c2f62 JM |
2059 | /* Restore the type of init unless it was used directly. */ |
2060 | if (init && TREE_CODE (stmt_expr) != INIT_EXPR) | |
f376e137 | 2061 | TREE_TYPE (init) = itype; |
f1dedc31 | 2062 | return stmt_expr; |
8d08fdba MS |
2063 | } |
2064 | ||
e32869a1 | 2065 | if (is_copy_initialization (init)) |
0655c6d5 JM |
2066 | flags |= LOOKUP_ONLYCONVERTING; |
2067 | ||
2a3398e1 | 2068 | is_global = begin_init_stmts (&stmt_expr, &compound_stmt); |
f2c5f623 | 2069 | destroy_temps = stmts_are_full_exprs_p (); |
ae499cce | 2070 | current_stmt_tree ()->stmts_are_full_exprs_p = 0; |
b9119edc PP |
2071 | bool ok = expand_aggr_init_1 (TYPE_BINFO (type), exp, exp, |
2072 | init, LOOKUP_NORMAL|flags, complain); | |
2a3398e1 | 2073 | stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt); |
ae499cce | 2074 | current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps; |
8d08fdba | 2075 | TREE_READONLY (exp) = was_const; |
f30432d7 | 2076 | TREE_THIS_VOLATILE (exp) = was_volatile; |
b9119edc PP |
2077 | if (!ok) |
2078 | return error_mark_node; | |
f1dedc31 | 2079 | |
b46b715d JM |
2080 | if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL) |
2081 | && TREE_SIDE_EFFECTS (stmt_expr) | |
2082 | && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type))) | |
2083 | /* Just know that we've seen something for this node. */ | |
2084 | TREE_USED (exp) = 1; | |
2085 | ||
f1dedc31 | 2086 | return stmt_expr; |
8d08fdba MS |
2087 | } |
2088 | ||
b9119edc | 2089 | static bool |
5ade1ed2 DG |
2090 | expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags, |
2091 | tsubst_flags_t complain) | |
8d08fdba | 2092 | { |
fc378698 MS |
2093 | tree type = TREE_TYPE (exp); |
2094 | ||
8d08fdba MS |
2095 | /* It fails because there may not be a constructor which takes |
2096 | its own type as the first (or only parameter), but which does | |
2097 | take other types via a conversion. So, if the thing initializing | |
2098 | the expression is a unit element of type X, first try X(X&), | |
2099 | followed by initialization by X. If neither of these work | |
2100 | out, then look hard. */ | |
2101 | tree rval; | |
9771b263 | 2102 | vec<tree, va_gc> *parms; |
8d08fdba | 2103 | |
2061820e JM |
2104 | /* If we have direct-initialization from an initializer list, pull |
2105 | it out of the TREE_LIST so the code below can see it. */ | |
2106 | if (init && TREE_CODE (init) == TREE_LIST | |
014397c2 | 2107 | && DIRECT_LIST_INIT_P (TREE_VALUE (init))) |
2061820e JM |
2108 | { |
2109 | gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0 | |
2110 | && TREE_CHAIN (init) == NULL_TREE); | |
2111 | init = TREE_VALUE (init); | |
f0516ca4 JJ |
2112 | /* Only call reshape_init if it has not been called earlier |
2113 | by the callers. */ | |
2114 | if (BRACE_ENCLOSED_INITIALIZER_P (init) && CP_AGGREGATE_TYPE_P (type)) | |
2115 | init = reshape_init (type, init, complain); | |
2061820e JM |
2116 | } |
2117 | ||
4c9b3895 JM |
2118 | if (init && BRACE_ENCLOSED_INITIALIZER_P (init) |
2119 | && CP_AGGREGATE_TYPE_P (type)) | |
e08cc018 JM |
2120 | /* A brace-enclosed initializer for an aggregate. In C++0x this can |
2121 | happen for direct-initialization, too. */ | |
f0516ca4 | 2122 | init = digest_init (type, init, complain); |
e08cc018 | 2123 | |
b9119edc PP |
2124 | if (init == error_mark_node) |
2125 | return false; | |
2126 | ||
e08cc018 JM |
2127 | /* A CONSTRUCTOR of the target's type is a previously digested |
2128 | initializer, whether that happened just above or in | |
2129 | cp_parser_late_parsing_nsdmi. | |
2130 | ||
36cbfdb0 JM |
2131 | A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P |
2132 | set represents the whole initialization, so we shouldn't build up | |
2133 | another ctor call. */ | |
e08cc018 JM |
2134 | if (init |
2135 | && (TREE_CODE (init) == CONSTRUCTOR | |
36cbfdb0 JM |
2136 | || (TREE_CODE (init) == TARGET_EXPR |
2137 | && (TARGET_EXPR_DIRECT_INIT_P (init) | |
2138 | || TARGET_EXPR_LIST_INIT_P (init)))) | |
e08cc018 | 2139 | && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type)) |
4c9b3895 | 2140 | { |
e08cc018 JM |
2141 | /* Early initialization via a TARGET_EXPR only works for |
2142 | complete objects. */ | |
2143 | gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp); | |
2144 | ||
6ffbf87c | 2145 | init = cp_build_init_expr (exp, init); |
4c9b3895 JM |
2146 | TREE_SIDE_EFFECTS (init) = 1; |
2147 | finish_expr_stmt (init); | |
b9119edc | 2148 | return true; |
4c9b3895 JM |
2149 | } |
2150 | ||
277294d7 | 2151 | if (init && TREE_CODE (init) != TREE_LIST |
32005478 JM |
2152 | && (flags & LOOKUP_ONLYCONVERTING) |
2153 | && !unsafe_return_slot_p (exp)) | |
faf5394a MS |
2154 | { |
2155 | /* Base subobjects should only get direct-initialization. */ | |
8dc2b103 | 2156 | gcc_assert (true_exp == exp); |
faf5394a | 2157 | |
c37dc68e JM |
2158 | if (flags & DIRECT_BIND) |
2159 | /* Do nothing. We hit this in two cases: Reference initialization, | |
2160 | where we aren't initializing a real variable, so we don't want | |
2161 | to run a new constructor; and catching an exception, where we | |
2162 | have already built up the constructor call so we could wrap it | |
2163 | in an exception region. */; | |
2164 | else | |
b9119edc PP |
2165 | { |
2166 | init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, | |
2167 | flags, complain | tf_no_cleanup); | |
2168 | if (init == error_mark_node) | |
2169 | return false; | |
2170 | } | |
faf5394a | 2171 | |
f8ba88b6 JM |
2172 | /* We need to protect the initialization of a catch parm with a |
2173 | call to terminate(), which shows up as a MUST_NOT_THROW_EXPR | |
2174 | around the TARGET_EXPR for the copy constructor. See | |
2175 | initialize_handler_parm. */ | |
2176 | tree *p = &init; | |
2177 | while (TREE_CODE (*p) == MUST_NOT_THROW_EXPR | |
2178 | || TREE_CODE (*p) == CLEANUP_POINT_EXPR) | |
4e8dca1c | 2179 | { |
f8ba88b6 JM |
2180 | /* Avoid voidify_wrapper_expr making a temporary. */ |
2181 | TREE_TYPE (*p) = void_type_node; | |
2182 | p = &TREE_OPERAND (*p, 0); | |
4e8dca1c | 2183 | } |
6ffbf87c | 2184 | *p = cp_build_init_expr (exp, *p); |
f1dedc31 | 2185 | finish_expr_stmt (init); |
b9119edc | 2186 | return true; |
faf5394a MS |
2187 | } |
2188 | ||
c166b898 ILT |
2189 | if (init == NULL_TREE) |
2190 | parms = NULL; | |
2191 | else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init)) | |
8d08fdba | 2192 | { |
c166b898 ILT |
2193 | parms = make_tree_vector (); |
2194 | for (; init != NULL_TREE; init = TREE_CHAIN (init)) | |
9771b263 | 2195 | vec_safe_push (parms, TREE_VALUE (init)); |
8d08fdba | 2196 | } |
8d08fdba | 2197 | else |
c166b898 | 2198 | parms = make_tree_vector_single (init); |
8d08fdba | 2199 | |
238e471c VV |
2200 | if (exp == current_class_ref && current_function_decl |
2201 | && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl)) | |
2202 | { | |
2203 | /* Delegating constructor. */ | |
2204 | tree complete; | |
2205 | tree base; | |
c4ce224c JM |
2206 | tree elt; unsigned i; |
2207 | ||
2208 | /* Unshare the arguments for the second call. */ | |
cd9cf97b | 2209 | releasing_vec parms2; |
9771b263 | 2210 | FOR_EACH_VEC_SAFE_ELT (parms, i, elt) |
c4ce224c JM |
2211 | { |
2212 | elt = break_out_target_exprs (elt); | |
9771b263 | 2213 | vec_safe_push (parms2, elt); |
c4ce224c | 2214 | } |
238e471c | 2215 | complete = build_special_member_call (exp, complete_ctor_identifier, |
c4ce224c JM |
2216 | &parms2, binfo, flags, |
2217 | complain); | |
2218 | complete = fold_build_cleanup_point_expr (void_type_node, complete); | |
c4ce224c | 2219 | |
238e471c VV |
2220 | base = build_special_member_call (exp, base_ctor_identifier, |
2221 | &parms, binfo, flags, | |
2222 | complain); | |
c4ce224c | 2223 | base = fold_build_cleanup_point_expr (void_type_node, base); |
b9119edc PP |
2224 | if (complete == error_mark_node || base == error_mark_node) |
2225 | return false; | |
eb0dbdc7 | 2226 | rval = build_if_in_charge (complete, base); |
238e471c VV |
2227 | } |
2228 | else | |
2229 | { | |
d6ef53f2 NS |
2230 | tree ctor_name = (true_exp == exp |
2231 | ? complete_ctor_identifier : base_ctor_identifier); | |
2232 | ||
238e471c VV |
2233 | rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags, |
2234 | complain); | |
b9119edc PP |
2235 | if (rval == error_mark_node) |
2236 | return false; | |
c0cdf62c | 2237 | } |
c166b898 ILT |
2238 | |
2239 | if (parms != NULL) | |
2240 | release_tree_vector (parms); | |
2241 | ||
fa2200cb JM |
2242 | if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR) |
2243 | { | |
2244 | tree fn = get_callee_fndecl (rval); | |
a76c13bf | 2245 | if (fn && DECL_DECLARED_CONSTEXPR_P (fn)) |
fa2200cb | 2246 | { |
3e605b20 | 2247 | tree e = maybe_constant_init (rval, exp); |
fa2200cb | 2248 | if (TREE_CONSTANT (e)) |
6ffbf87c | 2249 | rval = cp_build_init_expr (exp, e); |
fa2200cb JM |
2250 | } |
2251 | } | |
2252 | ||
2253 | /* FIXME put back convert_to_void? */ | |
25eb19ff | 2254 | if (TREE_SIDE_EFFECTS (rval)) |
fa2200cb | 2255 | finish_expr_stmt (rval); |
b9119edc PP |
2256 | |
2257 | return true; | |
8d08fdba MS |
2258 | } |
2259 | ||
2260 | /* This function is responsible for initializing EXP with INIT | |
b9119edc | 2261 | (if any). Returns true on success, false on failure. |
8d08fdba MS |
2262 | |
2263 | BINFO is the binfo of the type for who we are performing the | |
2264 | initialization. For example, if W is a virtual base class of A and B, | |
2265 | and C : A, B. | |
2266 | If we are initializing B, then W must contain B's W vtable, whereas | |
2267 | were we initializing C, W must contain C's W vtable. | |
2268 | ||
2269 | TRUE_EXP is nonzero if it is the true expression being initialized. | |
2270 | In this case, it may be EXP, or may just contain EXP. The reason we | |
2271 | need this is because if EXP is a base element of TRUE_EXP, we | |
2272 | don't necessarily know by looking at EXP where its virtual | |
2273 | baseclass fields should really be pointing. But we do know | |
2274 | from TRUE_EXP. In constructors, we don't know anything about | |
2275 | the value being initialized. | |
2276 | ||
9f880ef9 MM |
2277 | FLAGS is just passed to `build_new_method_call'. See that function |
2278 | for its description. */ | |
8d08fdba | 2279 | |
b9119edc | 2280 | static bool |
5ade1ed2 DG |
2281 | expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags, |
2282 | tsubst_flags_t complain) | |
8d08fdba MS |
2283 | { |
2284 | tree type = TREE_TYPE (exp); | |
8d08fdba | 2285 | |
50bc768d | 2286 | gcc_assert (init != error_mark_node && type != error_mark_node); |
38e01f9e | 2287 | gcc_assert (building_stmt_list_p ()); |
8d08fdba MS |
2288 | |
2289 | /* Use a function returning the desired type to initialize EXP for us. | |
2290 | If the function is a constructor, and its first argument is | |
2291 | NULL_TREE, know that it was meant for us--just slide exp on | |
2292 | in and expand the constructor. Constructors now come | |
2293 | as TARGET_EXPRs. */ | |
faf5394a | 2294 | |
5a6ccc94 | 2295 | if (init && VAR_P (exp) |
3b2db49f | 2296 | && COMPOUND_LITERAL_P (init)) |
faf5394a | 2297 | { |
9771b263 | 2298 | vec<tree, va_gc> *cleanups = NULL; |
f1dedc31 | 2299 | /* If store_init_value returns NULL_TREE, the INIT has been |
3b2db49f | 2300 | recorded as the DECL_INITIAL for EXP. That means there's |
f1dedc31 | 2301 | nothing more we have to do. */ |
b25dd954 | 2302 | init = store_init_value (exp, init, &cleanups, flags); |
25ebb82a RH |
2303 | if (init) |
2304 | finish_expr_stmt (init); | |
b25dd954 | 2305 | gcc_assert (!cleanups); |
b9119edc | 2306 | return true; |
faf5394a MS |
2307 | } |
2308 | ||
f388b7be | 2309 | /* List-initialization from {} becomes value-initialization for non-aggregate |
49b5925f JM |
2310 | classes with default constructors. Handle this here when we're |
2311 | initializing a base, so protected access works. */ | |
2312 | if (exp != true_exp && init && TREE_CODE (init) == TREE_LIST) | |
f388b7be JM |
2313 | { |
2314 | tree elt = TREE_VALUE (init); | |
2315 | if (DIRECT_LIST_INIT_P (elt) | |
2316 | && CONSTRUCTOR_ELTS (elt) == 0 | |
2317 | && CLASSTYPE_NON_AGGREGATE (type) | |
2318 | && TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) | |
2319 | init = void_type_node; | |
2320 | } | |
2321 | ||
fd97a96a JM |
2322 | /* If an explicit -- but empty -- initializer list was present, |
2323 | that's value-initialization. */ | |
2324 | if (init == void_type_node) | |
2325 | { | |
ac0bc21b | 2326 | /* If the type has data but no user-provided default ctor, we need to zero |
c2b3ec18 | 2327 | out the object. */ |
ac0bc21b | 2328 | if (type_has_non_user_provided_default_constructor (type) |
dbcd32f8 | 2329 | && !is_really_empty_class (type, /*ignore_vptr*/true)) |
fd97a96a | 2330 | { |
1fb0b801 JM |
2331 | tree field_size = NULL_TREE; |
2332 | if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type) | |
2333 | /* Don't clobber already initialized virtual bases. */ | |
2334 | field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type)); | |
2335 | init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false, | |
2336 | field_size); | |
6ffbf87c | 2337 | init = cp_build_init_expr (exp, init); |
fd97a96a | 2338 | finish_expr_stmt (init); |
fd97a96a | 2339 | } |
1fb0b801 | 2340 | |
fd97a96a | 2341 | /* If we don't need to mess with the constructor at all, |
1fb0b801 JM |
2342 | then we're done. */ |
2343 | if (! type_build_ctor_call (type)) | |
b9119edc | 2344 | return true; |
1fb0b801 JM |
2345 | |
2346 | /* Otherwise fall through and call the constructor. */ | |
fd97a96a JM |
2347 | init = NULL_TREE; |
2348 | } | |
2349 | ||
9e9ff709 MS |
2350 | /* We know that expand_default_init can handle everything we want |
2351 | at this point. */ | |
b9119edc | 2352 | return expand_default_init (binfo, true_exp, exp, init, flags, complain); |
8d08fdba MS |
2353 | } |
2354 | ||
9e1e64ec | 2355 | /* Report an error if TYPE is not a user-defined, class type. If |
be99da77 | 2356 | OR_ELSE is nonzero, give an error message. */ |
e92cc029 | 2357 | |
be99da77 | 2358 | int |
9e1e64ec | 2359 | is_class_type (tree type, int or_else) |
be99da77 MS |
2360 | { |
2361 | if (type == error_mark_node) | |
2362 | return 0; | |
2363 | ||
9e1e64ec | 2364 | if (! CLASS_TYPE_P (type)) |
be99da77 MS |
2365 | { |
2366 | if (or_else) | |
9e1e64ec | 2367 | error ("%qT is not a class type", type); |
be99da77 MS |
2368 | return 0; |
2369 | } | |
2370 | return 1; | |
2371 | } | |
2372 | ||
e32869a1 PP |
2373 | /* Returns true iff the initializer INIT represents copy-initialization |
2374 | (and therefore we must set LOOKUP_ONLYCONVERTING when processing it). */ | |
2375 | ||
2376 | bool | |
2377 | is_copy_initialization (tree init) | |
2378 | { | |
2379 | return (init && init != void_type_node | |
2380 | && TREE_CODE (init) != TREE_LIST | |
2381 | && !(TREE_CODE (init) == TARGET_EXPR | |
2382 | && TARGET_EXPR_DIRECT_INIT_P (init)) | |
2383 | && !DIRECT_LIST_INIT_P (init)); | |
2384 | } | |
2385 | ||
a5ac359a MM |
2386 | /* Build a reference to a member of an aggregate. This is not a C++ |
2387 | `&', but really something which can have its address taken, and | |
2388 | then act as a pointer to member, for example TYPE :: FIELD can have | |
2389 | its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if | |
2390 | this expression is the operand of "&". | |
8d08fdba MS |
2391 | |
2392 | @@ Prints out lousy diagnostics for operator <typename> | |
2393 | @@ fields. | |
2394 | ||
e53b6e56 | 2395 | @@ This function should be rewritten and placed in search.cc. */ |
e92cc029 | 2396 | |
8d08fdba | 2397 | tree |
a378996b PC |
2398 | build_offset_ref (tree type, tree member, bool address_p, |
2399 | tsubst_flags_t complain) | |
8d08fdba | 2400 | { |
8d245821 | 2401 | tree decl; |
fc378698 | 2402 | tree basebinfo = NULL_TREE; |
8d08fdba | 2403 | |
5f311aec | 2404 | /* class templates can come in as TEMPLATE_DECLs here. */ |
d4f0f205 MM |
2405 | if (TREE_CODE (member) == TEMPLATE_DECL) |
2406 | return member; | |
93cdc044 | 2407 | |
627bc938 JM |
2408 | if (dependent_scope_p (type) || type_dependent_expression_p (member)) |
2409 | return build_qualified_name (NULL_TREE, type, member, | |
2d660b7f | 2410 | /*template_p=*/false); |
5566b478 | 2411 | |
d4f0f205 | 2412 | gcc_assert (TYPE_P (type)); |
9e1e64ec | 2413 | if (! is_class_type (type, 1)) |
c833d2be NS |
2414 | return error_mark_node; |
2415 | ||
d4f0f205 | 2416 | gcc_assert (DECL_P (member) || BASELINK_P (member)); |
c352ef0e JM |
2417 | /* Callers should call mark_used before this point, except for functions. */ |
2418 | gcc_assert (!DECL_P (member) || TREE_USED (member) | |
2419 | || TREE_CODE (member) == FUNCTION_DECL); | |
be99da77 | 2420 | |
627bc938 | 2421 | type = TYPE_MAIN_VARIANT (type); |
01628e54 | 2422 | if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type))) |
8d08fdba | 2423 | { |
a378996b PC |
2424 | if (complain & tf_error) |
2425 | error ("incomplete type %qT does not have member %qD", type, member); | |
a5ac359a MM |
2426 | return error_mark_node; |
2427 | } | |
2428 | ||
d4f0f205 | 2429 | /* Entities other than non-static members need no further |
3db45ab5 | 2430 | processing. */ |
a5ac359a | 2431 | if (TREE_CODE (member) == TYPE_DECL) |
d4f0f205 | 2432 | return member; |
5a6ccc94 | 2433 | if (VAR_P (member) || TREE_CODE (member) == CONST_DECL) |
d4f0f205 | 2434 | return convert_from_reference (member); |
a5ac359a MM |
2435 | |
2436 | if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member)) | |
2437 | { | |
a378996b PC |
2438 | if (complain & tf_error) |
2439 | error ("invalid pointer to bit-field %qD", member); | |
a5ac359a MM |
2440 | return error_mark_node; |
2441 | } | |
2442 | ||
d4f0f205 MM |
2443 | /* Set up BASEBINFO for member lookup. */ |
2444 | decl = maybe_dummy_object (type, &basebinfo); | |
2445 | ||
aa52c1ff | 2446 | /* A lot of this logic is now handled in lookup_member. */ |
a5ac359a | 2447 | if (BASELINK_P (member)) |
8d08fdba | 2448 | { |
8d08fdba | 2449 | /* Go from the TREE_BASELINK to the member function info. */ |
7304fcb4 | 2450 | tree t = BASELINK_FUNCTIONS (member); |
8d08fdba | 2451 | |
50ad9642 | 2452 | if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t)) |
8d08fdba | 2453 | { |
f4f206f4 | 2454 | /* Get rid of a potential OVERLOAD around it. */ |
848bf88d | 2455 | t = OVL_FIRST (t); |
2c73f9f5 | 2456 | |
b54f5338 KL |
2457 | /* Unique functions are handled easily. */ |
2458 | ||
2459 | /* For non-static member of base class, we need a special rule | |
2460 | for access checking [class.protected]: | |
2461 | ||
2462 | If the access is to form a pointer to member, the | |
2463 | nested-name-specifier shall name the derived class | |
2464 | (or any class derived from that class). */ | |
080384d6 | 2465 | bool ok; |
b54f5338 KL |
2466 | if (address_p && DECL_P (t) |
2467 | && DECL_NONSTATIC_MEMBER_P (t)) | |
080384d6 JM |
2468 | ok = perform_or_defer_access_check (TYPE_BINFO (type), t, t, |
2469 | complain); | |
b54f5338 | 2470 | else |
080384d6 JM |
2471 | ok = perform_or_defer_access_check (basebinfo, t, t, |
2472 | complain); | |
2473 | if (!ok) | |
2474 | return error_mark_node; | |
848b92e1 | 2475 | if (DECL_STATIC_FUNCTION_P (t)) |
a1b3484a | 2476 | return member; |
a5ac359a MM |
2477 | member = t; |
2478 | } | |
2479 | else | |
7304fcb4 | 2480 | TREE_TYPE (member) = unknown_type_node; |
8d08fdba | 2481 | } |
b54f5338 | 2482 | else if (address_p && TREE_CODE (member) == FIELD_DECL) |
080384d6 JM |
2483 | { |
2484 | /* We need additional test besides the one in | |
2485 | check_accessibility_of_qualified_id in case it is | |
2486 | a pointer to non-static member. */ | |
2487 | if (!perform_or_defer_access_check (TYPE_BINFO (type), member, member, | |
2488 | complain)) | |
2489 | return error_mark_node; | |
2490 | } | |
8d08fdba | 2491 | |
a5ac359a | 2492 | if (!address_p) |
8d08fdba | 2493 | { |
a5ac359a MM |
2494 | /* If MEMBER is non-static, then the program has fallen afoul of |
2495 | [expr.prim]: | |
8d08fdba | 2496 | |
d20b7173 MP |
2497 | An id-expression that denotes a non-static data member or |
2498 | non-static member function of a class can only be used: | |
8d08fdba | 2499 | |
a5ac359a MM |
2500 | -- as part of a class member access (_expr.ref_) in which the |
2501 | object-expression refers to the member's class or a class | |
2502 | derived from that class, or | |
b7484fbe | 2503 | |
a5ac359a MM |
2504 | -- to form a pointer to member (_expr.unary.op_), or |
2505 | ||
d20b7173 MP |
2506 | -- in the body of a non-static member function of that class or |
2507 | of a class derived from that class (_class.mfct.non-static_), or | |
a5ac359a MM |
2508 | |
2509 | -- in a mem-initializer for a constructor for that class or for | |
2510 | a class derived from that class (_class.base.init_). */ | |
f9fbf93d | 2511 | if (DECL_OBJECT_MEMBER_FUNCTION_P (member)) |
a5ac359a | 2512 | { |
39a13be5 | 2513 | /* Build a representation of the qualified name suitable |
e9525111 MM |
2514 | for use as the operand to "&" -- even though the "&" is |
2515 | not actually present. */ | |
f293ce4b | 2516 | member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member); |
a5ac359a MM |
2517 | /* In Microsoft mode, treat a non-static member function as if |
2518 | it were a pointer-to-member. */ | |
2519 | if (flag_ms_extensions) | |
2520 | { | |
a5ac359a | 2521 | PTRMEM_OK_P (member) = 1; |
a378996b | 2522 | return cp_build_addr_expr (member, complain); |
a5ac359a | 2523 | } |
a378996b PC |
2524 | if (complain & tf_error) |
2525 | error ("invalid use of non-static member function %qD", | |
2526 | TREE_OPERAND (member, 1)); | |
07471dfb | 2527 | return error_mark_node; |
a5ac359a MM |
2528 | } |
2529 | else if (TREE_CODE (member) == FIELD_DECL) | |
2530 | { | |
a378996b PC |
2531 | if (complain & tf_error) |
2532 | error ("invalid use of non-static data member %qD", member); | |
a5ac359a MM |
2533 | return error_mark_node; |
2534 | } | |
2535 | return member; | |
2536 | } | |
8d08fdba | 2537 | |
f293ce4b | 2538 | member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member); |
8d245821 MM |
2539 | PTRMEM_OK_P (member) = 1; |
2540 | return member; | |
8d08fdba MS |
2541 | } |
2542 | ||
393e756d MM |
2543 | /* If DECL is a scalar enumeration constant or variable with a |
2544 | constant initializer, return the initializer (or, its initializers, | |
69eb4fde JM |
2545 | recursively); otherwise, return DECL. If STRICT_P, the |
2546 | initializer is only returned if DECL is a | |
90454da1 | 2547 | constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to |
8c00059c PP |
2548 | return an aggregate constant. If UNSHARE_P, return an unshared |
2549 | copy of the initializer. */ | |
8d08fdba | 2550 | |
393e756d | 2551 | static tree |
8c00059c PP |
2552 | constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p, |
2553 | bool unshare_p) | |
8d08fdba | 2554 | { |
f513e31f | 2555 | while (TREE_CODE (decl) == CONST_DECL |
fd338b13 JM |
2556 | || decl_constant_var_p (decl) |
2557 | || (!strict_p && VAR_P (decl) | |
2558 | && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))) | |
b794e321 MM |
2559 | { |
2560 | tree init; | |
fa2200cb JM |
2561 | /* If DECL is a static data member in a template |
2562 | specialization, we must instantiate it here. The | |
2563 | initializer for the static data member is not processed | |
2564 | until needed; we need it now. */ | |
fd5c817a | 2565 | mark_used (decl, tf_none); |
fa2200cb | 2566 | init = DECL_INITIAL (decl); |
d174af6c | 2567 | if (init == error_mark_node) |
88274c4d | 2568 | { |
cdd669f9 JJ |
2569 | if (TREE_CODE (decl) == CONST_DECL |
2570 | || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) | |
88274c4d JM |
2571 | /* Treat the error as a constant to avoid cascading errors on |
2572 | excessively recursive template instantiation (c++/9335). */ | |
2573 | return init; | |
2574 | else | |
2575 | return decl; | |
2576 | } | |
73ce7fcb JJ |
2577 | /* Initializers in templates are generally expanded during |
2578 | instantiation, so before that for const int i(2) | |
2579 | INIT is a TREE_LIST with the actual initializer as | |
2580 | TREE_VALUE. */ | |
2581 | if (processing_template_decl | |
2582 | && init | |
2583 | && TREE_CODE (init) == TREE_LIST | |
2584 | && TREE_CHAIN (init) == NULL_TREE) | |
2585 | init = TREE_VALUE (init); | |
d1dfa20d JM |
2586 | /* Instantiate a non-dependent initializer for user variables. We |
2587 | mustn't do this for the temporary for an array compound literal; | |
2588 | trying to instatiate the initializer will keep creating new | |
2589 | temporaries until we crash. Probably it's not useful to do it for | |
2590 | other artificial variables, either. */ | |
2591 | if (!DECL_ARTIFICIAL (decl)) | |
2592 | init = instantiate_non_dependent_or_null (init); | |
d174af6c | 2593 | if (!init |
b794e321 | 2594 | || !TREE_TYPE (init) |
8152661b | 2595 | || !TREE_CONSTANT (init) |
69eb4fde | 2596 | || (!return_aggregate_cst_ok_p |
90454da1 PC |
2597 | /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not |
2598 | return an aggregate constant (of which string | |
2599 | literals are a special case), as we do not want | |
2600 | to make inadvertent copies of such entities, and | |
2601 | we must be sure that their addresses are the | |
2602 | same everywhere. */ | |
8152661b JM |
2603 | && (TREE_CODE (init) == CONSTRUCTOR |
2604 | || TREE_CODE (init) == STRING_CST))) | |
b794e321 | 2605 | break; |
62f9ab0d | 2606 | /* Don't return a CONSTRUCTOR for a variable with partial run-time |
5a5da480 JJ |
2607 | initialization, since it doesn't represent the entire value. |
2608 | Similarly for VECTOR_CSTs created by cp_folding those | |
2609 | CONSTRUCTORs. */ | |
2610 | if ((TREE_CODE (init) == CONSTRUCTOR | |
2611 | || TREE_CODE (init) == VECTOR_CST) | |
62f9ab0d JM |
2612 | && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) |
2613 | break; | |
6443c7c0 MP |
2614 | /* If the variable has a dynamic initializer, don't use its |
2615 | DECL_INITIAL which doesn't reflect the real value. */ | |
2616 | if (VAR_P (decl) | |
2617 | && TREE_STATIC (decl) | |
2618 | && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) | |
2619 | && DECL_NONTRIVIALLY_INITIALIZED_P (decl)) | |
2620 | break; | |
8c00059c | 2621 | decl = init; |
b794e321 | 2622 | } |
8c00059c | 2623 | return unshare_p ? unshare_expr (decl) : decl; |
8a784e4a | 2624 | } |
a1652802 | 2625 | |
69eb4fde JM |
2626 | /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by constant |
2627 | of integral or enumeration type, or a constexpr variable of scalar type, | |
2628 | then return that value. These are those variables permitted in constant | |
2629 | expressions by [5.19/1]. */ | |
a1652802 | 2630 | |
8a784e4a | 2631 | tree |
69eb4fde | 2632 | scalar_constant_value (tree decl) |
8a784e4a | 2633 | { |
69eb4fde | 2634 | return constant_value_1 (decl, /*strict_p=*/true, |
8c00059c PP |
2635 | /*return_aggregate_cst_ok_p=*/false, |
2636 | /*unshare_p=*/true); | |
393e756d | 2637 | } |
c8094d83 | 2638 | |
8c00059c PP |
2639 | /* Like scalar_constant_value, but can also return aggregate initializers. |
2640 | If UNSHARE_P, return an unshared copy of the initializer. */ | |
393e756d MM |
2641 | |
2642 | tree | |
8c00059c | 2643 | decl_really_constant_value (tree decl, bool unshare_p /*= true*/) |
393e756d | 2644 | { |
69eb4fde | 2645 | return constant_value_1 (decl, /*strict_p=*/true, |
8c00059c PP |
2646 | /*return_aggregate_cst_ok_p=*/true, |
2647 | /*unshare_p=*/unshare_p); | |
90454da1 PC |
2648 | } |
2649 | ||
8c00059c | 2650 | /* A more relaxed version of decl_really_constant_value, used by the |
69eb4fde | 2651 | common C/C++ code. */ |
90454da1 PC |
2652 | |
2653 | tree | |
8c00059c | 2654 | decl_constant_value (tree decl, bool unshare_p) |
90454da1 | 2655 | { |
69eb4fde | 2656 | return constant_value_1 (decl, /*strict_p=*/processing_template_decl, |
8c00059c PP |
2657 | /*return_aggregate_cst_ok_p=*/true, |
2658 | /*unshare_p=*/unshare_p); | |
2659 | } | |
2660 | ||
2661 | tree | |
2662 | decl_constant_value (tree decl) | |
2663 | { | |
2664 | return decl_constant_value (decl, /*unshare_p=*/true); | |
8d08fdba MS |
2665 | } |
2666 | \f | |
8d08fdba | 2667 | /* Common subroutines of build_new and build_vec_delete. */ |
8d08fdba | 2668 | \f |
63c9a190 MM |
2669 | /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is |
2670 | the type of the object being allocated; otherwise, it's just TYPE. | |
2671 | INIT is the initializer, if any. USE_GLOBAL_NEW is true if the | |
2672 | user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is | |
c166b898 ILT |
2673 | a vector of arguments to be provided as arguments to a placement |
2674 | new operator. This routine performs no semantic checks; it just | |
2675 | creates and returns a NEW_EXPR. */ | |
a0d5fba7 | 2676 | |
63c9a190 | 2677 | static tree |
87d3f828 PC |
2678 | build_raw_new_expr (location_t loc, vec<tree, va_gc> *placement, tree type, |
2679 | tree nelts, vec<tree, va_gc> *init, int use_global_new) | |
743f140d | 2680 | { |
c166b898 | 2681 | tree init_list; |
63c9a190 | 2682 | tree new_expr; |
3db45ab5 | 2683 | |
c166b898 ILT |
2684 | /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR. |
2685 | If INIT is not NULL, then we want to store VOID_ZERO_NODE. This | |
2686 | permits us to distinguish the case of a missing initializer "new | |
2687 | int" from an empty initializer "new int()". */ | |
2688 | if (init == NULL) | |
2689 | init_list = NULL_TREE; | |
9771b263 | 2690 | else if (init->is_empty ()) |
632f2871 | 2691 | init_list = void_node; |
c166b898 | 2692 | else |
335a120f | 2693 | init_list = build_tree_list_vec (init); |
c166b898 | 2694 | |
87d3f828 PC |
2695 | new_expr = build4_loc (loc, NEW_EXPR, build_pointer_type (type), |
2696 | build_tree_list_vec (placement), type, nelts, | |
2697 | init_list); | |
63c9a190 MM |
2698 | NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new; |
2699 | TREE_SIDE_EFFECTS (new_expr) = 1; | |
2700 | ||
2701 | return new_expr; | |
743f140d PB |
2702 | } |
2703 | ||
9d809e8f FC |
2704 | /* Diagnose uninitialized const members or reference members of type |
2705 | TYPE. USING_NEW is used to disambiguate the diagnostic between a | |
40bb78ad FC |
2706 | new expression without a new-initializer and a declaration. Returns |
2707 | the error count. */ | |
9d809e8f | 2708 | |
40bb78ad | 2709 | static int |
9d809e8f | 2710 | diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin, |
40bb78ad | 2711 | bool using_new, bool complain) |
9d809e8f FC |
2712 | { |
2713 | tree field; | |
40bb78ad | 2714 | int error_count = 0; |
9d809e8f | 2715 | |
10ab8f62 | 2716 | if (type_has_user_provided_constructor (type)) |
40bb78ad | 2717 | return 0; |
10ab8f62 | 2718 | |
910ad8de | 2719 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
9d809e8f FC |
2720 | { |
2721 | tree field_type; | |
2722 | ||
2723 | if (TREE_CODE (field) != FIELD_DECL) | |
2724 | continue; | |
2725 | ||
2726 | field_type = strip_array_types (TREE_TYPE (field)); | |
2727 | ||
1c682d06 FC |
2728 | if (type_has_user_provided_constructor (field_type)) |
2729 | continue; | |
2730 | ||
9f613f06 | 2731 | if (TYPE_REF_P (field_type)) |
9d809e8f | 2732 | { |
40bb78ad FC |
2733 | ++ error_count; |
2734 | if (complain) | |
2735 | { | |
0e02d8e3 PC |
2736 | if (DECL_CONTEXT (field) == origin) |
2737 | { | |
2738 | if (using_new) | |
2739 | error ("uninitialized reference member in %q#T " | |
2740 | "using %<new%> without new-initializer", origin); | |
2741 | else | |
2742 | error ("uninitialized reference member in %q#T", origin); | |
2743 | } | |
40bb78ad | 2744 | else |
0e02d8e3 PC |
2745 | { |
2746 | if (using_new) | |
2747 | error ("uninitialized reference member in base %q#T " | |
2748 | "of %q#T using %<new%> without new-initializer", | |
2749 | DECL_CONTEXT (field), origin); | |
2750 | else | |
2751 | error ("uninitialized reference member in base %q#T " | |
2752 | "of %q#T", DECL_CONTEXT (field), origin); | |
2753 | } | |
40bb78ad | 2754 | inform (DECL_SOURCE_LOCATION (field), |
816551fe | 2755 | "%q#D should be initialized", field); |
40bb78ad | 2756 | } |
9d809e8f FC |
2757 | } |
2758 | ||
2759 | if (CP_TYPE_CONST_P (field_type)) | |
2760 | { | |
40bb78ad FC |
2761 | ++ error_count; |
2762 | if (complain) | |
2763 | { | |
0e02d8e3 PC |
2764 | if (DECL_CONTEXT (field) == origin) |
2765 | { | |
2766 | if (using_new) | |
2767 | error ("uninitialized const member in %q#T " | |
2768 | "using %<new%> without new-initializer", origin); | |
2769 | else | |
2770 | error ("uninitialized const member in %q#T", origin); | |
2771 | } | |
40bb78ad | 2772 | else |
0e02d8e3 PC |
2773 | { |
2774 | if (using_new) | |
2775 | error ("uninitialized const member in base %q#T " | |
2776 | "of %q#T using %<new%> without new-initializer", | |
2777 | DECL_CONTEXT (field), origin); | |
2778 | else | |
2779 | error ("uninitialized const member in base %q#T " | |
2780 | "of %q#T", DECL_CONTEXT (field), origin); | |
2781 | } | |
40bb78ad | 2782 | inform (DECL_SOURCE_LOCATION (field), |
816551fe | 2783 | "%q#D should be initialized", field); |
40bb78ad | 2784 | } |
9d809e8f FC |
2785 | } |
2786 | ||
2787 | if (CLASS_TYPE_P (field_type)) | |
40bb78ad FC |
2788 | error_count |
2789 | += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin, | |
2790 | using_new, complain); | |
9d809e8f | 2791 | } |
40bb78ad | 2792 | return error_count; |
9d809e8f FC |
2793 | } |
2794 | ||
40bb78ad FC |
2795 | int |
2796 | diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain) | |
9d809e8f | 2797 | { |
40bb78ad | 2798 | return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain); |
9d809e8f FC |
2799 | } |
2800 | ||
7d5e76c8 JM |
2801 | /* Call __cxa_bad_array_new_length to indicate that the size calculation |
2802 | overflowed. Pretend it returns sizetype so that it plays nicely in the | |
2803 | COND_EXPR. */ | |
2804 | ||
2805 | tree | |
2806 | throw_bad_array_new_length (void) | |
2807 | { | |
4b4b2e58 NS |
2808 | if (!fn) |
2809 | { | |
2810 | tree name = get_identifier ("__cxa_throw_bad_array_new_length"); | |
2811 | ||
87e3d7cf | 2812 | fn = get_global_binding (name); |
4b4b2e58 NS |
2813 | if (!fn) |
2814 | fn = push_throw_library_fn | |
2815 | (name, build_function_type_list (sizetype, NULL_TREE)); | |
2816 | } | |
7d5e76c8 JM |
2817 | |
2818 | return build_cxx_call (fn, 0, NULL, tf_warning_or_error); | |
2819 | } | |
2820 | ||
e2f5cc96 MS |
2821 | /* Attempt to verify that the argument, OPER, of a placement new expression |
2822 | refers to an object sufficiently large for an object of TYPE or an array | |
2823 | of NELTS of such objects when NELTS is non-null, and issue a warning when | |
2824 | it does not. SIZE specifies the size needed to construct the object or | |
2825 | array and captures the result of NELTS * sizeof (TYPE). (SIZE could be | |
2826 | greater when the array under construction requires a cookie to store | |
2827 | NELTS. GCC's placement new expression stores the cookie when invoking | |
2828 | a user-defined placement new operator function but not the default one. | |
2829 | Placement new expressions with user-defined placement new operator are | |
2830 | not diagnosed since we don't know how they use the buffer (this could | |
2831 | be a future extension). */ | |
2832 | static void | |
2833 | warn_placement_new_too_small (tree type, tree nelts, tree size, tree oper) | |
2834 | { | |
f9d0ca40 | 2835 | location_t loc = cp_expr_loc_or_input_loc (oper); |
e2f5cc96 | 2836 | |
e2f5cc96 MS |
2837 | STRIP_NOPS (oper); |
2838 | ||
2839 | /* Using a function argument or a (non-array) variable as an argument | |
2840 | to placement new is not checked since it's unknown what it might | |
2841 | point to. */ | |
2842 | if (TREE_CODE (oper) == PARM_DECL | |
86287716 | 2843 | || VAR_P (oper) |
e2f5cc96 MS |
2844 | || TREE_CODE (oper) == COMPONENT_REF) |
2845 | return; | |
2846 | ||
2847 | /* Evaluate any constant expressions. */ | |
2848 | size = fold_non_dependent_expr (size); | |
2849 | ||
de05c19d MS |
2850 | access_ref ref; |
2851 | ref.eval = [](tree x){ return fold_non_dependent_expr (x); }; | |
2852 | ref.trail1special = warn_placement_new < 2; | |
2853 | tree objsize = compute_objsize (oper, 1, &ref); | |
2854 | if (!objsize) | |
2855 | return; | |
e2f5cc96 | 2856 | |
790b02af JM |
2857 | /* We can only draw conclusions if ref.deref == -1, |
2858 | i.e. oper is the address of the object. */ | |
2859 | if (ref.deref != -1) | |
2860 | return; | |
2861 | ||
de05c19d MS |
2862 | offset_int bytes_avail = wi::to_offset (objsize); |
2863 | offset_int bytes_need; | |
e2f5cc96 | 2864 | |
de05c19d MS |
2865 | if (CONSTANT_CLASS_P (size)) |
2866 | bytes_need = wi::to_offset (size); | |
2867 | else if (nelts && CONSTANT_CLASS_P (nelts)) | |
2868 | bytes_need = (wi::to_offset (nelts) | |
2869 | * wi::to_offset (TYPE_SIZE_UNIT (type))); | |
2870 | else if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))) | |
2871 | bytes_need = wi::to_offset (TYPE_SIZE_UNIT (type)); | |
2872 | else | |
46cb9332 | 2873 | { |
de05c19d MS |
2874 | /* The type is a VLA. */ |
2875 | return; | |
46cb9332 | 2876 | } |
e2f5cc96 | 2877 | |
de05c19d MS |
2878 | if (bytes_avail >= bytes_need) |
2879 | return; | |
e2f5cc96 | 2880 | |
de05c19d MS |
2881 | /* True when the size to mention in the warning is exact as opposed |
2882 | to "at least N". */ | |
2883 | const bool exact_size = (ref.offrng[0] == ref.offrng[1] | |
2884 | || ref.sizrng[1] - ref.offrng[0] == 0); | |
2885 | ||
2886 | tree opertype = ref.ref ? TREE_TYPE (ref.ref) : TREE_TYPE (oper); | |
2887 | bool warned = false; | |
2888 | if (nelts) | |
2889 | nelts = fold_for_warn (nelts); | |
2890 | if (nelts) | |
2891 | if (CONSTANT_CLASS_P (nelts)) | |
2892 | warned = warning_at (loc, OPT_Wplacement_new_, | |
2893 | (exact_size | |
2894 | ? G_("placement new constructing an object " | |
2895 | "of type %<%T [%wu]%> and size %qwu " | |
2896 | "in a region of type %qT and size %qwi") | |
2897 | : G_("placement new constructing an object " | |
2898 | "of type %<%T [%wu]%> and size %qwu " | |
2899 | "in a region of type %qT and size " | |
2900 | "at most %qwu")), | |
2901 | type, tree_to_uhwi (nelts), | |
2902 | bytes_need.to_uhwi (), | |
2903 | opertype, bytes_avail.to_uhwi ()); | |
2904 | else | |
2905 | warned = warning_at (loc, OPT_Wplacement_new_, | |
2906 | (exact_size | |
2907 | ? G_("placement new constructing an array " | |
2908 | "of objects of type %qT and size %qwu " | |
2909 | "in a region of type %qT and size %qwi") | |
2910 | : G_("placement new constructing an array " | |
2911 | "of objects of type %qT and size %qwu " | |
2912 | "in a region of type %qT and size " | |
2913 | "at most %qwu")), | |
2914 | type, bytes_need.to_uhwi (), opertype, | |
2915 | bytes_avail.to_uhwi ()); | |
2916 | else | |
2917 | warned = warning_at (loc, OPT_Wplacement_new_, | |
2918 | (exact_size | |
2919 | ? G_("placement new constructing an object " | |
2920 | "of type %qT and size %qwu in a region " | |
2921 | "of type %qT and size %qwi") | |
2922 | : G_("placement new constructing an object " | |
2923 | "of type %qT " | |
2924 | "and size %qwu in a region of type %qT " | |
2925 | "and size at most %qwu")), | |
2926 | type, bytes_need.to_uhwi (), opertype, | |
2927 | bytes_avail.to_uhwi ()); | |
2928 | ||
2929 | if (!warned || !ref.ref) | |
2930 | return; | |
e2f5cc96 | 2931 | |
de05c19d MS |
2932 | if (ref.offrng[0] == 0 || !ref.offset_bounded ()) |
2933 | /* Avoid mentioning the offset when its lower bound is zero | |
2934 | or when it's impossibly large. */ | |
2935 | inform (DECL_SOURCE_LOCATION (ref.ref), | |
2936 | "%qD declared here", ref.ref); | |
2937 | else if (ref.offrng[0] == ref.offrng[1]) | |
2938 | inform (DECL_SOURCE_LOCATION (ref.ref), | |
2939 | "at offset %wi from %qD declared here", | |
2940 | ref.offrng[0].to_shwi (), ref.ref); | |
2941 | else | |
2942 | inform (DECL_SOURCE_LOCATION (ref.ref), | |
2943 | "at offset [%wi, %wi] from %qD declared here", | |
2944 | ref.offrng[0].to_shwi (), ref.offrng[1].to_shwi (), ref.ref); | |
e2f5cc96 MS |
2945 | } |
2946 | ||
af63ba4b JM |
2947 | /* True if alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__. */ |
2948 | ||
2949 | bool | |
2950 | type_has_new_extended_alignment (tree t) | |
2951 | { | |
2e1c20b1 JJ |
2952 | return (aligned_new_threshold |
2953 | && TYPE_ALIGN_UNIT (t) > (unsigned)aligned_new_threshold); | |
af63ba4b JM |
2954 | } |
2955 | ||
2ec69f56 JM |
2956 | /* Return the alignment we expect malloc to guarantee. This should just be |
2957 | MALLOC_ABI_ALIGNMENT, but that macro defaults to only BITS_PER_WORD for some | |
2958 | reason, so don't let the threshold be smaller than max_align_t_align. */ | |
2959 | ||
2960 | unsigned | |
2961 | malloc_alignment () | |
2962 | { | |
2963 | return MAX (max_align_t_align(), MALLOC_ABI_ALIGNMENT); | |
2964 | } | |
2965 | ||
ece3b7e6 | 2966 | /* Determine whether an allocation function is a namespace-scope |
cf9847d2 | 2967 | non-replaceable placement new function. See DR 1748. */ |
8bee092e JJ |
2968 | static bool |
2969 | std_placement_new_fn_p (tree alloc_fn) | |
ece3b7e6 | 2970 | { |
8f2b097e | 2971 | if (DECL_NAMESPACE_SCOPE_P (alloc_fn)) |
ece3b7e6 VV |
2972 | { |
2973 | tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn))); | |
2974 | if ((TREE_VALUE (first_arg) == ptr_type_node) | |
2975 | && TREE_CHAIN (first_arg) == void_list_node) | |
2976 | return true; | |
2977 | } | |
2978 | return false; | |
2979 | } | |
2980 | ||
8e007055 | 2981 | /* For element type ELT_TYPE, return the appropriate type of the heap object |
0a0c2c3f JJ |
2982 | containing such element(s). COOKIE_SIZE is the size of cookie in bytes. |
2983 | Return | |
8e007055 | 2984 | struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; } |
0a0c2c3f JJ |
2985 | where N is nothing (flexible array member) if ITYPE2 is NULL, otherwise |
2986 | the array has ITYPE2 as its TYPE_DOMAIN. */ | |
8e007055 JJ |
2987 | |
2988 | tree | |
0a0c2c3f | 2989 | build_new_constexpr_heap_type (tree elt_type, tree cookie_size, tree itype2) |
8e007055 | 2990 | { |
0a0c2c3f JJ |
2991 | gcc_assert (tree_fits_uhwi_p (cookie_size)); |
2992 | unsigned HOST_WIDE_INT csz = tree_to_uhwi (cookie_size); | |
8e007055 JJ |
2993 | csz /= int_size_in_bytes (sizetype); |
2994 | tree itype1 = build_index_type (size_int (csz - 1)); | |
2995 | tree atype1 = build_cplus_array_type (sizetype, itype1); | |
2996 | tree atype2 = build_cplus_array_type (elt_type, itype2); | |
2997 | tree rtype = cxx_make_type (RECORD_TYPE); | |
2998 | TYPE_NAME (rtype) = heap_identifier; | |
2999 | tree fld1 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype1); | |
3000 | tree fld2 = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, atype2); | |
3001 | DECL_FIELD_CONTEXT (fld1) = rtype; | |
3002 | DECL_FIELD_CONTEXT (fld2) = rtype; | |
3003 | DECL_ARTIFICIAL (fld1) = true; | |
3004 | DECL_ARTIFICIAL (fld2) = true; | |
3005 | TYPE_FIELDS (rtype) = fld1; | |
3006 | DECL_CHAIN (fld1) = fld2; | |
3007 | layout_type (rtype); | |
3008 | return rtype; | |
3009 | } | |
3010 | ||
3011 | /* Help the constexpr code to find the right type for the heap variable | |
3012 | by adding a NOP_EXPR around ALLOC_CALL if needed for cookie_size. | |
3013 | Return ALLOC_CALL or ALLOC_CALL cast to a pointer to | |
3014 | struct { size_t[cookie_size/sizeof(size_t)]; elt_type[]; }. */ | |
3015 | ||
3016 | static tree | |
3017 | maybe_wrap_new_for_constexpr (tree alloc_call, tree elt_type, tree cookie_size) | |
3018 | { | |
b04445d4 | 3019 | if (cxx_dialect < cxx20) |
8e007055 JJ |
3020 | return alloc_call; |
3021 | ||
3022 | if (current_function_decl != NULL_TREE | |
3023 | && !DECL_DECLARED_CONSTEXPR_P (current_function_decl)) | |
3024 | return alloc_call; | |
3025 | ||
3026 | tree call_expr = extract_call_expr (alloc_call); | |
3027 | if (call_expr == error_mark_node) | |
3028 | return alloc_call; | |
3029 | ||
3030 | tree alloc_call_fndecl = cp_get_callee_fndecl_nofold (call_expr); | |
3031 | if (alloc_call_fndecl == NULL_TREE | |
3032 | || !IDENTIFIER_NEW_OP_P (DECL_NAME (alloc_call_fndecl)) | |
3033 | || CP_DECL_CONTEXT (alloc_call_fndecl) != global_namespace) | |
3034 | return alloc_call; | |
3035 | ||
3036 | tree rtype = build_new_constexpr_heap_type (elt_type, cookie_size, | |
3037 | NULL_TREE); | |
3038 | return build_nop (build_pointer_type (rtype), alloc_call); | |
3039 | } | |
3040 | ||
63c9a190 MM |
3041 | /* Generate code for a new-expression, including calling the "operator |
3042 | new" function, initializing the object, and, if an exception occurs | |
3043 | during construction, cleaning up. The arguments are as for | |
685c8340 MS |
3044 | build_raw_new_expr. This may change PLACEMENT and INIT. |
3045 | TYPE is the type of the object being constructed, possibly an array | |
3046 | of NELTS elements when NELTS is non-null (in "new T[NELTS]", T may | |
3047 | be an array of the form U[inner], with the whole expression being | |
3048 | "new U[NELTS][inner]"). */ | |
a0d5fba7 | 3049 | |
834c6dff | 3050 | static tree |
9771b263 DN |
3051 | build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts, |
3052 | vec<tree, va_gc> **init, bool globally_qualified_p, | |
c166b898 | 3053 | tsubst_flags_t complain) |
a0d5fba7 | 3054 | { |
d746e87d MM |
3055 | tree size, rval; |
3056 | /* True iff this is a call to "operator new[]" instead of just | |
c8094d83 | 3057 | "operator new". */ |
d746e87d | 3058 | bool array_p = false; |
9207099b JM |
3059 | /* If ARRAY_P is true, the element type of the array. This is never |
3060 | an ARRAY_TYPE; for something like "new int[3][4]", the | |
d746e87d | 3061 | ELT_TYPE is "int". If ARRAY_P is false, this is the same type as |
9207099b | 3062 | TYPE. */ |
d746e87d | 3063 | tree elt_type; |
f4f4610e MM |
3064 | /* The type of the new-expression. (This type is always a pointer |
3065 | type.) */ | |
3066 | tree pointer_type; | |
25357d1e | 3067 | tree non_const_pointer_type; |
685c8340 | 3068 | /* The most significant array bound in int[OUTER_NELTS][inner]. */ |
a48cccea | 3069 | tree outer_nelts = NULL_TREE; |
685c8340 MS |
3070 | /* For arrays with a non-constant number of elements, a bounds checks |
3071 | on the NELTS parameter to avoid integer overflow at runtime. */ | |
4a84253c | 3072 | tree outer_nelts_check = NULL_TREE; |
4ebc46e9 | 3073 | bool outer_nelts_from_type = false; |
685c8340 | 3074 | /* Number of the "inner" elements in "new T[OUTER_NELTS][inner]". */ |
807e902e | 3075 | offset_int inner_nelts_count = 1; |
f4f4610e | 3076 | tree alloc_call, alloc_expr; |
685c8340 | 3077 | /* Size of the inner array elements (those with constant dimensions). */ |
807e902e | 3078 | offset_int inner_size; |
f4f4610e MM |
3079 | /* The address returned by the call to "operator new". This node is |
3080 | a VAR_DECL and is therefore reusable. */ | |
3081 | tree alloc_node; | |
46ff5047 | 3082 | tree alloc_fn; |
8b5e2ce4 | 3083 | tree cookie_expr, init_expr; |
089d6ea7 | 3084 | int nothrow, check_new; |
834c6dff MM |
3085 | /* If non-NULL, the number of extra bytes to allocate at the |
3086 | beginning of the storage allocated for an array-new expression in | |
3087 | order to store the number of elements. */ | |
3088 | tree cookie_size = NULL_TREE; | |
c166b898 | 3089 | tree placement_first; |
a9de800a | 3090 | tree placement_expr = NULL_TREE; |
3f41ffd8 MM |
3091 | /* True if the function we are calling is a placement allocation |
3092 | function. */ | |
3093 | bool placement_allocation_fn_p; | |
f4f4610e | 3094 | /* True if the storage must be initialized, either by a constructor |
34cd5ae7 | 3095 | or due to an explicit new-initializer. */ |
f4f4610e MM |
3096 | bool is_initialized; |
3097 | /* The address of the thing allocated, not including any cookie. In | |
3098 | particular, if an array cookie is in use, DATA_ADDR is the | |
3099 | address of the first array element. This node is a VAR_DECL, and | |
3100 | is therefore reusable. */ | |
3101 | tree data_addr; | |
b75bf8b1 | 3102 | tree orig_type = type; |
a0d5fba7 | 3103 | |
058b15c1 | 3104 | if (nelts) |
a0d5fba7 | 3105 | { |
058b15c1 | 3106 | outer_nelts = nelts; |
d746e87d | 3107 | array_p = true; |
a0d5fba7 | 3108 | } |
9207099b | 3109 | else if (TREE_CODE (type) == ARRAY_TYPE) |
d746e87d | 3110 | { |
4ebc46e9 FW |
3111 | /* Transforms new (T[N]) to new T[N]. The former is a GNU |
3112 | extension for variable N. (This also covers new T where T is | |
3113 | a VLA typedef.) */ | |
9207099b JM |
3114 | array_p = true; |
3115 | nelts = array_type_nelts_top (type); | |
3116 | outer_nelts = nelts; | |
3117 | type = TREE_TYPE (type); | |
4ebc46e9 | 3118 | outer_nelts_from_type = true; |
d746e87d | 3119 | } |
834c6dff | 3120 | |
ea219e6e | 3121 | /* Lots of logic below depends on whether we have a constant number of |
cda0a029 | 3122 | elements, so go ahead and fold it now. */ |
e56f6629 | 3123 | const_tree cst_outer_nelts = fold_non_dependent_expr (outer_nelts, complain); |
cda0a029 | 3124 | |
8d08fdba MS |
3125 | /* If our base type is an array, then make sure we know how many elements |
3126 | it has. */ | |
d746e87d MM |
3127 | for (elt_type = type; |
3128 | TREE_CODE (elt_type) == ARRAY_TYPE; | |
3129 | elt_type = TREE_TYPE (elt_type)) | |
4ebc46e9 FW |
3130 | { |
3131 | tree inner_nelts = array_type_nelts_top (elt_type); | |
3132 | tree inner_nelts_cst = maybe_constant_value (inner_nelts); | |
e1f10dd9 | 3133 | if (TREE_CODE (inner_nelts_cst) == INTEGER_CST) |
4a84253c | 3134 | { |
4a669ac3 | 3135 | wi::overflow_type overflow; |
807e902e KZ |
3136 | offset_int result = wi::mul (wi::to_offset (inner_nelts_cst), |
3137 | inner_nelts_count, SIGNED, &overflow); | |
9be0ac8c | 3138 | if (overflow) |
4a84253c FW |
3139 | { |
3140 | if (complain & tf_error) | |
3141 | error ("integer overflow in array size"); | |
3142 | nelts = error_mark_node; | |
3143 | } | |
3144 | inner_nelts_count = result; | |
3145 | } | |
3146 | else | |
4ebc46e9 FW |
3147 | { |
3148 | if (complain & tf_error) | |
3149 | { | |
f9d0ca40 | 3150 | error_at (cp_expr_loc_or_input_loc (inner_nelts), |
b75bf8b1 | 3151 | "array size in new-expression must be constant"); |
4ebc46e9 FW |
3152 | cxx_constant_value(inner_nelts); |
3153 | } | |
3154 | nelts = error_mark_node; | |
3155 | } | |
3156 | if (nelts != error_mark_node) | |
3157 | nelts = cp_build_binary_op (input_location, | |
3158 | MULT_EXPR, nelts, | |
3159 | inner_nelts_cst, | |
3160 | complain); | |
3161 | } | |
3162 | ||
02a32ab4 RS |
3163 | if (!verify_type_context (input_location, TCTX_ALLOCATION, elt_type, |
3164 | !(complain & tf_error))) | |
3165 | return error_mark_node; | |
3166 | ||
4ebc46e9 FW |
3167 | if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error)) |
3168 | { | |
b75bf8b1 | 3169 | error ("variably modified type not allowed in new-expression"); |
4ebc46e9 FW |
3170 | return error_mark_node; |
3171 | } | |
3172 | ||
3173 | if (nelts == error_mark_node) | |
3174 | return error_mark_node; | |
3175 | ||
3176 | /* Warn if we performed the (T[N]) to T[N] transformation and N is | |
3177 | variable. */ | |
3178 | if (outer_nelts_from_type | |
ea219e6e | 3179 | && !TREE_CONSTANT (cst_outer_nelts)) |
4ebc46e9 FW |
3180 | { |
3181 | if (complain & tf_warning_or_error) | |
b75bf8b1 | 3182 | { |
f9d0ca40 | 3183 | pedwarn (cp_expr_loc_or_input_loc (outer_nelts), OPT_Wvla, |
24f12823 | 3184 | typedef_variant_p (orig_type) |
324ff1a0 | 3185 | ? G_("non-constant array new length must be specified " |
a9c697b8 | 3186 | "directly, not by %<typedef%>") |
24f12823 VR |
3187 | : G_("non-constant array new length must be specified " |
3188 | "without parentheses around the type-id")); | |
b75bf8b1 | 3189 | } |
4ebc46e9 FW |
3190 | else |
3191 | return error_mark_node; | |
3192 | } | |
5566b478 | 3193 | |
50e10fa8 | 3194 | if (VOID_TYPE_P (elt_type)) |
e1cd6e56 | 3195 | { |
5ade1ed2 | 3196 | if (complain & tf_error) |
a9c697b8 | 3197 | error ("invalid type %<void%> for %<new%>"); |
e1cd6e56 MS |
3198 | return error_mark_node; |
3199 | } | |
3200 | ||
f620e64a | 3201 | if (is_std_init_list (elt_type) && !cp_unevaluated_operand) |
04eb9c55 | 3202 | warning (OPT_Winit_list_lifetime, |
a9c697b8 | 3203 | "%<new%> of %<initializer_list%> does not " |
04eb9c55 JM |
3204 | "extend the lifetime of the underlying array"); |
3205 | ||
c17fa0f2 | 3206 | if (abstract_virtuals_error (ACU_NEW, elt_type, complain)) |
a7a64a77 | 3207 | return error_mark_node; |
8926095f | 3208 | |
95552437 | 3209 | is_initialized = (type_build_ctor_call (elt_type) || *init != NULL); |
b87d79e6 | 3210 | |
eca7fc57 | 3211 | if (*init == NULL && cxx_dialect < cxx11) |
9d809e8f | 3212 | { |
40bb78ad | 3213 | bool maybe_uninitialized_error = false; |
9d809e8f FC |
3214 | /* A program that calls for default-initialization [...] of an |
3215 | entity of reference type is ill-formed. */ | |
3216 | if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type)) | |
40bb78ad | 3217 | maybe_uninitialized_error = true; |
9d809e8f FC |
3218 | |
3219 | /* A new-expression that creates an object of type T initializes | |
3220 | that object as follows: | |
3221 | - If the new-initializer is omitted: | |
3222 | -- If T is a (possibly cv-qualified) non-POD class type | |
3223 | (or array thereof), the object is default-initialized (8.5). | |
3224 | [...] | |
3225 | -- Otherwise, the object created has indeterminate | |
3226 | value. If T is a const-qualified type, or a (possibly | |
3227 | cv-qualified) POD class type (or array thereof) | |
3228 | containing (directly or indirectly) a member of | |
3229 | const-qualified type, the program is ill-formed; */ | |
3230 | ||
3231 | if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type)) | |
40bb78ad | 3232 | maybe_uninitialized_error = true; |
9d809e8f | 3233 | |
40bb78ad FC |
3234 | if (maybe_uninitialized_error |
3235 | && diagnose_uninitialized_cst_or_ref_member (elt_type, | |
3236 | /*using_new=*/true, | |
3237 | complain & tf_error)) | |
3238 | return error_mark_node; | |
9d809e8f FC |
3239 | } |
3240 | ||
c166b898 | 3241 | if (CP_TYPE_CONST_P (elt_type) && *init == NULL |
6132bdd7 | 3242 | && default_init_uninitialized_part (elt_type)) |
f4f4610e | 3243 | { |
5ade1ed2 DG |
3244 | if (complain & tf_error) |
3245 | error ("uninitialized const in %<new%> of %q#T", elt_type); | |
f4f4610e MM |
3246 | return error_mark_node; |
3247 | } | |
3248 | ||
d746e87d MM |
3249 | size = size_in_bytes (elt_type); |
3250 | if (array_p) | |
4a84253c FW |
3251 | { |
3252 | /* Maximum available size in bytes. Half of the address space | |
3253 | minus the cookie size. */ | |
807e902e KZ |
3254 | offset_int max_size |
3255 | = wi::set_bit_in_zero <offset_int> (TYPE_PRECISION (sizetype) - 1); | |
4a84253c | 3256 | /* Maximum number of outer elements which can be allocated. */ |
807e902e | 3257 | offset_int max_outer_nelts; |
4a84253c FW |
3258 | tree max_outer_nelts_tree; |
3259 | ||
3260 | gcc_assert (TREE_CODE (size) == INTEGER_CST); | |
3261 | cookie_size = targetm.cxx.get_cookie_size (elt_type); | |
3262 | gcc_assert (TREE_CODE (cookie_size) == INTEGER_CST); | |
807e902e | 3263 | gcc_checking_assert (wi::ltu_p (wi::to_offset (cookie_size), max_size)); |
c65cb8d1 | 3264 | /* Unconditionally subtract the cookie size. This decreases the |
4a84253c FW |
3265 | maximum object size and is safe even if we choose not to use |
3266 | a cookie after all. */ | |
807e902e | 3267 | max_size -= wi::to_offset (cookie_size); |
4a669ac3 | 3268 | wi::overflow_type overflow; |
807e902e KZ |
3269 | inner_size = wi::mul (wi::to_offset (size), inner_nelts_count, SIGNED, |
3270 | &overflow); | |
3271 | if (overflow || wi::gtu_p (inner_size, max_size)) | |
4a84253c FW |
3272 | { |
3273 | if (complain & tf_error) | |
28a8cef1 MS |
3274 | { |
3275 | cst_size_error error; | |
3276 | if (overflow) | |
3277 | error = cst_size_overflow; | |
3278 | else | |
3279 | { | |
3280 | error = cst_size_too_big; | |
3281 | size = size_binop (MULT_EXPR, size, | |
3282 | wide_int_to_tree (sizetype, | |
3283 | inner_nelts_count)); | |
3284 | size = cp_fully_fold (size); | |
3285 | } | |
3286 | invalid_array_size_error (input_location, error, size, | |
3287 | /*name=*/NULL_TREE); | |
3288 | } | |
4a84253c FW |
3289 | return error_mark_node; |
3290 | } | |
807e902e KZ |
3291 | |
3292 | max_outer_nelts = wi::udiv_trunc (max_size, inner_size); | |
807e902e | 3293 | max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts); |
4a84253c | 3294 | |
6ff8b93c | 3295 | size = build2 (MULT_EXPR, sizetype, size, nelts); |
685c8340 | 3296 | |
ea219e6e | 3297 | if (TREE_CODE (cst_outer_nelts) == INTEGER_CST) |
685c8340 | 3298 | { |
ea219e6e | 3299 | if (tree_int_cst_lt (max_outer_nelts_tree, cst_outer_nelts)) |
685c8340 MS |
3300 | { |
3301 | /* When the array size is constant, check it at compile time | |
3302 | to make sure it doesn't exceed the implementation-defined | |
3303 | maximum, as required by C++ 14 (in C++ 11 this requirement | |
3304 | isn't explicitly stated but it's enforced anyway -- see | |
e53b6e56 | 3305 | grokdeclarator in cp/decl.cc). */ |
685c8340 | 3306 | if (complain & tf_error) |
28a8cef1 MS |
3307 | { |
3308 | size = cp_fully_fold (size); | |
3309 | invalid_array_size_error (input_location, cst_size_too_big, | |
3310 | size, NULL_TREE); | |
3311 | } | |
685c8340 MS |
3312 | return error_mark_node; |
3313 | } | |
3314 | } | |
3315 | else | |
3316 | { | |
3317 | /* When a runtime check is necessary because the array size | |
3318 | isn't constant, keep only the top-most seven bits (starting | |
3319 | with the most significant non-zero bit) of the maximum size | |
3320 | to compare the array size against, to simplify encoding the | |
3321 | constant maximum size in the instruction stream. */ | |
3322 | ||
3323 | unsigned shift = (max_outer_nelts.get_precision ()) - 7 | |
3324 | - wi::clz (max_outer_nelts); | |
8de73453 | 3325 | max_outer_nelts = (max_outer_nelts >> shift) << shift; |
685c8340 | 3326 | |
6ff8b93c PP |
3327 | outer_nelts_check = build2 (LE_EXPR, boolean_type_node, |
3328 | outer_nelts, | |
3329 | max_outer_nelts_tree); | |
685c8340 | 3330 | } |
4a84253c | 3331 | } |
a28e3c7f | 3332 | |
af63ba4b JM |
3333 | tree align_arg = NULL_TREE; |
3334 | if (type_has_new_extended_alignment (elt_type)) | |
19b87a06 JM |
3335 | { |
3336 | unsigned align = TYPE_ALIGN_UNIT (elt_type); | |
3337 | /* Also consider the alignment of the cookie, if any. */ | |
164c6a1c | 3338 | if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)) |
19b87a06 JM |
3339 | align = MAX (align, TYPE_ALIGN_UNIT (size_type_node)); |
3340 | align_arg = build_int_cst (align_type_node, align); | |
3341 | } | |
af63ba4b | 3342 | |
63c9a190 MM |
3343 | alloc_fn = NULL_TREE; |
3344 | ||
c166b898 ILT |
3345 | /* If PLACEMENT is a single simple pointer type not passed by |
3346 | reference, prepare to capture it in a temporary variable. Do | |
3347 | this now, since PLACEMENT will change in the calls below. */ | |
c166b898 | 3348 | placement_first = NULL_TREE; |
9771b263 | 3349 | if (vec_safe_length (*placement) == 1 |
50e10fa8 | 3350 | && (TYPE_PTR_P (TREE_TYPE ((**placement)[0])))) |
9771b263 | 3351 | placement_first = (**placement)[0]; |
c166b898 | 3352 | |
e2f5cc96 MS |
3353 | bool member_new_p = false; |
3354 | ||
e92cc029 | 3355 | /* Allocate the object. */ |
bfecd57c JJ |
3356 | tree fnname; |
3357 | tree fns; | |
6de9cd9a | 3358 | |
88a819be | 3359 | fnname = ovl_op_identifier (false, array_p ? VEC_NEW_EXPR : NEW_EXPR); |
71b71b96 | 3360 | |
bfecd57c JJ |
3361 | member_new_p = !globally_qualified_p |
3362 | && CLASS_TYPE_P (elt_type) | |
3363 | && (array_p | |
3364 | ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type) | |
3365 | : TYPE_HAS_NEW_OPERATOR (elt_type)); | |
a3d536f1 | 3366 | |
6cd51207 JM |
3367 | bool member_delete_p = (!globally_qualified_p |
3368 | && CLASS_TYPE_P (elt_type) | |
3369 | && (array_p | |
3370 | ? TYPE_GETS_VEC_DELETE (elt_type) | |
3371 | : TYPE_GETS_REG_DELETE (elt_type))); | |
3372 | ||
bfecd57c JJ |
3373 | if (member_new_p) |
3374 | { | |
3375 | /* Use a class-specific operator new. */ | |
3376 | /* If a cookie is required, add some extra space. */ | |
3377 | if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)) | |
d80a26cc | 3378 | size = build2 (PLUS_EXPR, sizetype, size, cookie_size); |
bfecd57c | 3379 | else |
b1e5b86c | 3380 | { |
bfecd57c JJ |
3381 | cookie_size = NULL_TREE; |
3382 | /* No size arithmetic necessary, so the size check is | |
3383 | not needed. */ | |
3384 | if (outer_nelts_check != NULL && inner_size == 1) | |
3385 | outer_nelts_check = NULL_TREE; | |
3386 | } | |
3387 | /* Perform the overflow check. */ | |
3388 | tree errval = TYPE_MAX_VALUE (sizetype); | |
3389 | if (cxx_dialect >= cxx11 && flag_exceptions) | |
3390 | errval = throw_bad_array_new_length (); | |
3391 | if (outer_nelts_check != NULL_TREE) | |
d80a26cc PP |
3392 | size = build3 (COND_EXPR, sizetype, outer_nelts_check, size, errval); |
3393 | size = cp_fully_fold (size); | |
bfecd57c JJ |
3394 | /* Create the argument list. */ |
3395 | vec_safe_insert (*placement, 0, size); | |
3396 | /* Do name-lookup to find the appropriate operator. */ | |
098cf31a | 3397 | fns = lookup_fnfields (elt_type, fnname, /*protect=*/2, complain); |
bfecd57c | 3398 | if (fns == NULL_TREE) |
b1e5b86c | 3399 | { |
bfecd57c JJ |
3400 | if (complain & tf_error) |
3401 | error ("no suitable %qD found in class %qT", fnname, elt_type); | |
6961a592 GB |
3402 | return error_mark_node; |
3403 | } | |
bfecd57c | 3404 | if (TREE_CODE (fns) == TREE_LIST) |
dd125026 JJ |
3405 | { |
3406 | if (complain & tf_error) | |
bfecd57c JJ |
3407 | { |
3408 | error ("request for member %qD is ambiguous", fnname); | |
3409 | print_candidates (fns); | |
3410 | } | |
dd125026 JJ |
3411 | return error_mark_node; |
3412 | } | |
bfecd57c JJ |
3413 | tree dummy = build_dummy_object (elt_type); |
3414 | alloc_call = NULL_TREE; | |
3415 | if (align_arg) | |
3416 | { | |
3417 | vec<tree, va_gc> *align_args | |
3418 | = vec_copy_and_insert (*placement, align_arg, 1); | |
3419 | alloc_call | |
3420 | = build_new_method_call (dummy, fns, &align_args, | |
3421 | /*conversion_path=*/NULL_TREE, | |
3422 | LOOKUP_NORMAL, &alloc_fn, tf_none); | |
3423 | /* If no matching function is found and the allocated object type | |
3424 | has new-extended alignment, the alignment argument is removed | |
3425 | from the argument list, and overload resolution is performed | |
3426 | again. */ | |
3427 | if (alloc_call == error_mark_node) | |
3428 | alloc_call = NULL_TREE; | |
3429 | } | |
3430 | if (!alloc_call) | |
3431 | alloc_call = build_new_method_call (dummy, fns, placement, | |
3432 | /*conversion_path=*/NULL_TREE, | |
3433 | LOOKUP_NORMAL, | |
3434 | &alloc_fn, complain); | |
360f866c | 3435 | } |
8d08fdba MS |
3436 | else |
3437 | { | |
bfecd57c JJ |
3438 | /* Use a global operator new. */ |
3439 | /* See if a cookie might be required. */ | |
3440 | if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))) | |
089d6ea7 | 3441 | { |
bfecd57c JJ |
3442 | cookie_size = NULL_TREE; |
3443 | /* No size arithmetic necessary, so the size check is | |
3444 | not needed. */ | |
3445 | if (outer_nelts_check != NULL && inner_size == 1) | |
3446 | outer_nelts_check = NULL_TREE; | |
089d6ea7 | 3447 | } |
125e6594 | 3448 | |
d80a26cc | 3449 | size = cp_fully_fold (size); |
0a0c2c3f JJ |
3450 | /* If size is zero e.g. due to type having zero size, try to |
3451 | preserve outer_nelts for constant expression evaluation | |
3452 | purposes. */ | |
3453 | if (integer_zerop (size) && outer_nelts) | |
3454 | size = build2 (MULT_EXPR, TREE_TYPE (size), size, outer_nelts); | |
3455 | ||
bfecd57c JJ |
3456 | alloc_call = build_operator_new_call (fnname, placement, |
3457 | &size, &cookie_size, | |
3458 | align_arg, outer_nelts_check, | |
3459 | &alloc_fn, complain); | |
8d08fdba MS |
3460 | } |
3461 | ||
96790071 | 3462 | if (alloc_call == error_mark_node) |
2bb5d995 JM |
3463 | return error_mark_node; |
3464 | ||
63c9a190 MM |
3465 | gcc_assert (alloc_fn != NULL_TREE); |
3466 | ||
2ec69f56 JM |
3467 | /* Now, check to see if this function is actually a placement |
3468 | allocation function. This can happen even when PLACEMENT is NULL | |
3469 | because we might have something like: | |
3470 | ||
3471 | struct S { void* operator new (size_t, int i = 0); }; | |
3472 | ||
3473 | A call to `new S' will get this allocation function, even though | |
3474 | there is no explicit placement argument. If there is more than | |
3475 | one argument, or there are variable arguments, then this is a | |
3476 | placement allocation function. */ | |
3477 | placement_allocation_fn_p | |
3478 | = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1 | |
3479 | || varargs_function_p (alloc_fn)); | |
3480 | ||
f2da9e26 NS |
3481 | if (complain & tf_warning_or_error |
3482 | && warn_aligned_new | |
2ec69f56 JM |
3483 | && !placement_allocation_fn_p |
3484 | && TYPE_ALIGN (elt_type) > malloc_alignment () | |
af63ba4b JM |
3485 | && (warn_aligned_new > 1 |
3486 | || CP_DECL_CONTEXT (alloc_fn) == global_namespace) | |
3487 | && !aligned_allocation_fn_p (alloc_fn)) | |
3488 | { | |
097f82ec | 3489 | auto_diagnostic_group d; |
34d57a10 JW |
3490 | if (warning (OPT_Waligned_new_, "%<new%> of type %qT with extended " |
3491 | "alignment %d", elt_type, TYPE_ALIGN_UNIT (elt_type))) | |
3492 | { | |
3493 | inform (input_location, "uses %qD, which does not have an alignment " | |
3494 | "parameter", alloc_fn); | |
3495 | if (!aligned_new_threshold) | |
3496 | inform (input_location, "use %<-faligned-new%> to enable C++17 " | |
3497 | "over-aligned new support"); | |
3498 | } | |
af63ba4b JM |
3499 | } |
3500 | ||
c166b898 ILT |
3501 | /* If we found a simple case of PLACEMENT_EXPR above, then copy it |
3502 | into a temporary variable. */ | |
a9de800a | 3503 | if (!processing_template_decl |
a9de800a JJ |
3504 | && TREE_CODE (alloc_call) == CALL_EXPR |
3505 | && call_expr_nargs (alloc_call) == 2 | |
3506 | && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE | |
50e10fa8 | 3507 | && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1)))) |
a9de800a | 3508 | { |
e2f5cc96 | 3509 | tree placement = CALL_EXPR_ARG (alloc_call, 1); |
a9de800a | 3510 | |
e2f5cc96 MS |
3511 | if (placement_first != NULL_TREE |
3512 | && (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement))) | |
3513 | || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement))))) | |
a9de800a | 3514 | { |
c166b898 | 3515 | placement_expr = get_target_expr (placement_first); |
a9de800a | 3516 | CALL_EXPR_ARG (alloc_call, 1) |
cda0a029 | 3517 | = fold_convert (TREE_TYPE (placement), placement_expr); |
e2f5cc96 MS |
3518 | } |
3519 | ||
3520 | if (!member_new_p | |
3521 | && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))))) | |
3522 | { | |
3523 | /* Attempt to make the warning point at the operator new argument. */ | |
3524 | if (placement_first) | |
3525 | placement = placement_first; | |
3526 | ||
3527 | warn_placement_new_too_small (orig_type, nelts, size, placement); | |
a9de800a JJ |
3528 | } |
3529 | } | |
3530 | ||
19b87a06 | 3531 | alloc_expr = alloc_call; |
8e007055 | 3532 | if (cookie_size) |
19b87a06 | 3533 | alloc_expr = maybe_wrap_new_for_constexpr (alloc_expr, type, |
8e007055 JJ |
3534 | cookie_size); |
3535 | ||
a6111661 JM |
3536 | /* In the simple case, we can stop now. */ |
3537 | pointer_type = build_pointer_type (type); | |
6cd51207 | 3538 | if (!cookie_size && !is_initialized && !member_delete_p) |
19b87a06 | 3539 | return build_nop (pointer_type, alloc_expr); |
a6111661 | 3540 | |
10ee5386 JM |
3541 | /* Store the result of the allocation call in a variable so that we can |
3542 | use it more than once. */ | |
19b87a06 | 3543 | alloc_expr = get_target_expr (alloc_expr); |
a6111661 JM |
3544 | alloc_node = TARGET_EXPR_SLOT (alloc_expr); |
3545 | ||
3546 | /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */ | |
c8094d83 | 3547 | while (TREE_CODE (alloc_call) == COMPOUND_EXPR) |
a6111661 | 3548 | alloc_call = TREE_OPERAND (alloc_call, 1); |
089d6ea7 | 3549 | |
a6111661 JM |
3550 | /* Preevaluate the placement args so that we don't reevaluate them for a |
3551 | placement delete. */ | |
3552 | if (placement_allocation_fn_p) | |
3553 | { | |
6de9cd9a DN |
3554 | tree inits; |
3555 | stabilize_call (alloc_call, &inits); | |
a6111661 | 3556 | if (inits) |
f293ce4b RS |
3557 | alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits, |
3558 | alloc_expr); | |
a6111661 JM |
3559 | } |
3560 | ||
047f64a3 JM |
3561 | /* unless an allocation function is declared with an empty excep- |
3562 | tion-specification (_except.spec_), throw(), it indicates failure to | |
3563 | allocate storage by throwing a bad_alloc exception (clause _except_, | |
3564 | _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo- | |
3565 | cation function is declared with an empty exception-specification, | |
3566 | throw(), it returns null to indicate failure to allocate storage and a | |
3567 | non-null pointer otherwise. | |
3568 | ||
3569 | So check for a null exception spec on the op new we just called. */ | |
3570 | ||
46ff5047 | 3571 | nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn)); |
8bee092e JJ |
3572 | check_new |
3573 | = flag_check_new || (nothrow && !std_placement_new_fn_p (alloc_fn)); | |
047f64a3 | 3574 | |
089d6ea7 | 3575 | if (cookie_size) |
8d08fdba | 3576 | { |
96790071 | 3577 | tree cookie; |
46e995e0 | 3578 | tree cookie_ptr; |
b5119fa1 | 3579 | tree size_ptr_type; |
f4f4610e MM |
3580 | |
3581 | /* Adjust so we're pointing to the start of the object. */ | |
5d49b6a7 | 3582 | data_addr = fold_build_pointer_plus (alloc_node, cookie_size); |
96790071 | 3583 | |
834c6dff | 3584 | /* Store the number of bytes allocated so that we can know how |
3461fba7 NS |
3585 | many elements to destroy later. We use the last sizeof |
3586 | (size_t) bytes to store the number of elements. */ | |
10ee5386 | 3587 | cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype)); |
5d49b6a7 RG |
3588 | cookie_ptr = fold_build_pointer_plus_loc (input_location, |
3589 | alloc_node, cookie_ptr); | |
b5119fa1 | 3590 | size_ptr_type = build_pointer_type (sizetype); |
10ee5386 | 3591 | cookie_ptr = fold_convert (size_ptr_type, cookie_ptr); |
04757a2a | 3592 | cookie = cp_build_fold_indirect_ref (cookie_ptr); |
1f84ec23 | 3593 | |
f293ce4b | 3594 | cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts); |
46e995e0 PB |
3595 | |
3596 | if (targetm.cxx.cookie_has_size ()) | |
3597 | { | |
3598 | /* Also store the element size. */ | |
5d49b6a7 | 3599 | cookie_ptr = fold_build_pointer_plus (cookie_ptr, |
db3927fb | 3600 | fold_build1_loc (input_location, |
5d49b6a7 RG |
3601 | NEGATE_EXPR, sizetype, |
3602 | size_in_bytes (sizetype))); | |
b2ec1738 | 3603 | |
04757a2a | 3604 | cookie = cp_build_fold_indirect_ref (cookie_ptr); |
f293ce4b | 3605 | cookie = build2 (MODIFY_EXPR, sizetype, cookie, |
10ee5386 | 3606 | size_in_bytes (elt_type)); |
f293ce4b RS |
3607 | cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr), |
3608 | cookie, cookie_expr); | |
46e995e0 | 3609 | } |
8d08fdba | 3610 | } |
96790071 | 3611 | else |
8b5e2ce4 JM |
3612 | { |
3613 | cookie_expr = NULL_TREE; | |
3614 | data_addr = alloc_node; | |
3615 | } | |
8d08fdba | 3616 | |
10ee5386 | 3617 | /* Now use a pointer to the type we've actually allocated. */ |
25357d1e JM |
3618 | |
3619 | /* But we want to operate on a non-const version to start with, | |
3620 | since we'll be modifying the elements. */ | |
3621 | non_const_pointer_type = build_pointer_type | |
a3360e77 | 3622 | (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST)); |
25357d1e JM |
3623 | |
3624 | data_addr = fold_convert (non_const_pointer_type, data_addr); | |
9207099b | 3625 | /* Any further uses of alloc_node will want this type, too. */ |
25357d1e | 3626 | alloc_node = fold_convert (non_const_pointer_type, alloc_node); |
10ee5386 | 3627 | |
6de9cd9a DN |
3628 | /* Now initialize the allocated object. Note that we preevaluate the |
3629 | initialization expression, apart from the actual constructor call or | |
3630 | assignment--we do this because we want to delay the allocation as long | |
3631 | as possible in order to minimize the size of the exception region for | |
3632 | placement delete. */ | |
f4f4610e | 3633 | if (is_initialized) |
8d08fdba | 3634 | { |
844ae01d | 3635 | bool explicit_value_init_p = false; |
6de9cd9a | 3636 | |
9771b263 | 3637 | if (*init != NULL && (*init)->is_empty ()) |
6de9cd9a | 3638 | { |
c166b898 | 3639 | *init = NULL; |
844ae01d JM |
3640 | explicit_value_init_p = true; |
3641 | } | |
b84f4651 | 3642 | |
38e10026 | 3643 | if (processing_template_decl) |
a67e7daa | 3644 | { |
38e10026 MP |
3645 | /* Avoid an ICE when converting to a base in build_simple_base_path. |
3646 | We'll throw this all away anyway, and build_new will create | |
3647 | a NEW_EXPR. */ | |
3648 | tree t = fold_convert (build_pointer_type (elt_type), data_addr); | |
a67e7daa JM |
3649 | /* build_value_init doesn't work in templates, and we don't need |
3650 | the initializer anyway since we're going to throw it away and | |
3651 | rebuild it at instantiation time, so just build up a single | |
3652 | constructor call to get any appropriate diagnostics. */ | |
38e10026 | 3653 | init_expr = cp_build_fold_indirect_ref (t); |
95552437 | 3654 | if (type_build_ctor_call (elt_type)) |
a67e7daa JM |
3655 | init_expr = build_special_member_call (init_expr, |
3656 | complete_ctor_identifier, | |
3657 | init, elt_type, | |
3658 | LOOKUP_NORMAL, | |
3659 | complain); | |
a67e7daa JM |
3660 | } |
3661 | else if (array_p) | |
844ae01d | 3662 | { |
25357d1e | 3663 | tree vecinit = NULL_TREE; |
73a2b8dd MP |
3664 | const size_t len = vec_safe_length (*init); |
3665 | if (len == 1 && DIRECT_LIST_INIT_P ((**init)[0])) | |
25357d1e | 3666 | { |
9771b263 | 3667 | vecinit = (**init)[0]; |
1f65a8c8 JM |
3668 | if (CONSTRUCTOR_NELTS (vecinit) == 0) |
3669 | /* List-value-initialization, leave it alone. */; | |
25357d1e JM |
3670 | else |
3671 | { | |
1f65a8c8 JM |
3672 | tree arraytype, domain; |
3673 | if (TREE_CONSTANT (nelts)) | |
3674 | domain = compute_array_index_type (NULL_TREE, nelts, | |
3675 | complain); | |
3676 | else | |
7d5e76c8 JM |
3677 | /* We'll check the length at runtime. */ |
3678 | domain = NULL_TREE; | |
1f65a8c8 | 3679 | arraytype = build_cplus_array_type (type, domain); |
b1c59b31 MP |
3680 | /* If we have new char[4]{"foo"}, we have to reshape |
3681 | so that the STRING_CST isn't wrapped in { }. */ | |
3682 | vecinit = reshape_init (arraytype, vecinit, complain); | |
3683 | /* The middle end doesn't cope with the location wrapper | |
3684 | around a STRING_CST. */ | |
3685 | STRIP_ANY_LOCATION_WRAPPER (vecinit); | |
1f65a8c8 | 3686 | vecinit = digest_init (arraytype, vecinit, complain); |
25357d1e | 3687 | } |
25357d1e JM |
3688 | } |
3689 | else if (*init) | |
5ade1ed2 DG |
3690 | { |
3691 | if (complain & tf_error) | |
3ec16e36 AO |
3692 | error ("parenthesized initializer in array new"); |
3693 | return error_mark_node; | |
5ade1ed2 | 3694 | } |
6de9cd9a | 3695 | init_expr |
b73a4704 JM |
3696 | = build_vec_init (data_addr, |
3697 | cp_build_binary_op (input_location, | |
3698 | MINUS_EXPR, outer_nelts, | |
3699 | integer_one_node, | |
3700 | complain), | |
3701 | vecinit, | |
3702 | explicit_value_init_p, | |
3703 | /*from_array=*/0, | |
3704 | complain); | |
6de9cd9a | 3705 | } |
f30efcb7 | 3706 | else |
8d08fdba | 3707 | { |
04757a2a | 3708 | init_expr = cp_build_fold_indirect_ref (data_addr); |
9207099b | 3709 | |
95552437 | 3710 | if (type_build_ctor_call (type) && !explicit_value_init_p) |
b84f4651 MM |
3711 | { |
3712 | init_expr = build_special_member_call (init_expr, | |
3713 | complete_ctor_identifier, | |
3714 | init, elt_type, | |
5ade1ed2 | 3715 | LOOKUP_NORMAL, |
0e5def81 | 3716 | complain|tf_no_cleanup); |
844ae01d JM |
3717 | } |
3718 | else if (explicit_value_init_p) | |
3719 | { | |
1d7bc790 NS |
3720 | /* Something like `new int()'. NO_CLEANUP is needed so |
3721 | we don't try and build a (possibly ill-formed) | |
3722 | destructor. */ | |
3723 | tree val = build_value_init (type, complain | tf_no_cleanup); | |
a67e7daa JM |
3724 | if (val == error_mark_node) |
3725 | return error_mark_node; | |
6ffbf87c | 3726 | init_expr = cp_build_init_expr (init_expr, val); |
b84f4651 | 3727 | } |
8dc2b103 | 3728 | else |
b84f4651 | 3729 | { |
c166b898 ILT |
3730 | tree ie; |
3731 | ||
b84f4651 | 3732 | /* We are processing something like `new int (10)', which |
c20f7e99 | 3733 | means allocate an int, and initialize it with 10. |
3db45ab5 | 3734 | |
c20f7e99 | 3735 | In C++20, also handle `new A(1, 2)'. */ |
b04445d4 | 3736 | if (cxx_dialect >= cxx20 |
c20f7e99 MP |
3737 | && AGGREGATE_TYPE_P (type) |
3738 | && (*init)->length () > 1) | |
3739 | { | |
73a2b8dd | 3740 | ie = build_constructor_from_vec (init_list_type_node, *init); |
c20f7e99 MP |
3741 | CONSTRUCTOR_IS_DIRECT_INIT (ie) = true; |
3742 | CONSTRUCTOR_IS_PAREN_INIT (ie) = true; | |
3743 | ie = digest_init (type, ie, complain); | |
3744 | } | |
3745 | else | |
3746 | ie = build_x_compound_expr_from_vec (*init, "new initializer", | |
3747 | complain); | |
4f2e1536 MP |
3748 | init_expr = cp_build_modify_expr (input_location, init_expr, |
3749 | INIT_EXPR, ie, complain); | |
b84f4651 | 3750 | } |
817a77e4 JM |
3751 | /* If the initializer uses C++14 aggregate NSDMI that refer to the |
3752 | object being initialized, replace them now and don't try to | |
3753 | preevaluate. */ | |
3754 | bool had_placeholder = false; | |
2166aeb3 | 3755 | if (!processing_template_decl |
817a77e4 JM |
3756 | && TREE_CODE (init_expr) == INIT_EXPR) |
3757 | TREE_OPERAND (init_expr, 1) | |
3758 | = replace_placeholders (TREE_OPERAND (init_expr, 1), | |
3759 | TREE_OPERAND (init_expr, 0), | |
3760 | &had_placeholder); | |
96790071 JM |
3761 | } |
3762 | ||
3763 | if (init_expr == error_mark_node) | |
3764 | return error_mark_node; | |
6cd51207 JM |
3765 | } |
3766 | else | |
3767 | init_expr = NULL_TREE; | |
1f109f0f | 3768 | |
6cd51207 JM |
3769 | /* If any part of the object initialization terminates by throwing an |
3770 | exception and a suitable deallocation function can be found, the | |
3771 | deallocation function is called to free the memory in which the | |
3772 | object was being constructed, after which the exception continues | |
3773 | to propagate in the context of the new-expression. If no | |
3774 | unambiguous matching deallocation function can be found, | |
3775 | propagating the exception does not cause the object's memory to be | |
3776 | freed. */ | |
3777 | if (flag_exceptions && (init_expr || member_delete_p)) | |
3778 | { | |
3779 | enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR; | |
3780 | tree cleanup; | |
3781 | ||
3782 | /* The Standard is unclear here, but the right thing to do | |
3783 | is to use the same method for finding deallocation | |
3784 | functions that we use for finding allocation functions. */ | |
3785 | cleanup = (build_op_delete_call | |
3786 | (dcode, | |
3787 | alloc_node, | |
3788 | size, | |
3789 | globally_qualified_p, | |
3790 | placement_allocation_fn_p ? alloc_call : NULL_TREE, | |
3791 | alloc_fn, | |
3792 | complain)); | |
3793 | ||
3794 | if (cleanup && init_expr && !processing_template_decl) | |
3795 | /* Ack! First we allocate the memory. Then we set our sentry | |
3796 | variable to true, and expand a cleanup that deletes the | |
3797 | memory if sentry is true. Then we run the constructor, and | |
3798 | finally clear the sentry. | |
3799 | ||
3800 | We need to do this because we allocate the space first, so | |
3801 | if there are any temporaries with cleanups in the | |
3802 | constructor args, we need this EH region to extend until | |
3803 | end of full-expression to preserve nesting. | |
3804 | ||
3805 | We used to try to evaluate the args first to avoid this, but | |
3806 | since C++17 [expr.new] says that "The invocation of the | |
3807 | allocation function is sequenced before the evaluations of | |
3808 | expressions in the new-initializer." */ | |
1f109f0f | 3809 | { |
6cd51207 | 3810 | tree end, sentry, begin; |
2face519 | 3811 | |
6cd51207 JM |
3812 | begin = get_target_expr (boolean_true_node); |
3813 | CLEANUP_EH_ONLY (begin) = 1; | |
2face519 | 3814 | |
6cd51207 | 3815 | sentry = TARGET_EXPR_SLOT (begin); |
659e5a7a | 3816 | |
6cd51207 JM |
3817 | /* CLEANUP is compiler-generated, so no diagnostics. */ |
3818 | suppress_warning (cleanup); | |
d665b6e5 | 3819 | |
6cd51207 JM |
3820 | TARGET_EXPR_CLEANUP (begin) |
3821 | = build3 (COND_EXPR, void_type_node, sentry, | |
3822 | cleanup, void_node); | |
2face519 | 3823 | |
6cd51207 JM |
3824 | end = build2 (MODIFY_EXPR, TREE_TYPE (sentry), |
3825 | sentry, boolean_false_node); | |
2face519 | 3826 | |
6cd51207 JM |
3827 | init_expr |
3828 | = build2 (COMPOUND_EXPR, void_type_node, begin, | |
3829 | build2 (COMPOUND_EXPR, void_type_node, init_expr, | |
3830 | end)); | |
3831 | /* Likewise, this is compiler-generated. */ | |
3832 | suppress_warning (init_expr); | |
1f109f0f | 3833 | } |
f4f4610e | 3834 | } |
8b5e2ce4 JM |
3835 | |
3836 | /* Now build up the return value in reverse order. */ | |
96790071 | 3837 | |
8b5e2ce4 | 3838 | rval = data_addr; |
2face519 | 3839 | |
8b5e2ce4 | 3840 | if (init_expr) |
f293ce4b | 3841 | rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval); |
8b5e2ce4 | 3842 | if (cookie_expr) |
f293ce4b | 3843 | rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval); |
8b5e2ce4 | 3844 | |
e3585e6a MP |
3845 | suppress_warning (rval, OPT_Wunused_value); |
3846 | ||
847a8301 | 3847 | if (rval == data_addr && TREE_CODE (alloc_expr) == TARGET_EXPR) |
8b5e2ce4 JM |
3848 | /* If we don't have an initializer or a cookie, strip the TARGET_EXPR |
3849 | and return the call (which doesn't need to be adjusted). */ | |
3850 | rval = TARGET_EXPR_INITIAL (alloc_expr); | |
3851 | else | |
d18c083e | 3852 | { |
8b5e2ce4 JM |
3853 | if (check_new) |
3854 | { | |
ba47d38d AH |
3855 | tree ifexp = cp_build_binary_op (input_location, |
3856 | NE_EXPR, alloc_node, | |
6d96d7ff | 3857 | nullptr_node, |
5ade1ed2 | 3858 | complain); |
4cbc4bd7 PC |
3859 | rval = build_conditional_expr (input_location, ifexp, rval, |
3860 | alloc_node, complain); | |
8b5e2ce4 | 3861 | } |
d18c083e | 3862 | |
8b5e2ce4 JM |
3863 | /* Perform the allocation before anything else, so that ALLOC_NODE |
3864 | has been initialized before we start using it. */ | |
f293ce4b | 3865 | rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval); |
8b5e2ce4 | 3866 | } |
51c184be | 3867 | |
d04a575f | 3868 | /* A new-expression is never an lvalue. */ |
bb19d4af | 3869 | gcc_assert (!obvalue_p (rval)); |
058dcc25 | 3870 | |
25357d1e | 3871 | return convert (pointer_type, rval); |
8d08fdba | 3872 | } |
63c9a190 | 3873 | |
c166b898 ILT |
3874 | /* Generate a representation for a C++ "new" expression. *PLACEMENT |
3875 | is a vector of placement-new arguments (or NULL if none). If NELTS | |
3876 | is NULL, TYPE is the type of the storage to be allocated. If NELTS | |
3877 | is not NULL, then this is an array-new allocation; TYPE is the type | |
3878 | of the elements in the array and NELTS is the number of elements in | |
3879 | the array. *INIT, if non-NULL, is the initializer for the new | |
3880 | object, or an empty vector to indicate an initializer of "()". If | |
3881 | USE_GLOBAL_NEW is true, then the user explicitly wrote "::new" | |
3882 | rather than just "new". This may change PLACEMENT and INIT. */ | |
63c9a190 MM |
3883 | |
3884 | tree | |
87d3f828 PC |
3885 | build_new (location_t loc, vec<tree, va_gc> **placement, tree type, |
3886 | tree nelts, vec<tree, va_gc> **init, int use_global_new, | |
3887 | tsubst_flags_t complain) | |
63c9a190 MM |
3888 | { |
3889 | tree rval; | |
9771b263 | 3890 | vec<tree, va_gc> *orig_placement = NULL; |
c166b898 | 3891 | tree orig_nelts = NULL_TREE; |
9771b263 | 3892 | vec<tree, va_gc> *orig_init = NULL; |
63c9a190 | 3893 | |
c166b898 | 3894 | if (type == error_mark_node) |
63c9a190 MM |
3895 | return error_mark_node; |
3896 | ||
9a642cca | 3897 | if (nelts == NULL_TREE |
c19267cb JM |
3898 | /* Don't do auto deduction where it might affect mangling. */ |
3899 | && (!processing_template_decl || at_function_scope_p ())) | |
86a09a9e JM |
3900 | { |
3901 | tree auto_node = type_uses_auto (type); | |
2e5748d2 JM |
3902 | if (auto_node) |
3903 | { | |
9a642cca | 3904 | tree d_init = NULL_TREE; |
009bb506 MP |
3905 | const size_t len = vec_safe_length (*init); |
3906 | /* E.g. new auto(x) must have exactly one element, or | |
3907 | a {} initializer will have one element. */ | |
3908 | if (len == 1) | |
9a642cca JM |
3909 | { |
3910 | d_init = (**init)[0]; | |
3911 | d_init = resolve_nondeduced_context (d_init, complain); | |
3912 | } | |
009bb506 MP |
3913 | /* For the rest, e.g. new A(1, 2, 3), create a list. */ |
3914 | else if (len > 1) | |
3915 | { | |
3916 | unsigned int n; | |
3917 | tree t; | |
3918 | tree *pp = &d_init; | |
3919 | FOR_EACH_VEC_ELT (**init, n, t) | |
3920 | { | |
3921 | t = resolve_nondeduced_context (t, complain); | |
3922 | *pp = build_tree_list (NULL_TREE, t); | |
3923 | pp = &TREE_CHAIN (*pp); | |
3924 | } | |
3925 | } | |
87ca4015 | 3926 | type = do_auto_deduction (type, d_init, auto_node, complain); |
2e5748d2 | 3927 | } |
86a09a9e JM |
3928 | } |
3929 | ||
63c9a190 MM |
3930 | if (processing_template_decl) |
3931 | { | |
3932 | if (dependent_type_p (type) | |
c166b898 | 3933 | || any_type_dependent_arguments_p (*placement) |
63c9a190 | 3934 | || (nelts && type_dependent_expression_p (nelts)) |
879b0a1d | 3935 | || (nelts && *init) |
c166b898 | 3936 | || any_type_dependent_arguments_p (*init)) |
87d3f828 | 3937 | return build_raw_new_expr (loc, *placement, type, nelts, *init, |
63c9a190 | 3938 | use_global_new); |
c166b898 ILT |
3939 | |
3940 | orig_placement = make_tree_vector_copy (*placement); | |
3941 | orig_nelts = nelts; | |
a4bbf910 | 3942 | if (*init) |
817a77e4 JM |
3943 | { |
3944 | orig_init = make_tree_vector_copy (*init); | |
3945 | /* Also copy any CONSTRUCTORs in *init, since reshape_init and | |
3946 | digest_init clobber them in place. */ | |
3947 | for (unsigned i = 0; i < orig_init->length(); ++i) | |
3948 | { | |
3949 | tree e = (**init)[i]; | |
3950 | if (TREE_CODE (e) == CONSTRUCTOR) | |
3951 | (**init)[i] = copy_node (e); | |
3952 | } | |
3953 | } | |
63c9a190 MM |
3954 | } |
3955 | ||
3956 | if (nelts) | |
3957 | { | |
87d3f828 | 3958 | location_t nelts_loc = cp_expr_loc_or_loc (nelts, loc); |
63c9a190 | 3959 | if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false)) |
5ade1ed2 DG |
3960 | { |
3961 | if (complain & tf_error) | |
87d3f828 | 3962 | permerror (nelts_loc, |
f5fc32e4 | 3963 | "size in array new must have integral type"); |
5ade1ed2 DG |
3964 | else |
3965 | return error_mark_node; | |
3966 | } | |
685c8340 MS |
3967 | |
3968 | /* Try to determine the constant value only for the purposes | |
3969 | of the diagnostic below but continue to use the original | |
3970 | value and handle const folding later. */ | |
e56f6629 | 3971 | const_tree cst_nelts = fold_non_dependent_expr (nelts, complain); |
685c8340 MS |
3972 | |
3973 | /* The expression in a noptr-new-declarator is erroneous if it's of | |
3974 | non-class type and its value before converting to std::size_t is | |
3975 | less than zero. ... If the expression is a constant expression, | |
3976 | the program is ill-fomed. */ | |
ea219e6e | 3977 | if (TREE_CODE (cst_nelts) == INTEGER_CST |
87d3f828 | 3978 | && !valid_array_size_p (nelts_loc, cst_nelts, NULL_TREE, |
28a8cef1 MS |
3979 | complain & tf_error)) |
3980 | return error_mark_node; | |
685c8340 | 3981 | |
03a904b5 | 3982 | nelts = mark_rvalue_use (nelts); |
4b978f96 | 3983 | nelts = cp_save_expr (cp_convert (sizetype, nelts, complain)); |
63c9a190 MM |
3984 | } |
3985 | ||
3986 | /* ``A reference cannot be created by the new operator. A reference | |
3987 | is not an object (8.2.2, 8.4.3), so a pointer to it could not be | |
3988 | returned by new.'' ARM 5.3.3 */ | |
9f613f06 | 3989 | if (TYPE_REF_P (type)) |
63c9a190 | 3990 | { |
5ade1ed2 | 3991 | if (complain & tf_error) |
87d3f828 | 3992 | error_at (loc, "new cannot be applied to a reference type"); |
5ade1ed2 DG |
3993 | else |
3994 | return error_mark_node; | |
63c9a190 MM |
3995 | type = TREE_TYPE (type); |
3996 | } | |
3997 | ||
3998 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
3999 | { | |
5ade1ed2 | 4000 | if (complain & tf_error) |
87d3f828 | 4001 | error_at (loc, "new cannot be applied to a function type"); |
63c9a190 MM |
4002 | return error_mark_node; |
4003 | } | |
4004 | ||
73a2b8dd | 4005 | /* P1009: Array size deduction in new-expressions. */ |
81de459e | 4006 | const bool array_p = TREE_CODE (type) == ARRAY_TYPE; |
ae48b74c MP |
4007 | if (*init |
4008 | /* If ARRAY_P, we have to deduce the array bound. For C++20 paren-init, | |
4009 | we have to process the parenthesized-list. But don't do it for (), | |
4010 | which is value-initialization, and INIT should stay empty. */ | |
4011 | && (array_p || (cxx_dialect >= cxx20 && nelts && !(*init)->is_empty ()))) | |
73a2b8dd MP |
4012 | { |
4013 | /* This means we have 'new T[]()'. */ | |
4014 | if ((*init)->is_empty ()) | |
4015 | { | |
4016 | tree ctor = build_constructor (init_list_type_node, NULL); | |
4017 | CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; | |
4018 | vec_safe_push (*init, ctor); | |
4019 | } | |
4020 | tree &elt = (**init)[0]; | |
4021 | /* The C++20 'new T[](e_0, ..., e_k)' case allowed by P0960. */ | |
4022 | if (!DIRECT_LIST_INIT_P (elt) && cxx_dialect >= cxx20) | |
4023 | { | |
81de459e MP |
4024 | tree ctor = build_constructor_from_vec (init_list_type_node, *init); |
4025 | CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; | |
4026 | CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; | |
4027 | elt = ctor; | |
4028 | /* We've squashed all the vector elements into the first one; | |
4029 | truncate the rest. */ | |
4030 | (*init)->truncate (1); | |
73a2b8dd MP |
4031 | } |
4032 | /* Otherwise we should have 'new T[]{e_0, ..., e_k}'. */ | |
81de459e MP |
4033 | if (array_p && !TYPE_DOMAIN (type)) |
4034 | { | |
4035 | /* We need to reshape before deducing the bounds to handle code like | |
4036 | ||
4037 | struct S { int x, y; }; | |
4038 | new S[]{1, 2, 3, 4}; | |
4039 | ||
4040 | which should deduce S[2]. But don't change ELT itself: we want to | |
4041 | pass a list-initializer to build_new_1, even for STRING_CSTs. */ | |
4042 | tree e = elt; | |
4043 | if (BRACE_ENCLOSED_INITIALIZER_P (e)) | |
4044 | e = reshape_init (type, e, complain); | |
4045 | cp_complete_array_type (&type, e, /*do_default*/false); | |
4046 | } | |
73a2b8dd MP |
4047 | } |
4048 | ||
57ccb546 MM |
4049 | /* The type allocated must be complete. If the new-type-id was |
4050 | "T[N]" then we are just checking that "T" is complete here, but | |
4051 | that is equivalent, since the value of "N" doesn't matter. */ | |
309714d4 | 4052 | if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)) |
39fb9d72 DB |
4053 | return error_mark_node; |
4054 | ||
5ade1ed2 | 4055 | rval = build_new_1 (placement, type, nelts, init, use_global_new, complain); |
63c9a190 MM |
4056 | if (rval == error_mark_node) |
4057 | return error_mark_node; | |
4058 | ||
4059 | if (processing_template_decl) | |
c166b898 | 4060 | { |
87d3f828 | 4061 | tree ret = build_raw_new_expr (loc, orig_placement, type, orig_nelts, |
c166b898 ILT |
4062 | orig_init, use_global_new); |
4063 | release_tree_vector (orig_placement); | |
4064 | release_tree_vector (orig_init); | |
4065 | return ret; | |
4066 | } | |
63c9a190 MM |
4067 | |
4068 | /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */ | |
87d3f828 | 4069 | rval = build1_loc (loc, NOP_EXPR, TREE_TYPE (rval), rval); |
65870e75 | 4070 | suppress_warning (rval, OPT_Wunused_value); |
63c9a190 MM |
4071 | |
4072 | return rval; | |
4073 | } | |
8d08fdba | 4074 | \f |
f30432d7 | 4075 | static tree |
04e4997a | 4076 | build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type, |
574cfaa4 | 4077 | special_function_kind auto_delete_vec, |
2fbc4548 JM |
4078 | int use_global_delete, tsubst_flags_t complain, |
4079 | bool in_cleanup = false) | |
f30432d7 MS |
4080 | { |
4081 | tree virtual_size; | |
e92cc029 | 4082 | tree ptype = build_pointer_type (type = complete_type (type)); |
c9b0866a | 4083 | tree size_exp; |
f30432d7 MS |
4084 | |
4085 | /* Temporary variables used by the loop. */ | |
4086 | tree tbase, tbase_init; | |
4087 | ||
4088 | /* This is the body of the loop that implements the deletion of a | |
4089 | single element, and moves temp variables to next elements. */ | |
4090 | tree body; | |
4091 | ||
4092 | /* This is the LOOP_EXPR that governs the deletion of the elements. */ | |
c7b62f14 | 4093 | tree loop = 0; |
f30432d7 MS |
4094 | |
4095 | /* This is the thing that governs what to do after the loop has run. */ | |
4096 | tree deallocate_expr = 0; | |
4097 | ||
4098 | /* This is the BIND_EXPR which holds the outermost iterator of the | |
4099 | loop. It is convenient to set this variable up and test it before | |
4100 | executing any other code in the loop. | |
4101 | This is also the containing expression returned by this function. */ | |
4102 | tree controller = NULL_TREE; | |
5be014d5 | 4103 | tree tmp; |
f30432d7 | 4104 | |
b2153b98 | 4105 | /* We should only have 1-D arrays here. */ |
8dc2b103 | 4106 | gcc_assert (TREE_CODE (type) != ARRAY_TYPE); |
b2153b98 | 4107 | |
574cfaa4 JM |
4108 | if (base == error_mark_node || maxindex == error_mark_node) |
4109 | return error_mark_node; | |
4110 | ||
04e4997a | 4111 | if (!verify_type_context (loc, TCTX_DEALLOCATION, type, |
02a32ab4 RS |
4112 | !(complain & tf_error))) |
4113 | return error_mark_node; | |
4114 | ||
c9b0866a PC |
4115 | if (!COMPLETE_TYPE_P (type)) |
4116 | { | |
097f82ec DM |
4117 | if (complain & tf_warning) |
4118 | { | |
4119 | auto_diagnostic_group d; | |
04e4997a PC |
4120 | if (warning_at (loc, OPT_Wdelete_incomplete, |
4121 | "possible problem detected in invocation of " | |
4122 | "operator %<delete []%>")) | |
097f82ec DM |
4123 | { |
4124 | cxx_incomplete_type_diagnostic (base, type, DK_WARNING); | |
04e4997a PC |
4125 | inform (loc, "neither the destructor nor the " |
4126 | "class-specific operator %<delete []%> will be called, " | |
4127 | "even if they are declared when the class is defined"); | |
097f82ec DM |
4128 | } |
4129 | } | |
20b06add JM |
4130 | /* This size won't actually be used. */ |
4131 | size_exp = size_one_node; | |
4132 | goto no_destructor; | |
c9b0866a PC |
4133 | } |
4134 | ||
4135 | size_exp = size_in_bytes (type); | |
4136 | ||
eca7fc57 | 4137 | if (! MAYBE_CLASS_TYPE_P (type)) |
c7b62f14 | 4138 | goto no_destructor; |
eca7fc57 JM |
4139 | else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) |
4140 | { | |
4141 | /* Make sure the destructor is callable. */ | |
4142 | if (type_build_dtor_call (type)) | |
4143 | { | |
04e4997a | 4144 | tmp = build_delete (loc, ptype, base, sfk_complete_destructor, |
a47e615f NY |
4145 | LOOKUP_NORMAL|LOOKUP_DESTRUCTOR|LOOKUP_NONVIRTUAL, |
4146 | 1, complain); | |
eca7fc57 JM |
4147 | if (tmp == error_mark_node) |
4148 | return error_mark_node; | |
4149 | } | |
4150 | goto no_destructor; | |
4151 | } | |
f30432d7 | 4152 | |
708cae97 | 4153 | /* The below is short by the cookie size. */ |
fed3cef0 | 4154 | virtual_size = size_binop (MULT_EXPR, size_exp, |
cda0a029 | 4155 | fold_convert (sizetype, maxindex)); |
f30432d7 | 4156 | |
46e8c075 | 4157 | tbase = create_temporary_var (ptype); |
8e007055 | 4158 | DECL_INITIAL (tbase) |
04e4997a | 4159 | = fold_build_pointer_plus_loc (loc, fold_convert (ptype, base), |
8e007055 | 4160 | virtual_size); |
04e4997a | 4161 | tbase_init = build_stmt (loc, DECL_EXPR, tbase); |
8e007055 | 4162 | controller = build3 (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE); |
f30432d7 | 4163 | TREE_SIDE_EFFECTS (controller) = 1; |
4c6afbbd | 4164 | BIND_EXPR_VEC_DTOR (controller) = true; |
f30432d7 | 4165 | |
f293ce4b | 4166 | body = build1 (EXIT_EXPR, void_type_node, |
5cd88d68 RS |
4167 | build2 (EQ_EXPR, boolean_type_node, tbase, |
4168 | fold_convert (ptype, base))); | |
04e4997a | 4169 | tmp = fold_build1_loc (loc, NEGATE_EXPR, sizetype, size_exp); |
5d49b6a7 | 4170 | tmp = fold_build_pointer_plus (tbase, tmp); |
04e4997a | 4171 | tmp = cp_build_modify_expr (loc, tbase, NOP_EXPR, tmp, complain); |
574cfaa4 JM |
4172 | if (tmp == error_mark_node) |
4173 | return error_mark_node; | |
04e4997a | 4174 | body = build_compound_expr (loc, body, tmp); |
a47e615f NY |
4175 | /* [expr.delete]/3: "In an array delete expression, if the dynamic type of |
4176 | the object to be deleted is not similar to its static type, the behavior | |
4177 | is undefined." So we can set LOOKUP_NONVIRTUAL. */ | |
04e4997a | 4178 | tmp = build_delete (loc, ptype, tbase, sfk_complete_destructor, |
a47e615f NY |
4179 | LOOKUP_NORMAL|LOOKUP_DESTRUCTOR|LOOKUP_NONVIRTUAL, |
4180 | 1, complain); | |
574cfaa4 JM |
4181 | if (tmp == error_mark_node) |
4182 | return error_mark_node; | |
04e4997a | 4183 | body = build_compound_expr (loc, body, tmp); |
f30432d7 | 4184 | |
f293ce4b | 4185 | loop = build1 (LOOP_EXPR, void_type_node, body); |
2fbc4548 JM |
4186 | |
4187 | /* If one destructor throws, keep trying to clean up the rest, unless we're | |
4188 | already in a build_vec_init cleanup. */ | |
d33c3b5a PP |
4189 | if (flag_exceptions && !in_cleanup && !processing_template_decl |
4190 | && !expr_noexcept_p (tmp, tf_none)) | |
2fbc4548 JM |
4191 | { |
4192 | loop = build2 (TRY_CATCH_EXPR, void_type_node, loop, | |
4193 | unshare_expr (loop)); | |
4194 | /* Tell honor_protect_cleanup_actions to discard this on the | |
4195 | exceptional path. */ | |
4196 | TRY_CATCH_IS_CLEANUP (loop) = true; | |
4197 | } | |
4198 | ||
04e4997a | 4199 | loop = build_compound_expr (loc, tbase_init, loop); |
f30432d7 MS |
4200 | |
4201 | no_destructor: | |
e79a6b40 JM |
4202 | /* Delete the storage if appropriate. */ |
4203 | if (auto_delete_vec == sfk_deleting_destructor) | |
f30432d7 MS |
4204 | { |
4205 | tree base_tbd; | |
4206 | ||
708cae97 | 4207 | /* The below is short by the cookie size. */ |
fed3cef0 | 4208 | virtual_size = size_binop (MULT_EXPR, size_exp, |
cda0a029 | 4209 | fold_convert (sizetype, maxindex)); |
f30432d7 MS |
4210 | |
4211 | if (! TYPE_VEC_NEW_USES_COOKIE (type)) | |
4212 | /* no header */ | |
4213 | base_tbd = base; | |
4214 | else | |
4215 | { | |
834c6dff MM |
4216 | tree cookie_size; |
4217 | ||
46e995e0 | 4218 | cookie_size = targetm.cxx.get_cookie_size (type); |
04e4997a | 4219 | base_tbd = cp_build_binary_op (loc, |
574cfaa4 JM |
4220 | MINUS_EXPR, |
4221 | cp_convert (string_type_node, | |
4b978f96 | 4222 | base, complain), |
574cfaa4 JM |
4223 | cookie_size, |
4224 | complain); | |
4225 | if (base_tbd == error_mark_node) | |
4226 | return error_mark_node; | |
4b978f96 | 4227 | base_tbd = cp_convert (ptype, base_tbd, complain); |
e92cc029 | 4228 | /* True size with header. */ |
834c6dff | 4229 | virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size); |
f30432d7 | 4230 | } |
86f45d2c | 4231 | |
e79a6b40 JM |
4232 | deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR, |
4233 | base_tbd, virtual_size, | |
4234 | use_global_delete & 1, | |
4235 | /*placement=*/NULL_TREE, | |
4b978f96 PC |
4236 | /*alloc_fn=*/NULL_TREE, |
4237 | complain); | |
f30432d7 MS |
4238 | } |
4239 | ||
c7b62f14 | 4240 | body = loop; |
4924293a PP |
4241 | if (deallocate_expr == error_mark_node) |
4242 | return error_mark_node; | |
4243 | else if (!deallocate_expr) | |
c7b62f14 NS |
4244 | ; |
4245 | else if (!body) | |
4246 | body = deallocate_expr; | |
f30432d7 | 4247 | else |
4e8e460b | 4248 | /* The delete operator must be called, even if a destructor |
ee392fc2 NS |
4249 | throws. */ |
4250 | body = build2 (TRY_FINALLY_EXPR, void_type_node, body, deallocate_expr); | |
c8094d83 | 4251 | |
c7b62f14 NS |
4252 | if (!body) |
4253 | body = integer_zero_node; | |
c8094d83 | 4254 | |
f30432d7 | 4255 | /* Outermost wrapper: If pointer is null, punt. */ |
04e4997a | 4256 | tree cond = build2_loc (loc, NE_EXPR, boolean_type_node, base, |
03ca8fb3 | 4257 | fold_convert (TREE_TYPE (base), nullptr_node)); |
03a616ac JJ |
4258 | /* This is a compiler generated comparison, don't emit |
4259 | e.g. -Wnonnull-compare warning for it. */ | |
65870e75 | 4260 | suppress_warning (cond, OPT_Wnonnull_compare); |
04e4997a | 4261 | body = build3_loc (loc, COND_EXPR, void_type_node, |
03ca8fb3 | 4262 | cond, body, integer_zero_node); |
d5bcd6d4 | 4263 | COND_EXPR_IS_VEC_DELETE (body) = true; |
f30432d7 MS |
4264 | body = build1 (NOP_EXPR, void_type_node, body); |
4265 | ||
4266 | if (controller) | |
4267 | { | |
4268 | TREE_OPERAND (controller, 1) = body; | |
4e8dca1c | 4269 | body = controller; |
f30432d7 | 4270 | } |
4e8dca1c JM |
4271 | |
4272 | if (TREE_CODE (base) == SAVE_EXPR) | |
4273 | /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */ | |
f293ce4b | 4274 | body = build2 (COMPOUND_EXPR, void_type_node, base, body); |
4e8dca1c | 4275 | |
574cfaa4 | 4276 | return convert_to_void (body, ICV_CAST, complain); |
f30432d7 MS |
4277 | } |
4278 | ||
c8094d83 | 4279 | /* Create an unnamed variable of the indicated TYPE. */ |
c395453c | 4280 | |
f1dedc31 | 4281 | tree |
362efdc1 | 4282 | create_temporary_var (tree type) |
8a72a046 | 4283 | { |
f1dedc31 | 4284 | tree decl; |
c8094d83 | 4285 | |
c2255bc4 AH |
4286 | decl = build_decl (input_location, |
4287 | VAR_DECL, NULL_TREE, type); | |
f1dedc31 MM |
4288 | TREE_USED (decl) = 1; |
4289 | DECL_ARTIFICIAL (decl) = 1; | |
f1dedc31 | 4290 | DECL_IGNORED_P (decl) = 1; |
e60032b3 | 4291 | DECL_CONTEXT (decl) = current_function_decl; |
f1dedc31 | 4292 | |
f1dedc31 | 4293 | return decl; |
8a72a046 MM |
4294 | } |
4295 | ||
f1dedc31 MM |
4296 | /* Create a new temporary variable of the indicated TYPE, initialized |
4297 | to INIT. | |
8a72a046 | 4298 | |
f1dedc31 MM |
4299 | It is not entered into current_binding_level, because that breaks |
4300 | things when it comes time to do final cleanups (which take place | |
4301 | "outside" the binding contour of the function). */ | |
4302 | ||
fe5b5c36 | 4303 | tree |
362efdc1 | 4304 | get_temp_regvar (tree type, tree init) |
f30432d7 | 4305 | { |
f1dedc31 | 4306 | tree decl; |
8a72a046 | 4307 | |
f1dedc31 | 4308 | decl = create_temporary_var (type); |
350fae66 | 4309 | add_decl_expr (decl); |
c8094d83 | 4310 | |
4f2e1536 MP |
4311 | finish_expr_stmt (cp_build_modify_expr (input_location, decl, INIT_EXPR, |
4312 | init, tf_warning_or_error)); | |
8a72a046 | 4313 | |
f1dedc31 | 4314 | return decl; |
f30432d7 MS |
4315 | } |
4316 | ||
024f2d89 JM |
4317 | /* Subroutine of build_vec_init. Returns true if assigning to an array of |
4318 | INNER_ELT_TYPE from INIT is trivial. */ | |
4319 | ||
4320 | static bool | |
4321 | vec_copy_assign_is_trivial (tree inner_elt_type, tree init) | |
4322 | { | |
4323 | tree fromtype = inner_elt_type; | |
72b3e203 | 4324 | if (lvalue_p (init)) |
024f2d89 JM |
4325 | fromtype = cp_build_reference_type (fromtype, /*rval*/false); |
4326 | return is_trivially_xible (MODIFY_EXPR, inner_elt_type, fromtype); | |
4327 | } | |
4328 | ||
5a68fae7 JM |
4329 | /* Subroutine of build_vec_init: Check that the array has at least N |
4330 | elements. Other parameters are local variables in build_vec_init. */ | |
4331 | ||
4332 | void | |
4333 | finish_length_check (tree atype, tree iterator, tree obase, unsigned n) | |
4334 | { | |
4335 | tree nelts = build_int_cst (ptrdiff_type_node, n - 1); | |
4336 | if (TREE_CODE (atype) != ARRAY_TYPE) | |
4337 | { | |
4338 | if (flag_exceptions) | |
4339 | { | |
4340 | tree c = fold_build2 (LT_EXPR, boolean_type_node, iterator, | |
4341 | nelts); | |
4342 | c = build3 (COND_EXPR, void_type_node, c, | |
4343 | throw_bad_array_new_length (), void_node); | |
4344 | finish_expr_stmt (c); | |
4345 | } | |
4346 | /* Don't check an array new when -fno-exceptions. */ | |
4347 | } | |
f34ebeb2 ML |
4348 | else if (sanitize_flags_p (SANITIZE_BOUNDS) |
4349 | && current_function_decl != NULL_TREE) | |
5a68fae7 JM |
4350 | { |
4351 | /* Make sure the last element of the initializer is in bounds. */ | |
4352 | finish_expr_stmt | |
4353 | (ubsan_instrument_bounds | |
4354 | (input_location, obase, &nelts, /*ignore_off_by_one*/false)); | |
4355 | } | |
4356 | } | |
4357 | ||
1e184761 JM |
4358 | /* walk_tree callback to collect temporaries in an expression. */ |
4359 | ||
4360 | tree | |
4361 | find_temps_r (tree *tp, int *walk_subtrees, void *data) | |
4362 | { | |
4363 | vec<tree*> &temps = *static_cast<auto_vec<tree*> *>(data); | |
4364 | tree t = *tp; | |
4365 | if (TREE_CODE (t) == TARGET_EXPR | |
4366 | && !TARGET_EXPR_ELIDING_P (t)) | |
4367 | temps.safe_push (tp); | |
4368 | else if (TYPE_P (t)) | |
4369 | *walk_subtrees = 0; | |
4370 | ||
4371 | return NULL_TREE; | |
4372 | } | |
4373 | ||
e0324e26 JM |
4374 | /* walk_tree callback to collect temporaries in an expression that |
4375 | are allocator arguments to standard library classes. */ | |
4376 | ||
4377 | static tree | |
4378 | find_allocator_temps_r (tree *tp, int *walk_subtrees, void *data) | |
4379 | { | |
4380 | vec<tree*> &temps = *static_cast<auto_vec<tree*> *>(data); | |
4381 | tree t = *tp; | |
4382 | if (TYPE_P (t)) | |
4383 | { | |
4384 | *walk_subtrees = 0; | |
4385 | return NULL_TREE; | |
4386 | } | |
4387 | ||
4388 | /* If this is a call to a constructor for a std:: class, look for | |
4389 | a reference-to-allocator argument. */ | |
4390 | tree fn = cp_get_callee_fndecl_nofold (t); | |
4391 | if (fn && DECL_CONSTRUCTOR_P (fn) | |
4392 | && decl_in_std_namespace_p (TYPE_NAME (DECL_CONTEXT (fn)))) | |
4393 | { | |
4394 | int nargs = call_expr_nargs (t); | |
4395 | for (int i = 1; i < nargs; ++i) | |
4396 | { | |
4397 | tree arg = get_nth_callarg (t, i); | |
4398 | tree atype = TREE_TYPE (arg); | |
4399 | if (TREE_CODE (atype) == REFERENCE_TYPE | |
4400 | && is_std_allocator (TREE_TYPE (atype))) | |
4401 | { | |
4402 | STRIP_NOPS (arg); | |
4403 | if (TREE_CODE (arg) == ADDR_EXPR) | |
4404 | { | |
4405 | tree *ap = &TREE_OPERAND (arg, 0); | |
4406 | if (TREE_CODE (*ap) == TARGET_EXPR) | |
4407 | temps.safe_push (ap); | |
4408 | } | |
4409 | } | |
4410 | } | |
4411 | } | |
4412 | ||
4413 | return NULL_TREE; | |
4414 | } | |
4415 | ||
1e184761 | 4416 | /* If INIT initializes a standard library class, and involves a temporary |
e0324e26 JM |
4417 | std::allocator<T>, use ALLOC_OBJ for all such temporaries. |
4418 | ||
4419 | Note that this can clobber the input to build_vec_init; no unsharing is | |
4420 | done. To make this safe we use the TARGET_EXPR in all places rather than | |
4421 | pulling out the TARGET_EXPR_SLOT. | |
1e184761 JM |
4422 | |
4423 | Used by build_vec_init when initializing an array of e.g. strings to reuse | |
4424 | the same temporary allocator for all of the strings. We can do this because | |
4425 | std::allocator has no data and the standard library doesn't care about the | |
4426 | address of allocator objects. | |
4427 | ||
4428 | ??? Add an attribute to allow users to assert the same property for other | |
4429 | classes, i.e. one object of the type is interchangeable with any other? */ | |
4430 | ||
e0324e26 JM |
4431 | static void |
4432 | combine_allocator_temps (tree &init, tree &alloc_obj) | |
1e184761 | 4433 | { |
1e184761 | 4434 | auto_vec<tree*> temps; |
e0324e26 | 4435 | cp_walk_tree_without_duplicates (&init, find_allocator_temps_r, &temps); |
1e184761 | 4436 | for (tree *p : temps) |
e0324e26 JM |
4437 | { |
4438 | if (!alloc_obj) | |
4439 | alloc_obj = *p; | |
4440 | else | |
4441 | *p = alloc_obj; | |
4442 | } | |
1e184761 JM |
4443 | } |
4444 | ||
f1dedc31 MM |
4445 | /* `build_vec_init' returns tree structure that performs |
4446 | initialization of a vector of aggregate types. | |
8d08fdba | 4447 | |
9207099b JM |
4448 | BASE is a reference to the vector, of ARRAY_TYPE, or a pointer |
4449 | to the first element, of POINTER_TYPE. | |
a48cccea | 4450 | MAXINDEX is the maximum index of the array (one less than the |
9207099b | 4451 | number of elements). It is only used if BASE is a pointer or |
a48cccea | 4452 | TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE. |
b84f4651 | 4453 | |
8d08fdba MS |
4454 | INIT is the (possibly NULL) initializer. |
4455 | ||
844ae01d JM |
4456 | If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All |
4457 | elements in the array are value-initialized. | |
b84f4651 | 4458 | |
8d08fdba MS |
4459 | FROM_ARRAY is 0 if we should init everything with INIT |
4460 | (i.e., every element initialized from INIT). | |
4461 | FROM_ARRAY is 1 if we should index into INIT in parallel | |
4462 | with initialization of DECL. | |
4463 | FROM_ARRAY is 2 if we should index into INIT in parallel, | |
4464 | but use assignment instead of initialization. */ | |
4465 | ||
4466 | tree | |
3db45ab5 | 4467 | build_vec_init (tree base, tree maxindex, tree init, |
844ae01d | 4468 | bool explicit_value_init_p, |
beaee0a8 JM |
4469 | int from_array, |
4470 | tsubst_flags_t complain, | |
57ab3714 | 4471 | vec<tree, va_gc>** cleanup_flags /* = nullptr */) |
8d08fdba MS |
4472 | { |
4473 | tree rval; | |
8a72a046 | 4474 | tree base2 = NULL_TREE; |
e833cb11 | 4475 | tree itype = NULL_TREE; |
8a72a046 | 4476 | tree iterator; |
9207099b | 4477 | /* The type of BASE. */ |
f30efcb7 | 4478 | tree atype = TREE_TYPE (base); |
f1dedc31 | 4479 | /* The type of an element in the array. */ |
f30efcb7 | 4480 | tree type = TREE_TYPE (atype); |
c8094d83 | 4481 | /* The element type reached after removing all outer array |
b5af3133 MM |
4482 | types. */ |
4483 | tree inner_elt_type; | |
f1dedc31 MM |
4484 | /* The type of a pointer to an element in the array. */ |
4485 | tree ptype; | |
4486 | tree stmt_expr; | |
4487 | tree compound_stmt; | |
4488 | int destroy_temps; | |
faa9232d | 4489 | HOST_WIDE_INT num_initialized_elts = 0; |
2a3398e1 | 4490 | bool is_global; |
fa2200cb | 4491 | tree obase = base; |
f91352dc | 4492 | bool xvalue = false; |
574cfaa4 | 4493 | bool errors = false; |
f9d0ca40 | 4494 | location_t loc = (init ? cp_expr_loc_or_input_loc (init) |
5a68fae7 | 4495 | : location_of (base)); |
c8094d83 | 4496 | |
9207099b | 4497 | if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype)) |
a48cccea JM |
4498 | maxindex = array_type_nelts (atype); |
4499 | ||
ddffee68 | 4500 | if (maxindex == NULL_TREE || maxindex == error_mark_node) |
8d08fdba MS |
4501 | return error_mark_node; |
4502 | ||
cda0a029 | 4503 | maxindex = maybe_constant_value (maxindex); |
844ae01d | 4504 | if (explicit_value_init_p) |
b84f4651 MM |
4505 | gcc_assert (!init); |
4506 | ||
9207099b | 4507 | inner_elt_type = strip_array_types (type); |
567ef749 JM |
4508 | |
4509 | /* Look through the TARGET_EXPR around a compound literal. */ | |
4510 | if (init && TREE_CODE (init) == TARGET_EXPR | |
5a4d8044 | 4511 | && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR |
2764335b JM |
4512 | && from_array != 2 |
4513 | && (same_type_ignoring_top_level_qualifiers_p | |
4514 | (TREE_TYPE (init), atype))) | |
567ef749 JM |
4515 | init = TARGET_EXPR_INITIAL (init); |
4516 | ||
119cea98 JM |
4517 | if (tree vi = get_vec_init_expr (init)) |
4518 | init = VEC_INIT_EXPR_INIT (vi); | |
e948436e | 4519 | |
0655c6d5 JM |
4520 | bool direct_init = false; |
4521 | if (from_array && init && BRACE_ENCLOSED_INITIALIZER_P (init) | |
4522 | && CONSTRUCTOR_NELTS (init) == 1) | |
4523 | { | |
4524 | tree elt = CONSTRUCTOR_ELT (init, 0)->value; | |
e948436e JM |
4525 | if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE |
4526 | && TREE_CODE (elt) != VEC_INIT_EXPR) | |
0655c6d5 JM |
4527 | { |
4528 | direct_init = DIRECT_LIST_INIT_P (init); | |
4529 | init = elt; | |
4530 | } | |
4531 | } | |
4532 | ||
dd9c8f64 JM |
4533 | /* from_array doesn't apply to initialization from CONSTRUCTOR. */ |
4534 | if (init && TREE_CODE (init) == CONSTRUCTOR) | |
4535 | from_array = 0; | |
4536 | ||
5a68fae7 | 4537 | /* If we have a braced-init-list or string constant, make sure that the array |
7d5e76c8 | 4538 | is big enough for all the initializers. */ |
5a68fae7 JM |
4539 | bool length_check = (init |
4540 | && (TREE_CODE (init) == STRING_CST | |
4541 | || (TREE_CODE (init) == CONSTRUCTOR | |
4542 | && CONSTRUCTOR_NELTS (init) > 0)) | |
46621807 | 4543 | && !TREE_CONSTANT (maxindex)); |
7d5e76c8 | 4544 | |
c8a3d889 | 4545 | if (init |
25357d1e | 4546 | && TREE_CODE (atype) == ARRAY_TYPE |
06b76c7f | 4547 | && TREE_CONSTANT (maxindex) |
c65bd532 | 4548 | && !vla_type_p (type) |
c8a3d889 | 4549 | && (from_array == 2 |
024f2d89 | 4550 | ? vec_copy_assign_is_trivial (inner_elt_type, init) |
c8a3d889 | 4551 | : !TYPE_NEEDS_CONSTRUCTING (type)) |
f30efcb7 | 4552 | && ((TREE_CODE (init) == CONSTRUCTOR |
d1a73b0b JM |
4553 | && (BRACE_ENCLOSED_INITIALIZER_P (init) |
4554 | || (same_type_ignoring_top_level_qualifiers_p | |
4555 | (atype, TREE_TYPE (init)))) | |
f30efcb7 JM |
4556 | /* Don't do this if the CONSTRUCTOR might contain something |
4557 | that might throw and require us to clean up. */ | |
9771b263 | 4558 | && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)) |
b5af3133 | 4559 | || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type))) |
f30efcb7 JM |
4560 | || from_array)) |
4561 | { | |
c32097d8 | 4562 | /* Do non-default initialization of trivial arrays resulting from |
f30efcb7 JM |
4563 | brace-enclosed initializers. In this case, digest_init and |
4564 | store_constructor will handle the semantics for us. */ | |
4565 | ||
a47c2f62 JM |
4566 | if (BRACE_ENCLOSED_INITIALIZER_P (init)) |
4567 | init = digest_init (atype, init, complain); | |
6ffbf87c | 4568 | stmt_expr = cp_build_init_expr (base, init); |
f30efcb7 JM |
4569 | return stmt_expr; |
4570 | } | |
4571 | ||
4b978f96 | 4572 | maxindex = cp_convert (ptrdiff_type_node, maxindex, complain); |
cda0a029 JM |
4573 | maxindex = fold_simple (maxindex); |
4574 | ||
9207099b JM |
4575 | if (TREE_CODE (atype) == ARRAY_TYPE) |
4576 | { | |
4577 | ptype = build_pointer_type (type); | |
89fcabaf PC |
4578 | base = decay_conversion (base, complain); |
4579 | if (base == error_mark_node) | |
4580 | return error_mark_node; | |
4b978f96 | 4581 | base = cp_convert (ptype, base, complain); |
9207099b JM |
4582 | } |
4583 | else | |
4584 | ptype = atype; | |
8d08fdba | 4585 | |
08e1ff9d JM |
4586 | if (integer_all_onesp (maxindex)) |
4587 | { | |
4588 | /* Shortcut zero element case to avoid unneeded constructor synthesis. */ | |
4589 | if (init && TREE_SIDE_EFFECTS (init)) | |
ff4deb4b | 4590 | base = build2 (COMPOUND_EXPR, ptype, init, base); |
08e1ff9d JM |
4591 | return base; |
4592 | } | |
4593 | ||
f1dedc31 | 4594 | /* The code we are generating looks like: |
303b7406 | 4595 | ({ |
f1dedc31 | 4596 | T* t1 = (T*) base; |
f30efcb7 | 4597 | T* rval = t1; |
f1dedc31 MM |
4598 | ptrdiff_t iterator = maxindex; |
4599 | try { | |
4977bab6 | 4600 | for (; iterator != -1; --iterator) { |
f30efcb7 JM |
4601 | ... initialize *t1 ... |
4602 | ++t1; | |
4977bab6 | 4603 | } |
f1dedc31 | 4604 | } catch (...) { |
0cbd7506 | 4605 | ... destroy elements that were constructed ... |
f1dedc31 | 4606 | } |
303b7406 NS |
4607 | rval; |
4608 | }) | |
c8094d83 | 4609 | |
f1dedc31 MM |
4610 | We can omit the try and catch blocks if we know that the |
4611 | initialization will never throw an exception, or if the array | |
f30efcb7 | 4612 | elements do not have destructors. We can omit the loop completely if |
c8094d83 | 4613 | the elements of the array do not have constructors. |
f1dedc31 MM |
4614 | |
4615 | We actually wrap the entire body of the above in a STMT_EXPR, for | |
c8094d83 | 4616 | tidiness. |
f1dedc31 MM |
4617 | |
4618 | When copying from array to another, when the array elements have | |
4619 | only trivial copy constructors, we should use __builtin_memcpy | |
4620 | rather than generating a loop. That way, we could take advantage | |
3b426391 | 4621 | of whatever cleverness the back end has for dealing with copies |
f1dedc31 MM |
4622 | of blocks of memory. */ |
4623 | ||
2a3398e1 | 4624 | is_global = begin_init_stmts (&stmt_expr, &compound_stmt); |
f2c5f623 | 4625 | destroy_temps = stmts_are_full_exprs_p (); |
ae499cce | 4626 | current_stmt_tree ()->stmts_are_full_exprs_p = 0; |
f30efcb7 | 4627 | rval = get_temp_regvar (ptype, base); |
f1dedc31 | 4628 | base = get_temp_regvar (ptype, rval); |
beaee0a8 JM |
4629 | tree iterator_targ = get_target_expr (maxindex); |
4630 | add_stmt (iterator_targ); | |
4631 | iterator = TARGET_EXPR_SLOT (iterator_targ); | |
8d08fdba | 4632 | |
5a4d8044 JM |
4633 | /* If initializing one array from another, initialize element by |
4634 | element. We rely upon the below calls to do the argument | |
4635 | checking. Evaluate the initializer before entering the try block. */ | |
dd9c8f64 | 4636 | if (from_array) |
5a4d8044 | 4637 | { |
f91352dc JM |
4638 | if (lvalue_kind (init) & clk_rvalueref) |
4639 | xvalue = true; | |
2764335b JM |
4640 | if (TREE_CODE (init) == TARGET_EXPR) |
4641 | { | |
4642 | /* Avoid error in decay_conversion. */ | |
4643 | base2 = decay_conversion (TARGET_EXPR_SLOT (init), complain); | |
4644 | base2 = cp_build_compound_expr (init, base2, tf_none); | |
4645 | } | |
4646 | else | |
4647 | base2 = decay_conversion (init, complain); | |
89fcabaf PC |
4648 | if (base2 == error_mark_node) |
4649 | return error_mark_node; | |
5a4d8044 JM |
4650 | itype = TREE_TYPE (base2); |
4651 | base2 = get_temp_regvar (itype, base2); | |
4652 | itype = TREE_TYPE (itype); | |
4653 | } | |
4654 | ||
8a72a046 | 4655 | /* Protect the entire array initialization so that we can destroy |
f30efcb7 JM |
4656 | the partially constructed array if an exception is thrown. |
4657 | But don't do this if we're assigning. */ | |
4658 | if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
4659 | && from_array != 2) | |
ed5511d9 | 4660 | { |
beaee0a8 JM |
4661 | tree e; |
4662 | tree m = cp_build_binary_op (input_location, | |
4663 | MINUS_EXPR, maxindex, iterator, | |
4664 | complain); | |
4665 | ||
4666 | /* Flatten multi-dimensional array since build_vec_delete only | |
4667 | expects one-dimensional array. */ | |
4668 | if (TREE_CODE (type) == ARRAY_TYPE) | |
4669 | m = cp_build_binary_op (input_location, | |
4670 | MULT_EXPR, m, | |
4671 | /* Avoid mixing signed and unsigned. */ | |
4672 | convert (TREE_TYPE (m), | |
4673 | array_type_nelts_total (type)), | |
4674 | complain); | |
4675 | ||
4676 | e = build_vec_delete_1 (input_location, rval, m, | |
4677 | inner_elt_type, sfk_complete_destructor, | |
2fbc4548 JM |
4678 | /*use_global_delete=*/0, complain, |
4679 | /*in_cleanup*/true); | |
beaee0a8 JM |
4680 | if (e == error_mark_node) |
4681 | errors = true; | |
4682 | TARGET_EXPR_CLEANUP (iterator_targ) = e; | |
4683 | CLEANUP_EH_ONLY (iterator_targ) = true; | |
4684 | ||
4685 | /* Since we push this cleanup before doing any initialization, cleanups | |
4686 | for any temporaries in the initialization are naturally within our | |
4687 | cleanup region, so we don't want wrap_temporary_cleanups to do | |
4688 | anything for arrays. But if the array is a subobject, we need to | |
12882830 JM |
4689 | tell split_nonconstant_init or cp_genericize_target_expr how to turn |
4690 | off this cleanup in favor of the cleanup for the complete object. | |
4691 | ||
4692 | ??? For an array temporary such as an initializer_list backing array, | |
4693 | it would avoid redundancy to leave this cleanup active, clear | |
4694 | CLEANUP_EH_ONLY, and not build another cleanup for the temporary | |
4695 | itself. But that breaks when gimplify_target_expr adds a clobber | |
4696 | cleanup that runs before the build_vec_init cleanup. */ | |
57ab3714 JM |
4697 | if (cleanup_flags) |
4698 | vec_safe_push (*cleanup_flags, build_tree_list (iterator, maxindex)); | |
ed5511d9 | 4699 | } |
8a72a046 | 4700 | |
c0014b07 JM |
4701 | /* Should we try to create a constant initializer? */ |
4702 | bool try_const = (TREE_CODE (atype) == ARRAY_TYPE | |
4703 | && TREE_CONSTANT (maxindex) | |
a02f26f6 JM |
4704 | && (init ? TREE_CODE (init) == CONSTRUCTOR |
4705 | : (type_has_constexpr_default_constructor | |
4706 | (inner_elt_type))) | |
c0014b07 JM |
4707 | && (literal_type_p (inner_elt_type) |
4708 | || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type))); | |
4709 | vec<constructor_elt, va_gc> *const_vec = NULL; | |
4710 | bool saw_non_const = false; | |
4711 | /* If we're initializing a static array, we want to do static | |
4712 | initialization of any elements with constant initializers even if | |
4713 | some are non-constant. */ | |
4714 | bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase)); | |
4715 | ||
3bc63227 | 4716 | bool empty_list = false; |
1f65a8c8 JM |
4717 | if (init && BRACE_ENCLOSED_INITIALIZER_P (init) |
4718 | && CONSTRUCTOR_NELTS (init) == 0) | |
3bc63227 JM |
4719 | /* Skip over the handling of non-empty init lists. */ |
4720 | empty_list = true; | |
1f65a8c8 | 4721 | |
fa2200cb JM |
4722 | /* Maybe pull out constant value when from_array? */ |
4723 | ||
1f65a8c8 | 4724 | else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR) |
8d08fdba | 4725 | { |
c32097d8 | 4726 | /* Do non-default initialization of non-trivial arrays resulting from |
f30efcb7 | 4727 | brace-enclosed initializers. */ |
4038c495 | 4728 | unsigned HOST_WIDE_INT idx; |
fa2200cb | 4729 | tree field, elt; |
b25dd954 JM |
4730 | /* If the constructor already has the array type, it's been through |
4731 | digest_init, so we shouldn't try to do anything more. */ | |
4732 | bool digested = same_type_p (atype, TREE_TYPE (init)); | |
094fe153 JM |
4733 | from_array = 0; |
4734 | ||
7d5e76c8 | 4735 | if (length_check) |
5a68fae7 | 4736 | finish_length_check (atype, iterator, obase, CONSTRUCTOR_NELTS (init)); |
7d5e76c8 | 4737 | |
fa2200cb | 4738 | if (try_const) |
c0014b07 | 4739 | vec_alloc (const_vec, CONSTRUCTOR_NELTS (init)); |
fa2200cb | 4740 | |
1e184761 JM |
4741 | tree alloc_obj = NULL_TREE; |
4742 | ||
fa2200cb | 4743 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt) |
8d08fdba | 4744 | { |
f1dedc31 | 4745 | tree baseref = build1 (INDIRECT_REF, type, base); |
fa2200cb | 4746 | tree one_init; |
8d08fdba | 4747 | |
8a72a046 | 4748 | num_initialized_elts++; |
8d08fdba | 4749 | |
119cea98 JM |
4750 | /* We need to see sub-array TARGET_EXPR before cp_fold_r so we can |
4751 | handle cleanup flags properly. */ | |
4752 | gcc_checking_assert (!target_expr_needs_replace (elt)); | |
4753 | ||
b25dd954 | 4754 | if (digested) |
6ffbf87c | 4755 | one_init = cp_build_init_expr (baseref, elt); |
119cea98 | 4756 | else if (tree vi = get_vec_init_expr (elt)) |
57ab3714 JM |
4757 | one_init = expand_vec_init_expr (baseref, vi, complain, |
4758 | cleanup_flags); | |
b25dd954 | 4759 | else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE) |
fa2200cb | 4760 | one_init = build_aggr_init (baseref, elt, 0, complain); |
8a72a046 | 4761 | else |
4f2e1536 MP |
4762 | one_init = cp_build_modify_expr (input_location, baseref, |
4763 | NOP_EXPR, elt, complain); | |
574cfaa4 JM |
4764 | if (one_init == error_mark_node) |
4765 | errors = true; | |
fa2200cb JM |
4766 | if (try_const) |
4767 | { | |
f84aded8 MP |
4768 | if (!field) |
4769 | field = size_int (idx); | |
c0014b07 | 4770 | tree e = maybe_constant_init (one_init); |
fa2200cb JM |
4771 | if (reduced_constant_expression_p (e)) |
4772 | { | |
c0014b07 | 4773 | CONSTRUCTOR_APPEND_ELT (const_vec, field, e); |
fa2200cb JM |
4774 | if (do_static_init) |
4775 | one_init = NULL_TREE; | |
4776 | else | |
6ffbf87c | 4777 | one_init = cp_build_init_expr (baseref, e); |
fa2200cb JM |
4778 | } |
4779 | else | |
4780 | { | |
4781 | if (do_static_init) | |
f11c7048 JJ |
4782 | { |
4783 | tree value = build_zero_init (TREE_TYPE (e), NULL_TREE, | |
4784 | true); | |
4785 | if (value) | |
c0014b07 | 4786 | CONSTRUCTOR_APPEND_ELT (const_vec, field, value); |
f11c7048 | 4787 | } |
fa2200cb JM |
4788 | saw_non_const = true; |
4789 | } | |
4790 | } | |
4791 | ||
4792 | if (one_init) | |
1e184761 JM |
4793 | { |
4794 | /* Only create one std::allocator temporary. */ | |
e0324e26 | 4795 | combine_allocator_temps (one_init, alloc_obj); |
1e184761 JM |
4796 | finish_expr_stmt (one_init); |
4797 | } | |
8a72a046 | 4798 | |
e51fbec3 MP |
4799 | one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, false, |
4800 | complain); | |
574cfaa4 JM |
4801 | if (one_init == error_mark_node) |
4802 | errors = true; | |
4803 | else | |
4804 | finish_expr_stmt (one_init); | |
4805 | ||
e51fbec3 | 4806 | one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false, |
574cfaa4 JM |
4807 | complain); |
4808 | if (one_init == error_mark_node) | |
4809 | errors = true; | |
4810 | else | |
4811 | finish_expr_stmt (one_init); | |
8d08fdba | 4812 | } |
8d08fdba | 4813 | |
3bc63227 JM |
4814 | /* Any elements without explicit initializers get T{}. */ |
4815 | empty_list = true; | |
8d08fdba | 4816 | } |
5a68fae7 JM |
4817 | else if (init && TREE_CODE (init) == STRING_CST) |
4818 | { | |
4819 | /* Check that the array is at least as long as the string. */ | |
4820 | if (length_check) | |
4821 | finish_length_check (atype, iterator, obase, | |
4822 | TREE_STRING_LENGTH (init)); | |
4823 | tree length = build_int_cst (ptrdiff_type_node, | |
4824 | TREE_STRING_LENGTH (init)); | |
4825 | ||
4826 | /* Copy the string to the first part of the array. */ | |
4827 | tree alias_set = build_int_cst (build_pointer_type (type), 0); | |
4828 | tree lhs = build2 (MEM_REF, TREE_TYPE (init), base, alias_set); | |
4829 | tree stmt = build2 (MODIFY_EXPR, void_type_node, lhs, init); | |
4830 | finish_expr_stmt (stmt); | |
4831 | ||
4832 | /* Adjust the counter and pointer. */ | |
4833 | stmt = cp_build_binary_op (loc, MINUS_EXPR, iterator, length, complain); | |
4834 | stmt = build2 (MODIFY_EXPR, void_type_node, iterator, stmt); | |
4835 | finish_expr_stmt (stmt); | |
4836 | ||
4837 | stmt = cp_build_binary_op (loc, PLUS_EXPR, base, length, complain); | |
4838 | stmt = build2 (MODIFY_EXPR, void_type_node, base, stmt); | |
4839 | finish_expr_stmt (stmt); | |
4840 | ||
4841 | /* And set the rest of the array to NUL. */ | |
4842 | from_array = 0; | |
4843 | explicit_value_init_p = true; | |
4844 | } | |
8a72a046 | 4845 | else if (from_array) |
8d08fdba | 4846 | { |
8a72a046 | 4847 | if (init) |
5a4d8044 | 4848 | /* OK, we set base2 above. */; |
95552437 | 4849 | else if (CLASS_TYPE_P (type) |
8a72a046 MM |
4850 | && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) |
4851 | { | |
5ade1ed2 DG |
4852 | if (complain & tf_error) |
4853 | error ("initializer ends prematurely"); | |
574cfaa4 | 4854 | errors = true; |
8a72a046 MM |
4855 | } |
4856 | } | |
8d08fdba | 4857 | |
8a72a046 MM |
4858 | /* Now, default-initialize any remaining elements. We don't need to |
4859 | do that if a) the type does not need constructing, or b) we've | |
094fe153 JM |
4860 | already initialized all the elements. |
4861 | ||
4862 | We do need to keep going if we're copying an array. */ | |
4863 | ||
bd95d75f JM |
4864 | if (try_const && !init |
4865 | && (cxx_dialect < cxx20 | |
4866 | || !default_init_uninitialized_part (inner_elt_type))) | |
a02f26f6 JM |
4867 | /* With a constexpr default constructor, which we checked for when |
4868 | setting try_const above, default-initialization is equivalent to | |
4869 | value-initialization, and build_value_init gives us something more | |
bd95d75f JM |
4870 | friendly to maybe_constant_init. Except in C++20 and up a constexpr |
4871 | constructor need not initialize all the members. */ | |
a02f26f6 | 4872 | explicit_value_init_p = true; |
094fe153 | 4873 | if (from_array |
1f65a8c8 | 4874 | || ((type_build_ctor_call (type) || init || explicit_value_init_p) |
9541ffee | 4875 | && ! (tree_fits_shwi_p (maxindex) |
05bccae2 | 4876 | && (num_initialized_elts |
9439e9a1 | 4877 | == tree_to_shwi (maxindex) + 1)))) |
8a72a046 | 4878 | { |
5453bfed | 4879 | /* If the ITERATOR is lesser or equal to -1, then we don't have to loop; |
8a72a046 | 4880 | we've already initialized all the elements. */ |
4977bab6 | 4881 | tree for_stmt; |
f1dedc31 | 4882 | tree elt_init; |
b84f4651 | 4883 | tree to; |
f1dedc31 | 4884 | |
3f43ac31 | 4885 | for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE); |
17a9e380 | 4886 | finish_init_stmt (for_stmt); |
5453bfed | 4887 | finish_for_cond (build2 (GT_EXPR, boolean_type_node, iterator, |
aab384ae | 4888 | build_int_cst (TREE_TYPE (iterator), -1)), |
73b98860 | 4889 | for_stmt, false, 0, false); |
c743614e JM |
4890 | /* We used to pass this decrement to finish_for_expr; now we add it to |
4891 | elt_init below so it's part of the same full-expression as the | |
4892 | initialization, and thus happens before any potentially throwing | |
4893 | temporary cleanups. */ | |
4894 | tree decr = cp_build_unary_op (PREDECREMENT_EXPR, iterator, false, | |
4895 | complain); | |
4896 | ||
8d08fdba | 4897 | |
b84f4651 MM |
4898 | to = build1 (INDIRECT_REF, type, base); |
4899 | ||
ced2fb08 JM |
4900 | /* If the initializer is {}, then all elements are initialized from T{}. |
4901 | But for non-classes, that's the same as value-initialization. */ | |
4902 | if (empty_list) | |
4903 | { | |
136a8287 JM |
4904 | if (cxx_dialect >= cxx11 |
4905 | && (CLASS_TYPE_P (type) | |
4906 | || TREE_CODE (type) == ARRAY_TYPE)) | |
ced2fb08 | 4907 | { |
08227658 | 4908 | init = build_constructor (init_list_type_node, NULL); |
ced2fb08 JM |
4909 | } |
4910 | else | |
4911 | { | |
4912 | init = NULL_TREE; | |
4913 | explicit_value_init_p = true; | |
4914 | } | |
4915 | } | |
4916 | ||
8d08fdba MS |
4917 | if (from_array) |
4918 | { | |
8d08fdba MS |
4919 | tree from; |
4920 | ||
4921 | if (base2) | |
f91352dc JM |
4922 | { |
4923 | from = build1 (INDIRECT_REF, itype, base2); | |
4924 | if (xvalue) | |
4925 | from = move (from); | |
70f40fea JJ |
4926 | if (direct_init) |
4927 | from = build_tree_list (NULL_TREE, from); | |
f91352dc | 4928 | } |
8d08fdba MS |
4929 | else |
4930 | from = NULL_TREE; | |
4931 | ||
e278212e PC |
4932 | if (TREE_CODE (type) == ARRAY_TYPE) |
4933 | elt_init = build_vec_init (to, NULL_TREE, from, /*val_init*/false, | |
4934 | from_array, complain); | |
4935 | else if (from_array == 2) | |
4f2e1536 MP |
4936 | elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR, |
4937 | from, complain); | |
95552437 | 4938 | else if (type_build_ctor_call (type)) |
5ade1ed2 | 4939 | elt_init = build_aggr_init (to, from, 0, complain); |
8d08fdba | 4940 | else if (from) |
4f2e1536 | 4941 | elt_init = cp_build_modify_expr (input_location, to, NOP_EXPR, from, |
5ade1ed2 | 4942 | complain); |
8d08fdba | 4943 | else |
8dc2b103 | 4944 | gcc_unreachable (); |
8d08fdba MS |
4945 | } |
4946 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
4947 | { | |
16b53405 | 4948 | if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)) |
0186f684 AO |
4949 | { |
4950 | if ((complain & tf_error)) | |
4951 | error_at (loc, "array must be initialized " | |
4952 | "with a brace-enclosed initializer"); | |
4953 | elt_init = error_mark_node; | |
4954 | } | |
4955 | else | |
4956 | elt_init = build_vec_init (build1 (INDIRECT_REF, type, base), | |
4957 | 0, init, | |
4958 | explicit_value_init_p, | |
4959 | 0, complain); | |
f1dedc31 | 4960 | } |
844ae01d | 4961 | else if (explicit_value_init_p) |
309714d4 JM |
4962 | { |
4963 | elt_init = build_value_init (type, complain); | |
574cfaa4 | 4964 | if (elt_init != error_mark_node) |
6ffbf87c | 4965 | elt_init = cp_build_init_expr (to, elt_init); |
309714d4 | 4966 | } |
f1dedc31 | 4967 | else |
844ae01d | 4968 | { |
1f65a8c8 JM |
4969 | gcc_assert (type_build_ctor_call (type) || init); |
4970 | if (CLASS_TYPE_P (type)) | |
4971 | elt_init = build_aggr_init (to, init, 0, complain); | |
4972 | else | |
4973 | { | |
4974 | if (TREE_CODE (init) == TREE_LIST) | |
4975 | init = build_x_compound_expr_from_list (init, ELK_INIT, | |
4976 | complain); | |
143aa5cc PC |
4977 | elt_init = (init == error_mark_node |
4978 | ? error_mark_node | |
4979 | : build2 (INIT_EXPR, type, to, init)); | |
1f65a8c8 | 4980 | } |
844ae01d | 4981 | } |
c8094d83 | 4982 | |
574cfaa4 JM |
4983 | if (elt_init == error_mark_node) |
4984 | errors = true; | |
4985 | ||
c0014b07 JM |
4986 | if (try_const) |
4987 | { | |
a02f26f6 | 4988 | /* FIXME refs to earlier elts */ |
c0014b07 JM |
4989 | tree e = maybe_constant_init (elt_init); |
4990 | if (reduced_constant_expression_p (e)) | |
4991 | { | |
4992 | if (initializer_zerop (e)) | |
4993 | /* Don't fill the CONSTRUCTOR with zeros. */ | |
4994 | e = NULL_TREE; | |
4995 | if (do_static_init) | |
4996 | elt_init = NULL_TREE; | |
4997 | } | |
4998 | else | |
4999 | { | |
5000 | saw_non_const = true; | |
5001 | if (do_static_init) | |
5002 | e = build_zero_init (TREE_TYPE (e), NULL_TREE, true); | |
a02f26f6 JM |
5003 | else |
5004 | e = NULL_TREE; | |
c0014b07 JM |
5005 | } |
5006 | ||
5007 | if (e) | |
5008 | { | |
faa9232d JJ |
5009 | HOST_WIDE_INT last = tree_to_shwi (maxindex); |
5010 | if (num_initialized_elts <= last) | |
c0014b07 JM |
5011 | { |
5012 | tree field = size_int (num_initialized_elts); | |
faa9232d JJ |
5013 | if (num_initialized_elts != last) |
5014 | field = build2 (RANGE_EXPR, sizetype, field, | |
5015 | size_int (last)); | |
c0014b07 JM |
5016 | CONSTRUCTOR_APPEND_ELT (const_vec, field, e); |
5017 | } | |
5018 | } | |
5019 | } | |
5020 | ||
beaee0a8 JM |
5021 | /* [class.temporary]: "There are three contexts in which temporaries are |
5022 | destroyed at a different point than the end of the full- | |
5023 | expression. The first context is when a default constructor is called | |
5024 | to initialize an element of an array with no corresponding | |
5025 | initializer. The second context is when a copy constructor is called | |
5026 | to copy an element of an array while the entire array is copied. In | |
5027 | either case, if the constructor has one or more default arguments, the | |
5028 | destruction of every temporary created in a default argument is | |
5029 | sequenced before the construction of the next array element, if any." | |
5030 | ||
5031 | So, for this loop, statements are full-expressions. */ | |
2a3398e1 | 5032 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
0186f684 | 5033 | if (elt_init && !errors) |
c743614e JM |
5034 | elt_init = build2 (COMPOUND_EXPR, void_type_node, elt_init, decr); |
5035 | else | |
5036 | elt_init = decr; | |
5037 | finish_expr_stmt (elt_init); | |
2a3398e1 | 5038 | current_stmt_tree ()->stmts_are_full_exprs_p = 0; |
8d08fdba | 5039 | |
e51fbec3 | 5040 | finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, false, |
5ade1ed2 | 5041 | complain)); |
8d08fdba | 5042 | if (base2) |
e51fbec3 | 5043 | finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, false, |
5ade1ed2 | 5044 | complain)); |
0fac6b0b | 5045 | |
4977bab6 | 5046 | finish_for_stmt (for_stmt); |
8d08fdba | 5047 | } |
8a72a046 | 5048 | |
303b7406 NS |
5049 | /* The value of the array initialization is the array itself, RVAL |
5050 | is a pointer to the first element. */ | |
325c3691 | 5051 | finish_stmt_expr_expr (rval, stmt_expr); |
f1dedc31 | 5052 | |
2a3398e1 | 5053 | stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt); |
303b7406 | 5054 | |
5fb4d142 JM |
5055 | current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps; |
5056 | ||
5057 | if (errors) | |
5058 | return error_mark_node; | |
c0014b07 JM |
5059 | |
5060 | if (try_const) | |
5061 | { | |
5062 | if (!saw_non_const) | |
5063 | { | |
5064 | tree const_init = build_constructor (atype, const_vec); | |
5065 | return build2 (INIT_EXPR, atype, obase, const_init); | |
5066 | } | |
5067 | else if (do_static_init && !vec_safe_is_empty (const_vec)) | |
5068 | DECL_INITIAL (obase) = build_constructor (atype, const_vec); | |
5069 | else | |
5070 | vec_free (const_vec); | |
5071 | } | |
5fb4d142 | 5072 | |
9207099b JM |
5073 | /* Now make the result have the correct type. */ |
5074 | if (TREE_CODE (atype) == ARRAY_TYPE) | |
5075 | { | |
cc76c502 | 5076 | atype = build_reference_type (atype); |
9207099b | 5077 | stmt_expr = build1 (NOP_EXPR, atype, stmt_expr); |
cc76c502 | 5078 | stmt_expr = convert_from_reference (stmt_expr); |
9207099b | 5079 | } |
c8094d83 | 5080 | |
f1dedc31 | 5081 | return stmt_expr; |
8d08fdba MS |
5082 | } |
5083 | ||
86f45d2c MM |
5084 | /* Call the DTOR_KIND destructor for EXP. FLAGS are as for |
5085 | build_delete. */ | |
298d6f60 MM |
5086 | |
5087 | static tree | |
574cfaa4 JM |
5088 | build_dtor_call (tree exp, special_function_kind dtor_kind, int flags, |
5089 | tsubst_flags_t complain) | |
298d6f60 | 5090 | { |
86f45d2c | 5091 | tree name; |
86f45d2c MM |
5092 | switch (dtor_kind) |
5093 | { | |
5094 | case sfk_complete_destructor: | |
5095 | name = complete_dtor_identifier; | |
5096 | break; | |
5097 | ||
5098 | case sfk_base_destructor: | |
5099 | name = base_dtor_identifier; | |
5100 | break; | |
5101 | ||
5102 | case sfk_deleting_destructor: | |
5103 | name = deleting_dtor_identifier; | |
5104 | break; | |
5105 | ||
5106 | default: | |
8dc2b103 | 5107 | gcc_unreachable (); |
86f45d2c | 5108 | } |
4d20f490 JM |
5109 | |
5110 | return build_special_member_call (exp, name, | |
5111 | /*args=*/NULL, | |
5112 | /*binfo=*/TREE_TYPE (exp), | |
5113 | flags, | |
5114 | complain); | |
298d6f60 MM |
5115 | } |
5116 | ||
8d08fdba MS |
5117 | /* Generate a call to a destructor. TYPE is the type to cast ADDR to. |
5118 | ADDR is an expression which yields the store to be destroyed. | |
86f45d2c MM |
5119 | AUTO_DELETE is the name of the destructor to call, i.e., either |
5120 | sfk_complete_destructor, sfk_base_destructor, or | |
5121 | sfk_deleting_destructor. | |
8d08fdba MS |
5122 | |
5123 | FLAGS is the logical disjunction of zero or more LOOKUP_ | |
ade3dc07 | 5124 | flags. See cp-tree.h for more info. */ |
e92cc029 | 5125 | |
8d08fdba | 5126 | tree |
04e4997a PC |
5127 | build_delete (location_t loc, tree otype, tree addr, |
5128 | special_function_kind auto_delete, | |
574cfaa4 | 5129 | int flags, int use_global_delete, tsubst_flags_t complain) |
8d08fdba | 5130 | { |
8d08fdba | 5131 | tree expr; |
8d08fdba MS |
5132 | |
5133 | if (addr == error_mark_node) | |
5134 | return error_mark_node; | |
5135 | ||
eca7fc57 JM |
5136 | tree type = TYPE_MAIN_VARIANT (otype); |
5137 | ||
8d08fdba MS |
5138 | /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type |
5139 | set to `error_mark_node' before it gets properly cleaned up. */ | |
5140 | if (type == error_mark_node) | |
5141 | return error_mark_node; | |
5142 | ||
9f613f06 | 5143 | if (TYPE_PTR_P (type)) |
eca7fc57 | 5144 | type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); |
8d08fdba | 5145 | |
eca7fc57 JM |
5146 | if (TREE_CODE (type) == ARRAY_TYPE) |
5147 | { | |
5148 | if (TYPE_DOMAIN (type) == NULL_TREE) | |
5149 | { | |
5150 | if (complain & tf_error) | |
04e4997a | 5151 | error_at (loc, "unknown array size in delete"); |
eca7fc57 JM |
5152 | return error_mark_node; |
5153 | } | |
04e4997a | 5154 | return build_vec_delete (loc, addr, array_type_nelts (type), |
eca7fc57 JM |
5155 | auto_delete, use_global_delete, complain); |
5156 | } | |
03a904b5 | 5157 | |
9cbc7d65 JM |
5158 | bool deleting = (auto_delete == sfk_deleting_destructor); |
5159 | gcc_assert (deleting == !(flags & LOOKUP_DESTRUCTOR)); | |
5160 | ||
eca7fc57 | 5161 | if (TYPE_PTR_P (otype)) |
8d08fdba | 5162 | { |
eca7fc57 | 5163 | addr = mark_rvalue_use (addr); |
23b4deba | 5164 | |
b1e5b86c GB |
5165 | /* We don't want to warn about delete of void*, only other |
5166 | incomplete types. Deleting other incomplete types | |
5167 | invokes undefined behavior, but it is not ill-formed, so | |
5168 | compile to something that would even do The Right Thing | |
5169 | (TM) should the type have a trivial dtor and no delete | |
5170 | operator. */ | |
5171 | if (!VOID_TYPE_P (type)) | |
8d08fdba | 5172 | { |
b1e5b86c | 5173 | complete_type (type); |
02a32ab4 | 5174 | if (deleting |
04e4997a | 5175 | && !verify_type_context (loc, TCTX_DEALLOCATION, type, |
02a32ab4 RS |
5176 | !(complain & tf_error))) |
5177 | return error_mark_node; | |
5178 | ||
b1e5b86c GB |
5179 | if (!COMPLETE_TYPE_P (type)) |
5180 | { | |
097f82ec | 5181 | if (complain & tf_warning) |
71205d17 | 5182 | { |
097f82ec | 5183 | auto_diagnostic_group d; |
04e4997a PC |
5184 | if (warning_at (loc, OPT_Wdelete_incomplete, |
5185 | "possible problem detected in invocation of " | |
5186 | "%<operator delete%>")) | |
097f82ec DM |
5187 | { |
5188 | cxx_incomplete_type_diagnostic (addr, type, DK_WARNING); | |
04e4997a | 5189 | inform (loc, |
a9c697b8 MS |
5190 | "neither the destructor nor the class-specific " |
5191 | "%<operator delete%> will be called, even if " | |
5192 | "they are declared when the class is defined"); | |
097f82ec | 5193 | } |
71205d17 | 5194 | } |
b1e5b86c | 5195 | } |
9cbc7d65 | 5196 | else if (deleting && warn_delnonvdtor |
e2ab8a0f JW |
5197 | && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type) |
5198 | && TYPE_POLYMORPHIC_P (type)) | |
014ab419 | 5199 | { |
b2cf76f3 | 5200 | tree dtor = CLASSTYPE_DESTRUCTOR (type); |
014ab419 JW |
5201 | if (!dtor || !DECL_VINDEX (dtor)) |
5202 | { | |
5203 | if (CLASSTYPE_PURE_VIRTUALS (type)) | |
04e4997a PC |
5204 | warning_at (loc, OPT_Wdelete_non_virtual_dtor, |
5205 | "deleting object of abstract class type %qT" | |
5206 | " which has non-virtual destructor" | |
5207 | " will cause undefined behavior", type); | |
014ab419 | 5208 | else |
04e4997a PC |
5209 | warning_at (loc, OPT_Wdelete_non_virtual_dtor, |
5210 | "deleting object of polymorphic class type %qT" | |
5211 | " which has non-virtual destructor" | |
5212 | " might cause undefined behavior", type); | |
014ab419 JW |
5213 | } |
5214 | } | |
8d08fdba | 5215 | } |
2986ae00 | 5216 | |
f4f206f4 | 5217 | /* Throw away const and volatile on target type of addr. */ |
4b978f96 | 5218 | addr = convert_force (build_pointer_type (type), addr, 0, complain); |
8d08fdba | 5219 | } |
8d08fdba MS |
5220 | else |
5221 | { | |
5222 | /* Don't check PROTECT here; leave that decision to the | |
5223 | destructor. If the destructor is accessible, call it, | |
5224 | else report error. */ | |
574cfaa4 JM |
5225 | addr = cp_build_addr_expr (addr, complain); |
5226 | if (addr == error_mark_node) | |
5227 | return error_mark_node; | |
8d08fdba | 5228 | |
4b978f96 | 5229 | addr = convert_force (build_pointer_type (type), addr, 0, complain); |
8d08fdba MS |
5230 | } |
5231 | ||
ed63cd2a | 5232 | tree addr_expr = NULL_TREE; |
9cbc7d65 JM |
5233 | if (deleting) |
5234 | /* We will use ADDR multiple times so we must save it. */ | |
ed63cd2a JM |
5235 | { |
5236 | addr_expr = get_target_expr (addr); | |
5237 | addr = TARGET_EXPR_SLOT (addr_expr); | |
5238 | } | |
eca7fc57 | 5239 | |
cdc18417 JM |
5240 | bool virtual_p = false; |
5241 | if (type_build_dtor_call (type)) | |
5242 | { | |
5243 | if (CLASSTYPE_LAZY_DESTRUCTOR (type)) | |
5244 | lazily_declare_fn (sfk_destructor, type); | |
5245 | virtual_p = DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTOR (type)); | |
5246 | } | |
8d08fdba | 5247 | |
9cbc7d65 JM |
5248 | tree head = NULL_TREE; |
5249 | tree do_delete = NULL_TREE; | |
a6bb6b07 | 5250 | bool destroying_delete = false; |
9cbc7d65 JM |
5251 | |
5252 | if (!deleting) | |
5253 | { | |
5254 | /* Leave do_delete null. */ | |
5255 | } | |
cdc18417 JM |
5256 | /* For `::delete x', we must not use the deleting destructor |
5257 | since then we would not be sure to get the global `operator | |
5258 | delete'. */ | |
9cbc7d65 | 5259 | else if (use_global_delete) |
cdc18417 | 5260 | { |
cdc18417 JM |
5261 | head = get_target_expr (build_headof (addr)); |
5262 | /* Delete the object. */ | |
5263 | do_delete = build_op_delete_call (DELETE_EXPR, | |
5264 | head, | |
5265 | cxx_sizeof_nowarn (type), | |
5266 | /*global_p=*/true, | |
5267 | /*placement=*/NULL_TREE, | |
5268 | /*alloc_fn=*/NULL_TREE, | |
5269 | complain); | |
5270 | /* Otherwise, treat this like a complete object destructor | |
5271 | call. */ | |
5272 | auto_delete = sfk_complete_destructor; | |
8d08fdba | 5273 | } |
cdc18417 JM |
5274 | /* If the destructor is non-virtual, there is no deleting |
5275 | variant. Instead, we must explicitly call the appropriate | |
5276 | `operator delete' here. */ | |
9cbc7d65 | 5277 | else if (!virtual_p) |
8d08fdba | 5278 | { |
cdc18417 JM |
5279 | /* Build the call. */ |
5280 | do_delete = build_op_delete_call (DELETE_EXPR, | |
5281 | addr, | |
5282 | cxx_sizeof_nowarn (type), | |
5283 | /*global_p=*/false, | |
5284 | /*placement=*/NULL_TREE, | |
5285 | /*alloc_fn=*/NULL_TREE, | |
5286 | complain); | |
5287 | /* Call the complete object destructor. */ | |
5288 | auto_delete = sfk_complete_destructor; | |
a6bb6b07 JM |
5289 | if (do_delete != error_mark_node) |
5290 | { | |
5291 | tree fn = get_callee_fndecl (do_delete); | |
5292 | destroying_delete = destroying_delete_p (fn); | |
5293 | } | |
cdc18417 | 5294 | } |
9cbc7d65 | 5295 | else if (TYPE_GETS_REG_DELETE (type)) |
cdc18417 JM |
5296 | { |
5297 | /* Make sure we have access to the member op delete, even though | |
5298 | we'll actually be calling it from the destructor. */ | |
5299 | build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type), | |
5300 | /*global_p=*/false, | |
5301 | /*placement=*/NULL_TREE, | |
5302 | /*alloc_fn=*/NULL_TREE, | |
5303 | complain); | |
5304 | } | |
700f8a87 | 5305 | |
cf2b7020 JM |
5306 | if (destroying_delete) |
5307 | /* The operator delete will call the destructor. */ | |
5308 | expr = addr; | |
5309 | else if (type_build_dtor_call (type)) | |
cdc18417 JM |
5310 | expr = build_dtor_call (cp_build_fold_indirect_ref (addr), |
5311 | auto_delete, flags, complain); | |
5312 | else | |
5313 | expr = build_trivial_dtor_call (addr); | |
5314 | if (expr == error_mark_node) | |
5315 | return error_mark_node; | |
ade3dc07 | 5316 | |
9cbc7d65 | 5317 | if (!deleting) |
04e4997a PC |
5318 | { |
5319 | protected_set_expr_location (expr, loc); | |
5320 | return expr; | |
5321 | } | |
9cbc7d65 | 5322 | |
4924293a PP |
5323 | if (do_delete == error_mark_node) |
5324 | return error_mark_node; | |
8412b939 | 5325 | |
cdc18417 JM |
5326 | if (do_delete && !TREE_SIDE_EFFECTS (expr)) |
5327 | expr = do_delete; | |
5328 | else if (do_delete) | |
5329 | /* The delete operator must be called, regardless of whether | |
5330 | the destructor throws. | |
8d08fdba | 5331 | |
cdc18417 JM |
5332 | [expr.delete]/7 The deallocation function is called |
5333 | regardless of whether the destructor for the object or some | |
5334 | element of the array throws an exception. */ | |
5335 | expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, do_delete); | |
8d08fdba | 5336 | |
cdc18417 JM |
5337 | /* We need to calculate this before the dtor changes the vptr. */ |
5338 | if (head) | |
5339 | expr = build2 (COMPOUND_EXPR, void_type_node, head, expr); | |
8d08fdba | 5340 | |
9cbc7d65 JM |
5341 | /* Handle deleting a null pointer. */ |
5342 | warning_sentinel s (warn_address); | |
04e4997a | 5343 | tree ifexp = cp_build_binary_op (loc, NE_EXPR, addr, |
9cbc7d65 JM |
5344 | nullptr_node, complain); |
5345 | ifexp = cp_fully_fold (ifexp); | |
5346 | ||
5347 | if (ifexp == error_mark_node) | |
5348 | return error_mark_node; | |
5349 | /* This is a compiler generated comparison, don't emit | |
5350 | e.g. -Wnonnull-compare warning for it. */ | |
5351 | else if (TREE_CODE (ifexp) == NE_EXPR) | |
65870e75 | 5352 | suppress_warning (ifexp, OPT_Wnonnull_compare); |
cdc18417 | 5353 | |
9cbc7d65 | 5354 | if (!integer_nonzerop (ifexp)) |
cdc18417 JM |
5355 | expr = build3 (COND_EXPR, void_type_node, ifexp, expr, void_node); |
5356 | ||
ed63cd2a JM |
5357 | if (addr_expr) |
5358 | expr = cp_build_compound_expr (addr_expr, expr, tf_none); | |
5359 | ||
04e4997a | 5360 | protected_set_expr_location (expr, loc); |
cdc18417 | 5361 | return expr; |
ade3dc07 | 5362 | } |
8d08fdba | 5363 | |
ade3dc07 JM |
5364 | /* At the beginning of a destructor, push cleanups that will call the |
5365 | destructors for our base classes and members. | |
2a2480e1 | 5366 | |
a29e1034 | 5367 | Called from begin_destructor_body. */ |
8d08fdba | 5368 | |
ade3dc07 | 5369 | void |
edaf3e03 | 5370 | push_base_cleanups (void) |
ade3dc07 | 5371 | { |
fa743e8c NS |
5372 | tree binfo, base_binfo; |
5373 | int i; | |
ade3dc07 JM |
5374 | tree member; |
5375 | tree expr; | |
9771b263 | 5376 | vec<tree, va_gc> *vbases; |
8d08fdba | 5377 | |
ade3dc07 | 5378 | /* Run destructors for all virtual baseclasses. */ |
45e2bf2e NS |
5379 | if (!ABSTRACT_CLASS_TYPE_P (current_class_type) |
5380 | && CLASSTYPE_VBASECLASSES (current_class_type)) | |
ade3dc07 | 5381 | { |
ade3dc07 | 5382 | tree cond = (condition_conversion |
f293ce4b RS |
5383 | (build2 (BIT_AND_EXPR, integer_type_node, |
5384 | current_in_charge_parm, | |
5385 | integer_two_node))); | |
8d08fdba | 5386 | |
58c42dc2 | 5387 | /* The CLASSTYPE_VBASECLASSES vector is in initialization |
ade3dc07 | 5388 | order, which is also the right order for pushing cleanups. */ |
9ba5ff0f | 5389 | for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0; |
9771b263 | 5390 | vec_safe_iterate (vbases, i, &base_binfo); i++) |
8d08fdba | 5391 | { |
eca7fc57 | 5392 | if (type_build_dtor_call (BINFO_TYPE (base_binfo))) |
8d08fdba | 5393 | { |
c8094d83 | 5394 | expr = build_special_member_call (current_class_ref, |
4ba126e4 | 5395 | base_dtor_identifier, |
c166b898 | 5396 | NULL, |
9ba5ff0f | 5397 | base_binfo, |
c8094d83 | 5398 | (LOOKUP_NORMAL |
5ade1ed2 | 5399 | | LOOKUP_NONVIRTUAL), |
eca7fc57 JM |
5400 | tf_warning_or_error); |
5401 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))) | |
5402 | { | |
5403 | expr = build3 (COND_EXPR, void_type_node, cond, | |
632f2871 | 5404 | expr, void_node); |
eca7fc57 JM |
5405 | finish_decl_cleanup (NULL_TREE, expr); |
5406 | } | |
8d08fdba MS |
5407 | } |
5408 | } | |
ade3dc07 JM |
5409 | } |
5410 | ||
ade3dc07 | 5411 | /* Take care of the remaining baseclasses. */ |
fa743e8c NS |
5412 | for (binfo = TYPE_BINFO (current_class_type), i = 0; |
5413 | BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) | |
ade3dc07 | 5414 | { |
eca7fc57 JM |
5415 | if (BINFO_VIRTUAL_P (base_binfo) |
5416 | || !type_build_dtor_call (BINFO_TYPE (base_binfo))) | |
ade3dc07 JM |
5417 | continue; |
5418 | ||
c8094d83 | 5419 | expr = build_special_member_call (current_class_ref, |
4ba126e4 | 5420 | base_dtor_identifier, |
c166b898 | 5421 | NULL, base_binfo, |
5ade1ed2 DG |
5422 | LOOKUP_NORMAL | LOOKUP_NONVIRTUAL, |
5423 | tf_warning_or_error); | |
eca7fc57 JM |
5424 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))) |
5425 | finish_decl_cleanup (NULL_TREE, expr); | |
ade3dc07 JM |
5426 | } |
5427 | ||
57ece258 JM |
5428 | /* Don't automatically destroy union members. */ |
5429 | if (TREE_CODE (current_class_type) == UNION_TYPE) | |
5430 | return; | |
5431 | ||
ade3dc07 | 5432 | for (member = TYPE_FIELDS (current_class_type); member; |
910ad8de | 5433 | member = DECL_CHAIN (member)) |
ade3dc07 | 5434 | { |
57ece258 JM |
5435 | tree this_type = TREE_TYPE (member); |
5436 | if (this_type == error_mark_node | |
2e5d2970 VR |
5437 | || TREE_CODE (member) != FIELD_DECL |
5438 | || DECL_ARTIFICIAL (member)) | |
ade3dc07 | 5439 | continue; |
04056396 | 5440 | if (ANON_AGGR_TYPE_P (this_type)) |
57ece258 | 5441 | continue; |
eca7fc57 | 5442 | if (type_build_dtor_call (this_type)) |
ade3dc07 | 5443 | { |
c8094d83 MS |
5444 | tree this_member = (build_class_member_access_expr |
5445 | (current_class_ref, member, | |
50ad9642 | 5446 | /*access_path=*/NULL_TREE, |
5ade1ed2 DG |
5447 | /*preserve_reference=*/false, |
5448 | tf_warning_or_error)); | |
04e4997a | 5449 | expr = build_delete (input_location, this_type, this_member, |
ade3dc07 JM |
5450 | sfk_complete_destructor, |
5451 | LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, | |
574cfaa4 | 5452 | 0, tf_warning_or_error); |
eca7fc57 JM |
5453 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type)) |
5454 | finish_decl_cleanup (NULL_TREE, expr); | |
ade3dc07 | 5455 | } |
8d08fdba MS |
5456 | } |
5457 | } | |
5458 | ||
8d08fdba MS |
5459 | /* Build a C++ vector delete expression. |
5460 | MAXINDEX is the number of elements to be deleted. | |
5461 | ELT_SIZE is the nominal size of each element in the vector. | |
5462 | BASE is the expression that should yield the store to be deleted. | |
8d08fdba MS |
5463 | This function expands (or synthesizes) these calls itself. |
5464 | AUTO_DELETE_VEC says whether the container (vector) should be deallocated. | |
8d08fdba MS |
5465 | |
5466 | This also calls delete for virtual baseclasses of elements of the vector. | |
5467 | ||
5468 | Update: MAXINDEX is no longer needed. The size can be extracted from the | |
5469 | start of the vector for pointers, and from the type for arrays. We still | |
5470 | use MAXINDEX for arrays because it happens to already have one of the | |
5471 | values we'd have to extract. (We could use MAXINDEX with pointers to | |
5472 | confirm the size, and trap if the numbers differ; not clear that it'd | |
5473 | be worth bothering.) */ | |
e92cc029 | 5474 | |
8d08fdba | 5475 | tree |
04e4997a | 5476 | build_vec_delete (location_t loc, tree base, tree maxindex, |
574cfaa4 JM |
5477 | special_function_kind auto_delete_vec, |
5478 | int use_global_delete, tsubst_flags_t complain) | |
8d08fdba | 5479 | { |
f30432d7 | 5480 | tree type; |
49b7aacb JM |
5481 | tree rval; |
5482 | tree base_init = NULL_TREE; | |
8d08fdba | 5483 | |
f30432d7 | 5484 | type = TREE_TYPE (base); |
c407792d | 5485 | |
50e10fa8 | 5486 | if (TYPE_PTR_P (type)) |
8d08fdba MS |
5487 | { |
5488 | /* Step back one from start of vector, and read dimension. */ | |
834c6dff | 5489 | tree cookie_addr; |
726a989a | 5490 | tree size_ptr_type = build_pointer_type (sizetype); |
834c6dff | 5491 | |
3c784bca | 5492 | base = mark_rvalue_use (base); |
6742d92b | 5493 | if (TREE_SIDE_EFFECTS (base)) |
49b7aacb JM |
5494 | { |
5495 | base_init = get_target_expr (base); | |
5496 | base = TARGET_EXPR_SLOT (base_init); | |
5497 | } | |
708cae97 | 5498 | type = strip_array_types (TREE_TYPE (type)); |
04e4997a | 5499 | cookie_addr = fold_build1_loc (loc, NEGATE_EXPR, |
db3927fb | 5500 | sizetype, TYPE_SIZE_UNIT (sizetype)); |
5d49b6a7 RG |
5501 | cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base), |
5502 | cookie_addr); | |
04757a2a | 5503 | maxindex = cp_build_fold_indirect_ref (cookie_addr); |
8d08fdba | 5504 | } |
f30432d7 | 5505 | else if (TREE_CODE (type) == ARRAY_TYPE) |
8d08fdba | 5506 | { |
f4f206f4 KH |
5507 | /* Get the total number of things in the array, maxindex is a |
5508 | bad name. */ | |
f30432d7 | 5509 | maxindex = array_type_nelts_total (type); |
834c6dff | 5510 | type = strip_array_types (type); |
16b53405 | 5511 | base = decay_conversion (base, complain); |
574cfaa4 JM |
5512 | if (base == error_mark_node) |
5513 | return error_mark_node; | |
6742d92b | 5514 | if (TREE_SIDE_EFFECTS (base)) |
49b7aacb JM |
5515 | { |
5516 | base_init = get_target_expr (base); | |
5517 | base = TARGET_EXPR_SLOT (base_init); | |
5518 | } | |
8d08fdba MS |
5519 | } |
5520 | else | |
5521 | { | |
574cfaa4 | 5522 | if (base != error_mark_node && !(complain & tf_error)) |
04e4997a PC |
5523 | error_at (loc, |
5524 | "type to vector delete is neither pointer or array type"); | |
8d08fdba MS |
5525 | return error_mark_node; |
5526 | } | |
8d08fdba | 5527 | |
04e4997a | 5528 | rval = build_vec_delete_1 (loc, base, maxindex, type, auto_delete_vec, |
574cfaa4 JM |
5529 | use_global_delete, complain); |
5530 | if (base_init && rval != error_mark_node) | |
f293ce4b | 5531 | rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval); |
49b7aacb | 5532 | |
04e4997a | 5533 | protected_set_expr_location (rval, loc); |
49b7aacb | 5534 | return rval; |
8d08fdba | 5535 | } |
ff502317 BE |
5536 | |
5537 | #include "gt-cp-init.h" |