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