Implement C++11 inheriting constructors.
[official-gcc.git] / gcc / cp / typeck2.c
blob3dbfcb6663a09b9ff65acae8f81201a438edc465
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, 2007, 2008, 2009, 2010, 2011,
5 2012
6 Free Software Foundation, Inc.
7 Hacked by Michael Tiemann (tiemann@cygnus.com)
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3, or (at your option)
14 any later version.
16 GCC is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
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 "intl.h"
37 #include "cp-tree.h"
38 #include "flags.h"
39 #include "diagnostic-core.h"
41 static tree
42 process_init_constructor (tree type, tree init, tsubst_flags_t complain);
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 (tree basetype, tree type)
51 if (TREE_CODE (basetype) == FUNCTION_DECL)
52 basetype = DECL_CONTEXT (basetype);
53 error ("type %qT is not a base type for type %qT", basetype, type);
54 return error_mark_node;
57 tree
58 binfo_or_else (tree base, tree type)
60 tree binfo = lookup_base (type, base, ba_unique,
61 NULL, tf_warning_or_error);
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. */
73 void
74 cxx_readonly_error (tree arg, enum lvalue_use errstring)
77 /* This macro is used to emit diagnostics to ensure that all format
78 strings are complete sentences, visible to gettext and checked at
79 compile time. */
81 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
82 do { \
83 switch (errstring) \
84 { \
85 case lv_assign: \
86 error(AS, ARG); \
87 break; \
88 case lv_asm: \
89 error(ASM, ARG); \
90 break; \
91 case lv_increment: \
92 error (IN, ARG); \
93 break; \
94 case lv_decrement: \
95 error (DE, ARG); \
96 break; \
97 default: \
98 gcc_unreachable (); \
99 } \
100 } while (0)
102 /* Handle C++-specific things first. */
104 if (TREE_CODE (arg) == VAR_DECL
105 && DECL_LANG_SPECIFIC (arg)
106 && DECL_IN_AGGR_P (arg)
107 && !TREE_STATIC (arg))
108 ERROR_FOR_ASSIGNMENT (G_("assignment of "
109 "constant field %qD"),
110 G_("constant field %qD "
111 "used as %<asm%> output"),
112 G_("increment of "
113 "constant field %qD"),
114 G_("decrement of "
115 "constant field %qD"),
116 arg);
117 else if (TREE_CODE (arg) == INDIRECT_REF
118 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
119 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
120 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
121 ERROR_FOR_ASSIGNMENT (G_("assignment of "
122 "read-only reference %qD"),
123 G_("read-only reference %qD "
124 "used as %<asm%> output"),
125 G_("increment of "
126 "read-only reference %qD"),
127 G_("decrement of "
128 "read-only reference %qD"),
129 TREE_OPERAND (arg, 0));
130 else
131 readonly_error (arg, errstring);
135 /* Structure that holds information about declarations whose type was
136 incomplete and we could not check whether it was abstract or not. */
138 struct GTY((chain_next ("%h.next"))) pending_abstract_type {
139 /* Declaration which we are checking for abstractness. It is either
140 a DECL node, or an IDENTIFIER_NODE if we do not have a full
141 declaration available. */
142 tree decl;
144 /* Type which will be checked for abstractness. */
145 tree type;
147 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
148 because DECLs already carry locus information. */
149 location_t locus;
151 /* Link to the next element in list. */
152 struct pending_abstract_type* next;
156 /* Compute the hash value of the node VAL. This function is used by the
157 hash table abstract_pending_vars. */
159 static hashval_t
160 pat_calc_hash (const void* val)
162 const struct pending_abstract_type *pat =
163 (const struct pending_abstract_type *) val;
164 return (hashval_t) TYPE_UID (pat->type);
168 /* Compare node VAL1 with the type VAL2. This function is used by the
169 hash table abstract_pending_vars. */
171 static int
172 pat_compare (const void* val1, const void* val2)
174 const struct pending_abstract_type *const pat1 =
175 (const struct pending_abstract_type *) val1;
176 const_tree const type2 = (const_tree)val2;
178 return (pat1->type == type2);
181 /* Hash table that maintains pending_abstract_type nodes, for which we still
182 need to check for type abstractness. The key of the table is the type
183 of the declaration. */
184 static GTY ((param_is (struct pending_abstract_type)))
185 htab_t abstract_pending_vars = NULL;
188 /* This function is called after TYPE is completed, and will check if there
189 are pending declarations for which we still need to verify the abstractness
190 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
191 turned out to be incomplete. */
193 void
194 complete_type_check_abstract (tree type)
196 void **slot;
197 struct pending_abstract_type *pat;
198 location_t cur_loc = input_location;
200 gcc_assert (COMPLETE_TYPE_P (type));
202 if (!abstract_pending_vars)
203 return;
205 /* Retrieve the list of pending declarations for this type. */
206 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
207 (hashval_t)TYPE_UID (type), NO_INSERT);
208 if (!slot)
209 return;
210 pat = (struct pending_abstract_type*)*slot;
211 gcc_assert (pat);
213 /* If the type is not abstract, do not do anything. */
214 if (CLASSTYPE_PURE_VIRTUALS (type))
216 struct pending_abstract_type *prev = 0, *next;
218 /* Reverse the list to emit the errors in top-down order. */
219 for (; pat; pat = next)
221 next = pat->next;
222 pat->next = prev;
223 prev = pat;
225 pat = prev;
227 /* Go through the list, and call abstract_virtuals_error for each
228 element: it will issue a diagnostic if the type is abstract. */
229 while (pat)
231 gcc_assert (type == pat->type);
233 /* Tweak input_location so that the diagnostic appears at the correct
234 location. Notice that this is only needed if the decl is an
235 IDENTIFIER_NODE. */
236 input_location = pat->locus;
237 abstract_virtuals_error (pat->decl, pat->type);
238 pat = pat->next;
242 htab_clear_slot (abstract_pending_vars, slot);
244 input_location = cur_loc;
248 /* If TYPE has abstract virtual functions, issue an error about trying
249 to create an object of that type. DECL is the object declared, or
250 NULL_TREE if the declaration is unavailable. Returns 1 if an error
251 occurred; zero if all was well. */
254 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
256 VEC(tree,gc) *pure;
258 /* This function applies only to classes. Any other entity can never
259 be abstract. */
260 if (!CLASS_TYPE_P (type))
261 return 0;
262 type = TYPE_MAIN_VARIANT (type);
264 /* If the type is incomplete, we register it within a hash table,
265 so that we can check again once it is completed. This makes sense
266 only for objects for which we have a declaration or at least a
267 name. */
268 if (!COMPLETE_TYPE_P (type))
270 void **slot;
271 struct pending_abstract_type *pat;
273 gcc_assert (!decl || DECL_P (decl)
274 || TREE_CODE (decl) == IDENTIFIER_NODE);
276 if (!abstract_pending_vars)
277 abstract_pending_vars = htab_create_ggc (31, &pat_calc_hash,
278 &pat_compare, NULL);
280 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
281 (hashval_t)TYPE_UID (type), INSERT);
283 pat = ggc_alloc_pending_abstract_type ();
284 pat->type = type;
285 pat->decl = decl;
286 pat->locus = ((decl && DECL_P (decl))
287 ? DECL_SOURCE_LOCATION (decl)
288 : input_location);
290 pat->next = (struct pending_abstract_type *) *slot;
291 *slot = pat;
293 return 0;
296 if (!TYPE_SIZE (type))
297 /* TYPE is being defined, and during that time
298 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
299 return 0;
301 pure = CLASSTYPE_PURE_VIRTUALS (type);
302 if (!pure)
303 return 0;
305 if (!(complain & tf_error))
306 return 1;
308 if (decl)
310 if (TREE_CODE (decl) == VAR_DECL)
311 error ("cannot declare variable %q+D to be of abstract "
312 "type %qT", decl, type);
313 else if (TREE_CODE (decl) == PARM_DECL)
314 error ("cannot declare parameter %q+D to be of abstract type %qT",
315 decl, type);
316 else if (TREE_CODE (decl) == FIELD_DECL)
317 error ("cannot declare field %q+D to be of abstract type %qT",
318 decl, type);
319 else if (TREE_CODE (decl) == FUNCTION_DECL
320 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
321 error ("invalid abstract return type for member function %q+#D", decl);
322 else if (TREE_CODE (decl) == FUNCTION_DECL)
323 error ("invalid abstract return type for function %q+#D", decl);
324 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
325 /* Here we do not have location information. */
326 error ("invalid abstract type %qT for %qE", type, decl);
327 else
328 error ("invalid abstract type for %q+D", decl);
330 else
331 error ("cannot allocate an object of abstract type %qT", type);
333 /* Only go through this once. */
334 if (VEC_length (tree, pure))
336 unsigned ix;
337 tree fn;
339 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
340 " because the following virtual functions are pure within %qT:",
341 type);
343 FOR_EACH_VEC_ELT (tree, pure, ix, fn)
344 if (! DECL_CLONED_FUNCTION_P (fn)
345 || DECL_COMPLETE_DESTRUCTOR_P (fn))
346 inform (input_location, "\t%+#D", fn);
348 /* Now truncate the vector. This leaves it non-null, so we know
349 there are pure virtuals, but empty so we don't list them out
350 again. */
351 VEC_truncate (tree, pure, 0);
353 else
354 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
355 " since type %qT has pure virtual functions",
356 type);
358 return 1;
361 /* Wrapper for the above function in the common case of wanting errors. */
364 abstract_virtuals_error (tree decl, tree type)
366 return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
369 /* Print an error message for invalid use of an incomplete type.
370 VALUE is the expression that was used (or 0 if that isn't known)
371 and TYPE is the type that was invalid. DIAG_KIND indicates the
372 type of diagnostic (see diagnostic.def). */
374 void
375 cxx_incomplete_type_diagnostic (const_tree value, const_tree type,
376 diagnostic_t diag_kind)
378 int decl = 0;
380 gcc_assert (diag_kind == DK_WARNING
381 || diag_kind == DK_PEDWARN
382 || diag_kind == DK_ERROR);
384 /* Avoid duplicate error message. */
385 if (TREE_CODE (type) == ERROR_MARK)
386 return;
388 if (value != 0 && (TREE_CODE (value) == VAR_DECL
389 || TREE_CODE (value) == PARM_DECL
390 || TREE_CODE (value) == FIELD_DECL))
392 emit_diagnostic (diag_kind, input_location, 0,
393 "%q+D has incomplete type", value);
394 decl = 1;
396 retry:
397 /* We must print an error message. Be clever about what it says. */
399 switch (TREE_CODE (type))
401 case RECORD_TYPE:
402 case UNION_TYPE:
403 case ENUMERAL_TYPE:
404 if (!decl)
405 emit_diagnostic (diag_kind, input_location, 0,
406 "invalid use of incomplete type %q#T", type);
407 if (!TYPE_TEMPLATE_INFO (type))
408 emit_diagnostic (diag_kind, input_location, 0,
409 "forward declaration of %q+#T", type);
410 else
411 emit_diagnostic (diag_kind, input_location, 0,
412 "declaration of %q+#T", type);
413 break;
415 case VOID_TYPE:
416 emit_diagnostic (diag_kind, input_location, 0,
417 "invalid use of %qT", type);
418 break;
420 case ARRAY_TYPE:
421 if (TYPE_DOMAIN (type))
423 type = TREE_TYPE (type);
424 goto retry;
426 emit_diagnostic (diag_kind, input_location, 0,
427 "invalid use of array with unspecified bounds");
428 break;
430 case OFFSET_TYPE:
431 bad_member:
433 tree member = TREE_OPERAND (value, 1);
434 if (is_overloaded_fn (member))
435 member = get_first_fn (member);
436 if (DECL_FUNCTION_MEMBER_P (member)
437 && ! flag_ms_extensions)
438 emit_diagnostic (diag_kind, input_location, 0,
439 "invalid use of member function "
440 "(did you forget the %<()%> ?)");
441 else
442 emit_diagnostic (diag_kind, input_location, 0,
443 "invalid use of member "
444 "(did you forget the %<&%> ?)");
446 break;
448 case TEMPLATE_TYPE_PARM:
449 if (is_auto (type))
450 emit_diagnostic (diag_kind, input_location, 0,
451 "invalid use of %<auto%>");
452 else
453 emit_diagnostic (diag_kind, input_location, 0,
454 "invalid use of template type parameter %qT", type);
455 break;
457 case BOUND_TEMPLATE_TEMPLATE_PARM:
458 emit_diagnostic (diag_kind, input_location, 0,
459 "invalid use of template template parameter %qT",
460 TYPE_NAME (type));
461 break;
463 case TYPENAME_TYPE:
464 emit_diagnostic (diag_kind, input_location, 0,
465 "invalid use of dependent type %qT", type);
466 break;
468 case LANG_TYPE:
469 if (type == init_list_type_node)
471 emit_diagnostic (diag_kind, input_location, 0,
472 "invalid use of brace-enclosed initializer list");
473 break;
475 gcc_assert (type == unknown_type_node);
476 if (value && TREE_CODE (value) == COMPONENT_REF)
477 goto bad_member;
478 else if (value && TREE_CODE (value) == ADDR_EXPR)
479 emit_diagnostic (diag_kind, input_location, 0,
480 "address of overloaded function with no contextual "
481 "type information");
482 else if (value && TREE_CODE (value) == OVERLOAD)
483 emit_diagnostic (diag_kind, input_location, 0,
484 "overloaded function with no contextual type information");
485 else
486 emit_diagnostic (diag_kind, input_location, 0,
487 "insufficient contextual information to determine type");
488 break;
490 default:
491 gcc_unreachable ();
495 /* Backward-compatibility interface to incomplete_type_diagnostic;
496 required by ../tree.c. */
497 #undef cxx_incomplete_type_error
498 void
499 cxx_incomplete_type_error (const_tree value, const_tree type)
501 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
505 /* The recursive part of split_nonconstant_init. DEST is an lvalue
506 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
507 Return true if the whole of the value was initialized by the
508 generated statements. */
510 static bool
511 split_nonconstant_init_1 (tree dest, tree init)
513 unsigned HOST_WIDE_INT idx;
514 tree field_index, value;
515 tree type = TREE_TYPE (dest);
516 tree inner_type = NULL;
517 bool array_type_p = false;
518 bool complete_p = true;
519 HOST_WIDE_INT num_split_elts = 0;
521 switch (TREE_CODE (type))
523 case ARRAY_TYPE:
524 inner_type = TREE_TYPE (type);
525 array_type_p = true;
526 /* FALLTHRU */
528 case RECORD_TYPE:
529 case UNION_TYPE:
530 case QUAL_UNION_TYPE:
531 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
532 field_index, value)
534 /* The current implementation of this algorithm assumes that
535 the field was set for all the elements. This is usually done
536 by process_init_constructor. */
537 gcc_assert (field_index);
539 if (!array_type_p)
540 inner_type = TREE_TYPE (field_index);
542 if (TREE_CODE (value) == CONSTRUCTOR)
544 tree sub;
546 if (array_type_p)
547 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
548 NULL_TREE, NULL_TREE);
549 else
550 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
551 NULL_TREE);
553 if (!split_nonconstant_init_1 (sub, value))
554 complete_p = false;
555 num_split_elts++;
557 else if (!initializer_constant_valid_p (value, inner_type))
559 tree code;
560 tree sub;
562 /* FIXME: Ordered removal is O(1) so the whole function is
563 worst-case quadratic. This could be fixed using an aside
564 bitmap to record which elements must be removed and remove
565 them all at the same time. Or by merging
566 split_non_constant_init into process_init_constructor_array,
567 that is separating constants from non-constants while building
568 the vector. */
569 VEC_ordered_remove (constructor_elt, CONSTRUCTOR_ELTS (init),
570 idx);
571 --idx;
573 if (array_type_p)
574 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
575 NULL_TREE, NULL_TREE);
576 else
577 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
578 NULL_TREE);
580 code = build2 (INIT_EXPR, inner_type, sub, value);
581 code = build_stmt (input_location, EXPR_STMT, code);
582 code = maybe_cleanup_point_expr_void (code);
583 add_stmt (code);
584 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type))
586 code = (build_special_member_call
587 (sub, complete_dtor_identifier, NULL, inner_type,
588 LOOKUP_NORMAL, tf_warning_or_error));
589 finish_eh_cleanup (code);
592 num_split_elts++;
595 break;
597 case VECTOR_TYPE:
598 if (!initializer_constant_valid_p (init, type))
600 tree code;
601 tree cons = copy_node (init);
602 CONSTRUCTOR_ELTS (init) = NULL;
603 code = build2 (MODIFY_EXPR, type, dest, cons);
604 code = build_stmt (input_location, EXPR_STMT, code);
605 add_stmt (code);
606 num_split_elts += CONSTRUCTOR_NELTS (init);
608 break;
610 default:
611 gcc_unreachable ();
614 /* The rest of the initializer is now a constant. */
615 TREE_CONSTANT (init) = 1;
616 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
617 num_split_elts, inner_type);
620 /* A subroutine of store_init_value. Splits non-constant static
621 initializer INIT into a constant part and generates code to
622 perform the non-constant part of the initialization to DEST.
623 Returns the code for the runtime init. */
625 static tree
626 split_nonconstant_init (tree dest, tree init)
628 tree code;
630 if (TREE_CODE (init) == CONSTRUCTOR)
632 code = push_stmt_list ();
633 if (split_nonconstant_init_1 (dest, init))
634 init = NULL_TREE;
635 code = pop_stmt_list (code);
636 DECL_INITIAL (dest) = init;
637 TREE_READONLY (dest) = 0;
639 else
640 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
642 return code;
645 /* Perform appropriate conversions on the initial value of a variable,
646 store it in the declaration DECL,
647 and print any error messages that are appropriate.
648 If the init is invalid, store an ERROR_MARK.
650 C++: Note that INIT might be a TREE_LIST, which would mean that it is
651 a base class initializer for some aggregate type, hopefully compatible
652 with DECL. If INIT is a single element, and DECL is an aggregate
653 type, we silently convert INIT into a TREE_LIST, allowing a constructor
654 to be called.
656 If INIT is a TREE_LIST and there is no constructor, turn INIT
657 into a CONSTRUCTOR and use standard initialization techniques.
658 Perhaps a warning should be generated?
660 Returns code to be executed if initialization could not be performed
661 for static variable. In that case, caller must emit the code. */
663 tree
664 store_init_value (tree decl, tree init, VEC(tree,gc)** cleanups, int flags)
666 tree value, type;
668 /* If variable's type was invalidly declared, just ignore it. */
670 type = TREE_TYPE (decl);
671 if (TREE_CODE (type) == ERROR_MARK)
672 return NULL_TREE;
674 if (MAYBE_CLASS_TYPE_P (type))
676 if (TREE_CODE (init) == TREE_LIST)
678 error ("constructor syntax used, but no constructor declared "
679 "for type %qT", type);
680 init = build_constructor_from_list (init_list_type_node, nreverse (init));
683 else if (TREE_CODE (init) == TREE_LIST
684 && TREE_TYPE (init) != unknown_type_node)
686 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
688 if (TREE_CODE (init) == TREE_LIST
689 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
691 error ("cannot initialize arrays using this syntax");
692 return NULL_TREE;
694 else
695 /* We get here with code like `int a (2);' */
696 init = build_x_compound_expr_from_list (init, ELK_INIT,
697 tf_warning_or_error);
700 /* End of special C++ code. */
702 if (flags & LOOKUP_ALREADY_DIGESTED)
703 value = init;
704 else
705 /* Digest the specified initializer into an expression. */
706 value = digest_init_flags (type, init, flags);
708 value = extend_ref_init_temps (decl, value, cleanups);
710 /* In C++0x constant expression is a semantic, not syntactic, property.
711 In C++98, make sure that what we thought was a constant expression at
712 template definition time is still constant. */
713 if ((cxx_dialect >= cxx0x
714 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
715 && (decl_maybe_constant_var_p (decl)
716 || TREE_STATIC (decl)))
718 bool const_init;
719 value = fold_non_dependent_expr (value);
720 value = maybe_constant_init (value);
721 if (DECL_DECLARED_CONSTEXPR_P (decl))
723 /* Diagnose a non-constant initializer for constexpr. */
724 if (processing_template_decl
725 && !require_potential_constant_expression (value))
726 value = error_mark_node;
727 else
728 value = cxx_constant_value (value);
730 const_init = (reduced_constant_expression_p (value)
731 || error_operand_p (value));
732 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
733 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
736 /* If the initializer is not a constant, fill in DECL_INITIAL with
737 the bits that are constant, and then return an expression that
738 will perform the dynamic initialization. */
739 if (value != error_mark_node
740 && (TREE_SIDE_EFFECTS (value)
741 || ! initializer_constant_valid_p (value, TREE_TYPE (value))))
743 if (TREE_CODE (type) == ARRAY_TYPE
744 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)))
745 /* For an array, we only need/want a single cleanup region rather
746 than one per element. */
747 return build_vec_init (decl, NULL_TREE, value, false, 1,
748 tf_warning_or_error);
749 else
750 return split_nonconstant_init (decl, value);
752 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
753 is an automatic variable, the middle end will turn this into a
754 dynamic initialization later. */
755 DECL_INITIAL (decl) = value;
756 return NULL_TREE;
760 /* Give errors about narrowing conversions within { }. */
762 void
763 check_narrowing (tree type, tree init)
765 tree ftype = unlowered_expr_type (init);
766 bool ok = true;
767 REAL_VALUE_TYPE d;
769 if (!warn_narrowing || !ARITHMETIC_TYPE_P (type))
770 return;
772 if (BRACE_ENCLOSED_INITIALIZER_P (init)
773 && TREE_CODE (type) == COMPLEX_TYPE)
775 tree elttype = TREE_TYPE (type);
776 check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value);
777 if (CONSTRUCTOR_NELTS (init) > 1)
778 check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value);
779 return;
782 init = maybe_constant_value (init);
784 if (TREE_CODE (type) == INTEGER_TYPE
785 && TREE_CODE (ftype) == REAL_TYPE)
786 ok = false;
787 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
788 && CP_INTEGRAL_TYPE_P (type))
790 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
791 /* Check for narrowing based on the values of the enumeration. */
792 ftype = ENUM_UNDERLYING_TYPE (ftype);
793 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
794 TYPE_MAX_VALUE (ftype))
795 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
796 TYPE_MIN_VALUE (type)))
797 && (TREE_CODE (init) != INTEGER_CST
798 || !int_fits_type_p (init, type)))
799 ok = false;
801 else if (TREE_CODE (ftype) == REAL_TYPE
802 && TREE_CODE (type) == REAL_TYPE)
804 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
806 if (TREE_CODE (init) == REAL_CST)
808 /* Issue 703: Loss of precision is OK as long as the value is
809 within the representable range of the new type. */
810 REAL_VALUE_TYPE r;
811 d = TREE_REAL_CST (init);
812 real_convert (&r, TYPE_MODE (type), &d);
813 if (real_isinf (&r))
814 ok = false;
816 else
817 ok = false;
820 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
821 && TREE_CODE (type) == REAL_TYPE)
823 ok = false;
824 if (TREE_CODE (init) == INTEGER_CST)
826 d = real_value_from_int_cst (0, init);
827 if (exact_real_truncate (TYPE_MODE (type), &d))
828 ok = true;
832 if (!ok)
834 if (cxx_dialect >= cxx0x)
835 pedwarn (EXPR_LOC_OR_HERE (init), OPT_Wnarrowing,
836 "narrowing conversion of %qE from %qT to %qT inside { }",
837 init, ftype, type);
838 else
839 warning_at (EXPR_LOC_OR_HERE (init), OPT_Wnarrowing,
840 "narrowing conversion of %qE from %qT to %qT inside { } "
841 "is ill-formed in C++11", init, ftype, type);
845 /* Process the initializer INIT for a variable of type TYPE, emitting
846 diagnostics for invalid initializers and converting the initializer as
847 appropriate.
849 For aggregate types, it assumes that reshape_init has already run, thus the
850 initializer will have the right shape (brace elision has been undone).
852 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
854 static tree
855 digest_init_r (tree type, tree init, bool nested, int flags,
856 tsubst_flags_t complain)
858 enum tree_code code = TREE_CODE (type);
860 if (error_operand_p (init))
861 return error_mark_node;
863 gcc_assert (init);
865 /* We must strip the outermost array type when completing the type,
866 because the its bounds might be incomplete at the moment. */
867 if (!complete_type_or_maybe_complain (TREE_CODE (type) == ARRAY_TYPE
868 ? TREE_TYPE (type) : type, NULL_TREE,
869 complain))
870 return error_mark_node;
872 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
873 (g++.old-deja/g++.law/casts2.C). */
874 if (TREE_CODE (init) == NON_LVALUE_EXPR)
875 init = TREE_OPERAND (init, 0);
877 /* Initialization of an array of chars from a string constant. The initializer
878 can be optionally enclosed in braces, but reshape_init has already removed
879 them if they were present. */
880 if (code == ARRAY_TYPE)
882 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
883 if (char_type_p (typ1)
884 /*&& init */
885 && TREE_CODE (init) == STRING_CST)
887 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
889 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
891 if (char_type != char_type_node)
893 if (complain & tf_error)
894 error ("char-array initialized from wide string");
895 return error_mark_node;
898 else
900 if (char_type == char_type_node)
902 if (complain & tf_error)
903 error ("int-array initialized from non-wide string");
904 return error_mark_node;
906 else if (char_type != typ1)
908 if (complain & tf_error)
909 error ("int-array initialized from incompatible "
910 "wide string");
911 return error_mark_node;
915 if (type != TREE_TYPE (init))
917 init = copy_node (init);
918 TREE_TYPE (init) = type;
920 if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
922 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
923 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
924 /* In C it is ok to subtract 1 from the length of the string
925 because it's ok to ignore the terminating null char that is
926 counted in the length of the constant, but in C++ this would
927 be invalid. */
928 if (size < TREE_STRING_LENGTH (init))
929 permerror (input_location, "initializer-string for array "
930 "of chars is too long");
932 return init;
936 /* Handle scalar types (including conversions) and references. */
937 if ((TREE_CODE (type) != COMPLEX_TYPE
938 || BRACE_ENCLOSED_INITIALIZER_P (init))
939 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
941 tree *exp;
943 if (nested)
944 check_narrowing (type, init);
945 init = convert_for_initialization (0, type, init, flags,
946 ICR_INIT, NULL_TREE, 0,
947 complain);
948 exp = &init;
950 /* Skip any conversions since we'll be outputting the underlying
951 constant. */
952 while (CONVERT_EXPR_P (*exp)
953 || TREE_CODE (*exp) == NON_LVALUE_EXPR)
954 exp = &TREE_OPERAND (*exp, 0);
956 *exp = cplus_expand_constant (*exp);
958 return init;
961 /* Come here only for aggregates: records, arrays, unions, complex numbers
962 and vectors. */
963 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
964 || TREE_CODE (type) == VECTOR_TYPE
965 || TREE_CODE (type) == RECORD_TYPE
966 || TREE_CODE (type) == UNION_TYPE
967 || TREE_CODE (type) == COMPLEX_TYPE);
969 if (BRACE_ENCLOSED_INITIALIZER_P (init)
970 && !TYPE_NON_AGGREGATE_CLASS (type))
971 return process_init_constructor (type, init, complain);
972 else
974 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
976 if (complain & tf_error)
977 error ("cannot initialize aggregate of type %qT with "
978 "a compound literal", type);
980 return error_mark_node;
983 if (TREE_CODE (type) == ARRAY_TYPE
984 && !BRACE_ENCLOSED_INITIALIZER_P (init))
986 /* Allow the result of build_array_copy and of
987 build_value_init_noctor. */
988 if ((TREE_CODE (init) == VEC_INIT_EXPR
989 || TREE_CODE (init) == CONSTRUCTOR)
990 && (same_type_ignoring_top_level_qualifiers_p
991 (type, TREE_TYPE (init))))
992 return init;
994 if (complain & tf_error)
995 error ("array must be initialized with a brace-enclosed"
996 " initializer");
997 return error_mark_node;
1000 return convert_for_initialization (NULL_TREE, type, init,
1001 flags,
1002 ICR_INIT, NULL_TREE, 0,
1003 complain);
1007 tree
1008 digest_init (tree type, tree init, tsubst_flags_t complain)
1010 return digest_init_r (type, init, false, LOOKUP_IMPLICIT, complain);
1013 tree
1014 digest_init_flags (tree type, tree init, int flags)
1016 return digest_init_r (type, init, false, flags, tf_warning_or_error);
1019 /* Set of flags used within process_init_constructor to describe the
1020 initializers. */
1021 #define PICFLAG_ERRONEOUS 1
1022 #define PICFLAG_NOT_ALL_CONSTANT 2
1023 #define PICFLAG_NOT_ALL_SIMPLE 4
1025 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1026 describe it. */
1028 static int
1029 picflag_from_initializer (tree init)
1031 if (init == error_mark_node)
1032 return PICFLAG_ERRONEOUS;
1033 else if (!TREE_CONSTANT (init))
1034 return PICFLAG_NOT_ALL_CONSTANT;
1035 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1036 return PICFLAG_NOT_ALL_SIMPLE;
1037 return 0;
1040 /* Subroutine of process_init_constructor, which will process an initializer
1041 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1042 which describe the initializers. */
1044 static int
1045 process_init_constructor_array (tree type, tree init,
1046 tsubst_flags_t complain)
1048 unsigned HOST_WIDE_INT i, len = 0;
1049 int flags = 0;
1050 bool unbounded = false;
1051 constructor_elt *ce;
1052 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (init);
1054 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1055 || TREE_CODE (type) == VECTOR_TYPE);
1057 if (TREE_CODE (type) == ARRAY_TYPE)
1059 tree domain = TYPE_DOMAIN (type);
1060 if (domain)
1061 len = (tree_to_double_int (TYPE_MAX_VALUE (domain))
1062 - tree_to_double_int (TYPE_MIN_VALUE (domain))
1063 + double_int_one)
1064 .ext (TYPE_PRECISION (TREE_TYPE (domain)),
1065 TYPE_UNSIGNED (TREE_TYPE (domain)))
1066 .low;
1067 else
1068 unbounded = true; /* Take as many as there are. */
1070 else
1071 /* Vectors are like simple fixed-size arrays. */
1072 len = TYPE_VECTOR_SUBPARTS (type);
1074 /* There must not be more initializers than needed. */
1075 if (!unbounded && VEC_length (constructor_elt, v) > len)
1077 if (complain & tf_error)
1078 error ("too many initializers for %qT", type);
1079 else
1080 return PICFLAG_ERRONEOUS;
1083 FOR_EACH_VEC_ELT (constructor_elt, v, i, ce)
1085 if (ce->index)
1087 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1088 if (compare_tree_int (ce->index, i) != 0)
1090 ce->value = error_mark_node;
1091 sorry ("non-trivial designated initializers not supported");
1094 else
1095 ce->index = size_int (i);
1096 gcc_assert (ce->value);
1097 ce->value = digest_init_r (TREE_TYPE (type), ce->value, true,
1098 LOOKUP_IMPLICIT, complain);
1100 if (ce->value != error_mark_node)
1101 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1102 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1104 flags |= picflag_from_initializer (ce->value);
1107 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1108 we must add initializers ourselves. */
1109 if (!unbounded)
1110 for (; i < len; ++i)
1112 tree next;
1114 if (type_build_ctor_call (TREE_TYPE (type)))
1116 /* If this type needs constructors run for default-initialization,
1117 we can't rely on the back end to do it for us, so make the
1118 initialization explicit by list-initializing from {}. */
1119 next = build_constructor (init_list_type_node, NULL);
1120 next = digest_init (TREE_TYPE (type), next, complain);
1122 else if (!zero_init_p (TREE_TYPE (type)))
1123 next = build_zero_init (TREE_TYPE (type),
1124 /*nelts=*/NULL_TREE,
1125 /*static_storage_p=*/false);
1126 else
1127 /* The default zero-initialization is fine for us; don't
1128 add anything to the CONSTRUCTOR. */
1129 break;
1131 flags |= picflag_from_initializer (next);
1132 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1135 CONSTRUCTOR_ELTS (init) = v;
1136 return flags;
1139 /* Subroutine of process_init_constructor, which will process an initializer
1140 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1141 the initializers. */
1143 static int
1144 process_init_constructor_record (tree type, tree init,
1145 tsubst_flags_t complain)
1147 VEC(constructor_elt,gc) *v = NULL;
1148 int flags = 0;
1149 tree field;
1150 unsigned HOST_WIDE_INT idx = 0;
1152 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1153 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1154 gcc_assert (!TYPE_BINFO (type)
1155 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1156 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1158 /* Generally, we will always have an index for each initializer (which is
1159 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1160 reshape_init. So we need to handle both cases. */
1161 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1163 tree next;
1164 tree type;
1166 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1168 flags |= picflag_from_initializer (integer_zero_node);
1169 CONSTRUCTOR_APPEND_ELT (v, field, integer_zero_node);
1170 continue;
1173 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1174 continue;
1176 /* If this is a bitfield, first convert to the declared type. */
1177 type = TREE_TYPE (field);
1178 if (DECL_BIT_FIELD_TYPE (field))
1179 type = DECL_BIT_FIELD_TYPE (field);
1181 if (idx < VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)))
1183 constructor_elt *ce = &VEC_index (constructor_elt,
1184 CONSTRUCTOR_ELTS (init), idx);
1185 if (ce->index)
1187 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1188 latter case can happen in templates where lookup has to be
1189 deferred. */
1190 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1191 || TREE_CODE (ce->index) == IDENTIFIER_NODE);
1192 if (ce->index != field
1193 && ce->index != DECL_NAME (field))
1195 ce->value = error_mark_node;
1196 sorry ("non-trivial designated initializers not supported");
1200 gcc_assert (ce->value);
1201 next = digest_init_r (type, ce->value, true,
1202 LOOKUP_IMPLICIT, complain);
1203 ++idx;
1205 else if (type_build_ctor_call (TREE_TYPE (field)))
1207 /* If this type needs constructors run for
1208 default-initialization, we can't rely on the back end to do it
1209 for us, so build up TARGET_EXPRs. If the type in question is
1210 a class, just build one up; if it's an array, recurse. */
1211 next = build_constructor (init_list_type_node, NULL);
1212 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field)))
1214 next = finish_compound_literal (TREE_TYPE (field), next,
1215 complain);
1216 /* direct-initialize the target. No temporary is going
1217 to be involved. */
1218 if (TREE_CODE (next) == TARGET_EXPR)
1219 TARGET_EXPR_DIRECT_INIT_P (next) = true;
1222 next = digest_init_r (TREE_TYPE (field), next, true,
1223 LOOKUP_IMPLICIT, complain);
1225 /* Warn when some struct elements are implicitly initialized. */
1226 warning (OPT_Wmissing_field_initializers,
1227 "missing initializer for member %qD", field);
1229 else
1231 if (TREE_READONLY (field))
1233 if (complain & tf_error)
1234 error ("uninitialized const member %qD", field);
1235 else
1236 return PICFLAG_ERRONEOUS;
1238 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1240 if (complain & tf_error)
1241 error ("member %qD with uninitialized const fields", field);
1242 else
1243 return PICFLAG_ERRONEOUS;
1245 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1247 if (complain & tf_error)
1248 error ("member %qD is uninitialized reference", field);
1249 else
1250 return PICFLAG_ERRONEOUS;
1253 /* Warn when some struct elements are implicitly initialized
1254 to zero. */
1255 warning (OPT_Wmissing_field_initializers,
1256 "missing initializer for member %qD", field);
1258 if (!zero_init_p (TREE_TYPE (field)))
1259 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1260 /*static_storage_p=*/false);
1261 else
1262 /* The default zero-initialization is fine for us; don't
1263 add anything to the CONSTRUCTOR. */
1264 continue;
1267 /* If this is a bitfield, now convert to the lowered type. */
1268 if (type != TREE_TYPE (field))
1269 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1270 flags |= picflag_from_initializer (next);
1271 CONSTRUCTOR_APPEND_ELT (v, field, next);
1274 if (idx < VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)))
1276 if (complain & tf_error)
1277 error ("too many initializers for %qT", type);
1278 else
1279 return PICFLAG_ERRONEOUS;
1282 CONSTRUCTOR_ELTS (init) = v;
1283 return flags;
1286 /* Subroutine of process_init_constructor, which will process a single
1287 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1288 which describe the initializer. */
1290 static int
1291 process_init_constructor_union (tree type, tree init,
1292 tsubst_flags_t complain)
1294 constructor_elt *ce;
1295 int len;
1297 /* If the initializer was empty, use default zero initialization. */
1298 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
1299 return 0;
1301 len = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init));
1302 if (len > 1)
1304 if (!(complain & tf_error))
1305 return PICFLAG_ERRONEOUS;
1306 error ("too many initializers for %qT", type);
1307 VEC_block_remove (constructor_elt, CONSTRUCTOR_ELTS (init), 1, len-1);
1310 ce = &VEC_index (constructor_elt, CONSTRUCTOR_ELTS (init), 0);
1312 /* If this element specifies a field, initialize via that field. */
1313 if (ce->index)
1315 if (TREE_CODE (ce->index) == FIELD_DECL)
1317 else if (TREE_CODE (ce->index) == IDENTIFIER_NODE)
1319 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1320 tree name = ce->index;
1321 tree field;
1322 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1323 if (DECL_NAME (field) == name)
1324 break;
1325 if (!field)
1327 if (complain & tf_error)
1328 error ("no field %qD found in union being initialized",
1329 field);
1330 ce->value = error_mark_node;
1332 ce->index = field;
1334 else
1336 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1337 || TREE_CODE (ce->index) == RANGE_EXPR);
1338 if (complain & tf_error)
1339 error ("index value instead of field name in union initializer");
1340 ce->value = error_mark_node;
1343 else
1345 /* Find the first named field. ANSI decided in September 1990
1346 that only named fields count here. */
1347 tree field = TYPE_FIELDS (type);
1348 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1349 field = TREE_CHAIN (field);
1350 if (field == NULL_TREE)
1352 if (complain & tf_error)
1353 error ("too many initializers for %qT", type);
1354 ce->value = error_mark_node;
1356 ce->index = field;
1359 if (ce->value && ce->value != error_mark_node)
1360 ce->value = digest_init_r (TREE_TYPE (ce->index), ce->value,
1361 true, LOOKUP_IMPLICIT, complain);
1363 return picflag_from_initializer (ce->value);
1366 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1367 constructor is a brace-enclosed initializer, and will be modified in-place.
1369 Each element is converted to the right type through digest_init, and
1370 missing initializers are added following the language rules (zero-padding,
1371 etc.).
1373 After the execution, the initializer will have TREE_CONSTANT if all elts are
1374 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1375 constants that the assembler and linker can compute them.
1377 The function returns the initializer itself, or error_mark_node in case
1378 of error. */
1380 static tree
1381 process_init_constructor (tree type, tree init, tsubst_flags_t complain)
1383 int flags;
1385 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1387 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1388 flags = process_init_constructor_array (type, init, complain);
1389 else if (TREE_CODE (type) == RECORD_TYPE)
1390 flags = process_init_constructor_record (type, init, complain);
1391 else if (TREE_CODE (type) == UNION_TYPE)
1392 flags = process_init_constructor_union (type, init, complain);
1393 else
1394 gcc_unreachable ();
1396 if (flags & PICFLAG_ERRONEOUS)
1397 return error_mark_node;
1399 TREE_TYPE (init) = type;
1400 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1401 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1402 if (flags & PICFLAG_NOT_ALL_CONSTANT)
1403 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1404 TREE_CONSTANT (init) = false;
1405 else
1407 TREE_CONSTANT (init) = 1;
1408 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1409 TREE_STATIC (init) = 1;
1411 return init;
1414 /* Given a structure or union value DATUM, construct and return
1415 the structure or union component which results from narrowing
1416 that value to the base specified in BASETYPE. For example, given the
1417 hierarchy
1419 class L { int ii; };
1420 class A : L { ... };
1421 class B : L { ... };
1422 class C : A, B { ... };
1424 and the declaration
1426 C x;
1428 then the expression
1430 x.A::ii refers to the ii member of the L part of
1431 the A part of the C object named by X. In this case,
1432 DATUM would be x, and BASETYPE would be A.
1434 I used to think that this was nonconformant, that the standard specified
1435 that first we look up ii in A, then convert x to an L& and pull out the
1436 ii part. But in fact, it does say that we convert x to an A&; A here
1437 is known as the "naming class". (jason 2000-12-19)
1439 BINFO_P points to a variable initialized either to NULL_TREE or to the
1440 binfo for the specific base subobject we want to convert to. */
1442 tree
1443 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1445 tree binfo;
1447 if (datum == error_mark_node)
1448 return error_mark_node;
1449 if (*binfo_p)
1450 binfo = *binfo_p;
1451 else
1452 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1453 NULL, tf_warning_or_error);
1455 if (!binfo || binfo == error_mark_node)
1457 *binfo_p = NULL_TREE;
1458 if (!binfo)
1459 error_not_base_type (basetype, TREE_TYPE (datum));
1460 return error_mark_node;
1463 *binfo_p = binfo;
1464 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1465 tf_warning_or_error);
1468 /* Build a reference to an object specified by the C++ `->' operator.
1469 Usually this just involves dereferencing the object, but if the
1470 `->' operator is overloaded, then such overloads must be
1471 performed until an object which does not have the `->' operator
1472 overloaded is found. An error is reported when circular pointer
1473 delegation is detected. */
1475 tree
1476 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1478 tree orig_expr = expr;
1479 tree type = TREE_TYPE (expr);
1480 tree last_rval = NULL_TREE;
1481 VEC(tree,gc) *types_memoized = NULL;
1483 if (type == error_mark_node)
1484 return error_mark_node;
1486 if (processing_template_decl)
1488 if (type_dependent_expression_p (expr))
1489 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1490 expr = build_non_dependent_expr (expr);
1493 if (MAYBE_CLASS_TYPE_P (type))
1495 struct tinst_level *actual_inst = current_instantiation ();
1496 tree fn = NULL;
1498 while ((expr = build_new_op (loc, COMPONENT_REF,
1499 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1500 &fn, complain)))
1502 if (expr == error_mark_node)
1503 return error_mark_node;
1505 if (fn && DECL_USE_TEMPLATE (fn))
1506 push_tinst_level (fn);
1507 fn = NULL;
1509 if (vec_member (TREE_TYPE (expr), types_memoized))
1511 if (complain & tf_error)
1512 error ("circular pointer delegation detected");
1513 return error_mark_node;
1516 VEC_safe_push (tree, gc, types_memoized, TREE_TYPE (expr));
1517 last_rval = expr;
1520 while (current_instantiation () != actual_inst)
1521 pop_tinst_level ();
1523 if (last_rval == NULL_TREE)
1525 if (complain & tf_error)
1526 error ("base operand of %<->%> has non-pointer type %qT", type);
1527 return error_mark_node;
1530 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1531 last_rval = convert_from_reference (last_rval);
1533 else
1534 last_rval = decay_conversion (expr, complain);
1536 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1538 if (processing_template_decl)
1540 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1541 orig_expr);
1542 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1543 return expr;
1546 return cp_build_indirect_ref (last_rval, RO_NULL, complain);
1549 if (complain & tf_error)
1551 if (types_memoized)
1552 error ("result of %<operator->()%> yields non-pointer result");
1553 else
1554 error ("base operand of %<->%> is not a pointer");
1556 return error_mark_node;
1559 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1560 already been checked out to be of aggregate type. */
1562 tree
1563 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1565 tree ptrmem_type;
1566 tree objtype;
1567 tree type;
1568 tree binfo;
1569 tree ctype;
1571 if (error_operand_p (datum) || error_operand_p (component))
1572 return error_mark_node;
1574 datum = mark_lvalue_use (datum);
1575 component = mark_rvalue_use (component);
1577 ptrmem_type = TREE_TYPE (component);
1578 if (!TYPE_PTRMEM_P (ptrmem_type))
1580 if (complain & tf_error)
1581 error ("%qE cannot be used as a member pointer, since it is of "
1582 "type %qT", component, ptrmem_type);
1583 return error_mark_node;
1586 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1587 if (! MAYBE_CLASS_TYPE_P (objtype))
1589 if (complain & tf_error)
1590 error ("cannot apply member pointer %qE to %qE, which is of "
1591 "non-class type %qT", component, datum, objtype);
1592 return error_mark_node;
1595 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1596 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1598 if (!COMPLETE_TYPE_P (ctype))
1600 if (!same_type_p (ctype, objtype))
1601 goto mismatch;
1602 binfo = NULL;
1604 else
1606 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1608 if (!binfo)
1610 mismatch:
1611 if (complain & tf_error)
1612 error ("pointer to member type %qT incompatible with object "
1613 "type %qT", type, objtype);
1614 return error_mark_node;
1616 else if (binfo == error_mark_node)
1617 return error_mark_node;
1620 if (TYPE_PTRDATAMEM_P (ptrmem_type))
1622 bool is_lval = real_lvalue_p (datum);
1623 tree ptype;
1625 /* Compute the type of the field, as described in [expr.ref].
1626 There's no such thing as a mutable pointer-to-member, so
1627 things are not as complex as they are for references to
1628 non-static data members. */
1629 type = cp_build_qualified_type (type,
1630 (cp_type_quals (type)
1631 | cp_type_quals (TREE_TYPE (datum))));
1633 datum = build_address (datum);
1635 /* Convert object to the correct base. */
1636 if (binfo)
1638 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
1639 if (datum == error_mark_node)
1640 return error_mark_node;
1643 /* Build an expression for "object + offset" where offset is the
1644 value stored in the pointer-to-data-member. */
1645 ptype = build_pointer_type (type);
1646 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
1647 datum = cp_build_indirect_ref (datum, RO_NULL, complain);
1648 if (datum == error_mark_node)
1649 return error_mark_node;
1651 /* If the object expression was an rvalue, return an rvalue. */
1652 if (!is_lval)
1653 datum = move (datum);
1654 return datum;
1656 else
1657 return build2 (OFFSET_REF, type, datum, component);
1660 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1662 tree
1663 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1665 /* This is either a call to a constructor,
1666 or a C cast in C++'s `functional' notation. */
1668 /* The type to which we are casting. */
1669 tree type;
1670 VEC(tree,gc) *parmvec;
1672 if (exp == error_mark_node || parms == error_mark_node)
1673 return error_mark_node;
1675 if (TREE_CODE (exp) == TYPE_DECL)
1676 type = TREE_TYPE (exp);
1677 else
1678 type = exp;
1680 /* We need to check this explicitly, since value-initialization of
1681 arrays is allowed in other situations. */
1682 if (TREE_CODE (type) == ARRAY_TYPE)
1684 if (complain & tf_error)
1685 error ("functional cast to array type %qT", type);
1686 return error_mark_node;
1689 if (type_uses_auto (type))
1691 if (complain & tf_error)
1692 error ("invalid use of %<auto%>");
1693 return error_mark_node;
1696 if (processing_template_decl)
1698 tree t;
1700 /* Diagnose this even in a template. We could also try harder
1701 to give all the usual errors when the type and args are
1702 non-dependent... */
1703 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1705 if (complain & tf_error)
1706 error ("invalid value-initialization of reference type");
1707 return error_mark_node;
1710 t = build_min (CAST_EXPR, type, parms);
1711 /* We don't know if it will or will not have side effects. */
1712 TREE_SIDE_EFFECTS (t) = 1;
1713 return t;
1716 if (! MAYBE_CLASS_TYPE_P (type))
1718 if (parms == NULL_TREE)
1720 if (VOID_TYPE_P (type))
1721 return void_zero_node;
1722 return build_value_init (cv_unqualified (type), complain);
1725 /* This must build a C cast. */
1726 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1727 return cp_build_c_cast (type, parms, complain);
1730 /* Prepare to evaluate as a call to a constructor. If this expression
1731 is actually used, for example,
1733 return X (arg1, arg2, ...);
1735 then the slot being initialized will be filled in. */
1737 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1738 return error_mark_node;
1739 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
1740 return error_mark_node;
1742 /* [expr.type.conv]
1744 If the expression list is a single-expression, the type
1745 conversion is equivalent (in definedness, and if defined in
1746 meaning) to the corresponding cast expression. */
1747 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1748 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1750 /* [expr.type.conv]
1752 The expression T(), where T is a simple-type-specifier for a
1753 non-array complete object type or the (possibly cv-qualified)
1754 void type, creates an rvalue of the specified type, which is
1755 value-initialized. */
1757 if (parms == NULL_TREE)
1759 exp = build_value_init (type, complain);
1760 exp = get_target_expr_sfinae (exp, complain);
1761 return exp;
1764 /* Call the constructor. */
1765 parmvec = make_tree_vector ();
1766 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
1767 VEC_safe_push (tree, gc, parmvec, TREE_VALUE (parms));
1768 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
1769 &parmvec, type, LOOKUP_NORMAL, complain);
1770 release_tree_vector (parmvec);
1772 if (exp == error_mark_node)
1773 return error_mark_node;
1775 return build_cplus_new (type, exp, complain);
1779 /* Add new exception specifier SPEC, to the LIST we currently have.
1780 If it's already in LIST then do nothing.
1781 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1782 know what we're doing. */
1784 tree
1785 add_exception_specifier (tree list, tree spec, int complain)
1787 bool ok;
1788 tree core = spec;
1789 bool is_ptr;
1790 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
1792 if (spec == error_mark_node)
1793 return list;
1795 gcc_assert (spec && (!list || TREE_VALUE (list)));
1797 /* [except.spec] 1, type in an exception specifier shall not be
1798 incomplete, or pointer or ref to incomplete other than pointer
1799 to cv void. */
1800 is_ptr = TREE_CODE (core) == POINTER_TYPE;
1801 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1802 core = TREE_TYPE (core);
1803 if (complain < 0)
1804 ok = true;
1805 else if (VOID_TYPE_P (core))
1806 ok = is_ptr;
1807 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1808 ok = true;
1809 else if (processing_template_decl)
1810 ok = true;
1811 else
1813 ok = true;
1814 /* 15.4/1 says that types in an exception specifier must be complete,
1815 but it seems more reasonable to only require this on definitions
1816 and calls. So just give a pedwarn at this point; we will give an
1817 error later if we hit one of those two cases. */
1818 if (!COMPLETE_TYPE_P (complete_type (core)))
1819 diag_type = DK_PEDWARN; /* pedwarn */
1822 if (ok)
1824 tree probe;
1826 for (probe = list; probe; probe = TREE_CHAIN (probe))
1827 if (same_type_p (TREE_VALUE (probe), spec))
1828 break;
1829 if (!probe)
1830 list = tree_cons (NULL_TREE, spec, list);
1832 else
1833 diag_type = DK_ERROR; /* error */
1835 if (diag_type != DK_UNSPECIFIED
1836 && (complain & tf_warning_or_error))
1837 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1839 return list;
1842 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
1844 static bool
1845 nothrow_spec_p_uninst (const_tree spec)
1847 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
1848 return false;
1849 return nothrow_spec_p (spec);
1852 /* Combine the two exceptions specifier lists LIST and ADD, and return
1853 their union. If FN is non-null, it's the source of ADD. */
1855 tree
1856 merge_exception_specifiers (tree list, tree add, tree fn)
1858 tree noex, orig_list;
1860 /* No exception-specifier or noexcept(false) are less strict than
1861 anything else. Prefer the newer variant (LIST). */
1862 if (!list || list == noexcept_false_spec)
1863 return list;
1864 else if (!add || add == noexcept_false_spec)
1865 return add;
1867 /* noexcept(true) and throw() are stricter than anything else.
1868 As above, prefer the more recent one (LIST). */
1869 if (nothrow_spec_p_uninst (add))
1870 return list;
1872 noex = TREE_PURPOSE (list);
1873 if (DEFERRED_NOEXCEPT_SPEC_P (add))
1875 /* If ADD is a deferred noexcept, we must have been called from
1876 process_subob_fn. For implicitly declared functions, we build up
1877 a list of functions to consider at instantiation time. */
1878 if (noex == boolean_true_node)
1879 noex = NULL_TREE;
1880 gcc_assert (fn && (!noex || is_overloaded_fn (noex)));
1881 noex = build_overload (fn, noex);
1883 else if (nothrow_spec_p_uninst (list))
1884 return add;
1885 else
1886 gcc_checking_assert (!TREE_PURPOSE (add)
1887 || cp_tree_equal (noex, TREE_PURPOSE (add)));
1889 /* Combine the dynamic-exception-specifiers, if any. */
1890 orig_list = list;
1891 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
1893 tree spec = TREE_VALUE (add);
1894 tree probe;
1896 for (probe = orig_list; probe && TREE_VALUE (probe);
1897 probe = TREE_CHAIN (probe))
1898 if (same_type_p (TREE_VALUE (probe), spec))
1899 break;
1900 if (!probe)
1902 spec = build_tree_list (NULL_TREE, spec);
1903 TREE_CHAIN (spec) = list;
1904 list = spec;
1908 /* Keep the noexcept-specifier at the beginning of the list. */
1909 if (noex != TREE_PURPOSE (list))
1910 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
1912 return list;
1915 /* Subroutine of build_call. Ensure that each of the types in the
1916 exception specification is complete. Technically, 15.4/1 says that
1917 they need to be complete when we see a declaration of the function,
1918 but we should be able to get away with only requiring this when the
1919 function is defined or called. See also add_exception_specifier. */
1921 void
1922 require_complete_eh_spec_types (tree fntype, tree decl)
1924 tree raises;
1925 /* Don't complain about calls to op new. */
1926 if (decl && DECL_ARTIFICIAL (decl))
1927 return;
1928 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1929 raises = TREE_CHAIN (raises))
1931 tree type = TREE_VALUE (raises);
1932 if (type && !COMPLETE_TYPE_P (type))
1934 if (decl)
1935 error
1936 ("call to function %qD which throws incomplete type %q#T",
1937 decl, type);
1938 else
1939 error ("call to function which throws incomplete type %q#T",
1940 decl);
1946 #include "gt-cp-typeck2.h"