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