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