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