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