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