]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/typeck2.c
Update copyright years.
[thirdparty/gcc.git] / gcc / cp / typeck2.c
CommitLineData
471086d6 1/* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
fbd26352 3 Copyright (C) 1987-2019 Free Software Foundation, Inc.
471086d6 4 Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6f0d25a6 6This file is part of GCC.
471086d6 7
6f0d25a6 8GCC is free software; you can redistribute it and/or modify
471086d6 9it under the terms of the GNU General Public License as published by
aa139c3f 10the Free Software Foundation; either version 3, or (at your option)
471086d6 11any later version.
12
6f0d25a6 13GCC is distributed in the hope that it will be useful,
471086d6 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
aa139c3f 19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
471086d6 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
c857cd60 26 checks, and some optimization. */
471086d6 27
28#include "config.h"
b3ef7553 29#include "system.h"
805e22b2 30#include "coretypes.h"
4cba6f60 31#include "cp-tree.h"
9ed99284 32#include "stor-layout.h"
33#include "varasm.h"
5c892d4a 34#include "intl.h"
6639e735 35#include "gcc-rich-location.h"
471086d6 36
c75b4594 37static tree
7aac6ee6 38process_init_constructor (tree type, tree init, int nested,
39 tsubst_flags_t complain);
c75b4594 40
471086d6 41
471086d6 42/* Print an error message stemming from an attempt to use
43 BASETYPE as a base class for TYPE. */
96624a9e 44
471086d6 45tree
e394ed0f 46error_not_base_type (tree basetype, tree type)
471086d6 47{
48 if (TREE_CODE (basetype) == FUNCTION_DECL)
9ba4048d 49 basetype = DECL_CONTEXT (basetype);
03d6ca9e 50 error ("type %qT is not a base type for type %qT", basetype, type);
471086d6 51 return error_mark_node;
52}
53
54tree
e394ed0f 55binfo_or_else (tree base, tree type)
471086d6 56{
ae260dcc 57 tree binfo = lookup_base (type, base, ba_unique,
58 NULL, tf_warning_or_error);
7e6960e0 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;
471086d6 65}
66
471086d6 67/* According to ARM $7.1.6, "A `const' object may be initialized, but its
2c592404 68 value may not be changed thereafter. */
96624a9e 69
471086d6 70void
91e06df2 71cxx_readonly_error (location_t loc, tree arg, enum lvalue_use errstring)
471086d6 72{
5c892d4a 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
91e06df2 78#define ERROR_FOR_ASSIGNMENT(LOC, AS, ASM, IN, DE, ARG) \
5c892d4a 79 do { \
80 switch (errstring) \
81 { \
a1f90215 82 case lv_assign: \
91e06df2 83 error_at (LOC, AS, ARG); \
5c892d4a 84 break; \
a1f90215 85 case lv_asm: \
91e06df2 86 error_at (LOC, ASM, ARG); \
5c892d4a 87 break; \
a1f90215 88 case lv_increment: \
91e06df2 89 error_at (LOC, IN, ARG); \
5c892d4a 90 break; \
91e06df2 91 case lv_decrement: \
92 error_at (LOC, DE, ARG); \
5c892d4a 93 break; \
94 default: \
95 gcc_unreachable (); \
96 } \
97 } while (0)
471086d6 98
a1f90215 99 /* Handle C++-specific things first. */
100
80a58eb0 101 if (VAR_P (arg)
a1f90215 102 && DECL_LANG_SPECIFIC (arg)
103 && DECL_IN_AGGR_P (arg)
104 && !TREE_STATIC (arg))
91e06df2 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"),
a1f90215 110 arg);
86ccc124 111 else if (INDIRECT_REF_P (arg)
90ad495b 112 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
80a58eb0 113 && (VAR_P (TREE_OPERAND (arg, 0))
653e5405 114 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
91e06df2 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));
938b347c 121 else
91e06df2 122 readonly_error (loc, arg, errstring);
471086d6 123}
d97a7640 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
2ef51f0e 128struct GTY((chain_next ("%h.next"), for_user)) pending_abstract_type {
d97a7640 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
d28993f1 137 /* Kind of use in an unnamed declarator. */
6dc50383 138 enum abstract_class_use use;
d28993f1 139
d97a7640 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
b594087e 148struct abstract_type_hasher : ggc_ptr_hash<pending_abstract_type>
2ef51f0e 149{
150 typedef tree compare_type;
151 static hashval_t hash (pending_abstract_type *);
152 static bool equal (pending_abstract_type *, tree);
153};
d97a7640 154
155/* Compute the hash value of the node VAL. This function is used by the
156 hash table abstract_pending_vars. */
157
2ef51f0e 158hashval_t
159abstract_type_hasher::hash (pending_abstract_type *pat)
d97a7640 160{
d97a7640 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
2ef51f0e 168bool
169abstract_type_hasher::equal (pending_abstract_type *pat1, tree type2)
d97a7640 170{
d97a7640 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. */
2ef51f0e 177static GTY (()) hash_table<abstract_type_hasher> *abstract_pending_vars = NULL;
d97a7640 178
d28993f1 179static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t);
d97a7640 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
186void
187complete_type_check_abstract (tree type)
188{
d97a7640 189 struct pending_abstract_type *pat;
190 location_t cur_loc = input_location;
191
b4df430b 192 gcc_assert (COMPLETE_TYPE_P (type));
d97a7640 193
194 if (!abstract_pending_vars)
195 return;
196
197 /* Retrieve the list of pending declarations for this type. */
2ef51f0e 198 pending_abstract_type **slot
199 = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
200 NO_INSERT);
d97a7640 201 if (!slot)
202 return;
2ef51f0e 203 pat = *slot;
b4df430b 204 gcc_assert (pat);
d97a7640 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
4a44ba29 221 element: it will issue a diagnostic if the type is abstract. */
d97a7640 222 while (pat)
223 {
b4df430b 224 gcc_assert (type == pat->type);
d97a7640 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
3cf8b391 228 IDENTIFIER_NODE. */
d97a7640 229 input_location = pat->locus;
d28993f1 230 abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use,
231 tf_warning_or_error);
d97a7640 232 pat = pat->next;
233 }
234 }
235
2ef51f0e 236 abstract_pending_vars->clear_slot (slot);
d97a7640 237
238 input_location = cur_loc;
239}
240
241
8c18e707 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
d28993f1 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. */
96624a9e 247
d28993f1 248static int
249abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use,
250 tsubst_flags_t complain)
471086d6 251{
f1f41a6c 252 vec<tree, va_gc> *pure;
9031d10b 253
d97a7640 254 /* This function applies only to classes. Any other entity can never
255 be abstract. */
256 if (!CLASS_TYPE_P (type))
257 return 0;
013143e0 258 type = TYPE_MAIN_VARIANT (type);
d97a7640 259
5fc994dd 260#if 0
261 /* Instantiation here seems to be required by the standard,
262 but breaks e.g. boost::bind. FIXME! */
fac5b155 263 /* In SFINAE, non-N3276 context, force instantiation. */
264 if (!(complain & (tf_error|tf_decltype)))
f8f092de 265 complete_type (type);
5fc994dd 266#endif
f8f092de 267
d97a7640 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. */
fac5b155 272 if (!COMPLETE_TYPE_P (type) && (complain & tf_error))
d97a7640 273 {
d97a7640 274 struct pending_abstract_type *pat;
275
694683bb 276 gcc_assert (!decl || DECL_P (decl) || identifier_p (decl));
d97a7640 277
278 if (!abstract_pending_vars)
2ef51f0e 279 abstract_pending_vars
280 = hash_table<abstract_type_hasher>::create_ggc (31);
d97a7640 281
2ef51f0e 282 pending_abstract_type **slot
283 = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
284 INSERT);
d97a7640 285
25a27413 286 pat = ggc_alloc<pending_abstract_type> ();
d97a7640 287 pat->type = type;
288 pat->decl = decl;
d28993f1 289 pat->use = use;
d97a7640 290 pat->locus = ((decl && DECL_P (decl))
291 ? DECL_SOURCE_LOCATION (decl)
292 : input_location);
293
2ef51f0e 294 pat->next = *slot;
d97a7640 295 *slot = pat;
296
297 return 0;
298 }
299
5f30d323 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
03106e7d 305 pure = CLASSTYPE_PURE_VIRTUALS (type);
306 if (!pure)
307 return 0;
308
70cac69d 309 if (!(complain & tf_error))
310 return 1;
311
bc35ef65 312 auto_diagnostic_group d;
471086d6 313 if (decl)
314 {
80a58eb0 315 if (VAR_P (decl))
3cf8b391 316 error ("cannot declare variable %q+D to be of abstract "
317 "type %qT", decl, type);
471086d6 318 else if (TREE_CODE (decl) == PARM_DECL)
d28993f1 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 }
471086d6 327 else if (TREE_CODE (decl) == FIELD_DECL)
3cf8b391 328 error ("cannot declare field %q+D to be of abstract type %qT",
329 decl, type);
471086d6 330 else if (TREE_CODE (decl) == FUNCTION_DECL
331 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
3cf8b391 332 error ("invalid abstract return type for member function %q+#D", decl);
471086d6 333 else if (TREE_CODE (decl) == FUNCTION_DECL)
3cf8b391 334 error ("invalid abstract return type for function %q+#D", decl);
694683bb 335 else if (identifier_p (decl))
3cf8b391 336 /* Here we do not have location information. */
03d6ca9e 337 error ("invalid abstract type %qT for %qE", type, decl);
8837568f 338 else
3cf8b391 339 error ("invalid abstract type for %q+D", decl);
471086d6 340 }
d28993f1 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 }
6588242c 369
471086d6 370 /* Only go through this once. */
f1f41a6c 371 if (pure->length ())
471086d6 372 {
03106e7d 373 unsigned ix;
374 tree fn;
9031d10b 375
712d2297 376 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
377 " because the following virtual functions are pure within %qT:",
378 type);
8837568f 379
f1f41a6c 380 FOR_EACH_VEC_ELT (*pure, ix, fn)
bd97209d 381 if (! DECL_CLONED_FUNCTION_P (fn)
382 || DECL_COMPLETE_DESTRUCTOR_P (fn))
8c41abe8 383 inform (DECL_SOURCE_LOCATION (fn), "\t%#qD", fn);
bd97209d 384
03106e7d 385 /* Now truncate the vector. This leaves it non-null, so we know
653e5405 386 there are pure virtuals, but empty so we don't list them out
387 again. */
f1f41a6c 388 pure->truncate (0);
471086d6 389 }
8c18e707 390
391 return 1;
471086d6 392}
393
d28993f1 394int
395abstract_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
400int
401abstract_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
70cac69d 408/* Wrapper for the above function in the common case of wanting errors. */
409
410int
411abstract_virtuals_error (tree decl, tree type)
412{
413 return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
414}
415
d28993f1 416int
417abstract_virtuals_error (abstract_class_use use, tree type)
418{
419 return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
420}
421
775b9c9f 422/* Print an inform about the declaration of the incomplete type TYPE. */
423
424void
425cxx_incomplete_type_inform (const_tree type)
426{
5444a0b4 427 if (!TYPE_MAIN_DECL (type))
428 return;
429
775b9c9f 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
471086d6 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)
a52d5726 446 and TYPE is the type that was invalid. DIAG_KIND indicates the
447 type of diagnostic (see diagnostic.def). */
471086d6 448
449void
22a3f7bd 450cxx_incomplete_type_diagnostic (location_t loc, const_tree value,
451 const_tree type, diagnostic_t diag_kind)
471086d6 452{
a4d705f9 453 bool is_decl = false, complained = false;
e097fb33 454
a52d5726 455 gcc_assert (diag_kind == DK_WARNING
456 || diag_kind == DK_PEDWARN
457 || diag_kind == DK_ERROR);
9031d10b 458
471086d6 459 /* Avoid duplicate error message. */
460 if (TREE_CODE (type) == ERROR_MARK)
461 return;
462
d582d140 463 if (value)
313d903f 464 {
d582d140 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 }
2e3e31d2 476 retry:
aeef2be5 477 /* We must print an error message. Be clever about what it says. */
478
479 switch (TREE_CODE (type))
471086d6 480 {
aeef2be5 481 case RECORD_TYPE:
482 case UNION_TYPE:
483 case ENUMERAL_TYPE:
a4d705f9 484 if (!is_decl)
d09c3780 485 complained = emit_diagnostic (diag_kind, loc, 0,
a4d705f9 486 "invalid use of incomplete type %q#T",
487 type);
488 if (complained)
775b9c9f 489 cxx_incomplete_type_inform (type);
aeef2be5 490 break;
491
492 case VOID_TYPE:
d09c3780 493 emit_diagnostic (diag_kind, loc, 0,
a52d5726 494 "invalid use of %qT", type);
aeef2be5 495 break;
496
497 case ARRAY_TYPE:
498 if (TYPE_DOMAIN (type))
653e5405 499 {
500 type = TREE_TYPE (type);
501 goto retry;
502 }
d09c3780 503 emit_diagnostic (diag_kind, loc, 0,
a52d5726 504 "invalid use of array with unspecified bounds");
aeef2be5 505 break;
506
507 case OFFSET_TYPE:
508 bad_member:
5f72d880 509 {
126b78a0 510 tree member = TREE_OPERAND (value, 1);
511 if (is_overloaded_fn (member))
512 member = get_first_fn (member);
bc9c1170 513
5f72d880 514 if (DECL_FUNCTION_MEMBER_P (member)
515 && ! flag_ms_extensions)
6639e735 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 }
5f72d880 526 else
d09c3780 527 emit_diagnostic (diag_kind, loc, 0,
442e766c 528 "invalid use of member %qD "
529 "(did you forget the %<&%> ?)", member);
5f72d880 530 }
aeef2be5 531 break;
532
533 case TEMPLATE_TYPE_PARM:
46f4817e 534 if (is_auto (type))
11aaa98e 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 }
46f4817e 543 else
d09c3780 544 emit_diagnostic (diag_kind, loc, 0,
46f4817e 545 "invalid use of template type parameter %qT", type);
8a757045 546 break;
547
548 case BOUND_TEMPLATE_TEMPLATE_PARM:
d09c3780 549 emit_diagnostic (diag_kind, loc, 0,
a52d5726 550 "invalid use of template template parameter %qT",
551 TYPE_NAME (type));
aeef2be5 552 break;
553
3de2e08e 554 case TYPENAME_TYPE:
24011c2c 555 case DECLTYPE_TYPE:
d09c3780 556 emit_diagnostic (diag_kind, loc, 0,
a52d5726 557 "invalid use of dependent type %qT", type);
3de2e08e 558 break;
559
4fdaf896 560 case LANG_TYPE:
54c44343 561 if (type == init_list_type_node)
562 {
d09c3780 563 emit_diagnostic (diag_kind, loc, 0,
54c44343 564 "invalid use of brace-enclosed initializer list");
565 break;
566 }
4fdaf896 567 gcc_assert (type == unknown_type_node);
aeef2be5 568 if (value && TREE_CODE (value) == COMPONENT_REF)
653e5405 569 goto bad_member;
aeef2be5 570 else if (value && TREE_CODE (value) == ADDR_EXPR)
d09c3780 571 emit_diagnostic (diag_kind, loc, 0,
a52d5726 572 "address of overloaded function with no contextual "
573 "type information");
aeef2be5 574 else if (value && TREE_CODE (value) == OVERLOAD)
d09c3780 575 emit_diagnostic (diag_kind, loc, 0,
a52d5726 576 "overloaded function with no contextual type information");
aeef2be5 577 else
d09c3780 578 emit_diagnostic (diag_kind, loc, 0,
a52d5726 579 "insufficient contextual information to determine type");
aeef2be5 580 break;
9031d10b 581
aeef2be5 582 default:
2e3e31d2 583 gcc_unreachable ();
471086d6 584 }
585}
586
22a3f7bd 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
e097fb33 591void
22a3f7bd 592cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type)
e097fb33 593{
22a3f7bd 594 cxx_incomplete_type_diagnostic (loc, value, type, DK_ERROR);
e097fb33 595}
596
471086d6 597\f
3afe9b43 598/* The recursive part of split_nonconstant_init. DEST is an lvalue
927b65fb 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. */
3afe9b43 602
927b65fb 603static bool
604split_nonconstant_init_1 (tree dest, tree init)
3afe9b43 605{
c75b4594 606 unsigned HOST_WIDE_INT idx;
607 tree field_index, value;
608 tree type = TREE_TYPE (dest);
609 tree inner_type = NULL;
3afe9b43 610 bool array_type_p = false;
927b65fb 611 bool complete_p = true;
612 HOST_WIDE_INT num_split_elts = 0;
3afe9b43 613
3afe9b43 614 switch (TREE_CODE (type))
615 {
616 case ARRAY_TYPE:
617 inner_type = TREE_TYPE (type);
618 array_type_p = true;
39786c80 619 if ((TREE_SIDE_EFFECTS (init)
620 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
0b8d327e 621 || vla_type_p (type))
39786c80 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 }
3afe9b43 630 /* FALLTHRU */
631
632 case RECORD_TYPE:
633 case UNION_TYPE:
634 case QUAL_UNION_TYPE:
c75b4594 635 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
636 field_index, value)
3afe9b43 637 {
c75b4594 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);
3afe9b43 642
643 if (!array_type_p)
644 inner_type = TREE_TYPE (field_index);
645
646 if (TREE_CODE (value) == CONSTRUCTOR)
647 {
c75b4594 648 tree sub;
649
3afe9b43 650 if (array_type_p)
653e5405 651 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
831d52a2 652 NULL_TREE, NULL_TREE);
3afe9b43 653 else
653e5405 654 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
831d52a2 655 NULL_TREE);
3afe9b43 656
927b65fb 657 if (!split_nonconstant_init_1 (sub, value))
658 complete_p = false;
d552a498 659 else
660 CONSTRUCTOR_ELTS (init)->ordered_remove (idx--);
927b65fb 661 num_split_elts++;
3afe9b43 662 }
663 else if (!initializer_constant_valid_p (value, inner_type))
664 {
c75b4594 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. */
f1f41a6c 675 CONSTRUCTOR_ELTS (init)->ordered_remove (idx);
c75b4594 676 --idx;
3afe9b43 677
7e173c71 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);
e5827c8e 690 if (tree_fits_shwi_p (max))
691 num_split_elts += tree_to_shwi (max);
7e173c71 692 }
3afe9b43 693 else
b3c93f85 694 {
7e173c71 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);
9b89ee44 706 if (tree cleanup
707 = cxx_maybe_build_cleanup (sub, tf_warning_or_error))
708 finish_eh_cleanup (cleanup);
b3c93f85 709 }
38c55c8a 710
927b65fb 711 num_split_elts++;
3afe9b43 712 }
3afe9b43 713 }
714 break;
715
716 case VECTOR_TYPE:
717 if (!initializer_constant_valid_p (init, type))
718 {
c75b4594 719 tree code;
4634db76 720 tree cons = copy_node (init);
3afe9b43 721 CONSTRUCTOR_ELTS (init) = NULL;
4634db76 722 code = build2 (MODIFY_EXPR, type, dest, cons);
e60a6f7b 723 code = build_stmt (input_location, EXPR_STMT, code);
2363ef00 724 add_stmt (code);
927b65fb 725 num_split_elts += CONSTRUCTOR_NELTS (init);
3afe9b43 726 }
727 break;
728
729 default:
2e3e31d2 730 gcc_unreachable ();
3afe9b43 731 }
52acb923 732
733 /* The rest of the initializer is now a constant. */
734 TREE_CONSTANT (init) = 1;
96fa24ad 735
736 /* We didn't split out anything. */
737 if (num_split_elts == 0)
738 return false;
739
927b65fb 740 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
741 num_split_elts, inner_type);
3afe9b43 742}
743
9031d10b 744/* A subroutine of store_init_value. Splits non-constant static
3afe9b43 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
39786c80 749tree
3afe9b43 750split_nonconstant_init (tree dest, tree init)
751{
752 tree code;
753
39786c80 754 if (TREE_CODE (init) == TARGET_EXPR)
755 init = TARGET_EXPR_INITIAL (init);
3afe9b43 756 if (TREE_CODE (init) == CONSTRUCTOR)
757 {
d560f985 758 init = cp_fully_fold_init (init);
2363ef00 759 code = push_stmt_list ();
927b65fb 760 if (split_nonconstant_init_1 (dest, init))
761 init = NULL_TREE;
2363ef00 762 code = pop_stmt_list (code);
3afe9b43 763 DECL_INITIAL (dest) = init;
09742c66 764 TREE_READONLY (dest) = 0;
3afe9b43 765 }
c571b0c6 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);
3afe9b43 770 else
831d52a2 771 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
3afe9b43 772
773 return code;
774}
775
471086d6 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
3afe9b43 791 Returns code to be executed if initialization could not be performed
792 for static variable. In that case, caller must emit the code. */
471086d6 793
794tree
f1f41a6c 795store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
471086d6 796{
cd16867a 797 tree value, type;
471086d6 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
95397ff9 805 if (MAYBE_CLASS_TYPE_P (type))
471086d6 806 {
771665d8 807 if (TREE_CODE (init) == TREE_LIST)
471086d6 808 {
03d6ca9e 809 error ("constructor syntax used, but no constructor declared "
653e5405 810 "for type %qT", type);
f82f1250 811 init = build_constructor_from_list (init_list_type_node, nreverse (init));
471086d6 812 }
471086d6 813 }
471086d6 814
815 /* End of special C++ code. */
816
ce984e5e 817 if (flags & LOOKUP_ALREADY_DIGESTED)
818 value = init;
819 else
820 /* Digest the specified initializer into an expression. */
4b373fc1 821 value = digest_init_flags (type, init, flags, tf_warning_or_error);
ce984e5e 822
d839099f 823 if (TREE_CODE (type) == ARRAY_TYPE
824 && TYPE_STRING_FLAG (TREE_TYPE (type))
825 && TREE_CODE (value) == CONSTRUCTOR)
826 value = braced_list_to_string (type, value);
827
c7b89256 828 value = extend_ref_init_temps (decl, value, cleanups);
829
a7e27879 830 /* In C++11 constant expression is a semantic, not syntactic, property.
ce984e5e 831 In C++98, make sure that what we thought was a constant expression at
1fff8e07 832 template definition time is still constant and otherwise perform this
833 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
834 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
ce984e5e 835 {
836 bool const_init;
006b503a 837 tree oldval = value;
7dffa847 838 value = fold_non_dependent_expr (value);
cd50b62d 839 if (DECL_DECLARED_CONSTEXPR_P (decl)
bd20946f 840 || (DECL_IN_AGGR_P (decl)
841 && DECL_INITIALIZED_IN_CLASS_P (decl)
842 && !DECL_VAR_DECLARED_INLINE_P (decl)))
03cbe7c9 843 {
bd20946f 844 /* Diagnose a non-constant initializer for constexpr variable or
845 non-inline in-class-initialized static data member. */
7e1f8be4 846 if (!require_constant_expression (value))
03cbe7c9 847 value = error_mark_node;
848 else
0c703e14 849 value = cxx_constant_init (value, decl);
03cbe7c9 850 }
0c703e14 851 else
18d371d3 852 value = maybe_constant_init (value, decl, true);
bbcc9042 853 if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
854 /* Poison this CONSTRUCTOR so it can't be copied to another
855 constexpr variable. */
856 CONSTRUCTOR_MUTABLE_POISON (value) = true;
ce984e5e 857 const_init = (reduced_constant_expression_p (value)
858 || error_operand_p (value));
859 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
39682fd5 860 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
90ad495b 861 if (!TYPE_REF_P (type))
39682fd5 862 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
006b503a 863 if (!const_init)
864 value = oldval;
ce984e5e 865 }
d560f985 866 value = cp_fully_fold_init (value);
ce984e5e 867
ffc5ad9b 868 /* Handle aggregate NSDMI in non-constant initializers, too. */
869 value = replace_placeholders (value, decl);
cf72f34d 870
f553d9f8 871 /* DECL may change value; purge caches. */
872 clear_cv_and_fold_caches ();
873
07969d83 874 /* If the initializer is not a constant, fill in DECL_INITIAL with
875 the bits that are constant, and then return an expression that
876 will perform the dynamic initialization. */
877 if (value != error_mark_node
878 && (TREE_SIDE_EFFECTS (value)
0b8d327e 879 || vla_type_p (type)
88af6243 880 || ! reduced_constant_expression_p (value)))
39786c80 881 return split_nonconstant_init (decl, value);
07969d83 882 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
883 is an automatic variable, the middle end will turn this into a
884 dynamic initialization later. */
471086d6 885 DECL_INITIAL (decl) = value;
886 return NULL_TREE;
887}
e63bd8ae 888
471086d6 889\f
ee6d34bb 890/* Give diagnostic about narrowing conversions within { }, or as part of
891 a converted constant expression. If CONST_ONLY, only check
892 constants. */
f82f1250 893
718affe2 894bool
ee6d34bb 895check_narrowing (tree type, tree init, tsubst_flags_t complain, bool const_only)
f82f1250 896{
ca01d9d9 897 tree ftype = unlowered_expr_type (init);
f82f1250 898 bool ok = true;
899 REAL_VALUE_TYPE d;
900
718affe2 901 if (((!warn_narrowing || !(complain & tf_warning))
902 && cxx_dialect == cxx98)
ee6d34bb 903 || !ARITHMETIC_TYPE_P (type)
904 /* Don't emit bogus warnings with e.g. value-dependent trees. */
905 || instantiation_dependent_expression_p (init))
718affe2 906 return ok;
2732b026 907
581edfc4 908 if (BRACE_ENCLOSED_INITIALIZER_P (init)
909 && TREE_CODE (type) == COMPLEX_TYPE)
910 {
911 tree elttype = TREE_TYPE (type);
a2704374 912 if (CONSTRUCTOR_NELTS (init) > 0)
718affe2 913 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
914 complain);
581edfc4 915 if (CONSTRUCTOR_NELTS (init) > 1)
718affe2 916 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
917 complain);
918 return ok;
581edfc4 919 }
920
ee6d34bb 921 init = maybe_constant_value (init);
922
923 /* If we were asked to only check constants, return early. */
924 if (const_only && !TREE_CONSTANT (init))
925 return ok;
f82f1250 926
df6bc621 927 if (CP_INTEGRAL_TYPE_P (type)
f82f1250 928 && TREE_CODE (ftype) == REAL_TYPE)
929 ok = false;
930 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
931 && CP_INTEGRAL_TYPE_P (type))
932 {
89269893 933 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
934 /* Check for narrowing based on the values of the enumeration. */
935 ftype = ENUM_UNDERLYING_TYPE (ftype);
b0337f5c 936 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
937 TYPE_MAX_VALUE (ftype))
938 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
939 TYPE_MIN_VALUE (type)))
f82f1250 940 && (TREE_CODE (init) != INTEGER_CST
941 || !int_fits_type_p (init, type)))
942 ok = false;
943 }
944 else if (TREE_CODE (ftype) == REAL_TYPE
945 && TREE_CODE (type) == REAL_TYPE)
946 {
947 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
948 {
f82f1250 949 if (TREE_CODE (init) == REAL_CST)
950 {
eed2e6c3 951 /* Issue 703: Loss of precision is OK as long as the value is
952 within the representable range of the new type. */
953 REAL_VALUE_TYPE r;
f82f1250 954 d = TREE_REAL_CST (init);
eed2e6c3 955 real_convert (&r, TYPE_MODE (type), &d);
956 if (real_isinf (&r))
957 ok = false;
f82f1250 958 }
eed2e6c3 959 else
960 ok = false;
f82f1250 961 }
962 }
963 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
964 && TREE_CODE (type) == REAL_TYPE)
965 {
966 ok = false;
967 if (TREE_CODE (init) == INTEGER_CST)
968 {
969 d = real_value_from_int_cst (0, init);
970 if (exact_real_truncate (TYPE_MODE (type), &d))
971 ok = true;
972 }
973 }
974
d2c63826 975 bool almost_ok = ok;
976 if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error))
977 {
978 tree folded = cp_fully_fold (init);
979 if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none))
980 almost_ok = true;
981 }
982
f82f1250 983 if (!ok)
7e783eb3 984 {
d3a3cfb8 985 location_t loc = cp_expr_loc_or_loc (init, input_location);
718affe2 986 if (cxx_dialect == cxx98)
d2c63826 987 {
988 if (complain & tf_warning)
989 warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
ee6d34bb 990 "from %qH to %qI is ill-formed in C++11",
d2c63826 991 init, ftype, type);
992 ok = true;
993 }
994 else if (!CONSTANT_CLASS_P (init))
718affe2 995 {
996 if (complain & tf_warning_or_error)
997 {
bc35ef65 998 auto_diagnostic_group d;
22d09025 999 if ((!almost_ok || pedantic)
1000 && pedwarn (loc, OPT_Wnarrowing,
ee6d34bb 1001 "narrowing conversion of %qE from %qH to %qI",
22d09025 1002 init, ftype, type)
1003 && almost_ok)
d2c63826 1004 inform (loc, " the expression has a constant value but is not "
1005 "a C++ constant-expression");
718affe2 1006 ok = true;
1007 }
1008 }
1009 else if (complain & tf_error)
e36ce2a1 1010 {
eba4cf96 1011 int savederrorcount = errorcount;
e36ce2a1 1012 global_dc->pedantic_errors = 1;
d2c63826 1013 pedwarn (loc, OPT_Wnarrowing,
ee6d34bb 1014 "narrowing conversion of %qE from %qH to %qI ",
1015 init, ftype, type);
eba4cf96 1016 if (errorcount == savederrorcount)
d3a3b710 1017 ok = true;
e36ce2a1 1018 global_dc->pedantic_errors = flag_pedantic_errors;
1019 }
7e783eb3 1020 }
718affe2 1021
d2c63826 1022 return ok;
f82f1250 1023}
1024
c75b4594 1025/* Process the initializer INIT for a variable of type TYPE, emitting
1026 diagnostics for invalid initializers and converting the initializer as
1027 appropriate.
471086d6 1028
c75b4594 1029 For aggregate types, it assumes that reshape_init has already run, thus the
f82f1250 1030 initializer will have the right shape (brace elision has been undone).
471086d6 1031
7aac6ee6 1032 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1033 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
f82f1250 1034
1035static tree
7aac6ee6 1036digest_init_r (tree type, tree init, int nested, int flags,
b52bd4ae 1037 tsubst_flags_t complain)
471086d6 1038{
1039 enum tree_code code = TREE_CODE (type);
471086d6 1040
247f7e36 1041 if (error_operand_p (init))
471086d6 1042 return error_mark_node;
1043
c75b4594 1044 gcc_assert (init);
581af848 1045
1046 /* We must strip the outermost array type when completing the type,
1047 because the its bounds might be incomplete at the moment. */
7aac6ee6 1048 if (!complete_type_or_maybe_complain (code == ARRAY_TYPE
6500d307 1049 ? TREE_TYPE (type) : type, NULL_TREE,
1050 complain))
581af848 1051 return error_mark_node;
9031d10b 1052
9ab4658b 1053 location_t loc = cp_expr_loc_or_loc (init, input_location);
1054
1055 tree stripped_init = init;
1056
c75b4594 1057 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1058 (g++.old-deja/g++.law/casts2.C). */
471086d6 1059 if (TREE_CODE (init) == NON_LVALUE_EXPR)
9ab4658b 1060 stripped_init = TREE_OPERAND (init, 0);
035778de 1061
9ab4658b 1062 stripped_init = tree_strip_any_location_wrapper (stripped_init);
d582d140 1063
c75b4594 1064 /* Initialization of an array of chars from a string constant. The initializer
1065 can be optionally enclosed in braces, but reshape_init has already removed
1066 them if they were present. */
471086d6 1067 if (code == ARRAY_TYPE)
1068 {
6b94e133 1069 if (nested && !TYPE_DOMAIN (type))
7aac6ee6 1070 /* C++ flexible array members have a null domain. */
1071 pedwarn (loc, OPT_Wpedantic,
1072 "initialization of a flexible array member");
6b94e133 1073
c75b4594 1074 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
e67164f5 1075 if (char_type_p (typ1)
c75b4594 1076 /*&& init */
d582d140 1077 && TREE_CODE (stripped_init) == STRING_CST)
471086d6 1078 {
c75b4594 1079 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
471086d6 1080
b0726616 1081 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
471086d6 1082 {
04a73995 1083 if (char_type != char_type_node
1084 && char_type != signed_char_type_node
1085 && char_type != unsigned_char_type_node)
b0726616 1086 {
b52bd4ae 1087 if (complain & tf_error)
035778de 1088 error_at (loc, "char-array initialized from wide string");
b0726616 1089 return error_mark_node;
1090 }
471086d6 1091 }
b0726616 1092 else
471086d6 1093 {
04a73995 1094 if (char_type == char_type_node
1095 || char_type == signed_char_type_node
1096 || char_type == unsigned_char_type_node)
b0726616 1097 {
b52bd4ae 1098 if (complain & tf_error)
035778de 1099 error_at (loc,
1100 "int-array initialized from non-wide string");
b0726616 1101 return error_mark_node;
1102 }
1103 else if (char_type != typ1)
1104 {
b52bd4ae 1105 if (complain & tf_error)
035778de 1106 error_at (loc, "int-array initialized from incompatible "
1107 "wide string");
b0726616 1108 return error_mark_node;
1109 }
471086d6 1110 }
1111
7aac6ee6 1112 if (nested == 2 && !TYPE_DOMAIN (type))
1113 {
1114 if (complain & tf_error)
1115 error_at (loc, "initialization of flexible array member "
1116 "in a nested context");
1117 return error_mark_node;
1118 }
1119
c571b0c6 1120 if (type != TREE_TYPE (init)
1121 && !variably_modified_type_p (type, NULL_TREE))
bd46a023 1122 {
1123 init = copy_node (init);
1124 TREE_TYPE (init) = type;
d582d140 1125 /* If we have a location wrapper, then also copy the wrapped
1126 node, and update the copy's type. */
1127 if (location_wrapper_p (init))
1128 {
1129 stripped_init = copy_node (stripped_init);
1130 TREE_OPERAND (init, 0) = stripped_init;
1131 TREE_TYPE (stripped_init) = type;
1132 }
bd46a023 1133 }
6b94e133 1134 if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type)))
471086d6 1135 {
73d282c6 1136 /* Not a flexible array member. */
f9ae6f95 1137 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
471086d6 1138 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1139 /* In C it is ok to subtract 1 from the length of the string
1140 because it's ok to ignore the terminating null char that is
1141 counted in the length of the constant, but in C++ this would
1142 be invalid. */
d582d140 1143 if (size < TREE_STRING_LENGTH (stripped_init))
35f9aa56 1144 {
1145 permerror (loc, "initializer-string for array "
1146 "of chars is too long");
1147
d582d140 1148 init = build_string (size,
1149 TREE_STRING_POINTER (stripped_init));
35f9aa56 1150 TREE_TYPE (init) = type;
1151 }
471086d6 1152 }
c75b4594 1153 return init;
471086d6 1154 }
1155 }
1156
2d42b7d7 1157 /* Handle scalar types (including conversions) and references. */
d582d140 1158 if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
04791a75 1159 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1b49e7ef 1160 {
7e783eb3 1161 if (nested)
c4991e3e 1162 flags |= LOOKUP_NO_NARROWING;
cf7aa2e5 1163 init = convert_for_initialization (0, type, init, flags,
183407ee 1164 ICR_INIT, NULL_TREE, 0,
b52bd4ae 1165 complain);
1b49e7ef 1166
1167 return init;
1168 }
c75b4594 1169
1170 /* Come here only for aggregates: records, arrays, unions, complex numbers
1171 and vectors. */
7aac6ee6 1172 gcc_assert (code == ARRAY_TYPE
76249021 1173 || VECTOR_TYPE_P (type)
7aac6ee6 1174 || code == RECORD_TYPE
1175 || code == UNION_TYPE
1176 || code == COMPLEX_TYPE);
c75b4594 1177
3dd2833b 1178 /* "If T is a class type and the initializer list has a single
1179 element of type cv U, where U is T or a class derived from T,
1180 the object is initialized from that element." */
5e8689fb 1181 if (flag_checking
1182 && cxx_dialect >= cxx11
d582d140 1183 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1184 && CONSTRUCTOR_NELTS (stripped_init) == 1
671608f2 1185 && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
1186 || VECTOR_TYPE_P (type)))
3dd2833b 1187 {
d582d140 1188 tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value;
3dd2833b 1189 if (reference_related_p (type, TREE_TYPE (elt)))
a7023863 1190 /* We should have fixed this in reshape_init. */
1191 gcc_unreachable ();
3dd2833b 1192 }
1193
d582d140 1194 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1ee7f2fb 1195 && !TYPE_NON_AGGREGATE_CLASS (type))
d582d140 1196 return process_init_constructor (type, stripped_init, nested, complain);
c75b4594 1197 else
471086d6 1198 {
d582d140 1199 if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE)
471086d6 1200 {
b52bd4ae 1201 if (complain & tf_error)
035778de 1202 error_at (loc, "cannot initialize aggregate of type %qT with "
1203 "a compound literal", type);
471086d6 1204
c75b4594 1205 return error_mark_node;
1206 }
f8d96752 1207
7aac6ee6 1208 if (code == ARRAY_TYPE
d582d140 1209 && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
f8d96752 1210 {
29ada811 1211 /* Allow the result of build_array_copy and of
1212 build_value_init_noctor. */
d582d140 1213 if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR
1214 || TREE_CODE (stripped_init) == CONSTRUCTOR)
a8b75081 1215 && (same_type_ignoring_top_level_qualifiers_p
1216 (type, TREE_TYPE (init))))
1217 return init;
1218
b52bd4ae 1219 if (complain & tf_error)
035778de 1220 error_at (loc, "array must be initialized with a brace-enclosed"
1221 " initializer");
f8d96752 1222 return error_mark_node;
1223 }
1224
c75b4594 1225 return convert_for_initialization (NULL_TREE, type, init,
cf7aa2e5 1226 flags,
183407ee 1227 ICR_INIT, NULL_TREE, 0,
b52bd4ae 1228 complain);
471086d6 1229 }
c75b4594 1230}
1231
f82f1250 1232tree
b52bd4ae 1233digest_init (tree type, tree init, tsubst_flags_t complain)
f82f1250 1234{
7aac6ee6 1235 return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain);
cf7aa2e5 1236}
1237
1238tree
4b373fc1 1239digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
cf7aa2e5 1240{
7aac6ee6 1241 return digest_init_r (type, init, 0, flags, complain);
f82f1250 1242}
089c9c49 1243
1244/* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1245tree
b9e17a4a 1246digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
089c9c49 1247{
1248 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1249
34569785 1250 tree type = TREE_TYPE (decl);
089c9c49 1251 int flags = LOOKUP_IMPLICIT;
1252 if (DIRECT_LIST_INIT_P (init))
1253 flags = LOOKUP_NORMAL;
34569785 1254 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1255 && CP_AGGREGATE_TYPE_P (type))
b9e17a4a 1256 init = reshape_init (type, init, complain);
1257 init = digest_init_flags (type, init, flags, complain);
089c9c49 1258 if (TREE_CODE (init) == TARGET_EXPR)
1259 /* This represents the whole initialization. */
1260 TARGET_EXPR_DIRECT_INIT_P (init) = true;
1261 return init;
1262}
c75b4594 1263\f
1264/* Set of flags used within process_init_constructor to describe the
1265 initializers. */
1266#define PICFLAG_ERRONEOUS 1
1267#define PICFLAG_NOT_ALL_CONSTANT 2
1268#define PICFLAG_NOT_ALL_SIMPLE 4
48f76477 1269#define PICFLAG_SIDE_EFFECTS 8
c75b4594 1270
1271/* Given an initializer INIT, return the flag (PICFLAG_*) which better
1272 describe it. */
1273
1274static int
1275picflag_from_initializer (tree init)
1276{
1277 if (init == error_mark_node)
1278 return PICFLAG_ERRONEOUS;
1279 else if (!TREE_CONSTANT (init))
48f76477 1280 {
1281 if (TREE_SIDE_EFFECTS (init))
1282 return PICFLAG_SIDE_EFFECTS;
1283 else
1284 return PICFLAG_NOT_ALL_CONSTANT;
1285 }
c75b4594 1286 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1287 return PICFLAG_NOT_ALL_SIMPLE;
1288 return 0;
1289}
471086d6 1290
7e173c71 1291/* Adjust INIT for going into a CONSTRUCTOR. */
1292
1293static tree
7aac6ee6 1294massage_init_elt (tree type, tree init, int nested, tsubst_flags_t complain)
7e173c71 1295{
7aac6ee6 1296 init = digest_init_r (type, init, nested ? 2 : 1, LOOKUP_IMPLICIT, complain);
7e173c71 1297 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1d56a349 1298 if (SIMPLE_TARGET_EXPR_P (init))
7e173c71 1299 init = TARGET_EXPR_INITIAL (init);
1300 /* When we defer constant folding within a statement, we may want to
1301 defer this folding as well. */
006b503a 1302 tree t = fold_non_dependent_expr (init, complain);
1d911add 1303 t = maybe_constant_init (t);
0e65aeb6 1304 if (TREE_CONSTANT (t))
1305 init = t;
7e173c71 1306 return init;
1307}
1308
c75b4594 1309/* Subroutine of process_init_constructor, which will process an initializer
45cc50c5 1310 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1311 which describe the initializers. */
471086d6 1312
c75b4594 1313static int
7aac6ee6 1314process_init_constructor_array (tree type, tree init, int nested,
b52bd4ae 1315 tsubst_flags_t complain)
c75b4594 1316{
1317 unsigned HOST_WIDE_INT i, len = 0;
1318 int flags = 0;
1319 bool unbounded = false;
1320 constructor_elt *ce;
f1f41a6c 1321 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
c75b4594 1322
1323 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
76249021 1324 || VECTOR_TYPE_P (type));
c75b4594 1325
1326 if (TREE_CODE (type) == ARRAY_TYPE)
471086d6 1327 {
6b94e133 1328 /* C++ flexible array members have a null domain. */
c75b4594 1329 tree domain = TYPE_DOMAIN (type);
6b94e133 1330 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1331 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1332 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
796b6678 1333 TYPE_PRECISION (TREE_TYPE (domain)),
1334 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
c75b4594 1335 else
1336 unbounded = true; /* Take as many as there are. */
7aac6ee6 1337
1338 if (nested == 2 && !domain && !vec_safe_is_empty (v))
1339 {
1340 if (complain & tf_error)
d3a3cfb8 1341 error_at (cp_expr_loc_or_loc (init, input_location),
7aac6ee6 1342 "initialization of flexible array member "
1343 "in a nested context");
1344 return PICFLAG_ERRONEOUS;
1345 }
471086d6 1346 }
c75b4594 1347 else
1348 /* Vectors are like simple fixed-size arrays. */
f08ee65f 1349 unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len);
471086d6 1350
a8fe6bf4 1351 /* There must not be more initializers than needed. */
f1f41a6c 1352 if (!unbounded && vec_safe_length (v) > len)
b52bd4ae 1353 {
1354 if (complain & tf_error)
1355 error ("too many initializers for %qT", type);
1356 else
1357 return PICFLAG_ERRONEOUS;
1358 }
c75b4594 1359
f1f41a6c 1360 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
471086d6 1361 {
caf629d3 1362 if (!ce->index)
c75b4594 1363 ce->index = size_int (i);
caf629d3 1364 else if (!check_array_designated_initializer (ce, i))
1365 ce->index = error_mark_node;
c75b4594 1366 gcc_assert (ce->value);
7aac6ee6 1367 ce->value
1368 = massage_init_elt (TREE_TYPE (type), ce->value, nested, complain);
48ec80c8 1369
ee1f7409 1370 gcc_checking_assert
1371 (ce->value == error_mark_node
1372 || (same_type_ignoring_top_level_qualifiers_p
1373 (strip_array_types (TREE_TYPE (type)),
1374 strip_array_types (TREE_TYPE (ce->value)))));
2133b374 1375
c75b4594 1376 flags |= picflag_from_initializer (ce->value);
471086d6 1377 }
1378
c75b4594 1379 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1380 we must add initializers ourselves. */
1381 if (!unbounded)
1382 for (; i < len; ++i)
1383 {
1384 tree next;
1385
883e1020 1386 if (type_build_ctor_call (TREE_TYPE (type)))
c75b4594 1387 {
1388 /* If this type needs constructors run for default-initialization,
8feda8f8 1389 we can't rely on the back end to do it for us, so make the
79f1f232 1390 initialization explicit by list-initializing from T{}. */
9705da72 1391 next = build_constructor (init_list_type_node, NULL);
7aac6ee6 1392 next = massage_init_elt (TREE_TYPE (type), next, nested, complain);
8feda8f8 1393 if (initializer_zerop (next))
1394 /* The default zero-initialization is fine for us; don't
1395 add anything to the CONSTRUCTOR. */
1396 next = NULL_TREE;
c75b4594 1397 }
1398 else if (!zero_init_p (TREE_TYPE (type)))
1399 next = build_zero_init (TREE_TYPE (type),
1400 /*nelts=*/NULL_TREE,
1401 /*static_storage_p=*/false);
1402 else
1403 /* The default zero-initialization is fine for us; don't
1404 add anything to the CONSTRUCTOR. */
8feda8f8 1405 next = NULL_TREE;
c75b4594 1406
8feda8f8 1407 if (next)
1408 {
1409 flags |= picflag_from_initializer (next);
cf755750 1410 if (len > i+1
1411 && (initializer_constant_valid_p (next, TREE_TYPE (next))
1412 == null_pointer_node))
1413 {
1414 tree range = build2 (RANGE_EXPR, size_type_node,
1415 build_int_cst (size_type_node, i),
1416 build_int_cst (size_type_node, len - 1));
1417 CONSTRUCTOR_APPEND_ELT (v, range, next);
1418 break;
1419 }
1420 else
1421 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
8feda8f8 1422 }
cf755750 1423 else
1424 /* Don't bother checking all the other elements. */
1425 break;
c75b4594 1426 }
1427
1428 CONSTRUCTOR_ELTS (init) = v;
1429 return flags;
471086d6 1430}
471086d6 1431
c75b4594 1432/* Subroutine of process_init_constructor, which will process an initializer
1433 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1434 the initializers. */
471086d6 1435
c75b4594 1436static int
7aac6ee6 1437process_init_constructor_record (tree type, tree init, int nested,
b52bd4ae 1438 tsubst_flags_t complain)
471086d6 1439{
f1f41a6c 1440 vec<constructor_elt, va_gc> *v = NULL;
c75b4594 1441 tree field;
cf72f34d 1442 int skipped = 0;
c75b4594 1443
1444 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1445 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1446 gcc_assert (!TYPE_BINFO (type)
40e2decb 1447 || cxx_dialect >= cxx17
c75b4594 1448 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1449 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1450
cf72f34d 1451 restart:
1452 int flags = 0;
1453 unsigned HOST_WIDE_INT idx = 0;
8e79b5ff 1454 int designator_skip = -1;
c75b4594 1455 /* Generally, we will always have an index for each initializer (which is
1456 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1457 reshape_init. So we need to handle both cases. */
1767a056 1458 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
471086d6 1459 {
c75b4594 1460 tree next;
ca01d9d9 1461 tree type;
471086d6 1462
a83affb5 1463 if (TREE_CODE (field) != FIELD_DECL
1464 || (DECL_ARTIFICIAL (field)
40e2decb 1465 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))
c75b4594 1466 continue;
1467
3cde3c29 1468 if (DECL_UNNAMED_BIT_FIELD (field))
1469 continue;
1470
ca01d9d9 1471 /* If this is a bitfield, first convert to the declared type. */
1472 type = TREE_TYPE (field);
1473 if (DECL_BIT_FIELD_TYPE (field))
1474 type = DECL_BIT_FIELD_TYPE (field);
53b567c1 1475 if (type == error_mark_node)
1476 return PICFLAG_ERRONEOUS;
ca01d9d9 1477
8e79b5ff 1478 next = NULL_TREE;
8698843f 1479 if (idx < CONSTRUCTOR_NELTS (init))
471086d6 1480 {
f1f41a6c 1481 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
c75b4594 1482 if (ce->index)
471086d6 1483 {
c75b4594 1484 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1485 latter case can happen in templates where lookup has to be
1486 deferred. */
1487 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
694683bb 1488 || identifier_p (ce->index));
8e79b5ff 1489 if (ce->index == field || ce->index == DECL_NAME (field))
1490 next = ce->value;
1491 else if (ANON_AGGR_TYPE_P (type)
1492 && search_anon_aggr (type,
1493 TREE_CODE (ce->index) == FIELD_DECL
1494 ? DECL_NAME (ce->index)
1495 : ce->index))
1496 /* If the element is an anonymous union object and the
1497 initializer list is a designated-initializer-list, the
1498 anonymous union object is initialized by the
1499 designated-initializer-list { D }, where D is the
1500 designated-initializer-clause naming a member of the
1501 anonymous union object. */
91fdd720 1502 next = build_constructor_single (init_list_type_node,
1503 ce->index, ce->value);
8e79b5ff 1504 else
b81452c9 1505 {
8e79b5ff 1506 ce = NULL;
1507 if (designator_skip == -1)
1508 designator_skip = 1;
b81452c9 1509 }
471086d6 1510 }
8e79b5ff 1511 else
1512 {
1513 designator_skip = 0;
1514 next = ce->value;
1515 }
72ba5dfe 1516
8e79b5ff 1517 if (ce)
1518 {
1519 gcc_assert (ce->value);
7aac6ee6 1520 next = massage_init_elt (type, next, nested, complain);
8e79b5ff 1521 ++idx;
1522 }
c75b4594 1523 }
8e79b5ff 1524 if (next)
1525 /* Already handled above. */;
cf72f34d 1526 else if (DECL_INITIAL (field))
1527 {
1528 if (skipped > 0)
1529 {
1530 /* We're using an NSDMI past a field with implicit
1531 zero-init. Go back and make it explicit. */
1532 skipped = -1;
1533 vec_safe_truncate (v, 0);
1534 goto restart;
1535 }
1536 /* C++14 aggregate NSDMI. */
b9e17a4a 1537 next = get_nsdmi (field, /*ctor*/false, complain);
7604a798 1538 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1539 && find_placeholders (next))
1540 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
cf72f34d 1541 }
883e1020 1542 else if (type_build_ctor_call (TREE_TYPE (field)))
c75b4594 1543 {
1544 /* If this type needs constructors run for
a17c2a3a 1545 default-initialization, we can't rely on the back end to do it
c75b4594 1546 for us, so build up TARGET_EXPRs. If the type in question is
1547 a class, just build one up; if it's an array, recurse. */
dbfcf378 1548 next = build_constructor (init_list_type_node, NULL);
7aac6ee6 1549 next = massage_init_elt (TREE_TYPE (field), next, nested, complain);
c75b4594 1550
1551 /* Warn when some struct elements are implicitly initialized. */
639d66a3 1552 if ((complain & tf_warning)
1553 && !EMPTY_CONSTRUCTOR_P (init))
af71319a 1554 warning (OPT_Wmissing_field_initializers,
1555 "missing initializer for member %qD", field);
c75b4594 1556 }
1557 else
1558 {
73d282c6 1559 const_tree fldtype = TREE_TYPE (field);
90ad495b 1560 if (TYPE_REF_P (fldtype))
b52bd4ae 1561 {
1562 if (complain & tf_error)
af71319a 1563 error ("member %qD is uninitialized reference", field);
b52bd4ae 1564 else
1565 return PICFLAG_ERRONEOUS;
1566 }
73d282c6 1567 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype))
b52bd4ae 1568 {
1569 if (complain & tf_error)
af71319a 1570 error ("member %qD with uninitialized reference fields", field);
b52bd4ae 1571 else
1572 return PICFLAG_ERRONEOUS;
1573 }
c75b4594 1574
1575 /* Warn when some struct elements are implicitly initialized
73d282c6 1576 to zero. However, avoid issuing the warning for flexible
1577 array members since they need not have any elements. */
6b94e133 1578 if ((TREE_CODE (fldtype) != ARRAY_TYPE || TYPE_DOMAIN (fldtype))
73d282c6 1579 && (complain & tf_warning)
639d66a3 1580 && !EMPTY_CONSTRUCTOR_P (init))
af71319a 1581 warning (OPT_Wmissing_field_initializers,
1582 "missing initializer for member %qD", field);
c75b4594 1583
73d282c6 1584 if (!zero_init_p (fldtype)
cf72f34d 1585 || skipped < 0)
c75b4594 1586 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1587 /*static_storage_p=*/false);
72ba5dfe 1588 else
cf72f34d 1589 {
1590 /* The default zero-initialization is fine for us; don't
1591 add anything to the CONSTRUCTOR. */
1592 skipped = 1;
1593 continue;
1594 }
471086d6 1595 }
c75b4594 1596
ca01d9d9 1597 /* If this is a bitfield, now convert to the lowered type. */
1598 if (type != TREE_TYPE (field))
c4698a21 1599 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
c75b4594 1600 flags |= picflag_from_initializer (next);
1601 CONSTRUCTOR_APPEND_ELT (v, field, next);
471086d6 1602 }
471086d6 1603
8698843f 1604 if (idx < CONSTRUCTOR_NELTS (init))
b52bd4ae 1605 {
1606 if (complain & tf_error)
8e79b5ff 1607 {
1608 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1609 /* For better diagnostics, try to find out if it is really
1610 the case of too many initializers or if designators are
1611 in incorrect order. */
1612 if (designator_skip == 1 && ce->index)
1613 {
1614 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1615 || identifier_p (ce->index));
1616 for (field = TYPE_FIELDS (type);
1617 field; field = DECL_CHAIN (field))
1618 {
8e79b5ff 1619 if (TREE_CODE (field) != FIELD_DECL
1620 || (DECL_ARTIFICIAL (field)
1621 && !(cxx_dialect >= cxx17
1622 && DECL_FIELD_IS_BASE (field))))
1623 continue;
1624
3cde3c29 1625 if (DECL_UNNAMED_BIT_FIELD (field))
1626 continue;
1627
8e79b5ff 1628 if (ce->index == field || ce->index == DECL_NAME (field))
1629 break;
1630 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1631 {
1632 tree t
1633 = search_anon_aggr (TREE_TYPE (field),
1634 TREE_CODE (ce->index) == FIELD_DECL
1635 ? DECL_NAME (ce->index)
1636 : ce->index);
1637 if (t)
1638 {
1639 field = t;
1640 break;
1641 }
1642 }
1643 }
1644 }
1645 if (field)
1646 error ("designator order for field %qD does not match declaration "
1647 "order in %qT", field, type);
1648 else
1649 error ("too many initializers for %qT", type);
1650 }
b52bd4ae 1651 else
1652 return PICFLAG_ERRONEOUS;
1653 }
1654
c75b4594 1655 CONSTRUCTOR_ELTS (init) = v;
1656 return flags;
1657}
471086d6 1658
c75b4594 1659/* Subroutine of process_init_constructor, which will process a single
cd7043f9 1660 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
c75b4594 1661 which describe the initializer. */
471086d6 1662
c75b4594 1663static int
7aac6ee6 1664process_init_constructor_union (tree type, tree init, int nested,
b52bd4ae 1665 tsubst_flags_t complain)
c75b4594 1666{
1667 constructor_elt *ce;
1ee7f2fb 1668 int len;
471086d6 1669
5a869ab8 1670 /* If the initializer was empty, use the union's NSDMI if it has one.
1671 Otherwise use default zero initialization. */
f1f41a6c 1672 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5a869ab8 1673 {
1674 for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1675 {
b06ecd3c 1676 if (TREE_CODE (field) == FIELD_DECL
1677 && DECL_INITIAL (field) != NULL_TREE)
5a869ab8 1678 {
7604a798 1679 tree val = get_nsdmi (field, /*in_ctor=*/false, complain);
1680 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1681 && find_placeholders (val))
1682 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1683 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val);
5a869ab8 1684 break;
1685 }
1686 }
1687
1688 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1689 return 0;
1690 }
c75b4594 1691
f1f41a6c 1692 len = CONSTRUCTOR_ELTS (init)->length ();
1ee7f2fb 1693 if (len > 1)
1694 {
b52bd4ae 1695 if (!(complain & tf_error))
1696 return PICFLAG_ERRONEOUS;
1ee7f2fb 1697 error ("too many initializers for %qT", type);
f1f41a6c 1698 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1ee7f2fb 1699 }
1700
f1f41a6c 1701 ce = &(*CONSTRUCTOR_ELTS (init))[0];
c75b4594 1702
1703 /* If this element specifies a field, initialize via that field. */
1704 if (ce->index)
1705 {
1706 if (TREE_CODE (ce->index) == FIELD_DECL)
1707 ;
694683bb 1708 else if (identifier_p (ce->index))
c75b4594 1709 {
1710 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1711 tree name = ce->index;
1712 tree field;
1713 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1714 if (DECL_NAME (field) == name)
1715 break;
1716 if (!field)
471086d6 1717 {
b52bd4ae 1718 if (complain & tf_error)
1719 error ("no field %qD found in union being initialized",
1720 field);
c75b4594 1721 ce->value = error_mark_node;
471086d6 1722 }
c75b4594 1723 ce->index = field;
1724 }
1725 else
1726 {
1727 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1728 || TREE_CODE (ce->index) == RANGE_EXPR);
b52bd4ae 1729 if (complain & tf_error)
1730 error ("index value instead of field name in union initializer");
c75b4594 1731 ce->value = error_mark_node;
471086d6 1732 }
471086d6 1733 }
c75b4594 1734 else
471086d6 1735 {
471086d6 1736 /* Find the first named field. ANSI decided in September 1990
1737 that only named fields count here. */
c75b4594 1738 tree field = TYPE_FIELDS (type);
23ed74d8 1739 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
471086d6 1740 field = TREE_CHAIN (field);
23a0ce43 1741 if (field == NULL_TREE)
1742 {
b52bd4ae 1743 if (complain & tf_error)
1744 error ("too many initializers for %qT", type);
23a0ce43 1745 ce->value = error_mark_node;
1746 }
c75b4594 1747 ce->index = field;
1748 }
471086d6 1749
c75b4594 1750 if (ce->value && ce->value != error_mark_node)
7aac6ee6 1751 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested,
1752 complain);
471086d6 1753
c75b4594 1754 return picflag_from_initializer (ce->value);
1755}
471086d6 1756
c75b4594 1757/* Process INIT, a constructor for a variable of aggregate type TYPE. The
1758 constructor is a brace-enclosed initializer, and will be modified in-place.
1759
1760 Each element is converted to the right type through digest_init, and
1761 missing initializers are added following the language rules (zero-padding,
1762 etc.).
471086d6 1763
c75b4594 1764 After the execution, the initializer will have TREE_CONSTANT if all elts are
1765 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1766 constants that the assembler and linker can compute them.
074ab442 1767
c75b4594 1768 The function returns the initializer itself, or error_mark_node in case
1769 of error. */
1770
1771static tree
7aac6ee6 1772process_init_constructor (tree type, tree init, int nested,
1773 tsubst_flags_t complain)
c75b4594 1774{
1775 int flags;
1776
1777 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1778
76249021 1779 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
7aac6ee6 1780 flags = process_init_constructor_array (type, init, nested, complain);
c75b4594 1781 else if (TREE_CODE (type) == RECORD_TYPE)
7aac6ee6 1782 flags = process_init_constructor_record (type, init, nested, complain);
c75b4594 1783 else if (TREE_CODE (type) == UNION_TYPE)
7aac6ee6 1784 flags = process_init_constructor_union (type, init, nested, complain);
c75b4594 1785 else
1786 gcc_unreachable ();
471086d6 1787
c75b4594 1788 if (flags & PICFLAG_ERRONEOUS)
471086d6 1789 return error_mark_node;
1790
c75b4594 1791 TREE_TYPE (init) = type;
981a251c 1792 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
c75b4594 1793 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
48f76477 1794 if (flags & PICFLAG_SIDE_EFFECTS)
1795 {
1796 TREE_CONSTANT (init) = false;
1797 TREE_SIDE_EFFECTS (init) = true;
1798 }
1799 else if (flags & PICFLAG_NOT_ALL_CONSTANT)
0138a8ea 1800 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1801 TREE_CONSTANT (init) = false;
1802 else
4ee9c684 1803 {
c75b4594 1804 TREE_CONSTANT (init) = 1;
c75b4594 1805 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1806 TREE_STATIC (init) = 1;
4ee9c684 1807 }
c75b4594 1808 return init;
471086d6 1809}
1810\f
1811/* Given a structure or union value DATUM, construct and return
1812 the structure or union component which results from narrowing
8ef5085e 1813 that value to the base specified in BASETYPE. For example, given the
471086d6 1814 hierarchy
1815
1816 class L { int ii; };
1817 class A : L { ... };
1818 class B : L { ... };
1819 class C : A, B { ... };
1820
1821 and the declaration
1822
1823 C x;
1824
1825 then the expression
1826
652e1a2d 1827 x.A::ii refers to the ii member of the L part of
3398e91d 1828 the A part of the C object named by X. In this case,
c161288a 1829 DATUM would be x, and BASETYPE would be A.
1830
2a88c9e6 1831 I used to think that this was nonconformant, that the standard specified
1832 that first we look up ii in A, then convert x to an L& and pull out the
1833 ii part. But in fact, it does say that we convert x to an A&; A here
8ef5085e 1834 is known as the "naming class". (jason 2000-12-19)
1835
1836 BINFO_P points to a variable initialized either to NULL_TREE or to the
1837 binfo for the specific base subobject we want to convert to. */
471086d6 1838
1839tree
e394ed0f 1840build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
471086d6 1841{
4a2680fc 1842 tree binfo;
471086d6 1843
1844 if (datum == error_mark_node)
1845 return error_mark_node;
8ef5085e 1846 if (*binfo_p)
1847 binfo = *binfo_p;
1848 else
ae260dcc 1849 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1850 NULL, tf_warning_or_error);
471086d6 1851
8ef5085e 1852 if (!binfo || binfo == error_mark_node)
1853 {
1854 *binfo_p = NULL_TREE;
1855 if (!binfo)
1856 error_not_base_type (basetype, TREE_TYPE (datum));
1857 return error_mark_node;
1858 }
471086d6 1859
8ef5085e 1860 *binfo_p = binfo;
1e74225a 1861 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1862 tf_warning_or_error);
471086d6 1863}
1864
1865/* Build a reference to an object specified by the C++ `->' operator.
1866 Usually this just involves dereferencing the object, but if the
1867 `->' operator is overloaded, then such overloads must be
1868 performed until an object which does not have the `->' operator
1869 overloaded is found. An error is reported when circular pointer
1870 delegation is detected. */
96624a9e 1871
471086d6 1872tree
ef0b0c72 1873build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
471086d6 1874{
13795292 1875 tree orig_expr = expr;
13795292 1876 tree type = TREE_TYPE (expr);
034b484a 1877 tree last_rval = NULL_TREE;
f1f41a6c 1878 vec<tree, va_gc> *types_memoized = NULL;
471086d6 1879
1880 if (type == error_mark_node)
1881 return error_mark_node;
1882
3cc0b4b9 1883 if (processing_template_decl)
13795292 1884 {
90ad495b 1885 if (type && TYPE_PTR_P (type)
eee80116 1886 && !dependent_scope_p (TREE_TYPE (type)))
1887 /* Pointer to current instantiation, don't treat as dependent. */;
1888 else if (type_dependent_expression_p (expr))
255b5d15 1889 return build_min_nt_loc (loc, ARROW_EXPR, expr);
13795292 1890 expr = build_non_dependent_expr (expr);
1891 }
e857e9c7 1892
95397ff9 1893 if (MAYBE_CLASS_TYPE_P (type))
471086d6 1894 {
9951fe5d 1895 struct tinst_level *actual_inst = current_instantiation ();
1896 tree fn = NULL;
1897
ef0b0c72 1898 while ((expr = build_new_op (loc, COMPONENT_REF,
1899 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
2ad2700d 1900 &fn, complain)))
471086d6 1901 {
13795292 1902 if (expr == error_mark_node)
471086d6 1903 return error_mark_node;
1904
8f51a05f 1905 /* This provides a better instantiation backtrace in case of
1906 error. */
9951fe5d 1907 if (fn && DECL_USE_TEMPLATE (fn))
8f51a05f 1908 push_tinst_level_loc (fn,
1909 (current_instantiation () != actual_inst)
1910 ? DECL_SOURCE_LOCATION (fn)
1911 : input_location);
9951fe5d 1912 fn = NULL;
1913
1df3c246 1914 if (vec_member (TREE_TYPE (expr), types_memoized))
1915 {
2ad2700d 1916 if (complain & tf_error)
1917 error ("circular pointer delegation detected");
1df3c246 1918 return error_mark_node;
1919 }
c6723852 1920
f1f41a6c 1921 vec_safe_push (types_memoized, TREE_TYPE (expr));
13795292 1922 last_rval = expr;
9031d10b 1923 }
02add86d 1924
9951fe5d 1925 while (current_instantiation () != actual_inst)
1926 pop_tinst_level ();
1927
02add86d 1928 if (last_rval == NULL_TREE)
1929 {
2ad2700d 1930 if (complain & tf_error)
1931 error ("base operand of %<->%> has non-pointer type %qT", type);
02add86d 1932 return error_mark_node;
1933 }
1934
90ad495b 1935 if (TYPE_REF_P (TREE_TYPE (last_rval)))
471086d6 1936 last_rval = convert_from_reference (last_rval);
1937 }
1938 else
4405c1ad 1939 last_rval = decay_conversion (expr, complain);
471086d6 1940
c21c015b 1941 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
13795292 1942 {
1943 if (processing_template_decl)
b6691ff5 1944 {
4cbcb428 1945 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1946 orig_expr);
1947 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
b6691ff5 1948 return expr;
1949 }
13795292 1950
0744a0c1 1951 return cp_build_indirect_ref (last_rval, RO_ARROW, complain);
13795292 1952 }
471086d6 1953
2ad2700d 1954 if (complain & tf_error)
1955 {
1956 if (types_memoized)
1957 error ("result of %<operator->()%> yields non-pointer result");
1958 else
1959 error ("base operand of %<->%> is not a pointer");
1960 }
471086d6 1961 return error_mark_node;
1962}
1963
a63bc44c 1964/* Return an expression for "DATUM .* COMPONENT". DATUM has not
1965 already been checked out to be of aggregate type. */
96624a9e 1966
471086d6 1967tree
4405c1ad 1968build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
471086d6 1969{
a63bc44c 1970 tree ptrmem_type;
24f9a660 1971 tree objtype;
a63bc44c 1972 tree type;
74002e1d 1973 tree binfo;
a6460bf1 1974 tree ctype;
471086d6 1975
24357002 1976 if (error_operand_p (datum) || error_operand_p (component))
ab9559dc 1977 return error_mark_node;
1978
6ac80fa2 1979 datum = mark_lvalue_use (datum);
1980 component = mark_rvalue_use (component);
c507cbd7 1981
a63bc44c 1982 ptrmem_type = TREE_TYPE (component);
05765a91 1983 if (!TYPE_PTRMEM_P (ptrmem_type))
471086d6 1984 {
4405c1ad 1985 if (complain & tf_error)
1986 error ("%qE cannot be used as a member pointer, since it is of "
1987 "type %qT", component, ptrmem_type);
471086d6 1988 return error_mark_node;
1989 }
9031d10b 1990
1991 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
95397ff9 1992 if (! MAYBE_CLASS_TYPE_P (objtype))
ac9386a0 1993 {
4405c1ad 1994 if (complain & tf_error)
1995 error ("cannot apply member pointer %qE to %qE, which is of "
1996 "non-class type %qT", component, datum, objtype);
ac9386a0 1997 return error_mark_node;
1998 }
74002e1d 1999
a63bc44c 2000 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
a6460bf1 2001 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
2002
2003 if (!COMPLETE_TYPE_P (ctype))
471086d6 2004 {
a6460bf1 2005 if (!same_type_p (ctype, objtype))
2006 goto mismatch;
2007 binfo = NULL;
2008 }
2009 else
2010 {
ae260dcc 2011 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
9031d10b 2012
a6460bf1 2013 if (!binfo)
2014 {
2015 mismatch:
4405c1ad 2016 if (complain & tf_error)
2017 error ("pointer to member type %qT incompatible with object "
2018 "type %qT", type, objtype);
a6460bf1 2019 return error_mark_node;
2020 }
2021 else if (binfo == error_mark_node)
2022 return error_mark_node;
471086d6 2023 }
2024
05765a91 2025 if (TYPE_PTRDATAMEM_P (ptrmem_type))
a63bc44c 2026 {
3f5730c2 2027 bool is_lval = real_lvalue_p (datum);
69db191c 2028 tree ptype;
2029
a63bc44c 2030 /* Compute the type of the field, as described in [expr.ref].
2031 There's no such thing as a mutable pointer-to-member, so
2032 things are not as complex as they are for references to
2033 non-static data members. */
2034 type = cp_build_qualified_type (type,
9031d10b 2035 (cp_type_quals (type)
a63bc44c 2036 | cp_type_quals (TREE_TYPE (datum))));
a6460bf1 2037
2038 datum = build_address (datum);
9031d10b 2039
a6460bf1 2040 /* Convert object to the correct base. */
2041 if (binfo)
4405c1ad 2042 {
2043 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
2044 if (datum == error_mark_node)
2045 return error_mark_node;
2046 }
9031d10b 2047
1bc16cab 2048 /* Build an expression for "object + offset" where offset is the
2049 value stored in the pointer-to-data-member. */
69db191c 2050 ptype = build_pointer_type (type);
2cc66f2a 2051 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
0744a0c1 2052 datum = cp_build_fold_indirect_ref (datum);
4405c1ad 2053 if (datum == error_mark_node)
2054 return error_mark_node;
2055
0277b639 2056 /* If the object expression was an rvalue, return an rvalue. */
3f5730c2 2057 if (!is_lval)
0277b639 2058 datum = move (datum);
2059 return datum;
a63bc44c 2060 }
2061 else
e116411c 2062 {
2063 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2064 program is ill-formed if the second operand is a pointer to member
dabb58ae 2065 function with ref-qualifier & (for C++2A: unless its cv-qualifier-seq
2066 is const). In a .* expression whose object expression is an lvalue,
2067 the program is ill-formed if the second operand is a pointer to member
2068 function with ref-qualifier &&. */
e116411c 2069 if (FUNCTION_REF_QUALIFIED (type))
2070 {
18bede74 2071 bool lval = lvalue_p (datum);
e116411c 2072 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
6f2aadc8 2073 {
2074 if (complain & tf_error)
2075 error ("pointer-to-member-function type %qT requires an rvalue",
2076 ptrmem_type);
2077 return error_mark_node;
2078 }
5753ddf9 2079 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
6f2aadc8 2080 {
5753ddf9 2081 if ((type_memfn_quals (type)
2082 & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
2083 != TYPE_QUAL_CONST)
2084 {
2085 if (complain & tf_error)
2086 error ("pointer-to-member-function type %qT requires "
2087 "an lvalue", ptrmem_type);
2088 return error_mark_node;
2089 }
2090 else if (cxx_dialect < cxx2a)
2091 {
2092 if (complain & tf_warning_or_error)
2093 pedwarn (input_location, OPT_Wpedantic,
2094 "pointer-to-member-function type %qT requires "
2095 "an lvalue before C++2a", ptrmem_type);
2096 else
2097 return error_mark_node;
2098 }
6f2aadc8 2099 }
e116411c 2100 }
2101 return build2 (OFFSET_REF, type, datum, component);
2102 }
471086d6 2103}
2104
d2a15a12 2105/* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
96624a9e 2106
471086d6 2107tree
ebd21de4 2108build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
471086d6 2109{
2110 /* This is either a call to a constructor,
2111 or a C cast in C++'s `functional' notation. */
930e8175 2112
2113 /* The type to which we are casting. */
d2a15a12 2114 tree type;
f1f41a6c 2115 vec<tree, va_gc> *parmvec;
471086d6 2116
bd157edd 2117 if (error_operand_p (exp) || parms == error_mark_node)
471086d6 2118 return error_mark_node;
2119
b7d1e8ea 2120 if (TREE_CODE (exp) == TYPE_DECL)
2b2b5104 2121 {
2122 type = TREE_TYPE (exp);
2123
17847cff 2124 if (DECL_ARTIFICIAL (exp))
e57bcc46 2125 cp_warn_deprecated_use (type);
2b2b5104 2126 }
471086d6 2127 else
2128 type = exp;
2129
04725249 2130 /* We need to check this explicitly, since value-initialization of
2131 arrays is allowed in other situations. */
2132 if (TREE_CODE (type) == ARRAY_TYPE)
2133 {
2134 if (complain & tf_error)
2135 error ("functional cast to array type %qT", type);
2136 return error_mark_node;
2137 }
2138
c2ab9194 2139 if (tree anode = type_uses_auto (type))
32d9c766 2140 {
c2ab9194 2141 if (!CLASS_PLACEHOLDER_TEMPLATE (anode))
2142 {
2143 if (complain & tf_error)
cb7f839b 2144 error_at (DECL_SOURCE_LOCATION (TEMPLATE_TYPE_DECL (anode)),
2145 "invalid use of %qT", anode);
c2ab9194 2146 return error_mark_node;
2147 }
2148 else if (!parms)
2149 {
2150 if (complain & tf_error)
2151 error ("cannot deduce template arguments for %qT from ()", anode);
2152 return error_mark_node;
2153 }
2154 else
2155 type = do_auto_deduction (type, parms, anode, complain,
2156 adc_variable_type);
32d9c766 2157 }
2158
3cc0b4b9 2159 if (processing_template_decl)
b6691ff5 2160 {
74b7a9bc 2161 tree t;
2162
2163 /* Diagnose this even in a template. We could also try harder
2164 to give all the usual errors when the type and args are
2165 non-dependent... */
90ad495b 2166 if (TYPE_REF_P (type) && !parms)
74b7a9bc 2167 {
2168 if (complain & tf_error)
2169 error ("invalid value-initialization of reference type");
2170 return error_mark_node;
2171 }
2172
2173 t = build_min (CAST_EXPR, type, parms);
b6691ff5 2174 /* We don't know if it will or will not have side effects. */
2175 TREE_SIDE_EFFECTS (t) = 1;
2176 return t;
2177 }
e857e9c7 2178
95397ff9 2179 if (! MAYBE_CLASS_TYPE_P (type))
471086d6 2180 {
471086d6 2181 if (parms == NULL_TREE)
61e4f7d9 2182 {
2183 if (VOID_TYPE_P (type))
3ab4693e 2184 return void_node;
6f905cd1 2185 return build_value_init (cv_unqualified (type), complain);
61e4f7d9 2186 }
1a3f833b 2187
5c775397 2188 /* This must build a C cast. */
1f3d2e3f 2189 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
717ecce9 2190 return cp_build_c_cast (type, parms, complain);
471086d6 2191 }
2192
e178a5a9 2193 /* Prepare to evaluate as a call to a constructor. If this expression
2194 is actually used, for example,
9031d10b 2195
e178a5a9 2196 return X (arg1, arg2, ...);
9031d10b 2197
e178a5a9 2198 then the slot being initialized will be filled in. */
2199
a5f2d620 2200 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
4b72716d 2201 return error_mark_node;
d28993f1 2202 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
8c18e707 2203 return error_mark_node;
471086d6 2204
930e8175 2205 /* [expr.type.conv]
2206
2207 If the expression list is a single-expression, the type
2208 conversion is equivalent (in definedness, and if defined in
2209 meaning) to the corresponding cast expression. */
471086d6 2210 if (parms && TREE_CHAIN (parms) == NULL_TREE)
717ecce9 2211 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
471086d6 2212
930e8175 2213 /* [expr.type.conv]
2214
2215 The expression T(), where T is a simple-type-specifier for a
2216 non-array complete object type or the (possibly cv-qualified)
2217 void type, creates an rvalue of the specified type, which is
2218 value-initialized. */
2219
a366cfa9 2220 if (parms == NULL_TREE)
df7d45e1 2221 {
a5f2d620 2222 exp = build_value_init (type, complain);
9e505437 2223 exp = get_target_expr_sfinae (exp, complain);
ce984e5e 2224 return exp;
df7d45e1 2225 }
2226
930e8175 2227 /* Call the constructor. */
f352a3fb 2228 parmvec = make_tree_vector ();
2229 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
f1f41a6c 2230 vec_safe_push (parmvec, TREE_VALUE (parms));
f352a3fb 2231 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2232 &parmvec, type, LOOKUP_NORMAL, complain);
2233 release_tree_vector (parmvec);
471086d6 2234
d2a15a12 2235 if (exp == error_mark_node)
ddb9bca7 2236 return error_mark_node;
471086d6 2237
3d4bed93 2238 return build_cplus_new (type, exp, complain);
471086d6 2239}
2240\f
0e0a0dea 2241
316b7a44 2242/* Add new exception specifier SPEC, to the LIST we currently have.
2243 If it's already in LIST then do nothing.
2244 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2245 know what we're doing. */
2246
2247tree
a470c772 2248add_exception_specifier (tree list, tree spec, tsubst_flags_t complain)
316b7a44 2249{
bd57a622 2250 bool ok;
316b7a44 2251 tree core = spec;
bd57a622 2252 bool is_ptr;
a52d5726 2253 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
9031d10b 2254
316b7a44 2255 if (spec == error_mark_node)
2256 return list;
9031d10b 2257
b4df430b 2258 gcc_assert (spec && (!list || TREE_VALUE (list)));
9031d10b 2259
316b7a44 2260 /* [except.spec] 1, type in an exception specifier shall not be
2261 incomplete, or pointer or ref to incomplete other than pointer
2262 to cv void. */
c21c015b 2263 is_ptr = TYPE_PTR_P (core);
90ad495b 2264 if (is_ptr || TYPE_REF_P (core))
316b7a44 2265 core = TREE_TYPE (core);
2266 if (complain < 0)
bd57a622 2267 ok = true;
e3cfe3ce 2268 else if (VOID_TYPE_P (core))
316b7a44 2269 ok = is_ptr;
2270 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
bd57a622 2271 ok = true;
ba1f2de0 2272 else if (processing_template_decl)
bd57a622 2273 ok = true;
316b7a44 2274 else
6fee0737 2275 {
bd57a622 2276 ok = true;
6fee0737 2277 /* 15.4/1 says that types in an exception specifier must be complete,
653e5405 2278 but it seems more reasonable to only require this on definitions
2279 and calls. So just give a pedwarn at this point; we will give an
2280 error later if we hit one of those two cases. */
6fee0737 2281 if (!COMPLETE_TYPE_P (complete_type (core)))
a52d5726 2282 diag_type = DK_PEDWARN; /* pedwarn */
6fee0737 2283 }
ba1f2de0 2284
316b7a44 2285 if (ok)
2286 {
2287 tree probe;
9031d10b 2288
316b7a44 2289 for (probe = list; probe; probe = TREE_CHAIN (probe))
653e5405 2290 if (same_type_p (TREE_VALUE (probe), spec))
2291 break;
316b7a44 2292 if (!probe)
0a7d30b7 2293 list = tree_cons (NULL_TREE, spec, list);
316b7a44 2294 }
6fee0737 2295 else
a52d5726 2296 diag_type = DK_ERROR; /* error */
9031d10b 2297
86359a65 2298 if (diag_type != DK_UNSPECIFIED
2299 && (complain & tf_warning_or_error))
6fee0737 2300 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2301
316b7a44 2302 return list;
2303}
d8ae7647 2304
3239620b 2305/* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2306
2307static bool
2308nothrow_spec_p_uninst (const_tree spec)
2309{
2310 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2311 return false;
2312 return nothrow_spec_p (spec);
2313}
2314
d8ae7647 2315/* Combine the two exceptions specifier lists LIST and ADD, and return
f2c1aabc 2316 their union. */
d8ae7647 2317
2318tree
f2c1aabc 2319merge_exception_specifiers (tree list, tree add)
d8ae7647 2320{
3239620b 2321 tree noex, orig_list;
2322
9f354539 2323 /* No exception-specifier or noexcept(false) are less strict than
2324 anything else. Prefer the newer variant (LIST). */
2325 if (!list || list == noexcept_false_spec)
2326 return list;
2327 else if (!add || add == noexcept_false_spec)
2328 return add;
6bb4902d 2329
3239620b 2330 /* noexcept(true) and throw() are stricter than anything else.
2331 As above, prefer the more recent one (LIST). */
2332 if (nothrow_spec_p_uninst (add))
d8ae7647 2333 return list;
3239620b 2334
f2c1aabc 2335 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2336 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2337 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2338 return list;
2339 /* We should have instantiated other deferred noexcept specs by now. */
2340 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2341
2342 if (nothrow_spec_p_uninst (list))
02fe61a4 2343 return add;
f2c1aabc 2344 noex = TREE_PURPOSE (list);
2345 gcc_checking_assert (!TREE_PURPOSE (add)
4c3bd1e3 2346 || errorcount || !flag_exceptions
f2c1aabc 2347 || cp_tree_equal (noex, TREE_PURPOSE (add)));
3239620b 2348
2349 /* Combine the dynamic-exception-specifiers, if any. */
2350 orig_list = list;
2351 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
d8ae7647 2352 {
3239620b 2353 tree spec = TREE_VALUE (add);
2354 tree probe;
9031d10b 2355
3239620b 2356 for (probe = orig_list; probe && TREE_VALUE (probe);
2357 probe = TREE_CHAIN (probe))
2358 if (same_type_p (TREE_VALUE (probe), spec))
2359 break;
2360 if (!probe)
653e5405 2361 {
3239620b 2362 spec = build_tree_list (NULL_TREE, spec);
2363 TREE_CHAIN (spec) = list;
2364 list = spec;
653e5405 2365 }
d8ae7647 2366 }
3239620b 2367
2368 /* Keep the noexcept-specifier at the beginning of the list. */
2369 if (noex != TREE_PURPOSE (list))
2370 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2371
d8ae7647 2372 return list;
2373}
6fee0737 2374
2375/* Subroutine of build_call. Ensure that each of the types in the
2376 exception specification is complete. Technically, 15.4/1 says that
2377 they need to be complete when we see a declaration of the function,
2378 but we should be able to get away with only requiring this when the
2379 function is defined or called. See also add_exception_specifier. */
2380
2381void
e394ed0f 2382require_complete_eh_spec_types (tree fntype, tree decl)
6fee0737 2383{
2384 tree raises;
2385 /* Don't complain about calls to op new. */
2386 if (decl && DECL_ARTIFICIAL (decl))
2387 return;
2388 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2389 raises = TREE_CHAIN (raises))
2390 {
2391 tree type = TREE_VALUE (raises);
2392 if (type && !COMPLETE_TYPE_P (type))
2393 {
2394 if (decl)
2395 error
03d6ca9e 2396 ("call to function %qD which throws incomplete type %q#T",
6fee0737 2397 decl, type);
2398 else
03d6ca9e 2399 error ("call to function which throws incomplete type %q#T",
6fee0737 2400 decl);
2401 }
2402 }
2403}
d97a7640 2404
2405\f
2406#include "gt-cp-typeck2.h"