2013-12-29 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / cp / typeck2.c
blob9fa201dca2c3b57e9d37ab79aa6254bcf3f4713f
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 "stor-layout.h"
34 #include "varasm.h"
35 #include "intl.h"
36 #include "cp-tree.h"
37 #include "flags.h"
38 #include "diagnostic-core.h"
40 static tree
41 process_init_constructor (tree type, tree init, tsubst_flags_t complain);
44 /* Print an error message stemming from an attempt to use
45 BASETYPE as a base class for TYPE. */
47 tree
48 error_not_base_type (tree basetype, tree type)
50 if (TREE_CODE (basetype) == FUNCTION_DECL)
51 basetype = DECL_CONTEXT (basetype);
52 error ("type %qT is not a base type for type %qT", basetype, type);
53 return error_mark_node;
56 tree
57 binfo_or_else (tree base, tree type)
59 tree binfo = lookup_base (type, base, ba_unique,
60 NULL, tf_warning_or_error);
62 if (binfo == error_mark_node)
63 return NULL_TREE;
64 else if (!binfo)
65 error_not_base_type (base, type);
66 return binfo;
69 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
70 value may not be changed thereafter. */
72 void
73 cxx_readonly_error (tree arg, enum lvalue_use errstring)
76 /* This macro is used to emit diagnostics to ensure that all format
77 strings are complete sentences, visible to gettext and checked at
78 compile time. */
80 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
81 do { \
82 switch (errstring) \
83 { \
84 case lv_assign: \
85 error(AS, ARG); \
86 break; \
87 case lv_asm: \
88 error(ASM, ARG); \
89 break; \
90 case lv_increment: \
91 error (IN, ARG); \
92 break; \
93 case lv_decrement: \
94 error (DE, ARG); \
95 break; \
96 default: \
97 gcc_unreachable (); \
98 } \
99 } while (0)
101 /* Handle C++-specific things first. */
103 if (VAR_P (arg)
104 && DECL_LANG_SPECIFIC (arg)
105 && DECL_IN_AGGR_P (arg)
106 && !TREE_STATIC (arg))
107 ERROR_FOR_ASSIGNMENT (G_("assignment of "
108 "constant field %qD"),
109 G_("constant field %qD "
110 "used as %<asm%> output"),
111 G_("increment of "
112 "constant field %qD"),
113 G_("decrement of "
114 "constant field %qD"),
115 arg);
116 else if (INDIRECT_REF_P (arg)
117 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
118 && (VAR_P (TREE_OPERAND (arg, 0))
119 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
120 ERROR_FOR_ASSIGNMENT (G_("assignment of "
121 "read-only reference %qD"),
122 G_("read-only reference %qD "
123 "used as %<asm%> output"),
124 G_("increment of "
125 "read-only reference %qD"),
126 G_("decrement of "
127 "read-only reference %qD"),
128 TREE_OPERAND (arg, 0));
129 else
130 readonly_error (input_location, arg, errstring);
134 /* Structure that holds information about declarations whose type was
135 incomplete and we could not check whether it was abstract or not. */
137 struct GTY((chain_next ("%h.next"))) pending_abstract_type {
138 /* Declaration which we are checking for abstractness. It is either
139 a DECL node, or an IDENTIFIER_NODE if we do not have a full
140 declaration available. */
141 tree decl;
143 /* Type which will be checked for abstractness. */
144 tree type;
146 /* Kind of use in an unnamed declarator. */
147 abstract_class_use use;
149 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
150 because DECLs already carry locus information. */
151 location_t locus;
153 /* Link to the next element in list. */
154 struct pending_abstract_type* next;
158 /* Compute the hash value of the node VAL. This function is used by the
159 hash table abstract_pending_vars. */
161 static hashval_t
162 pat_calc_hash (const void* val)
164 const struct pending_abstract_type *pat =
165 (const struct pending_abstract_type *) val;
166 return (hashval_t) TYPE_UID (pat->type);
170 /* Compare node VAL1 with the type VAL2. This function is used by the
171 hash table abstract_pending_vars. */
173 static int
174 pat_compare (const void* val1, const void* val2)
176 const struct pending_abstract_type *const pat1 =
177 (const struct pending_abstract_type *) val1;
178 const_tree const type2 = (const_tree)val2;
180 return (pat1->type == type2);
183 /* Hash table that maintains pending_abstract_type nodes, for which we still
184 need to check for type abstractness. The key of the table is the type
185 of the declaration. */
186 static GTY ((param_is (struct pending_abstract_type)))
187 htab_t abstract_pending_vars = NULL;
189 static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t);
191 /* This function is called after TYPE is completed, and will check if there
192 are pending declarations for which we still need to verify the abstractness
193 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
194 turned out to be incomplete. */
196 void
197 complete_type_check_abstract (tree type)
199 void **slot;
200 struct pending_abstract_type *pat;
201 location_t cur_loc = input_location;
203 gcc_assert (COMPLETE_TYPE_P (type));
205 if (!abstract_pending_vars)
206 return;
208 /* Retrieve the list of pending declarations for this type. */
209 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
210 (hashval_t)TYPE_UID (type), NO_INSERT);
211 if (!slot)
212 return;
213 pat = (struct pending_abstract_type*)*slot;
214 gcc_assert (pat);
216 /* If the type is not abstract, do not do anything. */
217 if (CLASSTYPE_PURE_VIRTUALS (type))
219 struct pending_abstract_type *prev = 0, *next;
221 /* Reverse the list to emit the errors in top-down order. */
222 for (; pat; pat = next)
224 next = pat->next;
225 pat->next = prev;
226 prev = pat;
228 pat = prev;
230 /* Go through the list, and call abstract_virtuals_error for each
231 element: it will issue a diagnostic if the type is abstract. */
232 while (pat)
234 gcc_assert (type == pat->type);
236 /* Tweak input_location so that the diagnostic appears at the correct
237 location. Notice that this is only needed if the decl is an
238 IDENTIFIER_NODE. */
239 input_location = pat->locus;
240 abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use,
241 tf_warning_or_error);
242 pat = pat->next;
246 htab_clear_slot (abstract_pending_vars, slot);
248 input_location = cur_loc;
252 /* If TYPE has abstract virtual functions, issue an error about trying
253 to create an object of that type. DECL is the object declared, or
254 NULL_TREE if the declaration is unavailable, in which case USE specifies
255 the kind of invalid use. Returns 1 if an error occurred; zero if
256 all was well. */
258 static int
259 abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use,
260 tsubst_flags_t complain)
262 vec<tree, va_gc> *pure;
264 /* This function applies only to classes. Any other entity can never
265 be abstract. */
266 if (!CLASS_TYPE_P (type))
267 return 0;
268 type = TYPE_MAIN_VARIANT (type);
270 #if 0
271 /* Instantiation here seems to be required by the standard,
272 but breaks e.g. boost::bind. FIXME! */
273 /* In SFINAE, non-N3276 context, force instantiation. */
274 if (!(complain & (tf_error|tf_decltype)))
275 complete_type (type);
276 #endif
278 /* If the type is incomplete, we register it within a hash table,
279 so that we can check again once it is completed. This makes sense
280 only for objects for which we have a declaration or at least a
281 name. */
282 if (!COMPLETE_TYPE_P (type) && (complain & tf_error))
284 void **slot;
285 struct pending_abstract_type *pat;
287 gcc_assert (!decl || DECL_P (decl) || identifier_p (decl));
289 if (!abstract_pending_vars)
290 abstract_pending_vars = htab_create_ggc (31, &pat_calc_hash,
291 &pat_compare, NULL);
293 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
294 (hashval_t)TYPE_UID (type), INSERT);
296 pat = ggc_alloc_pending_abstract_type ();
297 pat->type = type;
298 pat->decl = decl;
299 pat->use = use;
300 pat->locus = ((decl && DECL_P (decl))
301 ? DECL_SOURCE_LOCATION (decl)
302 : input_location);
304 pat->next = (struct pending_abstract_type *) *slot;
305 *slot = pat;
307 return 0;
310 if (!TYPE_SIZE (type))
311 /* TYPE is being defined, and during that time
312 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
313 return 0;
315 pure = CLASSTYPE_PURE_VIRTUALS (type);
316 if (!pure)
317 return 0;
319 if (!(complain & tf_error))
320 return 1;
322 if (decl)
324 if (VAR_P (decl))
325 error ("cannot declare variable %q+D to be of abstract "
326 "type %qT", decl, type);
327 else if (TREE_CODE (decl) == PARM_DECL)
329 if (DECL_NAME (decl))
330 error ("cannot declare parameter %q+D to be of abstract type %qT",
331 decl, type);
332 else
333 error ("cannot declare parameter to be of abstract type %qT",
334 type);
336 else if (TREE_CODE (decl) == FIELD_DECL)
337 error ("cannot declare field %q+D to be of abstract type %qT",
338 decl, type);
339 else if (TREE_CODE (decl) == FUNCTION_DECL
340 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
341 error ("invalid abstract return type for member function %q+#D", decl);
342 else if (TREE_CODE (decl) == FUNCTION_DECL)
343 error ("invalid abstract return type for function %q+#D", decl);
344 else if (identifier_p (decl))
345 /* Here we do not have location information. */
346 error ("invalid abstract type %qT for %qE", type, decl);
347 else
348 error ("invalid abstract type for %q+D", decl);
350 else switch (use)
352 case ACU_ARRAY:
353 error ("creating array of %qT, which is an abstract class type", type);
354 break;
355 case ACU_CAST:
356 error ("invalid cast to abstract class type %qT", type);
357 break;
358 case ACU_NEW:
359 error ("invalid new-expression of abstract class type %qT", type);
360 break;
361 case ACU_RETURN:
362 error ("invalid abstract return type %qT", type);
363 break;
364 case ACU_PARM:
365 error ("invalid abstract parameter type %qT", type);
366 break;
367 case ACU_THROW:
368 error ("expression of abstract class type %qT cannot "
369 "be used in throw-expression", type);
370 break;
371 case ACU_CATCH:
372 error ("cannot declare catch parameter to be of abstract "
373 "class type %qT", type);
374 break;
375 default:
376 error ("cannot allocate an object of abstract type %qT", type);
379 /* Only go through this once. */
380 if (pure->length ())
382 unsigned ix;
383 tree fn;
385 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
386 " because the following virtual functions are pure within %qT:",
387 type);
389 FOR_EACH_VEC_ELT (*pure, ix, fn)
390 if (! DECL_CLONED_FUNCTION_P (fn)
391 || DECL_COMPLETE_DESTRUCTOR_P (fn))
392 inform (input_location, "\t%+#D", fn);
394 /* Now truncate the vector. This leaves it non-null, so we know
395 there are pure virtuals, but empty so we don't list them out
396 again. */
397 pure->truncate (0);
400 return 1;
404 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
406 return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain);
410 abstract_virtuals_error_sfinae (abstract_class_use use, tree type,
411 tsubst_flags_t complain)
413 return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain);
417 /* Wrapper for the above function in the common case of wanting errors. */
420 abstract_virtuals_error (tree decl, tree type)
422 return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
426 abstract_virtuals_error (abstract_class_use use, tree type)
428 return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
431 /* Print an error message for invalid use of an incomplete type.
432 VALUE is the expression that was used (or 0 if that isn't known)
433 and TYPE is the type that was invalid. DIAG_KIND indicates the
434 type of diagnostic (see diagnostic.def). */
436 void
437 cxx_incomplete_type_diagnostic (const_tree value, const_tree type,
438 diagnostic_t diag_kind)
440 int decl = 0;
442 gcc_assert (diag_kind == DK_WARNING
443 || diag_kind == DK_PEDWARN
444 || diag_kind == DK_ERROR);
446 /* Avoid duplicate error message. */
447 if (TREE_CODE (type) == ERROR_MARK)
448 return;
450 if (value != 0 && (VAR_P (value)
451 || TREE_CODE (value) == PARM_DECL
452 || TREE_CODE (value) == FIELD_DECL))
454 emit_diagnostic (diag_kind, input_location, 0,
455 "%q+D has incomplete type", value);
456 decl = 1;
458 retry:
459 /* We must print an error message. Be clever about what it says. */
461 switch (TREE_CODE (type))
463 case RECORD_TYPE:
464 case UNION_TYPE:
465 case ENUMERAL_TYPE:
466 if (!decl)
467 emit_diagnostic (diag_kind, input_location, 0,
468 "invalid use of incomplete type %q#T", type);
469 if (!TYPE_TEMPLATE_INFO (type))
470 emit_diagnostic (diag_kind, input_location, 0,
471 "forward declaration of %q+#T", type);
472 else
473 emit_diagnostic (diag_kind, input_location, 0,
474 "declaration of %q+#T", type);
475 break;
477 case VOID_TYPE:
478 emit_diagnostic (diag_kind, input_location, 0,
479 "invalid use of %qT", type);
480 break;
482 case ARRAY_TYPE:
483 if (TYPE_DOMAIN (type))
485 type = TREE_TYPE (type);
486 goto retry;
488 emit_diagnostic (diag_kind, input_location, 0,
489 "invalid use of array with unspecified bounds");
490 break;
492 case OFFSET_TYPE:
493 bad_member:
495 tree member = TREE_OPERAND (value, 1);
496 if (is_overloaded_fn (member))
497 member = get_first_fn (member);
498 if (DECL_FUNCTION_MEMBER_P (member)
499 && ! flag_ms_extensions)
500 emit_diagnostic (diag_kind, input_location, 0,
501 "invalid use of member function "
502 "(did you forget the %<()%> ?)");
503 else
504 emit_diagnostic (diag_kind, input_location, 0,
505 "invalid use of member "
506 "(did you forget the %<&%> ?)");
508 break;
510 case TEMPLATE_TYPE_PARM:
511 if (is_auto (type))
512 emit_diagnostic (diag_kind, input_location, 0,
513 "invalid use of %<auto%>");
514 else
515 emit_diagnostic (diag_kind, input_location, 0,
516 "invalid use of template type parameter %qT", type);
517 break;
519 case BOUND_TEMPLATE_TEMPLATE_PARM:
520 emit_diagnostic (diag_kind, input_location, 0,
521 "invalid use of template template parameter %qT",
522 TYPE_NAME (type));
523 break;
525 case TYPENAME_TYPE:
526 emit_diagnostic (diag_kind, input_location, 0,
527 "invalid use of dependent type %qT", type);
528 break;
530 case LANG_TYPE:
531 if (type == init_list_type_node)
533 emit_diagnostic (diag_kind, input_location, 0,
534 "invalid use of brace-enclosed initializer list");
535 break;
537 gcc_assert (type == unknown_type_node);
538 if (value && TREE_CODE (value) == COMPONENT_REF)
539 goto bad_member;
540 else if (value && TREE_CODE (value) == ADDR_EXPR)
541 emit_diagnostic (diag_kind, input_location, 0,
542 "address of overloaded function with no contextual "
543 "type information");
544 else if (value && TREE_CODE (value) == OVERLOAD)
545 emit_diagnostic (diag_kind, input_location, 0,
546 "overloaded function with no contextual type information");
547 else
548 emit_diagnostic (diag_kind, input_location, 0,
549 "insufficient contextual information to determine type");
550 break;
552 default:
553 gcc_unreachable ();
557 /* Backward-compatibility interface to incomplete_type_diagnostic;
558 required by ../tree.c. */
559 #undef cxx_incomplete_type_error
560 void
561 cxx_incomplete_type_error (const_tree value, const_tree type)
563 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
567 /* The recursive part of split_nonconstant_init. DEST is an lvalue
568 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
569 Return true if the whole of the value was initialized by the
570 generated statements. */
572 static bool
573 split_nonconstant_init_1 (tree dest, tree init)
575 unsigned HOST_WIDE_INT idx;
576 tree field_index, value;
577 tree type = TREE_TYPE (dest);
578 tree inner_type = NULL;
579 bool array_type_p = false;
580 bool complete_p = true;
581 HOST_WIDE_INT num_split_elts = 0;
583 switch (TREE_CODE (type))
585 case ARRAY_TYPE:
586 inner_type = TREE_TYPE (type);
587 array_type_p = true;
588 /* FALLTHRU */
590 case RECORD_TYPE:
591 case UNION_TYPE:
592 case QUAL_UNION_TYPE:
593 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
594 field_index, value)
596 /* The current implementation of this algorithm assumes that
597 the field was set for all the elements. This is usually done
598 by process_init_constructor. */
599 gcc_assert (field_index);
601 if (!array_type_p)
602 inner_type = TREE_TYPE (field_index);
604 if (TREE_CODE (value) == CONSTRUCTOR)
606 tree sub;
608 if (array_type_p)
609 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
610 NULL_TREE, NULL_TREE);
611 else
612 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
613 NULL_TREE);
615 if (!split_nonconstant_init_1 (sub, value))
616 complete_p = false;
617 num_split_elts++;
619 else if (!initializer_constant_valid_p (value, inner_type))
621 tree code;
622 tree sub;
624 /* FIXME: Ordered removal is O(1) so the whole function is
625 worst-case quadratic. This could be fixed using an aside
626 bitmap to record which elements must be removed and remove
627 them all at the same time. Or by merging
628 split_non_constant_init into process_init_constructor_array,
629 that is separating constants from non-constants while building
630 the vector. */
631 CONSTRUCTOR_ELTS (init)->ordered_remove (idx);
632 --idx;
634 if (array_type_p)
635 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
636 NULL_TREE, NULL_TREE);
637 else
638 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
639 NULL_TREE);
641 code = build2 (INIT_EXPR, inner_type, sub, value);
642 code = build_stmt (input_location, EXPR_STMT, code);
643 code = maybe_cleanup_point_expr_void (code);
644 add_stmt (code);
645 if (type_build_dtor_call (inner_type))
647 code = (build_special_member_call
648 (sub, complete_dtor_identifier, NULL, inner_type,
649 LOOKUP_NORMAL, tf_warning_or_error));
650 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type))
651 finish_eh_cleanup (code);
654 num_split_elts++;
657 break;
659 case VECTOR_TYPE:
660 if (!initializer_constant_valid_p (init, type))
662 tree code;
663 tree cons = copy_node (init);
664 CONSTRUCTOR_ELTS (init) = NULL;
665 code = build2 (MODIFY_EXPR, type, dest, cons);
666 code = build_stmt (input_location, EXPR_STMT, code);
667 add_stmt (code);
668 num_split_elts += CONSTRUCTOR_NELTS (init);
670 break;
672 default:
673 gcc_unreachable ();
676 /* The rest of the initializer is now a constant. */
677 TREE_CONSTANT (init) = 1;
678 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
679 num_split_elts, inner_type);
682 /* A subroutine of store_init_value. Splits non-constant static
683 initializer INIT into a constant part and generates code to
684 perform the non-constant part of the initialization to DEST.
685 Returns the code for the runtime init. */
687 static tree
688 split_nonconstant_init (tree dest, tree init)
690 tree code;
692 if (TREE_CODE (init) == CONSTRUCTOR)
694 code = push_stmt_list ();
695 if (split_nonconstant_init_1 (dest, init))
696 init = NULL_TREE;
697 code = pop_stmt_list (code);
698 DECL_INITIAL (dest) = init;
699 TREE_READONLY (dest) = 0;
701 else
702 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
704 return code;
707 /* Perform appropriate conversions on the initial value of a variable,
708 store it in the declaration DECL,
709 and print any error messages that are appropriate.
710 If the init is invalid, store an ERROR_MARK.
712 C++: Note that INIT might be a TREE_LIST, which would mean that it is
713 a base class initializer for some aggregate type, hopefully compatible
714 with DECL. If INIT is a single element, and DECL is an aggregate
715 type, we silently convert INIT into a TREE_LIST, allowing a constructor
716 to be called.
718 If INIT is a TREE_LIST and there is no constructor, turn INIT
719 into a CONSTRUCTOR and use standard initialization techniques.
720 Perhaps a warning should be generated?
722 Returns code to be executed if initialization could not be performed
723 for static variable. In that case, caller must emit the code. */
725 tree
726 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
728 tree value, type;
730 /* If variable's type was invalidly declared, just ignore it. */
732 type = TREE_TYPE (decl);
733 if (TREE_CODE (type) == ERROR_MARK)
734 return NULL_TREE;
736 if (MAYBE_CLASS_TYPE_P (type))
738 if (TREE_CODE (init) == TREE_LIST)
740 error ("constructor syntax used, but no constructor declared "
741 "for type %qT", type);
742 init = build_constructor_from_list (init_list_type_node, nreverse (init));
745 else if (TREE_CODE (init) == TREE_LIST
746 && TREE_TYPE (init) != unknown_type_node)
748 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
750 if (TREE_CODE (init) == TREE_LIST
751 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
753 error ("cannot initialize arrays using this syntax");
754 return NULL_TREE;
756 else
757 /* We get here with code like `int a (2);' */
758 init = build_x_compound_expr_from_list (init, ELK_INIT,
759 tf_warning_or_error);
762 /* End of special C++ code. */
764 if (flags & LOOKUP_ALREADY_DIGESTED)
765 value = init;
766 else
767 /* Digest the specified initializer into an expression. */
768 value = digest_init_flags (type, init, flags);
770 value = extend_ref_init_temps (decl, value, cleanups);
772 /* In C++0x constant expression is a semantic, not syntactic, property.
773 In C++98, make sure that what we thought was a constant expression at
774 template definition time is still constant and otherwise perform this
775 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
776 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
778 bool const_init;
779 value = fold_non_dependent_expr (value);
780 if (DECL_DECLARED_CONSTEXPR_P (decl)
781 || DECL_IN_AGGR_P (decl))
783 /* Diagnose a non-constant initializer for constexpr. */
784 if (processing_template_decl
785 && !require_potential_constant_expression (value))
786 value = error_mark_node;
787 else
788 value = cxx_constant_value (value);
790 value = maybe_constant_init (value);
791 const_init = (reduced_constant_expression_p (value)
792 || error_operand_p (value));
793 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
794 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
797 /* If the initializer is not a constant, fill in DECL_INITIAL with
798 the bits that are constant, and then return an expression that
799 will perform the dynamic initialization. */
800 if (value != error_mark_node
801 && (TREE_SIDE_EFFECTS (value)
802 || array_of_runtime_bound_p (type)
803 || ! reduced_constant_expression_p (value)))
805 if (TREE_CODE (type) == ARRAY_TYPE
806 && (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type))
807 || array_of_runtime_bound_p (type)))
808 /* For an array, we only need/want a single cleanup region rather
809 than one per element. */
810 return build_vec_init (decl, NULL_TREE, value, false, 1,
811 tf_warning_or_error);
812 else
813 return split_nonconstant_init (decl, value);
815 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
816 is an automatic variable, the middle end will turn this into a
817 dynamic initialization later. */
818 DECL_INITIAL (decl) = value;
819 return NULL_TREE;
823 /* Give errors about narrowing conversions within { }. */
825 void
826 check_narrowing (tree type, tree init)
828 tree ftype = unlowered_expr_type (init);
829 bool ok = true;
830 REAL_VALUE_TYPE d;
832 if (!warn_narrowing || !ARITHMETIC_TYPE_P (type))
833 return;
835 if (BRACE_ENCLOSED_INITIALIZER_P (init)
836 && TREE_CODE (type) == COMPLEX_TYPE)
838 tree elttype = TREE_TYPE (type);
839 if (CONSTRUCTOR_NELTS (init) > 0)
840 check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value);
841 if (CONSTRUCTOR_NELTS (init) > 1)
842 check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value);
843 return;
846 init = maybe_constant_value (init);
848 if (TREE_CODE (type) == INTEGER_TYPE
849 && TREE_CODE (ftype) == REAL_TYPE)
850 ok = false;
851 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
852 && CP_INTEGRAL_TYPE_P (type))
854 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
855 /* Check for narrowing based on the values of the enumeration. */
856 ftype = ENUM_UNDERLYING_TYPE (ftype);
857 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
858 TYPE_MAX_VALUE (ftype))
859 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
860 TYPE_MIN_VALUE (type)))
861 && (TREE_CODE (init) != INTEGER_CST
862 || !int_fits_type_p (init, type)))
863 ok = false;
865 else if (TREE_CODE (ftype) == REAL_TYPE
866 && TREE_CODE (type) == REAL_TYPE)
868 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
870 if (TREE_CODE (init) == REAL_CST)
872 /* Issue 703: Loss of precision is OK as long as the value is
873 within the representable range of the new type. */
874 REAL_VALUE_TYPE r;
875 d = TREE_REAL_CST (init);
876 real_convert (&r, TYPE_MODE (type), &d);
877 if (real_isinf (&r))
878 ok = false;
880 else
881 ok = false;
884 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
885 && TREE_CODE (type) == REAL_TYPE)
887 ok = false;
888 if (TREE_CODE (init) == INTEGER_CST)
890 d = real_value_from_int_cst (0, init);
891 if (exact_real_truncate (TYPE_MODE (type), &d))
892 ok = true;
896 if (!ok)
898 if (cxx_dialect >= cxx11)
899 pedwarn (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
900 "narrowing conversion of %qE from %qT to %qT inside { }",
901 init, ftype, type);
902 else
903 warning_at (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
904 "narrowing conversion of %qE from %qT to %qT inside { } "
905 "is ill-formed in C++11", init, ftype, type);
909 /* Process the initializer INIT for a variable of type TYPE, emitting
910 diagnostics for invalid initializers and converting the initializer as
911 appropriate.
913 For aggregate types, it assumes that reshape_init has already run, thus the
914 initializer will have the right shape (brace elision has been undone).
916 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
918 static tree
919 digest_init_r (tree type, tree init, bool nested, int flags,
920 tsubst_flags_t complain)
922 enum tree_code code = TREE_CODE (type);
924 if (error_operand_p (init))
925 return error_mark_node;
927 gcc_assert (init);
929 /* We must strip the outermost array type when completing the type,
930 because the its bounds might be incomplete at the moment. */
931 if (!complete_type_or_maybe_complain (TREE_CODE (type) == ARRAY_TYPE
932 ? TREE_TYPE (type) : type, NULL_TREE,
933 complain))
934 return error_mark_node;
936 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
937 (g++.old-deja/g++.law/casts2.C). */
938 if (TREE_CODE (init) == NON_LVALUE_EXPR)
939 init = TREE_OPERAND (init, 0);
941 /* Initialization of an array of chars from a string constant. The initializer
942 can be optionally enclosed in braces, but reshape_init has already removed
943 them if they were present. */
944 if (code == ARRAY_TYPE)
946 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
947 if (char_type_p (typ1)
948 /*&& init */
949 && TREE_CODE (init) == STRING_CST)
951 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
953 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
955 if (char_type != char_type_node)
957 if (complain & tf_error)
958 error ("char-array initialized from wide string");
959 return error_mark_node;
962 else
964 if (char_type == char_type_node)
966 if (complain & tf_error)
967 error ("int-array initialized from non-wide string");
968 return error_mark_node;
970 else if (char_type != typ1)
972 if (complain & tf_error)
973 error ("int-array initialized from incompatible "
974 "wide string");
975 return error_mark_node;
979 if (type != TREE_TYPE (init))
981 init = copy_node (init);
982 TREE_TYPE (init) = type;
984 if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
986 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
987 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
988 /* In C it is ok to subtract 1 from the length of the string
989 because it's ok to ignore the terminating null char that is
990 counted in the length of the constant, but in C++ this would
991 be invalid. */
992 if (size < TREE_STRING_LENGTH (init))
993 permerror (input_location, "initializer-string for array "
994 "of chars is too long");
996 return init;
1000 /* Handle scalar types (including conversions) and references. */
1001 if ((TREE_CODE (type) != COMPLEX_TYPE
1002 || BRACE_ENCLOSED_INITIALIZER_P (init))
1003 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1005 tree *exp;
1007 if (nested)
1008 check_narrowing (type, init);
1009 init = convert_for_initialization (0, type, init, flags,
1010 ICR_INIT, NULL_TREE, 0,
1011 complain);
1012 exp = &init;
1014 /* Skip any conversions since we'll be outputting the underlying
1015 constant. */
1016 while (CONVERT_EXPR_P (*exp)
1017 || TREE_CODE (*exp) == NON_LVALUE_EXPR)
1018 exp = &TREE_OPERAND (*exp, 0);
1020 *exp = cplus_expand_constant (*exp);
1022 return init;
1025 /* Come here only for aggregates: records, arrays, unions, complex numbers
1026 and vectors. */
1027 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1028 || TREE_CODE (type) == VECTOR_TYPE
1029 || TREE_CODE (type) == RECORD_TYPE
1030 || TREE_CODE (type) == UNION_TYPE
1031 || TREE_CODE (type) == COMPLEX_TYPE);
1033 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1034 && !TYPE_NON_AGGREGATE_CLASS (type))
1035 return process_init_constructor (type, init, complain);
1036 else
1038 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
1040 if (complain & tf_error)
1041 error ("cannot initialize aggregate of type %qT with "
1042 "a compound literal", type);
1044 return error_mark_node;
1047 if (TREE_CODE (type) == ARRAY_TYPE
1048 && !BRACE_ENCLOSED_INITIALIZER_P (init))
1050 /* Allow the result of build_array_copy and of
1051 build_value_init_noctor. */
1052 if ((TREE_CODE (init) == VEC_INIT_EXPR
1053 || TREE_CODE (init) == CONSTRUCTOR)
1054 && (same_type_ignoring_top_level_qualifiers_p
1055 (type, TREE_TYPE (init))))
1056 return init;
1058 if (complain & tf_error)
1059 error ("array must be initialized with a brace-enclosed"
1060 " initializer");
1061 return error_mark_node;
1064 return convert_for_initialization (NULL_TREE, type, init,
1065 flags,
1066 ICR_INIT, NULL_TREE, 0,
1067 complain);
1071 tree
1072 digest_init (tree type, tree init, tsubst_flags_t complain)
1074 return digest_init_r (type, init, false, LOOKUP_IMPLICIT, complain);
1077 tree
1078 digest_init_flags (tree type, tree init, int flags)
1080 return digest_init_r (type, init, false, flags, tf_warning_or_error);
1083 /* Set of flags used within process_init_constructor to describe the
1084 initializers. */
1085 #define PICFLAG_ERRONEOUS 1
1086 #define PICFLAG_NOT_ALL_CONSTANT 2
1087 #define PICFLAG_NOT_ALL_SIMPLE 4
1089 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1090 describe it. */
1092 static int
1093 picflag_from_initializer (tree init)
1095 if (init == error_mark_node)
1096 return PICFLAG_ERRONEOUS;
1097 else if (!TREE_CONSTANT (init))
1098 return PICFLAG_NOT_ALL_CONSTANT;
1099 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1100 return PICFLAG_NOT_ALL_SIMPLE;
1101 return 0;
1104 /* Subroutine of process_init_constructor, which will process an initializer
1105 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1106 which describe the initializers. */
1108 static int
1109 process_init_constructor_array (tree type, tree init,
1110 tsubst_flags_t complain)
1112 unsigned HOST_WIDE_INT i, len = 0;
1113 int flags = 0;
1114 bool unbounded = false;
1115 constructor_elt *ce;
1116 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1118 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1119 || TREE_CODE (type) == VECTOR_TYPE);
1121 if (TREE_CODE (type) == ARRAY_TYPE)
1123 tree domain = TYPE_DOMAIN (type);
1124 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1125 len = (tree_to_double_int (TYPE_MAX_VALUE (domain))
1126 - tree_to_double_int (TYPE_MIN_VALUE (domain))
1127 + double_int_one)
1128 .ext (TYPE_PRECISION (TREE_TYPE (domain)),
1129 TYPE_UNSIGNED (TREE_TYPE (domain)))
1130 .low;
1131 else
1132 unbounded = true; /* Take as many as there are. */
1134 else
1135 /* Vectors are like simple fixed-size arrays. */
1136 len = TYPE_VECTOR_SUBPARTS (type);
1138 /* There must not be more initializers than needed. */
1139 if (!unbounded && vec_safe_length (v) > len)
1141 if (complain & tf_error)
1142 error ("too many initializers for %qT", type);
1143 else
1144 return PICFLAG_ERRONEOUS;
1147 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1149 if (ce->index)
1151 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1152 if (compare_tree_int (ce->index, i) != 0)
1154 ce->value = error_mark_node;
1155 sorry ("non-trivial designated initializers not supported");
1158 else
1159 ce->index = size_int (i);
1160 gcc_assert (ce->value);
1161 ce->value = digest_init_r (TREE_TYPE (type), ce->value, true,
1162 LOOKUP_IMPLICIT, complain);
1164 if (ce->value != error_mark_node)
1165 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1166 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1168 flags |= picflag_from_initializer (ce->value);
1171 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1172 we must add initializers ourselves. */
1173 if (!unbounded)
1174 for (; i < len; ++i)
1176 tree next;
1178 if (type_build_ctor_call (TREE_TYPE (type)))
1180 /* If this type needs constructors run for default-initialization,
1181 we can't rely on the back end to do it for us, so make the
1182 initialization explicit by list-initializing from {}. */
1183 next = build_constructor (init_list_type_node, NULL);
1184 next = digest_init (TREE_TYPE (type), next, complain);
1186 else if (!zero_init_p (TREE_TYPE (type)))
1187 next = build_zero_init (TREE_TYPE (type),
1188 /*nelts=*/NULL_TREE,
1189 /*static_storage_p=*/false);
1190 else
1191 /* The default zero-initialization is fine for us; don't
1192 add anything to the CONSTRUCTOR. */
1193 break;
1195 flags |= picflag_from_initializer (next);
1196 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1199 CONSTRUCTOR_ELTS (init) = v;
1200 return flags;
1203 /* Subroutine of process_init_constructor, which will process an initializer
1204 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1205 the initializers. */
1207 static int
1208 process_init_constructor_record (tree type, tree init,
1209 tsubst_flags_t complain)
1211 vec<constructor_elt, va_gc> *v = NULL;
1212 int flags = 0;
1213 tree field;
1214 unsigned HOST_WIDE_INT idx = 0;
1216 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1217 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1218 gcc_assert (!TYPE_BINFO (type)
1219 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1220 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1222 /* Generally, we will always have an index for each initializer (which is
1223 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1224 reshape_init. So we need to handle both cases. */
1225 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1227 tree next;
1228 tree type;
1230 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1232 flags |= picflag_from_initializer (integer_zero_node);
1233 CONSTRUCTOR_APPEND_ELT (v, field, integer_zero_node);
1234 continue;
1237 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1238 continue;
1240 /* If this is a bitfield, first convert to the declared type. */
1241 type = TREE_TYPE (field);
1242 if (DECL_BIT_FIELD_TYPE (field))
1243 type = DECL_BIT_FIELD_TYPE (field);
1244 if (type == error_mark_node)
1245 return PICFLAG_ERRONEOUS;
1247 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1249 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1250 if (ce->index)
1252 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1253 latter case can happen in templates where lookup has to be
1254 deferred. */
1255 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1256 || identifier_p (ce->index));
1257 if (ce->index != field
1258 && ce->index != DECL_NAME (field))
1260 ce->value = error_mark_node;
1261 sorry ("non-trivial designated initializers not supported");
1265 gcc_assert (ce->value);
1266 next = digest_init_r (type, ce->value, true,
1267 LOOKUP_IMPLICIT, complain);
1268 ++idx;
1270 else if (type_build_ctor_call (TREE_TYPE (field)))
1272 /* If this type needs constructors run for
1273 default-initialization, we can't rely on the back end to do it
1274 for us, so build up TARGET_EXPRs. If the type in question is
1275 a class, just build one up; if it's an array, recurse. */
1276 next = build_constructor (init_list_type_node, NULL);
1277 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field)))
1279 next = finish_compound_literal (TREE_TYPE (field), next,
1280 complain);
1281 /* direct-initialize the target. No temporary is going
1282 to be involved. */
1283 if (TREE_CODE (next) == TARGET_EXPR)
1284 TARGET_EXPR_DIRECT_INIT_P (next) = true;
1287 next = digest_init_r (TREE_TYPE (field), next, true,
1288 LOOKUP_IMPLICIT, complain);
1290 /* Warn when some struct elements are implicitly initialized. */
1291 warning (OPT_Wmissing_field_initializers,
1292 "missing initializer for member %qD", field);
1294 else
1296 if (TREE_READONLY (field))
1298 if (complain & tf_error)
1299 error ("uninitialized const member %qD", field);
1300 else
1301 return PICFLAG_ERRONEOUS;
1303 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1305 if (complain & tf_error)
1306 error ("member %qD with uninitialized const fields", field);
1307 else
1308 return PICFLAG_ERRONEOUS;
1310 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1312 if (complain & tf_error)
1313 error ("member %qD is uninitialized reference", field);
1314 else
1315 return PICFLAG_ERRONEOUS;
1318 /* Warn when some struct elements are implicitly initialized
1319 to zero. */
1320 warning (OPT_Wmissing_field_initializers,
1321 "missing initializer for member %qD", field);
1323 if (!zero_init_p (TREE_TYPE (field)))
1324 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1325 /*static_storage_p=*/false);
1326 else
1327 /* The default zero-initialization is fine for us; don't
1328 add anything to the CONSTRUCTOR. */
1329 continue;
1332 /* If this is a bitfield, now convert to the lowered type. */
1333 if (type != TREE_TYPE (field))
1334 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1335 flags |= picflag_from_initializer (next);
1336 CONSTRUCTOR_APPEND_ELT (v, field, next);
1339 if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1341 if (complain & tf_error)
1342 error ("too many initializers for %qT", type);
1343 else
1344 return PICFLAG_ERRONEOUS;
1347 CONSTRUCTOR_ELTS (init) = v;
1348 return flags;
1351 /* Subroutine of process_init_constructor, which will process a single
1352 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1353 which describe the initializer. */
1355 static int
1356 process_init_constructor_union (tree type, tree init,
1357 tsubst_flags_t complain)
1359 constructor_elt *ce;
1360 int len;
1362 /* If the initializer was empty, use default zero initialization. */
1363 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1364 return 0;
1366 len = CONSTRUCTOR_ELTS (init)->length ();
1367 if (len > 1)
1369 if (!(complain & tf_error))
1370 return PICFLAG_ERRONEOUS;
1371 error ("too many initializers for %qT", type);
1372 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1375 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1377 /* If this element specifies a field, initialize via that field. */
1378 if (ce->index)
1380 if (TREE_CODE (ce->index) == FIELD_DECL)
1382 else if (identifier_p (ce->index))
1384 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1385 tree name = ce->index;
1386 tree field;
1387 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1388 if (DECL_NAME (field) == name)
1389 break;
1390 if (!field)
1392 if (complain & tf_error)
1393 error ("no field %qD found in union being initialized",
1394 field);
1395 ce->value = error_mark_node;
1397 ce->index = field;
1399 else
1401 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1402 || TREE_CODE (ce->index) == RANGE_EXPR);
1403 if (complain & tf_error)
1404 error ("index value instead of field name in union initializer");
1405 ce->value = error_mark_node;
1408 else
1410 /* Find the first named field. ANSI decided in September 1990
1411 that only named fields count here. */
1412 tree field = TYPE_FIELDS (type);
1413 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1414 field = TREE_CHAIN (field);
1415 if (field == NULL_TREE)
1417 if (complain & tf_error)
1418 error ("too many initializers for %qT", type);
1419 ce->value = error_mark_node;
1421 ce->index = field;
1424 if (ce->value && ce->value != error_mark_node)
1425 ce->value = digest_init_r (TREE_TYPE (ce->index), ce->value,
1426 true, LOOKUP_IMPLICIT, complain);
1428 return picflag_from_initializer (ce->value);
1431 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1432 constructor is a brace-enclosed initializer, and will be modified in-place.
1434 Each element is converted to the right type through digest_init, and
1435 missing initializers are added following the language rules (zero-padding,
1436 etc.).
1438 After the execution, the initializer will have TREE_CONSTANT if all elts are
1439 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1440 constants that the assembler and linker can compute them.
1442 The function returns the initializer itself, or error_mark_node in case
1443 of error. */
1445 static tree
1446 process_init_constructor (tree type, tree init, tsubst_flags_t complain)
1448 int flags;
1450 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1452 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1453 flags = process_init_constructor_array (type, init, complain);
1454 else if (TREE_CODE (type) == RECORD_TYPE)
1455 flags = process_init_constructor_record (type, init, complain);
1456 else if (TREE_CODE (type) == UNION_TYPE)
1457 flags = process_init_constructor_union (type, init, complain);
1458 else
1459 gcc_unreachable ();
1461 if (flags & PICFLAG_ERRONEOUS)
1462 return error_mark_node;
1464 TREE_TYPE (init) = type;
1465 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1466 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1467 if (flags & PICFLAG_NOT_ALL_CONSTANT)
1468 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1469 TREE_CONSTANT (init) = false;
1470 else
1472 TREE_CONSTANT (init) = 1;
1473 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1474 TREE_STATIC (init) = 1;
1476 return init;
1479 /* Given a structure or union value DATUM, construct and return
1480 the structure or union component which results from narrowing
1481 that value to the base specified in BASETYPE. For example, given the
1482 hierarchy
1484 class L { int ii; };
1485 class A : L { ... };
1486 class B : L { ... };
1487 class C : A, B { ... };
1489 and the declaration
1491 C x;
1493 then the expression
1495 x.A::ii refers to the ii member of the L part of
1496 the A part of the C object named by X. In this case,
1497 DATUM would be x, and BASETYPE would be A.
1499 I used to think that this was nonconformant, that the standard specified
1500 that first we look up ii in A, then convert x to an L& and pull out the
1501 ii part. But in fact, it does say that we convert x to an A&; A here
1502 is known as the "naming class". (jason 2000-12-19)
1504 BINFO_P points to a variable initialized either to NULL_TREE or to the
1505 binfo for the specific base subobject we want to convert to. */
1507 tree
1508 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1510 tree binfo;
1512 if (datum == error_mark_node)
1513 return error_mark_node;
1514 if (*binfo_p)
1515 binfo = *binfo_p;
1516 else
1517 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1518 NULL, tf_warning_or_error);
1520 if (!binfo || binfo == error_mark_node)
1522 *binfo_p = NULL_TREE;
1523 if (!binfo)
1524 error_not_base_type (basetype, TREE_TYPE (datum));
1525 return error_mark_node;
1528 *binfo_p = binfo;
1529 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1530 tf_warning_or_error);
1533 /* Build a reference to an object specified by the C++ `->' operator.
1534 Usually this just involves dereferencing the object, but if the
1535 `->' operator is overloaded, then such overloads must be
1536 performed until an object which does not have the `->' operator
1537 overloaded is found. An error is reported when circular pointer
1538 delegation is detected. */
1540 tree
1541 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1543 tree orig_expr = expr;
1544 tree type = TREE_TYPE (expr);
1545 tree last_rval = NULL_TREE;
1546 vec<tree, va_gc> *types_memoized = NULL;
1548 if (type == error_mark_node)
1549 return error_mark_node;
1551 if (processing_template_decl)
1553 if (type_dependent_expression_p (expr))
1554 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1555 expr = build_non_dependent_expr (expr);
1558 if (MAYBE_CLASS_TYPE_P (type))
1560 struct tinst_level *actual_inst = current_instantiation ();
1561 tree fn = NULL;
1563 while ((expr = build_new_op (loc, COMPONENT_REF,
1564 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1565 &fn, complain)))
1567 if (expr == error_mark_node)
1568 return error_mark_node;
1570 if (fn && DECL_USE_TEMPLATE (fn))
1571 push_tinst_level (fn);
1572 fn = NULL;
1574 if (vec_member (TREE_TYPE (expr), types_memoized))
1576 if (complain & tf_error)
1577 error ("circular pointer delegation detected");
1578 return error_mark_node;
1581 vec_safe_push (types_memoized, TREE_TYPE (expr));
1582 last_rval = expr;
1585 while (current_instantiation () != actual_inst)
1586 pop_tinst_level ();
1588 if (last_rval == NULL_TREE)
1590 if (complain & tf_error)
1591 error ("base operand of %<->%> has non-pointer type %qT", type);
1592 return error_mark_node;
1595 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1596 last_rval = convert_from_reference (last_rval);
1598 else
1599 last_rval = decay_conversion (expr, complain);
1601 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1603 if (processing_template_decl)
1605 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1606 orig_expr);
1607 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1608 return expr;
1611 return cp_build_indirect_ref (last_rval, RO_NULL, complain);
1614 if (complain & tf_error)
1616 if (types_memoized)
1617 error ("result of %<operator->()%> yields non-pointer result");
1618 else
1619 error ("base operand of %<->%> is not a pointer");
1621 return error_mark_node;
1624 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1625 already been checked out to be of aggregate type. */
1627 tree
1628 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1630 tree ptrmem_type;
1631 tree objtype;
1632 tree type;
1633 tree binfo;
1634 tree ctype;
1636 if (error_operand_p (datum) || error_operand_p (component))
1637 return error_mark_node;
1639 datum = mark_lvalue_use (datum);
1640 component = mark_rvalue_use (component);
1642 ptrmem_type = TREE_TYPE (component);
1643 if (!TYPE_PTRMEM_P (ptrmem_type))
1645 if (complain & tf_error)
1646 error ("%qE cannot be used as a member pointer, since it is of "
1647 "type %qT", component, ptrmem_type);
1648 return error_mark_node;
1651 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1652 if (! MAYBE_CLASS_TYPE_P (objtype))
1654 if (complain & tf_error)
1655 error ("cannot apply member pointer %qE to %qE, which is of "
1656 "non-class type %qT", component, datum, objtype);
1657 return error_mark_node;
1660 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1661 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1663 if (!COMPLETE_TYPE_P (ctype))
1665 if (!same_type_p (ctype, objtype))
1666 goto mismatch;
1667 binfo = NULL;
1669 else
1671 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1673 if (!binfo)
1675 mismatch:
1676 if (complain & tf_error)
1677 error ("pointer to member type %qT incompatible with object "
1678 "type %qT", type, objtype);
1679 return error_mark_node;
1681 else if (binfo == error_mark_node)
1682 return error_mark_node;
1685 if (TYPE_PTRDATAMEM_P (ptrmem_type))
1687 cp_lvalue_kind kind = lvalue_kind (datum);
1688 tree ptype;
1690 /* Compute the type of the field, as described in [expr.ref].
1691 There's no such thing as a mutable pointer-to-member, so
1692 things are not as complex as they are for references to
1693 non-static data members. */
1694 type = cp_build_qualified_type (type,
1695 (cp_type_quals (type)
1696 | cp_type_quals (TREE_TYPE (datum))));
1698 datum = build_address (datum);
1700 /* Convert object to the correct base. */
1701 if (binfo)
1703 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
1704 if (datum == error_mark_node)
1705 return error_mark_node;
1708 /* Build an expression for "object + offset" where offset is the
1709 value stored in the pointer-to-data-member. */
1710 ptype = build_pointer_type (type);
1711 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
1712 datum = cp_build_indirect_ref (datum, RO_NULL, complain);
1713 if (datum == error_mark_node)
1714 return error_mark_node;
1716 /* If the object expression was an rvalue, return an rvalue. */
1717 if (kind & clk_class)
1718 datum = rvalue (datum);
1719 else if (kind & clk_rvalueref)
1720 datum = move (datum);
1721 return datum;
1723 else
1725 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1726 program is ill-formed if the second operand is a pointer to member
1727 function with ref-qualifier &. In a .* expression whose object
1728 expression is an lvalue, the program is ill-formed if the second
1729 operand is a pointer to member function with ref-qualifier &&. */
1730 if (FUNCTION_REF_QUALIFIED (type))
1732 bool lval = real_lvalue_p (datum);
1733 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
1735 if (complain & tf_error)
1736 error ("pointer-to-member-function type %qT requires an rvalue",
1737 ptrmem_type);
1738 return error_mark_node;
1740 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
1742 if (complain & tf_error)
1743 error ("pointer-to-member-function type %qT requires an lvalue",
1744 ptrmem_type);
1745 return error_mark_node;
1748 return build2 (OFFSET_REF, type, datum, component);
1752 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1754 tree
1755 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1757 /* This is either a call to a constructor,
1758 or a C cast in C++'s `functional' notation. */
1760 /* The type to which we are casting. */
1761 tree type;
1762 vec<tree, va_gc> *parmvec;
1764 if (error_operand_p (exp) || parms == error_mark_node)
1765 return error_mark_node;
1767 if (TREE_CODE (exp) == TYPE_DECL)
1769 type = TREE_TYPE (exp);
1771 if (complain & tf_warning
1772 && TREE_DEPRECATED (type)
1773 && DECL_ARTIFICIAL (exp))
1774 warn_deprecated_use (type, NULL_TREE);
1776 else
1777 type = exp;
1779 /* We need to check this explicitly, since value-initialization of
1780 arrays is allowed in other situations. */
1781 if (TREE_CODE (type) == ARRAY_TYPE)
1783 if (complain & tf_error)
1784 error ("functional cast to array type %qT", type);
1785 return error_mark_node;
1788 if (type_uses_auto (type))
1790 if (complain & tf_error)
1791 error ("invalid use of %<auto%>");
1792 return error_mark_node;
1795 if (processing_template_decl)
1797 tree t;
1799 /* Diagnose this even in a template. We could also try harder
1800 to give all the usual errors when the type and args are
1801 non-dependent... */
1802 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1804 if (complain & tf_error)
1805 error ("invalid value-initialization of reference type");
1806 return error_mark_node;
1809 t = build_min (CAST_EXPR, type, parms);
1810 /* We don't know if it will or will not have side effects. */
1811 TREE_SIDE_EFFECTS (t) = 1;
1812 return t;
1815 if (! MAYBE_CLASS_TYPE_P (type))
1817 if (parms == NULL_TREE)
1819 if (VOID_TYPE_P (type))
1820 return void_zero_node;
1821 return build_value_init (cv_unqualified (type), complain);
1824 /* This must build a C cast. */
1825 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1826 return cp_build_c_cast (type, parms, complain);
1829 /* Prepare to evaluate as a call to a constructor. If this expression
1830 is actually used, for example,
1832 return X (arg1, arg2, ...);
1834 then the slot being initialized will be filled in. */
1836 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1837 return error_mark_node;
1838 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
1839 return error_mark_node;
1841 /* [expr.type.conv]
1843 If the expression list is a single-expression, the type
1844 conversion is equivalent (in definedness, and if defined in
1845 meaning) to the corresponding cast expression. */
1846 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1847 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1849 /* [expr.type.conv]
1851 The expression T(), where T is a simple-type-specifier for a
1852 non-array complete object type or the (possibly cv-qualified)
1853 void type, creates an rvalue of the specified type, which is
1854 value-initialized. */
1856 if (parms == NULL_TREE)
1858 exp = build_value_init (type, complain);
1859 exp = get_target_expr_sfinae (exp, complain);
1860 return exp;
1863 /* Call the constructor. */
1864 parmvec = make_tree_vector ();
1865 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
1866 vec_safe_push (parmvec, TREE_VALUE (parms));
1867 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
1868 &parmvec, type, LOOKUP_NORMAL, complain);
1869 release_tree_vector (parmvec);
1871 if (exp == error_mark_node)
1872 return error_mark_node;
1874 return build_cplus_new (type, exp, complain);
1878 /* Add new exception specifier SPEC, to the LIST we currently have.
1879 If it's already in LIST then do nothing.
1880 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1881 know what we're doing. */
1883 tree
1884 add_exception_specifier (tree list, tree spec, int complain)
1886 bool ok;
1887 tree core = spec;
1888 bool is_ptr;
1889 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
1891 if (spec == error_mark_node)
1892 return list;
1894 gcc_assert (spec && (!list || TREE_VALUE (list)));
1896 /* [except.spec] 1, type in an exception specifier shall not be
1897 incomplete, or pointer or ref to incomplete other than pointer
1898 to cv void. */
1899 is_ptr = TYPE_PTR_P (core);
1900 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1901 core = TREE_TYPE (core);
1902 if (complain < 0)
1903 ok = true;
1904 else if (VOID_TYPE_P (core))
1905 ok = is_ptr;
1906 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1907 ok = true;
1908 else if (processing_template_decl)
1909 ok = true;
1910 else
1912 ok = true;
1913 /* 15.4/1 says that types in an exception specifier must be complete,
1914 but it seems more reasonable to only require this on definitions
1915 and calls. So just give a pedwarn at this point; we will give an
1916 error later if we hit one of those two cases. */
1917 if (!COMPLETE_TYPE_P (complete_type (core)))
1918 diag_type = DK_PEDWARN; /* pedwarn */
1921 if (ok)
1923 tree probe;
1925 for (probe = list; probe; probe = TREE_CHAIN (probe))
1926 if (same_type_p (TREE_VALUE (probe), spec))
1927 break;
1928 if (!probe)
1929 list = tree_cons (NULL_TREE, spec, list);
1931 else
1932 diag_type = DK_ERROR; /* error */
1934 if (diag_type != DK_UNSPECIFIED
1935 && (complain & tf_warning_or_error))
1936 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1938 return list;
1941 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
1943 static bool
1944 nothrow_spec_p_uninst (const_tree spec)
1946 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
1947 return false;
1948 return nothrow_spec_p (spec);
1951 /* Combine the two exceptions specifier lists LIST and ADD, and return
1952 their union. If FN is non-null, it's the source of ADD. */
1954 tree
1955 merge_exception_specifiers (tree list, tree add, tree fn)
1957 tree noex, orig_list;
1959 /* No exception-specifier or noexcept(false) are less strict than
1960 anything else. Prefer the newer variant (LIST). */
1961 if (!list || list == noexcept_false_spec)
1962 return list;
1963 else if (!add || add == noexcept_false_spec)
1964 return add;
1966 /* noexcept(true) and throw() are stricter than anything else.
1967 As above, prefer the more recent one (LIST). */
1968 if (nothrow_spec_p_uninst (add))
1969 return list;
1971 noex = TREE_PURPOSE (list);
1972 if (DEFERRED_NOEXCEPT_SPEC_P (add))
1974 /* If ADD is a deferred noexcept, we must have been called from
1975 process_subob_fn. For implicitly declared functions, we build up
1976 a list of functions to consider at instantiation time. */
1977 if (noex && operand_equal_p (noex, boolean_true_node, 0))
1978 noex = NULL_TREE;
1979 gcc_assert (fn && (!noex || is_overloaded_fn (noex)));
1980 noex = build_overload (fn, noex);
1982 else if (nothrow_spec_p_uninst (list))
1983 return add;
1984 else
1985 gcc_checking_assert (!TREE_PURPOSE (add)
1986 || cp_tree_equal (noex, TREE_PURPOSE (add)));
1988 /* Combine the dynamic-exception-specifiers, if any. */
1989 orig_list = list;
1990 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
1992 tree spec = TREE_VALUE (add);
1993 tree probe;
1995 for (probe = orig_list; probe && TREE_VALUE (probe);
1996 probe = TREE_CHAIN (probe))
1997 if (same_type_p (TREE_VALUE (probe), spec))
1998 break;
1999 if (!probe)
2001 spec = build_tree_list (NULL_TREE, spec);
2002 TREE_CHAIN (spec) = list;
2003 list = spec;
2007 /* Keep the noexcept-specifier at the beginning of the list. */
2008 if (noex != TREE_PURPOSE (list))
2009 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2011 return list;
2014 /* Subroutine of build_call. Ensure that each of the types in the
2015 exception specification is complete. Technically, 15.4/1 says that
2016 they need to be complete when we see a declaration of the function,
2017 but we should be able to get away with only requiring this when the
2018 function is defined or called. See also add_exception_specifier. */
2020 void
2021 require_complete_eh_spec_types (tree fntype, tree decl)
2023 tree raises;
2024 /* Don't complain about calls to op new. */
2025 if (decl && DECL_ARTIFICIAL (decl))
2026 return;
2027 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2028 raises = TREE_CHAIN (raises))
2030 tree type = TREE_VALUE (raises);
2031 if (type && !COMPLETE_TYPE_P (type))
2033 if (decl)
2034 error
2035 ("call to function %qD which throws incomplete type %q#T",
2036 decl, type);
2037 else
2038 error ("call to function which throws incomplete type %q#T",
2039 decl);
2045 #include "gt-cp-typeck2.h"