]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/typeck2.c
c-typeck.c (readonly_error): Delete.
[gcc.git] / gcc / cp / typeck2.c
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
5 Free Software Foundation, Inc.
6 Hacked by Michael Tiemann (tiemann@cygnus.com)
7
8 This file is part of GCC.
9
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 3, or (at your option)
13 any later version.
14
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.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
23
24
25 /* This file is part of the C++ front end.
26 It contains routines to build C++ expressions given their operands,
27 including computing the types of the result, C and C++ specific error
28 checks, and some optimization. */
29
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "tree.h"
35 #include "intl.h"
36 #include "cp-tree.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "diagnostic-core.h"
40
41 static tree
42 process_init_constructor (tree type, tree init);
43
44
45 /* Print an error message stemming from an attempt to use
46 BASETYPE as a base class for TYPE. */
47
48 tree
49 error_not_base_type (tree basetype, tree type)
50 {
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;
55 }
56
57 tree
58 binfo_or_else (tree base, tree type)
59 {
60 tree binfo = lookup_base (type, base, ba_unique, NULL);
61
62 if (binfo == error_mark_node)
63 return NULL_TREE;
64 else if (!binfo)
65 error_not_base_type (base, type);
66 return binfo;
67 }
68
69 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
70 value may not be changed thereafter. */
71
72 void
73 cxx_readonly_error (tree arg, enum lvalue_use errstring)
74 {
75
76 /* This macro is used to emit diagnostics to ensure that all format
77 strings are complete sentences, visible to gettext and checked at
78 compile time. */
79
80 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
81 do { \
82 switch (errstring) \
83 { \
84 case lv_assign: \
85 error(AS, ARG); \
86 break; \
87 case lv_asm: \
88 error(ASM, ARG); \
89 break; \
90 case lv_increment: \
91 error (IN, ARG); \
92 break; \
93 case lv_decrement: \
94 error (DE, ARG); \
95 break; \
96 default: \
97 gcc_unreachable (); \
98 } \
99 } while (0)
100
101 /* Handle C++-specific things first. */
102
103 if (TREE_CODE (arg) == VAR_DECL
104 && DECL_LANG_SPECIFIC (arg)
105 && DECL_IN_AGGR_P (arg)
106 && !TREE_STATIC (arg))
107 ERROR_FOR_ASSIGNMENT (G_("assignment of "
108 "constant field %qD"),
109 G_("constant field %qD "
110 "used as %<asm%> output"),
111 G_("increment of "
112 "constant field %qD"),
113 G_("decrement of "
114 "constant field %qD"),
115 arg);
116 else if (TREE_CODE (arg) == INDIRECT_REF
117 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
118 && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
119 || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
120 ERROR_FOR_ASSIGNMENT (G_("assignment of "
121 "read-only reference %qD"),
122 G_("read-only reference %qD "
123 "used as %<asm%> output"),
124 G_("increment of "
125 "read-only reference %qD"),
126 G_("decrement of "
127 "read-only reference %qD"),
128 TREE_OPERAND (arg, 0));
129 else
130 readonly_error (arg, errstring);
131 }
132
133 \f
134 /* Structure that holds information about declarations whose type was
135 incomplete and we could not check whether it was abstract or not. */
136
137 struct GTY((chain_next ("%h.next"))) pending_abstract_type {
138 /* Declaration which we are checking for abstractness. It is either
139 a DECL node, or an IDENTIFIER_NODE if we do not have a full
140 declaration available. */
141 tree decl;
142
143 /* Type which will be checked for abstractness. */
144 tree type;
145
146 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
147 because DECLs already carry locus information. */
148 location_t locus;
149
150 /* Link to the next element in list. */
151 struct pending_abstract_type* next;
152 };
153
154
155 /* Compute the hash value of the node VAL. This function is used by the
156 hash table abstract_pending_vars. */
157
158 static hashval_t
159 pat_calc_hash (const void* val)
160 {
161 const struct pending_abstract_type *pat =
162 (const struct pending_abstract_type *) val;
163 return (hashval_t) TYPE_UID (pat->type);
164 }
165
166
167 /* Compare node VAL1 with the type VAL2. This function is used by the
168 hash table abstract_pending_vars. */
169
170 static int
171 pat_compare (const void* val1, const void* val2)
172 {
173 const struct pending_abstract_type *const pat1 =
174 (const struct pending_abstract_type *) val1;
175 const_tree const type2 = (const_tree)val2;
176
177 return (pat1->type == type2);
178 }
179
180 /* Hash table that maintains pending_abstract_type nodes, for which we still
181 need to check for type abstractness. The key of the table is the type
182 of the declaration. */
183 static GTY ((param_is (struct pending_abstract_type)))
184 htab_t abstract_pending_vars = NULL;
185
186
187 /* This function is called after TYPE is completed, and will check if there
188 are pending declarations for which we still need to verify the abstractness
189 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
190 turned out to be incomplete. */
191
192 void
193 complete_type_check_abstract (tree type)
194 {
195 void **slot;
196 struct pending_abstract_type *pat;
197 location_t cur_loc = input_location;
198
199 gcc_assert (COMPLETE_TYPE_P (type));
200
201 if (!abstract_pending_vars)
202 return;
203
204 /* Retrieve the list of pending declarations for this type. */
205 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
206 (hashval_t)TYPE_UID (type), NO_INSERT);
207 if (!slot)
208 return;
209 pat = (struct pending_abstract_type*)*slot;
210 gcc_assert (pat);
211
212 /* If the type is not abstract, do not do anything. */
213 if (CLASSTYPE_PURE_VIRTUALS (type))
214 {
215 struct pending_abstract_type *prev = 0, *next;
216
217 /* Reverse the list to emit the errors in top-down order. */
218 for (; pat; pat = next)
219 {
220 next = pat->next;
221 pat->next = prev;
222 prev = pat;
223 }
224 pat = prev;
225
226 /* Go through the list, and call abstract_virtuals_error for each
227 element: it will issue a diagnostic if the type is abstract. */
228 while (pat)
229 {
230 gcc_assert (type == pat->type);
231
232 /* Tweak input_location so that the diagnostic appears at the correct
233 location. Notice that this is only needed if the decl is an
234 IDENTIFIER_NODE. */
235 input_location = pat->locus;
236 abstract_virtuals_error (pat->decl, pat->type);
237 pat = pat->next;
238 }
239 }
240
241 htab_clear_slot (abstract_pending_vars, slot);
242
243 input_location = cur_loc;
244 }
245
246
247 /* If TYPE has abstract virtual functions, issue an error about trying
248 to create an object of that type. DECL is the object declared, or
249 NULL_TREE if the declaration is unavailable. Returns 1 if an error
250 occurred; zero if all was well. */
251
252 int
253 abstract_virtuals_error (tree decl, tree type)
254 {
255 VEC(tree,gc) *pure;
256
257 /* This function applies only to classes. Any other entity can never
258 be abstract. */
259 if (!CLASS_TYPE_P (type))
260 return 0;
261 type = TYPE_MAIN_VARIANT (type);
262
263 /* If the type is incomplete, we register it within a hash table,
264 so that we can check again once it is completed. This makes sense
265 only for objects for which we have a declaration or at least a
266 name. */
267 if (!COMPLETE_TYPE_P (type))
268 {
269 void **slot;
270 struct pending_abstract_type *pat;
271
272 gcc_assert (!decl || DECL_P (decl)
273 || TREE_CODE (decl) == IDENTIFIER_NODE);
274
275 if (!abstract_pending_vars)
276 abstract_pending_vars = htab_create_ggc (31, &pat_calc_hash,
277 &pat_compare, NULL);
278
279 slot = htab_find_slot_with_hash (abstract_pending_vars, type,
280 (hashval_t)TYPE_UID (type), INSERT);
281
282 pat = ggc_alloc_pending_abstract_type ();
283 pat->type = type;
284 pat->decl = decl;
285 pat->locus = ((decl && DECL_P (decl))
286 ? DECL_SOURCE_LOCATION (decl)
287 : input_location);
288
289 pat->next = (struct pending_abstract_type *) *slot;
290 *slot = pat;
291
292 return 0;
293 }
294
295 if (!TYPE_SIZE (type))
296 /* TYPE is being defined, and during that time
297 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
298 return 0;
299
300 pure = CLASSTYPE_PURE_VIRTUALS (type);
301 if (!pure)
302 return 0;
303
304 if (decl)
305 {
306 if (TREE_CODE (decl) == RESULT_DECL)
307 return 0;
308
309 if (TREE_CODE (decl) == VAR_DECL)
310 error ("cannot declare variable %q+D to be of abstract "
311 "type %qT", decl, type);
312 else if (TREE_CODE (decl) == PARM_DECL)
313 error ("cannot declare parameter %q+D to be of abstract type %qT",
314 decl, type);
315 else if (TREE_CODE (decl) == FIELD_DECL)
316 error ("cannot declare field %q+D to be of abstract type %qT",
317 decl, type);
318 else if (TREE_CODE (decl) == FUNCTION_DECL
319 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
320 error ("invalid abstract return type for member function %q+#D", decl);
321 else if (TREE_CODE (decl) == FUNCTION_DECL)
322 error ("invalid abstract return type for function %q+#D", decl);
323 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
324 /* Here we do not have location information. */
325 error ("invalid abstract type %qT for %qE", type, decl);
326 else
327 error ("invalid abstract type for %q+D", decl);
328 }
329 else
330 error ("cannot allocate an object of abstract type %qT", type);
331
332 /* Only go through this once. */
333 if (VEC_length (tree, pure))
334 {
335 unsigned ix;
336 tree fn;
337
338 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
339 " because the following virtual functions are pure within %qT:",
340 type);
341
342 FOR_EACH_VEC_ELT (tree, pure, ix, fn)
343 inform (input_location, "\t%+#D", fn);
344 /* Now truncate the vector. This leaves it non-null, so we know
345 there are pure virtuals, but empty so we don't list them out
346 again. */
347 VEC_truncate (tree, pure, 0);
348 }
349 else
350 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
351 " since type %qT has pure virtual functions",
352 type);
353
354 return 1;
355 }
356
357 /* Print an error message for invalid use of an incomplete type.
358 VALUE is the expression that was used (or 0 if that isn't known)
359 and TYPE is the type that was invalid. DIAG_KIND indicates the
360 type of diagnostic (see diagnostic.def). */
361
362 void
363 cxx_incomplete_type_diagnostic (const_tree value, const_tree type,
364 diagnostic_t diag_kind)
365 {
366 int decl = 0;
367
368 gcc_assert (diag_kind == DK_WARNING
369 || diag_kind == DK_PEDWARN
370 || diag_kind == DK_ERROR);
371
372 /* Avoid duplicate error message. */
373 if (TREE_CODE (type) == ERROR_MARK)
374 return;
375
376 if (value != 0 && (TREE_CODE (value) == VAR_DECL
377 || TREE_CODE (value) == PARM_DECL
378 || TREE_CODE (value) == FIELD_DECL))
379 {
380 emit_diagnostic (diag_kind, input_location, 0,
381 "%q+D has incomplete type", value);
382 decl = 1;
383 }
384 retry:
385 /* We must print an error message. Be clever about what it says. */
386
387 switch (TREE_CODE (type))
388 {
389 case RECORD_TYPE:
390 case UNION_TYPE:
391 case ENUMERAL_TYPE:
392 if (!decl)
393 emit_diagnostic (diag_kind, input_location, 0,
394 "invalid use of incomplete type %q#T", type);
395 if (!TYPE_TEMPLATE_INFO (type))
396 emit_diagnostic (diag_kind, input_location, 0,
397 "forward declaration of %q+#T", type);
398 else
399 emit_diagnostic (diag_kind, input_location, 0,
400 "declaration of %q+#T", type);
401 break;
402
403 case VOID_TYPE:
404 emit_diagnostic (diag_kind, input_location, 0,
405 "invalid use of %qT", type);
406 break;
407
408 case ARRAY_TYPE:
409 if (TYPE_DOMAIN (type))
410 {
411 type = TREE_TYPE (type);
412 goto retry;
413 }
414 emit_diagnostic (diag_kind, input_location, 0,
415 "invalid use of array with unspecified bounds");
416 break;
417
418 case OFFSET_TYPE:
419 bad_member:
420 emit_diagnostic (diag_kind, input_location, 0,
421 "invalid use of member (did you forget the %<&%> ?)");
422 break;
423
424 case TEMPLATE_TYPE_PARM:
425 if (is_auto (type))
426 emit_diagnostic (diag_kind, input_location, 0,
427 "invalid use of %<auto%>");
428 else
429 emit_diagnostic (diag_kind, input_location, 0,
430 "invalid use of template type parameter %qT", type);
431 break;
432
433 case BOUND_TEMPLATE_TEMPLATE_PARM:
434 emit_diagnostic (diag_kind, input_location, 0,
435 "invalid use of template template parameter %qT",
436 TYPE_NAME (type));
437 break;
438
439 case TYPENAME_TYPE:
440 emit_diagnostic (diag_kind, input_location, 0,
441 "invalid use of dependent type %qT", type);
442 break;
443
444 case LANG_TYPE:
445 gcc_assert (type == unknown_type_node);
446 if (value && TREE_CODE (value) == COMPONENT_REF)
447 goto bad_member;
448 else if (value && TREE_CODE (value) == ADDR_EXPR)
449 emit_diagnostic (diag_kind, input_location, 0,
450 "address of overloaded function with no contextual "
451 "type information");
452 else if (value && TREE_CODE (value) == OVERLOAD)
453 emit_diagnostic (diag_kind, input_location, 0,
454 "overloaded function with no contextual type information");
455 else
456 emit_diagnostic (diag_kind, input_location, 0,
457 "insufficient contextual information to determine type");
458 break;
459
460 default:
461 gcc_unreachable ();
462 }
463 }
464
465 /* Backward-compatibility interface to incomplete_type_diagnostic;
466 required by ../tree.c. */
467 #undef cxx_incomplete_type_error
468 void
469 cxx_incomplete_type_error (const_tree value, const_tree type)
470 {
471 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
472 }
473
474 \f
475 /* The recursive part of split_nonconstant_init. DEST is an lvalue
476 expression to which INIT should be assigned. INIT is a CONSTRUCTOR. */
477
478 static void
479 split_nonconstant_init_1 (tree dest, tree *initp)
480 {
481 unsigned HOST_WIDE_INT idx;
482 tree init = *initp;
483 tree field_index, value;
484 tree type = TREE_TYPE (dest);
485 tree inner_type = NULL;
486 bool array_type_p = false;
487 HOST_WIDE_INT num_type_elements, num_initialized_elements;
488
489 switch (TREE_CODE (type))
490 {
491 case ARRAY_TYPE:
492 inner_type = TREE_TYPE (type);
493 array_type_p = true;
494 /* FALLTHRU */
495
496 case RECORD_TYPE:
497 case UNION_TYPE:
498 case QUAL_UNION_TYPE:
499 num_initialized_elements = 0;
500 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
501 field_index, value)
502 {
503 /* The current implementation of this algorithm assumes that
504 the field was set for all the elements. This is usually done
505 by process_init_constructor. */
506 gcc_assert (field_index);
507
508 if (!array_type_p)
509 inner_type = TREE_TYPE (field_index);
510
511 if (TREE_CODE (value) == CONSTRUCTOR)
512 {
513 tree sub;
514
515 if (array_type_p)
516 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
517 NULL_TREE, NULL_TREE);
518 else
519 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
520 NULL_TREE);
521
522 split_nonconstant_init_1 (sub, &value);
523 }
524 else if (!initializer_constant_valid_p (value, inner_type))
525 {
526 tree code;
527 tree sub;
528 HOST_WIDE_INT inner_elements;
529
530 /* FIXME: Ordered removal is O(1) so the whole function is
531 worst-case quadratic. This could be fixed using an aside
532 bitmap to record which elements must be removed and remove
533 them all at the same time. Or by merging
534 split_non_constant_init into process_init_constructor_array,
535 that is separating constants from non-constants while building
536 the vector. */
537 VEC_ordered_remove (constructor_elt, CONSTRUCTOR_ELTS (init),
538 idx);
539 --idx;
540
541 if (array_type_p)
542 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
543 NULL_TREE, NULL_TREE);
544 else
545 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
546 NULL_TREE);
547
548 code = build2 (INIT_EXPR, inner_type, sub, value);
549 code = build_stmt (input_location, EXPR_STMT, code);
550 add_stmt (code);
551
552 inner_elements = count_type_elements (inner_type, true);
553 if (inner_elements < 0)
554 num_initialized_elements = -1;
555 else if (num_initialized_elements >= 0)
556 num_initialized_elements += inner_elements;
557 continue;
558 }
559 }
560
561 num_type_elements = count_type_elements (type, true);
562 /* If all elements of the initializer are non-constant and
563 have been split out, we don't need the empty CONSTRUCTOR. */
564 if (num_type_elements > 0
565 && num_type_elements == num_initialized_elements)
566 *initp = NULL;
567 break;
568
569 case VECTOR_TYPE:
570 if (!initializer_constant_valid_p (init, type))
571 {
572 tree code;
573 tree cons = copy_node (init);
574 CONSTRUCTOR_ELTS (init) = NULL;
575 code = build2 (MODIFY_EXPR, type, dest, cons);
576 code = build_stmt (input_location, EXPR_STMT, code);
577 add_stmt (code);
578 }
579 break;
580
581 default:
582 gcc_unreachable ();
583 }
584
585 /* The rest of the initializer is now a constant. */
586 TREE_CONSTANT (init) = 1;
587 }
588
589 /* A subroutine of store_init_value. Splits non-constant static
590 initializer INIT into a constant part and generates code to
591 perform the non-constant part of the initialization to DEST.
592 Returns the code for the runtime init. */
593
594 static tree
595 split_nonconstant_init (tree dest, tree init)
596 {
597 tree code;
598
599 if (TREE_CODE (init) == CONSTRUCTOR)
600 {
601 code = push_stmt_list ();
602 split_nonconstant_init_1 (dest, &init);
603 code = pop_stmt_list (code);
604 DECL_INITIAL (dest) = init;
605 TREE_READONLY (dest) = 0;
606 }
607 else
608 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
609
610 return code;
611 }
612
613 /* Perform appropriate conversions on the initial value of a variable,
614 store it in the declaration DECL,
615 and print any error messages that are appropriate.
616 If the init is invalid, store an ERROR_MARK.
617
618 C++: Note that INIT might be a TREE_LIST, which would mean that it is
619 a base class initializer for some aggregate type, hopefully compatible
620 with DECL. If INIT is a single element, and DECL is an aggregate
621 type, we silently convert INIT into a TREE_LIST, allowing a constructor
622 to be called.
623
624 If INIT is a TREE_LIST and there is no constructor, turn INIT
625 into a CONSTRUCTOR and use standard initialization techniques.
626 Perhaps a warning should be generated?
627
628 Returns code to be executed if initialization could not be performed
629 for static variable. In that case, caller must emit the code. */
630
631 tree
632 store_init_value (tree decl, tree init, int flags)
633 {
634 tree value, type;
635
636 /* If variable's type was invalidly declared, just ignore it. */
637
638 type = TREE_TYPE (decl);
639 if (TREE_CODE (type) == ERROR_MARK)
640 return NULL_TREE;
641
642 if (MAYBE_CLASS_TYPE_P (type))
643 {
644 if (TREE_CODE (init) == TREE_LIST)
645 {
646 error ("constructor syntax used, but no constructor declared "
647 "for type %qT", type);
648 init = build_constructor_from_list (init_list_type_node, nreverse (init));
649 }
650 }
651 else if (TREE_CODE (init) == TREE_LIST
652 && TREE_TYPE (init) != unknown_type_node)
653 {
654 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
655
656 if (TREE_CODE (init) == TREE_LIST
657 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
658 {
659 error ("cannot initialize arrays using this syntax");
660 return NULL_TREE;
661 }
662 else
663 /* We get here with code like `int a (2);' */
664 init = build_x_compound_expr_from_list (init, ELK_INIT,
665 tf_warning_or_error);
666 }
667
668 /* End of special C++ code. */
669
670 if (flags & LOOKUP_ALREADY_DIGESTED)
671 value = init;
672 else
673 /* Digest the specified initializer into an expression. */
674 value = digest_init_flags (type, init, flags);
675
676 /* In C++0x constant expression is a semantic, not syntactic, property.
677 In C++98, make sure that what we thought was a constant expression at
678 template definition time is still constant. */
679 if ((cxx_dialect >= cxx0x
680 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
681 && (decl_maybe_constant_var_p (decl)
682 || TREE_STATIC (decl)))
683 {
684 bool const_init;
685 value = fold_non_dependent_expr (value);
686 value = maybe_constant_init (value);
687 if (DECL_DECLARED_CONSTEXPR_P (decl))
688 /* Diagnose a non-constant initializer for constexpr. */
689 value = cxx_constant_value (value);
690 const_init = (reduced_constant_expression_p (value)
691 || error_operand_p (value));
692 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
693 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
694 }
695
696 /* If the initializer is not a constant, fill in DECL_INITIAL with
697 the bits that are constant, and then return an expression that
698 will perform the dynamic initialization. */
699 if (value != error_mark_node
700 && (TREE_SIDE_EFFECTS (value)
701 || ! initializer_constant_valid_p (value, TREE_TYPE (value))))
702 return split_nonconstant_init (decl, value);
703 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
704 is an automatic variable, the middle end will turn this into a
705 dynamic initialization later. */
706 DECL_INITIAL (decl) = value;
707 return NULL_TREE;
708 }
709
710 \f
711 /* Give errors about narrowing conversions within { }. */
712
713 void
714 check_narrowing (tree type, tree init)
715 {
716 tree ftype = unlowered_expr_type (init);
717 bool ok = true;
718 REAL_VALUE_TYPE d;
719
720 init = maybe_constant_value (init);
721
722 if (TREE_CODE (type) == INTEGER_TYPE
723 && TREE_CODE (ftype) == REAL_TYPE)
724 ok = false;
725 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
726 && CP_INTEGRAL_TYPE_P (type))
727 {
728 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype)
729 && (TREE_CODE (init) != INTEGER_CST
730 || !int_fits_type_p (init, type)))
731 ok = false;
732 }
733 else if (TREE_CODE (ftype) == REAL_TYPE
734 && TREE_CODE (type) == REAL_TYPE)
735 {
736 if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
737 {
738 if (TREE_CODE (init) == REAL_CST)
739 {
740 /* Issue 703: Loss of precision is OK as long as the value is
741 within the representable range of the new type. */
742 REAL_VALUE_TYPE r;
743 d = TREE_REAL_CST (init);
744 real_convert (&r, TYPE_MODE (type), &d);
745 if (real_isinf (&r))
746 ok = false;
747 }
748 else
749 ok = false;
750 }
751 }
752 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
753 && TREE_CODE (type) == REAL_TYPE)
754 {
755 ok = false;
756 if (TREE_CODE (init) == INTEGER_CST)
757 {
758 d = real_value_from_int_cst (0, init);
759 if (exact_real_truncate (TYPE_MODE (type), &d))
760 ok = true;
761 }
762 }
763
764 if (!ok)
765 permerror (input_location, "narrowing conversion of %qE from %qT to %qT inside { }",
766 init, ftype, type);
767 }
768
769 /* Process the initializer INIT for a variable of type TYPE, emitting
770 diagnostics for invalid initializers and converting the initializer as
771 appropriate.
772
773 For aggregate types, it assumes that reshape_init has already run, thus the
774 initializer will have the right shape (brace elision has been undone).
775
776 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
777
778 static tree
779 digest_init_r (tree type, tree init, bool nested, int flags)
780 {
781 enum tree_code code = TREE_CODE (type);
782
783 if (error_operand_p (init))
784 return error_mark_node;
785
786 gcc_assert (init);
787
788 /* We must strip the outermost array type when completing the type,
789 because the its bounds might be incomplete at the moment. */
790 if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
791 ? TREE_TYPE (type) : type, NULL_TREE))
792 return error_mark_node;
793
794 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
795 (g++.old-deja/g++.law/casts2.C). */
796 if (TREE_CODE (init) == NON_LVALUE_EXPR)
797 init = TREE_OPERAND (init, 0);
798
799 /* Initialization of an array of chars from a string constant. The initializer
800 can be optionally enclosed in braces, but reshape_init has already removed
801 them if they were present. */
802 if (code == ARRAY_TYPE)
803 {
804 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
805 if (char_type_p (typ1)
806 /*&& init */
807 && TREE_CODE (init) == STRING_CST)
808 {
809 tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
810
811 if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
812 {
813 if (char_type != char_type_node)
814 {
815 error ("char-array initialized from wide string");
816 return error_mark_node;
817 }
818 }
819 else
820 {
821 if (char_type == char_type_node)
822 {
823 error ("int-array initialized from non-wide string");
824 return error_mark_node;
825 }
826 else if (char_type != typ1)
827 {
828 error ("int-array initialized from incompatible wide string");
829 return error_mark_node;
830 }
831 }
832
833 TREE_TYPE (init) = type;
834 if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
835 {
836 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
837 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
838 /* In C it is ok to subtract 1 from the length of the string
839 because it's ok to ignore the terminating null char that is
840 counted in the length of the constant, but in C++ this would
841 be invalid. */
842 if (size < TREE_STRING_LENGTH (init))
843 permerror (input_location, "initializer-string for array of chars is too long");
844 }
845 return init;
846 }
847 }
848
849 /* Handle scalar types (including conversions) and references. */
850 if ((TREE_CODE (type) != COMPLEX_TYPE
851 || BRACE_ENCLOSED_INITIALIZER_P (init))
852 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
853 {
854 tree *exp;
855
856 if (cxx_dialect != cxx98 && nested)
857 check_narrowing (type, init);
858 init = convert_for_initialization (0, type, init, flags,
859 ICR_INIT, NULL_TREE, 0,
860 tf_warning_or_error);
861 exp = &init;
862
863 /* Skip any conversions since we'll be outputting the underlying
864 constant. */
865 while (CONVERT_EXPR_P (*exp)
866 || TREE_CODE (*exp) == NON_LVALUE_EXPR)
867 exp = &TREE_OPERAND (*exp, 0);
868
869 *exp = cplus_expand_constant (*exp);
870
871 return init;
872 }
873
874 /* Come here only for aggregates: records, arrays, unions, complex numbers
875 and vectors. */
876 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
877 || TREE_CODE (type) == VECTOR_TYPE
878 || TREE_CODE (type) == RECORD_TYPE
879 || TREE_CODE (type) == UNION_TYPE
880 || TREE_CODE (type) == COMPLEX_TYPE);
881
882 if (BRACE_ENCLOSED_INITIALIZER_P (init)
883 && !TYPE_NON_AGGREGATE_CLASS (type))
884 return process_init_constructor (type, init);
885 else
886 {
887 if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
888 {
889 error ("cannot initialize aggregate of type %qT with "
890 "a compound literal", type);
891
892 return error_mark_node;
893 }
894
895 if (TREE_CODE (type) == ARRAY_TYPE
896 && !BRACE_ENCLOSED_INITIALIZER_P (init))
897 {
898 /* Allow the result of build_array_copy and of
899 build_value_init_noctor. */
900 if ((TREE_CODE (init) == TARGET_EXPR
901 || TREE_CODE (init) == CONSTRUCTOR)
902 && (same_type_ignoring_top_level_qualifiers_p
903 (type, TREE_TYPE (init))))
904 return init;
905
906 error ("array must be initialized with a brace-enclosed"
907 " initializer");
908 return error_mark_node;
909 }
910
911 return convert_for_initialization (NULL_TREE, type, init,
912 flags,
913 ICR_INIT, NULL_TREE, 0,
914 tf_warning_or_error);
915 }
916 }
917
918 tree
919 digest_init (tree type, tree init)
920 {
921 return digest_init_r (type, init, false, LOOKUP_IMPLICIT);
922 }
923
924 tree
925 digest_init_flags (tree type, tree init, int flags)
926 {
927 return digest_init_r (type, init, false, flags);
928 }
929 \f
930 /* Set of flags used within process_init_constructor to describe the
931 initializers. */
932 #define PICFLAG_ERRONEOUS 1
933 #define PICFLAG_NOT_ALL_CONSTANT 2
934 #define PICFLAG_NOT_ALL_SIMPLE 4
935
936 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
937 describe it. */
938
939 static int
940 picflag_from_initializer (tree init)
941 {
942 if (init == error_mark_node)
943 return PICFLAG_ERRONEOUS;
944 else if (!TREE_CONSTANT (init))
945 return PICFLAG_NOT_ALL_CONSTANT;
946 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
947 return PICFLAG_NOT_ALL_SIMPLE;
948 return 0;
949 }
950
951 /* Subroutine of process_init_constructor, which will process an initializer
952 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
953 which describe the initializers. */
954
955 static int
956 process_init_constructor_array (tree type, tree init)
957 {
958 unsigned HOST_WIDE_INT i, len = 0;
959 int flags = 0;
960 bool unbounded = false;
961 constructor_elt *ce;
962 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (init);
963
964 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
965 || TREE_CODE (type) == VECTOR_TYPE);
966
967 if (TREE_CODE (type) == ARRAY_TYPE)
968 {
969 tree domain = TYPE_DOMAIN (type);
970 if (domain)
971 len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
972 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
973 + 1);
974 else
975 unbounded = true; /* Take as many as there are. */
976 }
977 else
978 /* Vectors are like simple fixed-size arrays. */
979 len = TYPE_VECTOR_SUBPARTS (type);
980
981 /* There must not be more initializers than needed. */
982 if (!unbounded && VEC_length (constructor_elt, v) > len)
983 error ("too many initializers for %qT", type);
984
985 FOR_EACH_VEC_ELT (constructor_elt, v, i, ce)
986 {
987 if (ce->index)
988 {
989 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
990 if (compare_tree_int (ce->index, i) != 0)
991 {
992 ce->value = error_mark_node;
993 sorry ("non-trivial designated initializers not supported");
994 }
995 }
996 else
997 ce->index = size_int (i);
998 gcc_assert (ce->value);
999 ce->value = digest_init_r (TREE_TYPE (type), ce->value, true, LOOKUP_IMPLICIT);
1000
1001 if (ce->value != error_mark_node)
1002 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1003 (TREE_TYPE (type), TREE_TYPE (ce->value)));
1004
1005 flags |= picflag_from_initializer (ce->value);
1006 }
1007
1008 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1009 we must add initializers ourselves. */
1010 if (!unbounded)
1011 for (; i < len; ++i)
1012 {
1013 tree next;
1014
1015 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
1016 {
1017 /* If this type needs constructors run for default-initialization,
1018 we can't rely on the back end to do it for us, so build up
1019 TARGET_EXPRs. If the type in question is a class, just build
1020 one up; if it's an array, recurse. */
1021 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
1022 next = build_functional_cast (TREE_TYPE (type), NULL_TREE,
1023 tf_warning_or_error);
1024 else
1025 next = build_constructor (init_list_type_node, NULL);
1026 next = digest_init (TREE_TYPE (type), next);
1027 }
1028 else if (!zero_init_p (TREE_TYPE (type)))
1029 next = build_zero_init (TREE_TYPE (type),
1030 /*nelts=*/NULL_TREE,
1031 /*static_storage_p=*/false);
1032 else
1033 /* The default zero-initialization is fine for us; don't
1034 add anything to the CONSTRUCTOR. */
1035 break;
1036
1037 flags |= picflag_from_initializer (next);
1038 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1039 }
1040
1041 CONSTRUCTOR_ELTS (init) = v;
1042 return flags;
1043 }
1044
1045 /* Subroutine of process_init_constructor, which will process an initializer
1046 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1047 the initializers. */
1048
1049 static int
1050 process_init_constructor_record (tree type, tree init)
1051 {
1052 VEC(constructor_elt,gc) *v = NULL;
1053 int flags = 0;
1054 tree field;
1055 unsigned HOST_WIDE_INT idx = 0;
1056
1057 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1058 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1059 gcc_assert (!TYPE_BINFO (type)
1060 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1061 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1062
1063 /* Generally, we will always have an index for each initializer (which is
1064 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1065 reshape_init. So we need to handle both cases. */
1066 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1067 {
1068 tree next;
1069 tree type;
1070
1071 if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1072 {
1073 flags |= picflag_from_initializer (integer_zero_node);
1074 CONSTRUCTOR_APPEND_ELT (v, field, integer_zero_node);
1075 continue;
1076 }
1077
1078 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1079 continue;
1080
1081 /* If this is a bitfield, first convert to the declared type. */
1082 type = TREE_TYPE (field);
1083 if (DECL_BIT_FIELD_TYPE (field))
1084 type = DECL_BIT_FIELD_TYPE (field);
1085
1086 if (idx < VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)))
1087 {
1088 constructor_elt *ce = VEC_index (constructor_elt,
1089 CONSTRUCTOR_ELTS (init), idx);
1090 if (ce->index)
1091 {
1092 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1093 latter case can happen in templates where lookup has to be
1094 deferred. */
1095 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1096 || TREE_CODE (ce->index) == IDENTIFIER_NODE);
1097 if (ce->index != field
1098 && ce->index != DECL_NAME (field))
1099 {
1100 ce->value = error_mark_node;
1101 sorry ("non-trivial designated initializers not supported");
1102 }
1103 }
1104
1105 gcc_assert (ce->value);
1106 next = digest_init_r (type, ce->value, true, LOOKUP_IMPLICIT);
1107 ++idx;
1108 }
1109 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
1110 {
1111 /* If this type needs constructors run for
1112 default-initialization, we can't rely on the back end to do it
1113 for us, so build up TARGET_EXPRs. If the type in question is
1114 a class, just build one up; if it's an array, recurse. */
1115 next = build_constructor (init_list_type_node, NULL);
1116 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field)))
1117 {
1118 next = finish_compound_literal (TREE_TYPE (field), next);
1119 /* direct-initialize the target. No temporary is going
1120 to be involved. */
1121 if (TREE_CODE (next) == TARGET_EXPR)
1122 TARGET_EXPR_DIRECT_INIT_P (next) = true;
1123 }
1124
1125 next = digest_init_r (TREE_TYPE (field), next, true, LOOKUP_IMPLICIT);
1126
1127 /* Warn when some struct elements are implicitly initialized. */
1128 warning (OPT_Wmissing_field_initializers,
1129 "missing initializer for member %qD", field);
1130 }
1131 else
1132 {
1133 if (TREE_READONLY (field))
1134 error ("uninitialized const member %qD", field);
1135 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1136 error ("member %qD with uninitialized const fields", field);
1137 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1138 error ("member %qD is uninitialized reference", field);
1139
1140 /* Warn when some struct elements are implicitly initialized
1141 to zero. */
1142 warning (OPT_Wmissing_field_initializers,
1143 "missing initializer for member %qD", field);
1144
1145 if (!zero_init_p (TREE_TYPE (field)))
1146 next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1147 /*static_storage_p=*/false);
1148 else
1149 /* The default zero-initialization is fine for us; don't
1150 add anything to the CONSTRUCTOR. */
1151 continue;
1152 }
1153
1154 /* If this is a bitfield, now convert to the lowered type. */
1155 if (type != TREE_TYPE (field))
1156 next = cp_convert_and_check (TREE_TYPE (field), next);
1157 flags |= picflag_from_initializer (next);
1158 CONSTRUCTOR_APPEND_ELT (v, field, next);
1159 }
1160
1161 if (idx < VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)))
1162 error ("too many initializers for %qT", type);
1163
1164 CONSTRUCTOR_ELTS (init) = v;
1165 return flags;
1166 }
1167
1168 /* Subroutine of process_init_constructor, which will process a single
1169 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1170 which describe the initializer. */
1171
1172 static int
1173 process_init_constructor_union (tree type, tree init)
1174 {
1175 constructor_elt *ce;
1176 int len;
1177
1178 /* If the initializer was empty, use default zero initialization. */
1179 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
1180 return 0;
1181
1182 len = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init));
1183 if (len > 1)
1184 {
1185 error ("too many initializers for %qT", type);
1186 VEC_block_remove (constructor_elt, CONSTRUCTOR_ELTS (init), 1, len-1);
1187 }
1188
1189 ce = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (init), 0);
1190
1191 /* If this element specifies a field, initialize via that field. */
1192 if (ce->index)
1193 {
1194 if (TREE_CODE (ce->index) == FIELD_DECL)
1195 ;
1196 else if (TREE_CODE (ce->index) == IDENTIFIER_NODE)
1197 {
1198 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1199 tree name = ce->index;
1200 tree field;
1201 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1202 if (DECL_NAME (field) == name)
1203 break;
1204 if (!field)
1205 {
1206 error ("no field %qD found in union being initialized", field);
1207 ce->value = error_mark_node;
1208 }
1209 ce->index = field;
1210 }
1211 else
1212 {
1213 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1214 || TREE_CODE (ce->index) == RANGE_EXPR);
1215 error ("index value instead of field name in union initializer");
1216 ce->value = error_mark_node;
1217 }
1218 }
1219 else
1220 {
1221 /* Find the first named field. ANSI decided in September 1990
1222 that only named fields count here. */
1223 tree field = TYPE_FIELDS (type);
1224 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1225 field = TREE_CHAIN (field);
1226 if (field == NULL_TREE)
1227 {
1228 error ("too many initializers for %qT", type);
1229 ce->value = error_mark_node;
1230 }
1231 ce->index = field;
1232 }
1233
1234 if (ce->value && ce->value != error_mark_node)
1235 ce->value = digest_init_r (TREE_TYPE (ce->index), ce->value, true, LOOKUP_IMPLICIT);
1236
1237 return picflag_from_initializer (ce->value);
1238 }
1239
1240 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1241 constructor is a brace-enclosed initializer, and will be modified in-place.
1242
1243 Each element is converted to the right type through digest_init, and
1244 missing initializers are added following the language rules (zero-padding,
1245 etc.).
1246
1247 After the execution, the initializer will have TREE_CONSTANT if all elts are
1248 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1249 constants that the assembler and linker can compute them.
1250
1251 The function returns the initializer itself, or error_mark_node in case
1252 of error. */
1253
1254 static tree
1255 process_init_constructor (tree type, tree init)
1256 {
1257 int flags;
1258
1259 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1260
1261 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1262 flags = process_init_constructor_array (type, init);
1263 else if (TREE_CODE (type) == RECORD_TYPE)
1264 flags = process_init_constructor_record (type, init);
1265 else if (TREE_CODE (type) == UNION_TYPE)
1266 flags = process_init_constructor_union (type, init);
1267 else
1268 gcc_unreachable ();
1269
1270 if (flags & PICFLAG_ERRONEOUS)
1271 return error_mark_node;
1272
1273 TREE_TYPE (init) = type;
1274 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1275 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1276 if (!(flags & PICFLAG_NOT_ALL_CONSTANT))
1277 {
1278 TREE_CONSTANT (init) = 1;
1279 if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1280 TREE_STATIC (init) = 1;
1281 }
1282 return init;
1283 }
1284 \f
1285 /* Given a structure or union value DATUM, construct and return
1286 the structure or union component which results from narrowing
1287 that value to the base specified in BASETYPE. For example, given the
1288 hierarchy
1289
1290 class L { int ii; };
1291 class A : L { ... };
1292 class B : L { ... };
1293 class C : A, B { ... };
1294
1295 and the declaration
1296
1297 C x;
1298
1299 then the expression
1300
1301 x.A::ii refers to the ii member of the L part of
1302 the A part of the C object named by X. In this case,
1303 DATUM would be x, and BASETYPE would be A.
1304
1305 I used to think that this was nonconformant, that the standard specified
1306 that first we look up ii in A, then convert x to an L& and pull out the
1307 ii part. But in fact, it does say that we convert x to an A&; A here
1308 is known as the "naming class". (jason 2000-12-19)
1309
1310 BINFO_P points to a variable initialized either to NULL_TREE or to the
1311 binfo for the specific base subobject we want to convert to. */
1312
1313 tree
1314 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1315 {
1316 tree binfo;
1317
1318 if (datum == error_mark_node)
1319 return error_mark_node;
1320 if (*binfo_p)
1321 binfo = *binfo_p;
1322 else
1323 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
1324
1325 if (!binfo || binfo == error_mark_node)
1326 {
1327 *binfo_p = NULL_TREE;
1328 if (!binfo)
1329 error_not_base_type (basetype, TREE_TYPE (datum));
1330 return error_mark_node;
1331 }
1332
1333 *binfo_p = binfo;
1334 return build_base_path (PLUS_EXPR, datum, binfo, 1);
1335 }
1336
1337 /* Build a reference to an object specified by the C++ `->' operator.
1338 Usually this just involves dereferencing the object, but if the
1339 `->' operator is overloaded, then such overloads must be
1340 performed until an object which does not have the `->' operator
1341 overloaded is found. An error is reported when circular pointer
1342 delegation is detected. */
1343
1344 tree
1345 build_x_arrow (tree expr)
1346 {
1347 tree orig_expr = expr;
1348 tree type = TREE_TYPE (expr);
1349 tree last_rval = NULL_TREE;
1350 VEC(tree,gc) *types_memoized = NULL;
1351
1352 if (type == error_mark_node)
1353 return error_mark_node;
1354
1355 if (processing_template_decl)
1356 {
1357 if (type_dependent_expression_p (expr))
1358 return build_min_nt (ARROW_EXPR, expr);
1359 expr = build_non_dependent_expr (expr);
1360 }
1361
1362 if (MAYBE_CLASS_TYPE_P (type))
1363 {
1364 while ((expr = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, expr,
1365 NULL_TREE, NULL_TREE,
1366 /*overloaded_p=*/NULL,
1367 tf_warning_or_error)))
1368 {
1369 if (expr == error_mark_node)
1370 return error_mark_node;
1371
1372 if (vec_member (TREE_TYPE (expr), types_memoized))
1373 {
1374 error ("circular pointer delegation detected");
1375 return error_mark_node;
1376 }
1377
1378 VEC_safe_push (tree, gc, types_memoized, TREE_TYPE (expr));
1379 last_rval = expr;
1380 }
1381
1382 if (last_rval == NULL_TREE)
1383 {
1384 error ("base operand of %<->%> has non-pointer type %qT", type);
1385 return error_mark_node;
1386 }
1387
1388 if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1389 last_rval = convert_from_reference (last_rval);
1390 }
1391 else
1392 last_rval = decay_conversion (expr);
1393
1394 if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1395 {
1396 if (processing_template_decl)
1397 {
1398 expr = build_min_non_dep (ARROW_EXPR, last_rval, orig_expr);
1399 /* It will be dereferenced. */
1400 TREE_TYPE (expr) = TREE_TYPE (TREE_TYPE (last_rval));
1401 return expr;
1402 }
1403
1404 return cp_build_indirect_ref (last_rval, RO_NULL, tf_warning_or_error);
1405 }
1406
1407 if (types_memoized)
1408 error ("result of %<operator->()%> yields non-pointer result");
1409 else
1410 error ("base operand of %<->%> is not a pointer");
1411 return error_mark_node;
1412 }
1413
1414 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1415 already been checked out to be of aggregate type. */
1416
1417 tree
1418 build_m_component_ref (tree datum, tree component)
1419 {
1420 tree ptrmem_type;
1421 tree objtype;
1422 tree type;
1423 tree binfo;
1424 tree ctype;
1425
1426 if (error_operand_p (datum) || error_operand_p (component))
1427 return error_mark_node;
1428
1429 datum = mark_lvalue_use (datum);
1430 component = mark_rvalue_use (component);
1431
1432 ptrmem_type = TREE_TYPE (component);
1433 if (!TYPE_PTR_TO_MEMBER_P (ptrmem_type))
1434 {
1435 error ("%qE cannot be used as a member pointer, since it is of "
1436 "type %qT",
1437 component, ptrmem_type);
1438 return error_mark_node;
1439 }
1440
1441 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1442 if (! MAYBE_CLASS_TYPE_P (objtype))
1443 {
1444 error ("cannot apply member pointer %qE to %qE, which is of "
1445 "non-class type %qT",
1446 component, datum, objtype);
1447 return error_mark_node;
1448 }
1449
1450 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1451 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1452
1453 if (!COMPLETE_TYPE_P (ctype))
1454 {
1455 if (!same_type_p (ctype, objtype))
1456 goto mismatch;
1457 binfo = NULL;
1458 }
1459 else
1460 {
1461 binfo = lookup_base (objtype, ctype, ba_check, NULL);
1462
1463 if (!binfo)
1464 {
1465 mismatch:
1466 error ("pointer to member type %qT incompatible with object "
1467 "type %qT",
1468 type, objtype);
1469 return error_mark_node;
1470 }
1471 else if (binfo == error_mark_node)
1472 return error_mark_node;
1473 }
1474
1475 if (TYPE_PTRMEM_P (ptrmem_type))
1476 {
1477 tree ptype;
1478
1479 /* Compute the type of the field, as described in [expr.ref].
1480 There's no such thing as a mutable pointer-to-member, so
1481 things are not as complex as they are for references to
1482 non-static data members. */
1483 type = cp_build_qualified_type (type,
1484 (cp_type_quals (type)
1485 | cp_type_quals (TREE_TYPE (datum))));
1486
1487 datum = build_address (datum);
1488
1489 /* Convert object to the correct base. */
1490 if (binfo)
1491 datum = build_base_path (PLUS_EXPR, datum, binfo, 1);
1492
1493 /* Build an expression for "object + offset" where offset is the
1494 value stored in the pointer-to-data-member. */
1495 ptype = build_pointer_type (type);
1496 datum = build2 (POINTER_PLUS_EXPR, ptype,
1497 fold_convert (ptype, datum),
1498 build_nop (sizetype, component));
1499 return cp_build_indirect_ref (datum, RO_NULL, tf_warning_or_error);
1500 }
1501 else
1502 return build2 (OFFSET_REF, type, datum, component);
1503 }
1504
1505 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1506
1507 tree
1508 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1509 {
1510 /* This is either a call to a constructor,
1511 or a C cast in C++'s `functional' notation. */
1512
1513 /* The type to which we are casting. */
1514 tree type;
1515 VEC(tree,gc) *parmvec;
1516
1517 if (exp == error_mark_node || parms == error_mark_node)
1518 return error_mark_node;
1519
1520 if (TREE_CODE (exp) == TYPE_DECL)
1521 type = TREE_TYPE (exp);
1522 else
1523 type = exp;
1524
1525 if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1526 {
1527 error ("invalid value-initialization of reference type");
1528 return error_mark_node;
1529 }
1530
1531 if (processing_template_decl)
1532 {
1533 tree t = build_min (CAST_EXPR, type, parms);
1534 /* We don't know if it will or will not have side effects. */
1535 TREE_SIDE_EFFECTS (t) = 1;
1536 return t;
1537 }
1538
1539 if (! MAYBE_CLASS_TYPE_P (type))
1540 {
1541 if (parms == NULL_TREE)
1542 return cp_convert (type, integer_zero_node);
1543
1544 /* This must build a C cast. */
1545 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1546 return cp_build_c_cast (type, parms, complain);
1547 }
1548
1549 /* Prepare to evaluate as a call to a constructor. If this expression
1550 is actually used, for example,
1551
1552 return X (arg1, arg2, ...);
1553
1554 then the slot being initialized will be filled in. */
1555
1556 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1557 return error_mark_node;
1558 if (abstract_virtuals_error (NULL_TREE, type))
1559 return error_mark_node;
1560
1561 /* [expr.type.conv]
1562
1563 If the expression list is a single-expression, the type
1564 conversion is equivalent (in definedness, and if defined in
1565 meaning) to the corresponding cast expression. */
1566 if (parms && TREE_CHAIN (parms) == NULL_TREE)
1567 return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1568
1569 /* [expr.type.conv]
1570
1571 The expression T(), where T is a simple-type-specifier for a
1572 non-array complete object type or the (possibly cv-qualified)
1573 void type, creates an rvalue of the specified type, which is
1574 value-initialized. */
1575
1576 if (parms == NULL_TREE
1577 /* If there's a user-defined constructor, value-initialization is
1578 just calling the constructor, so fall through. */
1579 && !TYPE_HAS_USER_CONSTRUCTOR (type))
1580 {
1581 exp = build_value_init (type, complain);
1582 exp = get_target_expr (exp);
1583 /* FIXME this is wrong */
1584 if (literal_type_p (type))
1585 TREE_CONSTANT (exp) = true;
1586 return exp;
1587 }
1588
1589 /* Call the constructor. */
1590 parmvec = make_tree_vector ();
1591 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
1592 VEC_safe_push (tree, gc, parmvec, TREE_VALUE (parms));
1593 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
1594 &parmvec, type, LOOKUP_NORMAL, complain);
1595 release_tree_vector (parmvec);
1596
1597 if (exp == error_mark_node)
1598 return error_mark_node;
1599
1600 return build_cplus_new (type, exp);
1601 }
1602 \f
1603
1604 /* Add new exception specifier SPEC, to the LIST we currently have.
1605 If it's already in LIST then do nothing.
1606 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1607 know what we're doing. */
1608
1609 tree
1610 add_exception_specifier (tree list, tree spec, int complain)
1611 {
1612 bool ok;
1613 tree core = spec;
1614 bool is_ptr;
1615 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
1616
1617 if (spec == error_mark_node)
1618 return list;
1619
1620 gcc_assert (spec && (!list || TREE_VALUE (list)));
1621
1622 /* [except.spec] 1, type in an exception specifier shall not be
1623 incomplete, or pointer or ref to incomplete other than pointer
1624 to cv void. */
1625 is_ptr = TREE_CODE (core) == POINTER_TYPE;
1626 if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1627 core = TREE_TYPE (core);
1628 if (complain < 0)
1629 ok = true;
1630 else if (VOID_TYPE_P (core))
1631 ok = is_ptr;
1632 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1633 ok = true;
1634 else if (processing_template_decl)
1635 ok = true;
1636 else
1637 {
1638 ok = true;
1639 /* 15.4/1 says that types in an exception specifier must be complete,
1640 but it seems more reasonable to only require this on definitions
1641 and calls. So just give a pedwarn at this point; we will give an
1642 error later if we hit one of those two cases. */
1643 if (!COMPLETE_TYPE_P (complete_type (core)))
1644 diag_type = DK_PEDWARN; /* pedwarn */
1645 }
1646
1647 if (ok)
1648 {
1649 tree probe;
1650
1651 for (probe = list; probe; probe = TREE_CHAIN (probe))
1652 if (same_type_p (TREE_VALUE (probe), spec))
1653 break;
1654 if (!probe)
1655 list = tree_cons (NULL_TREE, spec, list);
1656 }
1657 else
1658 diag_type = DK_ERROR; /* error */
1659
1660 if (diag_type != DK_UNSPECIFIED && complain)
1661 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1662
1663 return list;
1664 }
1665
1666 /* Combine the two exceptions specifier lists LIST and ADD, and return
1667 their union. */
1668
1669 tree
1670 merge_exception_specifiers (tree list, tree add)
1671 {
1672 if (!list || !add)
1673 return NULL_TREE;
1674 /* For merging noexcept(true) and throw(), take the more recent one (LIST).
1675 A throw(type-list) spec takes precedence over a noexcept(false) spec.
1676 Any other noexcept-spec should only be merged with an equivalent one.
1677 So the !TREE_VALUE code below is correct for all cases. */
1678 else if (!TREE_VALUE (add))
1679 return list;
1680 else if (!TREE_VALUE (list))
1681 return add;
1682 else
1683 {
1684 tree orig_list = list;
1685
1686 for (; add; add = TREE_CHAIN (add))
1687 {
1688 tree spec = TREE_VALUE (add);
1689 tree probe;
1690
1691 for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1692 if (same_type_p (TREE_VALUE (probe), spec))
1693 break;
1694 if (!probe)
1695 {
1696 spec = build_tree_list (NULL_TREE, spec);
1697 TREE_CHAIN (spec) = list;
1698 list = spec;
1699 }
1700 }
1701 }
1702 return list;
1703 }
1704
1705 /* Subroutine of build_call. Ensure that each of the types in the
1706 exception specification is complete. Technically, 15.4/1 says that
1707 they need to be complete when we see a declaration of the function,
1708 but we should be able to get away with only requiring this when the
1709 function is defined or called. See also add_exception_specifier. */
1710
1711 void
1712 require_complete_eh_spec_types (tree fntype, tree decl)
1713 {
1714 tree raises;
1715 /* Don't complain about calls to op new. */
1716 if (decl && DECL_ARTIFICIAL (decl))
1717 return;
1718 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1719 raises = TREE_CHAIN (raises))
1720 {
1721 tree type = TREE_VALUE (raises);
1722 if (type && !COMPLETE_TYPE_P (type))
1723 {
1724 if (decl)
1725 error
1726 ("call to function %qD which throws incomplete type %q#T",
1727 decl, type);
1728 else
1729 error ("call to function which throws incomplete type %q#T",
1730 decl);
1731 }
1732 }
1733 }
1734
1735 \f
1736 #include "gt-cp-typeck2.h"
This page took 0.122904 seconds and 6 git commands to generate.