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