1 | /* Report error messages, build initializers, and perform |
2 | some front-end optimizations for C++ compiler. |
3 | Copyright (C) 1987-2024 Free Software Foundation, Inc. |
4 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
5 | |
6 | This file is part of GCC. |
7 | |
8 | GCC is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 3, or (at your option) |
11 | any later version. |
12 | |
13 | GCC is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ |
21 | |
22 | |
23 | /* This file is part of the C++ front end. |
24 | It contains routines to build C++ expressions given their operands, |
25 | including computing the types of the result, C and C++ specific error |
26 | checks, and some optimization. */ |
27 | |
28 | #include "config.h" |
29 | #include "system.h" |
30 | #include "coretypes.h" |
31 | #include "cp-tree.h" |
32 | #include "stor-layout.h" |
33 | #include "varasm.h" |
34 | #include "intl.h" |
35 | #include "gcc-rich-location.h" |
36 | #include "target.h" |
37 | |
38 | static tree |
39 | process_init_constructor (tree type, tree init, int nested, int flags, |
40 | tsubst_flags_t complain); |
41 | |
42 | |
43 | /* Print an error message stemming from an attempt to use |
44 | BASETYPE as a base class for TYPE. */ |
45 | |
46 | tree |
47 | error_not_base_type (tree basetype, tree type) |
48 | { |
49 | if (TREE_CODE (basetype) == FUNCTION_DECL) |
50 | basetype = DECL_CONTEXT (basetype); |
51 | error ("type %qT is not a base type for type %qT" , basetype, type); |
52 | return error_mark_node; |
53 | } |
54 | |
55 | tree |
56 | binfo_or_else (tree base, tree type) |
57 | { |
58 | tree binfo = lookup_base (type, base, ba_unique, |
59 | NULL, tf_warning_or_error); |
60 | |
61 | if (binfo == error_mark_node) |
62 | return NULL_TREE; |
63 | else if (!binfo) |
64 | error_not_base_type (basetype: base, type); |
65 | return binfo; |
66 | } |
67 | |
68 | /* According to ARM $7.1.6, "A `const' object may be initialized, but its |
69 | value may not be changed thereafter. */ |
70 | |
71 | void |
72 | cxx_readonly_error (location_t loc, tree arg, enum lvalue_use errstring) |
73 | { |
74 | |
75 | /* This macro is used to emit diagnostics to ensure that all format |
76 | strings are complete sentences, visible to gettext and checked at |
77 | compile time. */ |
78 | |
79 | #define ERROR_FOR_ASSIGNMENT(LOC, AS, ASM, IN, DE, ARG) \ |
80 | do { \ |
81 | switch (errstring) \ |
82 | { \ |
83 | case lv_assign: \ |
84 | error_at (LOC, AS, ARG); \ |
85 | break; \ |
86 | case lv_asm: \ |
87 | error_at (LOC, ASM, ARG); \ |
88 | break; \ |
89 | case lv_increment: \ |
90 | error_at (LOC, IN, ARG); \ |
91 | break; \ |
92 | case lv_decrement: \ |
93 | error_at (LOC, DE, ARG); \ |
94 | break; \ |
95 | default: \ |
96 | gcc_unreachable (); \ |
97 | } \ |
98 | } while (0) |
99 | |
100 | /* Handle C++-specific things first. */ |
101 | |
102 | if (VAR_P (arg) |
103 | && DECL_LANG_SPECIFIC (arg) |
104 | && DECL_IN_AGGR_P (arg) |
105 | && !TREE_STATIC (arg)) |
106 | ERROR_FOR_ASSIGNMENT (loc, |
107 | G_("assignment of constant field %qD" ), |
108 | G_("constant field %qD used as %<asm%> output" ), |
109 | G_("increment of constant field %qD" ), |
110 | G_("decrement of constant field %qD" ), |
111 | arg); |
112 | else if (INDIRECT_REF_P (arg) |
113 | && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (arg, 0))) |
114 | && (VAR_P (TREE_OPERAND (arg, 0)) |
115 | || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL)) |
116 | ERROR_FOR_ASSIGNMENT (loc, |
117 | G_("assignment of read-only reference %qD" ), |
118 | G_("read-only reference %qD used as %<asm%> output" ), |
119 | G_("increment of read-only reference %qD" ), |
120 | G_("decrement of read-only reference %qD" ), |
121 | TREE_OPERAND (arg, 0)); |
122 | else |
123 | readonly_error (loc, arg, errstring); |
124 | } |
125 | |
126 | /* If TYPE has abstract virtual functions, issue an error about trying |
127 | to create an object of that type. DECL is the object declared, or |
128 | NULL_TREE if the declaration is unavailable, in which case USE specifies |
129 | the kind of invalid use. Returns 1 if an error occurred; zero if |
130 | all was well. */ |
131 | |
132 | static int |
133 | abstract_virtuals_error (tree decl, tree type, abstract_class_use use, |
134 | tsubst_flags_t complain) |
135 | { |
136 | vec<tree, va_gc> *pure; |
137 | |
138 | if (TREE_CODE (type) == ARRAY_TYPE) |
139 | { |
140 | decl = NULL_TREE; |
141 | use = ACU_ARRAY; |
142 | type = strip_array_types (type); |
143 | } |
144 | |
145 | /* This function applies only to classes. Any other entity can never |
146 | be abstract. */ |
147 | if (!CLASS_TYPE_P (type)) |
148 | return 0; |
149 | type = TYPE_MAIN_VARIANT (type); |
150 | |
151 | #if 0 |
152 | /* Instantiation here seems to be required by the standard, |
153 | but breaks e.g. boost::bind. FIXME! */ |
154 | /* In SFINAE, non-N3276 context, force instantiation. */ |
155 | if (!(complain & (tf_error|tf_decltype))) |
156 | complete_type (type); |
157 | #endif |
158 | |
159 | if (!TYPE_SIZE (type)) |
160 | /* TYPE is being defined, and during that time |
161 | CLASSTYPE_PURE_VIRTUALS holds the inline friends. */ |
162 | return 0; |
163 | |
164 | pure = CLASSTYPE_PURE_VIRTUALS (type); |
165 | if (!pure) |
166 | return 0; |
167 | |
168 | if (!(complain & tf_error)) |
169 | return 1; |
170 | |
171 | auto_diagnostic_group d; |
172 | if (decl) |
173 | { |
174 | if (VAR_P (decl)) |
175 | error ("cannot declare variable %q+D to be of abstract " |
176 | "type %qT" , decl, type); |
177 | else if (TREE_CODE (decl) == PARM_DECL) |
178 | { |
179 | if (DECL_NAME (decl)) |
180 | error ("cannot declare parameter %q+D to be of abstract type %qT" , |
181 | decl, type); |
182 | else |
183 | error ("cannot declare parameter to be of abstract type %qT" , |
184 | type); |
185 | } |
186 | else if (TREE_CODE (decl) == FIELD_DECL) |
187 | error ("cannot declare field %q+D to be of abstract type %qT" , |
188 | decl, type); |
189 | else if (TREE_CODE (decl) == FUNCTION_DECL |
190 | && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) |
191 | error ("invalid abstract return type for member function %q+#D" , decl); |
192 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
193 | error ("invalid abstract return type for function %q+#D" , decl); |
194 | else if (identifier_p (t: decl)) |
195 | /* Here we do not have location information. */ |
196 | error ("invalid abstract type %qT for %qE" , type, decl); |
197 | else |
198 | error ("invalid abstract type for %q+D" , decl); |
199 | } |
200 | else switch (use) |
201 | { |
202 | case ACU_ARRAY: |
203 | error ("creating array of %qT, which is an abstract class type" , type); |
204 | break; |
205 | case ACU_CAST: |
206 | error ("invalid cast to abstract class type %qT" , type); |
207 | break; |
208 | case ACU_NEW: |
209 | error ("invalid new-expression of abstract class type %qT" , type); |
210 | break; |
211 | case ACU_RETURN: |
212 | error ("invalid abstract return type %qT" , type); |
213 | break; |
214 | case ACU_PARM: |
215 | error ("invalid abstract parameter type %qT" , type); |
216 | break; |
217 | case ACU_THROW: |
218 | error ("expression of abstract class type %qT cannot " |
219 | "be used in throw-expression" , type); |
220 | break; |
221 | case ACU_CATCH: |
222 | error ("cannot declare %<catch%> parameter to be of abstract " |
223 | "class type %qT" , type); |
224 | break; |
225 | default: |
226 | error ("cannot allocate an object of abstract type %qT" , type); |
227 | } |
228 | |
229 | /* Only go through this once. */ |
230 | if (pure->length ()) |
231 | { |
232 | unsigned ix; |
233 | tree fn; |
234 | |
235 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), |
236 | " because the following virtual functions are pure within %qT:" , |
237 | type); |
238 | |
239 | FOR_EACH_VEC_ELT (*pure, ix, fn) |
240 | if (! DECL_CLONED_FUNCTION_P (fn) |
241 | || DECL_COMPLETE_DESTRUCTOR_P (fn)) |
242 | inform (DECL_SOURCE_LOCATION (fn), " %#qD" , fn); |
243 | |
244 | /* Now truncate the vector. This leaves it non-null, so we know |
245 | there are pure virtuals, but empty so we don't list them out |
246 | again. */ |
247 | pure->truncate (size: 0); |
248 | } |
249 | |
250 | return 1; |
251 | } |
252 | |
253 | int |
254 | abstract_virtuals_error (tree decl, tree type, |
255 | tsubst_flags_t complain /* = tf_warning_or_error */) |
256 | { |
257 | return abstract_virtuals_error (decl, type, use: ACU_UNKNOWN, complain); |
258 | } |
259 | |
260 | int |
261 | abstract_virtuals_error (abstract_class_use use, tree type, |
262 | tsubst_flags_t complain /* = tf_warning_or_error */) |
263 | { |
264 | return abstract_virtuals_error (NULL_TREE, type, use, complain); |
265 | } |
266 | |
267 | |
268 | /* Print an inform about the declaration of the incomplete type TYPE. */ |
269 | |
270 | void |
271 | cxx_incomplete_type_inform (const_tree type) |
272 | { |
273 | if (!TYPE_MAIN_DECL (type)) |
274 | return; |
275 | |
276 | location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)); |
277 | tree ptype = strip_top_quals (CONST_CAST_TREE (type)); |
278 | |
279 | if (current_class_type |
280 | && TYPE_BEING_DEFINED (current_class_type) |
281 | && same_type_p (ptype, current_class_type)) |
282 | inform (loc, "definition of %q#T is not complete until " |
283 | "the closing brace" , ptype); |
284 | else if (!TYPE_TEMPLATE_INFO (ptype)) |
285 | inform (loc, "forward declaration of %q#T" , ptype); |
286 | else |
287 | inform (loc, "declaration of %q#T" , ptype); |
288 | } |
289 | |
290 | /* Print an error message for invalid use of an incomplete type. |
291 | VALUE is the expression that was used (or 0 if that isn't known) |
292 | and TYPE is the type that was invalid. DIAG_KIND indicates the |
293 | type of diagnostic (see diagnostic.def). */ |
294 | |
295 | bool |
296 | cxx_incomplete_type_diagnostic (location_t loc, const_tree value, |
297 | const_tree type, diagnostic_t diag_kind) |
298 | { |
299 | bool is_decl = false, complained = false; |
300 | |
301 | /* Avoid duplicate error message. */ |
302 | if (TREE_CODE (type) == ERROR_MARK) |
303 | return false; |
304 | |
305 | if (value) |
306 | { |
307 | STRIP_ANY_LOCATION_WRAPPER (value); |
308 | |
309 | if (VAR_P (value) |
310 | || TREE_CODE (value) == PARM_DECL |
311 | || TREE_CODE (value) == FIELD_DECL) |
312 | { |
313 | complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (value), 0, |
314 | "%qD has incomplete type" , value); |
315 | is_decl = true; |
316 | } |
317 | } |
318 | retry: |
319 | /* We must print an error message. Be clever about what it says. */ |
320 | |
321 | switch (TREE_CODE (type)) |
322 | { |
323 | case RECORD_TYPE: |
324 | case UNION_TYPE: |
325 | case ENUMERAL_TYPE: |
326 | if (!is_decl) |
327 | complained = emit_diagnostic (diag_kind, loc, 0, |
328 | "invalid use of incomplete type %q#T" , |
329 | type); |
330 | if (complained) |
331 | cxx_incomplete_type_inform (type); |
332 | break; |
333 | |
334 | case VOID_TYPE: |
335 | complained = emit_diagnostic (diag_kind, loc, 0, |
336 | "invalid use of %qT" , type); |
337 | break; |
338 | |
339 | case ARRAY_TYPE: |
340 | if (TYPE_DOMAIN (type)) |
341 | { |
342 | type = TREE_TYPE (type); |
343 | goto retry; |
344 | } |
345 | complained = emit_diagnostic (diag_kind, loc, 0, |
346 | "invalid use of array with unspecified bounds" ); |
347 | break; |
348 | |
349 | case OFFSET_TYPE: |
350 | bad_member: |
351 | { |
352 | tree member = TREE_OPERAND (value, 1); |
353 | if (is_overloaded_fn (member) && !flag_ms_extensions) |
354 | { |
355 | gcc_rich_location richloc (loc); |
356 | /* If "member" has no arguments (other than "this"), then |
357 | add a fix-it hint. */ |
358 | member = MAYBE_BASELINK_FUNCTIONS (member); |
359 | if (TREE_CODE (member) == FUNCTION_DECL |
360 | && DECL_OBJECT_MEMBER_FUNCTION_P (member) |
361 | && type_num_arguments (TREE_TYPE (member)) == 1) |
362 | richloc.add_fixit_insert_after (new_content: "()" ); |
363 | complained = emit_diagnostic (diag_kind, &richloc, 0, |
364 | "invalid use of member function %qD " |
365 | "(did you forget the %<()%> ?)" , member); |
366 | } |
367 | else |
368 | complained = emit_diagnostic (diag_kind, loc, 0, |
369 | "invalid use of member %qD " |
370 | "(did you forget the %<&%> ?)" , member); |
371 | } |
372 | break; |
373 | |
374 | case TEMPLATE_TYPE_PARM: |
375 | if (is_auto (type)) |
376 | { |
377 | if (CLASS_PLACEHOLDER_TEMPLATE (type)) |
378 | complained = emit_diagnostic (diag_kind, loc, 0, |
379 | "invalid use of placeholder %qT" , type); |
380 | else |
381 | complained = emit_diagnostic (diag_kind, loc, 0, |
382 | "invalid use of %qT" , type); |
383 | } |
384 | else |
385 | complained = emit_diagnostic (diag_kind, loc, 0, |
386 | "invalid use of template type parameter %qT" , type); |
387 | break; |
388 | |
389 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
390 | complained = emit_diagnostic (diag_kind, loc, 0, |
391 | "invalid use of template template parameter %qT" , |
392 | TYPE_NAME (type)); |
393 | break; |
394 | |
395 | case TYPE_PACK_EXPANSION: |
396 | complained = emit_diagnostic (diag_kind, loc, 0, |
397 | "invalid use of pack expansion %qT" , type); |
398 | break; |
399 | |
400 | case TYPENAME_TYPE: |
401 | case DECLTYPE_TYPE: |
402 | complained = emit_diagnostic (diag_kind, loc, 0, |
403 | "invalid use of dependent type %qT" , type); |
404 | break; |
405 | |
406 | case LANG_TYPE: |
407 | if (type == init_list_type_node) |
408 | { |
409 | complained = emit_diagnostic (diag_kind, loc, 0, |
410 | "invalid use of brace-enclosed initializer list" ); |
411 | break; |
412 | } |
413 | gcc_assert (type == unknown_type_node); |
414 | if (value && TREE_CODE (value) == COMPONENT_REF) |
415 | goto bad_member; |
416 | else if (value && TREE_CODE (value) == ADDR_EXPR) |
417 | complained = emit_diagnostic (diag_kind, loc, 0, |
418 | "address of overloaded function with no contextual " |
419 | "type information" ); |
420 | else if (value && TREE_CODE (value) == OVERLOAD) |
421 | complained = emit_diagnostic (diag_kind, loc, 0, |
422 | "overloaded function with no contextual type information" ); |
423 | else |
424 | complained = emit_diagnostic (diag_kind, loc, 0, |
425 | "insufficient contextual information to determine type" ); |
426 | break; |
427 | |
428 | default: |
429 | gcc_unreachable (); |
430 | } |
431 | |
432 | return complained; |
433 | } |
434 | |
435 | /* Print an error message for invalid use of an incomplete type. |
436 | VALUE is the expression that was used (or 0 if that isn't known) |
437 | and TYPE is the type that was invalid. */ |
438 | |
439 | void |
440 | cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type) |
441 | { |
442 | cxx_incomplete_type_diagnostic (loc, value, type, diag_kind: DK_ERROR); |
443 | } |
444 | |
445 | |
446 | /* We've just initialized subobject SUB; also insert a TARGET_EXPR with an |
447 | EH-only cleanup for SUB. Because of EH region nesting issues, we need to |
448 | make the cleanup conditional on a flag that we will clear once the object is |
449 | fully initialized, so push a new flag onto FLAGS. */ |
450 | |
451 | static void |
452 | maybe_push_temp_cleanup (tree sub, vec<tree,va_gc> **flags) |
453 | { |
454 | if (!flag_exceptions) |
455 | return; |
456 | if (tree cleanup |
457 | = cxx_maybe_build_cleanup (sub, tf_warning_or_error)) |
458 | { |
459 | tree tx = get_target_expr (boolean_true_node); |
460 | tree flag = TARGET_EXPR_SLOT (tx); |
461 | CLEANUP_EH_ONLY (tx) = true; |
462 | TARGET_EXPR_CLEANUP (tx) = build3 (COND_EXPR, void_type_node, |
463 | flag, cleanup, void_node); |
464 | add_stmt (tx); |
465 | vec_safe_push (v&: *flags, obj: flag); |
466 | } |
467 | } |
468 | |
469 | /* The recursive part of split_nonconstant_init. DEST is an lvalue |
470 | expression to which INIT should be assigned. INIT is a CONSTRUCTOR. |
471 | Return true if the whole of the value was initialized by the |
472 | generated statements. */ |
473 | |
474 | static bool |
475 | split_nonconstant_init_1 (tree dest, tree init, bool last, |
476 | vec<tree,va_gc> **flags) |
477 | { |
478 | unsigned HOST_WIDE_INT idx, tidx = HOST_WIDE_INT_M1U; |
479 | tree field_index, value; |
480 | tree type = TREE_TYPE (dest); |
481 | tree inner_type = NULL; |
482 | bool array_type_p = false; |
483 | bool complete_p = true; |
484 | HOST_WIDE_INT num_split_elts = 0; |
485 | tree last_split_elt = NULL_TREE; |
486 | |
487 | switch (TREE_CODE (type)) |
488 | { |
489 | case ARRAY_TYPE: |
490 | inner_type = TREE_TYPE (type); |
491 | array_type_p = true; |
492 | if ((TREE_SIDE_EFFECTS (init) |
493 | && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
494 | || vla_type_p (type)) |
495 | { |
496 | if (!TYPE_DOMAIN (type) |
497 | && TREE_CODE (init) == CONSTRUCTOR |
498 | && CONSTRUCTOR_NELTS (init)) |
499 | { |
500 | /* Flexible array. */ |
501 | cp_complete_array_type (&type, init, /*default*/true); |
502 | dest = build1 (VIEW_CONVERT_EXPR, type, dest); |
503 | } |
504 | |
505 | /* For an array, we only need/want a single cleanup region rather |
506 | than one per element. build_vec_init will handle it. */ |
507 | tree code = build_vec_init (dest, NULL_TREE, init, false, 1, |
508 | tf_warning_or_error, flags); |
509 | add_stmt (code); |
510 | return true; |
511 | } |
512 | /* FALLTHRU */ |
513 | |
514 | case RECORD_TYPE: |
515 | case UNION_TYPE: |
516 | case QUAL_UNION_TYPE: |
517 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, |
518 | field_index, value) |
519 | { |
520 | /* The current implementation of this algorithm assumes that |
521 | the field was set for all the elements. This is usually done |
522 | by process_init_constructor. */ |
523 | gcc_assert (field_index); |
524 | |
525 | if (!array_type_p) |
526 | inner_type = TREE_TYPE (field_index); |
527 | |
528 | tree sub; |
529 | if (array_type_p) |
530 | sub = build4 (ARRAY_REF, inner_type, dest, field_index, |
531 | NULL_TREE, NULL_TREE); |
532 | else |
533 | sub = build3 (COMPONENT_REF, inner_type, dest, field_index, |
534 | NULL_TREE); |
535 | |
536 | bool elt_last = last && idx == CONSTRUCTOR_NELTS (init) - 1; |
537 | |
538 | /* We need to see sub-array TARGET_EXPR before cp_fold_r so we can |
539 | handle cleanup flags properly. */ |
540 | gcc_checking_assert (!target_expr_needs_replace (value)); |
541 | |
542 | if (TREE_CODE (value) == CONSTRUCTOR) |
543 | { |
544 | if (!split_nonconstant_init_1 (dest: sub, init: value, last: elt_last, flags) |
545 | /* For flexible array member with initializer we |
546 | can't remove the initializer, because only the |
547 | initializer determines how many elements the |
548 | flexible array member has. */ |
549 | || (!array_type_p |
550 | && TREE_CODE (inner_type) == ARRAY_TYPE |
551 | && TYPE_DOMAIN (inner_type) == NULL |
552 | && TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE |
553 | && COMPLETE_TYPE_P (TREE_TYPE (value)) |
554 | && !integer_zerop (TYPE_SIZE (TREE_TYPE (value))) |
555 | && elt_last |
556 | && TYPE_HAS_TRIVIAL_DESTRUCTOR |
557 | (strip_array_types (inner_type)))) |
558 | complete_p = false; |
559 | else |
560 | { |
561 | /* Mark element for removal. */ |
562 | last_split_elt = field_index; |
563 | CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE; |
564 | if (idx < tidx) |
565 | tidx = idx; |
566 | num_split_elts++; |
567 | } |
568 | } |
569 | else if (tree vi = get_vec_init_expr (t: value)) |
570 | { |
571 | add_stmt (expand_vec_init_expr (sub, vi, tf_warning_or_error, |
572 | flags)); |
573 | |
574 | /* Mark element for removal. */ |
575 | last_split_elt = field_index; |
576 | CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE; |
577 | if (idx < tidx) |
578 | tidx = idx; |
579 | num_split_elts++; |
580 | } |
581 | else if (!initializer_constant_valid_p (value, inner_type)) |
582 | { |
583 | tree code; |
584 | |
585 | /* Push cleanups for any preceding members with constant |
586 | initialization. */ |
587 | if (CLASS_TYPE_P (type)) |
588 | for (tree prev = (last_split_elt ? |
589 | DECL_CHAIN (last_split_elt) |
590 | : TYPE_FIELDS (type)); |
591 | ; prev = DECL_CHAIN (prev)) |
592 | { |
593 | prev = next_aggregate_field (prev); |
594 | if (prev == field_index) |
595 | break; |
596 | tree ptype = TREE_TYPE (prev); |
597 | if (TYPE_P (ptype) && type_build_dtor_call (ptype)) |
598 | { |
599 | tree pcref = build3 (COMPONENT_REF, ptype, dest, prev, |
600 | NULL_TREE); |
601 | maybe_push_temp_cleanup (sub: pcref, flags); |
602 | } |
603 | } |
604 | |
605 | /* Mark element for removal. */ |
606 | CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE; |
607 | if (idx < tidx) |
608 | tidx = idx; |
609 | |
610 | if (TREE_CODE (field_index) == RANGE_EXPR) |
611 | { |
612 | /* Use build_vec_init to initialize a range. */ |
613 | tree low = TREE_OPERAND (field_index, 0); |
614 | tree hi = TREE_OPERAND (field_index, 1); |
615 | sub = build4 (ARRAY_REF, inner_type, dest, low, |
616 | NULL_TREE, NULL_TREE); |
617 | sub = cp_build_addr_expr (sub, tf_warning_or_error); |
618 | tree max = size_binop (MINUS_EXPR, hi, low); |
619 | code = build_vec_init (sub, max, value, false, 0, |
620 | tf_warning_or_error); |
621 | add_stmt (code); |
622 | if (tree_fits_shwi_p (max)) |
623 | num_split_elts += tree_to_shwi (max); |
624 | } |
625 | else |
626 | { |
627 | /* We may need to add a copy constructor call if |
628 | the field has [[no_unique_address]]. */ |
629 | if (unsafe_return_slot_p (sub)) |
630 | { |
631 | /* But not if the initializer is an implicit ctor call |
632 | we just built in digest_init. */ |
633 | if (TREE_CODE (value) == TARGET_EXPR |
634 | && TARGET_EXPR_LIST_INIT_P (value) |
635 | && make_safe_copy_elision (sub, value)) |
636 | goto build_init; |
637 | |
638 | tree name = (DECL_FIELD_IS_BASE (field_index) |
639 | ? base_ctor_identifier |
640 | : complete_ctor_identifier); |
641 | releasing_vec args = make_tree_vector_single (value); |
642 | code = build_special_member_call |
643 | (sub, name, &args, inner_type, |
644 | LOOKUP_NORMAL, tf_warning_or_error); |
645 | } |
646 | else |
647 | { |
648 | build_init: |
649 | code = cp_build_init_expr (t: sub, i: value); |
650 | } |
651 | code = build_stmt (input_location, EXPR_STMT, code); |
652 | add_stmt (code); |
653 | if (!elt_last) |
654 | maybe_push_temp_cleanup (sub, flags); |
655 | } |
656 | |
657 | last_split_elt = field_index; |
658 | num_split_elts++; |
659 | } |
660 | } |
661 | if (num_split_elts == 1) |
662 | CONSTRUCTOR_ELTS (init)->ordered_remove (ix: tidx); |
663 | else if (num_split_elts > 1) |
664 | { |
665 | /* Perform the delayed ordered removal of non-constant elements |
666 | we split out. */ |
667 | for (idx = tidx; idx < CONSTRUCTOR_NELTS (init); ++idx) |
668 | if (CONSTRUCTOR_ELT (init, idx)->index == NULL_TREE) |
669 | ; |
670 | else |
671 | { |
672 | *CONSTRUCTOR_ELT (init, tidx) = *CONSTRUCTOR_ELT (init, idx); |
673 | ++tidx; |
674 | } |
675 | vec_safe_truncate (CONSTRUCTOR_ELTS (init), size: tidx); |
676 | } |
677 | break; |
678 | |
679 | case VECTOR_TYPE: |
680 | if (!initializer_constant_valid_p (init, type)) |
681 | { |
682 | tree code; |
683 | tree cons = copy_node (init); |
684 | CONSTRUCTOR_ELTS (init) = NULL; |
685 | code = build2 (MODIFY_EXPR, type, dest, cons); |
686 | code = build_stmt (input_location, EXPR_STMT, code); |
687 | add_stmt (code); |
688 | num_split_elts += CONSTRUCTOR_NELTS (init); |
689 | } |
690 | break; |
691 | |
692 | default: |
693 | gcc_unreachable (); |
694 | } |
695 | |
696 | /* The rest of the initializer is now a constant. */ |
697 | TREE_CONSTANT (init) = 1; |
698 | TREE_SIDE_EFFECTS (init) = 0; |
699 | |
700 | /* We didn't split out anything. */ |
701 | if (num_split_elts == 0) |
702 | return false; |
703 | |
704 | return complete_p && complete_ctor_at_level_p (TREE_TYPE (init), |
705 | num_split_elts, inner_type); |
706 | } |
707 | |
708 | /* A subroutine of store_init_value. Splits non-constant static |
709 | initializer INIT into a constant part and generates code to |
710 | perform the non-constant part of the initialization to DEST. |
711 | Returns the code for the runtime init. */ |
712 | |
713 | tree |
714 | split_nonconstant_init (tree dest, tree init) |
715 | { |
716 | tree code; |
717 | |
718 | if (TREE_CODE (init) == TARGET_EXPR) |
719 | init = TARGET_EXPR_INITIAL (init); |
720 | if (TREE_CODE (init) == CONSTRUCTOR) |
721 | { |
722 | /* Subobject initializers are not full-expressions. */ |
723 | auto fe = (make_temp_override |
724 | (var&: current_stmt_tree ()->stmts_are_full_exprs_p, overrider: 0)); |
725 | |
726 | init = cp_fully_fold_init (init); |
727 | code = push_stmt_list (); |
728 | |
729 | /* If the complete object is an array, build_vec_init's cleanup is |
730 | enough. Otherwise, collect flags for disabling subobject |
731 | cleanups once the complete object is fully constructed. */ |
732 | vec<tree, va_gc> *flags = nullptr; |
733 | if (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE) |
734 | flags = make_tree_vector (); |
735 | |
736 | if (split_nonconstant_init_1 (dest, init, last: true, flags: &flags)) |
737 | init = NULL_TREE; |
738 | |
739 | for (tree f : flags) |
740 | { |
741 | /* See maybe_push_temp_cleanup. */ |
742 | tree d = f; |
743 | tree i = boolean_false_node; |
744 | if (TREE_CODE (f) == TREE_LIST) |
745 | { |
746 | /* To disable a build_vec_init cleanup, set |
747 | iterator = maxindex. */ |
748 | d = TREE_PURPOSE (f); |
749 | i = TREE_VALUE (f); |
750 | ggc_free (f); |
751 | } |
752 | add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (d), d, i)); |
753 | } |
754 | release_tree_vector (flags); |
755 | |
756 | code = pop_stmt_list (code); |
757 | if (VAR_P (dest) && !is_local_temp (dest)) |
758 | { |
759 | DECL_INITIAL (dest) = init; |
760 | TREE_READONLY (dest) = 0; |
761 | } |
762 | else if (init) |
763 | { |
764 | tree ie = cp_build_init_expr (t: dest, i: init); |
765 | code = add_stmt_to_compound (ie, code); |
766 | } |
767 | } |
768 | else if (TREE_CODE (init) == STRING_CST |
769 | && array_of_runtime_bound_p (TREE_TYPE (dest))) |
770 | code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false, |
771 | /*from array*/1, tf_warning_or_error); |
772 | else |
773 | code = cp_build_init_expr (t: dest, i: init); |
774 | |
775 | return code; |
776 | } |
777 | |
778 | /* T is the initializer of a constexpr variable. Set CONSTRUCTOR_MUTABLE_POISON |
779 | for any CONSTRUCTOR within T that contains (directly or indirectly) a mutable |
780 | member, thereby poisoning it so it can't be copied to another a constexpr |
781 | variable or read during constexpr evaluation. */ |
782 | |
783 | static void |
784 | poison_mutable_constructors (tree t) |
785 | { |
786 | if (TREE_CODE (t) != CONSTRUCTOR) |
787 | return; |
788 | |
789 | if (cp_has_mutable_p (TREE_TYPE (t))) |
790 | { |
791 | CONSTRUCTOR_MUTABLE_POISON (t) = true; |
792 | |
793 | if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (t)) |
794 | for (const constructor_elt &ce : *elts) |
795 | poison_mutable_constructors (t: ce.value); |
796 | } |
797 | } |
798 | |
799 | /* Perform appropriate conversions on the initial value of a variable, |
800 | store it in the declaration DECL, |
801 | and print any error messages that are appropriate. |
802 | If the init is invalid, store an ERROR_MARK. |
803 | |
804 | C++: Note that INIT might be a TREE_LIST, which would mean that it is |
805 | a base class initializer for some aggregate type, hopefully compatible |
806 | with DECL. If INIT is a single element, and DECL is an aggregate |
807 | type, we silently convert INIT into a TREE_LIST, allowing a constructor |
808 | to be called. |
809 | |
810 | If INIT is a TREE_LIST and there is no constructor, turn INIT |
811 | into a CONSTRUCTOR and use standard initialization techniques. |
812 | Perhaps a warning should be generated? |
813 | |
814 | Returns code to be executed if initialization could not be performed |
815 | for static variable. In that case, caller must emit the code. */ |
816 | |
817 | tree |
818 | store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags) |
819 | { |
820 | tree value, type; |
821 | |
822 | /* If variable's type was invalidly declared, just ignore it. */ |
823 | |
824 | type = TREE_TYPE (decl); |
825 | if (TREE_CODE (type) == ERROR_MARK) |
826 | return NULL_TREE; |
827 | |
828 | if (MAYBE_CLASS_TYPE_P (type)) |
829 | { |
830 | if (TREE_CODE (init) == TREE_LIST) |
831 | { |
832 | error ("constructor syntax used, but no constructor declared " |
833 | "for type %qT" , type); |
834 | init = build_constructor_from_list (init_list_type_node, nreverse (init)); |
835 | } |
836 | } |
837 | |
838 | /* End of special C++ code. */ |
839 | |
840 | if (flags & LOOKUP_ALREADY_DIGESTED) |
841 | value = init; |
842 | else |
843 | { |
844 | if (TREE_STATIC (decl)) |
845 | flags |= LOOKUP_ALLOW_FLEXARRAY_INIT; |
846 | /* Digest the specified initializer into an expression. */ |
847 | value = digest_init_flags (type, init, flags, tf_warning_or_error); |
848 | } |
849 | |
850 | /* Look for braced array initializers for character arrays and |
851 | recursively convert them into STRING_CSTs. */ |
852 | value = braced_lists_to_strings (type, value); |
853 | |
854 | current_ref_temp_count = 0; |
855 | value = extend_ref_init_temps (decl, value, cleanups); |
856 | |
857 | /* In C++11 constant expression is a semantic, not syntactic, property. |
858 | In C++98, make sure that what we thought was a constant expression at |
859 | template definition time is still constant and otherwise perform this |
860 | as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */ |
861 | if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl)) |
862 | { |
863 | bool const_init; |
864 | tree oldval = value; |
865 | if (DECL_DECLARED_CONSTEXPR_P (decl) |
866 | || DECL_DECLARED_CONSTINIT_P (decl) |
867 | || (DECL_IN_AGGR_P (decl) |
868 | && DECL_INITIALIZED_IN_CLASS_P (decl))) |
869 | { |
870 | value = fold_non_dependent_expr (value, tf_warning_or_error, |
871 | /*manifestly_const_eval=*/true, |
872 | decl); |
873 | if (value == error_mark_node) |
874 | ; |
875 | /* Diagnose a non-constant initializer for constexpr variable or |
876 | non-inline in-class-initialized static data member. */ |
877 | else if (!is_constant_expression (value)) |
878 | { |
879 | /* Maybe we want to give this message for constexpr variables as |
880 | well, but that will mean a lot of testsuite adjustment. */ |
881 | if (DECL_DECLARED_CONSTINIT_P (decl)) |
882 | error_at (location_of (decl), |
883 | "%<constinit%> variable %qD does not have a " |
884 | "constant initializer" , decl); |
885 | require_constant_expression (value); |
886 | value = error_mark_node; |
887 | } |
888 | else |
889 | { |
890 | value = maybe_constant_init (value, decl, true); |
891 | |
892 | /* In a template we might not have done the necessary |
893 | transformations to make value actually constant, |
894 | e.g. extend_ref_init_temps. */ |
895 | if (!processing_template_decl |
896 | && !TREE_CONSTANT (value)) |
897 | { |
898 | if (DECL_DECLARED_CONSTINIT_P (decl)) |
899 | error_at (location_of (decl), |
900 | "%<constinit%> variable %qD does not have a " |
901 | "constant initializer" , decl); |
902 | value = cxx_constant_init (value, decl); |
903 | } |
904 | } |
905 | } |
906 | else |
907 | value = fold_non_dependent_init (value, tf_warning_or_error, |
908 | /*manifestly_const_eval=*/true, decl); |
909 | poison_mutable_constructors (t: value); |
910 | const_init = (reduced_constant_expression_p (value) |
911 | || error_operand_p (t: value)); |
912 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init; |
913 | /* FIXME setting TREE_CONSTANT on refs breaks the back end. */ |
914 | if (!TYPE_REF_P (type)) |
915 | TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl); |
916 | if (!const_init) |
917 | value = oldval; |
918 | } |
919 | /* Don't fold initializers of automatic variables in constexpr functions, |
920 | that might fold away something that needs to be diagnosed at constexpr |
921 | evaluation time. */ |
922 | if (!current_function_decl |
923 | || !DECL_DECLARED_CONSTEXPR_P (current_function_decl) |
924 | || TREE_STATIC (decl)) |
925 | value = cp_fully_fold_init (value); |
926 | |
927 | /* Handle aggregate NSDMI in non-constant initializers, too. */ |
928 | value = replace_placeholders (value, decl); |
929 | |
930 | /* A COMPOUND_LITERAL_P CONSTRUCTOR is the syntactic form; by the time we get |
931 | here it should have been digested into an actual value for the type. */ |
932 | gcc_checking_assert (TREE_CODE (value) != CONSTRUCTOR |
933 | || processing_template_decl |
934 | || VECTOR_TYPE_P (type) |
935 | || !TREE_HAS_CONSTRUCTOR (value)); |
936 | |
937 | /* If the initializer is not a constant, fill in DECL_INITIAL with |
938 | the bits that are constant, and then return an expression that |
939 | will perform the dynamic initialization. */ |
940 | if (value != error_mark_node |
941 | && !processing_template_decl |
942 | && (TREE_SIDE_EFFECTS (value) |
943 | || vla_type_p (type) |
944 | || ! reduced_constant_expression_p (value))) |
945 | return split_nonconstant_init (dest: decl, init: value); |
946 | |
947 | /* DECL may change value; purge caches. */ |
948 | clear_cv_and_fold_caches (); |
949 | |
950 | /* If the value is a constant, just put it in DECL_INITIAL. If DECL |
951 | is an automatic variable, the middle end will turn this into a |
952 | dynamic initialization later. */ |
953 | DECL_INITIAL (decl) = value; |
954 | return NULL_TREE; |
955 | } |
956 | |
957 | |
958 | /* Give diagnostic about narrowing conversions within { }, or as part of |
959 | a converted constant expression. If CONST_ONLY, only check |
960 | constants. */ |
961 | |
962 | bool |
963 | check_narrowing (tree type, tree init, tsubst_flags_t complain, |
964 | bool const_only/*= false*/) |
965 | { |
966 | tree ftype = unlowered_expr_type (init); |
967 | bool ok = true; |
968 | REAL_VALUE_TYPE d; |
969 | |
970 | if (((!warn_narrowing || !(complain & tf_warning)) |
971 | && cxx_dialect == cxx98) |
972 | || !ARITHMETIC_TYPE_P (type) |
973 | /* Don't emit bogus warnings with e.g. value-dependent trees. */ |
974 | || instantiation_dependent_expression_p (init)) |
975 | return ok; |
976 | |
977 | if (BRACE_ENCLOSED_INITIALIZER_P (init) |
978 | && TREE_CODE (type) == COMPLEX_TYPE) |
979 | { |
980 | tree elttype = TREE_TYPE (type); |
981 | if (CONSTRUCTOR_NELTS (init) > 0) |
982 | ok &= check_narrowing (type: elttype, CONSTRUCTOR_ELT (init, 0)->value, |
983 | complain); |
984 | if (CONSTRUCTOR_NELTS (init) > 1) |
985 | ok &= check_narrowing (type: elttype, CONSTRUCTOR_ELT (init, 1)->value, |
986 | complain); |
987 | return ok; |
988 | } |
989 | |
990 | /* Even non-dependent expressions can still have template |
991 | codes like CAST_EXPR, so use *_non_dependent_expr to cope. */ |
992 | init = fold_non_dependent_expr (init, complain, /*manifest*/true); |
993 | if (init == error_mark_node) |
994 | return ok; |
995 | |
996 | /* If we were asked to only check constants, return early. */ |
997 | if (const_only && !TREE_CONSTANT (init)) |
998 | return ok; |
999 | |
1000 | if (CP_INTEGRAL_TYPE_P (type) |
1001 | && SCALAR_FLOAT_TYPE_P (ftype)) |
1002 | ok = false; |
1003 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) |
1004 | && CP_INTEGRAL_TYPE_P (type)) |
1005 | { |
1006 | if (TREE_CODE (ftype) == ENUMERAL_TYPE) |
1007 | /* Check for narrowing based on the values of the enumeration. */ |
1008 | ftype = ENUM_UNDERLYING_TYPE (ftype); |
1009 | if ((tree_int_cst_lt (TYPE_MAX_VALUE (type), |
1010 | TYPE_MAX_VALUE (ftype)) |
1011 | || tree_int_cst_lt (TYPE_MIN_VALUE (ftype), |
1012 | TYPE_MIN_VALUE (type))) |
1013 | && (TREE_CODE (init) != INTEGER_CST |
1014 | || !int_fits_type_p (init, type))) |
1015 | ok = false; |
1016 | } |
1017 | /* [dcl.init.list]#7.2: "from long double to double or float, or from |
1018 | double to float". */ |
1019 | else if (SCALAR_FLOAT_TYPE_P (ftype) |
1020 | && SCALAR_FLOAT_TYPE_P (type)) |
1021 | { |
1022 | if ((extended_float_type_p (type: ftype) || extended_float_type_p (type)) |
1023 | ? /* "from a floating-point type T to another floating-point type |
1024 | whose floating-point conversion rank is neither greater than |
1025 | nor equal to that of T". |
1026 | So, it is ok if |
1027 | cp_compare_floating_point_conversion_ranks (ftype, type) |
1028 | returns -2 (type has greater conversion rank than ftype) |
1029 | or [-1..1] (type has equal conversion rank as ftype, possibly |
1030 | different subrank. Only do this if at least one of the |
1031 | types is extended floating-point type, otherwise keep doing |
1032 | what we did before (for the sake of non-standard |
1033 | backend types). */ |
1034 | cp_compare_floating_point_conversion_ranks (ftype, type) >= 2 |
1035 | : ((same_type_p (ftype, long_double_type_node) |
1036 | && (same_type_p (type, double_type_node) |
1037 | || same_type_p (type, float_type_node))) |
1038 | || (same_type_p (ftype, double_type_node) |
1039 | && same_type_p (type, float_type_node)) |
1040 | || (TYPE_PRECISION (type) < TYPE_PRECISION (ftype)))) |
1041 | { |
1042 | if (TREE_CODE (init) == REAL_CST) |
1043 | { |
1044 | /* Issue 703: Loss of precision is OK as long as the value is |
1045 | within the representable range of the new type. */ |
1046 | REAL_VALUE_TYPE r; |
1047 | d = TREE_REAL_CST (init); |
1048 | real_convert (&r, TYPE_MODE (type), &d); |
1049 | if (real_isinf (&r)) |
1050 | ok = false; |
1051 | } |
1052 | else |
1053 | ok = false; |
1054 | } |
1055 | } |
1056 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) |
1057 | && SCALAR_FLOAT_TYPE_P (type)) |
1058 | { |
1059 | ok = false; |
1060 | if (TREE_CODE (init) == INTEGER_CST) |
1061 | { |
1062 | d = real_value_from_int_cst (0, init); |
1063 | if (exact_real_truncate (TYPE_MODE (type), &d)) |
1064 | ok = true; |
1065 | } |
1066 | } |
1067 | else if (TREE_CODE (type) == BOOLEAN_TYPE |
1068 | && (TYPE_PTR_P (ftype) || TYPE_PTRMEM_P (ftype))) |
1069 | /* C++20 P1957R2: converting from a pointer type or a pointer-to-member |
1070 | type to bool should be considered narrowing. This is a DR so is not |
1071 | limited to C++20 only. */ |
1072 | ok = false; |
1073 | |
1074 | bool almost_ok = ok; |
1075 | if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error)) |
1076 | { |
1077 | tree folded = cp_fully_fold (init); |
1078 | if (TREE_CONSTANT (folded) && check_narrowing (type, init: folded, complain: tf_none)) |
1079 | almost_ok = true; |
1080 | } |
1081 | |
1082 | if (!ok) |
1083 | { |
1084 | location_t loc = cp_expr_loc_or_input_loc (t: init); |
1085 | if (cxx_dialect == cxx98) |
1086 | { |
1087 | if (complain & tf_warning) |
1088 | warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE " |
1089 | "from %qH to %qI is ill-formed in C++11" , |
1090 | init, ftype, type); |
1091 | ok = true; |
1092 | } |
1093 | else if (!CONSTANT_CLASS_P (init)) |
1094 | { |
1095 | if (complain & tf_warning_or_error) |
1096 | { |
1097 | auto_diagnostic_group d; |
1098 | if ((!almost_ok || pedantic) |
1099 | && pedwarn (loc, OPT_Wnarrowing, |
1100 | "narrowing conversion of %qE from %qH to %qI" , |
1101 | init, ftype, type) |
1102 | && almost_ok) |
1103 | inform (loc, " the expression has a constant value but is not " |
1104 | "a C++ constant-expression" ); |
1105 | ok = true; |
1106 | } |
1107 | } |
1108 | else if (complain & tf_error) |
1109 | { |
1110 | int savederrorcount = errorcount; |
1111 | permerror_opt (loc, OPT_Wnarrowing, |
1112 | "narrowing conversion of %qE from %qH to %qI" , |
1113 | init, ftype, type); |
1114 | if (errorcount == savederrorcount) |
1115 | ok = true; |
1116 | } |
1117 | } |
1118 | |
1119 | return ok; |
1120 | } |
1121 | |
1122 | /* True iff TYPE is a C++20 "ordinary" character type. */ |
1123 | |
1124 | bool |
1125 | ordinary_char_type_p (tree type) |
1126 | { |
1127 | type = TYPE_MAIN_VARIANT (type); |
1128 | return (type == char_type_node |
1129 | || type == signed_char_type_node |
1130 | || type == unsigned_char_type_node); |
1131 | } |
1132 | |
1133 | /* True iff the string literal INIT has a type suitable for initializing array |
1134 | TYPE. */ |
1135 | |
1136 | bool |
1137 | array_string_literal_compatible_p (tree type, tree init) |
1138 | { |
1139 | tree to_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); |
1140 | tree from_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init))); |
1141 | |
1142 | if (to_char_type == from_char_type) |
1143 | return true; |
1144 | /* The array element type does not match the initializing string |
1145 | literal element type; this is only allowed when both types are |
1146 | ordinary character type. There are no string literals of |
1147 | signed or unsigned char type in the language, but we can get |
1148 | them internally from converting braced-init-lists to |
1149 | STRING_CST. */ |
1150 | if (ordinary_char_type_p (type: to_char_type) |
1151 | && ordinary_char_type_p (type: from_char_type)) |
1152 | return true; |
1153 | |
1154 | /* P2513 (C++20/C++23): "an array of char or unsigned char may |
1155 | be initialized by a UTF-8 string literal, or by such a string |
1156 | literal enclosed in braces." */ |
1157 | if (from_char_type == char8_type_node |
1158 | && (to_char_type == char_type_node |
1159 | || to_char_type == unsigned_char_type_node)) |
1160 | return true; |
1161 | |
1162 | return false; |
1163 | } |
1164 | |
1165 | /* Process the initializer INIT for a variable of type TYPE, emitting |
1166 | diagnostics for invalid initializers and converting the initializer as |
1167 | appropriate. |
1168 | |
1169 | For aggregate types, it assumes that reshape_init has already run, thus the |
1170 | initializer will have the right shape (brace elision has been undone). |
1171 | |
1172 | NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR, |
1173 | 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */ |
1174 | |
1175 | static tree |
1176 | digest_init_r (tree type, tree init, int nested, int flags, |
1177 | tsubst_flags_t complain) |
1178 | { |
1179 | enum tree_code code = TREE_CODE (type); |
1180 | |
1181 | if (error_operand_p (t: init)) |
1182 | return error_mark_node; |
1183 | |
1184 | gcc_assert (init); |
1185 | |
1186 | /* We must strip the outermost array type when completing the type, |
1187 | because the its bounds might be incomplete at the moment. */ |
1188 | if (!complete_type_or_maybe_complain (code == ARRAY_TYPE |
1189 | ? TREE_TYPE (type) : type, NULL_TREE, |
1190 | complain)) |
1191 | return error_mark_node; |
1192 | |
1193 | location_t loc = cp_expr_loc_or_input_loc (t: init); |
1194 | |
1195 | tree stripped_init = init; |
1196 | |
1197 | if (BRACE_ENCLOSED_INITIALIZER_P (init) |
1198 | && CONSTRUCTOR_IS_PAREN_INIT (init)) |
1199 | flags |= LOOKUP_AGGREGATE_PAREN_INIT; |
1200 | |
1201 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue |
1202 | (g++.old-deja/g++.law/casts2.C). */ |
1203 | if (TREE_CODE (init) == NON_LVALUE_EXPR) |
1204 | stripped_init = TREE_OPERAND (init, 0); |
1205 | |
1206 | stripped_init = tree_strip_any_location_wrapper (exp: stripped_init); |
1207 | |
1208 | /* Initialization of an array of chars from a string constant. The initializer |
1209 | can be optionally enclosed in braces, but reshape_init has already removed |
1210 | them if they were present. */ |
1211 | if (code == ARRAY_TYPE) |
1212 | { |
1213 | if (nested && !TYPE_DOMAIN (type)) |
1214 | /* C++ flexible array members have a null domain. */ |
1215 | { |
1216 | if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT) |
1217 | pedwarn (loc, OPT_Wpedantic, |
1218 | "initialization of a flexible array member" ); |
1219 | else |
1220 | { |
1221 | if (complain & tf_error) |
1222 | error_at (loc, "non-static initialization of" |
1223 | " a flexible array member" ); |
1224 | return error_mark_node; |
1225 | } |
1226 | } |
1227 | |
1228 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); |
1229 | if (char_type_p (typ1) |
1230 | && TREE_CODE (stripped_init) == STRING_CST) |
1231 | { |
1232 | if (!array_string_literal_compatible_p (type, init)) |
1233 | { |
1234 | if (complain & tf_error) |
1235 | error_at (loc, "cannot initialize array of %qT from " |
1236 | "a string literal with type array of %qT" , |
1237 | typ1, |
1238 | TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)))); |
1239 | return error_mark_node; |
1240 | } |
1241 | |
1242 | if (nested == 2 && !TYPE_DOMAIN (type)) |
1243 | { |
1244 | if (complain & tf_error) |
1245 | error_at (loc, "initialization of flexible array member " |
1246 | "in a nested context" ); |
1247 | return error_mark_node; |
1248 | } |
1249 | |
1250 | if (type != TREE_TYPE (init) |
1251 | && !variably_modified_type_p (type, NULL_TREE)) |
1252 | { |
1253 | init = copy_node (init); |
1254 | TREE_TYPE (init) = type; |
1255 | /* If we have a location wrapper, then also copy the wrapped |
1256 | node, and update the copy's type. */ |
1257 | if (location_wrapper_p (exp: init)) |
1258 | { |
1259 | stripped_init = copy_node (stripped_init); |
1260 | TREE_OPERAND (init, 0) = stripped_init; |
1261 | TREE_TYPE (stripped_init) = type; |
1262 | } |
1263 | } |
1264 | if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type))) |
1265 | { |
1266 | /* Not a flexible array member. */ |
1267 | int size = TREE_INT_CST_LOW (TYPE_SIZE (type)); |
1268 | size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; |
1269 | /* In C it is ok to subtract 1 from the length of the string |
1270 | because it's ok to ignore the terminating null char that is |
1271 | counted in the length of the constant, but in C++ this would |
1272 | be invalid. */ |
1273 | if (size < TREE_STRING_LENGTH (stripped_init)) |
1274 | { |
1275 | permerror (loc, "initializer-string for %qT is too long" , |
1276 | type); |
1277 | |
1278 | init = build_string (size, |
1279 | TREE_STRING_POINTER (stripped_init)); |
1280 | TREE_TYPE (init) = type; |
1281 | } |
1282 | } |
1283 | return init; |
1284 | } |
1285 | } |
1286 | |
1287 | /* Handle scalar types (including conversions) and references. */ |
1288 | if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) |
1289 | && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE)) |
1290 | { |
1291 | /* Narrowing is OK when initializing an aggregate from |
1292 | a parenthesized list. */ |
1293 | if (nested && !(flags & LOOKUP_AGGREGATE_PAREN_INIT)) |
1294 | flags |= LOOKUP_NO_NARROWING; |
1295 | init = convert_for_initialization (0, type, init, flags, |
1296 | ICR_INIT, NULL_TREE, 0, |
1297 | complain); |
1298 | |
1299 | return init; |
1300 | } |
1301 | |
1302 | /* Come here only for aggregates: records, arrays, unions, complex numbers |
1303 | and vectors. */ |
1304 | gcc_assert (code == ARRAY_TYPE |
1305 | || VECTOR_TYPE_P (type) |
1306 | || code == RECORD_TYPE |
1307 | || code == UNION_TYPE |
1308 | || code == OPAQUE_TYPE |
1309 | || code == COMPLEX_TYPE); |
1310 | |
1311 | /* "If T is a class type and the initializer list has a single |
1312 | element of type cv U, where U is T or a class derived from T, |
1313 | the object is initialized from that element." */ |
1314 | if (cxx_dialect >= cxx11 |
1315 | && BRACE_ENCLOSED_INITIALIZER_P (stripped_init) |
1316 | && !CONSTRUCTOR_IS_DESIGNATED_INIT (stripped_init) |
1317 | && CONSTRUCTOR_NELTS (stripped_init) == 1 |
1318 | && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type)) |
1319 | || VECTOR_TYPE_P (type))) |
1320 | { |
1321 | tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value; |
1322 | if (reference_related_p (type, TREE_TYPE (elt))) |
1323 | { |
1324 | /* In C++17, aggregates can have bases, thus participate in |
1325 | aggregate initialization. In the following case: |
1326 | |
1327 | struct B { int c; }; |
1328 | struct D : B { }; |
1329 | D d{{D{{42}}}}; |
1330 | |
1331 | there's an extra set of braces, so the D temporary initializes |
1332 | the first element of d, which is the B base subobject. The base |
1333 | of type B is copy-initialized from the D temporary, causing |
1334 | object slicing. */ |
1335 | tree field = next_aggregate_field (TYPE_FIELDS (type)); |
1336 | if (field && DECL_FIELD_IS_BASE (field)) |
1337 | { |
1338 | if (warning_at (loc, 0, "initializing a base class of type %qT " |
1339 | "results in object slicing" , TREE_TYPE (field))) |
1340 | inform (loc, "remove %<{ }%> around initializer" ); |
1341 | } |
1342 | else if (flag_checking) |
1343 | /* We should have fixed this in reshape_init. */ |
1344 | gcc_unreachable (); |
1345 | } |
1346 | } |
1347 | |
1348 | if (SIMPLE_TARGET_EXPR_P (stripped_init)) |
1349 | stripped_init = TARGET_EXPR_INITIAL (stripped_init); |
1350 | |
1351 | if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init) |
1352 | && !TYPE_NON_AGGREGATE_CLASS (type)) |
1353 | return process_init_constructor (type, init: stripped_init, nested, flags, |
1354 | complain); |
1355 | else |
1356 | { |
1357 | if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE) |
1358 | { |
1359 | if (complain & tf_error) |
1360 | error_at (loc, "cannot initialize aggregate of type %qT with " |
1361 | "a compound literal" , type); |
1362 | |
1363 | return error_mark_node; |
1364 | } |
1365 | |
1366 | if (code == ARRAY_TYPE |
1367 | && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) |
1368 | { |
1369 | /* Allow the result of build_array_copy and of |
1370 | build_value_init_noctor. */ |
1371 | if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR |
1372 | || TREE_CODE (stripped_init) == CONSTRUCTOR) |
1373 | && (same_type_ignoring_top_level_qualifiers_p |
1374 | (type, TREE_TYPE (init)))) |
1375 | return init; |
1376 | |
1377 | if (complain & tf_error) |
1378 | error_at (loc, "array must be initialized with a brace-enclosed" |
1379 | " initializer" ); |
1380 | return error_mark_node; |
1381 | } |
1382 | |
1383 | return convert_for_initialization (NULL_TREE, type, init, |
1384 | flags, |
1385 | ICR_INIT, NULL_TREE, 0, |
1386 | complain); |
1387 | } |
1388 | } |
1389 | |
1390 | tree |
1391 | digest_init (tree type, tree init, tsubst_flags_t complain) |
1392 | { |
1393 | return digest_init_r (type, init, nested: 0, LOOKUP_IMPLICIT, complain); |
1394 | } |
1395 | |
1396 | tree |
1397 | digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain) |
1398 | { |
1399 | return digest_init_r (type, init, nested: 0, flags, complain); |
1400 | } |
1401 | |
1402 | /* Callback to replace PLACEHOLDER_EXPRs in a TARGET_EXPR (which isn't used |
1403 | in the context of guaranteed copy elision). */ |
1404 | |
1405 | static tree |
1406 | replace_placeholders_for_class_temp_r (tree *tp, int *, void *data) |
1407 | { |
1408 | tree t = *tp; |
1409 | auto pset = static_cast<hash_set<tree> *>(data); |
1410 | |
1411 | /* We're looking for a TARGET_EXPR nested in the whole expression. */ |
1412 | if (TREE_CODE (t) == TARGET_EXPR |
1413 | /* That serves as temporary materialization, not an initializer. */ |
1414 | && !TARGET_EXPR_ELIDING_P (t) |
1415 | && !pset->add (k: t)) |
1416 | { |
1417 | tree init = TARGET_EXPR_INITIAL (t); |
1418 | while (TREE_CODE (init) == COMPOUND_EXPR) |
1419 | init = TREE_OPERAND (init, 1); |
1420 | if (TREE_CODE (init) == CONSTRUCTOR |
1421 | && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)) |
1422 | { |
1423 | tree obj = TARGET_EXPR_SLOT (t); |
1424 | replace_placeholders (init, obj); |
1425 | /* We should have dealt with all PLACEHOLDER_EXPRs. */ |
1426 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = false; |
1427 | gcc_checking_assert (!find_placeholders (init)); |
1428 | } |
1429 | } |
1430 | /* TARGET_EXPRs initializing function arguments are not marked as eliding, |
1431 | even though gimplify_arg drops them on the floor. Don't go replacing |
1432 | placeholders in them. */ |
1433 | else if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR) |
1434 | for (int i = 0; i < call_expr_nargs (t); ++i) |
1435 | { |
1436 | tree arg = get_nth_callarg (t, n: i); |
1437 | if (TREE_CODE (arg) == TARGET_EXPR && !TARGET_EXPR_ELIDING_P (arg)) |
1438 | pset->add (k: arg); |
1439 | } |
1440 | |
1441 | return NULL_TREE; |
1442 | } |
1443 | |
1444 | /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */ |
1445 | tree |
1446 | digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain) |
1447 | { |
1448 | gcc_assert (TREE_CODE (decl) == FIELD_DECL); |
1449 | |
1450 | tree type = TREE_TYPE (decl); |
1451 | if (DECL_BIT_FIELD_TYPE (decl)) |
1452 | type = DECL_BIT_FIELD_TYPE (decl); |
1453 | int flags = LOOKUP_IMPLICIT; |
1454 | if (DIRECT_LIST_INIT_P (init)) |
1455 | { |
1456 | flags = LOOKUP_NORMAL; |
1457 | complain |= tf_no_cleanup; |
1458 | } |
1459 | if (BRACE_ENCLOSED_INITIALIZER_P (init) |
1460 | && CP_AGGREGATE_TYPE_P (type)) |
1461 | init = reshape_init (type, init, complain); |
1462 | init = digest_init_flags (type, init, flags, complain); |
1463 | set_target_expr_eliding (init); |
1464 | |
1465 | /* We may have temporary materialization in a NSDMI, if the initializer |
1466 | has something like A{} in it. Digesting the {} could have introduced |
1467 | a PLACEHOLDER_EXPR referring to A. Now that we've got a TARGET_EXPR, |
1468 | we have an object we can refer to. The reason we bother doing this |
1469 | here is for code like |
1470 | |
1471 | struct A { |
1472 | int x; |
1473 | int y = x; |
1474 | }; |
1475 | |
1476 | struct B { |
1477 | int x = 0; |
1478 | int y = A{x}.y; // #1 |
1479 | }; |
1480 | |
1481 | where in #1 we don't want to end up with two PLACEHOLDER_EXPRs for |
1482 | different types on the same level in a {} when lookup_placeholder |
1483 | wouldn't find a named object for the PLACEHOLDER_EXPR for A. Note, |
1484 | temporary materialization does not occur when initializing an object |
1485 | from a prvalue of the same type, therefore we must not replace the |
1486 | placeholder with a temporary object so that it can be elided. */ |
1487 | hash_set<tree> pset; |
1488 | cp_walk_tree (&init, replace_placeholders_for_class_temp_r, &pset, nullptr); |
1489 | |
1490 | return init; |
1491 | } |
1492 | |
1493 | /* Set of flags used within process_init_constructor to describe the |
1494 | initializers. */ |
1495 | #define PICFLAG_ERRONEOUS 1 |
1496 | #define PICFLAG_NOT_ALL_CONSTANT 2 |
1497 | #define PICFLAG_NOT_ALL_SIMPLE 4 |
1498 | #define PICFLAG_SIDE_EFFECTS 8 |
1499 | #define PICFLAG_VEC_INIT 16 |
1500 | |
1501 | /* Given an initializer INIT, return the flag (PICFLAG_*) which better |
1502 | describe it. */ |
1503 | |
1504 | static int |
1505 | picflag_from_initializer (tree init) |
1506 | { |
1507 | if (init == error_mark_node) |
1508 | return PICFLAG_ERRONEOUS; |
1509 | else if (!TREE_CONSTANT (init)) |
1510 | { |
1511 | if (TREE_SIDE_EFFECTS (init)) |
1512 | return PICFLAG_SIDE_EFFECTS; |
1513 | else |
1514 | return PICFLAG_NOT_ALL_CONSTANT; |
1515 | } |
1516 | else if (!initializer_constant_valid_p (init, TREE_TYPE (init))) |
1517 | return PICFLAG_NOT_ALL_SIMPLE; |
1518 | return 0; |
1519 | } |
1520 | |
1521 | /* Adjust INIT for going into a CONSTRUCTOR. */ |
1522 | |
1523 | static tree |
1524 | massage_init_elt (tree type, tree init, int nested, int flags, |
1525 | tsubst_flags_t complain) |
1526 | { |
1527 | int new_flags = LOOKUP_IMPLICIT; |
1528 | if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT) |
1529 | new_flags |= LOOKUP_ALLOW_FLEXARRAY_INIT; |
1530 | if (flags & LOOKUP_AGGREGATE_PAREN_INIT) |
1531 | new_flags |= LOOKUP_AGGREGATE_PAREN_INIT; |
1532 | init = digest_init_r (type, init, nested: nested ? 2 : 1, flags: new_flags, complain); |
1533 | /* When we defer constant folding within a statement, we may want to |
1534 | defer this folding as well. Don't call this on CONSTRUCTORs because |
1535 | their elements have already been folded, and we must avoid folding |
1536 | the result of get_nsdmi. */ |
1537 | if (TREE_CODE (init) != CONSTRUCTOR) |
1538 | { |
1539 | tree t = fold_non_dependent_init (init, complain); |
1540 | if (TREE_CONSTANT (t)) |
1541 | init = t; |
1542 | set_target_expr_eliding (init); |
1543 | } |
1544 | return init; |
1545 | } |
1546 | |
1547 | /* Subroutine of process_init_constructor, which will process an initializer |
1548 | INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*) |
1549 | which describe the initializers. */ |
1550 | |
1551 | static int |
1552 | process_init_constructor_array (tree type, tree init, int nested, int flags, |
1553 | tsubst_flags_t complain) |
1554 | { |
1555 | unsigned HOST_WIDE_INT i, len = 0; |
1556 | int picflags = 0; |
1557 | bool unbounded = false; |
1558 | constructor_elt *ce; |
1559 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init); |
1560 | |
1561 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE |
1562 | || VECTOR_TYPE_P (type)); |
1563 | |
1564 | if (TREE_CODE (type) == ARRAY_TYPE) |
1565 | { |
1566 | /* C++ flexible array members have a null domain. */ |
1567 | tree domain = TYPE_DOMAIN (type); |
1568 | if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain))) |
1569 | len = wi::ext (x: wi::to_offset (TYPE_MAX_VALUE (domain)) |
1570 | - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1, |
1571 | TYPE_PRECISION (TREE_TYPE (domain)), |
1572 | TYPE_SIGN (TREE_TYPE (domain))).to_uhwi (); |
1573 | else |
1574 | unbounded = true; /* Take as many as there are. */ |
1575 | |
1576 | if (nested == 2 && !domain && !vec_safe_is_empty (v)) |
1577 | { |
1578 | if (complain & tf_error) |
1579 | error_at (cp_expr_loc_or_input_loc (t: init), |
1580 | "initialization of flexible array member " |
1581 | "in a nested context" ); |
1582 | return PICFLAG_ERRONEOUS; |
1583 | } |
1584 | } |
1585 | else |
1586 | /* Vectors are like simple fixed-size arrays. */ |
1587 | unbounded = !TYPE_VECTOR_SUBPARTS (node: type).is_constant (const_value: &len); |
1588 | |
1589 | /* There must not be more initializers than needed. */ |
1590 | if (!unbounded && vec_safe_length (v) > len) |
1591 | { |
1592 | if (complain & tf_error) |
1593 | error ("too many initializers for %qT" , type); |
1594 | else |
1595 | return PICFLAG_ERRONEOUS; |
1596 | } |
1597 | |
1598 | FOR_EACH_VEC_SAFE_ELT (v, i, ce) |
1599 | { |
1600 | if (!ce->index) |
1601 | ce->index = size_int (i); |
1602 | else if (!check_array_designated_initializer (ce, i)) |
1603 | ce->index = error_mark_node; |
1604 | gcc_assert (ce->value); |
1605 | ce->value |
1606 | = massage_init_elt (TREE_TYPE (type), init: ce->value, nested, flags, |
1607 | complain); |
1608 | |
1609 | gcc_checking_assert |
1610 | (ce->value == error_mark_node |
1611 | || (same_type_ignoring_top_level_qualifiers_p |
1612 | (strip_array_types (TREE_TYPE (type)), |
1613 | strip_array_types (TREE_TYPE (ce->value))))); |
1614 | |
1615 | picflags |= picflag_from_initializer (init: ce->value); |
1616 | /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer |
1617 | CONSTRUCTOR. */ |
1618 | if (TREE_CODE (ce->value) == CONSTRUCTOR |
1619 | && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value)) |
1620 | { |
1621 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; |
1622 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0; |
1623 | } |
1624 | } |
1625 | |
1626 | /* No more initializers. If the array is unbounded, we are done. Otherwise, |
1627 | we must add initializers ourselves. */ |
1628 | if (!unbounded) |
1629 | for (; i < len; ++i) |
1630 | { |
1631 | tree next; |
1632 | |
1633 | if (type_build_ctor_call (TREE_TYPE (type))) |
1634 | { |
1635 | /* If this type needs constructors run for default-initialization, |
1636 | we can't rely on the back end to do it for us, so make the |
1637 | initialization explicit by list-initializing from T{}. */ |
1638 | next = build_constructor (init_list_type_node, NULL); |
1639 | next = massage_init_elt (TREE_TYPE (type), init: next, nested, flags, |
1640 | complain); |
1641 | if (initializer_zerop (next)) |
1642 | /* The default zero-initialization is fine for us; don't |
1643 | add anything to the CONSTRUCTOR. */ |
1644 | next = NULL_TREE; |
1645 | } |
1646 | else if (!zero_init_p (TREE_TYPE (type))) |
1647 | next = build_zero_init (TREE_TYPE (type), |
1648 | /*nelts=*/NULL_TREE, |
1649 | /*static_storage_p=*/false); |
1650 | else |
1651 | /* The default zero-initialization is fine for us; don't |
1652 | add anything to the CONSTRUCTOR. */ |
1653 | next = NULL_TREE; |
1654 | |
1655 | if (next) |
1656 | { |
1657 | if (next != error_mark_node |
1658 | && (initializer_constant_valid_p (next, TREE_TYPE (next)) |
1659 | != null_pointer_node)) |
1660 | { |
1661 | /* Use VEC_INIT_EXPR for non-constant initialization of |
1662 | trailing elements with no explicit initializers. */ |
1663 | picflags |= PICFLAG_VEC_INIT; |
1664 | break; |
1665 | } |
1666 | |
1667 | picflags |= picflag_from_initializer (init: next); |
1668 | /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer |
1669 | CONSTRUCTOR. */ |
1670 | if (TREE_CODE (next) == CONSTRUCTOR |
1671 | && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next)) |
1672 | { |
1673 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; |
1674 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0; |
1675 | } |
1676 | if (len > i+1) |
1677 | { |
1678 | tree range = build2 (RANGE_EXPR, size_type_node, |
1679 | build_int_cst (size_type_node, i), |
1680 | build_int_cst (size_type_node, len - 1)); |
1681 | CONSTRUCTOR_APPEND_ELT (v, range, next); |
1682 | break; |
1683 | } |
1684 | else |
1685 | CONSTRUCTOR_APPEND_ELT (v, size_int (i), next); |
1686 | } |
1687 | else |
1688 | /* Don't bother checking all the other elements. */ |
1689 | break; |
1690 | } |
1691 | |
1692 | CONSTRUCTOR_ELTS (init) = v; |
1693 | return picflags; |
1694 | } |
1695 | |
1696 | /* Subroutine of process_init_constructor, which will process an initializer |
1697 | INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe |
1698 | the initializers. */ |
1699 | |
1700 | static int |
1701 | process_init_constructor_record (tree type, tree init, int nested, int flags, |
1702 | tsubst_flags_t complain) |
1703 | { |
1704 | vec<constructor_elt, va_gc> *v = NULL; |
1705 | tree field; |
1706 | int skipped = 0; |
1707 | |
1708 | gcc_assert (TREE_CODE (type) == RECORD_TYPE); |
1709 | gcc_assert (!CLASSTYPE_VBASECLASSES (type)); |
1710 | gcc_assert (!TYPE_BINFO (type) |
1711 | || cxx_dialect >= cxx17 |
1712 | || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type))); |
1713 | gcc_assert (!TYPE_POLYMORPHIC_P (type)); |
1714 | |
1715 | restart: |
1716 | int picflags = 0; |
1717 | unsigned HOST_WIDE_INT idx = 0; |
1718 | int designator_skip = -1; |
1719 | /* Generally, we will always have an index for each initializer (which is |
1720 | a FIELD_DECL, put by reshape_init), but compound literals don't go trough |
1721 | reshape_init. So we need to handle both cases. */ |
1722 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
1723 | { |
1724 | tree next; |
1725 | |
1726 | if (TREE_CODE (field) != FIELD_DECL |
1727 | || (DECL_ARTIFICIAL (field) |
1728 | && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))) |
1729 | continue; |
1730 | |
1731 | if (DECL_UNNAMED_BIT_FIELD (field)) |
1732 | continue; |
1733 | |
1734 | /* If this is a bitfield, first convert to the declared type. */ |
1735 | tree fldtype = TREE_TYPE (field); |
1736 | if (DECL_BIT_FIELD_TYPE (field)) |
1737 | fldtype = DECL_BIT_FIELD_TYPE (field); |
1738 | if (fldtype == error_mark_node) |
1739 | return PICFLAG_ERRONEOUS; |
1740 | |
1741 | next = NULL_TREE; |
1742 | if (idx < CONSTRUCTOR_NELTS (init)) |
1743 | { |
1744 | constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx]; |
1745 | if (ce->index) |
1746 | { |
1747 | /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The |
1748 | latter case can happen in templates where lookup has to be |
1749 | deferred. */ |
1750 | gcc_assert (TREE_CODE (ce->index) == FIELD_DECL |
1751 | || identifier_p (ce->index)); |
1752 | if (ce->index == field || ce->index == DECL_NAME (field)) |
1753 | next = ce->value; |
1754 | else |
1755 | { |
1756 | ce = NULL; |
1757 | if (designator_skip == -1) |
1758 | designator_skip = 1; |
1759 | } |
1760 | } |
1761 | else |
1762 | { |
1763 | designator_skip = 0; |
1764 | next = ce->value; |
1765 | } |
1766 | |
1767 | if (ce) |
1768 | { |
1769 | gcc_assert (ce->value); |
1770 | next = massage_init_elt (type: fldtype, init: next, nested, flags, complain); |
1771 | /* We can't actually elide the temporary when initializing a |
1772 | potentially-overlapping field from a function that returns by |
1773 | value. */ |
1774 | if (ce->index |
1775 | && TREE_CODE (next) == TARGET_EXPR |
1776 | && unsafe_copy_elision_p (ce->index, next)) |
1777 | TARGET_EXPR_ELIDING_P (next) = false; |
1778 | ++idx; |
1779 | } |
1780 | } |
1781 | if (next == error_mark_node) |
1782 | /* We skip initializers for empty bases/fields, so skipping an invalid |
1783 | one could make us accept invalid code. */ |
1784 | return PICFLAG_ERRONEOUS; |
1785 | else if (next) |
1786 | /* Already handled above. */; |
1787 | else if (DECL_INITIAL (field)) |
1788 | { |
1789 | if (skipped > 0) |
1790 | { |
1791 | /* We're using an NSDMI past a field with implicit |
1792 | zero-init. Go back and make it explicit. */ |
1793 | skipped = -1; |
1794 | vec_safe_truncate (v, size: 0); |
1795 | goto restart; |
1796 | } |
1797 | /* C++14 aggregate NSDMI. */ |
1798 | next = get_nsdmi (field, /*ctor*/false, complain); |
1799 | if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) |
1800 | && find_placeholders (next)) |
1801 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; |
1802 | } |
1803 | else if (type_build_ctor_call (fldtype)) |
1804 | { |
1805 | /* If this type needs constructors run for |
1806 | default-initialization, we can't rely on the back end to do it |
1807 | for us, so build up TARGET_EXPRs. If the type in question is |
1808 | a class, just build one up; if it's an array, recurse. */ |
1809 | next = build_constructor (init_list_type_node, NULL); |
1810 | next = massage_init_elt (type: fldtype, init: next, nested, flags, complain); |
1811 | if (TREE_CODE (next) == TARGET_EXPR |
1812 | && unsafe_copy_elision_p (field, next)) |
1813 | TARGET_EXPR_ELIDING_P (next) = false; |
1814 | |
1815 | /* Warn when some struct elements are implicitly initialized. */ |
1816 | if ((complain & tf_warning) |
1817 | && !cp_unevaluated_operand |
1818 | && !EMPTY_CONSTRUCTOR_P (init)) |
1819 | warning (OPT_Wmissing_field_initializers, |
1820 | "missing initializer for member %qD" , field); |
1821 | } |
1822 | else |
1823 | { |
1824 | if (TYPE_REF_P (fldtype)) |
1825 | { |
1826 | if (complain & tf_error) |
1827 | error ("member %qD is uninitialized reference" , field); |
1828 | else |
1829 | return PICFLAG_ERRONEOUS; |
1830 | } |
1831 | else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype)) |
1832 | { |
1833 | if (complain & tf_error) |
1834 | error ("member %qD with uninitialized reference fields" , field); |
1835 | else |
1836 | return PICFLAG_ERRONEOUS; |
1837 | } |
1838 | /* Do nothing for flexible array members since they need not have any |
1839 | elements. Don't worry about 'skipped' because a flexarray has to |
1840 | be the last field. */ |
1841 | else if (TREE_CODE (fldtype) == ARRAY_TYPE && !TYPE_DOMAIN (fldtype)) |
1842 | continue; |
1843 | |
1844 | /* Warn when some struct elements are implicitly initialized |
1845 | to zero. */ |
1846 | if ((complain & tf_warning) |
1847 | && !cp_unevaluated_operand |
1848 | && !EMPTY_CONSTRUCTOR_P (init) |
1849 | && !is_really_empty_class (fldtype, /*ignore_vptr*/false)) |
1850 | warning (OPT_Wmissing_field_initializers, |
1851 | "missing initializer for member %qD" , field); |
1852 | |
1853 | if (!zero_init_p (fldtype) || skipped < 0) |
1854 | { |
1855 | if (TYPE_REF_P (fldtype)) |
1856 | next = build_zero_cst (fldtype); |
1857 | else |
1858 | next = build_zero_init (fldtype, /*nelts=*/NULL_TREE, |
1859 | /*static_storage_p=*/false); |
1860 | } |
1861 | else |
1862 | { |
1863 | /* The default zero-initialization is fine for us; don't |
1864 | add anything to the CONSTRUCTOR. */ |
1865 | skipped = 1; |
1866 | continue; |
1867 | } |
1868 | } |
1869 | |
1870 | if (is_empty_field (field) |
1871 | && !TREE_SIDE_EFFECTS (next)) |
1872 | /* Don't add trivial initialization of an empty base/field to the |
1873 | constructor, as they might not be ordered the way the back-end |
1874 | expects. */ |
1875 | continue; |
1876 | |
1877 | /* If this is a bitfield, now convert to the lowered type. */ |
1878 | if (fldtype != TREE_TYPE (field)) |
1879 | next = cp_convert_and_check (TREE_TYPE (field), next, complain); |
1880 | picflags |= picflag_from_initializer (init: next); |
1881 | /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */ |
1882 | if (TREE_CODE (next) == CONSTRUCTOR |
1883 | && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next)) |
1884 | { |
1885 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; |
1886 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0; |
1887 | } |
1888 | CONSTRUCTOR_APPEND_ELT (v, field, next); |
1889 | } |
1890 | |
1891 | if (idx < CONSTRUCTOR_NELTS (init)) |
1892 | { |
1893 | if (complain & tf_error) |
1894 | { |
1895 | constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx]; |
1896 | /* For better diagnostics, try to find out if it is really |
1897 | the case of too many initializers or if designators are |
1898 | in incorrect order. */ |
1899 | if (designator_skip == 1 && ce->index) |
1900 | { |
1901 | gcc_assert (TREE_CODE (ce->index) == FIELD_DECL |
1902 | || identifier_p (ce->index)); |
1903 | for (field = TYPE_FIELDS (type); |
1904 | field; field = DECL_CHAIN (field)) |
1905 | { |
1906 | if (TREE_CODE (field) != FIELD_DECL |
1907 | || (DECL_ARTIFICIAL (field) |
1908 | && !(cxx_dialect >= cxx17 |
1909 | && DECL_FIELD_IS_BASE (field)))) |
1910 | continue; |
1911 | |
1912 | if (DECL_UNNAMED_BIT_FIELD (field)) |
1913 | continue; |
1914 | |
1915 | if (ce->index == field || ce->index == DECL_NAME (field)) |
1916 | break; |
1917 | } |
1918 | } |
1919 | if (field) |
1920 | error ("designator order for field %qD does not match declaration " |
1921 | "order in %qT" , field, type); |
1922 | else |
1923 | error ("too many initializers for %qT" , type); |
1924 | } |
1925 | else |
1926 | return PICFLAG_ERRONEOUS; |
1927 | } |
1928 | |
1929 | CONSTRUCTOR_ELTS (init) = v; |
1930 | return picflags; |
1931 | } |
1932 | |
1933 | /* Subroutine of process_init_constructor, which will process a single |
1934 | initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*) |
1935 | which describe the initializer. */ |
1936 | |
1937 | static int |
1938 | process_init_constructor_union (tree type, tree init, int nested, int flags, |
1939 | tsubst_flags_t complain) |
1940 | { |
1941 | constructor_elt *ce; |
1942 | int len; |
1943 | |
1944 | /* If the initializer was empty, use the union's NSDMI if it has one. |
1945 | Otherwise use default zero initialization. */ |
1946 | if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) |
1947 | { |
1948 | for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) |
1949 | { |
1950 | if (TREE_CODE (field) == FIELD_DECL |
1951 | && DECL_INITIAL (field) != NULL_TREE) |
1952 | { |
1953 | tree val = get_nsdmi (field, /*in_ctor=*/false, complain); |
1954 | if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) |
1955 | && find_placeholders (val)) |
1956 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; |
1957 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val); |
1958 | break; |
1959 | } |
1960 | } |
1961 | |
1962 | if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) |
1963 | return 0; |
1964 | } |
1965 | |
1966 | len = CONSTRUCTOR_ELTS (init)->length (); |
1967 | if (len > 1) |
1968 | { |
1969 | if (!(complain & tf_error)) |
1970 | return PICFLAG_ERRONEOUS; |
1971 | error ("too many initializers for %qT" , type); |
1972 | CONSTRUCTOR_ELTS (init)->block_remove (ix: 1, len: len-1); |
1973 | } |
1974 | |
1975 | ce = &(*CONSTRUCTOR_ELTS (init))[0]; |
1976 | |
1977 | /* If this element specifies a field, initialize via that field. */ |
1978 | if (ce->index) |
1979 | { |
1980 | if (TREE_CODE (ce->index) == FIELD_DECL) |
1981 | ; |
1982 | else if (identifier_p (t: ce->index)) |
1983 | { |
1984 | /* This can happen within a cast, see g++.dg/opt/cse2.C. */ |
1985 | tree name = ce->index; |
1986 | tree field; |
1987 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) |
1988 | if (DECL_NAME (field) == name) |
1989 | break; |
1990 | if (!field) |
1991 | { |
1992 | if (complain & tf_error) |
1993 | error ("no field %qD found in union being initialized" , |
1994 | field); |
1995 | ce->value = error_mark_node; |
1996 | } |
1997 | ce->index = field; |
1998 | } |
1999 | else |
2000 | { |
2001 | gcc_assert (TREE_CODE (ce->index) == INTEGER_CST |
2002 | || TREE_CODE (ce->index) == RANGE_EXPR); |
2003 | if (complain & tf_error) |
2004 | error ("index value instead of field name in union initializer" ); |
2005 | ce->value = error_mark_node; |
2006 | } |
2007 | } |
2008 | else |
2009 | { |
2010 | /* Find the first named field. ANSI decided in September 1990 |
2011 | that only named fields count here. */ |
2012 | tree field = TYPE_FIELDS (type); |
2013 | while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL)) |
2014 | field = TREE_CHAIN (field); |
2015 | if (field == NULL_TREE) |
2016 | { |
2017 | if (complain & tf_error) |
2018 | error ("too many initializers for %qT" , type); |
2019 | ce->value = error_mark_node; |
2020 | } |
2021 | ce->index = field; |
2022 | } |
2023 | |
2024 | if (ce->value && ce->value != error_mark_node) |
2025 | ce->value = massage_init_elt (TREE_TYPE (ce->index), init: ce->value, nested, |
2026 | flags, complain); |
2027 | |
2028 | /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */ |
2029 | if (ce->value |
2030 | && TREE_CODE (ce->value) == CONSTRUCTOR |
2031 | && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value)) |
2032 | { |
2033 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1; |
2034 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0; |
2035 | } |
2036 | return picflag_from_initializer (init: ce->value); |
2037 | } |
2038 | |
2039 | /* Process INIT, a constructor for a variable of aggregate type TYPE. The |
2040 | constructor is a brace-enclosed initializer, and will be modified in-place. |
2041 | |
2042 | Each element is converted to the right type through digest_init, and |
2043 | missing initializers are added following the language rules (zero-padding, |
2044 | etc.). |
2045 | |
2046 | After the execution, the initializer will have TREE_CONSTANT if all elts are |
2047 | constant, and TREE_STATIC set if, in addition, all elts are simple enough |
2048 | constants that the assembler and linker can compute them. |
2049 | |
2050 | The function returns the initializer itself, or error_mark_node in case |
2051 | of error. */ |
2052 | |
2053 | static tree |
2054 | process_init_constructor (tree type, tree init, int nested, int flags, |
2055 | tsubst_flags_t complain) |
2056 | { |
2057 | int picflags; |
2058 | |
2059 | gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); |
2060 | |
2061 | if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type)) |
2062 | picflags = process_init_constructor_array (type, init, nested, flags, |
2063 | complain); |
2064 | else if (TREE_CODE (type) == RECORD_TYPE) |
2065 | picflags = process_init_constructor_record (type, init, nested, flags, |
2066 | complain); |
2067 | else if (TREE_CODE (type) == UNION_TYPE) |
2068 | picflags = process_init_constructor_union (type, init, nested, flags, |
2069 | complain); |
2070 | else |
2071 | gcc_unreachable (); |
2072 | |
2073 | if (picflags & PICFLAG_ERRONEOUS) |
2074 | return error_mark_node; |
2075 | |
2076 | TREE_TYPE (init) = type; |
2077 | if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE) |
2078 | cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0); |
2079 | if (picflags & PICFLAG_SIDE_EFFECTS) |
2080 | { |
2081 | TREE_CONSTANT (init) = false; |
2082 | TREE_SIDE_EFFECTS (init) = true; |
2083 | } |
2084 | else if (picflags & PICFLAG_NOT_ALL_CONSTANT) |
2085 | { |
2086 | /* Make sure TREE_CONSTANT isn't set from build_constructor. */ |
2087 | TREE_CONSTANT (init) = false; |
2088 | TREE_SIDE_EFFECTS (init) = false; |
2089 | } |
2090 | else |
2091 | { |
2092 | TREE_CONSTANT (init) = 1; |
2093 | TREE_SIDE_EFFECTS (init) = false; |
2094 | if (!(picflags & PICFLAG_NOT_ALL_SIMPLE)) |
2095 | TREE_STATIC (init) = 1; |
2096 | } |
2097 | if (picflags & PICFLAG_VEC_INIT) |
2098 | { |
2099 | /* Defer default-initialization of array elements with no corresponding |
2100 | initializer-clause until later so we can use a loop. */ |
2101 | TREE_TYPE (init) = init_list_type_node; |
2102 | init = build_vec_init_expr (type, init, complain); |
2103 | init = get_target_expr (init); |
2104 | } |
2105 | return init; |
2106 | } |
2107 | |
2108 | /* Given a structure or union value DATUM, construct and return |
2109 | the structure or union component which results from narrowing |
2110 | that value to the base specified in BASETYPE. For example, given the |
2111 | hierarchy |
2112 | |
2113 | class L { int ii; }; |
2114 | class A : L { ... }; |
2115 | class B : L { ... }; |
2116 | class C : A, B { ... }; |
2117 | |
2118 | and the declaration |
2119 | |
2120 | C x; |
2121 | |
2122 | then the expression |
2123 | |
2124 | x.A::ii refers to the ii member of the L part of |
2125 | the A part of the C object named by X. In this case, |
2126 | DATUM would be x, and BASETYPE would be A. |
2127 | |
2128 | I used to think that this was nonconformant, that the standard specified |
2129 | that first we look up ii in A, then convert x to an L& and pull out the |
2130 | ii part. But in fact, it does say that we convert x to an A&; A here |
2131 | is known as the "naming class". (jason 2000-12-19) |
2132 | |
2133 | BINFO_P points to a variable initialized either to NULL_TREE or to the |
2134 | binfo for the specific base subobject we want to convert to. */ |
2135 | |
2136 | tree |
2137 | build_scoped_ref (tree datum, tree basetype, tree* binfo_p) |
2138 | { |
2139 | tree binfo; |
2140 | |
2141 | if (datum == error_mark_node) |
2142 | return error_mark_node; |
2143 | if (*binfo_p) |
2144 | binfo = *binfo_p; |
2145 | else |
2146 | binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, |
2147 | NULL, tf_warning_or_error); |
2148 | |
2149 | if (!binfo || binfo == error_mark_node) |
2150 | { |
2151 | *binfo_p = NULL_TREE; |
2152 | if (!binfo) |
2153 | error_not_base_type (basetype, TREE_TYPE (datum)); |
2154 | return error_mark_node; |
2155 | } |
2156 | |
2157 | *binfo_p = binfo; |
2158 | return build_base_path (PLUS_EXPR, datum, binfo, 1, |
2159 | tf_warning_or_error); |
2160 | } |
2161 | |
2162 | /* Build a reference to an object specified by the C++ `->' operator. |
2163 | Usually this just involves dereferencing the object, but if the |
2164 | `->' operator is overloaded, then such overloads must be |
2165 | performed until an object which does not have the `->' operator |
2166 | overloaded is found. An error is reported when circular pointer |
2167 | delegation is detected. */ |
2168 | |
2169 | tree |
2170 | build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain) |
2171 | { |
2172 | tree orig_expr = expr; |
2173 | tree type = TREE_TYPE (expr); |
2174 | tree last_rval = NULL_TREE; |
2175 | vec<tree, va_gc> *types_memoized = NULL; |
2176 | |
2177 | if (type == error_mark_node) |
2178 | return error_mark_node; |
2179 | |
2180 | if (processing_template_decl) |
2181 | { |
2182 | tree ttype = NULL_TREE; |
2183 | if (type && TYPE_PTR_P (type)) |
2184 | ttype = TREE_TYPE (type); |
2185 | if (ttype && !dependent_scope_p (ttype)) |
2186 | /* Pointer to current instantiation, don't treat as dependent. */; |
2187 | else if (type_dependent_expression_p (expr)) |
2188 | { |
2189 | expr = build_min_nt_loc (loc, ARROW_EXPR, expr); |
2190 | TREE_TYPE (expr) = ttype; |
2191 | return expr; |
2192 | } |
2193 | } |
2194 | |
2195 | if (MAYBE_CLASS_TYPE_P (type)) |
2196 | { |
2197 | struct tinst_level *actual_inst = current_instantiation (); |
2198 | tree fn = NULL; |
2199 | |
2200 | while ((expr = build_new_op (loc, COMPONENT_REF, |
2201 | LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE, |
2202 | NULL_TREE, &fn, complain))) |
2203 | { |
2204 | if (expr == error_mark_node) |
2205 | return error_mark_node; |
2206 | |
2207 | /* This provides a better instantiation backtrace in case of |
2208 | error. */ |
2209 | if (fn && DECL_USE_TEMPLATE (fn)) |
2210 | push_tinst_level_loc (fn, |
2211 | (current_instantiation () != actual_inst) |
2212 | ? DECL_SOURCE_LOCATION (fn) |
2213 | : input_location); |
2214 | fn = NULL; |
2215 | |
2216 | if (vec_member (TREE_TYPE (expr), types_memoized)) |
2217 | { |
2218 | if (complain & tf_error) |
2219 | error ("circular pointer delegation detected" ); |
2220 | return error_mark_node; |
2221 | } |
2222 | |
2223 | vec_safe_push (v&: types_memoized, TREE_TYPE (expr)); |
2224 | last_rval = expr; |
2225 | } |
2226 | |
2227 | while (current_instantiation () != actual_inst) |
2228 | pop_tinst_level (); |
2229 | |
2230 | if (last_rval == NULL_TREE) |
2231 | { |
2232 | if (complain & tf_error) |
2233 | error ("base operand of %<->%> has non-pointer type %qT" , type); |
2234 | return error_mark_node; |
2235 | } |
2236 | |
2237 | if (TYPE_REF_P (TREE_TYPE (last_rval))) |
2238 | last_rval = convert_from_reference (last_rval); |
2239 | } |
2240 | else |
2241 | { |
2242 | last_rval = decay_conversion (expr, complain); |
2243 | if (last_rval == error_mark_node) |
2244 | return error_mark_node; |
2245 | } |
2246 | |
2247 | if (TYPE_PTR_P (TREE_TYPE (last_rval))) |
2248 | { |
2249 | if (processing_template_decl) |
2250 | { |
2251 | expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)), |
2252 | orig_expr); |
2253 | TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval); |
2254 | return expr; |
2255 | } |
2256 | |
2257 | return cp_build_indirect_ref (loc, last_rval, RO_ARROW, complain); |
2258 | } |
2259 | |
2260 | if (complain & tf_error) |
2261 | { |
2262 | if (types_memoized) |
2263 | error ("result of %<operator->()%> yields non-pointer result" ); |
2264 | else |
2265 | error ("base operand of %<->%> is not a pointer" ); |
2266 | } |
2267 | return error_mark_node; |
2268 | } |
2269 | |
2270 | /* Return an expression for "DATUM .* COMPONENT". DATUM has not |
2271 | already been checked out to be of aggregate type. */ |
2272 | |
2273 | tree |
2274 | build_m_component_ref (tree datum, tree component, tsubst_flags_t complain) |
2275 | { |
2276 | tree ptrmem_type; |
2277 | tree objtype; |
2278 | tree type; |
2279 | tree binfo; |
2280 | tree ctype; |
2281 | |
2282 | datum = mark_lvalue_use (datum); |
2283 | component = mark_rvalue_use (component); |
2284 | |
2285 | if (error_operand_p (t: datum) || error_operand_p (t: component)) |
2286 | return error_mark_node; |
2287 | |
2288 | ptrmem_type = TREE_TYPE (component); |
2289 | if (!TYPE_PTRMEM_P (ptrmem_type)) |
2290 | { |
2291 | if (complain & tf_error) |
2292 | error ("%qE cannot be used as a member pointer, since it is of " |
2293 | "type %qT" , component, ptrmem_type); |
2294 | return error_mark_node; |
2295 | } |
2296 | |
2297 | objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum)); |
2298 | if (! MAYBE_CLASS_TYPE_P (objtype)) |
2299 | { |
2300 | if (complain & tf_error) |
2301 | error ("cannot apply member pointer %qE to %qE, which is of " |
2302 | "non-class type %qT" , component, datum, objtype); |
2303 | return error_mark_node; |
2304 | } |
2305 | |
2306 | type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type); |
2307 | ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type)); |
2308 | |
2309 | if (!COMPLETE_TYPE_P (ctype)) |
2310 | { |
2311 | if (!same_type_p (ctype, objtype)) |
2312 | goto mismatch; |
2313 | binfo = NULL; |
2314 | } |
2315 | else |
2316 | { |
2317 | binfo = lookup_base (objtype, ctype, ba_check, NULL, complain); |
2318 | |
2319 | if (!binfo) |
2320 | { |
2321 | mismatch: |
2322 | if (complain & tf_error) |
2323 | error ("pointer to member type %qT incompatible with object " |
2324 | "type %qT" , type, objtype); |
2325 | return error_mark_node; |
2326 | } |
2327 | else if (binfo == error_mark_node) |
2328 | return error_mark_node; |
2329 | } |
2330 | |
2331 | if (TYPE_PTRDATAMEM_P (ptrmem_type)) |
2332 | { |
2333 | bool is_lval = real_lvalue_p (datum); |
2334 | tree ptype; |
2335 | |
2336 | /* Compute the type of the field, as described in [expr.ref]. |
2337 | There's no such thing as a mutable pointer-to-member, so |
2338 | things are not as complex as they are for references to |
2339 | non-static data members. */ |
2340 | type = cp_build_qualified_type (type, |
2341 | (cp_type_quals (type) |
2342 | | cp_type_quals (TREE_TYPE (datum)))); |
2343 | |
2344 | datum = build_address (datum); |
2345 | |
2346 | /* Convert object to the correct base. */ |
2347 | if (binfo) |
2348 | { |
2349 | datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain); |
2350 | if (datum == error_mark_node) |
2351 | return error_mark_node; |
2352 | } |
2353 | |
2354 | /* Build an expression for "object + offset" where offset is the |
2355 | value stored in the pointer-to-data-member. */ |
2356 | ptype = build_pointer_type (type); |
2357 | datum = convert (ptype, datum); |
2358 | if (!processing_template_decl) |
2359 | datum = build2 (POINTER_PLUS_EXPR, ptype, |
2360 | datum, convert_to_ptrofftype (component)); |
2361 | datum = cp_fully_fold (datum); |
2362 | datum = cp_build_fold_indirect_ref (datum); |
2363 | if (datum == error_mark_node) |
2364 | return error_mark_node; |
2365 | |
2366 | /* If the object expression was an rvalue, return an rvalue. */ |
2367 | if (!is_lval) |
2368 | datum = move (datum); |
2369 | return datum; |
2370 | } |
2371 | else |
2372 | { |
2373 | /* 5.5/6: In a .* expression whose object expression is an rvalue, the |
2374 | program is ill-formed if the second operand is a pointer to member |
2375 | function with ref-qualifier & (for C++20: unless its cv-qualifier-seq |
2376 | is const). In a .* expression whose object expression is an lvalue, |
2377 | the program is ill-formed if the second operand is a pointer to member |
2378 | function with ref-qualifier &&. */ |
2379 | if (FUNCTION_REF_QUALIFIED (type)) |
2380 | { |
2381 | bool lval = lvalue_p (datum); |
2382 | if (lval && FUNCTION_RVALUE_QUALIFIED (type)) |
2383 | { |
2384 | if (complain & tf_error) |
2385 | error ("pointer-to-member-function type %qT requires an rvalue" , |
2386 | ptrmem_type); |
2387 | return error_mark_node; |
2388 | } |
2389 | else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type)) |
2390 | { |
2391 | if ((type_memfn_quals (type) |
2392 | & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)) |
2393 | != TYPE_QUAL_CONST) |
2394 | { |
2395 | if (complain & tf_error) |
2396 | error ("pointer-to-member-function type %qT requires " |
2397 | "an lvalue" , ptrmem_type); |
2398 | return error_mark_node; |
2399 | } |
2400 | else if (cxx_dialect < cxx20) |
2401 | { |
2402 | if (complain & tf_warning_or_error) |
2403 | pedwarn (input_location, OPT_Wpedantic, |
2404 | "pointer-to-member-function type %qT requires " |
2405 | "an lvalue before C++20" , ptrmem_type); |
2406 | else |
2407 | return error_mark_node; |
2408 | } |
2409 | } |
2410 | } |
2411 | return build2 (OFFSET_REF, type, datum, component); |
2412 | } |
2413 | } |
2414 | |
2415 | /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */ |
2416 | |
2417 | static tree |
2418 | build_functional_cast_1 (location_t loc, tree exp, tree parms, |
2419 | tsubst_flags_t complain) |
2420 | { |
2421 | /* This is either a call to a constructor, |
2422 | or a C cast in C++'s `functional' notation. */ |
2423 | |
2424 | /* The type to which we are casting. */ |
2425 | tree type; |
2426 | |
2427 | if (error_operand_p (t: exp) || parms == error_mark_node) |
2428 | return error_mark_node; |
2429 | |
2430 | if (TREE_CODE (exp) == TYPE_DECL) |
2431 | { |
2432 | type = TREE_TYPE (exp); |
2433 | |
2434 | if (DECL_ARTIFICIAL (exp)) |
2435 | cp_handle_deprecated_or_unavailable (type); |
2436 | } |
2437 | else |
2438 | type = exp; |
2439 | |
2440 | /* We need to check this explicitly, since value-initialization of |
2441 | arrays is allowed in other situations. */ |
2442 | if (TREE_CODE (type) == ARRAY_TYPE) |
2443 | { |
2444 | if (complain & tf_error) |
2445 | error_at (loc, "functional cast to array type %qT" , type); |
2446 | return error_mark_node; |
2447 | } |
2448 | |
2449 | if (tree anode = type_uses_auto (type)) |
2450 | { |
2451 | tree init; |
2452 | if (CLASS_PLACEHOLDER_TEMPLATE (anode)) |
2453 | init = parms; |
2454 | /* C++23 auto(x). */ |
2455 | else if (!AUTO_IS_DECLTYPE (anode) |
2456 | && list_length (parms) == 1) |
2457 | { |
2458 | init = TREE_VALUE (parms); |
2459 | if (is_constrained_auto (t: anode)) |
2460 | { |
2461 | if (complain & tf_error) |
2462 | error_at (loc, "%<auto(x)%> cannot be constrained" ); |
2463 | return error_mark_node; |
2464 | } |
2465 | else if (cxx_dialect < cxx23) |
2466 | pedwarn (loc, OPT_Wc__23_extensions, |
2467 | "%<auto(x)%> only available with " |
2468 | "%<-std=c++2b%> or %<-std=gnu++2b%>" ); |
2469 | } |
2470 | else |
2471 | { |
2472 | if (complain & tf_error) |
2473 | error_at (loc, "invalid use of %qT" , anode); |
2474 | return error_mark_node; |
2475 | } |
2476 | type = do_auto_deduction (type, init, anode, complain, |
2477 | adc_variable_type); |
2478 | if (type == error_mark_node) |
2479 | return error_mark_node; |
2480 | } |
2481 | |
2482 | if (processing_template_decl) |
2483 | { |
2484 | tree t; |
2485 | |
2486 | /* Diagnose this even in a template. We could also try harder |
2487 | to give all the usual errors when the type and args are |
2488 | non-dependent... */ |
2489 | if (TYPE_REF_P (type) && !parms) |
2490 | { |
2491 | if (complain & tf_error) |
2492 | error_at (loc, "invalid value-initialization of reference type" ); |
2493 | return error_mark_node; |
2494 | } |
2495 | |
2496 | t = build_min (CAST_EXPR, type, parms); |
2497 | /* We don't know if it will or will not have side effects. */ |
2498 | TREE_SIDE_EFFECTS (t) = 1; |
2499 | return t; |
2500 | } |
2501 | |
2502 | if (! MAYBE_CLASS_TYPE_P (type)) |
2503 | { |
2504 | if (parms == NULL_TREE) |
2505 | { |
2506 | if (VOID_TYPE_P (type)) |
2507 | return void_node; |
2508 | return build_value_init (cv_unqualified (type), complain); |
2509 | } |
2510 | |
2511 | /* This must build a C cast. */ |
2512 | parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain); |
2513 | return cp_build_c_cast (loc, type, parms, complain); |
2514 | } |
2515 | |
2516 | /* Prepare to evaluate as a call to a constructor. If this expression |
2517 | is actually used, for example, |
2518 | |
2519 | return X (arg1, arg2, ...); |
2520 | |
2521 | then the slot being initialized will be filled in. */ |
2522 | |
2523 | if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)) |
2524 | return error_mark_node; |
2525 | if (abstract_virtuals_error (use: ACU_CAST, type, complain)) |
2526 | return error_mark_node; |
2527 | |
2528 | /* [expr.type.conv] |
2529 | |
2530 | If the expression list is a single-expression, the type |
2531 | conversion is equivalent (in definedness, and if defined in |
2532 | meaning) to the corresponding cast expression. */ |
2533 | if (parms && TREE_CHAIN (parms) == NULL_TREE) |
2534 | return cp_build_c_cast (loc, type, TREE_VALUE (parms), complain); |
2535 | |
2536 | /* [expr.type.conv] |
2537 | |
2538 | The expression T(), where T is a simple-type-specifier for a |
2539 | non-array complete object type or the (possibly cv-qualified) |
2540 | void type, creates an rvalue of the specified type, which is |
2541 | value-initialized. */ |
2542 | |
2543 | if (parms == NULL_TREE) |
2544 | { |
2545 | exp = build_value_init (type, complain); |
2546 | exp = get_target_expr (exp, complain); |
2547 | return exp; |
2548 | } |
2549 | |
2550 | /* Call the constructor. */ |
2551 | releasing_vec parmvec; |
2552 | for (; parms != NULL_TREE; parms = TREE_CHAIN (parms)) |
2553 | vec_safe_push (r&: parmvec, TREE_VALUE (parms)); |
2554 | exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
2555 | &parmvec, type, LOOKUP_NORMAL, complain); |
2556 | |
2557 | if (exp == error_mark_node) |
2558 | return error_mark_node; |
2559 | |
2560 | return build_cplus_new (type, exp, complain); |
2561 | } |
2562 | |
2563 | tree |
2564 | build_functional_cast (location_t loc, tree exp, tree parms, |
2565 | tsubst_flags_t complain) |
2566 | { |
2567 | tree result = build_functional_cast_1 (loc, exp, parms, complain); |
2568 | protected_set_expr_location (result, loc); |
2569 | return result; |
2570 | } |
2571 | |
2572 | |
2573 | /* Add new exception specifier SPEC, to the LIST we currently have. |
2574 | If it's already in LIST then do nothing. |
2575 | Moan if it's bad and we're allowed to. COMPLAIN < 0 means we |
2576 | know what we're doing. */ |
2577 | |
2578 | tree |
2579 | add_exception_specifier (tree list, tree spec, tsubst_flags_t complain) |
2580 | { |
2581 | bool ok; |
2582 | tree core = spec; |
2583 | bool is_ptr; |
2584 | diagnostic_t diag_type = DK_UNSPECIFIED; /* none */ |
2585 | |
2586 | if (spec == error_mark_node) |
2587 | return list; |
2588 | |
2589 | gcc_assert (spec && (!list || TREE_VALUE (list))); |
2590 | |
2591 | /* [except.spec] 1, type in an exception specifier shall not be |
2592 | incomplete, or pointer or ref to incomplete other than pointer |
2593 | to cv void. */ |
2594 | is_ptr = TYPE_PTR_P (core); |
2595 | if (is_ptr || TYPE_REF_P (core)) |
2596 | core = TREE_TYPE (core); |
2597 | if (complain < 0) |
2598 | ok = true; |
2599 | else if (VOID_TYPE_P (core)) |
2600 | ok = is_ptr; |
2601 | else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM) |
2602 | ok = true; |
2603 | else if (processing_template_decl) |
2604 | ok = true; |
2605 | else if (!verify_type_context (input_location, TCTX_EXCEPTIONS, core, |
2606 | !(complain & tf_error))) |
2607 | return error_mark_node; |
2608 | else |
2609 | { |
2610 | ok = true; |
2611 | /* 15.4/1 says that types in an exception specifier must be complete, |
2612 | but it seems more reasonable to only require this on definitions |
2613 | and calls. So just give a pedwarn at this point; we will give an |
2614 | error later if we hit one of those two cases. */ |
2615 | if (!COMPLETE_TYPE_P (complete_type (core))) |
2616 | diag_type = DK_PEDWARN; /* pedwarn */ |
2617 | } |
2618 | |
2619 | if (ok) |
2620 | { |
2621 | tree probe; |
2622 | |
2623 | for (probe = list; probe; probe = TREE_CHAIN (probe)) |
2624 | if (same_type_p (TREE_VALUE (probe), spec)) |
2625 | break; |
2626 | if (!probe) |
2627 | list = tree_cons (NULL_TREE, spec, list); |
2628 | } |
2629 | else |
2630 | diag_type = DK_ERROR; /* error */ |
2631 | |
2632 | if (diag_type != DK_UNSPECIFIED |
2633 | && (complain & tf_warning_or_error)) |
2634 | cxx_incomplete_type_diagnostic (NULL_TREE, type: core, diag_kind: diag_type); |
2635 | |
2636 | return list; |
2637 | } |
2638 | |
2639 | /* Like nothrow_spec_p, but don't abort on deferred noexcept. */ |
2640 | |
2641 | static bool |
2642 | nothrow_spec_p_uninst (const_tree spec) |
2643 | { |
2644 | if (DEFERRED_NOEXCEPT_SPEC_P (spec)) |
2645 | return false; |
2646 | return nothrow_spec_p (spec); |
2647 | } |
2648 | |
2649 | /* Combine the two exceptions specifier lists LIST and ADD, and return |
2650 | their union. */ |
2651 | |
2652 | tree |
2653 | merge_exception_specifiers (tree list, tree add) |
2654 | { |
2655 | tree noex, orig_list; |
2656 | |
2657 | if (list == error_mark_node || add == error_mark_node) |
2658 | return error_mark_node; |
2659 | |
2660 | /* No exception-specifier or noexcept(false) are less strict than |
2661 | anything else. Prefer the newer variant (LIST). */ |
2662 | if (!list || list == noexcept_false_spec) |
2663 | return list; |
2664 | else if (!add || add == noexcept_false_spec) |
2665 | return add; |
2666 | |
2667 | /* noexcept(true) and throw() are stricter than anything else. |
2668 | As above, prefer the more recent one (LIST). */ |
2669 | if (nothrow_spec_p_uninst (spec: add)) |
2670 | return list; |
2671 | |
2672 | /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */ |
2673 | if (UNEVALUATED_NOEXCEPT_SPEC_P (add) |
2674 | && UNEVALUATED_NOEXCEPT_SPEC_P (list)) |
2675 | return list; |
2676 | /* We should have instantiated other deferred noexcept specs by now. */ |
2677 | gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add)); |
2678 | |
2679 | if (nothrow_spec_p_uninst (spec: list)) |
2680 | return add; |
2681 | noex = TREE_PURPOSE (list); |
2682 | gcc_checking_assert (!TREE_PURPOSE (add) |
2683 | || errorcount || !flag_exceptions |
2684 | || cp_tree_equal (noex, TREE_PURPOSE (add))); |
2685 | |
2686 | /* Combine the dynamic-exception-specifiers, if any. */ |
2687 | orig_list = list; |
2688 | for (; add && TREE_VALUE (add); add = TREE_CHAIN (add)) |
2689 | { |
2690 | tree spec = TREE_VALUE (add); |
2691 | tree probe; |
2692 | |
2693 | for (probe = orig_list; probe && TREE_VALUE (probe); |
2694 | probe = TREE_CHAIN (probe)) |
2695 | if (same_type_p (TREE_VALUE (probe), spec)) |
2696 | break; |
2697 | if (!probe) |
2698 | { |
2699 | spec = build_tree_list (NULL_TREE, spec); |
2700 | TREE_CHAIN (spec) = list; |
2701 | list = spec; |
2702 | } |
2703 | } |
2704 | |
2705 | /* Keep the noexcept-specifier at the beginning of the list. */ |
2706 | if (noex != TREE_PURPOSE (list)) |
2707 | list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list)); |
2708 | |
2709 | return list; |
2710 | } |
2711 | |
2712 | /* Subroutine of build_call. Ensure that each of the types in the |
2713 | exception specification is complete. Technically, 15.4/1 says that |
2714 | they need to be complete when we see a declaration of the function, |
2715 | but we should be able to get away with only requiring this when the |
2716 | function is defined or called. See also add_exception_specifier. */ |
2717 | |
2718 | void |
2719 | require_complete_eh_spec_types (tree fntype, tree decl) |
2720 | { |
2721 | tree raises; |
2722 | /* Don't complain about calls to op new. */ |
2723 | if (decl && DECL_ARTIFICIAL (decl)) |
2724 | return; |
2725 | for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises; |
2726 | raises = TREE_CHAIN (raises)) |
2727 | { |
2728 | tree type = TREE_VALUE (raises); |
2729 | if (type && !COMPLETE_TYPE_P (type)) |
2730 | { |
2731 | if (decl) |
2732 | error |
2733 | ("call to function %qD which throws incomplete type %q#T" , |
2734 | decl, type); |
2735 | else |
2736 | error ("call to function which throws incomplete type %q#T" , |
2737 | decl); |
2738 | } |
2739 | } |
2740 | } |
2741 | |
2742 | /* Record that any TARGET_EXPR in T are going to be elided in |
2743 | cp_gimplify_init_expr (or sooner). */ |
2744 | |
2745 | void |
2746 | set_target_expr_eliding (tree t) |
2747 | { |
2748 | if (!t) |
2749 | return; |
2750 | switch (TREE_CODE (t)) |
2751 | { |
2752 | case TARGET_EXPR: |
2753 | TARGET_EXPR_ELIDING_P (t) = true; |
2754 | break; |
2755 | case COMPOUND_EXPR: |
2756 | set_target_expr_eliding (TREE_OPERAND (t, 1)); |
2757 | break; |
2758 | case COND_EXPR: |
2759 | set_target_expr_eliding (TREE_OPERAND (t, 1)); |
2760 | set_target_expr_eliding (TREE_OPERAND (t, 2)); |
2761 | break; |
2762 | |
2763 | default: |
2764 | break; |
2765 | } |
2766 | } |
2767 | |
2768 | /* Call the above in the process of building an INIT_EXPR. */ |
2769 | |
2770 | tree |
2771 | cp_build_init_expr (location_t loc, tree target, tree init) |
2772 | { |
2773 | set_target_expr_eliding (init); |
2774 | tree ie = build2_loc (loc, code: INIT_EXPR, TREE_TYPE (target), |
2775 | arg0: target, arg1: init); |
2776 | TREE_SIDE_EFFECTS (ie) = true; |
2777 | return ie; |
2778 | } |
2779 | |