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 Free Software Foundation, Inc.
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* This file is part of the C++ front end.
26 It contains routines to build C++ expressions given their operands,
27 including computing the types of the result, C and C++ specific error
28 checks, and some optimization.
30 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
31 and to process initializations in declarations (since they work
32 like a strange sort of assignment). */
42 static tree process_init_constructor PARAMS ((tree, tree, tree *));
44 /* Print an error message stemming from an attempt to use
45 BASETYPE as a base class for TYPE. */
48 error_not_base_type (basetype, type)
51 if (TREE_CODE (basetype) == FUNCTION_DECL)
52 basetype = DECL_CONTEXT (basetype);
53 cp_error ("type `%T' is not a base type for type `%T'", basetype, type);
54 return error_mark_node;
58 binfo_or_else (parent_or_type, type)
59 tree parent_or_type, type;
62 if (TYPE_MAIN_VARIANT (parent_or_type) == TYPE_MAIN_VARIANT (type))
63 return TYPE_BINFO (parent_or_type);
64 if ((binfo = get_binfo (parent_or_type, TYPE_MAIN_VARIANT (type), 0)))
66 if (binfo == error_mark_node)
70 error_not_base_type (parent_or_type, type);
74 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
75 value may not be changed thereafter. Thus, we emit hard errors for these,
76 rather than just pedwarns. If `SOFT' is 1, then we just pedwarn. (For
77 example, conversions to references.) */
80 readonly_error (arg, string, soft)
86 void (*fn) PARAMS ((const char *, ...));
93 if (TREE_CODE (arg) == COMPONENT_REF)
95 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
96 fmt = "%s of data-member `%D' in read-only structure";
98 fmt = "%s of read-only data-member `%D'";
99 (*fn) (fmt, string, TREE_OPERAND (arg, 1));
101 else if (TREE_CODE (arg) == VAR_DECL)
103 if (DECL_LANG_SPECIFIC (arg)
104 && DECL_IN_AGGR_P (arg)
105 && !TREE_STATIC (arg))
106 fmt = "%s of constant field `%D'";
108 fmt = "%s of read-only variable `%D'";
109 (*fn) (fmt, string, arg);
111 else if (TREE_CODE (arg) == PARM_DECL)
112 (*fn) ("%s of read-only parameter `%D'", string, arg);
113 else if (TREE_CODE (arg) == INDIRECT_REF
114 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
115 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
116 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
117 (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0));
118 else if (TREE_CODE (arg) == RESULT_DECL)
119 (*fn) ("%s of read-only named return value `%D'", string, arg);
120 else if (TREE_CODE (arg) == FUNCTION_DECL)
121 (*fn) ("%s of function `%D'", string, arg);
123 (*fn) ("%s of read-only location", string);
126 /* If TYPE has abstract virtual functions, issue an error about trying
127 to create an object of that type. DECL is the object declared, or
128 NULL_TREE if the declaration is unavailable. Returns 1 if an error
129 occurred; zero if all was well. */
132 abstract_virtuals_error (decl, type)
139 if (!CLASS_TYPE_P (type) || !CLASSTYPE_PURE_VIRTUALS (type))
142 u = CLASSTYPE_PURE_VIRTUALS (type);
145 if (TREE_CODE (decl) == RESULT_DECL)
148 if (TREE_CODE (decl) == VAR_DECL)
149 cp_error ("cannot declare variable `%D' to be of type `%T'",
151 else if (TREE_CODE (decl) == PARM_DECL)
152 cp_error ("cannot declare parameter `%D' to be of type `%T'",
154 else if (TREE_CODE (decl) == FIELD_DECL)
155 cp_error ("cannot declare field `%D' to be of type `%T'",
157 else if (TREE_CODE (decl) == FUNCTION_DECL
158 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
159 cp_error ("invalid return type for member function `%#D'", decl);
160 else if (TREE_CODE (decl) == FUNCTION_DECL)
161 cp_error ("invalid return type for function `%#D'", decl);
164 cp_error ("cannot allocate an object of type `%T'", type);
166 /* Only go through this once. */
167 if (TREE_PURPOSE (u) == NULL_TREE)
169 TREE_PURPOSE (u) = error_mark_node;
171 error (" since the following virtual functions are abstract:");
172 for (tu = u; tu; tu = TREE_CHAIN (tu))
173 cp_error_at ("\t%#D", TREE_VALUE (tu));
176 cp_error (" since type `%T' has abstract virtual functions", type);
181 /* Print an error message for invalid use of an incomplete type.
182 VALUE is the expression that was used (or 0 if that isn't known)
183 and TYPE is the type that was invalid. */
186 incomplete_type_error (value, type)
190 /* Avoid duplicate error message. */
191 if (TREE_CODE (type) == ERROR_MARK)
195 /* We must print an error message. Be clever about what it says. */
197 switch (TREE_CODE (type))
202 cp_error ("invalid use of undefined type `%#T'", type);
203 cp_error_at ("forward declaration of `%#T'", type);
207 cp_error ("invalid use of void expression");
211 if (TYPE_DOMAIN (type))
213 type = TREE_TYPE (type);
216 cp_error ("invalid use of array with unspecified bounds");
221 cp_error ("invalid use of member (did you forget the `&' ?)");
224 case TEMPLATE_TYPE_PARM:
225 cp_error ("invalid use of template type parameter");
229 if (value && TREE_CODE (value) == COMPONENT_REF)
231 else if (value && TREE_CODE (value) == ADDR_EXPR)
232 cp_error ("address of overloaded function with no contextual type information");
233 else if (value && TREE_CODE (value) == OVERLOAD)
234 cp_error ("overloaded function with no contextual type information");
236 cp_error ("insufficient contextual information to determine type");
240 my_friendly_abort (108);
243 if (value != 0 && (TREE_CODE (value) == VAR_DECL
244 || TREE_CODE (value) == PARM_DECL))
245 cp_error_at ("incomplete `%D' defined here", value);
248 /* This is a wrapper around fancy_abort, as used in the back end and
249 other front ends. It will also report the magic number assigned to
250 this particular abort. That is for backward compatibility with the
251 old C++ abort handler, which would just report the magic number. */
253 friendly_abort (where, file, line, func)
260 error ("Internal error #%d.", where);
262 fancy_abort (file, line, func);
266 /* Perform appropriate conversions on the initial value of a variable,
267 store it in the declaration DECL,
268 and print any error messages that are appropriate.
269 If the init is invalid, store an ERROR_MARK.
271 C++: Note that INIT might be a TREE_LIST, which would mean that it is
272 a base class initializer for some aggregate type, hopefully compatible
273 with DECL. If INIT is a single element, and DECL is an aggregate
274 type, we silently convert INIT into a TREE_LIST, allowing a constructor
277 If INIT is a TREE_LIST and there is no constructor, turn INIT
278 into a CONSTRUCTOR and use standard initialization techniques.
279 Perhaps a warning should be generated?
281 Returns value of initializer if initialization could not be
282 performed for static variable. In that case, caller must do
286 store_init_value (decl, init)
289 register tree value, type;
291 /* If variable's type was invalidly declared, just ignore it. */
293 type = TREE_TYPE (decl);
294 if (TREE_CODE (type) == ERROR_MARK)
298 /* This breaks arrays, and should not have any effect for other decls. */
299 /* Take care of C++ business up here. */
300 type = TYPE_MAIN_VARIANT (type);
303 if (IS_AGGR_TYPE (type))
305 if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
306 && TREE_CODE (init) != CONSTRUCTOR)
307 my_friendly_abort (109);
309 if (TREE_CODE (init) == TREE_LIST)
311 cp_error ("constructor syntax used, but no constructor declared for type `%T'", type);
312 init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
315 if (TREE_CODE (init) == CONSTRUCTOR)
319 /* Check that we're really an aggregate as ARM 8.4.1 defines it. */
320 if (CLASSTYPE_N_BASECLASSES (type))
321 cp_error_at ("initializer list construction invalid for derived class object `%D'", decl);
322 if (CLASSTYPE_VTBL_PTR (type))
323 cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl);
324 if (TYPE_NEEDS_CONSTRUCTING (type))
326 cp_error_at ("initializer list construction invalid for `%D'", decl);
327 error ("due to the presence of a constructor");
329 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
330 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
332 cp_error_at ("initializer list construction invalid for `%D'", decl);
333 cp_error_at ("due to non-public access of member `%D'", field);
335 for (field = TYPE_METHODS (type); field; field = TREE_CHAIN (field))
336 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
338 cp_error_at ("initializer list construction invalid for `%D'", decl);
339 cp_error_at ("due to non-public access of member `%D'", field);
344 else if (TREE_CODE (init) == TREE_LIST
345 && TREE_TYPE (init) != unknown_type_node)
347 if (TREE_CODE (decl) == RESULT_DECL)
349 if (TREE_CHAIN (init))
351 warning ("comma expression used to initialize return value");
352 init = build_compound_expr (init);
355 init = TREE_VALUE (init);
357 else if (TREE_CODE (init) == TREE_LIST
358 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
360 error ("cannot initialize arrays using this syntax");
365 /* We get here with code like `int a (2);' */
367 if (TREE_CHAIN (init) != NULL_TREE)
369 pedwarn ("initializer list being treated as compound expression");
370 init = build_compound_expr (init);
373 init = TREE_VALUE (init);
377 /* End of special C++ code. */
379 /* Digest the specified initializer into an expression. */
381 value = digest_init (type, init, (tree *) 0);
383 /* Store the expression if valid; else report error. */
385 if (TREE_CODE (value) == ERROR_MARK)
387 /* Other code expects that initializers for objects of types that need
388 constructing never make it into DECL_INITIAL, and passes 'init' to
389 build_aggr_init without checking DECL_INITIAL. So just return. */
390 else if (TYPE_NEEDS_CONSTRUCTING (type))
392 else if (TREE_STATIC (decl)
393 && (! TREE_CONSTANT (value)
394 || ! initializer_constant_valid_p (value, TREE_TYPE (value))
396 /* A STATIC PUBLIC int variable doesn't have to be
397 run time inited when doing pic. (mrs) */
398 /* Since ctors and dtors are the only things that can
399 reference vtables, and they are always written down
400 the vtable definition, we can leave the
401 vtables in initialized data space.
402 However, other initialized data cannot be initialized
403 this way. Instead a global file-level initializer
405 || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl))
410 #if 0 /* No, that's C. jason 9/19/94 */
413 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
415 if (! TREE_CONSTANT (value) || ! TREE_STATIC (value))
416 pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions");
421 /* Store the VALUE in DECL_INITIAL. If we're building a
422 statement-tree we will actually expand the initialization later
423 when we output this function. */
424 DECL_INITIAL (decl) = value;
428 /* Digest the parser output INIT as an initializer for type TYPE.
429 Return a C expression of type TYPE to represent the initial value.
431 If TAIL is nonzero, it points to a variable holding a list of elements
432 of which INIT is the first. We update the list stored there by
433 removing from the head all the elements that we use.
434 Normally this is only one; we use more than one element only if
435 TYPE is an aggregate and INIT is not a constructor. */
438 digest_init (type, init, tail)
439 tree type, init, *tail;
441 enum tree_code code = TREE_CODE (type);
442 tree element = NULL_TREE;
443 tree old_tail_contents = NULL_TREE;
444 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
445 tree node which has no TREE_TYPE. */
448 /* By default, assume we use one element from a list.
449 We correct this later in the sole case where it is not true. */
453 old_tail_contents = *tail;
454 *tail = TREE_CHAIN (*tail);
457 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
458 && TREE_VALUE (init) == error_mark_node))
459 return error_mark_node;
461 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
462 if (TREE_CODE (init) == NON_LVALUE_EXPR)
463 init = TREE_OPERAND (init, 0);
465 if (TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == type)
468 raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
471 && CONSTRUCTOR_ELTS (init) != 0
472 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
474 element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
475 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
476 if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
477 element = TREE_OPERAND (element, 0);
478 if (element == error_mark_node)
482 /* Initialization of an array of chars from a string constant
483 optionally enclosed in braces. */
485 if (code == ARRAY_TYPE)
489 if (TREE_CODE (init) == TREE_LIST)
491 error ("initializing array with parameter list");
492 return error_mark_node;
495 typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
496 if (char_type_p (typ1)
497 && ((init && TREE_CODE (init) == STRING_CST)
498 || (element && TREE_CODE (element) == STRING_CST)))
500 tree string = element ? element : init;
502 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
504 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
506 error ("char-array initialized from wide string");
507 return error_mark_node;
509 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
511 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
513 error ("int-array initialized from non-wide string");
514 return error_mark_node;
517 TREE_TYPE (string) = type;
518 if (TYPE_DOMAIN (type) != 0
519 && TREE_CONSTANT (TYPE_SIZE (type)))
522 = TREE_INT_CST_LOW (TYPE_SIZE (type));
523 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
524 /* In C it is ok to subtract 1 from the length of the string
525 because it's ok to ignore the terminating null char that is
526 counted in the length of the constant, but in C++ this would
528 if (size < TREE_STRING_LENGTH (string))
529 pedwarn ("initializer-string for array of chars is too long");
535 /* Handle scalar types, including conversions,
536 and signature pointers and references. */
538 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
539 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
540 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
541 || TYPE_PTRMEMFUNC_P (type))
547 error ("initializer for scalar variable requires one element");
548 return error_mark_node;
552 while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
554 cp_pedwarn ("braces around scalar initializer for `%T'", type);
555 init = CONSTRUCTOR_ELTS (init);
556 if (TREE_CHAIN (init))
557 cp_pedwarn ("ignoring extra initializers for `%T'", type);
558 init = TREE_VALUE (init);
561 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
562 "initialization", NULL_TREE, 0);
565 /* Come here only for records and arrays (and unions with constructors). */
567 if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
569 cp_error ("variable-sized object of type `%T' may not be initialized",
571 return error_mark_node;
574 if (code == ARRAY_TYPE || IS_AGGR_TYPE_CODE (code))
576 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type))
578 cp_error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
580 return error_mark_node;
582 else if (raw_constructor)
583 return process_init_constructor (type, init, (tree *)0);
584 else if (can_convert_arg (type, TREE_TYPE (init), init)
585 || TYPE_NON_AGGREGATE_CLASS (type))
586 /* These are never initialized from multiple constructor elements. */;
589 *tail = old_tail_contents;
590 return process_init_constructor (type, 0, tail);
593 if (code != ARRAY_TYPE)
595 int flags = LOOKUP_NORMAL;
596 /* Initialization from { } is copy-initialization. */
598 flags |= LOOKUP_ONLYCONVERTING;
600 return convert_for_initialization (NULL_TREE, type, init, flags,
601 "initialization", NULL_TREE, 0);
605 error ("invalid initializer");
606 return error_mark_node;
609 /* Process a constructor for a variable of type TYPE.
610 The constructor elements may be specified either with INIT or with ELTS,
611 only one of which should be non-null.
613 If INIT is specified, it is a CONSTRUCTOR node which is specifically
614 and solely for initializing this datum.
616 If ELTS is specified, it is the address of a variable containing
617 a list of expressions. We take as many elements as we need
618 from the head of the list and update the list.
620 In the resulting constructor, TREE_CONSTANT is set if all elts are
621 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
622 constants that the assembler and linker can compute them. */
625 process_init_constructor (type, init, elts)
626 tree type, init, *elts;
629 /* List of the elements of the result constructor,
631 register tree members = NULL;
638 /* Make TAIL be the list of elements to use for the initialization,
639 no matter how the data was given to us. */
643 if (warn_missing_braces)
644 warning ("aggregate has a partly bracketed initializer");
648 tail = CONSTRUCTOR_ELTS (init);
650 /* Gobble as many elements as needed, and make a constructor or initial value
651 for each element of this aggregate. Chain them together in result.
652 If there are too few, use 0 for each scalar ultimate component. */
654 if (TREE_CODE (type) == ARRAY_TYPE)
656 tree domain = TYPE_DOMAIN (type);
661 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
662 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
665 len = -1; /* Take as many as there are */
667 for (i = 0; len < 0 || i < len; i++)
671 if (TREE_PURPOSE (tail)
672 && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
673 || compare_tree_int (TREE_PURPOSE (tail), i) != 0))
674 sorry ("non-trivial labeled initializers");
676 if (TREE_VALUE (tail) != 0)
679 next1 = digest_init (TREE_TYPE (type),
680 TREE_VALUE (tail), &tail1);
681 if (next1 == error_mark_node)
684 (same_type_ignoring_top_level_qualifiers_p
685 (TREE_TYPE (type), TREE_TYPE (next1)),
687 my_friendly_assert (tail1 == 0
688 || TREE_CODE (tail1) == TREE_LIST, 319);
689 if (tail == tail1 && len < 0)
691 error ("non-empty initializer for array of empty elements");
692 /* Just ignore what we were supposed to use. */
699 next1 = error_mark_node;
700 tail = TREE_CHAIN (tail);
706 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
708 /* If this type needs constructors run for
709 default-initialization, we can't rely on the backend to do it
710 for us, so build up TARGET_EXPRs. If the type in question is
711 a class, just build one up; if it's an array, recurse. */
713 if (IS_AGGR_TYPE (TREE_TYPE (type)))
714 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
716 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE);
717 next1 = digest_init (TREE_TYPE (type), next1, 0);
720 /* The default zero-initialization is fine for us; don't
721 add anything to the CONSTRUCTOR. */
724 if (next1 == error_mark_node)
726 else if (!TREE_CONSTANT (next1))
728 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
730 members = tree_cons (size_int (i), next1, members);
733 else if (TREE_CODE (type) == RECORD_TYPE)
739 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
741 sorry ("initializer list for object of class with virtual base classes");
742 return error_mark_node;
745 if (TYPE_BINFO_BASETYPES (type))
747 sorry ("initializer list for object of class with base classes");
748 return error_mark_node;
751 if (TYPE_POLYMORPHIC_P (type))
753 sorry ("initializer list for object using virtual functions");
754 return error_mark_node;
758 for (field = TYPE_FIELDS (type); field;
759 field = TREE_CHAIN (field))
761 if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
763 members = tree_cons (field, integer_zero_node, members);
767 if (TREE_CODE (field) != FIELD_DECL)
772 if (TREE_PURPOSE (tail)
773 && TREE_PURPOSE (tail) != field
774 && TREE_PURPOSE (tail) != DECL_NAME (field))
775 sorry ("non-trivial labeled initializers");
777 if (TREE_VALUE (tail) != 0)
781 next1 = digest_init (TREE_TYPE (field),
782 TREE_VALUE (tail), &tail1);
783 my_friendly_assert (tail1 == 0
784 || TREE_CODE (tail1) == TREE_LIST, 320);
789 next1 = error_mark_node;
790 tail = TREE_CHAIN (tail);
793 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
795 /* If this type needs constructors run for
796 default-initialization, we can't rely on the backend to do it
797 for us, so build up TARGET_EXPRs. If the type in question is
798 a class, just build one up; if it's an array, recurse. */
800 if (IS_AGGR_TYPE (TREE_TYPE (field)))
801 next1 = build_functional_cast (TREE_TYPE (field),
804 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE,
806 next1 = digest_init (TREE_TYPE (field), next1, 0);
808 /* Warn when some struct elements are implicitly initialized. */
810 cp_warning ("missing initializer for member `%D'", field);
814 if (TREE_READONLY (field))
815 cp_error ("uninitialized const member `%D'", field);
816 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
817 && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
818 cp_error ("member `%D' with uninitialized const fields",
820 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
821 cp_error ("member `%D' is uninitialized reference", field);
823 /* Warn when some struct elements are implicitly initialized
826 cp_warning ("missing initializer for member `%D'", field);
828 /* The default zero-initialization is fine for us; don't
829 add anything to the CONSTRUCTOR. */
833 if (next1 == error_mark_node)
835 else if (!TREE_CONSTANT (next1))
837 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
839 members = tree_cons (field, next1, members);
842 else if (TREE_CODE (type) == UNION_TYPE
843 /* If the initializer was empty, use default zero initialization. */
846 register tree field = TYPE_FIELDS (type);
848 /* Find the first named field. ANSI decided in September 1990
849 that only named fields count here. */
850 while (field && (DECL_NAME (field) == 0
851 || TREE_CODE (field) != FIELD_DECL))
852 field = TREE_CHAIN (field);
854 /* If this element specifies a field, initialize via that field. */
855 if (TREE_PURPOSE (tail) != NULL_TREE)
859 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
860 /* Handle the case of a call by build_c_cast. */
861 field = TREE_PURPOSE (tail), win = 1;
862 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
863 error ("index value instead of field name in union initializer");
867 for (temp = TYPE_FIELDS (type);
869 temp = TREE_CHAIN (temp))
870 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
873 field = temp, win = 1;
875 cp_error ("no field `%D' in union being initialized",
876 TREE_PURPOSE (tail));
879 TREE_VALUE (tail) = error_mark_node;
883 cp_error ("union `%T' with no named members cannot be initialized",
885 TREE_VALUE (tail) = error_mark_node;
888 if (TREE_VALUE (tail) != 0)
892 next1 = digest_init (TREE_TYPE (field),
893 TREE_VALUE (tail), &tail1);
894 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
895 my_friendly_abort (357);
900 next1 = error_mark_node;
901 tail = TREE_CHAIN (tail);
904 if (next1 == error_mark_node)
906 else if (!TREE_CONSTANT (next1))
908 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
910 members = tree_cons (field, next1, members);
913 /* If arguments were specified as a list, just remove the ones we used. */
916 /* If arguments were specified as a constructor,
917 complain unless we used all the elements of the constructor. */
919 pedwarn ("excess elements in aggregate initializer");
922 return error_mark_node;
924 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
926 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
927 if (allconstant) TREE_CONSTANT (result) = 1;
928 if (allconstant && allsimple) TREE_STATIC (result) = 1;
932 /* Given a structure or union value DATUM, construct and return
933 the structure or union component which results from narrowing
934 that value by the type specified in BASETYPE. For example, given the
940 class C : A, B { ... };
948 x.A::ii refers to the ii member of the L part of
949 the A part of the C object named by X. In this case,
950 DATUM would be x, and BASETYPE would be A.
952 Note that this is nonconformant; the standard specifies that first
953 we look up ii in A, then convert x to an L& and pull out the ii part.
954 But narrowing seems to be standard practice, so let's do it anyway. */
957 build_scoped_ref (datum, basetype)
962 tree type = TREE_TYPE (datum);
964 if (datum == error_mark_node)
965 return error_mark_node;
967 /* Don't do this if it would cause an error or if we're being pedantic. */
968 if (! ACCESSIBLY_UNIQUELY_DERIVED_P (basetype, type)
972 ref = build_unary_op (ADDR_EXPR, datum, 0);
973 ref = convert_pointer_to (basetype, ref);
975 return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
978 /* Build a reference to an object specified by the C++ `->' operator.
979 Usually this just involves dereferencing the object, but if the
980 `->' operator is overloaded, then such overloads must be
981 performed until an object which does not have the `->' operator
982 overloaded is found. An error is reported when circular pointer
983 delegation is detected. */
986 build_x_arrow (datum)
989 tree types_memoized = NULL_TREE;
990 register tree rval = datum;
991 tree type = TREE_TYPE (rval);
992 tree last_rval = NULL_TREE;
994 if (type == error_mark_node)
995 return error_mark_node;
997 if (processing_template_decl)
998 return build_min_nt (ARROW_EXPR, rval);
1000 if (TREE_CODE (rval) == OFFSET_REF)
1002 rval = resolve_offset_ref (datum);
1003 type = TREE_TYPE (rval);
1006 if (TREE_CODE (type) == REFERENCE_TYPE)
1008 rval = convert_from_reference (rval);
1009 type = TREE_TYPE (rval);
1012 if (IS_AGGR_TYPE (type))
1014 while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval,
1015 NULL_TREE, NULL_TREE)))
1017 if (rval == error_mark_node)
1018 return error_mark_node;
1020 if (value_member (TREE_TYPE (rval), types_memoized))
1022 error ("circular pointer delegation detected");
1023 return error_mark_node;
1027 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1033 if (last_rval == NULL_TREE)
1035 cp_error ("base operand of `->' has non-pointer type `%T'", type);
1036 return error_mark_node;
1039 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1040 last_rval = convert_from_reference (last_rval);
1043 last_rval = default_conversion (rval);
1045 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1046 return build_indirect_ref (last_rval, NULL_PTR);
1049 error ("result of `operator->()' yields non-pointer result");
1051 error ("base operand of `->' is not a pointer");
1052 return error_mark_node;
1055 /* Make an expression to refer to the COMPONENT field of
1056 structure or union value DATUM. COMPONENT is an arbitrary
1057 expression. DATUM has not already been checked out to be of
1060 For C++, COMPONENT may be a TREE_LIST. This happens when we must
1061 return an object of member type to a method of the current class,
1062 but there is not yet enough typing information to know which one.
1063 As a special case, if there is only one method by that name,
1064 it is returned. Otherwise we return an expression which other
1065 routines will have to know how to deal with later. */
1068 build_m_component_ref (datum, component)
1069 tree datum, component;
1072 tree objtype = TREE_TYPE (datum);
1076 if (processing_template_decl)
1077 return build_min_nt (DOTSTAR_EXPR, datum, component);
1079 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1081 type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1086 type = TREE_TYPE (TREE_TYPE (component));
1087 rettype = TREE_TYPE (type);
1090 if (datum == error_mark_node || component == error_mark_node)
1091 return error_mark_node;
1093 if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
1095 cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type);
1096 return error_mark_node;
1099 if (TREE_CODE (objtype) == REFERENCE_TYPE)
1100 objtype = TREE_TYPE (objtype);
1101 objtype = TYPE_MAIN_VARIANT (objtype);
1103 if (! IS_AGGR_TYPE (objtype))
1105 cp_error ("cannot apply member pointer `%E' to `%E'", component, datum);
1106 cp_error ("which is of non-aggregate type `%T'", objtype);
1107 return error_mark_node;
1110 binfo = get_binfo (TYPE_METHOD_BASETYPE (type), objtype, 1);
1111 if (binfo == NULL_TREE)
1113 cp_error ("member type `%T::' incompatible with object type `%T'",
1114 TYPE_METHOD_BASETYPE (type), objtype);
1115 return error_mark_node;
1117 else if (binfo == error_mark_node)
1118 return error_mark_node;
1120 component = build (OFFSET_REF, rettype, datum, component);
1121 if (TREE_CODE (type) == OFFSET_TYPE)
1122 component = resolve_offset_ref (component);
1126 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1129 build_functional_cast (exp, parms)
1133 /* This is either a call to a constructor,
1134 or a C cast in C++'s `functional' notation. */
1137 if (exp == error_mark_node || parms == error_mark_node)
1138 return error_mark_node;
1140 if (TREE_CODE (exp) == IDENTIFIER_NODE)
1142 if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1143 /* Either an enum or an aggregate type. */
1144 type = IDENTIFIER_TYPE_VALUE (exp);
1147 type = lookup_name (exp, 1);
1148 if (!type || TREE_CODE (type) != TYPE_DECL)
1150 cp_error ("`%T' fails to be a typedef or built-in type", exp);
1151 return error_mark_node;
1153 type = TREE_TYPE (type);
1156 else if (TREE_CODE (exp) == TYPE_DECL)
1157 type = TREE_TYPE (exp);
1161 if (processing_template_decl)
1162 return build_min (CAST_EXPR, type, parms);
1164 if (! IS_AGGR_TYPE (type))
1166 /* this must build a C cast */
1167 if (parms == NULL_TREE)
1168 parms = integer_zero_node;
1171 if (TREE_CHAIN (parms) != NULL_TREE)
1172 pedwarn ("initializer list being treated as compound expression");
1173 parms = build_compound_expr (parms);
1176 return build_c_cast (type, parms);
1179 /* Prepare to evaluate as a call to a constructor. If this expression
1180 is actually used, for example,
1182 return X (arg1, arg2, ...);
1184 then the slot being initialized will be filled in. */
1186 if (!complete_type_or_else (type, NULL_TREE))
1187 return error_mark_node;
1188 if (abstract_virtuals_error (NULL_TREE, type))
1189 return error_mark_node;
1191 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1192 return build_c_cast (type, TREE_VALUE (parms));
1194 /* We need to zero-initialize POD types. Let's do that for everything
1195 that doesn't need a constructor. */
1196 if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1197 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1199 exp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
1200 return get_target_expr (exp);
1203 exp = build_method_call (NULL_TREE, complete_ctor_identifier, parms,
1204 TYPE_BINFO (type), LOOKUP_NORMAL);
1206 if (exp == error_mark_node)
1207 return error_mark_node;
1209 return build_cplus_new (type, exp);
1213 /* Complain about defining new types in inappropriate places. We give an
1214 exception for C-style casts, to accommodate GNU C stylings. */
1217 check_for_new_type (string, inptree)
1219 flagged_type_tree inptree;
1221 if (inptree.new_type_flag
1222 && (pedantic || strcmp (string, "cast") != 0))
1223 pedwarn ("ISO C++ forbids defining types within %s", string);
1226 /* Add new exception specifier SPEC, to the LIST we currently have.
1227 If it's already in LIST then do nothing.
1228 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1229 know what we're doing. */
1232 add_exception_specifier (list, spec, complain)
1240 if (spec == error_mark_node)
1243 my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317);
1245 /* [except.spec] 1, type in an exception specifier shall not be
1246 incomplete, or pointer or ref to incomplete other than pointer
1248 is_ptr = TREE_CODE (core) == POINTER_TYPE;
1249 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1250 core = TREE_TYPE (core);
1253 else if (VOID_TYPE_P (core))
1255 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1258 ok = COMPLETE_TYPE_P (complete_type (core));
1264 for (probe = list; probe; probe = TREE_CHAIN (probe))
1265 if (same_type_p (TREE_VALUE (probe), spec))
1269 spec = build_tree_list (NULL_TREE, spec);
1270 TREE_CHAIN (spec) = list;
1275 incomplete_type_error (NULL_TREE, core);