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