]> gcc.gnu.org Git - gcc.git/blob - gcc/c/c-typeck.c
re PR c/60197 (ICE with _Cilk_spawn in expression)
[gcc.git] / gcc / c / c-typeck.c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "stor-layout.h"
32 #include "trans-mem.h"
33 #include "varasm.h"
34 #include "stmt.h"
35 #include "langhooks.h"
36 #include "c-tree.h"
37 #include "c-lang.h"
38 #include "flags.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "pointer-set.h"
44 #include "basic-block.h"
45 #include "gimple-expr.h"
46 #include "gimplify.h"
47 #include "tree-inline.h"
48 #include "omp-low.h"
49 #include "c-family/c-objc.h"
50 #include "c-family/c-common.h"
51 #include "c-family/c-ubsan.h"
52 #include "cilk.h"
53
54 /* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56 enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
61 };
62
63 /* The level of nesting inside "__alignof__". */
64 int in_alignof;
65
66 /* The level of nesting inside "sizeof". */
67 int in_sizeof;
68
69 /* The level of nesting inside "typeof". */
70 int in_typeof;
71
72 /* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74 tree c_last_sizeof_arg;
75
76 /* Nonzero if we've already printed a "missing braces around initializer"
77 message within this initializer. */
78 static int missing_braces_mentioned;
79
80 static int require_constant_value;
81 static int require_constant_elements;
82
83 static bool null_pointer_constant_p (const_tree);
84 static tree qualify_type (tree, tree);
85 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
87 static int comp_target_types (location_t, tree, tree);
88 static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
91 static tree lookup_field (tree, tree);
92 static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
94 tree);
95 static tree pointer_diff (location_t, tree, tree);
96 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
97 enum impl_conv, bool, tree, tree, int);
98 static tree valid_compound_expr_initializer (tree, tree);
99 static void push_string (const char *);
100 static void push_member_name (tree);
101 static int spelling_length (void);
102 static char *print_spelling (char *);
103 static void warning_init (int, const char *);
104 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
105 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
106 struct obstack *);
107 static void output_pending_init_elements (int, struct obstack *);
108 static int set_designator (int, struct obstack *);
109 static void push_range_stack (tree, struct obstack *);
110 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
111 static void set_nonincremental_init (struct obstack *);
112 static void set_nonincremental_init_from_string (tree, struct obstack *);
113 static tree find_init_member (tree, struct obstack *);
114 static void readonly_warning (tree, enum lvalue_use);
115 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
116 static void record_maybe_used_decl (tree);
117 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
118 \f
119 /* Return true if EXP is a null pointer constant, false otherwise. */
120
121 static bool
122 null_pointer_constant_p (const_tree expr)
123 {
124 /* This should really operate on c_expr structures, but they aren't
125 yet available everywhere required. */
126 tree type = TREE_TYPE (expr);
127 return (TREE_CODE (expr) == INTEGER_CST
128 && !TREE_OVERFLOW (expr)
129 && integer_zerop (expr)
130 && (INTEGRAL_TYPE_P (type)
131 || (TREE_CODE (type) == POINTER_TYPE
132 && VOID_TYPE_P (TREE_TYPE (type))
133 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
134 }
135
136 /* EXPR may appear in an unevaluated part of an integer constant
137 expression, but not in an evaluated part. Wrap it in a
138 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
139 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
140
141 static tree
142 note_integer_operands (tree expr)
143 {
144 tree ret;
145 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
146 {
147 ret = copy_node (expr);
148 TREE_OVERFLOW (ret) = 1;
149 }
150 else
151 {
152 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
153 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
154 }
155 return ret;
156 }
157
158 /* Having checked whether EXPR may appear in an unevaluated part of an
159 integer constant expression and found that it may, remove any
160 C_MAYBE_CONST_EXPR noting this fact and return the resulting
161 expression. */
162
163 static inline tree
164 remove_c_maybe_const_expr (tree expr)
165 {
166 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
167 return C_MAYBE_CONST_EXPR_EXPR (expr);
168 else
169 return expr;
170 }
171
172 \f/* This is a cache to hold if two types are compatible or not. */
173
174 struct tagged_tu_seen_cache {
175 const struct tagged_tu_seen_cache * next;
176 const_tree t1;
177 const_tree t2;
178 /* The return value of tagged_types_tu_compatible_p if we had seen
179 these two types already. */
180 int val;
181 };
182
183 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
184 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
185
186 /* Do `exp = require_complete_type (exp);' to make sure exp
187 does not have an incomplete type. (That includes void types.) */
188
189 tree
190 require_complete_type (tree value)
191 {
192 tree type = TREE_TYPE (value);
193
194 if (value == error_mark_node || type == error_mark_node)
195 return error_mark_node;
196
197 /* First, detect a valid value with a complete type. */
198 if (COMPLETE_TYPE_P (type))
199 return value;
200
201 c_incomplete_type_error (value, type);
202 return error_mark_node;
203 }
204
205 /* Print an error message for invalid use of an incomplete type.
206 VALUE is the expression that was used (or 0 if that isn't known)
207 and TYPE is the type that was invalid. */
208
209 void
210 c_incomplete_type_error (const_tree value, const_tree type)
211 {
212 const char *type_code_string;
213
214 /* Avoid duplicate error message. */
215 if (TREE_CODE (type) == ERROR_MARK)
216 return;
217
218 if (value != 0 && (TREE_CODE (value) == VAR_DECL
219 || TREE_CODE (value) == PARM_DECL))
220 error ("%qD has an incomplete type", value);
221 else
222 {
223 retry:
224 /* We must print an error message. Be clever about what it says. */
225
226 switch (TREE_CODE (type))
227 {
228 case RECORD_TYPE:
229 type_code_string = "struct";
230 break;
231
232 case UNION_TYPE:
233 type_code_string = "union";
234 break;
235
236 case ENUMERAL_TYPE:
237 type_code_string = "enum";
238 break;
239
240 case VOID_TYPE:
241 error ("invalid use of void expression");
242 return;
243
244 case ARRAY_TYPE:
245 if (TYPE_DOMAIN (type))
246 {
247 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
248 {
249 error ("invalid use of flexible array member");
250 return;
251 }
252 type = TREE_TYPE (type);
253 goto retry;
254 }
255 error ("invalid use of array with unspecified bounds");
256 return;
257
258 default:
259 gcc_unreachable ();
260 }
261
262 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
263 error ("invalid use of undefined type %<%s %E%>",
264 type_code_string, TYPE_NAME (type));
265 else
266 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
267 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
268 }
269 }
270
271 /* Given a type, apply default promotions wrt unnamed function
272 arguments and return the new type. */
273
274 tree
275 c_type_promotes_to (tree type)
276 {
277 tree ret = NULL_TREE;
278
279 if (TYPE_MAIN_VARIANT (type) == float_type_node)
280 ret = double_type_node;
281 else if (c_promoting_integer_type_p (type))
282 {
283 /* Preserve unsignedness if not really getting any wider. */
284 if (TYPE_UNSIGNED (type)
285 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
286 ret = unsigned_type_node;
287 else
288 ret = integer_type_node;
289 }
290
291 if (ret != NULL_TREE)
292 return (TYPE_ATOMIC (type)
293 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
294 : ret);
295
296 return type;
297 }
298
299 /* Return true if between two named address spaces, whether there is a superset
300 named address space that encompasses both address spaces. If there is a
301 superset, return which address space is the superset. */
302
303 static bool
304 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
305 {
306 if (as1 == as2)
307 {
308 *common = as1;
309 return true;
310 }
311 else if (targetm.addr_space.subset_p (as1, as2))
312 {
313 *common = as2;
314 return true;
315 }
316 else if (targetm.addr_space.subset_p (as2, as1))
317 {
318 *common = as1;
319 return true;
320 }
321 else
322 return false;
323 }
324
325 /* Return a variant of TYPE which has all the type qualifiers of LIKE
326 as well as those of TYPE. */
327
328 static tree
329 qualify_type (tree type, tree like)
330 {
331 addr_space_t as_type = TYPE_ADDR_SPACE (type);
332 addr_space_t as_like = TYPE_ADDR_SPACE (like);
333 addr_space_t as_common;
334
335 /* If the two named address spaces are different, determine the common
336 superset address space. If there isn't one, raise an error. */
337 if (!addr_space_superset (as_type, as_like, &as_common))
338 {
339 as_common = as_type;
340 error ("%qT and %qT are in disjoint named address spaces",
341 type, like);
342 }
343
344 return c_build_qualified_type (type,
345 TYPE_QUALS_NO_ADDR_SPACE (type)
346 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
347 | ENCODE_QUAL_ADDR_SPACE (as_common));
348 }
349
350 /* Return true iff the given tree T is a variable length array. */
351
352 bool
353 c_vla_type_p (const_tree t)
354 {
355 if (TREE_CODE (t) == ARRAY_TYPE
356 && C_TYPE_VARIABLE_SIZE (t))
357 return true;
358 return false;
359 }
360 \f
361 /* Return the composite type of two compatible types.
362
363 We assume that comptypes has already been done and returned
364 nonzero; if that isn't so, this may crash. In particular, we
365 assume that qualifiers match. */
366
367 tree
368 composite_type (tree t1, tree t2)
369 {
370 enum tree_code code1;
371 enum tree_code code2;
372 tree attributes;
373
374 /* Save time if the two types are the same. */
375
376 if (t1 == t2) return t1;
377
378 /* If one type is nonsense, use the other. */
379 if (t1 == error_mark_node)
380 return t2;
381 if (t2 == error_mark_node)
382 return t1;
383
384 code1 = TREE_CODE (t1);
385 code2 = TREE_CODE (t2);
386
387 /* Merge the attributes. */
388 attributes = targetm.merge_type_attributes (t1, t2);
389
390 /* If one is an enumerated type and the other is the compatible
391 integer type, the composite type might be either of the two
392 (DR#013 question 3). For consistency, use the enumerated type as
393 the composite type. */
394
395 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
396 return t1;
397 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
398 return t2;
399
400 gcc_assert (code1 == code2);
401
402 switch (code1)
403 {
404 case POINTER_TYPE:
405 /* For two pointers, do this recursively on the target type. */
406 {
407 tree pointed_to_1 = TREE_TYPE (t1);
408 tree pointed_to_2 = TREE_TYPE (t2);
409 tree target = composite_type (pointed_to_1, pointed_to_2);
410 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
411 t1 = build_type_attribute_variant (t1, attributes);
412 return qualify_type (t1, t2);
413 }
414
415 case ARRAY_TYPE:
416 {
417 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
418 int quals;
419 tree unqual_elt;
420 tree d1 = TYPE_DOMAIN (t1);
421 tree d2 = TYPE_DOMAIN (t2);
422 bool d1_variable, d2_variable;
423 bool d1_zero, d2_zero;
424 bool t1_complete, t2_complete;
425
426 /* We should not have any type quals on arrays at all. */
427 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
428 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
429
430 t1_complete = COMPLETE_TYPE_P (t1);
431 t2_complete = COMPLETE_TYPE_P (t2);
432
433 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
434 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
435
436 d1_variable = (!d1_zero
437 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
438 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
439 d2_variable = (!d2_zero
440 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
441 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
442 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
443 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
444
445 /* Save space: see if the result is identical to one of the args. */
446 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
447 && (d2_variable || d2_zero || !d1_variable))
448 return build_type_attribute_variant (t1, attributes);
449 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
450 && (d1_variable || d1_zero || !d2_variable))
451 return build_type_attribute_variant (t2, attributes);
452
453 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
454 return build_type_attribute_variant (t1, attributes);
455 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
456 return build_type_attribute_variant (t2, attributes);
457
458 /* Merge the element types, and have a size if either arg has
459 one. We may have qualifiers on the element types. To set
460 up TYPE_MAIN_VARIANT correctly, we need to form the
461 composite of the unqualified types and add the qualifiers
462 back at the end. */
463 quals = TYPE_QUALS (strip_array_types (elt));
464 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
465 t1 = build_array_type (unqual_elt,
466 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
467 && (d2_variable
468 || d2_zero
469 || !d1_variable))
470 ? t1
471 : t2));
472 /* Ensure a composite type involving a zero-length array type
473 is a zero-length type not an incomplete type. */
474 if (d1_zero && d2_zero
475 && (t1_complete || t2_complete)
476 && !COMPLETE_TYPE_P (t1))
477 {
478 TYPE_SIZE (t1) = bitsize_zero_node;
479 TYPE_SIZE_UNIT (t1) = size_zero_node;
480 }
481 t1 = c_build_qualified_type (t1, quals);
482 return build_type_attribute_variant (t1, attributes);
483 }
484
485 case ENUMERAL_TYPE:
486 case RECORD_TYPE:
487 case UNION_TYPE:
488 if (attributes != NULL)
489 {
490 /* Try harder not to create a new aggregate type. */
491 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
492 return t1;
493 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
494 return t2;
495 }
496 return build_type_attribute_variant (t1, attributes);
497
498 case FUNCTION_TYPE:
499 /* Function types: prefer the one that specified arg types.
500 If both do, merge the arg types. Also merge the return types. */
501 {
502 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
503 tree p1 = TYPE_ARG_TYPES (t1);
504 tree p2 = TYPE_ARG_TYPES (t2);
505 int len;
506 tree newargs, n;
507 int i;
508
509 /* Save space: see if the result is identical to one of the args. */
510 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
511 return build_type_attribute_variant (t1, attributes);
512 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
513 return build_type_attribute_variant (t2, attributes);
514
515 /* Simple way if one arg fails to specify argument types. */
516 if (TYPE_ARG_TYPES (t1) == 0)
517 {
518 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
519 t1 = build_type_attribute_variant (t1, attributes);
520 return qualify_type (t1, t2);
521 }
522 if (TYPE_ARG_TYPES (t2) == 0)
523 {
524 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
525 t1 = build_type_attribute_variant (t1, attributes);
526 return qualify_type (t1, t2);
527 }
528
529 /* If both args specify argument types, we must merge the two
530 lists, argument by argument. */
531
532 len = list_length (p1);
533 newargs = 0;
534
535 for (i = 0; i < len; i++)
536 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
537
538 n = newargs;
539
540 for (; p1;
541 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
542 {
543 /* A null type means arg type is not specified.
544 Take whatever the other function type has. */
545 if (TREE_VALUE (p1) == 0)
546 {
547 TREE_VALUE (n) = TREE_VALUE (p2);
548 goto parm_done;
549 }
550 if (TREE_VALUE (p2) == 0)
551 {
552 TREE_VALUE (n) = TREE_VALUE (p1);
553 goto parm_done;
554 }
555
556 /* Given wait (union {union wait *u; int *i} *)
557 and wait (union wait *),
558 prefer union wait * as type of parm. */
559 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
560 && TREE_VALUE (p1) != TREE_VALUE (p2))
561 {
562 tree memb;
563 tree mv2 = TREE_VALUE (p2);
564 if (mv2 && mv2 != error_mark_node
565 && TREE_CODE (mv2) != ARRAY_TYPE)
566 mv2 = TYPE_MAIN_VARIANT (mv2);
567 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
568 memb; memb = DECL_CHAIN (memb))
569 {
570 tree mv3 = TREE_TYPE (memb);
571 if (mv3 && mv3 != error_mark_node
572 && TREE_CODE (mv3) != ARRAY_TYPE)
573 mv3 = TYPE_MAIN_VARIANT (mv3);
574 if (comptypes (mv3, mv2))
575 {
576 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
577 TREE_VALUE (p2));
578 pedwarn (input_location, OPT_Wpedantic,
579 "function types not truly compatible in ISO C");
580 goto parm_done;
581 }
582 }
583 }
584 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
585 && TREE_VALUE (p2) != TREE_VALUE (p1))
586 {
587 tree memb;
588 tree mv1 = TREE_VALUE (p1);
589 if (mv1 && mv1 != error_mark_node
590 && TREE_CODE (mv1) != ARRAY_TYPE)
591 mv1 = TYPE_MAIN_VARIANT (mv1);
592 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
593 memb; memb = DECL_CHAIN (memb))
594 {
595 tree mv3 = TREE_TYPE (memb);
596 if (mv3 && mv3 != error_mark_node
597 && TREE_CODE (mv3) != ARRAY_TYPE)
598 mv3 = TYPE_MAIN_VARIANT (mv3);
599 if (comptypes (mv3, mv1))
600 {
601 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
602 TREE_VALUE (p1));
603 pedwarn (input_location, OPT_Wpedantic,
604 "function types not truly compatible in ISO C");
605 goto parm_done;
606 }
607 }
608 }
609 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
610 parm_done: ;
611 }
612
613 t1 = build_function_type (valtype, newargs);
614 t1 = qualify_type (t1, t2);
615 /* ... falls through ... */
616 }
617
618 default:
619 return build_type_attribute_variant (t1, attributes);
620 }
621
622 }
623
624 /* Return the type of a conditional expression between pointers to
625 possibly differently qualified versions of compatible types.
626
627 We assume that comp_target_types has already been done and returned
628 nonzero; if that isn't so, this may crash. */
629
630 static tree
631 common_pointer_type (tree t1, tree t2)
632 {
633 tree attributes;
634 tree pointed_to_1, mv1;
635 tree pointed_to_2, mv2;
636 tree target;
637 unsigned target_quals;
638 addr_space_t as1, as2, as_common;
639 int quals1, quals2;
640
641 /* Save time if the two types are the same. */
642
643 if (t1 == t2) return t1;
644
645 /* If one type is nonsense, use the other. */
646 if (t1 == error_mark_node)
647 return t2;
648 if (t2 == error_mark_node)
649 return t1;
650
651 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
652 && TREE_CODE (t2) == POINTER_TYPE);
653
654 /* Merge the attributes. */
655 attributes = targetm.merge_type_attributes (t1, t2);
656
657 /* Find the composite type of the target types, and combine the
658 qualifiers of the two types' targets. Do not lose qualifiers on
659 array element types by taking the TYPE_MAIN_VARIANT. */
660 mv1 = pointed_to_1 = TREE_TYPE (t1);
661 mv2 = pointed_to_2 = TREE_TYPE (t2);
662 if (TREE_CODE (mv1) != ARRAY_TYPE)
663 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
664 if (TREE_CODE (mv2) != ARRAY_TYPE)
665 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
666 target = composite_type (mv1, mv2);
667
668 /* For function types do not merge const qualifiers, but drop them
669 if used inconsistently. The middle-end uses these to mark const
670 and noreturn functions. */
671 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
672 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
673
674 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
675 target_quals = (quals1 & quals2);
676 else
677 target_quals = (quals1 | quals2);
678
679 /* If the two named address spaces are different, determine the common
680 superset address space. This is guaranteed to exist due to the
681 assumption that comp_target_type returned non-zero. */
682 as1 = TYPE_ADDR_SPACE (pointed_to_1);
683 as2 = TYPE_ADDR_SPACE (pointed_to_2);
684 if (!addr_space_superset (as1, as2, &as_common))
685 gcc_unreachable ();
686
687 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
688
689 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
690 return build_type_attribute_variant (t1, attributes);
691 }
692
693 /* Return the common type for two arithmetic types under the usual
694 arithmetic conversions. The default conversions have already been
695 applied, and enumerated types converted to their compatible integer
696 types. The resulting type is unqualified and has no attributes.
697
698 This is the type for the result of most arithmetic operations
699 if the operands have the given two types. */
700
701 static tree
702 c_common_type (tree t1, tree t2)
703 {
704 enum tree_code code1;
705 enum tree_code code2;
706
707 /* If one type is nonsense, use the other. */
708 if (t1 == error_mark_node)
709 return t2;
710 if (t2 == error_mark_node)
711 return t1;
712
713 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
714 t1 = TYPE_MAIN_VARIANT (t1);
715
716 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
717 t2 = TYPE_MAIN_VARIANT (t2);
718
719 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
720 t1 = build_type_attribute_variant (t1, NULL_TREE);
721
722 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
723 t2 = build_type_attribute_variant (t2, NULL_TREE);
724
725 /* Save time if the two types are the same. */
726
727 if (t1 == t2) return t1;
728
729 code1 = TREE_CODE (t1);
730 code2 = TREE_CODE (t2);
731
732 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
733 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
734 || code1 == INTEGER_TYPE);
735 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
736 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
737 || code2 == INTEGER_TYPE);
738
739 /* When one operand is a decimal float type, the other operand cannot be
740 a generic float type or a complex type. We also disallow vector types
741 here. */
742 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
743 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
744 {
745 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
746 {
747 error ("can%'t mix operands of decimal float and vector types");
748 return error_mark_node;
749 }
750 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
751 {
752 error ("can%'t mix operands of decimal float and complex types");
753 return error_mark_node;
754 }
755 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
756 {
757 error ("can%'t mix operands of decimal float and other float types");
758 return error_mark_node;
759 }
760 }
761
762 /* If one type is a vector type, return that type. (How the usual
763 arithmetic conversions apply to the vector types extension is not
764 precisely specified.) */
765 if (code1 == VECTOR_TYPE)
766 return t1;
767
768 if (code2 == VECTOR_TYPE)
769 return t2;
770
771 /* If one type is complex, form the common type of the non-complex
772 components, then make that complex. Use T1 or T2 if it is the
773 required type. */
774 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
775 {
776 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
777 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
778 tree subtype = c_common_type (subtype1, subtype2);
779
780 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
781 return t1;
782 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
783 return t2;
784 else
785 return build_complex_type (subtype);
786 }
787
788 /* If only one is real, use it as the result. */
789
790 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
791 return t1;
792
793 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
794 return t2;
795
796 /* If both are real and either are decimal floating point types, use
797 the decimal floating point type with the greater precision. */
798
799 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
800 {
801 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
802 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
803 return dfloat128_type_node;
804 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
805 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
806 return dfloat64_type_node;
807 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
808 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
809 return dfloat32_type_node;
810 }
811
812 /* Deal with fixed-point types. */
813 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
814 {
815 unsigned int unsignedp = 0, satp = 0;
816 enum machine_mode m1, m2;
817 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
818
819 m1 = TYPE_MODE (t1);
820 m2 = TYPE_MODE (t2);
821
822 /* If one input type is saturating, the result type is saturating. */
823 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
824 satp = 1;
825
826 /* If both fixed-point types are unsigned, the result type is unsigned.
827 When mixing fixed-point and integer types, follow the sign of the
828 fixed-point type.
829 Otherwise, the result type is signed. */
830 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
831 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
832 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t1))
834 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
835 && TYPE_UNSIGNED (t2)))
836 unsignedp = 1;
837
838 /* The result type is signed. */
839 if (unsignedp == 0)
840 {
841 /* If the input type is unsigned, we need to convert to the
842 signed type. */
843 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
844 {
845 enum mode_class mclass = (enum mode_class) 0;
846 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
847 mclass = MODE_FRACT;
848 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
849 mclass = MODE_ACCUM;
850 else
851 gcc_unreachable ();
852 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
853 }
854 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
855 {
856 enum mode_class mclass = (enum mode_class) 0;
857 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
858 mclass = MODE_FRACT;
859 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
860 mclass = MODE_ACCUM;
861 else
862 gcc_unreachable ();
863 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
864 }
865 }
866
867 if (code1 == FIXED_POINT_TYPE)
868 {
869 fbit1 = GET_MODE_FBIT (m1);
870 ibit1 = GET_MODE_IBIT (m1);
871 }
872 else
873 {
874 fbit1 = 0;
875 /* Signed integers need to subtract one sign bit. */
876 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
877 }
878
879 if (code2 == FIXED_POINT_TYPE)
880 {
881 fbit2 = GET_MODE_FBIT (m2);
882 ibit2 = GET_MODE_IBIT (m2);
883 }
884 else
885 {
886 fbit2 = 0;
887 /* Signed integers need to subtract one sign bit. */
888 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
889 }
890
891 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
892 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
893 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
894 satp);
895 }
896
897 /* Both real or both integers; use the one with greater precision. */
898
899 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
900 return t1;
901 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
902 return t2;
903
904 /* Same precision. Prefer long longs to longs to ints when the
905 same precision, following the C99 rules on integer type rank
906 (which are equivalent to the C90 rules for C90 types). */
907
908 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
910 return long_long_unsigned_type_node;
911
912 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
914 {
915 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
916 return long_long_unsigned_type_node;
917 else
918 return long_long_integer_type_node;
919 }
920
921 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
923 return long_unsigned_type_node;
924
925 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
926 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
927 {
928 /* But preserve unsignedness from the other type,
929 since long cannot hold all the values of an unsigned int. */
930 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
931 return long_unsigned_type_node;
932 else
933 return long_integer_type_node;
934 }
935
936 /* Likewise, prefer long double to double even if same size. */
937 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
938 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
939 return long_double_type_node;
940
941 /* Likewise, prefer double to float even if same size.
942 We got a couple of embedded targets with 32 bit doubles, and the
943 pdp11 might have 64 bit floats. */
944 if (TYPE_MAIN_VARIANT (t1) == double_type_node
945 || TYPE_MAIN_VARIANT (t2) == double_type_node)
946 return double_type_node;
947
948 /* Otherwise prefer the unsigned one. */
949
950 if (TYPE_UNSIGNED (t1))
951 return t1;
952 else
953 return t2;
954 }
955 \f
956 /* Wrapper around c_common_type that is used by c-common.c and other
957 front end optimizations that remove promotions. ENUMERAL_TYPEs
958 are allowed here and are converted to their compatible integer types.
959 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
960 preferably a non-Boolean type as the common type. */
961 tree
962 common_type (tree t1, tree t2)
963 {
964 if (TREE_CODE (t1) == ENUMERAL_TYPE)
965 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
966 if (TREE_CODE (t2) == ENUMERAL_TYPE)
967 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
968
969 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
970 if (TREE_CODE (t1) == BOOLEAN_TYPE
971 && TREE_CODE (t2) == BOOLEAN_TYPE)
972 return boolean_type_node;
973
974 /* If either type is BOOLEAN_TYPE, then return the other. */
975 if (TREE_CODE (t1) == BOOLEAN_TYPE)
976 return t2;
977 if (TREE_CODE (t2) == BOOLEAN_TYPE)
978 return t1;
979
980 return c_common_type (t1, t2);
981 }
982
983 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
984 or various other operations. Return 2 if they are compatible
985 but a warning may be needed if you use them together. */
986
987 int
988 comptypes (tree type1, tree type2)
989 {
990 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
991 int val;
992
993 val = comptypes_internal (type1, type2, NULL, NULL);
994 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
995
996 return val;
997 }
998
999 /* Like comptypes, but if it returns non-zero because enum and int are
1000 compatible, it sets *ENUM_AND_INT_P to true. */
1001
1002 static int
1003 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1004 {
1005 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1006 int val;
1007
1008 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1009 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1010
1011 return val;
1012 }
1013
1014 /* Like comptypes, but if it returns nonzero for different types, it
1015 sets *DIFFERENT_TYPES_P to true. */
1016
1017 int
1018 comptypes_check_different_types (tree type1, tree type2,
1019 bool *different_types_p)
1020 {
1021 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1022 int val;
1023
1024 val = comptypes_internal (type1, type2, NULL, different_types_p);
1025 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1026
1027 return val;
1028 }
1029 \f
1030 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1031 or various other operations. Return 2 if they are compatible
1032 but a warning may be needed if you use them together. If
1033 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1034 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1035 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1036 NULL, and the types are compatible but different enough not to be
1037 permitted in C11 typedef redeclarations, then this sets
1038 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1039 false, but may or may not be set if the types are incompatible.
1040 This differs from comptypes, in that we don't free the seen
1041 types. */
1042
1043 static int
1044 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1045 bool *different_types_p)
1046 {
1047 const_tree t1 = type1;
1048 const_tree t2 = type2;
1049 int attrval, val;
1050
1051 /* Suppress errors caused by previously reported errors. */
1052
1053 if (t1 == t2 || !t1 || !t2
1054 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1055 return 1;
1056
1057 /* Enumerated types are compatible with integer types, but this is
1058 not transitive: two enumerated types in the same translation unit
1059 are compatible with each other only if they are the same type. */
1060
1061 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1062 {
1063 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1064 if (TREE_CODE (t2) != VOID_TYPE)
1065 {
1066 if (enum_and_int_p != NULL)
1067 *enum_and_int_p = true;
1068 if (different_types_p != NULL)
1069 *different_types_p = true;
1070 }
1071 }
1072 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1073 {
1074 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1075 if (TREE_CODE (t1) != VOID_TYPE)
1076 {
1077 if (enum_and_int_p != NULL)
1078 *enum_and_int_p = true;
1079 if (different_types_p != NULL)
1080 *different_types_p = true;
1081 }
1082 }
1083
1084 if (t1 == t2)
1085 return 1;
1086
1087 /* Different classes of types can't be compatible. */
1088
1089 if (TREE_CODE (t1) != TREE_CODE (t2))
1090 return 0;
1091
1092 /* Qualifiers must match. C99 6.7.3p9 */
1093
1094 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1095 return 0;
1096
1097 /* Allow for two different type nodes which have essentially the same
1098 definition. Note that we already checked for equality of the type
1099 qualifiers (just above). */
1100
1101 if (TREE_CODE (t1) != ARRAY_TYPE
1102 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1103 return 1;
1104
1105 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1106 if (!(attrval = comp_type_attributes (t1, t2)))
1107 return 0;
1108
1109 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1110 val = 0;
1111
1112 switch (TREE_CODE (t1))
1113 {
1114 case POINTER_TYPE:
1115 /* Do not remove mode or aliasing information. */
1116 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1117 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1118 break;
1119 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1120 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1121 enum_and_int_p, different_types_p));
1122 break;
1123
1124 case FUNCTION_TYPE:
1125 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1126 different_types_p);
1127 break;
1128
1129 case ARRAY_TYPE:
1130 {
1131 tree d1 = TYPE_DOMAIN (t1);
1132 tree d2 = TYPE_DOMAIN (t2);
1133 bool d1_variable, d2_variable;
1134 bool d1_zero, d2_zero;
1135 val = 1;
1136
1137 /* Target types must match incl. qualifiers. */
1138 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1139 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1140 enum_and_int_p,
1141 different_types_p)))
1142 return 0;
1143
1144 if (different_types_p != NULL
1145 && (d1 == 0) != (d2 == 0))
1146 *different_types_p = true;
1147 /* Sizes must match unless one is missing or variable. */
1148 if (d1 == 0 || d2 == 0 || d1 == d2)
1149 break;
1150
1151 d1_zero = !TYPE_MAX_VALUE (d1);
1152 d2_zero = !TYPE_MAX_VALUE (d2);
1153
1154 d1_variable = (!d1_zero
1155 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1156 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1157 d2_variable = (!d2_zero
1158 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1159 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1160 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1161 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1162
1163 if (different_types_p != NULL
1164 && d1_variable != d2_variable)
1165 *different_types_p = true;
1166 if (d1_variable || d2_variable)
1167 break;
1168 if (d1_zero && d2_zero)
1169 break;
1170 if (d1_zero || d2_zero
1171 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1172 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1173 val = 0;
1174
1175 break;
1176 }
1177
1178 case ENUMERAL_TYPE:
1179 case RECORD_TYPE:
1180 case UNION_TYPE:
1181 if (val != 1 && !same_translation_unit_p (t1, t2))
1182 {
1183 tree a1 = TYPE_ATTRIBUTES (t1);
1184 tree a2 = TYPE_ATTRIBUTES (t2);
1185
1186 if (! attribute_list_contained (a1, a2)
1187 && ! attribute_list_contained (a2, a1))
1188 break;
1189
1190 if (attrval != 2)
1191 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1192 different_types_p);
1193 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1194 different_types_p);
1195 }
1196 break;
1197
1198 case VECTOR_TYPE:
1199 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1200 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1201 enum_and_int_p, different_types_p));
1202 break;
1203
1204 default:
1205 break;
1206 }
1207 return attrval == 2 && val == 1 ? 2 : val;
1208 }
1209
1210 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1211 their qualifiers, except for named address spaces. If the pointers point to
1212 different named addresses, then we must determine if one address space is a
1213 subset of the other. */
1214
1215 static int
1216 comp_target_types (location_t location, tree ttl, tree ttr)
1217 {
1218 int val;
1219 tree mvl = TREE_TYPE (ttl);
1220 tree mvr = TREE_TYPE (ttr);
1221 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1222 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1223 addr_space_t as_common;
1224 bool enum_and_int_p;
1225
1226 /* Fail if pointers point to incompatible address spaces. */
1227 if (!addr_space_superset (asl, asr, &as_common))
1228 return 0;
1229
1230 /* Do not lose qualifiers on element types of array types that are
1231 pointer targets by taking their TYPE_MAIN_VARIANT. */
1232 if (TREE_CODE (mvl) != ARRAY_TYPE)
1233 mvl = (TYPE_ATOMIC (mvl)
1234 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1235 : TYPE_MAIN_VARIANT (mvl));
1236 if (TREE_CODE (mvr) != ARRAY_TYPE)
1237 mvr = (TYPE_ATOMIC (mvr)
1238 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1239 : TYPE_MAIN_VARIANT (mvr));
1240 enum_and_int_p = false;
1241 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1242
1243 if (val == 2)
1244 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1245
1246 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1247 warning_at (location, OPT_Wc___compat,
1248 "pointer target types incompatible in C++");
1249
1250 return val;
1251 }
1252 \f
1253 /* Subroutines of `comptypes'. */
1254
1255 /* Determine whether two trees derive from the same translation unit.
1256 If the CONTEXT chain ends in a null, that tree's context is still
1257 being parsed, so if two trees have context chains ending in null,
1258 they're in the same translation unit. */
1259 int
1260 same_translation_unit_p (const_tree t1, const_tree t2)
1261 {
1262 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1263 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1264 {
1265 case tcc_declaration:
1266 t1 = DECL_CONTEXT (t1); break;
1267 case tcc_type:
1268 t1 = TYPE_CONTEXT (t1); break;
1269 case tcc_exceptional:
1270 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1271 default: gcc_unreachable ();
1272 }
1273
1274 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1275 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1276 {
1277 case tcc_declaration:
1278 t2 = DECL_CONTEXT (t2); break;
1279 case tcc_type:
1280 t2 = TYPE_CONTEXT (t2); break;
1281 case tcc_exceptional:
1282 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1283 default: gcc_unreachable ();
1284 }
1285
1286 return t1 == t2;
1287 }
1288
1289 /* Allocate the seen two types, assuming that they are compatible. */
1290
1291 static struct tagged_tu_seen_cache *
1292 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1293 {
1294 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1295 tu->next = tagged_tu_seen_base;
1296 tu->t1 = t1;
1297 tu->t2 = t2;
1298
1299 tagged_tu_seen_base = tu;
1300
1301 /* The C standard says that two structures in different translation
1302 units are compatible with each other only if the types of their
1303 fields are compatible (among other things). We assume that they
1304 are compatible until proven otherwise when building the cache.
1305 An example where this can occur is:
1306 struct a
1307 {
1308 struct a *next;
1309 };
1310 If we are comparing this against a similar struct in another TU,
1311 and did not assume they were compatible, we end up with an infinite
1312 loop. */
1313 tu->val = 1;
1314 return tu;
1315 }
1316
1317 /* Free the seen types until we get to TU_TIL. */
1318
1319 static void
1320 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1321 {
1322 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1323 while (tu != tu_til)
1324 {
1325 const struct tagged_tu_seen_cache *const tu1
1326 = (const struct tagged_tu_seen_cache *) tu;
1327 tu = tu1->next;
1328 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1329 }
1330 tagged_tu_seen_base = tu_til;
1331 }
1332
1333 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1334 compatible. If the two types are not the same (which has been
1335 checked earlier), this can only happen when multiple translation
1336 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1337 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1338 comptypes_internal. */
1339
1340 static int
1341 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1342 bool *enum_and_int_p, bool *different_types_p)
1343 {
1344 tree s1, s2;
1345 bool needs_warning = false;
1346
1347 /* We have to verify that the tags of the types are the same. This
1348 is harder than it looks because this may be a typedef, so we have
1349 to go look at the original type. It may even be a typedef of a
1350 typedef...
1351 In the case of compiler-created builtin structs the TYPE_DECL
1352 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1353 while (TYPE_NAME (t1)
1354 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1355 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1356 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1357
1358 while (TYPE_NAME (t2)
1359 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1360 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1361 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1362
1363 /* C90 didn't have the requirement that the two tags be the same. */
1364 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1365 return 0;
1366
1367 /* C90 didn't say what happened if one or both of the types were
1368 incomplete; we choose to follow C99 rules here, which is that they
1369 are compatible. */
1370 if (TYPE_SIZE (t1) == NULL
1371 || TYPE_SIZE (t2) == NULL)
1372 return 1;
1373
1374 {
1375 const struct tagged_tu_seen_cache * tts_i;
1376 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1377 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1378 return tts_i->val;
1379 }
1380
1381 switch (TREE_CODE (t1))
1382 {
1383 case ENUMERAL_TYPE:
1384 {
1385 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1386 /* Speed up the case where the type values are in the same order. */
1387 tree tv1 = TYPE_VALUES (t1);
1388 tree tv2 = TYPE_VALUES (t2);
1389
1390 if (tv1 == tv2)
1391 {
1392 return 1;
1393 }
1394
1395 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1396 {
1397 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1398 break;
1399 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1400 {
1401 tu->val = 0;
1402 return 0;
1403 }
1404 }
1405
1406 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1407 {
1408 return 1;
1409 }
1410 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1411 {
1412 tu->val = 0;
1413 return 0;
1414 }
1415
1416 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1417 {
1418 tu->val = 0;
1419 return 0;
1420 }
1421
1422 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1423 {
1424 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1425 if (s2 == NULL
1426 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1427 {
1428 tu->val = 0;
1429 return 0;
1430 }
1431 }
1432 return 1;
1433 }
1434
1435 case UNION_TYPE:
1436 {
1437 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1438 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1439 {
1440 tu->val = 0;
1441 return 0;
1442 }
1443
1444 /* Speed up the common case where the fields are in the same order. */
1445 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1446 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1447 {
1448 int result;
1449
1450 if (DECL_NAME (s1) != DECL_NAME (s2))
1451 break;
1452 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1453 enum_and_int_p, different_types_p);
1454
1455 if (result != 1 && !DECL_NAME (s1))
1456 break;
1457 if (result == 0)
1458 {
1459 tu->val = 0;
1460 return 0;
1461 }
1462 if (result == 2)
1463 needs_warning = true;
1464
1465 if (TREE_CODE (s1) == FIELD_DECL
1466 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1467 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1468 {
1469 tu->val = 0;
1470 return 0;
1471 }
1472 }
1473 if (!s1 && !s2)
1474 {
1475 tu->val = needs_warning ? 2 : 1;
1476 return tu->val;
1477 }
1478
1479 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1480 {
1481 bool ok = false;
1482
1483 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1484 if (DECL_NAME (s1) == DECL_NAME (s2))
1485 {
1486 int result;
1487
1488 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1489 enum_and_int_p,
1490 different_types_p);
1491
1492 if (result != 1 && !DECL_NAME (s1))
1493 continue;
1494 if (result == 0)
1495 {
1496 tu->val = 0;
1497 return 0;
1498 }
1499 if (result == 2)
1500 needs_warning = true;
1501
1502 if (TREE_CODE (s1) == FIELD_DECL
1503 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1504 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1505 break;
1506
1507 ok = true;
1508 break;
1509 }
1510 if (!ok)
1511 {
1512 tu->val = 0;
1513 return 0;
1514 }
1515 }
1516 tu->val = needs_warning ? 2 : 10;
1517 return tu->val;
1518 }
1519
1520 case RECORD_TYPE:
1521 {
1522 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1523
1524 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1525 s1 && s2;
1526 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1527 {
1528 int result;
1529 if (TREE_CODE (s1) != TREE_CODE (s2)
1530 || DECL_NAME (s1) != DECL_NAME (s2))
1531 break;
1532 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1533 enum_and_int_p, different_types_p);
1534 if (result == 0)
1535 break;
1536 if (result == 2)
1537 needs_warning = true;
1538
1539 if (TREE_CODE (s1) == FIELD_DECL
1540 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1541 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1542 break;
1543 }
1544 if (s1 && s2)
1545 tu->val = 0;
1546 else
1547 tu->val = needs_warning ? 2 : 1;
1548 return tu->val;
1549 }
1550
1551 default:
1552 gcc_unreachable ();
1553 }
1554 }
1555
1556 /* Return 1 if two function types F1 and F2 are compatible.
1557 If either type specifies no argument types,
1558 the other must specify a fixed number of self-promoting arg types.
1559 Otherwise, if one type specifies only the number of arguments,
1560 the other must specify that number of self-promoting arg types.
1561 Otherwise, the argument types must match.
1562 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1563
1564 static int
1565 function_types_compatible_p (const_tree f1, const_tree f2,
1566 bool *enum_and_int_p, bool *different_types_p)
1567 {
1568 tree args1, args2;
1569 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1570 int val = 1;
1571 int val1;
1572 tree ret1, ret2;
1573
1574 ret1 = TREE_TYPE (f1);
1575 ret2 = TREE_TYPE (f2);
1576
1577 /* 'volatile' qualifiers on a function's return type used to mean
1578 the function is noreturn. */
1579 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1580 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1581 if (TYPE_VOLATILE (ret1))
1582 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1583 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1584 if (TYPE_VOLATILE (ret2))
1585 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1586 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1587 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1588 if (val == 0)
1589 return 0;
1590
1591 args1 = TYPE_ARG_TYPES (f1);
1592 args2 = TYPE_ARG_TYPES (f2);
1593
1594 if (different_types_p != NULL
1595 && (args1 == 0) != (args2 == 0))
1596 *different_types_p = true;
1597
1598 /* An unspecified parmlist matches any specified parmlist
1599 whose argument types don't need default promotions. */
1600
1601 if (args1 == 0)
1602 {
1603 if (!self_promoting_args_p (args2))
1604 return 0;
1605 /* If one of these types comes from a non-prototype fn definition,
1606 compare that with the other type's arglist.
1607 If they don't match, ask for a warning (but no error). */
1608 if (TYPE_ACTUAL_ARG_TYPES (f1)
1609 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1610 enum_and_int_p, different_types_p))
1611 val = 2;
1612 return val;
1613 }
1614 if (args2 == 0)
1615 {
1616 if (!self_promoting_args_p (args1))
1617 return 0;
1618 if (TYPE_ACTUAL_ARG_TYPES (f2)
1619 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1620 enum_and_int_p, different_types_p))
1621 val = 2;
1622 return val;
1623 }
1624
1625 /* Both types have argument lists: compare them and propagate results. */
1626 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1627 different_types_p);
1628 return val1 != 1 ? val1 : val;
1629 }
1630
1631 /* Check two lists of types for compatibility, returning 0 for
1632 incompatible, 1 for compatible, or 2 for compatible with
1633 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1634 comptypes_internal. */
1635
1636 static int
1637 type_lists_compatible_p (const_tree args1, const_tree args2,
1638 bool *enum_and_int_p, bool *different_types_p)
1639 {
1640 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1641 int val = 1;
1642 int newval = 0;
1643
1644 while (1)
1645 {
1646 tree a1, mv1, a2, mv2;
1647 if (args1 == 0 && args2 == 0)
1648 return val;
1649 /* If one list is shorter than the other,
1650 they fail to match. */
1651 if (args1 == 0 || args2 == 0)
1652 return 0;
1653 mv1 = a1 = TREE_VALUE (args1);
1654 mv2 = a2 = TREE_VALUE (args2);
1655 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1656 mv1 = (TYPE_ATOMIC (mv1)
1657 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1658 TYPE_QUAL_ATOMIC)
1659 : TYPE_MAIN_VARIANT (mv1));
1660 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1661 mv2 = (TYPE_ATOMIC (mv2)
1662 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1663 TYPE_QUAL_ATOMIC)
1664 : TYPE_MAIN_VARIANT (mv2));
1665 /* A null pointer instead of a type
1666 means there is supposed to be an argument
1667 but nothing is specified about what type it has.
1668 So match anything that self-promotes. */
1669 if (different_types_p != NULL
1670 && (a1 == 0) != (a2 == 0))
1671 *different_types_p = true;
1672 if (a1 == 0)
1673 {
1674 if (c_type_promotes_to (a2) != a2)
1675 return 0;
1676 }
1677 else if (a2 == 0)
1678 {
1679 if (c_type_promotes_to (a1) != a1)
1680 return 0;
1681 }
1682 /* If one of the lists has an error marker, ignore this arg. */
1683 else if (TREE_CODE (a1) == ERROR_MARK
1684 || TREE_CODE (a2) == ERROR_MARK)
1685 ;
1686 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1687 different_types_p)))
1688 {
1689 if (different_types_p != NULL)
1690 *different_types_p = true;
1691 /* Allow wait (union {union wait *u; int *i} *)
1692 and wait (union wait *) to be compatible. */
1693 if (TREE_CODE (a1) == UNION_TYPE
1694 && (TYPE_NAME (a1) == 0
1695 || TYPE_TRANSPARENT_AGGR (a1))
1696 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1697 && tree_int_cst_equal (TYPE_SIZE (a1),
1698 TYPE_SIZE (a2)))
1699 {
1700 tree memb;
1701 for (memb = TYPE_FIELDS (a1);
1702 memb; memb = DECL_CHAIN (memb))
1703 {
1704 tree mv3 = TREE_TYPE (memb);
1705 if (mv3 && mv3 != error_mark_node
1706 && TREE_CODE (mv3) != ARRAY_TYPE)
1707 mv3 = (TYPE_ATOMIC (mv3)
1708 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1709 TYPE_QUAL_ATOMIC)
1710 : TYPE_MAIN_VARIANT (mv3));
1711 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1712 different_types_p))
1713 break;
1714 }
1715 if (memb == 0)
1716 return 0;
1717 }
1718 else if (TREE_CODE (a2) == UNION_TYPE
1719 && (TYPE_NAME (a2) == 0
1720 || TYPE_TRANSPARENT_AGGR (a2))
1721 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1722 && tree_int_cst_equal (TYPE_SIZE (a2),
1723 TYPE_SIZE (a1)))
1724 {
1725 tree memb;
1726 for (memb = TYPE_FIELDS (a2);
1727 memb; memb = DECL_CHAIN (memb))
1728 {
1729 tree mv3 = TREE_TYPE (memb);
1730 if (mv3 && mv3 != error_mark_node
1731 && TREE_CODE (mv3) != ARRAY_TYPE)
1732 mv3 = (TYPE_ATOMIC (mv3)
1733 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1734 TYPE_QUAL_ATOMIC)
1735 : TYPE_MAIN_VARIANT (mv3));
1736 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1737 different_types_p))
1738 break;
1739 }
1740 if (memb == 0)
1741 return 0;
1742 }
1743 else
1744 return 0;
1745 }
1746
1747 /* comptypes said ok, but record if it said to warn. */
1748 if (newval > val)
1749 val = newval;
1750
1751 args1 = TREE_CHAIN (args1);
1752 args2 = TREE_CHAIN (args2);
1753 }
1754 }
1755 \f
1756 /* Compute the size to increment a pointer by. */
1757
1758 static tree
1759 c_size_in_bytes (const_tree type)
1760 {
1761 enum tree_code code = TREE_CODE (type);
1762
1763 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1764 return size_one_node;
1765
1766 if (!COMPLETE_OR_VOID_TYPE_P (type))
1767 {
1768 error ("arithmetic on pointer to an incomplete type");
1769 return size_one_node;
1770 }
1771
1772 /* Convert in case a char is more than one unit. */
1773 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1774 size_int (TYPE_PRECISION (char_type_node)
1775 / BITS_PER_UNIT));
1776 }
1777 \f
1778 /* Return either DECL or its known constant value (if it has one). */
1779
1780 tree
1781 decl_constant_value (tree decl)
1782 {
1783 if (/* Don't change a variable array bound or initial value to a constant
1784 in a place where a variable is invalid. Note that DECL_INITIAL
1785 isn't valid for a PARM_DECL. */
1786 current_function_decl != 0
1787 && TREE_CODE (decl) != PARM_DECL
1788 && !TREE_THIS_VOLATILE (decl)
1789 && TREE_READONLY (decl)
1790 && DECL_INITIAL (decl) != 0
1791 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1792 /* This is invalid if initial value is not constant.
1793 If it has either a function call, a memory reference,
1794 or a variable, then re-evaluating it could give different results. */
1795 && TREE_CONSTANT (DECL_INITIAL (decl))
1796 /* Check for cases where this is sub-optimal, even though valid. */
1797 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1798 return DECL_INITIAL (decl);
1799 return decl;
1800 }
1801
1802 /* Convert the array expression EXP to a pointer. */
1803 static tree
1804 array_to_pointer_conversion (location_t loc, tree exp)
1805 {
1806 tree orig_exp = exp;
1807 tree type = TREE_TYPE (exp);
1808 tree adr;
1809 tree restype = TREE_TYPE (type);
1810 tree ptrtype;
1811
1812 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1813
1814 STRIP_TYPE_NOPS (exp);
1815
1816 if (TREE_NO_WARNING (orig_exp))
1817 TREE_NO_WARNING (exp) = 1;
1818
1819 ptrtype = build_pointer_type (restype);
1820
1821 if (TREE_CODE (exp) == INDIRECT_REF)
1822 return convert (ptrtype, TREE_OPERAND (exp, 0));
1823
1824 /* In C++ array compound literals are temporary objects unless they are
1825 const or appear in namespace scope, so they are destroyed too soon
1826 to use them for much of anything (c++/53220). */
1827 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1828 {
1829 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1830 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1831 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1832 "converting an array compound literal to a pointer "
1833 "is ill-formed in C++");
1834 }
1835
1836 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1837 return convert (ptrtype, adr);
1838 }
1839
1840 /* Convert the function expression EXP to a pointer. */
1841 static tree
1842 function_to_pointer_conversion (location_t loc, tree exp)
1843 {
1844 tree orig_exp = exp;
1845
1846 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1847
1848 STRIP_TYPE_NOPS (exp);
1849
1850 if (TREE_NO_WARNING (orig_exp))
1851 TREE_NO_WARNING (exp) = 1;
1852
1853 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1854 }
1855
1856 /* Mark EXP as read, not just set, for set but not used -Wunused
1857 warning purposes. */
1858
1859 void
1860 mark_exp_read (tree exp)
1861 {
1862 switch (TREE_CODE (exp))
1863 {
1864 case VAR_DECL:
1865 case PARM_DECL:
1866 DECL_READ_P (exp) = 1;
1867 break;
1868 case ARRAY_REF:
1869 case COMPONENT_REF:
1870 case MODIFY_EXPR:
1871 case REALPART_EXPR:
1872 case IMAGPART_EXPR:
1873 CASE_CONVERT:
1874 case ADDR_EXPR:
1875 mark_exp_read (TREE_OPERAND (exp, 0));
1876 break;
1877 case COMPOUND_EXPR:
1878 case C_MAYBE_CONST_EXPR:
1879 mark_exp_read (TREE_OPERAND (exp, 1));
1880 break;
1881 default:
1882 break;
1883 }
1884 }
1885
1886 /* Perform the default conversion of arrays and functions to pointers.
1887 Return the result of converting EXP. For any other expression, just
1888 return EXP.
1889
1890 LOC is the location of the expression. */
1891
1892 struct c_expr
1893 default_function_array_conversion (location_t loc, struct c_expr exp)
1894 {
1895 tree orig_exp = exp.value;
1896 tree type = TREE_TYPE (exp.value);
1897 enum tree_code code = TREE_CODE (type);
1898
1899 switch (code)
1900 {
1901 case ARRAY_TYPE:
1902 {
1903 bool not_lvalue = false;
1904 bool lvalue_array_p;
1905
1906 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1907 || CONVERT_EXPR_P (exp.value))
1908 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1909 {
1910 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1911 not_lvalue = true;
1912 exp.value = TREE_OPERAND (exp.value, 0);
1913 }
1914
1915 if (TREE_NO_WARNING (orig_exp))
1916 TREE_NO_WARNING (exp.value) = 1;
1917
1918 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1919 if (!flag_isoc99 && !lvalue_array_p)
1920 {
1921 /* Before C99, non-lvalue arrays do not decay to pointers.
1922 Normally, using such an array would be invalid; but it can
1923 be used correctly inside sizeof or as a statement expression.
1924 Thus, do not give an error here; an error will result later. */
1925 return exp;
1926 }
1927
1928 exp.value = array_to_pointer_conversion (loc, exp.value);
1929 }
1930 break;
1931 case FUNCTION_TYPE:
1932 exp.value = function_to_pointer_conversion (loc, exp.value);
1933 break;
1934 default:
1935 break;
1936 }
1937
1938 return exp;
1939 }
1940
1941 struct c_expr
1942 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1943 {
1944 mark_exp_read (exp.value);
1945 return default_function_array_conversion (loc, exp);
1946 }
1947
1948 /* Return whether EXPR should be treated as an atomic lvalue for the
1949 purposes of load and store handling. */
1950
1951 static bool
1952 really_atomic_lvalue (tree expr)
1953 {
1954 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
1955 return false;
1956 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1957 return false;
1958 if (!lvalue_p (expr))
1959 return false;
1960
1961 /* Ignore _Atomic on register variables, since their addresses can't
1962 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1963 sequences wouldn't work. Ignore _Atomic on structures containing
1964 bit-fields, since accessing elements of atomic structures or
1965 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1966 it's undefined at translation time or execution time, and the
1967 normal atomic sequences again wouldn't work. */
1968 while (handled_component_p (expr))
1969 {
1970 if (TREE_CODE (expr) == COMPONENT_REF
1971 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1972 return false;
1973 expr = TREE_OPERAND (expr, 0);
1974 }
1975 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1976 return false;
1977 return true;
1978 }
1979
1980 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1981 including converting functions and arrays to pointers if CONVERT_P.
1982 If READ_P, also mark the expression as having been read. */
1983
1984 struct c_expr
1985 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1986 bool convert_p, bool read_p)
1987 {
1988 if (read_p)
1989 mark_exp_read (exp.value);
1990 if (convert_p)
1991 exp = default_function_array_conversion (loc, exp);
1992 if (really_atomic_lvalue (exp.value))
1993 {
1994 vec<tree, va_gc> *params;
1995 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1996 tree expr_type = TREE_TYPE (exp.value);
1997 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1998 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
1999
2000 gcc_assert (TYPE_ATOMIC (expr_type));
2001
2002 /* Expansion of a generic atomic load may require an addition
2003 element, so allocate enough to prevent a resize. */
2004 vec_alloc (params, 4);
2005
2006 /* Remove the qualifiers for the rest of the expressions and
2007 create the VAL temp variable to hold the RHS. */
2008 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2009 tmp = create_tmp_var (nonatomic_type, NULL);
2010 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2011 TREE_ADDRESSABLE (tmp) = 1;
2012 TREE_NO_WARNING (tmp) = 1;
2013
2014 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2015 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2016 params->quick_push (expr_addr);
2017 params->quick_push (tmp_addr);
2018 params->quick_push (seq_cst);
2019 func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2020
2021 /* EXPR is always read. */
2022 mark_exp_read (exp.value);
2023
2024 /* Return tmp which contains the value loaded. */
2025 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2026 }
2027 return exp;
2028 }
2029
2030 /* EXP is an expression of integer type. Apply the integer promotions
2031 to it and return the promoted value. */
2032
2033 tree
2034 perform_integral_promotions (tree exp)
2035 {
2036 tree type = TREE_TYPE (exp);
2037 enum tree_code code = TREE_CODE (type);
2038
2039 gcc_assert (INTEGRAL_TYPE_P (type));
2040
2041 /* Normally convert enums to int,
2042 but convert wide enums to something wider. */
2043 if (code == ENUMERAL_TYPE)
2044 {
2045 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2046 TYPE_PRECISION (integer_type_node)),
2047 ((TYPE_PRECISION (type)
2048 >= TYPE_PRECISION (integer_type_node))
2049 && TYPE_UNSIGNED (type)));
2050
2051 return convert (type, exp);
2052 }
2053
2054 /* ??? This should no longer be needed now bit-fields have their
2055 proper types. */
2056 if (TREE_CODE (exp) == COMPONENT_REF
2057 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2058 /* If it's thinner than an int, promote it like a
2059 c_promoting_integer_type_p, otherwise leave it alone. */
2060 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2061 TYPE_PRECISION (integer_type_node)))
2062 return convert (integer_type_node, exp);
2063
2064 if (c_promoting_integer_type_p (type))
2065 {
2066 /* Preserve unsignedness if not really getting any wider. */
2067 if (TYPE_UNSIGNED (type)
2068 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2069 return convert (unsigned_type_node, exp);
2070
2071 return convert (integer_type_node, exp);
2072 }
2073
2074 return exp;
2075 }
2076
2077
2078 /* Perform default promotions for C data used in expressions.
2079 Enumeral types or short or char are converted to int.
2080 In addition, manifest constants symbols are replaced by their values. */
2081
2082 tree
2083 default_conversion (tree exp)
2084 {
2085 tree orig_exp;
2086 tree type = TREE_TYPE (exp);
2087 enum tree_code code = TREE_CODE (type);
2088 tree promoted_type;
2089
2090 mark_exp_read (exp);
2091
2092 /* Functions and arrays have been converted during parsing. */
2093 gcc_assert (code != FUNCTION_TYPE);
2094 if (code == ARRAY_TYPE)
2095 return exp;
2096
2097 /* Constants can be used directly unless they're not loadable. */
2098 if (TREE_CODE (exp) == CONST_DECL)
2099 exp = DECL_INITIAL (exp);
2100
2101 /* Strip no-op conversions. */
2102 orig_exp = exp;
2103 STRIP_TYPE_NOPS (exp);
2104
2105 if (TREE_NO_WARNING (orig_exp))
2106 TREE_NO_WARNING (exp) = 1;
2107
2108 if (code == VOID_TYPE)
2109 {
2110 error ("void value not ignored as it ought to be");
2111 return error_mark_node;
2112 }
2113
2114 exp = require_complete_type (exp);
2115 if (exp == error_mark_node)
2116 return error_mark_node;
2117
2118 promoted_type = targetm.promoted_type (type);
2119 if (promoted_type)
2120 return convert (promoted_type, exp);
2121
2122 if (INTEGRAL_TYPE_P (type))
2123 return perform_integral_promotions (exp);
2124
2125 return exp;
2126 }
2127 \f
2128 /* Look up COMPONENT in a structure or union TYPE.
2129
2130 If the component name is not found, returns NULL_TREE. Otherwise,
2131 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2132 stepping down the chain to the component, which is in the last
2133 TREE_VALUE of the list. Normally the list is of length one, but if
2134 the component is embedded within (nested) anonymous structures or
2135 unions, the list steps down the chain to the component. */
2136
2137 static tree
2138 lookup_field (tree type, tree component)
2139 {
2140 tree field;
2141
2142 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2143 to the field elements. Use a binary search on this array to quickly
2144 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2145 will always be set for structures which have many elements. */
2146
2147 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2148 {
2149 int bot, top, half;
2150 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2151
2152 field = TYPE_FIELDS (type);
2153 bot = 0;
2154 top = TYPE_LANG_SPECIFIC (type)->s->len;
2155 while (top - bot > 1)
2156 {
2157 half = (top - bot + 1) >> 1;
2158 field = field_array[bot+half];
2159
2160 if (DECL_NAME (field) == NULL_TREE)
2161 {
2162 /* Step through all anon unions in linear fashion. */
2163 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2164 {
2165 field = field_array[bot++];
2166 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2167 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2168 {
2169 tree anon = lookup_field (TREE_TYPE (field), component);
2170
2171 if (anon)
2172 return tree_cons (NULL_TREE, field, anon);
2173
2174 /* The Plan 9 compiler permits referring
2175 directly to an anonymous struct/union field
2176 using a typedef name. */
2177 if (flag_plan9_extensions
2178 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2179 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2180 == TYPE_DECL)
2181 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2182 == component))
2183 break;
2184 }
2185 }
2186
2187 /* Entire record is only anon unions. */
2188 if (bot > top)
2189 return NULL_TREE;
2190
2191 /* Restart the binary search, with new lower bound. */
2192 continue;
2193 }
2194
2195 if (DECL_NAME (field) == component)
2196 break;
2197 if (DECL_NAME (field) < component)
2198 bot += half;
2199 else
2200 top = bot + half;
2201 }
2202
2203 if (DECL_NAME (field_array[bot]) == component)
2204 field = field_array[bot];
2205 else if (DECL_NAME (field) != component)
2206 return NULL_TREE;
2207 }
2208 else
2209 {
2210 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2211 {
2212 if (DECL_NAME (field) == NULL_TREE
2213 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2214 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2215 {
2216 tree anon = lookup_field (TREE_TYPE (field), component);
2217
2218 if (anon)
2219 return tree_cons (NULL_TREE, field, anon);
2220
2221 /* The Plan 9 compiler permits referring directly to an
2222 anonymous struct/union field using a typedef
2223 name. */
2224 if (flag_plan9_extensions
2225 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2226 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2227 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2228 == component))
2229 break;
2230 }
2231
2232 if (DECL_NAME (field) == component)
2233 break;
2234 }
2235
2236 if (field == NULL_TREE)
2237 return NULL_TREE;
2238 }
2239
2240 return tree_cons (NULL_TREE, field, NULL_TREE);
2241 }
2242
2243 /* Make an expression to refer to the COMPONENT field of structure or
2244 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2245 location of the COMPONENT_REF. */
2246
2247 tree
2248 build_component_ref (location_t loc, tree datum, tree component)
2249 {
2250 tree type = TREE_TYPE (datum);
2251 enum tree_code code = TREE_CODE (type);
2252 tree field = NULL;
2253 tree ref;
2254 bool datum_lvalue = lvalue_p (datum);
2255
2256 if (!objc_is_public (datum, component))
2257 return error_mark_node;
2258
2259 /* Detect Objective-C property syntax object.property. */
2260 if (c_dialect_objc ()
2261 && (ref = objc_maybe_build_component_ref (datum, component)))
2262 return ref;
2263
2264 /* See if there is a field or component with name COMPONENT. */
2265
2266 if (code == RECORD_TYPE || code == UNION_TYPE)
2267 {
2268 if (!COMPLETE_TYPE_P (type))
2269 {
2270 c_incomplete_type_error (NULL_TREE, type);
2271 return error_mark_node;
2272 }
2273
2274 field = lookup_field (type, component);
2275
2276 if (!field)
2277 {
2278 error_at (loc, "%qT has no member named %qE", type, component);
2279 return error_mark_node;
2280 }
2281
2282 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2283 This might be better solved in future the way the C++ front
2284 end does it - by giving the anonymous entities each a
2285 separate name and type, and then have build_component_ref
2286 recursively call itself. We can't do that here. */
2287 do
2288 {
2289 tree subdatum = TREE_VALUE (field);
2290 int quals;
2291 tree subtype;
2292 bool use_datum_quals;
2293
2294 if (TREE_TYPE (subdatum) == error_mark_node)
2295 return error_mark_node;
2296
2297 /* If this is an rvalue, it does not have qualifiers in C
2298 standard terms and we must avoid propagating such
2299 qualifiers down to a non-lvalue array that is then
2300 converted to a pointer. */
2301 use_datum_quals = (datum_lvalue
2302 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2303
2304 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2305 if (use_datum_quals)
2306 quals |= TYPE_QUALS (TREE_TYPE (datum));
2307 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2308
2309 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2310 NULL_TREE);
2311 SET_EXPR_LOCATION (ref, loc);
2312 if (TREE_READONLY (subdatum)
2313 || (use_datum_quals && TREE_READONLY (datum)))
2314 TREE_READONLY (ref) = 1;
2315 if (TREE_THIS_VOLATILE (subdatum)
2316 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2317 TREE_THIS_VOLATILE (ref) = 1;
2318
2319 if (TREE_DEPRECATED (subdatum))
2320 warn_deprecated_use (subdatum, NULL_TREE);
2321
2322 datum = ref;
2323
2324 field = TREE_CHAIN (field);
2325 }
2326 while (field);
2327
2328 return ref;
2329 }
2330 else if (code != ERROR_MARK)
2331 error_at (loc,
2332 "request for member %qE in something not a structure or union",
2333 component);
2334
2335 return error_mark_node;
2336 }
2337 \f
2338 /* Given an expression PTR for a pointer, return an expression
2339 for the value pointed to.
2340 ERRORSTRING is the name of the operator to appear in error messages.
2341
2342 LOC is the location to use for the generated tree. */
2343
2344 tree
2345 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2346 {
2347 tree pointer = default_conversion (ptr);
2348 tree type = TREE_TYPE (pointer);
2349 tree ref;
2350
2351 if (TREE_CODE (type) == POINTER_TYPE)
2352 {
2353 if (CONVERT_EXPR_P (pointer)
2354 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2355 {
2356 /* If a warning is issued, mark it to avoid duplicates from
2357 the backend. This only needs to be done at
2358 warn_strict_aliasing > 2. */
2359 if (warn_strict_aliasing > 2)
2360 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2361 type, TREE_OPERAND (pointer, 0)))
2362 TREE_NO_WARNING (pointer) = 1;
2363 }
2364
2365 if (TREE_CODE (pointer) == ADDR_EXPR
2366 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2367 == TREE_TYPE (type)))
2368 {
2369 ref = TREE_OPERAND (pointer, 0);
2370 protected_set_expr_location (ref, loc);
2371 return ref;
2372 }
2373 else
2374 {
2375 tree t = TREE_TYPE (type);
2376
2377 ref = build1 (INDIRECT_REF, t, pointer);
2378
2379 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2380 {
2381 error_at (loc, "dereferencing pointer to incomplete type");
2382 return error_mark_node;
2383 }
2384 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2385 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2386
2387 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2388 so that we get the proper error message if the result is used
2389 to assign to. Also, &* is supposed to be a no-op.
2390 And ANSI C seems to specify that the type of the result
2391 should be the const type. */
2392 /* A de-reference of a pointer to const is not a const. It is valid
2393 to change it via some other pointer. */
2394 TREE_READONLY (ref) = TYPE_READONLY (t);
2395 TREE_SIDE_EFFECTS (ref)
2396 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2397 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2398 protected_set_expr_location (ref, loc);
2399 return ref;
2400 }
2401 }
2402 else if (TREE_CODE (pointer) != ERROR_MARK)
2403 invalid_indirection_error (loc, type, errstring);
2404
2405 return error_mark_node;
2406 }
2407
2408 /* This handles expressions of the form "a[i]", which denotes
2409 an array reference.
2410
2411 This is logically equivalent in C to *(a+i), but we may do it differently.
2412 If A is a variable or a member, we generate a primitive ARRAY_REF.
2413 This avoids forcing the array out of registers, and can work on
2414 arrays that are not lvalues (for example, members of structures returned
2415 by functions).
2416
2417 For vector types, allow vector[i] but not i[vector], and create
2418 *(((type*)&vectortype) + i) for the expression.
2419
2420 LOC is the location to use for the returned expression. */
2421
2422 tree
2423 build_array_ref (location_t loc, tree array, tree index)
2424 {
2425 tree ret;
2426 bool swapped = false;
2427 if (TREE_TYPE (array) == error_mark_node
2428 || TREE_TYPE (index) == error_mark_node)
2429 return error_mark_node;
2430
2431 if (flag_cilkplus && contains_array_notation_expr (index))
2432 {
2433 size_t rank = 0;
2434 if (!find_rank (loc, index, index, true, &rank))
2435 return error_mark_node;
2436 if (rank > 1)
2437 {
2438 error_at (loc, "rank of the array's index is greater than 1");
2439 return error_mark_node;
2440 }
2441 }
2442 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2443 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2444 /* Allow vector[index] but not index[vector]. */
2445 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2446 {
2447 tree temp;
2448 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2449 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2450 {
2451 error_at (loc,
2452 "subscripted value is neither array nor pointer nor vector");
2453
2454 return error_mark_node;
2455 }
2456 temp = array;
2457 array = index;
2458 index = temp;
2459 swapped = true;
2460 }
2461
2462 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2463 {
2464 error_at (loc, "array subscript is not an integer");
2465 return error_mark_node;
2466 }
2467
2468 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2469 {
2470 error_at (loc, "subscripted value is pointer to function");
2471 return error_mark_node;
2472 }
2473
2474 /* ??? Existing practice has been to warn only when the char
2475 index is syntactically the index, not for char[array]. */
2476 if (!swapped)
2477 warn_array_subscript_with_type_char (index);
2478
2479 /* Apply default promotions *after* noticing character types. */
2480 index = default_conversion (index);
2481
2482 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2483
2484 convert_vector_to_pointer_for_subscript (loc, &array, index);
2485
2486 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2487 {
2488 tree rval, type;
2489
2490 /* An array that is indexed by a non-constant
2491 cannot be stored in a register; we must be able to do
2492 address arithmetic on its address.
2493 Likewise an array of elements of variable size. */
2494 if (TREE_CODE (index) != INTEGER_CST
2495 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2496 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2497 {
2498 if (!c_mark_addressable (array))
2499 return error_mark_node;
2500 }
2501 /* An array that is indexed by a constant value which is not within
2502 the array bounds cannot be stored in a register either; because we
2503 would get a crash in store_bit_field/extract_bit_field when trying
2504 to access a non-existent part of the register. */
2505 if (TREE_CODE (index) == INTEGER_CST
2506 && TYPE_DOMAIN (TREE_TYPE (array))
2507 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2508 {
2509 if (!c_mark_addressable (array))
2510 return error_mark_node;
2511 }
2512
2513 if (pedantic)
2514 {
2515 tree foo = array;
2516 while (TREE_CODE (foo) == COMPONENT_REF)
2517 foo = TREE_OPERAND (foo, 0);
2518 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2519 pedwarn (loc, OPT_Wpedantic,
2520 "ISO C forbids subscripting %<register%> array");
2521 else if (!flag_isoc99 && !lvalue_p (foo))
2522 pedwarn (loc, OPT_Wpedantic,
2523 "ISO C90 forbids subscripting non-lvalue array");
2524 }
2525
2526 type = TREE_TYPE (TREE_TYPE (array));
2527 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2528 /* Array ref is const/volatile if the array elements are
2529 or if the array is. */
2530 TREE_READONLY (rval)
2531 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2532 | TREE_READONLY (array));
2533 TREE_SIDE_EFFECTS (rval)
2534 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2535 | TREE_SIDE_EFFECTS (array));
2536 TREE_THIS_VOLATILE (rval)
2537 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2538 /* This was added by rms on 16 Nov 91.
2539 It fixes vol struct foo *a; a->elts[1]
2540 in an inline function.
2541 Hope it doesn't break something else. */
2542 | TREE_THIS_VOLATILE (array));
2543 ret = require_complete_type (rval);
2544 protected_set_expr_location (ret, loc);
2545 return ret;
2546 }
2547 else
2548 {
2549 tree ar = default_conversion (array);
2550
2551 if (ar == error_mark_node)
2552 return ar;
2553
2554 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2555 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2556
2557 return build_indirect_ref
2558 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2559 RO_ARRAY_INDEXING);
2560 }
2561 }
2562 \f
2563 /* Build an external reference to identifier ID. FUN indicates
2564 whether this will be used for a function call. LOC is the source
2565 location of the identifier. This sets *TYPE to the type of the
2566 identifier, which is not the same as the type of the returned value
2567 for CONST_DECLs defined as enum constants. If the type of the
2568 identifier is not available, *TYPE is set to NULL. */
2569 tree
2570 build_external_ref (location_t loc, tree id, int fun, tree *type)
2571 {
2572 tree ref;
2573 tree decl = lookup_name (id);
2574
2575 /* In Objective-C, an instance variable (ivar) may be preferred to
2576 whatever lookup_name() found. */
2577 decl = objc_lookup_ivar (decl, id);
2578
2579 *type = NULL;
2580 if (decl && decl != error_mark_node)
2581 {
2582 ref = decl;
2583 *type = TREE_TYPE (ref);
2584 }
2585 else if (fun)
2586 /* Implicit function declaration. */
2587 ref = implicitly_declare (loc, id);
2588 else if (decl == error_mark_node)
2589 /* Don't complain about something that's already been
2590 complained about. */
2591 return error_mark_node;
2592 else
2593 {
2594 undeclared_variable (loc, id);
2595 return error_mark_node;
2596 }
2597
2598 if (TREE_TYPE (ref) == error_mark_node)
2599 return error_mark_node;
2600
2601 if (TREE_DEPRECATED (ref))
2602 warn_deprecated_use (ref, NULL_TREE);
2603
2604 /* Recursive call does not count as usage. */
2605 if (ref != current_function_decl)
2606 {
2607 TREE_USED (ref) = 1;
2608 }
2609
2610 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2611 {
2612 if (!in_sizeof && !in_typeof)
2613 C_DECL_USED (ref) = 1;
2614 else if (DECL_INITIAL (ref) == 0
2615 && DECL_EXTERNAL (ref)
2616 && !TREE_PUBLIC (ref))
2617 record_maybe_used_decl (ref);
2618 }
2619
2620 if (TREE_CODE (ref) == CONST_DECL)
2621 {
2622 used_types_insert (TREE_TYPE (ref));
2623
2624 if (warn_cxx_compat
2625 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2626 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2627 {
2628 warning_at (loc, OPT_Wc___compat,
2629 ("enum constant defined in struct or union "
2630 "is not visible in C++"));
2631 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2632 }
2633
2634 ref = DECL_INITIAL (ref);
2635 TREE_CONSTANT (ref) = 1;
2636 }
2637 else if (current_function_decl != 0
2638 && !DECL_FILE_SCOPE_P (current_function_decl)
2639 && (TREE_CODE (ref) == VAR_DECL
2640 || TREE_CODE (ref) == PARM_DECL
2641 || TREE_CODE (ref) == FUNCTION_DECL))
2642 {
2643 tree context = decl_function_context (ref);
2644
2645 if (context != 0 && context != current_function_decl)
2646 DECL_NONLOCAL (ref) = 1;
2647 }
2648 /* C99 6.7.4p3: An inline definition of a function with external
2649 linkage ... shall not contain a reference to an identifier with
2650 internal linkage. */
2651 else if (current_function_decl != 0
2652 && DECL_DECLARED_INLINE_P (current_function_decl)
2653 && DECL_EXTERNAL (current_function_decl)
2654 && VAR_OR_FUNCTION_DECL_P (ref)
2655 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2656 && ! TREE_PUBLIC (ref)
2657 && DECL_CONTEXT (ref) != current_function_decl)
2658 record_inline_static (loc, current_function_decl, ref,
2659 csi_internal);
2660
2661 return ref;
2662 }
2663
2664 /* Record details of decls possibly used inside sizeof or typeof. */
2665 struct maybe_used_decl
2666 {
2667 /* The decl. */
2668 tree decl;
2669 /* The level seen at (in_sizeof + in_typeof). */
2670 int level;
2671 /* The next one at this level or above, or NULL. */
2672 struct maybe_used_decl *next;
2673 };
2674
2675 static struct maybe_used_decl *maybe_used_decls;
2676
2677 /* Record that DECL, an undefined static function reference seen
2678 inside sizeof or typeof, might be used if the operand of sizeof is
2679 a VLA type or the operand of typeof is a variably modified
2680 type. */
2681
2682 static void
2683 record_maybe_used_decl (tree decl)
2684 {
2685 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2686 t->decl = decl;
2687 t->level = in_sizeof + in_typeof;
2688 t->next = maybe_used_decls;
2689 maybe_used_decls = t;
2690 }
2691
2692 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2693 USED is false, just discard them. If it is true, mark them used
2694 (if no longer inside sizeof or typeof) or move them to the next
2695 level up (if still inside sizeof or typeof). */
2696
2697 void
2698 pop_maybe_used (bool used)
2699 {
2700 struct maybe_used_decl *p = maybe_used_decls;
2701 int cur_level = in_sizeof + in_typeof;
2702 while (p && p->level > cur_level)
2703 {
2704 if (used)
2705 {
2706 if (cur_level == 0)
2707 C_DECL_USED (p->decl) = 1;
2708 else
2709 p->level = cur_level;
2710 }
2711 p = p->next;
2712 }
2713 if (!used || cur_level == 0)
2714 maybe_used_decls = p;
2715 }
2716
2717 /* Return the result of sizeof applied to EXPR. */
2718
2719 struct c_expr
2720 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2721 {
2722 struct c_expr ret;
2723 if (expr.value == error_mark_node)
2724 {
2725 ret.value = error_mark_node;
2726 ret.original_code = ERROR_MARK;
2727 ret.original_type = NULL;
2728 pop_maybe_used (false);
2729 }
2730 else
2731 {
2732 bool expr_const_operands = true;
2733 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2734 &expr_const_operands);
2735 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2736 c_last_sizeof_arg = expr.value;
2737 ret.original_code = SIZEOF_EXPR;
2738 ret.original_type = NULL;
2739 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2740 {
2741 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2742 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2743 folded_expr, ret.value);
2744 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2745 SET_EXPR_LOCATION (ret.value, loc);
2746 }
2747 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2748 }
2749 return ret;
2750 }
2751
2752 /* Return the result of sizeof applied to T, a structure for the type
2753 name passed to sizeof (rather than the type itself). LOC is the
2754 location of the original expression. */
2755
2756 struct c_expr
2757 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2758 {
2759 tree type;
2760 struct c_expr ret;
2761 tree type_expr = NULL_TREE;
2762 bool type_expr_const = true;
2763 type = groktypename (t, &type_expr, &type_expr_const);
2764 ret.value = c_sizeof (loc, type);
2765 c_last_sizeof_arg = type;
2766 ret.original_code = SIZEOF_EXPR;
2767 ret.original_type = NULL;
2768 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2769 && c_vla_type_p (type))
2770 {
2771 /* If the type is a [*] array, it is a VLA but is represented as
2772 having a size of zero. In such a case we must ensure that
2773 the result of sizeof does not get folded to a constant by
2774 c_fully_fold, because if the size is evaluated the result is
2775 not constant and so constraints on zero or negative size
2776 arrays must not be applied when this sizeof call is inside
2777 another array declarator. */
2778 if (!type_expr)
2779 type_expr = integer_zero_node;
2780 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2781 type_expr, ret.value);
2782 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2783 }
2784 pop_maybe_used (type != error_mark_node
2785 ? C_TYPE_VARIABLE_SIZE (type) : false);
2786 return ret;
2787 }
2788
2789 /* Build a function call to function FUNCTION with parameters PARAMS.
2790 The function call is at LOC.
2791 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2792 TREE_VALUE of each node is a parameter-expression.
2793 FUNCTION's data type may be a function type or a pointer-to-function. */
2794
2795 tree
2796 build_function_call (location_t loc, tree function, tree params)
2797 {
2798 vec<tree, va_gc> *v;
2799 tree ret;
2800
2801 vec_alloc (v, list_length (params));
2802 for (; params; params = TREE_CHAIN (params))
2803 v->quick_push (TREE_VALUE (params));
2804 ret = build_function_call_vec (loc, vNULL, function, v, NULL);
2805 vec_free (v);
2806 return ret;
2807 }
2808
2809 /* Give a note about the location of the declaration of DECL. */
2810
2811 static void inform_declaration (tree decl)
2812 {
2813 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2814 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2815 }
2816
2817 /* Build a function call to function FUNCTION with parameters PARAMS.
2818 ORIGTYPES, if not NULL, is a vector of types; each element is
2819 either NULL or the original type of the corresponding element in
2820 PARAMS. The original type may differ from TREE_TYPE of the
2821 parameter for enums. FUNCTION's data type may be a function type
2822 or pointer-to-function. This function changes the elements of
2823 PARAMS. */
2824
2825 tree
2826 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2827 tree function, vec<tree, va_gc> *params,
2828 vec<tree, va_gc> *origtypes)
2829 {
2830 tree fntype, fundecl = 0;
2831 tree name = NULL_TREE, result;
2832 tree tem;
2833 int nargs;
2834 tree *argarray;
2835
2836
2837 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2838 STRIP_TYPE_NOPS (function);
2839
2840 /* Convert anything with function type to a pointer-to-function. */
2841 if (TREE_CODE (function) == FUNCTION_DECL)
2842 {
2843 /* Implement type-directed function overloading for builtins.
2844 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2845 handle all the type checking. The result is a complete expression
2846 that implements this function call. */
2847 tem = resolve_overloaded_builtin (loc, function, params);
2848 if (tem)
2849 return tem;
2850
2851 name = DECL_NAME (function);
2852
2853 if (flag_tm)
2854 tm_malloc_replacement (function);
2855 fundecl = function;
2856 /* Atomic functions have type checking/casting already done. They are
2857 often rewritten and don't match the original parameter list. */
2858 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2859 origtypes = NULL;
2860
2861 if (flag_cilkplus
2862 && is_cilkplus_reduce_builtin (function))
2863 origtypes = NULL;
2864 }
2865 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2866 function = function_to_pointer_conversion (loc, function);
2867
2868 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2869 expressions, like those used for ObjC messenger dispatches. */
2870 if (params && !params->is_empty ())
2871 function = objc_rewrite_function_call (function, (*params)[0]);
2872
2873 function = c_fully_fold (function, false, NULL);
2874
2875 fntype = TREE_TYPE (function);
2876
2877 if (TREE_CODE (fntype) == ERROR_MARK)
2878 return error_mark_node;
2879
2880 if (!(TREE_CODE (fntype) == POINTER_TYPE
2881 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2882 {
2883 if (!flag_diagnostics_show_caret)
2884 error_at (loc,
2885 "called object %qE is not a function or function pointer",
2886 function);
2887 else if (DECL_P (function))
2888 {
2889 error_at (loc,
2890 "called object %qD is not a function or function pointer",
2891 function);
2892 inform_declaration (function);
2893 }
2894 else
2895 error_at (loc,
2896 "called object is not a function or function pointer");
2897 return error_mark_node;
2898 }
2899
2900 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2901 current_function_returns_abnormally = 1;
2902
2903 /* fntype now gets the type of function pointed to. */
2904 fntype = TREE_TYPE (fntype);
2905
2906 /* Convert the parameters to the types declared in the
2907 function prototype, or apply default promotions. */
2908
2909 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2910 origtypes, function, fundecl);
2911 if (nargs < 0)
2912 return error_mark_node;
2913
2914 /* Check that the function is called through a compatible prototype.
2915 If it is not, warn. */
2916 if (CONVERT_EXPR_P (function)
2917 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2918 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2919 && !comptypes (fntype, TREE_TYPE (tem)))
2920 {
2921 tree return_type = TREE_TYPE (fntype);
2922
2923 /* This situation leads to run-time undefined behavior. We can't,
2924 therefore, simply error unless we can prove that all possible
2925 executions of the program must execute the code. */
2926 warning_at (loc, 0, "function called through a non-compatible type");
2927
2928 if (VOID_TYPE_P (return_type)
2929 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2930 pedwarn (loc, 0,
2931 "function with qualified void return type called");
2932 }
2933
2934 argarray = vec_safe_address (params);
2935
2936 /* Check that arguments to builtin functions match the expectations. */
2937 if (fundecl
2938 && DECL_BUILT_IN (fundecl)
2939 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2940 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2941 return error_mark_node;
2942
2943 /* Check that the arguments to the function are valid. */
2944 check_function_arguments (fntype, nargs, argarray);
2945
2946 if (name != NULL_TREE
2947 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2948 {
2949 if (require_constant_value)
2950 result =
2951 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2952 function, nargs, argarray);
2953 else
2954 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2955 function, nargs, argarray);
2956 if (TREE_CODE (result) == NOP_EXPR
2957 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2958 STRIP_TYPE_NOPS (result);
2959 }
2960 else
2961 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2962 function, nargs, argarray);
2963
2964 if (VOID_TYPE_P (TREE_TYPE (result)))
2965 {
2966 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2967 pedwarn (loc, 0,
2968 "function with qualified void return type called");
2969 return result;
2970 }
2971 return require_complete_type (result);
2972 }
2973 \f
2974 /* Convert the argument expressions in the vector VALUES
2975 to the types in the list TYPELIST.
2976
2977 If TYPELIST is exhausted, or when an element has NULL as its type,
2978 perform the default conversions.
2979
2980 ORIGTYPES is the original types of the expressions in VALUES. This
2981 holds the type of enum values which have been converted to integral
2982 types. It may be NULL.
2983
2984 FUNCTION is a tree for the called function. It is used only for
2985 error messages, where it is formatted with %qE.
2986
2987 This is also where warnings about wrong number of args are generated.
2988
2989 ARG_LOC are locations of function arguments (if any).
2990
2991 Returns the actual number of arguments processed (which may be less
2992 than the length of VALUES in some error situations), or -1 on
2993 failure. */
2994
2995 static int
2996 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
2997 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
2998 tree function, tree fundecl)
2999 {
3000 tree typetail, val;
3001 unsigned int parmnum;
3002 bool error_args = false;
3003 const bool type_generic = fundecl
3004 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3005 bool type_generic_remove_excess_precision = false;
3006 tree selector;
3007
3008 /* Change pointer to function to the function itself for
3009 diagnostics. */
3010 if (TREE_CODE (function) == ADDR_EXPR
3011 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3012 function = TREE_OPERAND (function, 0);
3013
3014 /* Handle an ObjC selector specially for diagnostics. */
3015 selector = objc_message_selector ();
3016
3017 /* For type-generic built-in functions, determine whether excess
3018 precision should be removed (classification) or not
3019 (comparison). */
3020 if (type_generic
3021 && DECL_BUILT_IN (fundecl)
3022 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3023 {
3024 switch (DECL_FUNCTION_CODE (fundecl))
3025 {
3026 case BUILT_IN_ISFINITE:
3027 case BUILT_IN_ISINF:
3028 case BUILT_IN_ISINF_SIGN:
3029 case BUILT_IN_ISNAN:
3030 case BUILT_IN_ISNORMAL:
3031 case BUILT_IN_FPCLASSIFY:
3032 type_generic_remove_excess_precision = true;
3033 break;
3034
3035 default:
3036 type_generic_remove_excess_precision = false;
3037 break;
3038 }
3039 }
3040 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3041 return vec_safe_length (values);
3042
3043 /* Scan the given expressions and types, producing individual
3044 converted arguments. */
3045
3046 for (typetail = typelist, parmnum = 0;
3047 values && values->iterate (parmnum, &val);
3048 ++parmnum)
3049 {
3050 tree type = typetail ? TREE_VALUE (typetail) : 0;
3051 tree valtype = TREE_TYPE (val);
3052 tree rname = function;
3053 int argnum = parmnum + 1;
3054 const char *invalid_func_diag;
3055 bool excess_precision = false;
3056 bool npc;
3057 tree parmval;
3058
3059 if (type == void_type_node)
3060 {
3061 if (selector)
3062 error_at (loc, "too many arguments to method %qE", selector);
3063 else
3064 error_at (loc, "too many arguments to function %qE", function);
3065 inform_declaration (fundecl);
3066 return parmnum;
3067 }
3068
3069 if (selector && argnum > 2)
3070 {
3071 rname = selector;
3072 argnum -= 2;
3073 }
3074
3075 npc = null_pointer_constant_p (val);
3076
3077 /* If there is excess precision and a prototype, convert once to
3078 the required type rather than converting via the semantic
3079 type. Likewise without a prototype a float value represented
3080 as long double should be converted once to double. But for
3081 type-generic classification functions excess precision must
3082 be removed here. */
3083 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3084 && (type || !type_generic || !type_generic_remove_excess_precision))
3085 {
3086 val = TREE_OPERAND (val, 0);
3087 excess_precision = true;
3088 }
3089 val = c_fully_fold (val, false, NULL);
3090 STRIP_TYPE_NOPS (val);
3091
3092 val = require_complete_type (val);
3093
3094 if (type != 0)
3095 {
3096 /* Formal parm type is specified by a function prototype. */
3097
3098 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3099 {
3100 error ("type of formal parameter %d is incomplete", parmnum + 1);
3101 parmval = val;
3102 }
3103 else
3104 {
3105 tree origtype;
3106
3107 /* Optionally warn about conversions that
3108 differ from the default conversions. */
3109 if (warn_traditional_conversion || warn_traditional)
3110 {
3111 unsigned int formal_prec = TYPE_PRECISION (type);
3112
3113 if (INTEGRAL_TYPE_P (type)
3114 && TREE_CODE (valtype) == REAL_TYPE)
3115 warning (0, "passing argument %d of %qE as integer "
3116 "rather than floating due to prototype",
3117 argnum, rname);
3118 if (INTEGRAL_TYPE_P (type)
3119 && TREE_CODE (valtype) == COMPLEX_TYPE)
3120 warning (0, "passing argument %d of %qE as integer "
3121 "rather than complex due to prototype",
3122 argnum, rname);
3123 else if (TREE_CODE (type) == COMPLEX_TYPE
3124 && TREE_CODE (valtype) == REAL_TYPE)
3125 warning (0, "passing argument %d of %qE as complex "
3126 "rather than floating due to prototype",
3127 argnum, rname);
3128 else if (TREE_CODE (type) == REAL_TYPE
3129 && INTEGRAL_TYPE_P (valtype))
3130 warning (0, "passing argument %d of %qE as floating "
3131 "rather than integer due to prototype",
3132 argnum, rname);
3133 else if (TREE_CODE (type) == COMPLEX_TYPE
3134 && INTEGRAL_TYPE_P (valtype))
3135 warning (0, "passing argument %d of %qE as complex "
3136 "rather than integer due to prototype",
3137 argnum, rname);
3138 else if (TREE_CODE (type) == REAL_TYPE
3139 && TREE_CODE (valtype) == COMPLEX_TYPE)
3140 warning (0, "passing argument %d of %qE as floating "
3141 "rather than complex due to prototype",
3142 argnum, rname);
3143 /* ??? At some point, messages should be written about
3144 conversions between complex types, but that's too messy
3145 to do now. */
3146 else if (TREE_CODE (type) == REAL_TYPE
3147 && TREE_CODE (valtype) == REAL_TYPE)
3148 {
3149 /* Warn if any argument is passed as `float',
3150 since without a prototype it would be `double'. */
3151 if (formal_prec == TYPE_PRECISION (float_type_node)
3152 && type != dfloat32_type_node)
3153 warning (0, "passing argument %d of %qE as %<float%> "
3154 "rather than %<double%> due to prototype",
3155 argnum, rname);
3156
3157 /* Warn if mismatch between argument and prototype
3158 for decimal float types. Warn of conversions with
3159 binary float types and of precision narrowing due to
3160 prototype. */
3161 else if (type != valtype
3162 && (type == dfloat32_type_node
3163 || type == dfloat64_type_node
3164 || type == dfloat128_type_node
3165 || valtype == dfloat32_type_node
3166 || valtype == dfloat64_type_node
3167 || valtype == dfloat128_type_node)
3168 && (formal_prec
3169 <= TYPE_PRECISION (valtype)
3170 || (type == dfloat128_type_node
3171 && (valtype
3172 != dfloat64_type_node
3173 && (valtype
3174 != dfloat32_type_node)))
3175 || (type == dfloat64_type_node
3176 && (valtype
3177 != dfloat32_type_node))))
3178 warning (0, "passing argument %d of %qE as %qT "
3179 "rather than %qT due to prototype",
3180 argnum, rname, type, valtype);
3181
3182 }
3183 /* Detect integer changing in width or signedness.
3184 These warnings are only activated with
3185 -Wtraditional-conversion, not with -Wtraditional. */
3186 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3187 && INTEGRAL_TYPE_P (valtype))
3188 {
3189 tree would_have_been = default_conversion (val);
3190 tree type1 = TREE_TYPE (would_have_been);
3191
3192 if (TREE_CODE (type) == ENUMERAL_TYPE
3193 && (TYPE_MAIN_VARIANT (type)
3194 == TYPE_MAIN_VARIANT (valtype)))
3195 /* No warning if function asks for enum
3196 and the actual arg is that enum type. */
3197 ;
3198 else if (formal_prec != TYPE_PRECISION (type1))
3199 warning (OPT_Wtraditional_conversion,
3200 "passing argument %d of %qE "
3201 "with different width due to prototype",
3202 argnum, rname);
3203 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3204 ;
3205 /* Don't complain if the formal parameter type
3206 is an enum, because we can't tell now whether
3207 the value was an enum--even the same enum. */
3208 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3209 ;
3210 else if (TREE_CODE (val) == INTEGER_CST
3211 && int_fits_type_p (val, type))
3212 /* Change in signedness doesn't matter
3213 if a constant value is unaffected. */
3214 ;
3215 /* If the value is extended from a narrower
3216 unsigned type, it doesn't matter whether we
3217 pass it as signed or unsigned; the value
3218 certainly is the same either way. */
3219 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3220 && TYPE_UNSIGNED (valtype))
3221 ;
3222 else if (TYPE_UNSIGNED (type))
3223 warning (OPT_Wtraditional_conversion,
3224 "passing argument %d of %qE "
3225 "as unsigned due to prototype",
3226 argnum, rname);
3227 else
3228 warning (OPT_Wtraditional_conversion,
3229 "passing argument %d of %qE "
3230 "as signed due to prototype", argnum, rname);
3231 }
3232 }
3233
3234 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3235 sake of better warnings from convert_and_check. */
3236 if (excess_precision)
3237 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3238 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3239 bool arg_loc_ok = !arg_loc.is_empty ()
3240 /* Some __atomic_* builtins have additional
3241 hidden argument at position 0. */
3242 && values->length () == arg_loc.length ();
3243 parmval = convert_for_assignment (loc,
3244 arg_loc_ok ? arg_loc[parmnum]
3245 : UNKNOWN_LOCATION, type,
3246 val, origtype, ic_argpass,
3247 npc, fundecl, function,
3248 parmnum + 1);
3249
3250 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3251 && INTEGRAL_TYPE_P (type)
3252 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3253 parmval = default_conversion (parmval);
3254 }
3255 }
3256 else if (TREE_CODE (valtype) == REAL_TYPE
3257 && (TYPE_PRECISION (valtype)
3258 <= TYPE_PRECISION (double_type_node))
3259 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3260 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3261 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3262 {
3263 if (type_generic)
3264 parmval = val;
3265 else
3266 {
3267 /* Convert `float' to `double'. */
3268 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3269 warning_at (arg_loc[parmnum], OPT_Wdouble_promotion,
3270 "implicit conversion from %qT to %qT when passing "
3271 "argument to function",
3272 valtype, double_type_node);
3273 parmval = convert (double_type_node, val);
3274 }
3275 }
3276 else if (excess_precision && !type_generic)
3277 /* A "double" argument with excess precision being passed
3278 without a prototype or in variable arguments. */
3279 parmval = convert (valtype, val);
3280 else if ((invalid_func_diag =
3281 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3282 {
3283 error (invalid_func_diag);
3284 return -1;
3285 }
3286 else
3287 /* Convert `short' and `char' to full-size `int'. */
3288 parmval = default_conversion (val);
3289
3290 (*values)[parmnum] = parmval;
3291 if (parmval == error_mark_node)
3292 error_args = true;
3293
3294 if (typetail)
3295 typetail = TREE_CHAIN (typetail);
3296 }
3297
3298 gcc_assert (parmnum == vec_safe_length (values));
3299
3300 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3301 {
3302 error_at (loc, "too few arguments to function %qE", function);
3303 inform_declaration (fundecl);
3304 return -1;
3305 }
3306
3307 return error_args ? -1 : (int) parmnum;
3308 }
3309 \f
3310 /* This is the entry point used by the parser to build unary operators
3311 in the input. CODE, a tree_code, specifies the unary operator, and
3312 ARG is the operand. For unary plus, the C parser currently uses
3313 CONVERT_EXPR for code.
3314
3315 LOC is the location to use for the tree generated.
3316 */
3317
3318 struct c_expr
3319 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3320 {
3321 struct c_expr result;
3322
3323 result.value = build_unary_op (loc, code, arg.value, 0);
3324 result.original_code = code;
3325 result.original_type = NULL;
3326
3327 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3328 overflow_warning (loc, result.value);
3329
3330 return result;
3331 }
3332
3333 /* This is the entry point used by the parser to build binary operators
3334 in the input. CODE, a tree_code, specifies the binary operator, and
3335 ARG1 and ARG2 are the operands. In addition to constructing the
3336 expression, we check for operands that were written with other binary
3337 operators in a way that is likely to confuse the user.
3338
3339 LOCATION is the location of the binary operator. */
3340
3341 struct c_expr
3342 parser_build_binary_op (location_t location, enum tree_code code,
3343 struct c_expr arg1, struct c_expr arg2)
3344 {
3345 struct c_expr result;
3346
3347 enum tree_code code1 = arg1.original_code;
3348 enum tree_code code2 = arg2.original_code;
3349 tree type1 = (arg1.original_type
3350 ? arg1.original_type
3351 : TREE_TYPE (arg1.value));
3352 tree type2 = (arg2.original_type
3353 ? arg2.original_type
3354 : TREE_TYPE (arg2.value));
3355
3356 result.value = build_binary_op (location, code,
3357 arg1.value, arg2.value, 1);
3358 result.original_code = code;
3359 result.original_type = NULL;
3360
3361 if (TREE_CODE (result.value) == ERROR_MARK)
3362 return result;
3363
3364 if (location != UNKNOWN_LOCATION)
3365 protected_set_expr_location (result.value, location);
3366
3367 /* Check for cases such as x+y<<z which users are likely
3368 to misinterpret. */
3369 if (warn_parentheses)
3370 warn_about_parentheses (location, code, code1, arg1.value, code2,
3371 arg2.value);
3372
3373 if (warn_logical_op)
3374 warn_logical_operator (location, code, TREE_TYPE (result.value),
3375 code1, arg1.value, code2, arg2.value);
3376
3377 /* Warn about comparisons against string literals, with the exception
3378 of testing for equality or inequality of a string literal with NULL. */
3379 if (code == EQ_EXPR || code == NE_EXPR)
3380 {
3381 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3382 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3383 warning_at (location, OPT_Waddress,
3384 "comparison with string literal results in unspecified behavior");
3385 }
3386 else if (TREE_CODE_CLASS (code) == tcc_comparison
3387 && (code1 == STRING_CST || code2 == STRING_CST))
3388 warning_at (location, OPT_Waddress,
3389 "comparison with string literal results in unspecified behavior");
3390
3391 if (TREE_OVERFLOW_P (result.value)
3392 && !TREE_OVERFLOW_P (arg1.value)
3393 && !TREE_OVERFLOW_P (arg2.value))
3394 overflow_warning (location, result.value);
3395
3396 /* Warn about comparisons of different enum types. */
3397 if (warn_enum_compare
3398 && TREE_CODE_CLASS (code) == tcc_comparison
3399 && TREE_CODE (type1) == ENUMERAL_TYPE
3400 && TREE_CODE (type2) == ENUMERAL_TYPE
3401 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3402 warning_at (location, OPT_Wenum_compare,
3403 "comparison between %qT and %qT",
3404 type1, type2);
3405
3406 return result;
3407 }
3408 \f
3409 /* Return a tree for the difference of pointers OP0 and OP1.
3410 The resulting tree has type int. */
3411
3412 static tree
3413 pointer_diff (location_t loc, tree op0, tree op1)
3414 {
3415 tree restype = ptrdiff_type_node;
3416 tree result, inttype;
3417
3418 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3419 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3420 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3421 tree con0, con1, lit0, lit1;
3422 tree orig_op1 = op1;
3423
3424 /* If the operands point into different address spaces, we need to
3425 explicitly convert them to pointers into the common address space
3426 before we can subtract the numerical address values. */
3427 if (as0 != as1)
3428 {
3429 addr_space_t as_common;
3430 tree common_type;
3431
3432 /* Determine the common superset address space. This is guaranteed
3433 to exist because the caller verified that comp_target_types
3434 returned non-zero. */
3435 if (!addr_space_superset (as0, as1, &as_common))
3436 gcc_unreachable ();
3437
3438 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3439 op0 = convert (common_type, op0);
3440 op1 = convert (common_type, op1);
3441 }
3442
3443 /* Determine integer type to perform computations in. This will usually
3444 be the same as the result type (ptrdiff_t), but may need to be a wider
3445 type if pointers for the address space are wider than ptrdiff_t. */
3446 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3447 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3448 else
3449 inttype = restype;
3450
3451
3452 if (TREE_CODE (target_type) == VOID_TYPE)
3453 pedwarn (loc, OPT_Wpointer_arith,
3454 "pointer of type %<void *%> used in subtraction");
3455 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3456 pedwarn (loc, OPT_Wpointer_arith,
3457 "pointer to a function used in subtraction");
3458
3459 /* If the conversion to ptrdiff_type does anything like widening or
3460 converting a partial to an integral mode, we get a convert_expression
3461 that is in the way to do any simplifications.
3462 (fold-const.c doesn't know that the extra bits won't be needed.
3463 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3464 different mode in place.)
3465 So first try to find a common term here 'by hand'; we want to cover
3466 at least the cases that occur in legal static initializers. */
3467 if (CONVERT_EXPR_P (op0)
3468 && (TYPE_PRECISION (TREE_TYPE (op0))
3469 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3470 con0 = TREE_OPERAND (op0, 0);
3471 else
3472 con0 = op0;
3473 if (CONVERT_EXPR_P (op1)
3474 && (TYPE_PRECISION (TREE_TYPE (op1))
3475 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3476 con1 = TREE_OPERAND (op1, 0);
3477 else
3478 con1 = op1;
3479
3480 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3481 {
3482 lit0 = TREE_OPERAND (con0, 1);
3483 con0 = TREE_OPERAND (con0, 0);
3484 }
3485 else
3486 lit0 = integer_zero_node;
3487
3488 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
3489 {
3490 lit1 = TREE_OPERAND (con1, 1);
3491 con1 = TREE_OPERAND (con1, 0);
3492 }
3493 else
3494 lit1 = integer_zero_node;
3495
3496 if (operand_equal_p (con0, con1, 0))
3497 {
3498 op0 = lit0;
3499 op1 = lit1;
3500 }
3501
3502
3503 /* First do the subtraction as integers;
3504 then drop through to build the divide operator.
3505 Do not do default conversions on the minus operator
3506 in case restype is a short type. */
3507
3508 op0 = build_binary_op (loc,
3509 MINUS_EXPR, convert (inttype, op0),
3510 convert (inttype, op1), 0);
3511 /* This generates an error if op1 is pointer to incomplete type. */
3512 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3513 error_at (loc, "arithmetic on pointer to an incomplete type");
3514
3515 /* This generates an error if op0 is pointer to incomplete type. */
3516 op1 = c_size_in_bytes (target_type);
3517
3518 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3519 error_at (loc, "arithmetic on pointer to an empty aggregate");
3520
3521 /* Divide by the size, in easiest possible way. */
3522 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3523 op0, convert (inttype, op1));
3524
3525 /* Convert to final result type if necessary. */
3526 return convert (restype, result);
3527 }
3528 \f
3529 /* Expand atomic compound assignments into an approriate sequence as
3530 specified by the C11 standard section 6.5.16.2.
3531 given
3532 _Atomic T1 E1
3533 T2 E2
3534 E1 op= E2
3535
3536 This sequence is used for all types for which these operations are
3537 supported.
3538
3539 In addition, built-in versions of the 'fe' prefixed routines may
3540 need to be invoked for floating point (real, complex or vector) when
3541 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3542
3543 T1 newval;
3544 T1 old;
3545 T1 *addr
3546 T2 val
3547 fenv_t fenv
3548
3549 addr = &E1;
3550 val = (E2);
3551 __atomic_load (addr, &old, SEQ_CST);
3552 feholdexcept (&fenv);
3553 loop:
3554 newval = old op val;
3555 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3556 SEQ_CST))
3557 goto done;
3558 feclearexcept (FE_ALL_EXCEPT);
3559 goto loop:
3560 done:
3561 feupdateenv (&fenv);
3562
3563 Also note that the compiler is simply issuing the generic form of
3564 the atomic operations. This requires temp(s) and has their address
3565 taken. The atomic processing is smart enough to figure out when the
3566 size of an object can utilize a lock-free version, and convert the
3567 built-in call to the appropriate lock-free routine. The optimizers
3568 will then dispose of any temps that are no longer required, and
3569 lock-free implementations are utilized as long as there is target
3570 support for the required size.
3571
3572 If the operator is NOP_EXPR, then this is a simple assignment, and
3573 an __atomic_store is issued to perform the assignment rather than
3574 the above loop.
3575
3576 */
3577
3578 /* Build an atomic assignment at LOC, expanding into the proper
3579 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3580 the result of the operation, unless RETURN_OLD_P in which case
3581 return the old value of LHS (this is only for postincrement and
3582 postdecrement). */
3583 static tree
3584 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3585 tree rhs, bool return_old_p)
3586 {
3587 tree fndecl, func_call;
3588 vec<tree, va_gc> *params;
3589 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3590 tree old, old_addr;
3591 tree compound_stmt;
3592 tree stmt, goto_stmt;
3593 tree loop_label, loop_decl, done_label, done_decl;
3594
3595 tree lhs_type = TREE_TYPE (lhs);
3596 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3597 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3598 tree rhs_type = TREE_TYPE (rhs);
3599
3600 gcc_assert (TYPE_ATOMIC (lhs_type));
3601
3602 if (return_old_p)
3603 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3604
3605 /* Allocate enough vector items for a compare_exchange. */
3606 vec_alloc (params, 6);
3607
3608 /* Create a compound statement to hold the sequence of statements
3609 with a loop. */
3610 compound_stmt = c_begin_compound_stmt (false);
3611
3612 /* Fold the RHS if it hasn't already been folded. */
3613 if (modifycode != NOP_EXPR)
3614 rhs = c_fully_fold (rhs, false, NULL);
3615
3616 /* Remove the qualifiers for the rest of the expressions and create
3617 the VAL temp variable to hold the RHS. */
3618 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3619 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3620 val = create_tmp_var (nonatomic_rhs_type, NULL);
3621 TREE_ADDRESSABLE (val) = 1;
3622 TREE_NO_WARNING (val) = 1;
3623 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3624 SET_EXPR_LOCATION (rhs, loc);
3625 add_stmt (rhs);
3626
3627 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3628 an atomic_store. */
3629 if (modifycode == NOP_EXPR)
3630 {
3631 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3632 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3633 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3634 params->quick_push (lhs_addr);
3635 params->quick_push (rhs);
3636 params->quick_push (seq_cst);
3637 func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3638 add_stmt (func_call);
3639
3640 /* Finish the compound statement. */
3641 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3642
3643 /* VAL is the value which was stored, return a COMPOUND_STMT of
3644 the statement and that value. */
3645 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3646 }
3647
3648 /* Create the variables and labels required for the op= form. */
3649 old = create_tmp_var (nonatomic_lhs_type, NULL);
3650 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3651 TREE_ADDRESSABLE (old) = 1;
3652 TREE_NO_WARNING (old) = 1;
3653
3654 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3655 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3656 TREE_ADDRESSABLE (newval) = 1;
3657
3658 loop_decl = create_artificial_label (loc);
3659 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3660
3661 done_decl = create_artificial_label (loc);
3662 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3663
3664 /* __atomic_load (addr, &old, SEQ_CST). */
3665 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3666 params->quick_push (lhs_addr);
3667 params->quick_push (old_addr);
3668 params->quick_push (seq_cst);
3669 func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3670 add_stmt (func_call);
3671 params->truncate (0);
3672
3673 /* Create the expressions for floating-point environment
3674 manipulation, if required. */
3675 bool need_fenv = (flag_trapping_math
3676 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3677 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3678 if (need_fenv)
3679 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3680
3681 if (hold_call)
3682 add_stmt (hold_call);
3683
3684 /* loop: */
3685 add_stmt (loop_label);
3686
3687 /* newval = old + val; */
3688 rhs = build_binary_op (loc, modifycode, old, val, 1);
3689 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3690 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3691 NULL_TREE, 0);
3692 if (rhs != error_mark_node)
3693 {
3694 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3695 SET_EXPR_LOCATION (rhs, loc);
3696 add_stmt (rhs);
3697 }
3698
3699 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3700 goto done; */
3701 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3702 params->quick_push (lhs_addr);
3703 params->quick_push (old_addr);
3704 params->quick_push (newval_addr);
3705 params->quick_push (integer_zero_node);
3706 params->quick_push (seq_cst);
3707 params->quick_push (seq_cst);
3708 func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3709
3710 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3711 SET_EXPR_LOCATION (goto_stmt, loc);
3712
3713 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3714 SET_EXPR_LOCATION (stmt, loc);
3715 add_stmt (stmt);
3716
3717 if (clear_call)
3718 add_stmt (clear_call);
3719
3720 /* goto loop; */
3721 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3722 SET_EXPR_LOCATION (goto_stmt, loc);
3723 add_stmt (goto_stmt);
3724
3725 /* done: */
3726 add_stmt (done_label);
3727
3728 if (update_call)
3729 add_stmt (update_call);
3730
3731 /* Finish the compound statement. */
3732 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3733
3734 /* NEWVAL is the value that was successfully stored, return a
3735 COMPOUND_EXPR of the statement and the appropriate value. */
3736 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3737 return_old_p ? old : newval);
3738 }
3739
3740 /* Construct and perhaps optimize a tree representation
3741 for a unary operation. CODE, a tree_code, specifies the operation
3742 and XARG is the operand.
3743 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3744 the default promotions (such as from short to int).
3745 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3746 allows non-lvalues; this is only used to handle conversion of non-lvalue
3747 arrays to pointers in C99.
3748
3749 LOCATION is the location of the operator. */
3750
3751 tree
3752 build_unary_op (location_t location,
3753 enum tree_code code, tree xarg, int flag)
3754 {
3755 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3756 tree arg = xarg;
3757 tree argtype = 0;
3758 enum tree_code typecode;
3759 tree val;
3760 tree ret = error_mark_node;
3761 tree eptype = NULL_TREE;
3762 int noconvert = flag;
3763 const char *invalid_op_diag;
3764 bool int_operands;
3765
3766 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3767 if (int_operands)
3768 arg = remove_c_maybe_const_expr (arg);
3769
3770 if (code != ADDR_EXPR)
3771 arg = require_complete_type (arg);
3772
3773 typecode = TREE_CODE (TREE_TYPE (arg));
3774 if (typecode == ERROR_MARK)
3775 return error_mark_node;
3776 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3777 typecode = INTEGER_TYPE;
3778
3779 if ((invalid_op_diag
3780 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3781 {
3782 error_at (location, invalid_op_diag);
3783 return error_mark_node;
3784 }
3785
3786 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3787 {
3788 eptype = TREE_TYPE (arg);
3789 arg = TREE_OPERAND (arg, 0);
3790 }
3791
3792 switch (code)
3793 {
3794 case CONVERT_EXPR:
3795 /* This is used for unary plus, because a CONVERT_EXPR
3796 is enough to prevent anybody from looking inside for
3797 associativity, but won't generate any code. */
3798 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3799 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3800 || typecode == VECTOR_TYPE))
3801 {
3802 error_at (location, "wrong type argument to unary plus");
3803 return error_mark_node;
3804 }
3805 else if (!noconvert)
3806 arg = default_conversion (arg);
3807 arg = non_lvalue_loc (location, arg);
3808 break;
3809
3810 case NEGATE_EXPR:
3811 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3812 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3813 || typecode == VECTOR_TYPE))
3814 {
3815 error_at (location, "wrong type argument to unary minus");
3816 return error_mark_node;
3817 }
3818 else if (!noconvert)
3819 arg = default_conversion (arg);
3820 break;
3821
3822 case BIT_NOT_EXPR:
3823 /* ~ works on integer types and non float vectors. */
3824 if (typecode == INTEGER_TYPE
3825 || (typecode == VECTOR_TYPE
3826 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3827 {
3828 if (!noconvert)
3829 arg = default_conversion (arg);
3830 }
3831 else if (typecode == COMPLEX_TYPE)
3832 {
3833 code = CONJ_EXPR;
3834 pedwarn (location, OPT_Wpedantic,
3835 "ISO C does not support %<~%> for complex conjugation");
3836 if (!noconvert)
3837 arg = default_conversion (arg);
3838 }
3839 else
3840 {
3841 error_at (location, "wrong type argument to bit-complement");
3842 return error_mark_node;
3843 }
3844 break;
3845
3846 case ABS_EXPR:
3847 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3848 {
3849 error_at (location, "wrong type argument to abs");
3850 return error_mark_node;
3851 }
3852 else if (!noconvert)
3853 arg = default_conversion (arg);
3854 break;
3855
3856 case CONJ_EXPR:
3857 /* Conjugating a real value is a no-op, but allow it anyway. */
3858 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3859 || typecode == COMPLEX_TYPE))
3860 {
3861 error_at (location, "wrong type argument to conjugation");
3862 return error_mark_node;
3863 }
3864 else if (!noconvert)
3865 arg = default_conversion (arg);
3866 break;
3867
3868 case TRUTH_NOT_EXPR:
3869 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3870 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3871 && typecode != COMPLEX_TYPE)
3872 {
3873 error_at (location,
3874 "wrong type argument to unary exclamation mark");
3875 return error_mark_node;
3876 }
3877 if (int_operands)
3878 {
3879 arg = c_objc_common_truthvalue_conversion (location, xarg);
3880 arg = remove_c_maybe_const_expr (arg);
3881 }
3882 else
3883 arg = c_objc_common_truthvalue_conversion (location, arg);
3884 ret = invert_truthvalue_loc (location, arg);
3885 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3886 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3887 location = EXPR_LOCATION (ret);
3888 goto return_build_unary_op;
3889
3890 case REALPART_EXPR:
3891 case IMAGPART_EXPR:
3892 ret = build_real_imag_expr (location, code, arg);
3893 if (ret == error_mark_node)
3894 return error_mark_node;
3895 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3896 eptype = TREE_TYPE (eptype);
3897 goto return_build_unary_op;
3898
3899 case PREINCREMENT_EXPR:
3900 case POSTINCREMENT_EXPR:
3901 case PREDECREMENT_EXPR:
3902 case POSTDECREMENT_EXPR:
3903
3904 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3905 {
3906 tree inner = build_unary_op (location, code,
3907 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3908 if (inner == error_mark_node)
3909 return error_mark_node;
3910 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3911 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3912 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3913 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3914 goto return_build_unary_op;
3915 }
3916
3917 /* Complain about anything that is not a true lvalue. In
3918 Objective-C, skip this check for property_refs. */
3919 if (!objc_is_property_ref (arg)
3920 && !lvalue_or_else (location,
3921 arg, ((code == PREINCREMENT_EXPR
3922 || code == POSTINCREMENT_EXPR)
3923 ? lv_increment
3924 : lv_decrement)))
3925 return error_mark_node;
3926
3927 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3928 {
3929 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3930 warning_at (location, OPT_Wc___compat,
3931 "increment of enumeration value is invalid in C++");
3932 else
3933 warning_at (location, OPT_Wc___compat,
3934 "decrement of enumeration value is invalid in C++");
3935 }
3936
3937 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3938 arg = c_fully_fold (arg, false, NULL);
3939
3940 bool atomic_op;
3941 atomic_op = really_atomic_lvalue (arg);
3942
3943 /* Increment or decrement the real part of the value,
3944 and don't change the imaginary part. */
3945 if (typecode == COMPLEX_TYPE)
3946 {
3947 tree real, imag;
3948
3949 pedwarn (location, OPT_Wpedantic,
3950 "ISO C does not support %<++%> and %<--%> on complex types");
3951
3952 if (!atomic_op)
3953 {
3954 arg = stabilize_reference (arg);
3955 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3956 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3957 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3958 if (real == error_mark_node || imag == error_mark_node)
3959 return error_mark_node;
3960 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3961 real, imag);
3962 goto return_build_unary_op;
3963 }
3964 }
3965
3966 /* Report invalid types. */
3967
3968 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3969 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
3970 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
3971 {
3972 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3973 error_at (location, "wrong type argument to increment");
3974 else
3975 error_at (location, "wrong type argument to decrement");
3976
3977 return error_mark_node;
3978 }
3979
3980 {
3981 tree inc;
3982
3983 argtype = TREE_TYPE (arg);
3984
3985 /* Compute the increment. */
3986
3987 if (typecode == POINTER_TYPE)
3988 {
3989 /* If pointer target is an undefined struct,
3990 we just cannot know how to do the arithmetic. */
3991 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3992 {
3993 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3994 error_at (location,
3995 "increment of pointer to unknown structure");
3996 else
3997 error_at (location,
3998 "decrement of pointer to unknown structure");
3999 }
4000 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4001 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4002 {
4003 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4004 pedwarn (location, OPT_Wpointer_arith,
4005 "wrong type argument to increment");
4006 else
4007 pedwarn (location, OPT_Wpointer_arith,
4008 "wrong type argument to decrement");
4009 }
4010
4011 inc = c_size_in_bytes (TREE_TYPE (argtype));
4012 inc = convert_to_ptrofftype_loc (location, inc);
4013 }
4014 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4015 {
4016 /* For signed fract types, we invert ++ to -- or
4017 -- to ++, and change inc from 1 to -1, because
4018 it is not possible to represent 1 in signed fract constants.
4019 For unsigned fract types, the result always overflows and
4020 we get an undefined (original) or the maximum value. */
4021 if (code == PREINCREMENT_EXPR)
4022 code = PREDECREMENT_EXPR;
4023 else if (code == PREDECREMENT_EXPR)
4024 code = PREINCREMENT_EXPR;
4025 else if (code == POSTINCREMENT_EXPR)
4026 code = POSTDECREMENT_EXPR;
4027 else /* code == POSTDECREMENT_EXPR */
4028 code = POSTINCREMENT_EXPR;
4029
4030 inc = integer_minus_one_node;
4031 inc = convert (argtype, inc);
4032 }
4033 else
4034 {
4035 inc = VECTOR_TYPE_P (argtype)
4036 ? build_one_cst (argtype)
4037 : integer_one_node;
4038 inc = convert (argtype, inc);
4039 }
4040
4041 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4042 need to ask Objective-C to build the increment or decrement
4043 expression for it. */
4044 if (objc_is_property_ref (arg))
4045 return objc_build_incr_expr_for_property_ref (location, code,
4046 arg, inc);
4047
4048 /* Report a read-only lvalue. */
4049 if (TYPE_READONLY (argtype))
4050 {
4051 readonly_error (location, arg,
4052 ((code == PREINCREMENT_EXPR
4053 || code == POSTINCREMENT_EXPR)
4054 ? lv_increment : lv_decrement));
4055 return error_mark_node;
4056 }
4057 else if (TREE_READONLY (arg))
4058 readonly_warning (arg,
4059 ((code == PREINCREMENT_EXPR
4060 || code == POSTINCREMENT_EXPR)
4061 ? lv_increment : lv_decrement));
4062
4063 /* If the argument is atomic, use the special code sequences for
4064 atomic compound assignment. */
4065 if (atomic_op)
4066 {
4067 arg = stabilize_reference (arg);
4068 ret = build_atomic_assign (location, arg,
4069 ((code == PREINCREMENT_EXPR
4070 || code == POSTINCREMENT_EXPR)
4071 ? PLUS_EXPR
4072 : MINUS_EXPR),
4073 (FRACT_MODE_P (TYPE_MODE (argtype))
4074 ? inc
4075 : integer_one_node),
4076 (code == POSTINCREMENT_EXPR
4077 || code == POSTDECREMENT_EXPR));
4078 goto return_build_unary_op;
4079 }
4080
4081 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4082 val = boolean_increment (code, arg);
4083 else
4084 val = build2 (code, TREE_TYPE (arg), arg, inc);
4085 TREE_SIDE_EFFECTS (val) = 1;
4086 if (TREE_CODE (val) != code)
4087 TREE_NO_WARNING (val) = 1;
4088 ret = val;
4089 goto return_build_unary_op;
4090 }
4091
4092 case ADDR_EXPR:
4093 /* Note that this operation never does default_conversion. */
4094
4095 /* The operand of unary '&' must be an lvalue (which excludes
4096 expressions of type void), or, in C99, the result of a [] or
4097 unary '*' operator. */
4098 if (VOID_TYPE_P (TREE_TYPE (arg))
4099 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4100 && (TREE_CODE (arg) != INDIRECT_REF
4101 || !flag_isoc99))
4102 pedwarn (location, 0, "taking address of expression of type %<void%>");
4103
4104 /* Let &* cancel out to simplify resulting code. */
4105 if (TREE_CODE (arg) == INDIRECT_REF)
4106 {
4107 /* Don't let this be an lvalue. */
4108 if (lvalue_p (TREE_OPERAND (arg, 0)))
4109 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4110 ret = TREE_OPERAND (arg, 0);
4111 goto return_build_unary_op;
4112 }
4113
4114 /* For &x[y], return x+y */
4115 if (TREE_CODE (arg) == ARRAY_REF)
4116 {
4117 tree op0 = TREE_OPERAND (arg, 0);
4118 if (!c_mark_addressable (op0))
4119 return error_mark_node;
4120 }
4121
4122 /* Anything not already handled and not a true memory reference
4123 or a non-lvalue array is an error. */
4124 else if (typecode != FUNCTION_TYPE && !flag
4125 && !lvalue_or_else (location, arg, lv_addressof))
4126 return error_mark_node;
4127
4128 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4129 folding later. */
4130 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4131 {
4132 tree inner = build_unary_op (location, code,
4133 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4134 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4135 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4136 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4137 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4138 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4139 goto return_build_unary_op;
4140 }
4141
4142 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4143 argtype = TREE_TYPE (arg);
4144
4145 /* If the lvalue is const or volatile, merge that into the type
4146 to which the address will point. This is only needed
4147 for function types. */
4148 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4149 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4150 && TREE_CODE (argtype) == FUNCTION_TYPE)
4151 {
4152 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4153 int quals = orig_quals;
4154
4155 if (TREE_READONLY (arg))
4156 quals |= TYPE_QUAL_CONST;
4157 if (TREE_THIS_VOLATILE (arg))
4158 quals |= TYPE_QUAL_VOLATILE;
4159
4160 argtype = c_build_qualified_type (argtype, quals);
4161 }
4162
4163 if (!c_mark_addressable (arg))
4164 return error_mark_node;
4165
4166 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4167 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4168
4169 argtype = build_pointer_type (argtype);
4170
4171 /* ??? Cope with user tricks that amount to offsetof. Delete this
4172 when we have proper support for integer constant expressions. */
4173 val = get_base_address (arg);
4174 if (val && TREE_CODE (val) == INDIRECT_REF
4175 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4176 {
4177 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4178 goto return_build_unary_op;
4179 }
4180
4181 val = build1 (ADDR_EXPR, argtype, arg);
4182
4183 ret = val;
4184 goto return_build_unary_op;
4185
4186 default:
4187 gcc_unreachable ();
4188 }
4189
4190 if (argtype == 0)
4191 argtype = TREE_TYPE (arg);
4192 if (TREE_CODE (arg) == INTEGER_CST)
4193 ret = (require_constant_value
4194 ? fold_build1_initializer_loc (location, code, argtype, arg)
4195 : fold_build1_loc (location, code, argtype, arg));
4196 else
4197 ret = build1 (code, argtype, arg);
4198 return_build_unary_op:
4199 gcc_assert (ret != error_mark_node);
4200 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4201 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4202 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4203 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4204 ret = note_integer_operands (ret);
4205 if (eptype)
4206 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4207 protected_set_expr_location (ret, location);
4208 return ret;
4209 }
4210
4211 /* Return nonzero if REF is an lvalue valid for this language.
4212 Lvalues can be assigned, unless their type has TYPE_READONLY.
4213 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4214
4215 bool
4216 lvalue_p (const_tree ref)
4217 {
4218 const enum tree_code code = TREE_CODE (ref);
4219
4220 switch (code)
4221 {
4222 case REALPART_EXPR:
4223 case IMAGPART_EXPR:
4224 case COMPONENT_REF:
4225 return lvalue_p (TREE_OPERAND (ref, 0));
4226
4227 case C_MAYBE_CONST_EXPR:
4228 return lvalue_p (TREE_OPERAND (ref, 1));
4229
4230 case COMPOUND_LITERAL_EXPR:
4231 case STRING_CST:
4232 return 1;
4233
4234 case INDIRECT_REF:
4235 case ARRAY_REF:
4236 case ARRAY_NOTATION_REF:
4237 case VAR_DECL:
4238 case PARM_DECL:
4239 case RESULT_DECL:
4240 case ERROR_MARK:
4241 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4242 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4243
4244 case BIND_EXPR:
4245 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4246
4247 default:
4248 return 0;
4249 }
4250 }
4251 \f
4252 /* Give a warning for storing in something that is read-only in GCC
4253 terms but not const in ISO C terms. */
4254
4255 static void
4256 readonly_warning (tree arg, enum lvalue_use use)
4257 {
4258 switch (use)
4259 {
4260 case lv_assign:
4261 warning (0, "assignment of read-only location %qE", arg);
4262 break;
4263 case lv_increment:
4264 warning (0, "increment of read-only location %qE", arg);
4265 break;
4266 case lv_decrement:
4267 warning (0, "decrement of read-only location %qE", arg);
4268 break;
4269 default:
4270 gcc_unreachable ();
4271 }
4272 return;
4273 }
4274
4275
4276 /* Return nonzero if REF is an lvalue valid for this language;
4277 otherwise, print an error message and return zero. USE says
4278 how the lvalue is being used and so selects the error message.
4279 LOCATION is the location at which any error should be reported. */
4280
4281 static int
4282 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4283 {
4284 int win = lvalue_p (ref);
4285
4286 if (!win)
4287 lvalue_error (loc, use);
4288
4289 return win;
4290 }
4291 \f
4292 /* Mark EXP saying that we need to be able to take the
4293 address of it; it should not be allocated in a register.
4294 Returns true if successful. */
4295
4296 bool
4297 c_mark_addressable (tree exp)
4298 {
4299 tree x = exp;
4300
4301 while (1)
4302 switch (TREE_CODE (x))
4303 {
4304 case COMPONENT_REF:
4305 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4306 {
4307 error
4308 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4309 return false;
4310 }
4311
4312 /* ... fall through ... */
4313
4314 case ADDR_EXPR:
4315 case ARRAY_REF:
4316 case REALPART_EXPR:
4317 case IMAGPART_EXPR:
4318 x = TREE_OPERAND (x, 0);
4319 break;
4320
4321 case COMPOUND_LITERAL_EXPR:
4322 case CONSTRUCTOR:
4323 TREE_ADDRESSABLE (x) = 1;
4324 return true;
4325
4326 case VAR_DECL:
4327 case CONST_DECL:
4328 case PARM_DECL:
4329 case RESULT_DECL:
4330 if (C_DECL_REGISTER (x)
4331 && DECL_NONLOCAL (x))
4332 {
4333 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4334 {
4335 error
4336 ("global register variable %qD used in nested function", x);
4337 return false;
4338 }
4339 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4340 }
4341 else if (C_DECL_REGISTER (x))
4342 {
4343 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4344 error ("address of global register variable %qD requested", x);
4345 else
4346 error ("address of register variable %qD requested", x);
4347 return false;
4348 }
4349
4350 /* drops in */
4351 case FUNCTION_DECL:
4352 TREE_ADDRESSABLE (x) = 1;
4353 /* drops out */
4354 default:
4355 return true;
4356 }
4357 }
4358 \f
4359 /* Convert EXPR to TYPE, warning about conversion problems with
4360 constants. SEMANTIC_TYPE is the type this conversion would use
4361 without excess precision. If SEMANTIC_TYPE is NULL, this function
4362 is equivalent to convert_and_check. This function is a wrapper that
4363 handles conversions that may be different than
4364 the usual ones because of excess precision. */
4365
4366 static tree
4367 ep_convert_and_check (location_t loc, tree type, tree expr,
4368 tree semantic_type)
4369 {
4370 if (TREE_TYPE (expr) == type)
4371 return expr;
4372
4373 if (!semantic_type)
4374 return convert_and_check (loc, type, expr);
4375
4376 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4377 && TREE_TYPE (expr) != semantic_type)
4378 {
4379 /* For integers, we need to check the real conversion, not
4380 the conversion to the excess precision type. */
4381 expr = convert_and_check (loc, semantic_type, expr);
4382 }
4383 /* Result type is the excess precision type, which should be
4384 large enough, so do not check. */
4385 return convert (type, expr);
4386 }
4387
4388 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4389 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4390 if folded to an integer constant then the unselected half may
4391 contain arbitrary operations not normally permitted in constant
4392 expressions. Set the location of the expression to LOC. */
4393
4394 tree
4395 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4396 tree op1, tree op1_original_type, tree op2,
4397 tree op2_original_type)
4398 {
4399 tree type1;
4400 tree type2;
4401 enum tree_code code1;
4402 enum tree_code code2;
4403 tree result_type = NULL;
4404 tree semantic_result_type = NULL;
4405 tree orig_op1 = op1, orig_op2 = op2;
4406 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4407 bool ifexp_int_operands;
4408 tree ret;
4409
4410 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4411 if (op1_int_operands)
4412 op1 = remove_c_maybe_const_expr (op1);
4413 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4414 if (op2_int_operands)
4415 op2 = remove_c_maybe_const_expr (op2);
4416 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4417 if (ifexp_int_operands)
4418 ifexp = remove_c_maybe_const_expr (ifexp);
4419
4420 /* Promote both alternatives. */
4421
4422 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4423 op1 = default_conversion (op1);
4424 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4425 op2 = default_conversion (op2);
4426
4427 if (TREE_CODE (ifexp) == ERROR_MARK
4428 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4429 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4430 return error_mark_node;
4431
4432 type1 = TREE_TYPE (op1);
4433 code1 = TREE_CODE (type1);
4434 type2 = TREE_TYPE (op2);
4435 code2 = TREE_CODE (type2);
4436
4437 /* C90 does not permit non-lvalue arrays in conditional expressions.
4438 In C99 they will be pointers by now. */
4439 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4440 {
4441 error_at (colon_loc, "non-lvalue array in conditional expression");
4442 return error_mark_node;
4443 }
4444
4445 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4446 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4447 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4448 || code1 == COMPLEX_TYPE)
4449 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4450 || code2 == COMPLEX_TYPE))
4451 {
4452 semantic_result_type = c_common_type (type1, type2);
4453 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4454 {
4455 op1 = TREE_OPERAND (op1, 0);
4456 type1 = TREE_TYPE (op1);
4457 gcc_assert (TREE_CODE (type1) == code1);
4458 }
4459 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4460 {
4461 op2 = TREE_OPERAND (op2, 0);
4462 type2 = TREE_TYPE (op2);
4463 gcc_assert (TREE_CODE (type2) == code2);
4464 }
4465 }
4466
4467 if (warn_cxx_compat)
4468 {
4469 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4470 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4471
4472 if (TREE_CODE (t1) == ENUMERAL_TYPE
4473 && TREE_CODE (t2) == ENUMERAL_TYPE
4474 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4475 warning_at (colon_loc, OPT_Wc___compat,
4476 ("different enum types in conditional is "
4477 "invalid in C++: %qT vs %qT"),
4478 t1, t2);
4479 }
4480
4481 /* Quickly detect the usual case where op1 and op2 have the same type
4482 after promotion. */
4483 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4484 {
4485 if (type1 == type2)
4486 result_type = type1;
4487 else
4488 result_type = TYPE_MAIN_VARIANT (type1);
4489 }
4490 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4491 || code1 == COMPLEX_TYPE)
4492 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4493 || code2 == COMPLEX_TYPE))
4494 {
4495 result_type = c_common_type (type1, type2);
4496 do_warn_double_promotion (result_type, type1, type2,
4497 "implicit conversion from %qT to %qT to "
4498 "match other result of conditional",
4499 colon_loc);
4500
4501 /* If -Wsign-compare, warn here if type1 and type2 have
4502 different signedness. We'll promote the signed to unsigned
4503 and later code won't know it used to be different.
4504 Do this check on the original types, so that explicit casts
4505 will be considered, but default promotions won't. */
4506 if (c_inhibit_evaluation_warnings == 0)
4507 {
4508 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4509 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4510
4511 if (unsigned_op1 ^ unsigned_op2)
4512 {
4513 bool ovf;
4514
4515 /* Do not warn if the result type is signed, since the
4516 signed type will only be chosen if it can represent
4517 all the values of the unsigned type. */
4518 if (!TYPE_UNSIGNED (result_type))
4519 /* OK */;
4520 else
4521 {
4522 bool op1_maybe_const = true;
4523 bool op2_maybe_const = true;
4524
4525 /* Do not warn if the signed quantity is an
4526 unsuffixed integer literal (or some static
4527 constant expression involving such literals) and
4528 it is non-negative. This warning requires the
4529 operands to be folded for best results, so do
4530 that folding in this case even without
4531 warn_sign_compare to avoid warning options
4532 possibly affecting code generation. */
4533 c_inhibit_evaluation_warnings
4534 += (ifexp == truthvalue_false_node);
4535 op1 = c_fully_fold (op1, require_constant_value,
4536 &op1_maybe_const);
4537 c_inhibit_evaluation_warnings
4538 -= (ifexp == truthvalue_false_node);
4539
4540 c_inhibit_evaluation_warnings
4541 += (ifexp == truthvalue_true_node);
4542 op2 = c_fully_fold (op2, require_constant_value,
4543 &op2_maybe_const);
4544 c_inhibit_evaluation_warnings
4545 -= (ifexp == truthvalue_true_node);
4546
4547 if (warn_sign_compare)
4548 {
4549 if ((unsigned_op2
4550 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4551 || (unsigned_op1
4552 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4553 /* OK */;
4554 else
4555 warning_at (colon_loc, OPT_Wsign_compare,
4556 ("signed and unsigned type in "
4557 "conditional expression"));
4558 }
4559 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4560 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4561 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4562 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4563 }
4564 }
4565 }
4566 }
4567 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4568 {
4569 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4570 pedwarn (colon_loc, OPT_Wpedantic,
4571 "ISO C forbids conditional expr with only one void side");
4572 result_type = void_type_node;
4573 }
4574 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4575 {
4576 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4577 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4578 addr_space_t as_common;
4579
4580 if (comp_target_types (colon_loc, type1, type2))
4581 result_type = common_pointer_type (type1, type2);
4582 else if (null_pointer_constant_p (orig_op1))
4583 result_type = type2;
4584 else if (null_pointer_constant_p (orig_op2))
4585 result_type = type1;
4586 else if (!addr_space_superset (as1, as2, &as_common))
4587 {
4588 error_at (colon_loc, "pointers to disjoint address spaces "
4589 "used in conditional expression");
4590 return error_mark_node;
4591 }
4592 else if (VOID_TYPE_P (TREE_TYPE (type1))
4593 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4594 {
4595 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4596 pedwarn (colon_loc, OPT_Wpedantic,
4597 "ISO C forbids conditional expr between "
4598 "%<void *%> and function pointer");
4599 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4600 TREE_TYPE (type2)));
4601 }
4602 else if (VOID_TYPE_P (TREE_TYPE (type2))
4603 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4604 {
4605 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4606 pedwarn (colon_loc, OPT_Wpedantic,
4607 "ISO C forbids conditional expr between "
4608 "%<void *%> and function pointer");
4609 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4610 TREE_TYPE (type1)));
4611 }
4612 /* Objective-C pointer comparisons are a bit more lenient. */
4613 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4614 result_type = objc_common_type (type1, type2);
4615 else
4616 {
4617 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4618
4619 pedwarn (colon_loc, 0,
4620 "pointer type mismatch in conditional expression");
4621 result_type = build_pointer_type
4622 (build_qualified_type (void_type_node, qual));
4623 }
4624 }
4625 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4626 {
4627 if (!null_pointer_constant_p (orig_op2))
4628 pedwarn (colon_loc, 0,
4629 "pointer/integer type mismatch in conditional expression");
4630 else
4631 {
4632 op2 = null_pointer_node;
4633 }
4634 result_type = type1;
4635 }
4636 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4637 {
4638 if (!null_pointer_constant_p (orig_op1))
4639 pedwarn (colon_loc, 0,
4640 "pointer/integer type mismatch in conditional expression");
4641 else
4642 {
4643 op1 = null_pointer_node;
4644 }
4645 result_type = type2;
4646 }
4647
4648 if (!result_type)
4649 {
4650 if (flag_cond_mismatch)
4651 result_type = void_type_node;
4652 else
4653 {
4654 error_at (colon_loc, "type mismatch in conditional expression");
4655 return error_mark_node;
4656 }
4657 }
4658
4659 /* Merge const and volatile flags of the incoming types. */
4660 result_type
4661 = build_type_variant (result_type,
4662 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4663 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4664
4665 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4666 semantic_result_type);
4667 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4668 semantic_result_type);
4669
4670 if (ifexp_bcp && ifexp == truthvalue_true_node)
4671 {
4672 op2_int_operands = true;
4673 op1 = c_fully_fold (op1, require_constant_value, NULL);
4674 }
4675 if (ifexp_bcp && ifexp == truthvalue_false_node)
4676 {
4677 op1_int_operands = true;
4678 op2 = c_fully_fold (op2, require_constant_value, NULL);
4679 }
4680 int_const = int_operands = (ifexp_int_operands
4681 && op1_int_operands
4682 && op2_int_operands);
4683 if (int_operands)
4684 {
4685 int_const = ((ifexp == truthvalue_true_node
4686 && TREE_CODE (orig_op1) == INTEGER_CST
4687 && !TREE_OVERFLOW (orig_op1))
4688 || (ifexp == truthvalue_false_node
4689 && TREE_CODE (orig_op2) == INTEGER_CST
4690 && !TREE_OVERFLOW (orig_op2)));
4691 }
4692 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4693 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4694 else
4695 {
4696 if (int_operands)
4697 {
4698 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4699 nested inside of the expression. */
4700 op1 = c_fully_fold (op1, false, NULL);
4701 op2 = c_fully_fold (op2, false, NULL);
4702 }
4703 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4704 if (int_operands)
4705 ret = note_integer_operands (ret);
4706 }
4707 if (semantic_result_type)
4708 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4709
4710 protected_set_expr_location (ret, colon_loc);
4711 return ret;
4712 }
4713 \f
4714 /* Return a compound expression that performs two expressions and
4715 returns the value of the second of them.
4716
4717 LOC is the location of the COMPOUND_EXPR. */
4718
4719 tree
4720 build_compound_expr (location_t loc, tree expr1, tree expr2)
4721 {
4722 bool expr1_int_operands, expr2_int_operands;
4723 tree eptype = NULL_TREE;
4724 tree ret;
4725
4726 if (flag_cilkplus
4727 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4728 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4729 {
4730 error_at (loc,
4731 "spawned function call cannot be part of a comma expression");
4732 return error_mark_node;
4733 }
4734 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4735 if (expr1_int_operands)
4736 expr1 = remove_c_maybe_const_expr (expr1);
4737 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4738 if (expr2_int_operands)
4739 expr2 = remove_c_maybe_const_expr (expr2);
4740
4741 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4742 expr1 = TREE_OPERAND (expr1, 0);
4743 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4744 {
4745 eptype = TREE_TYPE (expr2);
4746 expr2 = TREE_OPERAND (expr2, 0);
4747 }
4748
4749 if (!TREE_SIDE_EFFECTS (expr1))
4750 {
4751 /* The left-hand operand of a comma expression is like an expression
4752 statement: with -Wunused, we should warn if it doesn't have
4753 any side-effects, unless it was explicitly cast to (void). */
4754 if (warn_unused_value)
4755 {
4756 if (VOID_TYPE_P (TREE_TYPE (expr1))
4757 && CONVERT_EXPR_P (expr1))
4758 ; /* (void) a, b */
4759 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4760 && TREE_CODE (expr1) == COMPOUND_EXPR
4761 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4762 ; /* (void) a, (void) b, c */
4763 else
4764 warning_at (loc, OPT_Wunused_value,
4765 "left-hand operand of comma expression has no effect");
4766 }
4767 }
4768 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4769 && warn_unused_value)
4770 {
4771 tree r = expr1;
4772 location_t cloc = loc;
4773 while (TREE_CODE (r) == COMPOUND_EXPR)
4774 {
4775 if (EXPR_HAS_LOCATION (r))
4776 cloc = EXPR_LOCATION (r);
4777 r = TREE_OPERAND (r, 1);
4778 }
4779 if (!TREE_SIDE_EFFECTS (r)
4780 && !VOID_TYPE_P (TREE_TYPE (r))
4781 && !CONVERT_EXPR_P (r))
4782 warning_at (cloc, OPT_Wunused_value,
4783 "right-hand operand of comma expression has no effect");
4784 }
4785
4786 /* With -Wunused, we should also warn if the left-hand operand does have
4787 side-effects, but computes a value which is not used. For example, in
4788 `foo() + bar(), baz()' the result of the `+' operator is not used,
4789 so we should issue a warning. */
4790 else if (warn_unused_value)
4791 warn_if_unused_value (expr1, loc);
4792
4793 if (expr2 == error_mark_node)
4794 return error_mark_node;
4795
4796 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4797
4798 if (flag_isoc99
4799 && expr1_int_operands
4800 && expr2_int_operands)
4801 ret = note_integer_operands (ret);
4802
4803 if (eptype)
4804 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4805
4806 protected_set_expr_location (ret, loc);
4807 return ret;
4808 }
4809
4810 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4811 which we are casting. OTYPE is the type of the expression being
4812 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4813 of the cast. -Wcast-qual appeared on the command line. Named
4814 address space qualifiers are not handled here, because they result
4815 in different warnings. */
4816
4817 static void
4818 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4819 {
4820 tree in_type = type;
4821 tree in_otype = otype;
4822 int added = 0;
4823 int discarded = 0;
4824 bool is_const;
4825
4826 /* Check that the qualifiers on IN_TYPE are a superset of the
4827 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4828 nodes is uninteresting and we stop as soon as we hit a
4829 non-POINTER_TYPE node on either type. */
4830 do
4831 {
4832 in_otype = TREE_TYPE (in_otype);
4833 in_type = TREE_TYPE (in_type);
4834
4835 /* GNU C allows cv-qualified function types. 'const' means the
4836 function is very pure, 'volatile' means it can't return. We
4837 need to warn when such qualifiers are added, not when they're
4838 taken away. */
4839 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4840 && TREE_CODE (in_type) == FUNCTION_TYPE)
4841 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4842 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4843 else
4844 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4845 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4846 }
4847 while (TREE_CODE (in_type) == POINTER_TYPE
4848 && TREE_CODE (in_otype) == POINTER_TYPE);
4849
4850 if (added)
4851 warning_at (loc, OPT_Wcast_qual,
4852 "cast adds %q#v qualifier to function type", added);
4853
4854 if (discarded)
4855 /* There are qualifiers present in IN_OTYPE that are not present
4856 in IN_TYPE. */
4857 warning_at (loc, OPT_Wcast_qual,
4858 "cast discards %q#v qualifier from pointer target type",
4859 discarded);
4860
4861 if (added || discarded)
4862 return;
4863
4864 /* A cast from **T to const **T is unsafe, because it can cause a
4865 const value to be changed with no additional warning. We only
4866 issue this warning if T is the same on both sides, and we only
4867 issue the warning if there are the same number of pointers on
4868 both sides, as otherwise the cast is clearly unsafe anyhow. A
4869 cast is unsafe when a qualifier is added at one level and const
4870 is not present at all outer levels.
4871
4872 To issue this warning, we check at each level whether the cast
4873 adds new qualifiers not already seen. We don't need to special
4874 case function types, as they won't have the same
4875 TYPE_MAIN_VARIANT. */
4876
4877 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4878 return;
4879 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4880 return;
4881
4882 in_type = type;
4883 in_otype = otype;
4884 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4885 do
4886 {
4887 in_type = TREE_TYPE (in_type);
4888 in_otype = TREE_TYPE (in_otype);
4889 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4890 && !is_const)
4891 {
4892 warning_at (loc, OPT_Wcast_qual,
4893 "to be safe all intermediate pointers in cast from "
4894 "%qT to %qT must be %<const%> qualified",
4895 otype, type);
4896 break;
4897 }
4898 if (is_const)
4899 is_const = TYPE_READONLY (in_type);
4900 }
4901 while (TREE_CODE (in_type) == POINTER_TYPE);
4902 }
4903
4904 /* Build an expression representing a cast to type TYPE of expression EXPR.
4905 LOC is the location of the cast-- typically the open paren of the cast. */
4906
4907 tree
4908 build_c_cast (location_t loc, tree type, tree expr)
4909 {
4910 tree value;
4911
4912 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4913 expr = TREE_OPERAND (expr, 0);
4914
4915 value = expr;
4916
4917 if (type == error_mark_node || expr == error_mark_node)
4918 return error_mark_node;
4919
4920 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4921 only in <protocol> qualifications. But when constructing cast expressions,
4922 the protocols do matter and must be kept around. */
4923 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4924 return build1 (NOP_EXPR, type, expr);
4925
4926 type = TYPE_MAIN_VARIANT (type);
4927
4928 if (TREE_CODE (type) == ARRAY_TYPE)
4929 {
4930 error_at (loc, "cast specifies array type");
4931 return error_mark_node;
4932 }
4933
4934 if (TREE_CODE (type) == FUNCTION_TYPE)
4935 {
4936 error_at (loc, "cast specifies function type");
4937 return error_mark_node;
4938 }
4939
4940 if (!VOID_TYPE_P (type))
4941 {
4942 value = require_complete_type (value);
4943 if (value == error_mark_node)
4944 return error_mark_node;
4945 }
4946
4947 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4948 {
4949 if (TREE_CODE (type) == RECORD_TYPE
4950 || TREE_CODE (type) == UNION_TYPE)
4951 pedwarn (loc, OPT_Wpedantic,
4952 "ISO C forbids casting nonscalar to the same type");
4953 }
4954 else if (TREE_CODE (type) == UNION_TYPE)
4955 {
4956 tree field;
4957
4958 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4959 if (TREE_TYPE (field) != error_mark_node
4960 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4961 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4962 break;
4963
4964 if (field)
4965 {
4966 tree t;
4967 bool maybe_const = true;
4968
4969 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4970 t = c_fully_fold (value, false, &maybe_const);
4971 t = build_constructor_single (type, field, t);
4972 if (!maybe_const)
4973 t = c_wrap_maybe_const (t, true);
4974 t = digest_init (loc, type, t,
4975 NULL_TREE, false, true, 0);
4976 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4977 return t;
4978 }
4979 error_at (loc, "cast to union type from type not present in union");
4980 return error_mark_node;
4981 }
4982 else
4983 {
4984 tree otype, ovalue;
4985
4986 if (type == void_type_node)
4987 {
4988 tree t = build1 (CONVERT_EXPR, type, value);
4989 SET_EXPR_LOCATION (t, loc);
4990 return t;
4991 }
4992
4993 otype = TREE_TYPE (value);
4994
4995 /* Optionally warn about potentially worrisome casts. */
4996 if (warn_cast_qual
4997 && TREE_CODE (type) == POINTER_TYPE
4998 && TREE_CODE (otype) == POINTER_TYPE)
4999 handle_warn_cast_qual (loc, type, otype);
5000
5001 /* Warn about conversions between pointers to disjoint
5002 address spaces. */
5003 if (TREE_CODE (type) == POINTER_TYPE
5004 && TREE_CODE (otype) == POINTER_TYPE
5005 && !null_pointer_constant_p (value))
5006 {
5007 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5008 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5009 addr_space_t as_common;
5010
5011 if (!addr_space_superset (as_to, as_from, &as_common))
5012 {
5013 if (ADDR_SPACE_GENERIC_P (as_from))
5014 warning_at (loc, 0, "cast to %s address space pointer "
5015 "from disjoint generic address space pointer",
5016 c_addr_space_name (as_to));
5017
5018 else if (ADDR_SPACE_GENERIC_P (as_to))
5019 warning_at (loc, 0, "cast to generic address space pointer "
5020 "from disjoint %s address space pointer",
5021 c_addr_space_name (as_from));
5022
5023 else
5024 warning_at (loc, 0, "cast to %s address space pointer "
5025 "from disjoint %s address space pointer",
5026 c_addr_space_name (as_to),
5027 c_addr_space_name (as_from));
5028 }
5029 }
5030
5031 /* Warn about possible alignment problems. */
5032 if (STRICT_ALIGNMENT
5033 && TREE_CODE (type) == POINTER_TYPE
5034 && TREE_CODE (otype) == POINTER_TYPE
5035 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5036 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5037 /* Don't warn about opaque types, where the actual alignment
5038 restriction is unknown. */
5039 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5040 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5041 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5042 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5043 warning_at (loc, OPT_Wcast_align,
5044 "cast increases required alignment of target type");
5045
5046 if (TREE_CODE (type) == INTEGER_TYPE
5047 && TREE_CODE (otype) == POINTER_TYPE
5048 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5049 /* Unlike conversion of integers to pointers, where the
5050 warning is disabled for converting constants because
5051 of cases such as SIG_*, warn about converting constant
5052 pointers to integers. In some cases it may cause unwanted
5053 sign extension, and a warning is appropriate. */
5054 warning_at (loc, OPT_Wpointer_to_int_cast,
5055 "cast from pointer to integer of different size");
5056
5057 if (TREE_CODE (value) == CALL_EXPR
5058 && TREE_CODE (type) != TREE_CODE (otype))
5059 warning_at (loc, OPT_Wbad_function_cast,
5060 "cast from function call of type %qT "
5061 "to non-matching type %qT", otype, type);
5062
5063 if (TREE_CODE (type) == POINTER_TYPE
5064 && TREE_CODE (otype) == INTEGER_TYPE
5065 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5066 /* Don't warn about converting any constant. */
5067 && !TREE_CONSTANT (value))
5068 warning_at (loc,
5069 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5070 "of different size");
5071
5072 if (warn_strict_aliasing <= 2)
5073 strict_aliasing_warning (otype, type, expr);
5074
5075 /* If pedantic, warn for conversions between function and object
5076 pointer types, except for converting a null pointer constant
5077 to function pointer type. */
5078 if (pedantic
5079 && TREE_CODE (type) == POINTER_TYPE
5080 && TREE_CODE (otype) == POINTER_TYPE
5081 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5082 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5083 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5084 "conversion of function pointer to object pointer type");
5085
5086 if (pedantic
5087 && TREE_CODE (type) == POINTER_TYPE
5088 && TREE_CODE (otype) == POINTER_TYPE
5089 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5090 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5091 && !null_pointer_constant_p (value))
5092 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5093 "conversion of object pointer to function pointer type");
5094
5095 ovalue = value;
5096 value = convert (type, value);
5097
5098 /* Ignore any integer overflow caused by the cast. */
5099 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5100 {
5101 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5102 {
5103 if (!TREE_OVERFLOW (value))
5104 {
5105 /* Avoid clobbering a shared constant. */
5106 value = copy_node (value);
5107 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5108 }
5109 }
5110 else if (TREE_OVERFLOW (value))
5111 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5112 value = build_int_cst_wide (TREE_TYPE (value),
5113 TREE_INT_CST_LOW (value),
5114 TREE_INT_CST_HIGH (value));
5115 }
5116 }
5117
5118 /* Don't let a cast be an lvalue. */
5119 if (value == expr)
5120 value = non_lvalue_loc (loc, value);
5121
5122 /* Don't allow the results of casting to floating-point or complex
5123 types be confused with actual constants, or casts involving
5124 integer and pointer types other than direct integer-to-integer
5125 and integer-to-pointer be confused with integer constant
5126 expressions and null pointer constants. */
5127 if (TREE_CODE (value) == REAL_CST
5128 || TREE_CODE (value) == COMPLEX_CST
5129 || (TREE_CODE (value) == INTEGER_CST
5130 && !((TREE_CODE (expr) == INTEGER_CST
5131 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5132 || TREE_CODE (expr) == REAL_CST
5133 || TREE_CODE (expr) == COMPLEX_CST)))
5134 value = build1 (NOP_EXPR, type, value);
5135
5136 if (CAN_HAVE_LOCATION_P (value))
5137 SET_EXPR_LOCATION (value, loc);
5138 return value;
5139 }
5140
5141 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5142 location of the open paren of the cast, or the position of the cast
5143 expr. */
5144 tree
5145 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5146 {
5147 tree type;
5148 tree type_expr = NULL_TREE;
5149 bool type_expr_const = true;
5150 tree ret;
5151 int saved_wsp = warn_strict_prototypes;
5152
5153 /* This avoids warnings about unprototyped casts on
5154 integers. E.g. "#define SIG_DFL (void(*)())0". */
5155 if (TREE_CODE (expr) == INTEGER_CST)
5156 warn_strict_prototypes = 0;
5157 type = groktypename (type_name, &type_expr, &type_expr_const);
5158 warn_strict_prototypes = saved_wsp;
5159
5160 ret = build_c_cast (loc, type, expr);
5161 if (type_expr)
5162 {
5163 bool inner_expr_const = true;
5164 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5165 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5166 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5167 && inner_expr_const);
5168 SET_EXPR_LOCATION (ret, loc);
5169 }
5170
5171 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5172 SET_EXPR_LOCATION (ret, loc);
5173
5174 /* C++ does not permits types to be defined in a cast, but it
5175 allows references to incomplete types. */
5176 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5177 warning_at (loc, OPT_Wc___compat,
5178 "defining a type in a cast is invalid in C++");
5179
5180 return ret;
5181 }
5182 \f
5183 /* Build an assignment expression of lvalue LHS from value RHS.
5184 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5185 may differ from TREE_TYPE (LHS) for an enum bitfield.
5186 MODIFYCODE is the code for a binary operator that we use
5187 to combine the old value of LHS with RHS to get the new value.
5188 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5189 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5190 which may differ from TREE_TYPE (RHS) for an enum value.
5191
5192 LOCATION is the location of the MODIFYCODE operator.
5193 RHS_LOC is the location of the RHS. */
5194
5195 tree
5196 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5197 enum tree_code modifycode,
5198 location_t rhs_loc, tree rhs, tree rhs_origtype)
5199 {
5200 tree result;
5201 tree newrhs;
5202 tree rhseval = NULL_TREE;
5203 tree rhs_semantic_type = NULL_TREE;
5204 tree lhstype = TREE_TYPE (lhs);
5205 tree olhstype = lhstype;
5206 bool npc;
5207 bool is_atomic_op;
5208
5209 /* Types that aren't fully specified cannot be used in assignments. */
5210 lhs = require_complete_type (lhs);
5211
5212 /* Avoid duplicate error messages from operands that had errors. */
5213 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5214 return error_mark_node;
5215
5216 /* Ensure an error for assigning a non-lvalue array to an array in
5217 C90. */
5218 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5219 {
5220 error_at (location, "assignment to expression with array type");
5221 return error_mark_node;
5222 }
5223
5224 /* For ObjC properties, defer this check. */
5225 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5226 return error_mark_node;
5227
5228 is_atomic_op = really_atomic_lvalue (lhs);
5229
5230 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5231 {
5232 rhs_semantic_type = TREE_TYPE (rhs);
5233 rhs = TREE_OPERAND (rhs, 0);
5234 }
5235
5236 newrhs = rhs;
5237
5238 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5239 {
5240 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5241 lhs_origtype, modifycode, rhs_loc, rhs,
5242 rhs_origtype);
5243 if (inner == error_mark_node)
5244 return error_mark_node;
5245 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5246 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5247 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5248 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5249 protected_set_expr_location (result, location);
5250 return result;
5251 }
5252
5253 /* If a binary op has been requested, combine the old LHS value with the RHS
5254 producing the value we should actually store into the LHS. */
5255
5256 if (modifycode != NOP_EXPR)
5257 {
5258 lhs = c_fully_fold (lhs, false, NULL);
5259 lhs = stabilize_reference (lhs);
5260
5261 /* Construct the RHS for any non-atomic compound assignemnt. */
5262 if (!is_atomic_op)
5263 {
5264 /* If in LHS op= RHS the RHS has side-effects, ensure they
5265 are preevaluated before the rest of the assignment expression's
5266 side-effects, because RHS could contain e.g. function calls
5267 that modify LHS. */
5268 if (TREE_SIDE_EFFECTS (rhs))
5269 {
5270 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5271 rhseval = newrhs;
5272 }
5273 newrhs = build_binary_op (location,
5274 modifycode, lhs, newrhs, 1);
5275
5276 /* The original type of the right hand side is no longer
5277 meaningful. */
5278 rhs_origtype = NULL_TREE;
5279 }
5280 }
5281
5282 if (c_dialect_objc ())
5283 {
5284 /* Check if we are modifying an Objective-C property reference;
5285 if so, we need to generate setter calls. */
5286 result = objc_maybe_build_modify_expr (lhs, newrhs);
5287 if (result)
5288 goto return_result;
5289
5290 /* Else, do the check that we postponed for Objective-C. */
5291 if (!lvalue_or_else (location, lhs, lv_assign))
5292 return error_mark_node;
5293 }
5294
5295 /* Give an error for storing in something that is 'const'. */
5296
5297 if (TYPE_READONLY (lhstype)
5298 || ((TREE_CODE (lhstype) == RECORD_TYPE
5299 || TREE_CODE (lhstype) == UNION_TYPE)
5300 && C_TYPE_FIELDS_READONLY (lhstype)))
5301 {
5302 readonly_error (location, lhs, lv_assign);
5303 return error_mark_node;
5304 }
5305 else if (TREE_READONLY (lhs))
5306 readonly_warning (lhs, lv_assign);
5307
5308 /* If storing into a structure or union member,
5309 it has probably been given type `int'.
5310 Compute the type that would go with
5311 the actual amount of storage the member occupies. */
5312
5313 if (TREE_CODE (lhs) == COMPONENT_REF
5314 && (TREE_CODE (lhstype) == INTEGER_TYPE
5315 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5316 || TREE_CODE (lhstype) == REAL_TYPE
5317 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5318 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5319
5320 /* If storing in a field that is in actuality a short or narrower than one,
5321 we must store in the field in its actual type. */
5322
5323 if (lhstype != TREE_TYPE (lhs))
5324 {
5325 lhs = copy_node (lhs);
5326 TREE_TYPE (lhs) = lhstype;
5327 }
5328
5329 /* Issue -Wc++-compat warnings about an assignment to an enum type
5330 when LHS does not have its original type. This happens for,
5331 e.g., an enum bitfield in a struct. */
5332 if (warn_cxx_compat
5333 && lhs_origtype != NULL_TREE
5334 && lhs_origtype != lhstype
5335 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5336 {
5337 tree checktype = (rhs_origtype != NULL_TREE
5338 ? rhs_origtype
5339 : TREE_TYPE (rhs));
5340 if (checktype != error_mark_node
5341 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5342 || (is_atomic_op && modifycode != NOP_EXPR)))
5343 warning_at (location, OPT_Wc___compat,
5344 "enum conversion in assignment is invalid in C++");
5345 }
5346
5347 /* If the lhs is atomic, remove that qualifier. */
5348 if (is_atomic_op)
5349 {
5350 lhstype = build_qualified_type (lhstype,
5351 (TYPE_QUALS (lhstype)
5352 & ~TYPE_QUAL_ATOMIC));
5353 olhstype = build_qualified_type (olhstype,
5354 (TYPE_QUALS (lhstype)
5355 & ~TYPE_QUAL_ATOMIC));
5356 }
5357
5358 /* Convert new value to destination type. Fold it first, then
5359 restore any excess precision information, for the sake of
5360 conversion warnings. */
5361
5362 if (!(is_atomic_op && modifycode != NOP_EXPR))
5363 {
5364 npc = null_pointer_constant_p (newrhs);
5365 newrhs = c_fully_fold (newrhs, false, NULL);
5366 if (rhs_semantic_type)
5367 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5368 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5369 rhs_origtype, ic_assign, npc,
5370 NULL_TREE, NULL_TREE, 0);
5371 if (TREE_CODE (newrhs) == ERROR_MARK)
5372 return error_mark_node;
5373 }
5374
5375 /* Emit ObjC write barrier, if necessary. */
5376 if (c_dialect_objc () && flag_objc_gc)
5377 {
5378 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5379 if (result)
5380 {
5381 protected_set_expr_location (result, location);
5382 goto return_result;
5383 }
5384 }
5385
5386 /* Scan operands. */
5387
5388 if (is_atomic_op)
5389 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5390 else
5391 {
5392 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5393 TREE_SIDE_EFFECTS (result) = 1;
5394 protected_set_expr_location (result, location);
5395 }
5396
5397 /* If we got the LHS in a different type for storing in,
5398 convert the result back to the nominal type of LHS
5399 so that the value we return always has the same type
5400 as the LHS argument. */
5401
5402 if (olhstype == TREE_TYPE (result))
5403 goto return_result;
5404
5405 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5406 rhs_origtype, ic_assign, false, NULL_TREE,
5407 NULL_TREE, 0);
5408 protected_set_expr_location (result, location);
5409
5410 return_result:
5411 if (rhseval)
5412 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5413 return result;
5414 }
5415 \f
5416 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5417 This is used to implement -fplan9-extensions. */
5418
5419 static bool
5420 find_anonymous_field_with_type (tree struct_type, tree type)
5421 {
5422 tree field;
5423 bool found;
5424
5425 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5426 || TREE_CODE (struct_type) == UNION_TYPE);
5427 found = false;
5428 for (field = TYPE_FIELDS (struct_type);
5429 field != NULL_TREE;
5430 field = TREE_CHAIN (field))
5431 {
5432 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5433 ? c_build_qualified_type (TREE_TYPE (field),
5434 TYPE_QUAL_ATOMIC)
5435 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5436 if (DECL_NAME (field) == NULL
5437 && comptypes (type, fieldtype))
5438 {
5439 if (found)
5440 return false;
5441 found = true;
5442 }
5443 else if (DECL_NAME (field) == NULL
5444 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5445 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5446 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5447 {
5448 if (found)
5449 return false;
5450 found = true;
5451 }
5452 }
5453 return found;
5454 }
5455
5456 /* RHS is an expression whose type is pointer to struct. If there is
5457 an anonymous field in RHS with type TYPE, then return a pointer to
5458 that field in RHS. This is used with -fplan9-extensions. This
5459 returns NULL if no conversion could be found. */
5460
5461 static tree
5462 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5463 {
5464 tree rhs_struct_type, lhs_main_type;
5465 tree field, found_field;
5466 bool found_sub_field;
5467 tree ret;
5468
5469 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5470 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5471 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5472 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5473
5474 gcc_assert (POINTER_TYPE_P (type));
5475 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5476 ? c_build_qualified_type (TREE_TYPE (type),
5477 TYPE_QUAL_ATOMIC)
5478 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5479
5480 found_field = NULL_TREE;
5481 found_sub_field = false;
5482 for (field = TYPE_FIELDS (rhs_struct_type);
5483 field != NULL_TREE;
5484 field = TREE_CHAIN (field))
5485 {
5486 if (DECL_NAME (field) != NULL_TREE
5487 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5488 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5489 continue;
5490 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5491 ? c_build_qualified_type (TREE_TYPE (field),
5492 TYPE_QUAL_ATOMIC)
5493 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5494 if (comptypes (lhs_main_type, fieldtype))
5495 {
5496 if (found_field != NULL_TREE)
5497 return NULL_TREE;
5498 found_field = field;
5499 }
5500 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5501 lhs_main_type))
5502 {
5503 if (found_field != NULL_TREE)
5504 return NULL_TREE;
5505 found_field = field;
5506 found_sub_field = true;
5507 }
5508 }
5509
5510 if (found_field == NULL_TREE)
5511 return NULL_TREE;
5512
5513 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5514 build_fold_indirect_ref (rhs), found_field,
5515 NULL_TREE);
5516 ret = build_fold_addr_expr_loc (location, ret);
5517
5518 if (found_sub_field)
5519 {
5520 ret = convert_to_anonymous_field (location, type, ret);
5521 gcc_assert (ret != NULL_TREE);
5522 }
5523
5524 return ret;
5525 }
5526
5527 /* Convert value RHS to type TYPE as preparation for an assignment to
5528 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5529 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5530 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5531 constant before any folding.
5532 The real work of conversion is done by `convert'.
5533 The purpose of this function is to generate error messages
5534 for assignments that are not allowed in C.
5535 ERRTYPE says whether it is argument passing, assignment,
5536 initialization or return.
5537
5538 LOCATION is the location of the assignment, EXPR_LOC is the location of
5539 the RHS or, for a function, location of an argument.
5540 FUNCTION is a tree for the function being called.
5541 PARMNUM is the number of the argument, for printing in error messages. */
5542
5543 static tree
5544 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5545 tree rhs, tree origtype, enum impl_conv errtype,
5546 bool null_pointer_constant, tree fundecl,
5547 tree function, int parmnum)
5548 {
5549 enum tree_code codel = TREE_CODE (type);
5550 tree orig_rhs = rhs;
5551 tree rhstype;
5552 enum tree_code coder;
5553 tree rname = NULL_TREE;
5554 bool objc_ok = false;
5555
5556 if (errtype == ic_argpass)
5557 {
5558 tree selector;
5559 /* Change pointer to function to the function itself for
5560 diagnostics. */
5561 if (TREE_CODE (function) == ADDR_EXPR
5562 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5563 function = TREE_OPERAND (function, 0);
5564
5565 /* Handle an ObjC selector specially for diagnostics. */
5566 selector = objc_message_selector ();
5567 rname = function;
5568 if (selector && parmnum > 2)
5569 {
5570 rname = selector;
5571 parmnum -= 2;
5572 }
5573 }
5574
5575 /* This macro is used to emit diagnostics to ensure that all format
5576 strings are complete sentences, visible to gettext and checked at
5577 compile time. */
5578 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5579 do { \
5580 switch (errtype) \
5581 { \
5582 case ic_argpass: \
5583 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5584 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5585 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5586 "expected %qT but argument is of type %qT", \
5587 type, rhstype); \
5588 break; \
5589 case ic_assign: \
5590 pedwarn (LOCATION, OPT, AS); \
5591 break; \
5592 case ic_init: \
5593 pedwarn_init (LOCATION, OPT, IN); \
5594 break; \
5595 case ic_return: \
5596 pedwarn (LOCATION, OPT, RE); \
5597 break; \
5598 default: \
5599 gcc_unreachable (); \
5600 } \
5601 } while (0)
5602
5603 /* This macro is used to emit diagnostics to ensure that all format
5604 strings are complete sentences, visible to gettext and checked at
5605 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5606 extra parameter to enumerate qualifiers. */
5607
5608 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5609 do { \
5610 switch (errtype) \
5611 { \
5612 case ic_argpass: \
5613 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5614 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5615 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5616 "expected %qT but argument is of type %qT", \
5617 type, rhstype); \
5618 break; \
5619 case ic_assign: \
5620 pedwarn (LOCATION, OPT, AS, QUALS); \
5621 break; \
5622 case ic_init: \
5623 pedwarn (LOCATION, OPT, IN, QUALS); \
5624 break; \
5625 case ic_return: \
5626 pedwarn (LOCATION, OPT, RE, QUALS); \
5627 break; \
5628 default: \
5629 gcc_unreachable (); \
5630 } \
5631 } while (0)
5632
5633 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5634 rhs = TREE_OPERAND (rhs, 0);
5635
5636 rhstype = TREE_TYPE (rhs);
5637 coder = TREE_CODE (rhstype);
5638
5639 if (coder == ERROR_MARK)
5640 return error_mark_node;
5641
5642 if (c_dialect_objc ())
5643 {
5644 int parmno;
5645
5646 switch (errtype)
5647 {
5648 case ic_return:
5649 parmno = 0;
5650 break;
5651
5652 case ic_assign:
5653 parmno = -1;
5654 break;
5655
5656 case ic_init:
5657 parmno = -2;
5658 break;
5659
5660 default:
5661 parmno = parmnum;
5662 break;
5663 }
5664
5665 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5666 }
5667
5668 if (warn_cxx_compat)
5669 {
5670 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5671 if (checktype != error_mark_node
5672 && TREE_CODE (type) == ENUMERAL_TYPE
5673 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5674 {
5675 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5676 G_("enum conversion when passing argument "
5677 "%d of %qE is invalid in C++"),
5678 G_("enum conversion in assignment is "
5679 "invalid in C++"),
5680 G_("enum conversion in initialization is "
5681 "invalid in C++"),
5682 G_("enum conversion in return is "
5683 "invalid in C++"));
5684 }
5685 }
5686
5687 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5688 return rhs;
5689
5690 if (coder == VOID_TYPE)
5691 {
5692 /* Except for passing an argument to an unprototyped function,
5693 this is a constraint violation. When passing an argument to
5694 an unprototyped function, it is compile-time undefined;
5695 making it a constraint in that case was rejected in
5696 DR#252. */
5697 error_at (location, "void value not ignored as it ought to be");
5698 return error_mark_node;
5699 }
5700 rhs = require_complete_type (rhs);
5701 if (rhs == error_mark_node)
5702 return error_mark_node;
5703 /* A non-reference type can convert to a reference. This handles
5704 va_start, va_copy and possibly port built-ins. */
5705 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5706 {
5707 if (!lvalue_p (rhs))
5708 {
5709 error_at (location, "cannot pass rvalue to reference parameter");
5710 return error_mark_node;
5711 }
5712 if (!c_mark_addressable (rhs))
5713 return error_mark_node;
5714 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5715 SET_EXPR_LOCATION (rhs, location);
5716
5717 rhs = convert_for_assignment (location, expr_loc,
5718 build_pointer_type (TREE_TYPE (type)),
5719 rhs, origtype, errtype,
5720 null_pointer_constant, fundecl, function,
5721 parmnum);
5722 if (rhs == error_mark_node)
5723 return error_mark_node;
5724
5725 rhs = build1 (NOP_EXPR, type, rhs);
5726 SET_EXPR_LOCATION (rhs, location);
5727 return rhs;
5728 }
5729 /* Some types can interconvert without explicit casts. */
5730 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5731 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5732 return convert (type, rhs);
5733 /* Arithmetic types all interconvert, and enum is treated like int. */
5734 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5735 || codel == FIXED_POINT_TYPE
5736 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5737 || codel == BOOLEAN_TYPE)
5738 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5739 || coder == FIXED_POINT_TYPE
5740 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5741 || coder == BOOLEAN_TYPE))
5742 {
5743 tree ret;
5744 bool save = in_late_binary_op;
5745 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5746 in_late_binary_op = true;
5747 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5748 ? expr_loc : location, type, orig_rhs);
5749 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5750 in_late_binary_op = save;
5751 return ret;
5752 }
5753
5754 /* Aggregates in different TUs might need conversion. */
5755 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5756 && codel == coder
5757 && comptypes (type, rhstype))
5758 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5759 ? expr_loc : location, type, rhs);
5760
5761 /* Conversion to a transparent union or record from its member types.
5762 This applies only to function arguments. */
5763 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5764 && TYPE_TRANSPARENT_AGGR (type))
5765 && errtype == ic_argpass)
5766 {
5767 tree memb, marginal_memb = NULL_TREE;
5768
5769 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5770 {
5771 tree memb_type = TREE_TYPE (memb);
5772
5773 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5774 TYPE_MAIN_VARIANT (rhstype)))
5775 break;
5776
5777 if (TREE_CODE (memb_type) != POINTER_TYPE)
5778 continue;
5779
5780 if (coder == POINTER_TYPE)
5781 {
5782 tree ttl = TREE_TYPE (memb_type);
5783 tree ttr = TREE_TYPE (rhstype);
5784
5785 /* Any non-function converts to a [const][volatile] void *
5786 and vice versa; otherwise, targets must be the same.
5787 Meanwhile, the lhs target must have all the qualifiers of
5788 the rhs. */
5789 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5790 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5791 || comp_target_types (location, memb_type, rhstype))
5792 {
5793 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5794 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5795 /* If this type won't generate any warnings, use it. */
5796 if (lquals == rquals
5797 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5798 && TREE_CODE (ttl) == FUNCTION_TYPE)
5799 ? ((lquals | rquals) == rquals)
5800 : ((lquals | rquals) == lquals)))
5801 break;
5802
5803 /* Keep looking for a better type, but remember this one. */
5804 if (!marginal_memb)
5805 marginal_memb = memb;
5806 }
5807 }
5808
5809 /* Can convert integer zero to any pointer type. */
5810 if (null_pointer_constant)
5811 {
5812 rhs = null_pointer_node;
5813 break;
5814 }
5815 }
5816
5817 if (memb || marginal_memb)
5818 {
5819 if (!memb)
5820 {
5821 /* We have only a marginally acceptable member type;
5822 it needs a warning. */
5823 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5824 tree ttr = TREE_TYPE (rhstype);
5825
5826 /* Const and volatile mean something different for function
5827 types, so the usual warnings are not appropriate. */
5828 if (TREE_CODE (ttr) == FUNCTION_TYPE
5829 && TREE_CODE (ttl) == FUNCTION_TYPE)
5830 {
5831 /* Because const and volatile on functions are
5832 restrictions that say the function will not do
5833 certain things, it is okay to use a const or volatile
5834 function where an ordinary one is wanted, but not
5835 vice-versa. */
5836 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5837 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5838 WARN_FOR_QUALIFIERS (location, 0,
5839 G_("passing argument %d of %qE "
5840 "makes %q#v qualified function "
5841 "pointer from unqualified"),
5842 G_("assignment makes %q#v qualified "
5843 "function pointer from "
5844 "unqualified"),
5845 G_("initialization makes %q#v qualified "
5846 "function pointer from "
5847 "unqualified"),
5848 G_("return makes %q#v qualified function "
5849 "pointer from unqualified"),
5850 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5851 }
5852 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5853 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5854 WARN_FOR_QUALIFIERS (location, 0,
5855 G_("passing argument %d of %qE discards "
5856 "%qv qualifier from pointer target type"),
5857 G_("assignment discards %qv qualifier "
5858 "from pointer target type"),
5859 G_("initialization discards %qv qualifier "
5860 "from pointer target type"),
5861 G_("return discards %qv qualifier from "
5862 "pointer target type"),
5863 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5864
5865 memb = marginal_memb;
5866 }
5867
5868 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5869 pedwarn (location, OPT_Wpedantic,
5870 "ISO C prohibits argument conversion to union type");
5871
5872 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5873 return build_constructor_single (type, memb, rhs);
5874 }
5875 }
5876
5877 /* Conversions among pointers */
5878 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5879 && (coder == codel))
5880 {
5881 tree ttl = TREE_TYPE (type);
5882 tree ttr = TREE_TYPE (rhstype);
5883 tree mvl = ttl;
5884 tree mvr = ttr;
5885 bool is_opaque_pointer;
5886 int target_cmp = 0; /* Cache comp_target_types () result. */
5887 addr_space_t asl;
5888 addr_space_t asr;
5889
5890 if (TREE_CODE (mvl) != ARRAY_TYPE)
5891 mvl = (TYPE_ATOMIC (mvl)
5892 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5893 TYPE_QUAL_ATOMIC)
5894 : TYPE_MAIN_VARIANT (mvl));
5895 if (TREE_CODE (mvr) != ARRAY_TYPE)
5896 mvr = (TYPE_ATOMIC (mvr)
5897 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5898 TYPE_QUAL_ATOMIC)
5899 : TYPE_MAIN_VARIANT (mvr));
5900 /* Opaque pointers are treated like void pointers. */
5901 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5902
5903 /* The Plan 9 compiler permits a pointer to a struct to be
5904 automatically converted into a pointer to an anonymous field
5905 within the struct. */
5906 if (flag_plan9_extensions
5907 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5908 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5909 && mvl != mvr)
5910 {
5911 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5912 if (new_rhs != NULL_TREE)
5913 {
5914 rhs = new_rhs;
5915 rhstype = TREE_TYPE (rhs);
5916 coder = TREE_CODE (rhstype);
5917 ttr = TREE_TYPE (rhstype);
5918 mvr = TYPE_MAIN_VARIANT (ttr);
5919 }
5920 }
5921
5922 /* C++ does not allow the implicit conversion void* -> T*. However,
5923 for the purpose of reducing the number of false positives, we
5924 tolerate the special case of
5925
5926 int *p = NULL;
5927
5928 where NULL is typically defined in C to be '(void *) 0'. */
5929 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5930 warning_at (location, OPT_Wc___compat,
5931 "request for implicit conversion "
5932 "from %qT to %qT not permitted in C++", rhstype, type);
5933
5934 /* See if the pointers point to incompatible address spaces. */
5935 asl = TYPE_ADDR_SPACE (ttl);
5936 asr = TYPE_ADDR_SPACE (ttr);
5937 if (!null_pointer_constant_p (rhs)
5938 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5939 {
5940 switch (errtype)
5941 {
5942 case ic_argpass:
5943 error_at (location, "passing argument %d of %qE from pointer to "
5944 "non-enclosed address space", parmnum, rname);
5945 break;
5946 case ic_assign:
5947 error_at (location, "assignment from pointer to "
5948 "non-enclosed address space");
5949 break;
5950 case ic_init:
5951 error_at (location, "initialization from pointer to "
5952 "non-enclosed address space");
5953 break;
5954 case ic_return:
5955 error_at (location, "return from pointer to "
5956 "non-enclosed address space");
5957 break;
5958 default:
5959 gcc_unreachable ();
5960 }
5961 return error_mark_node;
5962 }
5963
5964 /* Check if the right-hand side has a format attribute but the
5965 left-hand side doesn't. */
5966 if (warn_suggest_attribute_format
5967 && check_missing_format_attribute (type, rhstype))
5968 {
5969 switch (errtype)
5970 {
5971 case ic_argpass:
5972 warning_at (location, OPT_Wsuggest_attribute_format,
5973 "argument %d of %qE might be "
5974 "a candidate for a format attribute",
5975 parmnum, rname);
5976 break;
5977 case ic_assign:
5978 warning_at (location, OPT_Wsuggest_attribute_format,
5979 "assignment left-hand side might be "
5980 "a candidate for a format attribute");
5981 break;
5982 case ic_init:
5983 warning_at (location, OPT_Wsuggest_attribute_format,
5984 "initialization left-hand side might be "
5985 "a candidate for a format attribute");
5986 break;
5987 case ic_return:
5988 warning_at (location, OPT_Wsuggest_attribute_format,
5989 "return type might be "
5990 "a candidate for a format attribute");
5991 break;
5992 default:
5993 gcc_unreachable ();
5994 }
5995 }
5996
5997 /* Any non-function converts to a [const][volatile] void *
5998 and vice versa; otherwise, targets must be the same.
5999 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6000 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6001 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6002 || (target_cmp = comp_target_types (location, type, rhstype))
6003 || is_opaque_pointer
6004 || ((c_common_unsigned_type (mvl)
6005 == c_common_unsigned_type (mvr))
6006 && (c_common_signed_type (mvl)
6007 == c_common_signed_type (mvr))
6008 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6009 {
6010 if (pedantic
6011 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6012 ||
6013 (VOID_TYPE_P (ttr)
6014 && !null_pointer_constant
6015 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6016 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
6017 G_("ISO C forbids passing argument %d of "
6018 "%qE between function pointer "
6019 "and %<void *%>"),
6020 G_("ISO C forbids assignment between "
6021 "function pointer and %<void *%>"),
6022 G_("ISO C forbids initialization between "
6023 "function pointer and %<void *%>"),
6024 G_("ISO C forbids return between function "
6025 "pointer and %<void *%>"));
6026 /* Const and volatile mean something different for function types,
6027 so the usual warnings are not appropriate. */
6028 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6029 && TREE_CODE (ttl) != FUNCTION_TYPE)
6030 {
6031 /* Assignments between atomic and non-atomic objects are OK. */
6032 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6033 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6034 {
6035 WARN_FOR_QUALIFIERS (location, 0,
6036 G_("passing argument %d of %qE discards "
6037 "%qv qualifier from pointer target type"),
6038 G_("assignment discards %qv qualifier "
6039 "from pointer target type"),
6040 G_("initialization discards %qv qualifier "
6041 "from pointer target type"),
6042 G_("return discards %qv qualifier from "
6043 "pointer target type"),
6044 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6045 }
6046 /* If this is not a case of ignoring a mismatch in signedness,
6047 no warning. */
6048 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6049 || target_cmp)
6050 ;
6051 /* If there is a mismatch, do warn. */
6052 else if (warn_pointer_sign)
6053 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
6054 G_("pointer targets in passing argument "
6055 "%d of %qE differ in signedness"),
6056 G_("pointer targets in assignment "
6057 "differ in signedness"),
6058 G_("pointer targets in initialization "
6059 "differ in signedness"),
6060 G_("pointer targets in return differ "
6061 "in signedness"));
6062 }
6063 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6064 && TREE_CODE (ttr) == FUNCTION_TYPE)
6065 {
6066 /* Because const and volatile on functions are restrictions
6067 that say the function will not do certain things,
6068 it is okay to use a const or volatile function
6069 where an ordinary one is wanted, but not vice-versa. */
6070 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6071 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6072 WARN_FOR_QUALIFIERS (location, 0,
6073 G_("passing argument %d of %qE makes "
6074 "%q#v qualified function pointer "
6075 "from unqualified"),
6076 G_("assignment makes %q#v qualified function "
6077 "pointer from unqualified"),
6078 G_("initialization makes %q#v qualified "
6079 "function pointer from unqualified"),
6080 G_("return makes %q#v qualified function "
6081 "pointer from unqualified"),
6082 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6083 }
6084 }
6085 else
6086 /* Avoid warning about the volatile ObjC EH puts on decls. */
6087 if (!objc_ok)
6088 WARN_FOR_ASSIGNMENT (location, 0,
6089 G_("passing argument %d of %qE from "
6090 "incompatible pointer type"),
6091 G_("assignment from incompatible pointer type"),
6092 G_("initialization from incompatible "
6093 "pointer type"),
6094 G_("return from incompatible pointer type"));
6095
6096 return convert (type, rhs);
6097 }
6098 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6099 {
6100 /* ??? This should not be an error when inlining calls to
6101 unprototyped functions. */
6102 error_at (location, "invalid use of non-lvalue array");
6103 return error_mark_node;
6104 }
6105 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6106 {
6107 /* An explicit constant 0 can convert to a pointer,
6108 or one that results from arithmetic, even including
6109 a cast to integer type. */
6110 if (!null_pointer_constant)
6111 WARN_FOR_ASSIGNMENT (location, 0,
6112 G_("passing argument %d of %qE makes "
6113 "pointer from integer without a cast"),
6114 G_("assignment makes pointer from integer "
6115 "without a cast"),
6116 G_("initialization makes pointer from "
6117 "integer without a cast"),
6118 G_("return makes pointer from integer "
6119 "without a cast"));
6120
6121 return convert (type, rhs);
6122 }
6123 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6124 {
6125 WARN_FOR_ASSIGNMENT (location, 0,
6126 G_("passing argument %d of %qE makes integer "
6127 "from pointer without a cast"),
6128 G_("assignment makes integer from pointer "
6129 "without a cast"),
6130 G_("initialization makes integer from pointer "
6131 "without a cast"),
6132 G_("return makes integer from pointer "
6133 "without a cast"));
6134 return convert (type, rhs);
6135 }
6136 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6137 {
6138 tree ret;
6139 bool save = in_late_binary_op;
6140 in_late_binary_op = true;
6141 ret = convert (type, rhs);
6142 in_late_binary_op = save;
6143 return ret;
6144 }
6145
6146 switch (errtype)
6147 {
6148 case ic_argpass:
6149 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
6150 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6151 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
6152 "expected %qT but argument is of type %qT", type, rhstype);
6153 break;
6154 case ic_assign:
6155 error_at (location, "incompatible types when assigning to type %qT from "
6156 "type %qT", type, rhstype);
6157 break;
6158 case ic_init:
6159 error_at (location,
6160 "incompatible types when initializing type %qT using type %qT",
6161 type, rhstype);
6162 break;
6163 case ic_return:
6164 error_at (location,
6165 "incompatible types when returning type %qT but %qT was "
6166 "expected", rhstype, type);
6167 break;
6168 default:
6169 gcc_unreachable ();
6170 }
6171
6172 return error_mark_node;
6173 }
6174 \f
6175 /* If VALUE is a compound expr all of whose expressions are constant, then
6176 return its value. Otherwise, return error_mark_node.
6177
6178 This is for handling COMPOUND_EXPRs as initializer elements
6179 which is allowed with a warning when -pedantic is specified. */
6180
6181 static tree
6182 valid_compound_expr_initializer (tree value, tree endtype)
6183 {
6184 if (TREE_CODE (value) == COMPOUND_EXPR)
6185 {
6186 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6187 == error_mark_node)
6188 return error_mark_node;
6189 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6190 endtype);
6191 }
6192 else if (!initializer_constant_valid_p (value, endtype))
6193 return error_mark_node;
6194 else
6195 return value;
6196 }
6197 \f
6198 /* Perform appropriate conversions on the initial value of a variable,
6199 store it in the declaration DECL,
6200 and print any error messages that are appropriate.
6201 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6202 If the init is invalid, store an ERROR_MARK.
6203
6204 INIT_LOC is the location of the initial value. */
6205
6206 void
6207 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6208 {
6209 tree value, type;
6210 bool npc = false;
6211
6212 /* If variable's type was invalidly declared, just ignore it. */
6213
6214 type = TREE_TYPE (decl);
6215 if (TREE_CODE (type) == ERROR_MARK)
6216 return;
6217
6218 /* Digest the specified initializer into an expression. */
6219
6220 if (init)
6221 npc = null_pointer_constant_p (init);
6222 value = digest_init (init_loc, type, init, origtype, npc,
6223 true, TREE_STATIC (decl));
6224
6225 /* Store the expression if valid; else report error. */
6226
6227 if (!in_system_header_at (input_location)
6228 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6229 warning (OPT_Wtraditional, "traditional C rejects automatic "
6230 "aggregate initialization");
6231
6232 DECL_INITIAL (decl) = value;
6233
6234 /* ANSI wants warnings about out-of-range constant initializers. */
6235 STRIP_TYPE_NOPS (value);
6236 if (TREE_STATIC (decl))
6237 constant_expression_warning (value);
6238
6239 /* Check if we need to set array size from compound literal size. */
6240 if (TREE_CODE (type) == ARRAY_TYPE
6241 && TYPE_DOMAIN (type) == 0
6242 && value != error_mark_node)
6243 {
6244 tree inside_init = init;
6245
6246 STRIP_TYPE_NOPS (inside_init);
6247 inside_init = fold (inside_init);
6248
6249 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6250 {
6251 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6252
6253 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6254 {
6255 /* For int foo[] = (int [3]){1}; we need to set array size
6256 now since later on array initializer will be just the
6257 brace enclosed list of the compound literal. */
6258 tree etype = strip_array_types (TREE_TYPE (decl));
6259 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6260 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6261 layout_type (type);
6262 layout_decl (cldecl, 0);
6263 TREE_TYPE (decl)
6264 = c_build_qualified_type (type, TYPE_QUALS (etype));
6265 }
6266 }
6267 }
6268 }
6269 \f
6270 /* Methods for storing and printing names for error messages. */
6271
6272 /* Implement a spelling stack that allows components of a name to be pushed
6273 and popped. Each element on the stack is this structure. */
6274
6275 struct spelling
6276 {
6277 int kind;
6278 union
6279 {
6280 unsigned HOST_WIDE_INT i;
6281 const char *s;
6282 } u;
6283 };
6284
6285 #define SPELLING_STRING 1
6286 #define SPELLING_MEMBER 2
6287 #define SPELLING_BOUNDS 3
6288
6289 static struct spelling *spelling; /* Next stack element (unused). */
6290 static struct spelling *spelling_base; /* Spelling stack base. */
6291 static int spelling_size; /* Size of the spelling stack. */
6292
6293 /* Macros to save and restore the spelling stack around push_... functions.
6294 Alternative to SAVE_SPELLING_STACK. */
6295
6296 #define SPELLING_DEPTH() (spelling - spelling_base)
6297 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6298
6299 /* Push an element on the spelling stack with type KIND and assign VALUE
6300 to MEMBER. */
6301
6302 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6303 { \
6304 int depth = SPELLING_DEPTH (); \
6305 \
6306 if (depth >= spelling_size) \
6307 { \
6308 spelling_size += 10; \
6309 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6310 spelling_size); \
6311 RESTORE_SPELLING_DEPTH (depth); \
6312 } \
6313 \
6314 spelling->kind = (KIND); \
6315 spelling->MEMBER = (VALUE); \
6316 spelling++; \
6317 }
6318
6319 /* Push STRING on the stack. Printed literally. */
6320
6321 static void
6322 push_string (const char *string)
6323 {
6324 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6325 }
6326
6327 /* Push a member name on the stack. Printed as '.' STRING. */
6328
6329 static void
6330 push_member_name (tree decl)
6331 {
6332 const char *const string
6333 = (DECL_NAME (decl)
6334 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6335 : _("<anonymous>"));
6336 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6337 }
6338
6339 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6340
6341 static void
6342 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6343 {
6344 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6345 }
6346
6347 /* Compute the maximum size in bytes of the printed spelling. */
6348
6349 static int
6350 spelling_length (void)
6351 {
6352 int size = 0;
6353 struct spelling *p;
6354
6355 for (p = spelling_base; p < spelling; p++)
6356 {
6357 if (p->kind == SPELLING_BOUNDS)
6358 size += 25;
6359 else
6360 size += strlen (p->u.s) + 1;
6361 }
6362
6363 return size;
6364 }
6365
6366 /* Print the spelling to BUFFER and return it. */
6367
6368 static char *
6369 print_spelling (char *buffer)
6370 {
6371 char *d = buffer;
6372 struct spelling *p;
6373
6374 for (p = spelling_base; p < spelling; p++)
6375 if (p->kind == SPELLING_BOUNDS)
6376 {
6377 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6378 d += strlen (d);
6379 }
6380 else
6381 {
6382 const char *s;
6383 if (p->kind == SPELLING_MEMBER)
6384 *d++ = '.';
6385 for (s = p->u.s; (*d = *s++); d++)
6386 ;
6387 }
6388 *d++ = '\0';
6389 return buffer;
6390 }
6391
6392 /* Issue an error message for a bad initializer component.
6393 GMSGID identifies the message.
6394 The component name is taken from the spelling stack. */
6395
6396 void
6397 error_init (const char *gmsgid)
6398 {
6399 char *ofwhat;
6400
6401 /* The gmsgid may be a format string with %< and %>. */
6402 error (gmsgid);
6403 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6404 if (*ofwhat)
6405 error ("(near initialization for %qs)", ofwhat);
6406 }
6407
6408 /* Issue a pedantic warning for a bad initializer component. OPT is
6409 the option OPT_* (from options.h) controlling this warning or 0 if
6410 it is unconditionally given. GMSGID identifies the message. The
6411 component name is taken from the spelling stack. */
6412
6413 void
6414 pedwarn_init (location_t location, int opt, const char *gmsgid)
6415 {
6416 char *ofwhat;
6417
6418 /* The gmsgid may be a format string with %< and %>. */
6419 pedwarn (location, opt, gmsgid);
6420 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6421 if (*ofwhat)
6422 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6423 }
6424
6425 /* Issue a warning for a bad initializer component.
6426
6427 OPT is the OPT_W* value corresponding to the warning option that
6428 controls this warning. GMSGID identifies the message. The
6429 component name is taken from the spelling stack. */
6430
6431 static void
6432 warning_init (int opt, const char *gmsgid)
6433 {
6434 char *ofwhat;
6435
6436 /* The gmsgid may be a format string with %< and %>. */
6437 warning (opt, gmsgid);
6438 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6439 if (*ofwhat)
6440 warning (opt, "(near initialization for %qs)", ofwhat);
6441 }
6442 \f
6443 /* If TYPE is an array type and EXPR is a parenthesized string
6444 constant, warn if pedantic that EXPR is being used to initialize an
6445 object of type TYPE. */
6446
6447 void
6448 maybe_warn_string_init (tree type, struct c_expr expr)
6449 {
6450 if (pedantic
6451 && TREE_CODE (type) == ARRAY_TYPE
6452 && TREE_CODE (expr.value) == STRING_CST
6453 && expr.original_code != STRING_CST)
6454 pedwarn_init (input_location, OPT_Wpedantic,
6455 "array initialized from parenthesized string constant");
6456 }
6457
6458 /* Digest the parser output INIT as an initializer for type TYPE.
6459 Return a C expression of type TYPE to represent the initial value.
6460
6461 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6462
6463 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6464
6465 If INIT is a string constant, STRICT_STRING is true if it is
6466 unparenthesized or we should not warn here for it being parenthesized.
6467 For other types of INIT, STRICT_STRING is not used.
6468
6469 INIT_LOC is the location of the INIT.
6470
6471 REQUIRE_CONSTANT requests an error if non-constant initializers or
6472 elements are seen. */
6473
6474 static tree
6475 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6476 bool null_pointer_constant, bool strict_string,
6477 int require_constant)
6478 {
6479 enum tree_code code = TREE_CODE (type);
6480 tree inside_init = init;
6481 tree semantic_type = NULL_TREE;
6482 bool maybe_const = true;
6483
6484 if (type == error_mark_node
6485 || !init
6486 || init == error_mark_node
6487 || TREE_TYPE (init) == error_mark_node)
6488 return error_mark_node;
6489
6490 STRIP_TYPE_NOPS (inside_init);
6491
6492 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6493 {
6494 semantic_type = TREE_TYPE (inside_init);
6495 inside_init = TREE_OPERAND (inside_init, 0);
6496 }
6497 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6498 inside_init = decl_constant_value_for_optimization (inside_init);
6499
6500 /* Initialization of an array of chars from a string constant
6501 optionally enclosed in braces. */
6502
6503 if (code == ARRAY_TYPE && inside_init
6504 && TREE_CODE (inside_init) == STRING_CST)
6505 {
6506 tree typ1
6507 = (TYPE_ATOMIC (TREE_TYPE (type))
6508 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6509 TYPE_QUAL_ATOMIC)
6510 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6511 /* Note that an array could be both an array of character type
6512 and an array of wchar_t if wchar_t is signed char or unsigned
6513 char. */
6514 bool char_array = (typ1 == char_type_node
6515 || typ1 == signed_char_type_node
6516 || typ1 == unsigned_char_type_node);
6517 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6518 bool char16_array = !!comptypes (typ1, char16_type_node);
6519 bool char32_array = !!comptypes (typ1, char32_type_node);
6520
6521 if (char_array || wchar_array || char16_array || char32_array)
6522 {
6523 struct c_expr expr;
6524 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6525 expr.value = inside_init;
6526 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6527 expr.original_type = NULL;
6528 maybe_warn_string_init (type, expr);
6529
6530 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6531 pedwarn_init (init_loc, OPT_Wpedantic,
6532 "initialization of a flexible array member");
6533
6534 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6535 TYPE_MAIN_VARIANT (type)))
6536 return inside_init;
6537
6538 if (char_array)
6539 {
6540 if (typ2 != char_type_node)
6541 {
6542 error_init ("char-array initialized from wide string");
6543 return error_mark_node;
6544 }
6545 }
6546 else
6547 {
6548 if (typ2 == char_type_node)
6549 {
6550 error_init ("wide character array initialized from non-wide "
6551 "string");
6552 return error_mark_node;
6553 }
6554 else if (!comptypes(typ1, typ2))
6555 {
6556 error_init ("wide character array initialized from "
6557 "incompatible wide string");
6558 return error_mark_node;
6559 }
6560 }
6561
6562 TREE_TYPE (inside_init) = type;
6563 if (TYPE_DOMAIN (type) != 0
6564 && TYPE_SIZE (type) != 0
6565 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6566 {
6567 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6568
6569 /* Subtract the size of a single (possibly wide) character
6570 because it's ok to ignore the terminating null char
6571 that is counted in the length of the constant. */
6572 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6573 (len
6574 - (TYPE_PRECISION (typ1)
6575 / BITS_PER_UNIT))))
6576 pedwarn_init (init_loc, 0,
6577 ("initializer-string for array of chars "
6578 "is too long"));
6579 else if (warn_cxx_compat
6580 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6581 warning_at (init_loc, OPT_Wc___compat,
6582 ("initializer-string for array chars "
6583 "is too long for C++"));
6584 }
6585
6586 return inside_init;
6587 }
6588 else if (INTEGRAL_TYPE_P (typ1))
6589 {
6590 error_init ("array of inappropriate type initialized "
6591 "from string constant");
6592 return error_mark_node;
6593 }
6594 }
6595
6596 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6597 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6598 below and handle as a constructor. */
6599 if (code == VECTOR_TYPE
6600 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6601 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6602 && TREE_CONSTANT (inside_init))
6603 {
6604 if (TREE_CODE (inside_init) == VECTOR_CST
6605 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6606 TYPE_MAIN_VARIANT (type)))
6607 return inside_init;
6608
6609 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6610 {
6611 unsigned HOST_WIDE_INT ix;
6612 tree value;
6613 bool constant_p = true;
6614
6615 /* Iterate through elements and check if all constructor
6616 elements are *_CSTs. */
6617 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6618 if (!CONSTANT_CLASS_P (value))
6619 {
6620 constant_p = false;
6621 break;
6622 }
6623
6624 if (constant_p)
6625 return build_vector_from_ctor (type,
6626 CONSTRUCTOR_ELTS (inside_init));
6627 }
6628 }
6629
6630 if (warn_sequence_point)
6631 verify_sequence_points (inside_init);
6632
6633 /* Any type can be initialized
6634 from an expression of the same type, optionally with braces. */
6635
6636 if (inside_init && TREE_TYPE (inside_init) != 0
6637 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6638 TYPE_MAIN_VARIANT (type))
6639 || (code == ARRAY_TYPE
6640 && comptypes (TREE_TYPE (inside_init), type))
6641 || (code == VECTOR_TYPE
6642 && comptypes (TREE_TYPE (inside_init), type))
6643 || (code == POINTER_TYPE
6644 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6645 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6646 TREE_TYPE (type)))))
6647 {
6648 if (code == POINTER_TYPE)
6649 {
6650 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6651 {
6652 if (TREE_CODE (inside_init) == STRING_CST
6653 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6654 inside_init = array_to_pointer_conversion
6655 (init_loc, inside_init);
6656 else
6657 {
6658 error_init ("invalid use of non-lvalue array");
6659 return error_mark_node;
6660 }
6661 }
6662 }
6663
6664 if (code == VECTOR_TYPE)
6665 /* Although the types are compatible, we may require a
6666 conversion. */
6667 inside_init = convert (type, inside_init);
6668
6669 if (require_constant
6670 && (code == VECTOR_TYPE || !flag_isoc99)
6671 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6672 {
6673 /* As an extension, allow initializing objects with static storage
6674 duration with compound literals (which are then treated just as
6675 the brace enclosed list they contain). Also allow this for
6676 vectors, as we can only assign them with compound literals. */
6677 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6678 inside_init = DECL_INITIAL (decl);
6679 }
6680
6681 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6682 && TREE_CODE (inside_init) != CONSTRUCTOR)
6683 {
6684 error_init ("array initialized from non-constant array expression");
6685 return error_mark_node;
6686 }
6687
6688 /* Compound expressions can only occur here if -Wpedantic or
6689 -pedantic-errors is specified. In the later case, we always want
6690 an error. In the former case, we simply want a warning. */
6691 if (require_constant && pedantic
6692 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6693 {
6694 inside_init
6695 = valid_compound_expr_initializer (inside_init,
6696 TREE_TYPE (inside_init));
6697 if (inside_init == error_mark_node)
6698 error_init ("initializer element is not constant");
6699 else
6700 pedwarn_init (init_loc, OPT_Wpedantic,
6701 "initializer element is not constant");
6702 if (flag_pedantic_errors)
6703 inside_init = error_mark_node;
6704 }
6705 else if (require_constant
6706 && !initializer_constant_valid_p (inside_init,
6707 TREE_TYPE (inside_init)))
6708 {
6709 error_init ("initializer element is not constant");
6710 inside_init = error_mark_node;
6711 }
6712 else if (require_constant && !maybe_const)
6713 pedwarn_init (init_loc, 0,
6714 "initializer element is not a constant expression");
6715
6716 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6717 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6718 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6719 type, inside_init, origtype,
6720 ic_init, null_pointer_constant,
6721 NULL_TREE, NULL_TREE, 0);
6722 return inside_init;
6723 }
6724
6725 /* Handle scalar types, including conversions. */
6726
6727 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6728 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6729 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6730 {
6731 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6732 && (TREE_CODE (init) == STRING_CST
6733 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6734 inside_init = init = array_to_pointer_conversion (init_loc, init);
6735 if (semantic_type)
6736 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6737 inside_init);
6738 inside_init
6739 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6740 inside_init, origtype, ic_init,
6741 null_pointer_constant, NULL_TREE, NULL_TREE,
6742 0);
6743
6744 /* Check to see if we have already given an error message. */
6745 if (inside_init == error_mark_node)
6746 ;
6747 else if (require_constant && !TREE_CONSTANT (inside_init))
6748 {
6749 error_init ("initializer element is not constant");
6750 inside_init = error_mark_node;
6751 }
6752 else if (require_constant
6753 && !initializer_constant_valid_p (inside_init,
6754 TREE_TYPE (inside_init)))
6755 {
6756 error_init ("initializer element is not computable at load time");
6757 inside_init = error_mark_node;
6758 }
6759 else if (require_constant && !maybe_const)
6760 pedwarn_init (init_loc, 0,
6761 "initializer element is not a constant expression");
6762
6763 return inside_init;
6764 }
6765
6766 /* Come here only for records and arrays. */
6767
6768 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6769 {
6770 error_init ("variable-sized object may not be initialized");
6771 return error_mark_node;
6772 }
6773
6774 error_init ("invalid initializer");
6775 return error_mark_node;
6776 }
6777 \f
6778 /* Handle initializers that use braces. */
6779
6780 /* Type of object we are accumulating a constructor for.
6781 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6782 static tree constructor_type;
6783
6784 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6785 left to fill. */
6786 static tree constructor_fields;
6787
6788 /* For an ARRAY_TYPE, this is the specified index
6789 at which to store the next element we get. */
6790 static tree constructor_index;
6791
6792 /* For an ARRAY_TYPE, this is the maximum index. */
6793 static tree constructor_max_index;
6794
6795 /* For a RECORD_TYPE, this is the first field not yet written out. */
6796 static tree constructor_unfilled_fields;
6797
6798 /* For an ARRAY_TYPE, this is the index of the first element
6799 not yet written out. */
6800 static tree constructor_unfilled_index;
6801
6802 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6803 This is so we can generate gaps between fields, when appropriate. */
6804 static tree constructor_bit_index;
6805
6806 /* If we are saving up the elements rather than allocating them,
6807 this is the list of elements so far (in reverse order,
6808 most recent first). */
6809 static vec<constructor_elt, va_gc> *constructor_elements;
6810
6811 /* 1 if constructor should be incrementally stored into a constructor chain,
6812 0 if all the elements should be kept in AVL tree. */
6813 static int constructor_incremental;
6814
6815 /* 1 if so far this constructor's elements are all compile-time constants. */
6816 static int constructor_constant;
6817
6818 /* 1 if so far this constructor's elements are all valid address constants. */
6819 static int constructor_simple;
6820
6821 /* 1 if this constructor has an element that cannot be part of a
6822 constant expression. */
6823 static int constructor_nonconst;
6824
6825 /* 1 if this constructor is erroneous so far. */
6826 static int constructor_erroneous;
6827
6828 /* Structure for managing pending initializer elements, organized as an
6829 AVL tree. */
6830
6831 struct init_node
6832 {
6833 struct init_node *left, *right;
6834 struct init_node *parent;
6835 int balance;
6836 tree purpose;
6837 tree value;
6838 tree origtype;
6839 };
6840
6841 /* Tree of pending elements at this constructor level.
6842 These are elements encountered out of order
6843 which belong at places we haven't reached yet in actually
6844 writing the output.
6845 Will never hold tree nodes across GC runs. */
6846 static struct init_node *constructor_pending_elts;
6847
6848 /* The SPELLING_DEPTH of this constructor. */
6849 static int constructor_depth;
6850
6851 /* DECL node for which an initializer is being read.
6852 0 means we are reading a constructor expression
6853 such as (struct foo) {...}. */
6854 static tree constructor_decl;
6855
6856 /* Nonzero if this is an initializer for a top-level decl. */
6857 static int constructor_top_level;
6858
6859 /* Nonzero if there were any member designators in this initializer. */
6860 static int constructor_designated;
6861
6862 /* Nesting depth of designator list. */
6863 static int designator_depth;
6864
6865 /* Nonzero if there were diagnosed errors in this designator list. */
6866 static int designator_erroneous;
6867
6868 \f
6869 /* This stack has a level for each implicit or explicit level of
6870 structuring in the initializer, including the outermost one. It
6871 saves the values of most of the variables above. */
6872
6873 struct constructor_range_stack;
6874
6875 struct constructor_stack
6876 {
6877 struct constructor_stack *next;
6878 tree type;
6879 tree fields;
6880 tree index;
6881 tree max_index;
6882 tree unfilled_index;
6883 tree unfilled_fields;
6884 tree bit_index;
6885 vec<constructor_elt, va_gc> *elements;
6886 struct init_node *pending_elts;
6887 int offset;
6888 int depth;
6889 /* If value nonzero, this value should replace the entire
6890 constructor at this level. */
6891 struct c_expr replacement_value;
6892 struct constructor_range_stack *range_stack;
6893 char constant;
6894 char simple;
6895 char nonconst;
6896 char implicit;
6897 char erroneous;
6898 char outer;
6899 char incremental;
6900 char designated;
6901 };
6902
6903 static struct constructor_stack *constructor_stack;
6904
6905 /* This stack represents designators from some range designator up to
6906 the last designator in the list. */
6907
6908 struct constructor_range_stack
6909 {
6910 struct constructor_range_stack *next, *prev;
6911 struct constructor_stack *stack;
6912 tree range_start;
6913 tree index;
6914 tree range_end;
6915 tree fields;
6916 };
6917
6918 static struct constructor_range_stack *constructor_range_stack;
6919
6920 /* This stack records separate initializers that are nested.
6921 Nested initializers can't happen in ANSI C, but GNU C allows them
6922 in cases like { ... (struct foo) { ... } ... }. */
6923
6924 struct initializer_stack
6925 {
6926 struct initializer_stack *next;
6927 tree decl;
6928 struct constructor_stack *constructor_stack;
6929 struct constructor_range_stack *constructor_range_stack;
6930 vec<constructor_elt, va_gc> *elements;
6931 struct spelling *spelling;
6932 struct spelling *spelling_base;
6933 int spelling_size;
6934 char top_level;
6935 char require_constant_value;
6936 char require_constant_elements;
6937 };
6938
6939 static struct initializer_stack *initializer_stack;
6940 \f
6941 /* Prepare to parse and output the initializer for variable DECL. */
6942
6943 void
6944 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6945 {
6946 const char *locus;
6947 struct initializer_stack *p = XNEW (struct initializer_stack);
6948
6949 p->decl = constructor_decl;
6950 p->require_constant_value = require_constant_value;
6951 p->require_constant_elements = require_constant_elements;
6952 p->constructor_stack = constructor_stack;
6953 p->constructor_range_stack = constructor_range_stack;
6954 p->elements = constructor_elements;
6955 p->spelling = spelling;
6956 p->spelling_base = spelling_base;
6957 p->spelling_size = spelling_size;
6958 p->top_level = constructor_top_level;
6959 p->next = initializer_stack;
6960 initializer_stack = p;
6961
6962 constructor_decl = decl;
6963 constructor_designated = 0;
6964 constructor_top_level = top_level;
6965
6966 if (decl != 0 && decl != error_mark_node)
6967 {
6968 require_constant_value = TREE_STATIC (decl);
6969 require_constant_elements
6970 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6971 /* For a scalar, you can always use any value to initialize,
6972 even within braces. */
6973 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6974 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6975 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6976 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6977 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6978 }
6979 else
6980 {
6981 require_constant_value = 0;
6982 require_constant_elements = 0;
6983 locus = _("(anonymous)");
6984 }
6985
6986 constructor_stack = 0;
6987 constructor_range_stack = 0;
6988
6989 missing_braces_mentioned = 0;
6990
6991 spelling_base = 0;
6992 spelling_size = 0;
6993 RESTORE_SPELLING_DEPTH (0);
6994
6995 if (locus)
6996 push_string (locus);
6997 }
6998
6999 void
7000 finish_init (void)
7001 {
7002 struct initializer_stack *p = initializer_stack;
7003
7004 /* Free the whole constructor stack of this initializer. */
7005 while (constructor_stack)
7006 {
7007 struct constructor_stack *q = constructor_stack;
7008 constructor_stack = q->next;
7009 free (q);
7010 }
7011
7012 gcc_assert (!constructor_range_stack);
7013
7014 /* Pop back to the data of the outer initializer (if any). */
7015 free (spelling_base);
7016
7017 constructor_decl = p->decl;
7018 require_constant_value = p->require_constant_value;
7019 require_constant_elements = p->require_constant_elements;
7020 constructor_stack = p->constructor_stack;
7021 constructor_range_stack = p->constructor_range_stack;
7022 constructor_elements = p->elements;
7023 spelling = p->spelling;
7024 spelling_base = p->spelling_base;
7025 spelling_size = p->spelling_size;
7026 constructor_top_level = p->top_level;
7027 initializer_stack = p->next;
7028 free (p);
7029 }
7030 \f
7031 /* Call here when we see the initializer is surrounded by braces.
7032 This is instead of a call to push_init_level;
7033 it is matched by a call to pop_init_level.
7034
7035 TYPE is the type to initialize, for a constructor expression.
7036 For an initializer for a decl, TYPE is zero. */
7037
7038 void
7039 really_start_incremental_init (tree type)
7040 {
7041 struct constructor_stack *p = XNEW (struct constructor_stack);
7042
7043 if (type == 0)
7044 type = TREE_TYPE (constructor_decl);
7045
7046 if (TREE_CODE (type) == VECTOR_TYPE
7047 && TYPE_VECTOR_OPAQUE (type))
7048 error ("opaque vector types cannot be initialized");
7049
7050 p->type = constructor_type;
7051 p->fields = constructor_fields;
7052 p->index = constructor_index;
7053 p->max_index = constructor_max_index;
7054 p->unfilled_index = constructor_unfilled_index;
7055 p->unfilled_fields = constructor_unfilled_fields;
7056 p->bit_index = constructor_bit_index;
7057 p->elements = constructor_elements;
7058 p->constant = constructor_constant;
7059 p->simple = constructor_simple;
7060 p->nonconst = constructor_nonconst;
7061 p->erroneous = constructor_erroneous;
7062 p->pending_elts = constructor_pending_elts;
7063 p->depth = constructor_depth;
7064 p->replacement_value.value = 0;
7065 p->replacement_value.original_code = ERROR_MARK;
7066 p->replacement_value.original_type = NULL;
7067 p->implicit = 0;
7068 p->range_stack = 0;
7069 p->outer = 0;
7070 p->incremental = constructor_incremental;
7071 p->designated = constructor_designated;
7072 p->next = 0;
7073 constructor_stack = p;
7074
7075 constructor_constant = 1;
7076 constructor_simple = 1;
7077 constructor_nonconst = 0;
7078 constructor_depth = SPELLING_DEPTH ();
7079 constructor_elements = NULL;
7080 constructor_pending_elts = 0;
7081 constructor_type = type;
7082 constructor_incremental = 1;
7083 constructor_designated = 0;
7084 designator_depth = 0;
7085 designator_erroneous = 0;
7086
7087 if (TREE_CODE (constructor_type) == RECORD_TYPE
7088 || TREE_CODE (constructor_type) == UNION_TYPE)
7089 {
7090 constructor_fields = TYPE_FIELDS (constructor_type);
7091 /* Skip any nameless bit fields at the beginning. */
7092 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7093 && DECL_NAME (constructor_fields) == 0)
7094 constructor_fields = DECL_CHAIN (constructor_fields);
7095
7096 constructor_unfilled_fields = constructor_fields;
7097 constructor_bit_index = bitsize_zero_node;
7098 }
7099 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7100 {
7101 if (TYPE_DOMAIN (constructor_type))
7102 {
7103 constructor_max_index
7104 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7105
7106 /* Detect non-empty initializations of zero-length arrays. */
7107 if (constructor_max_index == NULL_TREE
7108 && TYPE_SIZE (constructor_type))
7109 constructor_max_index = integer_minus_one_node;
7110
7111 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7112 to initialize VLAs will cause a proper error; avoid tree
7113 checking errors as well by setting a safe value. */
7114 if (constructor_max_index
7115 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7116 constructor_max_index = integer_minus_one_node;
7117
7118 constructor_index
7119 = convert (bitsizetype,
7120 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7121 }
7122 else
7123 {
7124 constructor_index = bitsize_zero_node;
7125 constructor_max_index = NULL_TREE;
7126 }
7127
7128 constructor_unfilled_index = constructor_index;
7129 }
7130 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7131 {
7132 /* Vectors are like simple fixed-size arrays. */
7133 constructor_max_index =
7134 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7135 constructor_index = bitsize_zero_node;
7136 constructor_unfilled_index = constructor_index;
7137 }
7138 else
7139 {
7140 /* Handle the case of int x = {5}; */
7141 constructor_fields = constructor_type;
7142 constructor_unfilled_fields = constructor_type;
7143 }
7144 }
7145 \f
7146 /* Push down into a subobject, for initialization.
7147 If this is for an explicit set of braces, IMPLICIT is 0.
7148 If it is because the next element belongs at a lower level,
7149 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7150
7151 void
7152 push_init_level (int implicit, struct obstack * braced_init_obstack)
7153 {
7154 struct constructor_stack *p;
7155 tree value = NULL_TREE;
7156
7157 /* If we've exhausted any levels that didn't have braces,
7158 pop them now. If implicit == 1, this will have been done in
7159 process_init_element; do not repeat it here because in the case
7160 of excess initializers for an empty aggregate this leads to an
7161 infinite cycle of popping a level and immediately recreating
7162 it. */
7163 if (implicit != 1)
7164 {
7165 while (constructor_stack->implicit)
7166 {
7167 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7168 || TREE_CODE (constructor_type) == UNION_TYPE)
7169 && constructor_fields == 0)
7170 process_init_element (pop_init_level (1, braced_init_obstack),
7171 true, braced_init_obstack);
7172 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7173 && constructor_max_index
7174 && tree_int_cst_lt (constructor_max_index,
7175 constructor_index))
7176 process_init_element (pop_init_level (1, braced_init_obstack),
7177 true, braced_init_obstack);
7178 else
7179 break;
7180 }
7181 }
7182
7183 /* Unless this is an explicit brace, we need to preserve previous
7184 content if any. */
7185 if (implicit)
7186 {
7187 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7188 || TREE_CODE (constructor_type) == UNION_TYPE)
7189 && constructor_fields)
7190 value = find_init_member (constructor_fields, braced_init_obstack);
7191 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7192 value = find_init_member (constructor_index, braced_init_obstack);
7193 }
7194
7195 p = XNEW (struct constructor_stack);
7196 p->type = constructor_type;
7197 p->fields = constructor_fields;
7198 p->index = constructor_index;
7199 p->max_index = constructor_max_index;
7200 p->unfilled_index = constructor_unfilled_index;
7201 p->unfilled_fields = constructor_unfilled_fields;
7202 p->bit_index = constructor_bit_index;
7203 p->elements = constructor_elements;
7204 p->constant = constructor_constant;
7205 p->simple = constructor_simple;
7206 p->nonconst = constructor_nonconst;
7207 p->erroneous = constructor_erroneous;
7208 p->pending_elts = constructor_pending_elts;
7209 p->depth = constructor_depth;
7210 p->replacement_value.value = 0;
7211 p->replacement_value.original_code = ERROR_MARK;
7212 p->replacement_value.original_type = NULL;
7213 p->implicit = implicit;
7214 p->outer = 0;
7215 p->incremental = constructor_incremental;
7216 p->designated = constructor_designated;
7217 p->next = constructor_stack;
7218 p->range_stack = 0;
7219 constructor_stack = p;
7220
7221 constructor_constant = 1;
7222 constructor_simple = 1;
7223 constructor_nonconst = 0;
7224 constructor_depth = SPELLING_DEPTH ();
7225 constructor_elements = NULL;
7226 constructor_incremental = 1;
7227 constructor_designated = 0;
7228 constructor_pending_elts = 0;
7229 if (!implicit)
7230 {
7231 p->range_stack = constructor_range_stack;
7232 constructor_range_stack = 0;
7233 designator_depth = 0;
7234 designator_erroneous = 0;
7235 }
7236
7237 /* Don't die if an entire brace-pair level is superfluous
7238 in the containing level. */
7239 if (constructor_type == 0)
7240 ;
7241 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7242 || TREE_CODE (constructor_type) == UNION_TYPE)
7243 {
7244 /* Don't die if there are extra init elts at the end. */
7245 if (constructor_fields == 0)
7246 constructor_type = 0;
7247 else
7248 {
7249 constructor_type = TREE_TYPE (constructor_fields);
7250 push_member_name (constructor_fields);
7251 constructor_depth++;
7252 }
7253 }
7254 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7255 {
7256 constructor_type = TREE_TYPE (constructor_type);
7257 push_array_bounds (tree_to_uhwi (constructor_index));
7258 constructor_depth++;
7259 }
7260
7261 if (constructor_type == 0)
7262 {
7263 error_init ("extra brace group at end of initializer");
7264 constructor_fields = 0;
7265 constructor_unfilled_fields = 0;
7266 return;
7267 }
7268
7269 if (value && TREE_CODE (value) == CONSTRUCTOR)
7270 {
7271 constructor_constant = TREE_CONSTANT (value);
7272 constructor_simple = TREE_STATIC (value);
7273 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7274 constructor_elements = CONSTRUCTOR_ELTS (value);
7275 if (!vec_safe_is_empty (constructor_elements)
7276 && (TREE_CODE (constructor_type) == RECORD_TYPE
7277 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7278 set_nonincremental_init (braced_init_obstack);
7279 }
7280
7281 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
7282 {
7283 missing_braces_mentioned = 1;
7284 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
7285 }
7286
7287 if (TREE_CODE (constructor_type) == RECORD_TYPE
7288 || TREE_CODE (constructor_type) == UNION_TYPE)
7289 {
7290 constructor_fields = TYPE_FIELDS (constructor_type);
7291 /* Skip any nameless bit fields at the beginning. */
7292 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7293 && DECL_NAME (constructor_fields) == 0)
7294 constructor_fields = DECL_CHAIN (constructor_fields);
7295
7296 constructor_unfilled_fields = constructor_fields;
7297 constructor_bit_index = bitsize_zero_node;
7298 }
7299 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7300 {
7301 /* Vectors are like simple fixed-size arrays. */
7302 constructor_max_index =
7303 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7304 constructor_index = bitsize_int (0);
7305 constructor_unfilled_index = constructor_index;
7306 }
7307 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7308 {
7309 if (TYPE_DOMAIN (constructor_type))
7310 {
7311 constructor_max_index
7312 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7313
7314 /* Detect non-empty initializations of zero-length arrays. */
7315 if (constructor_max_index == NULL_TREE
7316 && TYPE_SIZE (constructor_type))
7317 constructor_max_index = integer_minus_one_node;
7318
7319 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7320 to initialize VLAs will cause a proper error; avoid tree
7321 checking errors as well by setting a safe value. */
7322 if (constructor_max_index
7323 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7324 constructor_max_index = integer_minus_one_node;
7325
7326 constructor_index
7327 = convert (bitsizetype,
7328 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7329 }
7330 else
7331 constructor_index = bitsize_zero_node;
7332
7333 constructor_unfilled_index = constructor_index;
7334 if (value && TREE_CODE (value) == STRING_CST)
7335 {
7336 /* We need to split the char/wchar array into individual
7337 characters, so that we don't have to special case it
7338 everywhere. */
7339 set_nonincremental_init_from_string (value, braced_init_obstack);
7340 }
7341 }
7342 else
7343 {
7344 if (constructor_type != error_mark_node)
7345 warning_init (0, "braces around scalar initializer");
7346 constructor_fields = constructor_type;
7347 constructor_unfilled_fields = constructor_type;
7348 }
7349 }
7350
7351 /* At the end of an implicit or explicit brace level,
7352 finish up that level of constructor. If a single expression
7353 with redundant braces initialized that level, return the
7354 c_expr structure for that expression. Otherwise, the original_code
7355 element is set to ERROR_MARK.
7356 If we were outputting the elements as they are read, return 0 as the value
7357 from inner levels (process_init_element ignores that),
7358 but return error_mark_node as the value from the outermost level
7359 (that's what we want to put in DECL_INITIAL).
7360 Otherwise, return a CONSTRUCTOR expression as the value. */
7361
7362 struct c_expr
7363 pop_init_level (int implicit, struct obstack * braced_init_obstack)
7364 {
7365 struct constructor_stack *p;
7366 struct c_expr ret;
7367 ret.value = 0;
7368 ret.original_code = ERROR_MARK;
7369 ret.original_type = NULL;
7370
7371 if (implicit == 0)
7372 {
7373 /* When we come to an explicit close brace,
7374 pop any inner levels that didn't have explicit braces. */
7375 while (constructor_stack->implicit)
7376 {
7377 process_init_element (pop_init_level (1, braced_init_obstack),
7378 true, braced_init_obstack);
7379 }
7380 gcc_assert (!constructor_range_stack);
7381 }
7382
7383 /* Now output all pending elements. */
7384 constructor_incremental = 1;
7385 output_pending_init_elements (1, braced_init_obstack);
7386
7387 p = constructor_stack;
7388
7389 /* Error for initializing a flexible array member, or a zero-length
7390 array member in an inappropriate context. */
7391 if (constructor_type && constructor_fields
7392 && TREE_CODE (constructor_type) == ARRAY_TYPE
7393 && TYPE_DOMAIN (constructor_type)
7394 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7395 {
7396 /* Silently discard empty initializations. The parser will
7397 already have pedwarned for empty brackets. */
7398 if (integer_zerop (constructor_unfilled_index))
7399 constructor_type = NULL_TREE;
7400 else
7401 {
7402 gcc_assert (!TYPE_SIZE (constructor_type));
7403
7404 if (constructor_depth > 2)
7405 error_init ("initialization of flexible array member in a nested context");
7406 else
7407 pedwarn_init (input_location, OPT_Wpedantic,
7408 "initialization of a flexible array member");
7409
7410 /* We have already issued an error message for the existence
7411 of a flexible array member not at the end of the structure.
7412 Discard the initializer so that we do not die later. */
7413 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7414 constructor_type = NULL_TREE;
7415 }
7416 }
7417
7418 /* Warn when some struct elements are implicitly initialized to zero. */
7419 if (warn_missing_field_initializers
7420 && constructor_type
7421 && TREE_CODE (constructor_type) == RECORD_TYPE
7422 && constructor_unfilled_fields)
7423 {
7424 bool constructor_zeroinit =
7425 (vec_safe_length (constructor_elements) == 1
7426 && integer_zerop ((*constructor_elements)[0].value));
7427
7428 /* Do not warn for flexible array members or zero-length arrays. */
7429 while (constructor_unfilled_fields
7430 && (!DECL_SIZE (constructor_unfilled_fields)
7431 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7432 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7433
7434 if (constructor_unfilled_fields
7435 /* Do not warn if this level of the initializer uses member
7436 designators; it is likely to be deliberate. */
7437 && !constructor_designated
7438 /* Do not warn about initializing with ` = {0}'. */
7439 && !constructor_zeroinit)
7440 {
7441 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7442 "missing initializer for field %qD of %qT",
7443 constructor_unfilled_fields,
7444 constructor_type))
7445 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7446 "%qD declared here", constructor_unfilled_fields);
7447 }
7448 }
7449
7450 /* Pad out the end of the structure. */
7451 if (p->replacement_value.value)
7452 /* If this closes a superfluous brace pair,
7453 just pass out the element between them. */
7454 ret = p->replacement_value;
7455 else if (constructor_type == 0)
7456 ;
7457 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7458 && TREE_CODE (constructor_type) != UNION_TYPE
7459 && TREE_CODE (constructor_type) != ARRAY_TYPE
7460 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7461 {
7462 /* A nonincremental scalar initializer--just return
7463 the element, after verifying there is just one. */
7464 if (vec_safe_is_empty (constructor_elements))
7465 {
7466 if (!constructor_erroneous)
7467 error_init ("empty scalar initializer");
7468 ret.value = error_mark_node;
7469 }
7470 else if (vec_safe_length (constructor_elements) != 1)
7471 {
7472 error_init ("extra elements in scalar initializer");
7473 ret.value = (*constructor_elements)[0].value;
7474 }
7475 else
7476 ret.value = (*constructor_elements)[0].value;
7477 }
7478 else
7479 {
7480 if (constructor_erroneous)
7481 ret.value = error_mark_node;
7482 else
7483 {
7484 ret.value = build_constructor (constructor_type,
7485 constructor_elements);
7486 if (constructor_constant)
7487 TREE_CONSTANT (ret.value) = 1;
7488 if (constructor_constant && constructor_simple)
7489 TREE_STATIC (ret.value) = 1;
7490 if (constructor_nonconst)
7491 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7492 }
7493 }
7494
7495 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7496 {
7497 if (constructor_nonconst)
7498 ret.original_code = C_MAYBE_CONST_EXPR;
7499 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7500 ret.original_code = ERROR_MARK;
7501 }
7502
7503 constructor_type = p->type;
7504 constructor_fields = p->fields;
7505 constructor_index = p->index;
7506 constructor_max_index = p->max_index;
7507 constructor_unfilled_index = p->unfilled_index;
7508 constructor_unfilled_fields = p->unfilled_fields;
7509 constructor_bit_index = p->bit_index;
7510 constructor_elements = p->elements;
7511 constructor_constant = p->constant;
7512 constructor_simple = p->simple;
7513 constructor_nonconst = p->nonconst;
7514 constructor_erroneous = p->erroneous;
7515 constructor_incremental = p->incremental;
7516 constructor_designated = p->designated;
7517 constructor_pending_elts = p->pending_elts;
7518 constructor_depth = p->depth;
7519 if (!p->implicit)
7520 constructor_range_stack = p->range_stack;
7521 RESTORE_SPELLING_DEPTH (constructor_depth);
7522
7523 constructor_stack = p->next;
7524 free (p);
7525
7526 if (ret.value == 0 && constructor_stack == 0)
7527 ret.value = error_mark_node;
7528 return ret;
7529 }
7530
7531 /* Common handling for both array range and field name designators.
7532 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7533
7534 static int
7535 set_designator (int array, struct obstack * braced_init_obstack)
7536 {
7537 tree subtype;
7538 enum tree_code subcode;
7539
7540 /* Don't die if an entire brace-pair level is superfluous
7541 in the containing level. */
7542 if (constructor_type == 0)
7543 return 1;
7544
7545 /* If there were errors in this designator list already, bail out
7546 silently. */
7547 if (designator_erroneous)
7548 return 1;
7549
7550 if (!designator_depth)
7551 {
7552 gcc_assert (!constructor_range_stack);
7553
7554 /* Designator list starts at the level of closest explicit
7555 braces. */
7556 while (constructor_stack->implicit)
7557 {
7558 process_init_element (pop_init_level (1, braced_init_obstack),
7559 true, braced_init_obstack);
7560 }
7561 constructor_designated = 1;
7562 return 0;
7563 }
7564
7565 switch (TREE_CODE (constructor_type))
7566 {
7567 case RECORD_TYPE:
7568 case UNION_TYPE:
7569 subtype = TREE_TYPE (constructor_fields);
7570 if (subtype != error_mark_node)
7571 subtype = TYPE_MAIN_VARIANT (subtype);
7572 break;
7573 case ARRAY_TYPE:
7574 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7575 break;
7576 default:
7577 gcc_unreachable ();
7578 }
7579
7580 subcode = TREE_CODE (subtype);
7581 if (array && subcode != ARRAY_TYPE)
7582 {
7583 error_init ("array index in non-array initializer");
7584 return 1;
7585 }
7586 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7587 {
7588 error_init ("field name not in record or union initializer");
7589 return 1;
7590 }
7591
7592 constructor_designated = 1;
7593 push_init_level (2, braced_init_obstack);
7594 return 0;
7595 }
7596
7597 /* If there are range designators in designator list, push a new designator
7598 to constructor_range_stack. RANGE_END is end of such stack range or
7599 NULL_TREE if there is no range designator at this level. */
7600
7601 static void
7602 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7603 {
7604 struct constructor_range_stack *p;
7605
7606 p = (struct constructor_range_stack *)
7607 obstack_alloc (braced_init_obstack,
7608 sizeof (struct constructor_range_stack));
7609 p->prev = constructor_range_stack;
7610 p->next = 0;
7611 p->fields = constructor_fields;
7612 p->range_start = constructor_index;
7613 p->index = constructor_index;
7614 p->stack = constructor_stack;
7615 p->range_end = range_end;
7616 if (constructor_range_stack)
7617 constructor_range_stack->next = p;
7618 constructor_range_stack = p;
7619 }
7620
7621 /* Within an array initializer, specify the next index to be initialized.
7622 FIRST is that index. If LAST is nonzero, then initialize a range
7623 of indices, running from FIRST through LAST. */
7624
7625 void
7626 set_init_index (tree first, tree last,
7627 struct obstack * braced_init_obstack)
7628 {
7629 if (set_designator (1, braced_init_obstack))
7630 return;
7631
7632 designator_erroneous = 1;
7633
7634 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7635 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7636 {
7637 error_init ("array index in initializer not of integer type");
7638 return;
7639 }
7640
7641 if (TREE_CODE (first) != INTEGER_CST)
7642 {
7643 first = c_fully_fold (first, false, NULL);
7644 if (TREE_CODE (first) == INTEGER_CST)
7645 pedwarn_init (input_location, OPT_Wpedantic,
7646 "array index in initializer is not "
7647 "an integer constant expression");
7648 }
7649
7650 if (last && TREE_CODE (last) != INTEGER_CST)
7651 {
7652 last = c_fully_fold (last, false, NULL);
7653 if (TREE_CODE (last) == INTEGER_CST)
7654 pedwarn_init (input_location, OPT_Wpedantic,
7655 "array index in initializer is not "
7656 "an integer constant expression");
7657 }
7658
7659 if (TREE_CODE (first) != INTEGER_CST)
7660 error_init ("nonconstant array index in initializer");
7661 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7662 error_init ("nonconstant array index in initializer");
7663 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7664 error_init ("array index in non-array initializer");
7665 else if (tree_int_cst_sgn (first) == -1)
7666 error_init ("array index in initializer exceeds array bounds");
7667 else if (constructor_max_index
7668 && tree_int_cst_lt (constructor_max_index, first))
7669 error_init ("array index in initializer exceeds array bounds");
7670 else
7671 {
7672 constant_expression_warning (first);
7673 if (last)
7674 constant_expression_warning (last);
7675 constructor_index = convert (bitsizetype, first);
7676 if (tree_int_cst_lt (constructor_index, first))
7677 {
7678 constructor_index = copy_node (constructor_index);
7679 TREE_OVERFLOW (constructor_index) = 1;
7680 }
7681
7682 if (last)
7683 {
7684 if (tree_int_cst_equal (first, last))
7685 last = 0;
7686 else if (tree_int_cst_lt (last, first))
7687 {
7688 error_init ("empty index range in initializer");
7689 last = 0;
7690 }
7691 else
7692 {
7693 last = convert (bitsizetype, last);
7694 if (constructor_max_index != 0
7695 && tree_int_cst_lt (constructor_max_index, last))
7696 {
7697 error_init ("array index range in initializer exceeds array bounds");
7698 last = 0;
7699 }
7700 }
7701 }
7702
7703 designator_depth++;
7704 designator_erroneous = 0;
7705 if (constructor_range_stack || last)
7706 push_range_stack (last, braced_init_obstack);
7707 }
7708 }
7709
7710 /* Within a struct initializer, specify the next field to be initialized. */
7711
7712 void
7713 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7714 {
7715 tree field;
7716
7717 if (set_designator (0, braced_init_obstack))
7718 return;
7719
7720 designator_erroneous = 1;
7721
7722 if (TREE_CODE (constructor_type) != RECORD_TYPE
7723 && TREE_CODE (constructor_type) != UNION_TYPE)
7724 {
7725 error_init ("field name not in record or union initializer");
7726 return;
7727 }
7728
7729 field = lookup_field (constructor_type, fieldname);
7730
7731 if (field == 0)
7732 error ("unknown field %qE specified in initializer", fieldname);
7733 else
7734 do
7735 {
7736 constructor_fields = TREE_VALUE (field);
7737 designator_depth++;
7738 designator_erroneous = 0;
7739 if (constructor_range_stack)
7740 push_range_stack (NULL_TREE, braced_init_obstack);
7741 field = TREE_CHAIN (field);
7742 if (field)
7743 {
7744 if (set_designator (0, braced_init_obstack))
7745 return;
7746 }
7747 }
7748 while (field != NULL_TREE);
7749 }
7750 \f
7751 /* Add a new initializer to the tree of pending initializers. PURPOSE
7752 identifies the initializer, either array index or field in a structure.
7753 VALUE is the value of that index or field. If ORIGTYPE is not
7754 NULL_TREE, it is the original type of VALUE.
7755
7756 IMPLICIT is true if value comes from pop_init_level (1),
7757 the new initializer has been merged with the existing one
7758 and thus no warnings should be emitted about overriding an
7759 existing initializer. */
7760
7761 static void
7762 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7763 struct obstack * braced_init_obstack)
7764 {
7765 struct init_node *p, **q, *r;
7766
7767 q = &constructor_pending_elts;
7768 p = 0;
7769
7770 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7771 {
7772 while (*q != 0)
7773 {
7774 p = *q;
7775 if (tree_int_cst_lt (purpose, p->purpose))
7776 q = &p->left;
7777 else if (tree_int_cst_lt (p->purpose, purpose))
7778 q = &p->right;
7779 else
7780 {
7781 if (!implicit)
7782 {
7783 if (TREE_SIDE_EFFECTS (p->value))
7784 warning_init (0, "initialized field with side-effects overwritten");
7785 else if (warn_override_init)
7786 warning_init (OPT_Woverride_init, "initialized field overwritten");
7787 }
7788 p->value = value;
7789 p->origtype = origtype;
7790 return;
7791 }
7792 }
7793 }
7794 else
7795 {
7796 tree bitpos;
7797
7798 bitpos = bit_position (purpose);
7799 while (*q != NULL)
7800 {
7801 p = *q;
7802 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7803 q = &p->left;
7804 else if (p->purpose != purpose)
7805 q = &p->right;
7806 else
7807 {
7808 if (!implicit)
7809 {
7810 if (TREE_SIDE_EFFECTS (p->value))
7811 warning_init (0, "initialized field with side-effects overwritten");
7812 else if (warn_override_init)
7813 warning_init (OPT_Woverride_init, "initialized field overwritten");
7814 }
7815 p->value = value;
7816 p->origtype = origtype;
7817 return;
7818 }
7819 }
7820 }
7821
7822 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7823 sizeof (struct init_node));
7824 r->purpose = purpose;
7825 r->value = value;
7826 r->origtype = origtype;
7827
7828 *q = r;
7829 r->parent = p;
7830 r->left = 0;
7831 r->right = 0;
7832 r->balance = 0;
7833
7834 while (p)
7835 {
7836 struct init_node *s;
7837
7838 if (r == p->left)
7839 {
7840 if (p->balance == 0)
7841 p->balance = -1;
7842 else if (p->balance < 0)
7843 {
7844 if (r->balance < 0)
7845 {
7846 /* L rotation. */
7847 p->left = r->right;
7848 if (p->left)
7849 p->left->parent = p;
7850 r->right = p;
7851
7852 p->balance = 0;
7853 r->balance = 0;
7854
7855 s = p->parent;
7856 p->parent = r;
7857 r->parent = s;
7858 if (s)
7859 {
7860 if (s->left == p)
7861 s->left = r;
7862 else
7863 s->right = r;
7864 }
7865 else
7866 constructor_pending_elts = r;
7867 }
7868 else
7869 {
7870 /* LR rotation. */
7871 struct init_node *t = r->right;
7872
7873 r->right = t->left;
7874 if (r->right)
7875 r->right->parent = r;
7876 t->left = r;
7877
7878 p->left = t->right;
7879 if (p->left)
7880 p->left->parent = p;
7881 t->right = p;
7882
7883 p->balance = t->balance < 0;
7884 r->balance = -(t->balance > 0);
7885 t->balance = 0;
7886
7887 s = p->parent;
7888 p->parent = t;
7889 r->parent = t;
7890 t->parent = s;
7891 if (s)
7892 {
7893 if (s->left == p)
7894 s->left = t;
7895 else
7896 s->right = t;
7897 }
7898 else
7899 constructor_pending_elts = t;
7900 }
7901 break;
7902 }
7903 else
7904 {
7905 /* p->balance == +1; growth of left side balances the node. */
7906 p->balance = 0;
7907 break;
7908 }
7909 }
7910 else /* r == p->right */
7911 {
7912 if (p->balance == 0)
7913 /* Growth propagation from right side. */
7914 p->balance++;
7915 else if (p->balance > 0)
7916 {
7917 if (r->balance > 0)
7918 {
7919 /* R rotation. */
7920 p->right = r->left;
7921 if (p->right)
7922 p->right->parent = p;
7923 r->left = p;
7924
7925 p->balance = 0;
7926 r->balance = 0;
7927
7928 s = p->parent;
7929 p->parent = r;
7930 r->parent = s;
7931 if (s)
7932 {
7933 if (s->left == p)
7934 s->left = r;
7935 else
7936 s->right = r;
7937 }
7938 else
7939 constructor_pending_elts = r;
7940 }
7941 else /* r->balance == -1 */
7942 {
7943 /* RL rotation */
7944 struct init_node *t = r->left;
7945
7946 r->left = t->right;
7947 if (r->left)
7948 r->left->parent = r;
7949 t->right = r;
7950
7951 p->right = t->left;
7952 if (p->right)
7953 p->right->parent = p;
7954 t->left = p;
7955
7956 r->balance = (t->balance < 0);
7957 p->balance = -(t->balance > 0);
7958 t->balance = 0;
7959
7960 s = p->parent;
7961 p->parent = t;
7962 r->parent = t;
7963 t->parent = s;
7964 if (s)
7965 {
7966 if (s->left == p)
7967 s->left = t;
7968 else
7969 s->right = t;
7970 }
7971 else
7972 constructor_pending_elts = t;
7973 }
7974 break;
7975 }
7976 else
7977 {
7978 /* p->balance == -1; growth of right side balances the node. */
7979 p->balance = 0;
7980 break;
7981 }
7982 }
7983
7984 r = p;
7985 p = p->parent;
7986 }
7987 }
7988
7989 /* Build AVL tree from a sorted chain. */
7990
7991 static void
7992 set_nonincremental_init (struct obstack * braced_init_obstack)
7993 {
7994 unsigned HOST_WIDE_INT ix;
7995 tree index, value;
7996
7997 if (TREE_CODE (constructor_type) != RECORD_TYPE
7998 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7999 return;
8000
8001 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8002 {
8003 add_pending_init (index, value, NULL_TREE, true,
8004 braced_init_obstack);
8005 }
8006 constructor_elements = NULL;
8007 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8008 {
8009 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8010 /* Skip any nameless bit fields at the beginning. */
8011 while (constructor_unfilled_fields != 0
8012 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8013 && DECL_NAME (constructor_unfilled_fields) == 0)
8014 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8015
8016 }
8017 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8018 {
8019 if (TYPE_DOMAIN (constructor_type))
8020 constructor_unfilled_index
8021 = convert (bitsizetype,
8022 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8023 else
8024 constructor_unfilled_index = bitsize_zero_node;
8025 }
8026 constructor_incremental = 0;
8027 }
8028
8029 /* Build AVL tree from a string constant. */
8030
8031 static void
8032 set_nonincremental_init_from_string (tree str,
8033 struct obstack * braced_init_obstack)
8034 {
8035 tree value, purpose, type;
8036 HOST_WIDE_INT val[2];
8037 const char *p, *end;
8038 int byte, wchar_bytes, charwidth, bitpos;
8039
8040 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8041
8042 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8043 charwidth = TYPE_PRECISION (char_type_node);
8044 type = TREE_TYPE (constructor_type);
8045 p = TREE_STRING_POINTER (str);
8046 end = p + TREE_STRING_LENGTH (str);
8047
8048 for (purpose = bitsize_zero_node;
8049 p < end
8050 && !(constructor_max_index
8051 && tree_int_cst_lt (constructor_max_index, purpose));
8052 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8053 {
8054 if (wchar_bytes == 1)
8055 {
8056 val[1] = (unsigned char) *p++;
8057 val[0] = 0;
8058 }
8059 else
8060 {
8061 val[0] = 0;
8062 val[1] = 0;
8063 for (byte = 0; byte < wchar_bytes; byte++)
8064 {
8065 if (BYTES_BIG_ENDIAN)
8066 bitpos = (wchar_bytes - byte - 1) * charwidth;
8067 else
8068 bitpos = byte * charwidth;
8069 val[bitpos < HOST_BITS_PER_WIDE_INT]
8070 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8071 << (bitpos % HOST_BITS_PER_WIDE_INT);
8072 }
8073 }
8074
8075 if (!TYPE_UNSIGNED (type))
8076 {
8077 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8078 if (bitpos < HOST_BITS_PER_WIDE_INT)
8079 {
8080 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8081 {
8082 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
8083 val[0] = -1;
8084 }
8085 }
8086 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8087 {
8088 if (val[1] < 0)
8089 val[0] = -1;
8090 }
8091 else if (val[0] & (((HOST_WIDE_INT) 1)
8092 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8093 val[0] |= ((HOST_WIDE_INT) -1)
8094 << (bitpos - HOST_BITS_PER_WIDE_INT);
8095 }
8096
8097 value = build_int_cst_wide (type, val[1], val[0]);
8098 add_pending_init (purpose, value, NULL_TREE, true,
8099 braced_init_obstack);
8100 }
8101
8102 constructor_incremental = 0;
8103 }
8104
8105 /* Return value of FIELD in pending initializer or zero if the field was
8106 not initialized yet. */
8107
8108 static tree
8109 find_init_member (tree field, struct obstack * braced_init_obstack)
8110 {
8111 struct init_node *p;
8112
8113 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8114 {
8115 if (constructor_incremental
8116 && tree_int_cst_lt (field, constructor_unfilled_index))
8117 set_nonincremental_init (braced_init_obstack);
8118
8119 p = constructor_pending_elts;
8120 while (p)
8121 {
8122 if (tree_int_cst_lt (field, p->purpose))
8123 p = p->left;
8124 else if (tree_int_cst_lt (p->purpose, field))
8125 p = p->right;
8126 else
8127 return p->value;
8128 }
8129 }
8130 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8131 {
8132 tree bitpos = bit_position (field);
8133
8134 if (constructor_incremental
8135 && (!constructor_unfilled_fields
8136 || tree_int_cst_lt (bitpos,
8137 bit_position (constructor_unfilled_fields))))
8138 set_nonincremental_init (braced_init_obstack);
8139
8140 p = constructor_pending_elts;
8141 while (p)
8142 {
8143 if (field == p->purpose)
8144 return p->value;
8145 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8146 p = p->left;
8147 else
8148 p = p->right;
8149 }
8150 }
8151 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8152 {
8153 if (!vec_safe_is_empty (constructor_elements)
8154 && (constructor_elements->last ().index == field))
8155 return constructor_elements->last ().value;
8156 }
8157 return 0;
8158 }
8159
8160 /* "Output" the next constructor element.
8161 At top level, really output it to assembler code now.
8162 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8163 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8164 TYPE is the data type that the containing data type wants here.
8165 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8166 If VALUE is a string constant, STRICT_STRING is true if it is
8167 unparenthesized or we should not warn here for it being parenthesized.
8168 For other types of VALUE, STRICT_STRING is not used.
8169
8170 PENDING if non-nil means output pending elements that belong
8171 right after this element. (PENDING is normally 1;
8172 it is 0 while outputting pending elements, to avoid recursion.)
8173
8174 IMPLICIT is true if value comes from pop_init_level (1),
8175 the new initializer has been merged with the existing one
8176 and thus no warnings should be emitted about overriding an
8177 existing initializer. */
8178
8179 static void
8180 output_init_element (tree value, tree origtype, bool strict_string, tree type,
8181 tree field, int pending, bool implicit,
8182 struct obstack * braced_init_obstack)
8183 {
8184 tree semantic_type = NULL_TREE;
8185 bool maybe_const = true;
8186 bool npc;
8187
8188 if (type == error_mark_node || value == error_mark_node)
8189 {
8190 constructor_erroneous = 1;
8191 return;
8192 }
8193 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8194 && (TREE_CODE (value) == STRING_CST
8195 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8196 && !(TREE_CODE (value) == STRING_CST
8197 && TREE_CODE (type) == ARRAY_TYPE
8198 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8199 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8200 TYPE_MAIN_VARIANT (type)))
8201 value = array_to_pointer_conversion (input_location, value);
8202
8203 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8204 && require_constant_value && !flag_isoc99 && pending)
8205 {
8206 /* As an extension, allow initializing objects with static storage
8207 duration with compound literals (which are then treated just as
8208 the brace enclosed list they contain). */
8209 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8210 value = DECL_INITIAL (decl);
8211 }
8212
8213 npc = null_pointer_constant_p (value);
8214 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8215 {
8216 semantic_type = TREE_TYPE (value);
8217 value = TREE_OPERAND (value, 0);
8218 }
8219 value = c_fully_fold (value, require_constant_value, &maybe_const);
8220
8221 if (value == error_mark_node)
8222 constructor_erroneous = 1;
8223 else if (!TREE_CONSTANT (value))
8224 constructor_constant = 0;
8225 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8226 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8227 || TREE_CODE (constructor_type) == UNION_TYPE)
8228 && DECL_C_BIT_FIELD (field)
8229 && TREE_CODE (value) != INTEGER_CST))
8230 constructor_simple = 0;
8231 if (!maybe_const)
8232 constructor_nonconst = 1;
8233
8234 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8235 {
8236 if (require_constant_value)
8237 {
8238 error_init ("initializer element is not constant");
8239 value = error_mark_node;
8240 }
8241 else if (require_constant_elements)
8242 pedwarn (input_location, 0,
8243 "initializer element is not computable at load time");
8244 }
8245 else if (!maybe_const
8246 && (require_constant_value || require_constant_elements))
8247 pedwarn_init (input_location, 0,
8248 "initializer element is not a constant expression");
8249
8250 /* Issue -Wc++-compat warnings about initializing a bitfield with
8251 enum type. */
8252 if (warn_cxx_compat
8253 && field != NULL_TREE
8254 && TREE_CODE (field) == FIELD_DECL
8255 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8256 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8257 != TYPE_MAIN_VARIANT (type))
8258 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8259 {
8260 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8261 if (checktype != error_mark_node
8262 && (TYPE_MAIN_VARIANT (checktype)
8263 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8264 warning_init (OPT_Wc___compat,
8265 "enum conversion in initialization is invalid in C++");
8266 }
8267
8268 /* If this field is empty (and not at the end of structure),
8269 don't do anything other than checking the initializer. */
8270 if (field
8271 && (TREE_TYPE (field) == error_mark_node
8272 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8273 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8274 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8275 || DECL_CHAIN (field)))))
8276 return;
8277
8278 if (semantic_type)
8279 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8280 value = digest_init (input_location, type, value, origtype, npc,
8281 strict_string, require_constant_value);
8282 if (value == error_mark_node)
8283 {
8284 constructor_erroneous = 1;
8285 return;
8286 }
8287 if (require_constant_value || require_constant_elements)
8288 constant_expression_warning (value);
8289
8290 /* If this element doesn't come next in sequence,
8291 put it on constructor_pending_elts. */
8292 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8293 && (!constructor_incremental
8294 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8295 {
8296 if (constructor_incremental
8297 && tree_int_cst_lt (field, constructor_unfilled_index))
8298 set_nonincremental_init (braced_init_obstack);
8299
8300 add_pending_init (field, value, origtype, implicit,
8301 braced_init_obstack);
8302 return;
8303 }
8304 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8305 && (!constructor_incremental
8306 || field != constructor_unfilled_fields))
8307 {
8308 /* We do this for records but not for unions. In a union,
8309 no matter which field is specified, it can be initialized
8310 right away since it starts at the beginning of the union. */
8311 if (constructor_incremental)
8312 {
8313 if (!constructor_unfilled_fields)
8314 set_nonincremental_init (braced_init_obstack);
8315 else
8316 {
8317 tree bitpos, unfillpos;
8318
8319 bitpos = bit_position (field);
8320 unfillpos = bit_position (constructor_unfilled_fields);
8321
8322 if (tree_int_cst_lt (bitpos, unfillpos))
8323 set_nonincremental_init (braced_init_obstack);
8324 }
8325 }
8326
8327 add_pending_init (field, value, origtype, implicit,
8328 braced_init_obstack);
8329 return;
8330 }
8331 else if (TREE_CODE (constructor_type) == UNION_TYPE
8332 && !vec_safe_is_empty (constructor_elements))
8333 {
8334 if (!implicit)
8335 {
8336 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8337 warning_init (0,
8338 "initialized field with side-effects overwritten");
8339 else if (warn_override_init)
8340 warning_init (OPT_Woverride_init, "initialized field overwritten");
8341 }
8342
8343 /* We can have just one union field set. */
8344 constructor_elements = NULL;
8345 }
8346
8347 /* Otherwise, output this element either to
8348 constructor_elements or to the assembler file. */
8349
8350 constructor_elt celt = {field, value};
8351 vec_safe_push (constructor_elements, celt);
8352
8353 /* Advance the variable that indicates sequential elements output. */
8354 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8355 constructor_unfilled_index
8356 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8357 bitsize_one_node);
8358 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8359 {
8360 constructor_unfilled_fields
8361 = DECL_CHAIN (constructor_unfilled_fields);
8362
8363 /* Skip any nameless bit fields. */
8364 while (constructor_unfilled_fields != 0
8365 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8366 && DECL_NAME (constructor_unfilled_fields) == 0)
8367 constructor_unfilled_fields =
8368 DECL_CHAIN (constructor_unfilled_fields);
8369 }
8370 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8371 constructor_unfilled_fields = 0;
8372
8373 /* Now output any pending elements which have become next. */
8374 if (pending)
8375 output_pending_init_elements (0, braced_init_obstack);
8376 }
8377
8378 /* Output any pending elements which have become next.
8379 As we output elements, constructor_unfilled_{fields,index}
8380 advances, which may cause other elements to become next;
8381 if so, they too are output.
8382
8383 If ALL is 0, we return when there are
8384 no more pending elements to output now.
8385
8386 If ALL is 1, we output space as necessary so that
8387 we can output all the pending elements. */
8388 static void
8389 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8390 {
8391 struct init_node *elt = constructor_pending_elts;
8392 tree next;
8393
8394 retry:
8395
8396 /* Look through the whole pending tree.
8397 If we find an element that should be output now,
8398 output it. Otherwise, set NEXT to the element
8399 that comes first among those still pending. */
8400
8401 next = 0;
8402 while (elt)
8403 {
8404 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8405 {
8406 if (tree_int_cst_equal (elt->purpose,
8407 constructor_unfilled_index))
8408 output_init_element (elt->value, elt->origtype, true,
8409 TREE_TYPE (constructor_type),
8410 constructor_unfilled_index, 0, false,
8411 braced_init_obstack);
8412 else if (tree_int_cst_lt (constructor_unfilled_index,
8413 elt->purpose))
8414 {
8415 /* Advance to the next smaller node. */
8416 if (elt->left)
8417 elt = elt->left;
8418 else
8419 {
8420 /* We have reached the smallest node bigger than the
8421 current unfilled index. Fill the space first. */
8422 next = elt->purpose;
8423 break;
8424 }
8425 }
8426 else
8427 {
8428 /* Advance to the next bigger node. */
8429 if (elt->right)
8430 elt = elt->right;
8431 else
8432 {
8433 /* We have reached the biggest node in a subtree. Find
8434 the parent of it, which is the next bigger node. */
8435 while (elt->parent && elt->parent->right == elt)
8436 elt = elt->parent;
8437 elt = elt->parent;
8438 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8439 elt->purpose))
8440 {
8441 next = elt->purpose;
8442 break;
8443 }
8444 }
8445 }
8446 }
8447 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8448 || TREE_CODE (constructor_type) == UNION_TYPE)
8449 {
8450 tree ctor_unfilled_bitpos, elt_bitpos;
8451
8452 /* If the current record is complete we are done. */
8453 if (constructor_unfilled_fields == 0)
8454 break;
8455
8456 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8457 elt_bitpos = bit_position (elt->purpose);
8458 /* We can't compare fields here because there might be empty
8459 fields in between. */
8460 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8461 {
8462 constructor_unfilled_fields = elt->purpose;
8463 output_init_element (elt->value, elt->origtype, true,
8464 TREE_TYPE (elt->purpose),
8465 elt->purpose, 0, false,
8466 braced_init_obstack);
8467 }
8468 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8469 {
8470 /* Advance to the next smaller node. */
8471 if (elt->left)
8472 elt = elt->left;
8473 else
8474 {
8475 /* We have reached the smallest node bigger than the
8476 current unfilled field. Fill the space first. */
8477 next = elt->purpose;
8478 break;
8479 }
8480 }
8481 else
8482 {
8483 /* Advance to the next bigger node. */
8484 if (elt->right)
8485 elt = elt->right;
8486 else
8487 {
8488 /* We have reached the biggest node in a subtree. Find
8489 the parent of it, which is the next bigger node. */
8490 while (elt->parent && elt->parent->right == elt)
8491 elt = elt->parent;
8492 elt = elt->parent;
8493 if (elt
8494 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8495 bit_position (elt->purpose))))
8496 {
8497 next = elt->purpose;
8498 break;
8499 }
8500 }
8501 }
8502 }
8503 }
8504
8505 /* Ordinarily return, but not if we want to output all
8506 and there are elements left. */
8507 if (!(all && next != 0))
8508 return;
8509
8510 /* If it's not incremental, just skip over the gap, so that after
8511 jumping to retry we will output the next successive element. */
8512 if (TREE_CODE (constructor_type) == RECORD_TYPE
8513 || TREE_CODE (constructor_type) == UNION_TYPE)
8514 constructor_unfilled_fields = next;
8515 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8516 constructor_unfilled_index = next;
8517
8518 /* ELT now points to the node in the pending tree with the next
8519 initializer to output. */
8520 goto retry;
8521 }
8522 \f
8523 /* Add one non-braced element to the current constructor level.
8524 This adjusts the current position within the constructor's type.
8525 This may also start or terminate implicit levels
8526 to handle a partly-braced initializer.
8527
8528 Once this has found the correct level for the new element,
8529 it calls output_init_element.
8530
8531 IMPLICIT is true if value comes from pop_init_level (1),
8532 the new initializer has been merged with the existing one
8533 and thus no warnings should be emitted about overriding an
8534 existing initializer. */
8535
8536 void
8537 process_init_element (struct c_expr value, bool implicit,
8538 struct obstack * braced_init_obstack)
8539 {
8540 tree orig_value = value.value;
8541 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8542 bool strict_string = value.original_code == STRING_CST;
8543 bool was_designated = designator_depth != 0;
8544
8545 designator_depth = 0;
8546 designator_erroneous = 0;
8547
8548 /* Handle superfluous braces around string cst as in
8549 char x[] = {"foo"}; */
8550 if (string_flag
8551 && constructor_type
8552 && !was_designated
8553 && TREE_CODE (constructor_type) == ARRAY_TYPE
8554 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8555 && integer_zerop (constructor_unfilled_index))
8556 {
8557 if (constructor_stack->replacement_value.value)
8558 error_init ("excess elements in char array initializer");
8559 constructor_stack->replacement_value = value;
8560 return;
8561 }
8562
8563 if (constructor_stack->replacement_value.value != 0)
8564 {
8565 error_init ("excess elements in struct initializer");
8566 return;
8567 }
8568
8569 /* Ignore elements of a brace group if it is entirely superfluous
8570 and has already been diagnosed. */
8571 if (constructor_type == 0)
8572 return;
8573
8574 /* If we've exhausted any levels that didn't have braces,
8575 pop them now. */
8576 while (constructor_stack->implicit)
8577 {
8578 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8579 || TREE_CODE (constructor_type) == UNION_TYPE)
8580 && constructor_fields == 0)
8581 process_init_element (pop_init_level (1, braced_init_obstack),
8582 true, braced_init_obstack);
8583 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8584 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8585 && constructor_max_index
8586 && tree_int_cst_lt (constructor_max_index,
8587 constructor_index))
8588 process_init_element (pop_init_level (1, braced_init_obstack),
8589 true, braced_init_obstack);
8590 else
8591 break;
8592 }
8593
8594 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8595 if (constructor_range_stack)
8596 {
8597 /* If value is a compound literal and we'll be just using its
8598 content, don't put it into a SAVE_EXPR. */
8599 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8600 || !require_constant_value
8601 || flag_isoc99)
8602 {
8603 tree semantic_type = NULL_TREE;
8604 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8605 {
8606 semantic_type = TREE_TYPE (value.value);
8607 value.value = TREE_OPERAND (value.value, 0);
8608 }
8609 value.value = c_save_expr (value.value);
8610 if (semantic_type)
8611 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8612 value.value);
8613 }
8614 }
8615
8616 while (1)
8617 {
8618 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8619 {
8620 tree fieldtype;
8621 enum tree_code fieldcode;
8622
8623 if (constructor_fields == 0)
8624 {
8625 pedwarn_init (input_location, 0,
8626 "excess elements in struct initializer");
8627 break;
8628 }
8629
8630 fieldtype = TREE_TYPE (constructor_fields);
8631 if (fieldtype != error_mark_node)
8632 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8633 fieldcode = TREE_CODE (fieldtype);
8634
8635 /* Error for non-static initialization of a flexible array member. */
8636 if (fieldcode == ARRAY_TYPE
8637 && !require_constant_value
8638 && TYPE_SIZE (fieldtype) == NULL_TREE
8639 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8640 {
8641 error_init ("non-static initialization of a flexible array member");
8642 break;
8643 }
8644
8645 /* Accept a string constant to initialize a subarray. */
8646 if (value.value != 0
8647 && fieldcode == ARRAY_TYPE
8648 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8649 && string_flag)
8650 value.value = orig_value;
8651 /* Otherwise, if we have come to a subaggregate,
8652 and we don't have an element of its type, push into it. */
8653 else if (value.value != 0
8654 && value.value != error_mark_node
8655 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8656 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8657 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8658 {
8659 push_init_level (1, braced_init_obstack);
8660 continue;
8661 }
8662
8663 if (value.value)
8664 {
8665 push_member_name (constructor_fields);
8666 output_init_element (value.value, value.original_type,
8667 strict_string, fieldtype,
8668 constructor_fields, 1, implicit,
8669 braced_init_obstack);
8670 RESTORE_SPELLING_DEPTH (constructor_depth);
8671 }
8672 else
8673 /* Do the bookkeeping for an element that was
8674 directly output as a constructor. */
8675 {
8676 /* For a record, keep track of end position of last field. */
8677 if (DECL_SIZE (constructor_fields))
8678 constructor_bit_index
8679 = size_binop_loc (input_location, PLUS_EXPR,
8680 bit_position (constructor_fields),
8681 DECL_SIZE (constructor_fields));
8682
8683 /* If the current field was the first one not yet written out,
8684 it isn't now, so update. */
8685 if (constructor_unfilled_fields == constructor_fields)
8686 {
8687 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8688 /* Skip any nameless bit fields. */
8689 while (constructor_unfilled_fields != 0
8690 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8691 && DECL_NAME (constructor_unfilled_fields) == 0)
8692 constructor_unfilled_fields =
8693 DECL_CHAIN (constructor_unfilled_fields);
8694 }
8695 }
8696
8697 constructor_fields = DECL_CHAIN (constructor_fields);
8698 /* Skip any nameless bit fields at the beginning. */
8699 while (constructor_fields != 0
8700 && DECL_C_BIT_FIELD (constructor_fields)
8701 && DECL_NAME (constructor_fields) == 0)
8702 constructor_fields = DECL_CHAIN (constructor_fields);
8703 }
8704 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8705 {
8706 tree fieldtype;
8707 enum tree_code fieldcode;
8708
8709 if (constructor_fields == 0)
8710 {
8711 pedwarn_init (input_location, 0,
8712 "excess elements in union initializer");
8713 break;
8714 }
8715
8716 fieldtype = TREE_TYPE (constructor_fields);
8717 if (fieldtype != error_mark_node)
8718 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8719 fieldcode = TREE_CODE (fieldtype);
8720
8721 /* Warn that traditional C rejects initialization of unions.
8722 We skip the warning if the value is zero. This is done
8723 under the assumption that the zero initializer in user
8724 code appears conditioned on e.g. __STDC__ to avoid
8725 "missing initializer" warnings and relies on default
8726 initialization to zero in the traditional C case.
8727 We also skip the warning if the initializer is designated,
8728 again on the assumption that this must be conditional on
8729 __STDC__ anyway (and we've already complained about the
8730 member-designator already). */
8731 if (!in_system_header_at (input_location) && !constructor_designated
8732 && !(value.value && (integer_zerop (value.value)
8733 || real_zerop (value.value))))
8734 warning (OPT_Wtraditional, "traditional C rejects initialization "
8735 "of unions");
8736
8737 /* Accept a string constant to initialize a subarray. */
8738 if (value.value != 0
8739 && fieldcode == ARRAY_TYPE
8740 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8741 && string_flag)
8742 value.value = orig_value;
8743 /* Otherwise, if we have come to a subaggregate,
8744 and we don't have an element of its type, push into it. */
8745 else if (value.value != 0
8746 && value.value != error_mark_node
8747 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8748 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8749 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8750 {
8751 push_init_level (1, braced_init_obstack);
8752 continue;
8753 }
8754
8755 if (value.value)
8756 {
8757 push_member_name (constructor_fields);
8758 output_init_element (value.value, value.original_type,
8759 strict_string, fieldtype,
8760 constructor_fields, 1, implicit,
8761 braced_init_obstack);
8762 RESTORE_SPELLING_DEPTH (constructor_depth);
8763 }
8764 else
8765 /* Do the bookkeeping for an element that was
8766 directly output as a constructor. */
8767 {
8768 constructor_bit_index = DECL_SIZE (constructor_fields);
8769 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8770 }
8771
8772 constructor_fields = 0;
8773 }
8774 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8775 {
8776 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8777 enum tree_code eltcode = TREE_CODE (elttype);
8778
8779 /* Accept a string constant to initialize a subarray. */
8780 if (value.value != 0
8781 && eltcode == ARRAY_TYPE
8782 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8783 && string_flag)
8784 value.value = orig_value;
8785 /* Otherwise, if we have come to a subaggregate,
8786 and we don't have an element of its type, push into it. */
8787 else if (value.value != 0
8788 && value.value != error_mark_node
8789 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8790 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8791 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8792 {
8793 push_init_level (1, braced_init_obstack);
8794 continue;
8795 }
8796
8797 if (constructor_max_index != 0
8798 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8799 || integer_all_onesp (constructor_max_index)))
8800 {
8801 pedwarn_init (input_location, 0,
8802 "excess elements in array initializer");
8803 break;
8804 }
8805
8806 /* Now output the actual element. */
8807 if (value.value)
8808 {
8809 push_array_bounds (tree_to_uhwi (constructor_index));
8810 output_init_element (value.value, value.original_type,
8811 strict_string, elttype,
8812 constructor_index, 1, implicit,
8813 braced_init_obstack);
8814 RESTORE_SPELLING_DEPTH (constructor_depth);
8815 }
8816
8817 constructor_index
8818 = size_binop_loc (input_location, PLUS_EXPR,
8819 constructor_index, bitsize_one_node);
8820
8821 if (!value.value)
8822 /* If we are doing the bookkeeping for an element that was
8823 directly output as a constructor, we must update
8824 constructor_unfilled_index. */
8825 constructor_unfilled_index = constructor_index;
8826 }
8827 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8828 {
8829 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8830
8831 /* Do a basic check of initializer size. Note that vectors
8832 always have a fixed size derived from their type. */
8833 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8834 {
8835 pedwarn_init (input_location, 0,
8836 "excess elements in vector initializer");
8837 break;
8838 }
8839
8840 /* Now output the actual element. */
8841 if (value.value)
8842 {
8843 if (TREE_CODE (value.value) == VECTOR_CST)
8844 elttype = TYPE_MAIN_VARIANT (constructor_type);
8845 output_init_element (value.value, value.original_type,
8846 strict_string, elttype,
8847 constructor_index, 1, implicit,
8848 braced_init_obstack);
8849 }
8850
8851 constructor_index
8852 = size_binop_loc (input_location,
8853 PLUS_EXPR, constructor_index, bitsize_one_node);
8854
8855 if (!value.value)
8856 /* If we are doing the bookkeeping for an element that was
8857 directly output as a constructor, we must update
8858 constructor_unfilled_index. */
8859 constructor_unfilled_index = constructor_index;
8860 }
8861
8862 /* Handle the sole element allowed in a braced initializer
8863 for a scalar variable. */
8864 else if (constructor_type != error_mark_node
8865 && constructor_fields == 0)
8866 {
8867 pedwarn_init (input_location, 0,
8868 "excess elements in scalar initializer");
8869 break;
8870 }
8871 else
8872 {
8873 if (value.value)
8874 output_init_element (value.value, value.original_type,
8875 strict_string, constructor_type,
8876 NULL_TREE, 1, implicit,
8877 braced_init_obstack);
8878 constructor_fields = 0;
8879 }
8880
8881 /* Handle range initializers either at this level or anywhere higher
8882 in the designator stack. */
8883 if (constructor_range_stack)
8884 {
8885 struct constructor_range_stack *p, *range_stack;
8886 int finish = 0;
8887
8888 range_stack = constructor_range_stack;
8889 constructor_range_stack = 0;
8890 while (constructor_stack != range_stack->stack)
8891 {
8892 gcc_assert (constructor_stack->implicit);
8893 process_init_element (pop_init_level (1,
8894 braced_init_obstack),
8895 true, braced_init_obstack);
8896 }
8897 for (p = range_stack;
8898 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8899 p = p->prev)
8900 {
8901 gcc_assert (constructor_stack->implicit);
8902 process_init_element (pop_init_level (1, braced_init_obstack),
8903 true, braced_init_obstack);
8904 }
8905
8906 p->index = size_binop_loc (input_location,
8907 PLUS_EXPR, p->index, bitsize_one_node);
8908 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8909 finish = 1;
8910
8911 while (1)
8912 {
8913 constructor_index = p->index;
8914 constructor_fields = p->fields;
8915 if (finish && p->range_end && p->index == p->range_start)
8916 {
8917 finish = 0;
8918 p->prev = 0;
8919 }
8920 p = p->next;
8921 if (!p)
8922 break;
8923 push_init_level (2, braced_init_obstack);
8924 p->stack = constructor_stack;
8925 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8926 p->index = p->range_start;
8927 }
8928
8929 if (!finish)
8930 constructor_range_stack = range_stack;
8931 continue;
8932 }
8933
8934 break;
8935 }
8936
8937 constructor_range_stack = 0;
8938 }
8939 \f
8940 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8941 (guaranteed to be 'volatile' or null) and ARGS (represented using
8942 an ASM_EXPR node). */
8943 tree
8944 build_asm_stmt (tree cv_qualifier, tree args)
8945 {
8946 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8947 ASM_VOLATILE_P (args) = 1;
8948 return add_stmt (args);
8949 }
8950
8951 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8952 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8953 SIMPLE indicates whether there was anything at all after the
8954 string in the asm expression -- asm("blah") and asm("blah" : )
8955 are subtly different. We use a ASM_EXPR node to represent this. */
8956 tree
8957 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8958 tree clobbers, tree labels, bool simple)
8959 {
8960 tree tail;
8961 tree args;
8962 int i;
8963 const char *constraint;
8964 const char **oconstraints;
8965 bool allows_mem, allows_reg, is_inout;
8966 int ninputs, noutputs;
8967
8968 ninputs = list_length (inputs);
8969 noutputs = list_length (outputs);
8970 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8971
8972 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8973
8974 /* Remove output conversions that change the type but not the mode. */
8975 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8976 {
8977 tree output = TREE_VALUE (tail);
8978
8979 output = c_fully_fold (output, false, NULL);
8980
8981 /* ??? Really, this should not be here. Users should be using a
8982 proper lvalue, dammit. But there's a long history of using casts
8983 in the output operands. In cases like longlong.h, this becomes a
8984 primitive form of typechecking -- if the cast can be removed, then
8985 the output operand had a type of the proper width; otherwise we'll
8986 get an error. Gross, but ... */
8987 STRIP_NOPS (output);
8988
8989 if (!lvalue_or_else (loc, output, lv_asm))
8990 output = error_mark_node;
8991
8992 if (output != error_mark_node
8993 && (TREE_READONLY (output)
8994 || TYPE_READONLY (TREE_TYPE (output))
8995 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8996 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8997 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8998 readonly_error (loc, output, lv_asm);
8999
9000 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9001 oconstraints[i] = constraint;
9002
9003 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9004 &allows_mem, &allows_reg, &is_inout))
9005 {
9006 /* If the operand is going to end up in memory,
9007 mark it addressable. */
9008 if (!allows_reg && !c_mark_addressable (output))
9009 output = error_mark_node;
9010 if (!(!allows_reg && allows_mem)
9011 && output != error_mark_node
9012 && VOID_TYPE_P (TREE_TYPE (output)))
9013 {
9014 error_at (loc, "invalid use of void expression");
9015 output = error_mark_node;
9016 }
9017 }
9018 else
9019 output = error_mark_node;
9020
9021 TREE_VALUE (tail) = output;
9022 }
9023
9024 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9025 {
9026 tree input;
9027
9028 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9029 input = TREE_VALUE (tail);
9030
9031 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9032 oconstraints, &allows_mem, &allows_reg))
9033 {
9034 /* If the operand is going to end up in memory,
9035 mark it addressable. */
9036 if (!allows_reg && allows_mem)
9037 {
9038 input = c_fully_fold (input, false, NULL);
9039
9040 /* Strip the nops as we allow this case. FIXME, this really
9041 should be rejected or made deprecated. */
9042 STRIP_NOPS (input);
9043 if (!c_mark_addressable (input))
9044 input = error_mark_node;
9045 }
9046 else
9047 {
9048 struct c_expr expr;
9049 memset (&expr, 0, sizeof (expr));
9050 expr.value = input;
9051 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9052 input = c_fully_fold (expr.value, false, NULL);
9053
9054 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9055 {
9056 error_at (loc, "invalid use of void expression");
9057 input = error_mark_node;
9058 }
9059 }
9060 }
9061 else
9062 input = error_mark_node;
9063
9064 TREE_VALUE (tail) = input;
9065 }
9066
9067 /* ASMs with labels cannot have outputs. This should have been
9068 enforced by the parser. */
9069 gcc_assert (outputs == NULL || labels == NULL);
9070
9071 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9072
9073 /* asm statements without outputs, including simple ones, are treated
9074 as volatile. */
9075 ASM_INPUT_P (args) = simple;
9076 ASM_VOLATILE_P (args) = (noutputs == 0);
9077
9078 return args;
9079 }
9080 \f
9081 /* Generate a goto statement to LABEL. LOC is the location of the
9082 GOTO. */
9083
9084 tree
9085 c_finish_goto_label (location_t loc, tree label)
9086 {
9087 tree decl = lookup_label_for_goto (loc, label);
9088 if (!decl)
9089 return NULL_TREE;
9090 TREE_USED (decl) = 1;
9091 {
9092 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9093 SET_EXPR_LOCATION (t, loc);
9094 return add_stmt (t);
9095 }
9096 }
9097
9098 /* Generate a computed goto statement to EXPR. LOC is the location of
9099 the GOTO. */
9100
9101 tree
9102 c_finish_goto_ptr (location_t loc, tree expr)
9103 {
9104 tree t;
9105 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9106 expr = c_fully_fold (expr, false, NULL);
9107 expr = convert (ptr_type_node, expr);
9108 t = build1 (GOTO_EXPR, void_type_node, expr);
9109 SET_EXPR_LOCATION (t, loc);
9110 return add_stmt (t);
9111 }
9112
9113 /* Generate a C `return' statement. RETVAL is the expression for what
9114 to return, or a null pointer for `return;' with no value. LOC is
9115 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9116 is the original type of RETVAL. */
9117
9118 tree
9119 c_finish_return (location_t loc, tree retval, tree origtype)
9120 {
9121 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9122 bool no_warning = false;
9123 bool npc = false;
9124 size_t rank = 0;
9125
9126 if (TREE_THIS_VOLATILE (current_function_decl))
9127 warning_at (loc, 0,
9128 "function declared %<noreturn%> has a %<return%> statement");
9129
9130 if (flag_cilkplus && contains_array_notation_expr (retval))
9131 {
9132 /* Array notations are allowed in a return statement if it is inside a
9133 built-in array notation reduction function. */
9134 if (!find_rank (loc, retval, retval, false, &rank))
9135 return error_mark_node;
9136 if (rank >= 1)
9137 {
9138 error_at (loc, "array notation expression cannot be used as a "
9139 "return value");
9140 return error_mark_node;
9141 }
9142 }
9143 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9144 {
9145 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9146 "allowed");
9147 return error_mark_node;
9148 }
9149 if (retval)
9150 {
9151 tree semantic_type = NULL_TREE;
9152 npc = null_pointer_constant_p (retval);
9153 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9154 {
9155 semantic_type = TREE_TYPE (retval);
9156 retval = TREE_OPERAND (retval, 0);
9157 }
9158 retval = c_fully_fold (retval, false, NULL);
9159 if (semantic_type)
9160 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9161 }
9162
9163 if (!retval)
9164 {
9165 current_function_returns_null = 1;
9166 if ((warn_return_type || flag_isoc99)
9167 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9168 {
9169 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
9170 "%<return%> with no value, in "
9171 "function returning non-void");
9172 no_warning = true;
9173 }
9174 }
9175 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9176 {
9177 current_function_returns_null = 1;
9178 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9179 pedwarn (loc, 0,
9180 "%<return%> with a value, in function returning void");
9181 else
9182 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9183 "%<return%> with expression, in function returning void");
9184 }
9185 else
9186 {
9187 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9188 retval, origtype, ic_return,
9189 npc, NULL_TREE, NULL_TREE, 0);
9190 tree res = DECL_RESULT (current_function_decl);
9191 tree inner;
9192 bool save;
9193
9194 current_function_returns_value = 1;
9195 if (t == error_mark_node)
9196 return NULL_TREE;
9197
9198 save = in_late_binary_op;
9199 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9200 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9201 in_late_binary_op = true;
9202 inner = t = convert (TREE_TYPE (res), t);
9203 in_late_binary_op = save;
9204
9205 /* Strip any conversions, additions, and subtractions, and see if
9206 we are returning the address of a local variable. Warn if so. */
9207 while (1)
9208 {
9209 switch (TREE_CODE (inner))
9210 {
9211 CASE_CONVERT:
9212 case NON_LVALUE_EXPR:
9213 case PLUS_EXPR:
9214 case POINTER_PLUS_EXPR:
9215 inner = TREE_OPERAND (inner, 0);
9216 continue;
9217
9218 case MINUS_EXPR:
9219 /* If the second operand of the MINUS_EXPR has a pointer
9220 type (or is converted from it), this may be valid, so
9221 don't give a warning. */
9222 {
9223 tree op1 = TREE_OPERAND (inner, 1);
9224
9225 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9226 && (CONVERT_EXPR_P (op1)
9227 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9228 op1 = TREE_OPERAND (op1, 0);
9229
9230 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9231 break;
9232
9233 inner = TREE_OPERAND (inner, 0);
9234 continue;
9235 }
9236
9237 case ADDR_EXPR:
9238 inner = TREE_OPERAND (inner, 0);
9239
9240 while (REFERENCE_CLASS_P (inner)
9241 && TREE_CODE (inner) != INDIRECT_REF)
9242 inner = TREE_OPERAND (inner, 0);
9243
9244 if (DECL_P (inner)
9245 && !DECL_EXTERNAL (inner)
9246 && !TREE_STATIC (inner)
9247 && DECL_CONTEXT (inner) == current_function_decl)
9248 warning_at (loc,
9249 OPT_Wreturn_local_addr, "function returns address "
9250 "of local variable");
9251 break;
9252
9253 default:
9254 break;
9255 }
9256
9257 break;
9258 }
9259
9260 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9261 SET_EXPR_LOCATION (retval, loc);
9262
9263 if (warn_sequence_point)
9264 verify_sequence_points (retval);
9265 }
9266
9267 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9268 TREE_NO_WARNING (ret_stmt) |= no_warning;
9269 return add_stmt (ret_stmt);
9270 }
9271 \f
9272 struct c_switch {
9273 /* The SWITCH_EXPR being built. */
9274 tree switch_expr;
9275
9276 /* The original type of the testing expression, i.e. before the
9277 default conversion is applied. */
9278 tree orig_type;
9279
9280 /* A splay-tree mapping the low element of a case range to the high
9281 element, or NULL_TREE if there is no high element. Used to
9282 determine whether or not a new case label duplicates an old case
9283 label. We need a tree, rather than simply a hash table, because
9284 of the GNU case range extension. */
9285 splay_tree cases;
9286
9287 /* The bindings at the point of the switch. This is used for
9288 warnings crossing decls when branching to a case label. */
9289 struct c_spot_bindings *bindings;
9290
9291 /* The next node on the stack. */
9292 struct c_switch *next;
9293 };
9294
9295 /* A stack of the currently active switch statements. The innermost
9296 switch statement is on the top of the stack. There is no need to
9297 mark the stack for garbage collection because it is only active
9298 during the processing of the body of a function, and we never
9299 collect at that point. */
9300
9301 struct c_switch *c_switch_stack;
9302
9303 /* Start a C switch statement, testing expression EXP. Return the new
9304 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9305 SWITCH_COND_LOC is the location of the switch's condition. */
9306
9307 tree
9308 c_start_case (location_t switch_loc,
9309 location_t switch_cond_loc,
9310 tree exp)
9311 {
9312 tree orig_type = error_mark_node;
9313 struct c_switch *cs;
9314
9315 if (exp != error_mark_node)
9316 {
9317 orig_type = TREE_TYPE (exp);
9318
9319 if (!INTEGRAL_TYPE_P (orig_type))
9320 {
9321 if (orig_type != error_mark_node)
9322 {
9323 error_at (switch_cond_loc, "switch quantity not an integer");
9324 orig_type = error_mark_node;
9325 }
9326 exp = integer_zero_node;
9327 }
9328 else
9329 {
9330 tree type = TYPE_MAIN_VARIANT (orig_type);
9331
9332 if (!in_system_header_at (input_location)
9333 && (type == long_integer_type_node
9334 || type == long_unsigned_type_node))
9335 warning_at (switch_cond_loc,
9336 OPT_Wtraditional, "%<long%> switch expression not "
9337 "converted to %<int%> in ISO C");
9338
9339 exp = c_fully_fold (exp, false, NULL);
9340 exp = default_conversion (exp);
9341
9342 if (warn_sequence_point)
9343 verify_sequence_points (exp);
9344 }
9345 }
9346
9347 /* Add this new SWITCH_EXPR to the stack. */
9348 cs = XNEW (struct c_switch);
9349 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9350 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9351 cs->orig_type = orig_type;
9352 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9353 cs->bindings = c_get_switch_bindings ();
9354 cs->next = c_switch_stack;
9355 c_switch_stack = cs;
9356
9357 return add_stmt (cs->switch_expr);
9358 }
9359
9360 /* Process a case label at location LOC. */
9361
9362 tree
9363 do_case (location_t loc, tree low_value, tree high_value)
9364 {
9365 tree label = NULL_TREE;
9366
9367 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9368 {
9369 low_value = c_fully_fold (low_value, false, NULL);
9370 if (TREE_CODE (low_value) == INTEGER_CST)
9371 pedwarn (input_location, OPT_Wpedantic,
9372 "case label is not an integer constant expression");
9373 }
9374
9375 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9376 {
9377 high_value = c_fully_fold (high_value, false, NULL);
9378 if (TREE_CODE (high_value) == INTEGER_CST)
9379 pedwarn (input_location, OPT_Wpedantic,
9380 "case label is not an integer constant expression");
9381 }
9382
9383 if (c_switch_stack == NULL)
9384 {
9385 if (low_value)
9386 error_at (loc, "case label not within a switch statement");
9387 else
9388 error_at (loc, "%<default%> label not within a switch statement");
9389 return NULL_TREE;
9390 }
9391
9392 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9393 EXPR_LOCATION (c_switch_stack->switch_expr),
9394 loc))
9395 return NULL_TREE;
9396
9397 label = c_add_case_label (loc, c_switch_stack->cases,
9398 SWITCH_COND (c_switch_stack->switch_expr),
9399 c_switch_stack->orig_type,
9400 low_value, high_value);
9401 if (label == error_mark_node)
9402 label = NULL_TREE;
9403 return label;
9404 }
9405
9406 /* Finish the switch statement. */
9407
9408 void
9409 c_finish_case (tree body)
9410 {
9411 struct c_switch *cs = c_switch_stack;
9412 location_t switch_location;
9413
9414 SWITCH_BODY (cs->switch_expr) = body;
9415
9416 /* Emit warnings as needed. */
9417 switch_location = EXPR_LOCATION (cs->switch_expr);
9418 c_do_switch_warnings (cs->cases, switch_location,
9419 TREE_TYPE (cs->switch_expr),
9420 SWITCH_COND (cs->switch_expr));
9421
9422 /* Pop the stack. */
9423 c_switch_stack = cs->next;
9424 splay_tree_delete (cs->cases);
9425 c_release_switch_bindings (cs->bindings);
9426 XDELETE (cs);
9427 }
9428 \f
9429 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9430 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9431 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9432 statement, and was not surrounded with parenthesis. */
9433
9434 void
9435 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9436 tree else_block, bool nested_if)
9437 {
9438 tree stmt;
9439
9440 /* If the condition has array notations, then the rank of the then_block and
9441 else_block must be either 0 or be equal to the rank of the condition. If
9442 the condition does not have array notations then break them up as it is
9443 broken up in a normal expression. */
9444 if (flag_cilkplus && contains_array_notation_expr (cond))
9445 {
9446 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9447 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9448 return;
9449 if (then_block
9450 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9451 return;
9452 if (else_block
9453 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9454 return;
9455 if (cond_rank != then_rank && then_rank != 0)
9456 {
9457 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9458 " and the then-block");
9459 return;
9460 }
9461 else if (cond_rank != else_rank && else_rank != 0)
9462 {
9463 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9464 " and the else-block");
9465 return;
9466 }
9467 }
9468 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9469 if (warn_parentheses && nested_if && else_block == NULL)
9470 {
9471 tree inner_if = then_block;
9472
9473 /* We know from the grammar productions that there is an IF nested
9474 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9475 it might not be exactly THEN_BLOCK, but should be the last
9476 non-container statement within. */
9477 while (1)
9478 switch (TREE_CODE (inner_if))
9479 {
9480 case COND_EXPR:
9481 goto found;
9482 case BIND_EXPR:
9483 inner_if = BIND_EXPR_BODY (inner_if);
9484 break;
9485 case STATEMENT_LIST:
9486 inner_if = expr_last (then_block);
9487 break;
9488 case TRY_FINALLY_EXPR:
9489 case TRY_CATCH_EXPR:
9490 inner_if = TREE_OPERAND (inner_if, 0);
9491 break;
9492 default:
9493 gcc_unreachable ();
9494 }
9495 found:
9496
9497 if (COND_EXPR_ELSE (inner_if))
9498 warning_at (if_locus, OPT_Wparentheses,
9499 "suggest explicit braces to avoid ambiguous %<else%>");
9500 }
9501
9502 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9503 SET_EXPR_LOCATION (stmt, if_locus);
9504 add_stmt (stmt);
9505 }
9506
9507 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9508 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9509 is false for DO loops. INCR is the FOR increment expression. BODY is
9510 the statement controlled by the loop. BLAB is the break label. CLAB is
9511 the continue label. Everything is allowed to be NULL. */
9512
9513 void
9514 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9515 tree blab, tree clab, bool cond_is_first)
9516 {
9517 tree entry = NULL, exit = NULL, t;
9518
9519 if (flag_cilkplus && contains_array_notation_expr (cond))
9520 {
9521 error_at (start_locus, "array notation expression cannot be used in a "
9522 "loop%'s condition");
9523 return;
9524 }
9525
9526 /* If the condition is zero don't generate a loop construct. */
9527 if (cond && integer_zerop (cond))
9528 {
9529 if (cond_is_first)
9530 {
9531 t = build_and_jump (&blab);
9532 SET_EXPR_LOCATION (t, start_locus);
9533 add_stmt (t);
9534 }
9535 }
9536 else
9537 {
9538 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9539
9540 /* If we have an exit condition, then we build an IF with gotos either
9541 out of the loop, or to the top of it. If there's no exit condition,
9542 then we just build a jump back to the top. */
9543 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9544
9545 if (cond && !integer_nonzerop (cond))
9546 {
9547 /* Canonicalize the loop condition to the end. This means
9548 generating a branch to the loop condition. Reuse the
9549 continue label, if possible. */
9550 if (cond_is_first)
9551 {
9552 if (incr || !clab)
9553 {
9554 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9555 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9556 }
9557 else
9558 t = build1 (GOTO_EXPR, void_type_node, clab);
9559 SET_EXPR_LOCATION (t, start_locus);
9560 add_stmt (t);
9561 }
9562
9563 t = build_and_jump (&blab);
9564 if (cond_is_first)
9565 exit = fold_build3_loc (start_locus,
9566 COND_EXPR, void_type_node, cond, exit, t);
9567 else
9568 exit = fold_build3_loc (input_location,
9569 COND_EXPR, void_type_node, cond, exit, t);
9570 }
9571
9572 add_stmt (top);
9573 }
9574
9575 if (body)
9576 add_stmt (body);
9577 if (clab)
9578 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9579 if (incr)
9580 add_stmt (incr);
9581 if (entry)
9582 add_stmt (entry);
9583 if (exit)
9584 add_stmt (exit);
9585 if (blab)
9586 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9587 }
9588
9589 tree
9590 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9591 {
9592 bool skip;
9593 tree label = *label_p;
9594
9595 /* In switch statements break is sometimes stylistically used after
9596 a return statement. This can lead to spurious warnings about
9597 control reaching the end of a non-void function when it is
9598 inlined. Note that we are calling block_may_fallthru with
9599 language specific tree nodes; this works because
9600 block_may_fallthru returns true when given something it does not
9601 understand. */
9602 skip = !block_may_fallthru (cur_stmt_list);
9603
9604 if (!label)
9605 {
9606 if (!skip)
9607 *label_p = label = create_artificial_label (loc);
9608 }
9609 else if (TREE_CODE (label) == LABEL_DECL)
9610 ;
9611 else switch (TREE_INT_CST_LOW (label))
9612 {
9613 case 0:
9614 if (is_break)
9615 error_at (loc, "break statement not within loop or switch");
9616 else
9617 error_at (loc, "continue statement not within a loop");
9618 return NULL_TREE;
9619
9620 case 1:
9621 gcc_assert (is_break);
9622 error_at (loc, "break statement used with OpenMP for loop");
9623 return NULL_TREE;
9624
9625 case 2:
9626 if (is_break)
9627 error ("break statement within %<#pragma simd%> loop body");
9628 else
9629 error ("continue statement within %<#pragma simd%> loop body");
9630 return NULL_TREE;
9631
9632 default:
9633 gcc_unreachable ();
9634 }
9635
9636 if (skip)
9637 return NULL_TREE;
9638
9639 if (!is_break)
9640 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9641
9642 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9643 }
9644
9645 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9646
9647 static void
9648 emit_side_effect_warnings (location_t loc, tree expr)
9649 {
9650 if (expr == error_mark_node)
9651 ;
9652 else if (!TREE_SIDE_EFFECTS (expr))
9653 {
9654 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9655 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9656 }
9657 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9658 {
9659 tree r = expr;
9660 location_t cloc = loc;
9661 while (TREE_CODE (r) == COMPOUND_EXPR)
9662 {
9663 if (EXPR_HAS_LOCATION (r))
9664 cloc = EXPR_LOCATION (r);
9665 r = TREE_OPERAND (r, 1);
9666 }
9667 if (!TREE_SIDE_EFFECTS (r)
9668 && !VOID_TYPE_P (TREE_TYPE (r))
9669 && !CONVERT_EXPR_P (r)
9670 && !TREE_NO_WARNING (r)
9671 && !TREE_NO_WARNING (expr))
9672 warning_at (cloc, OPT_Wunused_value,
9673 "right-hand operand of comma expression has no effect");
9674 }
9675 else
9676 warn_if_unused_value (expr, loc);
9677 }
9678
9679 /* Process an expression as if it were a complete statement. Emit
9680 diagnostics, but do not call ADD_STMT. LOC is the location of the
9681 statement. */
9682
9683 tree
9684 c_process_expr_stmt (location_t loc, tree expr)
9685 {
9686 tree exprv;
9687
9688 if (!expr)
9689 return NULL_TREE;
9690
9691 expr = c_fully_fold (expr, false, NULL);
9692
9693 if (warn_sequence_point)
9694 verify_sequence_points (expr);
9695
9696 if (TREE_TYPE (expr) != error_mark_node
9697 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9698 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9699 error_at (loc, "expression statement has incomplete type");
9700
9701 /* If we're not processing a statement expression, warn about unused values.
9702 Warnings for statement expressions will be emitted later, once we figure
9703 out which is the result. */
9704 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9705 && warn_unused_value)
9706 emit_side_effect_warnings (loc, expr);
9707
9708 exprv = expr;
9709 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9710 exprv = TREE_OPERAND (exprv, 1);
9711 while (CONVERT_EXPR_P (exprv))
9712 exprv = TREE_OPERAND (exprv, 0);
9713 if (DECL_P (exprv)
9714 || handled_component_p (exprv)
9715 || TREE_CODE (exprv) == ADDR_EXPR)
9716 mark_exp_read (exprv);
9717
9718 /* If the expression is not of a type to which we cannot assign a line
9719 number, wrap the thing in a no-op NOP_EXPR. */
9720 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9721 {
9722 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9723 SET_EXPR_LOCATION (expr, loc);
9724 }
9725
9726 return expr;
9727 }
9728
9729 /* Emit an expression as a statement. LOC is the location of the
9730 expression. */
9731
9732 tree
9733 c_finish_expr_stmt (location_t loc, tree expr)
9734 {
9735 if (expr)
9736 return add_stmt (c_process_expr_stmt (loc, expr));
9737 else
9738 return NULL;
9739 }
9740
9741 /* Do the opposite and emit a statement as an expression. To begin,
9742 create a new binding level and return it. */
9743
9744 tree
9745 c_begin_stmt_expr (void)
9746 {
9747 tree ret;
9748
9749 /* We must force a BLOCK for this level so that, if it is not expanded
9750 later, there is a way to turn off the entire subtree of blocks that
9751 are contained in it. */
9752 keep_next_level ();
9753 ret = c_begin_compound_stmt (true);
9754
9755 c_bindings_start_stmt_expr (c_switch_stack == NULL
9756 ? NULL
9757 : c_switch_stack->bindings);
9758
9759 /* Mark the current statement list as belonging to a statement list. */
9760 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9761
9762 return ret;
9763 }
9764
9765 /* LOC is the location of the compound statement to which this body
9766 belongs. */
9767
9768 tree
9769 c_finish_stmt_expr (location_t loc, tree body)
9770 {
9771 tree last, type, tmp, val;
9772 tree *last_p;
9773
9774 body = c_end_compound_stmt (loc, body, true);
9775
9776 c_bindings_end_stmt_expr (c_switch_stack == NULL
9777 ? NULL
9778 : c_switch_stack->bindings);
9779
9780 /* Locate the last statement in BODY. See c_end_compound_stmt
9781 about always returning a BIND_EXPR. */
9782 last_p = &BIND_EXPR_BODY (body);
9783 last = BIND_EXPR_BODY (body);
9784
9785 continue_searching:
9786 if (TREE_CODE (last) == STATEMENT_LIST)
9787 {
9788 tree_stmt_iterator i;
9789
9790 /* This can happen with degenerate cases like ({ }). No value. */
9791 if (!TREE_SIDE_EFFECTS (last))
9792 return body;
9793
9794 /* If we're supposed to generate side effects warnings, process
9795 all of the statements except the last. */
9796 if (warn_unused_value)
9797 {
9798 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9799 {
9800 location_t tloc;
9801 tree t = tsi_stmt (i);
9802
9803 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9804 emit_side_effect_warnings (tloc, t);
9805 }
9806 }
9807 else
9808 i = tsi_last (last);
9809 last_p = tsi_stmt_ptr (i);
9810 last = *last_p;
9811 }
9812
9813 /* If the end of the list is exception related, then the list was split
9814 by a call to push_cleanup. Continue searching. */
9815 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9816 || TREE_CODE (last) == TRY_CATCH_EXPR)
9817 {
9818 last_p = &TREE_OPERAND (last, 0);
9819 last = *last_p;
9820 goto continue_searching;
9821 }
9822
9823 if (last == error_mark_node)
9824 return last;
9825
9826 /* In the case that the BIND_EXPR is not necessary, return the
9827 expression out from inside it. */
9828 if (last == BIND_EXPR_BODY (body)
9829 && BIND_EXPR_VARS (body) == NULL)
9830 {
9831 /* Even if this looks constant, do not allow it in a constant
9832 expression. */
9833 last = c_wrap_maybe_const (last, true);
9834 /* Do not warn if the return value of a statement expression is
9835 unused. */
9836 TREE_NO_WARNING (last) = 1;
9837 return last;
9838 }
9839
9840 /* Extract the type of said expression. */
9841 type = TREE_TYPE (last);
9842
9843 /* If we're not returning a value at all, then the BIND_EXPR that
9844 we already have is a fine expression to return. */
9845 if (!type || VOID_TYPE_P (type))
9846 return body;
9847
9848 /* Now that we've located the expression containing the value, it seems
9849 silly to make voidify_wrapper_expr repeat the process. Create a
9850 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9851 tmp = create_tmp_var_raw (type, NULL);
9852
9853 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9854 tree_expr_nonnegative_p giving up immediately. */
9855 val = last;
9856 if (TREE_CODE (val) == NOP_EXPR
9857 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9858 val = TREE_OPERAND (val, 0);
9859
9860 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9861 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9862
9863 {
9864 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9865 SET_EXPR_LOCATION (t, loc);
9866 return t;
9867 }
9868 }
9869 \f
9870 /* Begin and end compound statements. This is as simple as pushing
9871 and popping new statement lists from the tree. */
9872
9873 tree
9874 c_begin_compound_stmt (bool do_scope)
9875 {
9876 tree stmt = push_stmt_list ();
9877 if (do_scope)
9878 push_scope ();
9879 return stmt;
9880 }
9881
9882 /* End a compound statement. STMT is the statement. LOC is the
9883 location of the compound statement-- this is usually the location
9884 of the opening brace. */
9885
9886 tree
9887 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9888 {
9889 tree block = NULL;
9890
9891 if (do_scope)
9892 {
9893 if (c_dialect_objc ())
9894 objc_clear_super_receiver ();
9895 block = pop_scope ();
9896 }
9897
9898 stmt = pop_stmt_list (stmt);
9899 stmt = c_build_bind_expr (loc, block, stmt);
9900
9901 /* If this compound statement is nested immediately inside a statement
9902 expression, then force a BIND_EXPR to be created. Otherwise we'll
9903 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9904 STATEMENT_LISTs merge, and thus we can lose track of what statement
9905 was really last. */
9906 if (building_stmt_list_p ()
9907 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9908 && TREE_CODE (stmt) != BIND_EXPR)
9909 {
9910 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9911 TREE_SIDE_EFFECTS (stmt) = 1;
9912 SET_EXPR_LOCATION (stmt, loc);
9913 }
9914
9915 return stmt;
9916 }
9917
9918 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9919 when the current scope is exited. EH_ONLY is true when this is not
9920 meant to apply to normal control flow transfer. */
9921
9922 void
9923 push_cleanup (tree decl, tree cleanup, bool eh_only)
9924 {
9925 enum tree_code code;
9926 tree stmt, list;
9927 bool stmt_expr;
9928
9929 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9930 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9931 add_stmt (stmt);
9932 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9933 list = push_stmt_list ();
9934 TREE_OPERAND (stmt, 0) = list;
9935 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9936 }
9937 \f
9938 /* Build a binary-operation expression without default conversions.
9939 CODE is the kind of expression to build.
9940 LOCATION is the operator's location.
9941 This function differs from `build' in several ways:
9942 the data type of the result is computed and recorded in it,
9943 warnings are generated if arg data types are invalid,
9944 special handling for addition and subtraction of pointers is known,
9945 and some optimization is done (operations on narrow ints
9946 are done in the narrower type when that gives the same result).
9947 Constant folding is also done before the result is returned.
9948
9949 Note that the operands will never have enumeral types, or function
9950 or array types, because either they will have the default conversions
9951 performed or they have both just been converted to some other type in which
9952 the arithmetic is to be done. */
9953
9954 tree
9955 build_binary_op (location_t location, enum tree_code code,
9956 tree orig_op0, tree orig_op1, int convert_p)
9957 {
9958 tree type0, type1, orig_type0, orig_type1;
9959 tree eptype;
9960 enum tree_code code0, code1;
9961 tree op0, op1;
9962 tree ret = error_mark_node;
9963 const char *invalid_op_diag;
9964 bool op0_int_operands, op1_int_operands;
9965 bool int_const, int_const_or_overflow, int_operands;
9966
9967 /* Expression code to give to the expression when it is built.
9968 Normally this is CODE, which is what the caller asked for,
9969 but in some special cases we change it. */
9970 enum tree_code resultcode = code;
9971
9972 /* Data type in which the computation is to be performed.
9973 In the simplest cases this is the common type of the arguments. */
9974 tree result_type = NULL;
9975
9976 /* When the computation is in excess precision, the type of the
9977 final EXCESS_PRECISION_EXPR. */
9978 tree semantic_result_type = NULL;
9979
9980 /* Nonzero means operands have already been type-converted
9981 in whatever way is necessary.
9982 Zero means they need to be converted to RESULT_TYPE. */
9983 int converted = 0;
9984
9985 /* Nonzero means create the expression with this type, rather than
9986 RESULT_TYPE. */
9987 tree build_type = 0;
9988
9989 /* Nonzero means after finally constructing the expression
9990 convert it to this type. */
9991 tree final_type = 0;
9992
9993 /* Nonzero if this is an operation like MIN or MAX which can
9994 safely be computed in short if both args are promoted shorts.
9995 Also implies COMMON.
9996 -1 indicates a bitwise operation; this makes a difference
9997 in the exact conditions for when it is safe to do the operation
9998 in a narrower mode. */
9999 int shorten = 0;
10000
10001 /* Nonzero if this is a comparison operation;
10002 if both args are promoted shorts, compare the original shorts.
10003 Also implies COMMON. */
10004 int short_compare = 0;
10005
10006 /* Nonzero if this is a right-shift operation, which can be computed on the
10007 original short and then promoted if the operand is a promoted short. */
10008 int short_shift = 0;
10009
10010 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10011 int common = 0;
10012
10013 /* True means types are compatible as far as ObjC is concerned. */
10014 bool objc_ok;
10015
10016 /* True means this is an arithmetic operation that may need excess
10017 precision. */
10018 bool may_need_excess_precision;
10019
10020 /* True means this is a boolean operation that converts both its
10021 operands to truth-values. */
10022 bool boolean_op = false;
10023
10024 /* Remember whether we're doing / or %. */
10025 bool doing_div_or_mod = false;
10026
10027 /* Remember whether we're doing << or >>. */
10028 bool doing_shift = false;
10029
10030 /* Tree holding instrumentation expression. */
10031 tree instrument_expr = NULL;
10032
10033 if (location == UNKNOWN_LOCATION)
10034 location = input_location;
10035
10036 op0 = orig_op0;
10037 op1 = orig_op1;
10038
10039 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10040 if (op0_int_operands)
10041 op0 = remove_c_maybe_const_expr (op0);
10042 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10043 if (op1_int_operands)
10044 op1 = remove_c_maybe_const_expr (op1);
10045 int_operands = (op0_int_operands && op1_int_operands);
10046 if (int_operands)
10047 {
10048 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10049 && TREE_CODE (orig_op1) == INTEGER_CST);
10050 int_const = (int_const_or_overflow
10051 && !TREE_OVERFLOW (orig_op0)
10052 && !TREE_OVERFLOW (orig_op1));
10053 }
10054 else
10055 int_const = int_const_or_overflow = false;
10056
10057 /* Do not apply default conversion in mixed vector/scalar expression. */
10058 if (convert_p
10059 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10060 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10061 {
10062 op0 = default_conversion (op0);
10063 op1 = default_conversion (op1);
10064 }
10065
10066 /* When Cilk Plus is enabled and there are array notations inside op0, then
10067 we check to see if there are builtin array notation functions. If
10068 so, then we take on the type of the array notation inside it. */
10069 if (flag_cilkplus && contains_array_notation_expr (op0))
10070 orig_type0 = type0 = find_correct_array_notation_type (op0);
10071 else
10072 orig_type0 = type0 = TREE_TYPE (op0);
10073
10074 if (flag_cilkplus && contains_array_notation_expr (op1))
10075 orig_type1 = type1 = find_correct_array_notation_type (op1);
10076 else
10077 orig_type1 = type1 = TREE_TYPE (op1);
10078
10079 /* The expression codes of the data types of the arguments tell us
10080 whether the arguments are integers, floating, pointers, etc. */
10081 code0 = TREE_CODE (type0);
10082 code1 = TREE_CODE (type1);
10083
10084 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10085 STRIP_TYPE_NOPS (op0);
10086 STRIP_TYPE_NOPS (op1);
10087
10088 /* If an error was already reported for one of the arguments,
10089 avoid reporting another error. */
10090
10091 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10092 return error_mark_node;
10093
10094 if ((invalid_op_diag
10095 = targetm.invalid_binary_op (code, type0, type1)))
10096 {
10097 error_at (location, invalid_op_diag);
10098 return error_mark_node;
10099 }
10100
10101 switch (code)
10102 {
10103 case PLUS_EXPR:
10104 case MINUS_EXPR:
10105 case MULT_EXPR:
10106 case TRUNC_DIV_EXPR:
10107 case CEIL_DIV_EXPR:
10108 case FLOOR_DIV_EXPR:
10109 case ROUND_DIV_EXPR:
10110 case EXACT_DIV_EXPR:
10111 may_need_excess_precision = true;
10112 break;
10113 default:
10114 may_need_excess_precision = false;
10115 break;
10116 }
10117 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10118 {
10119 op0 = TREE_OPERAND (op0, 0);
10120 type0 = TREE_TYPE (op0);
10121 }
10122 else if (may_need_excess_precision
10123 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10124 {
10125 type0 = eptype;
10126 op0 = convert (eptype, op0);
10127 }
10128 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10129 {
10130 op1 = TREE_OPERAND (op1, 0);
10131 type1 = TREE_TYPE (op1);
10132 }
10133 else if (may_need_excess_precision
10134 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10135 {
10136 type1 = eptype;
10137 op1 = convert (eptype, op1);
10138 }
10139
10140 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10141
10142 /* In case when one of the operands of the binary operation is
10143 a vector and another is a scalar -- convert scalar to vector. */
10144 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10145 {
10146 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10147 true);
10148
10149 switch (convert_flag)
10150 {
10151 case stv_error:
10152 return error_mark_node;
10153 case stv_firstarg:
10154 {
10155 bool maybe_const = true;
10156 tree sc;
10157 sc = c_fully_fold (op0, false, &maybe_const);
10158 sc = save_expr (sc);
10159 sc = convert (TREE_TYPE (type1), sc);
10160 op0 = build_vector_from_val (type1, sc);
10161 if (!maybe_const)
10162 op0 = c_wrap_maybe_const (op0, true);
10163 orig_type0 = type0 = TREE_TYPE (op0);
10164 code0 = TREE_CODE (type0);
10165 converted = 1;
10166 break;
10167 }
10168 case stv_secondarg:
10169 {
10170 bool maybe_const = true;
10171 tree sc;
10172 sc = c_fully_fold (op1, false, &maybe_const);
10173 sc = save_expr (sc);
10174 sc = convert (TREE_TYPE (type0), sc);
10175 op1 = build_vector_from_val (type0, sc);
10176 if (!maybe_const)
10177 op1 = c_wrap_maybe_const (op1, true);
10178 orig_type1 = type1 = TREE_TYPE (op1);
10179 code1 = TREE_CODE (type1);
10180 converted = 1;
10181 break;
10182 }
10183 default:
10184 break;
10185 }
10186 }
10187
10188 switch (code)
10189 {
10190 case PLUS_EXPR:
10191 /* Handle the pointer + int case. */
10192 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10193 {
10194 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10195 goto return_build_binary_op;
10196 }
10197 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10198 {
10199 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10200 goto return_build_binary_op;
10201 }
10202 else
10203 common = 1;
10204 break;
10205
10206 case MINUS_EXPR:
10207 /* Subtraction of two similar pointers.
10208 We must subtract them as integers, then divide by object size. */
10209 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10210 && comp_target_types (location, type0, type1))
10211 {
10212 ret = pointer_diff (location, op0, op1);
10213 goto return_build_binary_op;
10214 }
10215 /* Handle pointer minus int. Just like pointer plus int. */
10216 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10217 {
10218 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10219 goto return_build_binary_op;
10220 }
10221 else
10222 common = 1;
10223 break;
10224
10225 case MULT_EXPR:
10226 common = 1;
10227 break;
10228
10229 case TRUNC_DIV_EXPR:
10230 case CEIL_DIV_EXPR:
10231 case FLOOR_DIV_EXPR:
10232 case ROUND_DIV_EXPR:
10233 case EXACT_DIV_EXPR:
10234 doing_div_or_mod = true;
10235 warn_for_div_by_zero (location, op1);
10236
10237 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10238 || code0 == FIXED_POINT_TYPE
10239 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10240 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10241 || code1 == FIXED_POINT_TYPE
10242 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10243 {
10244 enum tree_code tcode0 = code0, tcode1 = code1;
10245
10246 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10247 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10248 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10249 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10250
10251 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10252 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10253 resultcode = RDIV_EXPR;
10254 else
10255 /* Although it would be tempting to shorten always here, that
10256 loses on some targets, since the modulo instruction is
10257 undefined if the quotient can't be represented in the
10258 computation mode. We shorten only if unsigned or if
10259 dividing by something we know != -1. */
10260 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10261 || (TREE_CODE (op1) == INTEGER_CST
10262 && !integer_all_onesp (op1)));
10263 common = 1;
10264 }
10265 break;
10266
10267 case BIT_AND_EXPR:
10268 case BIT_IOR_EXPR:
10269 case BIT_XOR_EXPR:
10270 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10271 shorten = -1;
10272 /* Allow vector types which are not floating point types. */
10273 else if (code0 == VECTOR_TYPE
10274 && code1 == VECTOR_TYPE
10275 && !VECTOR_FLOAT_TYPE_P (type0)
10276 && !VECTOR_FLOAT_TYPE_P (type1))
10277 common = 1;
10278 break;
10279
10280 case TRUNC_MOD_EXPR:
10281 case FLOOR_MOD_EXPR:
10282 doing_div_or_mod = true;
10283 warn_for_div_by_zero (location, op1);
10284
10285 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10286 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10287 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10288 common = 1;
10289 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10290 {
10291 /* Although it would be tempting to shorten always here, that loses
10292 on some targets, since the modulo instruction is undefined if the
10293 quotient can't be represented in the computation mode. We shorten
10294 only if unsigned or if dividing by something we know != -1. */
10295 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10296 || (TREE_CODE (op1) == INTEGER_CST
10297 && !integer_all_onesp (op1)));
10298 common = 1;
10299 }
10300 break;
10301
10302 case TRUTH_ANDIF_EXPR:
10303 case TRUTH_ORIF_EXPR:
10304 case TRUTH_AND_EXPR:
10305 case TRUTH_OR_EXPR:
10306 case TRUTH_XOR_EXPR:
10307 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10308 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10309 || code0 == FIXED_POINT_TYPE)
10310 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10311 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10312 || code1 == FIXED_POINT_TYPE))
10313 {
10314 /* Result of these operations is always an int,
10315 but that does not mean the operands should be
10316 converted to ints! */
10317 result_type = integer_type_node;
10318 if (op0_int_operands)
10319 {
10320 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10321 op0 = remove_c_maybe_const_expr (op0);
10322 }
10323 else
10324 op0 = c_objc_common_truthvalue_conversion (location, op0);
10325 if (op1_int_operands)
10326 {
10327 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10328 op1 = remove_c_maybe_const_expr (op1);
10329 }
10330 else
10331 op1 = c_objc_common_truthvalue_conversion (location, op1);
10332 converted = 1;
10333 boolean_op = true;
10334 }
10335 if (code == TRUTH_ANDIF_EXPR)
10336 {
10337 int_const_or_overflow = (int_operands
10338 && TREE_CODE (orig_op0) == INTEGER_CST
10339 && (op0 == truthvalue_false_node
10340 || TREE_CODE (orig_op1) == INTEGER_CST));
10341 int_const = (int_const_or_overflow
10342 && !TREE_OVERFLOW (orig_op0)
10343 && (op0 == truthvalue_false_node
10344 || !TREE_OVERFLOW (orig_op1)));
10345 }
10346 else if (code == TRUTH_ORIF_EXPR)
10347 {
10348 int_const_or_overflow = (int_operands
10349 && TREE_CODE (orig_op0) == INTEGER_CST
10350 && (op0 == truthvalue_true_node
10351 || TREE_CODE (orig_op1) == INTEGER_CST));
10352 int_const = (int_const_or_overflow
10353 && !TREE_OVERFLOW (orig_op0)
10354 && (op0 == truthvalue_true_node
10355 || !TREE_OVERFLOW (orig_op1)));
10356 }
10357 break;
10358
10359 /* Shift operations: result has same type as first operand;
10360 always convert second operand to int.
10361 Also set SHORT_SHIFT if shifting rightward. */
10362
10363 case RSHIFT_EXPR:
10364 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10365 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10366 {
10367 result_type = type0;
10368 converted = 1;
10369 }
10370 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10371 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10372 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10373 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10374 {
10375 result_type = type0;
10376 converted = 1;
10377 }
10378 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10379 && code1 == INTEGER_TYPE)
10380 {
10381 doing_shift = true;
10382 if (TREE_CODE (op1) == INTEGER_CST)
10383 {
10384 if (tree_int_cst_sgn (op1) < 0)
10385 {
10386 int_const = false;
10387 if (c_inhibit_evaluation_warnings == 0)
10388 warning (0, "right shift count is negative");
10389 }
10390 else
10391 {
10392 if (!integer_zerop (op1))
10393 short_shift = 1;
10394
10395 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10396 {
10397 int_const = false;
10398 if (c_inhibit_evaluation_warnings == 0)
10399 warning (0, "right shift count >= width of type");
10400 }
10401 }
10402 }
10403
10404 /* Use the type of the value to be shifted. */
10405 result_type = type0;
10406 /* Convert the non vector shift-count to an integer, regardless
10407 of size of value being shifted. */
10408 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10409 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10410 op1 = convert (integer_type_node, op1);
10411 /* Avoid converting op1 to result_type later. */
10412 converted = 1;
10413 }
10414 break;
10415
10416 case LSHIFT_EXPR:
10417 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10418 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10419 {
10420 result_type = type0;
10421 converted = 1;
10422 }
10423 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10424 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10425 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10426 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10427 {
10428 result_type = type0;
10429 converted = 1;
10430 }
10431 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10432 && code1 == INTEGER_TYPE)
10433 {
10434 doing_shift = true;
10435 if (TREE_CODE (op1) == INTEGER_CST)
10436 {
10437 if (tree_int_cst_sgn (op1) < 0)
10438 {
10439 int_const = false;
10440 if (c_inhibit_evaluation_warnings == 0)
10441 warning (0, "left shift count is negative");
10442 }
10443
10444 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10445 {
10446 int_const = false;
10447 if (c_inhibit_evaluation_warnings == 0)
10448 warning (0, "left shift count >= width of type");
10449 }
10450 }
10451
10452 /* Use the type of the value to be shifted. */
10453 result_type = type0;
10454 /* Convert the non vector shift-count to an integer, regardless
10455 of size of value being shifted. */
10456 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10457 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10458 op1 = convert (integer_type_node, op1);
10459 /* Avoid converting op1 to result_type later. */
10460 converted = 1;
10461 }
10462 break;
10463
10464 case EQ_EXPR:
10465 case NE_EXPR:
10466 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10467 {
10468 tree intt;
10469 if (!vector_types_compatible_elements_p (type0, type1))
10470 {
10471 error_at (location, "comparing vectors with different "
10472 "element types");
10473 return error_mark_node;
10474 }
10475
10476 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10477 {
10478 error_at (location, "comparing vectors with different "
10479 "number of elements");
10480 return error_mark_node;
10481 }
10482
10483 /* Always construct signed integer vector type. */
10484 intt = c_common_type_for_size (GET_MODE_BITSIZE
10485 (TYPE_MODE (TREE_TYPE (type0))), 0);
10486 result_type = build_opaque_vector_type (intt,
10487 TYPE_VECTOR_SUBPARTS (type0));
10488 converted = 1;
10489 break;
10490 }
10491 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10492 warning_at (location,
10493 OPT_Wfloat_equal,
10494 "comparing floating point with == or != is unsafe");
10495 /* Result of comparison is always int,
10496 but don't convert the args to int! */
10497 build_type = integer_type_node;
10498 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10499 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10500 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10501 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10502 short_compare = 1;
10503 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10504 {
10505 if (TREE_CODE (op0) == ADDR_EXPR
10506 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10507 {
10508 if (code == EQ_EXPR)
10509 warning_at (location,
10510 OPT_Waddress,
10511 "the comparison will always evaluate as %<false%> "
10512 "for the address of %qD will never be NULL",
10513 TREE_OPERAND (op0, 0));
10514 else
10515 warning_at (location,
10516 OPT_Waddress,
10517 "the comparison will always evaluate as %<true%> "
10518 "for the address of %qD will never be NULL",
10519 TREE_OPERAND (op0, 0));
10520 }
10521 result_type = type0;
10522 }
10523 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10524 {
10525 if (TREE_CODE (op1) == ADDR_EXPR
10526 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10527 {
10528 if (code == EQ_EXPR)
10529 warning_at (location,
10530 OPT_Waddress,
10531 "the comparison will always evaluate as %<false%> "
10532 "for the address of %qD will never be NULL",
10533 TREE_OPERAND (op1, 0));
10534 else
10535 warning_at (location,
10536 OPT_Waddress,
10537 "the comparison will always evaluate as %<true%> "
10538 "for the address of %qD will never be NULL",
10539 TREE_OPERAND (op1, 0));
10540 }
10541 result_type = type1;
10542 }
10543 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10544 {
10545 tree tt0 = TREE_TYPE (type0);
10546 tree tt1 = TREE_TYPE (type1);
10547 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10548 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10549 addr_space_t as_common = ADDR_SPACE_GENERIC;
10550
10551 /* Anything compares with void *. void * compares with anything.
10552 Otherwise, the targets must be compatible
10553 and both must be object or both incomplete. */
10554 if (comp_target_types (location, type0, type1))
10555 result_type = common_pointer_type (type0, type1);
10556 else if (!addr_space_superset (as0, as1, &as_common))
10557 {
10558 error_at (location, "comparison of pointers to "
10559 "disjoint address spaces");
10560 return error_mark_node;
10561 }
10562 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10563 {
10564 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10565 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10566 "comparison of %<void *%> with function pointer");
10567 }
10568 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10569 {
10570 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10571 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10572 "comparison of %<void *%> with function pointer");
10573 }
10574 else
10575 /* Avoid warning about the volatile ObjC EH puts on decls. */
10576 if (!objc_ok)
10577 pedwarn (location, 0,
10578 "comparison of distinct pointer types lacks a cast");
10579
10580 if (result_type == NULL_TREE)
10581 {
10582 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10583 result_type = build_pointer_type
10584 (build_qualified_type (void_type_node, qual));
10585 }
10586 }
10587 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10588 {
10589 result_type = type0;
10590 pedwarn (location, 0, "comparison between pointer and integer");
10591 }
10592 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10593 {
10594 result_type = type1;
10595 pedwarn (location, 0, "comparison between pointer and integer");
10596 }
10597 break;
10598
10599 case LE_EXPR:
10600 case GE_EXPR:
10601 case LT_EXPR:
10602 case GT_EXPR:
10603 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10604 {
10605 tree intt;
10606 if (!vector_types_compatible_elements_p (type0, type1))
10607 {
10608 error_at (location, "comparing vectors with different "
10609 "element types");
10610 return error_mark_node;
10611 }
10612
10613 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10614 {
10615 error_at (location, "comparing vectors with different "
10616 "number of elements");
10617 return error_mark_node;
10618 }
10619
10620 /* Always construct signed integer vector type. */
10621 intt = c_common_type_for_size (GET_MODE_BITSIZE
10622 (TYPE_MODE (TREE_TYPE (type0))), 0);
10623 result_type = build_opaque_vector_type (intt,
10624 TYPE_VECTOR_SUBPARTS (type0));
10625 converted = 1;
10626 break;
10627 }
10628 build_type = integer_type_node;
10629 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10630 || code0 == FIXED_POINT_TYPE)
10631 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10632 || code1 == FIXED_POINT_TYPE))
10633 short_compare = 1;
10634 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10635 {
10636 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10637 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10638 addr_space_t as_common;
10639
10640 if (comp_target_types (location, type0, type1))
10641 {
10642 result_type = common_pointer_type (type0, type1);
10643 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10644 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10645 pedwarn (location, 0,
10646 "comparison of complete and incomplete pointers");
10647 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10648 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10649 "ordered comparisons of pointers to functions");
10650 else if (null_pointer_constant_p (orig_op0)
10651 || null_pointer_constant_p (orig_op1))
10652 warning_at (location, OPT_Wextra,
10653 "ordered comparison of pointer with null pointer");
10654
10655 }
10656 else if (!addr_space_superset (as0, as1, &as_common))
10657 {
10658 error_at (location, "comparison of pointers to "
10659 "disjoint address spaces");
10660 return error_mark_node;
10661 }
10662 else
10663 {
10664 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10665 result_type = build_pointer_type
10666 (build_qualified_type (void_type_node, qual));
10667 pedwarn (location, 0,
10668 "comparison of distinct pointer types lacks a cast");
10669 }
10670 }
10671 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10672 {
10673 result_type = type0;
10674 if (pedantic)
10675 pedwarn (location, OPT_Wpedantic,
10676 "ordered comparison of pointer with integer zero");
10677 else if (extra_warnings)
10678 warning_at (location, OPT_Wextra,
10679 "ordered comparison of pointer with integer zero");
10680 }
10681 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10682 {
10683 result_type = type1;
10684 if (pedantic)
10685 pedwarn (location, OPT_Wpedantic,
10686 "ordered comparison of pointer with integer zero");
10687 else if (extra_warnings)
10688 warning_at (location, OPT_Wextra,
10689 "ordered comparison of pointer with integer zero");
10690 }
10691 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10692 {
10693 result_type = type0;
10694 pedwarn (location, 0, "comparison between pointer and integer");
10695 }
10696 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10697 {
10698 result_type = type1;
10699 pedwarn (location, 0, "comparison between pointer and integer");
10700 }
10701 break;
10702
10703 default:
10704 gcc_unreachable ();
10705 }
10706
10707 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10708 return error_mark_node;
10709
10710 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10711 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10712 || !vector_types_compatible_elements_p (type0, type1)))
10713 {
10714 binary_op_error (location, code, type0, type1);
10715 return error_mark_node;
10716 }
10717
10718 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10719 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10720 &&
10721 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10722 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10723 {
10724 bool first_complex = (code0 == COMPLEX_TYPE);
10725 bool second_complex = (code1 == COMPLEX_TYPE);
10726 int none_complex = (!first_complex && !second_complex);
10727
10728 if (shorten || common || short_compare)
10729 {
10730 result_type = c_common_type (type0, type1);
10731 do_warn_double_promotion (result_type, type0, type1,
10732 "implicit conversion from %qT to %qT "
10733 "to match other operand of binary "
10734 "expression",
10735 location);
10736 if (result_type == error_mark_node)
10737 return error_mark_node;
10738 }
10739
10740 if (first_complex != second_complex
10741 && (code == PLUS_EXPR
10742 || code == MINUS_EXPR
10743 || code == MULT_EXPR
10744 || (code == TRUNC_DIV_EXPR && first_complex))
10745 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10746 && flag_signed_zeros)
10747 {
10748 /* An operation on mixed real/complex operands must be
10749 handled specially, but the language-independent code can
10750 more easily optimize the plain complex arithmetic if
10751 -fno-signed-zeros. */
10752 tree real_type = TREE_TYPE (result_type);
10753 tree real, imag;
10754 if (type0 != orig_type0 || type1 != orig_type1)
10755 {
10756 gcc_assert (may_need_excess_precision && common);
10757 semantic_result_type = c_common_type (orig_type0, orig_type1);
10758 }
10759 if (first_complex)
10760 {
10761 if (TREE_TYPE (op0) != result_type)
10762 op0 = convert_and_check (location, result_type, op0);
10763 if (TREE_TYPE (op1) != real_type)
10764 op1 = convert_and_check (location, real_type, op1);
10765 }
10766 else
10767 {
10768 if (TREE_TYPE (op0) != real_type)
10769 op0 = convert_and_check (location, real_type, op0);
10770 if (TREE_TYPE (op1) != result_type)
10771 op1 = convert_and_check (location, result_type, op1);
10772 }
10773 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10774 return error_mark_node;
10775 if (first_complex)
10776 {
10777 op0 = c_save_expr (op0);
10778 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10779 op0, 1);
10780 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10781 op0, 1);
10782 switch (code)
10783 {
10784 case MULT_EXPR:
10785 case TRUNC_DIV_EXPR:
10786 op1 = c_save_expr (op1);
10787 imag = build2 (resultcode, real_type, imag, op1);
10788 /* Fall through. */
10789 case PLUS_EXPR:
10790 case MINUS_EXPR:
10791 real = build2 (resultcode, real_type, real, op1);
10792 break;
10793 default:
10794 gcc_unreachable();
10795 }
10796 }
10797 else
10798 {
10799 op1 = c_save_expr (op1);
10800 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10801 op1, 1);
10802 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10803 op1, 1);
10804 switch (code)
10805 {
10806 case MULT_EXPR:
10807 op0 = c_save_expr (op0);
10808 imag = build2 (resultcode, real_type, op0, imag);
10809 /* Fall through. */
10810 case PLUS_EXPR:
10811 real = build2 (resultcode, real_type, op0, real);
10812 break;
10813 case MINUS_EXPR:
10814 real = build2 (resultcode, real_type, op0, real);
10815 imag = build1 (NEGATE_EXPR, real_type, imag);
10816 break;
10817 default:
10818 gcc_unreachable();
10819 }
10820 }
10821 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10822 goto return_build_binary_op;
10823 }
10824
10825 /* For certain operations (which identify themselves by shorten != 0)
10826 if both args were extended from the same smaller type,
10827 do the arithmetic in that type and then extend.
10828
10829 shorten !=0 and !=1 indicates a bitwise operation.
10830 For them, this optimization is safe only if
10831 both args are zero-extended or both are sign-extended.
10832 Otherwise, we might change the result.
10833 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10834 but calculated in (unsigned short) it would be (unsigned short)-1. */
10835
10836 if (shorten && none_complex)
10837 {
10838 final_type = result_type;
10839 result_type = shorten_binary_op (result_type, op0, op1,
10840 shorten == -1);
10841 }
10842
10843 /* Shifts can be shortened if shifting right. */
10844
10845 if (short_shift)
10846 {
10847 int unsigned_arg;
10848 tree arg0 = get_narrower (op0, &unsigned_arg);
10849
10850 final_type = result_type;
10851
10852 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10853 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10854
10855 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10856 && tree_int_cst_sgn (op1) > 0
10857 /* We can shorten only if the shift count is less than the
10858 number of bits in the smaller type size. */
10859 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10860 /* We cannot drop an unsigned shift after sign-extension. */
10861 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10862 {
10863 /* Do an unsigned shift if the operand was zero-extended. */
10864 result_type
10865 = c_common_signed_or_unsigned_type (unsigned_arg,
10866 TREE_TYPE (arg0));
10867 /* Convert value-to-be-shifted to that type. */
10868 if (TREE_TYPE (op0) != result_type)
10869 op0 = convert (result_type, op0);
10870 converted = 1;
10871 }
10872 }
10873
10874 /* Comparison operations are shortened too but differently.
10875 They identify themselves by setting short_compare = 1. */
10876
10877 if (short_compare)
10878 {
10879 /* Don't write &op0, etc., because that would prevent op0
10880 from being kept in a register.
10881 Instead, make copies of the our local variables and
10882 pass the copies by reference, then copy them back afterward. */
10883 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10884 enum tree_code xresultcode = resultcode;
10885 tree val
10886 = shorten_compare (location, &xop0, &xop1, &xresult_type,
10887 &xresultcode);
10888
10889 if (val != 0)
10890 {
10891 ret = val;
10892 goto return_build_binary_op;
10893 }
10894
10895 op0 = xop0, op1 = xop1;
10896 converted = 1;
10897 resultcode = xresultcode;
10898
10899 if (c_inhibit_evaluation_warnings == 0)
10900 {
10901 bool op0_maybe_const = true;
10902 bool op1_maybe_const = true;
10903 tree orig_op0_folded, orig_op1_folded;
10904
10905 if (in_late_binary_op)
10906 {
10907 orig_op0_folded = orig_op0;
10908 orig_op1_folded = orig_op1;
10909 }
10910 else
10911 {
10912 /* Fold for the sake of possible warnings, as in
10913 build_conditional_expr. This requires the
10914 "original" values to be folded, not just op0 and
10915 op1. */
10916 c_inhibit_evaluation_warnings++;
10917 op0 = c_fully_fold (op0, require_constant_value,
10918 &op0_maybe_const);
10919 op1 = c_fully_fold (op1, require_constant_value,
10920 &op1_maybe_const);
10921 c_inhibit_evaluation_warnings--;
10922 orig_op0_folded = c_fully_fold (orig_op0,
10923 require_constant_value,
10924 NULL);
10925 orig_op1_folded = c_fully_fold (orig_op1,
10926 require_constant_value,
10927 NULL);
10928 }
10929
10930 if (warn_sign_compare)
10931 warn_for_sign_compare (location, orig_op0_folded,
10932 orig_op1_folded, op0, op1,
10933 result_type, resultcode);
10934 if (!in_late_binary_op && !int_operands)
10935 {
10936 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10937 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10938 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10939 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10940 }
10941 }
10942 }
10943 }
10944
10945 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10946 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10947 Then the expression will be built.
10948 It will be given type FINAL_TYPE if that is nonzero;
10949 otherwise, it will be given type RESULT_TYPE. */
10950
10951 if (!result_type)
10952 {
10953 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10954 return error_mark_node;
10955 }
10956
10957 if (build_type == NULL_TREE)
10958 {
10959 build_type = result_type;
10960 if ((type0 != orig_type0 || type1 != orig_type1)
10961 && !boolean_op)
10962 {
10963 gcc_assert (may_need_excess_precision && common);
10964 semantic_result_type = c_common_type (orig_type0, orig_type1);
10965 }
10966 }
10967
10968 if (!converted)
10969 {
10970 op0 = ep_convert_and_check (location, result_type, op0,
10971 semantic_result_type);
10972 op1 = ep_convert_and_check (location, result_type, op1,
10973 semantic_result_type);
10974
10975 /* This can happen if one operand has a vector type, and the other
10976 has a different type. */
10977 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10978 return error_mark_node;
10979 }
10980
10981 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
10982 && current_function_decl != 0
10983 && !lookup_attribute ("no_sanitize_undefined",
10984 DECL_ATTRIBUTES (current_function_decl))
10985 && (doing_div_or_mod || doing_shift))
10986 {
10987 /* OP0 and/or OP1 might have side-effects. */
10988 op0 = c_save_expr (op0);
10989 op1 = c_save_expr (op1);
10990 op0 = c_fully_fold (op0, false, NULL);
10991 op1 = c_fully_fold (op1, false, NULL);
10992 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
10993 instrument_expr = ubsan_instrument_division (location, op0, op1);
10994 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
10995 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
10996 }
10997
10998 /* Treat expressions in initializers specially as they can't trap. */
10999 if (int_const_or_overflow)
11000 ret = (require_constant_value
11001 ? fold_build2_initializer_loc (location, resultcode, build_type,
11002 op0, op1)
11003 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11004 else
11005 ret = build2 (resultcode, build_type, op0, op1);
11006 if (final_type != 0)
11007 ret = convert (final_type, ret);
11008
11009 return_build_binary_op:
11010 gcc_assert (ret != error_mark_node);
11011 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11012 ret = (int_operands
11013 ? note_integer_operands (ret)
11014 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11015 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11016 && !in_late_binary_op)
11017 ret = note_integer_operands (ret);
11018 if (semantic_result_type)
11019 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11020 protected_set_expr_location (ret, location);
11021
11022 if (instrument_expr != NULL)
11023 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11024 instrument_expr, ret);
11025
11026 return ret;
11027 }
11028
11029
11030 /* Convert EXPR to be a truth-value, validating its type for this
11031 purpose. LOCATION is the source location for the expression. */
11032
11033 tree
11034 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11035 {
11036 bool int_const, int_operands;
11037
11038 switch (TREE_CODE (TREE_TYPE (expr)))
11039 {
11040 case ARRAY_TYPE:
11041 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11042 return error_mark_node;
11043
11044 case RECORD_TYPE:
11045 error_at (location, "used struct type value where scalar is required");
11046 return error_mark_node;
11047
11048 case UNION_TYPE:
11049 error_at (location, "used union type value where scalar is required");
11050 return error_mark_node;
11051
11052 case VOID_TYPE:
11053 error_at (location, "void value not ignored as it ought to be");
11054 return error_mark_node;
11055
11056 case FUNCTION_TYPE:
11057 gcc_unreachable ();
11058
11059 case VECTOR_TYPE:
11060 error_at (location, "used vector type where scalar is required");
11061 return error_mark_node;
11062
11063 default:
11064 break;
11065 }
11066
11067 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11068 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11069 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11070 {
11071 expr = remove_c_maybe_const_expr (expr);
11072 expr = build2 (NE_EXPR, integer_type_node, expr,
11073 convert (TREE_TYPE (expr), integer_zero_node));
11074 expr = note_integer_operands (expr);
11075 }
11076 else
11077 /* ??? Should we also give an error for vectors rather than leaving
11078 those to give errors later? */
11079 expr = c_common_truthvalue_conversion (location, expr);
11080
11081 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11082 {
11083 if (TREE_OVERFLOW (expr))
11084 return expr;
11085 else
11086 return note_integer_operands (expr);
11087 }
11088 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11089 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11090 return expr;
11091 }
11092 \f
11093
11094 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11095 required. */
11096
11097 tree
11098 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11099 {
11100 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11101 {
11102 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11103 /* Executing a compound literal inside a function reinitializes
11104 it. */
11105 if (!TREE_STATIC (decl))
11106 *se = true;
11107 return decl;
11108 }
11109 else
11110 return expr;
11111 }
11112 \f
11113 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11114
11115 tree
11116 c_begin_omp_parallel (void)
11117 {
11118 tree block;
11119
11120 keep_next_level ();
11121 block = c_begin_compound_stmt (true);
11122
11123 return block;
11124 }
11125
11126 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11127 statement. LOC is the location of the OMP_PARALLEL. */
11128
11129 tree
11130 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11131 {
11132 tree stmt;
11133
11134 block = c_end_compound_stmt (loc, block, true);
11135
11136 stmt = make_node (OMP_PARALLEL);
11137 TREE_TYPE (stmt) = void_type_node;
11138 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11139 OMP_PARALLEL_BODY (stmt) = block;
11140 SET_EXPR_LOCATION (stmt, loc);
11141
11142 return add_stmt (stmt);
11143 }
11144
11145 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11146
11147 tree
11148 c_begin_omp_task (void)
11149 {
11150 tree block;
11151
11152 keep_next_level ();
11153 block = c_begin_compound_stmt (true);
11154
11155 return block;
11156 }
11157
11158 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11159 statement. LOC is the location of the #pragma. */
11160
11161 tree
11162 c_finish_omp_task (location_t loc, tree clauses, tree block)
11163 {
11164 tree stmt;
11165
11166 block = c_end_compound_stmt (loc, block, true);
11167
11168 stmt = make_node (OMP_TASK);
11169 TREE_TYPE (stmt) = void_type_node;
11170 OMP_TASK_CLAUSES (stmt) = clauses;
11171 OMP_TASK_BODY (stmt) = block;
11172 SET_EXPR_LOCATION (stmt, loc);
11173
11174 return add_stmt (stmt);
11175 }
11176
11177 /* Generate GOMP_cancel call for #pragma omp cancel. */
11178
11179 void
11180 c_finish_omp_cancel (location_t loc, tree clauses)
11181 {
11182 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11183 int mask = 0;
11184 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11185 mask = 1;
11186 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11187 mask = 2;
11188 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11189 mask = 4;
11190 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11191 mask = 8;
11192 else
11193 {
11194 error_at (loc, "%<#pragma omp cancel must specify one of "
11195 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11196 "clauses");
11197 return;
11198 }
11199 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11200 if (ifc != NULL_TREE)
11201 {
11202 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11203 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11204 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11205 build_zero_cst (type));
11206 }
11207 else
11208 ifc = boolean_true_node;
11209 tree stmt = build_call_expr_loc (loc, fn, 2,
11210 build_int_cst (integer_type_node, mask),
11211 ifc);
11212 add_stmt (stmt);
11213 }
11214
11215 /* Generate GOMP_cancellation_point call for
11216 #pragma omp cancellation point. */
11217
11218 void
11219 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11220 {
11221 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11222 int mask = 0;
11223 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11224 mask = 1;
11225 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11226 mask = 2;
11227 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11228 mask = 4;
11229 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11230 mask = 8;
11231 else
11232 {
11233 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11234 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11235 "clauses");
11236 return;
11237 }
11238 tree stmt = build_call_expr_loc (loc, fn, 1,
11239 build_int_cst (integer_type_node, mask));
11240 add_stmt (stmt);
11241 }
11242
11243 /* Helper function for handle_omp_array_sections. Called recursively
11244 to handle multiple array-section-subscripts. C is the clause,
11245 T current expression (initially OMP_CLAUSE_DECL), which is either
11246 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11247 expression if specified, TREE_VALUE length expression if specified,
11248 TREE_CHAIN is what it has been specified after, or some decl.
11249 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11250 set to true if any of the array-section-subscript could have length
11251 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11252 first array-section-subscript which is known not to have length
11253 of one. Given say:
11254 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11255 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11256 all are or may have length of 1, array-section-subscript [:2] is the
11257 first one knonwn not to have length 1. For array-section-subscript
11258 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11259 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11260 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11261 case though, as some lengths could be zero. */
11262
11263 static tree
11264 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11265 bool &maybe_zero_len, unsigned int &first_non_one)
11266 {
11267 tree ret, low_bound, length, type;
11268 if (TREE_CODE (t) != TREE_LIST)
11269 {
11270 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11271 return error_mark_node;
11272 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11273 {
11274 if (DECL_P (t))
11275 error_at (OMP_CLAUSE_LOCATION (c),
11276 "%qD is not a variable in %qs clause", t,
11277 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11278 else
11279 error_at (OMP_CLAUSE_LOCATION (c),
11280 "%qE is not a variable in %qs clause", t,
11281 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11282 return error_mark_node;
11283 }
11284 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11285 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11286 {
11287 error_at (OMP_CLAUSE_LOCATION (c),
11288 "%qD is threadprivate variable in %qs clause", t,
11289 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11290 return error_mark_node;
11291 }
11292 return t;
11293 }
11294
11295 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11296 maybe_zero_len, first_non_one);
11297 if (ret == error_mark_node || ret == NULL_TREE)
11298 return ret;
11299
11300 type = TREE_TYPE (ret);
11301 low_bound = TREE_PURPOSE (t);
11302 length = TREE_VALUE (t);
11303
11304 if (low_bound == error_mark_node || length == error_mark_node)
11305 return error_mark_node;
11306
11307 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11308 {
11309 error_at (OMP_CLAUSE_LOCATION (c),
11310 "low bound %qE of array section does not have integral type",
11311 low_bound);
11312 return error_mark_node;
11313 }
11314 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11315 {
11316 error_at (OMP_CLAUSE_LOCATION (c),
11317 "length %qE of array section does not have integral type",
11318 length);
11319 return error_mark_node;
11320 }
11321 if (low_bound
11322 && TREE_CODE (low_bound) == INTEGER_CST
11323 && TYPE_PRECISION (TREE_TYPE (low_bound))
11324 > TYPE_PRECISION (sizetype))
11325 low_bound = fold_convert (sizetype, low_bound);
11326 if (length
11327 && TREE_CODE (length) == INTEGER_CST
11328 && TYPE_PRECISION (TREE_TYPE (length))
11329 > TYPE_PRECISION (sizetype))
11330 length = fold_convert (sizetype, length);
11331 if (low_bound == NULL_TREE)
11332 low_bound = integer_zero_node;
11333
11334 if (length != NULL_TREE)
11335 {
11336 if (!integer_nonzerop (length))
11337 maybe_zero_len = true;
11338 if (first_non_one == types.length ()
11339 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11340 first_non_one++;
11341 }
11342 if (TREE_CODE (type) == ARRAY_TYPE)
11343 {
11344 if (length == NULL_TREE
11345 && (TYPE_DOMAIN (type) == NULL_TREE
11346 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11347 {
11348 error_at (OMP_CLAUSE_LOCATION (c),
11349 "for unknown bound array type length expression must "
11350 "be specified");
11351 return error_mark_node;
11352 }
11353 if (TREE_CODE (low_bound) == INTEGER_CST
11354 && tree_int_cst_sgn (low_bound) == -1)
11355 {
11356 error_at (OMP_CLAUSE_LOCATION (c),
11357 "negative low bound in array section in %qs clause",
11358 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11359 return error_mark_node;
11360 }
11361 if (length != NULL_TREE
11362 && TREE_CODE (length) == INTEGER_CST
11363 && tree_int_cst_sgn (length) == -1)
11364 {
11365 error_at (OMP_CLAUSE_LOCATION (c),
11366 "negative length in array section in %qs clause",
11367 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11368 return error_mark_node;
11369 }
11370 if (TYPE_DOMAIN (type)
11371 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11372 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11373 == INTEGER_CST)
11374 {
11375 tree size = size_binop (PLUS_EXPR,
11376 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11377 size_one_node);
11378 if (TREE_CODE (low_bound) == INTEGER_CST)
11379 {
11380 if (tree_int_cst_lt (size, low_bound))
11381 {
11382 error_at (OMP_CLAUSE_LOCATION (c),
11383 "low bound %qE above array section size "
11384 "in %qs clause", low_bound,
11385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11386 return error_mark_node;
11387 }
11388 if (tree_int_cst_equal (size, low_bound))
11389 maybe_zero_len = true;
11390 else if (length == NULL_TREE
11391 && first_non_one == types.length ()
11392 && tree_int_cst_equal
11393 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11394 low_bound))
11395 first_non_one++;
11396 }
11397 else if (length == NULL_TREE)
11398 {
11399 maybe_zero_len = true;
11400 if (first_non_one == types.length ())
11401 first_non_one++;
11402 }
11403 if (length && TREE_CODE (length) == INTEGER_CST)
11404 {
11405 if (tree_int_cst_lt (size, length))
11406 {
11407 error_at (OMP_CLAUSE_LOCATION (c),
11408 "length %qE above array section size "
11409 "in %qs clause", length,
11410 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11411 return error_mark_node;
11412 }
11413 if (TREE_CODE (low_bound) == INTEGER_CST)
11414 {
11415 tree lbpluslen
11416 = size_binop (PLUS_EXPR,
11417 fold_convert (sizetype, low_bound),
11418 fold_convert (sizetype, length));
11419 if (TREE_CODE (lbpluslen) == INTEGER_CST
11420 && tree_int_cst_lt (size, lbpluslen))
11421 {
11422 error_at (OMP_CLAUSE_LOCATION (c),
11423 "high bound %qE above array section size "
11424 "in %qs clause", lbpluslen,
11425 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11426 return error_mark_node;
11427 }
11428 }
11429 }
11430 }
11431 else if (length == NULL_TREE)
11432 {
11433 maybe_zero_len = true;
11434 if (first_non_one == types.length ())
11435 first_non_one++;
11436 }
11437
11438 /* For [lb:] we will need to evaluate lb more than once. */
11439 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11440 {
11441 tree lb = c_save_expr (low_bound);
11442 if (lb != low_bound)
11443 {
11444 TREE_PURPOSE (t) = lb;
11445 low_bound = lb;
11446 }
11447 }
11448 }
11449 else if (TREE_CODE (type) == POINTER_TYPE)
11450 {
11451 if (length == NULL_TREE)
11452 {
11453 error_at (OMP_CLAUSE_LOCATION (c),
11454 "for pointer type length expression must be specified");
11455 return error_mark_node;
11456 }
11457 /* If there is a pointer type anywhere but in the very first
11458 array-section-subscript, the array section can't be contiguous. */
11459 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11460 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11461 {
11462 error_at (OMP_CLAUSE_LOCATION (c),
11463 "array section is not contiguous in %qs clause",
11464 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11465 return error_mark_node;
11466 }
11467 }
11468 else
11469 {
11470 error_at (OMP_CLAUSE_LOCATION (c),
11471 "%qE does not have pointer or array type", ret);
11472 return error_mark_node;
11473 }
11474 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11475 types.safe_push (TREE_TYPE (ret));
11476 /* We will need to evaluate lb more than once. */
11477 tree lb = c_save_expr (low_bound);
11478 if (lb != low_bound)
11479 {
11480 TREE_PURPOSE (t) = lb;
11481 low_bound = lb;
11482 }
11483 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11484 return ret;
11485 }
11486
11487 /* Handle array sections for clause C. */
11488
11489 static bool
11490 handle_omp_array_sections (tree c)
11491 {
11492 bool maybe_zero_len = false;
11493 unsigned int first_non_one = 0;
11494 vec<tree> types = vNULL;
11495 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11496 maybe_zero_len, first_non_one);
11497 if (first == error_mark_node)
11498 {
11499 types.release ();
11500 return true;
11501 }
11502 if (first == NULL_TREE)
11503 {
11504 types.release ();
11505 return false;
11506 }
11507 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11508 {
11509 tree t = OMP_CLAUSE_DECL (c);
11510 tree tem = NULL_TREE;
11511 types.release ();
11512 /* Need to evaluate side effects in the length expressions
11513 if any. */
11514 while (TREE_CODE (t) == TREE_LIST)
11515 {
11516 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11517 {
11518 if (tem == NULL_TREE)
11519 tem = TREE_VALUE (t);
11520 else
11521 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11522 TREE_VALUE (t), tem);
11523 }
11524 t = TREE_CHAIN (t);
11525 }
11526 if (tem)
11527 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11528 first = c_fully_fold (first, false, NULL);
11529 OMP_CLAUSE_DECL (c) = first;
11530 }
11531 else
11532 {
11533 unsigned int num = types.length (), i;
11534 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11535 tree condition = NULL_TREE;
11536
11537 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11538 maybe_zero_len = true;
11539
11540 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11541 t = TREE_CHAIN (t))
11542 {
11543 tree low_bound = TREE_PURPOSE (t);
11544 tree length = TREE_VALUE (t);
11545
11546 i--;
11547 if (low_bound
11548 && TREE_CODE (low_bound) == INTEGER_CST
11549 && TYPE_PRECISION (TREE_TYPE (low_bound))
11550 > TYPE_PRECISION (sizetype))
11551 low_bound = fold_convert (sizetype, low_bound);
11552 if (length
11553 && TREE_CODE (length) == INTEGER_CST
11554 && TYPE_PRECISION (TREE_TYPE (length))
11555 > TYPE_PRECISION (sizetype))
11556 length = fold_convert (sizetype, length);
11557 if (low_bound == NULL_TREE)
11558 low_bound = integer_zero_node;
11559 if (!maybe_zero_len && i > first_non_one)
11560 {
11561 if (integer_nonzerop (low_bound))
11562 goto do_warn_noncontiguous;
11563 if (length != NULL_TREE
11564 && TREE_CODE (length) == INTEGER_CST
11565 && TYPE_DOMAIN (types[i])
11566 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11567 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11568 == INTEGER_CST)
11569 {
11570 tree size;
11571 size = size_binop (PLUS_EXPR,
11572 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11573 size_one_node);
11574 if (!tree_int_cst_equal (length, size))
11575 {
11576 do_warn_noncontiguous:
11577 error_at (OMP_CLAUSE_LOCATION (c),
11578 "array section is not contiguous in %qs "
11579 "clause",
11580 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11581 types.release ();
11582 return true;
11583 }
11584 }
11585 if (length != NULL_TREE
11586 && TREE_SIDE_EFFECTS (length))
11587 {
11588 if (side_effects == NULL_TREE)
11589 side_effects = length;
11590 else
11591 side_effects = build2 (COMPOUND_EXPR,
11592 TREE_TYPE (side_effects),
11593 length, side_effects);
11594 }
11595 }
11596 else
11597 {
11598 tree l;
11599
11600 if (i > first_non_one && length && integer_nonzerop (length))
11601 continue;
11602 if (length)
11603 l = fold_convert (sizetype, length);
11604 else
11605 {
11606 l = size_binop (PLUS_EXPR,
11607 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11608 size_one_node);
11609 l = size_binop (MINUS_EXPR, l,
11610 fold_convert (sizetype, low_bound));
11611 }
11612 if (i > first_non_one)
11613 {
11614 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11615 size_zero_node);
11616 if (condition == NULL_TREE)
11617 condition = l;
11618 else
11619 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11620 l, condition);
11621 }
11622 else if (size == NULL_TREE)
11623 {
11624 size = size_in_bytes (TREE_TYPE (types[i]));
11625 size = size_binop (MULT_EXPR, size, l);
11626 if (condition)
11627 size = fold_build3 (COND_EXPR, sizetype, condition,
11628 size, size_zero_node);
11629 }
11630 else
11631 size = size_binop (MULT_EXPR, size, l);
11632 }
11633 }
11634 types.release ();
11635 if (side_effects)
11636 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11637 first = c_fully_fold (first, false, NULL);
11638 OMP_CLAUSE_DECL (c) = first;
11639 if (size)
11640 size = c_fully_fold (size, false, NULL);
11641 OMP_CLAUSE_SIZE (c) = size;
11642 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11643 return false;
11644 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11645 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11646 if (!c_mark_addressable (t))
11647 return false;
11648 OMP_CLAUSE_DECL (c2) = t;
11649 t = build_fold_addr_expr (first);
11650 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11651 tree ptr = OMP_CLAUSE_DECL (c2);
11652 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11653 ptr = build_fold_addr_expr (ptr);
11654 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11655 ptrdiff_type_node, t,
11656 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11657 ptrdiff_type_node, ptr));
11658 t = c_fully_fold (t, false, NULL);
11659 OMP_CLAUSE_SIZE (c2) = t;
11660 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11661 OMP_CLAUSE_CHAIN (c) = c2;
11662 }
11663 return false;
11664 }
11665
11666 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11667 an inline call. But, remap
11668 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11669 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11670
11671 static tree
11672 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11673 tree decl, tree placeholder)
11674 {
11675 copy_body_data id;
11676 struct pointer_map_t *decl_map = pointer_map_create ();
11677
11678 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
11679 *pointer_map_insert (decl_map, omp_decl2) = decl;
11680 memset (&id, 0, sizeof (id));
11681 id.src_fn = DECL_CONTEXT (omp_decl1);
11682 id.dst_fn = current_function_decl;
11683 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11684 id.decl_map = decl_map;
11685
11686 id.copy_decl = copy_decl_no_change;
11687 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11688 id.transform_new_cfg = true;
11689 id.transform_return_to_modify = false;
11690 id.transform_lang_insert_block = NULL;
11691 id.eh_lp_nr = 0;
11692 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11693 pointer_map_destroy (decl_map);
11694 return stmt;
11695 }
11696
11697 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11698 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11699
11700 static tree
11701 c_find_omp_placeholder_r (tree *tp, int *, void *data)
11702 {
11703 if (*tp == (tree) data)
11704 return *tp;
11705 return NULL_TREE;
11706 }
11707
11708 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11709 Remove any elements from the list that are invalid. */
11710
11711 tree
11712 c_finish_omp_clauses (tree clauses)
11713 {
11714 bitmap_head generic_head, firstprivate_head, lastprivate_head;
11715 bitmap_head aligned_head;
11716 tree c, t, *pc = &clauses;
11717 bool branch_seen = false;
11718 bool copyprivate_seen = false;
11719 tree *nowait_clause = NULL;
11720
11721 bitmap_obstack_initialize (NULL);
11722 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11723 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11724 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
11725 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
11726
11727 for (pc = &clauses, c = clauses; c ; c = *pc)
11728 {
11729 bool remove = false;
11730 bool need_complete = false;
11731 bool need_implicitly_determined = false;
11732
11733 switch (OMP_CLAUSE_CODE (c))
11734 {
11735 case OMP_CLAUSE_SHARED:
11736 need_implicitly_determined = true;
11737 goto check_dup_generic;
11738
11739 case OMP_CLAUSE_PRIVATE:
11740 need_complete = true;
11741 need_implicitly_determined = true;
11742 goto check_dup_generic;
11743
11744 case OMP_CLAUSE_REDUCTION:
11745 need_implicitly_determined = true;
11746 t = OMP_CLAUSE_DECL (c);
11747 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11748 && (FLOAT_TYPE_P (TREE_TYPE (t))
11749 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
11750 {
11751 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11752 const char *r_name = NULL;
11753
11754 switch (r_code)
11755 {
11756 case PLUS_EXPR:
11757 case MULT_EXPR:
11758 case MINUS_EXPR:
11759 break;
11760 case MIN_EXPR:
11761 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11762 r_name = "min";
11763 break;
11764 case MAX_EXPR:
11765 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11766 r_name = "max";
11767 break;
11768 case BIT_AND_EXPR:
11769 r_name = "&";
11770 break;
11771 case BIT_XOR_EXPR:
11772 r_name = "^";
11773 break;
11774 case BIT_IOR_EXPR:
11775 r_name = "|";
11776 break;
11777 case TRUTH_ANDIF_EXPR:
11778 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11779 r_name = "&&";
11780 break;
11781 case TRUTH_ORIF_EXPR:
11782 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11783 r_name = "||";
11784 break;
11785 default:
11786 gcc_unreachable ();
11787 }
11788 if (r_name)
11789 {
11790 error_at (OMP_CLAUSE_LOCATION (c),
11791 "%qE has invalid type for %<reduction(%s)%>",
11792 t, r_name);
11793 remove = true;
11794 break;
11795 }
11796 }
11797 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11798 {
11799 error_at (OMP_CLAUSE_LOCATION (c),
11800 "user defined reduction not found for %qD", t);
11801 remove = true;
11802 break;
11803 }
11804 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11805 {
11806 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11807 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11808 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11809 VAR_DECL, NULL_TREE, type);
11810 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11811 DECL_ARTIFICIAL (placeholder) = 1;
11812 DECL_IGNORED_P (placeholder) = 1;
11813 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11814 c_mark_addressable (placeholder);
11815 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11816 c_mark_addressable (OMP_CLAUSE_DECL (c));
11817 OMP_CLAUSE_REDUCTION_MERGE (c)
11818 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11819 TREE_VEC_ELT (list, 0),
11820 TREE_VEC_ELT (list, 1),
11821 OMP_CLAUSE_DECL (c), placeholder);
11822 OMP_CLAUSE_REDUCTION_MERGE (c)
11823 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11824 void_type_node, NULL_TREE,
11825 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11826 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11827 if (TREE_VEC_LENGTH (list) == 6)
11828 {
11829 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11830 c_mark_addressable (OMP_CLAUSE_DECL (c));
11831 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11832 c_mark_addressable (placeholder);
11833 tree init = TREE_VEC_ELT (list, 5);
11834 if (init == error_mark_node)
11835 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11836 OMP_CLAUSE_REDUCTION_INIT (c)
11837 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11838 TREE_VEC_ELT (list, 3),
11839 OMP_CLAUSE_DECL (c), placeholder);
11840 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11841 OMP_CLAUSE_REDUCTION_INIT (c)
11842 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11843 OMP_CLAUSE_REDUCTION_INIT (c));
11844 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11845 c_find_omp_placeholder_r,
11846 placeholder, NULL))
11847 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11848 }
11849 else
11850 {
11851 tree init;
11852 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11853 init = build_constructor (TREE_TYPE (t), NULL);
11854 else
11855 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11856 OMP_CLAUSE_REDUCTION_INIT (c)
11857 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11858 }
11859 OMP_CLAUSE_REDUCTION_INIT (c)
11860 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11861 void_type_node, NULL_TREE,
11862 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11863 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11864 }
11865 goto check_dup_generic;
11866
11867 case OMP_CLAUSE_COPYPRIVATE:
11868 copyprivate_seen = true;
11869 if (nowait_clause)
11870 {
11871 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11872 "%<nowait%> clause must not be used together "
11873 "with %<copyprivate%>");
11874 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11875 nowait_clause = NULL;
11876 }
11877 goto check_dup_generic;
11878
11879 case OMP_CLAUSE_COPYIN:
11880 t = OMP_CLAUSE_DECL (c);
11881 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11882 {
11883 error_at (OMP_CLAUSE_LOCATION (c),
11884 "%qE must be %<threadprivate%> for %<copyin%>", t);
11885 remove = true;
11886 break;
11887 }
11888 goto check_dup_generic;
11889
11890 case OMP_CLAUSE_LINEAR:
11891 t = OMP_CLAUSE_DECL (c);
11892 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11893 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11894 {
11895 error_at (OMP_CLAUSE_LOCATION (c),
11896 "linear clause applied to non-integral non-pointer "
11897 "variable with type %qT", TREE_TYPE (t));
11898 remove = true;
11899 break;
11900 }
11901 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11902 {
11903 tree s = OMP_CLAUSE_LINEAR_STEP (c);
11904 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
11905 OMP_CLAUSE_DECL (c), s);
11906 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11907 sizetype, s, OMP_CLAUSE_DECL (c));
11908 if (s == error_mark_node)
11909 s = size_one_node;
11910 OMP_CLAUSE_LINEAR_STEP (c) = s;
11911 }
11912 goto check_dup_generic;
11913
11914 check_dup_generic:
11915 t = OMP_CLAUSE_DECL (c);
11916 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11917 {
11918 error_at (OMP_CLAUSE_LOCATION (c),
11919 "%qE is not a variable in clause %qs", t,
11920 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11921 remove = true;
11922 }
11923 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11924 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
11925 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11926 {
11927 error_at (OMP_CLAUSE_LOCATION (c),
11928 "%qE appears more than once in data clauses", t);
11929 remove = true;
11930 }
11931 else
11932 bitmap_set_bit (&generic_head, DECL_UID (t));
11933 break;
11934
11935 case OMP_CLAUSE_FIRSTPRIVATE:
11936 t = OMP_CLAUSE_DECL (c);
11937 need_complete = true;
11938 need_implicitly_determined = true;
11939 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11940 {
11941 error_at (OMP_CLAUSE_LOCATION (c),
11942 "%qE is not a variable in clause %<firstprivate%>", t);
11943 remove = true;
11944 }
11945 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11946 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
11947 {
11948 error_at (OMP_CLAUSE_LOCATION (c),
11949 "%qE appears more than once in data clauses", t);
11950 remove = true;
11951 }
11952 else
11953 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
11954 break;
11955
11956 case OMP_CLAUSE_LASTPRIVATE:
11957 t = OMP_CLAUSE_DECL (c);
11958 need_complete = true;
11959 need_implicitly_determined = true;
11960 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11961 {
11962 error_at (OMP_CLAUSE_LOCATION (c),
11963 "%qE is not a variable in clause %<lastprivate%>", t);
11964 remove = true;
11965 }
11966 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11967 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11968 {
11969 error_at (OMP_CLAUSE_LOCATION (c),
11970 "%qE appears more than once in data clauses", t);
11971 remove = true;
11972 }
11973 else
11974 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
11975 break;
11976
11977 case OMP_CLAUSE_ALIGNED:
11978 t = OMP_CLAUSE_DECL (c);
11979 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11980 {
11981 error_at (OMP_CLAUSE_LOCATION (c),
11982 "%qE is not a variable in %<aligned%> clause", t);
11983 remove = true;
11984 }
11985 else if (!POINTER_TYPE_P (TREE_TYPE (t))
11986 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
11987 {
11988 error_at (OMP_CLAUSE_LOCATION (c),
11989 "%qE in %<aligned%> clause is neither a pointer nor "
11990 "an array", t);
11991 remove = true;
11992 }
11993 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
11994 {
11995 error_at (OMP_CLAUSE_LOCATION (c),
11996 "%qE appears more than once in %<aligned%> clauses",
11997 t);
11998 remove = true;
11999 }
12000 else
12001 bitmap_set_bit (&aligned_head, DECL_UID (t));
12002 break;
12003
12004 case OMP_CLAUSE_DEPEND:
12005 t = OMP_CLAUSE_DECL (c);
12006 if (TREE_CODE (t) == TREE_LIST)
12007 {
12008 if (handle_omp_array_sections (c))
12009 remove = true;
12010 break;
12011 }
12012 if (t == error_mark_node)
12013 remove = true;
12014 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12015 {
12016 error_at (OMP_CLAUSE_LOCATION (c),
12017 "%qE is not a variable in %<depend%> clause", t);
12018 remove = true;
12019 }
12020 else if (!c_mark_addressable (t))
12021 remove = true;
12022 break;
12023
12024 case OMP_CLAUSE_MAP:
12025 case OMP_CLAUSE_TO:
12026 case OMP_CLAUSE_FROM:
12027 t = OMP_CLAUSE_DECL (c);
12028 if (TREE_CODE (t) == TREE_LIST)
12029 {
12030 if (handle_omp_array_sections (c))
12031 remove = true;
12032 else
12033 {
12034 t = OMP_CLAUSE_DECL (c);
12035 if (!COMPLETE_TYPE_P (TREE_TYPE (t)))
12036 {
12037 error_at (OMP_CLAUSE_LOCATION (c),
12038 "array section does not have mappable type "
12039 "in %qs clause",
12040 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12041 remove = true;
12042 }
12043 }
12044 break;
12045 }
12046 if (t == error_mark_node)
12047 remove = true;
12048 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12049 {
12050 error_at (OMP_CLAUSE_LOCATION (c),
12051 "%qE is not a variable in %qs clause", t,
12052 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12053 remove = true;
12054 }
12055 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12056 {
12057 error_at (OMP_CLAUSE_LOCATION (c),
12058 "%qD is threadprivate variable in %qs clause", t,
12059 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12060 remove = true;
12061 }
12062 else if (!c_mark_addressable (t))
12063 remove = true;
12064 else if (!COMPLETE_TYPE_P (TREE_TYPE (t))
12065 && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12066 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER))
12067 {
12068 error_at (OMP_CLAUSE_LOCATION (c),
12069 "%qD does not have a mappable type in %qs clause", t,
12070 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12071 remove = true;
12072 }
12073 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12074 {
12075 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12076 error ("%qD appears more than once in motion clauses", t);
12077 else
12078 error ("%qD appears more than once in map clauses", t);
12079 remove = true;
12080 }
12081 else
12082 bitmap_set_bit (&generic_head, DECL_UID (t));
12083 break;
12084
12085 case OMP_CLAUSE_UNIFORM:
12086 t = OMP_CLAUSE_DECL (c);
12087 if (TREE_CODE (t) != PARM_DECL)
12088 {
12089 if (DECL_P (t))
12090 error_at (OMP_CLAUSE_LOCATION (c),
12091 "%qD is not an argument in %<uniform%> clause", t);
12092 else
12093 error_at (OMP_CLAUSE_LOCATION (c),
12094 "%qE is not an argument in %<uniform%> clause", t);
12095 remove = true;
12096 break;
12097 }
12098 goto check_dup_generic;
12099
12100 case OMP_CLAUSE_NOWAIT:
12101 if (copyprivate_seen)
12102 {
12103 error_at (OMP_CLAUSE_LOCATION (c),
12104 "%<nowait%> clause must not be used together "
12105 "with %<copyprivate%>");
12106 remove = true;
12107 break;
12108 }
12109 nowait_clause = pc;
12110 pc = &OMP_CLAUSE_CHAIN (c);
12111 continue;
12112
12113 case OMP_CLAUSE_IF:
12114 case OMP_CLAUSE_NUM_THREADS:
12115 case OMP_CLAUSE_NUM_TEAMS:
12116 case OMP_CLAUSE_THREAD_LIMIT:
12117 case OMP_CLAUSE_SCHEDULE:
12118 case OMP_CLAUSE_ORDERED:
12119 case OMP_CLAUSE_DEFAULT:
12120 case OMP_CLAUSE_UNTIED:
12121 case OMP_CLAUSE_COLLAPSE:
12122 case OMP_CLAUSE_FINAL:
12123 case OMP_CLAUSE_MERGEABLE:
12124 case OMP_CLAUSE_SAFELEN:
12125 case OMP_CLAUSE_SIMDLEN:
12126 case OMP_CLAUSE_DEVICE:
12127 case OMP_CLAUSE_DIST_SCHEDULE:
12128 case OMP_CLAUSE_PARALLEL:
12129 case OMP_CLAUSE_FOR:
12130 case OMP_CLAUSE_SECTIONS:
12131 case OMP_CLAUSE_TASKGROUP:
12132 case OMP_CLAUSE_PROC_BIND:
12133 pc = &OMP_CLAUSE_CHAIN (c);
12134 continue;
12135
12136 case OMP_CLAUSE_INBRANCH:
12137 case OMP_CLAUSE_NOTINBRANCH:
12138 if (branch_seen)
12139 {
12140 error_at (OMP_CLAUSE_LOCATION (c),
12141 "%<inbranch%> clause is incompatible with "
12142 "%<notinbranch%>");
12143 remove = true;
12144 break;
12145 }
12146 branch_seen = true;
12147 pc = &OMP_CLAUSE_CHAIN (c);
12148 continue;
12149
12150 default:
12151 gcc_unreachable ();
12152 }
12153
12154 if (!remove)
12155 {
12156 t = OMP_CLAUSE_DECL (c);
12157
12158 if (need_complete)
12159 {
12160 t = require_complete_type (t);
12161 if (t == error_mark_node)
12162 remove = true;
12163 }
12164
12165 if (need_implicitly_determined)
12166 {
12167 const char *share_name = NULL;
12168
12169 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12170 share_name = "threadprivate";
12171 else switch (c_omp_predetermined_sharing (t))
12172 {
12173 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12174 break;
12175 case OMP_CLAUSE_DEFAULT_SHARED:
12176 /* const vars may be specified in firstprivate clause. */
12177 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12178 && TREE_READONLY (t))
12179 break;
12180 share_name = "shared";
12181 break;
12182 case OMP_CLAUSE_DEFAULT_PRIVATE:
12183 share_name = "private";
12184 break;
12185 default:
12186 gcc_unreachable ();
12187 }
12188 if (share_name)
12189 {
12190 error_at (OMP_CLAUSE_LOCATION (c),
12191 "%qE is predetermined %qs for %qs",
12192 t, share_name,
12193 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12194 remove = true;
12195 }
12196 }
12197 }
12198
12199 if (remove)
12200 *pc = OMP_CLAUSE_CHAIN (c);
12201 else
12202 pc = &OMP_CLAUSE_CHAIN (c);
12203 }
12204
12205 bitmap_obstack_release (NULL);
12206 return clauses;
12207 }
12208
12209 /* Create a transaction node. */
12210
12211 tree
12212 c_finish_transaction (location_t loc, tree block, int flags)
12213 {
12214 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12215 if (flags & TM_STMT_ATTR_OUTER)
12216 TRANSACTION_EXPR_OUTER (stmt) = 1;
12217 if (flags & TM_STMT_ATTR_RELAXED)
12218 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12219 return add_stmt (stmt);
12220 }
12221
12222 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12223 down to the element type of an array. */
12224
12225 tree
12226 c_build_qualified_type (tree type, int type_quals)
12227 {
12228 if (type == error_mark_node)
12229 return type;
12230
12231 if (TREE_CODE (type) == ARRAY_TYPE)
12232 {
12233 tree t;
12234 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12235 type_quals);
12236
12237 /* See if we already have an identically qualified type. */
12238 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12239 {
12240 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12241 && TYPE_NAME (t) == TYPE_NAME (type)
12242 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12243 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12244 TYPE_ATTRIBUTES (type)))
12245 break;
12246 }
12247 if (!t)
12248 {
12249 tree domain = TYPE_DOMAIN (type);
12250
12251 t = build_variant_type_copy (type);
12252 TREE_TYPE (t) = element_type;
12253
12254 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12255 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12256 SET_TYPE_STRUCTURAL_EQUALITY (t);
12257 else if (TYPE_CANONICAL (element_type) != element_type
12258 || (domain && TYPE_CANONICAL (domain) != domain))
12259 {
12260 tree unqualified_canon
12261 = build_array_type (TYPE_CANONICAL (element_type),
12262 domain? TYPE_CANONICAL (domain)
12263 : NULL_TREE);
12264 TYPE_CANONICAL (t)
12265 = c_build_qualified_type (unqualified_canon, type_quals);
12266 }
12267 else
12268 TYPE_CANONICAL (t) = t;
12269 }
12270 return t;
12271 }
12272
12273 /* A restrict-qualified pointer type must be a pointer to object or
12274 incomplete type. Note that the use of POINTER_TYPE_P also allows
12275 REFERENCE_TYPEs, which is appropriate for C++. */
12276 if ((type_quals & TYPE_QUAL_RESTRICT)
12277 && (!POINTER_TYPE_P (type)
12278 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12279 {
12280 error ("invalid use of %<restrict%>");
12281 type_quals &= ~TYPE_QUAL_RESTRICT;
12282 }
12283
12284 return build_qualified_type (type, type_quals);
12285 }
12286
12287 /* Build a VA_ARG_EXPR for the C parser. */
12288
12289 tree
12290 c_build_va_arg (location_t loc, tree expr, tree type)
12291 {
12292 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12293 warning_at (loc, OPT_Wc___compat,
12294 "C++ requires promoted type, not enum type, in %<va_arg%>");
12295 return build_va_arg (loc, expr, type);
12296 }
12297
12298 /* Return truthvalue of whether T1 is the same tree structure as T2.
12299 Return 1 if they are the same. Return 0 if they are different. */
12300
12301 bool
12302 c_tree_equal (tree t1, tree t2)
12303 {
12304 enum tree_code code1, code2;
12305
12306 if (t1 == t2)
12307 return true;
12308 if (!t1 || !t2)
12309 return false;
12310
12311 for (code1 = TREE_CODE (t1);
12312 CONVERT_EXPR_CODE_P (code1)
12313 || code1 == NON_LVALUE_EXPR;
12314 code1 = TREE_CODE (t1))
12315 t1 = TREE_OPERAND (t1, 0);
12316 for (code2 = TREE_CODE (t2);
12317 CONVERT_EXPR_CODE_P (code2)
12318 || code2 == NON_LVALUE_EXPR;
12319 code2 = TREE_CODE (t2))
12320 t2 = TREE_OPERAND (t2, 0);
12321
12322 /* They might have become equal now. */
12323 if (t1 == t2)
12324 return true;
12325
12326 if (code1 != code2)
12327 return false;
12328
12329 switch (code1)
12330 {
12331 case INTEGER_CST:
12332 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
12333 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
12334
12335 case REAL_CST:
12336 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12337
12338 case STRING_CST:
12339 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12340 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12341 TREE_STRING_LENGTH (t1));
12342
12343 case FIXED_CST:
12344 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12345 TREE_FIXED_CST (t2));
12346
12347 case COMPLEX_CST:
12348 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12349 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12350
12351 case VECTOR_CST:
12352 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12353
12354 case CONSTRUCTOR:
12355 /* We need to do this when determining whether or not two
12356 non-type pointer to member function template arguments
12357 are the same. */
12358 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12359 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12360 return false;
12361 {
12362 tree field, value;
12363 unsigned int i;
12364 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12365 {
12366 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12367 if (!c_tree_equal (field, elt2->index)
12368 || !c_tree_equal (value, elt2->value))
12369 return false;
12370 }
12371 }
12372 return true;
12373
12374 case TREE_LIST:
12375 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12376 return false;
12377 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12378 return false;
12379 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12380
12381 case SAVE_EXPR:
12382 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12383
12384 case CALL_EXPR:
12385 {
12386 tree arg1, arg2;
12387 call_expr_arg_iterator iter1, iter2;
12388 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12389 return false;
12390 for (arg1 = first_call_expr_arg (t1, &iter1),
12391 arg2 = first_call_expr_arg (t2, &iter2);
12392 arg1 && arg2;
12393 arg1 = next_call_expr_arg (&iter1),
12394 arg2 = next_call_expr_arg (&iter2))
12395 if (!c_tree_equal (arg1, arg2))
12396 return false;
12397 if (arg1 || arg2)
12398 return false;
12399 return true;
12400 }
12401
12402 case TARGET_EXPR:
12403 {
12404 tree o1 = TREE_OPERAND (t1, 0);
12405 tree o2 = TREE_OPERAND (t2, 0);
12406
12407 /* Special case: if either target is an unallocated VAR_DECL,
12408 it means that it's going to be unified with whatever the
12409 TARGET_EXPR is really supposed to initialize, so treat it
12410 as being equivalent to anything. */
12411 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12412 && !DECL_RTL_SET_P (o1))
12413 /*Nop*/;
12414 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12415 && !DECL_RTL_SET_P (o2))
12416 /*Nop*/;
12417 else if (!c_tree_equal (o1, o2))
12418 return false;
12419
12420 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12421 }
12422
12423 case COMPONENT_REF:
12424 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12425 return false;
12426 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12427
12428 case PARM_DECL:
12429 case VAR_DECL:
12430 case CONST_DECL:
12431 case FIELD_DECL:
12432 case FUNCTION_DECL:
12433 case IDENTIFIER_NODE:
12434 case SSA_NAME:
12435 return false;
12436
12437 case TREE_VEC:
12438 {
12439 unsigned ix;
12440 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12441 return false;
12442 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12443 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12444 TREE_VEC_ELT (t2, ix)))
12445 return false;
12446 return true;
12447 }
12448
12449 default:
12450 break;
12451 }
12452
12453 switch (TREE_CODE_CLASS (code1))
12454 {
12455 case tcc_unary:
12456 case tcc_binary:
12457 case tcc_comparison:
12458 case tcc_expression:
12459 case tcc_vl_exp:
12460 case tcc_reference:
12461 case tcc_statement:
12462 {
12463 int i, n = TREE_OPERAND_LENGTH (t1);
12464
12465 switch (code1)
12466 {
12467 case PREINCREMENT_EXPR:
12468 case PREDECREMENT_EXPR:
12469 case POSTINCREMENT_EXPR:
12470 case POSTDECREMENT_EXPR:
12471 n = 1;
12472 break;
12473 case ARRAY_REF:
12474 n = 2;
12475 break;
12476 default:
12477 break;
12478 }
12479
12480 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12481 && n != TREE_OPERAND_LENGTH (t2))
12482 return false;
12483
12484 for (i = 0; i < n; ++i)
12485 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12486 return false;
12487
12488 return true;
12489 }
12490
12491 case tcc_type:
12492 return comptypes (t1, t2);
12493 default:
12494 gcc_unreachable ();
12495 }
12496 /* We can get here with --disable-checking. */
12497 return false;
12498 }
12499
12500 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12501 spawn-helper and BODY is the newly created body for FNDECL. */
12502
12503 void
12504 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12505 {
12506 tree list = alloc_stmt_list ();
12507 tree frame = make_cilk_frame (fndecl);
12508 tree dtor = create_cilk_function_exit (frame, false, true);
12509 add_local_decl (cfun, frame);
12510
12511 DECL_SAVED_TREE (fndecl) = list;
12512 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12513 frame);
12514 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12515 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12516
12517 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12518 append_to_statement_list (detach_expr, &body_list);
12519
12520 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12521 body = fold_build_cleanup_point_expr (void_type_node, body);
12522
12523 append_to_statement_list (body, &body_list);
12524 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12525 body_list, dtor), &list);
12526 }
This page took 0.61288 seconds and 6 git commands to generate.