re PR c/89525 (inform messages from -Wbuiltin-declaration-mismatch even with -w)
[gcc.git] / gcc / c / c-typeck.c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2019 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 "memmodel.h"
30 #include "target.h"
31 #include "function.h"
32 #include "bitmap.h"
33 #include "c-tree.h"
34 #include "gimple-expr.h"
35 #include "predict.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "langhooks.h"
41 #include "c-lang.h"
42 #include "intl.h"
43 #include "tree-iterator.h"
44 #include "gimplify.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "asan.h"
55
56 /* Possible cases of implicit bad conversions. Used to select
57 diagnostic messages in convert_for_assignment. */
58 enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_return
63 };
64
65 /* The level of nesting inside "__alignof__". */
66 int in_alignof;
67
68 /* The level of nesting inside "sizeof". */
69 int in_sizeof;
70
71 /* The level of nesting inside "typeof". */
72 int in_typeof;
73
74 /* The argument of last parsed sizeof expression, only to be tested
75 if expr.original_code == SIZEOF_EXPR. */
76 tree c_last_sizeof_arg;
77 location_t c_last_sizeof_loc;
78
79 /* Nonzero if we might need to print a "missing braces around
80 initializer" message within this initializer. */
81 static int found_missing_braces;
82
83 static int require_constant_value;
84 static int require_constant_elements;
85
86 static bool null_pointer_constant_p (const_tree);
87 static tree qualify_type (tree, tree);
88 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90 static int comp_target_types (location_t, tree, tree);
91 static int function_types_compatible_p (const_tree, const_tree, bool *,
92 bool *);
93 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
94 static tree lookup_field (tree, tree);
95 static int convert_arguments (location_t, vec<location_t>, tree,
96 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
97 tree);
98 static tree pointer_diff (location_t, tree, tree, tree *);
99 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
100 enum impl_conv, bool, tree, tree, int,
101 int = 0);
102 static tree valid_compound_expr_initializer (tree, tree);
103 static void push_string (const char *);
104 static void push_member_name (tree);
105 static int spelling_length (void);
106 static char *print_spelling (char *);
107 static void warning_init (location_t, int, const char *);
108 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
109 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
110 bool, struct obstack *);
111 static void output_pending_init_elements (int, struct obstack *);
112 static bool set_designator (location_t, bool, struct obstack *);
113 static void push_range_stack (tree, struct obstack *);
114 static void add_pending_init (location_t, tree, tree, tree, bool,
115 struct obstack *);
116 static void set_nonincremental_init (struct obstack *);
117 static void set_nonincremental_init_from_string (tree, struct obstack *);
118 static tree find_init_member (tree, struct obstack *);
119 static void readonly_warning (tree, enum lvalue_use);
120 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
121 static void record_maybe_used_decl (tree);
122 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
123 \f
124 /* Return true if EXP is a null pointer constant, false otherwise. */
125
126 static bool
127 null_pointer_constant_p (const_tree expr)
128 {
129 /* This should really operate on c_expr structures, but they aren't
130 yet available everywhere required. */
131 tree type = TREE_TYPE (expr);
132 return (TREE_CODE (expr) == INTEGER_CST
133 && !TREE_OVERFLOW (expr)
134 && integer_zerop (expr)
135 && (INTEGRAL_TYPE_P (type)
136 || (TREE_CODE (type) == POINTER_TYPE
137 && VOID_TYPE_P (TREE_TYPE (type))
138 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
139 }
140
141 /* EXPR may appear in an unevaluated part of an integer constant
142 expression, but not in an evaluated part. Wrap it in a
143 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
144 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
145
146 static tree
147 note_integer_operands (tree expr)
148 {
149 tree ret;
150 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
151 {
152 ret = copy_node (expr);
153 TREE_OVERFLOW (ret) = 1;
154 }
155 else
156 {
157 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
158 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
159 }
160 return ret;
161 }
162
163 /* Having checked whether EXPR may appear in an unevaluated part of an
164 integer constant expression and found that it may, remove any
165 C_MAYBE_CONST_EXPR noting this fact and return the resulting
166 expression. */
167
168 static inline tree
169 remove_c_maybe_const_expr (tree expr)
170 {
171 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
172 return C_MAYBE_CONST_EXPR_EXPR (expr);
173 else
174 return expr;
175 }
176
177 \f/* This is a cache to hold if two types are compatible or not. */
178
179 struct tagged_tu_seen_cache {
180 const struct tagged_tu_seen_cache * next;
181 const_tree t1;
182 const_tree t2;
183 /* The return value of tagged_types_tu_compatible_p if we had seen
184 these two types already. */
185 int val;
186 };
187
188 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
189 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
190
191 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
192 does not have an incomplete type. (That includes void types.)
193 LOC is the location of the use. */
194
195 tree
196 require_complete_type (location_t loc, tree value)
197 {
198 tree type = TREE_TYPE (value);
199
200 if (error_operand_p (value))
201 return error_mark_node;
202
203 /* First, detect a valid value with a complete type. */
204 if (COMPLETE_TYPE_P (type))
205 return value;
206
207 c_incomplete_type_error (loc, value, type);
208 return error_mark_node;
209 }
210
211 /* Print an error message for invalid use of an incomplete type.
212 VALUE is the expression that was used (or 0 if that isn't known)
213 and TYPE is the type that was invalid. LOC is the location for
214 the error. */
215
216 void
217 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
218 {
219 /* Avoid duplicate error message. */
220 if (TREE_CODE (type) == ERROR_MARK)
221 return;
222
223 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
224 error_at (loc, "%qD has an incomplete type %qT", value, type);
225 else
226 {
227 retry:
228 /* We must print an error message. Be clever about what it says. */
229
230 switch (TREE_CODE (type))
231 {
232 case RECORD_TYPE:
233 case UNION_TYPE:
234 case ENUMERAL_TYPE:
235 break;
236
237 case VOID_TYPE:
238 error_at (loc, "invalid use of void expression");
239 return;
240
241 case ARRAY_TYPE:
242 if (TYPE_DOMAIN (type))
243 {
244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 {
246 error_at (loc, "invalid use of flexible array member");
247 return;
248 }
249 type = TREE_TYPE (type);
250 goto retry;
251 }
252 error_at (loc, "invalid use of array with unspecified bounds");
253 return;
254
255 default:
256 gcc_unreachable ();
257 }
258
259 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
260 error_at (loc, "invalid use of undefined type %qT", type);
261 else
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
263 error_at (loc, "invalid use of incomplete typedef %qT", type);
264 }
265 }
266
267 /* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
269
270 tree
271 c_type_promotes_to (tree type)
272 {
273 tree ret = NULL_TREE;
274
275 if (TYPE_MAIN_VARIANT (type) == float_type_node)
276 ret = double_type_node;
277 else if (c_promoting_integer_type_p (type))
278 {
279 /* Preserve unsignedness if not really getting any wider. */
280 if (TYPE_UNSIGNED (type)
281 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
282 ret = unsigned_type_node;
283 else
284 ret = integer_type_node;
285 }
286
287 if (ret != NULL_TREE)
288 return (TYPE_ATOMIC (type)
289 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
290 : ret);
291
292 return type;
293 }
294
295 /* Return true if between two named address spaces, whether there is a superset
296 named address space that encompasses both address spaces. If there is a
297 superset, return which address space is the superset. */
298
299 static bool
300 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
301 {
302 if (as1 == as2)
303 {
304 *common = as1;
305 return true;
306 }
307 else if (targetm.addr_space.subset_p (as1, as2))
308 {
309 *common = as2;
310 return true;
311 }
312 else if (targetm.addr_space.subset_p (as2, as1))
313 {
314 *common = as1;
315 return true;
316 }
317 else
318 return false;
319 }
320
321 /* Return a variant of TYPE which has all the type qualifiers of LIKE
322 as well as those of TYPE. */
323
324 static tree
325 qualify_type (tree type, tree like)
326 {
327 addr_space_t as_type = TYPE_ADDR_SPACE (type);
328 addr_space_t as_like = TYPE_ADDR_SPACE (like);
329 addr_space_t as_common;
330
331 /* If the two named address spaces are different, determine the common
332 superset address space. If there isn't one, raise an error. */
333 if (!addr_space_superset (as_type, as_like, &as_common))
334 {
335 as_common = as_type;
336 error ("%qT and %qT are in disjoint named address spaces",
337 type, like);
338 }
339
340 return c_build_qualified_type (type,
341 TYPE_QUALS_NO_ADDR_SPACE (type)
342 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
343 | ENCODE_QUAL_ADDR_SPACE (as_common));
344 }
345
346 /* Return true iff the given tree T is a variable length array. */
347
348 bool
349 c_vla_type_p (const_tree t)
350 {
351 if (TREE_CODE (t) == ARRAY_TYPE
352 && C_TYPE_VARIABLE_SIZE (t))
353 return true;
354 return false;
355 }
356 \f
357 /* Return the composite type of two compatible types.
358
359 We assume that comptypes has already been done and returned
360 nonzero; if that isn't so, this may crash. In particular, we
361 assume that qualifiers match. */
362
363 tree
364 composite_type (tree t1, tree t2)
365 {
366 enum tree_code code1;
367 enum tree_code code2;
368 tree attributes;
369
370 /* Save time if the two types are the same. */
371
372 if (t1 == t2) return t1;
373
374 /* If one type is nonsense, use the other. */
375 if (t1 == error_mark_node)
376 return t2;
377 if (t2 == error_mark_node)
378 return t1;
379
380 code1 = TREE_CODE (t1);
381 code2 = TREE_CODE (t2);
382
383 /* Merge the attributes. */
384 attributes = targetm.merge_type_attributes (t1, t2);
385
386 /* If one is an enumerated type and the other is the compatible
387 integer type, the composite type might be either of the two
388 (DR#013 question 3). For consistency, use the enumerated type as
389 the composite type. */
390
391 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
392 return t1;
393 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
394 return t2;
395
396 gcc_assert (code1 == code2);
397
398 switch (code1)
399 {
400 case POINTER_TYPE:
401 /* For two pointers, do this recursively on the target type. */
402 {
403 tree pointed_to_1 = TREE_TYPE (t1);
404 tree pointed_to_2 = TREE_TYPE (t2);
405 tree target = composite_type (pointed_to_1, pointed_to_2);
406 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
407 t1 = build_type_attribute_variant (t1, attributes);
408 return qualify_type (t1, t2);
409 }
410
411 case ARRAY_TYPE:
412 {
413 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
414 int quals;
415 tree unqual_elt;
416 tree d1 = TYPE_DOMAIN (t1);
417 tree d2 = TYPE_DOMAIN (t2);
418 bool d1_variable, d2_variable;
419 bool d1_zero, d2_zero;
420 bool t1_complete, t2_complete;
421
422 /* We should not have any type quals on arrays at all. */
423 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
424 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
425
426 t1_complete = COMPLETE_TYPE_P (t1);
427 t2_complete = COMPLETE_TYPE_P (t2);
428
429 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
430 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
431
432 d1_variable = (!d1_zero
433 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
434 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
435 d2_variable = (!d2_zero
436 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
437 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
438 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
439 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
440
441 /* Save space: see if the result is identical to one of the args. */
442 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
443 && (d2_variable || d2_zero || !d1_variable))
444 return build_type_attribute_variant (t1, attributes);
445 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
446 && (d1_variable || d1_zero || !d2_variable))
447 return build_type_attribute_variant (t2, attributes);
448
449 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
450 return build_type_attribute_variant (t1, attributes);
451 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
452 return build_type_attribute_variant (t2, attributes);
453
454 /* Merge the element types, and have a size if either arg has
455 one. We may have qualifiers on the element types. To set
456 up TYPE_MAIN_VARIANT correctly, we need to form the
457 composite of the unqualified types and add the qualifiers
458 back at the end. */
459 quals = TYPE_QUALS (strip_array_types (elt));
460 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
461 t1 = build_array_type (unqual_elt,
462 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
463 && (d2_variable
464 || d2_zero
465 || !d1_variable))
466 ? t1
467 : t2));
468 /* Ensure a composite type involving a zero-length array type
469 is a zero-length type not an incomplete type. */
470 if (d1_zero && d2_zero
471 && (t1_complete || t2_complete)
472 && !COMPLETE_TYPE_P (t1))
473 {
474 TYPE_SIZE (t1) = bitsize_zero_node;
475 TYPE_SIZE_UNIT (t1) = size_zero_node;
476 }
477 t1 = c_build_qualified_type (t1, quals);
478 return build_type_attribute_variant (t1, attributes);
479 }
480
481 case ENUMERAL_TYPE:
482 case RECORD_TYPE:
483 case UNION_TYPE:
484 if (attributes != NULL)
485 {
486 /* Try harder not to create a new aggregate type. */
487 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
488 return t1;
489 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
490 return t2;
491 }
492 return build_type_attribute_variant (t1, attributes);
493
494 case FUNCTION_TYPE:
495 /* Function types: prefer the one that specified arg types.
496 If both do, merge the arg types. Also merge the return types. */
497 {
498 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
499 tree p1 = TYPE_ARG_TYPES (t1);
500 tree p2 = TYPE_ARG_TYPES (t2);
501 int len;
502 tree newargs, n;
503 int i;
504
505 /* Save space: see if the result is identical to one of the args. */
506 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
507 return build_type_attribute_variant (t1, attributes);
508 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
509 return build_type_attribute_variant (t2, attributes);
510
511 /* Simple way if one arg fails to specify argument types. */
512 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
513 {
514 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
515 t1 = build_type_attribute_variant (t1, attributes);
516 return qualify_type (t1, t2);
517 }
518 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
519 {
520 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
521 t1 = build_type_attribute_variant (t1, attributes);
522 return qualify_type (t1, t2);
523 }
524
525 /* If both args specify argument types, we must merge the two
526 lists, argument by argument. */
527
528 for (len = 0, newargs = p1;
529 newargs && newargs != void_list_node;
530 len++, newargs = TREE_CHAIN (newargs))
531 ;
532
533 for (i = 0; i < len; i++)
534 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
535
536 n = newargs;
537
538 for (; p1 && p1 != void_list_node;
539 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
540 {
541 /* A null type means arg type is not specified.
542 Take whatever the other function type has. */
543 if (TREE_VALUE (p1) == NULL_TREE)
544 {
545 TREE_VALUE (n) = TREE_VALUE (p2);
546 goto parm_done;
547 }
548 if (TREE_VALUE (p2) == NULL_TREE)
549 {
550 TREE_VALUE (n) = TREE_VALUE (p1);
551 goto parm_done;
552 }
553
554 /* Given wait (union {union wait *u; int *i} *)
555 and wait (union wait *),
556 prefer union wait * as type of parm. */
557 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
558 && TREE_VALUE (p1) != TREE_VALUE (p2))
559 {
560 tree memb;
561 tree mv2 = TREE_VALUE (p2);
562 if (mv2 && mv2 != error_mark_node
563 && TREE_CODE (mv2) != ARRAY_TYPE)
564 mv2 = TYPE_MAIN_VARIANT (mv2);
565 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
566 memb; memb = DECL_CHAIN (memb))
567 {
568 tree mv3 = TREE_TYPE (memb);
569 if (mv3 && mv3 != error_mark_node
570 && TREE_CODE (mv3) != ARRAY_TYPE)
571 mv3 = TYPE_MAIN_VARIANT (mv3);
572 if (comptypes (mv3, mv2))
573 {
574 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
575 TREE_VALUE (p2));
576 pedwarn (input_location, OPT_Wpedantic,
577 "function types not truly compatible in ISO C");
578 goto parm_done;
579 }
580 }
581 }
582 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
583 && TREE_VALUE (p2) != TREE_VALUE (p1))
584 {
585 tree memb;
586 tree mv1 = TREE_VALUE (p1);
587 if (mv1 && mv1 != error_mark_node
588 && TREE_CODE (mv1) != ARRAY_TYPE)
589 mv1 = TYPE_MAIN_VARIANT (mv1);
590 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
591 memb; memb = DECL_CHAIN (memb))
592 {
593 tree mv3 = TREE_TYPE (memb);
594 if (mv3 && mv3 != error_mark_node
595 && TREE_CODE (mv3) != ARRAY_TYPE)
596 mv3 = TYPE_MAIN_VARIANT (mv3);
597 if (comptypes (mv3, mv1))
598 {
599 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
600 TREE_VALUE (p1));
601 pedwarn (input_location, OPT_Wpedantic,
602 "function types not truly compatible in ISO C");
603 goto parm_done;
604 }
605 }
606 }
607 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
608 parm_done: ;
609 }
610
611 t1 = build_function_type (valtype, newargs);
612 t1 = qualify_type (t1, t2);
613 }
614 /* FALLTHRU */
615
616 default:
617 return build_type_attribute_variant (t1, attributes);
618 }
619
620 }
621
622 /* Return the type of a conditional expression between pointers to
623 possibly differently qualified versions of compatible types.
624
625 We assume that comp_target_types has already been done and returned
626 nonzero; if that isn't so, this may crash. */
627
628 static tree
629 common_pointer_type (tree t1, tree t2)
630 {
631 tree attributes;
632 tree pointed_to_1, mv1;
633 tree pointed_to_2, mv2;
634 tree target;
635 unsigned target_quals;
636 addr_space_t as1, as2, as_common;
637 int quals1, quals2;
638
639 /* Save time if the two types are the same. */
640
641 if (t1 == t2) return t1;
642
643 /* If one type is nonsense, use the other. */
644 if (t1 == error_mark_node)
645 return t2;
646 if (t2 == error_mark_node)
647 return t1;
648
649 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
650 && TREE_CODE (t2) == POINTER_TYPE);
651
652 /* Merge the attributes. */
653 attributes = targetm.merge_type_attributes (t1, t2);
654
655 /* Find the composite type of the target types, and combine the
656 qualifiers of the two types' targets. Do not lose qualifiers on
657 array element types by taking the TYPE_MAIN_VARIANT. */
658 mv1 = pointed_to_1 = TREE_TYPE (t1);
659 mv2 = pointed_to_2 = TREE_TYPE (t2);
660 if (TREE_CODE (mv1) != ARRAY_TYPE)
661 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
662 if (TREE_CODE (mv2) != ARRAY_TYPE)
663 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
664 target = composite_type (mv1, mv2);
665
666 /* Strip array types to get correct qualifier for pointers to arrays */
667 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
668 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
669
670 /* For function types do not merge const qualifiers, but drop them
671 if used inconsistently. The middle-end uses these to mark const
672 and noreturn functions. */
673 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
674 target_quals = (quals1 & quals2);
675 else
676 target_quals = (quals1 | quals2);
677
678 /* If the two named address spaces are different, determine the common
679 superset address space. This is guaranteed to exist due to the
680 assumption that comp_target_type returned non-zero. */
681 as1 = TYPE_ADDR_SPACE (pointed_to_1);
682 as2 = TYPE_ADDR_SPACE (pointed_to_2);
683 if (!addr_space_superset (as1, as2, &as_common))
684 gcc_unreachable ();
685
686 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
687
688 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
689 return build_type_attribute_variant (t1, attributes);
690 }
691
692 /* Return the common type for two arithmetic types under the usual
693 arithmetic conversions. The default conversions have already been
694 applied, and enumerated types converted to their compatible integer
695 types. The resulting type is unqualified and has no attributes.
696
697 This is the type for the result of most arithmetic operations
698 if the operands have the given two types. */
699
700 static tree
701 c_common_type (tree t1, tree t2)
702 {
703 enum tree_code code1;
704 enum tree_code code2;
705
706 /* If one type is nonsense, use the other. */
707 if (t1 == error_mark_node)
708 return t2;
709 if (t2 == error_mark_node)
710 return t1;
711
712 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
713 t1 = TYPE_MAIN_VARIANT (t1);
714
715 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
716 t2 = TYPE_MAIN_VARIANT (t2);
717
718 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
719 t1 = build_type_attribute_variant (t1, NULL_TREE);
720
721 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
722 t2 = build_type_attribute_variant (t2, NULL_TREE);
723
724 /* Save time if the two types are the same. */
725
726 if (t1 == t2) return t1;
727
728 code1 = TREE_CODE (t1);
729 code2 = TREE_CODE (t2);
730
731 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
732 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
733 || code1 == INTEGER_TYPE);
734 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
735 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
736 || code2 == INTEGER_TYPE);
737
738 /* When one operand is a decimal float type, the other operand cannot be
739 a generic float type or a complex type. We also disallow vector types
740 here. */
741 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
742 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
743 {
744 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
745 {
746 error ("can%'t mix operands of decimal float and vector types");
747 return error_mark_node;
748 }
749 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
750 {
751 error ("can%'t mix operands of decimal float and complex types");
752 return error_mark_node;
753 }
754 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
755 {
756 error ("can%'t mix operands of decimal float and other float types");
757 return error_mark_node;
758 }
759 }
760
761 /* If one type is a vector type, return that type. (How the usual
762 arithmetic conversions apply to the vector types extension is not
763 precisely specified.) */
764 if (code1 == VECTOR_TYPE)
765 return t1;
766
767 if (code2 == VECTOR_TYPE)
768 return t2;
769
770 /* If one type is complex, form the common type of the non-complex
771 components, then make that complex. Use T1 or T2 if it is the
772 required type. */
773 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
774 {
775 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
776 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
777 tree subtype = c_common_type (subtype1, subtype2);
778
779 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
780 return t1;
781 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
782 return t2;
783 else
784 return build_complex_type (subtype);
785 }
786
787 /* If only one is real, use it as the result. */
788
789 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
790 return t1;
791
792 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
793 return t2;
794
795 /* If both are real and either are decimal floating point types, use
796 the decimal floating point type with the greater precision. */
797
798 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
799 {
800 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
801 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
802 return dfloat128_type_node;
803 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
804 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
805 return dfloat64_type_node;
806 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
807 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
808 return dfloat32_type_node;
809 }
810
811 /* Deal with fixed-point types. */
812 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
813 {
814 unsigned int unsignedp = 0, satp = 0;
815 scalar_mode m1, m2;
816 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
817
818 m1 = SCALAR_TYPE_MODE (t1);
819 m2 = SCALAR_TYPE_MODE (t2);
820
821 /* If one input type is saturating, the result type is saturating. */
822 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
823 satp = 1;
824
825 /* If both fixed-point types are unsigned, the result type is unsigned.
826 When mixing fixed-point and integer types, follow the sign of the
827 fixed-point type.
828 Otherwise, the result type is signed. */
829 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
830 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
831 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
832 && TYPE_UNSIGNED (t1))
833 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
834 && TYPE_UNSIGNED (t2)))
835 unsignedp = 1;
836
837 /* The result type is signed. */
838 if (unsignedp == 0)
839 {
840 /* If the input type is unsigned, we need to convert to the
841 signed type. */
842 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
843 {
844 enum mode_class mclass = (enum mode_class) 0;
845 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
846 mclass = MODE_FRACT;
847 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
848 mclass = MODE_ACCUM;
849 else
850 gcc_unreachable ();
851 m1 = as_a <scalar_mode>
852 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
853 }
854 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
855 {
856 enum mode_class mclass = (enum mode_class) 0;
857 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
858 mclass = MODE_FRACT;
859 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
860 mclass = MODE_ACCUM;
861 else
862 gcc_unreachable ();
863 m2 = as_a <scalar_mode>
864 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
865 }
866 }
867
868 if (code1 == FIXED_POINT_TYPE)
869 {
870 fbit1 = GET_MODE_FBIT (m1);
871 ibit1 = GET_MODE_IBIT (m1);
872 }
873 else
874 {
875 fbit1 = 0;
876 /* Signed integers need to subtract one sign bit. */
877 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
878 }
879
880 if (code2 == FIXED_POINT_TYPE)
881 {
882 fbit2 = GET_MODE_FBIT (m2);
883 ibit2 = GET_MODE_IBIT (m2);
884 }
885 else
886 {
887 fbit2 = 0;
888 /* Signed integers need to subtract one sign bit. */
889 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
890 }
891
892 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
893 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
894 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
895 satp);
896 }
897
898 /* Both real or both integers; use the one with greater precision. */
899
900 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
901 return t1;
902 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
903 return t2;
904
905 /* Same precision. Prefer long longs to longs to ints when the
906 same precision, following the C99 rules on integer type rank
907 (which are equivalent to the C90 rules for C90 types). */
908
909 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
911 return long_long_unsigned_type_node;
912
913 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
914 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
915 {
916 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
917 return long_long_unsigned_type_node;
918 else
919 return long_long_integer_type_node;
920 }
921
922 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
923 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
924 return long_unsigned_type_node;
925
926 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
927 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
928 {
929 /* But preserve unsignedness from the other type,
930 since long cannot hold all the values of an unsigned int. */
931 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
932 return long_unsigned_type_node;
933 else
934 return long_integer_type_node;
935 }
936
937 /* For floating types of the same TYPE_PRECISION (which we here
938 assume means either the same set of values, or sets of values
939 neither a subset of the other, with behavior being undefined in
940 the latter case), follow the rules from TS 18661-3: prefer
941 interchange types _FloatN, then standard types long double,
942 double, float, then extended types _FloatNx. For extended types,
943 check them starting with _Float128x as that seems most consistent
944 in spirit with preferring long double to double; for interchange
945 types, also check in that order for consistency although it's not
946 possible for more than one of them to have the same
947 precision. */
948 tree mv1 = TYPE_MAIN_VARIANT (t1);
949 tree mv2 = TYPE_MAIN_VARIANT (t2);
950
951 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
952 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
953 return FLOATN_TYPE_NODE (i);
954
955 /* Likewise, prefer long double to double even if same size. */
956 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
957 return long_double_type_node;
958
959 /* Likewise, prefer double to float even if same size.
960 We got a couple of embedded targets with 32 bit doubles, and the
961 pdp11 might have 64 bit floats. */
962 if (mv1 == double_type_node || mv2 == double_type_node)
963 return double_type_node;
964
965 if (mv1 == float_type_node || mv2 == float_type_node)
966 return float_type_node;
967
968 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
969 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
970 return FLOATNX_TYPE_NODE (i);
971
972 /* Otherwise prefer the unsigned one. */
973
974 if (TYPE_UNSIGNED (t1))
975 return t1;
976 else
977 return t2;
978 }
979 \f
980 /* Wrapper around c_common_type that is used by c-common.c and other
981 front end optimizations that remove promotions. ENUMERAL_TYPEs
982 are allowed here and are converted to their compatible integer types.
983 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
984 preferably a non-Boolean type as the common type. */
985 tree
986 common_type (tree t1, tree t2)
987 {
988 if (TREE_CODE (t1) == ENUMERAL_TYPE)
989 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
990 if (TREE_CODE (t2) == ENUMERAL_TYPE)
991 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
992
993 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
994 if (TREE_CODE (t1) == BOOLEAN_TYPE
995 && TREE_CODE (t2) == BOOLEAN_TYPE)
996 return boolean_type_node;
997
998 /* If either type is BOOLEAN_TYPE, then return the other. */
999 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1000 return t2;
1001 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1002 return t1;
1003
1004 return c_common_type (t1, t2);
1005 }
1006
1007 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1008 or various other operations. Return 2 if they are compatible
1009 but a warning may be needed if you use them together. */
1010
1011 int
1012 comptypes (tree type1, tree type2)
1013 {
1014 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1015 int val;
1016
1017 val = comptypes_internal (type1, type2, NULL, NULL);
1018 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1019
1020 return val;
1021 }
1022
1023 /* Like comptypes, but if it returns non-zero because enum and int are
1024 compatible, it sets *ENUM_AND_INT_P to true. */
1025
1026 static int
1027 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1028 {
1029 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1030 int val;
1031
1032 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1033 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1034
1035 return val;
1036 }
1037
1038 /* Like comptypes, but if it returns nonzero for different types, it
1039 sets *DIFFERENT_TYPES_P to true. */
1040
1041 int
1042 comptypes_check_different_types (tree type1, tree type2,
1043 bool *different_types_p)
1044 {
1045 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1046 int val;
1047
1048 val = comptypes_internal (type1, type2, NULL, different_types_p);
1049 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1050
1051 return val;
1052 }
1053 \f
1054 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1055 or various other operations. Return 2 if they are compatible
1056 but a warning may be needed if you use them together. If
1057 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1058 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1059 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1060 NULL, and the types are compatible but different enough not to be
1061 permitted in C11 typedef redeclarations, then this sets
1062 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1063 false, but may or may not be set if the types are incompatible.
1064 This differs from comptypes, in that we don't free the seen
1065 types. */
1066
1067 static int
1068 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1069 bool *different_types_p)
1070 {
1071 const_tree t1 = type1;
1072 const_tree t2 = type2;
1073 int attrval, val;
1074
1075 /* Suppress errors caused by previously reported errors. */
1076
1077 if (t1 == t2 || !t1 || !t2
1078 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1079 return 1;
1080
1081 /* Enumerated types are compatible with integer types, but this is
1082 not transitive: two enumerated types in the same translation unit
1083 are compatible with each other only if they are the same type. */
1084
1085 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1086 {
1087 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1088 if (TREE_CODE (t2) != VOID_TYPE)
1089 {
1090 if (enum_and_int_p != NULL)
1091 *enum_and_int_p = true;
1092 if (different_types_p != NULL)
1093 *different_types_p = true;
1094 }
1095 }
1096 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1097 {
1098 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1099 if (TREE_CODE (t1) != VOID_TYPE)
1100 {
1101 if (enum_and_int_p != NULL)
1102 *enum_and_int_p = true;
1103 if (different_types_p != NULL)
1104 *different_types_p = true;
1105 }
1106 }
1107
1108 if (t1 == t2)
1109 return 1;
1110
1111 /* Different classes of types can't be compatible. */
1112
1113 if (TREE_CODE (t1) != TREE_CODE (t2))
1114 return 0;
1115
1116 /* Qualifiers must match. C99 6.7.3p9 */
1117
1118 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1119 return 0;
1120
1121 /* Allow for two different type nodes which have essentially the same
1122 definition. Note that we already checked for equality of the type
1123 qualifiers (just above). */
1124
1125 if (TREE_CODE (t1) != ARRAY_TYPE
1126 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1127 return 1;
1128
1129 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1130 if (!(attrval = comp_type_attributes (t1, t2)))
1131 return 0;
1132
1133 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1134 val = 0;
1135
1136 switch (TREE_CODE (t1))
1137 {
1138 case INTEGER_TYPE:
1139 case FIXED_POINT_TYPE:
1140 case REAL_TYPE:
1141 /* With these nodes, we can't determine type equivalence by
1142 looking at what is stored in the nodes themselves, because
1143 two nodes might have different TYPE_MAIN_VARIANTs but still
1144 represent the same type. For example, wchar_t and int could
1145 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1146 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1147 and are distinct types. On the other hand, int and the
1148 following typedef
1149
1150 typedef int INT __attribute((may_alias));
1151
1152 have identical properties, different TYPE_MAIN_VARIANTs, but
1153 represent the same type. The canonical type system keeps
1154 track of equivalence in this case, so we fall back on it. */
1155 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1156
1157 case POINTER_TYPE:
1158 /* Do not remove mode information. */
1159 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1160 break;
1161 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1162 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1163 enum_and_int_p, different_types_p));
1164 break;
1165
1166 case FUNCTION_TYPE:
1167 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1168 different_types_p);
1169 break;
1170
1171 case ARRAY_TYPE:
1172 {
1173 tree d1 = TYPE_DOMAIN (t1);
1174 tree d2 = TYPE_DOMAIN (t2);
1175 bool d1_variable, d2_variable;
1176 bool d1_zero, d2_zero;
1177 val = 1;
1178
1179 /* Target types must match incl. qualifiers. */
1180 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1181 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1182 enum_and_int_p,
1183 different_types_p)) == 0)
1184 return 0;
1185
1186 if (different_types_p != NULL
1187 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1188 *different_types_p = true;
1189 /* Sizes must match unless one is missing or variable. */
1190 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1191 break;
1192
1193 d1_zero = !TYPE_MAX_VALUE (d1);
1194 d2_zero = !TYPE_MAX_VALUE (d2);
1195
1196 d1_variable = (!d1_zero
1197 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1198 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1199 d2_variable = (!d2_zero
1200 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1201 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1202 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1203 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1204
1205 if (different_types_p != NULL
1206 && d1_variable != d2_variable)
1207 *different_types_p = true;
1208 if (d1_variable || d2_variable)
1209 break;
1210 if (d1_zero && d2_zero)
1211 break;
1212 if (d1_zero || d2_zero
1213 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1214 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1215 val = 0;
1216
1217 break;
1218 }
1219
1220 case ENUMERAL_TYPE:
1221 case RECORD_TYPE:
1222 case UNION_TYPE:
1223 if (val != 1 && !same_translation_unit_p (t1, t2))
1224 {
1225 tree a1 = TYPE_ATTRIBUTES (t1);
1226 tree a2 = TYPE_ATTRIBUTES (t2);
1227
1228 if (! attribute_list_contained (a1, a2)
1229 && ! attribute_list_contained (a2, a1))
1230 break;
1231
1232 if (attrval != 2)
1233 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1234 different_types_p);
1235 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1236 different_types_p);
1237 }
1238 break;
1239
1240 case VECTOR_TYPE:
1241 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1242 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1243 enum_and_int_p, different_types_p));
1244 break;
1245
1246 default:
1247 break;
1248 }
1249 return attrval == 2 && val == 1 ? 2 : val;
1250 }
1251
1252 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1253 their qualifiers, except for named address spaces. If the pointers point to
1254 different named addresses, then we must determine if one address space is a
1255 subset of the other. */
1256
1257 static int
1258 comp_target_types (location_t location, tree ttl, tree ttr)
1259 {
1260 int val;
1261 int val_ped;
1262 tree mvl = TREE_TYPE (ttl);
1263 tree mvr = TREE_TYPE (ttr);
1264 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1265 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1266 addr_space_t as_common;
1267 bool enum_and_int_p;
1268
1269 /* Fail if pointers point to incompatible address spaces. */
1270 if (!addr_space_superset (asl, asr, &as_common))
1271 return 0;
1272
1273 /* For pedantic record result of comptypes on arrays before losing
1274 qualifiers on the element type below. */
1275 val_ped = 1;
1276
1277 if (TREE_CODE (mvl) == ARRAY_TYPE
1278 && TREE_CODE (mvr) == ARRAY_TYPE)
1279 val_ped = comptypes (mvl, mvr);
1280
1281 /* Qualifiers on element types of array types that are
1282 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1283
1284 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1285 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1286 : TYPE_MAIN_VARIANT (mvl));
1287
1288 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1289 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1290 : TYPE_MAIN_VARIANT (mvr));
1291
1292 enum_and_int_p = false;
1293 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1294
1295 if (val == 1 && val_ped != 1)
1296 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1297 "are incompatible in ISO C");
1298
1299 if (val == 2)
1300 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1301
1302 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1303 warning_at (location, OPT_Wc___compat,
1304 "pointer target types incompatible in C++");
1305
1306 return val;
1307 }
1308 \f
1309 /* Subroutines of `comptypes'. */
1310
1311 /* Determine whether two trees derive from the same translation unit.
1312 If the CONTEXT chain ends in a null, that tree's context is still
1313 being parsed, so if two trees have context chains ending in null,
1314 they're in the same translation unit. */
1315
1316 bool
1317 same_translation_unit_p (const_tree t1, const_tree t2)
1318 {
1319 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1320 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1321 {
1322 case tcc_declaration:
1323 t1 = DECL_CONTEXT (t1); break;
1324 case tcc_type:
1325 t1 = TYPE_CONTEXT (t1); break;
1326 case tcc_exceptional:
1327 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1328 default: gcc_unreachable ();
1329 }
1330
1331 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1332 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1333 {
1334 case tcc_declaration:
1335 t2 = DECL_CONTEXT (t2); break;
1336 case tcc_type:
1337 t2 = TYPE_CONTEXT (t2); break;
1338 case tcc_exceptional:
1339 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1340 default: gcc_unreachable ();
1341 }
1342
1343 return t1 == t2;
1344 }
1345
1346 /* Allocate the seen two types, assuming that they are compatible. */
1347
1348 static struct tagged_tu_seen_cache *
1349 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1350 {
1351 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1352 tu->next = tagged_tu_seen_base;
1353 tu->t1 = t1;
1354 tu->t2 = t2;
1355
1356 tagged_tu_seen_base = tu;
1357
1358 /* The C standard says that two structures in different translation
1359 units are compatible with each other only if the types of their
1360 fields are compatible (among other things). We assume that they
1361 are compatible until proven otherwise when building the cache.
1362 An example where this can occur is:
1363 struct a
1364 {
1365 struct a *next;
1366 };
1367 If we are comparing this against a similar struct in another TU,
1368 and did not assume they were compatible, we end up with an infinite
1369 loop. */
1370 tu->val = 1;
1371 return tu;
1372 }
1373
1374 /* Free the seen types until we get to TU_TIL. */
1375
1376 static void
1377 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1378 {
1379 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1380 while (tu != tu_til)
1381 {
1382 const struct tagged_tu_seen_cache *const tu1
1383 = (const struct tagged_tu_seen_cache *) tu;
1384 tu = tu1->next;
1385 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1386 }
1387 tagged_tu_seen_base = tu_til;
1388 }
1389
1390 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1391 compatible. If the two types are not the same (which has been
1392 checked earlier), this can only happen when multiple translation
1393 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1394 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1395 comptypes_internal. */
1396
1397 static int
1398 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1399 bool *enum_and_int_p, bool *different_types_p)
1400 {
1401 tree s1, s2;
1402 bool needs_warning = false;
1403
1404 /* We have to verify that the tags of the types are the same. This
1405 is harder than it looks because this may be a typedef, so we have
1406 to go look at the original type. It may even be a typedef of a
1407 typedef...
1408 In the case of compiler-created builtin structs the TYPE_DECL
1409 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1410 while (TYPE_NAME (t1)
1411 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1412 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1413 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1414
1415 while (TYPE_NAME (t2)
1416 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1417 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1418 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1419
1420 /* C90 didn't have the requirement that the two tags be the same. */
1421 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1422 return 0;
1423
1424 /* C90 didn't say what happened if one or both of the types were
1425 incomplete; we choose to follow C99 rules here, which is that they
1426 are compatible. */
1427 if (TYPE_SIZE (t1) == NULL
1428 || TYPE_SIZE (t2) == NULL)
1429 return 1;
1430
1431 {
1432 const struct tagged_tu_seen_cache * tts_i;
1433 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1434 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1435 return tts_i->val;
1436 }
1437
1438 switch (TREE_CODE (t1))
1439 {
1440 case ENUMERAL_TYPE:
1441 {
1442 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1443 /* Speed up the case where the type values are in the same order. */
1444 tree tv1 = TYPE_VALUES (t1);
1445 tree tv2 = TYPE_VALUES (t2);
1446
1447 if (tv1 == tv2)
1448 {
1449 return 1;
1450 }
1451
1452 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1453 {
1454 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1455 break;
1456 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1457 {
1458 tu->val = 0;
1459 return 0;
1460 }
1461 }
1462
1463 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1464 {
1465 return 1;
1466 }
1467 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1468 {
1469 tu->val = 0;
1470 return 0;
1471 }
1472
1473 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1474 {
1475 tu->val = 0;
1476 return 0;
1477 }
1478
1479 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1480 {
1481 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1482 if (s2 == NULL
1483 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1484 {
1485 tu->val = 0;
1486 return 0;
1487 }
1488 }
1489 return 1;
1490 }
1491
1492 case UNION_TYPE:
1493 {
1494 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1495 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1496 {
1497 tu->val = 0;
1498 return 0;
1499 }
1500
1501 /* Speed up the common case where the fields are in the same order. */
1502 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1503 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1504 {
1505 int result;
1506
1507 if (DECL_NAME (s1) != DECL_NAME (s2))
1508 break;
1509 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1510 enum_and_int_p, different_types_p);
1511
1512 if (result != 1 && !DECL_NAME (s1))
1513 break;
1514 if (result == 0)
1515 {
1516 tu->val = 0;
1517 return 0;
1518 }
1519 if (result == 2)
1520 needs_warning = true;
1521
1522 if (TREE_CODE (s1) == FIELD_DECL
1523 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1524 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1525 {
1526 tu->val = 0;
1527 return 0;
1528 }
1529 }
1530 if (!s1 && !s2)
1531 {
1532 tu->val = needs_warning ? 2 : 1;
1533 return tu->val;
1534 }
1535
1536 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1537 {
1538 bool ok = false;
1539
1540 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1541 if (DECL_NAME (s1) == DECL_NAME (s2))
1542 {
1543 int result;
1544
1545 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1546 enum_and_int_p,
1547 different_types_p);
1548
1549 if (result != 1 && !DECL_NAME (s1))
1550 continue;
1551 if (result == 0)
1552 {
1553 tu->val = 0;
1554 return 0;
1555 }
1556 if (result == 2)
1557 needs_warning = true;
1558
1559 if (TREE_CODE (s1) == FIELD_DECL
1560 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1561 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1562 break;
1563
1564 ok = true;
1565 break;
1566 }
1567 if (!ok)
1568 {
1569 tu->val = 0;
1570 return 0;
1571 }
1572 }
1573 tu->val = needs_warning ? 2 : 10;
1574 return tu->val;
1575 }
1576
1577 case RECORD_TYPE:
1578 {
1579 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1580
1581 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1582 s1 && s2;
1583 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1584 {
1585 int result;
1586 if (TREE_CODE (s1) != TREE_CODE (s2)
1587 || DECL_NAME (s1) != DECL_NAME (s2))
1588 break;
1589 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1590 enum_and_int_p, different_types_p);
1591 if (result == 0)
1592 break;
1593 if (result == 2)
1594 needs_warning = true;
1595
1596 if (TREE_CODE (s1) == FIELD_DECL
1597 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1598 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1599 break;
1600 }
1601 if (s1 && s2)
1602 tu->val = 0;
1603 else
1604 tu->val = needs_warning ? 2 : 1;
1605 return tu->val;
1606 }
1607
1608 default:
1609 gcc_unreachable ();
1610 }
1611 }
1612
1613 /* Return 1 if two function types F1 and F2 are compatible.
1614 If either type specifies no argument types,
1615 the other must specify a fixed number of self-promoting arg types.
1616 Otherwise, if one type specifies only the number of arguments,
1617 the other must specify that number of self-promoting arg types.
1618 Otherwise, the argument types must match.
1619 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1620
1621 static int
1622 function_types_compatible_p (const_tree f1, const_tree f2,
1623 bool *enum_and_int_p, bool *different_types_p)
1624 {
1625 tree args1, args2;
1626 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1627 int val = 1;
1628 int val1;
1629 tree ret1, ret2;
1630
1631 ret1 = TREE_TYPE (f1);
1632 ret2 = TREE_TYPE (f2);
1633
1634 /* 'volatile' qualifiers on a function's return type used to mean
1635 the function is noreturn. */
1636 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1637 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1638 if (TYPE_VOLATILE (ret1))
1639 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1640 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1641 if (TYPE_VOLATILE (ret2))
1642 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1643 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1644 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1645 if (val == 0)
1646 return 0;
1647
1648 args1 = TYPE_ARG_TYPES (f1);
1649 args2 = TYPE_ARG_TYPES (f2);
1650
1651 if (different_types_p != NULL
1652 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1653 *different_types_p = true;
1654
1655 /* An unspecified parmlist matches any specified parmlist
1656 whose argument types don't need default promotions. */
1657
1658 if (args1 == NULL_TREE)
1659 {
1660 if (!self_promoting_args_p (args2))
1661 return 0;
1662 /* If one of these types comes from a non-prototype fn definition,
1663 compare that with the other type's arglist.
1664 If they don't match, ask for a warning (but no error). */
1665 if (TYPE_ACTUAL_ARG_TYPES (f1)
1666 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1667 enum_and_int_p, different_types_p) != 1)
1668 val = 2;
1669 return val;
1670 }
1671 if (args2 == NULL_TREE)
1672 {
1673 if (!self_promoting_args_p (args1))
1674 return 0;
1675 if (TYPE_ACTUAL_ARG_TYPES (f2)
1676 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1677 enum_and_int_p, different_types_p) != 1)
1678 val = 2;
1679 return val;
1680 }
1681
1682 /* Both types have argument lists: compare them and propagate results. */
1683 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1684 different_types_p);
1685 return val1 != 1 ? val1 : val;
1686 }
1687
1688 /* Check two lists of types for compatibility, returning 0 for
1689 incompatible, 1 for compatible, or 2 for compatible with
1690 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1691 comptypes_internal. */
1692
1693 static int
1694 type_lists_compatible_p (const_tree args1, const_tree args2,
1695 bool *enum_and_int_p, bool *different_types_p)
1696 {
1697 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1698 int val = 1;
1699 int newval = 0;
1700
1701 while (1)
1702 {
1703 tree a1, mv1, a2, mv2;
1704 if (args1 == NULL_TREE && args2 == NULL_TREE)
1705 return val;
1706 /* If one list is shorter than the other,
1707 they fail to match. */
1708 if (args1 == NULL_TREE || args2 == NULL_TREE)
1709 return 0;
1710 mv1 = a1 = TREE_VALUE (args1);
1711 mv2 = a2 = TREE_VALUE (args2);
1712 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1713 mv1 = (TYPE_ATOMIC (mv1)
1714 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1715 TYPE_QUAL_ATOMIC)
1716 : TYPE_MAIN_VARIANT (mv1));
1717 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1718 mv2 = (TYPE_ATOMIC (mv2)
1719 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1720 TYPE_QUAL_ATOMIC)
1721 : TYPE_MAIN_VARIANT (mv2));
1722 /* A null pointer instead of a type
1723 means there is supposed to be an argument
1724 but nothing is specified about what type it has.
1725 So match anything that self-promotes. */
1726 if (different_types_p != NULL
1727 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1728 *different_types_p = true;
1729 if (a1 == NULL_TREE)
1730 {
1731 if (c_type_promotes_to (a2) != a2)
1732 return 0;
1733 }
1734 else if (a2 == NULL_TREE)
1735 {
1736 if (c_type_promotes_to (a1) != a1)
1737 return 0;
1738 }
1739 /* If one of the lists has an error marker, ignore this arg. */
1740 else if (TREE_CODE (a1) == ERROR_MARK
1741 || TREE_CODE (a2) == ERROR_MARK)
1742 ;
1743 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1744 different_types_p)))
1745 {
1746 if (different_types_p != NULL)
1747 *different_types_p = true;
1748 /* Allow wait (union {union wait *u; int *i} *)
1749 and wait (union wait *) to be compatible. */
1750 if (TREE_CODE (a1) == UNION_TYPE
1751 && (TYPE_NAME (a1) == NULL_TREE
1752 || TYPE_TRANSPARENT_AGGR (a1))
1753 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1754 && tree_int_cst_equal (TYPE_SIZE (a1),
1755 TYPE_SIZE (a2)))
1756 {
1757 tree memb;
1758 for (memb = TYPE_FIELDS (a1);
1759 memb; memb = DECL_CHAIN (memb))
1760 {
1761 tree mv3 = TREE_TYPE (memb);
1762 if (mv3 && mv3 != error_mark_node
1763 && TREE_CODE (mv3) != ARRAY_TYPE)
1764 mv3 = (TYPE_ATOMIC (mv3)
1765 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1766 TYPE_QUAL_ATOMIC)
1767 : TYPE_MAIN_VARIANT (mv3));
1768 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1769 different_types_p))
1770 break;
1771 }
1772 if (memb == NULL_TREE)
1773 return 0;
1774 }
1775 else if (TREE_CODE (a2) == UNION_TYPE
1776 && (TYPE_NAME (a2) == NULL_TREE
1777 || TYPE_TRANSPARENT_AGGR (a2))
1778 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1779 && tree_int_cst_equal (TYPE_SIZE (a2),
1780 TYPE_SIZE (a1)))
1781 {
1782 tree memb;
1783 for (memb = TYPE_FIELDS (a2);
1784 memb; memb = DECL_CHAIN (memb))
1785 {
1786 tree mv3 = TREE_TYPE (memb);
1787 if (mv3 && mv3 != error_mark_node
1788 && TREE_CODE (mv3) != ARRAY_TYPE)
1789 mv3 = (TYPE_ATOMIC (mv3)
1790 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1791 TYPE_QUAL_ATOMIC)
1792 : TYPE_MAIN_VARIANT (mv3));
1793 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1794 different_types_p))
1795 break;
1796 }
1797 if (memb == NULL_TREE)
1798 return 0;
1799 }
1800 else
1801 return 0;
1802 }
1803
1804 /* comptypes said ok, but record if it said to warn. */
1805 if (newval > val)
1806 val = newval;
1807
1808 args1 = TREE_CHAIN (args1);
1809 args2 = TREE_CHAIN (args2);
1810 }
1811 }
1812 \f
1813 /* Compute the size to increment a pointer by. When a function type or void
1814 type or incomplete type is passed, size_one_node is returned.
1815 This function does not emit any diagnostics; the caller is responsible
1816 for that. */
1817
1818 static tree
1819 c_size_in_bytes (const_tree type)
1820 {
1821 enum tree_code code = TREE_CODE (type);
1822
1823 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1824 || !COMPLETE_TYPE_P (type))
1825 return size_one_node;
1826
1827 /* Convert in case a char is more than one unit. */
1828 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1829 size_int (TYPE_PRECISION (char_type_node)
1830 / BITS_PER_UNIT));
1831 }
1832 \f
1833 /* Return either DECL or its known constant value (if it has one). */
1834
1835 tree
1836 decl_constant_value_1 (tree decl, bool in_init)
1837 {
1838 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1839 TREE_CODE (decl) != PARM_DECL
1840 && !TREE_THIS_VOLATILE (decl)
1841 && TREE_READONLY (decl)
1842 && DECL_INITIAL (decl) != NULL_TREE
1843 && !error_operand_p (DECL_INITIAL (decl))
1844 /* This is invalid if initial value is not constant.
1845 If it has either a function call, a memory reference,
1846 or a variable, then re-evaluating it could give different results. */
1847 && TREE_CONSTANT (DECL_INITIAL (decl))
1848 /* Check for cases where this is sub-optimal, even though valid. */
1849 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1850 return DECL_INITIAL (decl);
1851 return decl;
1852 }
1853
1854 /* Return either DECL or its known constant value (if it has one).
1855 Like the above, but always return decl outside of functions. */
1856
1857 tree
1858 decl_constant_value (tree decl)
1859 {
1860 /* Don't change a variable array bound or initial value to a constant
1861 in a place where a variable is invalid. */
1862 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1863 }
1864
1865 /* Convert the array expression EXP to a pointer. */
1866 static tree
1867 array_to_pointer_conversion (location_t loc, tree exp)
1868 {
1869 tree orig_exp = exp;
1870 tree type = TREE_TYPE (exp);
1871 tree adr;
1872 tree restype = TREE_TYPE (type);
1873 tree ptrtype;
1874
1875 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1876
1877 STRIP_TYPE_NOPS (exp);
1878
1879 if (TREE_NO_WARNING (orig_exp))
1880 TREE_NO_WARNING (exp) = 1;
1881
1882 ptrtype = build_pointer_type (restype);
1883
1884 if (INDIRECT_REF_P (exp))
1885 return convert (ptrtype, TREE_OPERAND (exp, 0));
1886
1887 /* In C++ array compound literals are temporary objects unless they are
1888 const or appear in namespace scope, so they are destroyed too soon
1889 to use them for much of anything (c++/53220). */
1890 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1891 {
1892 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1893 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1894 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1895 "converting an array compound literal to a pointer "
1896 "is ill-formed in C++");
1897 }
1898
1899 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1900 return convert (ptrtype, adr);
1901 }
1902
1903 /* Convert the function expression EXP to a pointer. */
1904 static tree
1905 function_to_pointer_conversion (location_t loc, tree exp)
1906 {
1907 tree orig_exp = exp;
1908
1909 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1910
1911 STRIP_TYPE_NOPS (exp);
1912
1913 if (TREE_NO_WARNING (orig_exp))
1914 TREE_NO_WARNING (exp) = 1;
1915
1916 return build_unary_op (loc, ADDR_EXPR, exp, false);
1917 }
1918
1919 /* Mark EXP as read, not just set, for set but not used -Wunused
1920 warning purposes. */
1921
1922 void
1923 mark_exp_read (tree exp)
1924 {
1925 switch (TREE_CODE (exp))
1926 {
1927 case VAR_DECL:
1928 case PARM_DECL:
1929 DECL_READ_P (exp) = 1;
1930 break;
1931 case ARRAY_REF:
1932 case COMPONENT_REF:
1933 case MODIFY_EXPR:
1934 case REALPART_EXPR:
1935 case IMAGPART_EXPR:
1936 CASE_CONVERT:
1937 case ADDR_EXPR:
1938 case VIEW_CONVERT_EXPR:
1939 mark_exp_read (TREE_OPERAND (exp, 0));
1940 break;
1941 case COMPOUND_EXPR:
1942 case C_MAYBE_CONST_EXPR:
1943 mark_exp_read (TREE_OPERAND (exp, 1));
1944 break;
1945 default:
1946 break;
1947 }
1948 }
1949
1950 /* Perform the default conversion of arrays and functions to pointers.
1951 Return the result of converting EXP. For any other expression, just
1952 return EXP.
1953
1954 LOC is the location of the expression. */
1955
1956 struct c_expr
1957 default_function_array_conversion (location_t loc, struct c_expr exp)
1958 {
1959 tree orig_exp = exp.value;
1960 tree type = TREE_TYPE (exp.value);
1961 enum tree_code code = TREE_CODE (type);
1962
1963 switch (code)
1964 {
1965 case ARRAY_TYPE:
1966 {
1967 bool not_lvalue = false;
1968 bool lvalue_array_p;
1969
1970 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1971 || CONVERT_EXPR_P (exp.value))
1972 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1973 {
1974 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1975 not_lvalue = true;
1976 exp.value = TREE_OPERAND (exp.value, 0);
1977 }
1978
1979 if (TREE_NO_WARNING (orig_exp))
1980 TREE_NO_WARNING (exp.value) = 1;
1981
1982 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1983 if (!flag_isoc99 && !lvalue_array_p)
1984 {
1985 /* Before C99, non-lvalue arrays do not decay to pointers.
1986 Normally, using such an array would be invalid; but it can
1987 be used correctly inside sizeof or as a statement expression.
1988 Thus, do not give an error here; an error will result later. */
1989 return exp;
1990 }
1991
1992 exp.value = array_to_pointer_conversion (loc, exp.value);
1993 }
1994 break;
1995 case FUNCTION_TYPE:
1996 exp.value = function_to_pointer_conversion (loc, exp.value);
1997 break;
1998 default:
1999 break;
2000 }
2001
2002 return exp;
2003 }
2004
2005 struct c_expr
2006 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2007 {
2008 mark_exp_read (exp.value);
2009 return default_function_array_conversion (loc, exp);
2010 }
2011
2012 /* Return whether EXPR should be treated as an atomic lvalue for the
2013 purposes of load and store handling. */
2014
2015 static bool
2016 really_atomic_lvalue (tree expr)
2017 {
2018 if (error_operand_p (expr))
2019 return false;
2020 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2021 return false;
2022 if (!lvalue_p (expr))
2023 return false;
2024
2025 /* Ignore _Atomic on register variables, since their addresses can't
2026 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2027 sequences wouldn't work. Ignore _Atomic on structures containing
2028 bit-fields, since accessing elements of atomic structures or
2029 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2030 it's undefined at translation time or execution time, and the
2031 normal atomic sequences again wouldn't work. */
2032 while (handled_component_p (expr))
2033 {
2034 if (TREE_CODE (expr) == COMPONENT_REF
2035 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2036 return false;
2037 expr = TREE_OPERAND (expr, 0);
2038 }
2039 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2040 return false;
2041 return true;
2042 }
2043
2044 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2045 including converting functions and arrays to pointers if CONVERT_P.
2046 If READ_P, also mark the expression as having been read. */
2047
2048 struct c_expr
2049 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2050 bool convert_p, bool read_p)
2051 {
2052 if (read_p)
2053 mark_exp_read (exp.value);
2054 if (convert_p)
2055 exp = default_function_array_conversion (loc, exp);
2056 if (really_atomic_lvalue (exp.value))
2057 {
2058 vec<tree, va_gc> *params;
2059 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2060 tree expr_type = TREE_TYPE (exp.value);
2061 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2062 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2063
2064 gcc_assert (TYPE_ATOMIC (expr_type));
2065
2066 /* Expansion of a generic atomic load may require an addition
2067 element, so allocate enough to prevent a resize. */
2068 vec_alloc (params, 4);
2069
2070 /* Remove the qualifiers for the rest of the expressions and
2071 create the VAL temp variable to hold the RHS. */
2072 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2073 tmp = create_tmp_var_raw (nonatomic_type);
2074 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2075 TREE_ADDRESSABLE (tmp) = 1;
2076 TREE_NO_WARNING (tmp) = 1;
2077
2078 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2079 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2080 params->quick_push (expr_addr);
2081 params->quick_push (tmp_addr);
2082 params->quick_push (seq_cst);
2083 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2084
2085 /* EXPR is always read. */
2086 mark_exp_read (exp.value);
2087
2088 /* Return tmp which contains the value loaded. */
2089 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2090 NULL_TREE, NULL_TREE);
2091 }
2092 return exp;
2093 }
2094
2095 /* EXP is an expression of integer type. Apply the integer promotions
2096 to it and return the promoted value. */
2097
2098 tree
2099 perform_integral_promotions (tree exp)
2100 {
2101 tree type = TREE_TYPE (exp);
2102 enum tree_code code = TREE_CODE (type);
2103
2104 gcc_assert (INTEGRAL_TYPE_P (type));
2105
2106 /* Normally convert enums to int,
2107 but convert wide enums to something wider. */
2108 if (code == ENUMERAL_TYPE)
2109 {
2110 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2111 TYPE_PRECISION (integer_type_node)),
2112 ((TYPE_PRECISION (type)
2113 >= TYPE_PRECISION (integer_type_node))
2114 && TYPE_UNSIGNED (type)));
2115
2116 return convert (type, exp);
2117 }
2118
2119 /* ??? This should no longer be needed now bit-fields have their
2120 proper types. */
2121 if (TREE_CODE (exp) == COMPONENT_REF
2122 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2123 /* If it's thinner than an int, promote it like a
2124 c_promoting_integer_type_p, otherwise leave it alone. */
2125 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2126 TYPE_PRECISION (integer_type_node)) < 0)
2127 return convert (integer_type_node, exp);
2128
2129 if (c_promoting_integer_type_p (type))
2130 {
2131 /* Preserve unsignedness if not really getting any wider. */
2132 if (TYPE_UNSIGNED (type)
2133 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2134 return convert (unsigned_type_node, exp);
2135
2136 return convert (integer_type_node, exp);
2137 }
2138
2139 return exp;
2140 }
2141
2142
2143 /* Perform default promotions for C data used in expressions.
2144 Enumeral types or short or char are converted to int.
2145 In addition, manifest constants symbols are replaced by their values. */
2146
2147 tree
2148 default_conversion (tree exp)
2149 {
2150 tree orig_exp;
2151 tree type = TREE_TYPE (exp);
2152 enum tree_code code = TREE_CODE (type);
2153 tree promoted_type;
2154
2155 mark_exp_read (exp);
2156
2157 /* Functions and arrays have been converted during parsing. */
2158 gcc_assert (code != FUNCTION_TYPE);
2159 if (code == ARRAY_TYPE)
2160 return exp;
2161
2162 /* Constants can be used directly unless they're not loadable. */
2163 if (TREE_CODE (exp) == CONST_DECL)
2164 exp = DECL_INITIAL (exp);
2165
2166 /* Strip no-op conversions. */
2167 orig_exp = exp;
2168 STRIP_TYPE_NOPS (exp);
2169
2170 if (TREE_NO_WARNING (orig_exp))
2171 TREE_NO_WARNING (exp) = 1;
2172
2173 if (code == VOID_TYPE)
2174 {
2175 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2176 "void value not ignored as it ought to be");
2177 return error_mark_node;
2178 }
2179
2180 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2181 if (exp == error_mark_node)
2182 return error_mark_node;
2183
2184 promoted_type = targetm.promoted_type (type);
2185 if (promoted_type)
2186 return convert (promoted_type, exp);
2187
2188 if (INTEGRAL_TYPE_P (type))
2189 return perform_integral_promotions (exp);
2190
2191 return exp;
2192 }
2193 \f
2194 /* Look up COMPONENT in a structure or union TYPE.
2195
2196 If the component name is not found, returns NULL_TREE. Otherwise,
2197 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2198 stepping down the chain to the component, which is in the last
2199 TREE_VALUE of the list. Normally the list is of length one, but if
2200 the component is embedded within (nested) anonymous structures or
2201 unions, the list steps down the chain to the component. */
2202
2203 static tree
2204 lookup_field (tree type, tree component)
2205 {
2206 tree field;
2207
2208 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2209 to the field elements. Use a binary search on this array to quickly
2210 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2211 will always be set for structures which have many elements.
2212
2213 Duplicate field checking replaces duplicates with NULL_TREE so
2214 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2215 case just iterate using DECL_CHAIN. */
2216
2217 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2218 && !seen_error ())
2219 {
2220 int bot, top, half;
2221 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2222
2223 field = TYPE_FIELDS (type);
2224 bot = 0;
2225 top = TYPE_LANG_SPECIFIC (type)->s->len;
2226 while (top - bot > 1)
2227 {
2228 half = (top - bot + 1) >> 1;
2229 field = field_array[bot+half];
2230
2231 if (DECL_NAME (field) == NULL_TREE)
2232 {
2233 /* Step through all anon unions in linear fashion. */
2234 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2235 {
2236 field = field_array[bot++];
2237 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2238 {
2239 tree anon = lookup_field (TREE_TYPE (field), component);
2240
2241 if (anon)
2242 return tree_cons (NULL_TREE, field, anon);
2243
2244 /* The Plan 9 compiler permits referring
2245 directly to an anonymous struct/union field
2246 using a typedef name. */
2247 if (flag_plan9_extensions
2248 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2249 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2250 == TYPE_DECL)
2251 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2252 == component))
2253 break;
2254 }
2255 }
2256
2257 /* Entire record is only anon unions. */
2258 if (bot > top)
2259 return NULL_TREE;
2260
2261 /* Restart the binary search, with new lower bound. */
2262 continue;
2263 }
2264
2265 if (DECL_NAME (field) == component)
2266 break;
2267 if (DECL_NAME (field) < component)
2268 bot += half;
2269 else
2270 top = bot + half;
2271 }
2272
2273 if (DECL_NAME (field_array[bot]) == component)
2274 field = field_array[bot];
2275 else if (DECL_NAME (field) != component)
2276 return NULL_TREE;
2277 }
2278 else
2279 {
2280 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2281 {
2282 if (DECL_NAME (field) == NULL_TREE
2283 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2284 {
2285 tree anon = lookup_field (TREE_TYPE (field), component);
2286
2287 if (anon)
2288 return tree_cons (NULL_TREE, field, anon);
2289
2290 /* The Plan 9 compiler permits referring directly to an
2291 anonymous struct/union field using a typedef
2292 name. */
2293 if (flag_plan9_extensions
2294 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2295 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2296 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2297 == component))
2298 break;
2299 }
2300
2301 if (DECL_NAME (field) == component)
2302 break;
2303 }
2304
2305 if (field == NULL_TREE)
2306 return NULL_TREE;
2307 }
2308
2309 return tree_cons (NULL_TREE, field, NULL_TREE);
2310 }
2311
2312 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2313
2314 static void
2315 lookup_field_fuzzy_find_candidates (tree type, tree component,
2316 vec<tree> *candidates)
2317 {
2318 tree field;
2319 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2320 {
2321 if (DECL_NAME (field) == NULL_TREE
2322 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2323 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2324 candidates);
2325
2326 if (DECL_NAME (field))
2327 candidates->safe_push (DECL_NAME (field));
2328 }
2329 }
2330
2331 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2332 rather than returning a TREE_LIST for an exact match. */
2333
2334 static tree
2335 lookup_field_fuzzy (tree type, tree component)
2336 {
2337 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2338
2339 /* First, gather a list of candidates. */
2340 auto_vec <tree> candidates;
2341
2342 lookup_field_fuzzy_find_candidates (type, component,
2343 &candidates);
2344
2345 return find_closest_identifier (component, &candidates);
2346 }
2347
2348 /* Support function for build_component_ref's error-handling.
2349
2350 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2351 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2352
2353 static bool
2354 should_suggest_deref_p (tree datum_type)
2355 {
2356 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2357 allows "." for ptrs; we could be handling a failed attempt
2358 to access a property. */
2359 if (c_dialect_objc ())
2360 return false;
2361
2362 /* Only suggest it for pointers... */
2363 if (TREE_CODE (datum_type) != POINTER_TYPE)
2364 return false;
2365
2366 /* ...to structs/unions. */
2367 tree underlying_type = TREE_TYPE (datum_type);
2368 enum tree_code code = TREE_CODE (underlying_type);
2369 if (code == RECORD_TYPE || code == UNION_TYPE)
2370 return true;
2371 else
2372 return false;
2373 }
2374
2375 /* Make an expression to refer to the COMPONENT field of structure or
2376 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2377 location of the COMPONENT_REF. COMPONENT_LOC is the location
2378 of COMPONENT. */
2379
2380 tree
2381 build_component_ref (location_t loc, tree datum, tree component,
2382 location_t component_loc)
2383 {
2384 tree type = TREE_TYPE (datum);
2385 enum tree_code code = TREE_CODE (type);
2386 tree field = NULL;
2387 tree ref;
2388 bool datum_lvalue = lvalue_p (datum);
2389
2390 if (!objc_is_public (datum, component))
2391 return error_mark_node;
2392
2393 /* Detect Objective-C property syntax object.property. */
2394 if (c_dialect_objc ()
2395 && (ref = objc_maybe_build_component_ref (datum, component)))
2396 return ref;
2397
2398 /* See if there is a field or component with name COMPONENT. */
2399
2400 if (code == RECORD_TYPE || code == UNION_TYPE)
2401 {
2402 if (!COMPLETE_TYPE_P (type))
2403 {
2404 c_incomplete_type_error (loc, NULL_TREE, type);
2405 return error_mark_node;
2406 }
2407
2408 field = lookup_field (type, component);
2409
2410 if (!field)
2411 {
2412 tree guessed_id = lookup_field_fuzzy (type, component);
2413 if (guessed_id)
2414 {
2415 /* Attempt to provide a fixit replacement hint, if
2416 we have a valid range for the component. */
2417 location_t reported_loc
2418 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2419 gcc_rich_location rich_loc (reported_loc);
2420 if (component_loc != UNKNOWN_LOCATION)
2421 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2422 error_at (&rich_loc,
2423 "%qT has no member named %qE; did you mean %qE?",
2424 type, component, guessed_id);
2425 }
2426 else
2427 error_at (loc, "%qT has no member named %qE", type, component);
2428 return error_mark_node;
2429 }
2430
2431 /* Accessing elements of atomic structures or unions is undefined
2432 behavior (C11 6.5.2.3#5). */
2433 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2434 {
2435 if (code == RECORD_TYPE)
2436 warning_at (loc, 0, "accessing a member %qE of an atomic "
2437 "structure %qE", component, datum);
2438 else
2439 warning_at (loc, 0, "accessing a member %qE of an atomic "
2440 "union %qE", component, datum);
2441 }
2442
2443 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2444 This might be better solved in future the way the C++ front
2445 end does it - by giving the anonymous entities each a
2446 separate name and type, and then have build_component_ref
2447 recursively call itself. We can't do that here. */
2448 do
2449 {
2450 tree subdatum = TREE_VALUE (field);
2451 int quals;
2452 tree subtype;
2453 bool use_datum_quals;
2454
2455 if (TREE_TYPE (subdatum) == error_mark_node)
2456 return error_mark_node;
2457
2458 /* If this is an rvalue, it does not have qualifiers in C
2459 standard terms and we must avoid propagating such
2460 qualifiers down to a non-lvalue array that is then
2461 converted to a pointer. */
2462 use_datum_quals = (datum_lvalue
2463 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2464
2465 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2466 if (use_datum_quals)
2467 quals |= TYPE_QUALS (TREE_TYPE (datum));
2468 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2469
2470 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2471 NULL_TREE);
2472 SET_EXPR_LOCATION (ref, loc);
2473 if (TREE_READONLY (subdatum)
2474 || (use_datum_quals && TREE_READONLY (datum)))
2475 TREE_READONLY (ref) = 1;
2476 if (TREE_THIS_VOLATILE (subdatum)
2477 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2478 TREE_THIS_VOLATILE (ref) = 1;
2479
2480 if (TREE_DEPRECATED (subdatum))
2481 warn_deprecated_use (subdatum, NULL_TREE);
2482
2483 datum = ref;
2484
2485 field = TREE_CHAIN (field);
2486 }
2487 while (field);
2488
2489 return ref;
2490 }
2491 else if (should_suggest_deref_p (type))
2492 {
2493 /* Special-case the error message for "ptr.field" for the case
2494 where the user has confused "." vs "->". */
2495 rich_location richloc (line_table, loc);
2496 /* "loc" should be the "." token. */
2497 richloc.add_fixit_replace ("->");
2498 error_at (&richloc,
2499 "%qE is a pointer; did you mean to use %<->%>?",
2500 datum);
2501 return error_mark_node;
2502 }
2503 else if (code != ERROR_MARK)
2504 error_at (loc,
2505 "request for member %qE in something not a structure or union",
2506 component);
2507
2508 return error_mark_node;
2509 }
2510 \f
2511 /* Given an expression PTR for a pointer, return an expression
2512 for the value pointed to.
2513 ERRORSTRING is the name of the operator to appear in error messages.
2514
2515 LOC is the location to use for the generated tree. */
2516
2517 tree
2518 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2519 {
2520 tree pointer = default_conversion (ptr);
2521 tree type = TREE_TYPE (pointer);
2522 tree ref;
2523
2524 if (TREE_CODE (type) == POINTER_TYPE)
2525 {
2526 if (CONVERT_EXPR_P (pointer)
2527 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2528 {
2529 /* If a warning is issued, mark it to avoid duplicates from
2530 the backend. This only needs to be done at
2531 warn_strict_aliasing > 2. */
2532 if (warn_strict_aliasing > 2)
2533 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2534 type, TREE_OPERAND (pointer, 0)))
2535 TREE_NO_WARNING (pointer) = 1;
2536 }
2537
2538 if (TREE_CODE (pointer) == ADDR_EXPR
2539 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2540 == TREE_TYPE (type)))
2541 {
2542 ref = TREE_OPERAND (pointer, 0);
2543 protected_set_expr_location (ref, loc);
2544 return ref;
2545 }
2546 else
2547 {
2548 tree t = TREE_TYPE (type);
2549
2550 ref = build1 (INDIRECT_REF, t, pointer);
2551
2552 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2553 {
2554 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2555 {
2556 error_at (loc, "dereferencing pointer to incomplete type "
2557 "%qT", t);
2558 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2559 }
2560 return error_mark_node;
2561 }
2562 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2563 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2564
2565 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2566 so that we get the proper error message if the result is used
2567 to assign to. Also, &* is supposed to be a no-op.
2568 And ANSI C seems to specify that the type of the result
2569 should be the const type. */
2570 /* A de-reference of a pointer to const is not a const. It is valid
2571 to change it via some other pointer. */
2572 TREE_READONLY (ref) = TYPE_READONLY (t);
2573 TREE_SIDE_EFFECTS (ref)
2574 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2575 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2576 protected_set_expr_location (ref, loc);
2577 return ref;
2578 }
2579 }
2580 else if (TREE_CODE (pointer) != ERROR_MARK)
2581 invalid_indirection_error (loc, type, errstring);
2582
2583 return error_mark_node;
2584 }
2585
2586 /* This handles expressions of the form "a[i]", which denotes
2587 an array reference.
2588
2589 This is logically equivalent in C to *(a+i), but we may do it differently.
2590 If A is a variable or a member, we generate a primitive ARRAY_REF.
2591 This avoids forcing the array out of registers, and can work on
2592 arrays that are not lvalues (for example, members of structures returned
2593 by functions).
2594
2595 For vector types, allow vector[i] but not i[vector], and create
2596 *(((type*)&vectortype) + i) for the expression.
2597
2598 LOC is the location to use for the returned expression. */
2599
2600 tree
2601 build_array_ref (location_t loc, tree array, tree index)
2602 {
2603 tree ret;
2604 bool swapped = false;
2605 if (TREE_TYPE (array) == error_mark_node
2606 || TREE_TYPE (index) == error_mark_node)
2607 return error_mark_node;
2608
2609 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2610 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2611 /* Allow vector[index] but not index[vector]. */
2612 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2613 {
2614 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2615 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2616 {
2617 error_at (loc,
2618 "subscripted value is neither array nor pointer nor vector");
2619
2620 return error_mark_node;
2621 }
2622 std::swap (array, index);
2623 swapped = true;
2624 }
2625
2626 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2627 {
2628 error_at (loc, "array subscript is not an integer");
2629 return error_mark_node;
2630 }
2631
2632 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2633 {
2634 error_at (loc, "subscripted value is pointer to function");
2635 return error_mark_node;
2636 }
2637
2638 /* ??? Existing practice has been to warn only when the char
2639 index is syntactically the index, not for char[array]. */
2640 if (!swapped)
2641 warn_array_subscript_with_type_char (loc, index);
2642
2643 /* Apply default promotions *after* noticing character types. */
2644 index = default_conversion (index);
2645 if (index == error_mark_node)
2646 return error_mark_node;
2647
2648 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2649
2650 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2651 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2652
2653 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2654 {
2655 tree rval, type;
2656
2657 /* An array that is indexed by a non-constant
2658 cannot be stored in a register; we must be able to do
2659 address arithmetic on its address.
2660 Likewise an array of elements of variable size. */
2661 if (TREE_CODE (index) != INTEGER_CST
2662 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2663 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2664 {
2665 if (!c_mark_addressable (array, true))
2666 return error_mark_node;
2667 }
2668 /* An array that is indexed by a constant value which is not within
2669 the array bounds cannot be stored in a register either; because we
2670 would get a crash in store_bit_field/extract_bit_field when trying
2671 to access a non-existent part of the register. */
2672 if (TREE_CODE (index) == INTEGER_CST
2673 && TYPE_DOMAIN (TREE_TYPE (array))
2674 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2675 {
2676 if (!c_mark_addressable (array))
2677 return error_mark_node;
2678 }
2679
2680 if ((pedantic || warn_c90_c99_compat)
2681 && ! was_vector)
2682 {
2683 tree foo = array;
2684 while (TREE_CODE (foo) == COMPONENT_REF)
2685 foo = TREE_OPERAND (foo, 0);
2686 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2687 pedwarn (loc, OPT_Wpedantic,
2688 "ISO C forbids subscripting %<register%> array");
2689 else if (!lvalue_p (foo))
2690 pedwarn_c90 (loc, OPT_Wpedantic,
2691 "ISO C90 forbids subscripting non-lvalue "
2692 "array");
2693 }
2694
2695 type = TREE_TYPE (TREE_TYPE (array));
2696 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2697 /* Array ref is const/volatile if the array elements are
2698 or if the array is. */
2699 TREE_READONLY (rval)
2700 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2701 | TREE_READONLY (array));
2702 TREE_SIDE_EFFECTS (rval)
2703 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2704 | TREE_SIDE_EFFECTS (array));
2705 TREE_THIS_VOLATILE (rval)
2706 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2707 /* This was added by rms on 16 Nov 91.
2708 It fixes vol struct foo *a; a->elts[1]
2709 in an inline function.
2710 Hope it doesn't break something else. */
2711 | TREE_THIS_VOLATILE (array));
2712 ret = require_complete_type (loc, rval);
2713 protected_set_expr_location (ret, loc);
2714 if (non_lvalue)
2715 ret = non_lvalue_loc (loc, ret);
2716 return ret;
2717 }
2718 else
2719 {
2720 tree ar = default_conversion (array);
2721
2722 if (ar == error_mark_node)
2723 return ar;
2724
2725 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2726 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2727
2728 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2729 index, false),
2730 RO_ARRAY_INDEXING);
2731 if (non_lvalue)
2732 ret = non_lvalue_loc (loc, ret);
2733 return ret;
2734 }
2735 }
2736 \f
2737 /* Build an external reference to identifier ID. FUN indicates
2738 whether this will be used for a function call. LOC is the source
2739 location of the identifier. This sets *TYPE to the type of the
2740 identifier, which is not the same as the type of the returned value
2741 for CONST_DECLs defined as enum constants. If the type of the
2742 identifier is not available, *TYPE is set to NULL. */
2743 tree
2744 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2745 {
2746 tree ref;
2747 tree decl = lookup_name (id);
2748
2749 /* In Objective-C, an instance variable (ivar) may be preferred to
2750 whatever lookup_name() found. */
2751 decl = objc_lookup_ivar (decl, id);
2752
2753 *type = NULL;
2754 if (decl && decl != error_mark_node)
2755 {
2756 ref = decl;
2757 *type = TREE_TYPE (ref);
2758 }
2759 else if (fun)
2760 /* Implicit function declaration. */
2761 ref = implicitly_declare (loc, id);
2762 else if (decl == error_mark_node)
2763 /* Don't complain about something that's already been
2764 complained about. */
2765 return error_mark_node;
2766 else
2767 {
2768 undeclared_variable (loc, id);
2769 return error_mark_node;
2770 }
2771
2772 if (TREE_TYPE (ref) == error_mark_node)
2773 return error_mark_node;
2774
2775 if (TREE_DEPRECATED (ref))
2776 warn_deprecated_use (ref, NULL_TREE);
2777
2778 /* Recursive call does not count as usage. */
2779 if (ref != current_function_decl)
2780 {
2781 TREE_USED (ref) = 1;
2782 }
2783
2784 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2785 {
2786 if (!in_sizeof && !in_typeof)
2787 C_DECL_USED (ref) = 1;
2788 else if (DECL_INITIAL (ref) == NULL_TREE
2789 && DECL_EXTERNAL (ref)
2790 && !TREE_PUBLIC (ref))
2791 record_maybe_used_decl (ref);
2792 }
2793
2794 if (TREE_CODE (ref) == CONST_DECL)
2795 {
2796 used_types_insert (TREE_TYPE (ref));
2797
2798 if (warn_cxx_compat
2799 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2800 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2801 {
2802 warning_at (loc, OPT_Wc___compat,
2803 ("enum constant defined in struct or union "
2804 "is not visible in C++"));
2805 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2806 }
2807
2808 ref = DECL_INITIAL (ref);
2809 TREE_CONSTANT (ref) = 1;
2810 }
2811 else if (current_function_decl != NULL_TREE
2812 && !DECL_FILE_SCOPE_P (current_function_decl)
2813 && (VAR_OR_FUNCTION_DECL_P (ref)
2814 || TREE_CODE (ref) == PARM_DECL))
2815 {
2816 tree context = decl_function_context (ref);
2817
2818 if (context != NULL_TREE && context != current_function_decl)
2819 DECL_NONLOCAL (ref) = 1;
2820 }
2821 /* C99 6.7.4p3: An inline definition of a function with external
2822 linkage ... shall not contain a reference to an identifier with
2823 internal linkage. */
2824 else if (current_function_decl != NULL_TREE
2825 && DECL_DECLARED_INLINE_P (current_function_decl)
2826 && DECL_EXTERNAL (current_function_decl)
2827 && VAR_OR_FUNCTION_DECL_P (ref)
2828 && (!VAR_P (ref) || TREE_STATIC (ref))
2829 && ! TREE_PUBLIC (ref)
2830 && DECL_CONTEXT (ref) != current_function_decl)
2831 record_inline_static (loc, current_function_decl, ref,
2832 csi_internal);
2833
2834 return ref;
2835 }
2836
2837 /* Record details of decls possibly used inside sizeof or typeof. */
2838 struct maybe_used_decl
2839 {
2840 /* The decl. */
2841 tree decl;
2842 /* The level seen at (in_sizeof + in_typeof). */
2843 int level;
2844 /* The next one at this level or above, or NULL. */
2845 struct maybe_used_decl *next;
2846 };
2847
2848 static struct maybe_used_decl *maybe_used_decls;
2849
2850 /* Record that DECL, an undefined static function reference seen
2851 inside sizeof or typeof, might be used if the operand of sizeof is
2852 a VLA type or the operand of typeof is a variably modified
2853 type. */
2854
2855 static void
2856 record_maybe_used_decl (tree decl)
2857 {
2858 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2859 t->decl = decl;
2860 t->level = in_sizeof + in_typeof;
2861 t->next = maybe_used_decls;
2862 maybe_used_decls = t;
2863 }
2864
2865 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2866 USED is false, just discard them. If it is true, mark them used
2867 (if no longer inside sizeof or typeof) or move them to the next
2868 level up (if still inside sizeof or typeof). */
2869
2870 void
2871 pop_maybe_used (bool used)
2872 {
2873 struct maybe_used_decl *p = maybe_used_decls;
2874 int cur_level = in_sizeof + in_typeof;
2875 while (p && p->level > cur_level)
2876 {
2877 if (used)
2878 {
2879 if (cur_level == 0)
2880 C_DECL_USED (p->decl) = 1;
2881 else
2882 p->level = cur_level;
2883 }
2884 p = p->next;
2885 }
2886 if (!used || cur_level == 0)
2887 maybe_used_decls = p;
2888 }
2889
2890 /* Return the result of sizeof applied to EXPR. */
2891
2892 struct c_expr
2893 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2894 {
2895 struct c_expr ret;
2896 if (expr.value == error_mark_node)
2897 {
2898 ret.value = error_mark_node;
2899 ret.original_code = ERROR_MARK;
2900 ret.original_type = NULL;
2901 pop_maybe_used (false);
2902 }
2903 else
2904 {
2905 bool expr_const_operands = true;
2906
2907 if (TREE_CODE (expr.value) == PARM_DECL
2908 && C_ARRAY_PARAMETER (expr.value))
2909 {
2910 auto_diagnostic_group d;
2911 if (warning_at (loc, OPT_Wsizeof_array_argument,
2912 "%<sizeof%> on array function parameter %qE will "
2913 "return size of %qT", expr.value,
2914 TREE_TYPE (expr.value)))
2915 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2916 }
2917 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2918 &expr_const_operands);
2919 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2920 c_last_sizeof_arg = expr.value;
2921 c_last_sizeof_loc = loc;
2922 ret.original_code = SIZEOF_EXPR;
2923 ret.original_type = NULL;
2924 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2925 {
2926 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2927 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2928 folded_expr, ret.value);
2929 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2930 SET_EXPR_LOCATION (ret.value, loc);
2931 }
2932 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2933 }
2934 return ret;
2935 }
2936
2937 /* Return the result of sizeof applied to T, a structure for the type
2938 name passed to sizeof (rather than the type itself). LOC is the
2939 location of the original expression. */
2940
2941 struct c_expr
2942 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2943 {
2944 tree type;
2945 struct c_expr ret;
2946 tree type_expr = NULL_TREE;
2947 bool type_expr_const = true;
2948 type = groktypename (t, &type_expr, &type_expr_const);
2949 ret.value = c_sizeof (loc, type);
2950 c_last_sizeof_arg = type;
2951 c_last_sizeof_loc = loc;
2952 ret.original_code = SIZEOF_EXPR;
2953 ret.original_type = NULL;
2954 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2955 && c_vla_type_p (type))
2956 {
2957 /* If the type is a [*] array, it is a VLA but is represented as
2958 having a size of zero. In such a case we must ensure that
2959 the result of sizeof does not get folded to a constant by
2960 c_fully_fold, because if the size is evaluated the result is
2961 not constant and so constraints on zero or negative size
2962 arrays must not be applied when this sizeof call is inside
2963 another array declarator. */
2964 if (!type_expr)
2965 type_expr = integer_zero_node;
2966 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2967 type_expr, ret.value);
2968 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2969 }
2970 pop_maybe_used (type != error_mark_node
2971 ? C_TYPE_VARIABLE_SIZE (type) : false);
2972 return ret;
2973 }
2974
2975 /* Build a function call to function FUNCTION with parameters PARAMS.
2976 The function call is at LOC.
2977 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2978 TREE_VALUE of each node is a parameter-expression.
2979 FUNCTION's data type may be a function type or a pointer-to-function. */
2980
2981 tree
2982 build_function_call (location_t loc, tree function, tree params)
2983 {
2984 vec<tree, va_gc> *v;
2985 tree ret;
2986
2987 vec_alloc (v, list_length (params));
2988 for (; params; params = TREE_CHAIN (params))
2989 v->quick_push (TREE_VALUE (params));
2990 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2991 vec_free (v);
2992 return ret;
2993 }
2994
2995 /* Give a note about the location of the declaration of DECL. */
2996
2997 static void
2998 inform_declaration (tree decl)
2999 {
3000 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
3001 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3002 }
3003
3004 /* Build a function call to function FUNCTION with parameters PARAMS.
3005 ORIGTYPES, if not NULL, is a vector of types; each element is
3006 either NULL or the original type of the corresponding element in
3007 PARAMS. The original type may differ from TREE_TYPE of the
3008 parameter for enums. FUNCTION's data type may be a function type
3009 or pointer-to-function. This function changes the elements of
3010 PARAMS. */
3011
3012 tree
3013 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3014 tree function, vec<tree, va_gc> *params,
3015 vec<tree, va_gc> *origtypes)
3016 {
3017 tree fntype, fundecl = NULL_TREE;
3018 tree name = NULL_TREE, result;
3019 tree tem;
3020 int nargs;
3021 tree *argarray;
3022
3023
3024 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3025 STRIP_TYPE_NOPS (function);
3026
3027 /* Convert anything with function type to a pointer-to-function. */
3028 if (TREE_CODE (function) == FUNCTION_DECL)
3029 {
3030 name = DECL_NAME (function);
3031
3032 if (flag_tm)
3033 tm_malloc_replacement (function);
3034 fundecl = function;
3035 /* Atomic functions have type checking/casting already done. They are
3036 often rewritten and don't match the original parameter list. */
3037 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3038 origtypes = NULL;
3039 }
3040 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3041 function = function_to_pointer_conversion (loc, function);
3042
3043 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3044 expressions, like those used for ObjC messenger dispatches. */
3045 if (params && !params->is_empty ())
3046 function = objc_rewrite_function_call (function, (*params)[0]);
3047
3048 function = c_fully_fold (function, false, NULL);
3049
3050 fntype = TREE_TYPE (function);
3051
3052 if (TREE_CODE (fntype) == ERROR_MARK)
3053 return error_mark_node;
3054
3055 if (!(TREE_CODE (fntype) == POINTER_TYPE
3056 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3057 {
3058 if (!flag_diagnostics_show_caret)
3059 error_at (loc,
3060 "called object %qE is not a function or function pointer",
3061 function);
3062 else if (DECL_P (function))
3063 {
3064 error_at (loc,
3065 "called object %qD is not a function or function pointer",
3066 function);
3067 inform_declaration (function);
3068 }
3069 else
3070 error_at (loc,
3071 "called object is not a function or function pointer");
3072 return error_mark_node;
3073 }
3074
3075 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3076 current_function_returns_abnormally = 1;
3077
3078 /* fntype now gets the type of function pointed to. */
3079 fntype = TREE_TYPE (fntype);
3080
3081 /* Convert the parameters to the types declared in the
3082 function prototype, or apply default promotions. */
3083
3084 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3085 origtypes, function, fundecl);
3086 if (nargs < 0)
3087 return error_mark_node;
3088
3089 /* Check that the function is called through a compatible prototype.
3090 If it is not, warn. */
3091 if (CONVERT_EXPR_P (function)
3092 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3093 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3094 && !comptypes (fntype, TREE_TYPE (tem)))
3095 {
3096 tree return_type = TREE_TYPE (fntype);
3097
3098 /* This situation leads to run-time undefined behavior. We can't,
3099 therefore, simply error unless we can prove that all possible
3100 executions of the program must execute the code. */
3101 warning_at (loc, 0, "function called through a non-compatible type");
3102
3103 if (VOID_TYPE_P (return_type)
3104 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3105 pedwarn (loc, 0,
3106 "function with qualified void return type called");
3107 }
3108
3109 argarray = vec_safe_address (params);
3110
3111 /* Check that arguments to builtin functions match the expectations. */
3112 if (fundecl && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL)
3113 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3114 argarray))
3115 return error_mark_node;
3116
3117 /* Check that the arguments to the function are valid. */
3118 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3119 nargs, argarray, &arg_loc);
3120
3121 if (name != NULL_TREE
3122 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3123 {
3124 if (require_constant_value)
3125 result
3126 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3127 function, nargs, argarray);
3128 else
3129 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3130 function, nargs, argarray);
3131 if (TREE_CODE (result) == NOP_EXPR
3132 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3133 STRIP_TYPE_NOPS (result);
3134 }
3135 else
3136 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3137 function, nargs, argarray);
3138 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3139 later. */
3140 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3141 TREE_NO_WARNING (result) = 1;
3142
3143 /* In this improbable scenario, a nested function returns a VM type.
3144 Create a TARGET_EXPR so that the call always has a LHS, much as
3145 what the C++ FE does for functions returning non-PODs. */
3146 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3147 {
3148 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3149 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3150 NULL_TREE, NULL_TREE);
3151 }
3152
3153 if (VOID_TYPE_P (TREE_TYPE (result)))
3154 {
3155 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3156 pedwarn (loc, 0,
3157 "function with qualified void return type called");
3158 return result;
3159 }
3160 return require_complete_type (loc, result);
3161 }
3162
3163 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3164
3165 tree
3166 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3167 tree function, vec<tree, va_gc> *params,
3168 vec<tree, va_gc> *origtypes)
3169 {
3170 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3171 STRIP_TYPE_NOPS (function);
3172
3173 /* Convert anything with function type to a pointer-to-function. */
3174 if (TREE_CODE (function) == FUNCTION_DECL)
3175 {
3176 /* Implement type-directed function overloading for builtins.
3177 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3178 handle all the type checking. The result is a complete expression
3179 that implements this function call. */
3180 tree tem = resolve_overloaded_builtin (loc, function, params);
3181 if (tem)
3182 return tem;
3183 }
3184 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3185 }
3186 \f
3187 /* Helper for convert_arguments called to convert the VALue of argument
3188 number ARGNUM from ORIGTYPE to the corresponding parameter number
3189 PARMNUM and TYPE.
3190 PLOC is the location where the conversion is being performed.
3191 FUNCTION and FUNDECL are the same as in convert_arguments.
3192 VALTYPE is the original type of VAL before the conversion and,
3193 for EXCESS_PRECISION_EXPR, the operand of the expression.
3194 NPC is true if VAL represents the null pointer constant (VAL itself
3195 will have been folded to an integer constant).
3196 RNAME is the same as FUNCTION except in Objective C when it's
3197 the function selector.
3198 EXCESS_PRECISION is true when VAL was originally represented
3199 as EXCESS_PRECISION_EXPR.
3200 WARNOPT is the same as in convert_for_assignment. */
3201
3202 static tree
3203 convert_argument (location_t ploc, tree function, tree fundecl,
3204 tree type, tree origtype, tree val, tree valtype,
3205 bool npc, tree rname, int parmnum, int argnum,
3206 bool excess_precision, int warnopt)
3207 {
3208 /* Formal parm type is specified by a function prototype. */
3209
3210 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3211 {
3212 error_at (ploc, "type of formal parameter %d is incomplete",
3213 parmnum + 1);
3214 return val;
3215 }
3216
3217 /* Optionally warn about conversions that differ from the default
3218 conversions. */
3219 if (warn_traditional_conversion || warn_traditional)
3220 {
3221 unsigned int formal_prec = TYPE_PRECISION (type);
3222
3223 if (INTEGRAL_TYPE_P (type)
3224 && TREE_CODE (valtype) == REAL_TYPE)
3225 warning_at (ploc, OPT_Wtraditional_conversion,
3226 "passing argument %d of %qE as integer rather "
3227 "than floating due to prototype",
3228 argnum, rname);
3229 if (INTEGRAL_TYPE_P (type)
3230 && TREE_CODE (valtype) == COMPLEX_TYPE)
3231 warning_at (ploc, OPT_Wtraditional_conversion,
3232 "passing argument %d of %qE as integer rather "
3233 "than complex due to prototype",
3234 argnum, rname);
3235 else if (TREE_CODE (type) == COMPLEX_TYPE
3236 && TREE_CODE (valtype) == REAL_TYPE)
3237 warning_at (ploc, OPT_Wtraditional_conversion,
3238 "passing argument %d of %qE as complex rather "
3239 "than floating due to prototype",
3240 argnum, rname);
3241 else if (TREE_CODE (type) == REAL_TYPE
3242 && INTEGRAL_TYPE_P (valtype))
3243 warning_at (ploc, OPT_Wtraditional_conversion,
3244 "passing argument %d of %qE as floating rather "
3245 "than integer due to prototype",
3246 argnum, rname);
3247 else if (TREE_CODE (type) == COMPLEX_TYPE
3248 && INTEGRAL_TYPE_P (valtype))
3249 warning_at (ploc, OPT_Wtraditional_conversion,
3250 "passing argument %d of %qE as complex rather "
3251 "than integer due to prototype",
3252 argnum, rname);
3253 else if (TREE_CODE (type) == REAL_TYPE
3254 && TREE_CODE (valtype) == COMPLEX_TYPE)
3255 warning_at (ploc, OPT_Wtraditional_conversion,
3256 "passing argument %d of %qE as floating rather "
3257 "than complex due to prototype",
3258 argnum, rname);
3259 /* ??? At some point, messages should be written about
3260 conversions between complex types, but that's too messy
3261 to do now. */
3262 else if (TREE_CODE (type) == REAL_TYPE
3263 && TREE_CODE (valtype) == REAL_TYPE)
3264 {
3265 /* Warn if any argument is passed as `float',
3266 since without a prototype it would be `double'. */
3267 if (formal_prec == TYPE_PRECISION (float_type_node)
3268 && type != dfloat32_type_node)
3269 warning_at (ploc, 0,
3270 "passing argument %d of %qE as %<float%> "
3271 "rather than %<double%> due to prototype",
3272 argnum, rname);
3273
3274 /* Warn if mismatch between argument and prototype
3275 for decimal float types. Warn of conversions with
3276 binary float types and of precision narrowing due to
3277 prototype. */
3278 else if (type != valtype
3279 && (type == dfloat32_type_node
3280 || type == dfloat64_type_node
3281 || type == dfloat128_type_node
3282 || valtype == dfloat32_type_node
3283 || valtype == dfloat64_type_node
3284 || valtype == dfloat128_type_node)
3285 && (formal_prec
3286 <= TYPE_PRECISION (valtype)
3287 || (type == dfloat128_type_node
3288 && (valtype
3289 != dfloat64_type_node
3290 && (valtype
3291 != dfloat32_type_node)))
3292 || (type == dfloat64_type_node
3293 && (valtype
3294 != dfloat32_type_node))))
3295 warning_at (ploc, 0,
3296 "passing argument %d of %qE as %qT "
3297 "rather than %qT due to prototype",
3298 argnum, rname, type, valtype);
3299
3300 }
3301 /* Detect integer changing in width or signedness.
3302 These warnings are only activated with
3303 -Wtraditional-conversion, not with -Wtraditional. */
3304 else if (warn_traditional_conversion
3305 && INTEGRAL_TYPE_P (type)
3306 && INTEGRAL_TYPE_P (valtype))
3307 {
3308 tree would_have_been = default_conversion (val);
3309 tree type1 = TREE_TYPE (would_have_been);
3310
3311 if (val == error_mark_node)
3312 /* VAL could have been of incomplete type. */;
3313 else if (TREE_CODE (type) == ENUMERAL_TYPE
3314 && (TYPE_MAIN_VARIANT (type)
3315 == TYPE_MAIN_VARIANT (valtype)))
3316 /* No warning if function asks for enum
3317 and the actual arg is that enum type. */
3318 ;
3319 else if (formal_prec != TYPE_PRECISION (type1))
3320 warning_at (ploc, OPT_Wtraditional_conversion,
3321 "passing argument %d of %qE "
3322 "with different width due to prototype",
3323 argnum, rname);
3324 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3325 ;
3326 /* Don't complain if the formal parameter type
3327 is an enum, because we can't tell now whether
3328 the value was an enum--even the same enum. */
3329 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3330 ;
3331 else if (TREE_CODE (val) == INTEGER_CST
3332 && int_fits_type_p (val, type))
3333 /* Change in signedness doesn't matter
3334 if a constant value is unaffected. */
3335 ;
3336 /* If the value is extended from a narrower
3337 unsigned type, it doesn't matter whether we
3338 pass it as signed or unsigned; the value
3339 certainly is the same either way. */
3340 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3341 && TYPE_UNSIGNED (valtype))
3342 ;
3343 else if (TYPE_UNSIGNED (type))
3344 warning_at (ploc, OPT_Wtraditional_conversion,
3345 "passing argument %d of %qE "
3346 "as unsigned due to prototype",
3347 argnum, rname);
3348 else
3349 warning_at (ploc, OPT_Wtraditional_conversion,
3350 "passing argument %d of %qE "
3351 "as signed due to prototype",
3352 argnum, rname);
3353 }
3354 }
3355
3356 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3357 sake of better warnings from convert_and_check. */
3358 if (excess_precision)
3359 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3360
3361 tree parmval = convert_for_assignment (ploc, ploc, type,
3362 val, origtype, ic_argpass,
3363 npc, fundecl, function,
3364 parmnum + 1, warnopt);
3365
3366 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3367 && INTEGRAL_TYPE_P (type)
3368 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3369 parmval = default_conversion (parmval);
3370
3371 return parmval;
3372 }
3373
3374 /* Convert the argument expressions in the vector VALUES
3375 to the types in the list TYPELIST.
3376
3377 If TYPELIST is exhausted, or when an element has NULL as its type,
3378 perform the default conversions.
3379
3380 ORIGTYPES is the original types of the expressions in VALUES. This
3381 holds the type of enum values which have been converted to integral
3382 types. It may be NULL.
3383
3384 FUNCTION is a tree for the called function. It is used only for
3385 error messages, where it is formatted with %qE.
3386
3387 This is also where warnings about wrong number of args are generated.
3388
3389 ARG_LOC are locations of function arguments (if any).
3390
3391 Returns the actual number of arguments processed (which may be less
3392 than the length of VALUES in some error situations), or -1 on
3393 failure. */
3394
3395 static int
3396 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3397 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3398 tree function, tree fundecl)
3399 {
3400 unsigned int parmnum;
3401 bool error_args = false;
3402 const bool type_generic = fundecl
3403 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3404 bool type_generic_remove_excess_precision = false;
3405 bool type_generic_overflow_p = false;
3406 tree selector;
3407
3408 /* Change pointer to function to the function itself for
3409 diagnostics. */
3410 if (TREE_CODE (function) == ADDR_EXPR
3411 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3412 function = TREE_OPERAND (function, 0);
3413
3414 /* Handle an ObjC selector specially for diagnostics. */
3415 selector = objc_message_selector ();
3416
3417 /* For a call to a built-in function declared without a prototype,
3418 set to the built-in function's argument list. */
3419 tree builtin_typelist = NULL_TREE;
3420
3421 /* For type-generic built-in functions, determine whether excess
3422 precision should be removed (classification) or not
3423 (comparison). */
3424 if (fundecl
3425 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3426 {
3427 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3428 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3429 {
3430 /* For a call to a built-in function declared without a prototype
3431 use the types of the parameters of the internal built-in to
3432 match those of the arguments to. */
3433 if (tree bdecl = builtin_decl_explicit (code))
3434 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3435 }
3436
3437 /* For type-generic built-in functions, determine whether excess
3438 precision should be removed (classification) or not
3439 (comparison). */
3440 if (type_generic)
3441 switch (code)
3442 {
3443 case BUILT_IN_ISFINITE:
3444 case BUILT_IN_ISINF:
3445 case BUILT_IN_ISINF_SIGN:
3446 case BUILT_IN_ISNAN:
3447 case BUILT_IN_ISNORMAL:
3448 case BUILT_IN_FPCLASSIFY:
3449 type_generic_remove_excess_precision = true;
3450 break;
3451
3452 case BUILT_IN_ADD_OVERFLOW_P:
3453 case BUILT_IN_SUB_OVERFLOW_P:
3454 case BUILT_IN_MUL_OVERFLOW_P:
3455 /* The last argument of these type-generic builtins
3456 should not be promoted. */
3457 type_generic_overflow_p = true;
3458 break;
3459
3460 default:
3461 break;
3462 }
3463 }
3464
3465 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3466 individual converted arguments. */
3467
3468 tree typetail, builtin_typetail, val;
3469 for (typetail = typelist,
3470 builtin_typetail = builtin_typelist,
3471 parmnum = 0;
3472 values && values->iterate (parmnum, &val);
3473 ++parmnum)
3474 {
3475 /* The type of the function parameter (if it was declared with one). */
3476 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3477 /* The type of the built-in function parameter (if the function
3478 is a built-in). Used to detect type incompatibilities in
3479 calls to built-ins declared without a prototype. */
3480 tree builtin_type = (builtin_typetail
3481 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3482 /* The original type of the argument being passed to the function. */
3483 tree valtype = TREE_TYPE (val);
3484 /* The called function (or function selector in Objective C). */
3485 tree rname = function;
3486 int argnum = parmnum + 1;
3487 const char *invalid_func_diag;
3488 /* Set for EXCESS_PRECISION_EXPR arguments. */
3489 bool excess_precision = false;
3490 /* The value of the argument after conversion to the type
3491 of the function parameter it is passed to. */
3492 tree parmval;
3493 /* Some __atomic_* builtins have additional hidden argument at
3494 position 0. */
3495 location_t ploc
3496 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3497 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3498 : input_location;
3499
3500 if (type == void_type_node)
3501 {
3502 if (selector)
3503 error_at (loc, "too many arguments to method %qE", selector);
3504 else
3505 error_at (loc, "too many arguments to function %qE", function);
3506 inform_declaration (fundecl);
3507 return error_args ? -1 : (int) parmnum;
3508 }
3509
3510 if (builtin_type == void_type_node)
3511 {
3512 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3513 "too many arguments to built-in function %qE "
3514 "expecting %d", function, parmnum))
3515 inform_declaration (fundecl);
3516 builtin_typetail = NULL_TREE;
3517 }
3518
3519 if (selector && argnum > 2)
3520 {
3521 rname = selector;
3522 argnum -= 2;
3523 }
3524
3525 /* Determine if VAL is a null pointer constant before folding it. */
3526 bool npc = null_pointer_constant_p (val);
3527
3528 /* If there is excess precision and a prototype, convert once to
3529 the required type rather than converting via the semantic
3530 type. Likewise without a prototype a float value represented
3531 as long double should be converted once to double. But for
3532 type-generic classification functions excess precision must
3533 be removed here. */
3534 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3535 && (type || !type_generic || !type_generic_remove_excess_precision))
3536 {
3537 val = TREE_OPERAND (val, 0);
3538 excess_precision = true;
3539 }
3540 val = c_fully_fold (val, false, NULL);
3541 STRIP_TYPE_NOPS (val);
3542
3543 val = require_complete_type (ploc, val);
3544
3545 /* Some floating-point arguments must be promoted to double when
3546 no type is specified by a prototype. This applies to
3547 arguments of type float, and to architecture-specific types
3548 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3549 bool promote_float_arg = false;
3550 if (type == NULL_TREE
3551 && TREE_CODE (valtype) == REAL_TYPE
3552 && (TYPE_PRECISION (valtype)
3553 <= TYPE_PRECISION (double_type_node))
3554 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3555 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3556 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3557 {
3558 /* Promote this argument, unless it has a _FloatN or
3559 _FloatNx type. */
3560 promote_float_arg = true;
3561 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3562 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3563 {
3564 promote_float_arg = false;
3565 break;
3566 }
3567 }
3568
3569 if (type != NULL_TREE)
3570 {
3571 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3572 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3573 val, valtype, npc, rname, parmnum, argnum,
3574 excess_precision, 0);
3575 }
3576 else if (promote_float_arg)
3577 {
3578 if (type_generic)
3579 parmval = val;
3580 else
3581 {
3582 /* Convert `float' to `double'. */
3583 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3584 warning_at (ploc, OPT_Wdouble_promotion,
3585 "implicit conversion from %qT to %qT when passing "
3586 "argument to function",
3587 valtype, double_type_node);
3588 parmval = convert (double_type_node, val);
3589 }
3590 }
3591 else if ((excess_precision && !type_generic)
3592 || (type_generic_overflow_p && parmnum == 2))
3593 /* A "double" argument with excess precision being passed
3594 without a prototype or in variable arguments.
3595 The last argument of __builtin_*_overflow_p should not be
3596 promoted. */
3597 parmval = convert (valtype, val);
3598 else if ((invalid_func_diag =
3599 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3600 {
3601 error (invalid_func_diag);
3602 return -1;
3603 }
3604 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3605 {
3606 return -1;
3607 }
3608 else
3609 /* Convert `short' and `char' to full-size `int'. */
3610 parmval = default_conversion (val);
3611
3612 (*values)[parmnum] = parmval;
3613 if (parmval == error_mark_node)
3614 error_args = true;
3615
3616 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3617 {
3618 /* For a call to a built-in function declared without a prototype,
3619 perform the conversions from the argument to the expected type
3620 but issue warnings rather than errors for any mismatches.
3621 Ignore the converted argument and use the PARMVAL obtained
3622 above by applying default conversions instead. */
3623 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3624 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3625 val, valtype, npc, rname, parmnum, argnum,
3626 excess_precision,
3627 OPT_Wbuiltin_declaration_mismatch);
3628 }
3629
3630 if (typetail)
3631 typetail = TREE_CHAIN (typetail);
3632
3633 if (builtin_typetail)
3634 builtin_typetail = TREE_CHAIN (builtin_typetail);
3635 }
3636
3637 gcc_assert (parmnum == vec_safe_length (values));
3638
3639 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3640 {
3641 error_at (loc, "too few arguments to function %qE", function);
3642 inform_declaration (fundecl);
3643 return -1;
3644 }
3645
3646 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3647 {
3648 unsigned nargs = parmnum;
3649 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3650 ++nargs;
3651
3652 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3653 "too few arguments to built-in function %qE "
3654 "expecting %u", function, nargs - 1))
3655 inform_declaration (fundecl);
3656 }
3657
3658 return error_args ? -1 : (int) parmnum;
3659 }
3660 \f
3661 /* This is the entry point used by the parser to build unary operators
3662 in the input. CODE, a tree_code, specifies the unary operator, and
3663 ARG is the operand. For unary plus, the C parser currently uses
3664 CONVERT_EXPR for code.
3665
3666 LOC is the location to use for the tree generated.
3667 */
3668
3669 struct c_expr
3670 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3671 {
3672 struct c_expr result;
3673
3674 result.original_code = code;
3675 result.original_type = NULL;
3676
3677 if (reject_gcc_builtin (arg.value))
3678 {
3679 result.value = error_mark_node;
3680 }
3681 else
3682 {
3683 result.value = build_unary_op (loc, code, arg.value, false);
3684
3685 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3686 overflow_warning (loc, result.value, arg.value);
3687 }
3688
3689 /* We are typically called when parsing a prefix token at LOC acting on
3690 ARG. Reflect this by updating the source range of the result to
3691 start at LOC and end at the end of ARG. */
3692 set_c_expr_source_range (&result,
3693 loc, arg.get_finish ());
3694
3695 return result;
3696 }
3697
3698 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3699
3700 static bool
3701 char_type_p (tree type)
3702 {
3703 return (type == char_type_node
3704 || type == unsigned_char_type_node
3705 || type == signed_char_type_node
3706 || type == char16_type_node
3707 || type == char32_type_node);
3708 }
3709
3710 /* This is the entry point used by the parser to build binary operators
3711 in the input. CODE, a tree_code, specifies the binary operator, and
3712 ARG1 and ARG2 are the operands. In addition to constructing the
3713 expression, we check for operands that were written with other binary
3714 operators in a way that is likely to confuse the user.
3715
3716 LOCATION is the location of the binary operator. */
3717
3718 struct c_expr
3719 parser_build_binary_op (location_t location, enum tree_code code,
3720 struct c_expr arg1, struct c_expr arg2)
3721 {
3722 struct c_expr result;
3723
3724 enum tree_code code1 = arg1.original_code;
3725 enum tree_code code2 = arg2.original_code;
3726 tree type1 = (arg1.original_type
3727 ? arg1.original_type
3728 : TREE_TYPE (arg1.value));
3729 tree type2 = (arg2.original_type
3730 ? arg2.original_type
3731 : TREE_TYPE (arg2.value));
3732
3733 result.value = build_binary_op (location, code,
3734 arg1.value, arg2.value, true);
3735 result.original_code = code;
3736 result.original_type = NULL;
3737
3738 if (TREE_CODE (result.value) == ERROR_MARK)
3739 {
3740 set_c_expr_source_range (&result,
3741 arg1.get_start (),
3742 arg2.get_finish ());
3743 return result;
3744 }
3745
3746 if (location != UNKNOWN_LOCATION)
3747 protected_set_expr_location (result.value, location);
3748
3749 set_c_expr_source_range (&result,
3750 arg1.get_start (),
3751 arg2.get_finish ());
3752
3753 /* Check for cases such as x+y<<z which users are likely
3754 to misinterpret. */
3755 if (warn_parentheses)
3756 warn_about_parentheses (location, code, code1, arg1.value, code2,
3757 arg2.value);
3758
3759 if (warn_logical_op)
3760 warn_logical_operator (location, code, TREE_TYPE (result.value),
3761 code1, arg1.value, code2, arg2.value);
3762
3763 if (warn_tautological_compare)
3764 {
3765 tree lhs = arg1.value;
3766 tree rhs = arg2.value;
3767 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3768 {
3769 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3770 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3771 lhs = NULL_TREE;
3772 else
3773 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3774 }
3775 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3776 {
3777 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3778 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3779 rhs = NULL_TREE;
3780 else
3781 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3782 }
3783 if (lhs != NULL_TREE && rhs != NULL_TREE)
3784 warn_tautological_cmp (location, code, lhs, rhs);
3785 }
3786
3787 if (warn_logical_not_paren
3788 && TREE_CODE_CLASS (code) == tcc_comparison
3789 && code1 == TRUTH_NOT_EXPR
3790 && code2 != TRUTH_NOT_EXPR
3791 /* Avoid warning for !!x == y. */
3792 && (TREE_CODE (arg1.value) != NE_EXPR
3793 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3794 {
3795 /* Avoid warning for !b == y where b has _Bool type. */
3796 tree t = integer_zero_node;
3797 if (TREE_CODE (arg1.value) == EQ_EXPR
3798 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3799 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3800 {
3801 t = TREE_OPERAND (arg1.value, 0);
3802 do
3803 {
3804 if (TREE_TYPE (t) != integer_type_node)
3805 break;
3806 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3807 t = C_MAYBE_CONST_EXPR_EXPR (t);
3808 else if (CONVERT_EXPR_P (t))
3809 t = TREE_OPERAND (t, 0);
3810 else
3811 break;
3812 }
3813 while (1);
3814 }
3815 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3816 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3817 }
3818
3819 /* Warn about comparisons against string literals, with the exception
3820 of testing for equality or inequality of a string literal with NULL. */
3821 if (code == EQ_EXPR || code == NE_EXPR)
3822 {
3823 if ((code1 == STRING_CST
3824 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3825 || (code2 == STRING_CST
3826 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3827 warning_at (location, OPT_Waddress,
3828 "comparison with string literal results in unspecified behavior");
3829 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3830 if (POINTER_TYPE_P (type1)
3831 && null_pointer_constant_p (arg2.value)
3832 && char_type_p (type2))
3833 {
3834 auto_diagnostic_group d;
3835 if (warning_at (location, OPT_Wpointer_compare,
3836 "comparison between pointer and zero character "
3837 "constant"))
3838 inform (arg1.get_start (),
3839 "did you mean to dereference the pointer?");
3840 }
3841 else if (POINTER_TYPE_P (type2)
3842 && null_pointer_constant_p (arg1.value)
3843 && char_type_p (type1))
3844 {
3845 auto_diagnostic_group d;
3846 if (warning_at (location, OPT_Wpointer_compare,
3847 "comparison between pointer and zero character "
3848 "constant"))
3849 inform (arg2.get_start (),
3850 "did you mean to dereference the pointer?");
3851 }
3852 }
3853 else if (TREE_CODE_CLASS (code) == tcc_comparison
3854 && (code1 == STRING_CST || code2 == STRING_CST))
3855 warning_at (location, OPT_Waddress,
3856 "comparison with string literal results in unspecified behavior");
3857
3858 if (TREE_OVERFLOW_P (result.value)
3859 && !TREE_OVERFLOW_P (arg1.value)
3860 && !TREE_OVERFLOW_P (arg2.value))
3861 overflow_warning (location, result.value);
3862
3863 /* Warn about comparisons of different enum types. */
3864 if (warn_enum_compare
3865 && TREE_CODE_CLASS (code) == tcc_comparison
3866 && TREE_CODE (type1) == ENUMERAL_TYPE
3867 && TREE_CODE (type2) == ENUMERAL_TYPE
3868 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3869 warning_at (location, OPT_Wenum_compare,
3870 "comparison between %qT and %qT",
3871 type1, type2);
3872
3873 return result;
3874 }
3875 \f
3876 /* Return a tree for the difference of pointers OP0 and OP1.
3877 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3878 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3879
3880 static tree
3881 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3882 {
3883 tree restype = ptrdiff_type_node;
3884 tree result, inttype;
3885
3886 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3887 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3888 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3889 tree orig_op1 = op1;
3890
3891 /* If the operands point into different address spaces, we need to
3892 explicitly convert them to pointers into the common address space
3893 before we can subtract the numerical address values. */
3894 if (as0 != as1)
3895 {
3896 addr_space_t as_common;
3897 tree common_type;
3898
3899 /* Determine the common superset address space. This is guaranteed
3900 to exist because the caller verified that comp_target_types
3901 returned non-zero. */
3902 if (!addr_space_superset (as0, as1, &as_common))
3903 gcc_unreachable ();
3904
3905 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3906 op0 = convert (common_type, op0);
3907 op1 = convert (common_type, op1);
3908 }
3909
3910 /* Determine integer type result of the subtraction. This will usually
3911 be the same as the result type (ptrdiff_t), but may need to be a wider
3912 type if pointers for the address space are wider than ptrdiff_t. */
3913 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3914 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3915 else
3916 inttype = restype;
3917
3918 if (TREE_CODE (target_type) == VOID_TYPE)
3919 pedwarn (loc, OPT_Wpointer_arith,
3920 "pointer of type %<void *%> used in subtraction");
3921 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3922 pedwarn (loc, OPT_Wpointer_arith,
3923 "pointer to a function used in subtraction");
3924
3925 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3926 {
3927 gcc_assert (current_function_decl != NULL_TREE);
3928
3929 op0 = save_expr (op0);
3930 op1 = save_expr (op1);
3931
3932 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3933 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3934 }
3935
3936 /* First do the subtraction, then build the divide operator
3937 and only convert at the very end.
3938 Do not do default conversions in case restype is a short type. */
3939
3940 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3941 pointers. If some platform cannot provide that, or has a larger
3942 ptrdiff_type to support differences larger than half the address
3943 space, cast the pointers to some larger integer type and do the
3944 computations in that type. */
3945 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3946 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3947 convert (inttype, op1), false);
3948 else
3949 {
3950 /* Cast away qualifiers. */
3951 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
3952 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
3953 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3954 }
3955
3956 /* This generates an error if op1 is pointer to incomplete type. */
3957 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3958 error_at (loc, "arithmetic on pointer to an incomplete type");
3959
3960 op1 = c_size_in_bytes (target_type);
3961
3962 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3963 error_at (loc, "arithmetic on pointer to an empty aggregate");
3964
3965 /* Divide by the size, in easiest possible way. */
3966 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3967 op0, convert (inttype, op1));
3968
3969 /* Convert to final result type if necessary. */
3970 return convert (restype, result);
3971 }
3972 \f
3973 /* Expand atomic compound assignments into an appropriate sequence as
3974 specified by the C11 standard section 6.5.16.2.
3975
3976 _Atomic T1 E1
3977 T2 E2
3978 E1 op= E2
3979
3980 This sequence is used for all types for which these operations are
3981 supported.
3982
3983 In addition, built-in versions of the 'fe' prefixed routines may
3984 need to be invoked for floating point (real, complex or vector) when
3985 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3986
3987 T1 newval;
3988 T1 old;
3989 T1 *addr
3990 T2 val
3991 fenv_t fenv
3992
3993 addr = &E1;
3994 val = (E2);
3995 __atomic_load (addr, &old, SEQ_CST);
3996 feholdexcept (&fenv);
3997 loop:
3998 newval = old op val;
3999 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4000 SEQ_CST))
4001 goto done;
4002 feclearexcept (FE_ALL_EXCEPT);
4003 goto loop:
4004 done:
4005 feupdateenv (&fenv);
4006
4007 The compiler will issue the __atomic_fetch_* built-in when possible,
4008 otherwise it will generate the generic form of the atomic operations.
4009 This requires temp(s) and has their address taken. The atomic processing
4010 is smart enough to figure out when the size of an object can utilize
4011 a lock-free version, and convert the built-in call to the appropriate
4012 lock-free routine. The optimizers will then dispose of any temps that
4013 are no longer required, and lock-free implementations are utilized as
4014 long as there is target support for the required size.
4015
4016 If the operator is NOP_EXPR, then this is a simple assignment, and
4017 an __atomic_store is issued to perform the assignment rather than
4018 the above loop. */
4019
4020 /* Build an atomic assignment at LOC, expanding into the proper
4021 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4022 the result of the operation, unless RETURN_OLD_P, in which case
4023 return the old value of LHS (this is only for postincrement and
4024 postdecrement). */
4025
4026 static tree
4027 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4028 tree rhs, bool return_old_p)
4029 {
4030 tree fndecl, func_call;
4031 vec<tree, va_gc> *params;
4032 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4033 tree old, old_addr;
4034 tree compound_stmt;
4035 tree stmt, goto_stmt;
4036 tree loop_label, loop_decl, done_label, done_decl;
4037
4038 tree lhs_type = TREE_TYPE (lhs);
4039 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4040 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4041 tree rhs_semantic_type = TREE_TYPE (rhs);
4042 tree nonatomic_rhs_semantic_type;
4043 tree rhs_type;
4044
4045 gcc_assert (TYPE_ATOMIC (lhs_type));
4046
4047 if (return_old_p)
4048 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4049
4050 /* Allocate enough vector items for a compare_exchange. */
4051 vec_alloc (params, 6);
4052
4053 /* Create a compound statement to hold the sequence of statements
4054 with a loop. */
4055 compound_stmt = c_begin_compound_stmt (false);
4056
4057 /* Remove any excess precision (which is only present here in the
4058 case of compound assignments). */
4059 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4060 {
4061 gcc_assert (modifycode != NOP_EXPR);
4062 rhs = TREE_OPERAND (rhs, 0);
4063 }
4064 rhs_type = TREE_TYPE (rhs);
4065
4066 /* Fold the RHS if it hasn't already been folded. */
4067 if (modifycode != NOP_EXPR)
4068 rhs = c_fully_fold (rhs, false, NULL);
4069
4070 /* Remove the qualifiers for the rest of the expressions and create
4071 the VAL temp variable to hold the RHS. */
4072 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4073 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4074 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4075 TYPE_UNQUALIFIED);
4076 val = create_tmp_var_raw (nonatomic_rhs_type);
4077 TREE_ADDRESSABLE (val) = 1;
4078 TREE_NO_WARNING (val) = 1;
4079 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4080 NULL_TREE);
4081 SET_EXPR_LOCATION (rhs, loc);
4082 add_stmt (rhs);
4083
4084 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4085 an atomic_store. */
4086 if (modifycode == NOP_EXPR)
4087 {
4088 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4089 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4090 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4091 params->quick_push (lhs_addr);
4092 params->quick_push (rhs);
4093 params->quick_push (seq_cst);
4094 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4095 add_stmt (func_call);
4096
4097 /* Finish the compound statement. */
4098 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4099
4100 /* VAL is the value which was stored, return a COMPOUND_STMT of
4101 the statement and that value. */
4102 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4103 }
4104
4105 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4106 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4107 isn't applicable for such builtins. ??? Do we want to handle enums? */
4108 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4109 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4110 {
4111 built_in_function fncode;
4112 switch (modifycode)
4113 {
4114 case PLUS_EXPR:
4115 case POINTER_PLUS_EXPR:
4116 fncode = (return_old_p
4117 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4118 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4119 break;
4120 case MINUS_EXPR:
4121 fncode = (return_old_p
4122 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4123 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4124 break;
4125 case BIT_AND_EXPR:
4126 fncode = (return_old_p
4127 ? BUILT_IN_ATOMIC_FETCH_AND_N
4128 : BUILT_IN_ATOMIC_AND_FETCH_N);
4129 break;
4130 case BIT_IOR_EXPR:
4131 fncode = (return_old_p
4132 ? BUILT_IN_ATOMIC_FETCH_OR_N
4133 : BUILT_IN_ATOMIC_OR_FETCH_N);
4134 break;
4135 case BIT_XOR_EXPR:
4136 fncode = (return_old_p
4137 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4138 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4139 break;
4140 default:
4141 goto cas_loop;
4142 }
4143
4144 /* We can only use "_1" through "_16" variants of the atomic fetch
4145 built-ins. */
4146 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4147 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4148 goto cas_loop;
4149
4150 /* If this is a pointer type, we need to multiply by the size of
4151 the pointer target type. */
4152 if (POINTER_TYPE_P (lhs_type))
4153 {
4154 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4155 /* ??? This would introduce -Wdiscarded-qualifiers
4156 warning: __atomic_fetch_* expect volatile void *
4157 type as the first argument. (Assignments between
4158 atomic and non-atomic objects are OK.) */
4159 || TYPE_RESTRICT (lhs_type))
4160 goto cas_loop;
4161 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4162 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4163 convert (ptrdiff_type_node, rhs),
4164 convert (ptrdiff_type_node, sz));
4165 }
4166
4167 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4168 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4169 fndecl = builtin_decl_explicit (fncode);
4170 params->quick_push (lhs_addr);
4171 params->quick_push (rhs);
4172 params->quick_push (seq_cst);
4173 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4174
4175 newval = create_tmp_var_raw (nonatomic_lhs_type);
4176 TREE_ADDRESSABLE (newval) = 1;
4177 TREE_NO_WARNING (newval) = 1;
4178 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4179 NULL_TREE, NULL_TREE);
4180 SET_EXPR_LOCATION (rhs, loc);
4181 add_stmt (rhs);
4182
4183 /* Finish the compound statement. */
4184 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4185
4186 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4187 the statement and that value. */
4188 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4189 }
4190
4191 cas_loop:
4192 /* Create the variables and labels required for the op= form. */
4193 old = create_tmp_var_raw (nonatomic_lhs_type);
4194 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4195 TREE_ADDRESSABLE (old) = 1;
4196 TREE_NO_WARNING (old) = 1;
4197
4198 newval = create_tmp_var_raw (nonatomic_lhs_type);
4199 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4200 TREE_ADDRESSABLE (newval) = 1;
4201 TREE_NO_WARNING (newval) = 1;
4202
4203 loop_decl = create_artificial_label (loc);
4204 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4205
4206 done_decl = create_artificial_label (loc);
4207 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4208
4209 /* __atomic_load (addr, &old, SEQ_CST). */
4210 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4211 params->quick_push (lhs_addr);
4212 params->quick_push (old_addr);
4213 params->quick_push (seq_cst);
4214 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4215 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4216 NULL_TREE);
4217 add_stmt (old);
4218 params->truncate (0);
4219
4220 /* Create the expressions for floating-point environment
4221 manipulation, if required. */
4222 bool need_fenv = (flag_trapping_math
4223 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4224 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4225 if (need_fenv)
4226 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4227
4228 if (hold_call)
4229 add_stmt (hold_call);
4230
4231 /* loop: */
4232 add_stmt (loop_label);
4233
4234 /* newval = old + val; */
4235 if (rhs_type != rhs_semantic_type)
4236 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4237 rhs = build_binary_op (loc, modifycode, old, val, true);
4238 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4239 {
4240 tree eptype = TREE_TYPE (rhs);
4241 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4242 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4243 }
4244 else
4245 rhs = c_fully_fold (rhs, false, NULL);
4246 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4247 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4248 NULL_TREE, 0);
4249 if (rhs != error_mark_node)
4250 {
4251 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4252 NULL_TREE);
4253 SET_EXPR_LOCATION (rhs, loc);
4254 add_stmt (rhs);
4255 }
4256
4257 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4258 goto done; */
4259 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4260 params->quick_push (lhs_addr);
4261 params->quick_push (old_addr);
4262 params->quick_push (newval_addr);
4263 params->quick_push (integer_zero_node);
4264 params->quick_push (seq_cst);
4265 params->quick_push (seq_cst);
4266 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4267
4268 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4269 SET_EXPR_LOCATION (goto_stmt, loc);
4270
4271 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4272 SET_EXPR_LOCATION (stmt, loc);
4273 add_stmt (stmt);
4274
4275 if (clear_call)
4276 add_stmt (clear_call);
4277
4278 /* goto loop; */
4279 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4280 SET_EXPR_LOCATION (goto_stmt, loc);
4281 add_stmt (goto_stmt);
4282
4283 /* done: */
4284 add_stmt (done_label);
4285
4286 if (update_call)
4287 add_stmt (update_call);
4288
4289 /* Finish the compound statement. */
4290 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4291
4292 /* NEWVAL is the value that was successfully stored, return a
4293 COMPOUND_EXPR of the statement and the appropriate value. */
4294 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4295 return_old_p ? old : newval);
4296 }
4297
4298 /* Construct and perhaps optimize a tree representation
4299 for a unary operation. CODE, a tree_code, specifies the operation
4300 and XARG is the operand.
4301 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4302 promotions (such as from short to int).
4303 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4304 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4305 to pointers in C99.
4306
4307 LOCATION is the location of the operator. */
4308
4309 tree
4310 build_unary_op (location_t location, enum tree_code code, tree xarg,
4311 bool noconvert)
4312 {
4313 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4314 tree arg = xarg;
4315 tree argtype = NULL_TREE;
4316 enum tree_code typecode;
4317 tree val;
4318 tree ret = error_mark_node;
4319 tree eptype = NULL_TREE;
4320 const char *invalid_op_diag;
4321 bool int_operands;
4322
4323 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4324 if (int_operands)
4325 arg = remove_c_maybe_const_expr (arg);
4326
4327 if (code != ADDR_EXPR)
4328 arg = require_complete_type (location, arg);
4329
4330 typecode = TREE_CODE (TREE_TYPE (arg));
4331 if (typecode == ERROR_MARK)
4332 return error_mark_node;
4333 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4334 typecode = INTEGER_TYPE;
4335
4336 if ((invalid_op_diag
4337 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4338 {
4339 error_at (location, invalid_op_diag);
4340 return error_mark_node;
4341 }
4342
4343 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4344 {
4345 eptype = TREE_TYPE (arg);
4346 arg = TREE_OPERAND (arg, 0);
4347 }
4348
4349 switch (code)
4350 {
4351 case CONVERT_EXPR:
4352 /* This is used for unary plus, because a CONVERT_EXPR
4353 is enough to prevent anybody from looking inside for
4354 associativity, but won't generate any code. */
4355 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4356 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4357 || typecode == VECTOR_TYPE))
4358 {
4359 error_at (location, "wrong type argument to unary plus");
4360 return error_mark_node;
4361 }
4362 else if (!noconvert)
4363 arg = default_conversion (arg);
4364 arg = non_lvalue_loc (location, arg);
4365 break;
4366
4367 case NEGATE_EXPR:
4368 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4369 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4370 || typecode == VECTOR_TYPE))
4371 {
4372 error_at (location, "wrong type argument to unary minus");
4373 return error_mark_node;
4374 }
4375 else if (!noconvert)
4376 arg = default_conversion (arg);
4377 break;
4378
4379 case BIT_NOT_EXPR:
4380 /* ~ works on integer types and non float vectors. */
4381 if (typecode == INTEGER_TYPE
4382 || (typecode == VECTOR_TYPE
4383 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4384 {
4385 tree e = arg;
4386
4387 /* Warn if the expression has boolean value. */
4388 while (TREE_CODE (e) == COMPOUND_EXPR)
4389 e = TREE_OPERAND (e, 1);
4390
4391 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4392 || truth_value_p (TREE_CODE (e))))
4393 {
4394 auto_diagnostic_group d;
4395 if (warning_at (location, OPT_Wbool_operation,
4396 "%<~%> on a boolean expression"))
4397 {
4398 gcc_rich_location richloc (location);
4399 richloc.add_fixit_insert_before (location, "!");
4400 inform (&richloc, "did you mean to use logical not?");
4401 }
4402 }
4403 if (!noconvert)
4404 arg = default_conversion (arg);
4405 }
4406 else if (typecode == COMPLEX_TYPE)
4407 {
4408 code = CONJ_EXPR;
4409 pedwarn (location, OPT_Wpedantic,
4410 "ISO C does not support %<~%> for complex conjugation");
4411 if (!noconvert)
4412 arg = default_conversion (arg);
4413 }
4414 else
4415 {
4416 error_at (location, "wrong type argument to bit-complement");
4417 return error_mark_node;
4418 }
4419 break;
4420
4421 case ABS_EXPR:
4422 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4423 {
4424 error_at (location, "wrong type argument to abs");
4425 return error_mark_node;
4426 }
4427 else if (!noconvert)
4428 arg = default_conversion (arg);
4429 break;
4430
4431 case ABSU_EXPR:
4432 if (!(typecode == INTEGER_TYPE))
4433 {
4434 error_at (location, "wrong type argument to absu");
4435 return error_mark_node;
4436 }
4437 else if (!noconvert)
4438 arg = default_conversion (arg);
4439 break;
4440
4441 case CONJ_EXPR:
4442 /* Conjugating a real value is a no-op, but allow it anyway. */
4443 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4444 || typecode == COMPLEX_TYPE))
4445 {
4446 error_at (location, "wrong type argument to conjugation");
4447 return error_mark_node;
4448 }
4449 else if (!noconvert)
4450 arg = default_conversion (arg);
4451 break;
4452
4453 case TRUTH_NOT_EXPR:
4454 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4455 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4456 && typecode != COMPLEX_TYPE)
4457 {
4458 error_at (location,
4459 "wrong type argument to unary exclamation mark");
4460 return error_mark_node;
4461 }
4462 if (int_operands)
4463 {
4464 arg = c_objc_common_truthvalue_conversion (location, xarg);
4465 arg = remove_c_maybe_const_expr (arg);
4466 }
4467 else
4468 arg = c_objc_common_truthvalue_conversion (location, arg);
4469 ret = invert_truthvalue_loc (location, arg);
4470 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4471 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4472 location = EXPR_LOCATION (ret);
4473 goto return_build_unary_op;
4474
4475 case REALPART_EXPR:
4476 case IMAGPART_EXPR:
4477 ret = build_real_imag_expr (location, code, arg);
4478 if (ret == error_mark_node)
4479 return error_mark_node;
4480 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4481 eptype = TREE_TYPE (eptype);
4482 goto return_build_unary_op;
4483
4484 case PREINCREMENT_EXPR:
4485 case POSTINCREMENT_EXPR:
4486 case PREDECREMENT_EXPR:
4487 case POSTDECREMENT_EXPR:
4488
4489 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4490 {
4491 tree inner = build_unary_op (location, code,
4492 C_MAYBE_CONST_EXPR_EXPR (arg),
4493 noconvert);
4494 if (inner == error_mark_node)
4495 return error_mark_node;
4496 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4497 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4498 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4499 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4500 goto return_build_unary_op;
4501 }
4502
4503 /* Complain about anything that is not a true lvalue. In
4504 Objective-C, skip this check for property_refs. */
4505 if (!objc_is_property_ref (arg)
4506 && !lvalue_or_else (location,
4507 arg, ((code == PREINCREMENT_EXPR
4508 || code == POSTINCREMENT_EXPR)
4509 ? lv_increment
4510 : lv_decrement)))
4511 return error_mark_node;
4512
4513 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4514 {
4515 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4516 warning_at (location, OPT_Wc___compat,
4517 "increment of enumeration value is invalid in C++");
4518 else
4519 warning_at (location, OPT_Wc___compat,
4520 "decrement of enumeration value is invalid in C++");
4521 }
4522
4523 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4524 {
4525 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4526 warning_at (location, OPT_Wbool_operation,
4527 "increment of a boolean expression");
4528 else
4529 warning_at (location, OPT_Wbool_operation,
4530 "decrement of a boolean expression");
4531 }
4532
4533 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4534 arg = c_fully_fold (arg, false, NULL, true);
4535
4536 bool atomic_op;
4537 atomic_op = really_atomic_lvalue (arg);
4538
4539 /* Increment or decrement the real part of the value,
4540 and don't change the imaginary part. */
4541 if (typecode == COMPLEX_TYPE)
4542 {
4543 tree real, imag;
4544
4545 pedwarn (location, OPT_Wpedantic,
4546 "ISO C does not support %<++%> and %<--%> on complex types");
4547
4548 if (!atomic_op)
4549 {
4550 arg = stabilize_reference (arg);
4551 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4552 true);
4553 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4554 true);
4555 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4556 if (real == error_mark_node || imag == error_mark_node)
4557 return error_mark_node;
4558 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4559 real, imag);
4560 goto return_build_unary_op;
4561 }
4562 }
4563
4564 /* Report invalid types. */
4565
4566 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4567 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4568 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4569 {
4570 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4571 error_at (location, "wrong type argument to increment");
4572 else
4573 error_at (location, "wrong type argument to decrement");
4574
4575 return error_mark_node;
4576 }
4577
4578 {
4579 tree inc;
4580
4581 argtype = TREE_TYPE (arg);
4582
4583 /* Compute the increment. */
4584
4585 if (typecode == POINTER_TYPE)
4586 {
4587 /* If pointer target is an incomplete type,
4588 we just cannot know how to do the arithmetic. */
4589 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4590 {
4591 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4592 error_at (location,
4593 "increment of pointer to an incomplete type %qT",
4594 TREE_TYPE (argtype));
4595 else
4596 error_at (location,
4597 "decrement of pointer to an incomplete type %qT",
4598 TREE_TYPE (argtype));
4599 }
4600 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4601 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4602 {
4603 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4604 pedwarn (location, OPT_Wpointer_arith,
4605 "wrong type argument to increment");
4606 else
4607 pedwarn (location, OPT_Wpointer_arith,
4608 "wrong type argument to decrement");
4609 }
4610
4611 inc = c_size_in_bytes (TREE_TYPE (argtype));
4612 inc = convert_to_ptrofftype_loc (location, inc);
4613 }
4614 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4615 {
4616 /* For signed fract types, we invert ++ to -- or
4617 -- to ++, and change inc from 1 to -1, because
4618 it is not possible to represent 1 in signed fract constants.
4619 For unsigned fract types, the result always overflows and
4620 we get an undefined (original) or the maximum value. */
4621 if (code == PREINCREMENT_EXPR)
4622 code = PREDECREMENT_EXPR;
4623 else if (code == PREDECREMENT_EXPR)
4624 code = PREINCREMENT_EXPR;
4625 else if (code == POSTINCREMENT_EXPR)
4626 code = POSTDECREMENT_EXPR;
4627 else /* code == POSTDECREMENT_EXPR */
4628 code = POSTINCREMENT_EXPR;
4629
4630 inc = integer_minus_one_node;
4631 inc = convert (argtype, inc);
4632 }
4633 else
4634 {
4635 inc = VECTOR_TYPE_P (argtype)
4636 ? build_one_cst (argtype)
4637 : integer_one_node;
4638 inc = convert (argtype, inc);
4639 }
4640
4641 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4642 need to ask Objective-C to build the increment or decrement
4643 expression for it. */
4644 if (objc_is_property_ref (arg))
4645 return objc_build_incr_expr_for_property_ref (location, code,
4646 arg, inc);
4647
4648 /* Report a read-only lvalue. */
4649 if (TYPE_READONLY (argtype))
4650 {
4651 readonly_error (location, arg,
4652 ((code == PREINCREMENT_EXPR
4653 || code == POSTINCREMENT_EXPR)
4654 ? lv_increment : lv_decrement));
4655 return error_mark_node;
4656 }
4657 else if (TREE_READONLY (arg))
4658 readonly_warning (arg,
4659 ((code == PREINCREMENT_EXPR
4660 || code == POSTINCREMENT_EXPR)
4661 ? lv_increment : lv_decrement));
4662
4663 /* If the argument is atomic, use the special code sequences for
4664 atomic compound assignment. */
4665 if (atomic_op)
4666 {
4667 arg = stabilize_reference (arg);
4668 ret = build_atomic_assign (location, arg,
4669 ((code == PREINCREMENT_EXPR
4670 || code == POSTINCREMENT_EXPR)
4671 ? PLUS_EXPR
4672 : MINUS_EXPR),
4673 (FRACT_MODE_P (TYPE_MODE (argtype))
4674 ? inc
4675 : integer_one_node),
4676 (code == POSTINCREMENT_EXPR
4677 || code == POSTDECREMENT_EXPR));
4678 goto return_build_unary_op;
4679 }
4680
4681 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4682 val = boolean_increment (code, arg);
4683 else
4684 val = build2 (code, TREE_TYPE (arg), arg, inc);
4685 TREE_SIDE_EFFECTS (val) = 1;
4686 if (TREE_CODE (val) != code)
4687 TREE_NO_WARNING (val) = 1;
4688 ret = val;
4689 goto return_build_unary_op;
4690 }
4691
4692 case ADDR_EXPR:
4693 /* Note that this operation never does default_conversion. */
4694
4695 /* The operand of unary '&' must be an lvalue (which excludes
4696 expressions of type void), or, in C99, the result of a [] or
4697 unary '*' operator. */
4698 if (VOID_TYPE_P (TREE_TYPE (arg))
4699 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4700 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4701 pedwarn (location, 0, "taking address of expression of type %<void%>");
4702
4703 /* Let &* cancel out to simplify resulting code. */
4704 if (INDIRECT_REF_P (arg))
4705 {
4706 /* Don't let this be an lvalue. */
4707 if (lvalue_p (TREE_OPERAND (arg, 0)))
4708 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4709 ret = TREE_OPERAND (arg, 0);
4710 goto return_build_unary_op;
4711 }
4712
4713 /* Anything not already handled and not a true memory reference
4714 or a non-lvalue array is an error. */
4715 if (typecode != FUNCTION_TYPE && !noconvert
4716 && !lvalue_or_else (location, arg, lv_addressof))
4717 return error_mark_node;
4718
4719 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4720 folding later. */
4721 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4722 {
4723 tree inner = build_unary_op (location, code,
4724 C_MAYBE_CONST_EXPR_EXPR (arg),
4725 noconvert);
4726 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4727 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4728 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4729 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4730 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4731 goto return_build_unary_op;
4732 }
4733
4734 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4735 argtype = TREE_TYPE (arg);
4736
4737 /* If the lvalue is const or volatile, merge that into the type
4738 to which the address will point. This is only needed
4739 for function types. */
4740 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4741 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4742 && TREE_CODE (argtype) == FUNCTION_TYPE)
4743 {
4744 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4745 int quals = orig_quals;
4746
4747 if (TREE_READONLY (arg))
4748 quals |= TYPE_QUAL_CONST;
4749 if (TREE_THIS_VOLATILE (arg))
4750 quals |= TYPE_QUAL_VOLATILE;
4751
4752 argtype = c_build_qualified_type (argtype, quals);
4753 }
4754
4755 switch (TREE_CODE (arg))
4756 {
4757 case COMPONENT_REF:
4758 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4759 {
4760 error_at (location, "cannot take address of bit-field %qD",
4761 TREE_OPERAND (arg, 1));
4762 return error_mark_node;
4763 }
4764
4765 /* fall through */
4766
4767 case ARRAY_REF:
4768 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4769 {
4770 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4771 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4772 {
4773 error_at (location, "cannot take address of scalar with "
4774 "reverse storage order");
4775 return error_mark_node;
4776 }
4777
4778 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4779 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4780 warning_at (location, OPT_Wscalar_storage_order,
4781 "address of array with reverse scalar storage "
4782 "order requested");
4783 }
4784
4785 default:
4786 break;
4787 }
4788
4789 if (!c_mark_addressable (arg))
4790 return error_mark_node;
4791
4792 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4793 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4794
4795 argtype = build_pointer_type (argtype);
4796
4797 /* ??? Cope with user tricks that amount to offsetof. Delete this
4798 when we have proper support for integer constant expressions. */
4799 val = get_base_address (arg);
4800 if (val && INDIRECT_REF_P (val)
4801 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4802 {
4803 ret = fold_offsetof (arg, argtype);
4804 goto return_build_unary_op;
4805 }
4806
4807 val = build1 (ADDR_EXPR, argtype, arg);
4808
4809 ret = val;
4810 goto return_build_unary_op;
4811
4812 default:
4813 gcc_unreachable ();
4814 }
4815
4816 if (argtype == NULL_TREE)
4817 argtype = TREE_TYPE (arg);
4818 if (TREE_CODE (arg) == INTEGER_CST)
4819 ret = (require_constant_value
4820 ? fold_build1_initializer_loc (location, code, argtype, arg)
4821 : fold_build1_loc (location, code, argtype, arg));
4822 else
4823 ret = build1 (code, argtype, arg);
4824 return_build_unary_op:
4825 gcc_assert (ret != error_mark_node);
4826 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4827 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4828 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4829 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4830 ret = note_integer_operands (ret);
4831 if (eptype)
4832 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4833 protected_set_expr_location (ret, location);
4834 return ret;
4835 }
4836
4837 /* Return nonzero if REF is an lvalue valid for this language.
4838 Lvalues can be assigned, unless their type has TYPE_READONLY.
4839 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4840
4841 bool
4842 lvalue_p (const_tree ref)
4843 {
4844 const enum tree_code code = TREE_CODE (ref);
4845
4846 switch (code)
4847 {
4848 case REALPART_EXPR:
4849 case IMAGPART_EXPR:
4850 case COMPONENT_REF:
4851 return lvalue_p (TREE_OPERAND (ref, 0));
4852
4853 case C_MAYBE_CONST_EXPR:
4854 return lvalue_p (TREE_OPERAND (ref, 1));
4855
4856 case COMPOUND_LITERAL_EXPR:
4857 case STRING_CST:
4858 return true;
4859
4860 case INDIRECT_REF:
4861 case ARRAY_REF:
4862 case VAR_DECL:
4863 case PARM_DECL:
4864 case RESULT_DECL:
4865 case ERROR_MARK:
4866 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4867 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4868
4869 case BIND_EXPR:
4870 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4871
4872 default:
4873 return false;
4874 }
4875 }
4876 \f
4877 /* Give a warning for storing in something that is read-only in GCC
4878 terms but not const in ISO C terms. */
4879
4880 static void
4881 readonly_warning (tree arg, enum lvalue_use use)
4882 {
4883 switch (use)
4884 {
4885 case lv_assign:
4886 warning (0, "assignment of read-only location %qE", arg);
4887 break;
4888 case lv_increment:
4889 warning (0, "increment of read-only location %qE", arg);
4890 break;
4891 case lv_decrement:
4892 warning (0, "decrement of read-only location %qE", arg);
4893 break;
4894 default:
4895 gcc_unreachable ();
4896 }
4897 return;
4898 }
4899
4900
4901 /* Return nonzero if REF is an lvalue valid for this language;
4902 otherwise, print an error message and return zero. USE says
4903 how the lvalue is being used and so selects the error message.
4904 LOCATION is the location at which any error should be reported. */
4905
4906 static int
4907 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4908 {
4909 int win = lvalue_p (ref);
4910
4911 if (!win)
4912 lvalue_error (loc, use);
4913
4914 return win;
4915 }
4916 \f
4917 /* Mark EXP saying that we need to be able to take the
4918 address of it; it should not be allocated in a register.
4919 Returns true if successful. ARRAY_REF_P is true if this
4920 is for ARRAY_REF construction - in that case we don't want
4921 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4922 it is fine to use ARRAY_REFs for vector subscripts on vector
4923 register variables. */
4924
4925 bool
4926 c_mark_addressable (tree exp, bool array_ref_p)
4927 {
4928 tree x = exp;
4929
4930 while (1)
4931 switch (TREE_CODE (x))
4932 {
4933 case VIEW_CONVERT_EXPR:
4934 if (array_ref_p
4935 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4936 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4937 return true;
4938 /* FALLTHRU */
4939 case COMPONENT_REF:
4940 case ADDR_EXPR:
4941 case ARRAY_REF:
4942 case REALPART_EXPR:
4943 case IMAGPART_EXPR:
4944 x = TREE_OPERAND (x, 0);
4945 break;
4946
4947 case COMPOUND_LITERAL_EXPR:
4948 TREE_ADDRESSABLE (x) = 1;
4949 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
4950 return true;
4951
4952 case CONSTRUCTOR:
4953 TREE_ADDRESSABLE (x) = 1;
4954 return true;
4955
4956 case VAR_DECL:
4957 case CONST_DECL:
4958 case PARM_DECL:
4959 case RESULT_DECL:
4960 if (C_DECL_REGISTER (x)
4961 && DECL_NONLOCAL (x))
4962 {
4963 if (TREE_PUBLIC (x) || is_global_var (x))
4964 {
4965 error
4966 ("global register variable %qD used in nested function", x);
4967 return false;
4968 }
4969 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4970 }
4971 else if (C_DECL_REGISTER (x))
4972 {
4973 if (TREE_PUBLIC (x) || is_global_var (x))
4974 error ("address of global register variable %qD requested", x);
4975 else
4976 error ("address of register variable %qD requested", x);
4977 return false;
4978 }
4979
4980 /* FALLTHRU */
4981 case FUNCTION_DECL:
4982 TREE_ADDRESSABLE (x) = 1;
4983 /* FALLTHRU */
4984 default:
4985 return true;
4986 }
4987 }
4988 \f
4989 /* Convert EXPR to TYPE, warning about conversion problems with
4990 constants. SEMANTIC_TYPE is the type this conversion would use
4991 without excess precision. If SEMANTIC_TYPE is NULL, this function
4992 is equivalent to convert_and_check. This function is a wrapper that
4993 handles conversions that may be different than
4994 the usual ones because of excess precision. */
4995
4996 static tree
4997 ep_convert_and_check (location_t loc, tree type, tree expr,
4998 tree semantic_type)
4999 {
5000 if (TREE_TYPE (expr) == type)
5001 return expr;
5002
5003 /* For C11, integer conversions may have results with excess
5004 precision. */
5005 if (flag_isoc11 || !semantic_type)
5006 return convert_and_check (loc, type, expr);
5007
5008 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5009 && TREE_TYPE (expr) != semantic_type)
5010 {
5011 /* For integers, we need to check the real conversion, not
5012 the conversion to the excess precision type. */
5013 expr = convert_and_check (loc, semantic_type, expr);
5014 }
5015 /* Result type is the excess precision type, which should be
5016 large enough, so do not check. */
5017 return convert (type, expr);
5018 }
5019
5020 /* If EXPR refers to a built-in declared without a prototype returns
5021 the actual type of the built-in and, if non-null, set *BLTIN to
5022 a pointer to the built-in. Otherwise return the type of EXPR
5023 and clear *BLTIN if non-null. */
5024
5025 static tree
5026 type_or_builtin_type (tree expr, tree *bltin = NULL)
5027 {
5028 tree dummy;
5029 if (!bltin)
5030 bltin = &dummy;
5031
5032 *bltin = NULL_TREE;
5033
5034 tree type = TREE_TYPE (expr);
5035 if (TREE_CODE (expr) != ADDR_EXPR)
5036 return type;
5037
5038 tree oper = TREE_OPERAND (expr, 0);
5039 if (!DECL_P (oper)
5040 || TREE_CODE (oper) != FUNCTION_DECL
5041 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5042 return type;
5043
5044 built_in_function code = DECL_FUNCTION_CODE (oper);
5045 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5046 return type;
5047
5048 if ((*bltin = builtin_decl_implicit (code)))
5049 type = build_pointer_type (TREE_TYPE (*bltin));
5050
5051 return type;
5052 }
5053
5054 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5055 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5056 if folded to an integer constant then the unselected half may
5057 contain arbitrary operations not normally permitted in constant
5058 expressions. Set the location of the expression to LOC. */
5059
5060 tree
5061 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5062 tree op1, tree op1_original_type, location_t op1_loc,
5063 tree op2, tree op2_original_type, location_t op2_loc)
5064 {
5065 tree type1;
5066 tree type2;
5067 enum tree_code code1;
5068 enum tree_code code2;
5069 tree result_type = NULL;
5070 tree semantic_result_type = NULL;
5071 tree orig_op1 = op1, orig_op2 = op2;
5072 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5073 bool ifexp_int_operands;
5074 tree ret;
5075
5076 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5077 if (op1_int_operands)
5078 op1 = remove_c_maybe_const_expr (op1);
5079 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5080 if (op2_int_operands)
5081 op2 = remove_c_maybe_const_expr (op2);
5082 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5083 if (ifexp_int_operands)
5084 ifexp = remove_c_maybe_const_expr (ifexp);
5085
5086 /* Promote both alternatives. */
5087
5088 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5089 op1 = default_conversion (op1);
5090 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5091 op2 = default_conversion (op2);
5092
5093 if (TREE_CODE (ifexp) == ERROR_MARK
5094 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5095 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5096 return error_mark_node;
5097
5098 tree bltin1 = NULL_TREE;
5099 tree bltin2 = NULL_TREE;
5100 type1 = type_or_builtin_type (op1, &bltin1);
5101 code1 = TREE_CODE (type1);
5102 type2 = type_or_builtin_type (op2, &bltin2);
5103 code2 = TREE_CODE (type2);
5104
5105 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5106 return error_mark_node;
5107
5108 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5109 return error_mark_node;
5110
5111 /* C90 does not permit non-lvalue arrays in conditional expressions.
5112 In C99 they will be pointers by now. */
5113 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5114 {
5115 error_at (colon_loc, "non-lvalue array in conditional expression");
5116 return error_mark_node;
5117 }
5118
5119 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5120 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5121 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5122 || code1 == COMPLEX_TYPE)
5123 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5124 || code2 == COMPLEX_TYPE))
5125 {
5126 semantic_result_type = c_common_type (type1, type2);
5127 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5128 {
5129 op1 = TREE_OPERAND (op1, 0);
5130 type1 = TREE_TYPE (op1);
5131 gcc_assert (TREE_CODE (type1) == code1);
5132 }
5133 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5134 {
5135 op2 = TREE_OPERAND (op2, 0);
5136 type2 = TREE_TYPE (op2);
5137 gcc_assert (TREE_CODE (type2) == code2);
5138 }
5139 }
5140
5141 if (warn_cxx_compat)
5142 {
5143 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5144 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5145
5146 if (TREE_CODE (t1) == ENUMERAL_TYPE
5147 && TREE_CODE (t2) == ENUMERAL_TYPE
5148 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5149 warning_at (colon_loc, OPT_Wc___compat,
5150 ("different enum types in conditional is "
5151 "invalid in C++: %qT vs %qT"),
5152 t1, t2);
5153 }
5154
5155 /* Quickly detect the usual case where op1 and op2 have the same type
5156 after promotion. */
5157 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5158 {
5159 if (type1 == type2)
5160 result_type = type1;
5161 else
5162 result_type = TYPE_MAIN_VARIANT (type1);
5163 }
5164 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5165 || code1 == COMPLEX_TYPE)
5166 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5167 || code2 == COMPLEX_TYPE))
5168 {
5169 /* In C11, a conditional expression between a floating-point
5170 type and an integer type should convert the integer type to
5171 the evaluation format of the floating-point type, with
5172 possible excess precision. */
5173 tree eptype1 = type1;
5174 tree eptype2 = type2;
5175 if (flag_isoc11)
5176 {
5177 tree eptype;
5178 if (ANY_INTEGRAL_TYPE_P (type1)
5179 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5180 {
5181 eptype2 = eptype;
5182 if (!semantic_result_type)
5183 semantic_result_type = c_common_type (type1, type2);
5184 }
5185 else if (ANY_INTEGRAL_TYPE_P (type2)
5186 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5187 {
5188 eptype1 = eptype;
5189 if (!semantic_result_type)
5190 semantic_result_type = c_common_type (type1, type2);
5191 }
5192 }
5193 result_type = c_common_type (eptype1, eptype2);
5194 if (result_type == error_mark_node)
5195 return error_mark_node;
5196 do_warn_double_promotion (result_type, type1, type2,
5197 "implicit conversion from %qT to %qT to "
5198 "match other result of conditional",
5199 colon_loc);
5200
5201 /* If -Wsign-compare, warn here if type1 and type2 have
5202 different signedness. We'll promote the signed to unsigned
5203 and later code won't know it used to be different.
5204 Do this check on the original types, so that explicit casts
5205 will be considered, but default promotions won't. */
5206 if (c_inhibit_evaluation_warnings == 0)
5207 {
5208 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5209 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5210
5211 if (unsigned_op1 ^ unsigned_op2)
5212 {
5213 bool ovf;
5214
5215 /* Do not warn if the result type is signed, since the
5216 signed type will only be chosen if it can represent
5217 all the values of the unsigned type. */
5218 if (!TYPE_UNSIGNED (result_type))
5219 /* OK */;
5220 else
5221 {
5222 bool op1_maybe_const = true;
5223 bool op2_maybe_const = true;
5224
5225 /* Do not warn if the signed quantity is an
5226 unsuffixed integer literal (or some static
5227 constant expression involving such literals) and
5228 it is non-negative. This warning requires the
5229 operands to be folded for best results, so do
5230 that folding in this case even without
5231 warn_sign_compare to avoid warning options
5232 possibly affecting code generation. */
5233 c_inhibit_evaluation_warnings
5234 += (ifexp == truthvalue_false_node);
5235 op1 = c_fully_fold (op1, require_constant_value,
5236 &op1_maybe_const);
5237 c_inhibit_evaluation_warnings
5238 -= (ifexp == truthvalue_false_node);
5239
5240 c_inhibit_evaluation_warnings
5241 += (ifexp == truthvalue_true_node);
5242 op2 = c_fully_fold (op2, require_constant_value,
5243 &op2_maybe_const);
5244 c_inhibit_evaluation_warnings
5245 -= (ifexp == truthvalue_true_node);
5246
5247 if (warn_sign_compare)
5248 {
5249 if ((unsigned_op2
5250 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5251 || (unsigned_op1
5252 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5253 /* OK */;
5254 else if (unsigned_op2)
5255 warning_at (op1_loc, OPT_Wsign_compare,
5256 "operand of ?: changes signedness from "
5257 "%qT to %qT due to unsignedness of other "
5258 "operand", TREE_TYPE (orig_op1),
5259 TREE_TYPE (orig_op2));
5260 else
5261 warning_at (op2_loc, OPT_Wsign_compare,
5262 "operand of ?: changes signedness from "
5263 "%qT to %qT due to unsignedness of other "
5264 "operand", TREE_TYPE (orig_op2),
5265 TREE_TYPE (orig_op1));
5266 }
5267 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5268 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5269 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5270 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5271 }
5272 }
5273 }
5274 }
5275 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5276 {
5277 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5278 pedwarn (colon_loc, OPT_Wpedantic,
5279 "ISO C forbids conditional expr with only one void side");
5280 result_type = void_type_node;
5281 }
5282 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5283 {
5284 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5285 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5286 addr_space_t as_common;
5287
5288 if (comp_target_types (colon_loc, type1, type2))
5289 result_type = common_pointer_type (type1, type2);
5290 else if (null_pointer_constant_p (orig_op1))
5291 result_type = type2;
5292 else if (null_pointer_constant_p (orig_op2))
5293 result_type = type1;
5294 else if (!addr_space_superset (as1, as2, &as_common))
5295 {
5296 error_at (colon_loc, "pointers to disjoint address spaces "
5297 "used in conditional expression");
5298 return error_mark_node;
5299 }
5300 else if (VOID_TYPE_P (TREE_TYPE (type1))
5301 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5302 {
5303 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5304 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5305 & ~TYPE_QUALS (TREE_TYPE (type1))))
5306 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5307 "pointer to array loses qualifier "
5308 "in conditional expression");
5309
5310 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5311 pedwarn (colon_loc, OPT_Wpedantic,
5312 "ISO C forbids conditional expr between "
5313 "%<void *%> and function pointer");
5314 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5315 TREE_TYPE (type2)));
5316 }
5317 else if (VOID_TYPE_P (TREE_TYPE (type2))
5318 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5319 {
5320 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5321 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5322 & ~TYPE_QUALS (TREE_TYPE (type2))))
5323 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5324 "pointer to array loses qualifier "
5325 "in conditional expression");
5326
5327 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5328 pedwarn (colon_loc, OPT_Wpedantic,
5329 "ISO C forbids conditional expr between "
5330 "%<void *%> and function pointer");
5331 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5332 TREE_TYPE (type1)));
5333 }
5334 /* Objective-C pointer comparisons are a bit more lenient. */
5335 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5336 result_type = objc_common_type (type1, type2);
5337 else
5338 {
5339 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5340 if (bltin1 && bltin2)
5341 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5342 "pointer type mismatch between %qT and %qT "
5343 "of %qD and %qD in conditional expression",
5344 type1, type2, bltin1, bltin2);
5345 else
5346 pedwarn (colon_loc, 0,
5347 "pointer type mismatch in conditional expression");
5348 result_type = build_pointer_type
5349 (build_qualified_type (void_type_node, qual));
5350 }
5351 }
5352 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5353 {
5354 if (!null_pointer_constant_p (orig_op2))
5355 pedwarn (colon_loc, 0,
5356 "pointer/integer type mismatch in conditional expression");
5357 else
5358 {
5359 op2 = null_pointer_node;
5360 }
5361 result_type = type1;
5362 }
5363 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5364 {
5365 if (!null_pointer_constant_p (orig_op1))
5366 pedwarn (colon_loc, 0,
5367 "pointer/integer type mismatch in conditional expression");
5368 else
5369 {
5370 op1 = null_pointer_node;
5371 }
5372 result_type = type2;
5373 }
5374
5375 if (!result_type)
5376 {
5377 if (flag_cond_mismatch)
5378 result_type = void_type_node;
5379 else
5380 {
5381 error_at (colon_loc, "type mismatch in conditional expression");
5382 return error_mark_node;
5383 }
5384 }
5385
5386 /* Merge const and volatile flags of the incoming types. */
5387 result_type
5388 = build_type_variant (result_type,
5389 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5390 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5391
5392 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5393 semantic_result_type);
5394 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5395 semantic_result_type);
5396
5397 if (ifexp_bcp && ifexp == truthvalue_true_node)
5398 {
5399 op2_int_operands = true;
5400 op1 = c_fully_fold (op1, require_constant_value, NULL);
5401 }
5402 if (ifexp_bcp && ifexp == truthvalue_false_node)
5403 {
5404 op1_int_operands = true;
5405 op2 = c_fully_fold (op2, require_constant_value, NULL);
5406 }
5407 int_const = int_operands = (ifexp_int_operands
5408 && op1_int_operands
5409 && op2_int_operands);
5410 if (int_operands)
5411 {
5412 int_const = ((ifexp == truthvalue_true_node
5413 && TREE_CODE (orig_op1) == INTEGER_CST
5414 && !TREE_OVERFLOW (orig_op1))
5415 || (ifexp == truthvalue_false_node
5416 && TREE_CODE (orig_op2) == INTEGER_CST
5417 && !TREE_OVERFLOW (orig_op2)));
5418 }
5419
5420 /* Need to convert condition operand into a vector mask. */
5421 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5422 {
5423 tree vectype = TREE_TYPE (ifexp);
5424 tree elem_type = TREE_TYPE (vectype);
5425 tree zero = build_int_cst (elem_type, 0);
5426 tree zero_vec = build_vector_from_val (vectype, zero);
5427 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5428 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5429 }
5430
5431 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5432 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5433 else
5434 {
5435 if (int_operands)
5436 {
5437 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5438 nested inside of the expression. */
5439 op1 = c_fully_fold (op1, false, NULL);
5440 op2 = c_fully_fold (op2, false, NULL);
5441 }
5442 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5443 if (int_operands)
5444 ret = note_integer_operands (ret);
5445 }
5446 if (semantic_result_type)
5447 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5448
5449 protected_set_expr_location (ret, colon_loc);
5450
5451 /* If the OP1 and OP2 are the same and don't have side-effects,
5452 warn here, because the COND_EXPR will be turned into OP1. */
5453 if (warn_duplicated_branches
5454 && TREE_CODE (ret) == COND_EXPR
5455 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5456 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5457 "this condition has identical branches");
5458
5459 return ret;
5460 }
5461 \f
5462 /* Return a compound expression that performs two expressions and
5463 returns the value of the second of them.
5464
5465 LOC is the location of the COMPOUND_EXPR. */
5466
5467 tree
5468 build_compound_expr (location_t loc, tree expr1, tree expr2)
5469 {
5470 bool expr1_int_operands, expr2_int_operands;
5471 tree eptype = NULL_TREE;
5472 tree ret;
5473
5474 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5475 if (expr1_int_operands)
5476 expr1 = remove_c_maybe_const_expr (expr1);
5477 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5478 if (expr2_int_operands)
5479 expr2 = remove_c_maybe_const_expr (expr2);
5480
5481 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5482 expr1 = TREE_OPERAND (expr1, 0);
5483 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5484 {
5485 eptype = TREE_TYPE (expr2);
5486 expr2 = TREE_OPERAND (expr2, 0);
5487 }
5488
5489 if (!TREE_SIDE_EFFECTS (expr1))
5490 {
5491 /* The left-hand operand of a comma expression is like an expression
5492 statement: with -Wunused, we should warn if it doesn't have
5493 any side-effects, unless it was explicitly cast to (void). */
5494 if (warn_unused_value)
5495 {
5496 if (VOID_TYPE_P (TREE_TYPE (expr1))
5497 && CONVERT_EXPR_P (expr1))
5498 ; /* (void) a, b */
5499 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5500 && TREE_CODE (expr1) == COMPOUND_EXPR
5501 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5502 ; /* (void) a, (void) b, c */
5503 else
5504 warning_at (loc, OPT_Wunused_value,
5505 "left-hand operand of comma expression has no effect");
5506 }
5507 }
5508 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5509 && warn_unused_value)
5510 {
5511 tree r = expr1;
5512 location_t cloc = loc;
5513 while (TREE_CODE (r) == COMPOUND_EXPR)
5514 {
5515 if (EXPR_HAS_LOCATION (r))
5516 cloc = EXPR_LOCATION (r);
5517 r = TREE_OPERAND (r, 1);
5518 }
5519 if (!TREE_SIDE_EFFECTS (r)
5520 && !VOID_TYPE_P (TREE_TYPE (r))
5521 && !CONVERT_EXPR_P (r))
5522 warning_at (cloc, OPT_Wunused_value,
5523 "right-hand operand of comma expression has no effect");
5524 }
5525
5526 /* With -Wunused, we should also warn if the left-hand operand does have
5527 side-effects, but computes a value which is not used. For example, in
5528 `foo() + bar(), baz()' the result of the `+' operator is not used,
5529 so we should issue a warning. */
5530 else if (warn_unused_value)
5531 warn_if_unused_value (expr1, loc);
5532
5533 if (expr2 == error_mark_node)
5534 return error_mark_node;
5535
5536 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5537
5538 if (flag_isoc99
5539 && expr1_int_operands
5540 && expr2_int_operands)
5541 ret = note_integer_operands (ret);
5542
5543 if (eptype)
5544 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5545
5546 protected_set_expr_location (ret, loc);
5547 return ret;
5548 }
5549
5550 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5551 which we are casting. OTYPE is the type of the expression being
5552 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5553 of the cast. -Wcast-qual appeared on the command line. Named
5554 address space qualifiers are not handled here, because they result
5555 in different warnings. */
5556
5557 static void
5558 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5559 {
5560 tree in_type = type;
5561 tree in_otype = otype;
5562 int added = 0;
5563 int discarded = 0;
5564 bool is_const;
5565
5566 /* Check that the qualifiers on IN_TYPE are a superset of the
5567 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5568 nodes is uninteresting and we stop as soon as we hit a
5569 non-POINTER_TYPE node on either type. */
5570 do
5571 {
5572 in_otype = TREE_TYPE (in_otype);
5573 in_type = TREE_TYPE (in_type);
5574
5575 /* GNU C allows cv-qualified function types. 'const' means the
5576 function is very pure, 'volatile' means it can't return. We
5577 need to warn when such qualifiers are added, not when they're
5578 taken away. */
5579 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5580 && TREE_CODE (in_type) == FUNCTION_TYPE)
5581 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5582 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5583 else
5584 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5585 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5586 }
5587 while (TREE_CODE (in_type) == POINTER_TYPE
5588 && TREE_CODE (in_otype) == POINTER_TYPE);
5589
5590 if (added)
5591 warning_at (loc, OPT_Wcast_qual,
5592 "cast adds %q#v qualifier to function type", added);
5593
5594 if (discarded)
5595 /* There are qualifiers present in IN_OTYPE that are not present
5596 in IN_TYPE. */
5597 warning_at (loc, OPT_Wcast_qual,
5598 "cast discards %qv qualifier from pointer target type",
5599 discarded);
5600
5601 if (added || discarded)
5602 return;
5603
5604 /* A cast from **T to const **T is unsafe, because it can cause a
5605 const value to be changed with no additional warning. We only
5606 issue this warning if T is the same on both sides, and we only
5607 issue the warning if there are the same number of pointers on
5608 both sides, as otherwise the cast is clearly unsafe anyhow. A
5609 cast is unsafe when a qualifier is added at one level and const
5610 is not present at all outer levels.
5611
5612 To issue this warning, we check at each level whether the cast
5613 adds new qualifiers not already seen. We don't need to special
5614 case function types, as they won't have the same
5615 TYPE_MAIN_VARIANT. */
5616
5617 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5618 return;
5619 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5620 return;
5621
5622 in_type = type;
5623 in_otype = otype;
5624 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5625 do
5626 {
5627 in_type = TREE_TYPE (in_type);
5628 in_otype = TREE_TYPE (in_otype);
5629 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5630 && !is_const)
5631 {
5632 warning_at (loc, OPT_Wcast_qual,
5633 "to be safe all intermediate pointers in cast from "
5634 "%qT to %qT must be %<const%> qualified",
5635 otype, type);
5636 break;
5637 }
5638 if (is_const)
5639 is_const = TYPE_READONLY (in_type);
5640 }
5641 while (TREE_CODE (in_type) == POINTER_TYPE);
5642 }
5643
5644 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5645
5646 static bool
5647 c_safe_arg_type_equiv_p (tree t1, tree t2)
5648 {
5649 t1 = TYPE_MAIN_VARIANT (t1);
5650 t2 = TYPE_MAIN_VARIANT (t2);
5651
5652 if (TREE_CODE (t1) == POINTER_TYPE
5653 && TREE_CODE (t2) == POINTER_TYPE)
5654 return true;
5655
5656 /* The signedness of the parameter matters only when an integral
5657 type smaller than int is promoted to int, otherwise only the
5658 precision of the parameter matters.
5659 This check should make sure that the callee does not see
5660 undefined values in argument registers. */
5661 if (INTEGRAL_TYPE_P (t1)
5662 && INTEGRAL_TYPE_P (t2)
5663 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5664 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5665 || !targetm.calls.promote_prototypes (NULL_TREE)
5666 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5667 return true;
5668
5669 return comptypes (t1, t2);
5670 }
5671
5672 /* Check if a type cast between two function types can be considered safe. */
5673
5674 static bool
5675 c_safe_function_type_cast_p (tree t1, tree t2)
5676 {
5677 if (TREE_TYPE (t1) == void_type_node &&
5678 TYPE_ARG_TYPES (t1) == void_list_node)
5679 return true;
5680
5681 if (TREE_TYPE (t2) == void_type_node &&
5682 TYPE_ARG_TYPES (t2) == void_list_node)
5683 return true;
5684
5685 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5686 return false;
5687
5688 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5689 t1 && t2;
5690 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5691 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5692 return false;
5693
5694 return true;
5695 }
5696
5697 /* Build an expression representing a cast to type TYPE of expression EXPR.
5698 LOC is the location of the cast-- typically the open paren of the cast. */
5699
5700 tree
5701 build_c_cast (location_t loc, tree type, tree expr)
5702 {
5703 tree value;
5704
5705 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5706 expr = TREE_OPERAND (expr, 0);
5707
5708 value = expr;
5709
5710 if (type == error_mark_node || expr == error_mark_node)
5711 return error_mark_node;
5712
5713 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5714 only in <protocol> qualifications. But when constructing cast expressions,
5715 the protocols do matter and must be kept around. */
5716 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5717 return build1 (NOP_EXPR, type, expr);
5718
5719 type = TYPE_MAIN_VARIANT (type);
5720
5721 if (TREE_CODE (type) == ARRAY_TYPE)
5722 {
5723 error_at (loc, "cast specifies array type");
5724 return error_mark_node;
5725 }
5726
5727 if (TREE_CODE (type) == FUNCTION_TYPE)
5728 {
5729 error_at (loc, "cast specifies function type");
5730 return error_mark_node;
5731 }
5732
5733 if (!VOID_TYPE_P (type))
5734 {
5735 value = require_complete_type (loc, value);
5736 if (value == error_mark_node)
5737 return error_mark_node;
5738 }
5739
5740 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5741 {
5742 if (RECORD_OR_UNION_TYPE_P (type))
5743 pedwarn (loc, OPT_Wpedantic,
5744 "ISO C forbids casting nonscalar to the same type");
5745
5746 /* Convert to remove any qualifiers from VALUE's type. */
5747 value = convert (type, value);
5748 }
5749 else if (TREE_CODE (type) == UNION_TYPE)
5750 {
5751 tree field;
5752
5753 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5754 if (TREE_TYPE (field) != error_mark_node
5755 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5756 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5757 break;
5758
5759 if (field)
5760 {
5761 tree t;
5762 bool maybe_const = true;
5763
5764 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5765 t = c_fully_fold (value, false, &maybe_const);
5766 t = build_constructor_single (type, field, t);
5767 if (!maybe_const)
5768 t = c_wrap_maybe_const (t, true);
5769 t = digest_init (loc, type, t,
5770 NULL_TREE, false, true, 0);
5771 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5772 return t;
5773 }
5774 error_at (loc, "cast to union type from type not present in union");
5775 return error_mark_node;
5776 }
5777 else
5778 {
5779 tree otype, ovalue;
5780
5781 if (type == void_type_node)
5782 {
5783 tree t = build1 (CONVERT_EXPR, type, value);
5784 SET_EXPR_LOCATION (t, loc);
5785 return t;
5786 }
5787
5788 otype = TREE_TYPE (value);
5789
5790 /* Optionally warn about potentially worrisome casts. */
5791 if (warn_cast_qual
5792 && TREE_CODE (type) == POINTER_TYPE
5793 && TREE_CODE (otype) == POINTER_TYPE)
5794 handle_warn_cast_qual (loc, type, otype);
5795
5796 /* Warn about conversions between pointers to disjoint
5797 address spaces. */
5798 if (TREE_CODE (type) == POINTER_TYPE
5799 && TREE_CODE (otype) == POINTER_TYPE
5800 && !null_pointer_constant_p (value))
5801 {
5802 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5803 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5804 addr_space_t as_common;
5805
5806 if (!addr_space_superset (as_to, as_from, &as_common))
5807 {
5808 if (ADDR_SPACE_GENERIC_P (as_from))
5809 warning_at (loc, 0, "cast to %s address space pointer "
5810 "from disjoint generic address space pointer",
5811 c_addr_space_name (as_to));
5812
5813 else if (ADDR_SPACE_GENERIC_P (as_to))
5814 warning_at (loc, 0, "cast to generic address space pointer "
5815 "from disjoint %s address space pointer",
5816 c_addr_space_name (as_from));
5817
5818 else
5819 warning_at (loc, 0, "cast to %s address space pointer "
5820 "from disjoint %s address space pointer",
5821 c_addr_space_name (as_to),
5822 c_addr_space_name (as_from));
5823 }
5824 }
5825
5826 /* Warn about possible alignment problems. */
5827 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5828 && TREE_CODE (type) == POINTER_TYPE
5829 && TREE_CODE (otype) == POINTER_TYPE
5830 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5831 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5832 /* Don't warn about opaque types, where the actual alignment
5833 restriction is unknown. */
5834 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5835 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5836 && min_align_of_type (TREE_TYPE (type))
5837 > min_align_of_type (TREE_TYPE (otype)))
5838 warning_at (loc, OPT_Wcast_align,
5839 "cast increases required alignment of target type");
5840
5841 if (TREE_CODE (type) == INTEGER_TYPE
5842 && TREE_CODE (otype) == POINTER_TYPE
5843 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5844 /* Unlike conversion of integers to pointers, where the
5845 warning is disabled for converting constants because
5846 of cases such as SIG_*, warn about converting constant
5847 pointers to integers. In some cases it may cause unwanted
5848 sign extension, and a warning is appropriate. */
5849 warning_at (loc, OPT_Wpointer_to_int_cast,
5850 "cast from pointer to integer of different size");
5851
5852 if (TREE_CODE (value) == CALL_EXPR
5853 && TREE_CODE (type) != TREE_CODE (otype))
5854 warning_at (loc, OPT_Wbad_function_cast,
5855 "cast from function call of type %qT "
5856 "to non-matching type %qT", otype, type);
5857
5858 if (TREE_CODE (type) == POINTER_TYPE
5859 && TREE_CODE (otype) == INTEGER_TYPE
5860 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5861 /* Don't warn about converting any constant. */
5862 && !TREE_CONSTANT (value))
5863 warning_at (loc,
5864 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5865 "of different size");
5866
5867 if (warn_strict_aliasing <= 2)
5868 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
5869
5870 /* If pedantic, warn for conversions between function and object
5871 pointer types, except for converting a null pointer constant
5872 to function pointer type. */
5873 if (pedantic
5874 && TREE_CODE (type) == POINTER_TYPE
5875 && TREE_CODE (otype) == POINTER_TYPE
5876 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5877 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5878 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5879 "conversion of function pointer to object pointer type");
5880
5881 if (pedantic
5882 && TREE_CODE (type) == POINTER_TYPE
5883 && TREE_CODE (otype) == POINTER_TYPE
5884 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5885 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5886 && !null_pointer_constant_p (value))
5887 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5888 "conversion of object pointer to function pointer type");
5889
5890 if (TREE_CODE (type) == POINTER_TYPE
5891 && TREE_CODE (otype) == POINTER_TYPE
5892 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5893 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5894 && !c_safe_function_type_cast_p (TREE_TYPE (type),
5895 TREE_TYPE (otype)))
5896 warning_at (loc, OPT_Wcast_function_type,
5897 "cast between incompatible function types"
5898 " from %qT to %qT", otype, type);
5899
5900 ovalue = value;
5901 value = convert (type, value);
5902
5903 /* Ignore any integer overflow caused by the cast. */
5904 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5905 {
5906 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5907 {
5908 if (!TREE_OVERFLOW (value))
5909 {
5910 /* Avoid clobbering a shared constant. */
5911 value = copy_node (value);
5912 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5913 }
5914 }
5915 else if (TREE_OVERFLOW (value))
5916 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5917 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5918 }
5919 }
5920
5921 /* Don't let a cast be an lvalue. */
5922 if (lvalue_p (value))
5923 value = non_lvalue_loc (loc, value);
5924
5925 /* Don't allow the results of casting to floating-point or complex
5926 types be confused with actual constants, or casts involving
5927 integer and pointer types other than direct integer-to-integer
5928 and integer-to-pointer be confused with integer constant
5929 expressions and null pointer constants. */
5930 if (TREE_CODE (value) == REAL_CST
5931 || TREE_CODE (value) == COMPLEX_CST
5932 || (TREE_CODE (value) == INTEGER_CST
5933 && !((TREE_CODE (expr) == INTEGER_CST
5934 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5935 || TREE_CODE (expr) == REAL_CST
5936 || TREE_CODE (expr) == COMPLEX_CST)))
5937 value = build1 (NOP_EXPR, type, value);
5938
5939 protected_set_expr_location (value, loc);
5940 return value;
5941 }
5942
5943 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5944 location of the open paren of the cast, or the position of the cast
5945 expr. */
5946 tree
5947 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5948 {
5949 tree type;
5950 tree type_expr = NULL_TREE;
5951 bool type_expr_const = true;
5952 tree ret;
5953 int saved_wsp = warn_strict_prototypes;
5954
5955 /* This avoids warnings about unprototyped casts on
5956 integers. E.g. "#define SIG_DFL (void(*)())0". */
5957 if (TREE_CODE (expr) == INTEGER_CST)
5958 warn_strict_prototypes = 0;
5959 type = groktypename (type_name, &type_expr, &type_expr_const);
5960 warn_strict_prototypes = saved_wsp;
5961
5962 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5963 && reject_gcc_builtin (expr))
5964 return error_mark_node;
5965
5966 ret = build_c_cast (loc, type, expr);
5967 if (type_expr)
5968 {
5969 bool inner_expr_const = true;
5970 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5971 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5972 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5973 && inner_expr_const);
5974 SET_EXPR_LOCATION (ret, loc);
5975 }
5976
5977 if (!EXPR_HAS_LOCATION (ret))
5978 protected_set_expr_location (ret, loc);
5979
5980 /* C++ does not permits types to be defined in a cast, but it
5981 allows references to incomplete types. */
5982 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5983 warning_at (loc, OPT_Wc___compat,
5984 "defining a type in a cast is invalid in C++");
5985
5986 return ret;
5987 }
5988 \f
5989 /* Build an assignment expression of lvalue LHS from value RHS.
5990 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5991 may differ from TREE_TYPE (LHS) for an enum bitfield.
5992 MODIFYCODE is the code for a binary operator that we use
5993 to combine the old value of LHS with RHS to get the new value.
5994 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5995 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5996 which may differ from TREE_TYPE (RHS) for an enum value.
5997
5998 LOCATION is the location of the MODIFYCODE operator.
5999 RHS_LOC is the location of the RHS. */
6000
6001 tree
6002 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6003 enum tree_code modifycode,
6004 location_t rhs_loc, tree rhs, tree rhs_origtype)
6005 {
6006 tree result;
6007 tree newrhs;
6008 tree rhseval = NULL_TREE;
6009 tree lhstype = TREE_TYPE (lhs);
6010 tree olhstype = lhstype;
6011 bool npc;
6012 bool is_atomic_op;
6013
6014 /* Types that aren't fully specified cannot be used in assignments. */
6015 lhs = require_complete_type (location, lhs);
6016
6017 /* Avoid duplicate error messages from operands that had errors. */
6018 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6019 return error_mark_node;
6020
6021 /* Ensure an error for assigning a non-lvalue array to an array in
6022 C90. */
6023 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6024 {
6025 error_at (location, "assignment to expression with array type");
6026 return error_mark_node;
6027 }
6028
6029 /* For ObjC properties, defer this check. */
6030 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6031 return error_mark_node;
6032
6033 is_atomic_op = really_atomic_lvalue (lhs);
6034
6035 newrhs = rhs;
6036
6037 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6038 {
6039 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6040 lhs_origtype, modifycode, rhs_loc, rhs,
6041 rhs_origtype);
6042 if (inner == error_mark_node)
6043 return error_mark_node;
6044 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6045 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6046 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6047 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6048 protected_set_expr_location (result, location);
6049 return result;
6050 }
6051
6052 /* If a binary op has been requested, combine the old LHS value with the RHS
6053 producing the value we should actually store into the LHS. */
6054
6055 if (modifycode != NOP_EXPR)
6056 {
6057 lhs = c_fully_fold (lhs, false, NULL, true);
6058 lhs = stabilize_reference (lhs);
6059
6060 /* Construct the RHS for any non-atomic compound assignemnt. */
6061 if (!is_atomic_op)
6062 {
6063 /* If in LHS op= RHS the RHS has side-effects, ensure they
6064 are preevaluated before the rest of the assignment expression's
6065 side-effects, because RHS could contain e.g. function calls
6066 that modify LHS. */
6067 if (TREE_SIDE_EFFECTS (rhs))
6068 {
6069 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6070 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6071 else
6072 newrhs = save_expr (rhs);
6073 rhseval = newrhs;
6074 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6075 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6076 newrhs);
6077 }
6078 newrhs = build_binary_op (location,
6079 modifycode, lhs, newrhs, true);
6080
6081 /* The original type of the right hand side is no longer
6082 meaningful. */
6083 rhs_origtype = NULL_TREE;
6084 }
6085 }
6086
6087 if (c_dialect_objc ())
6088 {
6089 /* Check if we are modifying an Objective-C property reference;
6090 if so, we need to generate setter calls. */
6091 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6092 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6093 else
6094 result = objc_maybe_build_modify_expr (lhs, newrhs);
6095 if (result)
6096 goto return_result;
6097
6098 /* Else, do the check that we postponed for Objective-C. */
6099 if (!lvalue_or_else (location, lhs, lv_assign))
6100 return error_mark_node;
6101 }
6102
6103 /* Give an error for storing in something that is 'const'. */
6104
6105 if (TYPE_READONLY (lhstype)
6106 || (RECORD_OR_UNION_TYPE_P (lhstype)
6107 && C_TYPE_FIELDS_READONLY (lhstype)))
6108 {
6109 readonly_error (location, lhs, lv_assign);
6110 return error_mark_node;
6111 }
6112 else if (TREE_READONLY (lhs))
6113 readonly_warning (lhs, lv_assign);
6114
6115 /* If storing into a structure or union member,
6116 it has probably been given type `int'.
6117 Compute the type that would go with
6118 the actual amount of storage the member occupies. */
6119
6120 if (TREE_CODE (lhs) == COMPONENT_REF
6121 && (TREE_CODE (lhstype) == INTEGER_TYPE
6122 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6123 || TREE_CODE (lhstype) == REAL_TYPE
6124 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6125 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6126
6127 /* If storing in a field that is in actuality a short or narrower than one,
6128 we must store in the field in its actual type. */
6129
6130 if (lhstype != TREE_TYPE (lhs))
6131 {
6132 lhs = copy_node (lhs);
6133 TREE_TYPE (lhs) = lhstype;
6134 }
6135
6136 /* Issue -Wc++-compat warnings about an assignment to an enum type
6137 when LHS does not have its original type. This happens for,
6138 e.g., an enum bitfield in a struct. */
6139 if (warn_cxx_compat
6140 && lhs_origtype != NULL_TREE
6141 && lhs_origtype != lhstype
6142 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6143 {
6144 tree checktype = (rhs_origtype != NULL_TREE
6145 ? rhs_origtype
6146 : TREE_TYPE (rhs));
6147 if (checktype != error_mark_node
6148 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6149 || (is_atomic_op && modifycode != NOP_EXPR)))
6150 warning_at (location, OPT_Wc___compat,
6151 "enum conversion in assignment is invalid in C++");
6152 }
6153
6154 /* If the lhs is atomic, remove that qualifier. */
6155 if (is_atomic_op)
6156 {
6157 lhstype = build_qualified_type (lhstype,
6158 (TYPE_QUALS (lhstype)
6159 & ~TYPE_QUAL_ATOMIC));
6160 olhstype = build_qualified_type (olhstype,
6161 (TYPE_QUALS (lhstype)
6162 & ~TYPE_QUAL_ATOMIC));
6163 }
6164
6165 /* Convert new value to destination type. Fold it first, then
6166 restore any excess precision information, for the sake of
6167 conversion warnings. */
6168
6169 if (!(is_atomic_op && modifycode != NOP_EXPR))
6170 {
6171 tree rhs_semantic_type = NULL_TREE;
6172 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6173 {
6174 rhs_semantic_type = TREE_TYPE (newrhs);
6175 newrhs = TREE_OPERAND (newrhs, 0);
6176 }
6177 npc = null_pointer_constant_p (newrhs);
6178 newrhs = c_fully_fold (newrhs, false, NULL);
6179 if (rhs_semantic_type)
6180 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6181 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6182 rhs_origtype, ic_assign, npc,
6183 NULL_TREE, NULL_TREE, 0);
6184 if (TREE_CODE (newrhs) == ERROR_MARK)
6185 return error_mark_node;
6186 }
6187
6188 /* Emit ObjC write barrier, if necessary. */
6189 if (c_dialect_objc () && flag_objc_gc)
6190 {
6191 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6192 if (result)
6193 {
6194 protected_set_expr_location (result, location);
6195 goto return_result;
6196 }
6197 }
6198
6199 /* Scan operands. */
6200
6201 if (is_atomic_op)
6202 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6203 else
6204 {
6205 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6206 TREE_SIDE_EFFECTS (result) = 1;
6207 protected_set_expr_location (result, location);
6208 }
6209
6210 /* If we got the LHS in a different type for storing in,
6211 convert the result back to the nominal type of LHS
6212 so that the value we return always has the same type
6213 as the LHS argument. */
6214
6215 if (olhstype == TREE_TYPE (result))
6216 goto return_result;
6217
6218 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6219 rhs_origtype, ic_assign, false, NULL_TREE,
6220 NULL_TREE, 0);
6221 protected_set_expr_location (result, location);
6222
6223 return_result:
6224 if (rhseval)
6225 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6226 return result;
6227 }
6228 \f
6229 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6230 This is used to implement -fplan9-extensions. */
6231
6232 static bool
6233 find_anonymous_field_with_type (tree struct_type, tree type)
6234 {
6235 tree field;
6236 bool found;
6237
6238 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6239 found = false;
6240 for (field = TYPE_FIELDS (struct_type);
6241 field != NULL_TREE;
6242 field = TREE_CHAIN (field))
6243 {
6244 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6245 ? c_build_qualified_type (TREE_TYPE (field),
6246 TYPE_QUAL_ATOMIC)
6247 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6248 if (DECL_NAME (field) == NULL
6249 && comptypes (type, fieldtype))
6250 {
6251 if (found)
6252 return false;
6253 found = true;
6254 }
6255 else if (DECL_NAME (field) == NULL
6256 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6257 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6258 {
6259 if (found)
6260 return false;
6261 found = true;
6262 }
6263 }
6264 return found;
6265 }
6266
6267 /* RHS is an expression whose type is pointer to struct. If there is
6268 an anonymous field in RHS with type TYPE, then return a pointer to
6269 that field in RHS. This is used with -fplan9-extensions. This
6270 returns NULL if no conversion could be found. */
6271
6272 static tree
6273 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6274 {
6275 tree rhs_struct_type, lhs_main_type;
6276 tree field, found_field;
6277 bool found_sub_field;
6278 tree ret;
6279
6280 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6281 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6282 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6283
6284 gcc_assert (POINTER_TYPE_P (type));
6285 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6286 ? c_build_qualified_type (TREE_TYPE (type),
6287 TYPE_QUAL_ATOMIC)
6288 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6289
6290 found_field = NULL_TREE;
6291 found_sub_field = false;
6292 for (field = TYPE_FIELDS (rhs_struct_type);
6293 field != NULL_TREE;
6294 field = TREE_CHAIN (field))
6295 {
6296 if (DECL_NAME (field) != NULL_TREE
6297 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6298 continue;
6299 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6300 ? c_build_qualified_type (TREE_TYPE (field),
6301 TYPE_QUAL_ATOMIC)
6302 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6303 if (comptypes (lhs_main_type, fieldtype))
6304 {
6305 if (found_field != NULL_TREE)
6306 return NULL_TREE;
6307 found_field = field;
6308 }
6309 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6310 lhs_main_type))
6311 {
6312 if (found_field != NULL_TREE)
6313 return NULL_TREE;
6314 found_field = field;
6315 found_sub_field = true;
6316 }
6317 }
6318
6319 if (found_field == NULL_TREE)
6320 return NULL_TREE;
6321
6322 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6323 build_fold_indirect_ref (rhs), found_field,
6324 NULL_TREE);
6325 ret = build_fold_addr_expr_loc (location, ret);
6326
6327 if (found_sub_field)
6328 {
6329 ret = convert_to_anonymous_field (location, type, ret);
6330 gcc_assert (ret != NULL_TREE);
6331 }
6332
6333 return ret;
6334 }
6335
6336 /* Issue an error message for a bad initializer component.
6337 GMSGID identifies the message.
6338 The component name is taken from the spelling stack. */
6339
6340 static void ATTRIBUTE_GCC_DIAG (2,0)
6341 error_init (location_t loc, const char *gmsgid, ...)
6342 {
6343 char *ofwhat;
6344
6345 auto_diagnostic_group d;
6346
6347 /* The gmsgid may be a format string with %< and %>. */
6348 va_list ap;
6349 va_start (ap, gmsgid);
6350 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6351 va_end (ap);
6352
6353 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6354 if (*ofwhat && warned)
6355 inform (loc, "(near initialization for %qs)", ofwhat);
6356 }
6357
6358 /* Issue a pedantic warning for a bad initializer component. OPT is
6359 the option OPT_* (from options.h) controlling this warning or 0 if
6360 it is unconditionally given. GMSGID identifies the message. The
6361 component name is taken from the spelling stack. */
6362
6363 static void ATTRIBUTE_GCC_DIAG (3,0)
6364 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6365 {
6366 /* Use the location where a macro was expanded rather than where
6367 it was defined to make sure macros defined in system headers
6368 but used incorrectly elsewhere are diagnosed. */
6369 location_t exploc = expansion_point_location_if_in_system_header (loc);
6370 auto_diagnostic_group d;
6371 va_list ap;
6372 va_start (ap, gmsgid);
6373 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6374 va_end (ap);
6375 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6376 if (*ofwhat && warned)
6377 inform (exploc, "(near initialization for %qs)", ofwhat);
6378 }
6379
6380 /* Issue a warning for a bad initializer component.
6381
6382 OPT is the OPT_W* value corresponding to the warning option that
6383 controls this warning. GMSGID identifies the message. The
6384 component name is taken from the spelling stack. */
6385
6386 static void
6387 warning_init (location_t loc, int opt, const char *gmsgid)
6388 {
6389 char *ofwhat;
6390 bool warned;
6391
6392 auto_diagnostic_group d;
6393
6394 /* Use the location where a macro was expanded rather than where
6395 it was defined to make sure macros defined in system headers
6396 but used incorrectly elsewhere are diagnosed. */
6397 location_t exploc = expansion_point_location_if_in_system_header (loc);
6398
6399 /* The gmsgid may be a format string with %< and %>. */
6400 warned = warning_at (exploc, opt, gmsgid);
6401 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6402 if (*ofwhat && warned)
6403 inform (exploc, "(near initialization for %qs)", ofwhat);
6404 }
6405 \f
6406 /* If TYPE is an array type and EXPR is a parenthesized string
6407 constant, warn if pedantic that EXPR is being used to initialize an
6408 object of type TYPE. */
6409
6410 void
6411 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6412 {
6413 if (pedantic
6414 && TREE_CODE (type) == ARRAY_TYPE
6415 && TREE_CODE (expr.value) == STRING_CST
6416 && expr.original_code != STRING_CST)
6417 pedwarn_init (loc, OPT_Wpedantic,
6418 "array initialized from parenthesized string constant");
6419 }
6420
6421 /* Attempt to locate the parameter with the given index within FNDECL,
6422 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6423
6424 static location_t
6425 get_fndecl_argument_location (tree fndecl, int argnum)
6426 {
6427 int i;
6428 tree param;
6429
6430 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6431 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6432 i < argnum && param;
6433 i++, param = TREE_CHAIN (param))
6434 ;
6435
6436 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6437 return DECL_SOURCE_LOCATION (FNDECL). */
6438 if (param == NULL)
6439 return DECL_SOURCE_LOCATION (fndecl);
6440
6441 return DECL_SOURCE_LOCATION (param);
6442 }
6443
6444 /* Issue a note about a mismatching argument for parameter PARMNUM
6445 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6446 Attempt to issue the note at the pertinent parameter of the decl;
6447 failing that issue it at the location of FUNDECL; failing that
6448 issue it at PLOC. */
6449
6450 static void
6451 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6452 tree expected_type, tree actual_type)
6453 {
6454 location_t loc;
6455 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6456 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6457 else
6458 loc = ploc;
6459
6460 inform (loc,
6461 "expected %qT but argument is of type %qT",
6462 expected_type, actual_type);
6463 }
6464
6465 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6466 function FUNDECL declared without prototype to parameter PARMNUM of
6467 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6468
6469 static void
6470 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6471 tree parmtype, tree argtype)
6472 {
6473 tree_code parmcode = TREE_CODE (parmtype);
6474 tree_code argcode = TREE_CODE (argtype);
6475 tree promoted = c_type_promotes_to (argtype);
6476
6477 /* Avoid warning for enum arguments that promote to an integer type
6478 of the same size/mode. */
6479 if (parmcode == INTEGER_TYPE
6480 && argcode == ENUMERAL_TYPE
6481 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6482 return;
6483
6484 if ((parmcode == argcode
6485 || (parmcode == INTEGER_TYPE
6486 && argcode == ENUMERAL_TYPE))
6487 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6488 return;
6489
6490 /* This diagnoses even signed/unsigned mismatches. Those might be
6491 safe in many cases but GCC may emit suboptimal code for them so
6492 warning on those cases drives efficiency improvements. */
6493 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6494 TYPE_MAIN_VARIANT (promoted) == argtype
6495 ? G_("%qD argument %d type is %qT where %qT is expected "
6496 "in a call to built-in function declared without "
6497 "prototype")
6498 : G_("%qD argument %d promotes to %qT where %qT is expected "
6499 "in a call to built-in function declared without "
6500 "prototype"),
6501 fundecl, parmnum, promoted, parmtype))
6502 inform (DECL_SOURCE_LOCATION (fundecl),
6503 "built-in %qD declared here",
6504 fundecl);
6505 }
6506
6507 /* Convert value RHS to type TYPE as preparation for an assignment to
6508 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6509 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6510 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6511 constant before any folding.
6512 The real work of conversion is done by `convert'.
6513 The purpose of this function is to generate error messages
6514 for assignments that are not allowed in C.
6515 ERRTYPE says whether it is argument passing, assignment,
6516 initialization or return.
6517
6518 In the following example, '~' denotes where EXPR_LOC and '^' where
6519 LOCATION point to:
6520
6521 f (var); [ic_argpass]
6522 ^ ~~~
6523 x = var; [ic_assign]
6524 ^ ~~~;
6525 int x = var; [ic_init]
6526 ^^^
6527 return x; [ic_return]
6528 ^
6529
6530 FUNCTION is a tree for the function being called.
6531 PARMNUM is the number of the argument, for printing in error messages.
6532 WARNOPT may be set to a warning option to issue the corresponding warning
6533 rather than an error for invalid conversions. Used for calls to built-in
6534 functions declared without a prototype. */
6535
6536 static tree
6537 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6538 tree rhs, tree origtype, enum impl_conv errtype,
6539 bool null_pointer_constant, tree fundecl,
6540 tree function, int parmnum, int warnopt /* = 0 */)
6541 {
6542 enum tree_code codel = TREE_CODE (type);
6543 tree orig_rhs = rhs;
6544 tree rhstype;
6545 enum tree_code coder;
6546 tree rname = NULL_TREE;
6547 bool objc_ok = false;
6548
6549 /* Use the expansion point location to handle cases such as user's
6550 function returning a wrong-type macro defined in a system header. */
6551 location = expansion_point_location_if_in_system_header (location);
6552
6553 if (errtype == ic_argpass)
6554 {
6555 tree selector;
6556 /* Change pointer to function to the function itself for
6557 diagnostics. */
6558 if (TREE_CODE (function) == ADDR_EXPR
6559 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6560 function = TREE_OPERAND (function, 0);
6561
6562 /* Handle an ObjC selector specially for diagnostics. */
6563 selector = objc_message_selector ();
6564 rname = function;
6565 if (selector && parmnum > 2)
6566 {
6567 rname = selector;
6568 parmnum -= 2;
6569 }
6570 }
6571
6572 /* This macro is used to emit diagnostics to ensure that all format
6573 strings are complete sentences, visible to gettext and checked at
6574 compile time. */
6575 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6576 do { \
6577 switch (errtype) \
6578 { \
6579 case ic_argpass: \
6580 { \
6581 auto_diagnostic_group d; \
6582 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6583 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6584 } \
6585 break; \
6586 case ic_assign: \
6587 pedwarn (LOCATION, OPT, AS); \
6588 break; \
6589 case ic_init: \
6590 pedwarn_init (LOCATION, OPT, IN); \
6591 break; \
6592 case ic_return: \
6593 pedwarn (LOCATION, OPT, RE); \
6594 break; \
6595 default: \
6596 gcc_unreachable (); \
6597 } \
6598 } while (0)
6599
6600 /* This macro is used to emit diagnostics to ensure that all format
6601 strings are complete sentences, visible to gettext and checked at
6602 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6603 extra parameter to enumerate qualifiers. */
6604 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6605 do { \
6606 switch (errtype) \
6607 { \
6608 case ic_argpass: \
6609 { \
6610 auto_diagnostic_group d; \
6611 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6612 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6613 } \
6614 break; \
6615 case ic_assign: \
6616 pedwarn (LOCATION, OPT, AS, QUALS); \
6617 break; \
6618 case ic_init: \
6619 pedwarn (LOCATION, OPT, IN, QUALS); \
6620 break; \
6621 case ic_return: \
6622 pedwarn (LOCATION, OPT, RE, QUALS); \
6623 break; \
6624 default: \
6625 gcc_unreachable (); \
6626 } \
6627 } while (0)
6628
6629 /* This macro is used to emit diagnostics to ensure that all format
6630 strings are complete sentences, visible to gettext and checked at
6631 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6632 warning_at instead of pedwarn. */
6633 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6634 do { \
6635 switch (errtype) \
6636 { \
6637 case ic_argpass: \
6638 { \
6639 auto_diagnostic_group d; \
6640 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6641 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6642 } \
6643 break; \
6644 case ic_assign: \
6645 warning_at (LOCATION, OPT, AS, QUALS); \
6646 break; \
6647 case ic_init: \
6648 warning_at (LOCATION, OPT, IN, QUALS); \
6649 break; \
6650 case ic_return: \
6651 warning_at (LOCATION, OPT, RE, QUALS); \
6652 break; \
6653 default: \
6654 gcc_unreachable (); \
6655 } \
6656 } while (0)
6657
6658 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6659 rhs = TREE_OPERAND (rhs, 0);
6660
6661 rhstype = TREE_TYPE (rhs);
6662 coder = TREE_CODE (rhstype);
6663
6664 if (coder == ERROR_MARK)
6665 return error_mark_node;
6666
6667 if (c_dialect_objc ())
6668 {
6669 int parmno;
6670
6671 switch (errtype)
6672 {
6673 case ic_return:
6674 parmno = 0;
6675 break;
6676
6677 case ic_assign:
6678 parmno = -1;
6679 break;
6680
6681 case ic_init:
6682 parmno = -2;
6683 break;
6684
6685 default:
6686 parmno = parmnum;
6687 break;
6688 }
6689
6690 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6691 }
6692
6693 if (warn_cxx_compat)
6694 {
6695 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6696 if (checktype != error_mark_node
6697 && TREE_CODE (type) == ENUMERAL_TYPE
6698 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6699 switch (errtype)
6700 {
6701 case ic_argpass:
6702 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6703 "passing argument %d of %qE is invalid in C++",
6704 parmnum, rname))
6705 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6706 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6707 "expected %qT but argument is of type %qT",
6708 type, rhstype);
6709 break;
6710 case ic_assign:
6711 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6712 "%qT in assignment is invalid in C++", rhstype, type);
6713 break;
6714 case ic_init:
6715 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6716 "%qT to %qT in initialization is invalid in C++",
6717 rhstype, type);
6718 break;
6719 case ic_return:
6720 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6721 "%qT in return is invalid in C++", rhstype, type);
6722 break;
6723 default:
6724 gcc_unreachable ();
6725 }
6726 }
6727
6728 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6729 {
6730 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6731 return rhs;
6732 }
6733
6734 if (coder == VOID_TYPE)
6735 {
6736 /* Except for passing an argument to an unprototyped function,
6737 this is a constraint violation. When passing an argument to
6738 an unprototyped function, it is compile-time undefined;
6739 making it a constraint in that case was rejected in
6740 DR#252. */
6741 const char msg[] = "void value not ignored as it ought to be";
6742 if (warnopt)
6743 warning_at (location, warnopt, msg);
6744 else
6745 error_at (location, msg);
6746 return error_mark_node;
6747 }
6748 rhs = require_complete_type (location, rhs);
6749 if (rhs == error_mark_node)
6750 return error_mark_node;
6751
6752 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6753 return error_mark_node;
6754
6755 /* A non-reference type can convert to a reference. This handles
6756 va_start, va_copy and possibly port built-ins. */
6757 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6758 {
6759 if (!lvalue_p (rhs))
6760 {
6761 const char msg[] = "cannot pass rvalue to reference parameter";
6762 if (warnopt)
6763 warning_at (location, warnopt, msg);
6764 else
6765 error_at (location, msg);
6766 return error_mark_node;
6767 }
6768 if (!c_mark_addressable (rhs))
6769 return error_mark_node;
6770 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6771 SET_EXPR_LOCATION (rhs, location);
6772
6773 rhs = convert_for_assignment (location, expr_loc,
6774 build_pointer_type (TREE_TYPE (type)),
6775 rhs, origtype, errtype,
6776 null_pointer_constant, fundecl, function,
6777 parmnum, warnopt);
6778 if (rhs == error_mark_node)
6779 return error_mark_node;
6780
6781 rhs = build1 (NOP_EXPR, type, rhs);
6782 SET_EXPR_LOCATION (rhs, location);
6783 return rhs;
6784 }
6785 /* Some types can interconvert without explicit casts. */
6786 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6787 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6788 return convert (type, rhs);
6789 /* Arithmetic types all interconvert, and enum is treated like int. */
6790 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6791 || codel == FIXED_POINT_TYPE
6792 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6793 || codel == BOOLEAN_TYPE)
6794 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6795 || coder == FIXED_POINT_TYPE
6796 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6797 || coder == BOOLEAN_TYPE))
6798 {
6799 if (warnopt && errtype == ic_argpass)
6800 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
6801 rhstype);
6802
6803 bool save = in_late_binary_op;
6804 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6805 || (coder == REAL_TYPE
6806 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6807 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6808 in_late_binary_op = true;
6809 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6810 ? expr_loc : location, type, orig_rhs);
6811 in_late_binary_op = save;
6812 return ret;
6813 }
6814
6815 /* Aggregates in different TUs might need conversion. */
6816 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6817 && codel == coder
6818 && comptypes (type, rhstype))
6819 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6820 ? expr_loc : location, type, rhs);
6821
6822 /* Conversion to a transparent union or record from its member types.
6823 This applies only to function arguments. */
6824 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6825 && TYPE_TRANSPARENT_AGGR (type))
6826 && errtype == ic_argpass)
6827 {
6828 tree memb, marginal_memb = NULL_TREE;
6829
6830 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6831 {
6832 tree memb_type = TREE_TYPE (memb);
6833
6834 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6835 TYPE_MAIN_VARIANT (rhstype)))
6836 break;
6837
6838 if (TREE_CODE (memb_type) != POINTER_TYPE)
6839 continue;
6840
6841 if (coder == POINTER_TYPE)
6842 {
6843 tree ttl = TREE_TYPE (memb_type);
6844 tree ttr = TREE_TYPE (rhstype);
6845
6846 /* Any non-function converts to a [const][volatile] void *
6847 and vice versa; otherwise, targets must be the same.
6848 Meanwhile, the lhs target must have all the qualifiers of
6849 the rhs. */
6850 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6851 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6852 || comp_target_types (location, memb_type, rhstype))
6853 {
6854 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6855 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6856 /* If this type won't generate any warnings, use it. */
6857 if (lquals == rquals
6858 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6859 && TREE_CODE (ttl) == FUNCTION_TYPE)
6860 ? ((lquals | rquals) == rquals)
6861 : ((lquals | rquals) == lquals)))
6862 break;
6863
6864 /* Keep looking for a better type, but remember this one. */
6865 if (!marginal_memb)
6866 marginal_memb = memb;
6867 }
6868 }
6869
6870 /* Can convert integer zero to any pointer type. */
6871 if (null_pointer_constant)
6872 {
6873 rhs = null_pointer_node;
6874 break;
6875 }
6876 }
6877
6878 if (memb || marginal_memb)
6879 {
6880 if (!memb)
6881 {
6882 /* We have only a marginally acceptable member type;
6883 it needs a warning. */
6884 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6885 tree ttr = TREE_TYPE (rhstype);
6886
6887 /* Const and volatile mean something different for function
6888 types, so the usual warnings are not appropriate. */
6889 if (TREE_CODE (ttr) == FUNCTION_TYPE
6890 && TREE_CODE (ttl) == FUNCTION_TYPE)
6891 {
6892 /* Because const and volatile on functions are
6893 restrictions that say the function will not do
6894 certain things, it is okay to use a const or volatile
6895 function where an ordinary one is wanted, but not
6896 vice-versa. */
6897 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6898 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6899 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6900 OPT_Wdiscarded_qualifiers,
6901 G_("passing argument %d of %qE "
6902 "makes %q#v qualified function "
6903 "pointer from unqualified"),
6904 G_("assignment makes %q#v qualified "
6905 "function pointer from "
6906 "unqualified"),
6907 G_("initialization makes %q#v qualified "
6908 "function pointer from "
6909 "unqualified"),
6910 G_("return makes %q#v qualified function "
6911 "pointer from unqualified"),
6912 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6913 }
6914 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6915 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6916 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6917 OPT_Wdiscarded_qualifiers,
6918 G_("passing argument %d of %qE discards "
6919 "%qv qualifier from pointer target type"),
6920 G_("assignment discards %qv qualifier "
6921 "from pointer target type"),
6922 G_("initialization discards %qv qualifier "
6923 "from pointer target type"),
6924 G_("return discards %qv qualifier from "
6925 "pointer target type"),
6926 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6927
6928 memb = marginal_memb;
6929 }
6930
6931 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6932 pedwarn (location, OPT_Wpedantic,
6933 "ISO C prohibits argument conversion to union type");
6934
6935 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6936 return build_constructor_single (type, memb, rhs);
6937 }
6938 }
6939
6940 /* Conversions among pointers */
6941 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6942 && (coder == codel))
6943 {
6944 /* If RHS refers to a built-in declared without a prototype
6945 BLTIN is the declaration of the built-in with a prototype
6946 and RHSTYPE is set to the actual type of the built-in. */
6947 tree bltin;
6948 rhstype = type_or_builtin_type (rhs, &bltin);
6949
6950 tree ttl = TREE_TYPE (type);
6951 tree ttr = TREE_TYPE (rhstype);
6952 tree mvl = ttl;
6953 tree mvr = ttr;
6954 bool is_opaque_pointer;
6955 int target_cmp = 0; /* Cache comp_target_types () result. */
6956 addr_space_t asl;
6957 addr_space_t asr;
6958
6959 if (TREE_CODE (mvl) != ARRAY_TYPE)
6960 mvl = (TYPE_ATOMIC (mvl)
6961 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6962 TYPE_QUAL_ATOMIC)
6963 : TYPE_MAIN_VARIANT (mvl));
6964 if (TREE_CODE (mvr) != ARRAY_TYPE)
6965 mvr = (TYPE_ATOMIC (mvr)
6966 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6967 TYPE_QUAL_ATOMIC)
6968 : TYPE_MAIN_VARIANT (mvr));
6969 /* Opaque pointers are treated like void pointers. */
6970 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6971
6972 /* The Plan 9 compiler permits a pointer to a struct to be
6973 automatically converted into a pointer to an anonymous field
6974 within the struct. */
6975 if (flag_plan9_extensions
6976 && RECORD_OR_UNION_TYPE_P (mvl)
6977 && RECORD_OR_UNION_TYPE_P (mvr)
6978 && mvl != mvr)
6979 {
6980 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6981 if (new_rhs != NULL_TREE)
6982 {
6983 rhs = new_rhs;
6984 rhstype = TREE_TYPE (rhs);
6985 coder = TREE_CODE (rhstype);
6986 ttr = TREE_TYPE (rhstype);
6987 mvr = TYPE_MAIN_VARIANT (ttr);
6988 }
6989 }
6990
6991 /* C++ does not allow the implicit conversion void* -> T*. However,
6992 for the purpose of reducing the number of false positives, we
6993 tolerate the special case of
6994
6995 int *p = NULL;
6996
6997 where NULL is typically defined in C to be '(void *) 0'. */
6998 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6999 warning_at (errtype == ic_argpass ? expr_loc : location,
7000 OPT_Wc___compat,
7001 "request for implicit conversion "
7002 "from %qT to %qT not permitted in C++", rhstype, type);
7003
7004 /* See if the pointers point to incompatible address spaces. */
7005 asl = TYPE_ADDR_SPACE (ttl);
7006 asr = TYPE_ADDR_SPACE (ttr);
7007 if (!null_pointer_constant_p (rhs)
7008 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7009 {
7010 switch (errtype)
7011 {
7012 case ic_argpass:
7013 {
7014 const char msg[] = G_("passing argument %d of %qE from "
7015 "pointer to non-enclosed address space");
7016 if (warnopt)
7017 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7018 else
7019 error_at (expr_loc, msg, parmnum, rname);
7020 break;
7021 }
7022 case ic_assign:
7023 {
7024 const char msg[] = G_("assignment from pointer to "
7025 "non-enclosed address space");
7026 if (warnopt)
7027 warning_at (location, warnopt, msg);
7028 else
7029 error_at (location, msg);
7030 break;
7031 }
7032 case ic_init:
7033 {
7034 const char msg[] = G_("initialization from pointer to "
7035 "non-enclosed address space");
7036 if (warnopt)
7037 warning_at (location, warnopt, msg);
7038 else
7039 error_at (location, msg);
7040 break;
7041 }
7042 case ic_return:
7043 {
7044 const char msg[] = G_("return from pointer to "
7045 "non-enclosed address space");
7046 if (warnopt)
7047 warning_at (location, warnopt, msg);
7048 else
7049 error_at (location, msg);
7050 break;
7051 }
7052 default:
7053 gcc_unreachable ();
7054 }
7055 return error_mark_node;
7056 }
7057
7058 /* Check if the right-hand side has a format attribute but the
7059 left-hand side doesn't. */
7060 if (warn_suggest_attribute_format
7061 && check_missing_format_attribute (type, rhstype))
7062 {
7063 switch (errtype)
7064 {
7065 case ic_argpass:
7066 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7067 "argument %d of %qE might be "
7068 "a candidate for a format attribute",
7069 parmnum, rname);
7070 break;
7071 case ic_assign:
7072 warning_at (location, OPT_Wsuggest_attribute_format,
7073 "assignment left-hand side might be "
7074 "a candidate for a format attribute");
7075 break;
7076 case ic_init:
7077 warning_at (location, OPT_Wsuggest_attribute_format,
7078 "initialization left-hand side might be "
7079 "a candidate for a format attribute");
7080 break;
7081 case ic_return:
7082 warning_at (location, OPT_Wsuggest_attribute_format,
7083 "return type might be "
7084 "a candidate for a format attribute");
7085 break;
7086 default:
7087 gcc_unreachable ();
7088 }
7089 }
7090
7091 /* Any non-function converts to a [const][volatile] void *
7092 and vice versa; otherwise, targets must be the same.
7093 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7094 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7095 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7096 || (target_cmp = comp_target_types (location, type, rhstype))
7097 || is_opaque_pointer
7098 || ((c_common_unsigned_type (mvl)
7099 == c_common_unsigned_type (mvr))
7100 && (c_common_signed_type (mvl)
7101 == c_common_signed_type (mvr))
7102 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7103 {
7104 /* Warn about loss of qualifers from pointers to arrays with
7105 qualifiers on the element type. */
7106 if (TREE_CODE (ttr) == ARRAY_TYPE)
7107 {
7108 ttr = strip_array_types (ttr);
7109 ttl = strip_array_types (ttl);
7110
7111 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7112 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7113 WARNING_FOR_QUALIFIERS (location, expr_loc,
7114 OPT_Wdiscarded_array_qualifiers,
7115 G_("passing argument %d of %qE discards "
7116 "%qv qualifier from pointer target type"),
7117 G_("assignment discards %qv qualifier "
7118 "from pointer target type"),
7119 G_("initialization discards %qv qualifier "
7120 "from pointer target type"),
7121 G_("return discards %qv qualifier from "
7122 "pointer target type"),
7123 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7124 }
7125 else if (pedantic
7126 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7127 ||
7128 (VOID_TYPE_P (ttr)
7129 && !null_pointer_constant
7130 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7131 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7132 G_("ISO C forbids passing argument %d of "
7133 "%qE between function pointer "
7134 "and %<void *%>"),
7135 G_("ISO C forbids assignment between "
7136 "function pointer and %<void *%>"),
7137 G_("ISO C forbids initialization between "
7138 "function pointer and %<void *%>"),
7139 G_("ISO C forbids return between function "
7140 "pointer and %<void *%>"));
7141 /* Const and volatile mean something different for function types,
7142 so the usual warnings are not appropriate. */
7143 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7144 && TREE_CODE (ttl) != FUNCTION_TYPE)
7145 {
7146 /* Don't warn about loss of qualifier for conversions from
7147 qualified void* to pointers to arrays with corresponding
7148 qualifier on the element type. */
7149 if (!pedantic)
7150 ttl = strip_array_types (ttl);
7151
7152 /* Assignments between atomic and non-atomic objects are OK. */
7153 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7154 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7155 {
7156 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7157 OPT_Wdiscarded_qualifiers,
7158 G_("passing argument %d of %qE discards "
7159 "%qv qualifier from pointer target type"),
7160 G_("assignment discards %qv qualifier "
7161 "from pointer target type"),
7162 G_("initialization discards %qv qualifier "
7163 "from pointer target type"),
7164 G_("return discards %qv qualifier from "
7165 "pointer target type"),
7166 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7167 }
7168 /* If this is not a case of ignoring a mismatch in signedness,
7169 no warning. */
7170 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7171 || target_cmp)
7172 ;
7173 /* If there is a mismatch, do warn. */
7174 else if (warn_pointer_sign)
7175 switch (errtype)
7176 {
7177 case ic_argpass:
7178 {
7179 auto_diagnostic_group d;
7180 range_label_for_type_mismatch rhs_label (rhstype, type);
7181 gcc_rich_location richloc (expr_loc, &rhs_label);
7182 if (pedwarn (&richloc, OPT_Wpointer_sign,
7183 "pointer targets in passing argument %d of "
7184 "%qE differ in signedness", parmnum, rname))
7185 inform_for_arg (fundecl, expr_loc, parmnum, type,
7186 rhstype);
7187 }
7188 break;
7189 case ic_assign:
7190 pedwarn (location, OPT_Wpointer_sign,
7191 "pointer targets in assignment from %qT to %qT "
7192 "differ in signedness", rhstype, type);
7193 break;
7194 case ic_init:
7195 pedwarn_init (location, OPT_Wpointer_sign,
7196 "pointer targets in initialization of %qT "
7197 "from %qT differ in signedness", type,
7198 rhstype);
7199 break;
7200 case ic_return:
7201 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7202 "returning %qT from a function with return type "
7203 "%qT differ in signedness", rhstype, type);
7204 break;
7205 default:
7206 gcc_unreachable ();
7207 }
7208 }
7209 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7210 && TREE_CODE (ttr) == FUNCTION_TYPE)
7211 {
7212 /* Because const and volatile on functions are restrictions
7213 that say the function will not do certain things,
7214 it is okay to use a const or volatile function
7215 where an ordinary one is wanted, but not vice-versa. */
7216 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7217 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7218 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7219 OPT_Wdiscarded_qualifiers,
7220 G_("passing argument %d of %qE makes "
7221 "%q#v qualified function pointer "
7222 "from unqualified"),
7223 G_("assignment makes %q#v qualified function "
7224 "pointer from unqualified"),
7225 G_("initialization makes %q#v qualified "
7226 "function pointer from unqualified"),
7227 G_("return makes %q#v qualified function "
7228 "pointer from unqualified"),
7229 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7230 }
7231 }
7232 /* Avoid warning about the volatile ObjC EH puts on decls. */
7233 else if (!objc_ok)
7234 {
7235 switch (errtype)
7236 {
7237 case ic_argpass:
7238 {
7239 auto_diagnostic_group d;
7240 range_label_for_type_mismatch rhs_label (rhstype, type);
7241 gcc_rich_location richloc (expr_loc, &rhs_label);
7242 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7243 "passing argument %d of %qE from incompatible "
7244 "pointer type", parmnum, rname))
7245 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7246 }
7247 break;
7248 case ic_assign:
7249 if (bltin)
7250 pedwarn (location, OPT_Wincompatible_pointer_types,
7251 "assignment to %qT from pointer to "
7252 "%qD with incompatible type %qT",
7253 type, bltin, rhstype);
7254 else
7255 pedwarn (location, OPT_Wincompatible_pointer_types,
7256 "assignment to %qT from incompatible pointer type %qT",
7257 type, rhstype);
7258 break;
7259 case ic_init:
7260 if (bltin)
7261 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7262 "initialization of %qT from pointer to "
7263 "%qD with incompatible type %qT",
7264 type, bltin, rhstype);
7265 else
7266 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7267 "initialization of %qT from incompatible "
7268 "pointer type %qT",
7269 type, rhstype);
7270 break;
7271 case ic_return:
7272 if (bltin)
7273 pedwarn (location, OPT_Wincompatible_pointer_types,
7274 "returning pointer to %qD of type %qT from "
7275 "a function with incompatible type %qT",
7276 bltin, rhstype, type);
7277 else
7278 pedwarn (location, OPT_Wincompatible_pointer_types,
7279 "returning %qT from a function with incompatible "
7280 "return type %qT", rhstype, type);
7281 break;
7282 default:
7283 gcc_unreachable ();
7284 }
7285 }
7286
7287 /* If RHS isn't an address, check pointer or array of packed
7288 struct or union. */
7289 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7290
7291 return convert (type, rhs);
7292 }
7293 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7294 {
7295 /* ??? This should not be an error when inlining calls to
7296 unprototyped functions. */
7297 const char msg[] = "invalid use of non-lvalue array";
7298 if (warnopt)
7299 warning_at (location, warnopt, msg);
7300 else
7301 error_at (location, msg);
7302 return error_mark_node;
7303 }
7304 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7305 {
7306 /* An explicit constant 0 can convert to a pointer,
7307 or one that results from arithmetic, even including
7308 a cast to integer type. */
7309 if (!null_pointer_constant)
7310 switch (errtype)
7311 {
7312 case ic_argpass:
7313 {
7314 auto_diagnostic_group d;
7315 range_label_for_type_mismatch rhs_label (rhstype, type);
7316 gcc_rich_location richloc (expr_loc, &rhs_label);
7317 if (pedwarn (&richloc, OPT_Wint_conversion,
7318 "passing argument %d of %qE makes pointer from "
7319 "integer without a cast", parmnum, rname))
7320 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7321 }
7322 break;
7323 case ic_assign:
7324 pedwarn (location, OPT_Wint_conversion,
7325 "assignment to %qT from %qT makes pointer from integer "
7326 "without a cast", type, rhstype);
7327 break;
7328 case ic_init:
7329 pedwarn_init (location, OPT_Wint_conversion,
7330 "initialization of %qT from %qT makes pointer from "
7331 "integer without a cast", type, rhstype);
7332 break;
7333 case ic_return:
7334 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7335 "function with return type %qT makes pointer from "
7336 "integer without a cast", rhstype, type);
7337 break;
7338 default:
7339 gcc_unreachable ();
7340 }
7341
7342 return convert (type, rhs);
7343 }
7344 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7345 {
7346 switch (errtype)
7347 {
7348 case ic_argpass:
7349 {
7350 auto_diagnostic_group d;
7351 range_label_for_type_mismatch rhs_label (rhstype, type);
7352 gcc_rich_location richloc (expr_loc, &rhs_label);
7353 if (pedwarn (&richloc, OPT_Wint_conversion,
7354 "passing argument %d of %qE makes integer from "
7355 "pointer without a cast", parmnum, rname))
7356 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7357 }
7358 break;
7359 case ic_assign:
7360 pedwarn (location, OPT_Wint_conversion,
7361 "assignment to %qT from %qT makes integer from pointer "
7362 "without a cast", type, rhstype);
7363 break;
7364 case ic_init:
7365 pedwarn_init (location, OPT_Wint_conversion,
7366 "initialization of %qT from %qT makes integer from "
7367 "pointer without a cast", type, rhstype);
7368 break;
7369 case ic_return:
7370 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7371 "function with return type %qT makes integer from "
7372 "pointer without a cast", rhstype, type);
7373 break;
7374 default:
7375 gcc_unreachable ();
7376 }
7377
7378 return convert (type, rhs);
7379 }
7380 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7381 {
7382 tree ret;
7383 bool save = in_late_binary_op;
7384 in_late_binary_op = true;
7385 ret = convert (type, rhs);
7386 in_late_binary_op = save;
7387 return ret;
7388 }
7389
7390 switch (errtype)
7391 {
7392 case ic_argpass:
7393 {
7394 auto_diagnostic_group d;
7395 range_label_for_type_mismatch rhs_label (rhstype, type);
7396 gcc_rich_location richloc (expr_loc, &rhs_label);
7397 const char msg[] = G_("incompatible type for argument %d of %qE");
7398 if (warnopt)
7399 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7400 else
7401 error_at (&richloc, msg, parmnum, rname);
7402 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7403 }
7404 break;
7405 case ic_assign:
7406 {
7407 const char msg[]
7408 = G_("incompatible types when assigning to type %qT from type %qT");
7409 if (warnopt)
7410 warning_at (expr_loc, 0, msg, type, rhstype);
7411 else
7412 error_at (expr_loc, msg, type, rhstype);
7413 break;
7414 }
7415 case ic_init:
7416 {
7417 const char msg[]
7418 = G_("incompatible types when initializing type %qT using type %qT");
7419 if (warnopt)
7420 warning_at (location, 0, msg, type, rhstype);
7421 else
7422 error_at (location, msg, type, rhstype);
7423 break;
7424 }
7425 case ic_return:
7426 {
7427 const char msg[]
7428 = G_("incompatible types when returning type %qT but %qT was expected");
7429 if (warnopt)
7430 warning_at (location, 0, msg, rhstype, type);
7431 else
7432 error_at (location, msg, rhstype, type);
7433 break;
7434 }
7435 default:
7436 gcc_unreachable ();
7437 }
7438
7439 return error_mark_node;
7440 }
7441 \f
7442 /* If VALUE is a compound expr all of whose expressions are constant, then
7443 return its value. Otherwise, return error_mark_node.
7444
7445 This is for handling COMPOUND_EXPRs as initializer elements
7446 which is allowed with a warning when -pedantic is specified. */
7447
7448 static tree
7449 valid_compound_expr_initializer (tree value, tree endtype)
7450 {
7451 if (TREE_CODE (value) == COMPOUND_EXPR)
7452 {
7453 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7454 == error_mark_node)
7455 return error_mark_node;
7456 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7457 endtype);
7458 }
7459 else if (!initializer_constant_valid_p (value, endtype))
7460 return error_mark_node;
7461 else
7462 return value;
7463 }
7464 \f
7465 /* Perform appropriate conversions on the initial value of a variable,
7466 store it in the declaration DECL,
7467 and print any error messages that are appropriate.
7468 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7469 If the init is invalid, store an ERROR_MARK.
7470
7471 INIT_LOC is the location of the initial value. */
7472
7473 void
7474 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7475 {
7476 tree value, type;
7477 bool npc = false;
7478
7479 /* If variable's type was invalidly declared, just ignore it. */
7480
7481 type = TREE_TYPE (decl);
7482 if (TREE_CODE (type) == ERROR_MARK)
7483 return;
7484
7485 /* Digest the specified initializer into an expression. */
7486
7487 if (init)
7488 npc = null_pointer_constant_p (init);
7489 value = digest_init (init_loc, type, init, origtype, npc,
7490 true, TREE_STATIC (decl));
7491
7492 /* Store the expression if valid; else report error. */
7493
7494 if (!in_system_header_at (input_location)
7495 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7496 warning (OPT_Wtraditional, "traditional C rejects automatic "
7497 "aggregate initialization");
7498
7499 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7500 DECL_INITIAL (decl) = value;
7501
7502 /* ANSI wants warnings about out-of-range constant initializers. */
7503 STRIP_TYPE_NOPS (value);
7504 if (TREE_STATIC (decl))
7505 constant_expression_warning (value);
7506
7507 /* Check if we need to set array size from compound literal size. */
7508 if (TREE_CODE (type) == ARRAY_TYPE
7509 && TYPE_DOMAIN (type) == NULL_TREE
7510 && value != error_mark_node)
7511 {
7512 tree inside_init = init;
7513
7514 STRIP_TYPE_NOPS (inside_init);
7515 inside_init = fold (inside_init);
7516
7517 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7518 {
7519 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7520
7521 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7522 {
7523 /* For int foo[] = (int [3]){1}; we need to set array size
7524 now since later on array initializer will be just the
7525 brace enclosed list of the compound literal. */
7526 tree etype = strip_array_types (TREE_TYPE (decl));
7527 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7528 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7529 layout_type (type);
7530 layout_decl (cldecl, 0);
7531 TREE_TYPE (decl)
7532 = c_build_qualified_type (type, TYPE_QUALS (etype));
7533 }
7534 }
7535 }
7536 }
7537 \f
7538 /* Methods for storing and printing names for error messages. */
7539
7540 /* Implement a spelling stack that allows components of a name to be pushed
7541 and popped. Each element on the stack is this structure. */
7542
7543 struct spelling
7544 {
7545 int kind;
7546 union
7547 {
7548 unsigned HOST_WIDE_INT i;
7549 const char *s;
7550 } u;
7551 };
7552
7553 #define SPELLING_STRING 1
7554 #define SPELLING_MEMBER 2
7555 #define SPELLING_BOUNDS 3
7556
7557 static struct spelling *spelling; /* Next stack element (unused). */
7558 static struct spelling *spelling_base; /* Spelling stack base. */
7559 static int spelling_size; /* Size of the spelling stack. */
7560
7561 /* Macros to save and restore the spelling stack around push_... functions.
7562 Alternative to SAVE_SPELLING_STACK. */
7563
7564 #define SPELLING_DEPTH() (spelling - spelling_base)
7565 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7566
7567 /* Push an element on the spelling stack with type KIND and assign VALUE
7568 to MEMBER. */
7569
7570 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7571 { \
7572 int depth = SPELLING_DEPTH (); \
7573 \
7574 if (depth >= spelling_size) \
7575 { \
7576 spelling_size += 10; \
7577 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7578 spelling_size); \
7579 RESTORE_SPELLING_DEPTH (depth); \
7580 } \
7581 \
7582 spelling->kind = (KIND); \
7583 spelling->MEMBER = (VALUE); \
7584 spelling++; \
7585 }
7586
7587 /* Push STRING on the stack. Printed literally. */
7588
7589 static void
7590 push_string (const char *string)
7591 {
7592 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7593 }
7594
7595 /* Push a member name on the stack. Printed as '.' STRING. */
7596
7597 static void
7598 push_member_name (tree decl)
7599 {
7600 const char *const string
7601 = (DECL_NAME (decl)
7602 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7603 : _("<anonymous>"));
7604 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7605 }
7606
7607 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7608
7609 static void
7610 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7611 {
7612 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7613 }
7614
7615 /* Compute the maximum size in bytes of the printed spelling. */
7616
7617 static int
7618 spelling_length (void)
7619 {
7620 int size = 0;
7621 struct spelling *p;
7622
7623 for (p = spelling_base; p < spelling; p++)
7624 {
7625 if (p->kind == SPELLING_BOUNDS)
7626 size += 25;
7627 else
7628 size += strlen (p->u.s) + 1;
7629 }
7630
7631 return size;
7632 }
7633
7634 /* Print the spelling to BUFFER and return it. */
7635
7636 static char *
7637 print_spelling (char *buffer)
7638 {
7639 char *d = buffer;
7640 struct spelling *p;
7641
7642 for (p = spelling_base; p < spelling; p++)
7643 if (p->kind == SPELLING_BOUNDS)
7644 {
7645 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7646 d += strlen (d);
7647 }
7648 else
7649 {
7650 const char *s;
7651 if (p->kind == SPELLING_MEMBER)
7652 *d++ = '.';
7653 for (s = p->u.s; (*d = *s++); d++)
7654 ;
7655 }
7656 *d++ = '\0';
7657 return buffer;
7658 }
7659
7660 /* Digest the parser output INIT as an initializer for type TYPE.
7661 Return a C expression of type TYPE to represent the initial value.
7662
7663 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7664
7665 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7666
7667 If INIT is a string constant, STRICT_STRING is true if it is
7668 unparenthesized or we should not warn here for it being parenthesized.
7669 For other types of INIT, STRICT_STRING is not used.
7670
7671 INIT_LOC is the location of the INIT.
7672
7673 REQUIRE_CONSTANT requests an error if non-constant initializers or
7674 elements are seen. */
7675
7676 static tree
7677 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7678 bool null_pointer_constant, bool strict_string,
7679 int require_constant)
7680 {
7681 enum tree_code code = TREE_CODE (type);
7682 tree inside_init = init;
7683 tree semantic_type = NULL_TREE;
7684 bool maybe_const = true;
7685
7686 if (type == error_mark_node
7687 || !init
7688 || error_operand_p (init))
7689 return error_mark_node;
7690
7691 STRIP_TYPE_NOPS (inside_init);
7692
7693 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7694 {
7695 semantic_type = TREE_TYPE (inside_init);
7696 inside_init = TREE_OPERAND (inside_init, 0);
7697 }
7698 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7699
7700 /* Initialization of an array of chars from a string constant
7701 optionally enclosed in braces. */
7702
7703 if (code == ARRAY_TYPE && inside_init
7704 && TREE_CODE (inside_init) == STRING_CST)
7705 {
7706 tree typ1
7707 = (TYPE_ATOMIC (TREE_TYPE (type))
7708 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7709 TYPE_QUAL_ATOMIC)
7710 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7711 /* Note that an array could be both an array of character type
7712 and an array of wchar_t if wchar_t is signed char or unsigned
7713 char. */
7714 bool char_array = (typ1 == char_type_node
7715 || typ1 == signed_char_type_node
7716 || typ1 == unsigned_char_type_node);
7717 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7718 bool char16_array = !!comptypes (typ1, char16_type_node);
7719 bool char32_array = !!comptypes (typ1, char32_type_node);
7720
7721 if (char_array || wchar_array || char16_array || char32_array)
7722 {
7723 struct c_expr expr;
7724 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7725 bool incompat_string_cst = false;
7726 expr.value = inside_init;
7727 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7728 expr.original_type = NULL;
7729 maybe_warn_string_init (init_loc, type, expr);
7730
7731 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7732 pedwarn_init (init_loc, OPT_Wpedantic,
7733 "initialization of a flexible array member");
7734
7735 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7736 TYPE_MAIN_VARIANT (type)))
7737 return inside_init;
7738
7739 if (char_array)
7740 {
7741 if (typ2 != char_type_node)
7742 incompat_string_cst = true;
7743 }
7744 else if (!comptypes (typ1, typ2))
7745 incompat_string_cst = true;
7746
7747 if (incompat_string_cst)
7748 {
7749 error_init (init_loc, "cannot initialize array of %qT from "
7750 "a string literal with type array of %qT",
7751 typ1, typ2);
7752 return error_mark_node;
7753 }
7754
7755 if (TYPE_DOMAIN (type) != NULL_TREE
7756 && TYPE_SIZE (type) != NULL_TREE
7757 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7758 {
7759 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7760 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
7761
7762 /* Subtract the size of a single (possibly wide) character
7763 because it's ok to ignore the terminating null char
7764 that is counted in the length of the constant. */
7765 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
7766 pedwarn_init (init_loc, 0,
7767 ("initializer-string for array of chars "
7768 "is too long"));
7769 else if (warn_cxx_compat
7770 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7771 warning_at (init_loc, OPT_Wc___compat,
7772 ("initializer-string for array chars "
7773 "is too long for C++"));
7774 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7775 {
7776 unsigned HOST_WIDE_INT size
7777 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7778 const char *p = TREE_STRING_POINTER (inside_init);
7779
7780 inside_init = build_string (size, p);
7781 }
7782 }
7783
7784 TREE_TYPE (inside_init) = type;
7785 return inside_init;
7786 }
7787 else if (INTEGRAL_TYPE_P (typ1))
7788 {
7789 error_init (init_loc, "array of inappropriate type initialized "
7790 "from string constant");
7791 return error_mark_node;
7792 }
7793 }
7794
7795 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7796 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7797 below and handle as a constructor. */
7798 if (code == VECTOR_TYPE
7799 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7800 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7801 && TREE_CONSTANT (inside_init))
7802 {
7803 if (TREE_CODE (inside_init) == VECTOR_CST
7804 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7805 TYPE_MAIN_VARIANT (type)))
7806 return inside_init;
7807
7808 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7809 {
7810 unsigned HOST_WIDE_INT ix;
7811 tree value;
7812 bool constant_p = true;
7813
7814 /* Iterate through elements and check if all constructor
7815 elements are *_CSTs. */
7816 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7817 if (!CONSTANT_CLASS_P (value))
7818 {
7819 constant_p = false;
7820 break;
7821 }
7822
7823 if (constant_p)
7824 return build_vector_from_ctor (type,
7825 CONSTRUCTOR_ELTS (inside_init));
7826 }
7827 }
7828
7829 if (warn_sequence_point)
7830 verify_sequence_points (inside_init);
7831
7832 /* Any type can be initialized
7833 from an expression of the same type, optionally with braces. */
7834
7835 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7836 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7837 TYPE_MAIN_VARIANT (type))
7838 || (code == ARRAY_TYPE
7839 && comptypes (TREE_TYPE (inside_init), type))
7840 || (code == VECTOR_TYPE
7841 && comptypes (TREE_TYPE (inside_init), type))
7842 || (code == POINTER_TYPE
7843 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7844 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7845 TREE_TYPE (type)))))
7846 {
7847 if (code == POINTER_TYPE)
7848 {
7849 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7850 {
7851 if (TREE_CODE (inside_init) == STRING_CST
7852 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7853 inside_init = array_to_pointer_conversion
7854 (init_loc, inside_init);
7855 else
7856 {
7857 error_init (init_loc, "invalid use of non-lvalue array");
7858 return error_mark_node;
7859 }
7860 }
7861 }
7862
7863 if (code == VECTOR_TYPE)
7864 /* Although the types are compatible, we may require a
7865 conversion. */
7866 inside_init = convert (type, inside_init);
7867
7868 if (require_constant
7869 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7870 {
7871 /* As an extension, allow initializing objects with static storage
7872 duration with compound literals (which are then treated just as
7873 the brace enclosed list they contain). Also allow this for
7874 vectors, as we can only assign them with compound literals. */
7875 if (flag_isoc99 && code != VECTOR_TYPE)
7876 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7877 "is not constant");
7878 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7879 inside_init = DECL_INITIAL (decl);
7880 }
7881
7882 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7883 && TREE_CODE (inside_init) != CONSTRUCTOR)
7884 {
7885 error_init (init_loc, "array initialized from non-constant array "
7886 "expression");
7887 return error_mark_node;
7888 }
7889
7890 /* Compound expressions can only occur here if -Wpedantic or
7891 -pedantic-errors is specified. In the later case, we always want
7892 an error. In the former case, we simply want a warning. */
7893 if (require_constant && pedantic
7894 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7895 {
7896 inside_init
7897 = valid_compound_expr_initializer (inside_init,
7898 TREE_TYPE (inside_init));
7899 if (inside_init == error_mark_node)
7900 error_init (init_loc, "initializer element is not constant");
7901 else
7902 pedwarn_init (init_loc, OPT_Wpedantic,
7903 "initializer element is not constant");
7904 if (flag_pedantic_errors)
7905 inside_init = error_mark_node;
7906 }
7907 else if (require_constant
7908 && !initializer_constant_valid_p (inside_init,
7909 TREE_TYPE (inside_init)))
7910 {
7911 error_init (init_loc, "initializer element is not constant");
7912 inside_init = error_mark_node;
7913 }
7914 else if (require_constant && !maybe_const)
7915 pedwarn_init (init_loc, OPT_Wpedantic,
7916 "initializer element is not a constant expression");
7917
7918 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7919 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7920 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7921 type, inside_init, origtype,
7922 ic_init, null_pointer_constant,
7923 NULL_TREE, NULL_TREE, 0);
7924 return inside_init;
7925 }
7926
7927 /* Handle scalar types, including conversions. */
7928
7929 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7930 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7931 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7932 {
7933 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7934 && (TREE_CODE (init) == STRING_CST
7935 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7936 inside_init = init = array_to_pointer_conversion (init_loc, init);
7937 if (semantic_type)
7938 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7939 inside_init);
7940 inside_init
7941 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7942 inside_init, origtype, ic_init,
7943 null_pointer_constant, NULL_TREE, NULL_TREE,
7944 0);
7945
7946 /* Check to see if we have already given an error message. */
7947 if (inside_init == error_mark_node)
7948 ;
7949 else if (require_constant && !TREE_CONSTANT (inside_init))
7950 {
7951 error_init (init_loc, "initializer element is not constant");
7952 inside_init = error_mark_node;
7953 }
7954 else if (require_constant
7955 && !initializer_constant_valid_p (inside_init,
7956 TREE_TYPE (inside_init)))
7957 {
7958 error_init (init_loc, "initializer element is not computable at "
7959 "load time");
7960 inside_init = error_mark_node;
7961 }
7962 else if (require_constant && !maybe_const)
7963 pedwarn_init (init_loc, OPT_Wpedantic,
7964 "initializer element is not a constant expression");
7965
7966 return inside_init;
7967 }
7968
7969 /* Come here only for records and arrays. */
7970
7971 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7972 {
7973 error_init (init_loc, "variable-sized object may not be initialized");
7974 return error_mark_node;
7975 }
7976
7977 error_init (init_loc, "invalid initializer");
7978 return error_mark_node;
7979 }
7980 \f
7981 /* Handle initializers that use braces. */
7982
7983 /* Type of object we are accumulating a constructor for.
7984 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7985 static tree constructor_type;
7986
7987 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7988 left to fill. */
7989 static tree constructor_fields;
7990
7991 /* For an ARRAY_TYPE, this is the specified index
7992 at which to store the next element we get. */
7993 static tree constructor_index;
7994
7995 /* For an ARRAY_TYPE, this is the maximum index. */
7996 static tree constructor_max_index;
7997
7998 /* For a RECORD_TYPE, this is the first field not yet written out. */
7999 static tree constructor_unfilled_fields;
8000
8001 /* For an ARRAY_TYPE, this is the index of the first element
8002 not yet written out. */
8003 static tree constructor_unfilled_index;
8004
8005 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8006 This is so we can generate gaps between fields, when appropriate. */
8007 static tree constructor_bit_index;
8008
8009 /* If we are saving up the elements rather than allocating them,
8010 this is the list of elements so far (in reverse order,
8011 most recent first). */
8012 static vec<constructor_elt, va_gc> *constructor_elements;
8013
8014 /* 1 if constructor should be incrementally stored into a constructor chain,
8015 0 if all the elements should be kept in AVL tree. */
8016 static int constructor_incremental;
8017
8018 /* 1 if so far this constructor's elements are all compile-time constants. */
8019 static int constructor_constant;
8020
8021 /* 1 if so far this constructor's elements are all valid address constants. */
8022 static int constructor_simple;
8023
8024 /* 1 if this constructor has an element that cannot be part of a
8025 constant expression. */
8026 static int constructor_nonconst;
8027
8028 /* 1 if this constructor is erroneous so far. */
8029 static int constructor_erroneous;
8030
8031 /* 1 if this constructor is the universal zero initializer { 0 }. */
8032 static int constructor_zeroinit;
8033
8034 /* Structure for managing pending initializer elements, organized as an
8035 AVL tree. */
8036
8037 struct init_node
8038 {
8039 struct init_node *left, *right;
8040 struct init_node *parent;
8041 int balance;
8042 tree purpose;
8043 tree value;
8044 tree origtype;
8045 };
8046
8047 /* Tree of pending elements at this constructor level.
8048 These are elements encountered out of order
8049 which belong at places we haven't reached yet in actually
8050 writing the output.
8051 Will never hold tree nodes across GC runs. */
8052 static struct init_node *constructor_pending_elts;
8053
8054 /* The SPELLING_DEPTH of this constructor. */
8055 static int constructor_depth;
8056
8057 /* DECL node for which an initializer is being read.
8058 0 means we are reading a constructor expression
8059 such as (struct foo) {...}. */
8060 static tree constructor_decl;
8061
8062 /* Nonzero if this is an initializer for a top-level decl. */
8063 static int constructor_top_level;
8064
8065 /* Nonzero if there were any member designators in this initializer. */
8066 static int constructor_designated;
8067
8068 /* Nesting depth of designator list. */
8069 static int designator_depth;
8070
8071 /* Nonzero if there were diagnosed errors in this designator list. */
8072 static int designator_erroneous;
8073
8074 \f
8075 /* This stack has a level for each implicit or explicit level of
8076 structuring in the initializer, including the outermost one. It
8077 saves the values of most of the variables above. */
8078
8079 struct constructor_range_stack;
8080
8081 struct constructor_stack
8082 {
8083 struct constructor_stack *next;
8084 tree type;
8085 tree fields;
8086 tree index;
8087 tree max_index;
8088 tree unfilled_index;
8089 tree unfilled_fields;
8090 tree bit_index;
8091 vec<constructor_elt, va_gc> *elements;
8092 struct init_node *pending_elts;
8093 int offset;
8094 int depth;
8095 /* If value nonzero, this value should replace the entire
8096 constructor at this level. */
8097 struct c_expr replacement_value;
8098 struct constructor_range_stack *range_stack;
8099 char constant;
8100 char simple;
8101 char nonconst;
8102 char implicit;
8103 char erroneous;
8104 char outer;
8105 char incremental;
8106 char designated;
8107 int designator_depth;
8108 };
8109
8110 static struct constructor_stack *constructor_stack;
8111
8112 /* This stack represents designators from some range designator up to
8113 the last designator in the list. */
8114
8115 struct constructor_range_stack
8116 {
8117 struct constructor_range_stack *next, *prev;
8118 struct constructor_stack *stack;
8119 tree range_start;
8120 tree index;
8121 tree range_end;
8122 tree fields;
8123 };
8124
8125 static struct constructor_range_stack *constructor_range_stack;
8126
8127 /* This stack records separate initializers that are nested.
8128 Nested initializers can't happen in ANSI C, but GNU C allows them
8129 in cases like { ... (struct foo) { ... } ... }. */
8130
8131 struct initializer_stack
8132 {
8133 struct initializer_stack *next;
8134 tree decl;
8135 struct constructor_stack *constructor_stack;
8136 struct constructor_range_stack *constructor_range_stack;
8137 vec<constructor_elt, va_gc> *elements;
8138 struct spelling *spelling;
8139 struct spelling *spelling_base;
8140 int spelling_size;
8141 char top_level;
8142 char require_constant_value;
8143 char require_constant_elements;
8144 rich_location *missing_brace_richloc;
8145 };
8146
8147 static struct initializer_stack *initializer_stack;
8148 \f
8149 /* Prepare to parse and output the initializer for variable DECL. */
8150
8151 void
8152 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8153 rich_location *richloc)
8154 {
8155 const char *locus;
8156 struct initializer_stack *p = XNEW (struct initializer_stack);
8157
8158 p->decl = constructor_decl;
8159 p->require_constant_value = require_constant_value;
8160 p->require_constant_elements = require_constant_elements;
8161 p->constructor_stack = constructor_stack;
8162 p->constructor_range_stack = constructor_range_stack;
8163 p->elements = constructor_elements;
8164 p->spelling = spelling;
8165 p->spelling_base = spelling_base;
8166 p->spelling_size = spelling_size;
8167 p->top_level = constructor_top_level;
8168 p->next = initializer_stack;
8169 p->missing_brace_richloc = richloc;
8170 initializer_stack = p;
8171
8172 constructor_decl = decl;
8173 constructor_designated = 0;
8174 constructor_top_level = top_level;
8175
8176 if (decl != NULL_TREE && decl != error_mark_node)
8177 {
8178 require_constant_value = TREE_STATIC (decl);
8179 require_constant_elements
8180 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8181 /* For a scalar, you can always use any value to initialize,
8182 even within braces. */
8183 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8184 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8185 }
8186 else
8187 {
8188 require_constant_value = 0;
8189 require_constant_elements = 0;
8190 locus = _("(anonymous)");
8191 }
8192
8193 constructor_stack = 0;
8194 constructor_range_stack = 0;
8195
8196 found_missing_braces = 0;
8197
8198 spelling_base = 0;
8199 spelling_size = 0;
8200 RESTORE_SPELLING_DEPTH (0);
8201
8202 if (locus)
8203 push_string (locus);
8204 }
8205
8206 void
8207 finish_init (void)
8208 {
8209 struct initializer_stack *p = initializer_stack;
8210
8211 /* Free the whole constructor stack of this initializer. */
8212 while (constructor_stack)
8213 {
8214 struct constructor_stack *q = constructor_stack;
8215 constructor_stack = q->next;
8216 free (q);
8217 }
8218
8219 gcc_assert (!constructor_range_stack);
8220
8221 /* Pop back to the data of the outer initializer (if any). */
8222 free (spelling_base);
8223
8224 constructor_decl = p->decl;
8225 require_constant_value = p->require_constant_value;
8226 require_constant_elements = p->require_constant_elements;
8227 constructor_stack = p->constructor_stack;
8228 constructor_range_stack = p->constructor_range_stack;
8229 constructor_elements = p->elements;
8230 spelling = p->spelling;
8231 spelling_base = p->spelling_base;
8232 spelling_size = p->spelling_size;
8233 constructor_top_level = p->top_level;
8234 initializer_stack = p->next;
8235 free (p);
8236 }
8237 \f
8238 /* Call here when we see the initializer is surrounded by braces.
8239 This is instead of a call to push_init_level;
8240 it is matched by a call to pop_init_level.
8241
8242 TYPE is the type to initialize, for a constructor expression.
8243 For an initializer for a decl, TYPE is zero. */
8244
8245 void
8246 really_start_incremental_init (tree type)
8247 {
8248 struct constructor_stack *p = XNEW (struct constructor_stack);
8249
8250 if (type == NULL_TREE)
8251 type = TREE_TYPE (constructor_decl);
8252
8253 if (VECTOR_TYPE_P (type)
8254 && TYPE_VECTOR_OPAQUE (type))
8255 error ("opaque vector types cannot be initialized");
8256
8257 p->type = constructor_type;
8258 p->fields = constructor_fields;
8259 p->index = constructor_index;
8260 p->max_index = constructor_max_index;
8261 p->unfilled_index = constructor_unfilled_index;
8262 p->unfilled_fields = constructor_unfilled_fields;
8263 p->bit_index = constructor_bit_index;
8264 p->elements = constructor_elements;
8265 p->constant = constructor_constant;
8266 p->simple = constructor_simple;
8267 p->nonconst = constructor_nonconst;
8268 p->erroneous = constructor_erroneous;
8269 p->pending_elts = constructor_pending_elts;
8270 p->depth = constructor_depth;
8271 p->replacement_value.value = 0;
8272 p->replacement_value.original_code = ERROR_MARK;
8273 p->replacement_value.original_type = NULL;
8274 p->implicit = 0;
8275 p->range_stack = 0;
8276 p->outer = 0;
8277 p->incremental = constructor_incremental;
8278 p->designated = constructor_designated;
8279 p->designator_depth = designator_depth;
8280 p->next = 0;
8281 constructor_stack = p;
8282
8283 constructor_constant = 1;
8284 constructor_simple = 1;
8285 constructor_nonconst = 0;
8286 constructor_depth = SPELLING_DEPTH ();
8287 constructor_elements = NULL;
8288 constructor_pending_elts = 0;
8289 constructor_type = type;
8290 constructor_incremental = 1;
8291 constructor_designated = 0;
8292 constructor_zeroinit = 1;
8293 designator_depth = 0;
8294 designator_erroneous = 0;
8295
8296 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8297 {
8298 constructor_fields = TYPE_FIELDS (constructor_type);
8299 /* Skip any nameless bit fields at the beginning. */
8300 while (constructor_fields != NULL_TREE
8301 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8302 constructor_fields = DECL_CHAIN (constructor_fields);
8303
8304 constructor_unfilled_fields = constructor_fields;
8305 constructor_bit_index = bitsize_zero_node;
8306 }
8307 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8308 {
8309 if (TYPE_DOMAIN (constructor_type))
8310 {
8311 constructor_max_index
8312 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8313
8314 /* Detect non-empty initializations of zero-length arrays. */
8315 if (constructor_max_index == NULL_TREE
8316 && TYPE_SIZE (constructor_type))
8317 constructor_max_index = integer_minus_one_node;
8318
8319 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8320 to initialize VLAs will cause a proper error; avoid tree
8321 checking errors as well by setting a safe value. */
8322 if (constructor_max_index
8323 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8324 constructor_max_index = integer_minus_one_node;
8325
8326 constructor_index
8327 = convert (bitsizetype,
8328 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8329 }
8330 else
8331 {
8332 constructor_index = bitsize_zero_node;
8333 constructor_max_index = NULL_TREE;
8334 }
8335
8336 constructor_unfilled_index = constructor_index;
8337 }
8338 else if (VECTOR_TYPE_P (constructor_type))
8339 {
8340 /* Vectors are like simple fixed-size arrays. */
8341 constructor_max_index =
8342 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8343 constructor_index = bitsize_zero_node;
8344 constructor_unfilled_index = constructor_index;
8345 }
8346 else
8347 {
8348 /* Handle the case of int x = {5}; */
8349 constructor_fields = constructor_type;
8350 constructor_unfilled_fields = constructor_type;
8351 }
8352 }
8353 \f
8354 extern location_t last_init_list_comma;
8355
8356 /* Called when we see an open brace for a nested initializer. Finish
8357 off any pending levels with implicit braces. */
8358 void
8359 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8360 {
8361 while (constructor_stack->implicit)
8362 {
8363 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8364 && constructor_fields == NULL_TREE)
8365 process_init_element (input_location,
8366 pop_init_level (loc, 1, braced_init_obstack,
8367 last_init_list_comma),
8368 true, braced_init_obstack);
8369 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8370 && constructor_max_index
8371 && tree_int_cst_lt (constructor_max_index,
8372 constructor_index))
8373 process_init_element (input_location,
8374 pop_init_level (loc, 1, braced_init_obstack,
8375 last_init_list_comma),
8376 true, braced_init_obstack);
8377 else
8378 break;
8379 }
8380 }
8381
8382 /* Push down into a subobject, for initialization.
8383 If this is for an explicit set of braces, IMPLICIT is 0.
8384 If it is because the next element belongs at a lower level,
8385 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8386
8387 void
8388 push_init_level (location_t loc, int implicit,
8389 struct obstack *braced_init_obstack)
8390 {
8391 struct constructor_stack *p;
8392 tree value = NULL_TREE;
8393
8394 /* Unless this is an explicit brace, we need to preserve previous
8395 content if any. */
8396 if (implicit)
8397 {
8398 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8399 value = find_init_member (constructor_fields, braced_init_obstack);
8400 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8401 value = find_init_member (constructor_index, braced_init_obstack);
8402 }
8403
8404 p = XNEW (struct constructor_stack);
8405 p->type = constructor_type;
8406 p->fields = constructor_fields;
8407 p->index = constructor_index;
8408 p->max_index = constructor_max_index;
8409 p->unfilled_index = constructor_unfilled_index;
8410 p->unfilled_fields = constructor_unfilled_fields;
8411 p->bit_index = constructor_bit_index;
8412 p->elements = constructor_elements;
8413 p->constant = constructor_constant;
8414 p->simple = constructor_simple;
8415 p->nonconst = constructor_nonconst;
8416 p->erroneous = constructor_erroneous;
8417 p->pending_elts = constructor_pending_elts;
8418 p->depth = constructor_depth;
8419 p->replacement_value.value = NULL_TREE;
8420 p->replacement_value.original_code = ERROR_MARK;
8421 p->replacement_value.original_type = NULL;
8422 p->implicit = implicit;
8423 p->outer = 0;
8424 p->incremental = constructor_incremental;
8425 p->designated = constructor_designated;
8426 p->designator_depth = designator_depth;
8427 p->next = constructor_stack;
8428 p->range_stack = 0;
8429 constructor_stack = p;
8430
8431 constructor_constant = 1;
8432 constructor_simple = 1;
8433 constructor_nonconst = 0;
8434 constructor_depth = SPELLING_DEPTH ();
8435 constructor_elements = NULL;
8436 constructor_incremental = 1;
8437 constructor_designated = 0;
8438 constructor_pending_elts = 0;
8439 if (!implicit)
8440 {
8441 p->range_stack = constructor_range_stack;
8442 constructor_range_stack = 0;
8443 designator_depth = 0;
8444 designator_erroneous = 0;
8445 }
8446
8447 /* Don't die if an entire brace-pair level is superfluous
8448 in the containing level. */
8449 if (constructor_type == NULL_TREE)
8450 ;
8451 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8452 {
8453 /* Don't die if there are extra init elts at the end. */
8454 if (constructor_fields == NULL_TREE)
8455 constructor_type = NULL_TREE;
8456 else
8457 {
8458 constructor_type = TREE_TYPE (constructor_fields);
8459 push_member_name (constructor_fields);
8460 constructor_depth++;
8461 }
8462 /* If upper initializer is designated, then mark this as
8463 designated too to prevent bogus warnings. */
8464 constructor_designated = p->designated;
8465 }
8466 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8467 {
8468 constructor_type = TREE_TYPE (constructor_type);
8469 push_array_bounds (tree_to_uhwi (constructor_index));
8470 constructor_depth++;
8471 }
8472
8473 if (constructor_type == NULL_TREE)
8474 {
8475 error_init (loc, "extra brace group at end of initializer");
8476 constructor_fields = NULL_TREE;
8477 constructor_unfilled_fields = NULL_TREE;
8478 return;
8479 }
8480
8481 if (value && TREE_CODE (value) == CONSTRUCTOR)
8482 {
8483 constructor_constant = TREE_CONSTANT (value);
8484 constructor_simple = TREE_STATIC (value);
8485 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8486 constructor_elements = CONSTRUCTOR_ELTS (value);
8487 if (!vec_safe_is_empty (constructor_elements)
8488 && (TREE_CODE (constructor_type) == RECORD_TYPE
8489 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8490 set_nonincremental_init (braced_init_obstack);
8491 }
8492
8493 if (implicit == 1)
8494 {
8495 found_missing_braces = 1;
8496 if (initializer_stack->missing_brace_richloc)
8497 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8498 (loc, "{");
8499 }
8500
8501 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8502 {
8503 constructor_fields = TYPE_FIELDS (constructor_type);
8504 /* Skip any nameless bit fields at the beginning. */
8505 while (constructor_fields != NULL_TREE
8506 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8507 constructor_fields = DECL_CHAIN (constructor_fields);
8508
8509 constructor_unfilled_fields = constructor_fields;
8510 constructor_bit_index = bitsize_zero_node;
8511 }
8512 else if (VECTOR_TYPE_P (constructor_type))
8513 {
8514 /* Vectors are like simple fixed-size arrays. */
8515 constructor_max_index =
8516 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8517 constructor_index = bitsize_int (0);
8518 constructor_unfilled_index = constructor_index;
8519 }
8520 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8521 {
8522 if (TYPE_DOMAIN (constructor_type))
8523 {
8524 constructor_max_index
8525 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8526
8527 /* Detect non-empty initializations of zero-length arrays. */
8528 if (constructor_max_index == NULL_TREE
8529 && TYPE_SIZE (constructor_type))
8530 constructor_max_index = integer_minus_one_node;
8531
8532 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8533 to initialize VLAs will cause a proper error; avoid tree
8534 checking errors as well by setting a safe value. */
8535 if (constructor_max_index
8536 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8537 constructor_max_index = integer_minus_one_node;
8538
8539 constructor_index
8540 = convert (bitsizetype,
8541 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8542 }
8543 else
8544 constructor_index = bitsize_zero_node;
8545
8546 constructor_unfilled_index = constructor_index;
8547 if (value && TREE_CODE (value) == STRING_CST)
8548 {
8549 /* We need to split the char/wchar array into individual
8550 characters, so that we don't have to special case it
8551 everywhere. */
8552 set_nonincremental_init_from_string (value, braced_init_obstack);
8553 }
8554 }
8555 else
8556 {
8557 if (constructor_type != error_mark_node)
8558 warning_init (input_location, 0, "braces around scalar initializer");
8559 constructor_fields = constructor_type;
8560 constructor_unfilled_fields = constructor_type;
8561 }
8562 }
8563
8564 /* At the end of an implicit or explicit brace level,
8565 finish up that level of constructor. If a single expression
8566 with redundant braces initialized that level, return the
8567 c_expr structure for that expression. Otherwise, the original_code
8568 element is set to ERROR_MARK.
8569 If we were outputting the elements as they are read, return 0 as the value
8570 from inner levels (process_init_element ignores that),
8571 but return error_mark_node as the value from the outermost level
8572 (that's what we want to put in DECL_INITIAL).
8573 Otherwise, return a CONSTRUCTOR expression as the value. */
8574
8575 struct c_expr
8576 pop_init_level (location_t loc, int implicit,
8577 struct obstack *braced_init_obstack,
8578 location_t insert_before)
8579 {
8580 struct constructor_stack *p;
8581 struct c_expr ret;
8582 ret.value = NULL_TREE;
8583 ret.original_code = ERROR_MARK;
8584 ret.original_type = NULL;
8585
8586 if (implicit == 0)
8587 {
8588 /* When we come to an explicit close brace,
8589 pop any inner levels that didn't have explicit braces. */
8590 while (constructor_stack->implicit)
8591 process_init_element (input_location,
8592 pop_init_level (loc, 1, braced_init_obstack,
8593 insert_before),
8594 true, braced_init_obstack);
8595 gcc_assert (!constructor_range_stack);
8596 }
8597 else
8598 if (initializer_stack->missing_brace_richloc)
8599 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8600 (insert_before, "}");
8601
8602 /* Now output all pending elements. */
8603 constructor_incremental = 1;
8604 output_pending_init_elements (1, braced_init_obstack);
8605
8606 p = constructor_stack;
8607
8608 /* Error for initializing a flexible array member, or a zero-length
8609 array member in an inappropriate context. */
8610 if (constructor_type && constructor_fields
8611 && TREE_CODE (constructor_type) == ARRAY_TYPE
8612 && TYPE_DOMAIN (constructor_type)
8613 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8614 {
8615 /* Silently discard empty initializations. The parser will
8616 already have pedwarned for empty brackets. */
8617 if (integer_zerop (constructor_unfilled_index))
8618 constructor_type = NULL_TREE;
8619 else
8620 {
8621 gcc_assert (!TYPE_SIZE (constructor_type));
8622
8623 if (constructor_depth > 2)
8624 error_init (loc, "initialization of flexible array member in a nested context");
8625 else
8626 pedwarn_init (loc, OPT_Wpedantic,
8627 "initialization of a flexible array member");
8628
8629 /* We have already issued an error message for the existence
8630 of a flexible array member not at the end of the structure.
8631 Discard the initializer so that we do not die later. */
8632 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8633 constructor_type = NULL_TREE;
8634 }
8635 }
8636
8637 switch (vec_safe_length (constructor_elements))
8638 {
8639 case 0:
8640 /* Initialization with { } counts as zeroinit. */
8641 constructor_zeroinit = 1;
8642 break;
8643 case 1:
8644 /* This might be zeroinit as well. */
8645 if (integer_zerop ((*constructor_elements)[0].value))
8646 constructor_zeroinit = 1;
8647 break;
8648 default:
8649 /* If the constructor has more than one element, it can't be { 0 }. */
8650 constructor_zeroinit = 0;
8651 break;
8652 }
8653
8654 /* Warn when some structs are initialized with direct aggregation. */
8655 if (!implicit && found_missing_braces && warn_missing_braces
8656 && !constructor_zeroinit)
8657 {
8658 gcc_assert (initializer_stack->missing_brace_richloc);
8659 warning_at (initializer_stack->missing_brace_richloc,
8660 OPT_Wmissing_braces,
8661 "missing braces around initializer");
8662 }
8663
8664 /* Warn when some struct elements are implicitly initialized to zero. */
8665 if (warn_missing_field_initializers
8666 && constructor_type
8667 && TREE_CODE (constructor_type) == RECORD_TYPE
8668 && constructor_unfilled_fields)
8669 {
8670 /* Do not warn for flexible array members or zero-length arrays. */
8671 while (constructor_unfilled_fields
8672 && (!DECL_SIZE (constructor_unfilled_fields)
8673 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8674 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8675
8676 if (constructor_unfilled_fields
8677 /* Do not warn if this level of the initializer uses member
8678 designators; it is likely to be deliberate. */
8679 && !constructor_designated
8680 /* Do not warn about initializing with { 0 } or with { }. */
8681 && !constructor_zeroinit)
8682 {
8683 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8684 "missing initializer for field %qD of %qT",
8685 constructor_unfilled_fields,
8686 constructor_type))
8687 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8688 "%qD declared here", constructor_unfilled_fields);
8689 }
8690 }
8691
8692 /* Pad out the end of the structure. */
8693 if (p->replacement_value.value)
8694 /* If this closes a superfluous brace pair,
8695 just pass out the element between them. */
8696 ret = p->replacement_value;
8697 else if (constructor_type == NULL_TREE)
8698 ;
8699 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8700 && TREE_CODE (constructor_type) != ARRAY_TYPE
8701 && !VECTOR_TYPE_P (constructor_type))
8702 {
8703 /* A nonincremental scalar initializer--just return
8704 the element, after verifying there is just one. */
8705 if (vec_safe_is_empty (constructor_elements))
8706 {
8707 if (!constructor_erroneous)
8708 error_init (loc, "empty scalar initializer");
8709 ret.value = error_mark_node;
8710 }
8711 else if (vec_safe_length (constructor_elements) != 1)
8712 {
8713 error_init (loc, "extra elements in scalar initializer");
8714 ret.value = (*constructor_elements)[0].value;
8715 }
8716 else
8717 ret.value = (*constructor_elements)[0].value;
8718 }
8719 else
8720 {
8721 if (constructor_erroneous)
8722 ret.value = error_mark_node;
8723 else
8724 {
8725 ret.value = build_constructor (constructor_type,
8726 constructor_elements);
8727 if (constructor_constant)
8728 TREE_CONSTANT (ret.value) = 1;
8729 if (constructor_constant && constructor_simple)
8730 TREE_STATIC (ret.value) = 1;
8731 if (constructor_nonconst)
8732 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8733 }
8734 }
8735
8736 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8737 {
8738 if (constructor_nonconst)
8739 ret.original_code = C_MAYBE_CONST_EXPR;
8740 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8741 ret.original_code = ERROR_MARK;
8742 }
8743
8744 constructor_type = p->type;
8745 constructor_fields = p->fields;
8746 constructor_index = p->index;
8747 constructor_max_index = p->max_index;
8748 constructor_unfilled_index = p->unfilled_index;
8749 constructor_unfilled_fields = p->unfilled_fields;
8750 constructor_bit_index = p->bit_index;
8751 constructor_elements = p->elements;
8752 constructor_constant = p->constant;
8753 constructor_simple = p->simple;
8754 constructor_nonconst = p->nonconst;
8755 constructor_erroneous = p->erroneous;
8756 constructor_incremental = p->incremental;
8757 constructor_designated = p->designated;
8758 designator_depth = p->designator_depth;
8759 constructor_pending_elts = p->pending_elts;
8760 constructor_depth = p->depth;
8761 if (!p->implicit)
8762 constructor_range_stack = p->range_stack;
8763 RESTORE_SPELLING_DEPTH (constructor_depth);
8764
8765 constructor_stack = p->next;
8766 free (p);
8767
8768 if (ret.value == NULL_TREE && constructor_stack == 0)
8769 ret.value = error_mark_node;
8770 return ret;
8771 }
8772
8773 /* Common handling for both array range and field name designators.
8774 ARRAY argument is nonzero for array ranges. Returns false for success. */
8775
8776 static bool
8777 set_designator (location_t loc, bool array,
8778 struct obstack *braced_init_obstack)
8779 {
8780 tree subtype;
8781 enum tree_code subcode;
8782
8783 /* Don't die if an entire brace-pair level is superfluous
8784 in the containing level. */
8785 if (constructor_type == NULL_TREE)
8786 return true;
8787
8788 /* If there were errors in this designator list already, bail out
8789 silently. */
8790 if (designator_erroneous)
8791 return true;
8792
8793 if (!designator_depth)
8794 {
8795 gcc_assert (!constructor_range_stack);
8796
8797 /* Designator list starts at the level of closest explicit
8798 braces. */
8799 while (constructor_stack->implicit)
8800 process_init_element (input_location,
8801 pop_init_level (loc, 1, braced_init_obstack,
8802 last_init_list_comma),
8803 true, braced_init_obstack);
8804 constructor_designated = 1;
8805 return false;
8806 }
8807
8808 switch (TREE_CODE (constructor_type))
8809 {
8810 case RECORD_TYPE:
8811 case UNION_TYPE:
8812 subtype = TREE_TYPE (constructor_fields);
8813 if (subtype != error_mark_node)
8814 subtype = TYPE_MAIN_VARIANT (subtype);
8815 break;
8816 case ARRAY_TYPE:
8817 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8818 break;
8819 default:
8820 gcc_unreachable ();
8821 }
8822
8823 subcode = TREE_CODE (subtype);
8824 if (array && subcode != ARRAY_TYPE)
8825 {
8826 error_init (loc, "array index in non-array initializer");
8827 return true;
8828 }
8829 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8830 {
8831 error_init (loc, "field name not in record or union initializer");
8832 return true;
8833 }
8834
8835 constructor_designated = 1;
8836 finish_implicit_inits (loc, braced_init_obstack);
8837 push_init_level (loc, 2, braced_init_obstack);
8838 return false;
8839 }
8840
8841 /* If there are range designators in designator list, push a new designator
8842 to constructor_range_stack. RANGE_END is end of such stack range or
8843 NULL_TREE if there is no range designator at this level. */
8844
8845 static void
8846 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8847 {
8848 struct constructor_range_stack *p;
8849
8850 p = (struct constructor_range_stack *)
8851 obstack_alloc (braced_init_obstack,
8852 sizeof (struct constructor_range_stack));
8853 p->prev = constructor_range_stack;
8854 p->next = 0;
8855 p->fields = constructor_fields;
8856 p->range_start = constructor_index;
8857 p->index = constructor_index;
8858 p->stack = constructor_stack;
8859 p->range_end = range_end;
8860 if (constructor_range_stack)
8861 constructor_range_stack->next = p;
8862 constructor_range_stack = p;
8863 }
8864
8865 /* Within an array initializer, specify the next index to be initialized.
8866 FIRST is that index. If LAST is nonzero, then initialize a range
8867 of indices, running from FIRST through LAST. */
8868
8869 void
8870 set_init_index (location_t loc, tree first, tree last,
8871 struct obstack *braced_init_obstack)
8872 {
8873 if (set_designator (loc, true, braced_init_obstack))
8874 return;
8875
8876 designator_erroneous = 1;
8877
8878 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8879 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8880 {
8881 error_init (loc, "array index in initializer not of integer type");
8882 return;
8883 }
8884
8885 if (TREE_CODE (first) != INTEGER_CST)
8886 {
8887 first = c_fully_fold (first, false, NULL);
8888 if (TREE_CODE (first) == INTEGER_CST)
8889 pedwarn_init (loc, OPT_Wpedantic,
8890 "array index in initializer is not "
8891 "an integer constant expression");
8892 }
8893
8894 if (last && TREE_CODE (last) != INTEGER_CST)
8895 {
8896 last = c_fully_fold (last, false, NULL);
8897 if (TREE_CODE (last) == INTEGER_CST)
8898 pedwarn_init (loc, OPT_Wpedantic,
8899 "array index in initializer is not "
8900 "an integer constant expression");
8901 }
8902
8903 if (TREE_CODE (first) != INTEGER_CST)
8904 error_init (loc, "nonconstant array index in initializer");
8905 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8906 error_init (loc, "nonconstant array index in initializer");
8907 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8908 error_init (loc, "array index in non-array initializer");
8909 else if (tree_int_cst_sgn (first) == -1)
8910 error_init (loc, "array index in initializer exceeds array bounds");
8911 else if (constructor_max_index
8912 && tree_int_cst_lt (constructor_max_index, first))
8913 error_init (loc, "array index in initializer exceeds array bounds");
8914 else
8915 {
8916 constant_expression_warning (first);
8917 if (last)
8918 constant_expression_warning (last);
8919 constructor_index = convert (bitsizetype, first);
8920 if (tree_int_cst_lt (constructor_index, first))
8921 {
8922 constructor_index = copy_node (constructor_index);
8923 TREE_OVERFLOW (constructor_index) = 1;
8924 }
8925
8926 if (last)
8927 {
8928 if (tree_int_cst_equal (first, last))
8929 last = NULL_TREE;
8930 else if (tree_int_cst_lt (last, first))
8931 {
8932 error_init (loc, "empty index range in initializer");
8933 last = NULL_TREE;
8934 }
8935 else
8936 {
8937 last = convert (bitsizetype, last);
8938 if (constructor_max_index != NULL_TREE
8939 && tree_int_cst_lt (constructor_max_index, last))
8940 {
8941 error_init (loc, "array index range in initializer exceeds "
8942 "array bounds");
8943 last = NULL_TREE;
8944 }
8945 }
8946 }
8947
8948 designator_depth++;
8949 designator_erroneous = 0;
8950 if (constructor_range_stack || last)
8951 push_range_stack (last, braced_init_obstack);
8952 }
8953 }
8954
8955 /* Within a struct initializer, specify the next field to be initialized. */
8956
8957 void
8958 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8959 struct obstack *braced_init_obstack)
8960 {
8961 tree field;
8962
8963 if (set_designator (loc, false, braced_init_obstack))
8964 return;
8965
8966 designator_erroneous = 1;
8967
8968 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8969 {
8970 error_init (loc, "field name not in record or union initializer");
8971 return;
8972 }
8973
8974 field = lookup_field (constructor_type, fieldname);
8975
8976 if (field == NULL_TREE)
8977 {
8978 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8979 if (guessed_id)
8980 {
8981 gcc_rich_location rich_loc (fieldname_loc);
8982 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8983 error_at (&rich_loc,
8984 "%qT has no member named %qE; did you mean %qE?",
8985 constructor_type, fieldname, guessed_id);
8986 }
8987 else
8988 error_at (fieldname_loc, "%qT has no member named %qE",
8989 constructor_type, fieldname);
8990 }
8991 else
8992 do
8993 {
8994 constructor_fields = TREE_VALUE (field);
8995 designator_depth++;
8996 designator_erroneous = 0;
8997 if (constructor_range_stack)
8998 push_range_stack (NULL_TREE, braced_init_obstack);
8999 field = TREE_CHAIN (field);
9000 if (field)
9001 {
9002 if (set_designator (loc, false, braced_init_obstack))
9003 return;
9004 }
9005 }
9006 while (field != NULL_TREE);
9007 }
9008 \f
9009 /* Add a new initializer to the tree of pending initializers. PURPOSE
9010 identifies the initializer, either array index or field in a structure.
9011 VALUE is the value of that index or field. If ORIGTYPE is not
9012 NULL_TREE, it is the original type of VALUE.
9013
9014 IMPLICIT is true if value comes from pop_init_level (1),
9015 the new initializer has been merged with the existing one
9016 and thus no warnings should be emitted about overriding an
9017 existing initializer. */
9018
9019 static void
9020 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9021 bool implicit, struct obstack *braced_init_obstack)
9022 {
9023 struct init_node *p, **q, *r;
9024
9025 q = &constructor_pending_elts;
9026 p = 0;
9027
9028 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9029 {
9030 while (*q != 0)
9031 {
9032 p = *q;
9033 if (tree_int_cst_lt (purpose, p->purpose))
9034 q = &p->left;
9035 else if (tree_int_cst_lt (p->purpose, purpose))
9036 q = &p->right;
9037 else
9038 {
9039 if (!implicit)
9040 {
9041 if (TREE_SIDE_EFFECTS (p->value))
9042 warning_init (loc, OPT_Woverride_init_side_effects,
9043 "initialized field with side-effects "
9044 "overwritten");
9045 else if (warn_override_init)
9046 warning_init (loc, OPT_Woverride_init,
9047 "initialized field overwritten");
9048 }
9049 p->value = value;
9050 p->origtype = origtype;
9051 return;
9052 }
9053 }
9054 }
9055 else
9056 {
9057 tree bitpos;
9058
9059 bitpos = bit_position (purpose);
9060 while (*q != NULL)
9061 {
9062 p = *q;
9063 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9064 q = &p->left;
9065 else if (p->purpose != purpose)
9066 q = &p->right;
9067 else
9068 {
9069 if (!implicit)
9070 {
9071 if (TREE_SIDE_EFFECTS (p->value))
9072 warning_init (loc, OPT_Woverride_init_side_effects,
9073 "initialized field with side-effects "
9074 "overwritten");
9075 else if (warn_override_init)
9076 warning_init (loc, OPT_Woverride_init,
9077 "initialized field overwritten");
9078 }
9079 p->value = value;
9080 p->origtype = origtype;
9081 return;
9082 }
9083 }
9084 }
9085
9086 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9087 sizeof (struct init_node));
9088 r->purpose = purpose;
9089 r->value = value;
9090 r->origtype = origtype;
9091
9092 *q = r;
9093 r->parent = p;
9094 r->left = 0;
9095 r->right = 0;
9096 r->balance = 0;
9097
9098 while (p)
9099 {
9100 struct init_node *s;
9101
9102 if (r == p->left)
9103 {
9104 if (p->balance == 0)
9105 p->balance = -1;
9106 else if (p->balance < 0)
9107 {
9108 if (r->balance < 0)
9109 {
9110 /* L rotation. */
9111 p->left = r->right;
9112 if (p->left)
9113 p->left->parent = p;
9114 r->right = p;
9115
9116 p->balance = 0;
9117 r->balance = 0;
9118
9119 s = p->parent;
9120 p->parent = r;
9121 r->parent = s;
9122 if (s)
9123 {
9124 if (s->left == p)
9125 s->left = r;
9126 else
9127 s->right = r;
9128 }
9129 else
9130 constructor_pending_elts = r;
9131 }
9132 else
9133 {
9134 /* LR rotation. */
9135 struct init_node *t = r->right;
9136
9137 r->right = t->left;
9138 if (r->right)
9139 r->right->parent = r;
9140 t->left = r;
9141
9142 p->left = t->right;
9143 if (p->left)
9144 p->left->parent = p;
9145 t->right = p;
9146
9147 p->balance = t->balance < 0;
9148 r->balance = -(t->balance > 0);
9149 t->balance = 0;
9150
9151 s = p->parent;
9152 p->parent = t;
9153 r->parent = t;
9154 t->parent = s;
9155 if (s)
9156 {
9157 if (s->left == p)
9158 s->left = t;
9159 else
9160 s->right = t;
9161 }
9162 else
9163 constructor_pending_elts = t;
9164 }
9165 break;
9166 }
9167 else
9168 {
9169 /* p->balance == +1; growth of left side balances the node. */
9170 p->balance = 0;
9171 break;
9172 }
9173 }
9174 else /* r == p->right */
9175 {
9176 if (p->balance == 0)
9177 /* Growth propagation from right side. */
9178 p->balance++;
9179 else if (p->balance > 0)
9180 {
9181 if (r->balance > 0)
9182 {
9183 /* R rotation. */
9184 p->right = r->left;
9185 if (p->right)
9186 p->right->parent = p;
9187 r->left = p;
9188
9189 p->balance = 0;
9190 r->balance = 0;
9191
9192 s = p->parent;
9193 p->parent = r;
9194 r->parent = s;
9195 if (s)
9196 {
9197 if (s->left == p)
9198 s->left = r;
9199 else
9200 s->right = r;
9201 }
9202 else
9203 constructor_pending_elts = r;
9204 }
9205 else /* r->balance == -1 */
9206 {
9207 /* RL rotation */
9208 struct init_node *t = r->left;
9209
9210 r->left = t->right;
9211 if (r->left)
9212 r->left->parent = r;
9213 t->right = r;
9214
9215 p->right = t->left;
9216 if (p->right)
9217 p->right->parent = p;
9218 t->left = p;
9219
9220 r->balance = (t->balance < 0);
9221 p->balance = -(t->balance > 0);
9222 t->balance = 0;
9223
9224 s = p->parent;
9225 p->parent = t;
9226 r->parent = t;
9227 t->parent = s;
9228 if (s)
9229 {
9230 if (s->left == p)
9231 s->left = t;
9232 else
9233 s->right = t;
9234 }
9235 else
9236 constructor_pending_elts = t;
9237 }
9238 break;
9239 }
9240 else
9241 {
9242 /* p->balance == -1; growth of right side balances the node. */
9243 p->balance = 0;
9244 break;
9245 }
9246 }
9247
9248 r = p;
9249 p = p->parent;
9250 }
9251 }
9252
9253 /* Build AVL tree from a sorted chain. */
9254
9255 static void
9256 set_nonincremental_init (struct obstack * braced_init_obstack)
9257 {
9258 unsigned HOST_WIDE_INT ix;
9259 tree index, value;
9260
9261 if (TREE_CODE (constructor_type) != RECORD_TYPE
9262 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9263 return;
9264
9265 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9266 add_pending_init (input_location, index, value, NULL_TREE, true,
9267 braced_init_obstack);
9268 constructor_elements = NULL;
9269 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9270 {
9271 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9272 /* Skip any nameless bit fields at the beginning. */
9273 while (constructor_unfilled_fields != NULL_TREE
9274 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9275 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9276
9277 }
9278 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9279 {
9280 if (TYPE_DOMAIN (constructor_type))
9281 constructor_unfilled_index
9282 = convert (bitsizetype,
9283 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9284 else
9285 constructor_unfilled_index = bitsize_zero_node;
9286 }
9287 constructor_incremental = 0;
9288 }
9289
9290 /* Build AVL tree from a string constant. */
9291
9292 static void
9293 set_nonincremental_init_from_string (tree str,
9294 struct obstack * braced_init_obstack)
9295 {
9296 tree value, purpose, type;
9297 HOST_WIDE_INT val[2];
9298 const char *p, *end;
9299 int byte, wchar_bytes, charwidth, bitpos;
9300
9301 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9302
9303 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9304 charwidth = TYPE_PRECISION (char_type_node);
9305 gcc_assert ((size_t) wchar_bytes * charwidth
9306 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9307 type = TREE_TYPE (constructor_type);
9308 p = TREE_STRING_POINTER (str);
9309 end = p + TREE_STRING_LENGTH (str);
9310
9311 for (purpose = bitsize_zero_node;
9312 p < end
9313 && !(constructor_max_index
9314 && tree_int_cst_lt (constructor_max_index, purpose));
9315 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9316 {
9317 if (wchar_bytes == 1)
9318 {
9319 val[0] = (unsigned char) *p++;
9320 val[1] = 0;
9321 }
9322 else
9323 {
9324 val[1] = 0;
9325 val[0] = 0;
9326 for (byte = 0; byte < wchar_bytes; byte++)
9327 {
9328 if (BYTES_BIG_ENDIAN)
9329 bitpos = (wchar_bytes - byte - 1) * charwidth;
9330 else
9331 bitpos = byte * charwidth;
9332 val[bitpos / HOST_BITS_PER_WIDE_INT]
9333 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9334 << (bitpos % HOST_BITS_PER_WIDE_INT);
9335 }
9336 }
9337
9338 if (!TYPE_UNSIGNED (type))
9339 {
9340 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9341 if (bitpos < HOST_BITS_PER_WIDE_INT)
9342 {
9343 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9344 {
9345 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9346 val[1] = -1;
9347 }
9348 }
9349 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9350 {
9351 if (val[0] < 0)
9352 val[1] = -1;
9353 }
9354 else if (val[1] & (HOST_WIDE_INT_1
9355 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9356 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9357 }
9358
9359 value = wide_int_to_tree (type,
9360 wide_int::from_array (val, 2,
9361 HOST_BITS_PER_WIDE_INT * 2));
9362 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9363 braced_init_obstack);
9364 }
9365
9366 constructor_incremental = 0;
9367 }
9368
9369 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9370 not initialized yet. */
9371
9372 static tree
9373 find_init_member (tree field, struct obstack * braced_init_obstack)
9374 {
9375 struct init_node *p;
9376
9377 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9378 {
9379 if (constructor_incremental
9380 && tree_int_cst_lt (field, constructor_unfilled_index))
9381 set_nonincremental_init (braced_init_obstack);
9382
9383 p = constructor_pending_elts;
9384 while (p)
9385 {
9386 if (tree_int_cst_lt (field, p->purpose))
9387 p = p->left;
9388 else if (tree_int_cst_lt (p->purpose, field))
9389 p = p->right;
9390 else
9391 return p->value;
9392 }
9393 }
9394 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9395 {
9396 tree bitpos = bit_position (field);
9397
9398 if (constructor_incremental
9399 && (!constructor_unfilled_fields
9400 || tree_int_cst_lt (bitpos,
9401 bit_position (constructor_unfilled_fields))))
9402 set_nonincremental_init (braced_init_obstack);
9403
9404 p = constructor_pending_elts;
9405 while (p)
9406 {
9407 if (field == p->purpose)
9408 return p->value;
9409 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9410 p = p->left;
9411 else
9412 p = p->right;
9413 }
9414 }
9415 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9416 {
9417 if (!vec_safe_is_empty (constructor_elements)
9418 && (constructor_elements->last ().index == field))
9419 return constructor_elements->last ().value;
9420 }
9421 return NULL_TREE;
9422 }
9423
9424 /* "Output" the next constructor element.
9425 At top level, really output it to assembler code now.
9426 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9427 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9428 TYPE is the data type that the containing data type wants here.
9429 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9430 If VALUE is a string constant, STRICT_STRING is true if it is
9431 unparenthesized or we should not warn here for it being parenthesized.
9432 For other types of VALUE, STRICT_STRING is not used.
9433
9434 PENDING if true means output pending elements that belong
9435 right after this element. (PENDING is normally true;
9436 it is false while outputting pending elements, to avoid recursion.)
9437
9438 IMPLICIT is true if value comes from pop_init_level (1),
9439 the new initializer has been merged with the existing one
9440 and thus no warnings should be emitted about overriding an
9441 existing initializer. */
9442
9443 static void
9444 output_init_element (location_t loc, tree value, tree origtype,
9445 bool strict_string, tree type, tree field, bool pending,
9446 bool implicit, struct obstack * braced_init_obstack)
9447 {
9448 tree semantic_type = NULL_TREE;
9449 bool maybe_const = true;
9450 bool npc;
9451
9452 if (type == error_mark_node || value == error_mark_node)
9453 {
9454 constructor_erroneous = 1;
9455 return;
9456 }
9457 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9458 && (TREE_CODE (value) == STRING_CST
9459 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9460 && !(TREE_CODE (value) == STRING_CST
9461 && TREE_CODE (type) == ARRAY_TYPE
9462 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9463 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9464 TYPE_MAIN_VARIANT (type)))
9465 value = array_to_pointer_conversion (input_location, value);
9466
9467 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9468 && require_constant_value && pending)
9469 {
9470 /* As an extension, allow initializing objects with static storage
9471 duration with compound literals (which are then treated just as
9472 the brace enclosed list they contain). */
9473 if (flag_isoc99)
9474 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9475 "constant");
9476 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9477 value = DECL_INITIAL (decl);
9478 }
9479
9480 npc = null_pointer_constant_p (value);
9481 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9482 {
9483 semantic_type = TREE_TYPE (value);
9484 value = TREE_OPERAND (value, 0);
9485 }
9486 value = c_fully_fold (value, require_constant_value, &maybe_const);
9487
9488 if (value == error_mark_node)
9489 constructor_erroneous = 1;
9490 else if (!TREE_CONSTANT (value))
9491 constructor_constant = 0;
9492 else if (!initializer_constant_valid_p (value,
9493 TREE_TYPE (value),
9494 AGGREGATE_TYPE_P (constructor_type)
9495 && TYPE_REVERSE_STORAGE_ORDER
9496 (constructor_type))
9497 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9498 && DECL_C_BIT_FIELD (field)
9499 && TREE_CODE (value) != INTEGER_CST))
9500 constructor_simple = 0;
9501 if (!maybe_const)
9502 constructor_nonconst = 1;
9503
9504 /* Digest the initializer and issue any errors about incompatible
9505 types before issuing errors about non-constant initializers. */
9506 tree new_value = value;
9507 if (semantic_type)
9508 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9509 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9510 require_constant_value);
9511 if (new_value == error_mark_node)
9512 {
9513 constructor_erroneous = 1;
9514 return;
9515 }
9516 if (require_constant_value || require_constant_elements)
9517 constant_expression_warning (new_value);
9518
9519 /* Proceed to check the constness of the original initializer. */
9520 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9521 {
9522 if (require_constant_value)
9523 {
9524 error_init (loc, "initializer element is not constant");
9525 value = error_mark_node;
9526 }
9527 else if (require_constant_elements)
9528 pedwarn (loc, OPT_Wpedantic,
9529 "initializer element is not computable at load time");
9530 }
9531 else if (!maybe_const
9532 && (require_constant_value || require_constant_elements))
9533 pedwarn_init (loc, OPT_Wpedantic,
9534 "initializer element is not a constant expression");
9535
9536 /* Issue -Wc++-compat warnings about initializing a bitfield with
9537 enum type. */
9538 if (warn_cxx_compat
9539 && field != NULL_TREE
9540 && TREE_CODE (field) == FIELD_DECL
9541 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9542 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9543 != TYPE_MAIN_VARIANT (type))
9544 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9545 {
9546 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9547 if (checktype != error_mark_node
9548 && (TYPE_MAIN_VARIANT (checktype)
9549 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9550 warning_init (loc, OPT_Wc___compat,
9551 "enum conversion in initialization is invalid in C++");
9552 }
9553
9554 /* If this field is empty and does not have side effects (and is not at
9555 the end of structure), don't do anything other than checking the
9556 initializer. */
9557 if (field
9558 && (TREE_TYPE (field) == error_mark_node
9559 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9560 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9561 && !TREE_SIDE_EFFECTS (new_value)
9562 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9563 || DECL_CHAIN (field)))))
9564 return;
9565
9566 /* Finally, set VALUE to the initializer value digested above. */
9567 value = new_value;
9568
9569 /* If this element doesn't come next in sequence,
9570 put it on constructor_pending_elts. */
9571 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9572 && (!constructor_incremental
9573 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9574 {
9575 if (constructor_incremental
9576 && tree_int_cst_lt (field, constructor_unfilled_index))
9577 set_nonincremental_init (braced_init_obstack);
9578
9579 add_pending_init (loc, field, value, origtype, implicit,
9580 braced_init_obstack);
9581 return;
9582 }
9583 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9584 && (!constructor_incremental
9585 || field != constructor_unfilled_fields))
9586 {
9587 /* We do this for records but not for unions. In a union,
9588 no matter which field is specified, it can be initialized
9589 right away since it starts at the beginning of the union. */
9590 if (constructor_incremental)
9591 {
9592 if (!constructor_unfilled_fields)
9593 set_nonincremental_init (braced_init_obstack);
9594 else
9595 {
9596 tree bitpos, unfillpos;
9597
9598 bitpos = bit_position (field);
9599 unfillpos = bit_position (constructor_unfilled_fields);
9600
9601 if (tree_int_cst_lt (bitpos, unfillpos))
9602 set_nonincremental_init (braced_init_obstack);
9603 }
9604 }
9605
9606 add_pending_init (loc, field, value, origtype, implicit,
9607 braced_init_obstack);
9608 return;
9609 }
9610 else if (TREE_CODE (constructor_type) == UNION_TYPE
9611 && !vec_safe_is_empty (constructor_elements))
9612 {
9613 if (!implicit)
9614 {
9615 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9616 warning_init (loc, OPT_Woverride_init_side_effects,
9617 "initialized field with side-effects overwritten");
9618 else if (warn_override_init)
9619 warning_init (loc, OPT_Woverride_init,
9620 "initialized field overwritten");
9621 }
9622
9623 /* We can have just one union field set. */
9624 constructor_elements = NULL;
9625 }
9626
9627 /* Otherwise, output this element either to
9628 constructor_elements or to the assembler file. */
9629
9630 constructor_elt celt = {field, value};
9631 vec_safe_push (constructor_elements, celt);
9632
9633 /* Advance the variable that indicates sequential elements output. */
9634 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9635 constructor_unfilled_index
9636 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9637 bitsize_one_node);
9638 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9639 {
9640 constructor_unfilled_fields
9641 = DECL_CHAIN (constructor_unfilled_fields);
9642
9643 /* Skip any nameless bit fields. */
9644 while (constructor_unfilled_fields != NULL_TREE
9645 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9646 constructor_unfilled_fields =
9647 DECL_CHAIN (constructor_unfilled_fields);
9648 }
9649 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9650 constructor_unfilled_fields = NULL_TREE;
9651
9652 /* Now output any pending elements which have become next. */
9653 if (pending)
9654 output_pending_init_elements (0, braced_init_obstack);
9655 }
9656
9657 /* For two FIELD_DECLs in the same chain, return -1 if field1
9658 comes before field2, 1 if field1 comes after field2 and
9659 0 if field1 == field2. */
9660
9661 static int
9662 init_field_decl_cmp (tree field1, tree field2)
9663 {
9664 if (field1 == field2)
9665 return 0;
9666
9667 tree bitpos1 = bit_position (field1);
9668 tree bitpos2 = bit_position (field2);
9669 if (tree_int_cst_equal (bitpos1, bitpos2))
9670 {
9671 /* If one of the fields has non-zero bitsize, then that
9672 field must be the last one in a sequence of zero
9673 sized fields, fields after it will have bigger
9674 bit_position. */
9675 if (TREE_TYPE (field1) != error_mark_node
9676 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9677 && integer_nonzerop (TREE_TYPE (field1)))
9678 return 1;
9679 if (TREE_TYPE (field2) != error_mark_node
9680 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9681 && integer_nonzerop (TREE_TYPE (field2)))
9682 return -1;
9683 /* Otherwise, fallback to DECL_CHAIN walk to find out
9684 which field comes earlier. Walk chains of both
9685 fields, so that if field1 and field2 are close to each
9686 other in either order, it is found soon even for large
9687 sequences of zero sized fields. */
9688 tree f1 = field1, f2 = field2;
9689 while (1)
9690 {
9691 f1 = DECL_CHAIN (f1);
9692 f2 = DECL_CHAIN (f2);
9693 if (f1 == NULL_TREE)
9694 {
9695 gcc_assert (f2);
9696 return 1;
9697 }
9698 if (f2 == NULL_TREE)
9699 return -1;
9700 if (f1 == field2)
9701 return -1;
9702 if (f2 == field1)
9703 return 1;
9704 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9705 return 1;
9706 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9707 return -1;
9708 }
9709 }
9710 else if (tree_int_cst_lt (bitpos1, bitpos2))
9711 return -1;
9712 else
9713 return 1;
9714 }
9715
9716 /* Output any pending elements which have become next.
9717 As we output elements, constructor_unfilled_{fields,index}
9718 advances, which may cause other elements to become next;
9719 if so, they too are output.
9720
9721 If ALL is 0, we return when there are
9722 no more pending elements to output now.
9723
9724 If ALL is 1, we output space as necessary so that
9725 we can output all the pending elements. */
9726 static void
9727 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9728 {
9729 struct init_node *elt = constructor_pending_elts;
9730 tree next;
9731
9732 retry:
9733
9734 /* Look through the whole pending tree.
9735 If we find an element that should be output now,
9736 output it. Otherwise, set NEXT to the element
9737 that comes first among those still pending. */
9738
9739 next = NULL_TREE;
9740 while (elt)
9741 {
9742 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9743 {
9744 if (tree_int_cst_equal (elt->purpose,
9745 constructor_unfilled_index))
9746 output_init_element (input_location, elt->value, elt->origtype,
9747 true, TREE_TYPE (constructor_type),
9748 constructor_unfilled_index, false, false,
9749 braced_init_obstack);
9750 else if (tree_int_cst_lt (constructor_unfilled_index,
9751 elt->purpose))
9752 {
9753 /* Advance to the next smaller node. */
9754 if (elt->left)
9755 elt = elt->left;
9756 else
9757 {
9758 /* We have reached the smallest node bigger than the
9759 current unfilled index. Fill the space first. */
9760 next = elt->purpose;
9761 break;
9762 }
9763 }
9764 else
9765 {
9766 /* Advance to the next bigger node. */
9767 if (elt->right)
9768 elt = elt->right;
9769 else
9770 {
9771 /* We have reached the biggest node in a subtree. Find
9772 the parent of it, which is the next bigger node. */
9773 while (elt->parent && elt->parent->right == elt)
9774 elt = elt->parent;
9775 elt = elt->parent;
9776 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9777 elt->purpose))
9778 {
9779 next = elt->purpose;
9780 break;
9781 }
9782 }
9783 }
9784 }
9785 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9786 {
9787 /* If the current record is complete we are done. */
9788 if (constructor_unfilled_fields == NULL_TREE)
9789 break;
9790
9791 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9792 elt->purpose);
9793 if (cmp == 0)
9794 output_init_element (input_location, elt->value, elt->origtype,
9795 true, TREE_TYPE (elt->purpose),
9796 elt->purpose, false, false,
9797 braced_init_obstack);
9798 else if (cmp < 0)
9799 {
9800 /* Advance to the next smaller node. */
9801 if (elt->left)
9802 elt = elt->left;
9803 else
9804 {
9805 /* We have reached the smallest node bigger than the
9806 current unfilled field. Fill the space first. */
9807 next = elt->purpose;
9808 break;
9809 }
9810 }
9811 else
9812 {
9813 /* Advance to the next bigger node. */
9814 if (elt->right)
9815 elt = elt->right;
9816 else
9817 {
9818 /* We have reached the biggest node in a subtree. Find
9819 the parent of it, which is the next bigger node. */
9820 while (elt->parent && elt->parent->right == elt)
9821 elt = elt->parent;
9822 elt = elt->parent;
9823 if (elt
9824 && init_field_decl_cmp (constructor_unfilled_fields,
9825 elt->purpose) < 0)
9826 {
9827 next = elt->purpose;
9828 break;
9829 }
9830 }
9831 }
9832 }
9833 }
9834
9835 /* Ordinarily return, but not if we want to output all
9836 and there are elements left. */
9837 if (!(all && next != NULL_TREE))
9838 return;
9839
9840 /* If it's not incremental, just skip over the gap, so that after
9841 jumping to retry we will output the next successive element. */
9842 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9843 constructor_unfilled_fields = next;
9844 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9845 constructor_unfilled_index = next;
9846
9847 /* ELT now points to the node in the pending tree with the next
9848 initializer to output. */
9849 goto retry;
9850 }
9851 \f
9852 /* Add one non-braced element to the current constructor level.
9853 This adjusts the current position within the constructor's type.
9854 This may also start or terminate implicit levels
9855 to handle a partly-braced initializer.
9856
9857 Once this has found the correct level for the new element,
9858 it calls output_init_element.
9859
9860 IMPLICIT is true if value comes from pop_init_level (1),
9861 the new initializer has been merged with the existing one
9862 and thus no warnings should be emitted about overriding an
9863 existing initializer. */
9864
9865 void
9866 process_init_element (location_t loc, struct c_expr value, bool implicit,
9867 struct obstack * braced_init_obstack)
9868 {
9869 tree orig_value = value.value;
9870 int string_flag
9871 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9872 bool strict_string = value.original_code == STRING_CST;
9873 bool was_designated = designator_depth != 0;
9874
9875 designator_depth = 0;
9876 designator_erroneous = 0;
9877
9878 if (!implicit && value.value && !integer_zerop (value.value))
9879 constructor_zeroinit = 0;
9880
9881 /* Handle superfluous braces around string cst as in
9882 char x[] = {"foo"}; */
9883 if (string_flag
9884 && constructor_type
9885 && !was_designated
9886 && TREE_CODE (constructor_type) == ARRAY_TYPE
9887 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9888 && integer_zerop (constructor_unfilled_index))
9889 {
9890 if (constructor_stack->replacement_value.value)
9891 error_init (loc, "excess elements in char array initializer");
9892 constructor_stack->replacement_value = value;
9893 return;
9894 }
9895
9896 if (constructor_stack->replacement_value.value != NULL_TREE)
9897 {
9898 error_init (loc, "excess elements in struct initializer");
9899 return;
9900 }
9901
9902 /* Ignore elements of a brace group if it is entirely superfluous
9903 and has already been diagnosed. */
9904 if (constructor_type == NULL_TREE)
9905 return;
9906
9907 if (!implicit && warn_designated_init && !was_designated
9908 && TREE_CODE (constructor_type) == RECORD_TYPE
9909 && lookup_attribute ("designated_init",
9910 TYPE_ATTRIBUTES (constructor_type)))
9911 warning_init (loc,
9912 OPT_Wdesignated_init,
9913 "positional initialization of field "
9914 "in %<struct%> declared with %<designated_init%> attribute");
9915
9916 /* If we've exhausted any levels that didn't have braces,
9917 pop them now. */
9918 while (constructor_stack->implicit)
9919 {
9920 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9921 && constructor_fields == NULL_TREE)
9922 process_init_element (loc,
9923 pop_init_level (loc, 1, braced_init_obstack,
9924 last_init_list_comma),
9925 true, braced_init_obstack);
9926 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9927 || VECTOR_TYPE_P (constructor_type))
9928 && constructor_max_index
9929 && tree_int_cst_lt (constructor_max_index,
9930 constructor_index))
9931 process_init_element (loc,
9932 pop_init_level (loc, 1, braced_init_obstack,
9933 last_init_list_comma),
9934 true, braced_init_obstack);
9935 else
9936 break;
9937 }
9938
9939 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9940 if (constructor_range_stack)
9941 {
9942 /* If value is a compound literal and we'll be just using its
9943 content, don't put it into a SAVE_EXPR. */
9944 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9945 || !require_constant_value)
9946 {
9947 tree semantic_type = NULL_TREE;
9948 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9949 {
9950 semantic_type = TREE_TYPE (value.value);
9951 value.value = TREE_OPERAND (value.value, 0);
9952 }
9953 value.value = save_expr (value.value);
9954 if (semantic_type)
9955 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9956 value.value);
9957 }
9958 }
9959
9960 while (1)
9961 {
9962 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9963 {
9964 tree fieldtype;
9965 enum tree_code fieldcode;
9966
9967 if (constructor_fields == NULL_TREE)
9968 {
9969 pedwarn_init (loc, 0, "excess elements in struct initializer");
9970 break;
9971 }
9972
9973 fieldtype = TREE_TYPE (constructor_fields);
9974 if (fieldtype != error_mark_node)
9975 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9976 fieldcode = TREE_CODE (fieldtype);
9977
9978 /* Error for non-static initialization of a flexible array member. */
9979 if (fieldcode == ARRAY_TYPE
9980 && !require_constant_value
9981 && TYPE_SIZE (fieldtype) == NULL_TREE
9982 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9983 {
9984 error_init (loc, "non-static initialization of a flexible "
9985 "array member");
9986 break;
9987 }
9988
9989 /* Error for initialization of a flexible array member with
9990 a string constant if the structure is in an array. E.g.:
9991 struct S { int x; char y[]; };
9992 struct S s[] = { { 1, "foo" } };
9993 is invalid. */
9994 if (string_flag
9995 && fieldcode == ARRAY_TYPE
9996 && constructor_depth > 1
9997 && TYPE_SIZE (fieldtype) == NULL_TREE
9998 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9999 {
10000 bool in_array_p = false;
10001 for (struct constructor_stack *p = constructor_stack;
10002 p && p->type; p = p->next)
10003 if (TREE_CODE (p->type) == ARRAY_TYPE)
10004 {
10005 in_array_p = true;
10006 break;
10007 }
10008 if (in_array_p)
10009 {
10010 error_init (loc, "initialization of flexible array "
10011 "member in a nested context");
10012 break;
10013 }
10014 }
10015
10016 /* Accept a string constant to initialize a subarray. */
10017 if (value.value != NULL_TREE
10018 && fieldcode == ARRAY_TYPE
10019 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10020 && string_flag)
10021 value.value = orig_value;
10022 /* Otherwise, if we have come to a subaggregate,
10023 and we don't have an element of its type, push into it. */
10024 else if (value.value != NULL_TREE
10025 && value.value != error_mark_node
10026 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
10027 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
10028 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
10029 {
10030 push_init_level (loc, 1, braced_init_obstack);
10031 continue;
10032 }
10033
10034 if (value.value)
10035 {
10036 push_member_name (constructor_fields);
10037 output_init_element (loc, value.value, value.original_type,
10038 strict_string, fieldtype,
10039 constructor_fields, true, implicit,
10040 braced_init_obstack);
10041 RESTORE_SPELLING_DEPTH (constructor_depth);
10042 }
10043 else
10044 /* Do the bookkeeping for an element that was
10045 directly output as a constructor. */
10046 {
10047 /* For a record, keep track of end position of last field. */
10048 if (DECL_SIZE (constructor_fields))
10049 constructor_bit_index
10050 = size_binop_loc (input_location, PLUS_EXPR,
10051 bit_position (constructor_fields),
10052 DECL_SIZE (constructor_fields));
10053
10054 /* If the current field was the first one not yet written out,
10055 it isn't now, so update. */
10056 if (constructor_unfilled_fields == constructor_fields)
10057 {
10058 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10059 /* Skip any nameless bit fields. */
10060 while (constructor_unfilled_fields != 0
10061 && (DECL_UNNAMED_BIT_FIELD
10062 (constructor_unfilled_fields)))
10063 constructor_unfilled_fields =
10064 DECL_CHAIN (constructor_unfilled_fields);
10065 }
10066 }
10067
10068 constructor_fields = DECL_CHAIN (constructor_fields);
10069 /* Skip any nameless bit fields at the beginning. */
10070 while (constructor_fields != NULL_TREE
10071 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10072 constructor_fields = DECL_CHAIN (constructor_fields);
10073 }
10074 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10075 {
10076 tree fieldtype;
10077 enum tree_code fieldcode;
10078
10079 if (constructor_fields == NULL_TREE)
10080 {
10081 pedwarn_init (loc, 0,
10082 "excess elements in union initializer");
10083 break;
10084 }
10085
10086 fieldtype = TREE_TYPE (constructor_fields);
10087 if (fieldtype != error_mark_node)
10088 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10089 fieldcode = TREE_CODE (fieldtype);
10090
10091 /* Warn that traditional C rejects initialization of unions.
10092 We skip the warning if the value is zero. This is done
10093 under the assumption that the zero initializer in user
10094 code appears conditioned on e.g. __STDC__ to avoid
10095 "missing initializer" warnings and relies on default
10096 initialization to zero in the traditional C case.
10097 We also skip the warning if the initializer is designated,
10098 again on the assumption that this must be conditional on
10099 __STDC__ anyway (and we've already complained about the
10100 member-designator already). */
10101 if (!in_system_header_at (input_location) && !constructor_designated
10102 && !(value.value && (integer_zerop (value.value)
10103 || real_zerop (value.value))))
10104 warning (OPT_Wtraditional, "traditional C rejects initialization "
10105 "of unions");
10106
10107 /* Accept a string constant to initialize a subarray. */
10108 if (value.value != NULL_TREE
10109 && fieldcode == ARRAY_TYPE
10110 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10111 && string_flag)
10112 value.value = orig_value;
10113 /* Otherwise, if we have come to a subaggregate,
10114 and we don't have an element of its type, push into it. */
10115 else if (value.value != NULL_TREE
10116 && value.value != error_mark_node
10117 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
10118 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
10119 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
10120 {
10121 push_init_level (loc, 1, braced_init_obstack);
10122 continue;
10123 }
10124
10125 if (value.value)
10126 {
10127 push_member_name (constructor_fields);
10128 output_init_element (loc, value.value, value.original_type,
10129 strict_string, fieldtype,
10130 constructor_fields, true, implicit,
10131 braced_init_obstack);
10132 RESTORE_SPELLING_DEPTH (constructor_depth);
10133 }
10134 else
10135 /* Do the bookkeeping for an element that was
10136 directly output as a constructor. */
10137 {
10138 constructor_bit_index = DECL_SIZE (constructor_fields);
10139 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10140 }
10141
10142 constructor_fields = NULL_TREE;
10143 }
10144 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10145 {
10146 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10147 enum tree_code eltcode = TREE_CODE (elttype);
10148
10149 /* Accept a string constant to initialize a subarray. */
10150 if (value.value != NULL_TREE
10151 && eltcode == ARRAY_TYPE
10152 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10153 && string_flag)
10154 value.value = orig_value;
10155 /* Otherwise, if we have come to a subaggregate,
10156 and we don't have an element of its type, push into it. */
10157 else if (value.value != NULL_TREE
10158 && value.value != error_mark_node
10159 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
10160 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
10161 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
10162 {
10163 push_init_level (loc, 1, braced_init_obstack);
10164 continue;
10165 }
10166
10167 if (constructor_max_index != NULL_TREE
10168 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10169 || integer_all_onesp (constructor_max_index)))
10170 {
10171 pedwarn_init (loc, 0,
10172 "excess elements in array initializer");
10173 break;
10174 }
10175
10176 /* Now output the actual element. */
10177 if (value.value)
10178 {
10179 push_array_bounds (tree_to_uhwi (constructor_index));
10180 output_init_element (loc, value.value, value.original_type,
10181 strict_string, elttype,
10182 constructor_index, true, implicit,
10183 braced_init_obstack);
10184 RESTORE_SPELLING_DEPTH (constructor_depth);
10185 }
10186
10187 constructor_index
10188 = size_binop_loc (input_location, PLUS_EXPR,
10189 constructor_index, bitsize_one_node);
10190
10191 if (!value.value)
10192 /* If we are doing the bookkeeping for an element that was
10193 directly output as a constructor, we must update
10194 constructor_unfilled_index. */
10195 constructor_unfilled_index = constructor_index;
10196 }
10197 else if (VECTOR_TYPE_P (constructor_type))
10198 {
10199 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10200
10201 /* Do a basic check of initializer size. Note that vectors
10202 always have a fixed size derived from their type. */
10203 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10204 {
10205 pedwarn_init (loc, 0,
10206 "excess elements in vector initializer");
10207 break;
10208 }
10209
10210 /* Now output the actual element. */
10211 if (value.value)
10212 {
10213 if (TREE_CODE (value.value) == VECTOR_CST)
10214 elttype = TYPE_MAIN_VARIANT (constructor_type);
10215 output_init_element (loc, value.value, value.original_type,
10216 strict_string, elttype,
10217 constructor_index, true, implicit,
10218 braced_init_obstack);
10219 }
10220
10221 constructor_index
10222 = size_binop_loc (input_location,
10223 PLUS_EXPR, constructor_index, bitsize_one_node);
10224
10225 if (!value.value)
10226 /* If we are doing the bookkeeping for an element that was
10227 directly output as a constructor, we must update
10228 constructor_unfilled_index. */
10229 constructor_unfilled_index = constructor_index;
10230 }
10231
10232 /* Handle the sole element allowed in a braced initializer
10233 for a scalar variable. */
10234 else if (constructor_type != error_mark_node
10235 && constructor_fields == NULL_TREE)
10236 {
10237 pedwarn_init (loc, 0,
10238 "excess elements in scalar initializer");
10239 break;
10240 }
10241 else
10242 {
10243 if (value.value)
10244 output_init_element (loc, value.value, value.original_type,
10245 strict_string, constructor_type,
10246 NULL_TREE, true, implicit,
10247 braced_init_obstack);
10248 constructor_fields = NULL_TREE;
10249 }
10250
10251 /* Handle range initializers either at this level or anywhere higher
10252 in the designator stack. */
10253 if (constructor_range_stack)
10254 {
10255 struct constructor_range_stack *p, *range_stack;
10256 int finish = 0;
10257
10258 range_stack = constructor_range_stack;
10259 constructor_range_stack = 0;
10260 while (constructor_stack != range_stack->stack)
10261 {
10262 gcc_assert (constructor_stack->implicit);
10263 process_init_element (loc,
10264 pop_init_level (loc, 1,
10265 braced_init_obstack,
10266 last_init_list_comma),
10267 true, braced_init_obstack);
10268 }
10269 for (p = range_stack;
10270 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10271 p = p->prev)
10272 {
10273 gcc_assert (constructor_stack->implicit);
10274 process_init_element (loc,
10275 pop_init_level (loc, 1,
10276 braced_init_obstack,
10277 last_init_list_comma),
10278 true, braced_init_obstack);
10279 }
10280
10281 p->index = size_binop_loc (input_location,
10282 PLUS_EXPR, p->index, bitsize_one_node);
10283 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10284 finish = 1;
10285
10286 while (1)
10287 {
10288 constructor_index = p->index;
10289 constructor_fields = p->fields;
10290 if (finish && p->range_end && p->index == p->range_start)
10291 {
10292 finish = 0;
10293 p->prev = 0;
10294 }
10295 p = p->next;
10296 if (!p)
10297 break;
10298 finish_implicit_inits (loc, braced_init_obstack);
10299 push_init_level (loc, 2, braced_init_obstack);
10300 p->stack = constructor_stack;
10301 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10302 p->index = p->range_start;
10303 }
10304
10305 if (!finish)
10306 constructor_range_stack = range_stack;
10307 continue;
10308 }
10309
10310 break;
10311 }
10312
10313 constructor_range_stack = 0;
10314 }
10315 \f
10316 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10317 (guaranteed to be 'volatile' or null) and ARGS (represented using
10318 an ASM_EXPR node). */
10319 tree
10320 build_asm_stmt (bool is_volatile, tree args)
10321 {
10322 if (is_volatile)
10323 ASM_VOLATILE_P (args) = 1;
10324 return add_stmt (args);
10325 }
10326
10327 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10328 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10329 SIMPLE indicates whether there was anything at all after the
10330 string in the asm expression -- asm("blah") and asm("blah" : )
10331 are subtly different. We use a ASM_EXPR node to represent this.
10332 LOC is the location of the asm, and IS_INLINE says whether this
10333 is asm inline. */
10334 tree
10335 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10336 tree clobbers, tree labels, bool simple, bool is_inline)
10337 {
10338 tree tail;
10339 tree args;
10340 int i;
10341 const char *constraint;
10342 const char **oconstraints;
10343 bool allows_mem, allows_reg, is_inout;
10344 int ninputs, noutputs;
10345
10346 ninputs = list_length (inputs);
10347 noutputs = list_length (outputs);
10348 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10349
10350 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10351
10352 /* Remove output conversions that change the type but not the mode. */
10353 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10354 {
10355 tree output = TREE_VALUE (tail);
10356
10357 output = c_fully_fold (output, false, NULL, true);
10358
10359 /* ??? Really, this should not be here. Users should be using a
10360 proper lvalue, dammit. But there's a long history of using casts
10361 in the output operands. In cases like longlong.h, this becomes a
10362 primitive form of typechecking -- if the cast can be removed, then
10363 the output operand had a type of the proper width; otherwise we'll
10364 get an error. Gross, but ... */
10365 STRIP_NOPS (output);
10366
10367 if (!lvalue_or_else (loc, output, lv_asm))
10368 output = error_mark_node;
10369
10370 if (output != error_mark_node
10371 && (TREE_READONLY (output)
10372 || TYPE_READONLY (TREE_TYPE (output))
10373 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10374 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10375 readonly_error (loc, output, lv_asm);
10376
10377 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10378 oconstraints[i] = constraint;
10379
10380 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10381 &allows_mem, &allows_reg, &is_inout))
10382 {
10383 /* If the operand is going to end up in memory,
10384 mark it addressable. */
10385 if (!allows_reg && !c_mark_addressable (output))
10386 output = error_mark_node;
10387 if (!(!allows_reg && allows_mem)
10388 && output != error_mark_node
10389 && VOID_TYPE_P (TREE_TYPE (output)))
10390 {
10391 error_at (loc, "invalid use of void expression");
10392 output = error_mark_node;
10393 }
10394 }
10395 else
10396 output = error_mark_node;
10397
10398 TREE_VALUE (tail) = output;
10399 }
10400
10401 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10402 {
10403 tree input;
10404
10405 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10406 input = TREE_VALUE (tail);
10407
10408 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10409 oconstraints, &allows_mem, &allows_reg))
10410 {
10411 /* If the operand is going to end up in memory,
10412 mark it addressable. */
10413 if (!allows_reg && allows_mem)
10414 {
10415 input = c_fully_fold (input, false, NULL, true);
10416
10417 /* Strip the nops as we allow this case. FIXME, this really
10418 should be rejected or made deprecated. */
10419 STRIP_NOPS (input);
10420 if (!c_mark_addressable (input))
10421 input = error_mark_node;
10422 }
10423 else
10424 {
10425 struct c_expr expr;
10426 memset (&expr, 0, sizeof (expr));
10427 expr.value = input;
10428 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10429 input = c_fully_fold (expr.value, false, NULL);
10430
10431 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10432 {
10433 error_at (loc, "invalid use of void expression");
10434 input = error_mark_node;
10435 }
10436 }
10437 }
10438 else
10439 input = error_mark_node;
10440
10441 TREE_VALUE (tail) = input;
10442 }
10443
10444 /* ASMs with labels cannot have outputs. This should have been
10445 enforced by the parser. */
10446 gcc_assert (outputs == NULL || labels == NULL);
10447
10448 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10449
10450 /* asm statements without outputs, including simple ones, are treated
10451 as volatile. */
10452 ASM_INPUT_P (args) = simple;
10453 ASM_VOLATILE_P (args) = (noutputs == 0);
10454 ASM_INLINE_P (args) = is_inline;
10455
10456 return args;
10457 }
10458 \f
10459 /* Generate a goto statement to LABEL. LOC is the location of the
10460 GOTO. */
10461
10462 tree
10463 c_finish_goto_label (location_t loc, tree label)
10464 {
10465 tree decl = lookup_label_for_goto (loc, label);
10466 if (!decl)
10467 return NULL_TREE;
10468 TREE_USED (decl) = 1;
10469 {
10470 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10471 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10472 SET_EXPR_LOCATION (t, loc);
10473 return add_stmt (t);
10474 }
10475 }
10476
10477 /* Generate a computed goto statement to EXPR. LOC is the location of
10478 the GOTO. */
10479
10480 tree
10481 c_finish_goto_ptr (location_t loc, tree expr)
10482 {
10483 tree t;
10484 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10485 expr = c_fully_fold (expr, false, NULL);
10486 expr = convert (ptr_type_node, expr);
10487 t = build1 (GOTO_EXPR, void_type_node, expr);
10488 SET_EXPR_LOCATION (t, loc);
10489 return add_stmt (t);
10490 }
10491
10492 /* Generate a C `return' statement. RETVAL is the expression for what
10493 to return, or a null pointer for `return;' with no value. LOC is
10494 the location of the return statement, or the location of the expression,
10495 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10496 is the original type of RETVAL. */
10497
10498 tree
10499 c_finish_return (location_t loc, tree retval, tree origtype)
10500 {
10501 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10502 bool no_warning = false;
10503 bool npc = false;
10504
10505 /* Use the expansion point to handle cases such as returning NULL
10506 in a function returning void. */
10507 location_t xloc = expansion_point_location_if_in_system_header (loc);
10508
10509 if (TREE_THIS_VOLATILE (current_function_decl))
10510 warning_at (xloc, 0,
10511 "function declared %<noreturn%> has a %<return%> statement");
10512
10513 if (retval)
10514 {
10515 tree semantic_type = NULL_TREE;
10516 npc = null_pointer_constant_p (retval);
10517 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10518 {
10519 semantic_type = TREE_TYPE (retval);
10520 retval = TREE_OPERAND (retval, 0);
10521 }
10522 retval = c_fully_fold (retval, false, NULL);
10523 if (semantic_type)
10524 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10525 }
10526
10527 if (!retval)
10528 {
10529 current_function_returns_null = 1;
10530 if ((warn_return_type >= 0 || flag_isoc99)
10531 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10532 {
10533 bool warned_here;
10534 if (flag_isoc99)
10535 warned_here = pedwarn
10536 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10537 "%<return%> with no value, in function returning non-void");
10538 else
10539 warned_here = warning_at
10540 (loc, OPT_Wreturn_type,
10541 "%<return%> with no value, in function returning non-void");
10542 no_warning = true;
10543 if (warned_here)
10544 inform (DECL_SOURCE_LOCATION (current_function_decl),
10545 "declared here");
10546 }
10547 }
10548 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10549 {
10550 current_function_returns_null = 1;
10551 bool warned_here;
10552 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10553 warned_here = pedwarn
10554 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10555 "%<return%> with a value, in function returning void");
10556 else
10557 warned_here = pedwarn
10558 (xloc, OPT_Wpedantic, "ISO C forbids "
10559 "%<return%> with expression, in function returning void");
10560 if (warned_here)
10561 inform (DECL_SOURCE_LOCATION (current_function_decl),
10562 "declared here");
10563 }
10564 else
10565 {
10566 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10567 retval, origtype, ic_return,
10568 npc, NULL_TREE, NULL_TREE, 0);
10569 tree res = DECL_RESULT (current_function_decl);
10570 tree inner;
10571 bool save;
10572
10573 current_function_returns_value = 1;
10574 if (t == error_mark_node)
10575 return NULL_TREE;
10576
10577 save = in_late_binary_op;
10578 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10579 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10580 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10581 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10582 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10583 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10584 in_late_binary_op = true;
10585 inner = t = convert (TREE_TYPE (res), t);
10586 in_late_binary_op = save;
10587
10588 /* Strip any conversions, additions, and subtractions, and see if
10589 we are returning the address of a local variable. Warn if so. */
10590 while (1)
10591 {
10592 switch (TREE_CODE (inner))
10593 {
10594 CASE_CONVERT:
10595 case NON_LVALUE_EXPR:
10596 case PLUS_EXPR:
10597 case POINTER_PLUS_EXPR:
10598 inner = TREE_OPERAND (inner, 0);
10599 continue;
10600
10601 case MINUS_EXPR:
10602 /* If the second operand of the MINUS_EXPR has a pointer
10603 type (or is converted from it), this may be valid, so
10604 don't give a warning. */
10605 {
10606 tree op1 = TREE_OPERAND (inner, 1);
10607
10608 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10609 && (CONVERT_EXPR_P (op1)
10610 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10611 op1 = TREE_OPERAND (op1, 0);
10612
10613 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10614 break;
10615
10616 inner = TREE_OPERAND (inner, 0);
10617 continue;
10618 }
10619
10620 case ADDR_EXPR:
10621 inner = TREE_OPERAND (inner, 0);
10622
10623 while (REFERENCE_CLASS_P (inner)
10624 && !INDIRECT_REF_P (inner))
10625 inner = TREE_OPERAND (inner, 0);
10626
10627 if (DECL_P (inner)
10628 && !DECL_EXTERNAL (inner)
10629 && !TREE_STATIC (inner)
10630 && DECL_CONTEXT (inner) == current_function_decl)
10631 {
10632 if (TREE_CODE (inner) == LABEL_DECL)
10633 warning_at (loc, OPT_Wreturn_local_addr,
10634 "function returns address of label");
10635 else
10636 {
10637 warning_at (loc, OPT_Wreturn_local_addr,
10638 "function returns address of local variable");
10639 tree zero = build_zero_cst (TREE_TYPE (res));
10640 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10641 }
10642 }
10643 break;
10644
10645 default:
10646 break;
10647 }
10648
10649 break;
10650 }
10651
10652 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10653 SET_EXPR_LOCATION (retval, loc);
10654
10655 if (warn_sequence_point)
10656 verify_sequence_points (retval);
10657 }
10658
10659 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10660 TREE_NO_WARNING (ret_stmt) |= no_warning;
10661 return add_stmt (ret_stmt);
10662 }
10663 \f
10664 struct c_switch {
10665 /* The SWITCH_EXPR being built. */
10666 tree switch_expr;
10667
10668 /* The original type of the testing expression, i.e. before the
10669 default conversion is applied. */
10670 tree orig_type;
10671
10672 /* A splay-tree mapping the low element of a case range to the high
10673 element, or NULL_TREE if there is no high element. Used to
10674 determine whether or not a new case label duplicates an old case
10675 label. We need a tree, rather than simply a hash table, because
10676 of the GNU case range extension. */
10677 splay_tree cases;
10678
10679 /* The bindings at the point of the switch. This is used for
10680 warnings crossing decls when branching to a case label. */
10681 struct c_spot_bindings *bindings;
10682
10683 /* The next node on the stack. */
10684 struct c_switch *next;
10685
10686 /* Remember whether the controlling expression had boolean type
10687 before integer promotions for the sake of -Wswitch-bool. */
10688 bool bool_cond_p;
10689
10690 /* Remember whether there was a case value that is outside the
10691 range of the ORIG_TYPE. */
10692 bool outside_range_p;
10693 };
10694
10695 /* A stack of the currently active switch statements. The innermost
10696 switch statement is on the top of the stack. There is no need to
10697 mark the stack for garbage collection because it is only active
10698 during the processing of the body of a function, and we never
10699 collect at that point. */
10700
10701 struct c_switch *c_switch_stack;
10702
10703 /* Start a C switch statement, testing expression EXP. Return the new
10704 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10705 SWITCH_COND_LOC is the location of the switch's condition.
10706 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10707
10708 tree
10709 c_start_case (location_t switch_loc,
10710 location_t switch_cond_loc,
10711 tree exp, bool explicit_cast_p)
10712 {
10713 tree orig_type = error_mark_node;
10714 bool bool_cond_p = false;
10715 struct c_switch *cs;
10716
10717 if (exp != error_mark_node)
10718 {
10719 orig_type = TREE_TYPE (exp);
10720
10721 if (!INTEGRAL_TYPE_P (orig_type))
10722 {
10723 if (orig_type != error_mark_node)
10724 {
10725 error_at (switch_cond_loc, "switch quantity not an integer");
10726 orig_type = error_mark_node;
10727 }
10728 exp = integer_zero_node;
10729 }
10730 else
10731 {
10732 tree type = TYPE_MAIN_VARIANT (orig_type);
10733 tree e = exp;
10734
10735 /* Warn if the condition has boolean value. */
10736 while (TREE_CODE (e) == COMPOUND_EXPR)
10737 e = TREE_OPERAND (e, 1);
10738
10739 if ((TREE_CODE (type) == BOOLEAN_TYPE
10740 || truth_value_p (TREE_CODE (e)))
10741 /* Explicit cast to int suppresses this warning. */
10742 && !(TREE_CODE (type) == INTEGER_TYPE
10743 && explicit_cast_p))
10744 bool_cond_p = true;
10745
10746 if (!in_system_header_at (input_location)
10747 && (type == long_integer_type_node
10748 || type == long_unsigned_type_node))
10749 warning_at (switch_cond_loc,
10750 OPT_Wtraditional, "%<long%> switch expression not "
10751 "converted to %<int%> in ISO C");
10752
10753 exp = c_fully_fold (exp, false, NULL);
10754 exp = default_conversion (exp);
10755
10756 if (warn_sequence_point)
10757 verify_sequence_points (exp);
10758 }
10759 }
10760
10761 /* Add this new SWITCH_EXPR to the stack. */
10762 cs = XNEW (struct c_switch);
10763 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10764 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10765 cs->orig_type = orig_type;
10766 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10767 cs->bindings = c_get_switch_bindings ();
10768 cs->bool_cond_p = bool_cond_p;
10769 cs->outside_range_p = false;
10770 cs->next = c_switch_stack;
10771 c_switch_stack = cs;
10772
10773 return add_stmt (cs->switch_expr);
10774 }
10775
10776 /* Process a case label at location LOC. */
10777
10778 tree
10779 do_case (location_t loc, tree low_value, tree high_value)
10780 {
10781 tree label = NULL_TREE;
10782
10783 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10784 {
10785 low_value = c_fully_fold (low_value, false, NULL);
10786 if (TREE_CODE (low_value) == INTEGER_CST)
10787 pedwarn (loc, OPT_Wpedantic,
10788 "case label is not an integer constant expression");
10789 }
10790
10791 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10792 {
10793 high_value = c_fully_fold (high_value, false, NULL);
10794 if (TREE_CODE (high_value) == INTEGER_CST)
10795 pedwarn (input_location, OPT_Wpedantic,
10796 "case label is not an integer constant expression");
10797 }
10798
10799 if (c_switch_stack == NULL)
10800 {
10801 if (low_value)
10802 error_at (loc, "case label not within a switch statement");
10803 else
10804 error_at (loc, "%<default%> label not within a switch statement");
10805 return NULL_TREE;
10806 }
10807
10808 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10809 EXPR_LOCATION (c_switch_stack->switch_expr),
10810 loc))
10811 return NULL_TREE;
10812
10813 label = c_add_case_label (loc, c_switch_stack->cases,
10814 SWITCH_COND (c_switch_stack->switch_expr),
10815 c_switch_stack->orig_type,
10816 low_value, high_value,
10817 &c_switch_stack->outside_range_p);
10818 if (label == error_mark_node)
10819 label = NULL_TREE;
10820 return label;
10821 }
10822
10823 /* Finish the switch statement. TYPE is the original type of the
10824 controlling expression of the switch, or NULL_TREE. */
10825
10826 void
10827 c_finish_case (tree body, tree type)
10828 {
10829 struct c_switch *cs = c_switch_stack;
10830 location_t switch_location;
10831
10832 SWITCH_BODY (cs->switch_expr) = body;
10833
10834 /* Emit warnings as needed. */
10835 switch_location = EXPR_LOCATION (cs->switch_expr);
10836 c_do_switch_warnings (cs->cases, switch_location,
10837 type ? type : TREE_TYPE (cs->switch_expr),
10838 SWITCH_COND (cs->switch_expr),
10839 cs->bool_cond_p, cs->outside_range_p);
10840 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10841 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10842
10843 /* Pop the stack. */
10844 c_switch_stack = cs->next;
10845 splay_tree_delete (cs->cases);
10846 c_release_switch_bindings (cs->bindings);
10847 XDELETE (cs);
10848 }
10849 \f
10850 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10851 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10852 may be null. */
10853
10854 void
10855 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10856 tree else_block)
10857 {
10858 tree stmt;
10859
10860 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10861 SET_EXPR_LOCATION (stmt, if_locus);
10862 add_stmt (stmt);
10863 }
10864
10865 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10866 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10867 is false for DO loops. INCR is the FOR increment expression. BODY is
10868 the statement controlled by the loop. BLAB is the break label. CLAB is
10869 the continue label. Everything is allowed to be NULL. */
10870
10871 void
10872 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10873 tree blab, tree clab, bool cond_is_first)
10874 {
10875 tree entry = NULL, exit = NULL, t;
10876
10877 /* If the condition is zero don't generate a loop construct. */
10878 if (cond && integer_zerop (cond))
10879 {
10880 if (cond_is_first)
10881 {
10882 t = build_and_jump (&blab);
10883 SET_EXPR_LOCATION (t, start_locus);
10884 add_stmt (t);
10885 }
10886 }
10887 else
10888 {
10889 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10890
10891 /* If we have an exit condition, then we build an IF with gotos either
10892 out of the loop, or to the top of it. If there's no exit condition,
10893 then we just build a jump back to the top. */
10894 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10895
10896 if (cond && !integer_nonzerop (cond))
10897 {
10898 /* Canonicalize the loop condition to the end. This means
10899 generating a branch to the loop condition. Reuse the
10900 continue label, if possible. */
10901 if (cond_is_first)
10902 {
10903 if (incr || !clab)
10904 {
10905 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10906 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10907 }
10908 else
10909 t = build1 (GOTO_EXPR, void_type_node, clab);
10910 SET_EXPR_LOCATION (t, start_locus);
10911 add_stmt (t);
10912 }
10913
10914 t = build_and_jump (&blab);
10915 if (cond_is_first)
10916 exit = fold_build3_loc (start_locus,
10917 COND_EXPR, void_type_node, cond, exit, t);
10918 else
10919 exit = fold_build3_loc (input_location,
10920 COND_EXPR, void_type_node, cond, exit, t);
10921 }
10922 else
10923 {
10924 /* For the backward-goto's location of an unconditional loop
10925 use the beginning of the body, or, if there is none, the
10926 top of the loop. */
10927 location_t loc = EXPR_LOCATION (expr_first (body));
10928 if (loc == UNKNOWN_LOCATION)
10929 loc = start_locus;
10930 SET_EXPR_LOCATION (exit, loc);
10931 }
10932
10933 add_stmt (top);
10934 }
10935
10936 if (body)
10937 add_stmt (body);
10938 if (clab)
10939 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10940 if (incr)
10941 add_stmt (incr);
10942 if (entry)
10943 add_stmt (entry);
10944 if (exit)
10945 add_stmt (exit);
10946 if (blab)
10947 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10948 }
10949
10950 tree
10951 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10952 {
10953 bool skip;
10954 tree label = *label_p;
10955
10956 /* In switch statements break is sometimes stylistically used after
10957 a return statement. This can lead to spurious warnings about
10958 control reaching the end of a non-void function when it is
10959 inlined. Note that we are calling block_may_fallthru with
10960 language specific tree nodes; this works because
10961 block_may_fallthru returns true when given something it does not
10962 understand. */
10963 skip = !block_may_fallthru (cur_stmt_list);
10964
10965 if (!label)
10966 {
10967 if (!skip)
10968 *label_p = label = create_artificial_label (loc);
10969 }
10970 else if (TREE_CODE (label) == LABEL_DECL)
10971 ;
10972 else switch (TREE_INT_CST_LOW (label))
10973 {
10974 case 0:
10975 if (is_break)
10976 error_at (loc, "break statement not within loop or switch");
10977 else
10978 error_at (loc, "continue statement not within a loop");
10979 return NULL_TREE;
10980
10981 case 1:
10982 gcc_assert (is_break);
10983 error_at (loc, "break statement used with OpenMP for loop");
10984 return NULL_TREE;
10985
10986 case 2:
10987 if (is_break)
10988 error ("break statement within %<#pragma simd%> loop body");
10989 else
10990 error ("continue statement within %<#pragma simd%> loop body");
10991 return NULL_TREE;
10992
10993 default:
10994 gcc_unreachable ();
10995 }
10996
10997 if (skip)
10998 return NULL_TREE;
10999
11000 if (!is_break)
11001 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
11002
11003 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
11004 }
11005
11006 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11007
11008 static void
11009 emit_side_effect_warnings (location_t loc, tree expr)
11010 {
11011 if (expr == error_mark_node)
11012 ;
11013 else if (!TREE_SIDE_EFFECTS (expr))
11014 {
11015 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
11016 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11017 }
11018 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11019 {
11020 tree r = expr;
11021 location_t cloc = loc;
11022 while (TREE_CODE (r) == COMPOUND_EXPR)
11023 {
11024 if (EXPR_HAS_LOCATION (r))
11025 cloc = EXPR_LOCATION (r);
11026 r = TREE_OPERAND (r, 1);
11027 }
11028 if (!TREE_SIDE_EFFECTS (r)
11029 && !VOID_TYPE_P (TREE_TYPE (r))
11030 && !CONVERT_EXPR_P (r)
11031 && !TREE_NO_WARNING (r)
11032 && !TREE_NO_WARNING (expr))
11033 warning_at (cloc, OPT_Wunused_value,
11034 "right-hand operand of comma expression has no effect");
11035 }
11036 else
11037 warn_if_unused_value (expr, loc);
11038 }
11039
11040 /* Process an expression as if it were a complete statement. Emit
11041 diagnostics, but do not call ADD_STMT. LOC is the location of the
11042 statement. */
11043
11044 tree
11045 c_process_expr_stmt (location_t loc, tree expr)
11046 {
11047 tree exprv;
11048
11049 if (!expr)
11050 return NULL_TREE;
11051
11052 expr = c_fully_fold (expr, false, NULL);
11053
11054 if (warn_sequence_point)
11055 verify_sequence_points (expr);
11056
11057 if (TREE_TYPE (expr) != error_mark_node
11058 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11059 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11060 error_at (loc, "expression statement has incomplete type");
11061
11062 /* If we're not processing a statement expression, warn about unused values.
11063 Warnings for statement expressions will be emitted later, once we figure
11064 out which is the result. */
11065 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11066 && warn_unused_value)
11067 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11068
11069 exprv = expr;
11070 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11071 exprv = TREE_OPERAND (exprv, 1);
11072 while (CONVERT_EXPR_P (exprv))
11073 exprv = TREE_OPERAND (exprv, 0);
11074 if (DECL_P (exprv)
11075 || handled_component_p (exprv)
11076 || TREE_CODE (exprv) == ADDR_EXPR)
11077 mark_exp_read (exprv);
11078
11079 /* If the expression is not of a type to which we cannot assign a line
11080 number, wrap the thing in a no-op NOP_EXPR. */
11081 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11082 {
11083 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11084 SET_EXPR_LOCATION (expr, loc);
11085 }
11086
11087 return expr;
11088 }
11089
11090 /* Emit an expression as a statement. LOC is the location of the
11091 expression. */
11092
11093 tree
11094 c_finish_expr_stmt (location_t loc, tree expr)
11095 {
11096 if (expr)
11097 return add_stmt (c_process_expr_stmt (loc, expr));
11098 else
11099 return NULL;
11100 }
11101
11102 /* Do the opposite and emit a statement as an expression. To begin,
11103 create a new binding level and return it. */
11104
11105 tree
11106 c_begin_stmt_expr (void)
11107 {
11108 tree ret;
11109
11110 /* We must force a BLOCK for this level so that, if it is not expanded
11111 later, there is a way to turn off the entire subtree of blocks that
11112 are contained in it. */
11113 keep_next_level ();
11114 ret = c_begin_compound_stmt (true);
11115
11116 c_bindings_start_stmt_expr (c_switch_stack == NULL
11117 ? NULL
11118 : c_switch_stack->bindings);
11119
11120 /* Mark the current statement list as belonging to a statement list. */
11121 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11122
11123 return ret;
11124 }
11125
11126 /* LOC is the location of the compound statement to which this body
11127 belongs. */
11128
11129 tree
11130 c_finish_stmt_expr (location_t loc, tree body)
11131 {
11132 tree last, type, tmp, val;
11133 tree *last_p;
11134
11135 body = c_end_compound_stmt (loc, body, true);
11136
11137 c_bindings_end_stmt_expr (c_switch_stack == NULL
11138 ? NULL
11139 : c_switch_stack->bindings);
11140
11141 /* Locate the last statement in BODY. See c_end_compound_stmt
11142 about always returning a BIND_EXPR. */
11143 last_p = &BIND_EXPR_BODY (body);
11144 last = BIND_EXPR_BODY (body);
11145
11146 continue_searching:
11147 if (TREE_CODE (last) == STATEMENT_LIST)
11148 {
11149 tree_stmt_iterator l = tsi_last (last);
11150
11151 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11152 tsi_prev (&l);
11153
11154 /* This can happen with degenerate cases like ({ }). No value. */
11155 if (tsi_end_p (l))
11156 return body;
11157
11158 /* If we're supposed to generate side effects warnings, process
11159 all of the statements except the last. */
11160 if (warn_unused_value)
11161 {
11162 for (tree_stmt_iterator i = tsi_start (last);
11163 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11164 {
11165 location_t tloc;
11166 tree t = tsi_stmt (i);
11167
11168 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11169 emit_side_effect_warnings (tloc, t);
11170 }
11171 }
11172 last_p = tsi_stmt_ptr (l);
11173 last = *last_p;
11174 }
11175
11176 /* If the end of the list is exception related, then the list was split
11177 by a call to push_cleanup. Continue searching. */
11178 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11179 || TREE_CODE (last) == TRY_CATCH_EXPR)
11180 {
11181 last_p = &TREE_OPERAND (last, 0);
11182 last = *last_p;
11183 goto continue_searching;
11184 }
11185
11186 if (last == error_mark_node)
11187 return last;
11188
11189 /* In the case that the BIND_EXPR is not necessary, return the
11190 expression out from inside it. */
11191 if ((last == BIND_EXPR_BODY (body)
11192 /* Skip nested debug stmts. */
11193 || last == expr_first (BIND_EXPR_BODY (body)))
11194 && BIND_EXPR_VARS (body) == NULL)
11195 {
11196 /* Even if this looks constant, do not allow it in a constant
11197 expression. */
11198 last = c_wrap_maybe_const (last, true);
11199 /* Do not warn if the return value of a statement expression is
11200 unused. */
11201 TREE_NO_WARNING (last) = 1;
11202 return last;
11203 }
11204
11205 /* Extract the type of said expression. */
11206 type = TREE_TYPE (last);
11207
11208 /* If we're not returning a value at all, then the BIND_EXPR that
11209 we already have is a fine expression to return. */
11210 if (!type || VOID_TYPE_P (type))
11211 return body;
11212
11213 /* Now that we've located the expression containing the value, it seems
11214 silly to make voidify_wrapper_expr repeat the process. Create a
11215 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11216 tmp = create_tmp_var_raw (type);
11217
11218 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11219 tree_expr_nonnegative_p giving up immediately. */
11220 val = last;
11221 if (TREE_CODE (val) == NOP_EXPR
11222 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11223 val = TREE_OPERAND (val, 0);
11224
11225 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11226 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11227
11228 {
11229 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11230 SET_EXPR_LOCATION (t, loc);
11231 return t;
11232 }
11233 }
11234 \f
11235 /* Begin and end compound statements. This is as simple as pushing
11236 and popping new statement lists from the tree. */
11237
11238 tree
11239 c_begin_compound_stmt (bool do_scope)
11240 {
11241 tree stmt = push_stmt_list ();
11242 if (do_scope)
11243 push_scope ();
11244 return stmt;
11245 }
11246
11247 /* End a compound statement. STMT is the statement. LOC is the
11248 location of the compound statement-- this is usually the location
11249 of the opening brace. */
11250
11251 tree
11252 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11253 {
11254 tree block = NULL;
11255
11256 if (do_scope)
11257 {
11258 if (c_dialect_objc ())
11259 objc_clear_super_receiver ();
11260 block = pop_scope ();
11261 }
11262
11263 stmt = pop_stmt_list (stmt);
11264 stmt = c_build_bind_expr (loc, block, stmt);
11265
11266 /* If this compound statement is nested immediately inside a statement
11267 expression, then force a BIND_EXPR to be created. Otherwise we'll
11268 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11269 STATEMENT_LISTs merge, and thus we can lose track of what statement
11270 was really last. */
11271 if (building_stmt_list_p ()
11272 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11273 && TREE_CODE (stmt) != BIND_EXPR)
11274 {
11275 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11276 TREE_SIDE_EFFECTS (stmt) = 1;
11277 SET_EXPR_LOCATION (stmt, loc);
11278 }
11279
11280 return stmt;
11281 }
11282
11283 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11284 when the current scope is exited. EH_ONLY is true when this is not
11285 meant to apply to normal control flow transfer. */
11286
11287 void
11288 push_cleanup (tree decl, tree cleanup, bool eh_only)
11289 {
11290 enum tree_code code;
11291 tree stmt, list;
11292 bool stmt_expr;
11293
11294 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11295 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11296 add_stmt (stmt);
11297 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11298 list = push_stmt_list ();
11299 TREE_OPERAND (stmt, 0) = list;
11300 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11301 }
11302 \f
11303 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11304 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11305
11306 static tree
11307 build_vec_cmp (tree_code code, tree type,
11308 tree arg0, tree arg1)
11309 {
11310 tree zero_vec = build_zero_cst (type);
11311 tree minus_one_vec = build_minus_one_cst (type);
11312 tree cmp_type = build_same_sized_truth_vector_type (type);
11313 tree cmp = build2 (code, cmp_type, arg0, arg1);
11314 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11315 }
11316
11317 /* Build a binary-operation expression without default conversions.
11318 CODE is the kind of expression to build.
11319 LOCATION is the operator's location.
11320 This function differs from `build' in several ways:
11321 the data type of the result is computed and recorded in it,
11322 warnings are generated if arg data types are invalid,
11323 special handling for addition and subtraction of pointers is known,
11324 and some optimization is done (operations on narrow ints
11325 are done in the narrower type when that gives the same result).
11326 Constant folding is also done before the result is returned.
11327
11328 Note that the operands will never have enumeral types, or function
11329 or array types, because either they will have the default conversions
11330 performed or they have both just been converted to some other type in which
11331 the arithmetic is to be done. */
11332
11333 tree
11334 build_binary_op (location_t location, enum tree_code code,
11335 tree orig_op0, tree orig_op1, bool convert_p)
11336 {
11337 tree type0, type1, orig_type0, orig_type1;
11338 tree eptype;
11339 enum tree_code code0, code1;
11340 tree op0, op1;
11341 tree ret = error_mark_node;
11342 const char *invalid_op_diag;
11343 bool op0_int_operands, op1_int_operands;
11344 bool int_const, int_const_or_overflow, int_operands;
11345
11346 /* Expression code to give to the expression when it is built.
11347 Normally this is CODE, which is what the caller asked for,
11348 but in some special cases we change it. */
11349 enum tree_code resultcode = code;
11350
11351 /* Data type in which the computation is to be performed.
11352 In the simplest cases this is the common type of the arguments. */
11353 tree result_type = NULL;
11354
11355 /* When the computation is in excess precision, the type of the
11356 final EXCESS_PRECISION_EXPR. */
11357 tree semantic_result_type = NULL;
11358
11359 /* Nonzero means operands have already been type-converted
11360 in whatever way is necessary.
11361 Zero means they need to be converted to RESULT_TYPE. */
11362 int converted = 0;
11363
11364 /* Nonzero means create the expression with this type, rather than
11365 RESULT_TYPE. */
11366 tree build_type = NULL_TREE;
11367
11368 /* Nonzero means after finally constructing the expression
11369 convert it to this type. */
11370 tree final_type = NULL_TREE;
11371
11372 /* Nonzero if this is an operation like MIN or MAX which can
11373 safely be computed in short if both args are promoted shorts.
11374 Also implies COMMON.
11375 -1 indicates a bitwise operation; this makes a difference
11376 in the exact conditions for when it is safe to do the operation
11377 in a narrower mode. */
11378 int shorten = 0;
11379
11380 /* Nonzero if this is a comparison operation;
11381 if both args are promoted shorts, compare the original shorts.
11382 Also implies COMMON. */
11383 int short_compare = 0;
11384
11385 /* Nonzero if this is a right-shift operation, which can be computed on the
11386 original short and then promoted if the operand is a promoted short. */
11387 int short_shift = 0;
11388
11389 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11390 int common = 0;
11391
11392 /* True means types are compatible as far as ObjC is concerned. */
11393 bool objc_ok;
11394
11395 /* True means this is an arithmetic operation that may need excess
11396 precision. */
11397 bool may_need_excess_precision;
11398
11399 /* True means this is a boolean operation that converts both its
11400 operands to truth-values. */
11401 bool boolean_op = false;
11402
11403 /* Remember whether we're doing / or %. */
11404 bool doing_div_or_mod = false;
11405
11406 /* Remember whether we're doing << or >>. */
11407 bool doing_shift = false;
11408
11409 /* Tree holding instrumentation expression. */
11410 tree instrument_expr = NULL;
11411
11412 if (location == UNKNOWN_LOCATION)
11413 location = input_location;
11414
11415 op0 = orig_op0;
11416 op1 = orig_op1;
11417
11418 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11419 if (op0_int_operands)
11420 op0 = remove_c_maybe_const_expr (op0);
11421 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11422 if (op1_int_operands)
11423 op1 = remove_c_maybe_const_expr (op1);
11424 int_operands = (op0_int_operands && op1_int_operands);
11425 if (int_operands)
11426 {
11427 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11428 && TREE_CODE (orig_op1) == INTEGER_CST);
11429 int_const = (int_const_or_overflow
11430 && !TREE_OVERFLOW (orig_op0)
11431 && !TREE_OVERFLOW (orig_op1));
11432 }
11433 else
11434 int_const = int_const_or_overflow = false;
11435
11436 /* Do not apply default conversion in mixed vector/scalar expression. */
11437 if (convert_p
11438 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11439 {
11440 op0 = default_conversion (op0);
11441 op1 = default_conversion (op1);
11442 }
11443
11444 orig_type0 = type0 = TREE_TYPE (op0);
11445
11446 orig_type1 = type1 = TREE_TYPE (op1);
11447
11448 /* The expression codes of the data types of the arguments tell us
11449 whether the arguments are integers, floating, pointers, etc. */
11450 code0 = TREE_CODE (type0);
11451 code1 = TREE_CODE (type1);
11452
11453 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11454 STRIP_TYPE_NOPS (op0);
11455 STRIP_TYPE_NOPS (op1);
11456
11457 /* If an error was already reported for one of the arguments,
11458 avoid reporting another error. */
11459
11460 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11461 return error_mark_node;
11462
11463 if (code0 == POINTER_TYPE
11464 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11465 return error_mark_node;
11466
11467 if (code1 == POINTER_TYPE
11468 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11469 return error_mark_node;
11470
11471 if ((invalid_op_diag
11472 = targetm.invalid_binary_op (code, type0, type1)))
11473 {
11474 error_at (location, invalid_op_diag);
11475 return error_mark_node;
11476 }
11477
11478 switch (code)
11479 {
11480 case PLUS_EXPR:
11481 case MINUS_EXPR:
11482 case MULT_EXPR:
11483 case TRUNC_DIV_EXPR:
11484 case CEIL_DIV_EXPR:
11485 case FLOOR_DIV_EXPR:
11486 case ROUND_DIV_EXPR:
11487 case EXACT_DIV_EXPR:
11488 may_need_excess_precision = true;
11489 break;
11490
11491 case EQ_EXPR:
11492 case NE_EXPR:
11493 case LE_EXPR:
11494 case GE_EXPR:
11495 case LT_EXPR:
11496 case GT_EXPR:
11497 /* Excess precision for implicit conversions of integers to
11498 floating point in C11 and later. */
11499 may_need_excess_precision = (flag_isoc11
11500 && (ANY_INTEGRAL_TYPE_P (type0)
11501 || ANY_INTEGRAL_TYPE_P (type1)));
11502 break;
11503
11504 default:
11505 may_need_excess_precision = false;
11506 break;
11507 }
11508 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11509 {
11510 op0 = TREE_OPERAND (op0, 0);
11511 type0 = TREE_TYPE (op0);
11512 }
11513 else if (may_need_excess_precision
11514 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11515 {
11516 type0 = eptype;
11517 op0 = convert (eptype, op0);
11518 }
11519 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11520 {
11521 op1 = TREE_OPERAND (op1, 0);
11522 type1 = TREE_TYPE (op1);
11523 }
11524 else if (may_need_excess_precision
11525 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11526 {
11527 type1 = eptype;
11528 op1 = convert (eptype, op1);
11529 }
11530
11531 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11532
11533 /* In case when one of the operands of the binary operation is
11534 a vector and another is a scalar -- convert scalar to vector. */
11535 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11536 {
11537 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11538 true);
11539
11540 switch (convert_flag)
11541 {
11542 case stv_error:
11543 return error_mark_node;
11544 case stv_firstarg:
11545 {
11546 bool maybe_const = true;
11547 tree sc;
11548 sc = c_fully_fold (op0, false, &maybe_const);
11549 sc = save_expr (sc);
11550 sc = convert (TREE_TYPE (type1), sc);
11551 op0 = build_vector_from_val (type1, sc);
11552 if (!maybe_const)
11553 op0 = c_wrap_maybe_const (op0, true);
11554 orig_type0 = type0 = TREE_TYPE (op0);
11555 code0 = TREE_CODE (type0);
11556 converted = 1;
11557 break;
11558 }
11559 case stv_secondarg:
11560 {
11561 bool maybe_const = true;
11562 tree sc;
11563 sc = c_fully_fold (op1, false, &maybe_const);
11564 sc = save_expr (sc);
11565 sc = convert (TREE_TYPE (type0), sc);
11566 op1 = build_vector_from_val (type0, sc);
11567 if (!maybe_const)
11568 op1 = c_wrap_maybe_const (op1, true);
11569 orig_type1 = type1 = TREE_TYPE (op1);
11570 code1 = TREE_CODE (type1);
11571 converted = 1;
11572 break;
11573 }
11574 default:
11575 break;
11576 }
11577 }
11578
11579 switch (code)
11580 {
11581 case PLUS_EXPR:
11582 /* Handle the pointer + int case. */
11583 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11584 {
11585 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11586 goto return_build_binary_op;
11587 }
11588 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11589 {
11590 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11591 goto return_build_binary_op;
11592 }
11593 else
11594 common = 1;
11595 break;
11596
11597 case MINUS_EXPR:
11598 /* Subtraction of two similar pointers.
11599 We must subtract them as integers, then divide by object size. */
11600 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11601 && comp_target_types (location, type0, type1))
11602 {
11603 ret = pointer_diff (location, op0, op1, &instrument_expr);
11604 goto return_build_binary_op;
11605 }
11606 /* Handle pointer minus int. Just like pointer plus int. */
11607 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11608 {
11609 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11610 goto return_build_binary_op;
11611 }
11612 else
11613 common = 1;
11614 break;
11615
11616 case MULT_EXPR:
11617 common = 1;
11618 break;
11619
11620 case TRUNC_DIV_EXPR:
11621 case CEIL_DIV_EXPR:
11622 case FLOOR_DIV_EXPR:
11623 case ROUND_DIV_EXPR:
11624 case EXACT_DIV_EXPR:
11625 doing_div_or_mod = true;
11626 warn_for_div_by_zero (location, op1);
11627
11628 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11629 || code0 == FIXED_POINT_TYPE
11630 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11631 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11632 || code1 == FIXED_POINT_TYPE
11633 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11634 {
11635 enum tree_code tcode0 = code0, tcode1 = code1;
11636
11637 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11638 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11639 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11640 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11641
11642 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11643 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11644 resultcode = RDIV_EXPR;
11645 else
11646 /* Although it would be tempting to shorten always here, that
11647 loses on some targets, since the modulo instruction is
11648 undefined if the quotient can't be represented in the
11649 computation mode. We shorten only if unsigned or if
11650 dividing by something we know != -1. */
11651 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11652 || (TREE_CODE (op1) == INTEGER_CST
11653 && !integer_all_onesp (op1)));
11654 common = 1;
11655 }
11656 break;
11657
11658 case BIT_AND_EXPR:
11659 case BIT_IOR_EXPR:
11660 case BIT_XOR_EXPR:
11661 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11662 shorten = -1;
11663 /* Allow vector types which are not floating point types. */
11664 else if (code0 == VECTOR_TYPE
11665 && code1 == VECTOR_TYPE
11666 && !VECTOR_FLOAT_TYPE_P (type0)
11667 && !VECTOR_FLOAT_TYPE_P (type1))
11668 common = 1;
11669 break;
11670
11671 case TRUNC_MOD_EXPR:
11672 case FLOOR_MOD_EXPR:
11673 doing_div_or_mod = true;
11674 warn_for_div_by_zero (location, op1);
11675
11676 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11677 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11678 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11679 common = 1;
11680 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11681 {
11682 /* Although it would be tempting to shorten always here, that loses
11683 on some targets, since the modulo instruction is undefined if the
11684 quotient can't be represented in the computation mode. We shorten
11685 only if unsigned or if dividing by something we know != -1. */
11686 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11687 || (TREE_CODE (op1) == INTEGER_CST
11688 && !integer_all_onesp (op1)));
11689 common = 1;
11690 }
11691 break;
11692
11693 case TRUTH_ANDIF_EXPR:
11694 case TRUTH_ORIF_EXPR:
11695 case TRUTH_AND_EXPR:
11696 case TRUTH_OR_EXPR:
11697 case TRUTH_XOR_EXPR:
11698 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11699 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11700 || code0 == FIXED_POINT_TYPE)
11701 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11702 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11703 || code1 == FIXED_POINT_TYPE))
11704 {
11705 /* Result of these operations is always an int,
11706 but that does not mean the operands should be
11707 converted to ints! */
11708 result_type = integer_type_node;
11709 if (op0_int_operands)
11710 {
11711 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11712 op0 = remove_c_maybe_const_expr (op0);
11713 }
11714 else
11715 op0 = c_objc_common_truthvalue_conversion (location, op0);
11716 if (op1_int_operands)
11717 {
11718 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11719 op1 = remove_c_maybe_const_expr (op1);
11720 }
11721 else
11722 op1 = c_objc_common_truthvalue_conversion (location, op1);
11723 converted = 1;
11724 boolean_op = true;
11725 }
11726 if (code == TRUTH_ANDIF_EXPR)
11727 {
11728 int_const_or_overflow = (int_operands
11729 && TREE_CODE (orig_op0) == INTEGER_CST
11730 && (op0 == truthvalue_false_node
11731 || TREE_CODE (orig_op1) == INTEGER_CST));
11732 int_const = (int_const_or_overflow
11733 && !TREE_OVERFLOW (orig_op0)
11734 && (op0 == truthvalue_false_node
11735 || !TREE_OVERFLOW (orig_op1)));
11736 }
11737 else if (code == TRUTH_ORIF_EXPR)
11738 {
11739 int_const_or_overflow = (int_operands
11740 && TREE_CODE (orig_op0) == INTEGER_CST
11741 && (op0 == truthvalue_true_node
11742 || TREE_CODE (orig_op1) == INTEGER_CST));
11743 int_const = (int_const_or_overflow
11744 && !TREE_OVERFLOW (orig_op0)
11745 && (op0 == truthvalue_true_node
11746 || !TREE_OVERFLOW (orig_op1)));
11747 }
11748 break;
11749
11750 /* Shift operations: result has same type as first operand;
11751 always convert second operand to int.
11752 Also set SHORT_SHIFT if shifting rightward. */
11753
11754 case RSHIFT_EXPR:
11755 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11756 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11757 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11758 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11759 TYPE_VECTOR_SUBPARTS (type1)))
11760 {
11761 result_type = type0;
11762 converted = 1;
11763 }
11764 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11765 || (code0 == VECTOR_TYPE
11766 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11767 && code1 == INTEGER_TYPE)
11768 {
11769 doing_shift = true;
11770 if (TREE_CODE (op1) == INTEGER_CST)
11771 {
11772 if (tree_int_cst_sgn (op1) < 0)
11773 {
11774 int_const = false;
11775 if (c_inhibit_evaluation_warnings == 0)
11776 warning_at (location, OPT_Wshift_count_negative,
11777 "right shift count is negative");
11778 }
11779 else if (code0 == VECTOR_TYPE)
11780 {
11781 if (compare_tree_int (op1,
11782 TYPE_PRECISION (TREE_TYPE (type0)))
11783 >= 0)
11784 {
11785 int_const = false;
11786 if (c_inhibit_evaluation_warnings == 0)
11787 warning_at (location, OPT_Wshift_count_overflow,
11788 "right shift count >= width of vector element");
11789 }
11790 }
11791 else
11792 {
11793 if (!integer_zerop (op1))
11794 short_shift = 1;
11795
11796 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11797 {
11798 int_const = false;
11799 if (c_inhibit_evaluation_warnings == 0)
11800 warning_at (location, OPT_Wshift_count_overflow,
11801 "right shift count >= width of type");
11802 }
11803 }
11804 }
11805
11806 /* Use the type of the value to be shifted. */
11807 result_type = type0;
11808 /* Avoid converting op1 to result_type later. */
11809 converted = 1;
11810 }
11811 break;
11812
11813 case LSHIFT_EXPR:
11814 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11815 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11816 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11817 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11818 TYPE_VECTOR_SUBPARTS (type1)))
11819 {
11820 result_type = type0;
11821 converted = 1;
11822 }
11823 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11824 || (code0 == VECTOR_TYPE
11825 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11826 && code1 == INTEGER_TYPE)
11827 {
11828 doing_shift = true;
11829 if (TREE_CODE (op0) == INTEGER_CST
11830 && tree_int_cst_sgn (op0) < 0)
11831 {
11832 /* Don't reject a left shift of a negative value in a context
11833 where a constant expression is needed in C90. */
11834 if (flag_isoc99)
11835 int_const = false;
11836 if (c_inhibit_evaluation_warnings == 0)
11837 warning_at (location, OPT_Wshift_negative_value,
11838 "left shift of negative value");
11839 }
11840 if (TREE_CODE (op1) == INTEGER_CST)
11841 {
11842 if (tree_int_cst_sgn (op1) < 0)
11843 {
11844 int_const = false;
11845 if (c_inhibit_evaluation_warnings == 0)
11846 warning_at (location, OPT_Wshift_count_negative,
11847 "left shift count is negative");
11848 }
11849 else if (code0 == VECTOR_TYPE)
11850 {
11851 if (compare_tree_int (op1,
11852 TYPE_PRECISION (TREE_TYPE (type0)))
11853 >= 0)
11854 {
11855 int_const = false;
11856 if (c_inhibit_evaluation_warnings == 0)
11857 warning_at (location, OPT_Wshift_count_overflow,
11858 "left shift count >= width of vector element");
11859 }
11860 }
11861 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11862 {
11863 int_const = false;
11864 if (c_inhibit_evaluation_warnings == 0)
11865 warning_at (location, OPT_Wshift_count_overflow,
11866 "left shift count >= width of type");
11867 }
11868 else if (TREE_CODE (op0) == INTEGER_CST
11869 && maybe_warn_shift_overflow (location, op0, op1)
11870 && flag_isoc99)
11871 int_const = false;
11872 }
11873
11874 /* Use the type of the value to be shifted. */
11875 result_type = type0;
11876 /* Avoid converting op1 to result_type later. */
11877 converted = 1;
11878 }
11879 break;
11880
11881 case EQ_EXPR:
11882 case NE_EXPR:
11883 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11884 {
11885 tree intt;
11886 if (!vector_types_compatible_elements_p (type0, type1))
11887 {
11888 error_at (location, "comparing vectors with different "
11889 "element types");
11890 return error_mark_node;
11891 }
11892
11893 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11894 TYPE_VECTOR_SUBPARTS (type1)))
11895 {
11896 error_at (location, "comparing vectors with different "
11897 "number of elements");
11898 return error_mark_node;
11899 }
11900
11901 /* It's not precisely specified how the usual arithmetic
11902 conversions apply to the vector types. Here, we use
11903 the unsigned type if one of the operands is signed and
11904 the other one is unsigned. */
11905 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11906 {
11907 if (!TYPE_UNSIGNED (type0))
11908 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11909 else
11910 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11911 warning_at (location, OPT_Wsign_compare, "comparison between "
11912 "types %qT and %qT", type0, type1);
11913 }
11914
11915 /* Always construct signed integer vector type. */
11916 intt = c_common_type_for_size (GET_MODE_BITSIZE
11917 (SCALAR_TYPE_MODE
11918 (TREE_TYPE (type0))), 0);
11919 if (!intt)
11920 {
11921 error_at (location, "could not find an integer type "
11922 "of the same size as %qT",
11923 TREE_TYPE (type0));
11924 return error_mark_node;
11925 }
11926 result_type = build_opaque_vector_type (intt,
11927 TYPE_VECTOR_SUBPARTS (type0));
11928 converted = 1;
11929 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11930 goto return_build_binary_op;
11931 }
11932 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11933 warning_at (location,
11934 OPT_Wfloat_equal,
11935 "comparing floating point with == or != is unsafe");
11936 /* Result of comparison is always int,
11937 but don't convert the args to int! */
11938 build_type = integer_type_node;
11939 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11940 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11941 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11942 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11943 short_compare = 1;
11944 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11945 {
11946 if (TREE_CODE (op0) == ADDR_EXPR
11947 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11948 && !from_macro_expansion_at (location))
11949 {
11950 if (code == EQ_EXPR)
11951 warning_at (location,
11952 OPT_Waddress,
11953 "the comparison will always evaluate as %<false%> "
11954 "for the address of %qD will never be NULL",
11955 TREE_OPERAND (op0, 0));
11956 else
11957 warning_at (location,
11958 OPT_Waddress,
11959 "the comparison will always evaluate as %<true%> "
11960 "for the address of %qD will never be NULL",
11961 TREE_OPERAND (op0, 0));
11962 }
11963 result_type = type0;
11964 }
11965 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11966 {
11967 if (TREE_CODE (op1) == ADDR_EXPR
11968 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11969 && !from_macro_expansion_at (location))
11970 {
11971 if (code == EQ_EXPR)
11972 warning_at (location,
11973 OPT_Waddress,
11974 "the comparison will always evaluate as %<false%> "
11975 "for the address of %qD will never be NULL",
11976 TREE_OPERAND (op1, 0));
11977 else
11978 warning_at (location,
11979 OPT_Waddress,
11980 "the comparison will always evaluate as %<true%> "
11981 "for the address of %qD will never be NULL",
11982 TREE_OPERAND (op1, 0));
11983 }
11984 result_type = type1;
11985 }
11986 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11987 {
11988 tree tt0 = TREE_TYPE (type0);
11989 tree tt1 = TREE_TYPE (type1);
11990 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11991 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11992 addr_space_t as_common = ADDR_SPACE_GENERIC;
11993
11994 /* Anything compares with void *. void * compares with anything.
11995 Otherwise, the targets must be compatible
11996 and both must be object or both incomplete. */
11997 if (comp_target_types (location, type0, type1))
11998 result_type = common_pointer_type (type0, type1);
11999 else if (!addr_space_superset (as0, as1, &as_common))
12000 {
12001 error_at (location, "comparison of pointers to "
12002 "disjoint address spaces");
12003 return error_mark_node;
12004 }
12005 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12006 {
12007 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12008 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12009 "comparison of %<void *%> with function pointer");
12010 }
12011 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12012 {
12013 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12014 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12015 "comparison of %<void *%> with function pointer");
12016 }
12017 else
12018 /* Avoid warning about the volatile ObjC EH puts on decls. */
12019 if (!objc_ok)
12020 pedwarn (location, 0,
12021 "comparison of distinct pointer types lacks a cast");
12022
12023 if (result_type == NULL_TREE)
12024 {
12025 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12026 result_type = build_pointer_type
12027 (build_qualified_type (void_type_node, qual));
12028 }
12029 }
12030 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12031 {
12032 result_type = type0;
12033 pedwarn (location, 0, "comparison between pointer and integer");
12034 }
12035 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12036 {
12037 result_type = type1;
12038 pedwarn (location, 0, "comparison between pointer and integer");
12039 }
12040 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12041 || truth_value_p (TREE_CODE (orig_op0)))
12042 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12043 || truth_value_p (TREE_CODE (orig_op1))))
12044 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12045 break;
12046
12047 case LE_EXPR:
12048 case GE_EXPR:
12049 case LT_EXPR:
12050 case GT_EXPR:
12051 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
12052 {
12053 tree intt;
12054 if (!vector_types_compatible_elements_p (type0, type1))
12055 {
12056 error_at (location, "comparing vectors with different "
12057 "element types");
12058 return error_mark_node;
12059 }
12060
12061 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12062 TYPE_VECTOR_SUBPARTS (type1)))
12063 {
12064 error_at (location, "comparing vectors with different "
12065 "number of elements");
12066 return error_mark_node;
12067 }
12068
12069 /* It's not precisely specified how the usual arithmetic
12070 conversions apply to the vector types. Here, we use
12071 the unsigned type if one of the operands is signed and
12072 the other one is unsigned. */
12073 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12074 {
12075 if (!TYPE_UNSIGNED (type0))
12076 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12077 else
12078 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12079 warning_at (location, OPT_Wsign_compare, "comparison between "
12080 "types %qT and %qT", type0, type1);
12081 }
12082
12083 /* Always construct signed integer vector type. */
12084 intt = c_common_type_for_size (GET_MODE_BITSIZE
12085 (SCALAR_TYPE_MODE
12086 (TREE_TYPE (type0))), 0);
12087 if (!intt)
12088 {
12089 error_at (location, "could not find an integer type "
12090 "of the same size as %qT",
12091 TREE_TYPE (type0));
12092 return error_mark_node;
12093 }
12094 result_type = build_opaque_vector_type (intt,
12095 TYPE_VECTOR_SUBPARTS (type0));
12096 converted = 1;
12097 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12098 goto return_build_binary_op;
12099 }
12100 build_type = integer_type_node;
12101 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12102 || code0 == FIXED_POINT_TYPE)
12103 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12104 || code1 == FIXED_POINT_TYPE))
12105 short_compare = 1;
12106 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12107 {
12108 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12109 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12110 addr_space_t as_common;
12111
12112 if (comp_target_types (location, type0, type1))
12113 {
12114 result_type = common_pointer_type (type0, type1);
12115 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12116 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12117 pedwarn (location, 0,
12118 "comparison of complete and incomplete pointers");
12119 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12120 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12121 "ordered comparisons of pointers to functions");
12122 else if (null_pointer_constant_p (orig_op0)
12123 || null_pointer_constant_p (orig_op1))
12124 warning_at (location, OPT_Wextra,
12125 "ordered comparison of pointer with null pointer");
12126
12127 }
12128 else if (!addr_space_superset (as0, as1, &as_common))
12129 {
12130 error_at (location, "comparison of pointers to "
12131 "disjoint address spaces");
12132 return error_mark_node;
12133 }
12134 else
12135 {
12136 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12137 result_type = build_pointer_type
12138 (build_qualified_type (void_type_node, qual));
12139 pedwarn (location, 0,
12140 "comparison of distinct pointer types lacks a cast");
12141 }
12142 }
12143 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12144 {
12145 result_type = type0;
12146 if (pedantic)
12147 pedwarn (location, OPT_Wpedantic,
12148 "ordered comparison of pointer with integer zero");
12149 else if (extra_warnings)
12150 warning_at (location, OPT_Wextra,
12151 "ordered comparison of pointer with integer zero");
12152 }
12153 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12154 {
12155 result_type = type1;
12156 if (pedantic)
12157 pedwarn (location, OPT_Wpedantic,
12158 "ordered comparison of pointer with integer zero");
12159 else if (extra_warnings)
12160 warning_at (location, OPT_Wextra,
12161 "ordered comparison of pointer with integer zero");
12162 }
12163 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12164 {
12165 result_type = type0;
12166 pedwarn (location, 0, "comparison between pointer and integer");
12167 }
12168 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12169 {
12170 result_type = type1;
12171 pedwarn (location, 0, "comparison between pointer and integer");
12172 }
12173
12174 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12175 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12176 {
12177 op0 = save_expr (op0);
12178 op1 = save_expr (op1);
12179
12180 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12181 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12182 }
12183
12184 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12185 || truth_value_p (TREE_CODE (orig_op0)))
12186 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12187 || truth_value_p (TREE_CODE (orig_op1))))
12188 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12189 break;
12190
12191 default:
12192 gcc_unreachable ();
12193 }
12194
12195 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12196 return error_mark_node;
12197
12198 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
12199 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12200 || !vector_types_compatible_elements_p (type0, type1)))
12201 {
12202 gcc_rich_location richloc (location);
12203 maybe_range_label_for_tree_type_mismatch
12204 label_for_op0 (orig_op0, orig_op1),
12205 label_for_op1 (orig_op1, orig_op0);
12206 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12207 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12208 binary_op_error (&richloc, code, type0, type1);
12209 return error_mark_node;
12210 }
12211
12212 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12213 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
12214 &&
12215 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12216 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
12217 {
12218 bool first_complex = (code0 == COMPLEX_TYPE);
12219 bool second_complex = (code1 == COMPLEX_TYPE);
12220 int none_complex = (!first_complex && !second_complex);
12221
12222 if (shorten || common || short_compare)
12223 {
12224 result_type = c_common_type (type0, type1);
12225 do_warn_double_promotion (result_type, type0, type1,
12226 "implicit conversion from %qT to %qT "
12227 "to match other operand of binary "
12228 "expression",
12229 location);
12230 if (result_type == error_mark_node)
12231 return error_mark_node;
12232 }
12233
12234 if (first_complex != second_complex
12235 && (code == PLUS_EXPR
12236 || code == MINUS_EXPR
12237 || code == MULT_EXPR
12238 || (code == TRUNC_DIV_EXPR && first_complex))
12239 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12240 && flag_signed_zeros)
12241 {
12242 /* An operation on mixed real/complex operands must be
12243 handled specially, but the language-independent code can
12244 more easily optimize the plain complex arithmetic if
12245 -fno-signed-zeros. */
12246 tree real_type = TREE_TYPE (result_type);
12247 tree real, imag;
12248 if (type0 != orig_type0 || type1 != orig_type1)
12249 {
12250 gcc_assert (may_need_excess_precision && common);
12251 semantic_result_type = c_common_type (orig_type0, orig_type1);
12252 }
12253 if (first_complex)
12254 {
12255 if (TREE_TYPE (op0) != result_type)
12256 op0 = convert_and_check (location, result_type, op0);
12257 if (TREE_TYPE (op1) != real_type)
12258 op1 = convert_and_check (location, real_type, op1);
12259 }
12260 else
12261 {
12262 if (TREE_TYPE (op0) != real_type)
12263 op0 = convert_and_check (location, real_type, op0);
12264 if (TREE_TYPE (op1) != result_type)
12265 op1 = convert_and_check (location, result_type, op1);
12266 }
12267 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12268 return error_mark_node;
12269 if (first_complex)
12270 {
12271 op0 = save_expr (op0);
12272 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12273 op0, true);
12274 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12275 op0, true);
12276 switch (code)
12277 {
12278 case MULT_EXPR:
12279 case TRUNC_DIV_EXPR:
12280 op1 = save_expr (op1);
12281 imag = build2 (resultcode, real_type, imag, op1);
12282 /* Fall through. */
12283 case PLUS_EXPR:
12284 case MINUS_EXPR:
12285 real = build2 (resultcode, real_type, real, op1);
12286 break;
12287 default:
12288 gcc_unreachable();
12289 }
12290 }
12291 else
12292 {
12293 op1 = save_expr (op1);
12294 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12295 op1, true);
12296 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12297 op1, true);
12298 switch (code)
12299 {
12300 case MULT_EXPR:
12301 op0 = save_expr (op0);
12302 imag = build2 (resultcode, real_type, op0, imag);
12303 /* Fall through. */
12304 case PLUS_EXPR:
12305 real = build2 (resultcode, real_type, op0, real);
12306 break;
12307 case MINUS_EXPR:
12308 real = build2 (resultcode, real_type, op0, real);
12309 imag = build1 (NEGATE_EXPR, real_type, imag);
12310 break;
12311 default:
12312 gcc_unreachable();
12313 }
12314 }
12315 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12316 goto return_build_binary_op;
12317 }
12318
12319 /* For certain operations (which identify themselves by shorten != 0)
12320 if both args were extended from the same smaller type,
12321 do the arithmetic in that type and then extend.
12322
12323 shorten !=0 and !=1 indicates a bitwise operation.
12324 For them, this optimization is safe only if
12325 both args are zero-extended or both are sign-extended.
12326 Otherwise, we might change the result.
12327 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12328 but calculated in (unsigned short) it would be (unsigned short)-1. */
12329
12330 if (shorten && none_complex)
12331 {
12332 final_type = result_type;
12333 result_type = shorten_binary_op (result_type, op0, op1,
12334 shorten == -1);
12335 }
12336
12337 /* Shifts can be shortened if shifting right. */
12338
12339 if (short_shift)
12340 {
12341 int unsigned_arg;
12342 tree arg0 = get_narrower (op0, &unsigned_arg);
12343
12344 final_type = result_type;
12345
12346 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12347 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12348
12349 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12350 && tree_int_cst_sgn (op1) > 0
12351 /* We can shorten only if the shift count is less than the
12352 number of bits in the smaller type size. */
12353 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12354 /* We cannot drop an unsigned shift after sign-extension. */
12355 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12356 {
12357 /* Do an unsigned shift if the operand was zero-extended. */
12358 result_type
12359 = c_common_signed_or_unsigned_type (unsigned_arg,
12360 TREE_TYPE (arg0));
12361 /* Convert value-to-be-shifted to that type. */
12362 if (TREE_TYPE (op0) != result_type)
12363 op0 = convert (result_type, op0);
12364 converted = 1;
12365 }
12366 }
12367
12368 /* Comparison operations are shortened too but differently.
12369 They identify themselves by setting short_compare = 1. */
12370
12371 if (short_compare)
12372 {
12373 /* Don't write &op0, etc., because that would prevent op0
12374 from being kept in a register.
12375 Instead, make copies of the our local variables and
12376 pass the copies by reference, then copy them back afterward. */
12377 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12378 enum tree_code xresultcode = resultcode;
12379 tree val
12380 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12381 &xresultcode);
12382
12383 if (val != NULL_TREE)
12384 {
12385 ret = val;
12386 goto return_build_binary_op;
12387 }
12388
12389 op0 = xop0, op1 = xop1;
12390 converted = 1;
12391 resultcode = xresultcode;
12392
12393 if (c_inhibit_evaluation_warnings == 0)
12394 {
12395 bool op0_maybe_const = true;
12396 bool op1_maybe_const = true;
12397 tree orig_op0_folded, orig_op1_folded;
12398
12399 if (in_late_binary_op)
12400 {
12401 orig_op0_folded = orig_op0;
12402 orig_op1_folded = orig_op1;
12403 }
12404 else
12405 {
12406 /* Fold for the sake of possible warnings, as in
12407 build_conditional_expr. This requires the
12408 "original" values to be folded, not just op0 and
12409 op1. */
12410 c_inhibit_evaluation_warnings++;
12411 op0 = c_fully_fold (op0, require_constant_value,
12412 &op0_maybe_const);
12413 op1 = c_fully_fold (op1, require_constant_value,
12414 &op1_maybe_const);
12415 c_inhibit_evaluation_warnings--;
12416 orig_op0_folded = c_fully_fold (orig_op0,
12417 require_constant_value,
12418 NULL);
12419 orig_op1_folded = c_fully_fold (orig_op1,
12420 require_constant_value,
12421 NULL);
12422 }
12423
12424 if (warn_sign_compare)
12425 warn_for_sign_compare (location, orig_op0_folded,
12426 orig_op1_folded, op0, op1,
12427 result_type, resultcode);
12428 if (!in_late_binary_op && !int_operands)
12429 {
12430 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12431 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12432 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12433 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12434 }
12435 }
12436 }
12437 }
12438
12439 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12440 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12441 Then the expression will be built.
12442 It will be given type FINAL_TYPE if that is nonzero;
12443 otherwise, it will be given type RESULT_TYPE. */
12444
12445 if (!result_type)
12446 {
12447 /* Favor showing any expression locations that are available. */
12448 op_location_t oploc (location, UNKNOWN_LOCATION);
12449 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12450 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12451 return error_mark_node;
12452 }
12453
12454 if (build_type == NULL_TREE)
12455 {
12456 build_type = result_type;
12457 if ((type0 != orig_type0 || type1 != orig_type1)
12458 && !boolean_op)
12459 {
12460 gcc_assert (may_need_excess_precision && common);
12461 semantic_result_type = c_common_type (orig_type0, orig_type1);
12462 }
12463 }
12464
12465 if (!converted)
12466 {
12467 op0 = ep_convert_and_check (location, result_type, op0,
12468 semantic_result_type);
12469 op1 = ep_convert_and_check (location, result_type, op1,
12470 semantic_result_type);
12471
12472 /* This can happen if one operand has a vector type, and the other
12473 has a different type. */
12474 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12475 return error_mark_node;
12476 }
12477
12478 if (sanitize_flags_p ((SANITIZE_SHIFT
12479 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12480 && current_function_decl != NULL_TREE
12481 && (doing_div_or_mod || doing_shift)
12482 && !require_constant_value)
12483 {
12484 /* OP0 and/or OP1 might have side-effects. */
12485 op0 = save_expr (op0);
12486 op1 = save_expr (op1);
12487 op0 = c_fully_fold (op0, false, NULL);
12488 op1 = c_fully_fold (op1, false, NULL);
12489 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12490 | SANITIZE_FLOAT_DIVIDE))))
12491 instrument_expr = ubsan_instrument_division (location, op0, op1);
12492 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12493 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12494 }
12495
12496 /* Treat expressions in initializers specially as they can't trap. */
12497 if (int_const_or_overflow)
12498 ret = (require_constant_value
12499 ? fold_build2_initializer_loc (location, resultcode, build_type,
12500 op0, op1)
12501 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12502 else
12503 ret = build2 (resultcode, build_type, op0, op1);
12504 if (final_type != NULL_TREE)
12505 ret = convert (final_type, ret);
12506
12507 return_build_binary_op:
12508 gcc_assert (ret != error_mark_node);
12509 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12510 ret = (int_operands
12511 ? note_integer_operands (ret)
12512 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12513 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12514 && !in_late_binary_op)
12515 ret = note_integer_operands (ret);
12516 protected_set_expr_location (ret, location);
12517
12518 if (instrument_expr != NULL)
12519 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12520 instrument_expr, ret);
12521
12522 if (semantic_result_type)
12523 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12524 semantic_result_type, ret);
12525
12526 return ret;
12527 }
12528
12529
12530 /* Convert EXPR to be a truth-value, validating its type for this
12531 purpose. LOCATION is the source location for the expression. */
12532
12533 tree
12534 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12535 {
12536 bool int_const, int_operands;
12537
12538 switch (TREE_CODE (TREE_TYPE (expr)))
12539 {
12540 case ARRAY_TYPE:
12541 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12542 return error_mark_node;
12543
12544 case RECORD_TYPE:
12545 error_at (location, "used struct type value where scalar is required");
12546 return error_mark_node;
12547
12548 case UNION_TYPE:
12549 error_at (location, "used union type value where scalar is required");
12550 return error_mark_node;
12551
12552 case VOID_TYPE:
12553 error_at (location, "void value not ignored as it ought to be");
12554 return error_mark_node;
12555
12556 case POINTER_TYPE:
12557 if (reject_gcc_builtin (expr))
12558 return error_mark_node;
12559 break;
12560
12561 case FUNCTION_TYPE:
12562 gcc_unreachable ();
12563
12564 case VECTOR_TYPE:
12565 error_at (location, "used vector type where scalar is required");
12566 return error_mark_node;
12567
12568 default:
12569 break;
12570 }
12571
12572 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12573 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12574 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12575 {
12576 expr = remove_c_maybe_const_expr (expr);
12577 expr = build2 (NE_EXPR, integer_type_node, expr,
12578 convert (TREE_TYPE (expr), integer_zero_node));
12579 expr = note_integer_operands (expr);
12580 }
12581 else
12582 /* ??? Should we also give an error for vectors rather than leaving
12583 those to give errors later? */
12584 expr = c_common_truthvalue_conversion (location, expr);
12585
12586 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12587 {
12588 if (TREE_OVERFLOW (expr))
12589 return expr;
12590 else
12591 return note_integer_operands (expr);
12592 }
12593 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12594 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12595 return expr;
12596 }
12597 \f
12598
12599 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12600 required. */
12601
12602 tree
12603 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12604 {
12605 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12606 {
12607 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12608 /* Executing a compound literal inside a function reinitializes
12609 it. */
12610 if (!TREE_STATIC (decl))
12611 *se = true;
12612 return decl;
12613 }
12614 else
12615 return expr;
12616 }
12617 \f
12618 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12619 statement. LOC is the location of the construct. */
12620
12621 tree
12622 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12623 tree clauses)
12624 {
12625 body = c_end_compound_stmt (loc, body, true);
12626
12627 tree stmt = make_node (code);
12628 TREE_TYPE (stmt) = void_type_node;
12629 OMP_BODY (stmt) = body;
12630 OMP_CLAUSES (stmt) = clauses;
12631 SET_EXPR_LOCATION (stmt, loc);
12632
12633 return add_stmt (stmt);
12634 }
12635
12636 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12637 statement. LOC is the location of the OACC_DATA. */
12638
12639 tree
12640 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12641 {
12642 tree stmt;
12643
12644 block = c_end_compound_stmt (loc, block, true);
12645
12646 stmt = make_node (OACC_DATA);
12647 TREE_TYPE (stmt) = void_type_node;
12648 OACC_DATA_CLAUSES (stmt) = clauses;
12649 OACC_DATA_BODY (stmt) = block;
12650 SET_EXPR_LOCATION (stmt, loc);
12651
12652 return add_stmt (stmt);
12653 }
12654
12655 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12656 statement. LOC is the location of the OACC_HOST_DATA. */
12657
12658 tree
12659 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12660 {
12661 tree stmt;
12662
12663 block = c_end_compound_stmt (loc, block, true);
12664
12665 stmt = make_node (OACC_HOST_DATA);
12666 TREE_TYPE (stmt) = void_type_node;
12667 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12668 OACC_HOST_DATA_BODY (stmt) = block;
12669 SET_EXPR_LOCATION (stmt, loc);
12670
12671 return add_stmt (stmt);
12672 }
12673
12674 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12675
12676 tree
12677 c_begin_omp_parallel (void)
12678 {
12679 tree block;
12680
12681 keep_next_level ();
12682 block = c_begin_compound_stmt (true);
12683
12684 return block;
12685 }
12686
12687 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12688 statement. LOC is the location of the OMP_PARALLEL. */
12689
12690 tree
12691 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12692 {
12693 tree stmt;
12694
12695 block = c_end_compound_stmt (loc, block, true);
12696
12697 stmt = make_node (OMP_PARALLEL);
12698 TREE_TYPE (stmt) = void_type_node;
12699 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12700 OMP_PARALLEL_BODY (stmt) = block;
12701 SET_EXPR_LOCATION (stmt, loc);
12702
12703 return add_stmt (stmt);
12704 }
12705
12706 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12707
12708 tree
12709 c_begin_omp_task (void)
12710 {
12711 tree block;
12712
12713 keep_next_level ();
12714 block = c_begin_compound_stmt (true);
12715
12716 return block;
12717 }
12718
12719 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12720 statement. LOC is the location of the #pragma. */
12721
12722 tree
12723 c_finish_omp_task (location_t loc, tree clauses, tree block)
12724 {
12725 tree stmt;
12726
12727 block = c_end_compound_stmt (loc, block, true);
12728
12729 stmt = make_node (OMP_TASK);
12730 TREE_TYPE (stmt) = void_type_node;
12731 OMP_TASK_CLAUSES (stmt) = clauses;
12732 OMP_TASK_BODY (stmt) = block;
12733 SET_EXPR_LOCATION (stmt, loc);
12734
12735 return add_stmt (stmt);
12736 }
12737
12738 /* Generate GOMP_cancel call for #pragma omp cancel. */
12739
12740 void
12741 c_finish_omp_cancel (location_t loc, tree clauses)
12742 {
12743 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12744 int mask = 0;
12745 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12746 mask = 1;
12747 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12748 mask = 2;
12749 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12750 mask = 4;
12751 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12752 mask = 8;
12753 else
12754 {
12755 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12756 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12757 "clauses");
12758 return;
12759 }
12760 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12761 if (ifc != NULL_TREE)
12762 {
12763 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
12764 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
12765 error_at (OMP_CLAUSE_LOCATION (ifc),
12766 "expected %<cancel%> %<if%> clause modifier");
12767 else
12768 {
12769 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
12770 if (ifc2 != NULL_TREE)
12771 {
12772 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
12773 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
12774 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
12775 error_at (OMP_CLAUSE_LOCATION (ifc2),
12776 "expected %<cancel%> %<if%> clause modifier");
12777 }
12778 }
12779
12780 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12781 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12782 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12783 build_zero_cst (type));
12784 }
12785 else
12786 ifc = boolean_true_node;
12787 tree stmt = build_call_expr_loc (loc, fn, 2,
12788 build_int_cst (integer_type_node, mask),
12789 ifc);
12790 add_stmt (stmt);
12791 }
12792
12793 /* Generate GOMP_cancellation_point call for
12794 #pragma omp cancellation point. */
12795
12796 void
12797 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12798 {
12799 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12800 int mask = 0;
12801 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12802 mask = 1;
12803 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12804 mask = 2;
12805 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12806 mask = 4;
12807 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12808 mask = 8;
12809 else
12810 {
12811 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12812 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12813 "clauses");
12814 return;
12815 }
12816 tree stmt = build_call_expr_loc (loc, fn, 1,
12817 build_int_cst (integer_type_node, mask));
12818 add_stmt (stmt);
12819 }
12820
12821 /* Helper function for handle_omp_array_sections. Called recursively
12822 to handle multiple array-section-subscripts. C is the clause,
12823 T current expression (initially OMP_CLAUSE_DECL), which is either
12824 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12825 expression if specified, TREE_VALUE length expression if specified,
12826 TREE_CHAIN is what it has been specified after, or some decl.
12827 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12828 set to true if any of the array-section-subscript could have length
12829 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12830 first array-section-subscript which is known not to have length
12831 of one. Given say:
12832 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12833 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12834 all are or may have length of 1, array-section-subscript [:2] is the
12835 first one known not to have length 1. For array-section-subscript
12836 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12837 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12838 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12839 case though, as some lengths could be zero. */
12840
12841 static tree
12842 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12843 bool &maybe_zero_len, unsigned int &first_non_one,
12844 enum c_omp_region_type ort)
12845 {
12846 tree ret, low_bound, length, type;
12847 if (TREE_CODE (t) != TREE_LIST)
12848 {
12849 if (error_operand_p (t))
12850 return error_mark_node;
12851 ret = t;
12852 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12853 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12854 {
12855 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12856 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12857 return error_mark_node;
12858 }
12859 if (TREE_CODE (t) == COMPONENT_REF
12860 && ort == C_ORT_OMP
12861 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12862 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12863 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12864 {
12865 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12866 {
12867 error_at (OMP_CLAUSE_LOCATION (c),
12868 "bit-field %qE in %qs clause",
12869 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12870 return error_mark_node;
12871 }
12872 while (TREE_CODE (t) == COMPONENT_REF)
12873 {
12874 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12875 {
12876 error_at (OMP_CLAUSE_LOCATION (c),
12877 "%qE is a member of a union", t);
12878 return error_mark_node;
12879 }
12880 t = TREE_OPERAND (t, 0);
12881 }
12882 }
12883 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12884 {
12885 if (DECL_P (t))
12886 error_at (OMP_CLAUSE_LOCATION (c),
12887 "%qD is not a variable in %qs clause", t,
12888 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12889 else
12890 error_at (OMP_CLAUSE_LOCATION (c),
12891 "%qE is not a variable in %qs clause", t,
12892 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12893 return error_mark_node;
12894 }
12895 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12896 && TYPE_ATOMIC (TREE_TYPE (t)))
12897 {
12898 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12899 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12900 return error_mark_node;
12901 }
12902 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12903 && VAR_P (t)
12904 && DECL_THREAD_LOCAL_P (t))
12905 {
12906 error_at (OMP_CLAUSE_LOCATION (c),
12907 "%qD is threadprivate variable in %qs clause", t,
12908 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12909 return error_mark_node;
12910 }
12911 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12912 && TYPE_ATOMIC (TREE_TYPE (t))
12913 && POINTER_TYPE_P (TREE_TYPE (t)))
12914 {
12915 /* If the array section is pointer based and the pointer
12916 itself is _Atomic qualified, we need to atomically load
12917 the pointer. */
12918 c_expr expr;
12919 memset (&expr, 0, sizeof (expr));
12920 expr.value = ret;
12921 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12922 expr, false, false);
12923 ret = expr.value;
12924 }
12925 return ret;
12926 }
12927
12928 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12929 maybe_zero_len, first_non_one, ort);
12930 if (ret == error_mark_node || ret == NULL_TREE)
12931 return ret;
12932
12933 type = TREE_TYPE (ret);
12934 low_bound = TREE_PURPOSE (t);
12935 length = TREE_VALUE (t);
12936
12937 if (low_bound == error_mark_node || length == error_mark_node)
12938 return error_mark_node;
12939
12940 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12941 {
12942 error_at (OMP_CLAUSE_LOCATION (c),
12943 "low bound %qE of array section does not have integral type",
12944 low_bound);
12945 return error_mark_node;
12946 }
12947 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12948 {
12949 error_at (OMP_CLAUSE_LOCATION (c),
12950 "length %qE of array section does not have integral type",
12951 length);
12952 return error_mark_node;
12953 }
12954 if (low_bound
12955 && TREE_CODE (low_bound) == INTEGER_CST
12956 && TYPE_PRECISION (TREE_TYPE (low_bound))
12957 > TYPE_PRECISION (sizetype))
12958 low_bound = fold_convert (sizetype, low_bound);
12959 if (length
12960 && TREE_CODE (length) == INTEGER_CST
12961 && TYPE_PRECISION (TREE_TYPE (length))
12962 > TYPE_PRECISION (sizetype))
12963 length = fold_convert (sizetype, length);
12964 if (low_bound == NULL_TREE)
12965 low_bound = integer_zero_node;
12966
12967 if (length != NULL_TREE)
12968 {
12969 if (!integer_nonzerop (length))
12970 {
12971 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12972 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12973 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
12974 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
12975 {
12976 if (integer_zerop (length))
12977 {
12978 error_at (OMP_CLAUSE_LOCATION (c),
12979 "zero length array section in %qs clause",
12980 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12981 return error_mark_node;
12982 }
12983 }
12984 else
12985 maybe_zero_len = true;
12986 }
12987 if (first_non_one == types.length ()
12988 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12989 first_non_one++;
12990 }
12991 if (TREE_CODE (type) == ARRAY_TYPE)
12992 {
12993 if (length == NULL_TREE
12994 && (TYPE_DOMAIN (type) == NULL_TREE
12995 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12996 {
12997 error_at (OMP_CLAUSE_LOCATION (c),
12998 "for unknown bound array type length expression must "
12999 "be specified");
13000 return error_mark_node;
13001 }
13002 if (TREE_CODE (low_bound) == INTEGER_CST
13003 && tree_int_cst_sgn (low_bound) == -1)
13004 {
13005 error_at (OMP_CLAUSE_LOCATION (c),
13006 "negative low bound in array section in %qs clause",
13007 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13008 return error_mark_node;
13009 }
13010 if (length != NULL_TREE
13011 && TREE_CODE (length) == INTEGER_CST
13012 && tree_int_cst_sgn (length) == -1)
13013 {
13014 error_at (OMP_CLAUSE_LOCATION (c),
13015 "negative length in array section in %qs clause",
13016 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13017 return error_mark_node;
13018 }
13019 if (TYPE_DOMAIN (type)
13020 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13021 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13022 == INTEGER_CST)
13023 {
13024 tree size
13025 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13026 size = size_binop (PLUS_EXPR, size, size_one_node);
13027 if (TREE_CODE (low_bound) == INTEGER_CST)
13028 {
13029 if (tree_int_cst_lt (size, low_bound))
13030 {
13031 error_at (OMP_CLAUSE_LOCATION (c),
13032 "low bound %qE above array section size "
13033 "in %qs clause", low_bound,
13034 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13035 return error_mark_node;
13036 }
13037 if (tree_int_cst_equal (size, low_bound))
13038 {
13039 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13040 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13041 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13042 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13043 {
13044 error_at (OMP_CLAUSE_LOCATION (c),
13045 "zero length array section in %qs clause",
13046 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13047 return error_mark_node;
13048 }
13049 maybe_zero_len = true;
13050 }
13051 else if (length == NULL_TREE
13052 && first_non_one == types.length ()
13053 && tree_int_cst_equal
13054 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13055 low_bound))
13056 first_non_one++;
13057 }
13058 else if (length == NULL_TREE)
13059 {
13060 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13061 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13062 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13063 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13064 maybe_zero_len = true;
13065 if (first_non_one == types.length ())
13066 first_non_one++;
13067 }
13068 if (length && TREE_CODE (length) == INTEGER_CST)
13069 {
13070 if (tree_int_cst_lt (size, length))
13071 {
13072 error_at (OMP_CLAUSE_LOCATION (c),
13073 "length %qE above array section size "
13074 "in %qs clause", length,
13075 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13076 return error_mark_node;
13077 }
13078 if (TREE_CODE (low_bound) == INTEGER_CST)
13079 {
13080 tree lbpluslen
13081 = size_binop (PLUS_EXPR,
13082 fold_convert (sizetype, low_bound),
13083 fold_convert (sizetype, length));
13084 if (TREE_CODE (lbpluslen) == INTEGER_CST
13085 && tree_int_cst_lt (size, lbpluslen))
13086 {
13087 error_at (OMP_CLAUSE_LOCATION (c),
13088 "high bound %qE above array section size "
13089 "in %qs clause", lbpluslen,
13090 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13091 return error_mark_node;
13092 }
13093 }
13094 }
13095 }
13096 else if (length == NULL_TREE)
13097 {
13098 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13099 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13100 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13101 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13102 maybe_zero_len = true;
13103 if (first_non_one == types.length ())
13104 first_non_one++;
13105 }
13106
13107 /* For [lb:] we will need to evaluate lb more than once. */
13108 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13109 {
13110 tree lb = save_expr (low_bound);
13111 if (lb != low_bound)
13112 {
13113 TREE_PURPOSE (t) = lb;
13114 low_bound = lb;
13115 }
13116 }
13117 }
13118 else if (TREE_CODE (type) == POINTER_TYPE)
13119 {
13120 if (length == NULL_TREE)
13121 {
13122 error_at (OMP_CLAUSE_LOCATION (c),
13123 "for pointer type length expression must be specified");
13124 return error_mark_node;
13125 }
13126 if (length != NULL_TREE
13127 && TREE_CODE (length) == INTEGER_CST
13128 && tree_int_cst_sgn (length) == -1)
13129 {
13130 error_at (OMP_CLAUSE_LOCATION (c),
13131 "negative length in array section in %qs clause",
13132 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13133 return error_mark_node;
13134 }
13135 /* If there is a pointer type anywhere but in the very first
13136 array-section-subscript, the array section can't be contiguous. */
13137 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13138 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13139 {
13140 error_at (OMP_CLAUSE_LOCATION (c),
13141 "array section is not contiguous in %qs clause",
13142 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13143 return error_mark_node;
13144 }
13145 }
13146 else
13147 {
13148 error_at (OMP_CLAUSE_LOCATION (c),
13149 "%qE does not have pointer or array type", ret);
13150 return error_mark_node;
13151 }
13152 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13153 types.safe_push (TREE_TYPE (ret));
13154 /* We will need to evaluate lb more than once. */
13155 tree lb = save_expr (low_bound);
13156 if (lb != low_bound)
13157 {
13158 TREE_PURPOSE (t) = lb;
13159 low_bound = lb;
13160 }
13161 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13162 return ret;
13163 }
13164
13165 /* Handle array sections for clause C. */
13166
13167 static bool
13168 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13169 {
13170 bool maybe_zero_len = false;
13171 unsigned int first_non_one = 0;
13172 auto_vec<tree, 10> types;
13173 tree *tp = &OMP_CLAUSE_DECL (c);
13174 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13175 && TREE_CODE (*tp) == TREE_LIST
13176 && TREE_PURPOSE (*tp)
13177 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13178 tp = &TREE_VALUE (*tp);
13179 tree first = handle_omp_array_sections_1 (c, *tp, types,
13180 maybe_zero_len, first_non_one,
13181 ort);
13182 if (first == error_mark_node)
13183 return true;
13184 if (first == NULL_TREE)
13185 return false;
13186 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13187 {
13188 tree t = *tp;
13189 tree tem = NULL_TREE;
13190 /* Need to evaluate side effects in the length expressions
13191 if any. */
13192 while (TREE_CODE (t) == TREE_LIST)
13193 {
13194 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13195 {
13196 if (tem == NULL_TREE)
13197 tem = TREE_VALUE (t);
13198 else
13199 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13200 TREE_VALUE (t), tem);
13201 }
13202 t = TREE_CHAIN (t);
13203 }
13204 if (tem)
13205 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13206 first = c_fully_fold (first, false, NULL, true);
13207 *tp = first;
13208 }
13209 else
13210 {
13211 unsigned int num = types.length (), i;
13212 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13213 tree condition = NULL_TREE;
13214
13215 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13216 maybe_zero_len = true;
13217
13218 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13219 t = TREE_CHAIN (t))
13220 {
13221 tree low_bound = TREE_PURPOSE (t);
13222 tree length = TREE_VALUE (t);
13223
13224 i--;
13225 if (low_bound
13226 && TREE_CODE (low_bound) == INTEGER_CST
13227 && TYPE_PRECISION (TREE_TYPE (low_bound))
13228 > TYPE_PRECISION (sizetype))
13229 low_bound = fold_convert (sizetype, low_bound);
13230 if (length
13231 && TREE_CODE (length) == INTEGER_CST
13232 && TYPE_PRECISION (TREE_TYPE (length))
13233 > TYPE_PRECISION (sizetype))
13234 length = fold_convert (sizetype, length);
13235 if (low_bound == NULL_TREE)
13236 low_bound = integer_zero_node;
13237 if (!maybe_zero_len && i > first_non_one)
13238 {
13239 if (integer_nonzerop (low_bound))
13240 goto do_warn_noncontiguous;
13241 if (length != NULL_TREE
13242 && TREE_CODE (length) == INTEGER_CST
13243 && TYPE_DOMAIN (types[i])
13244 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13245 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13246 == INTEGER_CST)
13247 {
13248 tree size;
13249 size = size_binop (PLUS_EXPR,
13250 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13251 size_one_node);
13252 if (!tree_int_cst_equal (length, size))
13253 {
13254 do_warn_noncontiguous:
13255 error_at (OMP_CLAUSE_LOCATION (c),
13256 "array section is not contiguous in %qs "
13257 "clause",
13258 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13259 return true;
13260 }
13261 }
13262 if (length != NULL_TREE
13263 && TREE_SIDE_EFFECTS (length))
13264 {
13265 if (side_effects == NULL_TREE)
13266 side_effects = length;
13267 else
13268 side_effects = build2 (COMPOUND_EXPR,
13269 TREE_TYPE (side_effects),
13270 length, side_effects);
13271 }
13272 }
13273 else
13274 {
13275 tree l;
13276
13277 if (i > first_non_one
13278 && ((length && integer_nonzerop (length))
13279 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13280 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13281 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13282 continue;
13283 if (length)
13284 l = fold_convert (sizetype, length);
13285 else
13286 {
13287 l = size_binop (PLUS_EXPR,
13288 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13289 size_one_node);
13290 l = size_binop (MINUS_EXPR, l,
13291 fold_convert (sizetype, low_bound));
13292 }
13293 if (i > first_non_one)
13294 {
13295 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13296 size_zero_node);
13297 if (condition == NULL_TREE)
13298 condition = l;
13299 else
13300 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13301 l, condition);
13302 }
13303 else if (size == NULL_TREE)
13304 {
13305 size = size_in_bytes (TREE_TYPE (types[i]));
13306 tree eltype = TREE_TYPE (types[num - 1]);
13307 while (TREE_CODE (eltype) == ARRAY_TYPE)
13308 eltype = TREE_TYPE (eltype);
13309 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13310 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13311 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13312 {
13313 if (integer_zerop (size)
13314 || integer_zerop (size_in_bytes (eltype)))
13315 {
13316 error_at (OMP_CLAUSE_LOCATION (c),
13317 "zero length array section in %qs clause",
13318 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13319 return error_mark_node;
13320 }
13321 size = size_binop (EXACT_DIV_EXPR, size,
13322 size_in_bytes (eltype));
13323 }
13324 size = size_binop (MULT_EXPR, size, l);
13325 if (condition)
13326 size = fold_build3 (COND_EXPR, sizetype, condition,
13327 size, size_zero_node);
13328 }
13329 else
13330 size = size_binop (MULT_EXPR, size, l);
13331 }
13332 }
13333 if (side_effects)
13334 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13335 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13336 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13337 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13338 {
13339 size = size_binop (MINUS_EXPR, size, size_one_node);
13340 size = c_fully_fold (size, false, NULL);
13341 size = save_expr (size);
13342 tree index_type = build_index_type (size);
13343 tree eltype = TREE_TYPE (first);
13344 while (TREE_CODE (eltype) == ARRAY_TYPE)
13345 eltype = TREE_TYPE (eltype);
13346 tree type = build_array_type (eltype, index_type);
13347 tree ptype = build_pointer_type (eltype);
13348 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13349 t = build_fold_addr_expr (t);
13350 tree t2 = build_fold_addr_expr (first);
13351 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13352 ptrdiff_type_node, t2);
13353 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13354 ptrdiff_type_node, t2,
13355 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13356 ptrdiff_type_node, t));
13357 t2 = c_fully_fold (t2, false, NULL);
13358 if (tree_fits_shwi_p (t2))
13359 t = build2 (MEM_REF, type, t,
13360 build_int_cst (ptype, tree_to_shwi (t2)));
13361 else
13362 {
13363 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13364 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13365 TREE_TYPE (t), t, t2);
13366 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13367 }
13368 OMP_CLAUSE_DECL (c) = t;
13369 return false;
13370 }
13371 first = c_fully_fold (first, false, NULL);
13372 OMP_CLAUSE_DECL (c) = first;
13373 if (size)
13374 size = c_fully_fold (size, false, NULL);
13375 OMP_CLAUSE_SIZE (c) = size;
13376 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13377 || (TREE_CODE (t) == COMPONENT_REF
13378 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13379 return false;
13380 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13381 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
13382 switch (OMP_CLAUSE_MAP_KIND (c))
13383 {
13384 case GOMP_MAP_ALLOC:
13385 case GOMP_MAP_TO:
13386 case GOMP_MAP_FROM:
13387 case GOMP_MAP_TOFROM:
13388 case GOMP_MAP_ALWAYS_TO:
13389 case GOMP_MAP_ALWAYS_FROM:
13390 case GOMP_MAP_ALWAYS_TOFROM:
13391 case GOMP_MAP_RELEASE:
13392 case GOMP_MAP_DELETE:
13393 case GOMP_MAP_FORCE_TO:
13394 case GOMP_MAP_FORCE_FROM:
13395 case GOMP_MAP_FORCE_TOFROM:
13396 case GOMP_MAP_FORCE_PRESENT:
13397 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13398 break;
13399 default:
13400 break;
13401 }
13402 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13403 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13404 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13405 else if (TREE_CODE (t) == COMPONENT_REF)
13406 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
13407 else
13408 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13409 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13410 && !c_mark_addressable (t))
13411 return false;
13412 OMP_CLAUSE_DECL (c2) = t;
13413 t = build_fold_addr_expr (first);
13414 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13415 tree ptr = OMP_CLAUSE_DECL (c2);
13416 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13417 ptr = build_fold_addr_expr (ptr);
13418 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13419 ptrdiff_type_node, t,
13420 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13421 ptrdiff_type_node, ptr));
13422 t = c_fully_fold (t, false, NULL);
13423 OMP_CLAUSE_SIZE (c2) = t;
13424 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13425 OMP_CLAUSE_CHAIN (c) = c2;
13426 }
13427 return false;
13428 }
13429
13430 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13431 an inline call. But, remap
13432 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13433 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13434
13435 static tree
13436 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13437 tree decl, tree placeholder)
13438 {
13439 copy_body_data id;
13440 hash_map<tree, tree> decl_map;
13441
13442 decl_map.put (omp_decl1, placeholder);
13443 decl_map.put (omp_decl2, decl);
13444 memset (&id, 0, sizeof (id));
13445 id.src_fn = DECL_CONTEXT (omp_decl1);
13446 id.dst_fn = current_function_decl;
13447 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13448 id.decl_map = &decl_map;
13449
13450 id.copy_decl = copy_decl_no_change;
13451 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13452 id.transform_new_cfg = true;
13453 id.transform_return_to_modify = false;
13454 id.transform_lang_insert_block = NULL;
13455 id.eh_lp_nr = 0;
13456 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13457 return stmt;
13458 }
13459
13460 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13461 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13462
13463 static tree
13464 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13465 {
13466 if (*tp == (tree) data)
13467 return *tp;
13468 return NULL_TREE;
13469 }
13470
13471 /* Similarly, but also walk aggregate fields. */
13472
13473 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13474
13475 static tree
13476 c_find_omp_var_r (tree *tp, int *, void *data)
13477 {
13478 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13479 return *tp;
13480 if (RECORD_OR_UNION_TYPE_P (*tp))
13481 {
13482 tree field;
13483 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13484
13485 for (field = TYPE_FIELDS (*tp); field;
13486 field = DECL_CHAIN (field))
13487 if (TREE_CODE (field) == FIELD_DECL)
13488 {
13489 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13490 c_find_omp_var_r, data, pset);
13491 if (ret)
13492 return ret;
13493 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13494 if (ret)
13495 return ret;
13496 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13497 pset);
13498 if (ret)
13499 return ret;
13500 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13501 if (ret)
13502 return ret;
13503 }
13504 }
13505 else if (INTEGRAL_TYPE_P (*tp))
13506 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13507 ((struct c_find_omp_var_s *) data)->pset);
13508 return NULL_TREE;
13509 }
13510
13511 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13512 and clauses containing them should be removed. */
13513
13514 static bool
13515 c_omp_finish_iterators (tree iter)
13516 {
13517 bool ret = false;
13518 for (tree it = iter; it; it = TREE_CHAIN (it))
13519 {
13520 tree var = TREE_VEC_ELT (it, 0);
13521 tree begin = TREE_VEC_ELT (it, 1);
13522 tree end = TREE_VEC_ELT (it, 2);
13523 tree step = TREE_VEC_ELT (it, 3);
13524 tree orig_step;
13525 tree type = TREE_TYPE (var);
13526 location_t loc = DECL_SOURCE_LOCATION (var);
13527 if (type == error_mark_node)
13528 {
13529 ret = true;
13530 continue;
13531 }
13532 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13533 {
13534 error_at (loc, "iterator %qD has neither integral nor pointer type",
13535 var);
13536 ret = true;
13537 continue;
13538 }
13539 else if (TYPE_ATOMIC (type))
13540 {
13541 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13542 ret = true;
13543 continue;
13544 }
13545 else if (TYPE_READONLY (type))
13546 {
13547 error_at (loc, "iterator %qD has const qualified type", var);
13548 ret = true;
13549 continue;
13550 }
13551 else if (step == error_mark_node
13552 || TREE_TYPE (step) == error_mark_node)
13553 {
13554 ret = true;
13555 continue;
13556 }
13557 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13558 {
13559 error_at (EXPR_LOC_OR_LOC (step, loc),
13560 "iterator step with non-integral type");
13561 ret = true;
13562 continue;
13563 }
13564 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13565 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13566 orig_step = save_expr (c_fully_fold (step, false, NULL));
13567 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13568 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13569 if (POINTER_TYPE_P (type))
13570 {
13571 begin = save_expr (begin);
13572 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13573 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13574 fold_convert (sizetype, step),
13575 fold_convert (sizetype, begin));
13576 step = fold_convert (ssizetype, step);
13577 }
13578 if (integer_zerop (step))
13579 {
13580 error_at (loc, "iterator %qD has zero step", var);
13581 ret = true;
13582 continue;
13583 }
13584
13585 if (begin == error_mark_node
13586 || end == error_mark_node
13587 || step == error_mark_node
13588 || orig_step == error_mark_node)
13589 {
13590 ret = true;
13591 continue;
13592 }
13593 hash_set<tree> pset;
13594 tree it2;
13595 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13596 {
13597 tree var2 = TREE_VEC_ELT (it2, 0);
13598 tree begin2 = TREE_VEC_ELT (it2, 1);
13599 tree end2 = TREE_VEC_ELT (it2, 2);
13600 tree step2 = TREE_VEC_ELT (it2, 3);
13601 tree type2 = TREE_TYPE (var2);
13602 location_t loc2 = DECL_SOURCE_LOCATION (var2);
13603 struct c_find_omp_var_s data = { var, &pset };
13604 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13605 {
13606 error_at (loc2,
13607 "type of iterator %qD refers to outer iterator %qD",
13608 var2, var);
13609 break;
13610 }
13611 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13612 {
13613 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13614 "begin expression refers to outer iterator %qD", var);
13615 break;
13616 }
13617 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13618 {
13619 error_at (EXPR_LOC_OR_LOC (end2, loc2),
13620 "end expression refers to outer iterator %qD", var);
13621 break;
13622 }
13623 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13624 {
13625 error_at (EXPR_LOC_OR_LOC (step2, loc2),
13626 "step expression refers to outer iterator %qD", var);
13627 break;
13628 }
13629 }
13630 if (it2)
13631 {
13632 ret = true;
13633 continue;
13634 }
13635 TREE_VEC_ELT (it, 1) = begin;
13636 TREE_VEC_ELT (it, 2) = end;
13637 TREE_VEC_ELT (it, 3) = step;
13638 TREE_VEC_ELT (it, 4) = orig_step;
13639 }
13640 return ret;
13641 }
13642
13643 /* For all elements of CLAUSES, validate them against their constraints.
13644 Remove any elements from the list that are invalid. */
13645
13646 tree
13647 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13648 {
13649 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13650 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
13651 tree c, t, type, *pc;
13652 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13653 bool branch_seen = false;
13654 bool copyprivate_seen = false;
13655 bool linear_variable_step_check = false;
13656 tree *nowait_clause = NULL;
13657 bool ordered_seen = false;
13658 tree schedule_clause = NULL_TREE;
13659 bool oacc_async = false;
13660 tree last_iterators = NULL_TREE;
13661 bool last_iterators_remove = false;
13662 tree *nogroup_seen = NULL;
13663 bool reduction_seen = false;
13664
13665 bitmap_obstack_initialize (NULL);
13666 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13667 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13668 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13669 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13670 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
13671 bitmap_initialize (&map_head, &bitmap_default_obstack);
13672 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13673 /* If ort == C_ORT_OMP used as nontemporal_head instead. */
13674 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13675
13676 if (ort & C_ORT_ACC)
13677 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13678 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13679 {
13680 oacc_async = true;
13681 break;
13682 }
13683
13684 for (pc = &clauses, c = clauses; c ; c = *pc)
13685 {
13686 bool remove = false;
13687 bool need_complete = false;
13688 bool need_implicitly_determined = false;
13689
13690 switch (OMP_CLAUSE_CODE (c))
13691 {
13692 case OMP_CLAUSE_SHARED:
13693 need_implicitly_determined = true;
13694 goto check_dup_generic;
13695
13696 case OMP_CLAUSE_PRIVATE:
13697 need_complete = true;
13698 need_implicitly_determined = true;
13699 goto check_dup_generic;
13700
13701 case OMP_CLAUSE_REDUCTION:
13702 reduction_seen = true;
13703 /* FALLTHRU */
13704 case OMP_CLAUSE_IN_REDUCTION:
13705 case OMP_CLAUSE_TASK_REDUCTION:
13706 need_implicitly_determined = true;
13707 t = OMP_CLAUSE_DECL (c);
13708 if (TREE_CODE (t) == TREE_LIST)
13709 {
13710 if (handle_omp_array_sections (c, ort))
13711 {
13712 remove = true;
13713 break;
13714 }
13715
13716 t = OMP_CLAUSE_DECL (c);
13717 }
13718 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13719 if (t == error_mark_node)
13720 {
13721 remove = true;
13722 break;
13723 }
13724 if (oacc_async)
13725 c_mark_addressable (t);
13726 type = TREE_TYPE (t);
13727 if (TREE_CODE (t) == MEM_REF)
13728 type = TREE_TYPE (type);
13729 if (TREE_CODE (type) == ARRAY_TYPE)
13730 {
13731 tree oatype = type;
13732 gcc_assert (TREE_CODE (t) != MEM_REF);
13733 while (TREE_CODE (type) == ARRAY_TYPE)
13734 type = TREE_TYPE (type);
13735 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13736 {
13737 error_at (OMP_CLAUSE_LOCATION (c),
13738 "%qD in %<reduction%> clause is a zero size array",
13739 t);
13740 remove = true;
13741 break;
13742 }
13743 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13744 TYPE_SIZE_UNIT (type));
13745 if (integer_zerop (size))
13746 {
13747 error_at (OMP_CLAUSE_LOCATION (c),
13748 "%qD in %<reduction%> clause is a zero size array",
13749 t);
13750 remove = true;
13751 break;
13752 }
13753 size = size_binop (MINUS_EXPR, size, size_one_node);
13754 size = save_expr (size);
13755 tree index_type = build_index_type (size);
13756 tree atype = build_array_type (type, index_type);
13757 tree ptype = build_pointer_type (type);
13758 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13759 t = build_fold_addr_expr (t);
13760 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13761 OMP_CLAUSE_DECL (c) = t;
13762 }
13763 if (TYPE_ATOMIC (type))
13764 {
13765 error_at (OMP_CLAUSE_LOCATION (c),
13766 "%<_Atomic%> %qE in %<reduction%> clause", t);
13767 remove = true;
13768 break;
13769 }
13770 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13771 || OMP_CLAUSE_REDUCTION_TASK (c))
13772 {
13773 /* Disallow zero sized or potentially zero sized task
13774 reductions. */
13775 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13776 {
13777 error_at (OMP_CLAUSE_LOCATION (c),
13778 "zero sized type %qT in %qs clause", type,
13779 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13780 remove = true;
13781 break;
13782 }
13783 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
13784 {
13785 error_at (OMP_CLAUSE_LOCATION (c),
13786 "variable sized type %qT in %qs clause", type,
13787 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13788 remove = true;
13789 break;
13790 }
13791 }
13792 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13793 && (FLOAT_TYPE_P (type)
13794 || TREE_CODE (type) == COMPLEX_TYPE))
13795 {
13796 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13797 const char *r_name = NULL;
13798
13799 switch (r_code)
13800 {
13801 case PLUS_EXPR:
13802 case MULT_EXPR:
13803 case MINUS_EXPR:
13804 break;
13805 case MIN_EXPR:
13806 if (TREE_CODE (type) == COMPLEX_TYPE)
13807 r_name = "min";
13808 break;
13809 case MAX_EXPR:
13810 if (TREE_CODE (type) == COMPLEX_TYPE)
13811 r_name = "max";
13812 break;
13813 case BIT_AND_EXPR:
13814 r_name = "&";
13815 break;
13816 case BIT_XOR_EXPR:
13817 r_name = "^";
13818 break;
13819 case BIT_IOR_EXPR:
13820 r_name = "|";
13821 break;
13822 case TRUTH_ANDIF_EXPR:
13823 if (FLOAT_TYPE_P (type))
13824 r_name = "&&";
13825 break;
13826 case TRUTH_ORIF_EXPR:
13827 if (FLOAT_TYPE_P (type))
13828 r_name = "||";
13829 break;
13830 default:
13831 gcc_unreachable ();
13832 }
13833 if (r_name)
13834 {
13835 error_at (OMP_CLAUSE_LOCATION (c),
13836 "%qE has invalid type for %<reduction(%s)%>",
13837 t, r_name);
13838 remove = true;
13839 break;
13840 }
13841 }
13842 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13843 {
13844 error_at (OMP_CLAUSE_LOCATION (c),
13845 "user defined reduction not found for %qE", t);
13846 remove = true;
13847 break;
13848 }
13849 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13850 {
13851 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13852 type = TYPE_MAIN_VARIANT (type);
13853 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13854 VAR_DECL, NULL_TREE, type);
13855 tree decl_placeholder = NULL_TREE;
13856 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13857 DECL_ARTIFICIAL (placeholder) = 1;
13858 DECL_IGNORED_P (placeholder) = 1;
13859 if (TREE_CODE (t) == MEM_REF)
13860 {
13861 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13862 VAR_DECL, NULL_TREE, type);
13863 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13864 DECL_ARTIFICIAL (decl_placeholder) = 1;
13865 DECL_IGNORED_P (decl_placeholder) = 1;
13866 }
13867 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13868 c_mark_addressable (placeholder);
13869 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13870 c_mark_addressable (decl_placeholder ? decl_placeholder
13871 : OMP_CLAUSE_DECL (c));
13872 OMP_CLAUSE_REDUCTION_MERGE (c)
13873 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13874 TREE_VEC_ELT (list, 0),
13875 TREE_VEC_ELT (list, 1),
13876 decl_placeholder ? decl_placeholder
13877 : OMP_CLAUSE_DECL (c), placeholder);
13878 OMP_CLAUSE_REDUCTION_MERGE (c)
13879 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13880 void_type_node, NULL_TREE,
13881 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13882 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13883 if (TREE_VEC_LENGTH (list) == 6)
13884 {
13885 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13886 c_mark_addressable (decl_placeholder ? decl_placeholder
13887 : OMP_CLAUSE_DECL (c));
13888 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13889 c_mark_addressable (placeholder);
13890 tree init = TREE_VEC_ELT (list, 5);
13891 if (init == error_mark_node)
13892 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13893 OMP_CLAUSE_REDUCTION_INIT (c)
13894 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13895 TREE_VEC_ELT (list, 3),
13896 decl_placeholder ? decl_placeholder
13897 : OMP_CLAUSE_DECL (c), placeholder);
13898 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13899 {
13900 tree v = decl_placeholder ? decl_placeholder : t;
13901 OMP_CLAUSE_REDUCTION_INIT (c)
13902 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13903 OMP_CLAUSE_REDUCTION_INIT (c));
13904 }
13905 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13906 c_find_omp_placeholder_r,
13907 placeholder, NULL))
13908 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13909 }
13910 else
13911 {
13912 tree init;
13913 tree v = decl_placeholder ? decl_placeholder : t;
13914 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13915 init = build_constructor (TREE_TYPE (v), NULL);
13916 else
13917 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13918 OMP_CLAUSE_REDUCTION_INIT (c)
13919 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13920 }
13921 OMP_CLAUSE_REDUCTION_INIT (c)
13922 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13923 void_type_node, NULL_TREE,
13924 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13925 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13926 }
13927 if (TREE_CODE (t) == MEM_REF)
13928 {
13929 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13930 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13931 != INTEGER_CST)
13932 {
13933 sorry ("variable length element type in array "
13934 "%<reduction%> clause");
13935 remove = true;
13936 break;
13937 }
13938 t = TREE_OPERAND (t, 0);
13939 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13940 t = TREE_OPERAND (t, 0);
13941 if (TREE_CODE (t) == ADDR_EXPR)
13942 t = TREE_OPERAND (t, 0);
13943 }
13944 goto check_dup_generic_t;
13945
13946 case OMP_CLAUSE_COPYPRIVATE:
13947 copyprivate_seen = true;
13948 if (nowait_clause)
13949 {
13950 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13951 "%<nowait%> clause must not be used together "
13952 "with %<copyprivate%>");
13953 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13954 nowait_clause = NULL;
13955 }
13956 goto check_dup_generic;
13957
13958 case OMP_CLAUSE_COPYIN:
13959 t = OMP_CLAUSE_DECL (c);
13960 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13961 {
13962 error_at (OMP_CLAUSE_LOCATION (c),
13963 "%qE must be %<threadprivate%> for %<copyin%>", t);
13964 remove = true;
13965 break;
13966 }
13967 goto check_dup_generic;
13968
13969 case OMP_CLAUSE_LINEAR:
13970 if (ort != C_ORT_OMP_DECLARE_SIMD)
13971 need_implicitly_determined = true;
13972 t = OMP_CLAUSE_DECL (c);
13973 if (ort != C_ORT_OMP_DECLARE_SIMD
13974 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13975 {
13976 error_at (OMP_CLAUSE_LOCATION (c),
13977 "modifier should not be specified in %<linear%> "
13978 "clause on %<simd%> or %<for%> constructs");
13979 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13980 }
13981 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13982 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13983 {
13984 error_at (OMP_CLAUSE_LOCATION (c),
13985 "linear clause applied to non-integral non-pointer "
13986 "variable with type %qT", TREE_TYPE (t));
13987 remove = true;
13988 break;
13989 }
13990 if (TYPE_ATOMIC (TREE_TYPE (t)))
13991 {
13992 error_at (OMP_CLAUSE_LOCATION (c),
13993 "%<_Atomic%> %qD in %<linear%> clause", t);
13994 remove = true;
13995 break;
13996 }
13997 if (ort == C_ORT_OMP_DECLARE_SIMD)
13998 {
13999 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14000 if (TREE_CODE (s) == PARM_DECL)
14001 {
14002 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14003 /* map_head bitmap is used as uniform_head if
14004 declare_simd. */
14005 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14006 linear_variable_step_check = true;
14007 goto check_dup_generic;
14008 }
14009 if (TREE_CODE (s) != INTEGER_CST)
14010 {
14011 error_at (OMP_CLAUSE_LOCATION (c),
14012 "%<linear%> clause step %qE is neither constant "
14013 "nor a parameter", s);
14014 remove = true;
14015 break;
14016 }
14017 }
14018 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14019 {
14020 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14021 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14022 OMP_CLAUSE_DECL (c), s);
14023 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14024 sizetype, fold_convert (sizetype, s),
14025 fold_convert
14026 (sizetype, OMP_CLAUSE_DECL (c)));
14027 if (s == error_mark_node)
14028 s = size_one_node;
14029 OMP_CLAUSE_LINEAR_STEP (c) = s;
14030 }
14031 else
14032 OMP_CLAUSE_LINEAR_STEP (c)
14033 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14034 goto check_dup_generic;
14035
14036 check_dup_generic:
14037 t = OMP_CLAUSE_DECL (c);
14038 check_dup_generic_t:
14039 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14040 {
14041 error_at (OMP_CLAUSE_LOCATION (c),
14042 "%qE is not a variable in clause %qs", t,
14043 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14044 remove = true;
14045 }
14046 else if (ort == C_ORT_ACC
14047 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14048 {
14049 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14050 {
14051 error_at (OMP_CLAUSE_LOCATION (c),
14052 "%qD appears more than once in reduction clauses",
14053 t);
14054 remove = true;
14055 }
14056 else
14057 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14058 }
14059 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14060 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14061 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14062 {
14063 error_at (OMP_CLAUSE_LOCATION (c),
14064 "%qE appears more than once in data clauses", t);
14065 remove = true;
14066 }
14067 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14068 && bitmap_bit_p (&map_head, DECL_UID (t)))
14069 {
14070 if (ort == C_ORT_ACC)
14071 error_at (OMP_CLAUSE_LOCATION (c),
14072 "%qD appears more than once in data clauses", t);
14073 else
14074 error_at (OMP_CLAUSE_LOCATION (c),
14075 "%qD appears both in data and map clauses", t);
14076 remove = true;
14077 }
14078 else
14079 bitmap_set_bit (&generic_head, DECL_UID (t));
14080 break;
14081
14082 case OMP_CLAUSE_FIRSTPRIVATE:
14083 t = OMP_CLAUSE_DECL (c);
14084 need_complete = true;
14085 need_implicitly_determined = true;
14086 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14087 {
14088 error_at (OMP_CLAUSE_LOCATION (c),
14089 "%qE is not a variable in clause %<firstprivate%>", t);
14090 remove = true;
14091 }
14092 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14093 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14094 {
14095 error_at (OMP_CLAUSE_LOCATION (c),
14096 "%qE appears more than once in data clauses", t);
14097 remove = true;
14098 }
14099 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14100 {
14101 if (ort == C_ORT_ACC)
14102 error_at (OMP_CLAUSE_LOCATION (c),
14103 "%qD appears more than once in data clauses", t);
14104 else
14105 error_at (OMP_CLAUSE_LOCATION (c),
14106 "%qD appears both in data and map clauses", t);
14107 remove = true;
14108 }
14109 else
14110 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14111 break;
14112
14113 case OMP_CLAUSE_LASTPRIVATE:
14114 t = OMP_CLAUSE_DECL (c);
14115 need_complete = true;
14116 need_implicitly_determined = true;
14117 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14118 {
14119 error_at (OMP_CLAUSE_LOCATION (c),
14120 "%qE is not a variable in clause %<lastprivate%>", t);
14121 remove = true;
14122 }
14123 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14124 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14125 {
14126 error_at (OMP_CLAUSE_LOCATION (c),
14127 "%qE appears more than once in data clauses", t);
14128 remove = true;
14129 }
14130 else
14131 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14132 break;
14133
14134 case OMP_CLAUSE_ALIGNED:
14135 t = OMP_CLAUSE_DECL (c);
14136 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14137 {
14138 error_at (OMP_CLAUSE_LOCATION (c),
14139 "%qE is not a variable in %<aligned%> clause", t);
14140 remove = true;
14141 }
14142 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14143 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14144 {
14145 error_at (OMP_CLAUSE_LOCATION (c),
14146 "%qE in %<aligned%> clause is neither a pointer nor "
14147 "an array", t);
14148 remove = true;
14149 }
14150 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14151 {
14152 error_at (OMP_CLAUSE_LOCATION (c),
14153 "%<_Atomic%> %qD in %<aligned%> clause", t);
14154 remove = true;
14155 break;
14156 }
14157 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14158 {
14159 error_at (OMP_CLAUSE_LOCATION (c),
14160 "%qE appears more than once in %<aligned%> clauses",
14161 t);
14162 remove = true;
14163 }
14164 else
14165 bitmap_set_bit (&aligned_head, DECL_UID (t));
14166 break;
14167
14168 case OMP_CLAUSE_NONTEMPORAL:
14169 t = OMP_CLAUSE_DECL (c);
14170 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14171 {
14172 error_at (OMP_CLAUSE_LOCATION (c),
14173 "%qE is not a variable in %<nontemporal%> clause", t);
14174 remove = true;
14175 }
14176 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14177 {
14178 error_at (OMP_CLAUSE_LOCATION (c),
14179 "%qE appears more than once in %<nontemporal%> "
14180 "clauses", t);
14181 remove = true;
14182 }
14183 else
14184 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14185 break;
14186
14187 case OMP_CLAUSE_DEPEND:
14188 t = OMP_CLAUSE_DECL (c);
14189 if (t == NULL_TREE)
14190 {
14191 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14192 == OMP_CLAUSE_DEPEND_SOURCE);
14193 break;
14194 }
14195 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14196 {
14197 gcc_assert (TREE_CODE (t) == TREE_LIST);
14198 for (; t; t = TREE_CHAIN (t))
14199 {
14200 tree decl = TREE_VALUE (t);
14201 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14202 {
14203 tree offset = TREE_PURPOSE (t);
14204 bool neg = wi::neg_p (wi::to_wide (offset));
14205 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14206 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14207 neg ? MINUS_EXPR : PLUS_EXPR,
14208 decl, offset);
14209 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14210 sizetype,
14211 fold_convert (sizetype, t2),
14212 fold_convert (sizetype, decl));
14213 if (t2 == error_mark_node)
14214 {
14215 remove = true;
14216 break;
14217 }
14218 TREE_PURPOSE (t) = t2;
14219 }
14220 }
14221 break;
14222 }
14223 if (TREE_CODE (t) == TREE_LIST
14224 && TREE_PURPOSE (t)
14225 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14226 {
14227 if (TREE_PURPOSE (t) != last_iterators)
14228 last_iterators_remove
14229 = c_omp_finish_iterators (TREE_PURPOSE (t));
14230 last_iterators = TREE_PURPOSE (t);
14231 t = TREE_VALUE (t);
14232 if (last_iterators_remove)
14233 t = error_mark_node;
14234 }
14235 else
14236 last_iterators = NULL_TREE;
14237 if (TREE_CODE (t) == TREE_LIST)
14238 {
14239 if (handle_omp_array_sections (c, ort))
14240 remove = true;
14241 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14242 {
14243 error_at (OMP_CLAUSE_LOCATION (c),
14244 "%<depend%> clause with %<depobj%> dependence "
14245 "type on array section");
14246 remove = true;
14247 }
14248 break;
14249 }
14250 if (t == error_mark_node)
14251 remove = true;
14252 else if (!lvalue_p (t))
14253 {
14254 error_at (OMP_CLAUSE_LOCATION (c),
14255 "%qE is not lvalue expression nor array section in "
14256 "%<depend%> clause", t);
14257 remove = true;
14258 }
14259 else if (TREE_CODE (t) == COMPONENT_REF
14260 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14261 {
14262 error_at (OMP_CLAUSE_LOCATION (c),
14263 "bit-field %qE in %qs clause", t, "depend");
14264 remove = true;
14265 }
14266 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14267 {
14268 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14269 {
14270 error_at (OMP_CLAUSE_LOCATION (c),
14271 "%qE does not have %<omp_depend_t%> type in "
14272 "%<depend%> clause with %<depobj%> dependence "
14273 "type", t);
14274 remove = true;
14275 }
14276 }
14277 else if (c_omp_depend_t_p (TREE_TYPE (t)))
14278 {
14279 error_at (OMP_CLAUSE_LOCATION (c),
14280 "%qE should not have %<omp_depend_t%> type in "
14281 "%<depend%> clause with dependence type other than "
14282 "%<depobj%>", t);
14283 remove = true;
14284 }
14285 if (!remove)
14286 {
14287 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14288 t, false);
14289 if (addr == error_mark_node)
14290 remove = true;
14291 else
14292 {
14293 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14294 RO_UNARY_STAR);
14295 if (t == error_mark_node)
14296 remove = true;
14297 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14298 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14299 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14300 == TREE_VEC))
14301 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14302 else
14303 OMP_CLAUSE_DECL (c) = t;
14304 }
14305 }
14306 break;
14307
14308 case OMP_CLAUSE_MAP:
14309 case OMP_CLAUSE_TO:
14310 case OMP_CLAUSE_FROM:
14311 case OMP_CLAUSE__CACHE_:
14312 t = OMP_CLAUSE_DECL (c);
14313 if (TREE_CODE (t) == TREE_LIST)
14314 {
14315 if (handle_omp_array_sections (c, ort))
14316 remove = true;
14317 else
14318 {
14319 t = OMP_CLAUSE_DECL (c);
14320 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14321 {
14322 error_at (OMP_CLAUSE_LOCATION (c),
14323 "array section does not have mappable type "
14324 "in %qs clause",
14325 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14326 remove = true;
14327 }
14328 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14329 {
14330 error_at (OMP_CLAUSE_LOCATION (c),
14331 "%<_Atomic%> %qE in %qs clause", t,
14332 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14333 remove = true;
14334 }
14335 while (TREE_CODE (t) == ARRAY_REF)
14336 t = TREE_OPERAND (t, 0);
14337 if (TREE_CODE (t) == COMPONENT_REF
14338 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14339 {
14340 while (TREE_CODE (t) == COMPONENT_REF)
14341 t = TREE_OPERAND (t, 0);
14342 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14343 break;
14344 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14345 {
14346 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14347 error_at (OMP_CLAUSE_LOCATION (c),
14348 "%qD appears more than once in motion "
14349 "clauses", t);
14350 else if (ort == C_ORT_ACC)
14351 error_at (OMP_CLAUSE_LOCATION (c),
14352 "%qD appears more than once in data "
14353 "clauses", t);
14354 else
14355 error_at (OMP_CLAUSE_LOCATION (c),
14356 "%qD appears more than once in map "
14357 "clauses", t);
14358 remove = true;
14359 }
14360 else
14361 {
14362 bitmap_set_bit (&map_head, DECL_UID (t));
14363 bitmap_set_bit (&map_field_head, DECL_UID (t));
14364 }
14365 }
14366 }
14367 break;
14368 }
14369 if (t == error_mark_node)
14370 {
14371 remove = true;
14372 break;
14373 }
14374 if (TREE_CODE (t) == COMPONENT_REF
14375 && (ort & C_ORT_OMP)
14376 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14377 {
14378 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14379 {
14380 error_at (OMP_CLAUSE_LOCATION (c),
14381 "bit-field %qE in %qs clause",
14382 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14383 remove = true;
14384 }
14385 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14386 {
14387 error_at (OMP_CLAUSE_LOCATION (c),
14388 "%qE does not have a mappable type in %qs clause",
14389 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14390 remove = true;
14391 }
14392 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14393 {
14394 error_at (OMP_CLAUSE_LOCATION (c),
14395 "%<_Atomic%> %qE in %qs clause", t,
14396 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14397 remove = true;
14398 }
14399 while (TREE_CODE (t) == COMPONENT_REF)
14400 {
14401 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14402 == UNION_TYPE)
14403 {
14404 error_at (OMP_CLAUSE_LOCATION (c),
14405 "%qE is a member of a union", t);
14406 remove = true;
14407 break;
14408 }
14409 t = TREE_OPERAND (t, 0);
14410 }
14411 if (remove)
14412 break;
14413 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14414 {
14415 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14416 break;
14417 }
14418 }
14419 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14420 {
14421 error_at (OMP_CLAUSE_LOCATION (c),
14422 "%qE is not a variable in %qs clause", t,
14423 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14424 remove = true;
14425 }
14426 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14427 {
14428 error_at (OMP_CLAUSE_LOCATION (c),
14429 "%qD is threadprivate variable in %qs clause", t,
14430 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14431 remove = true;
14432 }
14433 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14434 || (OMP_CLAUSE_MAP_KIND (c)
14435 != GOMP_MAP_FIRSTPRIVATE_POINTER))
14436 && !c_mark_addressable (t))
14437 remove = true;
14438 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14439 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
14440 || (OMP_CLAUSE_MAP_KIND (c)
14441 == GOMP_MAP_FIRSTPRIVATE_POINTER)
14442 || (OMP_CLAUSE_MAP_KIND (c)
14443 == GOMP_MAP_FORCE_DEVICEPTR)))
14444 && t == OMP_CLAUSE_DECL (c)
14445 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14446 {
14447 error_at (OMP_CLAUSE_LOCATION (c),
14448 "%qD does not have a mappable type in %qs clause", t,
14449 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14450 remove = true;
14451 }
14452 else if (TREE_TYPE (t) == error_mark_node)
14453 remove = true;
14454 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14455 {
14456 error_at (OMP_CLAUSE_LOCATION (c),
14457 "%<_Atomic%> %qE in %qs clause", t,
14458 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14459 remove = true;
14460 }
14461 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14462 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14463 {
14464 if (bitmap_bit_p (&generic_head, DECL_UID (t))
14465 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14466 {
14467 error_at (OMP_CLAUSE_LOCATION (c),
14468 "%qD appears more than once in data clauses", t);
14469 remove = true;
14470 }
14471 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14472 {
14473 if (ort == C_ORT_ACC)
14474 error_at (OMP_CLAUSE_LOCATION (c),
14475 "%qD appears more than once in data clauses", t);
14476 else
14477 error_at (OMP_CLAUSE_LOCATION (c),
14478 "%qD appears both in data and map clauses", t);
14479 remove = true;
14480 }
14481 else
14482 bitmap_set_bit (&generic_head, DECL_UID (t));
14483 }
14484 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14485 {
14486 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14487 error_at (OMP_CLAUSE_LOCATION (c),
14488 "%qD appears more than once in motion clauses", t);
14489 else if (ort == C_ORT_ACC)
14490 error_at (OMP_CLAUSE_LOCATION (c),
14491 "%qD appears more than once in data clauses", t);
14492 else
14493 error_at (OMP_CLAUSE_LOCATION (c),
14494 "%qD appears more than once in map clauses", t);
14495 remove = true;
14496 }
14497 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14498 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14499 {
14500 if (ort == C_ORT_ACC)
14501 error_at (OMP_CLAUSE_LOCATION (c),
14502 "%qD appears more than once in data clauses", t);
14503 else
14504 error_at (OMP_CLAUSE_LOCATION (c),
14505 "%qD appears both in data and map clauses", t);
14506 remove = true;
14507 }
14508 else
14509 {
14510 bitmap_set_bit (&map_head, DECL_UID (t));
14511 if (t != OMP_CLAUSE_DECL (c)
14512 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14513 bitmap_set_bit (&map_field_head, DECL_UID (t));
14514 }
14515 break;
14516
14517 case OMP_CLAUSE_TO_DECLARE:
14518 case OMP_CLAUSE_LINK:
14519 t = OMP_CLAUSE_DECL (c);
14520 if (TREE_CODE (t) == FUNCTION_DECL
14521 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14522 ;
14523 else if (!VAR_P (t))
14524 {
14525 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14526 error_at (OMP_CLAUSE_LOCATION (c),
14527 "%qE is neither a variable nor a function name in "
14528 "clause %qs", t,
14529 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14530 else
14531 error_at (OMP_CLAUSE_LOCATION (c),
14532 "%qE is not a variable in clause %qs", t,
14533 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14534 remove = true;
14535 }
14536 else if (DECL_THREAD_LOCAL_P (t))
14537 {
14538 error_at (OMP_CLAUSE_LOCATION (c),
14539 "%qD is threadprivate variable in %qs clause", t,
14540 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14541 remove = true;
14542 }
14543 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14544 {
14545 error_at (OMP_CLAUSE_LOCATION (c),
14546 "%qD does not have a mappable type in %qs clause", t,
14547 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14548 remove = true;
14549 }
14550 if (remove)
14551 break;
14552 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
14553 {
14554 error_at (OMP_CLAUSE_LOCATION (c),
14555 "%qE appears more than once on the same "
14556 "%<declare target%> directive", t);
14557 remove = true;
14558 }
14559 else
14560 bitmap_set_bit (&generic_head, DECL_UID (t));
14561 break;
14562
14563 case OMP_CLAUSE_UNIFORM:
14564 t = OMP_CLAUSE_DECL (c);
14565 if (TREE_CODE (t) != PARM_DECL)
14566 {
14567 if (DECL_P (t))
14568 error_at (OMP_CLAUSE_LOCATION (c),
14569 "%qD is not an argument in %<uniform%> clause", t);
14570 else
14571 error_at (OMP_CLAUSE_LOCATION (c),
14572 "%qE is not an argument in %<uniform%> clause", t);
14573 remove = true;
14574 break;
14575 }
14576 /* map_head bitmap is used as uniform_head if declare_simd. */
14577 bitmap_set_bit (&map_head, DECL_UID (t));
14578 goto check_dup_generic;
14579
14580 case OMP_CLAUSE_IS_DEVICE_PTR:
14581 case OMP_CLAUSE_USE_DEVICE_PTR:
14582 t = OMP_CLAUSE_DECL (c);
14583 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
14584 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14585 {
14586 error_at (OMP_CLAUSE_LOCATION (c),
14587 "%qs variable is neither a pointer nor an array",
14588 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14589 remove = true;
14590 }
14591 goto check_dup_generic;
14592
14593 case OMP_CLAUSE_NOWAIT:
14594 if (copyprivate_seen)
14595 {
14596 error_at (OMP_CLAUSE_LOCATION (c),
14597 "%<nowait%> clause must not be used together "
14598 "with %<copyprivate%>");
14599 remove = true;
14600 break;
14601 }
14602 nowait_clause = pc;
14603 pc = &OMP_CLAUSE_CHAIN (c);
14604 continue;
14605
14606 case OMP_CLAUSE_IF:
14607 case OMP_CLAUSE_NUM_THREADS:
14608 case OMP_CLAUSE_NUM_TEAMS:
14609 case OMP_CLAUSE_THREAD_LIMIT:
14610 case OMP_CLAUSE_DEFAULT:
14611 case OMP_CLAUSE_UNTIED:
14612 case OMP_CLAUSE_COLLAPSE:
14613 case OMP_CLAUSE_FINAL:
14614 case OMP_CLAUSE_MERGEABLE:
14615 case OMP_CLAUSE_DEVICE:
14616 case OMP_CLAUSE_DIST_SCHEDULE:
14617 case OMP_CLAUSE_PARALLEL:
14618 case OMP_CLAUSE_FOR:
14619 case OMP_CLAUSE_SECTIONS:
14620 case OMP_CLAUSE_TASKGROUP:
14621 case OMP_CLAUSE_PROC_BIND:
14622 case OMP_CLAUSE_PRIORITY:
14623 case OMP_CLAUSE_GRAINSIZE:
14624 case OMP_CLAUSE_NUM_TASKS:
14625 case OMP_CLAUSE_THREADS:
14626 case OMP_CLAUSE_SIMD:
14627 case OMP_CLAUSE_HINT:
14628 case OMP_CLAUSE_DEFAULTMAP:
14629 case OMP_CLAUSE_NUM_GANGS:
14630 case OMP_CLAUSE_NUM_WORKERS:
14631 case OMP_CLAUSE_VECTOR_LENGTH:
14632 case OMP_CLAUSE_ASYNC:
14633 case OMP_CLAUSE_WAIT:
14634 case OMP_CLAUSE_AUTO:
14635 case OMP_CLAUSE_INDEPENDENT:
14636 case OMP_CLAUSE_SEQ:
14637 case OMP_CLAUSE_GANG:
14638 case OMP_CLAUSE_WORKER:
14639 case OMP_CLAUSE_VECTOR:
14640 case OMP_CLAUSE_TILE:
14641 case OMP_CLAUSE_IF_PRESENT:
14642 case OMP_CLAUSE_FINALIZE:
14643 pc = &OMP_CLAUSE_CHAIN (c);
14644 continue;
14645
14646 case OMP_CLAUSE_NOGROUP:
14647 nogroup_seen = pc;
14648 pc = &OMP_CLAUSE_CHAIN (c);
14649 continue;
14650
14651 case OMP_CLAUSE_SCHEDULE:
14652 schedule_clause = c;
14653 pc = &OMP_CLAUSE_CHAIN (c);
14654 continue;
14655
14656 case OMP_CLAUSE_ORDERED:
14657 ordered_seen = true;
14658 pc = &OMP_CLAUSE_CHAIN (c);
14659 continue;
14660
14661 case OMP_CLAUSE_SAFELEN:
14662 safelen = c;
14663 pc = &OMP_CLAUSE_CHAIN (c);
14664 continue;
14665 case OMP_CLAUSE_SIMDLEN:
14666 simdlen = c;
14667 pc = &OMP_CLAUSE_CHAIN (c);
14668 continue;
14669
14670 case OMP_CLAUSE_INBRANCH:
14671 case OMP_CLAUSE_NOTINBRANCH:
14672 if (branch_seen)
14673 {
14674 error_at (OMP_CLAUSE_LOCATION (c),
14675 "%<inbranch%> clause is incompatible with "
14676 "%<notinbranch%>");
14677 remove = true;
14678 break;
14679 }
14680 branch_seen = true;
14681 pc = &OMP_CLAUSE_CHAIN (c);
14682 continue;
14683
14684 default:
14685 gcc_unreachable ();
14686 }
14687
14688 if (!remove)
14689 {
14690 t = OMP_CLAUSE_DECL (c);
14691
14692 if (need_complete)
14693 {
14694 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14695 if (t == error_mark_node)
14696 remove = true;
14697 }
14698
14699 if (need_implicitly_determined)
14700 {
14701 const char *share_name = NULL;
14702
14703 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14704 share_name = "threadprivate";
14705 else switch (c_omp_predetermined_sharing (t))
14706 {
14707 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
14708 break;
14709 case OMP_CLAUSE_DEFAULT_SHARED:
14710 share_name = "shared";
14711 break;
14712 case OMP_CLAUSE_DEFAULT_PRIVATE:
14713 share_name = "private";
14714 break;
14715 default:
14716 gcc_unreachable ();
14717 }
14718 if (share_name)
14719 {
14720 error_at (OMP_CLAUSE_LOCATION (c),
14721 "%qE is predetermined %qs for %qs",
14722 t, share_name,
14723 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14724 remove = true;
14725 }
14726 else if (TREE_READONLY (t)
14727 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
14728 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
14729 {
14730 error_at (OMP_CLAUSE_LOCATION (c),
14731 "%<const%> qualified %qE may appear only in "
14732 "%<shared%> or %<firstprivate%> clauses", t);
14733 remove = true;
14734 }
14735 }
14736 }
14737
14738 if (remove)
14739 *pc = OMP_CLAUSE_CHAIN (c);
14740 else
14741 pc = &OMP_CLAUSE_CHAIN (c);
14742 }
14743
14744 if (simdlen
14745 && safelen
14746 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
14747 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
14748 {
14749 error_at (OMP_CLAUSE_LOCATION (simdlen),
14750 "%<simdlen%> clause value is bigger than "
14751 "%<safelen%> clause value");
14752 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
14753 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
14754 }
14755
14756 if (ordered_seen
14757 && schedule_clause
14758 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14759 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14760 {
14761 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
14762 "%<nonmonotonic%> schedule modifier specified together "
14763 "with %<ordered%> clause");
14764 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14765 = (enum omp_clause_schedule_kind)
14766 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14767 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14768 }
14769
14770 if (linear_variable_step_check)
14771 for (pc = &clauses, c = clauses; c ; c = *pc)
14772 {
14773 bool remove = false;
14774 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14775 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
14776 && !bitmap_bit_p (&map_head,
14777 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
14778 {
14779 error_at (OMP_CLAUSE_LOCATION (c),
14780 "%<linear%> clause step is a parameter %qD not "
14781 "specified in %<uniform%> clause",
14782 OMP_CLAUSE_LINEAR_STEP (c));
14783 remove = true;
14784 }
14785
14786 if (remove)
14787 *pc = OMP_CLAUSE_CHAIN (c);
14788 else
14789 pc = &OMP_CLAUSE_CHAIN (c);
14790 }
14791
14792 if (nogroup_seen && reduction_seen)
14793 {
14794 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
14795 "%<nogroup%> clause must not be used together with "
14796 "%<reduction%> clause");
14797 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
14798 }
14799
14800 bitmap_obstack_release (NULL);
14801 return clauses;
14802 }
14803
14804 /* Return code to initialize DST with a copy constructor from SRC.
14805 C doesn't have copy constructors nor assignment operators, only for
14806 _Atomic vars we need to perform __atomic_load from src into a temporary
14807 followed by __atomic_store of the temporary to dst. */
14808
14809 tree
14810 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
14811 {
14812 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
14813 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14814
14815 location_t loc = OMP_CLAUSE_LOCATION (clause);
14816 tree type = TREE_TYPE (dst);
14817 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
14818 tree tmp = create_tmp_var (nonatomic_type);
14819 tree tmp_addr = build_fold_addr_expr (tmp);
14820 TREE_ADDRESSABLE (tmp) = 1;
14821 TREE_NO_WARNING (tmp) = 1;
14822 tree src_addr = build_fold_addr_expr (src);
14823 tree dst_addr = build_fold_addr_expr (dst);
14824 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14825 vec<tree, va_gc> *params;
14826 /* Expansion of a generic atomic load may require an addition
14827 element, so allocate enough to prevent a resize. */
14828 vec_alloc (params, 4);
14829
14830 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14831 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14832 params->quick_push (src_addr);
14833 params->quick_push (tmp_addr);
14834 params->quick_push (seq_cst);
14835 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14836
14837 vec_alloc (params, 4);
14838
14839 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14840 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14841 params->quick_push (dst_addr);
14842 params->quick_push (tmp_addr);
14843 params->quick_push (seq_cst);
14844 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14845 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14846 }
14847
14848 /* Create a transaction node. */
14849
14850 tree
14851 c_finish_transaction (location_t loc, tree block, int flags)
14852 {
14853 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14854 if (flags & TM_STMT_ATTR_OUTER)
14855 TRANSACTION_EXPR_OUTER (stmt) = 1;
14856 if (flags & TM_STMT_ATTR_RELAXED)
14857 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14858 return add_stmt (stmt);
14859 }
14860
14861 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14862 down to the element type of an array. If ORIG_QUAL_TYPE is not
14863 NULL, then it should be used as the qualified type
14864 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14865 preserve information about the typedef name from which an array
14866 type was derived). */
14867
14868 tree
14869 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14870 size_t orig_qual_indirect)
14871 {
14872 if (type == error_mark_node)
14873 return type;
14874
14875 if (TREE_CODE (type) == ARRAY_TYPE)
14876 {
14877 tree t;
14878 tree element_type = c_build_qualified_type (TREE_TYPE (type),
14879 type_quals, orig_qual_type,
14880 orig_qual_indirect - 1);
14881
14882 /* See if we already have an identically qualified type. */
14883 if (orig_qual_type && orig_qual_indirect == 0)
14884 t = orig_qual_type;
14885 else
14886 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14887 {
14888 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14889 && TYPE_NAME (t) == TYPE_NAME (type)
14890 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14891 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14892 TYPE_ATTRIBUTES (type)))
14893 break;
14894 }
14895 if (!t)
14896 {
14897 tree domain = TYPE_DOMAIN (type);
14898
14899 t = build_variant_type_copy (type);
14900 TREE_TYPE (t) = element_type;
14901
14902 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14903 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14904 SET_TYPE_STRUCTURAL_EQUALITY (t);
14905 else if (TYPE_CANONICAL (element_type) != element_type
14906 || (domain && TYPE_CANONICAL (domain) != domain))
14907 {
14908 tree unqualified_canon
14909 = build_array_type (TYPE_CANONICAL (element_type),
14910 domain? TYPE_CANONICAL (domain)
14911 : NULL_TREE);
14912 if (TYPE_REVERSE_STORAGE_ORDER (type))
14913 {
14914 unqualified_canon
14915 = build_distinct_type_copy (unqualified_canon);
14916 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14917 }
14918 TYPE_CANONICAL (t)
14919 = c_build_qualified_type (unqualified_canon, type_quals);
14920 }
14921 else
14922 TYPE_CANONICAL (t) = t;
14923 }
14924 return t;
14925 }
14926
14927 /* A restrict-qualified pointer type must be a pointer to object or
14928 incomplete type. Note that the use of POINTER_TYPE_P also allows
14929 REFERENCE_TYPEs, which is appropriate for C++. */
14930 if ((type_quals & TYPE_QUAL_RESTRICT)
14931 && (!POINTER_TYPE_P (type)
14932 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14933 {
14934 error ("invalid use of %<restrict%>");
14935 type_quals &= ~TYPE_QUAL_RESTRICT;
14936 }
14937
14938 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14939 ? orig_qual_type
14940 : build_qualified_type (type, type_quals));
14941 /* A variant type does not inherit the list of incomplete vars from the
14942 type main variant. */
14943 if (RECORD_OR_UNION_TYPE_P (var_type)
14944 && TYPE_MAIN_VARIANT (var_type) != var_type)
14945 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14946 return var_type;
14947 }
14948
14949 /* Build a VA_ARG_EXPR for the C parser. */
14950
14951 tree
14952 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14953 {
14954 if (error_operand_p (type))
14955 return error_mark_node;
14956 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14957 order because it takes the address of the expression. */
14958 else if (handled_component_p (expr)
14959 && reverse_storage_order_for_component_p (expr))
14960 {
14961 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14962 return error_mark_node;
14963 }
14964 else if (!COMPLETE_TYPE_P (type))
14965 {
14966 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14967 "type %qT", type);
14968 return error_mark_node;
14969 }
14970 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14971 warning_at (loc2, OPT_Wc___compat,
14972 "C++ requires promoted type, not enum type, in %<va_arg%>");
14973 return build_va_arg (loc2, expr, type);
14974 }
14975
14976 /* Return truthvalue of whether T1 is the same tree structure as T2.
14977 Return 1 if they are the same. Return false if they are different. */
14978
14979 bool
14980 c_tree_equal (tree t1, tree t2)
14981 {
14982 enum tree_code code1, code2;
14983
14984 if (t1 == t2)
14985 return true;
14986 if (!t1 || !t2)
14987 return false;
14988
14989 for (code1 = TREE_CODE (t1);
14990 CONVERT_EXPR_CODE_P (code1)
14991 || code1 == NON_LVALUE_EXPR;
14992 code1 = TREE_CODE (t1))
14993 t1 = TREE_OPERAND (t1, 0);
14994 for (code2 = TREE_CODE (t2);
14995 CONVERT_EXPR_CODE_P (code2)
14996 || code2 == NON_LVALUE_EXPR;
14997 code2 = TREE_CODE (t2))
14998 t2 = TREE_OPERAND (t2, 0);
14999
15000 /* They might have become equal now. */
15001 if (t1 == t2)
15002 return true;
15003
15004 if (code1 != code2)
15005 return false;
15006
15007 switch (code1)
15008 {
15009 case INTEGER_CST:
15010 return wi::to_wide (t1) == wi::to_wide (t2);
15011
15012 case REAL_CST:
15013 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15014
15015 case STRING_CST:
15016 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15017 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15018 TREE_STRING_LENGTH (t1));
15019
15020 case FIXED_CST:
15021 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15022 TREE_FIXED_CST (t2));
15023
15024 case COMPLEX_CST:
15025 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15026 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15027
15028 case VECTOR_CST:
15029 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15030
15031 case CONSTRUCTOR:
15032 /* We need to do this when determining whether or not two
15033 non-type pointer to member function template arguments
15034 are the same. */
15035 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15036 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15037 return false;
15038 {
15039 tree field, value;
15040 unsigned int i;
15041 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15042 {
15043 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15044 if (!c_tree_equal (field, elt2->index)
15045 || !c_tree_equal (value, elt2->value))
15046 return false;
15047 }
15048 }
15049 return true;
15050
15051 case TREE_LIST:
15052 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15053 return false;
15054 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15055 return false;
15056 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15057
15058 case SAVE_EXPR:
15059 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15060
15061 case CALL_EXPR:
15062 {
15063 tree arg1, arg2;
15064 call_expr_arg_iterator iter1, iter2;
15065 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15066 return false;
15067 for (arg1 = first_call_expr_arg (t1, &iter1),
15068 arg2 = first_call_expr_arg (t2, &iter2);
15069 arg1 && arg2;
15070 arg1 = next_call_expr_arg (&iter1),
15071 arg2 = next_call_expr_arg (&iter2))
15072 if (!c_tree_equal (arg1, arg2))
15073 return false;
15074 if (arg1 || arg2)
15075 return false;
15076 return true;
15077 }
15078
15079 case TARGET_EXPR:
15080 {
15081 tree o1 = TREE_OPERAND (t1, 0);
15082 tree o2 = TREE_OPERAND (t2, 0);
15083
15084 /* Special case: if either target is an unallocated VAR_DECL,
15085 it means that it's going to be unified with whatever the
15086 TARGET_EXPR is really supposed to initialize, so treat it
15087 as being equivalent to anything. */
15088 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
15089 && !DECL_RTL_SET_P (o1))
15090 /*Nop*/;
15091 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
15092 && !DECL_RTL_SET_P (o2))
15093 /*Nop*/;
15094 else if (!c_tree_equal (o1, o2))
15095 return false;
15096
15097 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15098 }
15099
15100 case COMPONENT_REF:
15101 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15102 return false;
15103 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15104
15105 case PARM_DECL:
15106 case VAR_DECL:
15107 case CONST_DECL:
15108 case FIELD_DECL:
15109 case FUNCTION_DECL:
15110 case IDENTIFIER_NODE:
15111 case SSA_NAME:
15112 return false;
15113
15114 case TREE_VEC:
15115 {
15116 unsigned ix;
15117 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15118 return false;
15119 for (ix = TREE_VEC_LENGTH (t1); ix--;)
15120 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15121 TREE_VEC_ELT (t2, ix)))
15122 return false;
15123 return true;
15124 }
15125
15126 default:
15127 break;
15128 }
15129
15130 switch (TREE_CODE_CLASS (code1))
15131 {
15132 case tcc_unary:
15133 case tcc_binary:
15134 case tcc_comparison:
15135 case tcc_expression:
15136 case tcc_vl_exp:
15137 case tcc_reference:
15138 case tcc_statement:
15139 {
15140 int i, n = TREE_OPERAND_LENGTH (t1);
15141
15142 switch (code1)
15143 {
15144 case PREINCREMENT_EXPR:
15145 case PREDECREMENT_EXPR:
15146 case POSTINCREMENT_EXPR:
15147 case POSTDECREMENT_EXPR:
15148 n = 1;
15149 break;
15150 case ARRAY_REF:
15151 n = 2;
15152 break;
15153 default:
15154 break;
15155 }
15156
15157 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15158 && n != TREE_OPERAND_LENGTH (t2))
15159 return false;
15160
15161 for (i = 0; i < n; ++i)
15162 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15163 return false;
15164
15165 return true;
15166 }
15167
15168 case tcc_type:
15169 return comptypes (t1, t2);
15170 default:
15171 gcc_unreachable ();
15172 }
15173 /* We can get here with --disable-checking. */
15174 return false;
15175 }
15176
15177 /* Returns true when the function declaration FNDECL is implicit,
15178 introduced as a result of a call to an otherwise undeclared
15179 function, and false otherwise. */
15180
15181 bool
15182 c_decl_implicit (const_tree fndecl)
15183 {
15184 return C_DECL_IMPLICIT (fndecl);
15185 }