Merge from mainline.
[official-gcc.git] / gcc / cp / typeck2.c
blob5356faa25fa0f1c6b5a61236743288d6c3ebb41f
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, 2004, 2005, 2006
5 Free Software Foundation, Inc.
6 Hacked by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to
22 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
26 /* This file is part of the C++ front end.
27 It contains routines to build C++ expressions given their operands,
28 including computing the types of the result, C and C++ specific error
29 checks, and some optimization. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "cp-tree.h"
37 #include "flags.h"
38 #include "toplev.h"
39 #include "output.h"
40 #include "diagnostic.h"
42 static tree
43 process_init_constructor (tree type, tree init);
46 /* Print an error message stemming from an attempt to use
47 BASETYPE as a base class for TYPE. */
49 tree
50 error_not_base_type (tree basetype, tree type)
52 if (TREE_CODE (basetype) == FUNCTION_DECL)
53 basetype = DECL_CONTEXT (basetype);
54 error ("type %qT is not a base type for type %qT", basetype, type);
55 return error_mark_node;
58 tree
59 binfo_or_else (tree base, tree type)
61 tree binfo = lookup_base (type, base, ba_unique, NULL);
63 if (binfo == error_mark_node)
64 return NULL_TREE;
65 else if (!binfo)
66 error_not_base_type (base, type);
67 return binfo;
70 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
71 value may not be changed thereafter. Thus, we emit hard errors for these,
72 rather than just pedwarns. If `SOFT' is 1, then we just pedwarn. (For
73 example, conversions to references.) */
75 void
76 readonly_error (tree arg, const char* string, int soft)
78 const char *fmt;
79 void (*fn) (const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2);
81 if (soft)
82 fn = pedwarn;
83 else
84 fn = error;
86 if (TREE_CODE (arg) == COMPONENT_REF)
88 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
89 fmt = "%s of data-member %qD in read-only structure";
90 else
91 fmt = "%s of read-only data-member %qD";
92 (*fn) (fmt, string, TREE_OPERAND (arg, 1));
94 else if (TREE_CODE (arg) == VAR_DECL)
96 if (DECL_LANG_SPECIFIC (arg)
97 && DECL_IN_AGGR_P (arg)
98 && !TREE_STATIC (arg))
99 fmt = "%s of constant field %qD";
100 else
101 fmt = "%s of read-only variable %qD";
102 (*fn) (fmt, string, arg);
104 else if (TREE_CODE (arg) == PARM_DECL)
105 (*fn) ("%s of read-only parameter %qD", string, arg);
106 else if (TREE_CODE (arg) == INDIRECT_REF
107 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
108 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
109 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
110 (*fn) ("%s of read-only reference %qD", string, TREE_OPERAND (arg, 0));
111 else if (TREE_CODE (arg) == RESULT_DECL)
112 (*fn) ("%s of read-only named return value %qD", string, arg);
113 else if (TREE_CODE (arg) == FUNCTION_DECL)
114 (*fn) ("%s of function %qD", string, arg);
115 else
116 (*fn) ("%s of read-only location", string);
120 /* Structure that holds information about declarations whose type was
121 incomplete and we could not check whether it was abstract or not. */
123 struct pending_abstract_type GTY((chain_next ("%h.next")))
125 /* Declaration which we are checking for abstractness. It is either
126 a DECL node, or an IDENTIFIER_NODE if we do not have a full
127 declaration available. */
128 tree decl;
130 /* Type which will be checked for abstractness. */
131 tree type;
133 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
134 because DECLs already carry locus information. */
135 location_t locus;
137 /* Link to the next element in list. */
138 struct pending_abstract_type* next;
142 /* Compute the hash value of the node VAL. This function is used by the
143 hash table abstract_pending_vars. */
145 static hashval_t
146 pat_calc_hash (const void* val)
148 const struct pending_abstract_type *pat =
149 (const struct pending_abstract_type *) val;
150 return (hashval_t) TYPE_UID (pat->type);
154 /* Compare node VAL1 with the type VAL2. This function is used by the
155 hash table abstract_pending_vars. */
157 static int
158 pat_compare (const void* val1, const void* val2)
160 const struct pending_abstract_type *pat1 =
161 (const struct pending_abstract_type *) val1;
162 tree type2 = (tree)val2;
164 return (pat1->type == type2);
167 /* Hash table that maintains pending_abstract_type nodes, for which we still
168 need to check for type abstractness. The key of the table is the type
169 of the declaration. */
170 static GTY ((param_is (struct pending_abstract_type)))
171 htab_t abstract_pending_vars = NULL;
174 /* This function is called after TYPE is completed, and will check if there
175 are pending declarations for which we still need to verify the abstractness
176 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
177 turned out to be incomplete. */
179 void
180 complete_type_check_abstract (tree type)
182 void **slot;
183 struct pending_abstract_type *pat;
184 location_t cur_loc = input_location;
186 gcc_assert (COMPLETE_TYPE_P (type));
188 if (!abstract_pending_vars)
189 return;
191 /* Retrieve the list of pending declarations for this type. */
192 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
193 (hashval_t)TYPE_UID (type), NO_INSERT);
194 if (!slot)
195 return;
196 pat = (struct pending_abstract_type*)*slot;
197 gcc_assert (pat);
199 /* If the type is not abstract, do not do anything. */
200 if (CLASSTYPE_PURE_VIRTUALS (type))
202 struct pending_abstract_type *prev = 0, *next;
204 /* Reverse the list to emit the errors in top-down order. */
205 for (; pat; pat = next)
207 next = pat->next;
208 pat->next = prev;
209 prev = pat;
211 pat = prev;
213 /* Go through the list, and call abstract_virtuals_error for each
214 element: it will issue a diagnostic if the type is abstract. */
215 while (pat)
217 gcc_assert (type == pat->type);
219 /* Tweak input_location so that the diagnostic appears at the correct
220 location. Notice that this is only needed if the decl is an
221 IDENTIFIER_NODE. */
222 input_location = pat->locus;
223 abstract_virtuals_error (pat->decl, pat->type);
224 pat = pat->next;
228 htab_clear_slot (abstract_pending_vars, slot);
230 input_location = cur_loc;
234 /* If TYPE has abstract virtual functions, issue an error about trying
235 to create an object of that type. DECL is the object declared, or
236 NULL_TREE if the declaration is unavailable. Returns 1 if an error
237 occurred; zero if all was well. */
240 abstract_virtuals_error (tree decl, tree type)
242 VEC(tree,gc) *pure;
244 /* This function applies only to classes. Any other entity can never
245 be abstract. */
246 if (!CLASS_TYPE_P (type))
247 return 0;
249 /* If the type is incomplete, we register it within a hash table,
250 so that we can check again once it is completed. This makes sense
251 only for objects for which we have a declaration or at least a
252 name. */
253 if (!COMPLETE_TYPE_P (type))
255 void **slot;
256 struct pending_abstract_type *pat;
258 gcc_assert (!decl || DECL_P (decl)
259 || TREE_CODE (decl) == IDENTIFIER_NODE);
261 if (!abstract_pending_vars)
262 abstract_pending_vars = htab_create_ggc (31, &pat_calc_hash,
263 &pat_compare, NULL);
265 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
266 (hashval_t)TYPE_UID (type), INSERT);
268 pat = GGC_NEW (struct pending_abstract_type);
269 pat->type = type;
270 pat->decl = decl;
271 pat->locus = ((decl && DECL_P (decl))
272 ? DECL_SOURCE_LOCATION (decl)
273 : input_location);
275 pat->next = (struct pending_abstract_type *) *slot;
276 *slot = pat;
278 return 0;
281 if (!TYPE_SIZE (type))
282 /* TYPE is being defined, and during that time
283 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
284 return 0;
286 pure = CLASSTYPE_PURE_VIRTUALS (type);
287 if (!pure)
288 return 0;
290 if (decl)
292 if (TREE_CODE (decl) == RESULT_DECL)
293 return 0;
295 if (TREE_CODE (decl) == VAR_DECL)
296 error ("cannot declare variable %q+D to be of abstract "
297 "type %qT", decl, type);
298 else if (TREE_CODE (decl) == PARM_DECL)
299 error ("cannot declare parameter %q+D to be of abstract type %qT",
300 decl, type);
301 else if (TREE_CODE (decl) == FIELD_DECL)
302 error ("cannot declare field %q+D to be of abstract type %qT",
303 decl, type);
304 else if (TREE_CODE (decl) == FUNCTION_DECL
305 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
306 error ("invalid abstract return type for member function %q+#D", decl);
307 else if (TREE_CODE (decl) == FUNCTION_DECL)
308 error ("invalid abstract return type for function %q+#D", decl);
309 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
310 /* Here we do not have location information. */
311 error ("invalid abstract type %qT for %qE", type, decl);
312 else
313 error ("invalid abstract type for %q+D", decl);
315 else
316 error ("cannot allocate an object of abstract type %qT", type);
318 /* Only go through this once. */
319 if (VEC_length (tree, pure))
321 unsigned ix;
322 tree fn;
324 inform ("%J because the following virtual functions are pure "
325 "within %qT:", TYPE_MAIN_DECL (type), type);
327 for (ix = 0; VEC_iterate (tree, pure, ix, fn); ix++)
328 inform ("\t%+#D", fn);
329 /* Now truncate the vector. This leaves it non-null, so we know
330 there are pure virtuals, but empty so we don't list them out
331 again. */
332 VEC_truncate (tree, pure, 0);
334 else
335 inform ("%J since type %qT has pure virtual functions",
336 TYPE_MAIN_DECL (type), type);
338 return 1;
341 /* Print an error message for invalid use of an incomplete type.
342 VALUE is the expression that was used (or 0 if that isn't known)
343 and TYPE is the type that was invalid. DIAG_TYPE indicates the
344 type of diagnostic: 0 for an error, 1 for a warning, 2 for a
345 pedwarn. */
347 void
348 cxx_incomplete_type_diagnostic (tree value, tree type, int diag_type)
350 int decl = 0;
351 void (*p_msg) (const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2);
353 if (diag_type == 1)
354 p_msg = warning0;
355 else if (diag_type == 2)
356 p_msg = pedwarn;
357 else
358 p_msg = error;
360 /* Avoid duplicate error message. */
361 if (TREE_CODE (type) == ERROR_MARK)
362 return;
364 if (value != 0 && (TREE_CODE (value) == VAR_DECL
365 || TREE_CODE (value) == PARM_DECL
366 || TREE_CODE (value) == FIELD_DECL))
368 p_msg ("%q+D has incomplete type", value);
369 decl = 1;
371 retry:
372 /* We must print an error message. Be clever about what it says. */
374 switch (TREE_CODE (type))
376 case RECORD_TYPE:
377 case UNION_TYPE:
378 case ENUMERAL_TYPE:
379 if (!decl)
380 p_msg ("invalid use of incomplete type %q#T", type);
381 if (!TYPE_TEMPLATE_INFO (type))
382 p_msg ("forward declaration of %q+#T", type);
383 else
384 p_msg ("declaration of %q+#T", type);
385 break;
387 case VOID_TYPE:
388 p_msg ("invalid use of %qT", type);
389 break;
391 case ARRAY_TYPE:
392 if (TYPE_DOMAIN (type))
394 type = TREE_TYPE (type);
395 goto retry;
397 p_msg ("invalid use of array with unspecified bounds");
398 break;
400 case OFFSET_TYPE:
401 bad_member:
402 p_msg ("invalid use of member (did you forget the %<&%> ?)");
403 break;
405 case TEMPLATE_TYPE_PARM:
406 p_msg ("invalid use of template type parameter");
407 break;
409 case TYPENAME_TYPE:
410 p_msg ("invalid use of dependent type %qT", type);
411 break;
413 case UNKNOWN_TYPE:
414 if (value && TREE_CODE (value) == COMPONENT_REF)
415 goto bad_member;
416 else if (value && TREE_CODE (value) == ADDR_EXPR)
417 p_msg ("address of overloaded function with no contextual "
418 "type information");
419 else if (value && TREE_CODE (value) == OVERLOAD)
420 p_msg ("overloaded function with no contextual type information");
421 else
422 p_msg ("insufficient contextual information to determine type");
423 break;
425 default:
426 gcc_unreachable ();
430 /* Backward-compatibility interface to incomplete_type_diagnostic;
431 required by ../tree.c. */
432 #undef cxx_incomplete_type_error
433 void
434 cxx_incomplete_type_error (tree value, tree type)
436 cxx_incomplete_type_diagnostic (value, type, 0);
440 /* The recursive part of split_nonconstant_init. DEST is an lvalue
441 expression to which INIT should be assigned. INIT is a CONSTRUCTOR. */
443 static void
444 split_nonconstant_init_1 (tree dest, tree init)
446 unsigned HOST_WIDE_INT idx;
447 tree field_index, value;
448 tree type = TREE_TYPE (dest);
449 tree inner_type = NULL;
450 bool array_type_p = false;
452 switch (TREE_CODE (type))
454 case ARRAY_TYPE:
455 inner_type = TREE_TYPE (type);
456 array_type_p = true;
457 /* FALLTHRU */
459 case RECORD_TYPE:
460 case UNION_TYPE:
461 case QUAL_UNION_TYPE:
462 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
463 field_index, value)
465 /* The current implementation of this algorithm assumes that
466 the field was set for all the elements. This is usually done
467 by process_init_constructor. */
468 gcc_assert (field_index);
470 if (!array_type_p)
471 inner_type = TREE_TYPE (field_index);
473 if (TREE_CODE (value) == CONSTRUCTOR)
475 tree sub;
477 if (array_type_p)
478 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
479 NULL_TREE, NULL_TREE);
480 else
481 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
482 NULL_TREE);
484 split_nonconstant_init_1 (sub, value);
486 else if (!initializer_constant_valid_p (value, inner_type))
488 tree code;
489 tree sub;
491 /* FIXME: Ordered removal is O(1) so the whole function is
492 worst-case quadratic. This could be fixed using an aside
493 bitmap to record which elements must be removed and remove
494 them all at the same time. Or by merging
495 split_non_constant_init into process_init_constructor_array,
496 that is separating constants from non-constants while building
497 the vector. */
498 VEC_ordered_remove (constructor_elt, CONSTRUCTOR_ELTS (init),
499 idx);
500 --idx;
502 if (array_type_p)
503 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
504 NULL_TREE, NULL_TREE);
505 else
506 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
507 NULL_TREE);
509 code = build2 (INIT_EXPR, inner_type, sub, value);
510 code = build_stmt (EXPR_STMT, code);
511 add_stmt (code);
512 continue;
515 break;
517 case VECTOR_TYPE:
518 if (!initializer_constant_valid_p (init, type))
520 tree code;
521 tree cons = copy_node (init);
522 CONSTRUCTOR_ELTS (init) = NULL;
523 code = build2 (MODIFY_EXPR, type, dest, cons);
524 code = build_stmt (EXPR_STMT, code);
525 add_stmt (code);
527 break;
529 default:
530 gcc_unreachable ();
533 /* The rest of the initializer is now a constant. */
534 TREE_CONSTANT (init) = 1;
537 /* A subroutine of store_init_value. Splits non-constant static
538 initializer INIT into a constant part and generates code to
539 perform the non-constant part of the initialization to DEST.
540 Returns the code for the runtime init. */
542 static tree
543 split_nonconstant_init (tree dest, tree init)
545 tree code;
547 if (TREE_CODE (init) == CONSTRUCTOR)
549 code = push_stmt_list ();
550 split_nonconstant_init_1 (dest, init);
551 code = pop_stmt_list (code);
552 DECL_INITIAL (dest) = init;
553 TREE_READONLY (dest) = 0;
555 else
556 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
558 return code;
561 /* Perform appropriate conversions on the initial value of a variable,
562 store it in the declaration DECL,
563 and print any error messages that are appropriate.
564 If the init is invalid, store an ERROR_MARK.
566 C++: Note that INIT might be a TREE_LIST, which would mean that it is
567 a base class initializer for some aggregate type, hopefully compatible
568 with DECL. If INIT is a single element, and DECL is an aggregate
569 type, we silently convert INIT into a TREE_LIST, allowing a constructor
570 to be called.
572 If INIT is a TREE_LIST and there is no constructor, turn INIT
573 into a CONSTRUCTOR and use standard initialization techniques.
574 Perhaps a warning should be generated?
576 Returns code to be executed if initialization could not be performed
577 for static variable. In that case, caller must emit the code. */
579 tree
580 store_init_value (tree decl, tree init)
582 tree value, type;
584 /* If variable's type was invalidly declared, just ignore it. */
586 type = TREE_TYPE (decl);
587 if (TREE_CODE (type) == ERROR_MARK)
588 return NULL_TREE;
590 if (IS_AGGR_TYPE (type))
592 gcc_assert (TYPE_HAS_TRIVIAL_INIT_REF (type)
593 || TREE_CODE (init) == CONSTRUCTOR);
595 if (TREE_CODE (init) == TREE_LIST)
597 error ("constructor syntax used, but no constructor declared "
598 "for type %qT", type);
599 init = build_constructor_from_list (NULL_TREE, nreverse (init));
602 else if (TREE_CODE (init) == TREE_LIST
603 && TREE_TYPE (init) != unknown_type_node)
605 if (TREE_CODE (decl) == RESULT_DECL)
606 init = build_x_compound_expr_from_list (init,
607 "return value initializer");
608 else if (TREE_CODE (init) == TREE_LIST
609 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
611 error ("cannot initialize arrays using this syntax");
612 return NULL_TREE;
614 else
615 /* We get here with code like `int a (2);' */
616 init = build_x_compound_expr_from_list (init, "initializer");
619 /* End of special C++ code. */
621 /* Digest the specified initializer into an expression. */
622 value = digest_init (type, init);
623 /* If the initializer is not a constant, fill in DECL_INITIAL with
624 the bits that are constant, and then return an expression that
625 will perform the dynamic initialization. */
626 if (value != error_mark_node
627 && (TREE_SIDE_EFFECTS (value)
628 || ! initializer_constant_valid_p (value, TREE_TYPE (value))))
629 return split_nonconstant_init (decl, value);
630 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
631 is an automatic variable, the middle end will turn this into a
632 dynamic initialization later. */
633 DECL_INITIAL (decl) = value;
634 return NULL_TREE;
638 /* Process the initializer INIT for a variable of type TYPE, emitting
639 diagnostics for invalid initializers and converting the initializer as
640 appropriate.
642 For aggregate types, it assumes that reshape_init has already run, thus the
643 initializer will have the right shape (brace elision has been undone). */
645 tree
646 digest_init (tree type, tree init)
648 enum tree_code code = TREE_CODE (type);
650 if (init == error_mark_node)
651 return error_mark_node;
653 gcc_assert (init);
655 /* We must strip the outermost array type when completing the type,
656 because the its bounds might be incomplete at the moment. */
657 if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
658 ? TREE_TYPE (type) : type, NULL_TREE))
659 return error_mark_node;
661 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
662 (g++.old-deja/g++.law/casts2.C). */
663 if (TREE_CODE (init) == NON_LVALUE_EXPR)
664 init = TREE_OPERAND (init, 0);
666 /* Initialization of an array of chars from a string constant. The initializer
667 can be optionally enclosed in braces, but reshape_init has already removed
668 them if they were present. */
669 if (code == ARRAY_TYPE)
671 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
672 if (char_type_p (typ1)
673 /*&& init */
674 && TREE_CODE (init) == STRING_CST)
676 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
678 if (char_type != char_type_node
679 && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
681 error ("char-array initialized from wide string");
682 return error_mark_node;
684 if (char_type == char_type_node
685 && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
687 error ("int-array initialized from non-wide string");
688 return error_mark_node;
691 TREE_TYPE (init) = type;
692 if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
694 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
695 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
696 /* In C it is ok to subtract 1 from the length of the string
697 because it's ok to ignore the terminating null char that is
698 counted in the length of the constant, but in C++ this would
699 be invalid. */
700 if (size < TREE_STRING_LENGTH (init))
701 pedwarn ("initializer-string for array of chars is too long");
703 return init;
707 /* Handle scalar types (including conversions) and references. */
708 if (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE)
709 return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
710 "initialization", NULL_TREE, 0);
712 /* Come here only for aggregates: records, arrays, unions, complex numbers
713 and vectors. */
714 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
715 || TREE_CODE (type) == VECTOR_TYPE
716 || TREE_CODE (type) == RECORD_TYPE
717 || TREE_CODE (type) == UNION_TYPE
718 || TREE_CODE (type) == COMPLEX_TYPE);
720 if (BRACE_ENCLOSED_INITIALIZER_P (init))
721 return process_init_constructor (type, init);
722 else
724 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
726 error ("cannot initialize aggregate of type %qT with "
727 "a compound literal", type);
729 return error_mark_node;
731 return convert_for_initialization (NULL_TREE, type, init,
732 LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING,
733 "initialization", NULL_TREE, 0);
738 /* Set of flags used within process_init_constructor to describe the
739 initializers. */
740 #define PICFLAG_ERRONEOUS 1
741 #define PICFLAG_NOT_ALL_CONSTANT 2
742 #define PICFLAG_NOT_ALL_SIMPLE 4
744 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
745 describe it. */
747 static int
748 picflag_from_initializer (tree init)
750 if (init == error_mark_node)
751 return PICFLAG_ERRONEOUS;
752 else if (!TREE_CONSTANT (init))
753 return PICFLAG_NOT_ALL_CONSTANT;
754 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
755 return PICFLAG_NOT_ALL_SIMPLE;
756 return 0;
759 /* Subroutine of process_init_constructor, which will process an initializer
760 INIT for a array or vector of type TYPE. Returns the flags (PICFLAG_*) which
761 describe the initializers. */
763 static int
764 process_init_constructor_array (tree type, tree init)
766 unsigned HOST_WIDE_INT i, len = 0;
767 int flags = 0;
768 bool unbounded = false;
769 constructor_elt *ce;
770 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (init);
772 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
773 || TREE_CODE (type) == VECTOR_TYPE);
775 if (TREE_CODE (type) == ARRAY_TYPE)
777 tree domain = TYPE_DOMAIN (type);
778 if (domain)
779 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
780 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
781 + 1);
782 else
783 unbounded = true; /* Take as many as there are. */
785 else
786 /* Vectors are like simple fixed-size arrays. */
787 len = TYPE_VECTOR_SUBPARTS (type);
789 /* There cannot be more initializers than needed (or reshape_init would
790 detect this before we do. */
791 if (!unbounded)
792 gcc_assert (VEC_length (constructor_elt, v) <= len);
794 for (i = 0; VEC_iterate (constructor_elt, v, i, ce); ++i)
796 if (ce->index)
798 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
799 if (compare_tree_int (ce->index, i) != 0)
800 sorry ("non-trivial designated initializers not supported");
802 else
803 ce->index = size_int (i);
804 gcc_assert (ce->value);
805 ce->value = digest_init (TREE_TYPE (type), ce->value);
807 if (ce->value != error_mark_node)
808 gcc_assert (same_type_ignoring_top_level_qualifiers_p
809 (TREE_TYPE (type), TREE_TYPE (ce->value)));
811 flags |= picflag_from_initializer (ce->value);
814 /* No more initializers. If the array is unbounded, we are done. Otherwise,
815 we must add initializers ourselves. */
816 if (!unbounded)
817 for (; i < len; ++i)
819 tree next;
821 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
823 /* If this type needs constructors run for default-initialization,
824 we can't rely on the backend to do it for us, so build up
825 TARGET_EXPRs. If the type in question is a class, just build
826 one up; if it's an array, recurse. */
827 if (IS_AGGR_TYPE (TREE_TYPE (type)))
828 next = build_functional_cast (TREE_TYPE (type), NULL_TREE);
829 else
830 next = build_constructor (NULL_TREE, NULL);
831 next = digest_init (TREE_TYPE (type), next);
833 else if (!zero_init_p (TREE_TYPE (type)))
834 next = build_zero_init (TREE_TYPE (type),
835 /*nelts=*/NULL_TREE,
836 /*static_storage_p=*/false);
837 else
838 /* The default zero-initialization is fine for us; don't
839 add anything to the CONSTRUCTOR. */
840 break;
842 flags |= picflag_from_initializer (next);
843 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
846 CONSTRUCTOR_ELTS (init) = v;
847 return flags;
850 /* Subroutine of process_init_constructor, which will process an initializer
851 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
852 the initializers. */
854 static int
855 process_init_constructor_record (tree type, tree init)
857 VEC(constructor_elt,gc) *v = NULL;
858 int flags = 0;
859 tree field;
860 unsigned HOST_WIDE_INT idx = 0;
862 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
863 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
864 gcc_assert (!TYPE_BINFO (type)
865 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
866 gcc_assert (!TYPE_POLYMORPHIC_P (type));
868 /* Generally, we will always have an index for each initializer (which is
869 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
870 reshape_init. So we need to handle both cases. */
871 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
873 tree next;
875 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
877 flags |= picflag_from_initializer (integer_zero_node);
878 CONSTRUCTOR_APPEND_ELT (v, field, integer_zero_node);
879 continue;
882 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
883 continue;
885 if (idx < VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)))
887 constructor_elt *ce = VEC_index (constructor_elt,
888 CONSTRUCTOR_ELTS (init), idx);
889 if (ce->index)
891 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
892 latter case can happen in templates where lookup has to be
893 deferred. */
894 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
895 || TREE_CODE (ce->index) == IDENTIFIER_NODE);
896 if (ce->index != field
897 && ce->index != DECL_NAME (field))
898 sorry ("non-trivial designated initializers not supported");
901 gcc_assert (ce->value);
902 next = digest_init (TREE_TYPE (field), ce->value);
903 ++idx;
905 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
907 /* If this type needs constructors run for
908 default-initialization, we can't rely on the backend to do it
909 for us, so build up TARGET_EXPRs. If the type in question is
910 a class, just build one up; if it's an array, recurse. */
911 if (IS_AGGR_TYPE (TREE_TYPE (field)))
912 next = build_functional_cast (TREE_TYPE (field), NULL_TREE);
913 else
914 next = build_constructor (NULL_TREE, NULL);
916 next = digest_init (TREE_TYPE (field), next);
918 /* Warn when some struct elements are implicitly initialized. */
919 warning (OPT_Wmissing_field_initializers,
920 "missing initializer for member %qD", field);
922 else
924 if (TREE_READONLY (field))
925 error ("uninitialized const member %qD", field);
926 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
927 error ("member %qD with uninitialized const fields", field);
928 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
929 error ("member %qD is uninitialized reference", field);
931 /* Warn when some struct elements are implicitly initialized
932 to zero. */
933 warning (OPT_Wmissing_field_initializers,
934 "missing initializer for member %qD", field);
936 if (!zero_init_p (TREE_TYPE (field)))
937 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
938 /*static_storage_p=*/false);
939 else
940 /* The default zero-initialization is fine for us; don't
941 add anything to the CONSTRUCTOR. */
942 continue;
945 flags |= picflag_from_initializer (next);
946 CONSTRUCTOR_APPEND_ELT (v, field, next);
949 CONSTRUCTOR_ELTS (init) = v;
950 return flags;
953 /* Subroutine of process_init_constructor, which will process a single
954 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
955 which describe the initializer. */
957 static int
958 process_init_constructor_union (tree type, tree init)
960 constructor_elt *ce;
962 /* If the initializer was empty, use default zero initialization. */
963 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
964 return 0;
966 gcc_assert (VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)) == 1);
967 ce = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (init), 0);
969 /* If this element specifies a field, initialize via that field. */
970 if (ce->index)
972 if (TREE_CODE (ce->index) == FIELD_DECL)
974 else if (TREE_CODE (ce->index) == IDENTIFIER_NODE)
976 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
977 tree name = ce->index;
978 tree field;
979 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
980 if (DECL_NAME (field) == name)
981 break;
982 if (!field)
984 error ("no field %qD found in union being initialized", field);
985 ce->value = error_mark_node;
987 ce->index = field;
989 else
991 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
992 || TREE_CODE (ce->index) == RANGE_EXPR);
993 error ("index value instead of field name in union initializer");
994 ce->value = error_mark_node;
997 else
999 /* Find the first named field. ANSI decided in September 1990
1000 that only named fields count here. */
1001 tree field = TYPE_FIELDS (type);
1002 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1003 field = TREE_CHAIN (field);
1004 gcc_assert (field);
1005 ce->index = field;
1008 if (ce->value && ce->value != error_mark_node)
1009 ce->value = digest_init (TREE_TYPE (ce->index), ce->value);
1011 return picflag_from_initializer (ce->value);
1014 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1015 constructor is a brace-enclosed initializer, and will be modified in-place.
1017 Each element is converted to the right type through digest_init, and
1018 missing initializers are added following the language rules (zero-padding,
1019 etc.).
1021 After the execution, the initializer will have TREE_CONSTANT if all elts are
1022 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1023 constants that the assembler and linker can compute them.
1025 The function returns the initializer itself, or error_mark_node in case
1026 of error. */
1028 static tree
1029 process_init_constructor (tree type, tree init)
1031 int flags;
1033 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1035 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1036 flags = process_init_constructor_array (type, init);
1037 else if (TREE_CODE (type) == RECORD_TYPE)
1038 flags = process_init_constructor_record (type, init);
1039 else if (TREE_CODE (type) == UNION_TYPE)
1040 flags = process_init_constructor_union (type, init);
1041 else
1042 gcc_unreachable ();
1044 if (flags & PICFLAG_ERRONEOUS)
1045 return error_mark_node;
1047 TREE_TYPE (init) = type;
1048 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1049 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1050 if (!(flags & PICFLAG_NOT_ALL_CONSTANT))
1052 TREE_CONSTANT (init) = 1;
1053 TREE_INVARIANT (init) = 1;
1054 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1055 TREE_STATIC (init) = 1;
1057 return init;
1060 /* Given a structure or union value DATUM, construct and return
1061 the structure or union component which results from narrowing
1062 that value to the base specified in BASETYPE. For example, given the
1063 hierarchy
1065 class L { int ii; };
1066 class A : L { ... };
1067 class B : L { ... };
1068 class C : A, B { ... };
1070 and the declaration
1072 C x;
1074 then the expression
1076 x.A::ii refers to the ii member of the L part of
1077 the A part of the C object named by X. In this case,
1078 DATUM would be x, and BASETYPE would be A.
1080 I used to think that this was nonconformant, that the standard specified
1081 that first we look up ii in A, then convert x to an L& and pull out the
1082 ii part. But in fact, it does say that we convert x to an A&; A here
1083 is known as the "naming class". (jason 2000-12-19)
1085 BINFO_P points to a variable initialized either to NULL_TREE or to the
1086 binfo for the specific base subobject we want to convert to. */
1088 tree
1089 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1091 tree binfo;
1093 if (datum == error_mark_node)
1094 return error_mark_node;
1095 if (*binfo_p)
1096 binfo = *binfo_p;
1097 else
1098 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
1100 if (!binfo || binfo == error_mark_node)
1102 *binfo_p = NULL_TREE;
1103 if (!binfo)
1104 error_not_base_type (basetype, TREE_TYPE (datum));
1105 return error_mark_node;
1108 *binfo_p = binfo;
1109 return build_base_path (PLUS_EXPR, datum, binfo, 1);
1112 /* Build a reference to an object specified by the C++ `->' operator.
1113 Usually this just involves dereferencing the object, but if the
1114 `->' operator is overloaded, then such overloads must be
1115 performed until an object which does not have the `->' operator
1116 overloaded is found. An error is reported when circular pointer
1117 delegation is detected. */
1119 tree
1120 build_x_arrow (tree expr)
1122 tree orig_expr = expr;
1123 tree types_memoized = NULL_TREE;
1124 tree type = TREE_TYPE (expr);
1125 tree last_rval = NULL_TREE;
1127 if (type == error_mark_node)
1128 return error_mark_node;
1130 if (processing_template_decl)
1132 if (type_dependent_expression_p (expr))
1133 return build_min_nt (ARROW_EXPR, expr);
1134 expr = build_non_dependent_expr (expr);
1137 if (IS_AGGR_TYPE (type))
1139 while ((expr = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, expr,
1140 NULL_TREE, NULL_TREE,
1141 /*overloaded_p=*/NULL)))
1143 if (expr == error_mark_node)
1144 return error_mark_node;
1146 if (value_member (TREE_TYPE (expr), types_memoized))
1148 error ("circular pointer delegation detected");
1149 return error_mark_node;
1151 else
1153 types_memoized = tree_cons (NULL_TREE, TREE_TYPE (expr),
1154 types_memoized);
1156 last_rval = expr;
1159 if (last_rval == NULL_TREE)
1161 error ("base operand of %<->%> has non-pointer type %qT", type);
1162 return error_mark_node;
1165 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1166 last_rval = convert_from_reference (last_rval);
1168 else
1169 last_rval = decay_conversion (expr);
1171 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1173 if (processing_template_decl)
1175 expr = build_min_non_dep (ARROW_EXPR, last_rval, orig_expr);
1176 /* It will be dereferenced. */
1177 TREE_TYPE (expr) = TREE_TYPE (TREE_TYPE (last_rval));
1178 return expr;
1181 return build_indirect_ref (last_rval, NULL);
1184 if (types_memoized)
1185 error ("result of %<operator->()%> yields non-pointer result");
1186 else
1187 error ("base operand of %<->%> is not a pointer");
1188 return error_mark_node;
1191 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1192 already been checked out to be of aggregate type. */
1194 tree
1195 build_m_component_ref (tree datum, tree component)
1197 tree ptrmem_type;
1198 tree objtype;
1199 tree type;
1200 tree binfo;
1201 tree ctype;
1203 if (datum == error_mark_node || component == error_mark_node)
1204 return error_mark_node;
1206 ptrmem_type = TREE_TYPE (component);
1207 if (!TYPE_PTR_TO_MEMBER_P (ptrmem_type))
1209 error ("%qE cannot be used as a member pointer, since it is of "
1210 "type %qT",
1211 component, ptrmem_type);
1212 return error_mark_node;
1215 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1216 if (! IS_AGGR_TYPE (objtype))
1218 error ("cannot apply member pointer %qE to %qE, which is of "
1219 "non-class type %qT",
1220 component, datum, objtype);
1221 return error_mark_node;
1224 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1225 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1227 if (!COMPLETE_TYPE_P (ctype))
1229 if (!same_type_p (ctype, objtype))
1230 goto mismatch;
1231 binfo = NULL;
1233 else
1235 binfo = lookup_base (objtype, ctype, ba_check, NULL);
1237 if (!binfo)
1239 mismatch:
1240 error ("pointer to member type %qT incompatible with object "
1241 "type %qT",
1242 type, objtype);
1243 return error_mark_node;
1245 else if (binfo == error_mark_node)
1246 return error_mark_node;
1249 if (TYPE_PTRMEM_P (ptrmem_type))
1251 /* Compute the type of the field, as described in [expr.ref].
1252 There's no such thing as a mutable pointer-to-member, so
1253 things are not as complex as they are for references to
1254 non-static data members. */
1255 type = cp_build_qualified_type (type,
1256 (cp_type_quals (type)
1257 | cp_type_quals (TREE_TYPE (datum))));
1259 datum = build_address (datum);
1261 /* Convert object to the correct base. */
1262 if (binfo)
1263 datum = build_base_path (PLUS_EXPR, datum, binfo, 1);
1265 /* Build an expression for "object + offset" where offset is the
1266 value stored in the pointer-to-data-member. */
1267 datum = build2 (PLUS_EXPR, build_pointer_type (type),
1268 datum, build_nop (ptrdiff_type_node, component));
1269 return build_indirect_ref (datum, 0);
1271 else
1272 return build2 (OFFSET_REF, type, datum, component);
1275 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1277 tree
1278 build_functional_cast (tree exp, tree parms)
1280 /* This is either a call to a constructor,
1281 or a C cast in C++'s `functional' notation. */
1282 tree type;
1284 if (exp == error_mark_node || parms == error_mark_node)
1285 return error_mark_node;
1287 if (TREE_CODE (exp) == TYPE_DECL)
1288 type = TREE_TYPE (exp);
1289 else
1290 type = exp;
1292 if (processing_template_decl)
1294 tree t = build_min (CAST_EXPR, type, parms);
1295 /* We don't know if it will or will not have side effects. */
1296 TREE_SIDE_EFFECTS (t) = 1;
1297 return t;
1300 if (! IS_AGGR_TYPE (type))
1302 /* This must build a C cast. */
1303 if (parms == NULL_TREE)
1304 parms = integer_zero_node;
1305 else
1306 parms = build_x_compound_expr_from_list (parms, "functional cast");
1308 return build_c_cast (type, parms);
1311 /* Prepare to evaluate as a call to a constructor. If this expression
1312 is actually used, for example,
1314 return X (arg1, arg2, ...);
1316 then the slot being initialized will be filled in. */
1318 if (!complete_type_or_else (type, NULL_TREE))
1319 return error_mark_node;
1320 if (abstract_virtuals_error (NULL_TREE, type))
1321 return error_mark_node;
1323 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1324 return build_c_cast (type, TREE_VALUE (parms));
1326 /* We need to zero-initialize POD types. Let's do that for everything
1327 that doesn't need a constructor. */
1328 if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1329 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1331 exp = build_constructor (type, NULL);
1332 return get_target_expr (exp);
1335 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, parms,
1336 type, LOOKUP_NORMAL);
1338 if (exp == error_mark_node)
1339 return error_mark_node;
1341 return build_cplus_new (type, exp);
1345 /* Add new exception specifier SPEC, to the LIST we currently have.
1346 If it's already in LIST then do nothing.
1347 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1348 know what we're doing. */
1350 tree
1351 add_exception_specifier (tree list, tree spec, int complain)
1353 bool ok;
1354 tree core = spec;
1355 bool is_ptr;
1356 int diag_type = -1; /* none */
1358 if (spec == error_mark_node)
1359 return list;
1361 gcc_assert (spec && (!list || TREE_VALUE (list)));
1363 /* [except.spec] 1, type in an exception specifier shall not be
1364 incomplete, or pointer or ref to incomplete other than pointer
1365 to cv void. */
1366 is_ptr = TREE_CODE (core) == POINTER_TYPE;
1367 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1368 core = TREE_TYPE (core);
1369 if (complain < 0)
1370 ok = true;
1371 else if (VOID_TYPE_P (core))
1372 ok = is_ptr;
1373 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1374 ok = true;
1375 else if (processing_template_decl)
1376 ok = true;
1377 else
1379 ok = true;
1380 /* 15.4/1 says that types in an exception specifier must be complete,
1381 but it seems more reasonable to only require this on definitions
1382 and calls. So just give a pedwarn at this point; we will give an
1383 error later if we hit one of those two cases. */
1384 if (!COMPLETE_TYPE_P (complete_type (core)))
1385 diag_type = 2; /* pedwarn */
1388 if (ok)
1390 tree probe;
1392 for (probe = list; probe; probe = TREE_CHAIN (probe))
1393 if (same_type_p (TREE_VALUE (probe), spec))
1394 break;
1395 if (!probe)
1396 list = tree_cons (NULL_TREE, spec, list);
1398 else
1399 diag_type = 0; /* error */
1401 if (diag_type >= 0 && complain)
1402 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1404 return list;
1407 /* Combine the two exceptions specifier lists LIST and ADD, and return
1408 their union. */
1410 tree
1411 merge_exception_specifiers (tree list, tree add)
1413 if (!list || !add)
1414 return NULL_TREE;
1415 else if (!TREE_VALUE (list))
1416 return add;
1417 else if (!TREE_VALUE (add))
1418 return list;
1419 else
1421 tree orig_list = list;
1423 for (; add; add = TREE_CHAIN (add))
1425 tree spec = TREE_VALUE (add);
1426 tree probe;
1428 for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1429 if (same_type_p (TREE_VALUE (probe), spec))
1430 break;
1431 if (!probe)
1433 spec = build_tree_list (NULL_TREE, spec);
1434 TREE_CHAIN (spec) = list;
1435 list = spec;
1439 return list;
1442 /* Subroutine of build_call. Ensure that each of the types in the
1443 exception specification is complete. Technically, 15.4/1 says that
1444 they need to be complete when we see a declaration of the function,
1445 but we should be able to get away with only requiring this when the
1446 function is defined or called. See also add_exception_specifier. */
1448 void
1449 require_complete_eh_spec_types (tree fntype, tree decl)
1451 tree raises;
1452 /* Don't complain about calls to op new. */
1453 if (decl && DECL_ARTIFICIAL (decl))
1454 return;
1455 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1456 raises = TREE_CHAIN (raises))
1458 tree type = TREE_VALUE (raises);
1459 if (type && !COMPLETE_TYPE_P (type))
1461 if (decl)
1462 error
1463 ("call to function %qD which throws incomplete type %q#T",
1464 decl, type);
1465 else
1466 error ("call to function which throws incomplete type %q#T",
1467 decl);
1473 #include "gt-cp-typeck2.h"