PR c++/11786
[official-gcc.git] / gcc / cp / typeck2.c
blob28d876d0433079efac6d41dbd1fc728637a686b2
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 (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 (tree basetype, tree type)
53 if (TREE_CODE (basetype) == FUNCTION_DECL)
54 basetype = DECL_CONTEXT (basetype);
55 error ("type `%T' is not a base type for type `%T'", basetype, type);
56 return error_mark_node;
59 tree
60 binfo_or_else (tree base, tree type)
62 tree binfo = lookup_base (type, base, ba_ignore, NULL);
64 if (binfo == error_mark_node)
65 return NULL_TREE;
66 else if (!binfo)
67 error_not_base_type (base, type);
68 return binfo;
71 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
72 value may not be changed thereafter. Thus, we emit hard errors for these,
73 rather than just pedwarns. If `SOFT' is 1, then we just pedwarn. (For
74 example, conversions to references.) */
76 void
77 readonly_error (tree arg, const char* string, int soft)
79 const char *fmt;
80 void (*fn) (const char *, ...);
82 if (soft)
83 fn = pedwarn;
84 else
85 fn = error;
87 if (TREE_CODE (arg) == COMPONENT_REF)
89 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
90 fmt = "%s of data-member `%D' in read-only structure";
91 else
92 fmt = "%s of read-only data-member `%D'";
93 (*fn) (fmt, string, TREE_OPERAND (arg, 1));
95 else if (TREE_CODE (arg) == VAR_DECL)
97 if (DECL_LANG_SPECIFIC (arg)
98 && DECL_IN_AGGR_P (arg)
99 && !TREE_STATIC (arg))
100 fmt = "%s of constant field `%D'";
101 else
102 fmt = "%s of read-only variable `%D'";
103 (*fn) (fmt, string, arg);
105 else if (TREE_CODE (arg) == PARM_DECL)
106 (*fn) ("%s of read-only parameter `%D'", string, arg);
107 else if (TREE_CODE (arg) == INDIRECT_REF
108 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
109 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
110 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
111 (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0));
112 else if (TREE_CODE (arg) == RESULT_DECL)
113 (*fn) ("%s of read-only named return value `%D'", string, arg);
114 else if (TREE_CODE (arg) == FUNCTION_DECL)
115 (*fn) ("%s of function `%D'", string, arg);
116 else
117 (*fn) ("%s of read-only location", string);
120 /* If TYPE has abstract virtual functions, issue an error about trying
121 to create an object of that type. DECL is the object declared, or
122 NULL_TREE if the declaration is unavailable. Returns 1 if an error
123 occurred; zero if all was well. */
126 abstract_virtuals_error (tree decl, tree type)
128 tree u;
129 tree tu;
131 if (!CLASS_TYPE_P (type) || !CLASSTYPE_PURE_VIRTUALS (type))
132 return 0;
134 if (!TYPE_SIZE (type))
135 /* TYPE is being defined, and during that time
136 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
137 return 0;
139 if (dependent_type_p (type))
140 /* For a dependent type, we do not yet know which functions are pure
141 virtuals. */
142 return 0;
144 u = CLASSTYPE_PURE_VIRTUALS (type);
145 if (decl)
147 if (TREE_CODE (decl) == RESULT_DECL)
148 return 0;
150 if (TREE_CODE (decl) == VAR_DECL)
151 error ("cannot declare variable `%D' to be of type `%T'",
152 decl, type);
153 else if (TREE_CODE (decl) == PARM_DECL)
154 error ("cannot declare parameter `%D' to be of type `%T'",
155 decl, type);
156 else if (TREE_CODE (decl) == FIELD_DECL)
157 error ("cannot declare field `%D' to be of type `%T'",
158 decl, type);
159 else if (TREE_CODE (decl) == FUNCTION_DECL
160 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
161 error ("invalid return type for member function `%#D'", decl);
162 else if (TREE_CODE (decl) == FUNCTION_DECL)
163 error ("invalid return type for function `%#D'", decl);
165 else
166 error ("cannot allocate an object of type `%T'", type);
168 /* Only go through this once. */
169 if (TREE_PURPOSE (u) == NULL_TREE)
171 TREE_PURPOSE (u) = error_mark_node;
173 error (" because the following virtual functions are abstract:");
174 for (tu = u; tu; tu = TREE_CHAIN (tu))
175 cp_error_at ("\t%#D", TREE_VALUE (tu));
177 else
178 error (" since type `%T' has abstract virtual functions", type);
180 return 1;
183 /* Print an error message for invalid use of an incomplete type.
184 VALUE is the expression that was used (or 0 if that isn't known)
185 and TYPE is the type that was invalid. DIAG_TYPE indicates the
186 type of diagnostic: 0 for an error, 1 for a warning, 2 for a
187 pedwarn. */
189 void
190 cxx_incomplete_type_diagnostic (tree value, tree type, int diag_type)
192 int decl = 0;
193 void (*p_msg) (const char *, ...);
194 void (*p_msg_at) (const char *, ...);
196 if (diag_type == 1)
198 p_msg = warning;
199 p_msg_at = cp_warning_at;
201 else if (diag_type == 2)
203 p_msg = pedwarn;
204 p_msg_at = cp_pedwarn_at;
206 else
208 p_msg = error;
209 p_msg_at = cp_error_at;
212 /* Avoid duplicate error message. */
213 if (TREE_CODE (type) == ERROR_MARK)
214 return;
216 if (value != 0 && (TREE_CODE (value) == VAR_DECL
217 || TREE_CODE (value) == PARM_DECL
218 || TREE_CODE (value) == FIELD_DECL))
220 (*p_msg_at) ("`%D' has incomplete type", value);
221 decl = 1;
223 retry:
224 /* We must print an error message. Be clever about what it says. */
226 switch (TREE_CODE (type))
228 case RECORD_TYPE:
229 case UNION_TYPE:
230 case ENUMERAL_TYPE:
231 if (!decl)
232 (*p_msg) ("invalid use of undefined type `%#T'", type);
233 if (!TYPE_TEMPLATE_INFO (type))
234 (*p_msg_at) ("forward declaration of `%#T'", type);
235 else
236 (*p_msg_at) ("declaration of `%#T'", type);
237 break;
239 case VOID_TYPE:
240 (*p_msg) ("invalid use of `%T'", type);
241 break;
243 case ARRAY_TYPE:
244 if (TYPE_DOMAIN (type))
246 type = TREE_TYPE (type);
247 goto retry;
249 (*p_msg) ("invalid use of array with unspecified bounds");
250 break;
252 case OFFSET_TYPE:
253 bad_member:
254 (*p_msg) ("invalid use of member (did you forget the `&' ?)");
255 break;
257 case TEMPLATE_TYPE_PARM:
258 (*p_msg) ("invalid use of template type parameter");
259 break;
261 case UNKNOWN_TYPE:
262 if (value && TREE_CODE (value) == COMPONENT_REF)
263 goto bad_member;
264 else if (value && TREE_CODE (value) == ADDR_EXPR)
265 (*p_msg) ("address of overloaded function with no contextual type information");
266 else if (value && TREE_CODE (value) == OVERLOAD)
267 (*p_msg) ("overloaded function with no contextual type information");
268 else
269 (*p_msg) ("insufficient contextual information to determine type");
270 break;
272 default:
273 abort ();
277 /* Backward-compatibility interface to incomplete_type_diagnostic;
278 required by ../tree.c. */
279 #undef cxx_incomplete_type_error
280 void
281 cxx_incomplete_type_error (tree value, tree type)
283 cxx_incomplete_type_diagnostic (value, type, 0);
287 /* Perform appropriate conversions on the initial value of a variable,
288 store it in the declaration DECL,
289 and print any error messages that are appropriate.
290 If the init is invalid, store an ERROR_MARK.
292 C++: Note that INIT might be a TREE_LIST, which would mean that it is
293 a base class initializer for some aggregate type, hopefully compatible
294 with DECL. If INIT is a single element, and DECL is an aggregate
295 type, we silently convert INIT into a TREE_LIST, allowing a constructor
296 to be called.
298 If INIT is a TREE_LIST and there is no constructor, turn INIT
299 into a CONSTRUCTOR and use standard initialization techniques.
300 Perhaps a warning should be generated?
302 Returns value of initializer if initialization could not be
303 performed for static variable. In that case, caller must do
304 the storing. */
306 tree
307 store_init_value (tree decl, tree init)
309 register tree value, type;
311 /* If variable's type was invalidly declared, just ignore it. */
313 type = TREE_TYPE (decl);
314 if (TREE_CODE (type) == ERROR_MARK)
315 return NULL_TREE;
317 if (IS_AGGR_TYPE (type))
319 if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
320 && TREE_CODE (init) != CONSTRUCTOR)
321 abort ();
323 if (TREE_CODE (init) == TREE_LIST)
325 error ("constructor syntax used, but no constructor declared for type `%T'", type);
326 init = build_constructor (NULL_TREE, nreverse (init));
329 else if (TREE_CODE (init) == TREE_LIST
330 && TREE_TYPE (init) != unknown_type_node)
332 if (TREE_CODE (decl) == RESULT_DECL)
333 init = build_x_compound_expr_from_list (init,
334 "return value initializer");
335 else if (TREE_CODE (init) == TREE_LIST
336 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
338 error ("cannot initialize arrays using this syntax");
339 return NULL_TREE;
341 else
342 /* We get here with code like `int a (2);' */
343 init = build_x_compound_expr_from_list (init, "initializer");
346 /* End of special C++ code. */
348 /* Digest the specified initializer into an expression. */
349 value = digest_init (type, init, (tree *) 0);
351 /* Store the expression if valid; else report error. */
353 if (TREE_CODE (value) == ERROR_MARK)
355 /* Other code expects that initializers for objects of types that need
356 constructing never make it into DECL_INITIAL, and passes 'init' to
357 build_aggr_init without checking DECL_INITIAL. So just return. */
358 else if (TYPE_NEEDS_CONSTRUCTING (type))
359 return value;
360 else if (TREE_STATIC (decl)
361 && (! TREE_CONSTANT (value)
362 || ! initializer_constant_valid_p (value, TREE_TYPE (value))))
363 return value;
365 /* Store the VALUE in DECL_INITIAL. If we're building a
366 statement-tree we will actually expand the initialization later
367 when we output this function. */
368 DECL_INITIAL (decl) = value;
369 return NULL_TREE;
373 /* Digest the parser output INIT as an initializer for type TYPE.
374 Return a C expression of type TYPE to represent the initial value.
376 If TAIL is nonzero, it points to a variable holding a list of elements
377 of which INIT is the first. We update the list stored there by
378 removing from the head all the elements that we use.
379 Normally this is only one; we use more than one element only if
380 TYPE is an aggregate and INIT is not a constructor. */
382 tree
383 digest_init (tree type, tree init, tree* tail)
385 enum tree_code code = TREE_CODE (type);
386 tree element = NULL_TREE;
387 tree old_tail_contents = NULL_TREE;
388 /* Nonzero if INIT is a braced grouping. */
389 int raw_constructor;
391 /* By default, assume we use one element from a list.
392 We correct this later in the sole case where it is not true. */
394 if (tail)
396 old_tail_contents = *tail;
397 *tail = TREE_CHAIN (*tail);
400 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
401 && TREE_VALUE (init) == error_mark_node))
402 return error_mark_node;
404 if (TREE_CODE (init) == ERROR_MARK)
405 /* __PRETTY_FUNCTION__'s initializer is a bogus expression inside
406 a template function. This gets substituted during instantiation. */
407 return init;
409 /* We must strip the outermost array type when completing the type,
410 because the its bounds might be incomplete at the moment. */
411 if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
412 ? TREE_TYPE (type) : type, NULL_TREE))
413 return error_mark_node;
415 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
416 if (TREE_CODE (init) == NON_LVALUE_EXPR)
417 init = TREE_OPERAND (init, 0);
419 raw_constructor = (TREE_CODE (init) == CONSTRUCTOR
420 && TREE_HAS_CONSTRUCTOR (init));
422 if (raw_constructor
423 && CONSTRUCTOR_ELTS (init) != 0
424 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
426 element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
427 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
428 if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
429 element = TREE_OPERAND (element, 0);
430 if (element == error_mark_node)
431 return element;
434 /* Initialization of an array of chars from a string constant
435 optionally enclosed in braces. */
437 if (code == ARRAY_TYPE)
439 tree typ1;
441 if (TREE_CODE (init) == TREE_LIST)
443 error ("initializing array with parameter list");
444 return error_mark_node;
447 typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
448 if (char_type_p (typ1)
449 && ((init && TREE_CODE (init) == STRING_CST)
450 || (element && TREE_CODE (element) == STRING_CST)))
452 tree string = element ? element : init;
454 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
455 != char_type_node)
456 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
458 error ("char-array initialized from wide string");
459 return error_mark_node;
461 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
462 == char_type_node)
463 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
465 error ("int-array initialized from non-wide string");
466 return error_mark_node;
469 TREE_TYPE (string) = type;
470 if (TYPE_DOMAIN (type) != 0
471 && TREE_CONSTANT (TYPE_SIZE (type)))
473 register int size
474 = TREE_INT_CST_LOW (TYPE_SIZE (type));
475 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
476 /* In C it is ok to subtract 1 from the length of the string
477 because it's ok to ignore the terminating null char that is
478 counted in the length of the constant, but in C++ this would
479 be invalid. */
480 if (size < TREE_STRING_LENGTH (string))
481 pedwarn ("initializer-string for array of chars is too long");
483 return string;
487 /* Handle scalar types, including conversions,
488 and signature pointers and references. */
490 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
491 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
492 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
493 || TYPE_PTR_TO_MEMBER_P (type))
495 if (raw_constructor)
497 if (element == 0)
499 error ("initializer for scalar variable requires one element");
500 return error_mark_node;
502 init = element;
504 while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
506 pedwarn ("braces around scalar initializer for `%T'", type);
507 init = CONSTRUCTOR_ELTS (init);
508 if (TREE_CHAIN (init))
509 pedwarn ("ignoring extra initializers for `%T'", type);
510 init = TREE_VALUE (init);
513 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
514 "initialization", NULL_TREE, 0);
517 /* Come here only for records and arrays (and unions with constructors). */
519 if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
521 error ("variable-sized object of type `%T' may not be initialized",
522 type);
523 return error_mark_node;
526 if (code == ARRAY_TYPE || code == VECTOR_TYPE || IS_AGGR_TYPE_CODE (code))
528 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type)
529 && TREE_HAS_CONSTRUCTOR (init))
531 error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
532 type, init);
533 return error_mark_node;
535 else if (raw_constructor)
536 return process_init_constructor (type, init, (tree *)0);
537 else if (can_convert_arg (type, TREE_TYPE (init), init)
538 || TYPE_NON_AGGREGATE_CLASS (type))
539 /* These are never initialized from multiple constructor elements. */;
540 else if (tail != 0)
542 *tail = old_tail_contents;
543 return process_init_constructor (type, 0, tail);
546 if (code != ARRAY_TYPE)
548 int flags = LOOKUP_NORMAL;
549 /* Initialization from { } is copy-initialization. */
550 if (tail)
551 flags |= LOOKUP_ONLYCONVERTING;
553 return convert_for_initialization (NULL_TREE, type, init, flags,
554 "initialization", NULL_TREE, 0);
558 error ("invalid initializer");
559 return error_mark_node;
562 /* Process a constructor for a variable of type TYPE.
563 The constructor elements may be specified either with INIT or with ELTS,
564 only one of which should be non-null.
566 If INIT is specified, it is a CONSTRUCTOR node which is specifically
567 and solely for initializing this datum.
569 If ELTS is specified, it is the address of a variable containing
570 a list of expressions. We take as many elements as we need
571 from the head of the list and update the list.
573 In the resulting constructor, TREE_CONSTANT is set if all elts are
574 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
575 constants that the assembler and linker can compute them. */
577 static tree
578 process_init_constructor (tree type, tree init, tree* elts)
580 register tree tail;
581 /* List of the elements of the result constructor,
582 in reverse order. */
583 register tree members = NULL;
584 register tree next1;
585 tree result;
586 int allconstant = 1;
587 int allsimple = 1;
588 int erroneous = 0;
590 /* Make TAIL be the list of elements to use for the initialization,
591 no matter how the data was given to us. */
593 if (elts)
595 if (warn_missing_braces)
596 warning ("aggregate has a partly bracketed initializer");
597 tail = *elts;
599 else
600 tail = CONSTRUCTOR_ELTS (init);
602 /* Gobble as many elements as needed, and make a constructor or initial value
603 for each element of this aggregate. Chain them together in result.
604 If there are too few, use 0 for each scalar ultimate component. */
606 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
608 register long len;
609 register int i;
611 if (TREE_CODE (type) == ARRAY_TYPE)
613 tree domain = TYPE_DOMAIN (type);
614 if (domain)
615 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
616 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
617 + 1);
618 else
619 len = -1; /* Take as many as there are */
621 else
623 /* Vectors are like simple fixed-size arrays. */
624 len = TYPE_VECTOR_SUBPARTS (type);
627 for (i = 0; len < 0 || i < len; i++)
629 if (tail)
631 if (TREE_PURPOSE (tail)
632 && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
633 || compare_tree_int (TREE_PURPOSE (tail), i) != 0))
634 sorry ("non-trivial labeled initializers");
636 if (TREE_VALUE (tail) != 0)
638 tree tail1 = tail;
639 next1 = digest_init (TREE_TYPE (type),
640 TREE_VALUE (tail), &tail1);
641 if (next1 == error_mark_node)
642 return next1;
643 my_friendly_assert
644 (same_type_ignoring_top_level_qualifiers_p
645 (TREE_TYPE (type), TREE_TYPE (next1)),
646 981123);
647 my_friendly_assert (tail1 == 0
648 || TREE_CODE (tail1) == TREE_LIST, 319);
649 if (tail == tail1 && len < 0)
651 error ("non-empty initializer for array of empty elements");
652 /* Just ignore what we were supposed to use. */
653 tail1 = NULL_TREE;
655 tail = tail1;
657 else
659 next1 = error_mark_node;
660 tail = TREE_CHAIN (tail);
663 else if (len < 0)
664 /* We're done. */
665 break;
666 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
668 /* If this type needs constructors run for
669 default-initialization, we can't rely on the backend to do it
670 for us, so build up TARGET_EXPRs. If the type in question is
671 a class, just build one up; if it's an array, recurse. */
673 if (IS_AGGR_TYPE (TREE_TYPE (type)))
674 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
675 else
676 next1 = build_constructor (NULL_TREE, NULL_TREE);
677 next1 = digest_init (TREE_TYPE (type), next1, 0);
679 else if (! zero_init_p (TREE_TYPE (type)))
680 next1 = build_zero_init (TREE_TYPE (type),
681 /*nelts=*/NULL_TREE,
682 /*static_storage_p=*/false);
683 else
684 /* The default zero-initialization is fine for us; don't
685 add anything to the CONSTRUCTOR. */
686 break;
688 if (next1 == error_mark_node)
689 erroneous = 1;
690 else if (!TREE_CONSTANT (next1))
691 allconstant = 0;
692 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
693 allsimple = 0;
694 members = tree_cons (size_int (i), next1, members);
697 else if (TREE_CODE (type) == RECORD_TYPE)
699 register tree field;
701 if (tail)
703 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
705 sorry ("initializer list for object of class with virtual base classes");
706 return error_mark_node;
709 if (TYPE_BINFO_BASETYPES (type))
711 sorry ("initializer list for object of class with base classes");
712 return error_mark_node;
715 if (TYPE_POLYMORPHIC_P (type))
717 sorry ("initializer list for object using virtual functions");
718 return error_mark_node;
722 for (field = TYPE_FIELDS (type); field;
723 field = TREE_CHAIN (field))
725 if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
727 members = tree_cons (field, integer_zero_node, members);
728 continue;
731 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
732 continue;
734 if (tail)
736 if (TREE_PURPOSE (tail)
737 && TREE_PURPOSE (tail) != field
738 && TREE_PURPOSE (tail) != DECL_NAME (field))
739 sorry ("non-trivial labeled initializers");
741 if (TREE_VALUE (tail) != 0)
743 tree tail1 = tail;
745 next1 = digest_init (TREE_TYPE (field),
746 TREE_VALUE (tail), &tail1);
747 my_friendly_assert (tail1 == 0
748 || TREE_CODE (tail1) == TREE_LIST, 320);
749 tail = tail1;
751 else
753 next1 = error_mark_node;
754 tail = TREE_CHAIN (tail);
757 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
759 /* If this type needs constructors run for
760 default-initialization, we can't rely on the backend to do it
761 for us, so build up TARGET_EXPRs. If the type in question is
762 a class, just build one up; if it's an array, recurse. */
764 if (IS_AGGR_TYPE (TREE_TYPE (field)))
765 next1 = build_functional_cast (TREE_TYPE (field),
766 NULL_TREE);
767 else
769 next1 = build_constructor (NULL_TREE, NULL_TREE);
770 if (init)
771 TREE_HAS_CONSTRUCTOR (next1)
772 = TREE_HAS_CONSTRUCTOR (init);
774 next1 = digest_init (TREE_TYPE (field), next1, 0);
776 /* Warn when some struct elements are implicitly initialized. */
777 if (extra_warnings
778 && (!init || TREE_HAS_CONSTRUCTOR (init)))
779 warning ("missing initializer for member `%D'", field);
781 else
783 if (TREE_READONLY (field))
784 error ("uninitialized const member `%D'", field);
785 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
786 error ("member `%D' with uninitialized const fields",
787 field);
788 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
789 error ("member `%D' is uninitialized reference", field);
791 /* Warn when some struct elements are implicitly initialized
792 to zero. */
793 if (extra_warnings
794 && (!init || TREE_HAS_CONSTRUCTOR (init)))
795 warning ("missing initializer for member `%D'", field);
797 if (! zero_init_p (TREE_TYPE (field)))
798 next1 = build_zero_init (TREE_TYPE (field),
799 /*nelts=*/NULL_TREE,
800 /*static_storage_p=*/false);
801 else
802 /* The default zero-initialization is fine for us; don't
803 add anything to the CONSTRUCTOR. */
804 continue;
807 if (next1 == error_mark_node)
808 erroneous = 1;
809 else if (!TREE_CONSTANT (next1))
810 allconstant = 0;
811 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
812 allsimple = 0;
813 members = tree_cons (field, next1, members);
816 else if (TREE_CODE (type) == UNION_TYPE
817 /* If the initializer was empty, use default zero initialization. */
818 && tail)
820 register tree field = TYPE_FIELDS (type);
822 /* Find the first named field. ANSI decided in September 1990
823 that only named fields count here. */
824 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
825 field = TREE_CHAIN (field);
827 /* If this element specifies a field, initialize via that field. */
828 if (TREE_PURPOSE (tail) != NULL_TREE)
830 int win = 0;
832 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
833 /* Handle the case of a call by build_c_cast. */
834 field = TREE_PURPOSE (tail), win = 1;
835 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
836 error ("index value instead of field name in union initializer");
837 else
839 tree temp;
840 for (temp = TYPE_FIELDS (type);
841 temp;
842 temp = TREE_CHAIN (temp))
843 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
844 break;
845 if (temp)
846 field = temp, win = 1;
847 else
848 error ("no field `%D' in union being initialized",
849 TREE_PURPOSE (tail));
851 if (!win)
852 TREE_VALUE (tail) = error_mark_node;
854 else if (field == 0)
856 error ("union `%T' with no named members cannot be initialized",
857 type);
858 TREE_VALUE (tail) = error_mark_node;
861 if (TREE_VALUE (tail) != 0)
863 tree tail1 = tail;
865 next1 = digest_init (TREE_TYPE (field),
866 TREE_VALUE (tail), &tail1);
867 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
868 abort ();
869 tail = tail1;
871 else
873 next1 = error_mark_node;
874 tail = TREE_CHAIN (tail);
877 if (next1 == error_mark_node)
878 erroneous = 1;
879 else if (!TREE_CONSTANT (next1))
880 allconstant = 0;
881 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
882 allsimple = 0;
883 members = tree_cons (field, next1, members);
886 /* If arguments were specified as a list, just remove the ones we used. */
887 if (elts)
888 *elts = tail;
889 /* If arguments were specified as a constructor,
890 complain unless we used all the elements of the constructor. */
891 else if (tail)
892 pedwarn ("excess elements in aggregate initializer");
894 if (erroneous)
895 return error_mark_node;
897 result = build_constructor (type, nreverse (members));
898 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
899 complete_array_type (type, result, /*do_default=*/0);
900 if (init)
901 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
902 if (allconstant) TREE_CONSTANT (result) = 1;
903 if (allconstant && allsimple) TREE_STATIC (result) = 1;
904 return result;
907 /* Given a structure or union value DATUM, construct and return
908 the structure or union component which results from narrowing
909 that value to the base specified in BASETYPE. For example, given the
910 hierarchy
912 class L { int ii; };
913 class A : L { ... };
914 class B : L { ... };
915 class C : A, B { ... };
917 and the declaration
919 C x;
921 then the expression
923 x.A::ii refers to the ii member of the L part of
924 the A part of the C object named by X. In this case,
925 DATUM would be x, and BASETYPE would be A.
927 I used to think that this was nonconformant, that the standard specified
928 that first we look up ii in A, then convert x to an L& and pull out the
929 ii part. But in fact, it does say that we convert x to an A&; A here
930 is known as the "naming class". (jason 2000-12-19)
932 BINFO_P points to a variable initialized either to NULL_TREE or to the
933 binfo for the specific base subobject we want to convert to. */
935 tree
936 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
938 tree binfo;
940 if (datum == error_mark_node)
941 return error_mark_node;
942 if (*binfo_p)
943 binfo = *binfo_p;
944 else
945 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
947 if (!binfo || binfo == error_mark_node)
949 *binfo_p = NULL_TREE;
950 if (!binfo)
951 error_not_base_type (basetype, TREE_TYPE (datum));
952 return error_mark_node;
955 *binfo_p = binfo;
956 return build_base_path (PLUS_EXPR, datum, binfo, 1);
959 /* Build a reference to an object specified by the C++ `->' operator.
960 Usually this just involves dereferencing the object, but if the
961 `->' operator is overloaded, then such overloads must be
962 performed until an object which does not have the `->' operator
963 overloaded is found. An error is reported when circular pointer
964 delegation is detected. */
966 tree
967 build_x_arrow (tree expr)
969 tree orig_expr = expr;
970 tree types_memoized = NULL_TREE;
971 tree type = TREE_TYPE (expr);
972 tree last_rval = NULL_TREE;
974 if (type == error_mark_node)
975 return error_mark_node;
977 if (processing_template_decl)
979 if (type_dependent_expression_p (expr))
980 return build_min_nt (ARROW_EXPR, expr);
981 expr = build_non_dependent_expr (expr);
984 if (TREE_CODE (type) == REFERENCE_TYPE)
986 expr = convert_from_reference (expr);
987 type = TREE_TYPE (expr);
990 if (IS_AGGR_TYPE (type))
992 while ((expr = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, expr,
993 NULL_TREE, NULL_TREE)))
995 if (expr == error_mark_node)
996 return error_mark_node;
998 if (value_member (TREE_TYPE (expr), types_memoized))
1000 error ("circular pointer delegation detected");
1001 return error_mark_node;
1003 else
1005 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (expr),
1006 types_memoized);
1008 last_rval = expr;
1011 if (last_rval == NULL_TREE)
1013 error ("base operand of `->' has non-pointer type `%T'", type);
1014 return error_mark_node;
1017 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1018 last_rval = convert_from_reference (last_rval);
1020 else
1021 last_rval = decay_conversion (expr);
1023 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1025 if (processing_template_decl)
1027 expr = build_min_non_dep (ARROW_EXPR, last_rval, orig_expr);
1028 /* It will be dereferenced. */
1029 TREE_TYPE (expr) = TREE_TYPE (TREE_TYPE (last_rval));
1030 return expr;
1033 return build_indirect_ref (last_rval, NULL);
1036 if (types_memoized)
1037 error ("result of `operator->()' yields non-pointer result");
1038 else
1039 error ("base operand of `->' is not a pointer");
1040 return error_mark_node;
1043 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1044 already been checked out to be of aggregate type. */
1046 tree
1047 build_m_component_ref (tree datum, tree component)
1049 tree ptrmem_type;
1050 tree objtype;
1051 tree type;
1052 tree binfo;
1054 datum = decay_conversion (datum);
1056 if (datum == error_mark_node || component == error_mark_node)
1057 return error_mark_node;
1059 ptrmem_type = TREE_TYPE (component);
1060 if (!TYPE_PTR_TO_MEMBER_P (ptrmem_type))
1062 error ("`%E' cannot be used as a member pointer, since it is of type `%T'",
1063 component, ptrmem_type);
1064 return error_mark_node;
1067 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1068 if (! IS_AGGR_TYPE (objtype))
1070 error ("cannot apply member pointer `%E' to `%E', which is of non-aggregate type `%T'",
1071 component, datum, objtype);
1072 return error_mark_node;
1075 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1076 binfo = lookup_base (objtype, TYPE_PTRMEM_CLASS_TYPE (ptrmem_type),
1077 ba_check, NULL);
1078 if (!binfo)
1080 error ("member type `%T::' incompatible with object type `%T'",
1081 type, objtype);
1082 return error_mark_node;
1084 else if (binfo == error_mark_node)
1085 return error_mark_node;
1087 if (TYPE_PTRMEM_P (ptrmem_type))
1089 /* Compute the type of the field, as described in [expr.ref].
1090 There's no such thing as a mutable pointer-to-member, so
1091 things are not as complex as they are for references to
1092 non-static data members. */
1093 type = cp_build_qualified_type (type,
1094 (cp_type_quals (type)
1095 | cp_type_quals (TREE_TYPE (datum))));
1096 /* Build an expression for "object + offset" where offset is the
1097 value stored in the pointer-to-data-member. */
1098 datum = build (PLUS_EXPR, build_pointer_type (type),
1099 build_base_path (PLUS_EXPR, build_address (datum),
1100 binfo, 1),
1101 build_nop (ptrdiff_type_node, component));
1102 return build_indirect_ref (datum, 0);
1104 else
1105 return build (OFFSET_REF, type, datum, component);
1108 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1110 tree
1111 build_functional_cast (tree exp, tree parms)
1113 /* This is either a call to a constructor,
1114 or a C cast in C++'s `functional' notation. */
1115 tree type;
1117 if (exp == error_mark_node || parms == error_mark_node)
1118 return error_mark_node;
1120 if (TREE_CODE (exp) == TYPE_DECL)
1121 type = TREE_TYPE (exp);
1122 else
1123 type = exp;
1125 if (processing_template_decl)
1127 tree t = build_min (CAST_EXPR, type, parms);
1128 /* We don't know if it will or will not have side effects. */
1129 TREE_SIDE_EFFECTS (t) = 1;
1130 return t;
1133 if (! IS_AGGR_TYPE (type))
1135 /* this must build a C cast */
1136 if (parms == NULL_TREE)
1137 parms = integer_zero_node;
1138 else
1139 parms = build_x_compound_expr_from_list (parms, "functional cast");
1141 return build_c_cast (type, parms);
1144 /* Prepare to evaluate as a call to a constructor. If this expression
1145 is actually used, for example,
1147 return X (arg1, arg2, ...);
1149 then the slot being initialized will be filled in. */
1151 if (!complete_type_or_else (type, NULL_TREE))
1152 return error_mark_node;
1153 if (abstract_virtuals_error (NULL_TREE, type))
1154 return error_mark_node;
1156 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1157 return build_c_cast (type, TREE_VALUE (parms));
1159 /* We need to zero-initialize POD types. Let's do that for everything
1160 that doesn't need a constructor. */
1161 if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1162 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1164 exp = build_constructor (type, NULL_TREE);
1165 return get_target_expr (exp);
1168 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, parms,
1169 TYPE_BINFO (type), LOOKUP_NORMAL);
1171 if (exp == error_mark_node)
1172 return error_mark_node;
1174 return build_cplus_new (type, exp);
1178 /* Add new exception specifier SPEC, to the LIST we currently have.
1179 If it's already in LIST then do nothing.
1180 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1181 know what we're doing. */
1183 tree
1184 add_exception_specifier (tree list, tree spec, int complain)
1186 bool ok;
1187 tree core = spec;
1188 bool is_ptr;
1189 int diag_type = -1; /* none */
1191 if (spec == error_mark_node)
1192 return list;
1194 my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317);
1196 /* [except.spec] 1, type in an exception specifier shall not be
1197 incomplete, or pointer or ref to incomplete other than pointer
1198 to cv void. */
1199 is_ptr = TREE_CODE (core) == POINTER_TYPE;
1200 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1201 core = TREE_TYPE (core);
1202 if (complain < 0)
1203 ok = true;
1204 else if (VOID_TYPE_P (core))
1205 ok = is_ptr;
1206 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1207 ok = true;
1208 else if (processing_template_decl)
1209 ok = true;
1210 else
1212 ok = true;
1213 /* 15.4/1 says that types in an exception specifier must be complete,
1214 but it seems more reasonable to only require this on definitions
1215 and calls. So just give a pedwarn at this point; we will give an
1216 error later if we hit one of those two cases. */
1217 if (!COMPLETE_TYPE_P (complete_type (core)))
1218 diag_type = 2; /* pedwarn */
1221 if (ok)
1223 tree probe;
1225 for (probe = list; probe; probe = TREE_CHAIN (probe))
1226 if (same_type_p (TREE_VALUE (probe), spec))
1227 break;
1228 if (!probe)
1229 list = tree_cons (NULL_TREE, spec, list);
1231 else
1232 diag_type = 0; /* error */
1234 if (diag_type >= 0 && complain)
1235 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1237 return list;
1240 /* Combine the two exceptions specifier lists LIST and ADD, and return
1241 their union. */
1243 tree
1244 merge_exception_specifiers (tree list, tree add)
1246 if (!list || !add)
1247 return NULL_TREE;
1248 else if (!TREE_VALUE (list))
1249 return add;
1250 else if (!TREE_VALUE (add))
1251 return list;
1252 else
1254 tree orig_list = list;
1256 for (; add; add = TREE_CHAIN (add))
1258 tree spec = TREE_VALUE (add);
1259 tree probe;
1261 for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1262 if (same_type_p (TREE_VALUE (probe), spec))
1263 break;
1264 if (!probe)
1266 spec = build_tree_list (NULL_TREE, spec);
1267 TREE_CHAIN (spec) = list;
1268 list = spec;
1272 return list;
1275 /* Subroutine of build_call. Ensure that each of the types in the
1276 exception specification is complete. Technically, 15.4/1 says that
1277 they need to be complete when we see a declaration of the function,
1278 but we should be able to get away with only requiring this when the
1279 function is defined or called. See also add_exception_specifier. */
1281 void
1282 require_complete_eh_spec_types (tree fntype, tree decl)
1284 tree raises;
1285 /* Don't complain about calls to op new. */
1286 if (decl && DECL_ARTIFICIAL (decl))
1287 return;
1288 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1289 raises = TREE_CHAIN (raises))
1291 tree type = TREE_VALUE (raises);
1292 if (type && !COMPLETE_TYPE_P (type))
1294 if (decl)
1295 error
1296 ("call to function `%D' which throws incomplete type `%#T'",
1297 decl, type);
1298 else
1299 error ("call to function which throws incomplete type `%#T'",
1300 decl);