1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987, 88, 89, 92-98, 1999 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This file is part of the C++ front end.
25 It contains routines to build C++ expressions given their operands,
26 including computing the types of the result, C and C++ specific error
27 checks, and some optimization.
29 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
30 and to process initializations in declarations (since they work
31 like a strange sort of assignment). */
41 static tree process_init_constructor
PROTO((tree
, tree
, tree
*));
42 static void ack
PVPROTO ((const char *, ...)) ATTRIBUTE_PRINTF_1
;
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_CLASS_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
) PVPROTO ((const char *, ...));
93 if (TREE_CODE (arg
) == COMPONENT_REF
)
95 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
96 fmt
= "%s of member `%D' in read-only structure";
98 fmt
= "%s of read-only 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_ABSTRACT_VIRTUALS (type
))
142 u
= CLASSTYPE_ABSTRACT_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 method `%#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 /* Like error(), but don't call report_error_function(). */
251 ack
VPROTO ((const char *msg
, ...))
253 #ifndef ANSI_PROTOTYPES
260 #ifndef ANSI_PROTOTYPES
261 msg
= va_arg (ap
, const char *);
265 fprintf (stderr
, "%s:%d: ", input_filename
, lineno
);
267 fprintf (stderr
, "%s: ", progname
);
269 vfprintf (stderr
, msg
, ap
);
272 fprintf (stderr
, "\n");
275 /* There are times when the compiler can get very confused, confused
276 to the point of giving up by aborting, simply because of previous
277 input errors. It is much better to have the user go back and
278 correct those errors first, and see if it makes us happier, than it
279 is to abort on him. This is because when one has a 10,000 line
280 program, and the compiler comes back with ``core dump'', the user
281 is left not knowing even where to begin to fix things and no place
282 to even try and work around things.
284 The parameter is to uniquely identify the problem to the user, so
285 that they can say, I am having problem 59, and know that fix 7 will
286 probably solve their problem. Or, we can document what problem
287 59 is, so they can understand how to work around it, should they
290 We used to tell people to "fix the above error[s] and try recompiling
291 the program" via a call to fatal, but that message tended to look
292 silly. So instead, we just do the equivalent of a call to fatal in the
293 same situation (call exit).
295 We used to assign sequential numbers for the aborts; now we use an
296 encoding of the date the abort was added, since that has more meaning
297 when we only see the error message. */
299 static int abortcount
= 0;
302 my_friendly_abort (i
)
305 /* if the previous error came through here, i.e. report_error_function
306 ended up calling us again, don't just exit; we want a diagnostic of
309 current_function_decl
= NULL_TREE
;
310 else if (errorcount
> 0 || sorrycount
> 0)
315 ack ("Internal compiler error.");
317 ack ("Internal compiler error %d.", i
);
318 ack ("Please submit a full bug report.");
319 ack ("See <URL:http://www.gnu.org/software/gcc/faq.html#bugreport> for instructions.");
322 error ("confused by earlier errors, bailing out");
329 error ("Internal compiler error.");
331 error ("Internal compiler error %d.", i
);
333 error ("Please submit a full bug report.");
334 fatal ("See <URL:http://www.gnu.org/software/gcc/faq.html#bugreport> for instructions.");
338 my_friendly_assert (cond
, where
)
342 my_friendly_abort (where
);
345 /* Perform appropriate conversions on the initial value of a variable,
346 store it in the declaration DECL,
347 and print any error messages that are appropriate.
348 If the init is invalid, store an ERROR_MARK.
350 C++: Note that INIT might be a TREE_LIST, which would mean that it is
351 a base class initializer for some aggregate type, hopefully compatible
352 with DECL. If INIT is a single element, and DECL is an aggregate
353 type, we silently convert INIT into a TREE_LIST, allowing a constructor
356 If INIT is a TREE_LIST and there is no constructor, turn INIT
357 into a CONSTRUCTOR and use standard initialization techniques.
358 Perhaps a warning should be generated?
360 Returns value of initializer if initialization could not be
361 performed for static variable. In that case, caller must do
365 store_init_value (decl
, init
)
368 register tree value
, type
;
370 /* If variable's type was invalidly declared, just ignore it. */
372 type
= TREE_TYPE (decl
);
373 if (TREE_CODE (type
) == ERROR_MARK
)
377 /* This breaks arrays, and should not have any effect for other decls. */
378 /* Take care of C++ business up here. */
379 type
= TYPE_MAIN_VARIANT (type
);
382 if (IS_AGGR_TYPE (type
))
384 if (! TYPE_HAS_TRIVIAL_INIT_REF (type
)
385 && TREE_CODE (init
) != CONSTRUCTOR
)
386 my_friendly_abort (109);
388 if (TREE_CODE (init
) == TREE_LIST
)
390 cp_error ("constructor syntax used, but no constructor declared for type `%T'", type
);
391 init
= build_nt (CONSTRUCTOR
, NULL_TREE
, nreverse (init
));
394 if (TREE_CODE (init
) == CONSTRUCTOR
)
398 /* Check that we're really an aggregate as ARM 8.4.1 defines it. */
399 if (CLASSTYPE_N_BASECLASSES (type
))
400 cp_error_at ("initializer list construction invalid for derived class object `%D'", decl
);
401 if (CLASSTYPE_VTBL_PTR (type
))
402 cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl
);
403 if (TYPE_NEEDS_CONSTRUCTING (type
))
405 cp_error_at ("initializer list construction invalid for `%D'", decl
);
406 error ("due to the presence of a constructor");
408 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
409 if (TREE_PRIVATE (field
) || TREE_PROTECTED (field
))
411 cp_error_at ("initializer list construction invalid for `%D'", decl
);
412 cp_error_at ("due to non-public access of member `%D'", field
);
414 for (field
= TYPE_METHODS (type
); field
; field
= TREE_CHAIN (field
))
415 if (TREE_PRIVATE (field
) || TREE_PROTECTED (field
))
417 cp_error_at ("initializer list construction invalid for `%D'", decl
);
418 cp_error_at ("due to non-public access of member `%D'", field
);
423 else if (TREE_CODE (init
) == TREE_LIST
424 && TREE_TYPE (init
) != unknown_type_node
)
426 if (TREE_CODE (decl
) == RESULT_DECL
)
428 if (TREE_CHAIN (init
))
430 warning ("comma expression used to initialize return value");
431 init
= build_compound_expr (init
);
434 init
= TREE_VALUE (init
);
436 else if (TREE_CODE (init
) == TREE_LIST
437 && TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
439 error ("cannot initialize arrays using this syntax");
444 /* We get here with code like `int a (2);' */
446 if (TREE_CHAIN (init
) != NULL_TREE
)
448 pedwarn ("initializer list being treated as compound expression");
449 init
= build_compound_expr (init
);
452 init
= TREE_VALUE (init
);
456 /* End of special C++ code. */
458 /* Digest the specified initializer into an expression. */
460 value
= digest_init (type
, init
, (tree
*) 0);
462 /* Store the expression if valid; else report error. */
464 if (TREE_CODE (value
) == ERROR_MARK
)
466 /* Other code expects that initializers for objects of types that need
467 constructing never make it into DECL_INITIAL, and passes 'init' to
468 build_aggr_init without checking DECL_INITIAL. So just return. */
469 else if (TYPE_NEEDS_CONSTRUCTING (type
))
471 else if (TREE_STATIC (decl
)
472 && (! TREE_CONSTANT (value
)
473 || ! initializer_constant_valid_p (value
, TREE_TYPE (value
))
475 /* A STATIC PUBLIC int variable doesn't have to be
476 run time inited when doing pic. (mrs) */
477 /* Since ctors and dtors are the only things that can
478 reference vtables, and they are always written down
479 the vtable definition, we can leave the
480 vtables in initialized data space.
481 However, other initialized data cannot be initialized
482 this way. Instead a global file-level initializer
484 || (flag_pic
&& !DECL_VIRTUAL_P (decl
) && TREE_PUBLIC (decl
))
489 #if 0 /* No, that's C. jason 9/19/94 */
492 if (pedantic
&& TREE_CODE (value
) == CONSTRUCTOR
)
494 if (! TREE_CONSTANT (value
) || ! TREE_STATIC (value
))
495 pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions");
500 /* Store the VALUE in DECL_INITIAL. If we're building a
501 statement-tree we will actually expand the initialization later
502 when we output this function. */
503 DECL_INITIAL (decl
) = value
;
507 /* Digest the parser output INIT as an initializer for type TYPE.
508 Return a C expression of type TYPE to represent the initial value.
510 If TAIL is nonzero, it points to a variable holding a list of elements
511 of which INIT is the first. We update the list stored there by
512 removing from the head all the elements that we use.
513 Normally this is only one; we use more than one element only if
514 TYPE is an aggregate and INIT is not a constructor. */
517 digest_init (type
, init
, tail
)
518 tree type
, init
, *tail
;
520 enum tree_code code
= TREE_CODE (type
);
521 tree element
= NULL_TREE
;
522 tree old_tail_contents
= NULL_TREE
;
523 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
524 tree node which has no TREE_TYPE. */
527 /* By default, assume we use one element from a list.
528 We correct this later in the sole case where it is not true. */
532 old_tail_contents
= *tail
;
533 *tail
= TREE_CHAIN (*tail
);
536 if (init
== error_mark_node
|| (TREE_CODE (init
) == TREE_LIST
537 && TREE_VALUE (init
) == error_mark_node
))
538 return error_mark_node
;
540 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
541 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
542 init
= TREE_OPERAND (init
, 0);
544 if (TREE_CODE (init
) == CONSTRUCTOR
&& TREE_TYPE (init
) == type
)
547 raw_constructor
= TREE_CODE (init
) == CONSTRUCTOR
&& TREE_TYPE (init
) == 0;
550 && CONSTRUCTOR_ELTS (init
) != 0
551 && TREE_CHAIN (CONSTRUCTOR_ELTS (init
)) == 0)
553 element
= TREE_VALUE (CONSTRUCTOR_ELTS (init
));
554 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
555 if (element
&& TREE_CODE (element
) == NON_LVALUE_EXPR
)
556 element
= TREE_OPERAND (element
, 0);
557 if (element
== error_mark_node
)
561 /* Initialization of an array of chars from a string constant
562 optionally enclosed in braces. */
564 if (code
== ARRAY_TYPE
)
568 if (TREE_CODE (init
) == TREE_LIST
)
570 error ("initializing array with parameter list");
571 return error_mark_node
;
574 typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
575 if ((typ1
== char_type_node
576 || typ1
== signed_char_type_node
577 || typ1
== unsigned_char_type_node
578 || typ1
== unsigned_wchar_type_node
579 || typ1
== signed_wchar_type_node
)
580 && ((init
&& TREE_CODE (init
) == STRING_CST
)
581 || (element
&& TREE_CODE (element
) == STRING_CST
)))
583 tree string
= element
? element
: init
;
585 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string
)))
587 && TYPE_PRECISION (typ1
) == BITS_PER_UNIT
)
589 error ("char-array initialized from wide string");
590 return error_mark_node
;
592 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string
)))
594 && TYPE_PRECISION (typ1
) != BITS_PER_UNIT
)
596 error ("int-array initialized from non-wide string");
597 return error_mark_node
;
600 TREE_TYPE (string
) = type
;
601 if (TYPE_DOMAIN (type
) != 0
602 && TREE_CONSTANT (TYPE_SIZE (type
)))
605 = TREE_INT_CST_LOW (TYPE_SIZE (type
));
606 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
607 /* In C it is ok to subtract 1 from the length of the string
608 because it's ok to ignore the terminating null char that is
609 counted in the length of the constant, but in C++ this would
611 if (size
< TREE_STRING_LENGTH (string
))
612 pedwarn ("initializer-string for array of chars is too long");
618 /* Handle scalar types, including conversions,
619 and signature pointers and references. */
621 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== POINTER_TYPE
622 || code
== ENUMERAL_TYPE
|| code
== REFERENCE_TYPE
623 || code
== BOOLEAN_TYPE
|| code
== COMPLEX_TYPE
624 || TYPE_PTRMEMFUNC_P (type
))
630 error ("initializer for scalar variable requires one element");
631 return error_mark_node
;
635 while (TREE_CODE (init
) == CONSTRUCTOR
&& TREE_HAS_CONSTRUCTOR (init
))
637 cp_pedwarn ("braces around scalar initializer for `%T'", type
);
638 init
= CONSTRUCTOR_ELTS (init
);
639 if (TREE_CHAIN (init
))
640 cp_pedwarn ("ignoring extra initializers for `%T'", type
);
641 init
= TREE_VALUE (init
);
644 return convert_for_initialization (0, type
, init
, LOOKUP_NORMAL
,
645 "initialization", NULL_TREE
, 0);
648 /* Come here only for records and arrays (and unions with constructors). */
650 if (TYPE_SIZE (type
) && ! TREE_CONSTANT (TYPE_SIZE (type
)))
652 cp_error ("variable-sized object of type `%T' may not be initialized",
654 return error_mark_node
;
657 if (code
== ARRAY_TYPE
|| IS_AGGR_TYPE_CODE (code
))
659 if (raw_constructor
&& TYPE_NON_AGGREGATE_CLASS (type
))
661 cp_error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
663 return error_mark_node
;
665 else if (raw_constructor
)
666 return process_init_constructor (type
, init
, (tree
*)0);
667 else if (can_convert_arg (type
, TREE_TYPE (init
), init
)
668 || TYPE_NON_AGGREGATE_CLASS (type
))
669 /* These are never initialized from multiple constructor elements. */;
672 *tail
= old_tail_contents
;
673 return process_init_constructor (type
, 0, tail
);
676 if (code
!= ARRAY_TYPE
)
678 int flags
= LOOKUP_NORMAL
;
679 /* Initialization from { } is copy-initialization. */
681 flags
|= LOOKUP_ONLYCONVERTING
;
683 return convert_for_initialization (NULL_TREE
, type
, init
, flags
,
684 "initialization", NULL_TREE
, 0);
688 error ("invalid initializer");
689 return error_mark_node
;
692 /* Process a constructor for a variable of type TYPE.
693 The constructor elements may be specified either with INIT or with ELTS,
694 only one of which should be non-null.
696 If INIT is specified, it is a CONSTRUCTOR node which is specifically
697 and solely for initializing this datum.
699 If ELTS is specified, it is the address of a variable containing
700 a list of expressions. We take as many elements as we need
701 from the head of the list and update the list.
703 In the resulting constructor, TREE_CONSTANT is set if all elts are
704 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
705 constants that the assembler and linker can compute them. */
708 process_init_constructor (type
, init
, elts
)
709 tree type
, init
, *elts
;
712 /* List of the elements of the result constructor,
714 register tree members
= NULL
;
721 /* Make TAIL be the list of elements to use for the initialization,
722 no matter how the data was given to us. */
726 if (warn_missing_braces
)
727 warning ("aggregate has a partly bracketed initializer");
731 tail
= CONSTRUCTOR_ELTS (init
);
733 /* Gobble as many elements as needed, and make a constructor or initial value
734 for each element of this aggregate. Chain them together in result.
735 If there are too few, use 0 for each scalar ultimate component. */
737 if (TREE_CODE (type
) == ARRAY_TYPE
)
739 tree domain
= TYPE_DOMAIN (type
);
744 len
= (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain
))
745 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain
))
748 len
= -1; /* Take as many as there are */
750 for (i
= 0; len
< 0 || i
< len
; i
++)
754 if (TREE_PURPOSE (tail
)
755 && (TREE_CODE (TREE_PURPOSE (tail
)) != INTEGER_CST
756 || TREE_INT_CST_LOW (TREE_PURPOSE (tail
)) != i
))
757 sorry ("non-trivial labeled initializers");
759 if (TREE_VALUE (tail
) != 0)
762 next1
= digest_init (TREE_TYPE (type
),
763 TREE_VALUE (tail
), &tail1
);
764 if (next1
== error_mark_node
)
767 (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
768 TYPE_MAIN_VARIANT (TREE_TYPE (next1
))),
770 my_friendly_assert (tail1
== 0
771 || TREE_CODE (tail1
) == TREE_LIST
, 319);
772 if (tail
== tail1
&& len
< 0)
774 error ("non-empty initializer for array of empty elements");
775 /* Just ignore what we were supposed to use. */
782 next1
= error_mark_node
;
783 tail
= TREE_CHAIN (tail
);
789 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type
)))
791 /* If this type needs constructors run for
792 default-initialization, we can't rely on the backend to do it
793 for us, so build up TARGET_EXPRs. If the type in question is
794 a class, just build one up; if it's an array, recurse. */
796 if (IS_AGGR_TYPE (TREE_TYPE (type
)))
797 next1
= build_functional_cast (TREE_TYPE (type
), NULL_TREE
);
799 next1
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
800 next1
= digest_init (TREE_TYPE (type
), next1
, 0);
803 /* The default zero-initialization is fine for us; don't
804 add anything to the CONSTRUCTOR. */
807 if (next1
== error_mark_node
)
809 else if (!TREE_CONSTANT (next1
))
811 else if (! initializer_constant_valid_p (next1
, TREE_TYPE (next1
)))
813 members
= expr_tree_cons (NULL_TREE
, next1
, members
);
816 else if (TREE_CODE (type
) == RECORD_TYPE
)
822 if (TYPE_USES_VIRTUAL_BASECLASSES (type
))
824 sorry ("initializer list for object of class with virtual baseclasses");
825 return error_mark_node
;
828 if (TYPE_BINFO_BASETYPES (type
))
830 sorry ("initializer list for object of class with baseclasses");
831 return error_mark_node
;
834 if (TYPE_VIRTUAL_P (type
))
836 sorry ("initializer list for object using virtual functions");
837 return error_mark_node
;
841 for (field
= TYPE_FIELDS (type
); field
;
842 field
= TREE_CHAIN (field
))
844 if (! DECL_NAME (field
) && DECL_C_BIT_FIELD (field
))
846 members
= expr_tree_cons (field
, integer_zero_node
, members
);
850 if (TREE_CODE (field
) != FIELD_DECL
)
855 if (TREE_PURPOSE (tail
)
856 && TREE_PURPOSE (tail
) != field
857 && TREE_PURPOSE (tail
) != DECL_NAME (field
))
858 sorry ("non-trivial labeled initializers");
860 if (TREE_VALUE (tail
) != 0)
864 next1
= digest_init (TREE_TYPE (field
),
865 TREE_VALUE (tail
), &tail1
);
866 my_friendly_assert (tail1
== 0
867 || TREE_CODE (tail1
) == TREE_LIST
, 320);
872 next1
= error_mark_node
;
873 tail
= TREE_CHAIN (tail
);
876 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field
)))
878 /* If this type needs constructors run for
879 default-initialization, we can't rely on the backend to do it
880 for us, so build up TARGET_EXPRs. If the type in question is
881 a class, just build one up; if it's an array, recurse. */
883 if (IS_AGGR_TYPE (TREE_TYPE (field
)))
884 next1
= build_functional_cast (TREE_TYPE (field
),
887 next1
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
,
889 next1
= digest_init (TREE_TYPE (field
), next1
, 0);
891 /* Warn when some struct elements are implicitly initialized. */
893 cp_warning ("missing initializer for member `%D'", field
);
897 if (TREE_READONLY (field
))
898 cp_error ("uninitialized const member `%D'", field
);
899 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field
))
900 && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field
)))
901 cp_error ("member `%D' with uninitialized const fields",
903 else if (TREE_CODE (TREE_TYPE (field
)) == REFERENCE_TYPE
)
904 cp_error ("member `%D' is uninitialized reference", field
);
906 /* Warn when some struct elements are implicitly initialized
909 cp_warning ("missing initializer for member `%D'", field
);
911 /* The default zero-initialization is fine for us; don't
912 add anything to the CONSTRUCTOR. */
916 if (next1
== error_mark_node
)
918 else if (!TREE_CONSTANT (next1
))
920 else if (! initializer_constant_valid_p (next1
, TREE_TYPE (next1
)))
922 members
= expr_tree_cons (field
, next1
, members
);
925 else if (TREE_CODE (type
) == UNION_TYPE
926 /* If the initializer was empty, use default zero initialization. */
929 register tree field
= TYPE_FIELDS (type
);
931 /* Find the first named field. ANSI decided in September 1990
932 that only named fields count here. */
933 while (field
&& (DECL_NAME (field
) == 0
934 || TREE_CODE (field
) != FIELD_DECL
))
935 field
= TREE_CHAIN (field
);
937 /* If this element specifies a field, initialize via that field. */
938 if (TREE_PURPOSE (tail
) != NULL_TREE
)
942 if (TREE_CODE (TREE_PURPOSE (tail
)) == FIELD_DECL
)
943 /* Handle the case of a call by build_c_cast. */
944 field
= TREE_PURPOSE (tail
), win
= 1;
945 else if (TREE_CODE (TREE_PURPOSE (tail
)) != IDENTIFIER_NODE
)
946 error ("index value instead of field name in union initializer");
950 for (temp
= TYPE_FIELDS (type
);
952 temp
= TREE_CHAIN (temp
))
953 if (DECL_NAME (temp
) == TREE_PURPOSE (tail
))
956 field
= temp
, win
= 1;
958 cp_error ("no field `%D' in union being initialized",
959 TREE_PURPOSE (tail
));
962 TREE_VALUE (tail
) = error_mark_node
;
966 cp_error ("union `%T' with no named members cannot be initialized",
968 TREE_VALUE (tail
) = error_mark_node
;
971 if (TREE_VALUE (tail
) != 0)
975 next1
= digest_init (TREE_TYPE (field
),
976 TREE_VALUE (tail
), &tail1
);
977 if (tail1
!= 0 && TREE_CODE (tail1
) != TREE_LIST
)
978 my_friendly_abort (357);
983 next1
= error_mark_node
;
984 tail
= TREE_CHAIN (tail
);
987 if (next1
== error_mark_node
)
989 else if (!TREE_CONSTANT (next1
))
991 else if (initializer_constant_valid_p (next1
, TREE_TYPE (next1
)) == 0)
993 members
= expr_tree_cons (field
, next1
, members
);
996 /* If arguments were specified as a list, just remove the ones we used. */
999 /* If arguments were specified as a constructor,
1000 complain unless we used all the elements of the constructor. */
1002 pedwarn ("excess elements in aggregate initializer");
1005 return error_mark_node
;
1007 result
= build (CONSTRUCTOR
, type
, NULL_TREE
, nreverse (members
));
1009 TREE_HAS_CONSTRUCTOR (result
) = TREE_HAS_CONSTRUCTOR (init
);
1010 if (allconstant
) TREE_CONSTANT (result
) = 1;
1011 if (allconstant
&& allsimple
) TREE_STATIC (result
) = 1;
1015 /* Given a structure or union value DATUM, construct and return
1016 the structure or union component which results from narrowing
1017 that value by the type specified in BASETYPE. For example, given the
1020 class L { int ii; };
1021 class A : L { ... };
1022 class B : L { ... };
1023 class C : A, B { ... };
1031 x.A::ii refers to the ii member of the L part of
1032 the A part of the C object named by X. In this case,
1033 DATUM would be x, and BASETYPE would be A. */
1036 build_scoped_ref (datum
, basetype
)
1041 tree type
= TREE_TYPE (datum
);
1043 if (datum
== error_mark_node
)
1044 return error_mark_node
;
1046 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1047 type
= TREE_TYPE (type
);
1049 type
= TYPE_MAIN_VARIANT (type
);
1051 /* This is an easy conversion. */
1052 if (is_aggr_type (basetype
, 1))
1054 tree binfo
= TYPE_BINFO (basetype
);
1055 if (binfo
!= TYPE_BINFO (type
))
1057 binfo
= get_binfo (binfo
, type
, 1);
1058 if (binfo
== error_mark_node
)
1059 return error_mark_node
;
1061 return error_not_base_type (basetype
, type
);
1064 switch (TREE_CODE (datum
))
1069 case FIX_TRUNC_EXPR
:
1070 case FIX_FLOOR_EXPR
:
1071 case FIX_ROUND_EXPR
:
1073 ref
= convert_pointer_to (binfo
,
1074 build_unary_op (ADDR_EXPR
, TREE_OPERAND (datum
, 0), 0));
1077 ref
= convert_pointer_to (binfo
,
1078 build_unary_op (ADDR_EXPR
, datum
, 0));
1080 return build_indirect_ref (ref
, "(compiler error in build_scoped_ref)");
1082 return error_mark_node
;
1085 /* Build a reference to an object specified by the C++ `->' operator.
1086 Usually this just involves dereferencing the object, but if the
1087 `->' operator is overloaded, then such overloads must be
1088 performed until an object which does not have the `->' operator
1089 overloaded is found. An error is reported when circular pointer
1090 delegation is detected. */
1093 build_x_arrow (datum
)
1096 tree types_memoized
= NULL_TREE
;
1097 register tree rval
= datum
;
1098 tree type
= TREE_TYPE (rval
);
1099 tree last_rval
= NULL_TREE
;
1101 if (type
== error_mark_node
)
1102 return error_mark_node
;
1104 if (processing_template_decl
)
1105 return build_min_nt (ARROW_EXPR
, rval
);
1107 if (TREE_CODE (rval
) == OFFSET_REF
)
1109 rval
= resolve_offset_ref (datum
);
1110 type
= TREE_TYPE (rval
);
1113 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1115 rval
= convert_from_reference (rval
);
1116 type
= TREE_TYPE (rval
);
1119 if (IS_AGGR_TYPE (type
))
1121 while ((rval
= build_opfncall (COMPONENT_REF
, LOOKUP_NORMAL
, rval
,
1122 NULL_TREE
, NULL_TREE
)))
1124 if (rval
== error_mark_node
)
1125 return error_mark_node
;
1127 if (value_member (TREE_TYPE (rval
), types_memoized
))
1129 error ("circular pointer delegation detected");
1130 return error_mark_node
;
1134 types_memoized
= tree_cons (NULL_TREE
, TREE_TYPE (rval
),
1140 if (last_rval
== NULL_TREE
)
1142 cp_error ("base operand of `->' has non-pointer type `%T'", type
);
1143 return error_mark_node
;
1146 if (TREE_CODE (TREE_TYPE (last_rval
)) == REFERENCE_TYPE
)
1147 last_rval
= convert_from_reference (last_rval
);
1150 last_rval
= default_conversion (rval
);
1152 if (TREE_CODE (TREE_TYPE (last_rval
)) == POINTER_TYPE
)
1153 return build_indirect_ref (last_rval
, NULL_PTR
);
1156 error ("result of `operator->()' yields non-pointer result");
1158 error ("base operand of `->' is not a pointer");
1159 return error_mark_node
;
1162 /* Make an expression to refer to the COMPONENT field of
1163 structure or union value DATUM. COMPONENT is an arbitrary
1164 expression. DATUM has not already been checked out to be of
1167 For C++, COMPONENT may be a TREE_LIST. This happens when we must
1168 return an object of member type to a method of the current class,
1169 but there is not yet enough typing information to know which one.
1170 As a special case, if there is only one method by that name,
1171 it is returned. Otherwise we return an expression which other
1172 routines will have to know how to deal with later. */
1175 build_m_component_ref (datum
, component
)
1176 tree datum
, component
;
1179 tree objtype
= TREE_TYPE (datum
);
1183 if (processing_template_decl
)
1184 return build_min_nt (DOTSTAR_EXPR
, datum
, component
);
1186 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component
)))
1188 type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component
)));
1193 type
= TREE_TYPE (TREE_TYPE (component
));
1194 rettype
= TREE_TYPE (type
);
1197 if (datum
== error_mark_node
|| component
== error_mark_node
)
1198 return error_mark_node
;
1200 if (TREE_CODE (type
) != OFFSET_TYPE
&& TREE_CODE (type
) != METHOD_TYPE
)
1202 cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component
, type
);
1203 return error_mark_node
;
1206 if (TREE_CODE (objtype
) == REFERENCE_TYPE
)
1207 objtype
= TREE_TYPE (objtype
);
1208 objtype
= TYPE_MAIN_VARIANT (objtype
);
1210 if (! IS_AGGR_TYPE (objtype
))
1212 cp_error ("cannot apply member pointer `%E' to `%E'", component
, datum
);
1213 cp_error ("which is of non-aggregate type `%T'", objtype
);
1214 return error_mark_node
;
1217 binfo
= get_binfo (TYPE_METHOD_BASETYPE (type
), objtype
, 1);
1218 if (binfo
== NULL_TREE
)
1220 cp_error ("member type `%T::' incompatible with object type `%T'",
1221 TYPE_METHOD_BASETYPE (type
), objtype
);
1222 return error_mark_node
;
1224 else if (binfo
== error_mark_node
)
1225 return error_mark_node
;
1227 component
= build (OFFSET_REF
, rettype
, datum
, component
);
1228 if (TREE_CODE (type
) == OFFSET_TYPE
)
1229 component
= resolve_offset_ref (component
);
1233 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1236 build_functional_cast (exp
, parms
)
1240 /* This is either a call to a constructor,
1241 or a C cast in C++'s `functional' notation. */
1244 if (exp
== error_mark_node
|| parms
== error_mark_node
)
1245 return error_mark_node
;
1247 if (TREE_CODE (exp
) == IDENTIFIER_NODE
)
1249 if (IDENTIFIER_HAS_TYPE_VALUE (exp
))
1250 /* Either an enum or an aggregate type. */
1251 type
= IDENTIFIER_TYPE_VALUE (exp
);
1254 type
= lookup_name (exp
, 1);
1255 if (!type
|| TREE_CODE (type
) != TYPE_DECL
)
1257 cp_error ("`%T' fails to be a typedef or built-in type", exp
);
1258 return error_mark_node
;
1260 type
= TREE_TYPE (type
);
1263 else if (TREE_CODE (exp
) == TYPE_DECL
)
1264 type
= TREE_TYPE (exp
);
1268 if (processing_template_decl
)
1269 return build_min (CAST_EXPR
, type
, parms
);
1271 if (! IS_AGGR_TYPE (type
))
1273 /* this must build a C cast */
1274 if (parms
== NULL_TREE
)
1275 parms
= integer_zero_node
;
1278 if (TREE_CHAIN (parms
) != NULL_TREE
)
1279 pedwarn ("initializer list being treated as compound expression");
1280 parms
= build_compound_expr (parms
);
1283 return build_c_cast (type
, parms
);
1286 /* Prepare to evaluate as a call to a constructor. If this expression
1287 is actually used, for example,
1289 return X (arg1, arg2, ...);
1291 then the slot being initialized will be filled in. */
1293 if (TYPE_SIZE (complete_type (type
)) == NULL_TREE
)
1295 cp_error ("type `%T' is not yet defined", type
);
1296 return error_mark_node
;
1298 if (abstract_virtuals_error (NULL_TREE
, type
))
1299 return error_mark_node
;
1301 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
1302 return build_c_cast (type
, TREE_VALUE (parms
));
1304 /* We need to zero-initialize POD types. Let's do that for everything
1305 that doesn't need a constructor. */
1306 if (parms
== NULL_TREE
&& !TYPE_NEEDS_CONSTRUCTING (type
)
1307 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
1309 exp
= build (CONSTRUCTOR
, type
, NULL_TREE
, NULL_TREE
);
1310 return get_target_expr (exp
);
1313 exp
= build_method_call (NULL_TREE
, ctor_identifier
, parms
,
1314 TYPE_BINFO (type
), LOOKUP_NORMAL
);
1316 if (exp
== error_mark_node
)
1317 return error_mark_node
;
1319 return build_cplus_new (type
, exp
);
1322 /* Return the character string for the name that encodes the
1323 enumeral value VALUE in the domain TYPE. */
1326 enum_name_string (value
, type
)
1330 register tree values
= TYPE_VALUES (type
);
1331 register HOST_WIDE_INT intval
= TREE_INT_CST_LOW (value
);
1333 my_friendly_assert (TREE_CODE (type
) == ENUMERAL_TYPE
, 324);
1335 && TREE_INT_CST_LOW (TREE_VALUE (values
)) != intval
)
1336 values
= TREE_CHAIN (values
);
1337 if (values
== NULL_TREE
)
1339 char *buf
= (char *)oballoc (16 + TYPE_NAME_LENGTH (type
));
1341 /* Value must have been cast. */
1342 sprintf (buf
, "(enum %s)%ld",
1343 TYPE_NAME_STRING (type
), (long) intval
);
1346 return IDENTIFIER_POINTER (TREE_PURPOSE (values
));
1350 /* Print out a language-specific error message for
1351 (Pascal) case or (C) switch statements.
1352 CODE tells what sort of message to print.
1353 TYPE is the type of the switch index expression.
1354 NEW is the new value that we were trying to add.
1355 OLD is the old value that stopped us from adding it. */
1358 report_case_error (code
, type
, new_value
, old_value
)
1361 tree new_value
, old_value
;
1366 error ("case label not within a switch statement");
1368 error ("default label not within a switch statement");
1374 error ("multiple default labels in one switch");
1377 if (TREE_CODE (new_value
) == RANGE_EXPR
)
1378 if (TREE_CODE (old_value
) == RANGE_EXPR
)
1380 char *buf
= (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type
)));
1381 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1382 sprintf (buf
, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1383 enum_name_string (TREE_OPERAND (new_value
, 0), type
),
1384 enum_name_string (TREE_OPERAND (new_value
, 1), type
),
1385 enum_name_string (TREE_OPERAND (old_value
, 0), type
),
1386 enum_name_string (TREE_OPERAND (old_value
, 1), type
));
1388 sprintf (buf
, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1389 TREE_INT_CST_LOW (TREE_OPERAND (new_value
, 0)),
1390 TREE_INT_CST_LOW (TREE_OPERAND (new_value
, 1)),
1391 TREE_INT_CST_LOW (TREE_OPERAND (old_value
, 0)),
1392 TREE_INT_CST_LOW (TREE_OPERAND (old_value
, 1)));
1397 char *buf
= (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type
)));
1398 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1399 sprintf (buf
, "range [%s..%s] includes element `%s' in case expression",
1400 enum_name_string (TREE_OPERAND (new_value
, 0), type
),
1401 enum_name_string (TREE_OPERAND (new_value
, 1), type
),
1402 enum_name_string (old_value
, type
));
1404 sprintf (buf
, "range [%d..%d] includes (%d) in case expression",
1405 TREE_INT_CST_LOW (TREE_OPERAND (new_value
, 0)),
1406 TREE_INT_CST_LOW (TREE_OPERAND (new_value
, 1)),
1407 TREE_INT_CST_LOW (old_value
));
1410 else if (TREE_CODE (old_value
) == RANGE_EXPR
)
1412 char *buf
= (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type
)));
1413 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1414 sprintf (buf
, "range [%s..%s] includes element `%s' in case expression",
1415 enum_name_string (TREE_OPERAND (old_value
, 0), type
),
1416 enum_name_string (TREE_OPERAND (old_value
, 1), type
),
1417 enum_name_string (new_value
, type
));
1419 sprintf (buf
, "range [%d..%d] includes (%d) in case expression",
1420 TREE_INT_CST_LOW (TREE_OPERAND (old_value
, 0)),
1421 TREE_INT_CST_LOW (TREE_OPERAND (old_value
, 1)),
1422 TREE_INT_CST_LOW (new_value
));
1427 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1428 error ("duplicate label `%s' in switch statement",
1429 enum_name_string (new_value
, type
));
1431 error ("duplicate label (%d) in switch statement",
1432 TREE_INT_CST_LOW (new_value
));
1437 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1438 warning ("case value out of range for enum %s",
1439 TYPE_NAME_STRING (type
));
1441 warning ("case value out of range");
1445 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1446 error ("range values `%s' and `%s' reversed",
1447 enum_name_string (new_value
, type
),
1448 enum_name_string (old_value
, type
));
1450 error ("range values reversed");
1455 /* Complain about defining new types in inappropriate places. We give an
1456 exception for C-style casts, to accommodate GNU C stylings. */
1459 check_for_new_type (string
, inptree
)
1461 flagged_type_tree inptree
;
1463 if (inptree
.new_type_flag
1464 && (pedantic
|| strcmp (string
, "cast") != 0))
1465 pedwarn ("ANSI C++ forbids defining types within %s",string
);
1468 /* Add new exception specifier SPEC, to the LIST we currently have.
1469 If it's already in LIST then do nothing.
1470 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1471 know what we're doing. */
1474 add_exception_specifier (list
, spec
, complain
)
1482 if (spec
== error_mark_node
)
1485 my_friendly_assert (spec
&& (!list
|| TREE_VALUE (list
)), 19990317);
1487 /* [except.spec] 1, type in an exception specifier shall not be
1488 incomplete, or pointer or ref to incomplete other than pointer
1490 is_ptr
= TREE_CODE (core
) == POINTER_TYPE
;
1491 if (is_ptr
|| TREE_CODE (core
) == REFERENCE_TYPE
)
1492 core
= TREE_TYPE (core
);
1495 else if (TYPE_MAIN_VARIANT (core
) == void_type_node
)
1497 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
1500 ok
= TYPE_SIZE (complete_type (core
)) != NULL_TREE
;
1506 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
1507 if (same_type_p (TREE_VALUE (probe
), spec
))
1511 spec
= build_decl_list (NULL_TREE
, spec
);
1512 TREE_CHAIN (spec
) = list
;
1517 incomplete_type_error (NULL_TREE
, core
);