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