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