Daily bump.
[official-gcc.git] / gcc / cp / typeck2.c
blobd3febeb7eaa52497f4820be5932afb185955e85e
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 GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* This file is part of the C++ front end.
26 It contains routines to build C++ expressions given their operands,
27 including computing the types of the result, C and C++ specific error
28 checks, and some optimization.
30 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
31 and to process initializations in declarations (since they work
32 like a strange sort of assignment). */
34 #include "config.h"
35 #include "system.h"
36 #include "tree.h"
37 #include "cp-tree.h"
38 #include "flags.h"
39 #include "toplev.h"
40 #include "output.h"
41 #include "diagnostic.h"
43 static tree process_init_constructor PARAMS ((tree, tree, tree *));
45 /* Print an error message stemming from an attempt to use
46 BASETYPE as a base class for TYPE. */
48 tree
49 error_not_base_type (basetype, type)
50 tree basetype, type;
52 if (TREE_CODE (basetype) == FUNCTION_DECL)
53 basetype = DECL_CONTEXT (basetype);
54 error ("type `%T' is not a base type for type `%T'", basetype, type);
55 return error_mark_node;
58 tree
59 binfo_or_else (base, type)
60 tree base, 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 (arg, string, soft)
78 tree arg;
79 const char *string;
80 int soft;
82 const char *fmt;
83 void (*fn) PARAMS ((const char *, ...));
85 if (soft)
86 fn = pedwarn;
87 else
88 fn = error;
90 if (TREE_CODE (arg) == COMPONENT_REF)
92 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
93 fmt = "%s of data-member `%D' in read-only structure";
94 else
95 fmt = "%s of read-only data-member `%D'";
96 (*fn) (fmt, string, TREE_OPERAND (arg, 1));
98 else if (TREE_CODE (arg) == VAR_DECL)
100 if (DECL_LANG_SPECIFIC (arg)
101 && DECL_IN_AGGR_P (arg)
102 && !TREE_STATIC (arg))
103 fmt = "%s of constant field `%D'";
104 else
105 fmt = "%s of read-only variable `%D'";
106 (*fn) (fmt, string, arg);
108 else if (TREE_CODE (arg) == PARM_DECL)
109 (*fn) ("%s of read-only parameter `%D'", string, arg);
110 else if (TREE_CODE (arg) == INDIRECT_REF
111 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
112 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
113 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
114 (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0));
115 else if (TREE_CODE (arg) == RESULT_DECL)
116 (*fn) ("%s of read-only named return value `%D'", string, arg);
117 else if (TREE_CODE (arg) == FUNCTION_DECL)
118 (*fn) ("%s of function `%D'", string, arg);
119 else
120 (*fn) ("%s of read-only location", string);
123 /* If TYPE has abstract virtual functions, issue an error about trying
124 to create an object of that type. DECL is the object declared, or
125 NULL_TREE if the declaration is unavailable. Returns 1 if an error
126 occurred; zero if all was well. */
129 abstract_virtuals_error (decl, type)
130 tree decl;
131 tree type;
133 tree u;
134 tree tu;
136 if (!CLASS_TYPE_P (type) || !CLASSTYPE_PURE_VIRTUALS (type))
137 return 0;
139 if (!TYPE_SIZE (type))
140 /* TYPE is being defined, and during that time
141 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
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. */
187 void
188 incomplete_type_error (value, type)
189 tree value;
190 tree type;
192 int decl = 0;
194 /* Avoid duplicate error message. */
195 if (TREE_CODE (type) == ERROR_MARK)
196 return;
198 if (value != 0 && (TREE_CODE (value) == VAR_DECL
199 || TREE_CODE (value) == PARM_DECL
200 || TREE_CODE (value) == FIELD_DECL))
202 cp_error_at ("`%D' has incomplete type", value);
203 decl = 1;
205 retry:
206 /* We must print an error message. Be clever about what it says. */
208 switch (TREE_CODE (type))
210 case RECORD_TYPE:
211 case UNION_TYPE:
212 case ENUMERAL_TYPE:
213 if (!decl)
214 error ("invalid use of undefined type `%#T'", type);
215 if (!TYPE_TEMPLATE_INFO (type))
216 cp_error_at ("forward declaration of `%#T'", type);
217 else
218 cp_error_at ("declaration of `%#T'", type);
219 break;
221 case VOID_TYPE:
222 error ("invalid use of `%T'", type);
223 break;
225 case ARRAY_TYPE:
226 if (TYPE_DOMAIN (type))
228 type = TREE_TYPE (type);
229 goto retry;
231 error ("invalid use of array with unspecified bounds");
232 break;
234 case OFFSET_TYPE:
235 bad_member:
236 error ("invalid use of member (did you forget the `&' ?)");
237 break;
239 case TEMPLATE_TYPE_PARM:
240 error ("invalid use of template type parameter");
241 break;
243 case UNKNOWN_TYPE:
244 if (value && TREE_CODE (value) == COMPONENT_REF)
245 goto bad_member;
246 else if (value && TREE_CODE (value) == ADDR_EXPR)
247 error ("address of overloaded function with no contextual type information");
248 else if (value && TREE_CODE (value) == OVERLOAD)
249 error ("overloaded function with no contextual type information");
250 else
251 error ("insufficient contextual information to determine type");
252 break;
254 default:
255 abort ();
260 /* Perform appropriate conversions on the initial value of a variable,
261 store it in the declaration DECL,
262 and print any error messages that are appropriate.
263 If the init is invalid, store an ERROR_MARK.
265 C++: Note that INIT might be a TREE_LIST, which would mean that it is
266 a base class initializer for some aggregate type, hopefully compatible
267 with DECL. If INIT is a single element, and DECL is an aggregate
268 type, we silently convert INIT into a TREE_LIST, allowing a constructor
269 to be called.
271 If INIT is a TREE_LIST and there is no constructor, turn INIT
272 into a CONSTRUCTOR and use standard initialization techniques.
273 Perhaps a warning should be generated?
275 Returns value of initializer if initialization could not be
276 performed for static variable. In that case, caller must do
277 the storing. */
279 tree
280 store_init_value (decl, init)
281 tree decl, init;
283 register tree value, type;
285 /* If variable's type was invalidly declared, just ignore it. */
287 type = TREE_TYPE (decl);
288 if (TREE_CODE (type) == ERROR_MARK)
289 return NULL_TREE;
291 #if 0
292 /* This breaks arrays, and should not have any effect for other decls. */
293 /* Take care of C++ business up here. */
294 type = TYPE_MAIN_VARIANT (type);
295 #endif
297 if (IS_AGGR_TYPE (type))
299 if (! TYPE_HAS_TRIVIAL_INIT_REF (type)
300 && TREE_CODE (init) != CONSTRUCTOR)
301 abort ();
303 if (TREE_CODE (init) == TREE_LIST)
305 error ("constructor syntax used, but no constructor declared for type `%T'", type);
306 init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
308 #if 0
309 if (TREE_CODE (init) == CONSTRUCTOR)
311 tree field;
313 /* Check that we're really an aggregate as ARM 8.4.1 defines it. */
314 if (CLASSTYPE_N_BASECLASSES (type))
315 cp_error_at ("initializer list construction invalid for derived class object `%D'", decl);
316 if (CLASSTYPE_VTBL_PTR (type))
317 cp_error_at ("initializer list construction invalid for polymorphic class object `%D'", decl);
318 if (TYPE_NEEDS_CONSTRUCTING (type))
320 cp_error_at ("initializer list construction invalid for `%D'", decl);
321 error ("due to the presence of a constructor");
323 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
324 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
326 cp_error_at ("initializer list construction invalid for `%D'", decl);
327 cp_error_at ("due to non-public access of member `%D'", field);
329 for (field = TYPE_METHODS (type); field; field = TREE_CHAIN (field))
330 if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
332 cp_error_at ("initializer list construction invalid for `%D'", decl);
333 cp_error_at ("due to non-public access of member `%D'", field);
336 #endif
338 else if (TREE_CODE (init) == TREE_LIST
339 && TREE_TYPE (init) != unknown_type_node)
341 if (TREE_CODE (decl) == RESULT_DECL)
343 if (TREE_CHAIN (init))
345 warning ("comma expression used to initialize return value");
346 init = build_compound_expr (init);
348 else
349 init = TREE_VALUE (init);
351 else if (TREE_CODE (init) == TREE_LIST
352 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
354 error ("cannot initialize arrays using this syntax");
355 return NULL_TREE;
357 else
359 /* We get here with code like `int a (2);' */
361 if (TREE_CHAIN (init) != NULL_TREE)
363 pedwarn ("initializer list being treated as compound expression");
364 init = build_compound_expr (init);
366 else
367 init = TREE_VALUE (init);
371 /* End of special C++ code. */
373 /* We might have already run this bracketed initializer through
374 digest_init. Don't do so again. */
375 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init)
376 && TREE_TYPE (init)
377 && TYPE_MAIN_VARIANT (TREE_TYPE (init)) == TYPE_MAIN_VARIANT (type))
378 value = init;
379 else
380 /* Digest the specified initializer into an expression. */
381 value = digest_init (type, init, (tree *) 0);
383 /* Store the expression if valid; else report error. */
385 if (TREE_CODE (value) == ERROR_MARK)
387 /* Other code expects that initializers for objects of types that need
388 constructing never make it into DECL_INITIAL, and passes 'init' to
389 build_aggr_init without checking DECL_INITIAL. So just return. */
390 else if (TYPE_NEEDS_CONSTRUCTING (type))
391 return value;
392 else if (TREE_STATIC (decl)
393 && (! TREE_CONSTANT (value)
394 || ! initializer_constant_valid_p (value, TREE_TYPE (value))
395 #if 0
396 /* A STATIC PUBLIC int variable doesn't have to be
397 run time inited when doing pic. (mrs) */
398 /* Since ctors and dtors are the only things that can
399 reference vtables, and they are always written down
400 the vtable definition, we can leave the
401 vtables in initialized data space.
402 However, other initialized data cannot be initialized
403 this way. Instead a global file-level initializer
404 must do the job. */
405 || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl))
406 #endif
409 return value;
410 #if 0 /* No, that's C. jason 9/19/94 */
411 else
413 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
415 if (! TREE_CONSTANT (value) || ! TREE_STATIC (value))
416 pedwarn ("ISO C++ forbids non-constant aggregate initializer expressions");
419 #endif
421 /* Store the VALUE in DECL_INITIAL. If we're building a
422 statement-tree we will actually expand the initialization later
423 when we output this function. */
424 DECL_INITIAL (decl) = value;
425 return NULL_TREE;
428 /* Same as store_init_value, but used for known-to-be-valid static
429 initializers. Used to introduce a static initializer even in data
430 structures that may require dynamic initialization. */
432 tree
433 force_store_init_value (decl, init)
434 tree decl, init;
436 tree type = TREE_TYPE (decl);
437 int needs_constructing = TYPE_NEEDS_CONSTRUCTING (type);
439 TYPE_NEEDS_CONSTRUCTING (type) = 0;
441 init = store_init_value (decl, init);
442 if (init)
443 abort ();
445 TYPE_NEEDS_CONSTRUCTING (type) = needs_constructing;
447 return init;
450 /* Digest the parser output INIT as an initializer for type TYPE.
451 Return a C expression of type TYPE to represent the initial value.
453 If TAIL is nonzero, it points to a variable holding a list of elements
454 of which INIT is the first. We update the list stored there by
455 removing from the head all the elements that we use.
456 Normally this is only one; we use more than one element only if
457 TYPE is an aggregate and INIT is not a constructor. */
459 tree
460 digest_init (type, init, tail)
461 tree type, init, *tail;
463 enum tree_code code = TREE_CODE (type);
464 tree element = NULL_TREE;
465 tree old_tail_contents = NULL_TREE;
466 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
467 tree node which has no TREE_TYPE. */
468 int raw_constructor;
470 /* By default, assume we use one element from a list.
471 We correct this later in the sole case where it is not true. */
473 if (tail)
475 old_tail_contents = *tail;
476 *tail = TREE_CHAIN (*tail);
479 if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
480 && TREE_VALUE (init) == error_mark_node))
481 return error_mark_node;
483 if (TREE_CODE (init) == ERROR_MARK)
484 /* __PRETTY_FUNCTION__'s initializer is a bogus expression inside
485 a template function. This gets substituted during instantiation. */
486 return init;
488 /* We must strip the outermost array type when completing the type,
489 because the its bounds might be incomplete at the moment. */
490 if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
491 ? TREE_TYPE (type) : type, NULL_TREE))
492 return error_mark_node;
494 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
495 if (TREE_CODE (init) == NON_LVALUE_EXPR)
496 init = TREE_OPERAND (init, 0);
498 if (TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == type)
499 return init;
501 raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
503 if (raw_constructor
504 && CONSTRUCTOR_ELTS (init) != 0
505 && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
507 element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
508 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
509 if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
510 element = TREE_OPERAND (element, 0);
511 if (element == error_mark_node)
512 return element;
515 /* Initialization of an array of chars from a string constant
516 optionally enclosed in braces. */
518 if (code == ARRAY_TYPE)
520 tree typ1;
522 if (TREE_CODE (init) == TREE_LIST)
524 error ("initializing array with parameter list");
525 return error_mark_node;
528 typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
529 if (char_type_p (typ1)
530 && ((init && TREE_CODE (init) == STRING_CST)
531 || (element && TREE_CODE (element) == STRING_CST)))
533 tree string = element ? element : init;
535 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
536 != char_type_node)
537 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
539 error ("char-array initialized from wide string");
540 return error_mark_node;
542 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
543 == char_type_node)
544 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
546 error ("int-array initialized from non-wide string");
547 return error_mark_node;
550 TREE_TYPE (string) = type;
551 if (TYPE_DOMAIN (type) != 0
552 && TREE_CONSTANT (TYPE_SIZE (type)))
554 register int size
555 = TREE_INT_CST_LOW (TYPE_SIZE (type));
556 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
557 /* In C it is ok to subtract 1 from the length of the string
558 because it's ok to ignore the terminating null char that is
559 counted in the length of the constant, but in C++ this would
560 be invalid. */
561 if (size < TREE_STRING_LENGTH (string))
562 pedwarn ("initializer-string for array of chars is too long");
564 return string;
568 /* Handle scalar types, including conversions,
569 and signature pointers and references. */
571 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
572 || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
573 || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
574 || TYPE_PTRMEMFUNC_P (type))
576 if (raw_constructor)
578 if (element == 0)
580 error ("initializer for scalar variable requires one element");
581 return error_mark_node;
583 init = element;
585 while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
587 pedwarn ("braces around scalar initializer for `%T'", type);
588 init = CONSTRUCTOR_ELTS (init);
589 if (TREE_CHAIN (init))
590 pedwarn ("ignoring extra initializers for `%T'", type);
591 init = TREE_VALUE (init);
594 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
595 "initialization", NULL_TREE, 0);
598 /* Come here only for records and arrays (and unions with constructors). */
600 if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
602 error ("variable-sized object of type `%T' may not be initialized",
603 type);
604 return error_mark_node;
607 if (code == ARRAY_TYPE || code == VECTOR_TYPE || IS_AGGR_TYPE_CODE (code))
609 if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type)
610 && TREE_HAS_CONSTRUCTOR (init))
612 error ("subobject of type `%T' must be initialized by constructor, not by `%E'",
613 type, init);
614 return error_mark_node;
616 else if (raw_constructor)
617 return process_init_constructor (type, init, (tree *)0);
618 else if (can_convert_arg (type, TREE_TYPE (init), init)
619 || TYPE_NON_AGGREGATE_CLASS (type))
620 /* These are never initialized from multiple constructor elements. */;
621 else if (tail != 0)
623 *tail = old_tail_contents;
624 return process_init_constructor (type, 0, tail);
627 if (code != ARRAY_TYPE)
629 int flags = LOOKUP_NORMAL;
630 /* Initialization from { } is copy-initialization. */
631 if (tail)
632 flags |= LOOKUP_ONLYCONVERTING;
634 return convert_for_initialization (NULL_TREE, type, init, flags,
635 "initialization", NULL_TREE, 0);
639 error ("invalid initializer");
640 return error_mark_node;
643 /* Process a constructor for a variable of type TYPE.
644 The constructor elements may be specified either with INIT or with ELTS,
645 only one of which should be non-null.
647 If INIT is specified, it is a CONSTRUCTOR node which is specifically
648 and solely for initializing this datum.
650 If ELTS is specified, it is the address of a variable containing
651 a list of expressions. We take as many elements as we need
652 from the head of the list and update the list.
654 In the resulting constructor, TREE_CONSTANT is set if all elts are
655 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
656 constants that the assembler and linker can compute them. */
658 static tree
659 process_init_constructor (type, init, elts)
660 tree type, init, *elts;
662 register tree tail;
663 /* List of the elements of the result constructor,
664 in reverse order. */
665 register tree members = NULL;
666 register tree next1;
667 tree result;
668 int allconstant = 1;
669 int allsimple = 1;
670 int erroneous = 0;
672 /* Make TAIL be the list of elements to use for the initialization,
673 no matter how the data was given to us. */
675 if (elts)
677 if (warn_missing_braces)
678 warning ("aggregate has a partly bracketed initializer");
679 tail = *elts;
681 else
682 tail = CONSTRUCTOR_ELTS (init);
684 /* Gobble as many elements as needed, and make a constructor or initial value
685 for each element of this aggregate. Chain them together in result.
686 If there are too few, use 0 for each scalar ultimate component. */
688 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
690 register long len;
691 register int i;
693 if (TREE_CODE (type) == ARRAY_TYPE)
695 tree domain = TYPE_DOMAIN (type);
696 if (domain)
697 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
698 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
699 + 1);
700 else
701 len = -1; /* Take as many as there are */
703 else
705 /* Vectors are like simple fixed-size arrays. */
706 len = TYPE_VECTOR_SUBPARTS (type);
709 for (i = 0; len < 0 || i < len; i++)
711 if (tail)
713 if (TREE_PURPOSE (tail)
714 && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
715 || compare_tree_int (TREE_PURPOSE (tail), i) != 0))
716 sorry ("non-trivial labeled initializers");
718 if (TREE_VALUE (tail) != 0)
720 tree tail1 = tail;
721 next1 = digest_init (TREE_TYPE (type),
722 TREE_VALUE (tail), &tail1);
723 if (next1 == error_mark_node)
724 return next1;
725 my_friendly_assert
726 (same_type_ignoring_top_level_qualifiers_p
727 (TREE_TYPE (type), TREE_TYPE (next1)),
728 981123);
729 my_friendly_assert (tail1 == 0
730 || TREE_CODE (tail1) == TREE_LIST, 319);
731 if (tail == tail1 && len < 0)
733 error ("non-empty initializer for array of empty elements");
734 /* Just ignore what we were supposed to use. */
735 tail1 = NULL_TREE;
737 tail = tail1;
739 else
741 next1 = error_mark_node;
742 tail = TREE_CHAIN (tail);
745 else if (len < 0)
746 /* We're done. */
747 break;
748 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
750 /* If this type needs constructors run for
751 default-initialization, we can't rely on the backend to do it
752 for us, so build up TARGET_EXPRs. If the type in question is
753 a class, just build one up; if it's an array, recurse. */
755 if (IS_AGGR_TYPE (TREE_TYPE (type)))
756 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
757 else
758 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE);
759 next1 = digest_init (TREE_TYPE (type), next1, 0);
761 else if (! zero_init_p (TREE_TYPE (type)))
762 next1 = build_forced_zero_init (TREE_TYPE (type));
763 else
764 /* The default zero-initialization is fine for us; don't
765 add anything to the CONSTRUCTOR. */
766 break;
768 if (next1 == error_mark_node)
769 erroneous = 1;
770 else if (!TREE_CONSTANT (next1))
771 allconstant = 0;
772 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
773 allsimple = 0;
774 members = tree_cons (size_int (i), next1, members);
777 else if (TREE_CODE (type) == RECORD_TYPE)
779 register tree field;
781 if (tail)
783 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
785 sorry ("initializer list for object of class with virtual base classes");
786 return error_mark_node;
789 if (TYPE_BINFO_BASETYPES (type))
791 sorry ("initializer list for object of class with base classes");
792 return error_mark_node;
795 if (TYPE_POLYMORPHIC_P (type))
797 sorry ("initializer list for object using virtual functions");
798 return error_mark_node;
802 for (field = TYPE_FIELDS (type); field;
803 field = TREE_CHAIN (field))
805 if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
807 members = tree_cons (field, integer_zero_node, members);
808 continue;
811 if (TREE_CODE (field) != FIELD_DECL)
812 continue;
814 if (tail)
816 if (TREE_PURPOSE (tail)
817 && TREE_PURPOSE (tail) != field
818 && TREE_PURPOSE (tail) != DECL_NAME (field))
819 sorry ("non-trivial labeled initializers");
821 if (TREE_VALUE (tail) != 0)
823 tree tail1 = tail;
825 next1 = digest_init (TREE_TYPE (field),
826 TREE_VALUE (tail), &tail1);
827 my_friendly_assert (tail1 == 0
828 || TREE_CODE (tail1) == TREE_LIST, 320);
829 tail = tail1;
831 else
833 next1 = error_mark_node;
834 tail = TREE_CHAIN (tail);
837 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
839 /* If this type needs constructors run for
840 default-initialization, we can't rely on the backend to do it
841 for us, so build up TARGET_EXPRs. If the type in question is
842 a class, just build one up; if it's an array, recurse. */
844 if (IS_AGGR_TYPE (TREE_TYPE (field)))
845 next1 = build_functional_cast (TREE_TYPE (field),
846 NULL_TREE);
847 else
849 next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE,
850 NULL_TREE);
851 if (init)
852 TREE_HAS_CONSTRUCTOR (next1)
853 = TREE_HAS_CONSTRUCTOR (init);
855 next1 = digest_init (TREE_TYPE (field), next1, 0);
857 /* Warn when some struct elements are implicitly initialized. */
858 if (extra_warnings
859 && (!init || TREE_HAS_CONSTRUCTOR (init)))
860 warning ("missing initializer for member `%D'", field);
862 else
864 if (TREE_READONLY (field))
865 error ("uninitialized const member `%D'", field);
866 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
867 && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
868 error ("member `%D' with uninitialized const fields",
869 field);
870 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
871 error ("member `%D' is uninitialized reference", field);
873 /* Warn when some struct elements are implicitly initialized
874 to zero. */
875 if (extra_warnings
876 && (!init || TREE_HAS_CONSTRUCTOR (init)))
877 warning ("missing initializer for member `%D'", field);
879 if (! zero_init_p (TREE_TYPE (field)))
880 next1 = build_forced_zero_init (TREE_TYPE (field));
881 else
882 /* The default zero-initialization is fine for us; don't
883 add anything to the CONSTRUCTOR. */
884 continue;
887 if (next1 == error_mark_node)
888 erroneous = 1;
889 else if (!TREE_CONSTANT (next1))
890 allconstant = 0;
891 else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
892 allsimple = 0;
893 members = tree_cons (field, next1, members);
896 else if (TREE_CODE (type) == UNION_TYPE
897 /* If the initializer was empty, use default zero initialization. */
898 && tail)
900 register tree field = TYPE_FIELDS (type);
902 /* Find the first named field. ANSI decided in September 1990
903 that only named fields count here. */
904 while (field && (DECL_NAME (field) == 0
905 || TREE_CODE (field) != FIELD_DECL))
906 field = TREE_CHAIN (field);
908 /* If this element specifies a field, initialize via that field. */
909 if (TREE_PURPOSE (tail) != NULL_TREE)
911 int win = 0;
913 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
914 /* Handle the case of a call by build_c_cast. */
915 field = TREE_PURPOSE (tail), win = 1;
916 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
917 error ("index value instead of field name in union initializer");
918 else
920 tree temp;
921 for (temp = TYPE_FIELDS (type);
922 temp;
923 temp = TREE_CHAIN (temp))
924 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
925 break;
926 if (temp)
927 field = temp, win = 1;
928 else
929 error ("no field `%D' in union being initialized",
930 TREE_PURPOSE (tail));
932 if (!win)
933 TREE_VALUE (tail) = error_mark_node;
935 else if (field == 0)
937 error ("union `%T' with no named members cannot be initialized",
938 type);
939 TREE_VALUE (tail) = error_mark_node;
942 if (TREE_VALUE (tail) != 0)
944 tree tail1 = tail;
946 next1 = digest_init (TREE_TYPE (field),
947 TREE_VALUE (tail), &tail1);
948 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
949 abort ();
950 tail = tail1;
952 else
954 next1 = error_mark_node;
955 tail = TREE_CHAIN (tail);
958 if (next1 == error_mark_node)
959 erroneous = 1;
960 else if (!TREE_CONSTANT (next1))
961 allconstant = 0;
962 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
963 allsimple = 0;
964 members = tree_cons (field, next1, members);
967 /* If arguments were specified as a list, just remove the ones we used. */
968 if (elts)
969 *elts = tail;
970 /* If arguments were specified as a constructor,
971 complain unless we used all the elements of the constructor. */
972 else if (tail)
973 pedwarn ("excess elements in aggregate initializer");
975 if (erroneous)
976 return error_mark_node;
978 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
979 if (init)
980 TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
981 if (allconstant) TREE_CONSTANT (result) = 1;
982 if (allconstant && allsimple) TREE_STATIC (result) = 1;
983 return result;
986 /* Given a structure or union value DATUM, construct and return
987 the structure or union component which results from narrowing
988 that value by the type specified in BASETYPE. For example, given the
989 hierarchy
991 class L { int ii; };
992 class A : L { ... };
993 class B : L { ... };
994 class C : A, B { ... };
996 and the declaration
998 C x;
1000 then the expression
1002 x.A::ii refers to the ii member of the L part of
1003 the A part of the C object named by X. In this case,
1004 DATUM would be x, and BASETYPE would be A.
1006 I used to think that this was nonconformant, that the standard specified
1007 that first we look up ii in A, then convert x to an L& and pull out the
1008 ii part. But in fact, it does say that we convert x to an A&; A here
1009 is known as the "naming class". (jason 2000-12-19) */
1011 tree
1012 build_scoped_ref (datum, basetype)
1013 tree datum;
1014 tree basetype;
1016 tree ref;
1017 tree binfo;
1019 if (datum == error_mark_node)
1020 return error_mark_node;
1021 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
1023 if (binfo == error_mark_node)
1024 return error_mark_node;
1025 if (!binfo)
1026 return error_not_base_type (TREE_TYPE (datum), basetype);
1028 ref = build_unary_op (ADDR_EXPR, datum, 0);
1029 ref = build_base_path (PLUS_EXPR, ref, binfo, 1);
1031 return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
1034 /* Build a reference to an object specified by the C++ `->' operator.
1035 Usually this just involves dereferencing the object, but if the
1036 `->' operator is overloaded, then such overloads must be
1037 performed until an object which does not have the `->' operator
1038 overloaded is found. An error is reported when circular pointer
1039 delegation is detected. */
1041 tree
1042 build_x_arrow (datum)
1043 tree datum;
1045 tree types_memoized = NULL_TREE;
1046 register tree rval = datum;
1047 tree type = TREE_TYPE (rval);
1048 tree last_rval = NULL_TREE;
1050 if (type == error_mark_node)
1051 return error_mark_node;
1053 if (processing_template_decl)
1054 return build_min_nt (ARROW_EXPR, rval);
1056 if (TREE_CODE (rval) == OFFSET_REF)
1058 rval = resolve_offset_ref (datum);
1059 type = TREE_TYPE (rval);
1062 if (TREE_CODE (type) == REFERENCE_TYPE)
1064 rval = convert_from_reference (rval);
1065 type = TREE_TYPE (rval);
1068 if (IS_AGGR_TYPE (type))
1070 while ((rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval,
1071 NULL_TREE, NULL_TREE)))
1073 if (rval == error_mark_node)
1074 return error_mark_node;
1076 if (value_member (TREE_TYPE (rval), types_memoized))
1078 error ("circular pointer delegation detected");
1079 return error_mark_node;
1081 else
1083 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
1084 types_memoized);
1086 last_rval = rval;
1089 if (last_rval == NULL_TREE)
1091 error ("base operand of `->' has non-pointer type `%T'", type);
1092 return error_mark_node;
1095 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1096 last_rval = convert_from_reference (last_rval);
1098 else
1099 last_rval = default_conversion (rval);
1101 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1102 return build_indirect_ref (last_rval, NULL);
1104 if (types_memoized)
1105 error ("result of `operator->()' yields non-pointer result");
1106 else
1107 error ("base operand of `->' is not a pointer");
1108 return error_mark_node;
1111 /* Make an expression to refer to the COMPONENT field of
1112 structure or union value DATUM. COMPONENT is an arbitrary
1113 expression. DATUM has not already been checked out to be of
1114 aggregate type.
1116 For C++, COMPONENT may be a TREE_LIST. This happens when we must
1117 return an object of member type to a method of the current class,
1118 but there is not yet enough typing information to know which one.
1119 As a special case, if there is only one method by that name,
1120 it is returned. Otherwise we return an expression which other
1121 routines will have to know how to deal with later. */
1123 tree
1124 build_m_component_ref (datum, component)
1125 tree datum, component;
1127 tree type;
1128 tree objtype;
1129 tree field_type;
1130 int type_quals;
1131 tree binfo;
1133 if (processing_template_decl)
1134 return build_min_nt (DOTSTAR_EXPR, datum, component);
1136 datum = decay_conversion (datum);
1138 if (datum == error_mark_node || component == error_mark_node)
1139 return error_mark_node;
1141 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1143 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
1145 type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
1146 field_type = type;
1148 else if (TYPE_PTRMEM_P (TREE_TYPE (component)))
1150 type = TREE_TYPE (TREE_TYPE (component));
1151 field_type = TREE_TYPE (type);
1153 /* Compute the type of the field, as described in [expr.ref]. */
1154 type_quals = TYPE_UNQUALIFIED;
1155 if (TREE_CODE (field_type) == REFERENCE_TYPE)
1156 /* The standard says that the type of the result should be the
1157 type referred to by the reference. But for now, at least,
1158 we do the conversion from reference type later. */
1160 else
1162 type_quals = (cp_type_quals (field_type)
1163 | cp_type_quals (TREE_TYPE (datum)));
1165 /* There's no such thing as a mutable pointer-to-member, so
1166 we don't need to deal with that here like we do in
1167 build_component_ref. */
1168 field_type = cp_build_qualified_type (field_type, type_quals);
1171 else
1173 error ("`%E' cannot be used as a member pointer, since it is of type `%T'",
1174 component, TREE_TYPE (component));
1175 return error_mark_node;
1178 if (! IS_AGGR_TYPE (objtype))
1180 error ("cannot apply member pointer `%E' to `%E', which is of non-aggregate type `%T'",
1181 component, datum, objtype);
1182 return error_mark_node;
1185 binfo = lookup_base (objtype, TYPE_METHOD_BASETYPE (type),
1186 ba_check, NULL);
1187 if (!binfo)
1189 error ("member type `%T::' incompatible with object type `%T'",
1190 TYPE_METHOD_BASETYPE (type), objtype);
1191 return error_mark_node;
1193 else if (binfo == error_mark_node)
1194 return error_mark_node;
1196 component = build (OFFSET_REF, field_type, datum, component);
1197 if (TREE_CODE (type) == OFFSET_TYPE)
1198 component = resolve_offset_ref (component);
1199 return component;
1202 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1204 tree
1205 build_functional_cast (exp, parms)
1206 tree exp;
1207 tree parms;
1209 /* This is either a call to a constructor,
1210 or a C cast in C++'s `functional' notation. */
1211 tree type;
1213 if (exp == error_mark_node || parms == error_mark_node)
1214 return error_mark_node;
1216 if (TREE_CODE (exp) == IDENTIFIER_NODE)
1218 if (IDENTIFIER_HAS_TYPE_VALUE (exp))
1219 /* Either an enum or an aggregate type. */
1220 type = IDENTIFIER_TYPE_VALUE (exp);
1221 else
1223 type = lookup_name (exp, 1);
1224 if (!type || TREE_CODE (type) != TYPE_DECL)
1226 error ("`%T' fails to be a typedef or built-in type", exp);
1227 return error_mark_node;
1229 type = TREE_TYPE (type);
1232 else if (TREE_CODE (exp) == TYPE_DECL)
1233 type = TREE_TYPE (exp);
1234 else
1235 type = exp;
1237 if (processing_template_decl)
1238 return build_min (CAST_EXPR, type, parms);
1240 if (! IS_AGGR_TYPE (type))
1242 /* this must build a C cast */
1243 if (parms == NULL_TREE)
1244 parms = integer_zero_node;
1245 else
1247 if (TREE_CHAIN (parms) != NULL_TREE)
1248 pedwarn ("initializer list being treated as compound expression");
1249 parms = build_compound_expr (parms);
1252 return build_c_cast (type, parms);
1255 /* Prepare to evaluate as a call to a constructor. If this expression
1256 is actually used, for example,
1258 return X (arg1, arg2, ...);
1260 then the slot being initialized will be filled in. */
1262 if (!complete_type_or_else (type, NULL_TREE))
1263 return error_mark_node;
1264 if (abstract_virtuals_error (NULL_TREE, type))
1265 return error_mark_node;
1267 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1268 return build_c_cast (type, TREE_VALUE (parms));
1270 /* We need to zero-initialize POD types. Let's do that for everything
1271 that doesn't need a constructor. */
1272 if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1273 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1275 exp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
1276 return get_target_expr (exp);
1279 exp = build_method_call (NULL_TREE, complete_ctor_identifier, parms,
1280 TYPE_BINFO (type), LOOKUP_NORMAL);
1282 if (exp == error_mark_node)
1283 return error_mark_node;
1285 return build_cplus_new (type, exp);
1289 /* Complain about defining new types in inappropriate places. We give an
1290 exception for C-style casts, to accommodate GNU C stylings. */
1292 void
1293 check_for_new_type (string, inptree)
1294 const char *string;
1295 flagged_type_tree inptree;
1297 if (inptree.new_type_flag
1298 && (pedantic || strcmp (string, "cast") != 0))
1299 pedwarn ("ISO C++ forbids defining types within %s", string);
1302 /* Add new exception specifier SPEC, to the LIST we currently have.
1303 If it's already in LIST then do nothing.
1304 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1305 know what we're doing. */
1307 tree
1308 add_exception_specifier (list, spec, complain)
1309 tree list, spec;
1310 int complain;
1312 int ok;
1313 tree core = spec;
1314 int is_ptr;
1316 if (spec == error_mark_node)
1317 return list;
1319 my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317);
1321 /* [except.spec] 1, type in an exception specifier shall not be
1322 incomplete, or pointer or ref to incomplete other than pointer
1323 to cv void. */
1324 is_ptr = TREE_CODE (core) == POINTER_TYPE;
1325 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1326 core = TREE_TYPE (core);
1327 if (complain < 0)
1328 ok = 1;
1329 else if (VOID_TYPE_P (core))
1330 ok = is_ptr;
1331 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1332 ok = 1;
1333 else if (processing_template_decl)
1334 ok = 1;
1335 else
1336 ok = COMPLETE_TYPE_P (complete_type (core));
1338 if (ok)
1340 tree probe;
1342 for (probe = list; probe; probe = TREE_CHAIN (probe))
1343 if (same_type_p (TREE_VALUE (probe), spec))
1344 break;
1345 if (!probe)
1347 spec = build_tree_list (NULL_TREE, spec);
1348 TREE_CHAIN (spec) = list;
1349 list = spec;
1352 else if (complain)
1353 incomplete_type_error (NULL_TREE, core);
1354 return list;
1357 /* Combine the two exceptions specifier lists LIST and ADD, and return
1358 their union. */
1360 tree
1361 merge_exception_specifiers (list, add)
1362 tree list, add;
1364 if (!list || !add)
1365 return NULL_TREE;
1366 else if (!TREE_VALUE (list))
1367 return add;
1368 else if (!TREE_VALUE (add))
1369 return list;
1370 else
1372 tree orig_list = list;
1374 for (; add; add = TREE_CHAIN (add))
1376 tree spec = TREE_VALUE (add);
1377 tree probe;
1379 for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1380 if (same_type_p (TREE_VALUE (probe), spec))
1381 break;
1382 if (!probe)
1384 spec = build_tree_list (NULL_TREE, spec);
1385 TREE_CHAIN (spec) = list;
1386 list = spec;
1390 return list;