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