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 (char_type_p (typ1)
577 && ((init && TREE_CODE (init) == STRING_CST)
578 || (element && TREE_CODE (element) == STRING_CST)))
580 tree string = element ? element : init;
582 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
584 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
586 error ("char-array initialized from wide string");
587 return error_mark_node;
589 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
591 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
593 error ("int-array initialized from non-wide string");
594 return error_mark_node;
597 TREE_TYPE (string) = type;
598 if (TYPE_DOMAIN (type) != 0
599 && TREE_CONSTANT (TYPE_SIZE (type)))
602 = TREE_INT_CST_LOW (TYPE_SIZE (type));
603 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
604 /* In C it is ok to subtract 1 from the length of the string
605 because it's ok to ignore the terminating null char that is
606 counted in the length of the constant, but in C++ this would
608 if (size < TREE_STRING_LENGTH (string))
609 pedwarn ("initializer-string for array of chars is too long");
615 /* Handle scalar types, including conversions,
616 and signature pointers and references. */
618 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
619 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
620 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
621 || TYPE_PTRMEMFUNC_P (type))
627 error ("initializer for scalar variable requires one element");
628 return error_mark_node;
632 while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
634 cp_pedwarn ("braces around scalar initializer for `%T'", type);
635 init = CONSTRUCTOR_ELTS (init);
636 if (TREE_CHAIN (init))
637 cp_pedwarn ("ignoring extra initializers for `%T'", type);
638 init = TREE_VALUE (init);
641 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
642 "initialization", NULL_TREE, 0);
645 /* Come here only for records and arrays (and unions with constructors). */
647 if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
649 cp_error ("variable-sized object of type `%T' may not be initialized",
651 return error_mark_node;
654 if (code == ARRAY_TYPE || IS_AGGR_TYPE_CODE (code))
656 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type))
658 cp_error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
660 return error_mark_node;
662 else if (raw_constructor)
663 return process_init_constructor (type, init, (tree *)0);
664 else if (can_convert_arg (type, TREE_TYPE (init), init)
665 || TYPE_NON_AGGREGATE_CLASS (type))
666 /* These are never initialized from multiple constructor elements. */;
669 *tail = old_tail_contents;
670 return process_init_constructor (type, 0, tail);
673 if (code != ARRAY_TYPE)
675 int flags = LOOKUP_NORMAL;
676 /* Initialization from { } is copy-initialization. */
678 flags |= LOOKUP_ONLYCONVERTING;
680 return convert_for_initialization (NULL_TREE, type, init, flags,
681 "initialization", NULL_TREE, 0);
685 error ("invalid initializer");
686 return error_mark_node;
689 /* Process a constructor for a variable of type TYPE.
690 The constructor elements may be specified either with INIT or with ELTS,
691 only one of which should be non-null.
693 If INIT is specified, it is a CONSTRUCTOR node which is specifically
694 and solely for initializing this datum.
696 If ELTS is specified, it is the address of a variable containing
697 a list of expressions. We take as many elements as we need
698 from the head of the list and update the list.
700 In the resulting constructor, TREE_CONSTANT is set if all elts are
701 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
702 constants that the assembler and linker can compute them. */
705 process_init_constructor (type, init, elts)
706 tree type, init, *elts;
709 /* List of the elements of the result constructor,
711 register tree members = NULL;
718 /* Make TAIL be the list of elements to use for the initialization,
719 no matter how the data was given to us. */
723 if (warn_missing_braces)
724 warning ("aggregate has a partly bracketed initializer");
728 tail = CONSTRUCTOR_ELTS (init);
730 /* Gobble as many elements as needed, and make a constructor or initial value
731 for each element of this aggregate. Chain them together in result.
732 If there are too few, use 0 for each scalar ultimate component. */
734 if (TREE_CODE (type) == ARRAY_TYPE)
736 tree domain = TYPE_DOMAIN (type);
741 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
742 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
745 len = -1; /* Take as many as there are */
747 for (i = 0; len < 0 || i < len; i++)
751 if (TREE_PURPOSE (tail)
752 && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
753 || compare_tree_int (TREE_PURPOSE (tail), i) != 0))
754 sorry ("non-trivial labeled initializers");
756 if (TREE_VALUE (tail) != 0)
759 next1 = digest_init (TREE_TYPE (type),
760 TREE_VALUE (tail), &tail1);
761 if (next1 == error_mark_node)
764 (same_type_ignoring_top_level_qualifiers_p
765 (TREE_TYPE (type), TREE_TYPE (next1)),
767 my_friendly_assert (tail1 == 0
768 || TREE_CODE (tail1) == TREE_LIST, 319);
769 if (tail == tail1 && len < 0)
771 error ("non-empty initializer for array of empty elements");
772 /* Just ignore what we were supposed to use. */
779 next1 = error_mark_node;
780 tail = TREE_CHAIN (tail);
786 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
788 /* If this type needs constructors run for
789 default-initialization, we can't rely on the backend to do it
790 for us, so build up TARGET_EXPRs. If the type in question is
791 a class, just build one up; if it's an array, recurse. */
793 if (IS_AGGR_TYPE (TREE_TYPE (type)))
794 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
796 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE);
797 next1 = digest_init (TREE_TYPE (type), next1, 0);
800 /* The default zero-initialization is fine for us; don't
801 add anything to the CONSTRUCTOR. */
804 if (next1 == error_mark_node)
806 else if (!TREE_CONSTANT (next1))
808 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
810 members = tree_cons (size_int (i), next1, members);
813 else if (TREE_CODE (type) == RECORD_TYPE)
819 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
821 sorry ("initializer list for object of class with virtual base classes");
822 return error_mark_node;
825 if (TYPE_BINFO_BASETYPES (type))
827 sorry ("initializer list for object of class with base classes");
828 return error_mark_node;
831 if (TYPE_POLYMORPHIC_P (type))
833 sorry ("initializer list for object using virtual functions");
834 return error_mark_node;
838 for (field = TYPE_FIELDS (type); field;
839 field = TREE_CHAIN (field))
841 if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
843 members = tree_cons (field, integer_zero_node, members);
847 if (TREE_CODE (field) != FIELD_DECL)
852 if (TREE_PURPOSE (tail)
853 && TREE_PURPOSE (tail) != field
854 && TREE_PURPOSE (tail) != DECL_NAME (field))
855 sorry ("non-trivial labeled initializers");
857 if (TREE_VALUE (tail) != 0)
861 next1 = digest_init (TREE_TYPE (field),
862 TREE_VALUE (tail), &tail1);
863 my_friendly_assert (tail1 == 0
864 || TREE_CODE (tail1) == TREE_LIST, 320);
869 next1 = error_mark_node;
870 tail = TREE_CHAIN (tail);
873 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
875 /* If this type needs constructors run for
876 default-initialization, we can't rely on the backend to do it
877 for us, so build up TARGET_EXPRs. If the type in question is
878 a class, just build one up; if it's an array, recurse. */
880 if (IS_AGGR_TYPE (TREE_TYPE (field)))
881 next1 = build_functional_cast (TREE_TYPE (field),
884 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE,
886 next1 = digest_init (TREE_TYPE (field), next1, 0);
888 /* Warn when some struct elements are implicitly initialized. */
890 cp_warning ("missing initializer for member `%D'", field);
894 if (TREE_READONLY (field))
895 cp_error ("uninitialized const member `%D'", field);
896 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
897 && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
898 cp_error ("member `%D' with uninitialized const fields",
900 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
901 cp_error ("member `%D' is uninitialized reference", field);
903 /* Warn when some struct elements are implicitly initialized
906 cp_warning ("missing initializer for member `%D'", field);
908 /* The default zero-initialization is fine for us; don't
909 add anything to the CONSTRUCTOR. */
913 if (next1 == error_mark_node)
915 else if (!TREE_CONSTANT (next1))
917 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
919 members = tree_cons (field, next1, members);
922 else if (TREE_CODE (type) == UNION_TYPE
923 /* If the initializer was empty, use default zero initialization. */
926 register tree field = TYPE_FIELDS (type);
928 /* Find the first named field. ANSI decided in September 1990
929 that only named fields count here. */
930 while (field && (DECL_NAME (field) == 0
931 || TREE_CODE (field) != FIELD_DECL))
932 field = TREE_CHAIN (field);
934 /* If this element specifies a field, initialize via that field. */
935 if (TREE_PURPOSE (tail) != NULL_TREE)
939 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
940 /* Handle the case of a call by build_c_cast. */
941 field = TREE_PURPOSE (tail), win = 1;
942 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
943 error ("index value instead of field name in union initializer");
947 for (temp = TYPE_FIELDS (type);
949 temp = TREE_CHAIN (temp))
950 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
953 field = temp, win = 1;
955 cp_error ("no field `%D' in union being initialized",
956 TREE_PURPOSE (tail));
959 TREE_VALUE (tail) = error_mark_node;
963 cp_error ("union `%T' with no named members cannot be initialized",
965 TREE_VALUE (tail) = error_mark_node;
968 if (TREE_VALUE (tail) != 0)
972 next1 = digest_init (TREE_TYPE (field),
973 TREE_VALUE (tail), &tail1);
974 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
975 my_friendly_abort (357);
980 next1 = error_mark_node;
981 tail = TREE_CHAIN (tail);
984 if (next1 == error_mark_node)
986 else if (!TREE_CONSTANT (next1))
988 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
990 members = tree_cons (field, next1, members);
993 /* If arguments were specified as a list, just remove the ones we used. */
996 /* If arguments were specified as a constructor,
997 complain unless we used all the elements of the constructor. */
999 pedwarn ("excess elements in aggregate initializer");
1002 return error_mark_node;
1004 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
1006 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1007 if (allconstant) TREE_CONSTANT (result) = 1;
1008 if (allconstant && allsimple) TREE_STATIC (result) = 1;
1012 /* Given a structure or union value DATUM, construct and return
1013 the structure or union component which results from narrowing
1014 that value by the type specified in BASETYPE. For example, given the
1017 class L { int ii; };
1018 class A : L { ... };
1019 class B : L { ... };
1020 class C : A, B { ... };
1028 x.A::ii refers to the ii member of the L part of
1029 the A part of the C object named by X. In this case,
1030 DATUM would be x, and BASETYPE would be A.
1032 Note that this is nonconformant; the standard specifies that first
1033 we look up ii in A, then convert x to an L& and pull out the ii part.
1034 But narrowing seems to be standard practice, so let's do it anyway. */
1037 build_scoped_ref (datum, basetype)
1042 tree type = TREE_TYPE (datum);
1044 if (datum == error_mark_node)
1045 return error_mark_node;
1047 /* Don't do this if it would cause an error or if we're being pedantic. */
1048 if (! ACCESSIBLY_UNIQUELY_DERIVED_P (basetype, type)
1052 ref = build_unary_op (ADDR_EXPR, datum, 0);
1053 ref = convert_pointer_to (basetype, ref);
1055 return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1058 /* Build a reference to an object specified by the C++ `->' operator.
1059 Usually this just involves dereferencing the object, but if the
1060 `->' operator is overloaded, then such overloads must be
1061 performed until an object which does not have the `->' operator
1062 overloaded is found. An error is reported when circular pointer
1063 delegation is detected. */
1066 build_x_arrow (datum)
1069 tree types_memoized = NULL_TREE;
1070 register tree rval = datum;
1071 tree type = TREE_TYPE (rval);
1072 tree last_rval = NULL_TREE;
1074 if (type == error_mark_node)
1075 return error_mark_node;
1077 if (processing_template_decl)
1078 return build_min_nt (ARROW_EXPR, rval);
1080 if (TREE_CODE (rval) == OFFSET_REF)
1082 rval = resolve_offset_ref (datum);
1083 type = TREE_TYPE (rval);
1086 if (TREE_CODE (type) == REFERENCE_TYPE)
1088 rval = convert_from_reference (rval);
1089 type = TREE_TYPE (rval);
1092 if (IS_AGGR_TYPE (type))
1094 while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval,
1095 NULL_TREE, NULL_TREE)))
1097 if (rval == error_mark_node)
1098 return error_mark_node;
1100 if (value_member (TREE_TYPE (rval), types_memoized))
1102 error ("circular pointer delegation detected");
1103 return error_mark_node;
1107 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1113 if (last_rval == NULL_TREE)
1115 cp_error ("base operand of `->' has non-pointer type `%T'", type);
1116 return error_mark_node;
1119 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1120 last_rval = convert_from_reference (last_rval);
1123 last_rval = default_conversion (rval);
1125 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1126 return build_indirect_ref (last_rval, NULL_PTR);
1129 error ("result of `operator->()' yields non-pointer result");
1131 error ("base operand of `->' is not a pointer");
1132 return error_mark_node;
1135 /* Make an expression to refer to the COMPONENT field of
1136 structure or union value DATUM. COMPONENT is an arbitrary
1137 expression. DATUM has not already been checked out to be of
1140 For C++, COMPONENT may be a TREE_LIST. This happens when we must
1141 return an object of member type to a method of the current class,
1142 but there is not yet enough typing information to know which one.
1143 As a special case, if there is only one method by that name,
1144 it is returned. Otherwise we return an expression which other
1145 routines will have to know how to deal with later. */
1148 build_m_component_ref (datum, component)
1149 tree datum, component;
1152 tree objtype = TREE_TYPE (datum);
1156 if (processing_template_decl)
1157 return build_min_nt (DOTSTAR_EXPR, datum, component);
1159 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1161 type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1166 type = TREE_TYPE (TREE_TYPE (component));
1167 rettype = TREE_TYPE (type);
1170 if (datum == error_mark_node || component == error_mark_node)
1171 return error_mark_node;
1173 if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
1175 cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component, type);
1176 return error_mark_node;
1179 if (TREE_CODE (objtype) == REFERENCE_TYPE)
1180 objtype = TREE_TYPE (objtype);
1181 objtype = TYPE_MAIN_VARIANT (objtype);
1183 if (! IS_AGGR_TYPE (objtype))
1185 cp_error ("cannot apply member pointer `%E' to `%E'", component, datum);
1186 cp_error ("which is of non-aggregate type `%T'", objtype);
1187 return error_mark_node;
1190 binfo = get_binfo (TYPE_METHOD_BASETYPE (type), objtype, 1);
1191 if (binfo == NULL_TREE)
1193 cp_error ("member type `%T::' incompatible with object type `%T'",
1194 TYPE_METHOD_BASETYPE (type), objtype);
1195 return error_mark_node;
1197 else if (binfo == error_mark_node)
1198 return error_mark_node;
1200 component = build (OFFSET_REF, rettype, datum, component);
1201 if (TREE_CODE (type) == OFFSET_TYPE)
1202 component = resolve_offset_ref (component);
1206 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1209 build_functional_cast (exp, parms)
1213 /* This is either a call to a constructor,
1214 or a C cast in C++'s `functional' notation. */
1217 if (exp == error_mark_node || parms == error_mark_node)
1218 return error_mark_node;
1220 if (TREE_CODE (exp) == IDENTIFIER_NODE)
1222 if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1223 /* Either an enum or an aggregate type. */
1224 type = IDENTIFIER_TYPE_VALUE (exp);
1227 type = lookup_name (exp, 1);
1228 if (!type || TREE_CODE (type) != TYPE_DECL)
1230 cp_error ("`%T' fails to be a typedef or built-in type", exp);
1231 return error_mark_node;
1233 type = TREE_TYPE (type);
1236 else if (TREE_CODE (exp) == TYPE_DECL)
1237 type = TREE_TYPE (exp);
1241 if (processing_template_decl)
1242 return build_min (CAST_EXPR, type, parms);
1244 if (! IS_AGGR_TYPE (type))
1246 /* this must build a C cast */
1247 if (parms == NULL_TREE)
1248 parms = integer_zero_node;
1251 if (TREE_CHAIN (parms) != NULL_TREE)
1252 pedwarn ("initializer list being treated as compound expression");
1253 parms = build_compound_expr (parms);
1256 return build_c_cast (type, parms);
1259 /* Prepare to evaluate as a call to a constructor. If this expression
1260 is actually used, for example,
1262 return X (arg1, arg2, ...);
1264 then the slot being initialized will be filled in. */
1266 if (!complete_type_or_else (type, NULL_TREE))
1267 return error_mark_node;
1268 if (abstract_virtuals_error (NULL_TREE, type))
1269 return error_mark_node;
1271 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1272 return build_c_cast (type, TREE_VALUE (parms));
1274 /* We need to zero-initialize POD types. Let's do that for everything
1275 that doesn't need a constructor. */
1276 if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1277 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1279 exp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
1280 return get_target_expr (exp);
1283 exp = build_method_call (NULL_TREE, complete_ctor_identifier, parms,
1284 TYPE_BINFO (type), LOOKUP_NORMAL);
1286 if (exp == error_mark_node)
1287 return error_mark_node;
1289 return build_cplus_new (type, exp);
1292 /* Return the character string for the name that encodes the
1293 enumeral value VALUE in the domain TYPE. */
1296 enum_name_string (value, type)
1300 register tree values = TYPE_VALUES (type);
1302 my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324);
1304 while (values && ! tree_int_cst_equal (TREE_VALUE (values), value))
1305 values = TREE_CHAIN (values);
1307 if (values == NULL_TREE)
1309 char *buf = (char *) oballoc (16 + TYPE_NAME_LENGTH (type));
1311 /* Value must have been cast. */
1312 sprintf (buf, "(enum %s)%ld",
1313 TYPE_NAME_STRING (type), (long) TREE_INT_CST_LOW (value));
1316 return IDENTIFIER_POINTER (TREE_PURPOSE (values));
1320 /* Print out a language-specific error message for
1321 (Pascal) case or (C) switch statements.
1322 CODE tells what sort of message to print.
1323 TYPE is the type of the switch index expression.
1324 NEW is the new value that we were trying to add.
1325 OLD is the old value that stopped us from adding it. */
1328 report_case_error (code, type, new_value, old_value)
1331 tree new_value, old_value;
1336 error ("case label not within a switch statement");
1338 error ("default label not within a switch statement");
1344 error ("multiple default labels in one switch");
1347 if (TREE_CODE (new_value) == RANGE_EXPR)
1348 if (TREE_CODE (old_value) == RANGE_EXPR)
1350 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1351 if (TREE_CODE (type) == ENUMERAL_TYPE)
1352 sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1353 enum_name_string (TREE_OPERAND (new_value, 0), type),
1354 enum_name_string (TREE_OPERAND (new_value, 1), type),
1355 enum_name_string (TREE_OPERAND (old_value, 0), type),
1356 enum_name_string (TREE_OPERAND (old_value, 1), type));
1358 sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1359 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1360 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1361 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1362 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
1367 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1368 if (TREE_CODE (type) == ENUMERAL_TYPE)
1369 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1370 enum_name_string (TREE_OPERAND (new_value, 0), type),
1371 enum_name_string (TREE_OPERAND (new_value, 1), type),
1372 enum_name_string (old_value, type));
1374 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1375 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
1376 TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
1377 TREE_INT_CST_LOW (old_value));
1380 else if (TREE_CODE (old_value) == RANGE_EXPR)
1382 char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
1383 if (TREE_CODE (type) == ENUMERAL_TYPE)
1384 sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
1385 enum_name_string (TREE_OPERAND (old_value, 0), type),
1386 enum_name_string (TREE_OPERAND (old_value, 1), type),
1387 enum_name_string (new_value, type));
1389 sprintf (buf, "range [%d..%d] includes (%d) in case expression",
1390 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
1391 TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
1392 TREE_INT_CST_LOW (new_value));
1397 if (TREE_CODE (type) == ENUMERAL_TYPE)
1398 error ("duplicate label `%s' in switch statement",
1399 enum_name_string (new_value, type));
1401 error ("duplicate label (%d) in switch statement",
1402 TREE_INT_CST_LOW (new_value));
1407 if (TREE_CODE (type) == ENUMERAL_TYPE)
1408 warning ("case value out of range for enum %s",
1409 TYPE_NAME_STRING (type));
1411 warning ("case value out of range");
1415 if (TREE_CODE (type) == ENUMERAL_TYPE)
1416 error ("range values `%s' and `%s' reversed",
1417 enum_name_string (new_value, type),
1418 enum_name_string (old_value, type));
1420 error ("range values reversed");
1425 /* Complain about defining new types in inappropriate places. We give an
1426 exception for C-style casts, to accommodate GNU C stylings. */
1429 check_for_new_type (string, inptree)
1431 flagged_type_tree inptree;
1433 if (inptree.new_type_flag
1434 && (pedantic || strcmp (string, "cast") != 0))
1435 pedwarn ("ISO C++ forbids defining types within %s", string);
1438 /* Add new exception specifier SPEC, to the LIST we currently have.
1439 If it's already in LIST then do nothing.
1440 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1441 know what we're doing. */
1444 add_exception_specifier (list, spec, complain)
1452 if (spec == error_mark_node)
1455 my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317);
1457 /* [except.spec] 1, type in an exception specifier shall not be
1458 incomplete, or pointer or ref to incomplete other than pointer
1460 is_ptr = TREE_CODE (core) == POINTER_TYPE;
1461 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1462 core = TREE_TYPE (core);
1465 else if (VOID_TYPE_P (core))
1467 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1470 ok = COMPLETE_TYPE_P (complete_type (core));
1476 for (probe = list; probe; probe = TREE_CHAIN (probe))
1477 if (same_type_p (TREE_VALUE (probe), spec))
1481 spec = build_decl_list (NULL_TREE, spec);
1482 TREE_CHAIN (spec) = list;
1487 incomplete_type_error (NULL_TREE, core);