1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2004
5 Free Software Foundation, Inc.
6 Hacked by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to
22 the Free Software Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
26 /* This file is part of the C++ front end.
27 It contains routines to build C++ expressions given their operands,
28 including computing the types of the result, C and C++ specific error
29 checks, and some optimization.
31 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
32 and to process initializations in declarations (since they work
33 like a strange sort of assignment). */
37 #include "coretypes.h"
44 #include "diagnostic.h"
46 static tree process_init_constructor (tree, tree, tree *);
48 /* Print an error message stemming from an attempt to use
49 BASETYPE as a base class for TYPE. */
52 error_not_base_type (tree basetype, tree type)
54 if (TREE_CODE (basetype) == FUNCTION_DECL)
55 basetype = DECL_CONTEXT (basetype);
56 error ("type `%T' is not a base type for type `%T'", basetype, type);
57 return error_mark_node;
61 binfo_or_else (tree base, tree type)
63 tree binfo = lookup_base (type, base, ba_ignore, NULL);
65 if (binfo == error_mark_node)
68 error_not_base_type (base, type);
72 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
73 value may not be changed thereafter. Thus, we emit hard errors for these,
74 rather than just pedwarns. If `SOFT' is 1, then we just pedwarn. (For
75 example, conversions to references.) */
78 readonly_error (tree arg, const char* string, int soft)
81 void (*fn) (const char *, ...);
88 if (TREE_CODE (arg) == COMPONENT_REF)
90 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
91 fmt = "%s of data-member `%D' in read-only structure";
93 fmt = "%s of read-only data-member `%D'";
94 (*fn) (fmt, string, TREE_OPERAND (arg, 1));
96 else if (TREE_CODE (arg) == VAR_DECL)
98 if (DECL_LANG_SPECIFIC (arg)
99 && DECL_IN_AGGR_P (arg)
100 && !TREE_STATIC (arg))
101 fmt = "%s of constant field `%D'";
103 fmt = "%s of read-only variable `%D'";
104 (*fn) (fmt, string, arg);
106 else if (TREE_CODE (arg) == PARM_DECL)
107 (*fn) ("%s of read-only parameter `%D'", string, arg);
108 else if (TREE_CODE (arg) == INDIRECT_REF
109 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
110 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
111 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
112 (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0));
113 else if (TREE_CODE (arg) == RESULT_DECL)
114 (*fn) ("%s of read-only named return value `%D'", string, arg);
115 else if (TREE_CODE (arg) == FUNCTION_DECL)
116 (*fn) ("%s of function `%D'", string, arg);
118 (*fn) ("%s of read-only location", string);
121 /* If TYPE has abstract virtual functions, issue an error about trying
122 to create an object of that type. DECL is the object declared, or
123 NULL_TREE if the declaration is unavailable. Returns 1 if an error
124 occurred; zero if all was well. */
127 abstract_virtuals_error (tree decl, tree type)
132 if (!CLASS_TYPE_P (type) || !CLASSTYPE_PURE_VIRTUALS (type))
135 if (!TYPE_SIZE (type))
136 /* TYPE is being defined, and during that time
137 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
140 if (dependent_type_p (type))
141 /* For a dependent type, we do not yet know which functions are pure
145 u = CLASSTYPE_PURE_VIRTUALS (type);
148 if (TREE_CODE (decl) == RESULT_DECL)
151 if (TREE_CODE (decl) == VAR_DECL)
152 cp_error_at ("cannot declare variable `%+D' to be of abstract "
153 "type `%T'", decl, type);
154 else if (TREE_CODE (decl) == PARM_DECL)
155 cp_error_at ("cannot declare parameter `%+D' to be of abstract "
156 "type `%T'", decl, type);
157 else if (TREE_CODE (decl) == FIELD_DECL)
158 cp_error_at ("cannot declare field `%+D' to be of abstract "
159 "type `%T'", decl, type);
160 else if (TREE_CODE (decl) == FUNCTION_DECL
161 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
162 cp_error_at ("invalid abstract return type for member function `%+#D'",
164 else if (TREE_CODE (decl) == FUNCTION_DECL)
165 cp_error_at ("invalid abstract return type for function `%+#D'",
168 cp_error_at ("invalid abstract type for `%+D'", decl);
171 error ("cannot allocate an object of abstract type `%T'", type);
173 /* Only go through this once. */
174 if (TREE_PURPOSE (u) == NULL_TREE)
176 TREE_PURPOSE (u) = error_mark_node;
178 inform ("%J because the following virtual functions are pure "
179 "within `%T':", TYPE_MAIN_DECL (type), type);
181 for (tu = u; tu; tu = TREE_CHAIN (tu))
182 inform ("%J\t%#D", TREE_VALUE (tu), TREE_VALUE (tu));
185 inform ("%J since type `%T' has pure virtual functions",
186 TYPE_MAIN_DECL (type), type);
191 /* Print an error message for invalid use of an incomplete type.
192 VALUE is the expression that was used (or 0 if that isn't known)
193 and TYPE is the type that was invalid. DIAG_TYPE indicates the
194 type of diagnostic: 0 for an error, 1 for a warning, 2 for a
198 cxx_incomplete_type_diagnostic (tree value, tree type, int diag_type)
201 void (*p_msg) (const char *, ...);
202 void (*p_msg_at) (const char *, ...);
207 p_msg_at = cp_warning_at;
209 else if (diag_type == 2)
212 p_msg_at = cp_pedwarn_at;
217 p_msg_at = cp_error_at;
220 /* Avoid duplicate error message. */
221 if (TREE_CODE (type) == ERROR_MARK)
224 if (value != 0 && (TREE_CODE (value) == VAR_DECL
225 || TREE_CODE (value) == PARM_DECL
226 || TREE_CODE (value) == FIELD_DECL))
228 (*p_msg_at) ("`%D' has incomplete type", value);
232 /* We must print an error message. Be clever about what it says. */
234 switch (TREE_CODE (type))
240 (*p_msg) ("invalid use of undefined type `%#T'", type);
241 if (!TYPE_TEMPLATE_INFO (type))
242 (*p_msg_at) ("forward declaration of `%#T'", type);
244 (*p_msg_at) ("declaration of `%#T'", type);
248 (*p_msg) ("invalid use of `%T'", type);
252 if (TYPE_DOMAIN (type))
254 type = TREE_TYPE (type);
257 (*p_msg) ("invalid use of array with unspecified bounds");
262 (*p_msg) ("invalid use of member (did you forget the `&' ?)");
265 case TEMPLATE_TYPE_PARM:
266 (*p_msg) ("invalid use of template type parameter");
270 if (value && TREE_CODE (value) == COMPONENT_REF)
272 else if (value && TREE_CODE (value) == ADDR_EXPR)
273 (*p_msg) ("address of overloaded function with no contextual type information");
274 else if (value && TREE_CODE (value) == OVERLOAD)
275 (*p_msg) ("overloaded function with no contextual type information");
277 (*p_msg) ("insufficient contextual information to determine type");
285 /* Backward-compatibility interface to incomplete_type_diagnostic;
286 required by ../tree.c. */
287 #undef cxx_incomplete_type_error
289 cxx_incomplete_type_error (tree value, tree type)
291 cxx_incomplete_type_diagnostic (value, type, 0);
295 /* The recursive part of split_nonconstant_init. DEST is an lvalue
296 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
297 PCODE is a pointer to the tail of a chain of statements being emitted.
298 The return value is the new tail of that chain after new statements
302 split_nonconstant_init_1 (tree dest, tree init, tree *pcode)
304 tree *pelt, elt, type = TREE_TYPE (dest);
305 tree sub, code, inner_type = NULL;
306 bool array_type_p = false;
308 pelt = &CONSTRUCTOR_ELTS (init);
309 switch (TREE_CODE (type))
312 inner_type = TREE_TYPE (type);
318 case QUAL_UNION_TYPE:
319 while ((elt = *pelt))
321 tree field_index = TREE_PURPOSE (elt);
322 tree value = TREE_VALUE (elt);
325 inner_type = TREE_TYPE (field_index);
327 if (TREE_CODE (value) == CONSTRUCTOR)
330 sub = build (ARRAY_REF, inner_type, dest, field_index);
332 sub = build (COMPONENT_REF, inner_type, dest, field_index);
334 pcode = split_nonconstant_init_1 (sub, value, pcode);
336 else if (!initializer_constant_valid_p (value, inner_type))
338 *pelt = TREE_CHAIN (elt);
341 sub = build (ARRAY_REF, inner_type, dest, field_index);
343 sub = build (COMPONENT_REF, inner_type, dest, field_index);
345 code = build (MODIFY_EXPR, inner_type, sub, value);
346 code = build_stmt (EXPR_STMT, code);
349 pcode = &TREE_CHAIN (code);
352 pelt = &TREE_CHAIN (elt);
357 if (!initializer_constant_valid_p (init, type))
359 CONSTRUCTOR_ELTS (init) = NULL;
360 code = build (MODIFY_EXPR, type, dest, init);
361 code = build_stmt (EXPR_STMT, code);
362 pcode = &TREE_CHAIN (code);
373 /* A subroutine of store_init_value. Splits non-constant static
374 initializer INIT into a constant part and generates code to
375 perform the non-constant part of the initialization to DEST.
376 Returns the code for the runtime init. */
379 split_nonconstant_init (tree dest, tree init)
383 if (TREE_CODE (init) == CONSTRUCTOR)
385 code = build_stmt (COMPOUND_STMT, NULL_TREE);
386 split_nonconstant_init_1 (dest, init, &COMPOUND_BODY (code));
387 code = build1 (STMT_EXPR, void_type_node, code);
388 TREE_SIDE_EFFECTS (code) = 1;
389 DECL_INITIAL (dest) = init;
390 TREE_READONLY (dest) = 0;
393 code = build (INIT_EXPR, TREE_TYPE (dest), dest, init);
398 /* Perform appropriate conversions on the initial value of a variable,
399 store it in the declaration DECL,
400 and print any error messages that are appropriate.
401 If the init is invalid, store an ERROR_MARK.
403 C++: Note that INIT might be a TREE_LIST, which would mean that it is
404 a base class initializer for some aggregate type, hopefully compatible
405 with DECL. If INIT is a single element, and DECL is an aggregate
406 type, we silently convert INIT into a TREE_LIST, allowing a constructor
409 If INIT is a TREE_LIST and there is no constructor, turn INIT
410 into a CONSTRUCTOR and use standard initialization techniques.
411 Perhaps a warning should be generated?
413 Returns code to be executed if initialization could not be performed
414 for static variable. In that case, caller must emit the code. */
417 store_init_value (tree decl, tree init)
421 /* If variable's type was invalidly declared, just ignore it. */
423 type = TREE_TYPE (decl);
424 if (TREE_CODE (type) == ERROR_MARK)
427 if (IS_AGGR_TYPE (type))
429 if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
430 && TREE_CODE (init) != CONSTRUCTOR)
433 if (TREE_CODE (init) == TREE_LIST)
435 error ("constructor syntax used, but no constructor declared for type `%T'", type);
436 init = build_constructor (NULL_TREE, nreverse (init));
439 else if (TREE_CODE (init) == TREE_LIST
440 && TREE_TYPE (init) != unknown_type_node)
442 if (TREE_CODE (decl) == RESULT_DECL)
443 init = build_x_compound_expr_from_list (init,
444 "return value initializer");
445 else if (TREE_CODE (init) == TREE_LIST
446 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
448 error ("cannot initialize arrays using this syntax");
452 /* We get here with code like `int a (2);' */
453 init = build_x_compound_expr_from_list (init, "initializer");
456 /* End of special C++ code. */
458 /* Digest the specified initializer into an expression. */
459 value = digest_init (type, init, (tree *) 0);
461 /* Store the expression if valid; else report error. */
463 if (TREE_CODE (value) == ERROR_MARK)
465 /* Other code expects that initializers for objects of types that need
466 constructing never make it into DECL_INITIAL, and passes 'init' to
467 build_aggr_init without checking DECL_INITIAL. So just return. */
468 else if (TYPE_NEEDS_CONSTRUCTING (type))
469 return build (INIT_EXPR, type, decl, value);
470 else if (TREE_STATIC (decl)
471 && (! TREE_CONSTANT (value)
472 || ! initializer_constant_valid_p (value, TREE_TYPE (value))))
473 return split_nonconstant_init (decl, value);
475 /* Store the VALUE in DECL_INITIAL. If we're building a
476 statement-tree we will actually expand the initialization later
477 when we output this function. */
478 DECL_INITIAL (decl) = value;
483 /* Digest the parser output INIT as an initializer for type TYPE.
484 Return a C expression of type TYPE to represent the initial value.
486 If TAIL is nonzero, it points to a variable holding a list of elements
487 of which INIT is the first. We update the list stored there by
488 removing from the head all the elements that we use.
489 Normally this is only one; we use more than one element only if
490 TYPE is an aggregate and INIT is not a constructor. */
493 digest_init (tree type, tree init, tree* tail)
495 enum tree_code code = TREE_CODE (type);
496 tree element = NULL_TREE;
497 tree old_tail_contents = NULL_TREE;
499 /* By default, assume we use one element from a list.
500 We correct this later in the sole case where it is not true. */
504 old_tail_contents = *tail;
505 *tail = TREE_CHAIN (*tail);
508 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
509 && TREE_VALUE (init) == error_mark_node))
510 return error_mark_node;
512 if (TREE_CODE (init) == ERROR_MARK)
513 /* __PRETTY_FUNCTION__'s initializer is a bogus expression inside
514 a template function. This gets substituted during instantiation. */
517 /* We must strip the outermost array type when completing the type,
518 because the its bounds might be incomplete at the moment. */
519 if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
520 ? TREE_TYPE (type) : type, NULL_TREE))
521 return error_mark_node;
523 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
524 if (TREE_CODE (init) == NON_LVALUE_EXPR)
525 init = TREE_OPERAND (init, 0);
527 if (BRACE_ENCLOSED_INITIALIZER_P (init)
528 && CONSTRUCTOR_ELTS (init) != 0
529 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
531 element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
532 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
533 if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
534 element = TREE_OPERAND (element, 0);
535 if (element == error_mark_node)
539 /* Initialization of an array of chars from a string constant
540 optionally enclosed in braces. */
542 if (code == ARRAY_TYPE)
546 if (TREE_CODE (init) == TREE_LIST)
548 error ("initializing array with parameter list");
549 return error_mark_node;
552 typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
553 if (char_type_p (typ1)
554 && ((init && TREE_CODE (init) == STRING_CST)
555 || (element && TREE_CODE (element) == STRING_CST)))
557 tree string = element ? element : init;
559 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
561 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
563 error ("char-array initialized from wide string");
564 return error_mark_node;
566 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
568 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
570 error ("int-array initialized from non-wide string");
571 return error_mark_node;
574 TREE_TYPE (string) = type;
575 if (TYPE_DOMAIN (type) != 0
576 && TREE_CONSTANT (TYPE_SIZE (type)))
578 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
579 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
580 /* In C it is ok to subtract 1 from the length of the string
581 because it's ok to ignore the terminating null char that is
582 counted in the length of the constant, but in C++ this would
584 if (size < TREE_STRING_LENGTH (string))
585 pedwarn ("initializer-string for array of chars is too long");
591 /* Handle scalar types, including conversions,
592 and signature pointers and references. */
594 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
595 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
596 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
597 || TYPE_PTR_TO_MEMBER_P (type))
599 if (BRACE_ENCLOSED_INITIALIZER_P (init))
603 error ("initializer for scalar variable requires one element");
604 return error_mark_node;
608 while (BRACE_ENCLOSED_INITIALIZER_P (init))
610 pedwarn ("braces around scalar initializer for `%T'", type);
611 init = CONSTRUCTOR_ELTS (init);
612 if (TREE_CHAIN (init))
613 pedwarn ("ignoring extra initializers for `%T'", type);
614 init = TREE_VALUE (init);
617 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
618 "initialization", NULL_TREE, 0);
621 /* Come here only for records and arrays (and unions with constructors). */
623 if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
625 error ("variable-sized object of type `%T' may not be initialized",
627 return error_mark_node;
630 if (code == ARRAY_TYPE || code == VECTOR_TYPE || IS_AGGR_TYPE_CODE (code))
632 if (BRACE_ENCLOSED_INITIALIZER_P (init))
634 if (TYPE_NON_AGGREGATE_CLASS (type))
636 error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
638 return error_mark_node;
640 return process_init_constructor (type, init, (tree *)0);
642 else if (can_convert_arg (type, TREE_TYPE (init), init)
643 || TYPE_NON_AGGREGATE_CLASS (type))
644 /* These are never initialized from multiple constructor elements. */;
647 *tail = old_tail_contents;
648 return process_init_constructor (type, 0, tail);
651 if (code != ARRAY_TYPE)
653 int flags = LOOKUP_NORMAL;
654 /* Initialization from { } is copy-initialization. */
656 flags |= LOOKUP_ONLYCONVERTING;
658 return convert_for_initialization (NULL_TREE, type, init, flags,
659 "initialization", NULL_TREE, 0);
663 error ("invalid initializer");
664 return error_mark_node;
667 /* Process a constructor for a variable of type TYPE.
668 The constructor elements may be specified either with INIT or with ELTS,
669 only one of which should be non-null.
671 If INIT is specified, it is a CONSTRUCTOR node which is specifically
672 and solely for initializing this datum.
674 If ELTS is specified, it is the address of a variable containing
675 a list of expressions. We take as many elements as we need
676 from the head of the list and update the list.
678 In the resulting constructor, TREE_CONSTANT is set if all elts are
679 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
680 constants that the assembler and linker can compute them. */
683 process_init_constructor (tree type, tree init, tree* elts)
686 /* List of the elements of the result constructor,
695 /* Make TAIL be the list of elements to use for the initialization,
696 no matter how the data was given to us. */
700 if (warn_missing_braces)
701 warning ("aggregate has a partly bracketed initializer");
705 tail = CONSTRUCTOR_ELTS (init);
707 /* Gobble as many elements as needed, and make a constructor or initial value
708 for each element of this aggregate. Chain them together in result.
709 If there are too few, use 0 for each scalar ultimate component. */
711 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
716 if (TREE_CODE (type) == ARRAY_TYPE)
718 tree domain = TYPE_DOMAIN (type);
720 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
721 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
724 len = -1; /* Take as many as there are. */
728 /* Vectors are like simple fixed-size arrays. */
729 len = TYPE_VECTOR_SUBPARTS (type);
732 for (i = 0; len < 0 || i < len; i++)
736 if (TREE_PURPOSE (tail)
737 && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
738 || compare_tree_int (TREE_PURPOSE (tail), i) != 0))
739 sorry ("non-trivial labeled initializers");
741 if (TREE_VALUE (tail) != 0)
744 next1 = digest_init (TREE_TYPE (type),
745 TREE_VALUE (tail), &tail1);
746 if (next1 == error_mark_node)
749 (same_type_ignoring_top_level_qualifiers_p
750 (TREE_TYPE (type), TREE_TYPE (next1)),
752 my_friendly_assert (tail1 == 0
753 || TREE_CODE (tail1) == TREE_LIST, 319);
754 if (tail == tail1 && len < 0)
756 error ("non-empty initializer for array of empty elements");
757 /* Just ignore what we were supposed to use. */
764 next1 = error_mark_node;
765 tail = TREE_CHAIN (tail);
771 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
773 /* If this type needs constructors run for
774 default-initialization, we can't rely on the backend to do it
775 for us, so build up TARGET_EXPRs. If the type in question is
776 a class, just build one up; if it's an array, recurse. */
778 if (IS_AGGR_TYPE (TREE_TYPE (type)))
779 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
781 next1 = build_constructor (NULL_TREE, NULL_TREE);
782 next1 = digest_init (TREE_TYPE (type), next1, 0);
784 else if (! zero_init_p (TREE_TYPE (type)))
785 next1 = build_zero_init (TREE_TYPE (type),
787 /*static_storage_p=*/false);
789 /* The default zero-initialization is fine for us; don't
790 add anything to the CONSTRUCTOR. */
793 if (next1 == error_mark_node)
795 else if (!TREE_CONSTANT (next1))
797 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
799 members = tree_cons (size_int (i), next1, members);
802 else if (TREE_CODE (type) == RECORD_TYPE)
808 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
810 sorry ("initializer list for object of class with virtual base classes");
811 return error_mark_node;
814 if (TYPE_BINFO_BASETYPES (type))
816 sorry ("initializer list for object of class with base classes");
817 return error_mark_node;
820 if (TYPE_POLYMORPHIC_P (type))
822 sorry ("initializer list for object using virtual functions");
823 return error_mark_node;
827 for (field = TYPE_FIELDS (type); field;
828 field = TREE_CHAIN (field))
830 if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
832 members = tree_cons (field, integer_zero_node, members);
836 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
841 if (TREE_PURPOSE (tail)
842 && TREE_PURPOSE (tail) != field
843 && TREE_PURPOSE (tail) != DECL_NAME (field))
844 sorry ("non-trivial labeled initializers");
846 if (TREE_VALUE (tail) != 0)
850 next1 = digest_init (TREE_TYPE (field),
851 TREE_VALUE (tail), &tail1);
852 my_friendly_assert (tail1 == 0
853 || TREE_CODE (tail1) == TREE_LIST, 320);
858 next1 = error_mark_node;
859 tail = TREE_CHAIN (tail);
862 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
864 /* If this type needs constructors run for
865 default-initialization, we can't rely on the backend to do it
866 for us, so build up TARGET_EXPRs. If the type in question is
867 a class, just build one up; if it's an array, recurse. */
869 if (IS_AGGR_TYPE (TREE_TYPE (field)))
870 next1 = build_functional_cast (TREE_TYPE (field),
874 next1 = build_constructor (NULL_TREE, NULL_TREE);
876 TREE_HAS_CONSTRUCTOR (next1)
877 = TREE_HAS_CONSTRUCTOR (init);
879 next1 = digest_init (TREE_TYPE (field), next1, 0);
881 /* Warn when some struct elements are implicitly initialized. */
883 && (!init || BRACE_ENCLOSED_INITIALIZER_P (init)))
884 warning ("missing initializer for member `%D'", field);
888 if (TREE_READONLY (field))
889 error ("uninitialized const member `%D'", field);
890 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
891 error ("member `%D' with uninitialized const fields",
893 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
894 error ("member `%D' is uninitialized reference", field);
896 /* Warn when some struct elements are implicitly initialized
899 && (!init || BRACE_ENCLOSED_INITIALIZER_P (init)))
900 warning ("missing initializer for member `%D'", field);
902 if (! zero_init_p (TREE_TYPE (field)))
903 next1 = build_zero_init (TREE_TYPE (field),
905 /*static_storage_p=*/false);
907 /* The default zero-initialization is fine for us; don't
908 add anything to the CONSTRUCTOR. */
912 if (next1 == error_mark_node)
914 else if (!TREE_CONSTANT (next1))
916 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
918 members = tree_cons (field, next1, members);
921 else if (TREE_CODE (type) == UNION_TYPE
922 /* If the initializer was empty, use default zero initialization. */
925 tree field = TYPE_FIELDS (type);
927 /* Find the first named field. ANSI decided in September 1990
928 that only named fields count here. */
929 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
930 field = TREE_CHAIN (field);
932 /* If this element specifies a field, initialize via that field. */
933 if (TREE_PURPOSE (tail) != NULL_TREE)
937 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
938 /* Handle the case of a call by build_c_cast. */
939 field = TREE_PURPOSE (tail), win = 1;
940 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
941 error ("index value instead of field name in union initializer");
945 for (temp = TYPE_FIELDS (type);
947 temp = TREE_CHAIN (temp))
948 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
951 field = temp, win = 1;
953 error ("no field `%D' in union being initialized",
954 TREE_PURPOSE (tail));
957 TREE_VALUE (tail) = error_mark_node;
961 error ("union `%T' with no named members cannot be initialized",
963 TREE_VALUE (tail) = error_mark_node;
966 if (TREE_VALUE (tail) != 0)
970 next1 = digest_init (TREE_TYPE (field),
971 TREE_VALUE (tail), &tail1);
972 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
978 next1 = error_mark_node;
979 tail = TREE_CHAIN (tail);
982 if (next1 == error_mark_node)
984 else if (!TREE_CONSTANT (next1))
986 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
988 members = tree_cons (field, next1, members);
991 /* If arguments were specified as a list, just remove the ones we used. */
994 /* If arguments were specified as a constructor,
995 complain unless we used all the elements of the constructor. */
997 pedwarn ("excess elements in aggregate initializer");
1000 return error_mark_node;
1002 result = build_constructor (type, nreverse (members));
1003 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1004 complete_array_type (type, result, /*do_default=*/0);
1006 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1009 TREE_CONSTANT (result) = 1;
1010 TREE_INVARIANT (result) = 1;
1012 TREE_STATIC (result) = 1;
1017 /* Given a structure or union value DATUM, construct and return
1018 the structure or union component which results from narrowing
1019 that value to the base specified in BASETYPE. For example, given the
1022 class L { int ii; };
1023 class A : L { ... };
1024 class B : L { ... };
1025 class C : A, B { ... };
1033 x.A::ii refers to the ii member of the L part of
1034 the A part of the C object named by X. In this case,
1035 DATUM would be x, and BASETYPE would be A.
1037 I used to think that this was nonconformant, that the standard specified
1038 that first we look up ii in A, then convert x to an L& and pull out the
1039 ii part. But in fact, it does say that we convert x to an A&; A here
1040 is known as the "naming class". (jason 2000-12-19)
1042 BINFO_P points to a variable initialized either to NULL_TREE or to the
1043 binfo for the specific base subobject we want to convert to. */
1046 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1050 if (datum == error_mark_node)
1051 return error_mark_node;
1055 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
1057 if (!binfo || binfo == error_mark_node)
1059 *binfo_p = NULL_TREE;
1061 error_not_base_type (basetype, TREE_TYPE (datum));
1062 return error_mark_node;
1066 return build_base_path (PLUS_EXPR, datum, binfo, 1);
1069 /* Build a reference to an object specified by the C++ `->' operator.
1070 Usually this just involves dereferencing the object, but if the
1071 `->' operator is overloaded, then such overloads must be
1072 performed until an object which does not have the `->' operator
1073 overloaded is found. An error is reported when circular pointer
1074 delegation is detected. */
1077 build_x_arrow (tree expr)
1079 tree orig_expr = expr;
1080 tree types_memoized = NULL_TREE;
1081 tree type = TREE_TYPE (expr);
1082 tree last_rval = NULL_TREE;
1084 if (type == error_mark_node)
1085 return error_mark_node;
1087 if (processing_template_decl)
1089 if (type_dependent_expression_p (expr))
1090 return build_min_nt (ARROW_EXPR, expr);
1091 expr = build_non_dependent_expr (expr);
1094 if (TREE_CODE (type) == REFERENCE_TYPE)
1096 expr = convert_from_reference (expr);
1097 type = TREE_TYPE (expr);
1100 if (IS_AGGR_TYPE (type))
1102 while ((expr = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, expr,
1103 NULL_TREE, NULL_TREE,
1104 /*overloaded_p=*/NULL)))
1106 if (expr == error_mark_node)
1107 return error_mark_node;
1109 if (value_member (TREE_TYPE (expr), types_memoized))
1111 error ("circular pointer delegation detected");
1112 return error_mark_node;
1116 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (expr),
1122 if (last_rval == NULL_TREE)
1124 error ("base operand of `->' has non-pointer type `%T'", type);
1125 return error_mark_node;
1128 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1129 last_rval = convert_from_reference (last_rval);
1132 last_rval = decay_conversion (expr);
1134 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1136 if (processing_template_decl)
1138 expr = build_min_non_dep (ARROW_EXPR, last_rval, orig_expr);
1139 /* It will be dereferenced. */
1140 TREE_TYPE (expr) = TREE_TYPE (TREE_TYPE (last_rval));
1144 return build_indirect_ref (last_rval, NULL);
1148 error ("result of `operator->()' yields non-pointer result");
1150 error ("base operand of `->' is not a pointer");
1151 return error_mark_node;
1154 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1155 already been checked out to be of aggregate type. */
1158 build_m_component_ref (tree datum, tree component)
1165 datum = decay_conversion (datum);
1167 if (datum == error_mark_node || component == error_mark_node)
1168 return error_mark_node;
1170 ptrmem_type = TREE_TYPE (component);
1171 if (!TYPE_PTR_TO_MEMBER_P (ptrmem_type))
1173 error ("`%E' cannot be used as a member pointer, since it is of type `%T'",
1174 component, ptrmem_type);
1175 return error_mark_node;
1178 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1179 if (! IS_AGGR_TYPE (objtype))
1181 error ("cannot apply member pointer `%E' to `%E', which is of non-aggregate type `%T'",
1182 component, datum, objtype);
1183 return error_mark_node;
1186 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1187 binfo = lookup_base (objtype, TYPE_PTRMEM_CLASS_TYPE (ptrmem_type),
1191 error ("member type `%T::' incompatible with object type `%T'",
1193 return error_mark_node;
1195 else if (binfo == error_mark_node)
1196 return error_mark_node;
1198 if (TYPE_PTRMEM_P (ptrmem_type))
1200 /* Compute the type of the field, as described in [expr.ref].
1201 There's no such thing as a mutable pointer-to-member, so
1202 things are not as complex as they are for references to
1203 non-static data members. */
1204 type = cp_build_qualified_type (type,
1205 (cp_type_quals (type)
1206 | cp_type_quals (TREE_TYPE (datum))));
1207 /* Build an expression for "object + offset" where offset is the
1208 value stored in the pointer-to-data-member. */
1209 datum = build (PLUS_EXPR, build_pointer_type (type),
1210 build_base_path (PLUS_EXPR, build_address (datum),
1212 build_nop (ptrdiff_type_node, component));
1213 return build_indirect_ref (datum, 0);
1216 return build (OFFSET_REF, type, datum, component);
1219 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1222 build_functional_cast (tree exp, tree parms)
1224 /* This is either a call to a constructor,
1225 or a C cast in C++'s `functional' notation. */
1228 if (exp == error_mark_node || parms == error_mark_node)
1229 return error_mark_node;
1231 if (TREE_CODE (exp) == TYPE_DECL)
1232 type = TREE_TYPE (exp);
1236 if (processing_template_decl)
1238 tree t = build_min (CAST_EXPR, type, parms);
1239 /* We don't know if it will or will not have side effects. */
1240 TREE_SIDE_EFFECTS (t) = 1;
1244 if (! IS_AGGR_TYPE (type))
1246 /* This must build a C cast. */
1247 if (parms == NULL_TREE)
1248 parms = integer_zero_node;
1250 parms = build_x_compound_expr_from_list (parms, "functional cast");
1252 return build_c_cast (type, parms);
1255 /* Prepare to evaluate as a call to a constructor. If this expression
1256 is actually used, for example,
1258 return X (arg1, arg2, ...);
1260 then the slot being initialized will be filled in. */
1262 if (!complete_type_or_else (type, NULL_TREE))
1263 return error_mark_node;
1264 if (abstract_virtuals_error (NULL_TREE, type))
1265 return error_mark_node;
1267 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1268 return build_c_cast (type, TREE_VALUE (parms));
1270 /* We need to zero-initialize POD types. Let's do that for everything
1271 that doesn't need a constructor. */
1272 if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1273 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1275 exp = build_constructor (type, NULL_TREE);
1276 return get_target_expr (exp);
1279 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, parms,
1280 TYPE_BINFO (type), LOOKUP_NORMAL);
1282 if (exp == error_mark_node)
1283 return error_mark_node;
1285 return build_cplus_new (type, exp);
1289 /* Add new exception specifier SPEC, to the LIST we currently have.
1290 If it's already in LIST then do nothing.
1291 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1292 know what we're doing. */
1295 add_exception_specifier (tree list, tree spec, int complain)
1300 int diag_type = -1; /* none */
1302 if (spec == error_mark_node)
1305 my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317);
1307 /* [except.spec] 1, type in an exception specifier shall not be
1308 incomplete, or pointer or ref to incomplete other than pointer
1310 is_ptr = TREE_CODE (core) == POINTER_TYPE;
1311 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1312 core = TREE_TYPE (core);
1315 else if (VOID_TYPE_P (core))
1317 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1319 else if (processing_template_decl)
1324 /* 15.4/1 says that types in an exception specifier must be complete,
1325 but it seems more reasonable to only require this on definitions
1326 and calls. So just give a pedwarn at this point; we will give an
1327 error later if we hit one of those two cases. */
1328 if (!COMPLETE_TYPE_P (complete_type (core)))
1329 diag_type = 2; /* pedwarn */
1336 for (probe = list; probe; probe = TREE_CHAIN (probe))
1337 if (same_type_p (TREE_VALUE (probe), spec))
1340 list = tree_cons (NULL_TREE, spec, list);
1343 diag_type = 0; /* error */
1345 if (diag_type >= 0 && complain)
1346 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1351 /* Combine the two exceptions specifier lists LIST and ADD, and return
1355 merge_exception_specifiers (tree list, tree add)
1359 else if (!TREE_VALUE (list))
1361 else if (!TREE_VALUE (add))
1365 tree orig_list = list;
1367 for (; add; add = TREE_CHAIN (add))
1369 tree spec = TREE_VALUE (add);
1372 for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1373 if (same_type_p (TREE_VALUE (probe), spec))
1377 spec = build_tree_list (NULL_TREE, spec);
1378 TREE_CHAIN (spec) = list;
1386 /* Subroutine of build_call. Ensure that each of the types in the
1387 exception specification is complete. Technically, 15.4/1 says that
1388 they need to be complete when we see a declaration of the function,
1389 but we should be able to get away with only requiring this when the
1390 function is defined or called. See also add_exception_specifier. */
1393 require_complete_eh_spec_types (tree fntype, tree decl)
1396 /* Don't complain about calls to op new. */
1397 if (decl && DECL_ARTIFICIAL (decl))
1399 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1400 raises = TREE_CHAIN (raises))
1402 tree type = TREE_VALUE (raises);
1403 if (type && !COMPLETE_TYPE_P (type))
1407 ("call to function `%D' which throws incomplete type `%#T'",
1410 error ("call to function which throws incomplete type `%#T'",