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