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