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