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