FSF GCC merge 02/23/03
[official-gcc.git] / gcc / cp / typeck2.c
blobc7dcb527b82e8f043dbc7c08ed6ce64090e8790f
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, 2001, 2002 Free Software Foundation, Inc.
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC 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)
12 any later version.
14 GCC 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 GCC; 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). */
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "tm.h"
38 #include "tree.h"
39 #include "cp-tree.h"
40 #include "flags.h"
41 #include "toplev.h"
42 #include "output.h"
43 #include "diagnostic.h"
45 static tree process_init_constructor PARAMS ((tree, tree, tree *));
47 /* Print an error message stemming from an attempt to use
48 BASETYPE as a base class for TYPE. */
50 tree
51 error_not_base_type (basetype, type)
52 tree basetype, type;
54 if (TREE_CODE (basetype) == FUNCTION_DECL)
55 basetype = DECL_CONTEXT (basetype);
56 error ("type `%T' is not a base type for type `%T'", basetype, type);
57 return error_mark_node;
60 tree
61 binfo_or_else (base, type)
62 tree base, type;
64 tree binfo = lookup_base (type, base, ba_ignore, NULL);
66 if (binfo == error_mark_node)
67 return NULL_TREE;
68 else if (!binfo)
69 error_not_base_type (base, type);
70 return binfo;
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.) */
78 void
79 readonly_error (arg, string, soft)
80 tree arg;
81 const char *string;
82 int soft;
84 const char *fmt;
85 void (*fn) PARAMS ((const char *, ...));
87 if (soft)
88 fn = pedwarn;
89 else
90 fn = error;
92 if (TREE_CODE (arg) == COMPONENT_REF)
94 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
95 fmt = "%s of data-member `%D' in read-only structure";
96 else
97 fmt = "%s of read-only data-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'";
106 else
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);
121 else
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)
132 tree decl;
133 tree type;
135 tree u;
136 tree tu;
138 if (!CLASS_TYPE_P (type) || !CLASSTYPE_PURE_VIRTUALS (type))
139 return 0;
141 if (!TYPE_SIZE (type))
142 /* TYPE is being defined, and during that time
143 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
144 return 0;
146 u = CLASSTYPE_PURE_VIRTUALS (type);
147 if (decl)
149 if (TREE_CODE (decl) == RESULT_DECL)
150 return 0;
152 if (TREE_CODE (decl) == VAR_DECL)
153 error ("cannot declare variable `%D' to be of type `%T'",
154 decl, type);
155 else if (TREE_CODE (decl) == PARM_DECL)
156 error ("cannot declare parameter `%D' to be of type `%T'",
157 decl, type);
158 else if (TREE_CODE (decl) == FIELD_DECL)
159 error ("cannot declare field `%D' to be of type `%T'",
160 decl, type);
161 else if (TREE_CODE (decl) == FUNCTION_DECL
162 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
163 error ("invalid return type for member function `%#D'", decl);
164 else if (TREE_CODE (decl) == FUNCTION_DECL)
165 error ("invalid return type for function `%#D'", decl);
167 else
168 error ("cannot allocate an object of type `%T'", type);
170 /* Only go through this once. */
171 if (TREE_PURPOSE (u) == NULL_TREE)
173 TREE_PURPOSE (u) = error_mark_node;
175 error (" because the following virtual functions are abstract:");
176 for (tu = u; tu; tu = TREE_CHAIN (tu))
177 cp_error_at ("\t%#D", TREE_VALUE (tu));
179 else
180 error (" since type `%T' has abstract virtual functions", type);
182 return 1;
185 /* Print an error message for invalid use of an incomplete type.
186 VALUE is the expression that was used (or 0 if that isn't known)
187 and TYPE is the type that was invalid. DIAG_TYPE indicates the
188 type of diagnostic: 0 for an error, 1 for a warning, 2 for a
189 pedwarn. */
191 void
192 cxx_incomplete_type_diagnostic (value, type, diag_type)
193 tree value;
194 tree type;
195 int diag_type;
197 int decl = 0;
198 void (*p_msg) PARAMS ((const char *, ...));
199 void (*p_msg_at) PARAMS ((const char *, ...));
201 if (diag_type == 1)
203 p_msg = warning;
204 p_msg_at = cp_warning_at;
206 else if (diag_type == 2)
208 p_msg = pedwarn;
209 p_msg_at = cp_pedwarn_at;
211 else
213 p_msg = error;
214 p_msg_at = cp_error_at;
217 /* Avoid duplicate error message. */
218 if (TREE_CODE (type) == ERROR_MARK)
219 return;
221 if (value != 0 && (TREE_CODE (value) == VAR_DECL
222 || TREE_CODE (value) == PARM_DECL
223 || TREE_CODE (value) == FIELD_DECL))
225 (*p_msg_at) ("`%D' has incomplete type", value);
226 decl = 1;
228 retry:
229 /* We must print an error message. Be clever about what it says. */
231 switch (TREE_CODE (type))
233 case RECORD_TYPE:
234 case UNION_TYPE:
235 case ENUMERAL_TYPE:
236 if (!decl)
237 (*p_msg) ("invalid use of undefined type `%#T'", type);
238 if (!TYPE_TEMPLATE_INFO (type))
239 (*p_msg_at) ("forward declaration of `%#T'", type);
240 else
241 (*p_msg_at) ("declaration of `%#T'", type);
242 break;
244 case VOID_TYPE:
245 (*p_msg) ("invalid use of `%T'", type);
246 break;
248 case ARRAY_TYPE:
249 if (TYPE_DOMAIN (type))
251 type = TREE_TYPE (type);
252 goto retry;
254 (*p_msg) ("invalid use of array with unspecified bounds");
255 break;
257 case OFFSET_TYPE:
258 bad_member:
259 (*p_msg) ("invalid use of member (did you forget the `&' ?)");
260 break;
262 case TEMPLATE_TYPE_PARM:
263 (*p_msg) ("invalid use of template type parameter");
264 break;
266 case UNKNOWN_TYPE:
267 if (value && TREE_CODE (value) == COMPONENT_REF)
268 goto bad_member;
269 else if (value && TREE_CODE (value) == ADDR_EXPR)
270 (*p_msg) ("address of overloaded function with no contextual type information");
271 else if (value && TREE_CODE (value) == OVERLOAD)
272 (*p_msg) ("overloaded function with no contextual type information");
273 else
274 (*p_msg) ("insufficient contextual information to determine type");
275 break;
277 default:
278 abort ();
282 /* Backward-compatibility interface to incomplete_type_diagnostic;
283 required by ../tree.c. */
284 #undef cxx_incomplete_type_error
285 void
286 cxx_incomplete_type_error (value, type)
287 tree value;
288 tree type;
290 cxx_incomplete_type_diagnostic (value, type, 0);
294 /* Perform appropriate conversions on the initial value of a variable,
295 store it in the declaration DECL,
296 and print any error messages that are appropriate.
297 If the init is invalid, store an ERROR_MARK.
299 C++: Note that INIT might be a TREE_LIST, which would mean that it is
300 a base class initializer for some aggregate type, hopefully compatible
301 with DECL. If INIT is a single element, and DECL is an aggregate
302 type, we silently convert INIT into a TREE_LIST, allowing a constructor
303 to be called.
305 If INIT is a TREE_LIST and there is no constructor, turn INIT
306 into a CONSTRUCTOR and use standard initialization techniques.
307 Perhaps a warning should be generated?
309 Returns value of initializer if initialization could not be
310 performed for static variable. In that case, caller must do
311 the storing. */
313 tree
314 store_init_value (decl, init)
315 tree decl, init;
317 register tree value, type;
319 /* If variable's type was invalidly declared, just ignore it. */
321 type = TREE_TYPE (decl);
322 if (TREE_CODE (type) == ERROR_MARK)
323 return NULL_TREE;
325 if (IS_AGGR_TYPE (type))
327 if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
328 && TREE_CODE (init) != CONSTRUCTOR)
329 abort ();
331 if (TREE_CODE (init) == TREE_LIST)
333 error ("constructor syntax used, but no constructor declared for type `%T'", type);
334 init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
337 else if (TREE_CODE (init) == TREE_LIST
338 && TREE_TYPE (init) != unknown_type_node)
340 if (TREE_CODE (decl) == RESULT_DECL)
342 if (TREE_CHAIN (init))
344 warning ("comma expression used to initialize return value");
345 init = build_compound_expr (init);
347 else
348 init = TREE_VALUE (init);
350 else if (TREE_CODE (init) == TREE_LIST
351 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
353 error ("cannot initialize arrays using this syntax");
354 return NULL_TREE;
356 else
358 /* We get here with code like `int a (2);' */
360 if (TREE_CHAIN (init) != NULL_TREE)
362 pedwarn ("initializer list being treated as compound expression");
363 init = build_compound_expr (init);
365 else
366 init = TREE_VALUE (init);
370 /* End of special C++ code. */
372 /* Digest the specified initializer into an expression. */
373 value = digest_init (type, init, (tree *) 0);
375 /* Store the expression if valid; else report error. */
377 if (TREE_CODE (value) == ERROR_MARK)
379 /* Other code expects that initializers for objects of types that need
380 constructing never make it into DECL_INITIAL, and passes 'init' to
381 build_aggr_init without checking DECL_INITIAL. So just return. */
382 else if (TYPE_NEEDS_CONSTRUCTING (type))
383 return value;
384 else if (TREE_STATIC (decl)
385 && (! TREE_CONSTANT (value)
386 || ! initializer_constant_valid_p (value, TREE_TYPE (value))
387 #if 0
388 /* A STATIC PUBLIC int variable doesn't have to be
389 run time inited when doing pic. (mrs) */
390 /* Since ctors and dtors are the only things that can
391 reference vtables, and they are always written down
392 the vtable definition, we can leave the
393 vtables in initialized data space.
394 However, other initialized data cannot be initialized
395 this way. Instead a global file-level initializer
396 must do the job. */
397 || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl))
398 #endif
401 return value;
402 #if 0 /* No, that's C. jason 9/19/94 */
403 else
405 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
407 if (! TREE_CONSTANT (value) || ! TREE_STATIC (value))
408 pedwarn ("ISO C++ forbids non-constant aggregate initializer expressions");
411 #endif
413 /* Store the VALUE in DECL_INITIAL. If we're building a
414 statement-tree we will actually expand the initialization later
415 when we output this function. */
416 DECL_INITIAL (decl) = value;
417 return NULL_TREE;
421 /* Digest the parser output INIT as an initializer for type TYPE.
422 Return a C expression of type TYPE to represent the initial value.
424 If TAIL is nonzero, it points to a variable holding a list of elements
425 of which INIT is the first. We update the list stored there by
426 removing from the head all the elements that we use.
427 Normally this is only one; we use more than one element only if
428 TYPE is an aggregate and INIT is not a constructor. */
430 tree
431 digest_init (type, init, tail)
432 tree type, init, *tail;
434 enum tree_code code = TREE_CODE (type);
435 tree element = NULL_TREE;
436 tree old_tail_contents = NULL_TREE;
437 /* Nonzero if INIT is a braced grouping. */
438 int raw_constructor;
440 /* By default, assume we use one element from a list.
441 We correct this later in the sole case where it is not true. */
443 if (tail)
445 old_tail_contents = *tail;
446 *tail = TREE_CHAIN (*tail);
449 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
450 && TREE_VALUE (init) == error_mark_node))
451 return error_mark_node;
453 if (TREE_CODE (init) == ERROR_MARK)
454 /* __PRETTY_FUNCTION__'s initializer is a bogus expression inside
455 a template function. This gets substituted during instantiation. */
456 return init;
458 /* We must strip the outermost array type when completing the type,
459 because the its bounds might be incomplete at the moment. */
460 if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
461 ? TREE_TYPE (type) : type, NULL_TREE))
462 return error_mark_node;
464 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
465 if (TREE_CODE (init) == NON_LVALUE_EXPR)
466 init = TREE_OPERAND (init, 0);
468 raw_constructor = (TREE_CODE (init) == CONSTRUCTOR
469 && TREE_HAS_CONSTRUCTOR (init));
471 if (raw_constructor
472 && CONSTRUCTOR_ELTS (init) != 0
473 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
475 element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
476 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
477 if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
478 element = TREE_OPERAND (element, 0);
479 if (element == error_mark_node)
480 return element;
483 /* Initialization of an array of chars from a string constant
484 optionally enclosed in braces. */
486 if (code == ARRAY_TYPE)
488 tree typ1;
490 if (TREE_CODE (init) == TREE_LIST)
492 error ("initializing array with parameter list");
493 return error_mark_node;
496 typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
497 if (char_type_p (typ1)
498 && ((init && TREE_CODE (init) == STRING_CST)
499 || (element && TREE_CODE (element) == STRING_CST)))
501 tree string = element ? element : init;
503 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
504 != char_type_node)
505 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
507 error ("char-array initialized from wide string");
508 return error_mark_node;
510 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
511 == char_type_node)
512 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
514 error ("int-array initialized from non-wide string");
515 return error_mark_node;
518 TREE_TYPE (string) = type;
519 if (TYPE_DOMAIN (type) != 0
520 && TREE_CONSTANT (TYPE_SIZE (type)))
522 register int size
523 = TREE_INT_CST_LOW (TYPE_SIZE (type));
524 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
525 /* In C it is ok to subtract 1 from the length of the string
526 because it's ok to ignore the terminating null char that is
527 counted in the length of the constant, but in C++ this would
528 be invalid. */
529 if (size < TREE_STRING_LENGTH (string))
530 pedwarn ("initializer-string for array of chars is too long");
532 return string;
536 /* Handle scalar types, including conversions,
537 and signature pointers and references. */
539 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
540 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
541 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
542 || TYPE_PTRMEMFUNC_P (type))
544 if (raw_constructor)
546 if (element == 0)
548 error ("initializer for scalar variable requires one element");
549 return error_mark_node;
551 init = element;
553 while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
555 pedwarn ("braces around scalar initializer for `%T'", type);
556 init = CONSTRUCTOR_ELTS (init);
557 if (TREE_CHAIN (init))
558 pedwarn ("ignoring extra initializers for `%T'", type);
559 init = TREE_VALUE (init);
562 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
563 "initialization", NULL_TREE, 0);
566 /* Come here only for records and arrays (and unions with constructors). */
568 if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
570 error ("variable-sized object of type `%T' may not be initialized",
571 type);
572 return error_mark_node;
575 if (code == ARRAY_TYPE || code == VECTOR_TYPE || IS_AGGR_TYPE_CODE (code))
577 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type)
578 && TREE_HAS_CONSTRUCTOR (init))
580 error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
581 type, init);
582 return error_mark_node;
584 else if (raw_constructor)
585 return process_init_constructor (type, init, (tree *)0);
586 else if (can_convert_arg (type, TREE_TYPE (init), init)
587 || TYPE_NON_AGGREGATE_CLASS (type))
588 /* These are never initialized from multiple constructor elements. */;
589 else if (tail != 0)
591 *tail = old_tail_contents;
592 return process_init_constructor (type, 0, tail);
595 if (code != ARRAY_TYPE)
597 int flags = LOOKUP_NORMAL;
598 /* Initialization from { } is copy-initialization. */
599 if (tail)
600 flags |= LOOKUP_ONLYCONVERTING;
602 return convert_for_initialization (NULL_TREE, type, init, flags,
603 "initialization", NULL_TREE, 0);
607 error ("invalid initializer");
608 return error_mark_node;
611 /* Process a constructor for a variable of type TYPE.
612 The constructor elements may be specified either with INIT or with ELTS,
613 only one of which should be non-null.
615 If INIT is specified, it is a CONSTRUCTOR node which is specifically
616 and solely for initializing this datum.
618 If ELTS is specified, it is the address of a variable containing
619 a list of expressions. We take as many elements as we need
620 from the head of the list and update the list.
622 In the resulting constructor, TREE_CONSTANT is set if all elts are
623 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
624 constants that the assembler and linker can compute them. */
626 static tree
627 process_init_constructor (type, init, elts)
628 tree type, init, *elts;
630 register tree tail;
631 /* List of the elements of the result constructor,
632 in reverse order. */
633 register tree members = NULL;
634 register tree next1;
635 tree result;
636 int allconstant = 1;
637 int allsimple = 1;
638 int erroneous = 0;
640 /* Make TAIL be the list of elements to use for the initialization,
641 no matter how the data was given to us. */
643 if (elts)
645 if (warn_missing_braces)
646 warning ("aggregate has a partly bracketed initializer");
647 tail = *elts;
649 else
650 tail = CONSTRUCTOR_ELTS (init);
652 /* Gobble as many elements as needed, and make a constructor or initial value
653 for each element of this aggregate. Chain them together in result.
654 If there are too few, use 0 for each scalar ultimate component. */
656 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
658 register long len;
659 register int i;
661 if (TREE_CODE (type) == ARRAY_TYPE)
663 tree domain = TYPE_DOMAIN (type);
664 if (domain)
665 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
666 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
667 + 1);
668 else
669 len = -1; /* Take as many as there are */
671 else
673 /* Vectors are like simple fixed-size arrays. */
674 len = TYPE_VECTOR_SUBPARTS (type);
677 for (i = 0; len < 0 || i < len; i++)
679 if (tail)
681 if (TREE_PURPOSE (tail)
682 && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
683 || compare_tree_int (TREE_PURPOSE (tail), i) != 0))
684 sorry ("non-trivial labeled initializers");
686 if (TREE_VALUE (tail) != 0)
688 tree tail1 = tail;
689 next1 = digest_init (TREE_TYPE (type),
690 TREE_VALUE (tail), &tail1);
691 if (next1 == error_mark_node)
692 return next1;
693 my_friendly_assert
694 (same_type_ignoring_top_level_qualifiers_p
695 (TREE_TYPE (type), TREE_TYPE (next1)),
696 981123);
697 my_friendly_assert (tail1 == 0
698 || TREE_CODE (tail1) == TREE_LIST, 319);
699 if (tail == tail1 && len < 0)
701 error ("non-empty initializer for array of empty elements");
702 /* Just ignore what we were supposed to use. */
703 tail1 = NULL_TREE;
705 tail = tail1;
707 else
709 next1 = error_mark_node;
710 tail = TREE_CHAIN (tail);
713 else if (len < 0)
714 /* We're done. */
715 break;
716 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
718 /* If this type needs constructors run for
719 default-initialization, we can't rely on the backend to do it
720 for us, so build up TARGET_EXPRs. If the type in question is
721 a class, just build one up; if it's an array, recurse. */
723 if (IS_AGGR_TYPE (TREE_TYPE (type)))
724 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
725 else
726 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE);
727 next1 = digest_init (TREE_TYPE (type), next1, 0);
729 else if (! zero_init_p (TREE_TYPE (type)))
730 next1 = build_zero_init (TREE_TYPE (type),
731 /*static_storage_p=*/false);
732 else
733 /* The default zero-initialization is fine for us; don't
734 add anything to the CONSTRUCTOR. */
735 break;
737 if (next1 == error_mark_node)
738 erroneous = 1;
739 else if (!TREE_CONSTANT (next1))
740 allconstant = 0;
741 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
742 allsimple = 0;
743 members = tree_cons (size_int (i), next1, members);
746 else if (TREE_CODE (type) == RECORD_TYPE)
748 register tree field;
750 if (tail)
752 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
754 sorry ("initializer list for object of class with virtual base classes");
755 return error_mark_node;
758 if (TYPE_BINFO_BASETYPES (type))
760 sorry ("initializer list for object of class with base classes");
761 return error_mark_node;
764 if (TYPE_POLYMORPHIC_P (type))
766 sorry ("initializer list for object using virtual functions");
767 return error_mark_node;
771 for (field = TYPE_FIELDS (type); field;
772 field = TREE_CHAIN (field))
774 if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
776 members = tree_cons (field, integer_zero_node, members);
777 continue;
780 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
781 continue;
783 if (tail)
785 if (TREE_PURPOSE (tail)
786 && TREE_PURPOSE (tail) != field
787 && TREE_PURPOSE (tail) != DECL_NAME (field))
788 sorry ("non-trivial labeled initializers");
790 if (TREE_VALUE (tail) != 0)
792 tree tail1 = tail;
794 next1 = digest_init (TREE_TYPE (field),
795 TREE_VALUE (tail), &tail1);
796 my_friendly_assert (tail1 == 0
797 || TREE_CODE (tail1) == TREE_LIST, 320);
798 tail = tail1;
800 else
802 next1 = error_mark_node;
803 tail = TREE_CHAIN (tail);
806 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
808 /* If this type needs constructors run for
809 default-initialization, we can't rely on the backend to do it
810 for us, so build up TARGET_EXPRs. If the type in question is
811 a class, just build one up; if it's an array, recurse. */
813 if (IS_AGGR_TYPE (TREE_TYPE (field)))
814 next1 = build_functional_cast (TREE_TYPE (field),
815 NULL_TREE);
816 else
818 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE,
819 NULL_TREE);
820 if (init)
821 TREE_HAS_CONSTRUCTOR (next1)
822 = TREE_HAS_CONSTRUCTOR (init);
824 next1 = digest_init (TREE_TYPE (field), next1, 0);
826 /* Warn when some struct elements are implicitly initialized. */
827 if (extra_warnings
828 && (!init || TREE_HAS_CONSTRUCTOR (init)))
829 warning ("missing initializer for member `%D'", field);
831 else
833 if (TREE_READONLY (field))
834 error ("uninitialized const member `%D'", field);
835 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
836 error ("member `%D' with uninitialized const fields",
837 field);
838 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
839 error ("member `%D' is uninitialized reference", field);
841 /* Warn when some struct elements are implicitly initialized
842 to zero. */
843 if (extra_warnings
844 && (!init || TREE_HAS_CONSTRUCTOR (init)))
845 warning ("missing initializer for member `%D'", field);
847 if (! zero_init_p (TREE_TYPE (field)))
848 next1 = build_zero_init (TREE_TYPE (field),
849 /*static_storage_p=*/false);
850 else
851 /* The default zero-initialization is fine for us; don't
852 add anything to the CONSTRUCTOR. */
853 continue;
856 if (next1 == error_mark_node)
857 erroneous = 1;
858 else if (!TREE_CONSTANT (next1))
859 allconstant = 0;
860 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
861 allsimple = 0;
862 members = tree_cons (field, next1, members);
865 else if (TREE_CODE (type) == UNION_TYPE
866 /* If the initializer was empty, use default zero initialization. */
867 && tail)
869 register tree field = TYPE_FIELDS (type);
871 /* Find the first named field. ANSI decided in September 1990
872 that only named fields count here. */
873 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
874 field = TREE_CHAIN (field);
876 /* If this element specifies a field, initialize via that field. */
877 if (TREE_PURPOSE (tail) != NULL_TREE)
879 int win = 0;
881 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
882 /* Handle the case of a call by build_c_cast. */
883 field = TREE_PURPOSE (tail), win = 1;
884 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
885 error ("index value instead of field name in union initializer");
886 else
888 tree temp;
889 for (temp = TYPE_FIELDS (type);
890 temp;
891 temp = TREE_CHAIN (temp))
892 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
893 break;
894 if (temp)
895 field = temp, win = 1;
896 else
897 error ("no field `%D' in union being initialized",
898 TREE_PURPOSE (tail));
900 if (!win)
901 TREE_VALUE (tail) = error_mark_node;
903 else if (field == 0)
905 error ("union `%T' with no named members cannot be initialized",
906 type);
907 TREE_VALUE (tail) = error_mark_node;
910 if (TREE_VALUE (tail) != 0)
912 tree tail1 = tail;
914 next1 = digest_init (TREE_TYPE (field),
915 TREE_VALUE (tail), &tail1);
916 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
917 abort ();
918 tail = tail1;
920 else
922 next1 = error_mark_node;
923 tail = TREE_CHAIN (tail);
926 if (next1 == error_mark_node)
927 erroneous = 1;
928 else if (!TREE_CONSTANT (next1))
929 allconstant = 0;
930 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
931 allsimple = 0;
932 members = tree_cons (field, next1, members);
935 /* If arguments were specified as a list, just remove the ones we used. */
936 if (elts)
937 *elts = tail;
938 /* If arguments were specified as a constructor,
939 complain unless we used all the elements of the constructor. */
940 else if (tail)
941 pedwarn ("excess elements in aggregate initializer");
943 if (erroneous)
944 return error_mark_node;
946 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
947 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
948 complete_array_type (type, result, /*do_default=*/0);
949 if (init)
950 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
951 if (allconstant) TREE_CONSTANT (result) = 1;
952 if (allconstant && allsimple) TREE_STATIC (result) = 1;
953 return result;
956 /* Given a structure or union value DATUM, construct and return
957 the structure or union component which results from narrowing
958 that value to the base specified in BASETYPE. For example, given the
959 hierarchy
961 class L { int ii; };
962 class A : L { ... };
963 class B : L { ... };
964 class C : A, B { ... };
966 and the declaration
968 C x;
970 then the expression
972 x.A::ii refers to the ii member of the L part of
973 the A part of the C object named by X. In this case,
974 DATUM would be x, and BASETYPE would be A.
976 I used to think that this was nonconformant, that the standard specified
977 that first we look up ii in A, then convert x to an L& and pull out the
978 ii part. But in fact, it does say that we convert x to an A&; A here
979 is known as the "naming class". (jason 2000-12-19)
981 BINFO_P points to a variable initialized either to NULL_TREE or to the
982 binfo for the specific base subobject we want to convert to. */
984 tree
985 build_scoped_ref (datum, basetype, binfo_p)
986 tree datum;
987 tree basetype;
988 tree *binfo_p;
990 tree binfo;
992 if (datum == error_mark_node)
993 return error_mark_node;
994 if (*binfo_p)
995 binfo = *binfo_p;
996 else
997 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
999 if (!binfo || binfo == error_mark_node)
1001 *binfo_p = NULL_TREE;
1002 if (!binfo)
1003 error_not_base_type (basetype, TREE_TYPE (datum));
1004 return error_mark_node;
1007 *binfo_p = binfo;
1008 return build_base_path (PLUS_EXPR, datum, binfo, 1);
1011 /* Build a reference to an object specified by the C++ `->' operator.
1012 Usually this just involves dereferencing the object, but if the
1013 `->' operator is overloaded, then such overloads must be
1014 performed until an object which does not have the `->' operator
1015 overloaded is found. An error is reported when circular pointer
1016 delegation is detected. */
1018 tree
1019 build_x_arrow (datum)
1020 tree datum;
1022 tree types_memoized = NULL_TREE;
1023 register tree rval = datum;
1024 tree type = TREE_TYPE (rval);
1025 tree last_rval = NULL_TREE;
1027 if (type == error_mark_node)
1028 return error_mark_node;
1030 if (processing_template_decl)
1031 return build_min_nt (ARROW_EXPR, rval);
1033 if (TREE_CODE (rval) == OFFSET_REF)
1035 rval = resolve_offset_ref (datum);
1036 type = TREE_TYPE (rval);
1039 if (TREE_CODE (type) == REFERENCE_TYPE)
1041 rval = convert_from_reference (rval);
1042 type = TREE_TYPE (rval);
1045 if (IS_AGGR_TYPE (type))
1047 while ((rval = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, rval,
1048 NULL_TREE, NULL_TREE)))
1050 if (rval == error_mark_node)
1051 return error_mark_node;
1053 if (value_member (TREE_TYPE (rval), types_memoized))
1055 error ("circular pointer delegation detected");
1056 return error_mark_node;
1058 else
1060 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1061 types_memoized);
1063 last_rval = rval;
1066 if (last_rval == NULL_TREE)
1068 error ("base operand of `->' has non-pointer type `%T'", type);
1069 return error_mark_node;
1072 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1073 last_rval = convert_from_reference (last_rval);
1075 else
1076 last_rval = default_conversion (rval);
1078 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1079 return build_indirect_ref (last_rval, NULL);
1081 if (types_memoized)
1082 error ("result of `operator->()' yields non-pointer result");
1083 else
1084 error ("base operand of `->' is not a pointer");
1085 return error_mark_node;
1088 /* Make an expression to refer to the COMPONENT field of
1089 structure or union value DATUM. COMPONENT is an arbitrary
1090 expression. DATUM has not already been checked out to be of
1091 aggregate type.
1093 For C++, COMPONENT may be a TREE_LIST. This happens when we must
1094 return an object of member type to a method of the current class,
1095 but there is not yet enough typing information to know which one.
1096 As a special case, if there is only one method by that name,
1097 it is returned. Otherwise we return an expression which other
1098 routines will have to know how to deal with later. */
1100 tree
1101 build_m_component_ref (datum, component)
1102 tree datum, component;
1104 tree type;
1105 tree objtype;
1106 tree field_type;
1107 int type_quals;
1108 tree binfo;
1110 if (processing_template_decl)
1111 return build_min_nt (DOTSTAR_EXPR, datum, component);
1113 datum = decay_conversion (datum);
1115 if (datum == error_mark_node || component == error_mark_node)
1116 return error_mark_node;
1118 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1120 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1122 type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1123 field_type = type;
1125 else if (TYPE_PTRMEM_P (TREE_TYPE (component)))
1127 type = TREE_TYPE (TREE_TYPE (component));
1128 field_type = TREE_TYPE (type);
1130 /* Compute the type of the field, as described in [expr.ref]. */
1131 type_quals = TYPE_UNQUALIFIED;
1132 if (TREE_CODE (field_type) == REFERENCE_TYPE)
1133 /* The standard says that the type of the result should be the
1134 type referred to by the reference. But for now, at least,
1135 we do the conversion from reference type later. */
1137 else
1139 type_quals = (cp_type_quals (field_type)
1140 | cp_type_quals (TREE_TYPE (datum)));
1142 /* There's no such thing as a mutable pointer-to-member, so
1143 things are not as complex as they are for references to
1144 non-static data members. */
1145 field_type = cp_build_qualified_type (field_type, type_quals);
1148 else
1150 error ("`%E' cannot be used as a member pointer, since it is of type `%T'",
1151 component, TREE_TYPE (component));
1152 return error_mark_node;
1155 if (! IS_AGGR_TYPE (objtype))
1157 error ("cannot apply member pointer `%E' to `%E', which is of non-aggregate type `%T'",
1158 component, datum, objtype);
1159 return error_mark_node;
1162 binfo = lookup_base (objtype, TYPE_METHOD_BASETYPE (type),
1163 ba_check, NULL);
1164 if (!binfo)
1166 error ("member type `%T::' incompatible with object type `%T'",
1167 TYPE_METHOD_BASETYPE (type), objtype);
1168 return error_mark_node;
1170 else if (binfo == error_mark_node)
1171 return error_mark_node;
1173 component = build (OFFSET_REF, field_type, datum, component);
1174 if (TREE_CODE (type) == OFFSET_TYPE)
1175 component = resolve_offset_ref (component);
1176 return component;
1179 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1181 tree
1182 build_functional_cast (exp, parms)
1183 tree exp;
1184 tree parms;
1186 /* This is either a call to a constructor,
1187 or a C cast in C++'s `functional' notation. */
1188 tree type;
1190 if (exp == error_mark_node || parms == error_mark_node)
1191 return error_mark_node;
1193 if (TREE_CODE (exp) == IDENTIFIER_NODE)
1195 if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1196 /* Either an enum or an aggregate type. */
1197 type = IDENTIFIER_TYPE_VALUE (exp);
1198 else
1200 type = lookup_name (exp, 1);
1201 if (!type || TREE_CODE (type) != TYPE_DECL)
1203 error ("`%T' fails to be a typedef or built-in type", exp);
1204 return error_mark_node;
1206 type = TREE_TYPE (type);
1209 else if (TREE_CODE (exp) == TYPE_DECL)
1210 type = TREE_TYPE (exp);
1211 else
1212 type = exp;
1214 if (processing_template_decl)
1215 return build_min (CAST_EXPR, type, parms);
1217 if (! IS_AGGR_TYPE (type))
1219 /* this must build a C cast */
1220 if (parms == NULL_TREE)
1221 parms = integer_zero_node;
1222 else
1224 if (TREE_CHAIN (parms) != NULL_TREE)
1225 pedwarn ("initializer list being treated as compound expression");
1226 parms = build_compound_expr (parms);
1229 return build_c_cast (type, parms);
1232 /* Prepare to evaluate as a call to a constructor. If this expression
1233 is actually used, for example,
1235 return X (arg1, arg2, ...);
1237 then the slot being initialized will be filled in. */
1239 if (!complete_type_or_else (type, NULL_TREE))
1240 return error_mark_node;
1241 if (abstract_virtuals_error (NULL_TREE, type))
1242 return error_mark_node;
1244 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1245 return build_c_cast (type, TREE_VALUE (parms));
1247 /* We need to zero-initialize POD types. Let's do that for everything
1248 that doesn't need a constructor. */
1249 if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1250 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1252 exp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
1253 return get_target_expr (exp);
1256 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, parms,
1257 TYPE_BINFO (type), LOOKUP_NORMAL);
1259 if (exp == error_mark_node)
1260 return error_mark_node;
1262 return build_cplus_new (type, exp);
1266 /* Add new exception specifier SPEC, to the LIST we currently have.
1267 If it's already in LIST then do nothing.
1268 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1269 know what we're doing. */
1271 tree
1272 add_exception_specifier (list, spec, complain)
1273 tree list, spec;
1274 int complain;
1276 int ok;
1277 tree core = spec;
1278 int is_ptr;
1279 int diag_type = -1; /* none */
1281 if (spec == error_mark_node)
1282 return list;
1284 my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317);
1286 /* [except.spec] 1, type in an exception specifier shall not be
1287 incomplete, or pointer or ref to incomplete other than pointer
1288 to cv void. */
1289 is_ptr = TREE_CODE (core) == POINTER_TYPE;
1290 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1291 core = TREE_TYPE (core);
1292 if (complain < 0)
1293 ok = 1;
1294 else if (VOID_TYPE_P (core))
1295 ok = is_ptr;
1296 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1297 ok = 1;
1298 else if (processing_template_decl)
1299 ok = 1;
1300 else
1302 ok = 1;
1303 /* 15.4/1 says that types in an exception specifier must be complete,
1304 but it seems more reasonable to only require this on definitions
1305 and calls. So just give a pedwarn at this point; we will give an
1306 error later if we hit one of those two cases. */
1307 if (!COMPLETE_TYPE_P (complete_type (core)))
1308 diag_type = 2; /* pedwarn */
1311 if (ok)
1313 tree probe;
1315 for (probe = list; probe; probe = TREE_CHAIN (probe))
1316 if (same_type_p (TREE_VALUE (probe), spec))
1317 break;
1318 if (!probe)
1319 list = tree_cons (NULL_TREE, spec, list);
1321 else
1322 diag_type = 0; /* error */
1324 if (diag_type >= 0 && complain)
1325 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1327 return list;
1330 /* Combine the two exceptions specifier lists LIST and ADD, and return
1331 their union. */
1333 tree
1334 merge_exception_specifiers (list, add)
1335 tree list, add;
1337 if (!list || !add)
1338 return NULL_TREE;
1339 else if (!TREE_VALUE (list))
1340 return add;
1341 else if (!TREE_VALUE (add))
1342 return list;
1343 else
1345 tree orig_list = list;
1347 for (; add; add = TREE_CHAIN (add))
1349 tree spec = TREE_VALUE (add);
1350 tree probe;
1352 for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1353 if (same_type_p (TREE_VALUE (probe), spec))
1354 break;
1355 if (!probe)
1357 spec = build_tree_list (NULL_TREE, spec);
1358 TREE_CHAIN (spec) = list;
1359 list = spec;
1363 return list;
1366 /* Subroutine of build_call. Ensure that each of the types in the
1367 exception specification is complete. Technically, 15.4/1 says that
1368 they need to be complete when we see a declaration of the function,
1369 but we should be able to get away with only requiring this when the
1370 function is defined or called. See also add_exception_specifier. */
1372 void
1373 require_complete_eh_spec_types (fntype, decl)
1374 tree fntype, decl;
1376 tree raises;
1377 /* Don't complain about calls to op new. */
1378 if (decl && DECL_ARTIFICIAL (decl))
1379 return;
1380 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1381 raises = TREE_CHAIN (raises))
1383 tree type = TREE_VALUE (raises);
1384 if (type && !COMPLETE_TYPE_P (type))
1386 if (decl)
1387 error
1388 ("call to function `%D' which throws incomplete type `%#T'",
1389 decl, type);
1390 else
1391 error ("call to function which throws incomplete type `%#T'",
1392 decl);