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 *));
43 static void ack PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
45 /* Print an error message stemming from an attempt to use
46 BASETYPE as a base class for TYPE. */
49 error_not_base_type (basetype, type)
52 if (TREE_CODE (basetype) == FUNCTION_DECL)
53 basetype = DECL_CONTEXT (basetype);
54 cp_error ("type `%T' is not a base type for type `%T'", basetype, type);
55 return error_mark_node;
59 binfo_or_else (parent_or_type, type)
60 tree parent_or_type, type;
63 if (TYPE_MAIN_VARIANT (parent_or_type) == TYPE_MAIN_VARIANT (type))
64 return TYPE_BINFO (parent_or_type);
65 if ((binfo = get_binfo (parent_or_type, TYPE_MAIN_VARIANT (type), 0)))
67 if (binfo == error_mark_node)
71 error_not_base_type (parent_or_type, type);
75 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
76 value may not be changed thereafter. Thus, we emit hard errors for these,
77 rather than just pedwarns. If `SOFT' is 1, then we just pedwarn. (For
78 example, conversions to references.) */
81 readonly_error (arg, string, soft)
87 void (*fn) PARAMS ((const char *, ...));
94 if (TREE_CODE (arg) == COMPONENT_REF)
96 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
97 fmt = "%s of data-member `%D' in read-only structure";
99 fmt = "%s of read-only data-member `%D'";
100 (*fn) (fmt, string, TREE_OPERAND (arg, 1));
102 else if (TREE_CODE (arg) == VAR_DECL)
104 if (DECL_LANG_SPECIFIC (arg)
105 && DECL_IN_AGGR_P (arg)
106 && !TREE_STATIC (arg))
107 fmt = "%s of constant field `%D'";
109 fmt = "%s of read-only variable `%D'";
110 (*fn) (fmt, string, arg);
112 else if (TREE_CODE (arg) == PARM_DECL)
113 (*fn) ("%s of read-only parameter `%D'", string, arg);
114 else if (TREE_CODE (arg) == INDIRECT_REF
115 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
116 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
117 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
118 (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0));
119 else if (TREE_CODE (arg) == RESULT_DECL)
120 (*fn) ("%s of read-only named return value `%D'", string, arg);
121 else if (TREE_CODE (arg) == FUNCTION_DECL)
122 (*fn) ("%s of function `%D'", string, arg);
124 (*fn) ("%s of read-only location", string);
127 /* If TYPE has abstract virtual functions, issue an error about trying
128 to create an object of that type. DECL is the object declared, or
129 NULL_TREE if the declaration is unavailable. Returns 1 if an error
130 occurred; zero if all was well. */
133 abstract_virtuals_error (decl, type)
140 if (!CLASS_TYPE_P (type) || !CLASSTYPE_PURE_VIRTUALS (type))
143 u = CLASSTYPE_PURE_VIRTUALS (type);
146 if (TREE_CODE (decl) == RESULT_DECL)
149 if (TREE_CODE (decl) == VAR_DECL)
150 cp_error ("cannot declare variable `%D' to be of type `%T'",
152 else if (TREE_CODE (decl) == PARM_DECL)
153 cp_error ("cannot declare parameter `%D' to be of type `%T'",
155 else if (TREE_CODE (decl) == FIELD_DECL)
156 cp_error ("cannot declare field `%D' to be of type `%T'",
158 else if (TREE_CODE (decl) == FUNCTION_DECL
159 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
160 cp_error ("invalid return type for member function `%#D'", decl);
161 else if (TREE_CODE (decl) == FUNCTION_DECL)
162 cp_error ("invalid return type for function `%#D'", decl);
165 cp_error ("cannot allocate an object of type `%T'", type);
167 /* Only go through this once. */
168 if (TREE_PURPOSE (u) == NULL_TREE)
170 TREE_PURPOSE (u) = error_mark_node;
172 error (" since the following virtual functions are abstract:");
173 for (tu = u; tu; tu = TREE_CHAIN (tu))
174 cp_error_at ("\t%#D", TREE_VALUE (tu));
177 cp_error (" since type `%T' has abstract virtual functions", type);
182 /* Print an error message for invalid use of an incomplete type.
183 VALUE is the expression that was used (or 0 if that isn't known)
184 and TYPE is the type that was invalid. */
187 incomplete_type_error (value, type)
191 /* Avoid duplicate error message. */
192 if (TREE_CODE (type) == ERROR_MARK)
196 /* We must print an error message. Be clever about what it says. */
198 switch (TREE_CODE (type))
203 cp_error ("invalid use of undefined type `%#T'", type);
204 cp_error_at ("forward declaration of `%#T'", type);
208 cp_error ("invalid use of void expression");
212 if (TYPE_DOMAIN (type))
214 type = TREE_TYPE (type);
217 cp_error ("invalid use of array with unspecified bounds");
222 cp_error ("invalid use of member (did you forget the `&' ?)");
225 case TEMPLATE_TYPE_PARM:
226 cp_error ("invalid use of template type parameter");
230 if (value && TREE_CODE (value) == COMPONENT_REF)
232 else if (value && TREE_CODE (value) == ADDR_EXPR)
233 cp_error ("address of overloaded function with no contextual type information");
234 else if (value && TREE_CODE (value) == OVERLOAD)
235 cp_error ("overloaded function with no contextual type information");
237 cp_error ("insufficient contextual information to determine type");
241 my_friendly_abort (108);
244 if (value != 0 && (TREE_CODE (value) == VAR_DECL
245 || TREE_CODE (value) == PARM_DECL))
246 cp_error_at ("incomplete `%D' defined here", value);
249 /* Like error(), but don't call report_error_function(). */
252 ack VPARAMS ((const char *msg, ...))
254 #ifndef ANSI_PROTOTYPES
261 #ifndef ANSI_PROTOTYPES
262 msg = va_arg (ap, const char *);
266 fprintf (stderr, "%s:%d: ", input_filename, lineno);
268 fprintf (stderr, "%s: ", progname);
270 vfprintf (stderr, msg, ap);
273 fprintf (stderr, "\n");
276 /* There are times when the compiler can get very confused, confused
277 to the point of giving up by aborting, simply because of previous
278 input errors. It is much better to have the user go back and
279 correct those errors first, and see if it makes us happier, than it
280 is to abort on him. This is because when one has a 10,000 line
281 program, and the compiler comes back with ``core dump'', the user
282 is left not knowing even where to begin to fix things and no place
283 to even try and work around things.
285 The parameter is to uniquely identify the problem to the user, so
286 that they can say, I am having problem 59, and know that fix 7 will
287 probably solve their problem. Or, we can document what problem
288 59 is, so they can understand how to work around it, should they
291 We used to tell people to "fix the above error[s] and try recompiling
292 the program" via a call to fatal, but that message tended to look
293 silly. So instead, we just do the equivalent of a call to fatal in the
294 same situation (call exit).
296 We used to assign sequential numbers for the aborts; now we use an
297 encoding of the date the abort was added, since that has more meaning
298 when we only see the error message. */
300 static int abortcount = 0;
303 my_friendly_abort (i)
306 /* if the previous error came through here, i.e. report_error_function
307 ended up calling us again, don't just exit; we want a diagnostic of
310 current_function_decl = NULL_TREE;
311 else if (errorcount > 0 || sorrycount > 0)
316 ack ("Internal compiler error.");
318 ack ("Internal compiler error %d.", i);
319 ack ("Please submit a full bug report.");
320 ack ("See %s for instructions.", GCCBUGURL);
323 error ("confused by earlier errors, bailing out");
330 error ("Internal compiler error.");
332 error ("Internal compiler error %d.", i);
334 error ("Please submit a full bug report.");
335 fatal ("See %s for instructions.", GCCBUGURL);
339 my_friendly_assert (cond, where)
343 my_friendly_abort (where);
346 /* Perform appropriate conversions on the initial value of a variable,
347 store it in the declaration DECL,
348 and print any error messages that are appropriate.
349 If the init is invalid, store an ERROR_MARK.
351 C++: Note that INIT might be a TREE_LIST, which would mean that it is
352 a base class initializer for some aggregate type, hopefully compatible
353 with DECL. If INIT is a single element, and DECL is an aggregate
354 type, we silently convert INIT into a TREE_LIST, allowing a constructor
357 If INIT is a TREE_LIST and there is no constructor, turn INIT
358 into a CONSTRUCTOR and use standard initialization techniques.
359 Perhaps a warning should be generated?
361 Returns value of initializer if initialization could not be
362 performed for static variable. In that case, caller must do
366 store_init_value (decl, init)
369 register tree value, type;
371 /* If variable's type was invalidly declared, just ignore it. */
373 type = TREE_TYPE (decl);
374 if (TREE_CODE (type) == ERROR_MARK)
378 /* This breaks arrays, and should not have any effect for other decls. */
379 /* Take care of C++ business up here. */
380 type = TYPE_MAIN_VARIANT (type);
383 if (IS_AGGR_TYPE (type))
385 if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
386 && TREE_CODE (init) != CONSTRUCTOR)
387 my_friendly_abort (109);
389 if (TREE_CODE (init) == TREE_LIST)
391 cp_error ("constructor syntax used, but no constructor declared for type `%T'", type);
392 init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
395 if (TREE_CODE (init) == CONSTRUCTOR)
399 /* Check that we're really an aggregate as ARM 8.4.1 defines it. */
400 if (CLASSTYPE_N_BASECLASSES (type))
401 cp_error_at ("initializer list construction invalid for derived class object `%D'", decl);
402 if (CLASSTYPE_VTBL_PTR (type))
403 cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl);
404 if (TYPE_NEEDS_CONSTRUCTING (type))
406 cp_error_at ("initializer list construction invalid for `%D'", decl);
407 error ("due to the presence of a constructor");
409 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
410 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
412 cp_error_at ("initializer list construction invalid for `%D'", decl);
413 cp_error_at ("due to non-public access of member `%D'", field);
415 for (field = TYPE_METHODS (type); field; field = TREE_CHAIN (field))
416 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
418 cp_error_at ("initializer list construction invalid for `%D'", decl);
419 cp_error_at ("due to non-public access of member `%D'", field);
424 else if (TREE_CODE (init) == TREE_LIST
425 && TREE_TYPE (init) != unknown_type_node)
427 if (TREE_CODE (decl) == RESULT_DECL)
429 if (TREE_CHAIN (init))
431 warning ("comma expression used to initialize return value");
432 init = build_compound_expr (init);
435 init = TREE_VALUE (init);
437 else if (TREE_CODE (init) == TREE_LIST
438 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
440 error ("cannot initialize arrays using this syntax");
445 /* We get here with code like `int a (2);' */
447 if (TREE_CHAIN (init) != NULL_TREE)
449 pedwarn ("initializer list being treated as compound expression");
450 init = build_compound_expr (init);
453 init = TREE_VALUE (init);
457 /* End of special C++ code. */
459 /* Digest the specified initializer into an expression. */
461 value = digest_init (type, init, (tree *) 0);
463 /* Store the expression if valid; else report error. */
465 if (TREE_CODE (value) == ERROR_MARK)
467 /* Other code expects that initializers for objects of types that need
468 constructing never make it into DECL_INITIAL, and passes 'init' to
469 build_aggr_init without checking DECL_INITIAL. So just return. */
470 else if (TYPE_NEEDS_CONSTRUCTING (type))
472 else if (TREE_STATIC (decl)
473 && (! TREE_CONSTANT (value)
474 || ! initializer_constant_valid_p (value, TREE_TYPE (value))
476 /* A STATIC PUBLIC int variable doesn't have to be
477 run time inited when doing pic. (mrs) */
478 /* Since ctors and dtors are the only things that can
479 reference vtables, and they are always written down
480 the vtable definition, we can leave the
481 vtables in initialized data space.
482 However, other initialized data cannot be initialized
483 this way. Instead a global file-level initializer
485 || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl))
490 #if 0 /* No, that's C. jason 9/19/94 */
493 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
495 if (! TREE_CONSTANT (value) || ! TREE_STATIC (value))
496 pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions");
501 /* Store the VALUE in DECL_INITIAL. If we're building a
502 statement-tree we will actually expand the initialization later
503 when we output this function. */
504 DECL_INITIAL (decl) = value;
508 /* Digest the parser output INIT as an initializer for type TYPE.
509 Return a C expression of type TYPE to represent the initial value.
511 If TAIL is nonzero, it points to a variable holding a list of elements
512 of which INIT is the first. We update the list stored there by
513 removing from the head all the elements that we use.
514 Normally this is only one; we use more than one element only if
515 TYPE is an aggregate and INIT is not a constructor. */
518 digest_init (type, init, tail)
519 tree type, init, *tail;
521 enum tree_code code = TREE_CODE (type);
522 tree element = NULL_TREE;
523 tree old_tail_contents = NULL_TREE;
524 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
525 tree node which has no TREE_TYPE. */
528 /* By default, assume we use one element from a list.
529 We correct this later in the sole case where it is not true. */
533 old_tail_contents = *tail;
534 *tail = TREE_CHAIN (*tail);
537 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
538 && TREE_VALUE (init) == error_mark_node))
539 return error_mark_node;
541 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
542 if (TREE_CODE (init) == NON_LVALUE_EXPR)
543 init = TREE_OPERAND (init, 0);
545 if (TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == type)
548 raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
551 && CONSTRUCTOR_ELTS (init) != 0
552 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
554 element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
555 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
556 if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
557 element = TREE_OPERAND (element, 0);
558 if (element == error_mark_node)
562 /* Initialization of an array of chars from a string constant
563 optionally enclosed in braces. */
565 if (code == ARRAY_TYPE)
569 if (TREE_CODE (init) == TREE_LIST)
571 error ("initializing array with parameter list");
572 return error_mark_node;
575 typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
576 if ((typ1 == char_type_node
577 || typ1 == signed_char_type_node
578 || typ1 == unsigned_char_type_node
579 || typ1 == unsigned_wchar_type_node
580 || typ1 == signed_wchar_type_node)
581 && ((init && TREE_CODE (init) == STRING_CST)
582 || (element && TREE_CODE (element) == STRING_CST)))
584 tree string = element ? element : init;
586 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
588 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
590 error ("char-array initialized from wide string");
591 return error_mark_node;
593 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
595 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
597 error ("int-array initialized from non-wide string");
598 return error_mark_node;
601 TREE_TYPE (string) = type;
602 if (TYPE_DOMAIN (type) != 0
603 && TREE_CONSTANT (TYPE_SIZE (type)))
606 = TREE_INT_CST_LOW (TYPE_SIZE (type));
607 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
608 /* In C it is ok to subtract 1 from the length of the string
609 because it's ok to ignore the terminating null char that is
610 counted in the length of the constant, but in C++ this would
612 if (size < TREE_STRING_LENGTH (string))
613 pedwarn ("initializer-string for array of chars is too long");
619 /* Handle scalar types, including conversions,
620 and signature pointers and references. */
622 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
623 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
624 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
625 || TYPE_PTRMEMFUNC_P (type))
631 error ("initializer for scalar variable requires one element");
632 return error_mark_node;
636 while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
638 cp_pedwarn ("braces around scalar initializer for `%T'", type);
639 init = CONSTRUCTOR_ELTS (init);
640 if (TREE_CHAIN (init))
641 cp_pedwarn ("ignoring extra initializers for `%T'", type);
642 init = TREE_VALUE (init);
645 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
646 "initialization", NULL_TREE, 0);
649 /* Come here only for records and arrays (and unions with constructors). */
651 if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
653 cp_error ("variable-sized object of type `%T' may not be initialized",
655 return error_mark_node;
658 if (code == ARRAY_TYPE || IS_AGGR_TYPE_CODE (code))
660 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type))
662 cp_error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
664 return error_mark_node;
666 else if (raw_constructor)
667 return process_init_constructor (type, init, (tree *)0);
668 else if (can_convert_arg (type, TREE_TYPE (init), init)
669 || TYPE_NON_AGGREGATE_CLASS (type))
670 /* These are never initialized from multiple constructor elements. */;
673 *tail = old_tail_contents;
674 return process_init_constructor (type, 0, tail);
677 if (code != ARRAY_TYPE)
679 int flags = LOOKUP_NORMAL;
680 /* Initialization from { } is copy-initialization. */
682 flags |= LOOKUP_ONLYCONVERTING;
684 return convert_for_initialization (NULL_TREE, type, init, flags,
685 "initialization", NULL_TREE, 0);
689 error ("invalid initializer");
690 return error_mark_node;
693 /* Process a constructor for a variable of type TYPE.
694 The constructor elements may be specified either with INIT or with ELTS,
695 only one of which should be non-null.
697 If INIT is specified, it is a CONSTRUCTOR node which is specifically
698 and solely for initializing this datum.
700 If ELTS is specified, it is the address of a variable containing
701 a list of expressions. We take as many elements as we need
702 from the head of the list and update the list.
704 In the resulting constructor, TREE_CONSTANT is set if all elts are
705 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
706 constants that the assembler and linker can compute them. */
709 process_init_constructor (type, init, elts)
710 tree type, init, *elts;
713 /* List of the elements of the result constructor,
715 register tree members = NULL;
722 /* Make TAIL be the list of elements to use for the initialization,
723 no matter how the data was given to us. */
727 if (warn_missing_braces)
728 warning ("aggregate has a partly bracketed initializer");
732 tail = CONSTRUCTOR_ELTS (init);
734 /* Gobble as many elements as needed, and make a constructor or initial value
735 for each element of this aggregate. Chain them together in result.
736 If there are too few, use 0 for each scalar ultimate component. */
738 if (TREE_CODE (type) == ARRAY_TYPE)
740 tree domain = TYPE_DOMAIN (type);
745 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
746 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
749 len = -1; /* Take as many as there are */
751 for (i = 0; len < 0 || i < len; i++)
755 if (TREE_PURPOSE (tail)
756 && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
757 || compare_tree_int (TREE_PURPOSE (tail), i) != 0))
758 sorry ("non-trivial labeled initializers");
760 if (TREE_VALUE (tail) != 0)
763 next1 = digest_init (TREE_TYPE (type),
764 TREE_VALUE (tail), &tail1);
765 if (next1 == error_mark_node)
768 (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
769 TYPE_MAIN_VARIANT (TREE_TYPE (next1))),
771 my_friendly_assert (tail1 == 0
772 || TREE_CODE (tail1) == TREE_LIST, 319);
773 if (tail == tail1 && len < 0)
775 error ("non-empty initializer for array of empty elements");
776 /* Just ignore what we were supposed to use. */
783 next1 = error_mark_node;
784 tail = TREE_CHAIN (tail);
790 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
792 /* If this type needs constructors run for
793 default-initialization, we can't rely on the backend to do it
794 for us, so build up TARGET_EXPRs. If the type in question is
795 a class, just build one up; if it's an array, recurse. */
797 if (IS_AGGR_TYPE (TREE_TYPE (type)))
798 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
800 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE);
801 next1 = digest_init (TREE_TYPE (type), next1, 0);
804 /* The default zero-initialization is fine for us; don't
805 add anything to the CONSTRUCTOR. */
808 if (next1 == error_mark_node)
810 else if (!TREE_CONSTANT (next1))
812 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
814 members = tree_cons (size_int (i), next1, members);
817 else if (TREE_CODE (type) == RECORD_TYPE)
823 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
825 sorry ("initializer list for object of class with virtual base classes");
826 return error_mark_node;
829 if (TYPE_BINFO_BASETYPES (type))
831 sorry ("initializer list for object of class with base classes");
832 return error_mark_node;
835 if (TYPE_POLYMORPHIC_P (type))
837 sorry ("initializer list for object using virtual functions");
838 return error_mark_node;
842 for (field = TYPE_FIELDS (type); field;
843 field = TREE_CHAIN (field))
845 if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
847 members = tree_cons (field, integer_zero_node, members);
851 if (TREE_CODE (field) != FIELD_DECL)
856 if (TREE_PURPOSE (tail)
857 && TREE_PURPOSE (tail) != field
858 && TREE_PURPOSE (tail) != DECL_NAME (field))
859 sorry ("non-trivial labeled initializers");
861 if (TREE_VALUE (tail) != 0)
865 next1 = digest_init (TREE_TYPE (field),
866 TREE_VALUE (tail), &tail1);
867 my_friendly_assert (tail1 == 0
868 || TREE_CODE (tail1) == TREE_LIST, 320);
873 next1 = error_mark_node;
874 tail = TREE_CHAIN (tail);
877 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
879 /* If this type needs constructors run for
880 default-initialization, we can't rely on the backend to do it
881 for us, so build up TARGET_EXPRs. If the type in question is
882 a class, just build one up; if it's an array, recurse. */
884 if (IS_AGGR_TYPE (TREE_TYPE (field)))
885 next1 = build_functional_cast (TREE_TYPE (field),
888 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE,
890 next1 = digest_init (TREE_TYPE (field), next1, 0);
892 /* Warn when some struct elements are implicitly initialized. */
894 cp_warning ("missing initializer for member `%D'", field);
898 if (TREE_READONLY (field))
899 cp_error ("uninitialized const member `%D'", field);
900 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
901 && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
902 cp_error ("member `%D' with uninitialized const fields",
904 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
905 cp_error ("member `%D' is uninitialized reference", field);
907 /* Warn when some struct elements are implicitly initialized
910 cp_warning ("missing initializer for member `%D'", field);
912 /* The default zero-initialization is fine for us; don't
913 add anything to the CONSTRUCTOR. */
917 if (next1 == error_mark_node)
919 else if (!TREE_CONSTANT (next1))
921 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
923 members = tree_cons (field, next1, members);
926 else if (TREE_CODE (type) == UNION_TYPE
927 /* If the initializer was empty, use default zero initialization. */
930 register tree field = TYPE_FIELDS (type);
932 /* Find the first named field. ANSI decided in September 1990
933 that only named fields count here. */
934 while (field && (DECL_NAME (field) == 0
935 || TREE_CODE (field) != FIELD_DECL))
936 field = TREE_CHAIN (field);
938 /* If this element specifies a field, initialize via that field. */
939 if (TREE_PURPOSE (tail) != NULL_TREE)
943 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
944 /* Handle the case of a call by build_c_cast. */
945 field = TREE_PURPOSE (tail), win = 1;
946 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
947 error ("index value instead of field name in union initializer");
951 for (temp = TYPE_FIELDS (type);
953 temp = TREE_CHAIN (temp))
954 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
957 field = temp, win = 1;
959 cp_error ("no field `%D' in union being initialized",
960 TREE_PURPOSE (tail));
963 TREE_VALUE (tail) = error_mark_node;
967 cp_error ("union `%T' with no named members cannot be initialized",
969 TREE_VALUE (tail) = error_mark_node;
972 if (TREE_VALUE (tail) != 0)
976 next1 = digest_init (TREE_TYPE (field),
977 TREE_VALUE (tail), &tail1);
978 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
979 my_friendly_abort (357);
984 next1 = error_mark_node;
985 tail = TREE_CHAIN (tail);
988 if (next1 == error_mark_node)
990 else if (!TREE_CONSTANT (next1))
992 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
994 members = tree_cons (field, next1, members);
997 /* If arguments were specified as a list, just remove the ones we used. */
1000 /* If arguments were specified as a constructor,
1001 complain unless we used all the elements of the constructor. */
1003 pedwarn ("excess elements in aggregate initializer");
1006 return error_mark_node;
1008 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
1010 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1011 if (allconstant) TREE_CONSTANT (result) = 1;
1012 if (allconstant && allsimple) TREE_STATIC (result) = 1;
1016 /* Given a structure or union value DATUM, construct and return
1017 the structure or union component which results from narrowing
1018 that value by the type specified in BASETYPE. For example, given the
1021 class L { int ii; };
1022 class A : L { ... };
1023 class B : L { ... };
1024 class C : A, B { ... };
1032 x.A::ii refers to the ii member of the L part of
1033 the A part of the C object named by X. In this case,
1034 DATUM would be x, and BASETYPE would be A.
1036 Note that this is nonconformant; the standard specifies that first
1037 we look up ii in A, then convert x to an L& and pull out the ii part.
1038 But narrowing seems to be standard practice, so let's do it anyway. */
1041 build_scoped_ref (datum, basetype)
1046 tree type = TREE_TYPE (datum);
1048 if (datum == error_mark_node)
1049 return error_mark_node;
1051 /* Don't do this if it would cause an error or if we're being pedantic. */
1052 if (! ACCESSIBLY_UNIQUELY_DERIVED_P (basetype, type)
1056 ref = build_unary_op (ADDR_EXPR, datum, 0);
1057 ref = convert_pointer_to (basetype, ref);
1059 return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1062 /* Build a reference to an object specified by the C++ `->' operator.
1063 Usually this just involves dereferencing the object, but if the
1064 `->' operator is overloaded, then such overloads must be
1065 performed until an object which does not have the `->' operator
1066 overloaded is found. An error is reported when circular pointer
1067 delegation is detected. */
1070 build_x_arrow (datum)
1073 tree types_memoized = NULL_TREE;
1074 register tree rval = datum;
1075 tree type = TREE_TYPE (rval);
1076 tree last_rval = NULL_TREE;
1078 if (type == error_mark_node)
1079 return error_mark_node;
1081 if (processing_template_decl)
1082 return build_min_nt (ARROW_EXPR, rval);
1084 if (TREE_CODE (rval) == OFFSET_REF)
1086 rval = resolve_offset_ref (datum);
1087 type = TREE_TYPE (rval);
1090 if (TREE_CODE (type) == REFERENCE_TYPE)
1092 rval = convert_from_reference (rval);
1093 type = TREE_TYPE (rval);
1096 if (IS_AGGR_TYPE (type))
1098 while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval,
1099 NULL_TREE, NULL_TREE)))
1101 if (rval == error_mark_node)
1102 return error_mark_node;
1104 if (value_member (TREE_TYPE (rval), types_memoized))
1106 error ("circular pointer delegation detected");
1107 return error_mark_node;
1111 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1117 if (last_rval == NULL_TREE)
1119 cp_error ("base operand of `->' has non-pointer type `%T'", type);
1120 return error_mark_node;
1123 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1124 last_rval = convert_from_reference (last_rval);
1127 last_rval = default_conversion (rval);
1129 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1130 return build_indirect_ref (last_rval, NULL_PTR);
1133 error ("result of `operator->()' yields non-pointer result");
1135 error ("base operand of `->' is not a pointer");
1136 return error_mark_node;
1139 /* Make an expression to refer to the COMPONENT field of
1140 structure or union value DATUM. COMPONENT is an arbitrary
1141 expression. DATUM has not already been checked out to be of
1144 For C++, COMPONENT may be a TREE_LIST. This happens when we must
1145 return an object of member type to a method of the current class,
1146 but there is not yet enough typing information to know which one.
1147 As a special case, if there is only one method by that name,
1148 it is returned. Otherwise we return an expression which other
1149 routines will have to know how to deal with later. */
1152 build_m_component_ref (datum, component)
1153 tree datum, component;
1156 tree objtype = TREE_TYPE (datum);
1160 if (processing_template_decl)
1161 return build_min_nt (DOTSTAR_EXPR, datum, component);
1163 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1165 type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1170 type = TREE_TYPE (TREE_TYPE (component));
1171 rettype = TREE_TYPE (type);
1174 if (datum == error_mark_node || component == error_mark_node)
1175 return error_mark_node;
1177 if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
1179 cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type);
1180 return error_mark_node;
1183 if (TREE_CODE (objtype) == REFERENCE_TYPE)
1184 objtype = TREE_TYPE (objtype);
1185 objtype = TYPE_MAIN_VARIANT (objtype);
1187 if (! IS_AGGR_TYPE (objtype))
1189 cp_error ("cannot apply member pointer `%E' to `%E'", component, datum);
1190 cp_error ("which is of non-aggregate type `%T'", objtype);
1191 return error_mark_node;
1194 binfo = get_binfo (TYPE_METHOD_BASETYPE (type), objtype, 1);
1195 if (binfo == NULL_TREE)
1197 cp_error ("member type `%T::' incompatible with object type `%T'",
1198 TYPE_METHOD_BASETYPE (type), objtype);
1199 return error_mark_node;
1201 else if (binfo == error_mark_node)
1202 return error_mark_node;
1204 component = build (OFFSET_REF, rettype, datum, component);
1205 if (TREE_CODE (type) == OFFSET_TYPE)
1206 component = resolve_offset_ref (component);
1210 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1213 build_functional_cast (exp, parms)
1217 /* This is either a call to a constructor,
1218 or a C cast in C++'s `functional' notation. */
1221 if (exp == error_mark_node || parms == error_mark_node)
1222 return error_mark_node;
1224 if (TREE_CODE (exp) == IDENTIFIER_NODE)
1226 if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1227 /* Either an enum or an aggregate type. */
1228 type = IDENTIFIER_TYPE_VALUE (exp);
1231 type = lookup_name (exp, 1);
1232 if (!type || TREE_CODE (type) != TYPE_DECL)
1234 cp_error ("`%T' fails to be a typedef or built-in type", exp);
1235 return error_mark_node;
1237 type = TREE_TYPE (type);
1240 else if (TREE_CODE (exp) == TYPE_DECL)
1241 type = TREE_TYPE (exp);
1245 if (processing_template_decl)
1246 return build_min (CAST_EXPR, type, parms);
1248 if (! IS_AGGR_TYPE (type))
1250 /* this must build a C cast */
1251 if (parms == NULL_TREE)
1252 parms = integer_zero_node;
1255 if (TREE_CHAIN (parms) != NULL_TREE)
1256 pedwarn ("initializer list being treated as compound expression");
1257 parms = build_compound_expr (parms);
1260 return build_c_cast (type, parms);
1263 /* Prepare to evaluate as a call to a constructor. If this expression
1264 is actually used, for example,
1266 return X (arg1, arg2, ...);
1268 then the slot being initialized will be filled in. */
1270 if (!complete_type_or_else (type, NULL_TREE))
1271 return error_mark_node;
1272 if (abstract_virtuals_error (NULL_TREE, type))
1273 return error_mark_node;
1275 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1276 return build_c_cast (type, TREE_VALUE (parms));
1278 /* We need to zero-initialize POD types. Let's do that for everything
1279 that doesn't need a constructor. */
1280 if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1281 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1283 exp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
1284 return get_target_expr (exp);
1287 exp = build_method_call (NULL_TREE, ctor_identifier, parms,
1288 TYPE_BINFO (type), LOOKUP_NORMAL);
1290 if (exp == error_mark_node)
1291 return error_mark_node;
1293 return build_cplus_new (type, exp);
1296 /* Return the character string for the name that encodes the
1297 enumeral value VALUE in the domain TYPE. */
1300 enum_name_string (value, type)
1304 register tree values = TYPE_VALUES (type);
1306 my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324);
1308 while (values && ! tree_int_cst_equal (TREE_VALUE (values), value))
1309 values = TREE_CHAIN (values);
1311 if (values == NULL_TREE)
1313 char *buf = (char *) oballoc (16 + TYPE_NAME_LENGTH (type));
1315 /* Value must have been cast. */
1316 sprintf (buf, "(enum %s)%ld",
1317 TYPE_NAME_STRING (type), (long) TREE_INT_CST_LOW (value));
1320 return IDENTIFIER_POINTER (TREE_PURPOSE (values));
1324 /* Print out a language-specific error message for
1325 (Pascal) case or (C) switch statements.
1326 CODE tells what sort of message to print.
1327 TYPE is the type of the switch index expression.
1328 NEW is the new value that we were trying to add.
1329 OLD is the old value that stopped us from adding it. */
1332 report_case_error (code, type, new_value, old_value)
1335 tree new_value, old_value;
1340 error ("case label not within a switch statement");
1342 error ("default label not within a switch statement");
1348 error ("multiple default labels in one switch");
1351 if (TREE_CODE (new_value) == RANGE_EXPR)
1352 if (TREE_CODE (old_value) == RANGE_EXPR)
1354 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1355 if (TREE_CODE (type) == ENUMERAL_TYPE)
1356 sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1357 enum_name_string (TREE_OPERAND (new_value, 0), type),
1358 enum_name_string (TREE_OPERAND (new_value, 1), type),
1359 enum_name_string (TREE_OPERAND (old_value, 0), type),
1360 enum_name_string (TREE_OPERAND (old_value, 1), type));
1362 sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1363 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1364 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1365 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1366 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
1371 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1372 if (TREE_CODE (type) == ENUMERAL_TYPE)
1373 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1374 enum_name_string (TREE_OPERAND (new_value, 0), type),
1375 enum_name_string (TREE_OPERAND (new_value, 1), type),
1376 enum_name_string (old_value, type));
1378 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1379 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1380 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1381 TREE_INT_CST_LOW (old_value));
1384 else if (TREE_CODE (old_value) == RANGE_EXPR)
1386 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1387 if (TREE_CODE (type) == ENUMERAL_TYPE)
1388 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1389 enum_name_string (TREE_OPERAND (old_value, 0), type),
1390 enum_name_string (TREE_OPERAND (old_value, 1), type),
1391 enum_name_string (new_value, type));
1393 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1394 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1395 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
1396 TREE_INT_CST_LOW (new_value));
1401 if (TREE_CODE (type) == ENUMERAL_TYPE)
1402 error ("duplicate label `%s' in switch statement",
1403 enum_name_string (new_value, type));
1405 error ("duplicate label (%d) in switch statement",
1406 TREE_INT_CST_LOW (new_value));
1411 if (TREE_CODE (type) == ENUMERAL_TYPE)
1412 warning ("case value out of range for enum %s",
1413 TYPE_NAME_STRING (type));
1415 warning ("case value out of range");
1419 if (TREE_CODE (type) == ENUMERAL_TYPE)
1420 error ("range values `%s' and `%s' reversed",
1421 enum_name_string (new_value, type),
1422 enum_name_string (old_value, type));
1424 error ("range values reversed");
1429 /* Complain about defining new types in inappropriate places. We give an
1430 exception for C-style casts, to accommodate GNU C stylings. */
1433 check_for_new_type (string, inptree)
1435 flagged_type_tree inptree;
1437 if (inptree.new_type_flag
1438 && (pedantic || strcmp (string, "cast") != 0))
1439 pedwarn ("ISO C++ forbids defining types within %s", string);
1442 /* Add new exception specifier SPEC, to the LIST we currently have.
1443 If it's already in LIST then do nothing.
1444 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1445 know what we're doing. */
1448 add_exception_specifier (list, spec, complain)
1456 if (spec == error_mark_node)
1459 my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317);
1461 /* [except.spec] 1, type in an exception specifier shall not be
1462 incomplete, or pointer or ref to incomplete other than pointer
1464 is_ptr = TREE_CODE (core) == POINTER_TYPE;
1465 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1466 core = TREE_TYPE (core);
1469 else if (TYPE_MAIN_VARIANT (core) == void_type_node)
1471 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1474 ok = COMPLETE_TYPE_P (complete_type (core));
1480 for (probe = list; probe; probe = TREE_CHAIN (probe))
1481 if (same_type_p (TREE_VALUE (probe), spec))
1485 spec = build_decl_list (NULL_TREE, spec);
1486 TREE_CHAIN (spec) = list;
1491 incomplete_type_error (NULL_TREE, core);