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