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