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