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). */
40 static tree process_init_constructor
PROTO((tree
, tree
, tree
*));
41 static void ack
PVPROTO ((const char *, ...)) ATTRIBUTE_PRINTF_1
;
43 /* Print an error message stemming from an attempt to use
44 BASETYPE as a base class for TYPE. */
47 error_not_base_type (basetype
, type
)
50 if (TREE_CODE (basetype
) == FUNCTION_DECL
)
51 basetype
= DECL_CLASS_CONTEXT (basetype
);
52 cp_error ("type `%T' is not a base type for type `%T'", basetype
, type
);
53 return error_mark_node
;
57 binfo_or_else (parent_or_type
, type
)
58 tree parent_or_type
, type
;
61 if (TYPE_MAIN_VARIANT (parent_or_type
) == TYPE_MAIN_VARIANT (type
))
62 return TYPE_BINFO (parent_or_type
);
63 if ((binfo
= get_binfo (parent_or_type
, TYPE_MAIN_VARIANT (type
), 0)))
65 if (binfo
== error_mark_node
)
69 error_not_base_type (parent_or_type
, type
);
73 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
74 value may not be changed thereafter. Thus, we emit hard errors for these,
75 rather than just pedwarns. If `SOFT' is 1, then we just pedwarn. (For
76 example, conversions to references.) */
79 readonly_error (arg
, string
, soft
)
85 void (*fn
) PVPROTO ((const char *, ...));
92 if (TREE_CODE (arg
) == COMPONENT_REF
)
94 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
95 fmt
= "%s of member `%D' in read-only structure";
97 fmt
= "%s of read-only member `%D'";
98 (*fn
) (fmt
, string
, TREE_OPERAND (arg
, 1));
100 else if (TREE_CODE (arg
) == VAR_DECL
)
102 if (DECL_LANG_SPECIFIC (arg
)
103 && DECL_IN_AGGR_P (arg
)
104 && !TREE_STATIC (arg
))
105 fmt
= "%s of constant field `%D'";
107 fmt
= "%s of read-only variable `%D'";
108 (*fn
) (fmt
, string
, arg
);
110 else if (TREE_CODE (arg
) == PARM_DECL
)
111 (*fn
) ("%s of read-only parameter `%D'", string
, arg
);
112 else if (TREE_CODE (arg
) == INDIRECT_REF
113 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))) == REFERENCE_TYPE
114 && (TREE_CODE (TREE_OPERAND (arg
, 0)) == VAR_DECL
115 || TREE_CODE (TREE_OPERAND (arg
, 0)) == PARM_DECL
))
116 (*fn
) ("%s of read-only reference `%D'", string
, TREE_OPERAND (arg
, 0));
117 else if (TREE_CODE (arg
) == RESULT_DECL
)
118 (*fn
) ("%s of read-only named return value `%D'", string
, arg
);
119 else if (TREE_CODE (arg
) == FUNCTION_DECL
)
120 (*fn
) ("%s of function `%D'", string
, arg
);
122 (*fn
) ("%s of read-only location", string
);
125 /* If TYPE has abstract virtual functions, issue an error about trying
126 to create an object of that type. DECL is the object declared, or
127 NULL_TREE if the declaration is unavailable. Returns 1 if an error
128 occurred; zero if all was well. */
131 abstract_virtuals_error (decl
, type
)
138 if (!CLASS_TYPE_P (type
) || !CLASSTYPE_ABSTRACT_VIRTUALS (type
))
141 u
= CLASSTYPE_ABSTRACT_VIRTUALS (type
);
144 if (TREE_CODE (decl
) == RESULT_DECL
)
147 if (TREE_CODE (decl
) == VAR_DECL
)
148 cp_error ("cannot declare variable `%D' to be of type `%T'",
150 else if (TREE_CODE (decl
) == PARM_DECL
)
151 cp_error ("cannot declare parameter `%D' to be of type `%T'",
153 else if (TREE_CODE (decl
) == FIELD_DECL
)
154 cp_error ("cannot declare field `%D' to be of type `%T'",
156 else if (TREE_CODE (decl
) == FUNCTION_DECL
157 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
158 cp_error ("invalid return type for method `%#D'", decl
);
159 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
160 cp_error ("invalid return type for function `%#D'", decl
);
163 cp_error ("cannot allocate an object of type `%T'", type
);
165 /* Only go through this once. */
166 if (TREE_PURPOSE (u
) == NULL_TREE
)
168 TREE_PURPOSE (u
) = error_mark_node
;
170 error (" since the following virtual functions are abstract:");
171 for (tu
= u
; tu
; tu
= TREE_CHAIN (tu
))
172 cp_error_at ("\t%#D", TREE_VALUE (tu
));
175 cp_error (" since type `%T' has abstract virtual functions", type
);
180 /* Print an error message for invalid use of an incomplete type.
181 VALUE is the expression that was used (or 0 if that isn't known)
182 and TYPE is the type that was invalid. */
185 incomplete_type_error (value
, type
)
189 /* Avoid duplicate error message. */
190 if (TREE_CODE (type
) == ERROR_MARK
)
194 /* We must print an error message. Be clever about what it says. */
196 switch (TREE_CODE (type
))
201 cp_error ("invalid use of undefined type `%#T'", type
);
202 cp_error_at ("forward declaration of `%#T'", type
);
206 cp_error ("invalid use of void expression");
210 if (TYPE_DOMAIN (type
))
212 type
= TREE_TYPE (type
);
215 cp_error ("invalid use of array with unspecified bounds");
220 cp_error ("invalid use of member (did you forget the `&' ?)");
223 case TEMPLATE_TYPE_PARM
:
224 cp_error ("invalid use of template type parameter");
228 if (value
&& TREE_CODE (value
) == COMPONENT_REF
)
230 else if (value
&& TREE_CODE (value
) == ADDR_EXPR
)
231 cp_error ("address of overloaded function with no contextual type information");
232 else if (value
&& TREE_CODE (value
) == OVERLOAD
)
233 cp_error ("overloaded function with no contextual type information");
235 cp_error ("insufficient contextual information to determine type");
239 my_friendly_abort (108);
242 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
243 || TREE_CODE (value
) == PARM_DECL
))
244 cp_error_at ("incomplete `%D' defined here", value
);
247 /* Like error(), but don't call report_error_function(). */
250 ack
VPROTO ((const char *msg
, ...))
252 #ifndef ANSI_PROTOTYPES
256 extern char * progname
;
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
)
927 register tree field
= TYPE_FIELDS (type
);
929 /* Find the first named field. ANSI decided in September 1990
930 that only named fields count here. */
931 while (field
&& (DECL_NAME (field
) == 0
932 || TREE_CODE (field
) != FIELD_DECL
))
933 field
= TREE_CHAIN (field
);
935 /* If this element specifies a field, initialize via that field. */
936 if (TREE_PURPOSE (tail
) != NULL_TREE
)
940 if (TREE_CODE (TREE_PURPOSE (tail
)) == FIELD_DECL
)
941 /* Handle the case of a call by build_c_cast. */
942 field
= TREE_PURPOSE (tail
), win
= 1;
943 else if (TREE_CODE (TREE_PURPOSE (tail
)) != IDENTIFIER_NODE
)
944 error ("index value instead of field name in union initializer");
948 for (temp
= TYPE_FIELDS (type
);
950 temp
= TREE_CHAIN (temp
))
951 if (DECL_NAME (temp
) == TREE_PURPOSE (tail
))
954 field
= temp
, win
= 1;
956 cp_error ("no field `%D' in union being initialized",
957 TREE_PURPOSE (tail
));
960 TREE_VALUE (tail
) = error_mark_node
;
964 cp_error ("union `%T' with no named members cannot be initialized",
966 TREE_VALUE (tail
) = error_mark_node
;
969 if (TREE_VALUE (tail
) != 0)
973 next1
= digest_init (TREE_TYPE (field
),
974 TREE_VALUE (tail
), &tail1
);
975 if (tail1
!= 0 && TREE_CODE (tail1
) != TREE_LIST
)
976 my_friendly_abort (357);
981 next1
= error_mark_node
;
982 tail
= TREE_CHAIN (tail
);
985 if (next1
== error_mark_node
)
987 else if (!TREE_CONSTANT (next1
))
989 else if (initializer_constant_valid_p (next1
, TREE_TYPE (next1
)) == 0)
991 members
= expr_tree_cons (field
, next1
, members
);
994 /* If arguments were specified as a list, just remove the ones we used. */
997 /* If arguments were specified as a constructor,
998 complain unless we used all the elements of the constructor. */
1000 pedwarn ("excess elements in aggregate initializer");
1003 return error_mark_node
;
1005 result
= build (CONSTRUCTOR
, type
, NULL_TREE
, nreverse (members
));
1007 TREE_HAS_CONSTRUCTOR (result
) = TREE_HAS_CONSTRUCTOR (init
);
1008 if (allconstant
) TREE_CONSTANT (result
) = 1;
1009 if (allconstant
&& allsimple
) TREE_STATIC (result
) = 1;
1013 /* Given a structure or union value DATUM, construct and return
1014 the structure or union component which results from narrowing
1015 that value by the type specified in BASETYPE. For example, given the
1018 class L { int ii; };
1019 class A : L { ... };
1020 class B : L { ... };
1021 class C : A, B { ... };
1029 x.A::ii refers to the ii member of the L part of
1030 the A part of the C object named by X. In this case,
1031 DATUM would be x, and BASETYPE would be A. */
1034 build_scoped_ref (datum
, basetype
)
1039 tree type
= TREE_TYPE (datum
);
1041 if (datum
== error_mark_node
)
1042 return error_mark_node
;
1044 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1045 type
= TREE_TYPE (type
);
1047 type
= TYPE_MAIN_VARIANT (type
);
1049 /* This is an easy conversion. */
1050 if (is_aggr_type (basetype
, 1))
1052 tree binfo
= TYPE_BINFO (basetype
);
1053 if (binfo
!= TYPE_BINFO (type
))
1055 binfo
= get_binfo (binfo
, type
, 1);
1056 if (binfo
== error_mark_node
)
1057 return error_mark_node
;
1059 return error_not_base_type (basetype
, type
);
1062 switch (TREE_CODE (datum
))
1067 case FIX_TRUNC_EXPR
:
1068 case FIX_FLOOR_EXPR
:
1069 case FIX_ROUND_EXPR
:
1071 ref
= convert_pointer_to (binfo
,
1072 build_unary_op (ADDR_EXPR
, TREE_OPERAND (datum
, 0), 0));
1075 ref
= convert_pointer_to (binfo
,
1076 build_unary_op (ADDR_EXPR
, datum
, 0));
1078 return build_indirect_ref (ref
, "(compiler error in build_scoped_ref)");
1080 return error_mark_node
;
1083 /* Build a reference to an object specified by the C++ `->' operator.
1084 Usually this just involves dereferencing the object, but if the
1085 `->' operator is overloaded, then such overloads must be
1086 performed until an object which does not have the `->' operator
1087 overloaded is found. An error is reported when circular pointer
1088 delegation is detected. */
1091 build_x_arrow (datum
)
1094 tree types_memoized
= NULL_TREE
;
1095 register tree rval
= datum
;
1096 tree type
= TREE_TYPE (rval
);
1097 tree last_rval
= NULL_TREE
;
1099 if (type
== error_mark_node
)
1100 return error_mark_node
;
1102 if (processing_template_decl
)
1103 return build_min_nt (ARROW_EXPR
, rval
);
1105 if (TREE_CODE (rval
) == OFFSET_REF
)
1107 rval
= resolve_offset_ref (datum
);
1108 type
= TREE_TYPE (rval
);
1111 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1113 rval
= convert_from_reference (rval
);
1114 type
= TREE_TYPE (rval
);
1117 if (IS_AGGR_TYPE (type
))
1119 while ((rval
= build_opfncall (COMPONENT_REF
, LOOKUP_NORMAL
, rval
,
1120 NULL_TREE
, NULL_TREE
)))
1122 if (rval
== error_mark_node
)
1123 return error_mark_node
;
1125 if (value_member (TREE_TYPE (rval
), types_memoized
))
1127 error ("circular pointer delegation detected");
1128 return error_mark_node
;
1132 types_memoized
= tree_cons (NULL_TREE
, TREE_TYPE (rval
),
1138 if (last_rval
== NULL_TREE
)
1140 cp_error ("base operand of `->' has non-pointer type `%T'", type
);
1141 return error_mark_node
;
1144 if (TREE_CODE (TREE_TYPE (last_rval
)) == REFERENCE_TYPE
)
1145 last_rval
= convert_from_reference (last_rval
);
1148 last_rval
= default_conversion (rval
);
1150 if (TREE_CODE (TREE_TYPE (last_rval
)) == POINTER_TYPE
)
1151 return build_indirect_ref (last_rval
, NULL_PTR
);
1154 error ("result of `operator->()' yields non-pointer result");
1156 error ("base operand of `->' is not a pointer");
1157 return error_mark_node
;
1160 /* Make an expression to refer to the COMPONENT field of
1161 structure or union value DATUM. COMPONENT is an arbitrary
1162 expression. DATUM has not already been checked out to be of
1165 For C++, COMPONENT may be a TREE_LIST. This happens when we must
1166 return an object of member type to a method of the current class,
1167 but there is not yet enough typing information to know which one.
1168 As a special case, if there is only one method by that name,
1169 it is returned. Otherwise we return an expression which other
1170 routines will have to know how to deal with later. */
1173 build_m_component_ref (datum
, component
)
1174 tree datum
, component
;
1177 tree objtype
= TREE_TYPE (datum
);
1181 if (processing_template_decl
)
1182 return build_min_nt (DOTSTAR_EXPR
, datum
, component
);
1184 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component
)))
1186 type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component
)));
1191 type
= TREE_TYPE (TREE_TYPE (component
));
1192 rettype
= TREE_TYPE (type
);
1195 if (datum
== error_mark_node
|| component
== error_mark_node
)
1196 return error_mark_node
;
1198 if (TREE_CODE (type
) != OFFSET_TYPE
&& TREE_CODE (type
) != METHOD_TYPE
)
1200 cp_error ("`%E' cannot be used as a member pointer, since it is of type `%T'", component
, type
);
1201 return error_mark_node
;
1204 if (TREE_CODE (objtype
) == REFERENCE_TYPE
)
1205 objtype
= TREE_TYPE (objtype
);
1206 objtype
= TYPE_MAIN_VARIANT (objtype
);
1208 if (! IS_AGGR_TYPE (objtype
))
1210 cp_error ("cannot apply member pointer `%E' to `%E'", component
, datum
);
1211 cp_error ("which is of non-aggregate type `%T'", objtype
);
1212 return error_mark_node
;
1215 binfo
= get_binfo (TYPE_METHOD_BASETYPE (type
), objtype
, 1);
1216 if (binfo
== NULL_TREE
)
1218 cp_error ("member type `%T::' incompatible with object type `%T'",
1219 TYPE_METHOD_BASETYPE (type
), objtype
);
1220 return error_mark_node
;
1222 else if (binfo
== error_mark_node
)
1223 return error_mark_node
;
1225 component
= build (OFFSET_REF
, rettype
, datum
, component
);
1226 if (TREE_CODE (type
) == OFFSET_TYPE
)
1227 component
= resolve_offset_ref (component
);
1231 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1234 build_functional_cast (exp
, parms
)
1238 /* This is either a call to a constructor,
1239 or a C cast in C++'s `functional' notation. */
1242 if (exp
== error_mark_node
|| parms
== error_mark_node
)
1243 return error_mark_node
;
1245 if (TREE_CODE (exp
) == IDENTIFIER_NODE
)
1247 if (IDENTIFIER_HAS_TYPE_VALUE (exp
))
1248 /* Either an enum or an aggregate type. */
1249 type
= IDENTIFIER_TYPE_VALUE (exp
);
1252 type
= lookup_name (exp
, 1);
1253 if (!type
|| TREE_CODE (type
) != TYPE_DECL
)
1255 cp_error ("`%T' fails to be a typedef or built-in type", exp
);
1256 return error_mark_node
;
1258 type
= TREE_TYPE (type
);
1261 else if (TREE_CODE (exp
) == TYPE_DECL
)
1262 type
= TREE_TYPE (exp
);
1266 if (processing_template_decl
)
1267 return build_min (CAST_EXPR
, type
, parms
);
1269 if (! IS_AGGR_TYPE (type
))
1271 /* this must build a C cast */
1272 if (parms
== NULL_TREE
)
1273 parms
= integer_zero_node
;
1276 if (TREE_CHAIN (parms
) != NULL_TREE
)
1277 pedwarn ("initializer list being treated as compound expression");
1278 parms
= build_compound_expr (parms
);
1281 return build_c_cast (type
, parms
);
1284 /* Prepare to evaluate as a call to a constructor. If this expression
1285 is actually used, for example,
1287 return X (arg1, arg2, ...);
1289 then the slot being initialized will be filled in. */
1291 if (TYPE_SIZE (complete_type (type
)) == NULL_TREE
)
1293 cp_error ("type `%T' is not yet defined", type
);
1294 return error_mark_node
;
1296 if (abstract_virtuals_error (NULL_TREE
, type
))
1297 return error_mark_node
;
1299 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
1300 return build_c_cast (type
, TREE_VALUE (parms
));
1302 /* We need to zero-initialize POD types. Let's do that for everything
1303 that doesn't need a constructor. */
1304 if (parms
== NULL_TREE
&& !TYPE_NEEDS_CONSTRUCTING (type
)
1305 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
1307 exp
= build (CONSTRUCTOR
, type
, NULL_TREE
, NULL_TREE
);
1308 return get_target_expr (exp
);
1311 exp
= build_method_call (NULL_TREE
, ctor_identifier
, parms
,
1312 TYPE_BINFO (type
), LOOKUP_NORMAL
);
1314 if (exp
== error_mark_node
)
1315 return error_mark_node
;
1317 return build_cplus_new (type
, exp
);
1320 /* Return the character string for the name that encodes the
1321 enumeral value VALUE in the domain TYPE. */
1324 enum_name_string (value
, type
)
1328 register tree values
= TYPE_VALUES (type
);
1329 register HOST_WIDE_INT intval
= TREE_INT_CST_LOW (value
);
1331 my_friendly_assert (TREE_CODE (type
) == ENUMERAL_TYPE
, 324);
1333 && TREE_INT_CST_LOW (TREE_VALUE (values
)) != intval
)
1334 values
= TREE_CHAIN (values
);
1335 if (values
== NULL_TREE
)
1337 char *buf
= (char *)oballoc (16 + TYPE_NAME_LENGTH (type
));
1339 /* Value must have been cast. */
1340 sprintf (buf
, "(enum %s)%ld",
1341 TYPE_NAME_STRING (type
), (long) intval
);
1344 return IDENTIFIER_POINTER (TREE_PURPOSE (values
));
1348 /* Print out a language-specific error message for
1349 (Pascal) case or (C) switch statements.
1350 CODE tells what sort of message to print.
1351 TYPE is the type of the switch index expression.
1352 NEW is the new value that we were trying to add.
1353 OLD is the old value that stopped us from adding it. */
1356 report_case_error (code
, type
, new_value
, old_value
)
1359 tree new_value
, old_value
;
1364 error ("case label not within a switch statement");
1366 error ("default label not within a switch statement");
1372 error ("multiple default labels in one switch");
1375 if (TREE_CODE (new_value
) == RANGE_EXPR
)
1376 if (TREE_CODE (old_value
) == RANGE_EXPR
)
1378 char *buf
= (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type
)));
1379 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1380 sprintf (buf
, "overlapping ranges [%s..%s], [%s..%s] in case expression",
1381 enum_name_string (TREE_OPERAND (new_value
, 0), type
),
1382 enum_name_string (TREE_OPERAND (new_value
, 1), type
),
1383 enum_name_string (TREE_OPERAND (old_value
, 0), type
),
1384 enum_name_string (TREE_OPERAND (old_value
, 1), type
));
1386 sprintf (buf
, "overlapping ranges [%d..%d], [%d..%d] in case expression",
1387 TREE_INT_CST_LOW (TREE_OPERAND (new_value
, 0)),
1388 TREE_INT_CST_LOW (TREE_OPERAND (new_value
, 1)),
1389 TREE_INT_CST_LOW (TREE_OPERAND (old_value
, 0)),
1390 TREE_INT_CST_LOW (TREE_OPERAND (old_value
, 1)));
1395 char *buf
= (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type
)));
1396 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1397 sprintf (buf
, "range [%s..%s] includes element `%s' in case expression",
1398 enum_name_string (TREE_OPERAND (new_value
, 0), type
),
1399 enum_name_string (TREE_OPERAND (new_value
, 1), type
),
1400 enum_name_string (old_value
, type
));
1402 sprintf (buf
, "range [%d..%d] includes (%d) in case expression",
1403 TREE_INT_CST_LOW (TREE_OPERAND (new_value
, 0)),
1404 TREE_INT_CST_LOW (TREE_OPERAND (new_value
, 1)),
1405 TREE_INT_CST_LOW (old_value
));
1408 else if (TREE_CODE (old_value
) == RANGE_EXPR
)
1410 char *buf
= (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type
)));
1411 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1412 sprintf (buf
, "range [%s..%s] includes element `%s' in case expression",
1413 enum_name_string (TREE_OPERAND (old_value
, 0), type
),
1414 enum_name_string (TREE_OPERAND (old_value
, 1), type
),
1415 enum_name_string (new_value
, type
));
1417 sprintf (buf
, "range [%d..%d] includes (%d) in case expression",
1418 TREE_INT_CST_LOW (TREE_OPERAND (old_value
, 0)),
1419 TREE_INT_CST_LOW (TREE_OPERAND (old_value
, 1)),
1420 TREE_INT_CST_LOW (new_value
));
1425 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1426 error ("duplicate label `%s' in switch statement",
1427 enum_name_string (new_value
, type
));
1429 error ("duplicate label (%d) in switch statement",
1430 TREE_INT_CST_LOW (new_value
));
1435 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1436 warning ("case value out of range for enum %s",
1437 TYPE_NAME_STRING (type
));
1439 warning ("case value out of range");
1443 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1444 error ("range values `%s' and `%s' reversed",
1445 enum_name_string (new_value
, type
),
1446 enum_name_string (old_value
, type
));
1448 error ("range values reversed");
1453 /* Complain about defining new types in inappropriate places. We give an
1454 exception for C-style casts, to accommodate GNU C stylings. */
1457 check_for_new_type (string
, inptree
)
1459 flagged_type_tree inptree
;
1461 if (inptree
.new_type_flag
1462 && (pedantic
|| strcmp (string
, "cast") != 0))
1463 pedwarn ("ANSI C++ forbids defining types within %s",string
);
1466 /* Add new exception specifier SPEC, to the LIST we currently have.
1467 If it's already in LIST then do nothing.
1468 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1469 know what we're doing. */
1472 add_exception_specifier (list
, spec
, complain
)
1480 if (spec
== error_mark_node
)
1483 my_friendly_assert (spec
&& (!list
|| TREE_VALUE (list
)), 19990317);
1485 /* [except.spec] 1, type in an exception specifier shall not be
1486 incomplete, or pointer or ref to incomplete other than pointer
1488 is_ptr
= TREE_CODE (core
) == POINTER_TYPE
;
1489 if (is_ptr
|| TREE_CODE (core
) == REFERENCE_TYPE
)
1490 core
= TREE_TYPE (core
);
1493 else if (TYPE_MAIN_VARIANT (core
) == void_type_node
)
1495 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
1498 ok
= TYPE_SIZE (complete_type (core
)) != NULL_TREE
;
1504 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
1505 if (same_type_p (TREE_VALUE (probe
), spec
))
1509 spec
= build_decl_list (NULL_TREE
, spec
);
1510 TREE_CHAIN (spec
) = list
;
1515 incomplete_type_error (NULL_TREE
, core
);