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