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