2013-06-24 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cp / typeck2.c
bloba44789362e33f5d6dcdc119b302b1f17aadc9c04
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))
780 /* Diagnose a non-constant initializer for constexpr. */
781 if (processing_template_decl
782 && !require_potential_constant_expression (value))
783 value = error_mark_node;
784 else
785 value = cxx_constant_value (value);
787 const_init = (reduced_constant_expression_p (value)
788 || error_operand_p (value));
789 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
790 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
793 /* If the initializer is not a constant, fill in DECL_INITIAL with
794 the bits that are constant, and then return an expression that
795 will perform the dynamic initialization. */
796 if (value != error_mark_node
797 && (TREE_SIDE_EFFECTS (value)
798 || array_of_runtime_bound_p (type)
799 || ! reduced_constant_expression_p (value)))
801 if (TREE_CODE (type) == ARRAY_TYPE
802 && (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type))
803 || array_of_runtime_bound_p (type)))
804 /* For an array, we only need/want a single cleanup region rather
805 than one per element. */
806 return build_vec_init (decl, NULL_TREE, value, false, 1,
807 tf_warning_or_error);
808 else
809 return split_nonconstant_init (decl, value);
811 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
812 is an automatic variable, the middle end will turn this into a
813 dynamic initialization later. */
814 DECL_INITIAL (decl) = value;
815 return NULL_TREE;
819 /* Give errors about narrowing conversions within { }. */
821 void
822 check_narrowing (tree type, tree init)
824 tree ftype = unlowered_expr_type (init);
825 bool ok = true;
826 REAL_VALUE_TYPE d;
828 if (!warn_narrowing || !ARITHMETIC_TYPE_P (type))
829 return;
831 if (BRACE_ENCLOSED_INITIALIZER_P (init)
832 && TREE_CODE (type) == COMPLEX_TYPE)
834 tree elttype = TREE_TYPE (type);
835 check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value);
836 if (CONSTRUCTOR_NELTS (init) > 1)
837 check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value);
838 return;
841 init = maybe_constant_value (init);
843 if (TREE_CODE (type) == INTEGER_TYPE
844 && TREE_CODE (ftype) == REAL_TYPE)
845 ok = false;
846 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
847 && CP_INTEGRAL_TYPE_P (type))
849 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
850 /* Check for narrowing based on the values of the enumeration. */
851 ftype = ENUM_UNDERLYING_TYPE (ftype);
852 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
853 TYPE_MAX_VALUE (ftype))
854 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
855 TYPE_MIN_VALUE (type)))
856 && (TREE_CODE (init) != INTEGER_CST
857 || !int_fits_type_p (init, type)))
858 ok = false;
860 else if (TREE_CODE (ftype) == REAL_TYPE
861 && TREE_CODE (type) == REAL_TYPE)
863 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
865 if (TREE_CODE (init) == REAL_CST)
867 /* Issue 703: Loss of precision is OK as long as the value is
868 within the representable range of the new type. */
869 REAL_VALUE_TYPE r;
870 d = TREE_REAL_CST (init);
871 real_convert (&r, TYPE_MODE (type), &d);
872 if (real_isinf (&r))
873 ok = false;
875 else
876 ok = false;
879 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
880 && TREE_CODE (type) == REAL_TYPE)
882 ok = false;
883 if (TREE_CODE (init) == INTEGER_CST)
885 d = real_value_from_int_cst (0, init);
886 if (exact_real_truncate (TYPE_MODE (type), &d))
887 ok = true;
891 if (!ok)
893 if (cxx_dialect >= cxx11)
894 pedwarn (EXPR_LOC_OR_HERE (init), OPT_Wnarrowing,
895 "narrowing conversion of %qE from %qT to %qT inside { }",
896 init, ftype, type);
897 else
898 warning_at (EXPR_LOC_OR_HERE (init), OPT_Wnarrowing,
899 "narrowing conversion of %qE from %qT to %qT inside { } "
900 "is ill-formed in C++11", init, ftype, type);
904 /* Process the initializer INIT for a variable of type TYPE, emitting
905 diagnostics for invalid initializers and converting the initializer as
906 appropriate.
908 For aggregate types, it assumes that reshape_init has already run, thus the
909 initializer will have the right shape (brace elision has been undone).
911 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
913 static tree
914 digest_init_r (tree type, tree init, bool nested, int flags,
915 tsubst_flags_t complain)
917 enum tree_code code = TREE_CODE (type);
919 if (error_operand_p (init))
920 return error_mark_node;
922 gcc_assert (init);
924 /* We must strip the outermost array type when completing the type,
925 because the its bounds might be incomplete at the moment. */
926 if (!complete_type_or_maybe_complain (TREE_CODE (type) == ARRAY_TYPE
927 ? TREE_TYPE (type) : type, NULL_TREE,
928 complain))
929 return error_mark_node;
931 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
932 (g++.old-deja/g++.law/casts2.C). */
933 if (TREE_CODE (init) == NON_LVALUE_EXPR)
934 init = TREE_OPERAND (init, 0);
936 /* Initialization of an array of chars from a string constant. The initializer
937 can be optionally enclosed in braces, but reshape_init has already removed
938 them if they were present. */
939 if (code == ARRAY_TYPE)
941 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
942 if (char_type_p (typ1)
943 /*&& init */
944 && TREE_CODE (init) == STRING_CST)
946 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
948 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
950 if (char_type != char_type_node)
952 if (complain & tf_error)
953 error ("char-array initialized from wide string");
954 return error_mark_node;
957 else
959 if (char_type == char_type_node)
961 if (complain & tf_error)
962 error ("int-array initialized from non-wide string");
963 return error_mark_node;
965 else if (char_type != typ1)
967 if (complain & tf_error)
968 error ("int-array initialized from incompatible "
969 "wide string");
970 return error_mark_node;
974 if (type != TREE_TYPE (init))
976 init = copy_node (init);
977 TREE_TYPE (init) = type;
979 if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
981 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
982 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
983 /* In C it is ok to subtract 1 from the length of the string
984 because it's ok to ignore the terminating null char that is
985 counted in the length of the constant, but in C++ this would
986 be invalid. */
987 if (size < TREE_STRING_LENGTH (init))
988 permerror (input_location, "initializer-string for array "
989 "of chars is too long");
991 return init;
995 /* Handle scalar types (including conversions) and references. */
996 if ((TREE_CODE (type) != COMPLEX_TYPE
997 || BRACE_ENCLOSED_INITIALIZER_P (init))
998 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1000 tree *exp;
1002 if (nested)
1003 check_narrowing (type, init);
1004 init = convert_for_initialization (0, type, init, flags,
1005 ICR_INIT, NULL_TREE, 0,
1006 complain);
1007 exp = &init;
1009 /* Skip any conversions since we'll be outputting the underlying
1010 constant. */
1011 while (CONVERT_EXPR_P (*exp)
1012 || TREE_CODE (*exp) == NON_LVALUE_EXPR)
1013 exp = &TREE_OPERAND (*exp, 0);
1015 *exp = cplus_expand_constant (*exp);
1017 return init;
1020 /* Come here only for aggregates: records, arrays, unions, complex numbers
1021 and vectors. */
1022 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1023 || TREE_CODE (type) == VECTOR_TYPE
1024 || TREE_CODE (type) == RECORD_TYPE
1025 || TREE_CODE (type) == UNION_TYPE
1026 || TREE_CODE (type) == COMPLEX_TYPE);
1028 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1029 && !TYPE_NON_AGGREGATE_CLASS (type))
1030 return process_init_constructor (type, init, complain);
1031 else
1033 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
1035 if (complain & tf_error)
1036 error ("cannot initialize aggregate of type %qT with "
1037 "a compound literal", type);
1039 return error_mark_node;
1042 if (TREE_CODE (type) == ARRAY_TYPE
1043 && !BRACE_ENCLOSED_INITIALIZER_P (init))
1045 /* Allow the result of build_array_copy and of
1046 build_value_init_noctor. */
1047 if ((TREE_CODE (init) == VEC_INIT_EXPR
1048 || TREE_CODE (init) == CONSTRUCTOR)
1049 && (same_type_ignoring_top_level_qualifiers_p
1050 (type, TREE_TYPE (init))))
1051 return init;
1053 if (complain & tf_error)
1054 error ("array must be initialized with a brace-enclosed"
1055 " initializer");
1056 return error_mark_node;
1059 return convert_for_initialization (NULL_TREE, type, init,
1060 flags,
1061 ICR_INIT, NULL_TREE, 0,
1062 complain);
1066 tree
1067 digest_init (tree type, tree init, tsubst_flags_t complain)
1069 return digest_init_r (type, init, false, LOOKUP_IMPLICIT, complain);
1072 tree
1073 digest_init_flags (tree type, tree init, int flags)
1075 return digest_init_r (type, init, false, flags, tf_warning_or_error);
1078 /* Set of flags used within process_init_constructor to describe the
1079 initializers. */
1080 #define PICFLAG_ERRONEOUS 1
1081 #define PICFLAG_NOT_ALL_CONSTANT 2
1082 #define PICFLAG_NOT_ALL_SIMPLE 4
1084 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1085 describe it. */
1087 static int
1088 picflag_from_initializer (tree init)
1090 if (init == error_mark_node)
1091 return PICFLAG_ERRONEOUS;
1092 else if (!TREE_CONSTANT (init))
1093 return PICFLAG_NOT_ALL_CONSTANT;
1094 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1095 return PICFLAG_NOT_ALL_SIMPLE;
1096 return 0;
1099 /* Subroutine of process_init_constructor, which will process an initializer
1100 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1101 which describe the initializers. */
1103 static int
1104 process_init_constructor_array (tree type, tree init,
1105 tsubst_flags_t complain)
1107 unsigned HOST_WIDE_INT i, len = 0;
1108 int flags = 0;
1109 bool unbounded = false;
1110 constructor_elt *ce;
1111 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1113 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1114 || TREE_CODE (type) == VECTOR_TYPE);
1116 if (TREE_CODE (type) == ARRAY_TYPE)
1118 tree domain = TYPE_DOMAIN (type);
1119 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1120 len = (tree_to_double_int (TYPE_MAX_VALUE (domain))
1121 - tree_to_double_int (TYPE_MIN_VALUE (domain))
1122 + double_int_one)
1123 .ext (TYPE_PRECISION (TREE_TYPE (domain)),
1124 TYPE_UNSIGNED (TREE_TYPE (domain)))
1125 .low;
1126 else
1127 unbounded = true; /* Take as many as there are. */
1129 else
1130 /* Vectors are like simple fixed-size arrays. */
1131 len = TYPE_VECTOR_SUBPARTS (type);
1133 /* There must not be more initializers than needed. */
1134 if (!unbounded && vec_safe_length (v) > len)
1136 if (complain & tf_error)
1137 error ("too many initializers for %qT", type);
1138 else
1139 return PICFLAG_ERRONEOUS;
1142 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1144 if (ce->index)
1146 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1147 if (compare_tree_int (ce->index, i) != 0)
1149 ce->value = error_mark_node;
1150 sorry ("non-trivial designated initializers not supported");
1153 else
1154 ce->index = size_int (i);
1155 gcc_assert (ce->value);
1156 ce->value = digest_init_r (TREE_TYPE (type), ce->value, true,
1157 LOOKUP_IMPLICIT, complain);
1159 if (ce->value != error_mark_node)
1160 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1161 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1163 flags |= picflag_from_initializer (ce->value);
1166 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1167 we must add initializers ourselves. */
1168 if (!unbounded)
1169 for (; i < len; ++i)
1171 tree next;
1173 if (type_build_ctor_call (TREE_TYPE (type)))
1175 /* If this type needs constructors run for default-initialization,
1176 we can't rely on the back end to do it for us, so make the
1177 initialization explicit by list-initializing from {}. */
1178 next = build_constructor (init_list_type_node, NULL);
1179 next = digest_init (TREE_TYPE (type), next, complain);
1181 else if (!zero_init_p (TREE_TYPE (type)))
1182 next = build_zero_init (TREE_TYPE (type),
1183 /*nelts=*/NULL_TREE,
1184 /*static_storage_p=*/false);
1185 else
1186 /* The default zero-initialization is fine for us; don't
1187 add anything to the CONSTRUCTOR. */
1188 break;
1190 flags |= picflag_from_initializer (next);
1191 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1194 CONSTRUCTOR_ELTS (init) = v;
1195 return flags;
1198 /* Subroutine of process_init_constructor, which will process an initializer
1199 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1200 the initializers. */
1202 static int
1203 process_init_constructor_record (tree type, tree init,
1204 tsubst_flags_t complain)
1206 vec<constructor_elt, va_gc> *v = NULL;
1207 int flags = 0;
1208 tree field;
1209 unsigned HOST_WIDE_INT idx = 0;
1211 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1212 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1213 gcc_assert (!TYPE_BINFO (type)
1214 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1215 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1217 /* Generally, we will always have an index for each initializer (which is
1218 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1219 reshape_init. So we need to handle both cases. */
1220 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1222 tree next;
1223 tree type;
1225 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1227 flags |= picflag_from_initializer (integer_zero_node);
1228 CONSTRUCTOR_APPEND_ELT (v, field, integer_zero_node);
1229 continue;
1232 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1233 continue;
1235 /* If this is a bitfield, first convert to the declared type. */
1236 type = TREE_TYPE (field);
1237 if (DECL_BIT_FIELD_TYPE (field))
1238 type = DECL_BIT_FIELD_TYPE (field);
1239 if (type == error_mark_node)
1240 return PICFLAG_ERRONEOUS;
1242 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1244 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1245 if (ce->index)
1247 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1248 latter case can happen in templates where lookup has to be
1249 deferred. */
1250 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1251 || identifier_p (ce->index));
1252 if (ce->index != field
1253 && ce->index != DECL_NAME (field))
1255 ce->value = error_mark_node;
1256 sorry ("non-trivial designated initializers not supported");
1260 gcc_assert (ce->value);
1261 next = digest_init_r (type, ce->value, true,
1262 LOOKUP_IMPLICIT, complain);
1263 ++idx;
1265 else if (type_build_ctor_call (TREE_TYPE (field)))
1267 /* If this type needs constructors run for
1268 default-initialization, we can't rely on the back end to do it
1269 for us, so build up TARGET_EXPRs. If the type in question is
1270 a class, just build one up; if it's an array, recurse. */
1271 next = build_constructor (init_list_type_node, NULL);
1272 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field)))
1274 next = finish_compound_literal (TREE_TYPE (field), next,
1275 complain);
1276 /* direct-initialize the target. No temporary is going
1277 to be involved. */
1278 if (TREE_CODE (next) == TARGET_EXPR)
1279 TARGET_EXPR_DIRECT_INIT_P (next) = true;
1282 next = digest_init_r (TREE_TYPE (field), next, true,
1283 LOOKUP_IMPLICIT, complain);
1285 /* Warn when some struct elements are implicitly initialized. */
1286 warning (OPT_Wmissing_field_initializers,
1287 "missing initializer for member %qD", field);
1289 else
1291 if (TREE_READONLY (field))
1293 if (complain & tf_error)
1294 error ("uninitialized const member %qD", field);
1295 else
1296 return PICFLAG_ERRONEOUS;
1298 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1300 if (complain & tf_error)
1301 error ("member %qD with uninitialized const fields", field);
1302 else
1303 return PICFLAG_ERRONEOUS;
1305 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1307 if (complain & tf_error)
1308 error ("member %qD is uninitialized reference", field);
1309 else
1310 return PICFLAG_ERRONEOUS;
1313 /* Warn when some struct elements are implicitly initialized
1314 to zero. */
1315 warning (OPT_Wmissing_field_initializers,
1316 "missing initializer for member %qD", field);
1318 if (!zero_init_p (TREE_TYPE (field)))
1319 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1320 /*static_storage_p=*/false);
1321 else
1322 /* The default zero-initialization is fine for us; don't
1323 add anything to the CONSTRUCTOR. */
1324 continue;
1327 /* If this is a bitfield, now convert to the lowered type. */
1328 if (type != TREE_TYPE (field))
1329 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1330 flags |= picflag_from_initializer (next);
1331 CONSTRUCTOR_APPEND_ELT (v, field, next);
1334 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1336 if (complain & tf_error)
1337 error ("too many initializers for %qT", type);
1338 else
1339 return PICFLAG_ERRONEOUS;
1342 CONSTRUCTOR_ELTS (init) = v;
1343 return flags;
1346 /* Subroutine of process_init_constructor, which will process a single
1347 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1348 which describe the initializer. */
1350 static int
1351 process_init_constructor_union (tree type, tree init,
1352 tsubst_flags_t complain)
1354 constructor_elt *ce;
1355 int len;
1357 /* If the initializer was empty, use default zero initialization. */
1358 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1359 return 0;
1361 len = CONSTRUCTOR_ELTS (init)->length ();
1362 if (len > 1)
1364 if (!(complain & tf_error))
1365 return PICFLAG_ERRONEOUS;
1366 error ("too many initializers for %qT", type);
1367 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1370 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1372 /* If this element specifies a field, initialize via that field. */
1373 if (ce->index)
1375 if (TREE_CODE (ce->index) == FIELD_DECL)
1377 else if (identifier_p (ce->index))
1379 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1380 tree name = ce->index;
1381 tree field;
1382 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1383 if (DECL_NAME (field) == name)
1384 break;
1385 if (!field)
1387 if (complain & tf_error)
1388 error ("no field %qD found in union being initialized",
1389 field);
1390 ce->value = error_mark_node;
1392 ce->index = field;
1394 else
1396 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1397 || TREE_CODE (ce->index) == RANGE_EXPR);
1398 if (complain & tf_error)
1399 error ("index value instead of field name in union initializer");
1400 ce->value = error_mark_node;
1403 else
1405 /* Find the first named field. ANSI decided in September 1990
1406 that only named fields count here. */
1407 tree field = TYPE_FIELDS (type);
1408 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1409 field = TREE_CHAIN (field);
1410 if (field == NULL_TREE)
1412 if (complain & tf_error)
1413 error ("too many initializers for %qT", type);
1414 ce->value = error_mark_node;
1416 ce->index = field;
1419 if (ce->value && ce->value != error_mark_node)
1420 ce->value = digest_init_r (TREE_TYPE (ce->index), ce->value,
1421 true, LOOKUP_IMPLICIT, complain);
1423 return picflag_from_initializer (ce->value);
1426 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1427 constructor is a brace-enclosed initializer, and will be modified in-place.
1429 Each element is converted to the right type through digest_init, and
1430 missing initializers are added following the language rules (zero-padding,
1431 etc.).
1433 After the execution, the initializer will have TREE_CONSTANT if all elts are
1434 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1435 constants that the assembler and linker can compute them.
1437 The function returns the initializer itself, or error_mark_node in case
1438 of error. */
1440 static tree
1441 process_init_constructor (tree type, tree init, tsubst_flags_t complain)
1443 int flags;
1445 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1447 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1448 flags = process_init_constructor_array (type, init, complain);
1449 else if (TREE_CODE (type) == RECORD_TYPE)
1450 flags = process_init_constructor_record (type, init, complain);
1451 else if (TREE_CODE (type) == UNION_TYPE)
1452 flags = process_init_constructor_union (type, init, complain);
1453 else
1454 gcc_unreachable ();
1456 if (flags & PICFLAG_ERRONEOUS)
1457 return error_mark_node;
1459 TREE_TYPE (init) = type;
1460 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1461 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1462 if (flags & PICFLAG_NOT_ALL_CONSTANT)
1463 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1464 TREE_CONSTANT (init) = false;
1465 else
1467 TREE_CONSTANT (init) = 1;
1468 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1469 TREE_STATIC (init) = 1;
1471 return init;
1474 /* Given a structure or union value DATUM, construct and return
1475 the structure or union component which results from narrowing
1476 that value to the base specified in BASETYPE. For example, given the
1477 hierarchy
1479 class L { int ii; };
1480 class A : L { ... };
1481 class B : L { ... };
1482 class C : A, B { ... };
1484 and the declaration
1486 C x;
1488 then the expression
1490 x.A::ii refers to the ii member of the L part of
1491 the A part of the C object named by X. In this case,
1492 DATUM would be x, and BASETYPE would be A.
1494 I used to think that this was nonconformant, that the standard specified
1495 that first we look up ii in A, then convert x to an L& and pull out the
1496 ii part. But in fact, it does say that we convert x to an A&; A here
1497 is known as the "naming class". (jason 2000-12-19)
1499 BINFO_P points to a variable initialized either to NULL_TREE or to the
1500 binfo for the specific base subobject we want to convert to. */
1502 tree
1503 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1505 tree binfo;
1507 if (datum == error_mark_node)
1508 return error_mark_node;
1509 if (*binfo_p)
1510 binfo = *binfo_p;
1511 else
1512 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1513 NULL, tf_warning_or_error);
1515 if (!binfo || binfo == error_mark_node)
1517 *binfo_p = NULL_TREE;
1518 if (!binfo)
1519 error_not_base_type (basetype, TREE_TYPE (datum));
1520 return error_mark_node;
1523 *binfo_p = binfo;
1524 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1525 tf_warning_or_error);
1528 /* Build a reference to an object specified by the C++ `->' operator.
1529 Usually this just involves dereferencing the object, but if the
1530 `->' operator is overloaded, then such overloads must be
1531 performed until an object which does not have the `->' operator
1532 overloaded is found. An error is reported when circular pointer
1533 delegation is detected. */
1535 tree
1536 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1538 tree orig_expr = expr;
1539 tree type = TREE_TYPE (expr);
1540 tree last_rval = NULL_TREE;
1541 vec<tree, va_gc> *types_memoized = NULL;
1543 if (type == error_mark_node)
1544 return error_mark_node;
1546 if (processing_template_decl)
1548 if (type_dependent_expression_p (expr))
1549 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1550 expr = build_non_dependent_expr (expr);
1553 if (MAYBE_CLASS_TYPE_P (type))
1555 struct tinst_level *actual_inst = current_instantiation ();
1556 tree fn = NULL;
1558 while ((expr = build_new_op (loc, COMPONENT_REF,
1559 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1560 &fn, complain)))
1562 if (expr == error_mark_node)
1563 return error_mark_node;
1565 if (fn && DECL_USE_TEMPLATE (fn))
1566 push_tinst_level (fn);
1567 fn = NULL;
1569 if (vec_member (TREE_TYPE (expr), types_memoized))
1571 if (complain & tf_error)
1572 error ("circular pointer delegation detected");
1573 return error_mark_node;
1576 vec_safe_push (types_memoized, TREE_TYPE (expr));
1577 last_rval = expr;
1580 while (current_instantiation () != actual_inst)
1581 pop_tinst_level ();
1583 if (last_rval == NULL_TREE)
1585 if (complain & tf_error)
1586 error ("base operand of %<->%> has non-pointer type %qT", type);
1587 return error_mark_node;
1590 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1591 last_rval = convert_from_reference (last_rval);
1593 else
1594 last_rval = decay_conversion (expr, complain);
1596 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1598 if (processing_template_decl)
1600 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1601 orig_expr);
1602 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1603 return expr;
1606 return cp_build_indirect_ref (last_rval, RO_NULL, complain);
1609 if (complain & tf_error)
1611 if (types_memoized)
1612 error ("result of %<operator->()%> yields non-pointer result");
1613 else
1614 error ("base operand of %<->%> is not a pointer");
1616 return error_mark_node;
1619 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1620 already been checked out to be of aggregate type. */
1622 tree
1623 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1625 tree ptrmem_type;
1626 tree objtype;
1627 tree type;
1628 tree binfo;
1629 tree ctype;
1631 if (error_operand_p (datum) || error_operand_p (component))
1632 return error_mark_node;
1634 datum = mark_lvalue_use (datum);
1635 component = mark_rvalue_use (component);
1637 ptrmem_type = TREE_TYPE (component);
1638 if (!TYPE_PTRMEM_P (ptrmem_type))
1640 if (complain & tf_error)
1641 error ("%qE cannot be used as a member pointer, since it is of "
1642 "type %qT", component, ptrmem_type);
1643 return error_mark_node;
1646 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1647 if (! MAYBE_CLASS_TYPE_P (objtype))
1649 if (complain & tf_error)
1650 error ("cannot apply member pointer %qE to %qE, which is of "
1651 "non-class type %qT", component, datum, objtype);
1652 return error_mark_node;
1655 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1656 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1658 if (!COMPLETE_TYPE_P (ctype))
1660 if (!same_type_p (ctype, objtype))
1661 goto mismatch;
1662 binfo = NULL;
1664 else
1666 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1668 if (!binfo)
1670 mismatch:
1671 if (complain & tf_error)
1672 error ("pointer to member type %qT incompatible with object "
1673 "type %qT", type, objtype);
1674 return error_mark_node;
1676 else if (binfo == error_mark_node)
1677 return error_mark_node;
1680 if (TYPE_PTRDATAMEM_P (ptrmem_type))
1682 cp_lvalue_kind kind = lvalue_kind (datum);
1683 tree ptype;
1685 /* Compute the type of the field, as described in [expr.ref].
1686 There's no such thing as a mutable pointer-to-member, so
1687 things are not as complex as they are for references to
1688 non-static data members. */
1689 type = cp_build_qualified_type (type,
1690 (cp_type_quals (type)
1691 | cp_type_quals (TREE_TYPE (datum))));
1693 datum = build_address (datum);
1695 /* Convert object to the correct base. */
1696 if (binfo)
1698 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
1699 if (datum == error_mark_node)
1700 return error_mark_node;
1703 /* Build an expression for "object + offset" where offset is the
1704 value stored in the pointer-to-data-member. */
1705 ptype = build_pointer_type (type);
1706 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
1707 datum = cp_build_indirect_ref (datum, RO_NULL, complain);
1708 if (datum == error_mark_node)
1709 return error_mark_node;
1711 /* If the object expression was an rvalue, return an rvalue. */
1712 if (kind & clk_class)
1713 datum = rvalue (datum);
1714 else if (kind & clk_rvalueref)
1715 datum = move (datum);
1716 return datum;
1718 else
1720 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1721 program is ill-formed if the second operand is a pointer to member
1722 function with ref-qualifier &. In a .* expression whose object
1723 expression is an lvalue, the program is ill-formed if the second
1724 operand is a pointer to member function with ref-qualifier &&. */
1725 if (FUNCTION_REF_QUALIFIED (type))
1727 bool lval = real_lvalue_p (datum);
1728 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
1730 if (complain & tf_error)
1731 error ("pointer-to-member-function type %qT requires an rvalue",
1732 ptrmem_type);
1733 return error_mark_node;
1735 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
1737 if (complain & tf_error)
1738 error ("pointer-to-member-function type %qT requires an lvalue",
1739 ptrmem_type);
1740 return error_mark_node;
1743 return build2 (OFFSET_REF, type, datum, component);
1747 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1749 tree
1750 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1752 /* This is either a call to a constructor,
1753 or a C cast in C++'s `functional' notation. */
1755 /* The type to which we are casting. */
1756 tree type;
1757 vec<tree, va_gc> *parmvec;
1759 if (exp == error_mark_node || parms == error_mark_node)
1760 return error_mark_node;
1762 if (TREE_CODE (exp) == TYPE_DECL)
1763 type = TREE_TYPE (exp);
1764 else
1765 type = exp;
1767 /* We need to check this explicitly, since value-initialization of
1768 arrays is allowed in other situations. */
1769 if (TREE_CODE (type) == ARRAY_TYPE)
1771 if (complain & tf_error)
1772 error ("functional cast to array type %qT", type);
1773 return error_mark_node;
1776 if (type_uses_auto (type))
1778 if (complain & tf_error)
1779 error ("invalid use of %<auto%>");
1780 return error_mark_node;
1783 if (processing_template_decl)
1785 tree t;
1787 /* Diagnose this even in a template. We could also try harder
1788 to give all the usual errors when the type and args are
1789 non-dependent... */
1790 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1792 if (complain & tf_error)
1793 error ("invalid value-initialization of reference type");
1794 return error_mark_node;
1797 t = build_min (CAST_EXPR, type, parms);
1798 /* We don't know if it will or will not have side effects. */
1799 TREE_SIDE_EFFECTS (t) = 1;
1800 return t;
1803 if (! MAYBE_CLASS_TYPE_P (type))
1805 if (parms == NULL_TREE)
1807 if (VOID_TYPE_P (type))
1808 return void_zero_node;
1809 return build_value_init (cv_unqualified (type), complain);
1812 /* This must build a C cast. */
1813 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1814 return cp_build_c_cast (type, parms, complain);
1817 /* Prepare to evaluate as a call to a constructor. If this expression
1818 is actually used, for example,
1820 return X (arg1, arg2, ...);
1822 then the slot being initialized will be filled in. */
1824 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1825 return error_mark_node;
1826 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
1827 return error_mark_node;
1829 /* [expr.type.conv]
1831 If the expression list is a single-expression, the type
1832 conversion is equivalent (in definedness, and if defined in
1833 meaning) to the corresponding cast expression. */
1834 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1835 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1837 /* [expr.type.conv]
1839 The expression T(), where T is a simple-type-specifier for a
1840 non-array complete object type or the (possibly cv-qualified)
1841 void type, creates an rvalue of the specified type, which is
1842 value-initialized. */
1844 if (parms == NULL_TREE)
1846 exp = build_value_init (type, complain);
1847 exp = get_target_expr_sfinae (exp, complain);
1848 return exp;
1851 /* Call the constructor. */
1852 parmvec = make_tree_vector ();
1853 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
1854 vec_safe_push (parmvec, TREE_VALUE (parms));
1855 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
1856 &parmvec, type, LOOKUP_NORMAL, complain);
1857 release_tree_vector (parmvec);
1859 if (exp == error_mark_node)
1860 return error_mark_node;
1862 return build_cplus_new (type, exp, complain);
1866 /* Add new exception specifier SPEC, to the LIST we currently have.
1867 If it's already in LIST then do nothing.
1868 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1869 know what we're doing. */
1871 tree
1872 add_exception_specifier (tree list, tree spec, int complain)
1874 bool ok;
1875 tree core = spec;
1876 bool is_ptr;
1877 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
1879 if (spec == error_mark_node)
1880 return list;
1882 gcc_assert (spec && (!list || TREE_VALUE (list)));
1884 /* [except.spec] 1, type in an exception specifier shall not be
1885 incomplete, or pointer or ref to incomplete other than pointer
1886 to cv void. */
1887 is_ptr = TYPE_PTR_P (core);
1888 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1889 core = TREE_TYPE (core);
1890 if (complain < 0)
1891 ok = true;
1892 else if (VOID_TYPE_P (core))
1893 ok = is_ptr;
1894 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1895 ok = true;
1896 else if (processing_template_decl)
1897 ok = true;
1898 else
1900 ok = true;
1901 /* 15.4/1 says that types in an exception specifier must be complete,
1902 but it seems more reasonable to only require this on definitions
1903 and calls. So just give a pedwarn at this point; we will give an
1904 error later if we hit one of those two cases. */
1905 if (!COMPLETE_TYPE_P (complete_type (core)))
1906 diag_type = DK_PEDWARN; /* pedwarn */
1909 if (ok)
1911 tree probe;
1913 for (probe = list; probe; probe = TREE_CHAIN (probe))
1914 if (same_type_p (TREE_VALUE (probe), spec))
1915 break;
1916 if (!probe)
1917 list = tree_cons (NULL_TREE, spec, list);
1919 else
1920 diag_type = DK_ERROR; /* error */
1922 if (diag_type != DK_UNSPECIFIED
1923 && (complain & tf_warning_or_error))
1924 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1926 return list;
1929 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
1931 static bool
1932 nothrow_spec_p_uninst (const_tree spec)
1934 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
1935 return false;
1936 return nothrow_spec_p (spec);
1939 /* Combine the two exceptions specifier lists LIST and ADD, and return
1940 their union. If FN is non-null, it's the source of ADD. */
1942 tree
1943 merge_exception_specifiers (tree list, tree add, tree fn)
1945 tree noex, orig_list;
1947 /* No exception-specifier or noexcept(false) are less strict than
1948 anything else. Prefer the newer variant (LIST). */
1949 if (!list || list == noexcept_false_spec)
1950 return list;
1951 else if (!add || add == noexcept_false_spec)
1952 return add;
1954 /* noexcept(true) and throw() are stricter than anything else.
1955 As above, prefer the more recent one (LIST). */
1956 if (nothrow_spec_p_uninst (add))
1957 return list;
1959 noex = TREE_PURPOSE (list);
1960 if (DEFERRED_NOEXCEPT_SPEC_P (add))
1962 /* If ADD is a deferred noexcept, we must have been called from
1963 process_subob_fn. For implicitly declared functions, we build up
1964 a list of functions to consider at instantiation time. */
1965 if (noex && operand_equal_p (noex, boolean_true_node, 0))
1966 noex = NULL_TREE;
1967 gcc_assert (fn && (!noex || is_overloaded_fn (noex)));
1968 noex = build_overload (fn, noex);
1970 else if (nothrow_spec_p_uninst (list))
1971 return add;
1972 else
1973 gcc_checking_assert (!TREE_PURPOSE (add)
1974 || cp_tree_equal (noex, TREE_PURPOSE (add)));
1976 /* Combine the dynamic-exception-specifiers, if any. */
1977 orig_list = list;
1978 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
1980 tree spec = TREE_VALUE (add);
1981 tree probe;
1983 for (probe = orig_list; probe && TREE_VALUE (probe);
1984 probe = TREE_CHAIN (probe))
1985 if (same_type_p (TREE_VALUE (probe), spec))
1986 break;
1987 if (!probe)
1989 spec = build_tree_list (NULL_TREE, spec);
1990 TREE_CHAIN (spec) = list;
1991 list = spec;
1995 /* Keep the noexcept-specifier at the beginning of the list. */
1996 if (noex != TREE_PURPOSE (list))
1997 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
1999 return list;
2002 /* Subroutine of build_call. Ensure that each of the types in the
2003 exception specification is complete. Technically, 15.4/1 says that
2004 they need to be complete when we see a declaration of the function,
2005 but we should be able to get away with only requiring this when the
2006 function is defined or called. See also add_exception_specifier. */
2008 void
2009 require_complete_eh_spec_types (tree fntype, tree decl)
2011 tree raises;
2012 /* Don't complain about calls to op new. */
2013 if (decl && DECL_ARTIFICIAL (decl))
2014 return;
2015 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2016 raises = TREE_CHAIN (raises))
2018 tree type = TREE_VALUE (raises);
2019 if (type && !COMPLETE_TYPE_P (type))
2021 if (decl)
2022 error
2023 ("call to function %qD which throws incomplete type %q#T",
2024 decl, type);
2025 else
2026 error ("call to function which throws incomplete type %q#T",
2027 decl);
2033 #include "gt-cp-typeck2.h"