6abfd101f3016119b0d0904138fe604ba34f8dea
[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 ("cannot mix operands of decimal floating and vector types");
747 return error_mark_node;
748 }
749 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
750 {
751 error ("cannot mix operands of decimal floating and complex types");
752 return error_mark_node;
753 }
754 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
755 {
756 error ("cannot mix operands of decimal floating "
757 "and other floating types");
758 return error_mark_node;
759 }
760 }
761
762 /* If one type is a vector type, return that type. (How the usual
763 arithmetic conversions apply to the vector types extension is not
764 precisely specified.) */
765 if (code1 == VECTOR_TYPE)
766 return t1;
767
768 if (code2 == VECTOR_TYPE)
769 return t2;
770
771 /* If one type is complex, form the common type of the non-complex
772 components, then make that complex. Use T1 or T2 if it is the
773 required type. */
774 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
775 {
776 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
777 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
778 tree subtype = c_common_type (subtype1, subtype2);
779
780 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
781 return t1;
782 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
783 return t2;
784 else
785 return build_complex_type (subtype);
786 }
787
788 /* If only one is real, use it as the result. */
789
790 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
791 return t1;
792
793 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
794 return t2;
795
796 /* If both are real and either are decimal floating point types, use
797 the decimal floating point type with the greater precision. */
798
799 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
800 {
801 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
802 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
803 return dfloat128_type_node;
804 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
805 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
806 return dfloat64_type_node;
807 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
808 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
809 return dfloat32_type_node;
810 }
811
812 /* Deal with fixed-point types. */
813 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
814 {
815 unsigned int unsignedp = 0, satp = 0;
816 scalar_mode m1, m2;
817 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
818
819 m1 = SCALAR_TYPE_MODE (t1);
820 m2 = SCALAR_TYPE_MODE (t2);
821
822 /* If one input type is saturating, the result type is saturating. */
823 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
824 satp = 1;
825
826 /* If both fixed-point types are unsigned, the result type is unsigned.
827 When mixing fixed-point and integer types, follow the sign of the
828 fixed-point type.
829 Otherwise, the result type is signed. */
830 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
831 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
832 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t1))
834 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
835 && TYPE_UNSIGNED (t2)))
836 unsignedp = 1;
837
838 /* The result type is signed. */
839 if (unsignedp == 0)
840 {
841 /* If the input type is unsigned, we need to convert to the
842 signed type. */
843 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
844 {
845 enum mode_class mclass = (enum mode_class) 0;
846 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
847 mclass = MODE_FRACT;
848 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
849 mclass = MODE_ACCUM;
850 else
851 gcc_unreachable ();
852 m1 = as_a <scalar_mode>
853 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
854 }
855 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
856 {
857 enum mode_class mclass = (enum mode_class) 0;
858 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
859 mclass = MODE_FRACT;
860 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
861 mclass = MODE_ACCUM;
862 else
863 gcc_unreachable ();
864 m2 = as_a <scalar_mode>
865 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
866 }
867 }
868
869 if (code1 == FIXED_POINT_TYPE)
870 {
871 fbit1 = GET_MODE_FBIT (m1);
872 ibit1 = GET_MODE_IBIT (m1);
873 }
874 else
875 {
876 fbit1 = 0;
877 /* Signed integers need to subtract one sign bit. */
878 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
879 }
880
881 if (code2 == FIXED_POINT_TYPE)
882 {
883 fbit2 = GET_MODE_FBIT (m2);
884 ibit2 = GET_MODE_IBIT (m2);
885 }
886 else
887 {
888 fbit2 = 0;
889 /* Signed integers need to subtract one sign bit. */
890 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
891 }
892
893 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
894 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
895 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
896 satp);
897 }
898
899 /* Both real or both integers; use the one with greater precision. */
900
901 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
902 return t1;
903 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
904 return t2;
905
906 /* Same precision. Prefer long longs to longs to ints when the
907 same precision, following the C99 rules on integer type rank
908 (which are equivalent to the C90 rules for C90 types). */
909
910 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
911 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
912 return long_long_unsigned_type_node;
913
914 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
915 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
916 {
917 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
918 return long_long_unsigned_type_node;
919 else
920 return long_long_integer_type_node;
921 }
922
923 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
924 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
925 return long_unsigned_type_node;
926
927 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
928 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
929 {
930 /* But preserve unsignedness from the other type,
931 since long cannot hold all the values of an unsigned int. */
932 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
933 return long_unsigned_type_node;
934 else
935 return long_integer_type_node;
936 }
937
938 /* For floating types of the same TYPE_PRECISION (which we here
939 assume means either the same set of values, or sets of values
940 neither a subset of the other, with behavior being undefined in
941 the latter case), follow the rules from TS 18661-3: prefer
942 interchange types _FloatN, then standard types long double,
943 double, float, then extended types _FloatNx. For extended types,
944 check them starting with _Float128x as that seems most consistent
945 in spirit with preferring long double to double; for interchange
946 types, also check in that order for consistency although it's not
947 possible for more than one of them to have the same
948 precision. */
949 tree mv1 = TYPE_MAIN_VARIANT (t1);
950 tree mv2 = TYPE_MAIN_VARIANT (t2);
951
952 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
953 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
954 return FLOATN_TYPE_NODE (i);
955
956 /* Likewise, prefer long double to double even if same size. */
957 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
958 return long_double_type_node;
959
960 /* Likewise, prefer double to float even if same size.
961 We got a couple of embedded targets with 32 bit doubles, and the
962 pdp11 might have 64 bit floats. */
963 if (mv1 == double_type_node || mv2 == double_type_node)
964 return double_type_node;
965
966 if (mv1 == float_type_node || mv2 == float_type_node)
967 return float_type_node;
968
969 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
970 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
971 return FLOATNX_TYPE_NODE (i);
972
973 /* Otherwise prefer the unsigned one. */
974
975 if (TYPE_UNSIGNED (t1))
976 return t1;
977 else
978 return t2;
979 }
980 \f
981 /* Wrapper around c_common_type that is used by c-common.c and other
982 front end optimizations that remove promotions. ENUMERAL_TYPEs
983 are allowed here and are converted to their compatible integer types.
984 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
985 preferably a non-Boolean type as the common type. */
986 tree
987 common_type (tree t1, tree t2)
988 {
989 if (TREE_CODE (t1) == ENUMERAL_TYPE)
990 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
991 if (TREE_CODE (t2) == ENUMERAL_TYPE)
992 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
993
994 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
995 if (TREE_CODE (t1) == BOOLEAN_TYPE
996 && TREE_CODE (t2) == BOOLEAN_TYPE)
997 return boolean_type_node;
998
999 /* If either type is BOOLEAN_TYPE, then return the other. */
1000 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1001 return t2;
1002 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1003 return t1;
1004
1005 return c_common_type (t1, t2);
1006 }
1007
1008 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1009 or various other operations. Return 2 if they are compatible
1010 but a warning may be needed if you use them together. */
1011
1012 int
1013 comptypes (tree type1, tree type2)
1014 {
1015 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1016 int val;
1017
1018 val = comptypes_internal (type1, type2, NULL, NULL);
1019 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1020
1021 return val;
1022 }
1023
1024 /* Like comptypes, but if it returns non-zero because enum and int are
1025 compatible, it sets *ENUM_AND_INT_P to true. */
1026
1027 static int
1028 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1029 {
1030 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1031 int val;
1032
1033 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1034 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1035
1036 return val;
1037 }
1038
1039 /* Like comptypes, but if it returns nonzero for different types, it
1040 sets *DIFFERENT_TYPES_P to true. */
1041
1042 int
1043 comptypes_check_different_types (tree type1, tree type2,
1044 bool *different_types_p)
1045 {
1046 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1047 int val;
1048
1049 val = comptypes_internal (type1, type2, NULL, different_types_p);
1050 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1051
1052 return val;
1053 }
1054 \f
1055 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1056 or various other operations. Return 2 if they are compatible
1057 but a warning may be needed if you use them together. If
1058 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1059 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1060 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1061 NULL, and the types are compatible but different enough not to be
1062 permitted in C11 typedef redeclarations, then this sets
1063 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1064 false, but may or may not be set if the types are incompatible.
1065 This differs from comptypes, in that we don't free the seen
1066 types. */
1067
1068 static int
1069 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1070 bool *different_types_p)
1071 {
1072 const_tree t1 = type1;
1073 const_tree t2 = type2;
1074 int attrval, val;
1075
1076 /* Suppress errors caused by previously reported errors. */
1077
1078 if (t1 == t2 || !t1 || !t2
1079 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1080 return 1;
1081
1082 /* Enumerated types are compatible with integer types, but this is
1083 not transitive: two enumerated types in the same translation unit
1084 are compatible with each other only if they are the same type. */
1085
1086 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1087 {
1088 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1089 if (TREE_CODE (t2) != VOID_TYPE)
1090 {
1091 if (enum_and_int_p != NULL)
1092 *enum_and_int_p = true;
1093 if (different_types_p != NULL)
1094 *different_types_p = true;
1095 }
1096 }
1097 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1098 {
1099 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1100 if (TREE_CODE (t1) != VOID_TYPE)
1101 {
1102 if (enum_and_int_p != NULL)
1103 *enum_and_int_p = true;
1104 if (different_types_p != NULL)
1105 *different_types_p = true;
1106 }
1107 }
1108
1109 if (t1 == t2)
1110 return 1;
1111
1112 /* Different classes of types can't be compatible. */
1113
1114 if (TREE_CODE (t1) != TREE_CODE (t2))
1115 return 0;
1116
1117 /* Qualifiers must match. C99 6.7.3p9 */
1118
1119 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1120 return 0;
1121
1122 /* Allow for two different type nodes which have essentially the same
1123 definition. Note that we already checked for equality of the type
1124 qualifiers (just above). */
1125
1126 if (TREE_CODE (t1) != ARRAY_TYPE
1127 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1128 return 1;
1129
1130 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1131 if (!(attrval = comp_type_attributes (t1, t2)))
1132 return 0;
1133
1134 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1135 val = 0;
1136
1137 switch (TREE_CODE (t1))
1138 {
1139 case INTEGER_TYPE:
1140 case FIXED_POINT_TYPE:
1141 case REAL_TYPE:
1142 /* With these nodes, we can't determine type equivalence by
1143 looking at what is stored in the nodes themselves, because
1144 two nodes might have different TYPE_MAIN_VARIANTs but still
1145 represent the same type. For example, wchar_t and int could
1146 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1147 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1148 and are distinct types. On the other hand, int and the
1149 following typedef
1150
1151 typedef int INT __attribute((may_alias));
1152
1153 have identical properties, different TYPE_MAIN_VARIANTs, but
1154 represent the same type. The canonical type system keeps
1155 track of equivalence in this case, so we fall back on it. */
1156 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1157
1158 case POINTER_TYPE:
1159 /* Do not remove mode information. */
1160 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1161 break;
1162 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1163 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1164 enum_and_int_p, different_types_p));
1165 break;
1166
1167 case FUNCTION_TYPE:
1168 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1169 different_types_p);
1170 break;
1171
1172 case ARRAY_TYPE:
1173 {
1174 tree d1 = TYPE_DOMAIN (t1);
1175 tree d2 = TYPE_DOMAIN (t2);
1176 bool d1_variable, d2_variable;
1177 bool d1_zero, d2_zero;
1178 val = 1;
1179
1180 /* Target types must match incl. qualifiers. */
1181 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1182 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1183 enum_and_int_p,
1184 different_types_p)) == 0)
1185 return 0;
1186
1187 if (different_types_p != NULL
1188 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1189 *different_types_p = true;
1190 /* Sizes must match unless one is missing or variable. */
1191 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1192 break;
1193
1194 d1_zero = !TYPE_MAX_VALUE (d1);
1195 d2_zero = !TYPE_MAX_VALUE (d2);
1196
1197 d1_variable = (!d1_zero
1198 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1199 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1200 d2_variable = (!d2_zero
1201 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1202 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1203 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1204 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1205
1206 if (different_types_p != NULL
1207 && d1_variable != d2_variable)
1208 *different_types_p = true;
1209 if (d1_variable || d2_variable)
1210 break;
1211 if (d1_zero && d2_zero)
1212 break;
1213 if (d1_zero || d2_zero
1214 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1215 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1216 val = 0;
1217
1218 break;
1219 }
1220
1221 case ENUMERAL_TYPE:
1222 case RECORD_TYPE:
1223 case UNION_TYPE:
1224 if (val != 1 && !same_translation_unit_p (t1, t2))
1225 {
1226 tree a1 = TYPE_ATTRIBUTES (t1);
1227 tree a2 = TYPE_ATTRIBUTES (t2);
1228
1229 if (! attribute_list_contained (a1, a2)
1230 && ! attribute_list_contained (a2, a1))
1231 break;
1232
1233 if (attrval != 2)
1234 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1235 different_types_p);
1236 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1237 different_types_p);
1238 }
1239 break;
1240
1241 case VECTOR_TYPE:
1242 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1243 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1244 enum_and_int_p, different_types_p));
1245 break;
1246
1247 default:
1248 break;
1249 }
1250 return attrval == 2 && val == 1 ? 2 : val;
1251 }
1252
1253 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1254 their qualifiers, except for named address spaces. If the pointers point to
1255 different named addresses, then we must determine if one address space is a
1256 subset of the other. */
1257
1258 static int
1259 comp_target_types (location_t location, tree ttl, tree ttr)
1260 {
1261 int val;
1262 int val_ped;
1263 tree mvl = TREE_TYPE (ttl);
1264 tree mvr = TREE_TYPE (ttr);
1265 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1266 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1267 addr_space_t as_common;
1268 bool enum_and_int_p;
1269
1270 /* Fail if pointers point to incompatible address spaces. */
1271 if (!addr_space_superset (asl, asr, &as_common))
1272 return 0;
1273
1274 /* For pedantic record result of comptypes on arrays before losing
1275 qualifiers on the element type below. */
1276 val_ped = 1;
1277
1278 if (TREE_CODE (mvl) == ARRAY_TYPE
1279 && TREE_CODE (mvr) == ARRAY_TYPE)
1280 val_ped = comptypes (mvl, mvr);
1281
1282 /* Qualifiers on element types of array types that are
1283 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1284
1285 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1286 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1287 : TYPE_MAIN_VARIANT (mvl));
1288
1289 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1290 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1291 : TYPE_MAIN_VARIANT (mvr));
1292
1293 enum_and_int_p = false;
1294 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1295
1296 if (val == 1 && val_ped != 1)
1297 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1298 "are incompatible in ISO C");
1299
1300 if (val == 2)
1301 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1302
1303 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1304 warning_at (location, OPT_Wc___compat,
1305 "pointer target types incompatible in C++");
1306
1307 return val;
1308 }
1309 \f
1310 /* Subroutines of `comptypes'. */
1311
1312 /* Determine whether two trees derive from the same translation unit.
1313 If the CONTEXT chain ends in a null, that tree's context is still
1314 being parsed, so if two trees have context chains ending in null,
1315 they're in the same translation unit. */
1316
1317 bool
1318 same_translation_unit_p (const_tree t1, const_tree t2)
1319 {
1320 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1321 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1322 {
1323 case tcc_declaration:
1324 t1 = DECL_CONTEXT (t1); break;
1325 case tcc_type:
1326 t1 = TYPE_CONTEXT (t1); break;
1327 case tcc_exceptional:
1328 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1329 default: gcc_unreachable ();
1330 }
1331
1332 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1333 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1334 {
1335 case tcc_declaration:
1336 t2 = DECL_CONTEXT (t2); break;
1337 case tcc_type:
1338 t2 = TYPE_CONTEXT (t2); break;
1339 case tcc_exceptional:
1340 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1341 default: gcc_unreachable ();
1342 }
1343
1344 return t1 == t2;
1345 }
1346
1347 /* Allocate the seen two types, assuming that they are compatible. */
1348
1349 static struct tagged_tu_seen_cache *
1350 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1351 {
1352 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1353 tu->next = tagged_tu_seen_base;
1354 tu->t1 = t1;
1355 tu->t2 = t2;
1356
1357 tagged_tu_seen_base = tu;
1358
1359 /* The C standard says that two structures in different translation
1360 units are compatible with each other only if the types of their
1361 fields are compatible (among other things). We assume that they
1362 are compatible until proven otherwise when building the cache.
1363 An example where this can occur is:
1364 struct a
1365 {
1366 struct a *next;
1367 };
1368 If we are comparing this against a similar struct in another TU,
1369 and did not assume they were compatible, we end up with an infinite
1370 loop. */
1371 tu->val = 1;
1372 return tu;
1373 }
1374
1375 /* Free the seen types until we get to TU_TIL. */
1376
1377 static void
1378 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1379 {
1380 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1381 while (tu != tu_til)
1382 {
1383 const struct tagged_tu_seen_cache *const tu1
1384 = (const struct tagged_tu_seen_cache *) tu;
1385 tu = tu1->next;
1386 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1387 }
1388 tagged_tu_seen_base = tu_til;
1389 }
1390
1391 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1392 compatible. If the two types are not the same (which has been
1393 checked earlier), this can only happen when multiple translation
1394 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1395 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1396 comptypes_internal. */
1397
1398 static int
1399 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1400 bool *enum_and_int_p, bool *different_types_p)
1401 {
1402 tree s1, s2;
1403 bool needs_warning = false;
1404
1405 /* We have to verify that the tags of the types are the same. This
1406 is harder than it looks because this may be a typedef, so we have
1407 to go look at the original type. It may even be a typedef of a
1408 typedef...
1409 In the case of compiler-created builtin structs the TYPE_DECL
1410 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1411 while (TYPE_NAME (t1)
1412 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1413 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1414 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1415
1416 while (TYPE_NAME (t2)
1417 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1418 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1419 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1420
1421 /* C90 didn't have the requirement that the two tags be the same. */
1422 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1423 return 0;
1424
1425 /* C90 didn't say what happened if one or both of the types were
1426 incomplete; we choose to follow C99 rules here, which is that they
1427 are compatible. */
1428 if (TYPE_SIZE (t1) == NULL
1429 || TYPE_SIZE (t2) == NULL)
1430 return 1;
1431
1432 {
1433 const struct tagged_tu_seen_cache * tts_i;
1434 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1435 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1436 return tts_i->val;
1437 }
1438
1439 switch (TREE_CODE (t1))
1440 {
1441 case ENUMERAL_TYPE:
1442 {
1443 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1444 /* Speed up the case where the type values are in the same order. */
1445 tree tv1 = TYPE_VALUES (t1);
1446 tree tv2 = TYPE_VALUES (t2);
1447
1448 if (tv1 == tv2)
1449 {
1450 return 1;
1451 }
1452
1453 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1454 {
1455 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1456 break;
1457 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1458 {
1459 tu->val = 0;
1460 return 0;
1461 }
1462 }
1463
1464 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1465 {
1466 return 1;
1467 }
1468 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1469 {
1470 tu->val = 0;
1471 return 0;
1472 }
1473
1474 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1475 {
1476 tu->val = 0;
1477 return 0;
1478 }
1479
1480 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1481 {
1482 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1483 if (s2 == NULL
1484 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1485 {
1486 tu->val = 0;
1487 return 0;
1488 }
1489 }
1490 return 1;
1491 }
1492
1493 case UNION_TYPE:
1494 {
1495 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1496 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1497 {
1498 tu->val = 0;
1499 return 0;
1500 }
1501
1502 /* Speed up the common case where the fields are in the same order. */
1503 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1504 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1505 {
1506 int result;
1507
1508 if (DECL_NAME (s1) != DECL_NAME (s2))
1509 break;
1510 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1511 enum_and_int_p, different_types_p);
1512
1513 if (result != 1 && !DECL_NAME (s1))
1514 break;
1515 if (result == 0)
1516 {
1517 tu->val = 0;
1518 return 0;
1519 }
1520 if (result == 2)
1521 needs_warning = true;
1522
1523 if (TREE_CODE (s1) == FIELD_DECL
1524 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1525 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1526 {
1527 tu->val = 0;
1528 return 0;
1529 }
1530 }
1531 if (!s1 && !s2)
1532 {
1533 tu->val = needs_warning ? 2 : 1;
1534 return tu->val;
1535 }
1536
1537 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1538 {
1539 bool ok = false;
1540
1541 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1542 if (DECL_NAME (s1) == DECL_NAME (s2))
1543 {
1544 int result;
1545
1546 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1547 enum_and_int_p,
1548 different_types_p);
1549
1550 if (result != 1 && !DECL_NAME (s1))
1551 continue;
1552 if (result == 0)
1553 {
1554 tu->val = 0;
1555 return 0;
1556 }
1557 if (result == 2)
1558 needs_warning = true;
1559
1560 if (TREE_CODE (s1) == FIELD_DECL
1561 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1562 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1563 break;
1564
1565 ok = true;
1566 break;
1567 }
1568 if (!ok)
1569 {
1570 tu->val = 0;
1571 return 0;
1572 }
1573 }
1574 tu->val = needs_warning ? 2 : 10;
1575 return tu->val;
1576 }
1577
1578 case RECORD_TYPE:
1579 {
1580 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1581
1582 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1583 s1 && s2;
1584 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1585 {
1586 int result;
1587 if (TREE_CODE (s1) != TREE_CODE (s2)
1588 || DECL_NAME (s1) != DECL_NAME (s2))
1589 break;
1590 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1591 enum_and_int_p, different_types_p);
1592 if (result == 0)
1593 break;
1594 if (result == 2)
1595 needs_warning = true;
1596
1597 if (TREE_CODE (s1) == FIELD_DECL
1598 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1599 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1600 break;
1601 }
1602 if (s1 && s2)
1603 tu->val = 0;
1604 else
1605 tu->val = needs_warning ? 2 : 1;
1606 return tu->val;
1607 }
1608
1609 default:
1610 gcc_unreachable ();
1611 }
1612 }
1613
1614 /* Return 1 if two function types F1 and F2 are compatible.
1615 If either type specifies no argument types,
1616 the other must specify a fixed number of self-promoting arg types.
1617 Otherwise, if one type specifies only the number of arguments,
1618 the other must specify that number of self-promoting arg types.
1619 Otherwise, the argument types must match.
1620 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1621
1622 static int
1623 function_types_compatible_p (const_tree f1, const_tree f2,
1624 bool *enum_and_int_p, bool *different_types_p)
1625 {
1626 tree args1, args2;
1627 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1628 int val = 1;
1629 int val1;
1630 tree ret1, ret2;
1631
1632 ret1 = TREE_TYPE (f1);
1633 ret2 = TREE_TYPE (f2);
1634
1635 /* 'volatile' qualifiers on a function's return type used to mean
1636 the function is noreturn. */
1637 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1638 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1639 if (TYPE_VOLATILE (ret1))
1640 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1641 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1642 if (TYPE_VOLATILE (ret2))
1643 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1644 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1645 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1646 if (val == 0)
1647 return 0;
1648
1649 args1 = TYPE_ARG_TYPES (f1);
1650 args2 = TYPE_ARG_TYPES (f2);
1651
1652 if (different_types_p != NULL
1653 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1654 *different_types_p = true;
1655
1656 /* An unspecified parmlist matches any specified parmlist
1657 whose argument types don't need default promotions. */
1658
1659 if (args1 == NULL_TREE)
1660 {
1661 if (!self_promoting_args_p (args2))
1662 return 0;
1663 /* If one of these types comes from a non-prototype fn definition,
1664 compare that with the other type's arglist.
1665 If they don't match, ask for a warning (but no error). */
1666 if (TYPE_ACTUAL_ARG_TYPES (f1)
1667 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1668 enum_and_int_p, different_types_p) != 1)
1669 val = 2;
1670 return val;
1671 }
1672 if (args2 == NULL_TREE)
1673 {
1674 if (!self_promoting_args_p (args1))
1675 return 0;
1676 if (TYPE_ACTUAL_ARG_TYPES (f2)
1677 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1678 enum_and_int_p, different_types_p) != 1)
1679 val = 2;
1680 return val;
1681 }
1682
1683 /* Both types have argument lists: compare them and propagate results. */
1684 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1685 different_types_p);
1686 return val1 != 1 ? val1 : val;
1687 }
1688
1689 /* Check two lists of types for compatibility, returning 0 for
1690 incompatible, 1 for compatible, or 2 for compatible with
1691 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1692 comptypes_internal. */
1693
1694 static int
1695 type_lists_compatible_p (const_tree args1, const_tree args2,
1696 bool *enum_and_int_p, bool *different_types_p)
1697 {
1698 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1699 int val = 1;
1700 int newval = 0;
1701
1702 while (1)
1703 {
1704 tree a1, mv1, a2, mv2;
1705 if (args1 == NULL_TREE && args2 == NULL_TREE)
1706 return val;
1707 /* If one list is shorter than the other,
1708 they fail to match. */
1709 if (args1 == NULL_TREE || args2 == NULL_TREE)
1710 return 0;
1711 mv1 = a1 = TREE_VALUE (args1);
1712 mv2 = a2 = TREE_VALUE (args2);
1713 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1714 mv1 = (TYPE_ATOMIC (mv1)
1715 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1716 TYPE_QUAL_ATOMIC)
1717 : TYPE_MAIN_VARIANT (mv1));
1718 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1719 mv2 = (TYPE_ATOMIC (mv2)
1720 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1721 TYPE_QUAL_ATOMIC)
1722 : TYPE_MAIN_VARIANT (mv2));
1723 /* A null pointer instead of a type
1724 means there is supposed to be an argument
1725 but nothing is specified about what type it has.
1726 So match anything that self-promotes. */
1727 if (different_types_p != NULL
1728 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1729 *different_types_p = true;
1730 if (a1 == NULL_TREE)
1731 {
1732 if (c_type_promotes_to (a2) != a2)
1733 return 0;
1734 }
1735 else if (a2 == NULL_TREE)
1736 {
1737 if (c_type_promotes_to (a1) != a1)
1738 return 0;
1739 }
1740 /* If one of the lists has an error marker, ignore this arg. */
1741 else if (TREE_CODE (a1) == ERROR_MARK
1742 || TREE_CODE (a2) == ERROR_MARK)
1743 ;
1744 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1745 different_types_p)))
1746 {
1747 if (different_types_p != NULL)
1748 *different_types_p = true;
1749 /* Allow wait (union {union wait *u; int *i} *)
1750 and wait (union wait *) to be compatible. */
1751 if (TREE_CODE (a1) == UNION_TYPE
1752 && (TYPE_NAME (a1) == NULL_TREE
1753 || TYPE_TRANSPARENT_AGGR (a1))
1754 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1755 && tree_int_cst_equal (TYPE_SIZE (a1),
1756 TYPE_SIZE (a2)))
1757 {
1758 tree memb;
1759 for (memb = TYPE_FIELDS (a1);
1760 memb; memb = DECL_CHAIN (memb))
1761 {
1762 tree mv3 = TREE_TYPE (memb);
1763 if (mv3 && mv3 != error_mark_node
1764 && TREE_CODE (mv3) != ARRAY_TYPE)
1765 mv3 = (TYPE_ATOMIC (mv3)
1766 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1767 TYPE_QUAL_ATOMIC)
1768 : TYPE_MAIN_VARIANT (mv3));
1769 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1770 different_types_p))
1771 break;
1772 }
1773 if (memb == NULL_TREE)
1774 return 0;
1775 }
1776 else if (TREE_CODE (a2) == UNION_TYPE
1777 && (TYPE_NAME (a2) == NULL_TREE
1778 || TYPE_TRANSPARENT_AGGR (a2))
1779 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1780 && tree_int_cst_equal (TYPE_SIZE (a2),
1781 TYPE_SIZE (a1)))
1782 {
1783 tree memb;
1784 for (memb = TYPE_FIELDS (a2);
1785 memb; memb = DECL_CHAIN (memb))
1786 {
1787 tree mv3 = TREE_TYPE (memb);
1788 if (mv3 && mv3 != error_mark_node
1789 && TREE_CODE (mv3) != ARRAY_TYPE)
1790 mv3 = (TYPE_ATOMIC (mv3)
1791 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1792 TYPE_QUAL_ATOMIC)
1793 : TYPE_MAIN_VARIANT (mv3));
1794 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1795 different_types_p))
1796 break;
1797 }
1798 if (memb == NULL_TREE)
1799 return 0;
1800 }
1801 else
1802 return 0;
1803 }
1804
1805 /* comptypes said ok, but record if it said to warn. */
1806 if (newval > val)
1807 val = newval;
1808
1809 args1 = TREE_CHAIN (args1);
1810 args2 = TREE_CHAIN (args2);
1811 }
1812 }
1813 \f
1814 /* Compute the size to increment a pointer by. When a function type or void
1815 type or incomplete type is passed, size_one_node is returned.
1816 This function does not emit any diagnostics; the caller is responsible
1817 for that. */
1818
1819 static tree
1820 c_size_in_bytes (const_tree type)
1821 {
1822 enum tree_code code = TREE_CODE (type);
1823
1824 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1825 || !COMPLETE_TYPE_P (type))
1826 return size_one_node;
1827
1828 /* Convert in case a char is more than one unit. */
1829 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1830 size_int (TYPE_PRECISION (char_type_node)
1831 / BITS_PER_UNIT));
1832 }
1833 \f
1834 /* Return either DECL or its known constant value (if it has one). */
1835
1836 tree
1837 decl_constant_value_1 (tree decl, bool in_init)
1838 {
1839 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1840 TREE_CODE (decl) != PARM_DECL
1841 && !TREE_THIS_VOLATILE (decl)
1842 && TREE_READONLY (decl)
1843 && DECL_INITIAL (decl) != NULL_TREE
1844 && !error_operand_p (DECL_INITIAL (decl))
1845 /* This is invalid if initial value is not constant.
1846 If it has either a function call, a memory reference,
1847 or a variable, then re-evaluating it could give different results. */
1848 && TREE_CONSTANT (DECL_INITIAL (decl))
1849 /* Check for cases where this is sub-optimal, even though valid. */
1850 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1851 return DECL_INITIAL (decl);
1852 return decl;
1853 }
1854
1855 /* Return either DECL or its known constant value (if it has one).
1856 Like the above, but always return decl outside of functions. */
1857
1858 tree
1859 decl_constant_value (tree decl)
1860 {
1861 /* Don't change a variable array bound or initial value to a constant
1862 in a place where a variable is invalid. */
1863 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1864 }
1865
1866 /* Convert the array expression EXP to a pointer. */
1867 static tree
1868 array_to_pointer_conversion (location_t loc, tree exp)
1869 {
1870 tree orig_exp = exp;
1871 tree type = TREE_TYPE (exp);
1872 tree adr;
1873 tree restype = TREE_TYPE (type);
1874 tree ptrtype;
1875
1876 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1877
1878 STRIP_TYPE_NOPS (exp);
1879
1880 if (TREE_NO_WARNING (orig_exp))
1881 TREE_NO_WARNING (exp) = 1;
1882
1883 ptrtype = build_pointer_type (restype);
1884
1885 if (INDIRECT_REF_P (exp))
1886 return convert (ptrtype, TREE_OPERAND (exp, 0));
1887
1888 /* In C++ array compound literals are temporary objects unless they are
1889 const or appear in namespace scope, so they are destroyed too soon
1890 to use them for much of anything (c++/53220). */
1891 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1892 {
1893 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1894 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1895 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1896 "converting an array compound literal to a pointer "
1897 "is ill-formed in C++");
1898 }
1899
1900 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1901 return convert (ptrtype, adr);
1902 }
1903
1904 /* Convert the function expression EXP to a pointer. */
1905 static tree
1906 function_to_pointer_conversion (location_t loc, tree exp)
1907 {
1908 tree orig_exp = exp;
1909
1910 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1911
1912 STRIP_TYPE_NOPS (exp);
1913
1914 if (TREE_NO_WARNING (orig_exp))
1915 TREE_NO_WARNING (exp) = 1;
1916
1917 return build_unary_op (loc, ADDR_EXPR, exp, false);
1918 }
1919
1920 /* Mark EXP as read, not just set, for set but not used -Wunused
1921 warning purposes. */
1922
1923 void
1924 mark_exp_read (tree exp)
1925 {
1926 switch (TREE_CODE (exp))
1927 {
1928 case VAR_DECL:
1929 case PARM_DECL:
1930 DECL_READ_P (exp) = 1;
1931 break;
1932 case ARRAY_REF:
1933 case COMPONENT_REF:
1934 case MODIFY_EXPR:
1935 case REALPART_EXPR:
1936 case IMAGPART_EXPR:
1937 CASE_CONVERT:
1938 case ADDR_EXPR:
1939 case VIEW_CONVERT_EXPR:
1940 mark_exp_read (TREE_OPERAND (exp, 0));
1941 break;
1942 case COMPOUND_EXPR:
1943 case C_MAYBE_CONST_EXPR:
1944 mark_exp_read (TREE_OPERAND (exp, 1));
1945 break;
1946 default:
1947 break;
1948 }
1949 }
1950
1951 /* Perform the default conversion of arrays and functions to pointers.
1952 Return the result of converting EXP. For any other expression, just
1953 return EXP.
1954
1955 LOC is the location of the expression. */
1956
1957 struct c_expr
1958 default_function_array_conversion (location_t loc, struct c_expr exp)
1959 {
1960 tree orig_exp = exp.value;
1961 tree type = TREE_TYPE (exp.value);
1962 enum tree_code code = TREE_CODE (type);
1963
1964 switch (code)
1965 {
1966 case ARRAY_TYPE:
1967 {
1968 bool not_lvalue = false;
1969 bool lvalue_array_p;
1970
1971 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1972 || CONVERT_EXPR_P (exp.value))
1973 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1974 {
1975 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1976 not_lvalue = true;
1977 exp.value = TREE_OPERAND (exp.value, 0);
1978 }
1979
1980 if (TREE_NO_WARNING (orig_exp))
1981 TREE_NO_WARNING (exp.value) = 1;
1982
1983 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1984 if (!flag_isoc99 && !lvalue_array_p)
1985 {
1986 /* Before C99, non-lvalue arrays do not decay to pointers.
1987 Normally, using such an array would be invalid; but it can
1988 be used correctly inside sizeof or as a statement expression.
1989 Thus, do not give an error here; an error will result later. */
1990 return exp;
1991 }
1992
1993 exp.value = array_to_pointer_conversion (loc, exp.value);
1994 }
1995 break;
1996 case FUNCTION_TYPE:
1997 exp.value = function_to_pointer_conversion (loc, exp.value);
1998 break;
1999 default:
2000 break;
2001 }
2002
2003 return exp;
2004 }
2005
2006 struct c_expr
2007 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2008 {
2009 mark_exp_read (exp.value);
2010 return default_function_array_conversion (loc, exp);
2011 }
2012
2013 /* Return whether EXPR should be treated as an atomic lvalue for the
2014 purposes of load and store handling. */
2015
2016 static bool
2017 really_atomic_lvalue (tree expr)
2018 {
2019 if (error_operand_p (expr))
2020 return false;
2021 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2022 return false;
2023 if (!lvalue_p (expr))
2024 return false;
2025
2026 /* Ignore _Atomic on register variables, since their addresses can't
2027 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2028 sequences wouldn't work. Ignore _Atomic on structures containing
2029 bit-fields, since accessing elements of atomic structures or
2030 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2031 it's undefined at translation time or execution time, and the
2032 normal atomic sequences again wouldn't work. */
2033 while (handled_component_p (expr))
2034 {
2035 if (TREE_CODE (expr) == COMPONENT_REF
2036 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2037 return false;
2038 expr = TREE_OPERAND (expr, 0);
2039 }
2040 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2041 return false;
2042 return true;
2043 }
2044
2045 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2046 including converting functions and arrays to pointers if CONVERT_P.
2047 If READ_P, also mark the expression as having been read. */
2048
2049 struct c_expr
2050 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2051 bool convert_p, bool read_p)
2052 {
2053 if (read_p)
2054 mark_exp_read (exp.value);
2055 if (convert_p)
2056 exp = default_function_array_conversion (loc, exp);
2057 if (really_atomic_lvalue (exp.value))
2058 {
2059 vec<tree, va_gc> *params;
2060 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2061 tree expr_type = TREE_TYPE (exp.value);
2062 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2063 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2064
2065 gcc_assert (TYPE_ATOMIC (expr_type));
2066
2067 /* Expansion of a generic atomic load may require an addition
2068 element, so allocate enough to prevent a resize. */
2069 vec_alloc (params, 4);
2070
2071 /* Remove the qualifiers for the rest of the expressions and
2072 create the VAL temp variable to hold the RHS. */
2073 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2074 tmp = create_tmp_var_raw (nonatomic_type);
2075 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2076 TREE_ADDRESSABLE (tmp) = 1;
2077 TREE_NO_WARNING (tmp) = 1;
2078
2079 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2080 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2081 params->quick_push (expr_addr);
2082 params->quick_push (tmp_addr);
2083 params->quick_push (seq_cst);
2084 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2085
2086 /* EXPR is always read. */
2087 mark_exp_read (exp.value);
2088
2089 /* Return tmp which contains the value loaded. */
2090 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2091 NULL_TREE, NULL_TREE);
2092 }
2093 return exp;
2094 }
2095
2096 /* EXP is an expression of integer type. Apply the integer promotions
2097 to it and return the promoted value. */
2098
2099 tree
2100 perform_integral_promotions (tree exp)
2101 {
2102 tree type = TREE_TYPE (exp);
2103 enum tree_code code = TREE_CODE (type);
2104
2105 gcc_assert (INTEGRAL_TYPE_P (type));
2106
2107 /* Normally convert enums to int,
2108 but convert wide enums to something wider. */
2109 if (code == ENUMERAL_TYPE)
2110 {
2111 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2112 TYPE_PRECISION (integer_type_node)),
2113 ((TYPE_PRECISION (type)
2114 >= TYPE_PRECISION (integer_type_node))
2115 && TYPE_UNSIGNED (type)));
2116
2117 return convert (type, exp);
2118 }
2119
2120 /* ??? This should no longer be needed now bit-fields have their
2121 proper types. */
2122 if (TREE_CODE (exp) == COMPONENT_REF
2123 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2124 /* If it's thinner than an int, promote it like a
2125 c_promoting_integer_type_p, otherwise leave it alone. */
2126 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2127 TYPE_PRECISION (integer_type_node)) < 0)
2128 return convert (integer_type_node, exp);
2129
2130 if (c_promoting_integer_type_p (type))
2131 {
2132 /* Preserve unsignedness if not really getting any wider. */
2133 if (TYPE_UNSIGNED (type)
2134 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2135 return convert (unsigned_type_node, exp);
2136
2137 return convert (integer_type_node, exp);
2138 }
2139
2140 return exp;
2141 }
2142
2143
2144 /* Perform default promotions for C data used in expressions.
2145 Enumeral types or short or char are converted to int.
2146 In addition, manifest constants symbols are replaced by their values. */
2147
2148 tree
2149 default_conversion (tree exp)
2150 {
2151 tree orig_exp;
2152 tree type = TREE_TYPE (exp);
2153 enum tree_code code = TREE_CODE (type);
2154 tree promoted_type;
2155
2156 mark_exp_read (exp);
2157
2158 /* Functions and arrays have been converted during parsing. */
2159 gcc_assert (code != FUNCTION_TYPE);
2160 if (code == ARRAY_TYPE)
2161 return exp;
2162
2163 /* Constants can be used directly unless they're not loadable. */
2164 if (TREE_CODE (exp) == CONST_DECL)
2165 exp = DECL_INITIAL (exp);
2166
2167 /* Strip no-op conversions. */
2168 orig_exp = exp;
2169 STRIP_TYPE_NOPS (exp);
2170
2171 if (TREE_NO_WARNING (orig_exp))
2172 TREE_NO_WARNING (exp) = 1;
2173
2174 if (code == VOID_TYPE)
2175 {
2176 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2177 "void value not ignored as it ought to be");
2178 return error_mark_node;
2179 }
2180
2181 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2182 if (exp == error_mark_node)
2183 return error_mark_node;
2184
2185 promoted_type = targetm.promoted_type (type);
2186 if (promoted_type)
2187 return convert (promoted_type, exp);
2188
2189 if (INTEGRAL_TYPE_P (type))
2190 return perform_integral_promotions (exp);
2191
2192 return exp;
2193 }
2194 \f
2195 /* Look up COMPONENT in a structure or union TYPE.
2196
2197 If the component name is not found, returns NULL_TREE. Otherwise,
2198 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2199 stepping down the chain to the component, which is in the last
2200 TREE_VALUE of the list. Normally the list is of length one, but if
2201 the component is embedded within (nested) anonymous structures or
2202 unions, the list steps down the chain to the component. */
2203
2204 static tree
2205 lookup_field (tree type, tree component)
2206 {
2207 tree field;
2208
2209 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2210 to the field elements. Use a binary search on this array to quickly
2211 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2212 will always be set for structures which have many elements.
2213
2214 Duplicate field checking replaces duplicates with NULL_TREE so
2215 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2216 case just iterate using DECL_CHAIN. */
2217
2218 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2219 && !seen_error ())
2220 {
2221 int bot, top, half;
2222 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2223
2224 field = TYPE_FIELDS (type);
2225 bot = 0;
2226 top = TYPE_LANG_SPECIFIC (type)->s->len;
2227 while (top - bot > 1)
2228 {
2229 half = (top - bot + 1) >> 1;
2230 field = field_array[bot+half];
2231
2232 if (DECL_NAME (field) == NULL_TREE)
2233 {
2234 /* Step through all anon unions in linear fashion. */
2235 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2236 {
2237 field = field_array[bot++];
2238 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2239 {
2240 tree anon = lookup_field (TREE_TYPE (field), component);
2241
2242 if (anon)
2243 return tree_cons (NULL_TREE, field, anon);
2244
2245 /* The Plan 9 compiler permits referring
2246 directly to an anonymous struct/union field
2247 using a typedef name. */
2248 if (flag_plan9_extensions
2249 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2250 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2251 == TYPE_DECL)
2252 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2253 == component))
2254 break;
2255 }
2256 }
2257
2258 /* Entire record is only anon unions. */
2259 if (bot > top)
2260 return NULL_TREE;
2261
2262 /* Restart the binary search, with new lower bound. */
2263 continue;
2264 }
2265
2266 if (DECL_NAME (field) == component)
2267 break;
2268 if (DECL_NAME (field) < component)
2269 bot += half;
2270 else
2271 top = bot + half;
2272 }
2273
2274 if (DECL_NAME (field_array[bot]) == component)
2275 field = field_array[bot];
2276 else if (DECL_NAME (field) != component)
2277 return NULL_TREE;
2278 }
2279 else
2280 {
2281 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2282 {
2283 if (DECL_NAME (field) == NULL_TREE
2284 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2285 {
2286 tree anon = lookup_field (TREE_TYPE (field), component);
2287
2288 if (anon)
2289 return tree_cons (NULL_TREE, field, anon);
2290
2291 /* The Plan 9 compiler permits referring directly to an
2292 anonymous struct/union field using a typedef
2293 name. */
2294 if (flag_plan9_extensions
2295 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2296 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2297 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2298 == component))
2299 break;
2300 }
2301
2302 if (DECL_NAME (field) == component)
2303 break;
2304 }
2305
2306 if (field == NULL_TREE)
2307 return NULL_TREE;
2308 }
2309
2310 return tree_cons (NULL_TREE, field, NULL_TREE);
2311 }
2312
2313 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2314
2315 static void
2316 lookup_field_fuzzy_find_candidates (tree type, tree component,
2317 vec<tree> *candidates)
2318 {
2319 tree field;
2320 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2321 {
2322 if (DECL_NAME (field) == NULL_TREE
2323 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2324 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2325 candidates);
2326
2327 if (DECL_NAME (field))
2328 candidates->safe_push (DECL_NAME (field));
2329 }
2330 }
2331
2332 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2333 rather than returning a TREE_LIST for an exact match. */
2334
2335 static tree
2336 lookup_field_fuzzy (tree type, tree component)
2337 {
2338 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2339
2340 /* First, gather a list of candidates. */
2341 auto_vec <tree> candidates;
2342
2343 lookup_field_fuzzy_find_candidates (type, component,
2344 &candidates);
2345
2346 return find_closest_identifier (component, &candidates);
2347 }
2348
2349 /* Support function for build_component_ref's error-handling.
2350
2351 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2352 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2353
2354 static bool
2355 should_suggest_deref_p (tree datum_type)
2356 {
2357 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2358 allows "." for ptrs; we could be handling a failed attempt
2359 to access a property. */
2360 if (c_dialect_objc ())
2361 return false;
2362
2363 /* Only suggest it for pointers... */
2364 if (TREE_CODE (datum_type) != POINTER_TYPE)
2365 return false;
2366
2367 /* ...to structs/unions. */
2368 tree underlying_type = TREE_TYPE (datum_type);
2369 enum tree_code code = TREE_CODE (underlying_type);
2370 if (code == RECORD_TYPE || code == UNION_TYPE)
2371 return true;
2372 else
2373 return false;
2374 }
2375
2376 /* Make an expression to refer to the COMPONENT field of structure or
2377 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2378 location of the COMPONENT_REF. COMPONENT_LOC is the location
2379 of COMPONENT. */
2380
2381 tree
2382 build_component_ref (location_t loc, tree datum, tree component,
2383 location_t component_loc)
2384 {
2385 tree type = TREE_TYPE (datum);
2386 enum tree_code code = TREE_CODE (type);
2387 tree field = NULL;
2388 tree ref;
2389 bool datum_lvalue = lvalue_p (datum);
2390
2391 if (!objc_is_public (datum, component))
2392 return error_mark_node;
2393
2394 /* Detect Objective-C property syntax object.property. */
2395 if (c_dialect_objc ()
2396 && (ref = objc_maybe_build_component_ref (datum, component)))
2397 return ref;
2398
2399 /* See if there is a field or component with name COMPONENT. */
2400
2401 if (code == RECORD_TYPE || code == UNION_TYPE)
2402 {
2403 if (!COMPLETE_TYPE_P (type))
2404 {
2405 c_incomplete_type_error (loc, NULL_TREE, type);
2406 return error_mark_node;
2407 }
2408
2409 field = lookup_field (type, component);
2410
2411 if (!field)
2412 {
2413 tree guessed_id = lookup_field_fuzzy (type, component);
2414 if (guessed_id)
2415 {
2416 /* Attempt to provide a fixit replacement hint, if
2417 we have a valid range for the component. */
2418 location_t reported_loc
2419 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2420 gcc_rich_location rich_loc (reported_loc);
2421 if (component_loc != UNKNOWN_LOCATION)
2422 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2423 error_at (&rich_loc,
2424 "%qT has no member named %qE; did you mean %qE?",
2425 type, component, guessed_id);
2426 }
2427 else
2428 error_at (loc, "%qT has no member named %qE", type, component);
2429 return error_mark_node;
2430 }
2431
2432 /* Accessing elements of atomic structures or unions is undefined
2433 behavior (C11 6.5.2.3#5). */
2434 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2435 {
2436 if (code == RECORD_TYPE)
2437 warning_at (loc, 0, "accessing a member %qE of an atomic "
2438 "structure %qE", component, datum);
2439 else
2440 warning_at (loc, 0, "accessing a member %qE of an atomic "
2441 "union %qE", component, datum);
2442 }
2443
2444 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2445 This might be better solved in future the way the C++ front
2446 end does it - by giving the anonymous entities each a
2447 separate name and type, and then have build_component_ref
2448 recursively call itself. We can't do that here. */
2449 do
2450 {
2451 tree subdatum = TREE_VALUE (field);
2452 int quals;
2453 tree subtype;
2454 bool use_datum_quals;
2455
2456 if (TREE_TYPE (subdatum) == error_mark_node)
2457 return error_mark_node;
2458
2459 /* If this is an rvalue, it does not have qualifiers in C
2460 standard terms and we must avoid propagating such
2461 qualifiers down to a non-lvalue array that is then
2462 converted to a pointer. */
2463 use_datum_quals = (datum_lvalue
2464 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2465
2466 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2467 if (use_datum_quals)
2468 quals |= TYPE_QUALS (TREE_TYPE (datum));
2469 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2470
2471 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2472 NULL_TREE);
2473 SET_EXPR_LOCATION (ref, loc);
2474 if (TREE_READONLY (subdatum)
2475 || (use_datum_quals && TREE_READONLY (datum)))
2476 TREE_READONLY (ref) = 1;
2477 if (TREE_THIS_VOLATILE (subdatum)
2478 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2479 TREE_THIS_VOLATILE (ref) = 1;
2480
2481 if (TREE_DEPRECATED (subdatum))
2482 warn_deprecated_use (subdatum, NULL_TREE);
2483
2484 datum = ref;
2485
2486 field = TREE_CHAIN (field);
2487 }
2488 while (field);
2489
2490 return ref;
2491 }
2492 else if (should_suggest_deref_p (type))
2493 {
2494 /* Special-case the error message for "ptr.field" for the case
2495 where the user has confused "." vs "->". */
2496 rich_location richloc (line_table, loc);
2497 /* "loc" should be the "." token. */
2498 richloc.add_fixit_replace ("->");
2499 error_at (&richloc,
2500 "%qE is a pointer; did you mean to use %<->%>?",
2501 datum);
2502 return error_mark_node;
2503 }
2504 else if (code != ERROR_MARK)
2505 error_at (loc,
2506 "request for member %qE in something not a structure or union",
2507 component);
2508
2509 return error_mark_node;
2510 }
2511 \f
2512 /* Given an expression PTR for a pointer, return an expression
2513 for the value pointed to.
2514 ERRORSTRING is the name of the operator to appear in error messages.
2515
2516 LOC is the location to use for the generated tree. */
2517
2518 tree
2519 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2520 {
2521 tree pointer = default_conversion (ptr);
2522 tree type = TREE_TYPE (pointer);
2523 tree ref;
2524
2525 if (TREE_CODE (type) == POINTER_TYPE)
2526 {
2527 if (CONVERT_EXPR_P (pointer)
2528 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2529 {
2530 /* If a warning is issued, mark it to avoid duplicates from
2531 the backend. This only needs to be done at
2532 warn_strict_aliasing > 2. */
2533 if (warn_strict_aliasing > 2)
2534 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2535 type, TREE_OPERAND (pointer, 0)))
2536 TREE_NO_WARNING (pointer) = 1;
2537 }
2538
2539 if (TREE_CODE (pointer) == ADDR_EXPR
2540 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2541 == TREE_TYPE (type)))
2542 {
2543 ref = TREE_OPERAND (pointer, 0);
2544 protected_set_expr_location (ref, loc);
2545 return ref;
2546 }
2547 else
2548 {
2549 tree t = TREE_TYPE (type);
2550
2551 ref = build1 (INDIRECT_REF, t, pointer);
2552
2553 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2554 {
2555 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2556 {
2557 error_at (loc, "dereferencing pointer to incomplete type "
2558 "%qT", t);
2559 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2560 }
2561 return error_mark_node;
2562 }
2563 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2564 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2565
2566 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2567 so that we get the proper error message if the result is used
2568 to assign to. Also, &* is supposed to be a no-op.
2569 And ANSI C seems to specify that the type of the result
2570 should be the const type. */
2571 /* A de-reference of a pointer to const is not a const. It is valid
2572 to change it via some other pointer. */
2573 TREE_READONLY (ref) = TYPE_READONLY (t);
2574 TREE_SIDE_EFFECTS (ref)
2575 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2576 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2577 protected_set_expr_location (ref, loc);
2578 return ref;
2579 }
2580 }
2581 else if (TREE_CODE (pointer) != ERROR_MARK)
2582 invalid_indirection_error (loc, type, errstring);
2583
2584 return error_mark_node;
2585 }
2586
2587 /* This handles expressions of the form "a[i]", which denotes
2588 an array reference.
2589
2590 This is logically equivalent in C to *(a+i), but we may do it differently.
2591 If A is a variable or a member, we generate a primitive ARRAY_REF.
2592 This avoids forcing the array out of registers, and can work on
2593 arrays that are not lvalues (for example, members of structures returned
2594 by functions).
2595
2596 For vector types, allow vector[i] but not i[vector], and create
2597 *(((type*)&vectortype) + i) for the expression.
2598
2599 LOC is the location to use for the returned expression. */
2600
2601 tree
2602 build_array_ref (location_t loc, tree array, tree index)
2603 {
2604 tree ret;
2605 bool swapped = false;
2606 if (TREE_TYPE (array) == error_mark_node
2607 || TREE_TYPE (index) == error_mark_node)
2608 return error_mark_node;
2609
2610 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2611 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2612 /* Allow vector[index] but not index[vector]. */
2613 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2614 {
2615 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2616 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2617 {
2618 error_at (loc,
2619 "subscripted value is neither array nor pointer nor vector");
2620
2621 return error_mark_node;
2622 }
2623 std::swap (array, index);
2624 swapped = true;
2625 }
2626
2627 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2628 {
2629 error_at (loc, "array subscript is not an integer");
2630 return error_mark_node;
2631 }
2632
2633 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2634 {
2635 error_at (loc, "subscripted value is pointer to function");
2636 return error_mark_node;
2637 }
2638
2639 /* ??? Existing practice has been to warn only when the char
2640 index is syntactically the index, not for char[array]. */
2641 if (!swapped)
2642 warn_array_subscript_with_type_char (loc, index);
2643
2644 /* Apply default promotions *after* noticing character types. */
2645 index = default_conversion (index);
2646 if (index == error_mark_node)
2647 return error_mark_node;
2648
2649 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2650
2651 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2652 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2653
2654 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2655 {
2656 tree rval, type;
2657
2658 /* An array that is indexed by a non-constant
2659 cannot be stored in a register; we must be able to do
2660 address arithmetic on its address.
2661 Likewise an array of elements of variable size. */
2662 if (TREE_CODE (index) != INTEGER_CST
2663 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2664 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2665 {
2666 if (!c_mark_addressable (array, true))
2667 return error_mark_node;
2668 }
2669 /* An array that is indexed by a constant value which is not within
2670 the array bounds cannot be stored in a register either; because we
2671 would get a crash in store_bit_field/extract_bit_field when trying
2672 to access a non-existent part of the register. */
2673 if (TREE_CODE (index) == INTEGER_CST
2674 && TYPE_DOMAIN (TREE_TYPE (array))
2675 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2676 {
2677 if (!c_mark_addressable (array))
2678 return error_mark_node;
2679 }
2680
2681 if ((pedantic || warn_c90_c99_compat)
2682 && ! was_vector)
2683 {
2684 tree foo = array;
2685 while (TREE_CODE (foo) == COMPONENT_REF)
2686 foo = TREE_OPERAND (foo, 0);
2687 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2688 pedwarn (loc, OPT_Wpedantic,
2689 "ISO C forbids subscripting %<register%> array");
2690 else if (!lvalue_p (foo))
2691 pedwarn_c90 (loc, OPT_Wpedantic,
2692 "ISO C90 forbids subscripting non-lvalue "
2693 "array");
2694 }
2695
2696 type = TREE_TYPE (TREE_TYPE (array));
2697 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2698 /* Array ref is const/volatile if the array elements are
2699 or if the array is. */
2700 TREE_READONLY (rval)
2701 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2702 | TREE_READONLY (array));
2703 TREE_SIDE_EFFECTS (rval)
2704 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2705 | TREE_SIDE_EFFECTS (array));
2706 TREE_THIS_VOLATILE (rval)
2707 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2708 /* This was added by rms on 16 Nov 91.
2709 It fixes vol struct foo *a; a->elts[1]
2710 in an inline function.
2711 Hope it doesn't break something else. */
2712 | TREE_THIS_VOLATILE (array));
2713 ret = require_complete_type (loc, rval);
2714 protected_set_expr_location (ret, loc);
2715 if (non_lvalue)
2716 ret = non_lvalue_loc (loc, ret);
2717 return ret;
2718 }
2719 else
2720 {
2721 tree ar = default_conversion (array);
2722
2723 if (ar == error_mark_node)
2724 return ar;
2725
2726 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2727 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2728
2729 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2730 index, false),
2731 RO_ARRAY_INDEXING);
2732 if (non_lvalue)
2733 ret = non_lvalue_loc (loc, ret);
2734 return ret;
2735 }
2736 }
2737 \f
2738 /* Build an external reference to identifier ID. FUN indicates
2739 whether this will be used for a function call. LOC is the source
2740 location of the identifier. This sets *TYPE to the type of the
2741 identifier, which is not the same as the type of the returned value
2742 for CONST_DECLs defined as enum constants. If the type of the
2743 identifier is not available, *TYPE is set to NULL. */
2744 tree
2745 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2746 {
2747 tree ref;
2748 tree decl = lookup_name (id);
2749
2750 /* In Objective-C, an instance variable (ivar) may be preferred to
2751 whatever lookup_name() found. */
2752 decl = objc_lookup_ivar (decl, id);
2753
2754 *type = NULL;
2755 if (decl && decl != error_mark_node)
2756 {
2757 ref = decl;
2758 *type = TREE_TYPE (ref);
2759 }
2760 else if (fun)
2761 /* Implicit function declaration. */
2762 ref = implicitly_declare (loc, id);
2763 else if (decl == error_mark_node)
2764 /* Don't complain about something that's already been
2765 complained about. */
2766 return error_mark_node;
2767 else
2768 {
2769 undeclared_variable (loc, id);
2770 return error_mark_node;
2771 }
2772
2773 if (TREE_TYPE (ref) == error_mark_node)
2774 return error_mark_node;
2775
2776 if (TREE_DEPRECATED (ref))
2777 warn_deprecated_use (ref, NULL_TREE);
2778
2779 /* Recursive call does not count as usage. */
2780 if (ref != current_function_decl)
2781 {
2782 TREE_USED (ref) = 1;
2783 }
2784
2785 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2786 {
2787 if (!in_sizeof && !in_typeof)
2788 C_DECL_USED (ref) = 1;
2789 else if (DECL_INITIAL (ref) == NULL_TREE
2790 && DECL_EXTERNAL (ref)
2791 && !TREE_PUBLIC (ref))
2792 record_maybe_used_decl (ref);
2793 }
2794
2795 if (TREE_CODE (ref) == CONST_DECL)
2796 {
2797 used_types_insert (TREE_TYPE (ref));
2798
2799 if (warn_cxx_compat
2800 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2801 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2802 {
2803 warning_at (loc, OPT_Wc___compat,
2804 ("enum constant defined in struct or union "
2805 "is not visible in C++"));
2806 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2807 }
2808
2809 ref = DECL_INITIAL (ref);
2810 TREE_CONSTANT (ref) = 1;
2811 }
2812 else if (current_function_decl != NULL_TREE
2813 && !DECL_FILE_SCOPE_P (current_function_decl)
2814 && (VAR_OR_FUNCTION_DECL_P (ref)
2815 || TREE_CODE (ref) == PARM_DECL))
2816 {
2817 tree context = decl_function_context (ref);
2818
2819 if (context != NULL_TREE && context != current_function_decl)
2820 DECL_NONLOCAL (ref) = 1;
2821 }
2822 /* C99 6.7.4p3: An inline definition of a function with external
2823 linkage ... shall not contain a reference to an identifier with
2824 internal linkage. */
2825 else if (current_function_decl != NULL_TREE
2826 && DECL_DECLARED_INLINE_P (current_function_decl)
2827 && DECL_EXTERNAL (current_function_decl)
2828 && VAR_OR_FUNCTION_DECL_P (ref)
2829 && (!VAR_P (ref) || TREE_STATIC (ref))
2830 && ! TREE_PUBLIC (ref)
2831 && DECL_CONTEXT (ref) != current_function_decl)
2832 record_inline_static (loc, current_function_decl, ref,
2833 csi_internal);
2834
2835 return ref;
2836 }
2837
2838 /* Record details of decls possibly used inside sizeof or typeof. */
2839 struct maybe_used_decl
2840 {
2841 /* The decl. */
2842 tree decl;
2843 /* The level seen at (in_sizeof + in_typeof). */
2844 int level;
2845 /* The next one at this level or above, or NULL. */
2846 struct maybe_used_decl *next;
2847 };
2848
2849 static struct maybe_used_decl *maybe_used_decls;
2850
2851 /* Record that DECL, an undefined static function reference seen
2852 inside sizeof or typeof, might be used if the operand of sizeof is
2853 a VLA type or the operand of typeof is a variably modified
2854 type. */
2855
2856 static void
2857 record_maybe_used_decl (tree decl)
2858 {
2859 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2860 t->decl = decl;
2861 t->level = in_sizeof + in_typeof;
2862 t->next = maybe_used_decls;
2863 maybe_used_decls = t;
2864 }
2865
2866 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2867 USED is false, just discard them. If it is true, mark them used
2868 (if no longer inside sizeof or typeof) or move them to the next
2869 level up (if still inside sizeof or typeof). */
2870
2871 void
2872 pop_maybe_used (bool used)
2873 {
2874 struct maybe_used_decl *p = maybe_used_decls;
2875 int cur_level = in_sizeof + in_typeof;
2876 while (p && p->level > cur_level)
2877 {
2878 if (used)
2879 {
2880 if (cur_level == 0)
2881 C_DECL_USED (p->decl) = 1;
2882 else
2883 p->level = cur_level;
2884 }
2885 p = p->next;
2886 }
2887 if (!used || cur_level == 0)
2888 maybe_used_decls = p;
2889 }
2890
2891 /* Return the result of sizeof applied to EXPR. */
2892
2893 struct c_expr
2894 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2895 {
2896 struct c_expr ret;
2897 if (expr.value == error_mark_node)
2898 {
2899 ret.value = error_mark_node;
2900 ret.original_code = ERROR_MARK;
2901 ret.original_type = NULL;
2902 pop_maybe_used (false);
2903 }
2904 else
2905 {
2906 bool expr_const_operands = true;
2907
2908 if (TREE_CODE (expr.value) == PARM_DECL
2909 && C_ARRAY_PARAMETER (expr.value))
2910 {
2911 auto_diagnostic_group d;
2912 if (warning_at (loc, OPT_Wsizeof_array_argument,
2913 "%<sizeof%> on array function parameter %qE will "
2914 "return size of %qT", expr.value,
2915 TREE_TYPE (expr.value)))
2916 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2917 }
2918 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2919 &expr_const_operands);
2920 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2921 c_last_sizeof_arg = expr.value;
2922 c_last_sizeof_loc = loc;
2923 ret.original_code = SIZEOF_EXPR;
2924 ret.original_type = NULL;
2925 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2926 {
2927 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2928 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2929 folded_expr, ret.value);
2930 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2931 SET_EXPR_LOCATION (ret.value, loc);
2932 }
2933 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2934 }
2935 return ret;
2936 }
2937
2938 /* Return the result of sizeof applied to T, a structure for the type
2939 name passed to sizeof (rather than the type itself). LOC is the
2940 location of the original expression. */
2941
2942 struct c_expr
2943 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2944 {
2945 tree type;
2946 struct c_expr ret;
2947 tree type_expr = NULL_TREE;
2948 bool type_expr_const = true;
2949 type = groktypename (t, &type_expr, &type_expr_const);
2950 ret.value = c_sizeof (loc, type);
2951 c_last_sizeof_arg = type;
2952 c_last_sizeof_loc = loc;
2953 ret.original_code = SIZEOF_EXPR;
2954 ret.original_type = NULL;
2955 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2956 && c_vla_type_p (type))
2957 {
2958 /* If the type is a [*] array, it is a VLA but is represented as
2959 having a size of zero. In such a case we must ensure that
2960 the result of sizeof does not get folded to a constant by
2961 c_fully_fold, because if the size is evaluated the result is
2962 not constant and so constraints on zero or negative size
2963 arrays must not be applied when this sizeof call is inside
2964 another array declarator. */
2965 if (!type_expr)
2966 type_expr = integer_zero_node;
2967 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2968 type_expr, ret.value);
2969 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2970 }
2971 pop_maybe_used (type != error_mark_node
2972 ? C_TYPE_VARIABLE_SIZE (type) : false);
2973 return ret;
2974 }
2975
2976 /* Build a function call to function FUNCTION with parameters PARAMS.
2977 The function call is at LOC.
2978 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2979 TREE_VALUE of each node is a parameter-expression.
2980 FUNCTION's data type may be a function type or a pointer-to-function. */
2981
2982 tree
2983 build_function_call (location_t loc, tree function, tree params)
2984 {
2985 vec<tree, va_gc> *v;
2986 tree ret;
2987
2988 vec_alloc (v, list_length (params));
2989 for (; params; params = TREE_CHAIN (params))
2990 v->quick_push (TREE_VALUE (params));
2991 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2992 vec_free (v);
2993 return ret;
2994 }
2995
2996 /* Give a note about the location of the declaration of DECL. */
2997
2998 static void
2999 inform_declaration (tree decl)
3000 {
3001 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
3002 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3003 }
3004
3005 /* Build a function call to function FUNCTION with parameters PARAMS.
3006 ORIGTYPES, if not NULL, is a vector of types; each element is
3007 either NULL or the original type of the corresponding element in
3008 PARAMS. The original type may differ from TREE_TYPE of the
3009 parameter for enums. FUNCTION's data type may be a function type
3010 or pointer-to-function. This function changes the elements of
3011 PARAMS. */
3012
3013 tree
3014 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3015 tree function, vec<tree, va_gc> *params,
3016 vec<tree, va_gc> *origtypes)
3017 {
3018 tree fntype, fundecl = NULL_TREE;
3019 tree name = NULL_TREE, result;
3020 tree tem;
3021 int nargs;
3022 tree *argarray;
3023
3024
3025 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3026 STRIP_TYPE_NOPS (function);
3027
3028 /* Convert anything with function type to a pointer-to-function. */
3029 if (TREE_CODE (function) == FUNCTION_DECL)
3030 {
3031 name = DECL_NAME (function);
3032
3033 if (flag_tm)
3034 tm_malloc_replacement (function);
3035 fundecl = function;
3036 /* Atomic functions have type checking/casting already done. They are
3037 often rewritten and don't match the original parameter list. */
3038 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3039 origtypes = NULL;
3040 }
3041 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3042 function = function_to_pointer_conversion (loc, function);
3043
3044 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3045 expressions, like those used for ObjC messenger dispatches. */
3046 if (params && !params->is_empty ())
3047 function = objc_rewrite_function_call (function, (*params)[0]);
3048
3049 function = c_fully_fold (function, false, NULL);
3050
3051 fntype = TREE_TYPE (function);
3052
3053 if (TREE_CODE (fntype) == ERROR_MARK)
3054 return error_mark_node;
3055
3056 if (!(TREE_CODE (fntype) == POINTER_TYPE
3057 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3058 {
3059 if (!flag_diagnostics_show_caret)
3060 error_at (loc,
3061 "called object %qE is not a function or function pointer",
3062 function);
3063 else if (DECL_P (function))
3064 {
3065 error_at (loc,
3066 "called object %qD is not a function or function pointer",
3067 function);
3068 inform_declaration (function);
3069 }
3070 else
3071 error_at (loc,
3072 "called object is not a function or function pointer");
3073 return error_mark_node;
3074 }
3075
3076 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3077 current_function_returns_abnormally = 1;
3078
3079 /* fntype now gets the type of function pointed to. */
3080 fntype = TREE_TYPE (fntype);
3081
3082 /* Convert the parameters to the types declared in the
3083 function prototype, or apply default promotions. */
3084
3085 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3086 origtypes, function, fundecl);
3087 if (nargs < 0)
3088 return error_mark_node;
3089
3090 /* Check that the function is called through a compatible prototype.
3091 If it is not, warn. */
3092 if (CONVERT_EXPR_P (function)
3093 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3094 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3095 && !comptypes (fntype, TREE_TYPE (tem)))
3096 {
3097 tree return_type = TREE_TYPE (fntype);
3098
3099 /* This situation leads to run-time undefined behavior. We can't,
3100 therefore, simply error unless we can prove that all possible
3101 executions of the program must execute the code. */
3102 warning_at (loc, 0, "function called through a non-compatible type");
3103
3104 if (VOID_TYPE_P (return_type)
3105 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3106 pedwarn (loc, 0,
3107 "function with qualified void return type called");
3108 }
3109
3110 argarray = vec_safe_address (params);
3111
3112 /* Check that arguments to builtin functions match the expectations. */
3113 if (fundecl && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL)
3114 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3115 argarray))
3116 return error_mark_node;
3117
3118 /* Check that the arguments to the function are valid. */
3119 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3120 nargs, argarray, &arg_loc);
3121
3122 if (name != NULL_TREE
3123 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3124 {
3125 if (require_constant_value)
3126 result
3127 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3128 function, nargs, argarray);
3129 else
3130 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3131 function, nargs, argarray);
3132 if (TREE_CODE (result) == NOP_EXPR
3133 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3134 STRIP_TYPE_NOPS (result);
3135 }
3136 else
3137 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3138 function, nargs, argarray);
3139 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3140 later. */
3141 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3142 TREE_NO_WARNING (result) = 1;
3143
3144 /* In this improbable scenario, a nested function returns a VM type.
3145 Create a TARGET_EXPR so that the call always has a LHS, much as
3146 what the C++ FE does for functions returning non-PODs. */
3147 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3148 {
3149 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3150 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3151 NULL_TREE, NULL_TREE);
3152 }
3153
3154 if (VOID_TYPE_P (TREE_TYPE (result)))
3155 {
3156 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3157 pedwarn (loc, 0,
3158 "function with qualified void return type called");
3159 return result;
3160 }
3161 return require_complete_type (loc, result);
3162 }
3163
3164 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3165
3166 tree
3167 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3168 tree function, vec<tree, va_gc> *params,
3169 vec<tree, va_gc> *origtypes)
3170 {
3171 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3172 STRIP_TYPE_NOPS (function);
3173
3174 /* Convert anything with function type to a pointer-to-function. */
3175 if (TREE_CODE (function) == FUNCTION_DECL)
3176 {
3177 /* Implement type-directed function overloading for builtins.
3178 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3179 handle all the type checking. The result is a complete expression
3180 that implements this function call. */
3181 tree tem = resolve_overloaded_builtin (loc, function, params);
3182 if (tem)
3183 return tem;
3184 }
3185 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3186 }
3187 \f
3188 /* Helper for convert_arguments called to convert the VALue of argument
3189 number ARGNUM from ORIGTYPE to the corresponding parameter number
3190 PARMNUM and TYPE.
3191 PLOC is the location where the conversion is being performed.
3192 FUNCTION and FUNDECL are the same as in convert_arguments.
3193 VALTYPE is the original type of VAL before the conversion and,
3194 for EXCESS_PRECISION_EXPR, the operand of the expression.
3195 NPC is true if VAL represents the null pointer constant (VAL itself
3196 will have been folded to an integer constant).
3197 RNAME is the same as FUNCTION except in Objective C when it's
3198 the function selector.
3199 EXCESS_PRECISION is true when VAL was originally represented
3200 as EXCESS_PRECISION_EXPR.
3201 WARNOPT is the same as in convert_for_assignment. */
3202
3203 static tree
3204 convert_argument (location_t ploc, tree function, tree fundecl,
3205 tree type, tree origtype, tree val, tree valtype,
3206 bool npc, tree rname, int parmnum, int argnum,
3207 bool excess_precision, int warnopt)
3208 {
3209 /* Formal parm type is specified by a function prototype. */
3210
3211 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3212 {
3213 error_at (ploc, "type of formal parameter %d is incomplete",
3214 parmnum + 1);
3215 return val;
3216 }
3217
3218 /* Optionally warn about conversions that differ from the default
3219 conversions. */
3220 if (warn_traditional_conversion || warn_traditional)
3221 {
3222 unsigned int formal_prec = TYPE_PRECISION (type);
3223
3224 if (INTEGRAL_TYPE_P (type)
3225 && TREE_CODE (valtype) == REAL_TYPE)
3226 warning_at (ploc, OPT_Wtraditional_conversion,
3227 "passing argument %d of %qE as integer rather "
3228 "than floating due to prototype",
3229 argnum, rname);
3230 if (INTEGRAL_TYPE_P (type)
3231 && TREE_CODE (valtype) == COMPLEX_TYPE)
3232 warning_at (ploc, OPT_Wtraditional_conversion,
3233 "passing argument %d of %qE as integer rather "
3234 "than complex due to prototype",
3235 argnum, rname);
3236 else if (TREE_CODE (type) == COMPLEX_TYPE
3237 && TREE_CODE (valtype) == REAL_TYPE)
3238 warning_at (ploc, OPT_Wtraditional_conversion,
3239 "passing argument %d of %qE as complex rather "
3240 "than floating due to prototype",
3241 argnum, rname);
3242 else if (TREE_CODE (type) == REAL_TYPE
3243 && INTEGRAL_TYPE_P (valtype))
3244 warning_at (ploc, OPT_Wtraditional_conversion,
3245 "passing argument %d of %qE as floating rather "
3246 "than integer due to prototype",
3247 argnum, rname);
3248 else if (TREE_CODE (type) == COMPLEX_TYPE
3249 && INTEGRAL_TYPE_P (valtype))
3250 warning_at (ploc, OPT_Wtraditional_conversion,
3251 "passing argument %d of %qE as complex rather "
3252 "than integer due to prototype",
3253 argnum, rname);
3254 else if (TREE_CODE (type) == REAL_TYPE
3255 && TREE_CODE (valtype) == COMPLEX_TYPE)
3256 warning_at (ploc, OPT_Wtraditional_conversion,
3257 "passing argument %d of %qE as floating rather "
3258 "than complex due to prototype",
3259 argnum, rname);
3260 /* ??? At some point, messages should be written about
3261 conversions between complex types, but that's too messy
3262 to do now. */
3263 else if (TREE_CODE (type) == REAL_TYPE
3264 && TREE_CODE (valtype) == REAL_TYPE)
3265 {
3266 /* Warn if any argument is passed as `float',
3267 since without a prototype it would be `double'. */
3268 if (formal_prec == TYPE_PRECISION (float_type_node)
3269 && type != dfloat32_type_node)
3270 warning_at (ploc, 0,
3271 "passing argument %d of %qE as %<float%> "
3272 "rather than %<double%> due to prototype",
3273 argnum, rname);
3274
3275 /* Warn if mismatch between argument and prototype
3276 for decimal float types. Warn of conversions with
3277 binary float types and of precision narrowing due to
3278 prototype. */
3279 else if (type != valtype
3280 && (type == dfloat32_type_node
3281 || type == dfloat64_type_node
3282 || type == dfloat128_type_node
3283 || valtype == dfloat32_type_node
3284 || valtype == dfloat64_type_node
3285 || valtype == dfloat128_type_node)
3286 && (formal_prec
3287 <= TYPE_PRECISION (valtype)
3288 || (type == dfloat128_type_node
3289 && (valtype
3290 != dfloat64_type_node
3291 && (valtype
3292 != dfloat32_type_node)))
3293 || (type == dfloat64_type_node
3294 && (valtype
3295 != dfloat32_type_node))))
3296 warning_at (ploc, 0,
3297 "passing argument %d of %qE as %qT "
3298 "rather than %qT due to prototype",
3299 argnum, rname, type, valtype);
3300
3301 }
3302 /* Detect integer changing in width or signedness.
3303 These warnings are only activated with
3304 -Wtraditional-conversion, not with -Wtraditional. */
3305 else if (warn_traditional_conversion
3306 && INTEGRAL_TYPE_P (type)
3307 && INTEGRAL_TYPE_P (valtype))
3308 {
3309 tree would_have_been = default_conversion (val);
3310 tree type1 = TREE_TYPE (would_have_been);
3311
3312 if (val == error_mark_node)
3313 /* VAL could have been of incomplete type. */;
3314 else if (TREE_CODE (type) == ENUMERAL_TYPE
3315 && (TYPE_MAIN_VARIANT (type)
3316 == TYPE_MAIN_VARIANT (valtype)))
3317 /* No warning if function asks for enum
3318 and the actual arg is that enum type. */
3319 ;
3320 else if (formal_prec != TYPE_PRECISION (type1))
3321 warning_at (ploc, OPT_Wtraditional_conversion,
3322 "passing argument %d of %qE "
3323 "with different width due to prototype",
3324 argnum, rname);
3325 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3326 ;
3327 /* Don't complain if the formal parameter type
3328 is an enum, because we can't tell now whether
3329 the value was an enum--even the same enum. */
3330 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3331 ;
3332 else if (TREE_CODE (val) == INTEGER_CST
3333 && int_fits_type_p (val, type))
3334 /* Change in signedness doesn't matter
3335 if a constant value is unaffected. */
3336 ;
3337 /* If the value is extended from a narrower
3338 unsigned type, it doesn't matter whether we
3339 pass it as signed or unsigned; the value
3340 certainly is the same either way. */
3341 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3342 && TYPE_UNSIGNED (valtype))
3343 ;
3344 else if (TYPE_UNSIGNED (type))
3345 warning_at (ploc, OPT_Wtraditional_conversion,
3346 "passing argument %d of %qE "
3347 "as unsigned due to prototype",
3348 argnum, rname);
3349 else
3350 warning_at (ploc, OPT_Wtraditional_conversion,
3351 "passing argument %d of %qE "
3352 "as signed due to prototype",
3353 argnum, rname);
3354 }
3355 }
3356
3357 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3358 sake of better warnings from convert_and_check. */
3359 if (excess_precision)
3360 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3361
3362 tree parmval = convert_for_assignment (ploc, ploc, type,
3363 val, origtype, ic_argpass,
3364 npc, fundecl, function,
3365 parmnum + 1, warnopt);
3366
3367 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3368 && INTEGRAL_TYPE_P (type)
3369 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3370 parmval = default_conversion (parmval);
3371
3372 return parmval;
3373 }
3374
3375 /* Convert the argument expressions in the vector VALUES
3376 to the types in the list TYPELIST.
3377
3378 If TYPELIST is exhausted, or when an element has NULL as its type,
3379 perform the default conversions.
3380
3381 ORIGTYPES is the original types of the expressions in VALUES. This
3382 holds the type of enum values which have been converted to integral
3383 types. It may be NULL.
3384
3385 FUNCTION is a tree for the called function. It is used only for
3386 error messages, where it is formatted with %qE.
3387
3388 This is also where warnings about wrong number of args are generated.
3389
3390 ARG_LOC are locations of function arguments (if any).
3391
3392 Returns the actual number of arguments processed (which may be less
3393 than the length of VALUES in some error situations), or -1 on
3394 failure. */
3395
3396 static int
3397 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3398 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3399 tree function, tree fundecl)
3400 {
3401 unsigned int parmnum;
3402 bool error_args = false;
3403 const bool type_generic = fundecl
3404 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3405 bool type_generic_remove_excess_precision = false;
3406 bool type_generic_overflow_p = false;
3407 tree selector;
3408
3409 /* Change pointer to function to the function itself for
3410 diagnostics. */
3411 if (TREE_CODE (function) == ADDR_EXPR
3412 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3413 function = TREE_OPERAND (function, 0);
3414
3415 /* Handle an ObjC selector specially for diagnostics. */
3416 selector = objc_message_selector ();
3417
3418 /* For a call to a built-in function declared without a prototype,
3419 set to the built-in function's argument list. */
3420 tree builtin_typelist = NULL_TREE;
3421
3422 /* For type-generic built-in functions, determine whether excess
3423 precision should be removed (classification) or not
3424 (comparison). */
3425 if (fundecl
3426 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3427 {
3428 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3429 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3430 {
3431 /* For a call to a built-in function declared without a prototype
3432 use the types of the parameters of the internal built-in to
3433 match those of the arguments to. */
3434 if (tree bdecl = builtin_decl_explicit (code))
3435 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3436 }
3437
3438 /* For type-generic built-in functions, determine whether excess
3439 precision should be removed (classification) or not
3440 (comparison). */
3441 if (type_generic)
3442 switch (code)
3443 {
3444 case BUILT_IN_ISFINITE:
3445 case BUILT_IN_ISINF:
3446 case BUILT_IN_ISINF_SIGN:
3447 case BUILT_IN_ISNAN:
3448 case BUILT_IN_ISNORMAL:
3449 case BUILT_IN_FPCLASSIFY:
3450 type_generic_remove_excess_precision = true;
3451 break;
3452
3453 case BUILT_IN_ADD_OVERFLOW_P:
3454 case BUILT_IN_SUB_OVERFLOW_P:
3455 case BUILT_IN_MUL_OVERFLOW_P:
3456 /* The last argument of these type-generic builtins
3457 should not be promoted. */
3458 type_generic_overflow_p = true;
3459 break;
3460
3461 default:
3462 break;
3463 }
3464 }
3465
3466 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3467 individual converted arguments. */
3468
3469 tree typetail, builtin_typetail, val;
3470 for (typetail = typelist,
3471 builtin_typetail = builtin_typelist,
3472 parmnum = 0;
3473 values && values->iterate (parmnum, &val);
3474 ++parmnum)
3475 {
3476 /* The type of the function parameter (if it was declared with one). */
3477 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3478 /* The type of the built-in function parameter (if the function
3479 is a built-in). Used to detect type incompatibilities in
3480 calls to built-ins declared without a prototype. */
3481 tree builtin_type = (builtin_typetail
3482 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3483 /* The original type of the argument being passed to the function. */
3484 tree valtype = TREE_TYPE (val);
3485 /* The called function (or function selector in Objective C). */
3486 tree rname = function;
3487 int argnum = parmnum + 1;
3488 const char *invalid_func_diag;
3489 /* Set for EXCESS_PRECISION_EXPR arguments. */
3490 bool excess_precision = false;
3491 /* The value of the argument after conversion to the type
3492 of the function parameter it is passed to. */
3493 tree parmval;
3494 /* Some __atomic_* builtins have additional hidden argument at
3495 position 0. */
3496 location_t ploc
3497 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3498 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3499 : input_location;
3500
3501 if (type == void_type_node)
3502 {
3503 if (selector)
3504 error_at (loc, "too many arguments to method %qE", selector);
3505 else
3506 error_at (loc, "too many arguments to function %qE", function);
3507 inform_declaration (fundecl);
3508 return error_args ? -1 : (int) parmnum;
3509 }
3510
3511 if (builtin_type == void_type_node)
3512 {
3513 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3514 "too many arguments to built-in function %qE "
3515 "expecting %d", function, parmnum))
3516 inform_declaration (fundecl);
3517 builtin_typetail = NULL_TREE;
3518 }
3519
3520 if (selector && argnum > 2)
3521 {
3522 rname = selector;
3523 argnum -= 2;
3524 }
3525
3526 /* Determine if VAL is a null pointer constant before folding it. */
3527 bool npc = null_pointer_constant_p (val);
3528
3529 /* If there is excess precision and a prototype, convert once to
3530 the required type rather than converting via the semantic
3531 type. Likewise without a prototype a float value represented
3532 as long double should be converted once to double. But for
3533 type-generic classification functions excess precision must
3534 be removed here. */
3535 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3536 && (type || !type_generic || !type_generic_remove_excess_precision))
3537 {
3538 val = TREE_OPERAND (val, 0);
3539 excess_precision = true;
3540 }
3541 val = c_fully_fold (val, false, NULL);
3542 STRIP_TYPE_NOPS (val);
3543
3544 val = require_complete_type (ploc, val);
3545
3546 /* Some floating-point arguments must be promoted to double when
3547 no type is specified by a prototype. This applies to
3548 arguments of type float, and to architecture-specific types
3549 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3550 bool promote_float_arg = false;
3551 if (type == NULL_TREE
3552 && TREE_CODE (valtype) == REAL_TYPE
3553 && (TYPE_PRECISION (valtype)
3554 <= TYPE_PRECISION (double_type_node))
3555 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3556 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3557 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3558 {
3559 /* Promote this argument, unless it has a _FloatN or
3560 _FloatNx type. */
3561 promote_float_arg = true;
3562 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3563 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3564 {
3565 promote_float_arg = false;
3566 break;
3567 }
3568 }
3569
3570 if (type != NULL_TREE)
3571 {
3572 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3573 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3574 val, valtype, npc, rname, parmnum, argnum,
3575 excess_precision, 0);
3576 }
3577 else if (promote_float_arg)
3578 {
3579 if (type_generic)
3580 parmval = val;
3581 else
3582 {
3583 /* Convert `float' to `double'. */
3584 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3585 warning_at (ploc, OPT_Wdouble_promotion,
3586 "implicit conversion from %qT to %qT when passing "
3587 "argument to function",
3588 valtype, double_type_node);
3589 parmval = convert (double_type_node, val);
3590 }
3591 }
3592 else if ((excess_precision && !type_generic)
3593 || (type_generic_overflow_p && parmnum == 2))
3594 /* A "double" argument with excess precision being passed
3595 without a prototype or in variable arguments.
3596 The last argument of __builtin_*_overflow_p should not be
3597 promoted. */
3598 parmval = convert (valtype, val);
3599 else if ((invalid_func_diag =
3600 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3601 {
3602 error (invalid_func_diag);
3603 return -1;
3604 }
3605 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3606 {
3607 return -1;
3608 }
3609 else
3610 /* Convert `short' and `char' to full-size `int'. */
3611 parmval = default_conversion (val);
3612
3613 (*values)[parmnum] = parmval;
3614 if (parmval == error_mark_node)
3615 error_args = true;
3616
3617 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3618 {
3619 /* For a call to a built-in function declared without a prototype,
3620 perform the conversions from the argument to the expected type
3621 but issue warnings rather than errors for any mismatches.
3622 Ignore the converted argument and use the PARMVAL obtained
3623 above by applying default conversions instead. */
3624 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3625 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3626 val, valtype, npc, rname, parmnum, argnum,
3627 excess_precision,
3628 OPT_Wbuiltin_declaration_mismatch);
3629 }
3630
3631 if (typetail)
3632 typetail = TREE_CHAIN (typetail);
3633
3634 if (builtin_typetail)
3635 builtin_typetail = TREE_CHAIN (builtin_typetail);
3636 }
3637
3638 gcc_assert (parmnum == vec_safe_length (values));
3639
3640 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3641 {
3642 error_at (loc, "too few arguments to function %qE", function);
3643 inform_declaration (fundecl);
3644 return -1;
3645 }
3646
3647 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3648 {
3649 unsigned nargs = parmnum;
3650 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3651 ++nargs;
3652
3653 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3654 "too few arguments to built-in function %qE "
3655 "expecting %u", function, nargs - 1))
3656 inform_declaration (fundecl);
3657 }
3658
3659 return error_args ? -1 : (int) parmnum;
3660 }
3661 \f
3662 /* This is the entry point used by the parser to build unary operators
3663 in the input. CODE, a tree_code, specifies the unary operator, and
3664 ARG is the operand. For unary plus, the C parser currently uses
3665 CONVERT_EXPR for code.
3666
3667 LOC is the location to use for the tree generated.
3668 */
3669
3670 struct c_expr
3671 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3672 {
3673 struct c_expr result;
3674
3675 result.original_code = code;
3676 result.original_type = NULL;
3677
3678 if (reject_gcc_builtin (arg.value))
3679 {
3680 result.value = error_mark_node;
3681 }
3682 else
3683 {
3684 result.value = build_unary_op (loc, code, arg.value, false);
3685
3686 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3687 overflow_warning (loc, result.value, arg.value);
3688 }
3689
3690 /* We are typically called when parsing a prefix token at LOC acting on
3691 ARG. Reflect this by updating the source range of the result to
3692 start at LOC and end at the end of ARG. */
3693 set_c_expr_source_range (&result,
3694 loc, arg.get_finish ());
3695
3696 return result;
3697 }
3698
3699 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3700
3701 static bool
3702 char_type_p (tree type)
3703 {
3704 return (type == char_type_node
3705 || type == unsigned_char_type_node
3706 || type == signed_char_type_node
3707 || type == char16_type_node
3708 || type == char32_type_node);
3709 }
3710
3711 /* This is the entry point used by the parser to build binary operators
3712 in the input. CODE, a tree_code, specifies the binary operator, and
3713 ARG1 and ARG2 are the operands. In addition to constructing the
3714 expression, we check for operands that were written with other binary
3715 operators in a way that is likely to confuse the user.
3716
3717 LOCATION is the location of the binary operator. */
3718
3719 struct c_expr
3720 parser_build_binary_op (location_t location, enum tree_code code,
3721 struct c_expr arg1, struct c_expr arg2)
3722 {
3723 struct c_expr result;
3724
3725 enum tree_code code1 = arg1.original_code;
3726 enum tree_code code2 = arg2.original_code;
3727 tree type1 = (arg1.original_type
3728 ? arg1.original_type
3729 : TREE_TYPE (arg1.value));
3730 tree type2 = (arg2.original_type
3731 ? arg2.original_type
3732 : TREE_TYPE (arg2.value));
3733
3734 result.value = build_binary_op (location, code,
3735 arg1.value, arg2.value, true);
3736 result.original_code = code;
3737 result.original_type = NULL;
3738
3739 if (TREE_CODE (result.value) == ERROR_MARK)
3740 {
3741 set_c_expr_source_range (&result,
3742 arg1.get_start (),
3743 arg2.get_finish ());
3744 return result;
3745 }
3746
3747 if (location != UNKNOWN_LOCATION)
3748 protected_set_expr_location (result.value, location);
3749
3750 set_c_expr_source_range (&result,
3751 arg1.get_start (),
3752 arg2.get_finish ());
3753
3754 /* Check for cases such as x+y<<z which users are likely
3755 to misinterpret. */
3756 if (warn_parentheses)
3757 warn_about_parentheses (location, code, code1, arg1.value, code2,
3758 arg2.value);
3759
3760 if (warn_logical_op)
3761 warn_logical_operator (location, code, TREE_TYPE (result.value),
3762 code1, arg1.value, code2, arg2.value);
3763
3764 if (warn_tautological_compare)
3765 {
3766 tree lhs = arg1.value;
3767 tree rhs = arg2.value;
3768 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3769 {
3770 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3771 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3772 lhs = NULL_TREE;
3773 else
3774 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3775 }
3776 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3777 {
3778 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3779 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3780 rhs = NULL_TREE;
3781 else
3782 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3783 }
3784 if (lhs != NULL_TREE && rhs != NULL_TREE)
3785 warn_tautological_cmp (location, code, lhs, rhs);
3786 }
3787
3788 if (warn_logical_not_paren
3789 && TREE_CODE_CLASS (code) == tcc_comparison
3790 && code1 == TRUTH_NOT_EXPR
3791 && code2 != TRUTH_NOT_EXPR
3792 /* Avoid warning for !!x == y. */
3793 && (TREE_CODE (arg1.value) != NE_EXPR
3794 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3795 {
3796 /* Avoid warning for !b == y where b has _Bool type. */
3797 tree t = integer_zero_node;
3798 if (TREE_CODE (arg1.value) == EQ_EXPR
3799 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3800 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3801 {
3802 t = TREE_OPERAND (arg1.value, 0);
3803 do
3804 {
3805 if (TREE_TYPE (t) != integer_type_node)
3806 break;
3807 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3808 t = C_MAYBE_CONST_EXPR_EXPR (t);
3809 else if (CONVERT_EXPR_P (t))
3810 t = TREE_OPERAND (t, 0);
3811 else
3812 break;
3813 }
3814 while (1);
3815 }
3816 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3817 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3818 }
3819
3820 /* Warn about comparisons against string literals, with the exception
3821 of testing for equality or inequality of a string literal with NULL. */
3822 if (code == EQ_EXPR || code == NE_EXPR)
3823 {
3824 if ((code1 == STRING_CST
3825 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3826 || (code2 == STRING_CST
3827 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3828 warning_at (location, OPT_Waddress,
3829 "comparison with string literal results in unspecified behavior");
3830 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3831 if (POINTER_TYPE_P (type1)
3832 && null_pointer_constant_p (arg2.value)
3833 && char_type_p (type2))
3834 {
3835 auto_diagnostic_group d;
3836 if (warning_at (location, OPT_Wpointer_compare,
3837 "comparison between pointer and zero character "
3838 "constant"))
3839 inform (arg1.get_start (),
3840 "did you mean to dereference the pointer?");
3841 }
3842 else if (POINTER_TYPE_P (type2)
3843 && null_pointer_constant_p (arg1.value)
3844 && char_type_p (type1))
3845 {
3846 auto_diagnostic_group d;
3847 if (warning_at (location, OPT_Wpointer_compare,
3848 "comparison between pointer and zero character "
3849 "constant"))
3850 inform (arg2.get_start (),
3851 "did you mean to dereference the pointer?");
3852 }
3853 }
3854 else if (TREE_CODE_CLASS (code) == tcc_comparison
3855 && (code1 == STRING_CST || code2 == STRING_CST))
3856 warning_at (location, OPT_Waddress,
3857 "comparison with string literal results in unspecified behavior");
3858
3859 if (TREE_OVERFLOW_P (result.value)
3860 && !TREE_OVERFLOW_P (arg1.value)
3861 && !TREE_OVERFLOW_P (arg2.value))
3862 overflow_warning (location, result.value);
3863
3864 /* Warn about comparisons of different enum types. */
3865 if (warn_enum_compare
3866 && TREE_CODE_CLASS (code) == tcc_comparison
3867 && TREE_CODE (type1) == ENUMERAL_TYPE
3868 && TREE_CODE (type2) == ENUMERAL_TYPE
3869 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3870 warning_at (location, OPT_Wenum_compare,
3871 "comparison between %qT and %qT",
3872 type1, type2);
3873
3874 return result;
3875 }
3876 \f
3877 /* Return a tree for the difference of pointers OP0 and OP1.
3878 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3879 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3880
3881 static tree
3882 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3883 {
3884 tree restype = ptrdiff_type_node;
3885 tree result, inttype;
3886
3887 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3888 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3889 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3890 tree orig_op1 = op1;
3891
3892 /* If the operands point into different address spaces, we need to
3893 explicitly convert them to pointers into the common address space
3894 before we can subtract the numerical address values. */
3895 if (as0 != as1)
3896 {
3897 addr_space_t as_common;
3898 tree common_type;
3899
3900 /* Determine the common superset address space. This is guaranteed
3901 to exist because the caller verified that comp_target_types
3902 returned non-zero. */
3903 if (!addr_space_superset (as0, as1, &as_common))
3904 gcc_unreachable ();
3905
3906 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3907 op0 = convert (common_type, op0);
3908 op1 = convert (common_type, op1);
3909 }
3910
3911 /* Determine integer type result of the subtraction. This will usually
3912 be the same as the result type (ptrdiff_t), but may need to be a wider
3913 type if pointers for the address space are wider than ptrdiff_t. */
3914 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3915 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3916 else
3917 inttype = restype;
3918
3919 if (TREE_CODE (target_type) == VOID_TYPE)
3920 pedwarn (loc, OPT_Wpointer_arith,
3921 "pointer of type %<void *%> used in subtraction");
3922 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3923 pedwarn (loc, OPT_Wpointer_arith,
3924 "pointer to a function used in subtraction");
3925
3926 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3927 {
3928 gcc_assert (current_function_decl != NULL_TREE);
3929
3930 op0 = save_expr (op0);
3931 op1 = save_expr (op1);
3932
3933 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3934 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3935 }
3936
3937 /* First do the subtraction, then build the divide operator
3938 and only convert at the very end.
3939 Do not do default conversions in case restype is a short type. */
3940
3941 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3942 pointers. If some platform cannot provide that, or has a larger
3943 ptrdiff_type to support differences larger than half the address
3944 space, cast the pointers to some larger integer type and do the
3945 computations in that type. */
3946 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3947 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3948 convert (inttype, op1), false);
3949 else
3950 {
3951 /* Cast away qualifiers. */
3952 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
3953 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
3954 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3955 }
3956
3957 /* This generates an error if op1 is pointer to incomplete type. */
3958 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3959 error_at (loc, "arithmetic on pointer to an incomplete type");
3960
3961 op1 = c_size_in_bytes (target_type);
3962
3963 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3964 error_at (loc, "arithmetic on pointer to an empty aggregate");
3965
3966 /* Divide by the size, in easiest possible way. */
3967 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3968 op0, convert (inttype, op1));
3969
3970 /* Convert to final result type if necessary. */
3971 return convert (restype, result);
3972 }
3973 \f
3974 /* Expand atomic compound assignments into an appropriate sequence as
3975 specified by the C11 standard section 6.5.16.2.
3976
3977 _Atomic T1 E1
3978 T2 E2
3979 E1 op= E2
3980
3981 This sequence is used for all types for which these operations are
3982 supported.
3983
3984 In addition, built-in versions of the 'fe' prefixed routines may
3985 need to be invoked for floating point (real, complex or vector) when
3986 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3987
3988 T1 newval;
3989 T1 old;
3990 T1 *addr
3991 T2 val
3992 fenv_t fenv
3993
3994 addr = &E1;
3995 val = (E2);
3996 __atomic_load (addr, &old, SEQ_CST);
3997 feholdexcept (&fenv);
3998 loop:
3999 newval = old op val;
4000 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4001 SEQ_CST))
4002 goto done;
4003 feclearexcept (FE_ALL_EXCEPT);
4004 goto loop:
4005 done:
4006 feupdateenv (&fenv);
4007
4008 The compiler will issue the __atomic_fetch_* built-in when possible,
4009 otherwise it will generate the generic form of the atomic operations.
4010 This requires temp(s) and has their address taken. The atomic processing
4011 is smart enough to figure out when the size of an object can utilize
4012 a lock-free version, and convert the built-in call to the appropriate
4013 lock-free routine. The optimizers will then dispose of any temps that
4014 are no longer required, and lock-free implementations are utilized as
4015 long as there is target support for the required size.
4016
4017 If the operator is NOP_EXPR, then this is a simple assignment, and
4018 an __atomic_store is issued to perform the assignment rather than
4019 the above loop. */
4020
4021 /* Build an atomic assignment at LOC, expanding into the proper
4022 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4023 the result of the operation, unless RETURN_OLD_P, in which case
4024 return the old value of LHS (this is only for postincrement and
4025 postdecrement). */
4026
4027 static tree
4028 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4029 tree rhs, bool return_old_p)
4030 {
4031 tree fndecl, func_call;
4032 vec<tree, va_gc> *params;
4033 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4034 tree old, old_addr;
4035 tree compound_stmt;
4036 tree stmt, goto_stmt;
4037 tree loop_label, loop_decl, done_label, done_decl;
4038
4039 tree lhs_type = TREE_TYPE (lhs);
4040 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4041 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4042 tree rhs_semantic_type = TREE_TYPE (rhs);
4043 tree nonatomic_rhs_semantic_type;
4044 tree rhs_type;
4045
4046 gcc_assert (TYPE_ATOMIC (lhs_type));
4047
4048 if (return_old_p)
4049 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4050
4051 /* Allocate enough vector items for a compare_exchange. */
4052 vec_alloc (params, 6);
4053
4054 /* Create a compound statement to hold the sequence of statements
4055 with a loop. */
4056 compound_stmt = c_begin_compound_stmt (false);
4057
4058 /* Remove any excess precision (which is only present here in the
4059 case of compound assignments). */
4060 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4061 {
4062 gcc_assert (modifycode != NOP_EXPR);
4063 rhs = TREE_OPERAND (rhs, 0);
4064 }
4065 rhs_type = TREE_TYPE (rhs);
4066
4067 /* Fold the RHS if it hasn't already been folded. */
4068 if (modifycode != NOP_EXPR)
4069 rhs = c_fully_fold (rhs, false, NULL);
4070
4071 /* Remove the qualifiers for the rest of the expressions and create
4072 the VAL temp variable to hold the RHS. */
4073 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4074 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4075 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4076 TYPE_UNQUALIFIED);
4077 val = create_tmp_var_raw (nonatomic_rhs_type);
4078 TREE_ADDRESSABLE (val) = 1;
4079 TREE_NO_WARNING (val) = 1;
4080 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4081 NULL_TREE);
4082 SET_EXPR_LOCATION (rhs, loc);
4083 add_stmt (rhs);
4084
4085 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4086 an atomic_store. */
4087 if (modifycode == NOP_EXPR)
4088 {
4089 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4090 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4091 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4092 params->quick_push (lhs_addr);
4093 params->quick_push (rhs);
4094 params->quick_push (seq_cst);
4095 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4096 add_stmt (func_call);
4097
4098 /* Finish the compound statement. */
4099 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4100
4101 /* VAL is the value which was stored, return a COMPOUND_STMT of
4102 the statement and that value. */
4103 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4104 }
4105
4106 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4107 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4108 isn't applicable for such builtins. ??? Do we want to handle enums? */
4109 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4110 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4111 {
4112 built_in_function fncode;
4113 switch (modifycode)
4114 {
4115 case PLUS_EXPR:
4116 case POINTER_PLUS_EXPR:
4117 fncode = (return_old_p
4118 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4119 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4120 break;
4121 case MINUS_EXPR:
4122 fncode = (return_old_p
4123 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4124 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4125 break;
4126 case BIT_AND_EXPR:
4127 fncode = (return_old_p
4128 ? BUILT_IN_ATOMIC_FETCH_AND_N
4129 : BUILT_IN_ATOMIC_AND_FETCH_N);
4130 break;
4131 case BIT_IOR_EXPR:
4132 fncode = (return_old_p
4133 ? BUILT_IN_ATOMIC_FETCH_OR_N
4134 : BUILT_IN_ATOMIC_OR_FETCH_N);
4135 break;
4136 case BIT_XOR_EXPR:
4137 fncode = (return_old_p
4138 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4139 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4140 break;
4141 default:
4142 goto cas_loop;
4143 }
4144
4145 /* We can only use "_1" through "_16" variants of the atomic fetch
4146 built-ins. */
4147 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4148 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4149 goto cas_loop;
4150
4151 /* If this is a pointer type, we need to multiply by the size of
4152 the pointer target type. */
4153 if (POINTER_TYPE_P (lhs_type))
4154 {
4155 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4156 /* ??? This would introduce -Wdiscarded-qualifiers
4157 warning: __atomic_fetch_* expect volatile void *
4158 type as the first argument. (Assignments between
4159 atomic and non-atomic objects are OK.) */
4160 || TYPE_RESTRICT (lhs_type))
4161 goto cas_loop;
4162 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4163 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4164 convert (ptrdiff_type_node, rhs),
4165 convert (ptrdiff_type_node, sz));
4166 }
4167
4168 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4169 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4170 fndecl = builtin_decl_explicit (fncode);
4171 params->quick_push (lhs_addr);
4172 params->quick_push (rhs);
4173 params->quick_push (seq_cst);
4174 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4175
4176 newval = create_tmp_var_raw (nonatomic_lhs_type);
4177 TREE_ADDRESSABLE (newval) = 1;
4178 TREE_NO_WARNING (newval) = 1;
4179 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4180 NULL_TREE, NULL_TREE);
4181 SET_EXPR_LOCATION (rhs, loc);
4182 add_stmt (rhs);
4183
4184 /* Finish the compound statement. */
4185 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4186
4187 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4188 the statement and that value. */
4189 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4190 }
4191
4192 cas_loop:
4193 /* Create the variables and labels required for the op= form. */
4194 old = create_tmp_var_raw (nonatomic_lhs_type);
4195 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4196 TREE_ADDRESSABLE (old) = 1;
4197 TREE_NO_WARNING (old) = 1;
4198
4199 newval = create_tmp_var_raw (nonatomic_lhs_type);
4200 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4201 TREE_ADDRESSABLE (newval) = 1;
4202 TREE_NO_WARNING (newval) = 1;
4203
4204 loop_decl = create_artificial_label (loc);
4205 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4206
4207 done_decl = create_artificial_label (loc);
4208 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4209
4210 /* __atomic_load (addr, &old, SEQ_CST). */
4211 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4212 params->quick_push (lhs_addr);
4213 params->quick_push (old_addr);
4214 params->quick_push (seq_cst);
4215 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4216 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4217 NULL_TREE);
4218 add_stmt (old);
4219 params->truncate (0);
4220
4221 /* Create the expressions for floating-point environment
4222 manipulation, if required. */
4223 bool need_fenv = (flag_trapping_math
4224 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4225 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4226 if (need_fenv)
4227 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4228
4229 if (hold_call)
4230 add_stmt (hold_call);
4231
4232 /* loop: */
4233 add_stmt (loop_label);
4234
4235 /* newval = old + val; */
4236 if (rhs_type != rhs_semantic_type)
4237 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4238 rhs = build_binary_op (loc, modifycode, old, val, true);
4239 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4240 {
4241 tree eptype = TREE_TYPE (rhs);
4242 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4243 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4244 }
4245 else
4246 rhs = c_fully_fold (rhs, false, NULL);
4247 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4248 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4249 NULL_TREE, 0);
4250 if (rhs != error_mark_node)
4251 {
4252 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4253 NULL_TREE);
4254 SET_EXPR_LOCATION (rhs, loc);
4255 add_stmt (rhs);
4256 }
4257
4258 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4259 goto done; */
4260 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4261 params->quick_push (lhs_addr);
4262 params->quick_push (old_addr);
4263 params->quick_push (newval_addr);
4264 params->quick_push (integer_zero_node);
4265 params->quick_push (seq_cst);
4266 params->quick_push (seq_cst);
4267 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4268
4269 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4270 SET_EXPR_LOCATION (goto_stmt, loc);
4271
4272 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4273 SET_EXPR_LOCATION (stmt, loc);
4274 add_stmt (stmt);
4275
4276 if (clear_call)
4277 add_stmt (clear_call);
4278
4279 /* goto loop; */
4280 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4281 SET_EXPR_LOCATION (goto_stmt, loc);
4282 add_stmt (goto_stmt);
4283
4284 /* done: */
4285 add_stmt (done_label);
4286
4287 if (update_call)
4288 add_stmt (update_call);
4289
4290 /* Finish the compound statement. */
4291 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4292
4293 /* NEWVAL is the value that was successfully stored, return a
4294 COMPOUND_EXPR of the statement and the appropriate value. */
4295 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4296 return_old_p ? old : newval);
4297 }
4298
4299 /* Construct and perhaps optimize a tree representation
4300 for a unary operation. CODE, a tree_code, specifies the operation
4301 and XARG is the operand.
4302 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4303 promotions (such as from short to int).
4304 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4305 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4306 to pointers in C99.
4307
4308 LOCATION is the location of the operator. */
4309
4310 tree
4311 build_unary_op (location_t location, enum tree_code code, tree xarg,
4312 bool noconvert)
4313 {
4314 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4315 tree arg = xarg;
4316 tree argtype = NULL_TREE;
4317 enum tree_code typecode;
4318 tree val;
4319 tree ret = error_mark_node;
4320 tree eptype = NULL_TREE;
4321 const char *invalid_op_diag;
4322 bool int_operands;
4323
4324 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4325 if (int_operands)
4326 arg = remove_c_maybe_const_expr (arg);
4327
4328 if (code != ADDR_EXPR)
4329 arg = require_complete_type (location, arg);
4330
4331 typecode = TREE_CODE (TREE_TYPE (arg));
4332 if (typecode == ERROR_MARK)
4333 return error_mark_node;
4334 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4335 typecode = INTEGER_TYPE;
4336
4337 if ((invalid_op_diag
4338 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4339 {
4340 error_at (location, invalid_op_diag);
4341 return error_mark_node;
4342 }
4343
4344 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4345 {
4346 eptype = TREE_TYPE (arg);
4347 arg = TREE_OPERAND (arg, 0);
4348 }
4349
4350 switch (code)
4351 {
4352 case CONVERT_EXPR:
4353 /* This is used for unary plus, because a CONVERT_EXPR
4354 is enough to prevent anybody from looking inside for
4355 associativity, but won't generate any code. */
4356 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4357 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4358 || typecode == VECTOR_TYPE))
4359 {
4360 error_at (location, "wrong type argument to unary plus");
4361 return error_mark_node;
4362 }
4363 else if (!noconvert)
4364 arg = default_conversion (arg);
4365 arg = non_lvalue_loc (location, arg);
4366 break;
4367
4368 case NEGATE_EXPR:
4369 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4370 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4371 || typecode == VECTOR_TYPE))
4372 {
4373 error_at (location, "wrong type argument to unary minus");
4374 return error_mark_node;
4375 }
4376 else if (!noconvert)
4377 arg = default_conversion (arg);
4378 break;
4379
4380 case BIT_NOT_EXPR:
4381 /* ~ works on integer types and non float vectors. */
4382 if (typecode == INTEGER_TYPE
4383 || (typecode == VECTOR_TYPE
4384 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4385 {
4386 tree e = arg;
4387
4388 /* Warn if the expression has boolean value. */
4389 while (TREE_CODE (e) == COMPOUND_EXPR)
4390 e = TREE_OPERAND (e, 1);
4391
4392 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4393 || truth_value_p (TREE_CODE (e))))
4394 {
4395 auto_diagnostic_group d;
4396 if (warning_at (location, OPT_Wbool_operation,
4397 "%<~%> on a boolean expression"))
4398 {
4399 gcc_rich_location richloc (location);
4400 richloc.add_fixit_insert_before (location, "!");
4401 inform (&richloc, "did you mean to use logical not?");
4402 }
4403 }
4404 if (!noconvert)
4405 arg = default_conversion (arg);
4406 }
4407 else if (typecode == COMPLEX_TYPE)
4408 {
4409 code = CONJ_EXPR;
4410 pedwarn (location, OPT_Wpedantic,
4411 "ISO C does not support %<~%> for complex conjugation");
4412 if (!noconvert)
4413 arg = default_conversion (arg);
4414 }
4415 else
4416 {
4417 error_at (location, "wrong type argument to bit-complement");
4418 return error_mark_node;
4419 }
4420 break;
4421
4422 case ABS_EXPR:
4423 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4424 {
4425 error_at (location, "wrong type argument to abs");
4426 return error_mark_node;
4427 }
4428 else if (!noconvert)
4429 arg = default_conversion (arg);
4430 break;
4431
4432 case ABSU_EXPR:
4433 if (!(typecode == INTEGER_TYPE))
4434 {
4435 error_at (location, "wrong type argument to absu");
4436 return error_mark_node;
4437 }
4438 else if (!noconvert)
4439 arg = default_conversion (arg);
4440 break;
4441
4442 case CONJ_EXPR:
4443 /* Conjugating a real value is a no-op, but allow it anyway. */
4444 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4445 || typecode == COMPLEX_TYPE))
4446 {
4447 error_at (location, "wrong type argument to conjugation");
4448 return error_mark_node;
4449 }
4450 else if (!noconvert)
4451 arg = default_conversion (arg);
4452 break;
4453
4454 case TRUTH_NOT_EXPR:
4455 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4456 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4457 && typecode != COMPLEX_TYPE)
4458 {
4459 error_at (location,
4460 "wrong type argument to unary exclamation mark");
4461 return error_mark_node;
4462 }
4463 if (int_operands)
4464 {
4465 arg = c_objc_common_truthvalue_conversion (location, xarg);
4466 arg = remove_c_maybe_const_expr (arg);
4467 }
4468 else
4469 arg = c_objc_common_truthvalue_conversion (location, arg);
4470 ret = invert_truthvalue_loc (location, arg);
4471 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4472 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4473 location = EXPR_LOCATION (ret);
4474 goto return_build_unary_op;
4475
4476 case REALPART_EXPR:
4477 case IMAGPART_EXPR:
4478 ret = build_real_imag_expr (location, code, arg);
4479 if (ret == error_mark_node)
4480 return error_mark_node;
4481 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4482 eptype = TREE_TYPE (eptype);
4483 goto return_build_unary_op;
4484
4485 case PREINCREMENT_EXPR:
4486 case POSTINCREMENT_EXPR:
4487 case PREDECREMENT_EXPR:
4488 case POSTDECREMENT_EXPR:
4489
4490 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4491 {
4492 tree inner = build_unary_op (location, code,
4493 C_MAYBE_CONST_EXPR_EXPR (arg),
4494 noconvert);
4495 if (inner == error_mark_node)
4496 return error_mark_node;
4497 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4498 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4499 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4500 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4501 goto return_build_unary_op;
4502 }
4503
4504 /* Complain about anything that is not a true lvalue. In
4505 Objective-C, skip this check for property_refs. */
4506 if (!objc_is_property_ref (arg)
4507 && !lvalue_or_else (location,
4508 arg, ((code == PREINCREMENT_EXPR
4509 || code == POSTINCREMENT_EXPR)
4510 ? lv_increment
4511 : lv_decrement)))
4512 return error_mark_node;
4513
4514 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4515 {
4516 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4517 warning_at (location, OPT_Wc___compat,
4518 "increment of enumeration value is invalid in C++");
4519 else
4520 warning_at (location, OPT_Wc___compat,
4521 "decrement of enumeration value is invalid in C++");
4522 }
4523
4524 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4525 {
4526 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4527 warning_at (location, OPT_Wbool_operation,
4528 "increment of a boolean expression");
4529 else
4530 warning_at (location, OPT_Wbool_operation,
4531 "decrement of a boolean expression");
4532 }
4533
4534 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4535 arg = c_fully_fold (arg, false, NULL, true);
4536
4537 bool atomic_op;
4538 atomic_op = really_atomic_lvalue (arg);
4539
4540 /* Increment or decrement the real part of the value,
4541 and don't change the imaginary part. */
4542 if (typecode == COMPLEX_TYPE)
4543 {
4544 tree real, imag;
4545
4546 pedwarn (location, OPT_Wpedantic,
4547 "ISO C does not support %<++%> and %<--%> on complex types");
4548
4549 if (!atomic_op)
4550 {
4551 arg = stabilize_reference (arg);
4552 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4553 true);
4554 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4555 true);
4556 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4557 if (real == error_mark_node || imag == error_mark_node)
4558 return error_mark_node;
4559 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4560 real, imag);
4561 goto return_build_unary_op;
4562 }
4563 }
4564
4565 /* Report invalid types. */
4566
4567 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4568 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4569 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4570 {
4571 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4572 error_at (location, "wrong type argument to increment");
4573 else
4574 error_at (location, "wrong type argument to decrement");
4575
4576 return error_mark_node;
4577 }
4578
4579 {
4580 tree inc;
4581
4582 argtype = TREE_TYPE (arg);
4583
4584 /* Compute the increment. */
4585
4586 if (typecode == POINTER_TYPE)
4587 {
4588 /* If pointer target is an incomplete type,
4589 we just cannot know how to do the arithmetic. */
4590 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4591 {
4592 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4593 error_at (location,
4594 "increment of pointer to an incomplete type %qT",
4595 TREE_TYPE (argtype));
4596 else
4597 error_at (location,
4598 "decrement of pointer to an incomplete type %qT",
4599 TREE_TYPE (argtype));
4600 }
4601 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4602 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4603 {
4604 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4605 pedwarn (location, OPT_Wpointer_arith,
4606 "wrong type argument to increment");
4607 else
4608 pedwarn (location, OPT_Wpointer_arith,
4609 "wrong type argument to decrement");
4610 }
4611
4612 inc = c_size_in_bytes (TREE_TYPE (argtype));
4613 inc = convert_to_ptrofftype_loc (location, inc);
4614 }
4615 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4616 {
4617 /* For signed fract types, we invert ++ to -- or
4618 -- to ++, and change inc from 1 to -1, because
4619 it is not possible to represent 1 in signed fract constants.
4620 For unsigned fract types, the result always overflows and
4621 we get an undefined (original) or the maximum value. */
4622 if (code == PREINCREMENT_EXPR)
4623 code = PREDECREMENT_EXPR;
4624 else if (code == PREDECREMENT_EXPR)
4625 code = PREINCREMENT_EXPR;
4626 else if (code == POSTINCREMENT_EXPR)
4627 code = POSTDECREMENT_EXPR;
4628 else /* code == POSTDECREMENT_EXPR */
4629 code = POSTINCREMENT_EXPR;
4630
4631 inc = integer_minus_one_node;
4632 inc = convert (argtype, inc);
4633 }
4634 else
4635 {
4636 inc = VECTOR_TYPE_P (argtype)
4637 ? build_one_cst (argtype)
4638 : integer_one_node;
4639 inc = convert (argtype, inc);
4640 }
4641
4642 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4643 need to ask Objective-C to build the increment or decrement
4644 expression for it. */
4645 if (objc_is_property_ref (arg))
4646 return objc_build_incr_expr_for_property_ref (location, code,
4647 arg, inc);
4648
4649 /* Report a read-only lvalue. */
4650 if (TYPE_READONLY (argtype))
4651 {
4652 readonly_error (location, arg,
4653 ((code == PREINCREMENT_EXPR
4654 || code == POSTINCREMENT_EXPR)
4655 ? lv_increment : lv_decrement));
4656 return error_mark_node;
4657 }
4658 else if (TREE_READONLY (arg))
4659 readonly_warning (arg,
4660 ((code == PREINCREMENT_EXPR
4661 || code == POSTINCREMENT_EXPR)
4662 ? lv_increment : lv_decrement));
4663
4664 /* If the argument is atomic, use the special code sequences for
4665 atomic compound assignment. */
4666 if (atomic_op)
4667 {
4668 arg = stabilize_reference (arg);
4669 ret = build_atomic_assign (location, arg,
4670 ((code == PREINCREMENT_EXPR
4671 || code == POSTINCREMENT_EXPR)
4672 ? PLUS_EXPR
4673 : MINUS_EXPR),
4674 (FRACT_MODE_P (TYPE_MODE (argtype))
4675 ? inc
4676 : integer_one_node),
4677 (code == POSTINCREMENT_EXPR
4678 || code == POSTDECREMENT_EXPR));
4679 goto return_build_unary_op;
4680 }
4681
4682 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4683 val = boolean_increment (code, arg);
4684 else
4685 val = build2 (code, TREE_TYPE (arg), arg, inc);
4686 TREE_SIDE_EFFECTS (val) = 1;
4687 if (TREE_CODE (val) != code)
4688 TREE_NO_WARNING (val) = 1;
4689 ret = val;
4690 goto return_build_unary_op;
4691 }
4692
4693 case ADDR_EXPR:
4694 /* Note that this operation never does default_conversion. */
4695
4696 /* The operand of unary '&' must be an lvalue (which excludes
4697 expressions of type void), or, in C99, the result of a [] or
4698 unary '*' operator. */
4699 if (VOID_TYPE_P (TREE_TYPE (arg))
4700 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4701 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4702 pedwarn (location, 0, "taking address of expression of type %<void%>");
4703
4704 /* Let &* cancel out to simplify resulting code. */
4705 if (INDIRECT_REF_P (arg))
4706 {
4707 /* Don't let this be an lvalue. */
4708 if (lvalue_p (TREE_OPERAND (arg, 0)))
4709 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4710 ret = TREE_OPERAND (arg, 0);
4711 goto return_build_unary_op;
4712 }
4713
4714 /* Anything not already handled and not a true memory reference
4715 or a non-lvalue array is an error. */
4716 if (typecode != FUNCTION_TYPE && !noconvert
4717 && !lvalue_or_else (location, arg, lv_addressof))
4718 return error_mark_node;
4719
4720 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4721 folding later. */
4722 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4723 {
4724 tree inner = build_unary_op (location, code,
4725 C_MAYBE_CONST_EXPR_EXPR (arg),
4726 noconvert);
4727 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4728 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4729 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4730 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4731 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4732 goto return_build_unary_op;
4733 }
4734
4735 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4736 argtype = TREE_TYPE (arg);
4737
4738 /* If the lvalue is const or volatile, merge that into the type
4739 to which the address will point. This is only needed
4740 for function types. */
4741 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4742 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4743 && TREE_CODE (argtype) == FUNCTION_TYPE)
4744 {
4745 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4746 int quals = orig_quals;
4747
4748 if (TREE_READONLY (arg))
4749 quals |= TYPE_QUAL_CONST;
4750 if (TREE_THIS_VOLATILE (arg))
4751 quals |= TYPE_QUAL_VOLATILE;
4752
4753 argtype = c_build_qualified_type (argtype, quals);
4754 }
4755
4756 switch (TREE_CODE (arg))
4757 {
4758 case COMPONENT_REF:
4759 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4760 {
4761 error_at (location, "cannot take address of bit-field %qD",
4762 TREE_OPERAND (arg, 1));
4763 return error_mark_node;
4764 }
4765
4766 /* fall through */
4767
4768 case ARRAY_REF:
4769 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4770 {
4771 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4772 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4773 {
4774 error_at (location, "cannot take address of scalar with "
4775 "reverse storage order");
4776 return error_mark_node;
4777 }
4778
4779 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4780 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4781 warning_at (location, OPT_Wscalar_storage_order,
4782 "address of array with reverse scalar storage "
4783 "order requested");
4784 }
4785
4786 default:
4787 break;
4788 }
4789
4790 if (!c_mark_addressable (arg))
4791 return error_mark_node;
4792
4793 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4794 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4795
4796 argtype = build_pointer_type (argtype);
4797
4798 /* ??? Cope with user tricks that amount to offsetof. Delete this
4799 when we have proper support for integer constant expressions. */
4800 val = get_base_address (arg);
4801 if (val && INDIRECT_REF_P (val)
4802 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4803 {
4804 ret = fold_offsetof (arg, argtype);
4805 goto return_build_unary_op;
4806 }
4807
4808 val = build1 (ADDR_EXPR, argtype, arg);
4809
4810 ret = val;
4811 goto return_build_unary_op;
4812
4813 default:
4814 gcc_unreachable ();
4815 }
4816
4817 if (argtype == NULL_TREE)
4818 argtype = TREE_TYPE (arg);
4819 if (TREE_CODE (arg) == INTEGER_CST)
4820 ret = (require_constant_value
4821 ? fold_build1_initializer_loc (location, code, argtype, arg)
4822 : fold_build1_loc (location, code, argtype, arg));
4823 else
4824 ret = build1 (code, argtype, arg);
4825 return_build_unary_op:
4826 gcc_assert (ret != error_mark_node);
4827 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4828 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4829 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4830 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4831 ret = note_integer_operands (ret);
4832 if (eptype)
4833 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4834 protected_set_expr_location (ret, location);
4835 return ret;
4836 }
4837
4838 /* Return nonzero if REF is an lvalue valid for this language.
4839 Lvalues can be assigned, unless their type has TYPE_READONLY.
4840 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4841
4842 bool
4843 lvalue_p (const_tree ref)
4844 {
4845 const enum tree_code code = TREE_CODE (ref);
4846
4847 switch (code)
4848 {
4849 case REALPART_EXPR:
4850 case IMAGPART_EXPR:
4851 case COMPONENT_REF:
4852 return lvalue_p (TREE_OPERAND (ref, 0));
4853
4854 case C_MAYBE_CONST_EXPR:
4855 return lvalue_p (TREE_OPERAND (ref, 1));
4856
4857 case COMPOUND_LITERAL_EXPR:
4858 case STRING_CST:
4859 return true;
4860
4861 case INDIRECT_REF:
4862 case ARRAY_REF:
4863 case VAR_DECL:
4864 case PARM_DECL:
4865 case RESULT_DECL:
4866 case ERROR_MARK:
4867 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4868 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4869
4870 case BIND_EXPR:
4871 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4872
4873 default:
4874 return false;
4875 }
4876 }
4877 \f
4878 /* Give a warning for storing in something that is read-only in GCC
4879 terms but not const in ISO C terms. */
4880
4881 static void
4882 readonly_warning (tree arg, enum lvalue_use use)
4883 {
4884 switch (use)
4885 {
4886 case lv_assign:
4887 warning (0, "assignment of read-only location %qE", arg);
4888 break;
4889 case lv_increment:
4890 warning (0, "increment of read-only location %qE", arg);
4891 break;
4892 case lv_decrement:
4893 warning (0, "decrement of read-only location %qE", arg);
4894 break;
4895 default:
4896 gcc_unreachable ();
4897 }
4898 return;
4899 }
4900
4901
4902 /* Return nonzero if REF is an lvalue valid for this language;
4903 otherwise, print an error message and return zero. USE says
4904 how the lvalue is being used and so selects the error message.
4905 LOCATION is the location at which any error should be reported. */
4906
4907 static int
4908 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4909 {
4910 int win = lvalue_p (ref);
4911
4912 if (!win)
4913 lvalue_error (loc, use);
4914
4915 return win;
4916 }
4917 \f
4918 /* Mark EXP saying that we need to be able to take the
4919 address of it; it should not be allocated in a register.
4920 Returns true if successful. ARRAY_REF_P is true if this
4921 is for ARRAY_REF construction - in that case we don't want
4922 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4923 it is fine to use ARRAY_REFs for vector subscripts on vector
4924 register variables. */
4925
4926 bool
4927 c_mark_addressable (tree exp, bool array_ref_p)
4928 {
4929 tree x = exp;
4930
4931 while (1)
4932 switch (TREE_CODE (x))
4933 {
4934 case VIEW_CONVERT_EXPR:
4935 if (array_ref_p
4936 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4937 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4938 return true;
4939 /* FALLTHRU */
4940 case COMPONENT_REF:
4941 case ADDR_EXPR:
4942 case ARRAY_REF:
4943 case REALPART_EXPR:
4944 case IMAGPART_EXPR:
4945 x = TREE_OPERAND (x, 0);
4946 break;
4947
4948 case COMPOUND_LITERAL_EXPR:
4949 TREE_ADDRESSABLE (x) = 1;
4950 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
4951 return true;
4952
4953 case CONSTRUCTOR:
4954 TREE_ADDRESSABLE (x) = 1;
4955 return true;
4956
4957 case VAR_DECL:
4958 case CONST_DECL:
4959 case PARM_DECL:
4960 case RESULT_DECL:
4961 if (C_DECL_REGISTER (x)
4962 && DECL_NONLOCAL (x))
4963 {
4964 if (TREE_PUBLIC (x) || is_global_var (x))
4965 {
4966 error
4967 ("global register variable %qD used in nested function", x);
4968 return false;
4969 }
4970 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4971 }
4972 else if (C_DECL_REGISTER (x))
4973 {
4974 if (TREE_PUBLIC (x) || is_global_var (x))
4975 error ("address of global register variable %qD requested", x);
4976 else
4977 error ("address of register variable %qD requested", x);
4978 return false;
4979 }
4980
4981 /* FALLTHRU */
4982 case FUNCTION_DECL:
4983 TREE_ADDRESSABLE (x) = 1;
4984 /* FALLTHRU */
4985 default:
4986 return true;
4987 }
4988 }
4989 \f
4990 /* Convert EXPR to TYPE, warning about conversion problems with
4991 constants. SEMANTIC_TYPE is the type this conversion would use
4992 without excess precision. If SEMANTIC_TYPE is NULL, this function
4993 is equivalent to convert_and_check. This function is a wrapper that
4994 handles conversions that may be different than
4995 the usual ones because of excess precision. */
4996
4997 static tree
4998 ep_convert_and_check (location_t loc, tree type, tree expr,
4999 tree semantic_type)
5000 {
5001 if (TREE_TYPE (expr) == type)
5002 return expr;
5003
5004 /* For C11, integer conversions may have results with excess
5005 precision. */
5006 if (flag_isoc11 || !semantic_type)
5007 return convert_and_check (loc, type, expr);
5008
5009 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5010 && TREE_TYPE (expr) != semantic_type)
5011 {
5012 /* For integers, we need to check the real conversion, not
5013 the conversion to the excess precision type. */
5014 expr = convert_and_check (loc, semantic_type, expr);
5015 }
5016 /* Result type is the excess precision type, which should be
5017 large enough, so do not check. */
5018 return convert (type, expr);
5019 }
5020
5021 /* If EXPR refers to a built-in declared without a prototype returns
5022 the actual type of the built-in and, if non-null, set *BLTIN to
5023 a pointer to the built-in. Otherwise return the type of EXPR
5024 and clear *BLTIN if non-null. */
5025
5026 static tree
5027 type_or_builtin_type (tree expr, tree *bltin = NULL)
5028 {
5029 tree dummy;
5030 if (!bltin)
5031 bltin = &dummy;
5032
5033 *bltin = NULL_TREE;
5034
5035 tree type = TREE_TYPE (expr);
5036 if (TREE_CODE (expr) != ADDR_EXPR)
5037 return type;
5038
5039 tree oper = TREE_OPERAND (expr, 0);
5040 if (!DECL_P (oper)
5041 || TREE_CODE (oper) != FUNCTION_DECL
5042 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5043 return type;
5044
5045 built_in_function code = DECL_FUNCTION_CODE (oper);
5046 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5047 return type;
5048
5049 if ((*bltin = builtin_decl_implicit (code)))
5050 type = build_pointer_type (TREE_TYPE (*bltin));
5051
5052 return type;
5053 }
5054
5055 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5056 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5057 if folded to an integer constant then the unselected half may
5058 contain arbitrary operations not normally permitted in constant
5059 expressions. Set the location of the expression to LOC. */
5060
5061 tree
5062 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5063 tree op1, tree op1_original_type, location_t op1_loc,
5064 tree op2, tree op2_original_type, location_t op2_loc)
5065 {
5066 tree type1;
5067 tree type2;
5068 enum tree_code code1;
5069 enum tree_code code2;
5070 tree result_type = NULL;
5071 tree semantic_result_type = NULL;
5072 tree orig_op1 = op1, orig_op2 = op2;
5073 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5074 bool ifexp_int_operands;
5075 tree ret;
5076
5077 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5078 if (op1_int_operands)
5079 op1 = remove_c_maybe_const_expr (op1);
5080 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5081 if (op2_int_operands)
5082 op2 = remove_c_maybe_const_expr (op2);
5083 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5084 if (ifexp_int_operands)
5085 ifexp = remove_c_maybe_const_expr (ifexp);
5086
5087 /* Promote both alternatives. */
5088
5089 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5090 op1 = default_conversion (op1);
5091 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5092 op2 = default_conversion (op2);
5093
5094 if (TREE_CODE (ifexp) == ERROR_MARK
5095 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5096 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5097 return error_mark_node;
5098
5099 tree bltin1 = NULL_TREE;
5100 tree bltin2 = NULL_TREE;
5101 type1 = type_or_builtin_type (op1, &bltin1);
5102 code1 = TREE_CODE (type1);
5103 type2 = type_or_builtin_type (op2, &bltin2);
5104 code2 = TREE_CODE (type2);
5105
5106 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5107 return error_mark_node;
5108
5109 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5110 return error_mark_node;
5111
5112 /* C90 does not permit non-lvalue arrays in conditional expressions.
5113 In C99 they will be pointers by now. */
5114 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5115 {
5116 error_at (colon_loc, "non-lvalue array in conditional expression");
5117 return error_mark_node;
5118 }
5119
5120 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5121 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5122 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5123 || code1 == COMPLEX_TYPE)
5124 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5125 || code2 == COMPLEX_TYPE))
5126 {
5127 semantic_result_type = c_common_type (type1, type2);
5128 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5129 {
5130 op1 = TREE_OPERAND (op1, 0);
5131 type1 = TREE_TYPE (op1);
5132 gcc_assert (TREE_CODE (type1) == code1);
5133 }
5134 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5135 {
5136 op2 = TREE_OPERAND (op2, 0);
5137 type2 = TREE_TYPE (op2);
5138 gcc_assert (TREE_CODE (type2) == code2);
5139 }
5140 }
5141
5142 if (warn_cxx_compat)
5143 {
5144 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5145 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5146
5147 if (TREE_CODE (t1) == ENUMERAL_TYPE
5148 && TREE_CODE (t2) == ENUMERAL_TYPE
5149 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5150 warning_at (colon_loc, OPT_Wc___compat,
5151 ("different enum types in conditional is "
5152 "invalid in C++: %qT vs %qT"),
5153 t1, t2);
5154 }
5155
5156 /* Quickly detect the usual case where op1 and op2 have the same type
5157 after promotion. */
5158 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5159 {
5160 if (type1 == type2)
5161 result_type = type1;
5162 else
5163 result_type = TYPE_MAIN_VARIANT (type1);
5164 }
5165 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5166 || code1 == COMPLEX_TYPE)
5167 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5168 || code2 == COMPLEX_TYPE))
5169 {
5170 /* In C11, a conditional expression between a floating-point
5171 type and an integer type should convert the integer type to
5172 the evaluation format of the floating-point type, with
5173 possible excess precision. */
5174 tree eptype1 = type1;
5175 tree eptype2 = type2;
5176 if (flag_isoc11)
5177 {
5178 tree eptype;
5179 if (ANY_INTEGRAL_TYPE_P (type1)
5180 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5181 {
5182 eptype2 = eptype;
5183 if (!semantic_result_type)
5184 semantic_result_type = c_common_type (type1, type2);
5185 }
5186 else if (ANY_INTEGRAL_TYPE_P (type2)
5187 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5188 {
5189 eptype1 = eptype;
5190 if (!semantic_result_type)
5191 semantic_result_type = c_common_type (type1, type2);
5192 }
5193 }
5194 result_type = c_common_type (eptype1, eptype2);
5195 if (result_type == error_mark_node)
5196 return error_mark_node;
5197 do_warn_double_promotion (result_type, type1, type2,
5198 "implicit conversion from %qT to %qT to "
5199 "match other result of conditional",
5200 colon_loc);
5201
5202 /* If -Wsign-compare, warn here if type1 and type2 have
5203 different signedness. We'll promote the signed to unsigned
5204 and later code won't know it used to be different.
5205 Do this check on the original types, so that explicit casts
5206 will be considered, but default promotions won't. */
5207 if (c_inhibit_evaluation_warnings == 0)
5208 {
5209 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5210 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5211
5212 if (unsigned_op1 ^ unsigned_op2)
5213 {
5214 bool ovf;
5215
5216 /* Do not warn if the result type is signed, since the
5217 signed type will only be chosen if it can represent
5218 all the values of the unsigned type. */
5219 if (!TYPE_UNSIGNED (result_type))
5220 /* OK */;
5221 else
5222 {
5223 bool op1_maybe_const = true;
5224 bool op2_maybe_const = true;
5225
5226 /* Do not warn if the signed quantity is an
5227 unsuffixed integer literal (or some static
5228 constant expression involving such literals) and
5229 it is non-negative. This warning requires the
5230 operands to be folded for best results, so do
5231 that folding in this case even without
5232 warn_sign_compare to avoid warning options
5233 possibly affecting code generation. */
5234 c_inhibit_evaluation_warnings
5235 += (ifexp == truthvalue_false_node);
5236 op1 = c_fully_fold (op1, require_constant_value,
5237 &op1_maybe_const);
5238 c_inhibit_evaluation_warnings
5239 -= (ifexp == truthvalue_false_node);
5240
5241 c_inhibit_evaluation_warnings
5242 += (ifexp == truthvalue_true_node);
5243 op2 = c_fully_fold (op2, require_constant_value,
5244 &op2_maybe_const);
5245 c_inhibit_evaluation_warnings
5246 -= (ifexp == truthvalue_true_node);
5247
5248 if (warn_sign_compare)
5249 {
5250 if ((unsigned_op2
5251 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5252 || (unsigned_op1
5253 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5254 /* OK */;
5255 else if (unsigned_op2)
5256 warning_at (op1_loc, OPT_Wsign_compare,
5257 "operand of %<?:%> changes signedness from "
5258 "%qT to %qT due to unsignedness of other "
5259 "operand", TREE_TYPE (orig_op1),
5260 TREE_TYPE (orig_op2));
5261 else
5262 warning_at (op2_loc, OPT_Wsign_compare,
5263 "operand of %<?:%> changes signedness from "
5264 "%qT to %qT due to unsignedness of other "
5265 "operand", TREE_TYPE (orig_op2),
5266 TREE_TYPE (orig_op1));
5267 }
5268 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5269 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5270 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5271 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5272 }
5273 }
5274 }
5275 }
5276 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5277 {
5278 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5279 pedwarn (colon_loc, OPT_Wpedantic,
5280 "ISO C forbids conditional expr with only one void side");
5281 result_type = void_type_node;
5282 }
5283 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5284 {
5285 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5286 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5287 addr_space_t as_common;
5288
5289 if (comp_target_types (colon_loc, type1, type2))
5290 result_type = common_pointer_type (type1, type2);
5291 else if (null_pointer_constant_p (orig_op1))
5292 result_type = type2;
5293 else if (null_pointer_constant_p (orig_op2))
5294 result_type = type1;
5295 else if (!addr_space_superset (as1, as2, &as_common))
5296 {
5297 error_at (colon_loc, "pointers to disjoint address spaces "
5298 "used in conditional expression");
5299 return error_mark_node;
5300 }
5301 else if (VOID_TYPE_P (TREE_TYPE (type1))
5302 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5303 {
5304 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5305 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5306 & ~TYPE_QUALS (TREE_TYPE (type1))))
5307 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5308 "pointer to array loses qualifier "
5309 "in conditional expression");
5310
5311 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5312 pedwarn (colon_loc, OPT_Wpedantic,
5313 "ISO C forbids conditional expr between "
5314 "%<void *%> and function pointer");
5315 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5316 TREE_TYPE (type2)));
5317 }
5318 else if (VOID_TYPE_P (TREE_TYPE (type2))
5319 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5320 {
5321 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5322 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5323 & ~TYPE_QUALS (TREE_TYPE (type2))))
5324 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5325 "pointer to array loses qualifier "
5326 "in conditional expression");
5327
5328 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5329 pedwarn (colon_loc, OPT_Wpedantic,
5330 "ISO C forbids conditional expr between "
5331 "%<void *%> and function pointer");
5332 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5333 TREE_TYPE (type1)));
5334 }
5335 /* Objective-C pointer comparisons are a bit more lenient. */
5336 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5337 result_type = objc_common_type (type1, type2);
5338 else
5339 {
5340 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5341 if (bltin1 && bltin2)
5342 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5343 "pointer type mismatch between %qT and %qT "
5344 "of %qD and %qD in conditional expression",
5345 type1, type2, bltin1, bltin2);
5346 else
5347 pedwarn (colon_loc, 0,
5348 "pointer type mismatch in conditional expression");
5349 result_type = build_pointer_type
5350 (build_qualified_type (void_type_node, qual));
5351 }
5352 }
5353 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5354 {
5355 if (!null_pointer_constant_p (orig_op2))
5356 pedwarn (colon_loc, 0,
5357 "pointer/integer type mismatch in conditional expression");
5358 else
5359 {
5360 op2 = null_pointer_node;
5361 }
5362 result_type = type1;
5363 }
5364 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5365 {
5366 if (!null_pointer_constant_p (orig_op1))
5367 pedwarn (colon_loc, 0,
5368 "pointer/integer type mismatch in conditional expression");
5369 else
5370 {
5371 op1 = null_pointer_node;
5372 }
5373 result_type = type2;
5374 }
5375
5376 if (!result_type)
5377 {
5378 if (flag_cond_mismatch)
5379 result_type = void_type_node;
5380 else
5381 {
5382 error_at (colon_loc, "type mismatch in conditional expression");
5383 return error_mark_node;
5384 }
5385 }
5386
5387 /* Merge const and volatile flags of the incoming types. */
5388 result_type
5389 = build_type_variant (result_type,
5390 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5391 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5392
5393 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5394 semantic_result_type);
5395 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5396 semantic_result_type);
5397
5398 if (ifexp_bcp && ifexp == truthvalue_true_node)
5399 {
5400 op2_int_operands = true;
5401 op1 = c_fully_fold (op1, require_constant_value, NULL);
5402 }
5403 if (ifexp_bcp && ifexp == truthvalue_false_node)
5404 {
5405 op1_int_operands = true;
5406 op2 = c_fully_fold (op2, require_constant_value, NULL);
5407 }
5408 int_const = int_operands = (ifexp_int_operands
5409 && op1_int_operands
5410 && op2_int_operands);
5411 if (int_operands)
5412 {
5413 int_const = ((ifexp == truthvalue_true_node
5414 && TREE_CODE (orig_op1) == INTEGER_CST
5415 && !TREE_OVERFLOW (orig_op1))
5416 || (ifexp == truthvalue_false_node
5417 && TREE_CODE (orig_op2) == INTEGER_CST
5418 && !TREE_OVERFLOW (orig_op2)));
5419 }
5420
5421 /* Need to convert condition operand into a vector mask. */
5422 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5423 {
5424 tree vectype = TREE_TYPE (ifexp);
5425 tree elem_type = TREE_TYPE (vectype);
5426 tree zero = build_int_cst (elem_type, 0);
5427 tree zero_vec = build_vector_from_val (vectype, zero);
5428 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5429 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5430 }
5431
5432 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5433 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5434 else
5435 {
5436 if (int_operands)
5437 {
5438 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5439 nested inside of the expression. */
5440 op1 = c_fully_fold (op1, false, NULL);
5441 op2 = c_fully_fold (op2, false, NULL);
5442 }
5443 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5444 if (int_operands)
5445 ret = note_integer_operands (ret);
5446 }
5447 if (semantic_result_type)
5448 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5449
5450 protected_set_expr_location (ret, colon_loc);
5451
5452 /* If the OP1 and OP2 are the same and don't have side-effects,
5453 warn here, because the COND_EXPR will be turned into OP1. */
5454 if (warn_duplicated_branches
5455 && TREE_CODE (ret) == COND_EXPR
5456 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5457 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5458 "this condition has identical branches");
5459
5460 return ret;
5461 }
5462 \f
5463 /* Return a compound expression that performs two expressions and
5464 returns the value of the second of them.
5465
5466 LOC is the location of the COMPOUND_EXPR. */
5467
5468 tree
5469 build_compound_expr (location_t loc, tree expr1, tree expr2)
5470 {
5471 bool expr1_int_operands, expr2_int_operands;
5472 tree eptype = NULL_TREE;
5473 tree ret;
5474
5475 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5476 if (expr1_int_operands)
5477 expr1 = remove_c_maybe_const_expr (expr1);
5478 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5479 if (expr2_int_operands)
5480 expr2 = remove_c_maybe_const_expr (expr2);
5481
5482 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5483 expr1 = TREE_OPERAND (expr1, 0);
5484 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5485 {
5486 eptype = TREE_TYPE (expr2);
5487 expr2 = TREE_OPERAND (expr2, 0);
5488 }
5489
5490 if (!TREE_SIDE_EFFECTS (expr1))
5491 {
5492 /* The left-hand operand of a comma expression is like an expression
5493 statement: with -Wunused, we should warn if it doesn't have
5494 any side-effects, unless it was explicitly cast to (void). */
5495 if (warn_unused_value)
5496 {
5497 if (VOID_TYPE_P (TREE_TYPE (expr1))
5498 && CONVERT_EXPR_P (expr1))
5499 ; /* (void) a, b */
5500 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5501 && TREE_CODE (expr1) == COMPOUND_EXPR
5502 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5503 ; /* (void) a, (void) b, c */
5504 else
5505 warning_at (loc, OPT_Wunused_value,
5506 "left-hand operand of comma expression has no effect");
5507 }
5508 }
5509 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5510 && warn_unused_value)
5511 {
5512 tree r = expr1;
5513 location_t cloc = loc;
5514 while (TREE_CODE (r) == COMPOUND_EXPR)
5515 {
5516 if (EXPR_HAS_LOCATION (r))
5517 cloc = EXPR_LOCATION (r);
5518 r = TREE_OPERAND (r, 1);
5519 }
5520 if (!TREE_SIDE_EFFECTS (r)
5521 && !VOID_TYPE_P (TREE_TYPE (r))
5522 && !CONVERT_EXPR_P (r))
5523 warning_at (cloc, OPT_Wunused_value,
5524 "right-hand operand of comma expression has no effect");
5525 }
5526
5527 /* With -Wunused, we should also warn if the left-hand operand does have
5528 side-effects, but computes a value which is not used. For example, in
5529 `foo() + bar(), baz()' the result of the `+' operator is not used,
5530 so we should issue a warning. */
5531 else if (warn_unused_value)
5532 warn_if_unused_value (expr1, loc);
5533
5534 if (expr2 == error_mark_node)
5535 return error_mark_node;
5536
5537 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5538
5539 if (flag_isoc99
5540 && expr1_int_operands
5541 && expr2_int_operands)
5542 ret = note_integer_operands (ret);
5543
5544 if (eptype)
5545 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5546
5547 protected_set_expr_location (ret, loc);
5548 return ret;
5549 }
5550
5551 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5552 which we are casting. OTYPE is the type of the expression being
5553 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5554 of the cast. -Wcast-qual appeared on the command line. Named
5555 address space qualifiers are not handled here, because they result
5556 in different warnings. */
5557
5558 static void
5559 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5560 {
5561 tree in_type = type;
5562 tree in_otype = otype;
5563 int added = 0;
5564 int discarded = 0;
5565 bool is_const;
5566
5567 /* Check that the qualifiers on IN_TYPE are a superset of the
5568 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5569 nodes is uninteresting and we stop as soon as we hit a
5570 non-POINTER_TYPE node on either type. */
5571 do
5572 {
5573 in_otype = TREE_TYPE (in_otype);
5574 in_type = TREE_TYPE (in_type);
5575
5576 /* GNU C allows cv-qualified function types. 'const' means the
5577 function is very pure, 'volatile' means it can't return. We
5578 need to warn when such qualifiers are added, not when they're
5579 taken away. */
5580 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5581 && TREE_CODE (in_type) == FUNCTION_TYPE)
5582 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5583 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5584 else
5585 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5586 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5587 }
5588 while (TREE_CODE (in_type) == POINTER_TYPE
5589 && TREE_CODE (in_otype) == POINTER_TYPE);
5590
5591 if (added)
5592 warning_at (loc, OPT_Wcast_qual,
5593 "cast adds %q#v qualifier to function type", added);
5594
5595 if (discarded)
5596 /* There are qualifiers present in IN_OTYPE that are not present
5597 in IN_TYPE. */
5598 warning_at (loc, OPT_Wcast_qual,
5599 "cast discards %qv qualifier from pointer target type",
5600 discarded);
5601
5602 if (added || discarded)
5603 return;
5604
5605 /* A cast from **T to const **T is unsafe, because it can cause a
5606 const value to be changed with no additional warning. We only
5607 issue this warning if T is the same on both sides, and we only
5608 issue the warning if there are the same number of pointers on
5609 both sides, as otherwise the cast is clearly unsafe anyhow. A
5610 cast is unsafe when a qualifier is added at one level and const
5611 is not present at all outer levels.
5612
5613 To issue this warning, we check at each level whether the cast
5614 adds new qualifiers not already seen. We don't need to special
5615 case function types, as they won't have the same
5616 TYPE_MAIN_VARIANT. */
5617
5618 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5619 return;
5620 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5621 return;
5622
5623 in_type = type;
5624 in_otype = otype;
5625 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5626 do
5627 {
5628 in_type = TREE_TYPE (in_type);
5629 in_otype = TREE_TYPE (in_otype);
5630 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5631 && !is_const)
5632 {
5633 warning_at (loc, OPT_Wcast_qual,
5634 "to be safe all intermediate pointers in cast from "
5635 "%qT to %qT must be %<const%> qualified",
5636 otype, type);
5637 break;
5638 }
5639 if (is_const)
5640 is_const = TYPE_READONLY (in_type);
5641 }
5642 while (TREE_CODE (in_type) == POINTER_TYPE);
5643 }
5644
5645 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5646
5647 static bool
5648 c_safe_arg_type_equiv_p (tree t1, tree t2)
5649 {
5650 t1 = TYPE_MAIN_VARIANT (t1);
5651 t2 = TYPE_MAIN_VARIANT (t2);
5652
5653 if (TREE_CODE (t1) == POINTER_TYPE
5654 && TREE_CODE (t2) == POINTER_TYPE)
5655 return true;
5656
5657 /* The signedness of the parameter matters only when an integral
5658 type smaller than int is promoted to int, otherwise only the
5659 precision of the parameter matters.
5660 This check should make sure that the callee does not see
5661 undefined values in argument registers. */
5662 if (INTEGRAL_TYPE_P (t1)
5663 && INTEGRAL_TYPE_P (t2)
5664 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5665 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5666 || !targetm.calls.promote_prototypes (NULL_TREE)
5667 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5668 return true;
5669
5670 return comptypes (t1, t2);
5671 }
5672
5673 /* Check if a type cast between two function types can be considered safe. */
5674
5675 static bool
5676 c_safe_function_type_cast_p (tree t1, tree t2)
5677 {
5678 if (TREE_TYPE (t1) == void_type_node &&
5679 TYPE_ARG_TYPES (t1) == void_list_node)
5680 return true;
5681
5682 if (TREE_TYPE (t2) == void_type_node &&
5683 TYPE_ARG_TYPES (t2) == void_list_node)
5684 return true;
5685
5686 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5687 return false;
5688
5689 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5690 t1 && t2;
5691 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5692 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5693 return false;
5694
5695 return true;
5696 }
5697
5698 /* Build an expression representing a cast to type TYPE of expression EXPR.
5699 LOC is the location of the cast-- typically the open paren of the cast. */
5700
5701 tree
5702 build_c_cast (location_t loc, tree type, tree expr)
5703 {
5704 tree value;
5705
5706 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5707 expr = TREE_OPERAND (expr, 0);
5708
5709 value = expr;
5710
5711 if (type == error_mark_node || expr == error_mark_node)
5712 return error_mark_node;
5713
5714 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5715 only in <protocol> qualifications. But when constructing cast expressions,
5716 the protocols do matter and must be kept around. */
5717 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5718 return build1 (NOP_EXPR, type, expr);
5719
5720 type = TYPE_MAIN_VARIANT (type);
5721
5722 if (TREE_CODE (type) == ARRAY_TYPE)
5723 {
5724 error_at (loc, "cast specifies array type");
5725 return error_mark_node;
5726 }
5727
5728 if (TREE_CODE (type) == FUNCTION_TYPE)
5729 {
5730 error_at (loc, "cast specifies function type");
5731 return error_mark_node;
5732 }
5733
5734 if (!VOID_TYPE_P (type))
5735 {
5736 value = require_complete_type (loc, value);
5737 if (value == error_mark_node)
5738 return error_mark_node;
5739 }
5740
5741 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5742 {
5743 if (RECORD_OR_UNION_TYPE_P (type))
5744 pedwarn (loc, OPT_Wpedantic,
5745 "ISO C forbids casting nonscalar to the same type");
5746
5747 /* Convert to remove any qualifiers from VALUE's type. */
5748 value = convert (type, value);
5749 }
5750 else if (TREE_CODE (type) == UNION_TYPE)
5751 {
5752 tree field;
5753
5754 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5755 if (TREE_TYPE (field) != error_mark_node
5756 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5757 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5758 break;
5759
5760 if (field)
5761 {
5762 tree t;
5763 bool maybe_const = true;
5764
5765 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5766 t = c_fully_fold (value, false, &maybe_const);
5767 t = build_constructor_single (type, field, t);
5768 if (!maybe_const)
5769 t = c_wrap_maybe_const (t, true);
5770 t = digest_init (loc, type, t,
5771 NULL_TREE, false, true, 0);
5772 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5773 return t;
5774 }
5775 error_at (loc, "cast to union type from type not present in union");
5776 return error_mark_node;
5777 }
5778 else
5779 {
5780 tree otype, ovalue;
5781
5782 if (type == void_type_node)
5783 {
5784 tree t = build1 (CONVERT_EXPR, type, value);
5785 SET_EXPR_LOCATION (t, loc);
5786 return t;
5787 }
5788
5789 otype = TREE_TYPE (value);
5790
5791 /* Optionally warn about potentially worrisome casts. */
5792 if (warn_cast_qual
5793 && TREE_CODE (type) == POINTER_TYPE
5794 && TREE_CODE (otype) == POINTER_TYPE)
5795 handle_warn_cast_qual (loc, type, otype);
5796
5797 /* Warn about conversions between pointers to disjoint
5798 address spaces. */
5799 if (TREE_CODE (type) == POINTER_TYPE
5800 && TREE_CODE (otype) == POINTER_TYPE
5801 && !null_pointer_constant_p (value))
5802 {
5803 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5804 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5805 addr_space_t as_common;
5806
5807 if (!addr_space_superset (as_to, as_from, &as_common))
5808 {
5809 if (ADDR_SPACE_GENERIC_P (as_from))
5810 warning_at (loc, 0, "cast to %s address space pointer "
5811 "from disjoint generic address space pointer",
5812 c_addr_space_name (as_to));
5813
5814 else if (ADDR_SPACE_GENERIC_P (as_to))
5815 warning_at (loc, 0, "cast to generic address space pointer "
5816 "from disjoint %s address space pointer",
5817 c_addr_space_name (as_from));
5818
5819 else
5820 warning_at (loc, 0, "cast to %s address space pointer "
5821 "from disjoint %s address space pointer",
5822 c_addr_space_name (as_to),
5823 c_addr_space_name (as_from));
5824 }
5825 }
5826
5827 /* Warn about possible alignment problems. */
5828 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5829 && TREE_CODE (type) == POINTER_TYPE
5830 && TREE_CODE (otype) == POINTER_TYPE
5831 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5832 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5833 /* Don't warn about opaque types, where the actual alignment
5834 restriction is unknown. */
5835 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5836 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5837 && min_align_of_type (TREE_TYPE (type))
5838 > min_align_of_type (TREE_TYPE (otype)))
5839 warning_at (loc, OPT_Wcast_align,
5840 "cast increases required alignment of target type");
5841
5842 if (TREE_CODE (type) == INTEGER_TYPE
5843 && TREE_CODE (otype) == POINTER_TYPE
5844 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5845 /* Unlike conversion of integers to pointers, where the
5846 warning is disabled for converting constants because
5847 of cases such as SIG_*, warn about converting constant
5848 pointers to integers. In some cases it may cause unwanted
5849 sign extension, and a warning is appropriate. */
5850 warning_at (loc, OPT_Wpointer_to_int_cast,
5851 "cast from pointer to integer of different size");
5852
5853 if (TREE_CODE (value) == CALL_EXPR
5854 && TREE_CODE (type) != TREE_CODE (otype))
5855 warning_at (loc, OPT_Wbad_function_cast,
5856 "cast from function call of type %qT "
5857 "to non-matching type %qT", otype, type);
5858
5859 if (TREE_CODE (type) == POINTER_TYPE
5860 && TREE_CODE (otype) == INTEGER_TYPE
5861 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5862 /* Don't warn about converting any constant. */
5863 && !TREE_CONSTANT (value))
5864 warning_at (loc,
5865 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5866 "of different size");
5867
5868 if (warn_strict_aliasing <= 2)
5869 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
5870
5871 /* If pedantic, warn for conversions between function and object
5872 pointer types, except for converting a null pointer constant
5873 to function pointer type. */
5874 if (pedantic
5875 && TREE_CODE (type) == POINTER_TYPE
5876 && TREE_CODE (otype) == POINTER_TYPE
5877 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5878 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5879 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5880 "conversion of function pointer to object pointer type");
5881
5882 if (pedantic
5883 && TREE_CODE (type) == POINTER_TYPE
5884 && TREE_CODE (otype) == POINTER_TYPE
5885 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5886 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5887 && !null_pointer_constant_p (value))
5888 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5889 "conversion of object pointer to function pointer type");
5890
5891 if (TREE_CODE (type) == POINTER_TYPE
5892 && TREE_CODE (otype) == POINTER_TYPE
5893 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5894 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5895 && !c_safe_function_type_cast_p (TREE_TYPE (type),
5896 TREE_TYPE (otype)))
5897 warning_at (loc, OPT_Wcast_function_type,
5898 "cast between incompatible function types"
5899 " from %qT to %qT", otype, type);
5900
5901 ovalue = value;
5902 value = convert (type, value);
5903
5904 /* Ignore any integer overflow caused by the cast. */
5905 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5906 {
5907 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5908 {
5909 if (!TREE_OVERFLOW (value))
5910 {
5911 /* Avoid clobbering a shared constant. */
5912 value = copy_node (value);
5913 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5914 }
5915 }
5916 else if (TREE_OVERFLOW (value))
5917 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5918 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5919 }
5920 }
5921
5922 /* Don't let a cast be an lvalue. */
5923 if (lvalue_p (value))
5924 value = non_lvalue_loc (loc, value);
5925
5926 /* Don't allow the results of casting to floating-point or complex
5927 types be confused with actual constants, or casts involving
5928 integer and pointer types other than direct integer-to-integer
5929 and integer-to-pointer be confused with integer constant
5930 expressions and null pointer constants. */
5931 if (TREE_CODE (value) == REAL_CST
5932 || TREE_CODE (value) == COMPLEX_CST
5933 || (TREE_CODE (value) == INTEGER_CST
5934 && !((TREE_CODE (expr) == INTEGER_CST
5935 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5936 || TREE_CODE (expr) == REAL_CST
5937 || TREE_CODE (expr) == COMPLEX_CST)))
5938 value = build1 (NOP_EXPR, type, value);
5939
5940 protected_set_expr_location (value, loc);
5941 return value;
5942 }
5943
5944 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5945 location of the open paren of the cast, or the position of the cast
5946 expr. */
5947 tree
5948 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5949 {
5950 tree type;
5951 tree type_expr = NULL_TREE;
5952 bool type_expr_const = true;
5953 tree ret;
5954 int saved_wsp = warn_strict_prototypes;
5955
5956 /* This avoids warnings about unprototyped casts on
5957 integers. E.g. "#define SIG_DFL (void(*)())0". */
5958 if (TREE_CODE (expr) == INTEGER_CST)
5959 warn_strict_prototypes = 0;
5960 type = groktypename (type_name, &type_expr, &type_expr_const);
5961 warn_strict_prototypes = saved_wsp;
5962
5963 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5964 && reject_gcc_builtin (expr))
5965 return error_mark_node;
5966
5967 ret = build_c_cast (loc, type, expr);
5968 if (type_expr)
5969 {
5970 bool inner_expr_const = true;
5971 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5972 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5973 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5974 && inner_expr_const);
5975 SET_EXPR_LOCATION (ret, loc);
5976 }
5977
5978 if (!EXPR_HAS_LOCATION (ret))
5979 protected_set_expr_location (ret, loc);
5980
5981 /* C++ does not permits types to be defined in a cast, but it
5982 allows references to incomplete types. */
5983 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5984 warning_at (loc, OPT_Wc___compat,
5985 "defining a type in a cast is invalid in C++");
5986
5987 return ret;
5988 }
5989 \f
5990 /* Build an assignment expression of lvalue LHS from value RHS.
5991 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5992 may differ from TREE_TYPE (LHS) for an enum bitfield.
5993 MODIFYCODE is the code for a binary operator that we use
5994 to combine the old value of LHS with RHS to get the new value.
5995 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5996 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5997 which may differ from TREE_TYPE (RHS) for an enum value.
5998
5999 LOCATION is the location of the MODIFYCODE operator.
6000 RHS_LOC is the location of the RHS. */
6001
6002 tree
6003 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6004 enum tree_code modifycode,
6005 location_t rhs_loc, tree rhs, tree rhs_origtype)
6006 {
6007 tree result;
6008 tree newrhs;
6009 tree rhseval = NULL_TREE;
6010 tree lhstype = TREE_TYPE (lhs);
6011 tree olhstype = lhstype;
6012 bool npc;
6013 bool is_atomic_op;
6014
6015 /* Types that aren't fully specified cannot be used in assignments. */
6016 lhs = require_complete_type (location, lhs);
6017
6018 /* Avoid duplicate error messages from operands that had errors. */
6019 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6020 return error_mark_node;
6021
6022 /* Ensure an error for assigning a non-lvalue array to an array in
6023 C90. */
6024 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6025 {
6026 error_at (location, "assignment to expression with array type");
6027 return error_mark_node;
6028 }
6029
6030 /* For ObjC properties, defer this check. */
6031 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6032 return error_mark_node;
6033
6034 is_atomic_op = really_atomic_lvalue (lhs);
6035
6036 newrhs = rhs;
6037
6038 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6039 {
6040 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6041 lhs_origtype, modifycode, rhs_loc, rhs,
6042 rhs_origtype);
6043 if (inner == error_mark_node)
6044 return error_mark_node;
6045 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6046 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6047 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6048 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6049 protected_set_expr_location (result, location);
6050 return result;
6051 }
6052
6053 /* If a binary op has been requested, combine the old LHS value with the RHS
6054 producing the value we should actually store into the LHS. */
6055
6056 if (modifycode != NOP_EXPR)
6057 {
6058 lhs = c_fully_fold (lhs, false, NULL, true);
6059 lhs = stabilize_reference (lhs);
6060
6061 /* Construct the RHS for any non-atomic compound assignemnt. */
6062 if (!is_atomic_op)
6063 {
6064 /* If in LHS op= RHS the RHS has side-effects, ensure they
6065 are preevaluated before the rest of the assignment expression's
6066 side-effects, because RHS could contain e.g. function calls
6067 that modify LHS. */
6068 if (TREE_SIDE_EFFECTS (rhs))
6069 {
6070 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6071 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6072 else
6073 newrhs = save_expr (rhs);
6074 rhseval = newrhs;
6075 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6076 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6077 newrhs);
6078 }
6079 newrhs = build_binary_op (location,
6080 modifycode, lhs, newrhs, true);
6081
6082 /* The original type of the right hand side is no longer
6083 meaningful. */
6084 rhs_origtype = NULL_TREE;
6085 }
6086 }
6087
6088 if (c_dialect_objc ())
6089 {
6090 /* Check if we are modifying an Objective-C property reference;
6091 if so, we need to generate setter calls. */
6092 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6093 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6094 else
6095 result = objc_maybe_build_modify_expr (lhs, newrhs);
6096 if (result)
6097 goto return_result;
6098
6099 /* Else, do the check that we postponed for Objective-C. */
6100 if (!lvalue_or_else (location, lhs, lv_assign))
6101 return error_mark_node;
6102 }
6103
6104 /* Give an error for storing in something that is 'const'. */
6105
6106 if (TYPE_READONLY (lhstype)
6107 || (RECORD_OR_UNION_TYPE_P (lhstype)
6108 && C_TYPE_FIELDS_READONLY (lhstype)))
6109 {
6110 readonly_error (location, lhs, lv_assign);
6111 return error_mark_node;
6112 }
6113 else if (TREE_READONLY (lhs))
6114 readonly_warning (lhs, lv_assign);
6115
6116 /* If storing into a structure or union member,
6117 it has probably been given type `int'.
6118 Compute the type that would go with
6119 the actual amount of storage the member occupies. */
6120
6121 if (TREE_CODE (lhs) == COMPONENT_REF
6122 && (TREE_CODE (lhstype) == INTEGER_TYPE
6123 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6124 || TREE_CODE (lhstype) == REAL_TYPE
6125 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6126 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6127
6128 /* If storing in a field that is in actuality a short or narrower than one,
6129 we must store in the field in its actual type. */
6130
6131 if (lhstype != TREE_TYPE (lhs))
6132 {
6133 lhs = copy_node (lhs);
6134 TREE_TYPE (lhs) = lhstype;
6135 }
6136
6137 /* Issue -Wc++-compat warnings about an assignment to an enum type
6138 when LHS does not have its original type. This happens for,
6139 e.g., an enum bitfield in a struct. */
6140 if (warn_cxx_compat
6141 && lhs_origtype != NULL_TREE
6142 && lhs_origtype != lhstype
6143 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6144 {
6145 tree checktype = (rhs_origtype != NULL_TREE
6146 ? rhs_origtype
6147 : TREE_TYPE (rhs));
6148 if (checktype != error_mark_node
6149 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6150 || (is_atomic_op && modifycode != NOP_EXPR)))
6151 warning_at (location, OPT_Wc___compat,
6152 "enum conversion in assignment is invalid in C++");
6153 }
6154
6155 /* If the lhs is atomic, remove that qualifier. */
6156 if (is_atomic_op)
6157 {
6158 lhstype = build_qualified_type (lhstype,
6159 (TYPE_QUALS (lhstype)
6160 & ~TYPE_QUAL_ATOMIC));
6161 olhstype = build_qualified_type (olhstype,
6162 (TYPE_QUALS (lhstype)
6163 & ~TYPE_QUAL_ATOMIC));
6164 }
6165
6166 /* Convert new value to destination type. Fold it first, then
6167 restore any excess precision information, for the sake of
6168 conversion warnings. */
6169
6170 if (!(is_atomic_op && modifycode != NOP_EXPR))
6171 {
6172 tree rhs_semantic_type = NULL_TREE;
6173 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6174 {
6175 rhs_semantic_type = TREE_TYPE (newrhs);
6176 newrhs = TREE_OPERAND (newrhs, 0);
6177 }
6178 npc = null_pointer_constant_p (newrhs);
6179 newrhs = c_fully_fold (newrhs, false, NULL);
6180 if (rhs_semantic_type)
6181 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6182 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6183 rhs_origtype, ic_assign, npc,
6184 NULL_TREE, NULL_TREE, 0);
6185 if (TREE_CODE (newrhs) == ERROR_MARK)
6186 return error_mark_node;
6187 }
6188
6189 /* Emit ObjC write barrier, if necessary. */
6190 if (c_dialect_objc () && flag_objc_gc)
6191 {
6192 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6193 if (result)
6194 {
6195 protected_set_expr_location (result, location);
6196 goto return_result;
6197 }
6198 }
6199
6200 /* Scan operands. */
6201
6202 if (is_atomic_op)
6203 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6204 else
6205 {
6206 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6207 TREE_SIDE_EFFECTS (result) = 1;
6208 protected_set_expr_location (result, location);
6209 }
6210
6211 /* If we got the LHS in a different type for storing in,
6212 convert the result back to the nominal type of LHS
6213 so that the value we return always has the same type
6214 as the LHS argument. */
6215
6216 if (olhstype == TREE_TYPE (result))
6217 goto return_result;
6218
6219 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6220 rhs_origtype, ic_assign, false, NULL_TREE,
6221 NULL_TREE, 0);
6222 protected_set_expr_location (result, location);
6223
6224 return_result:
6225 if (rhseval)
6226 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6227 return result;
6228 }
6229 \f
6230 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6231 This is used to implement -fplan9-extensions. */
6232
6233 static bool
6234 find_anonymous_field_with_type (tree struct_type, tree type)
6235 {
6236 tree field;
6237 bool found;
6238
6239 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6240 found = false;
6241 for (field = TYPE_FIELDS (struct_type);
6242 field != NULL_TREE;
6243 field = TREE_CHAIN (field))
6244 {
6245 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6246 ? c_build_qualified_type (TREE_TYPE (field),
6247 TYPE_QUAL_ATOMIC)
6248 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6249 if (DECL_NAME (field) == NULL
6250 && comptypes (type, fieldtype))
6251 {
6252 if (found)
6253 return false;
6254 found = true;
6255 }
6256 else if (DECL_NAME (field) == NULL
6257 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6258 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6259 {
6260 if (found)
6261 return false;
6262 found = true;
6263 }
6264 }
6265 return found;
6266 }
6267
6268 /* RHS is an expression whose type is pointer to struct. If there is
6269 an anonymous field in RHS with type TYPE, then return a pointer to
6270 that field in RHS. This is used with -fplan9-extensions. This
6271 returns NULL if no conversion could be found. */
6272
6273 static tree
6274 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6275 {
6276 tree rhs_struct_type, lhs_main_type;
6277 tree field, found_field;
6278 bool found_sub_field;
6279 tree ret;
6280
6281 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6282 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6283 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6284
6285 gcc_assert (POINTER_TYPE_P (type));
6286 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6287 ? c_build_qualified_type (TREE_TYPE (type),
6288 TYPE_QUAL_ATOMIC)
6289 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6290
6291 found_field = NULL_TREE;
6292 found_sub_field = false;
6293 for (field = TYPE_FIELDS (rhs_struct_type);
6294 field != NULL_TREE;
6295 field = TREE_CHAIN (field))
6296 {
6297 if (DECL_NAME (field) != NULL_TREE
6298 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6299 continue;
6300 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6301 ? c_build_qualified_type (TREE_TYPE (field),
6302 TYPE_QUAL_ATOMIC)
6303 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6304 if (comptypes (lhs_main_type, fieldtype))
6305 {
6306 if (found_field != NULL_TREE)
6307 return NULL_TREE;
6308 found_field = field;
6309 }
6310 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6311 lhs_main_type))
6312 {
6313 if (found_field != NULL_TREE)
6314 return NULL_TREE;
6315 found_field = field;
6316 found_sub_field = true;
6317 }
6318 }
6319
6320 if (found_field == NULL_TREE)
6321 return NULL_TREE;
6322
6323 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6324 build_fold_indirect_ref (rhs), found_field,
6325 NULL_TREE);
6326 ret = build_fold_addr_expr_loc (location, ret);
6327
6328 if (found_sub_field)
6329 {
6330 ret = convert_to_anonymous_field (location, type, ret);
6331 gcc_assert (ret != NULL_TREE);
6332 }
6333
6334 return ret;
6335 }
6336
6337 /* Issue an error message for a bad initializer component.
6338 GMSGID identifies the message.
6339 The component name is taken from the spelling stack. */
6340
6341 static void ATTRIBUTE_GCC_DIAG (2,0)
6342 error_init (location_t loc, const char *gmsgid, ...)
6343 {
6344 char *ofwhat;
6345
6346 auto_diagnostic_group d;
6347
6348 /* The gmsgid may be a format string with %< and %>. */
6349 va_list ap;
6350 va_start (ap, gmsgid);
6351 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6352 va_end (ap);
6353
6354 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6355 if (*ofwhat && warned)
6356 inform (loc, "(near initialization for %qs)", ofwhat);
6357 }
6358
6359 /* Issue a pedantic warning for a bad initializer component. OPT is
6360 the option OPT_* (from options.h) controlling this warning or 0 if
6361 it is unconditionally given. GMSGID identifies the message. The
6362 component name is taken from the spelling stack. */
6363
6364 static void ATTRIBUTE_GCC_DIAG (3,0)
6365 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6366 {
6367 /* Use the location where a macro was expanded rather than where
6368 it was defined to make sure macros defined in system headers
6369 but used incorrectly elsewhere are diagnosed. */
6370 location_t exploc = expansion_point_location_if_in_system_header (loc);
6371 auto_diagnostic_group d;
6372 va_list ap;
6373 va_start (ap, gmsgid);
6374 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6375 va_end (ap);
6376 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6377 if (*ofwhat && warned)
6378 inform (exploc, "(near initialization for %qs)", ofwhat);
6379 }
6380
6381 /* Issue a warning for a bad initializer component.
6382
6383 OPT is the OPT_W* value corresponding to the warning option that
6384 controls this warning. GMSGID identifies the message. The
6385 component name is taken from the spelling stack. */
6386
6387 static void
6388 warning_init (location_t loc, int opt, const char *gmsgid)
6389 {
6390 char *ofwhat;
6391 bool warned;
6392
6393 auto_diagnostic_group d;
6394
6395 /* Use the location where a macro was expanded rather than where
6396 it was defined to make sure macros defined in system headers
6397 but used incorrectly elsewhere are diagnosed. */
6398 location_t exploc = expansion_point_location_if_in_system_header (loc);
6399
6400 /* The gmsgid may be a format string with %< and %>. */
6401 warned = warning_at (exploc, opt, gmsgid);
6402 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6403 if (*ofwhat && warned)
6404 inform (exploc, "(near initialization for %qs)", ofwhat);
6405 }
6406 \f
6407 /* If TYPE is an array type and EXPR is a parenthesized string
6408 constant, warn if pedantic that EXPR is being used to initialize an
6409 object of type TYPE. */
6410
6411 void
6412 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6413 {
6414 if (pedantic
6415 && TREE_CODE (type) == ARRAY_TYPE
6416 && TREE_CODE (expr.value) == STRING_CST
6417 && expr.original_code != STRING_CST)
6418 pedwarn_init (loc, OPT_Wpedantic,
6419 "array initialized from parenthesized string constant");
6420 }
6421
6422 /* Attempt to locate the parameter with the given index within FNDECL,
6423 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6424
6425 static location_t
6426 get_fndecl_argument_location (tree fndecl, int argnum)
6427 {
6428 int i;
6429 tree param;
6430
6431 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6432 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6433 i < argnum && param;
6434 i++, param = TREE_CHAIN (param))
6435 ;
6436
6437 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6438 return DECL_SOURCE_LOCATION (FNDECL). */
6439 if (param == NULL)
6440 return DECL_SOURCE_LOCATION (fndecl);
6441
6442 return DECL_SOURCE_LOCATION (param);
6443 }
6444
6445 /* Issue a note about a mismatching argument for parameter PARMNUM
6446 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6447 Attempt to issue the note at the pertinent parameter of the decl;
6448 failing that issue it at the location of FUNDECL; failing that
6449 issue it at PLOC. */
6450
6451 static void
6452 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6453 tree expected_type, tree actual_type)
6454 {
6455 location_t loc;
6456 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6457 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6458 else
6459 loc = ploc;
6460
6461 inform (loc,
6462 "expected %qT but argument is of type %qT",
6463 expected_type, actual_type);
6464 }
6465
6466 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6467 function FUNDECL declared without prototype to parameter PARMNUM of
6468 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6469
6470 static void
6471 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6472 tree parmtype, tree argtype)
6473 {
6474 tree_code parmcode = TREE_CODE (parmtype);
6475 tree_code argcode = TREE_CODE (argtype);
6476 tree promoted = c_type_promotes_to (argtype);
6477
6478 /* Avoid warning for enum arguments that promote to an integer type
6479 of the same size/mode. */
6480 if (parmcode == INTEGER_TYPE
6481 && argcode == ENUMERAL_TYPE
6482 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6483 return;
6484
6485 if ((parmcode == argcode
6486 || (parmcode == INTEGER_TYPE
6487 && argcode == ENUMERAL_TYPE))
6488 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6489 return;
6490
6491 /* This diagnoses even signed/unsigned mismatches. Those might be
6492 safe in many cases but GCC may emit suboptimal code for them so
6493 warning on those cases drives efficiency improvements. */
6494 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6495 TYPE_MAIN_VARIANT (promoted) == argtype
6496 ? G_("%qD argument %d type is %qT where %qT is expected "
6497 "in a call to built-in function declared without "
6498 "prototype")
6499 : G_("%qD argument %d promotes to %qT where %qT is expected "
6500 "in a call to built-in function declared without "
6501 "prototype"),
6502 fundecl, parmnum, promoted, parmtype))
6503 inform (DECL_SOURCE_LOCATION (fundecl),
6504 "built-in %qD declared here",
6505 fundecl);
6506 }
6507
6508 /* Convert value RHS to type TYPE as preparation for an assignment to
6509 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6510 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6511 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6512 constant before any folding.
6513 The real work of conversion is done by `convert'.
6514 The purpose of this function is to generate error messages
6515 for assignments that are not allowed in C.
6516 ERRTYPE says whether it is argument passing, assignment,
6517 initialization or return.
6518
6519 In the following example, '~' denotes where EXPR_LOC and '^' where
6520 LOCATION point to:
6521
6522 f (var); [ic_argpass]
6523 ^ ~~~
6524 x = var; [ic_assign]
6525 ^ ~~~;
6526 int x = var; [ic_init]
6527 ^^^
6528 return x; [ic_return]
6529 ^
6530
6531 FUNCTION is a tree for the function being called.
6532 PARMNUM is the number of the argument, for printing in error messages.
6533 WARNOPT may be set to a warning option to issue the corresponding warning
6534 rather than an error for invalid conversions. Used for calls to built-in
6535 functions declared without a prototype. */
6536
6537 static tree
6538 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6539 tree rhs, tree origtype, enum impl_conv errtype,
6540 bool null_pointer_constant, tree fundecl,
6541 tree function, int parmnum, int warnopt /* = 0 */)
6542 {
6543 enum tree_code codel = TREE_CODE (type);
6544 tree orig_rhs = rhs;
6545 tree rhstype;
6546 enum tree_code coder;
6547 tree rname = NULL_TREE;
6548 bool objc_ok = false;
6549
6550 /* Use the expansion point location to handle cases such as user's
6551 function returning a wrong-type macro defined in a system header. */
6552 location = expansion_point_location_if_in_system_header (location);
6553
6554 if (errtype == ic_argpass)
6555 {
6556 tree selector;
6557 /* Change pointer to function to the function itself for
6558 diagnostics. */
6559 if (TREE_CODE (function) == ADDR_EXPR
6560 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6561 function = TREE_OPERAND (function, 0);
6562
6563 /* Handle an ObjC selector specially for diagnostics. */
6564 selector = objc_message_selector ();
6565 rname = function;
6566 if (selector && parmnum > 2)
6567 {
6568 rname = selector;
6569 parmnum -= 2;
6570 }
6571 }
6572
6573 /* This macro is used to emit diagnostics to ensure that all format
6574 strings are complete sentences, visible to gettext and checked at
6575 compile time. */
6576 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6577 do { \
6578 switch (errtype) \
6579 { \
6580 case ic_argpass: \
6581 { \
6582 auto_diagnostic_group d; \
6583 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6584 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6585 } \
6586 break; \
6587 case ic_assign: \
6588 pedwarn (LOCATION, OPT, AS); \
6589 break; \
6590 case ic_init: \
6591 pedwarn_init (LOCATION, OPT, IN); \
6592 break; \
6593 case ic_return: \
6594 pedwarn (LOCATION, OPT, RE); \
6595 break; \
6596 default: \
6597 gcc_unreachable (); \
6598 } \
6599 } while (0)
6600
6601 /* This macro is used to emit diagnostics to ensure that all format
6602 strings are complete sentences, visible to gettext and checked at
6603 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6604 extra parameter to enumerate qualifiers. */
6605 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6606 do { \
6607 switch (errtype) \
6608 { \
6609 case ic_argpass: \
6610 { \
6611 auto_diagnostic_group d; \
6612 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6613 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6614 } \
6615 break; \
6616 case ic_assign: \
6617 pedwarn (LOCATION, OPT, AS, QUALS); \
6618 break; \
6619 case ic_init: \
6620 pedwarn (LOCATION, OPT, IN, QUALS); \
6621 break; \
6622 case ic_return: \
6623 pedwarn (LOCATION, OPT, RE, QUALS); \
6624 break; \
6625 default: \
6626 gcc_unreachable (); \
6627 } \
6628 } while (0)
6629
6630 /* This macro is used to emit diagnostics to ensure that all format
6631 strings are complete sentences, visible to gettext and checked at
6632 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6633 warning_at instead of pedwarn. */
6634 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6635 do { \
6636 switch (errtype) \
6637 { \
6638 case ic_argpass: \
6639 { \
6640 auto_diagnostic_group d; \
6641 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6642 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6643 } \
6644 break; \
6645 case ic_assign: \
6646 warning_at (LOCATION, OPT, AS, QUALS); \
6647 break; \
6648 case ic_init: \
6649 warning_at (LOCATION, OPT, IN, QUALS); \
6650 break; \
6651 case ic_return: \
6652 warning_at (LOCATION, OPT, RE, QUALS); \
6653 break; \
6654 default: \
6655 gcc_unreachable (); \
6656 } \
6657 } while (0)
6658
6659 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6660 rhs = TREE_OPERAND (rhs, 0);
6661
6662 rhstype = TREE_TYPE (rhs);
6663 coder = TREE_CODE (rhstype);
6664
6665 if (coder == ERROR_MARK)
6666 return error_mark_node;
6667
6668 if (c_dialect_objc ())
6669 {
6670 int parmno;
6671
6672 switch (errtype)
6673 {
6674 case ic_return:
6675 parmno = 0;
6676 break;
6677
6678 case ic_assign:
6679 parmno = -1;
6680 break;
6681
6682 case ic_init:
6683 parmno = -2;
6684 break;
6685
6686 default:
6687 parmno = parmnum;
6688 break;
6689 }
6690
6691 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6692 }
6693
6694 if (warn_cxx_compat)
6695 {
6696 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6697 if (checktype != error_mark_node
6698 && TREE_CODE (type) == ENUMERAL_TYPE
6699 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6700 switch (errtype)
6701 {
6702 case ic_argpass:
6703 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6704 "passing argument %d of %qE is invalid in C++",
6705 parmnum, rname))
6706 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6707 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6708 "expected %qT but argument is of type %qT",
6709 type, rhstype);
6710 break;
6711 case ic_assign:
6712 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6713 "%qT in assignment is invalid in C++", rhstype, type);
6714 break;
6715 case ic_init:
6716 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6717 "%qT to %qT in initialization is invalid in C++",
6718 rhstype, type);
6719 break;
6720 case ic_return:
6721 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6722 "%qT in return is invalid in C++", rhstype, type);
6723 break;
6724 default:
6725 gcc_unreachable ();
6726 }
6727 }
6728
6729 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6730 {
6731 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6732 return rhs;
6733 }
6734
6735 if (coder == VOID_TYPE)
6736 {
6737 /* Except for passing an argument to an unprototyped function,
6738 this is a constraint violation. When passing an argument to
6739 an unprototyped function, it is compile-time undefined;
6740 making it a constraint in that case was rejected in
6741 DR#252. */
6742 const char msg[] = "void value not ignored as it ought to be";
6743 if (warnopt)
6744 warning_at (location, warnopt, msg);
6745 else
6746 error_at (location, msg);
6747 return error_mark_node;
6748 }
6749 rhs = require_complete_type (location, rhs);
6750 if (rhs == error_mark_node)
6751 return error_mark_node;
6752
6753 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6754 return error_mark_node;
6755
6756 /* A non-reference type can convert to a reference. This handles
6757 va_start, va_copy and possibly port built-ins. */
6758 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6759 {
6760 if (!lvalue_p (rhs))
6761 {
6762 const char msg[] = "cannot pass rvalue to reference parameter";
6763 if (warnopt)
6764 warning_at (location, warnopt, msg);
6765 else
6766 error_at (location, msg);
6767 return error_mark_node;
6768 }
6769 if (!c_mark_addressable (rhs))
6770 return error_mark_node;
6771 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6772 SET_EXPR_LOCATION (rhs, location);
6773
6774 rhs = convert_for_assignment (location, expr_loc,
6775 build_pointer_type (TREE_TYPE (type)),
6776 rhs, origtype, errtype,
6777 null_pointer_constant, fundecl, function,
6778 parmnum, warnopt);
6779 if (rhs == error_mark_node)
6780 return error_mark_node;
6781
6782 rhs = build1 (NOP_EXPR, type, rhs);
6783 SET_EXPR_LOCATION (rhs, location);
6784 return rhs;
6785 }
6786 /* Some types can interconvert without explicit casts. */
6787 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6788 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6789 return convert (type, rhs);
6790 /* Arithmetic types all interconvert, and enum is treated like int. */
6791 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6792 || codel == FIXED_POINT_TYPE
6793 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6794 || codel == BOOLEAN_TYPE)
6795 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6796 || coder == FIXED_POINT_TYPE
6797 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6798 || coder == BOOLEAN_TYPE))
6799 {
6800 if (warnopt && errtype == ic_argpass)
6801 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
6802 rhstype);
6803
6804 bool save = in_late_binary_op;
6805 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6806 || (coder == REAL_TYPE
6807 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6808 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6809 in_late_binary_op = true;
6810 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6811 ? expr_loc : location, type, orig_rhs);
6812 in_late_binary_op = save;
6813 return ret;
6814 }
6815
6816 /* Aggregates in different TUs might need conversion. */
6817 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6818 && codel == coder
6819 && comptypes (type, rhstype))
6820 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6821 ? expr_loc : location, type, rhs);
6822
6823 /* Conversion to a transparent union or record from its member types.
6824 This applies only to function arguments. */
6825 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6826 && TYPE_TRANSPARENT_AGGR (type))
6827 && errtype == ic_argpass)
6828 {
6829 tree memb, marginal_memb = NULL_TREE;
6830
6831 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6832 {
6833 tree memb_type = TREE_TYPE (memb);
6834
6835 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6836 TYPE_MAIN_VARIANT (rhstype)))
6837 break;
6838
6839 if (TREE_CODE (memb_type) != POINTER_TYPE)
6840 continue;
6841
6842 if (coder == POINTER_TYPE)
6843 {
6844 tree ttl = TREE_TYPE (memb_type);
6845 tree ttr = TREE_TYPE (rhstype);
6846
6847 /* Any non-function converts to a [const][volatile] void *
6848 and vice versa; otherwise, targets must be the same.
6849 Meanwhile, the lhs target must have all the qualifiers of
6850 the rhs. */
6851 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6852 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6853 || comp_target_types (location, memb_type, rhstype))
6854 {
6855 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6856 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6857 /* If this type won't generate any warnings, use it. */
6858 if (lquals == rquals
6859 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6860 && TREE_CODE (ttl) == FUNCTION_TYPE)
6861 ? ((lquals | rquals) == rquals)
6862 : ((lquals | rquals) == lquals)))
6863 break;
6864
6865 /* Keep looking for a better type, but remember this one. */
6866 if (!marginal_memb)
6867 marginal_memb = memb;
6868 }
6869 }
6870
6871 /* Can convert integer zero to any pointer type. */
6872 if (null_pointer_constant)
6873 {
6874 rhs = null_pointer_node;
6875 break;
6876 }
6877 }
6878
6879 if (memb || marginal_memb)
6880 {
6881 if (!memb)
6882 {
6883 /* We have only a marginally acceptable member type;
6884 it needs a warning. */
6885 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6886 tree ttr = TREE_TYPE (rhstype);
6887
6888 /* Const and volatile mean something different for function
6889 types, so the usual warnings are not appropriate. */
6890 if (TREE_CODE (ttr) == FUNCTION_TYPE
6891 && TREE_CODE (ttl) == FUNCTION_TYPE)
6892 {
6893 /* Because const and volatile on functions are
6894 restrictions that say the function will not do
6895 certain things, it is okay to use a const or volatile
6896 function where an ordinary one is wanted, but not
6897 vice-versa. */
6898 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6899 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6900 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6901 OPT_Wdiscarded_qualifiers,
6902 G_("passing argument %d of %qE "
6903 "makes %q#v qualified function "
6904 "pointer from unqualified"),
6905 G_("assignment makes %q#v qualified "
6906 "function pointer from "
6907 "unqualified"),
6908 G_("initialization makes %q#v qualified "
6909 "function pointer from "
6910 "unqualified"),
6911 G_("return makes %q#v qualified function "
6912 "pointer from unqualified"),
6913 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6914 }
6915 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6916 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6917 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6918 OPT_Wdiscarded_qualifiers,
6919 G_("passing argument %d of %qE discards "
6920 "%qv qualifier from pointer target type"),
6921 G_("assignment discards %qv qualifier "
6922 "from pointer target type"),
6923 G_("initialization discards %qv qualifier "
6924 "from pointer target type"),
6925 G_("return discards %qv qualifier from "
6926 "pointer target type"),
6927 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6928
6929 memb = marginal_memb;
6930 }
6931
6932 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6933 pedwarn (location, OPT_Wpedantic,
6934 "ISO C prohibits argument conversion to union type");
6935
6936 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6937 return build_constructor_single (type, memb, rhs);
6938 }
6939 }
6940
6941 /* Conversions among pointers */
6942 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6943 && (coder == codel))
6944 {
6945 /* If RHS refers to a built-in declared without a prototype
6946 BLTIN is the declaration of the built-in with a prototype
6947 and RHSTYPE is set to the actual type of the built-in. */
6948 tree bltin;
6949 rhstype = type_or_builtin_type (rhs, &bltin);
6950
6951 tree ttl = TREE_TYPE (type);
6952 tree ttr = TREE_TYPE (rhstype);
6953 tree mvl = ttl;
6954 tree mvr = ttr;
6955 bool is_opaque_pointer;
6956 int target_cmp = 0; /* Cache comp_target_types () result. */
6957 addr_space_t asl;
6958 addr_space_t asr;
6959
6960 if (TREE_CODE (mvl) != ARRAY_TYPE)
6961 mvl = (TYPE_ATOMIC (mvl)
6962 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6963 TYPE_QUAL_ATOMIC)
6964 : TYPE_MAIN_VARIANT (mvl));
6965 if (TREE_CODE (mvr) != ARRAY_TYPE)
6966 mvr = (TYPE_ATOMIC (mvr)
6967 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6968 TYPE_QUAL_ATOMIC)
6969 : TYPE_MAIN_VARIANT (mvr));
6970 /* Opaque pointers are treated like void pointers. */
6971 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6972
6973 /* The Plan 9 compiler permits a pointer to a struct to be
6974 automatically converted into a pointer to an anonymous field
6975 within the struct. */
6976 if (flag_plan9_extensions
6977 && RECORD_OR_UNION_TYPE_P (mvl)
6978 && RECORD_OR_UNION_TYPE_P (mvr)
6979 && mvl != mvr)
6980 {
6981 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6982 if (new_rhs != NULL_TREE)
6983 {
6984 rhs = new_rhs;
6985 rhstype = TREE_TYPE (rhs);
6986 coder = TREE_CODE (rhstype);
6987 ttr = TREE_TYPE (rhstype);
6988 mvr = TYPE_MAIN_VARIANT (ttr);
6989 }
6990 }
6991
6992 /* C++ does not allow the implicit conversion void* -> T*. However,
6993 for the purpose of reducing the number of false positives, we
6994 tolerate the special case of
6995
6996 int *p = NULL;
6997
6998 where NULL is typically defined in C to be '(void *) 0'. */
6999 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7000 warning_at (errtype == ic_argpass ? expr_loc : location,
7001 OPT_Wc___compat,
7002 "request for implicit conversion "
7003 "from %qT to %qT not permitted in C++", rhstype, type);
7004
7005 /* See if the pointers point to incompatible address spaces. */
7006 asl = TYPE_ADDR_SPACE (ttl);
7007 asr = TYPE_ADDR_SPACE (ttr);
7008 if (!null_pointer_constant_p (rhs)
7009 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7010 {
7011 switch (errtype)
7012 {
7013 case ic_argpass:
7014 {
7015 const char msg[] = G_("passing argument %d of %qE from "
7016 "pointer to non-enclosed address space");
7017 if (warnopt)
7018 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7019 else
7020 error_at (expr_loc, msg, parmnum, rname);
7021 break;
7022 }
7023 case ic_assign:
7024 {
7025 const char msg[] = G_("assignment from pointer to "
7026 "non-enclosed address space");
7027 if (warnopt)
7028 warning_at (location, warnopt, msg);
7029 else
7030 error_at (location, msg);
7031 break;
7032 }
7033 case ic_init:
7034 {
7035 const char msg[] = G_("initialization from pointer to "
7036 "non-enclosed address space");
7037 if (warnopt)
7038 warning_at (location, warnopt, msg);
7039 else
7040 error_at (location, msg);
7041 break;
7042 }
7043 case ic_return:
7044 {
7045 const char msg[] = G_("return from pointer to "
7046 "non-enclosed address space");
7047 if (warnopt)
7048 warning_at (location, warnopt, msg);
7049 else
7050 error_at (location, msg);
7051 break;
7052 }
7053 default:
7054 gcc_unreachable ();
7055 }
7056 return error_mark_node;
7057 }
7058
7059 /* Check if the right-hand side has a format attribute but the
7060 left-hand side doesn't. */
7061 if (warn_suggest_attribute_format
7062 && check_missing_format_attribute (type, rhstype))
7063 {
7064 switch (errtype)
7065 {
7066 case ic_argpass:
7067 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7068 "argument %d of %qE might be "
7069 "a candidate for a format attribute",
7070 parmnum, rname);
7071 break;
7072 case ic_assign:
7073 warning_at (location, OPT_Wsuggest_attribute_format,
7074 "assignment left-hand side might be "
7075 "a candidate for a format attribute");
7076 break;
7077 case ic_init:
7078 warning_at (location, OPT_Wsuggest_attribute_format,
7079 "initialization left-hand side might be "
7080 "a candidate for a format attribute");
7081 break;
7082 case ic_return:
7083 warning_at (location, OPT_Wsuggest_attribute_format,
7084 "return type might be "
7085 "a candidate for a format attribute");
7086 break;
7087 default:
7088 gcc_unreachable ();
7089 }
7090 }
7091
7092 /* Any non-function converts to a [const][volatile] void *
7093 and vice versa; otherwise, targets must be the same.
7094 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7095 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7096 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7097 || (target_cmp = comp_target_types (location, type, rhstype))
7098 || is_opaque_pointer
7099 || ((c_common_unsigned_type (mvl)
7100 == c_common_unsigned_type (mvr))
7101 && (c_common_signed_type (mvl)
7102 == c_common_signed_type (mvr))
7103 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7104 {
7105 /* Warn about loss of qualifers from pointers to arrays with
7106 qualifiers on the element type. */
7107 if (TREE_CODE (ttr) == ARRAY_TYPE)
7108 {
7109 ttr = strip_array_types (ttr);
7110 ttl = strip_array_types (ttl);
7111
7112 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7113 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7114 WARNING_FOR_QUALIFIERS (location, expr_loc,
7115 OPT_Wdiscarded_array_qualifiers,
7116 G_("passing argument %d of %qE discards "
7117 "%qv qualifier from pointer target type"),
7118 G_("assignment discards %qv qualifier "
7119 "from pointer target type"),
7120 G_("initialization discards %qv qualifier "
7121 "from pointer target type"),
7122 G_("return discards %qv qualifier from "
7123 "pointer target type"),
7124 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7125 }
7126 else if (pedantic
7127 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7128 ||
7129 (VOID_TYPE_P (ttr)
7130 && !null_pointer_constant
7131 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7132 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7133 G_("ISO C forbids passing argument %d of "
7134 "%qE between function pointer "
7135 "and %<void *%>"),
7136 G_("ISO C forbids assignment between "
7137 "function pointer and %<void *%>"),
7138 G_("ISO C forbids initialization between "
7139 "function pointer and %<void *%>"),
7140 G_("ISO C forbids return between function "
7141 "pointer and %<void *%>"));
7142 /* Const and volatile mean something different for function types,
7143 so the usual warnings are not appropriate. */
7144 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7145 && TREE_CODE (ttl) != FUNCTION_TYPE)
7146 {
7147 /* Don't warn about loss of qualifier for conversions from
7148 qualified void* to pointers to arrays with corresponding
7149 qualifier on the element type. */
7150 if (!pedantic)
7151 ttl = strip_array_types (ttl);
7152
7153 /* Assignments between atomic and non-atomic objects are OK. */
7154 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7155 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7156 {
7157 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7158 OPT_Wdiscarded_qualifiers,
7159 G_("passing argument %d of %qE discards "
7160 "%qv qualifier from pointer target type"),
7161 G_("assignment discards %qv qualifier "
7162 "from pointer target type"),
7163 G_("initialization discards %qv qualifier "
7164 "from pointer target type"),
7165 G_("return discards %qv qualifier from "
7166 "pointer target type"),
7167 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7168 }
7169 /* If this is not a case of ignoring a mismatch in signedness,
7170 no warning. */
7171 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7172 || target_cmp)
7173 ;
7174 /* If there is a mismatch, do warn. */
7175 else if (warn_pointer_sign)
7176 switch (errtype)
7177 {
7178 case ic_argpass:
7179 {
7180 auto_diagnostic_group d;
7181 range_label_for_type_mismatch rhs_label (rhstype, type);
7182 gcc_rich_location richloc (expr_loc, &rhs_label);
7183 if (pedwarn (&richloc, OPT_Wpointer_sign,
7184 "pointer targets in passing argument %d of "
7185 "%qE differ in signedness", parmnum, rname))
7186 inform_for_arg (fundecl, expr_loc, parmnum, type,
7187 rhstype);
7188 }
7189 break;
7190 case ic_assign:
7191 pedwarn (location, OPT_Wpointer_sign,
7192 "pointer targets in assignment from %qT to %qT "
7193 "differ in signedness", rhstype, type);
7194 break;
7195 case ic_init:
7196 pedwarn_init (location, OPT_Wpointer_sign,
7197 "pointer targets in initialization of %qT "
7198 "from %qT differ in signedness", type,
7199 rhstype);
7200 break;
7201 case ic_return:
7202 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7203 "returning %qT from a function with return type "
7204 "%qT differ in signedness", rhstype, type);
7205 break;
7206 default:
7207 gcc_unreachable ();
7208 }
7209 }
7210 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7211 && TREE_CODE (ttr) == FUNCTION_TYPE)
7212 {
7213 /* Because const and volatile on functions are restrictions
7214 that say the function will not do certain things,
7215 it is okay to use a const or volatile function
7216 where an ordinary one is wanted, but not vice-versa. */
7217 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7218 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7219 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7220 OPT_Wdiscarded_qualifiers,
7221 G_("passing argument %d of %qE makes "
7222 "%q#v qualified function pointer "
7223 "from unqualified"),
7224 G_("assignment makes %q#v qualified function "
7225 "pointer from unqualified"),
7226 G_("initialization makes %q#v qualified "
7227 "function pointer from unqualified"),
7228 G_("return makes %q#v qualified function "
7229 "pointer from unqualified"),
7230 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7231 }
7232 }
7233 /* Avoid warning about the volatile ObjC EH puts on decls. */
7234 else if (!objc_ok)
7235 {
7236 switch (errtype)
7237 {
7238 case ic_argpass:
7239 {
7240 auto_diagnostic_group d;
7241 range_label_for_type_mismatch rhs_label (rhstype, type);
7242 gcc_rich_location richloc (expr_loc, &rhs_label);
7243 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7244 "passing argument %d of %qE from incompatible "
7245 "pointer type", parmnum, rname))
7246 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7247 }
7248 break;
7249 case ic_assign:
7250 if (bltin)
7251 pedwarn (location, OPT_Wincompatible_pointer_types,
7252 "assignment to %qT from pointer to "
7253 "%qD with incompatible type %qT",
7254 type, bltin, rhstype);
7255 else
7256 pedwarn (location, OPT_Wincompatible_pointer_types,
7257 "assignment to %qT from incompatible pointer type %qT",
7258 type, rhstype);
7259 break;
7260 case ic_init:
7261 if (bltin)
7262 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7263 "initialization of %qT from pointer to "
7264 "%qD with incompatible type %qT",
7265 type, bltin, rhstype);
7266 else
7267 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7268 "initialization of %qT from incompatible "
7269 "pointer type %qT",
7270 type, rhstype);
7271 break;
7272 case ic_return:
7273 if (bltin)
7274 pedwarn (location, OPT_Wincompatible_pointer_types,
7275 "returning pointer to %qD of type %qT from "
7276 "a function with incompatible type %qT",
7277 bltin, rhstype, type);
7278 else
7279 pedwarn (location, OPT_Wincompatible_pointer_types,
7280 "returning %qT from a function with incompatible "
7281 "return type %qT", rhstype, type);
7282 break;
7283 default:
7284 gcc_unreachable ();
7285 }
7286 }
7287
7288 /* If RHS isn't an address, check pointer or array of packed
7289 struct or union. */
7290 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7291
7292 return convert (type, rhs);
7293 }
7294 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7295 {
7296 /* ??? This should not be an error when inlining calls to
7297 unprototyped functions. */
7298 const char msg[] = "invalid use of non-lvalue array";
7299 if (warnopt)
7300 warning_at (location, warnopt, msg);
7301 else
7302 error_at (location, msg);
7303 return error_mark_node;
7304 }
7305 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7306 {
7307 /* An explicit constant 0 can convert to a pointer,
7308 or one that results from arithmetic, even including
7309 a cast to integer type. */
7310 if (!null_pointer_constant)
7311 switch (errtype)
7312 {
7313 case ic_argpass:
7314 {
7315 auto_diagnostic_group d;
7316 range_label_for_type_mismatch rhs_label (rhstype, type);
7317 gcc_rich_location richloc (expr_loc, &rhs_label);
7318 if (pedwarn (&richloc, OPT_Wint_conversion,
7319 "passing argument %d of %qE makes pointer from "
7320 "integer without a cast", parmnum, rname))
7321 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7322 }
7323 break;
7324 case ic_assign:
7325 pedwarn (location, OPT_Wint_conversion,
7326 "assignment to %qT from %qT makes pointer from integer "
7327 "without a cast", type, rhstype);
7328 break;
7329 case ic_init:
7330 pedwarn_init (location, OPT_Wint_conversion,
7331 "initialization of %qT from %qT makes pointer from "
7332 "integer without a cast", type, rhstype);
7333 break;
7334 case ic_return:
7335 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7336 "function with return type %qT makes pointer from "
7337 "integer without a cast", rhstype, type);
7338 break;
7339 default:
7340 gcc_unreachable ();
7341 }
7342
7343 return convert (type, rhs);
7344 }
7345 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7346 {
7347 switch (errtype)
7348 {
7349 case ic_argpass:
7350 {
7351 auto_diagnostic_group d;
7352 range_label_for_type_mismatch rhs_label (rhstype, type);
7353 gcc_rich_location richloc (expr_loc, &rhs_label);
7354 if (pedwarn (&richloc, OPT_Wint_conversion,
7355 "passing argument %d of %qE makes integer from "
7356 "pointer without a cast", parmnum, rname))
7357 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7358 }
7359 break;
7360 case ic_assign:
7361 pedwarn (location, OPT_Wint_conversion,
7362 "assignment to %qT from %qT makes integer from pointer "
7363 "without a cast", type, rhstype);
7364 break;
7365 case ic_init:
7366 pedwarn_init (location, OPT_Wint_conversion,
7367 "initialization of %qT from %qT makes integer from "
7368 "pointer without a cast", type, rhstype);
7369 break;
7370 case ic_return:
7371 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7372 "function with return type %qT makes integer from "
7373 "pointer without a cast", rhstype, type);
7374 break;
7375 default:
7376 gcc_unreachable ();
7377 }
7378
7379 return convert (type, rhs);
7380 }
7381 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7382 {
7383 tree ret;
7384 bool save = in_late_binary_op;
7385 in_late_binary_op = true;
7386 ret = convert (type, rhs);
7387 in_late_binary_op = save;
7388 return ret;
7389 }
7390
7391 switch (errtype)
7392 {
7393 case ic_argpass:
7394 {
7395 auto_diagnostic_group d;
7396 range_label_for_type_mismatch rhs_label (rhstype, type);
7397 gcc_rich_location richloc (expr_loc, &rhs_label);
7398 const char msg[] = G_("incompatible type for argument %d of %qE");
7399 if (warnopt)
7400 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7401 else
7402 error_at (&richloc, msg, parmnum, rname);
7403 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7404 }
7405 break;
7406 case ic_assign:
7407 {
7408 const char msg[]
7409 = G_("incompatible types when assigning to type %qT from type %qT");
7410 if (warnopt)
7411 warning_at (expr_loc, 0, msg, type, rhstype);
7412 else
7413 error_at (expr_loc, msg, type, rhstype);
7414 break;
7415 }
7416 case ic_init:
7417 {
7418 const char msg[]
7419 = G_("incompatible types when initializing type %qT using type %qT");
7420 if (warnopt)
7421 warning_at (location, 0, msg, type, rhstype);
7422 else
7423 error_at (location, msg, type, rhstype);
7424 break;
7425 }
7426 case ic_return:
7427 {
7428 const char msg[]
7429 = G_("incompatible types when returning type %qT but %qT was expected");
7430 if (warnopt)
7431 warning_at (location, 0, msg, rhstype, type);
7432 else
7433 error_at (location, msg, rhstype, type);
7434 break;
7435 }
7436 default:
7437 gcc_unreachable ();
7438 }
7439
7440 return error_mark_node;
7441 }
7442 \f
7443 /* If VALUE is a compound expr all of whose expressions are constant, then
7444 return its value. Otherwise, return error_mark_node.
7445
7446 This is for handling COMPOUND_EXPRs as initializer elements
7447 which is allowed with a warning when -pedantic is specified. */
7448
7449 static tree
7450 valid_compound_expr_initializer (tree value, tree endtype)
7451 {
7452 if (TREE_CODE (value) == COMPOUND_EXPR)
7453 {
7454 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7455 == error_mark_node)
7456 return error_mark_node;
7457 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7458 endtype);
7459 }
7460 else if (!initializer_constant_valid_p (value, endtype))
7461 return error_mark_node;
7462 else
7463 return value;
7464 }
7465 \f
7466 /* Perform appropriate conversions on the initial value of a variable,
7467 store it in the declaration DECL,
7468 and print any error messages that are appropriate.
7469 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7470 If the init is invalid, store an ERROR_MARK.
7471
7472 INIT_LOC is the location of the initial value. */
7473
7474 void
7475 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7476 {
7477 tree value, type;
7478 bool npc = false;
7479
7480 /* If variable's type was invalidly declared, just ignore it. */
7481
7482 type = TREE_TYPE (decl);
7483 if (TREE_CODE (type) == ERROR_MARK)
7484 return;
7485
7486 /* Digest the specified initializer into an expression. */
7487
7488 if (init)
7489 npc = null_pointer_constant_p (init);
7490 value = digest_init (init_loc, type, init, origtype, npc,
7491 true, TREE_STATIC (decl));
7492
7493 /* Store the expression if valid; else report error. */
7494
7495 if (!in_system_header_at (input_location)
7496 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7497 warning (OPT_Wtraditional, "traditional C rejects automatic "
7498 "aggregate initialization");
7499
7500 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7501 DECL_INITIAL (decl) = value;
7502
7503 /* ANSI wants warnings about out-of-range constant initializers. */
7504 STRIP_TYPE_NOPS (value);
7505 if (TREE_STATIC (decl))
7506 constant_expression_warning (value);
7507
7508 /* Check if we need to set array size from compound literal size. */
7509 if (TREE_CODE (type) == ARRAY_TYPE
7510 && TYPE_DOMAIN (type) == NULL_TREE
7511 && value != error_mark_node)
7512 {
7513 tree inside_init = init;
7514
7515 STRIP_TYPE_NOPS (inside_init);
7516 inside_init = fold (inside_init);
7517
7518 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7519 {
7520 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7521
7522 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7523 {
7524 /* For int foo[] = (int [3]){1}; we need to set array size
7525 now since later on array initializer will be just the
7526 brace enclosed list of the compound literal. */
7527 tree etype = strip_array_types (TREE_TYPE (decl));
7528 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7529 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7530 layout_type (type);
7531 layout_decl (cldecl, 0);
7532 TREE_TYPE (decl)
7533 = c_build_qualified_type (type, TYPE_QUALS (etype));
7534 }
7535 }
7536 }
7537 }
7538 \f
7539 /* Methods for storing and printing names for error messages. */
7540
7541 /* Implement a spelling stack that allows components of a name to be pushed
7542 and popped. Each element on the stack is this structure. */
7543
7544 struct spelling
7545 {
7546 int kind;
7547 union
7548 {
7549 unsigned HOST_WIDE_INT i;
7550 const char *s;
7551 } u;
7552 };
7553
7554 #define SPELLING_STRING 1
7555 #define SPELLING_MEMBER 2
7556 #define SPELLING_BOUNDS 3
7557
7558 static struct spelling *spelling; /* Next stack element (unused). */
7559 static struct spelling *spelling_base; /* Spelling stack base. */
7560 static int spelling_size; /* Size of the spelling stack. */
7561
7562 /* Macros to save and restore the spelling stack around push_... functions.
7563 Alternative to SAVE_SPELLING_STACK. */
7564
7565 #define SPELLING_DEPTH() (spelling - spelling_base)
7566 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7567
7568 /* Push an element on the spelling stack with type KIND and assign VALUE
7569 to MEMBER. */
7570
7571 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7572 { \
7573 int depth = SPELLING_DEPTH (); \
7574 \
7575 if (depth >= spelling_size) \
7576 { \
7577 spelling_size += 10; \
7578 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7579 spelling_size); \
7580 RESTORE_SPELLING_DEPTH (depth); \
7581 } \
7582 \
7583 spelling->kind = (KIND); \
7584 spelling->MEMBER = (VALUE); \
7585 spelling++; \
7586 }
7587
7588 /* Push STRING on the stack. Printed literally. */
7589
7590 static void
7591 push_string (const char *string)
7592 {
7593 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7594 }
7595
7596 /* Push a member name on the stack. Printed as '.' STRING. */
7597
7598 static void
7599 push_member_name (tree decl)
7600 {
7601 const char *const string
7602 = (DECL_NAME (decl)
7603 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7604 : _("<anonymous>"));
7605 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7606 }
7607
7608 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7609
7610 static void
7611 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7612 {
7613 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7614 }
7615
7616 /* Compute the maximum size in bytes of the printed spelling. */
7617
7618 static int
7619 spelling_length (void)
7620 {
7621 int size = 0;
7622 struct spelling *p;
7623
7624 for (p = spelling_base; p < spelling; p++)
7625 {
7626 if (p->kind == SPELLING_BOUNDS)
7627 size += 25;
7628 else
7629 size += strlen (p->u.s) + 1;
7630 }
7631
7632 return size;
7633 }
7634
7635 /* Print the spelling to BUFFER and return it. */
7636
7637 static char *
7638 print_spelling (char *buffer)
7639 {
7640 char *d = buffer;
7641 struct spelling *p;
7642
7643 for (p = spelling_base; p < spelling; p++)
7644 if (p->kind == SPELLING_BOUNDS)
7645 {
7646 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7647 d += strlen (d);
7648 }
7649 else
7650 {
7651 const char *s;
7652 if (p->kind == SPELLING_MEMBER)
7653 *d++ = '.';
7654 for (s = p->u.s; (*d = *s++); d++)
7655 ;
7656 }
7657 *d++ = '\0';
7658 return buffer;
7659 }
7660
7661 /* Digest the parser output INIT as an initializer for type TYPE.
7662 Return a C expression of type TYPE to represent the initial value.
7663
7664 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7665
7666 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7667
7668 If INIT is a string constant, STRICT_STRING is true if it is
7669 unparenthesized or we should not warn here for it being parenthesized.
7670 For other types of INIT, STRICT_STRING is not used.
7671
7672 INIT_LOC is the location of the INIT.
7673
7674 REQUIRE_CONSTANT requests an error if non-constant initializers or
7675 elements are seen. */
7676
7677 static tree
7678 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7679 bool null_pointer_constant, bool strict_string,
7680 int require_constant)
7681 {
7682 enum tree_code code = TREE_CODE (type);
7683 tree inside_init = init;
7684 tree semantic_type = NULL_TREE;
7685 bool maybe_const = true;
7686
7687 if (type == error_mark_node
7688 || !init
7689 || error_operand_p (init))
7690 return error_mark_node;
7691
7692 STRIP_TYPE_NOPS (inside_init);
7693
7694 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7695 {
7696 semantic_type = TREE_TYPE (inside_init);
7697 inside_init = TREE_OPERAND (inside_init, 0);
7698 }
7699 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7700
7701 /* Initialization of an array of chars from a string constant
7702 optionally enclosed in braces. */
7703
7704 if (code == ARRAY_TYPE && inside_init
7705 && TREE_CODE (inside_init) == STRING_CST)
7706 {
7707 tree typ1
7708 = (TYPE_ATOMIC (TREE_TYPE (type))
7709 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7710 TYPE_QUAL_ATOMIC)
7711 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7712 /* Note that an array could be both an array of character type
7713 and an array of wchar_t if wchar_t is signed char or unsigned
7714 char. */
7715 bool char_array = (typ1 == char_type_node
7716 || typ1 == signed_char_type_node
7717 || typ1 == unsigned_char_type_node);
7718 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7719 bool char16_array = !!comptypes (typ1, char16_type_node);
7720 bool char32_array = !!comptypes (typ1, char32_type_node);
7721
7722 if (char_array || wchar_array || char16_array || char32_array)
7723 {
7724 struct c_expr expr;
7725 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7726 bool incompat_string_cst = false;
7727 expr.value = inside_init;
7728 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7729 expr.original_type = NULL;
7730 maybe_warn_string_init (init_loc, type, expr);
7731
7732 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7733 pedwarn_init (init_loc, OPT_Wpedantic,
7734 "initialization of a flexible array member");
7735
7736 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7737 TYPE_MAIN_VARIANT (type)))
7738 return inside_init;
7739
7740 if (char_array)
7741 {
7742 if (typ2 != char_type_node)
7743 incompat_string_cst = true;
7744 }
7745 else if (!comptypes (typ1, typ2))
7746 incompat_string_cst = true;
7747
7748 if (incompat_string_cst)
7749 {
7750 error_init (init_loc, "cannot initialize array of %qT from "
7751 "a string literal with type array of %qT",
7752 typ1, typ2);
7753 return error_mark_node;
7754 }
7755
7756 if (TYPE_DOMAIN (type) != NULL_TREE
7757 && TYPE_SIZE (type) != NULL_TREE
7758 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7759 {
7760 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7761 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
7762
7763 /* Subtract the size of a single (possibly wide) character
7764 because it's ok to ignore the terminating null char
7765 that is counted in the length of the constant. */
7766 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
7767 pedwarn_init (init_loc, 0,
7768 ("initializer-string for array of %qT "
7769 "is too long"), typ1);
7770 else if (warn_cxx_compat
7771 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7772 warning_at (init_loc, OPT_Wc___compat,
7773 ("initializer-string for array of %qT "
7774 "is too long for C++"), typ1);
7775 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7776 {
7777 unsigned HOST_WIDE_INT size
7778 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7779 const char *p = TREE_STRING_POINTER (inside_init);
7780
7781 inside_init = build_string (size, p);
7782 }
7783 }
7784
7785 TREE_TYPE (inside_init) = type;
7786 return inside_init;
7787 }
7788 else if (INTEGRAL_TYPE_P (typ1))
7789 {
7790 error_init (init_loc, "array of inappropriate type initialized "
7791 "from string constant");
7792 return error_mark_node;
7793 }
7794 }
7795
7796 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7797 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7798 below and handle as a constructor. */
7799 if (code == VECTOR_TYPE
7800 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7801 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7802 && TREE_CONSTANT (inside_init))
7803 {
7804 if (TREE_CODE (inside_init) == VECTOR_CST
7805 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7806 TYPE_MAIN_VARIANT (type)))
7807 return inside_init;
7808
7809 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7810 {
7811 unsigned HOST_WIDE_INT ix;
7812 tree value;
7813 bool constant_p = true;
7814
7815 /* Iterate through elements and check if all constructor
7816 elements are *_CSTs. */
7817 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7818 if (!CONSTANT_CLASS_P (value))
7819 {
7820 constant_p = false;
7821 break;
7822 }
7823
7824 if (constant_p)
7825 return build_vector_from_ctor (type,
7826 CONSTRUCTOR_ELTS (inside_init));
7827 }
7828 }
7829
7830 if (warn_sequence_point)
7831 verify_sequence_points (inside_init);
7832
7833 /* Any type can be initialized
7834 from an expression of the same type, optionally with braces. */
7835
7836 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7837 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7838 TYPE_MAIN_VARIANT (type))
7839 || (code == ARRAY_TYPE
7840 && comptypes (TREE_TYPE (inside_init), type))
7841 || (code == VECTOR_TYPE
7842 && comptypes (TREE_TYPE (inside_init), type))
7843 || (code == POINTER_TYPE
7844 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7845 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7846 TREE_TYPE (type)))))
7847 {
7848 if (code == POINTER_TYPE)
7849 {
7850 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7851 {
7852 if (TREE_CODE (inside_init) == STRING_CST
7853 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7854 inside_init = array_to_pointer_conversion
7855 (init_loc, inside_init);
7856 else
7857 {
7858 error_init (init_loc, "invalid use of non-lvalue array");
7859 return error_mark_node;
7860 }
7861 }
7862 }
7863
7864 if (code == VECTOR_TYPE)
7865 /* Although the types are compatible, we may require a
7866 conversion. */
7867 inside_init = convert (type, inside_init);
7868
7869 if (require_constant
7870 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7871 {
7872 /* As an extension, allow initializing objects with static storage
7873 duration with compound literals (which are then treated just as
7874 the brace enclosed list they contain). Also allow this for
7875 vectors, as we can only assign them with compound literals. */
7876 if (flag_isoc99 && code != VECTOR_TYPE)
7877 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7878 "is not constant");
7879 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7880 inside_init = DECL_INITIAL (decl);
7881 }
7882
7883 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7884 && TREE_CODE (inside_init) != CONSTRUCTOR)
7885 {
7886 error_init (init_loc, "array initialized from non-constant array "
7887 "expression");
7888 return error_mark_node;
7889 }
7890
7891 /* Compound expressions can only occur here if -Wpedantic or
7892 -pedantic-errors is specified. In the later case, we always want
7893 an error. In the former case, we simply want a warning. */
7894 if (require_constant && pedantic
7895 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7896 {
7897 inside_init
7898 = valid_compound_expr_initializer (inside_init,
7899 TREE_TYPE (inside_init));
7900 if (inside_init == error_mark_node)
7901 error_init (init_loc, "initializer element is not constant");
7902 else
7903 pedwarn_init (init_loc, OPT_Wpedantic,
7904 "initializer element is not constant");
7905 if (flag_pedantic_errors)
7906 inside_init = error_mark_node;
7907 }
7908 else if (require_constant
7909 && !initializer_constant_valid_p (inside_init,
7910 TREE_TYPE (inside_init)))
7911 {
7912 error_init (init_loc, "initializer element is not constant");
7913 inside_init = error_mark_node;
7914 }
7915 else if (require_constant && !maybe_const)
7916 pedwarn_init (init_loc, OPT_Wpedantic,
7917 "initializer element is not a constant expression");
7918
7919 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7920 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7921 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7922 type, inside_init, origtype,
7923 ic_init, null_pointer_constant,
7924 NULL_TREE, NULL_TREE, 0);
7925 return inside_init;
7926 }
7927
7928 /* Handle scalar types, including conversions. */
7929
7930 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7931 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7932 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7933 {
7934 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7935 && (TREE_CODE (init) == STRING_CST
7936 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7937 inside_init = init = array_to_pointer_conversion (init_loc, init);
7938 if (semantic_type)
7939 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7940 inside_init);
7941 inside_init
7942 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7943 inside_init, origtype, ic_init,
7944 null_pointer_constant, NULL_TREE, NULL_TREE,
7945 0);
7946
7947 /* Check to see if we have already given an error message. */
7948 if (inside_init == error_mark_node)
7949 ;
7950 else if (require_constant && !TREE_CONSTANT (inside_init))
7951 {
7952 error_init (init_loc, "initializer element is not constant");
7953 inside_init = error_mark_node;
7954 }
7955 else if (require_constant
7956 && !initializer_constant_valid_p (inside_init,
7957 TREE_TYPE (inside_init)))
7958 {
7959 error_init (init_loc, "initializer element is not computable at "
7960 "load time");
7961 inside_init = error_mark_node;
7962 }
7963 else if (require_constant && !maybe_const)
7964 pedwarn_init (init_loc, OPT_Wpedantic,
7965 "initializer element is not a constant expression");
7966
7967 return inside_init;
7968 }
7969
7970 /* Come here only for records and arrays. */
7971
7972 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7973 {
7974 error_init (init_loc, "variable-sized object may not be initialized");
7975 return error_mark_node;
7976 }
7977
7978 error_init (init_loc, "invalid initializer");
7979 return error_mark_node;
7980 }
7981 \f
7982 /* Handle initializers that use braces. */
7983
7984 /* Type of object we are accumulating a constructor for.
7985 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7986 static tree constructor_type;
7987
7988 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7989 left to fill. */
7990 static tree constructor_fields;
7991
7992 /* For an ARRAY_TYPE, this is the specified index
7993 at which to store the next element we get. */
7994 static tree constructor_index;
7995
7996 /* For an ARRAY_TYPE, this is the maximum index. */
7997 static tree constructor_max_index;
7998
7999 /* For a RECORD_TYPE, this is the first field not yet written out. */
8000 static tree constructor_unfilled_fields;
8001
8002 /* For an ARRAY_TYPE, this is the index of the first element
8003 not yet written out. */
8004 static tree constructor_unfilled_index;
8005
8006 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8007 This is so we can generate gaps between fields, when appropriate. */
8008 static tree constructor_bit_index;
8009
8010 /* If we are saving up the elements rather than allocating them,
8011 this is the list of elements so far (in reverse order,
8012 most recent first). */
8013 static vec<constructor_elt, va_gc> *constructor_elements;
8014
8015 /* 1 if constructor should be incrementally stored into a constructor chain,
8016 0 if all the elements should be kept in AVL tree. */
8017 static int constructor_incremental;
8018
8019 /* 1 if so far this constructor's elements are all compile-time constants. */
8020 static int constructor_constant;
8021
8022 /* 1 if so far this constructor's elements are all valid address constants. */
8023 static int constructor_simple;
8024
8025 /* 1 if this constructor has an element that cannot be part of a
8026 constant expression. */
8027 static int constructor_nonconst;
8028
8029 /* 1 if this constructor is erroneous so far. */
8030 static int constructor_erroneous;
8031
8032 /* 1 if this constructor is the universal zero initializer { 0 }. */
8033 static int constructor_zeroinit;
8034
8035 /* Structure for managing pending initializer elements, organized as an
8036 AVL tree. */
8037
8038 struct init_node
8039 {
8040 struct init_node *left, *right;
8041 struct init_node *parent;
8042 int balance;
8043 tree purpose;
8044 tree value;
8045 tree origtype;
8046 };
8047
8048 /* Tree of pending elements at this constructor level.
8049 These are elements encountered out of order
8050 which belong at places we haven't reached yet in actually
8051 writing the output.
8052 Will never hold tree nodes across GC runs. */
8053 static struct init_node *constructor_pending_elts;
8054
8055 /* The SPELLING_DEPTH of this constructor. */
8056 static int constructor_depth;
8057
8058 /* DECL node for which an initializer is being read.
8059 0 means we are reading a constructor expression
8060 such as (struct foo) {...}. */
8061 static tree constructor_decl;
8062
8063 /* Nonzero if this is an initializer for a top-level decl. */
8064 static int constructor_top_level;
8065
8066 /* Nonzero if there were any member designators in this initializer. */
8067 static int constructor_designated;
8068
8069 /* Nesting depth of designator list. */
8070 static int designator_depth;
8071
8072 /* Nonzero if there were diagnosed errors in this designator list. */
8073 static int designator_erroneous;
8074
8075 \f
8076 /* This stack has a level for each implicit or explicit level of
8077 structuring in the initializer, including the outermost one. It
8078 saves the values of most of the variables above. */
8079
8080 struct constructor_range_stack;
8081
8082 struct constructor_stack
8083 {
8084 struct constructor_stack *next;
8085 tree type;
8086 tree fields;
8087 tree index;
8088 tree max_index;
8089 tree unfilled_index;
8090 tree unfilled_fields;
8091 tree bit_index;
8092 vec<constructor_elt, va_gc> *elements;
8093 struct init_node *pending_elts;
8094 int offset;
8095 int depth;
8096 /* If value nonzero, this value should replace the entire
8097 constructor at this level. */
8098 struct c_expr replacement_value;
8099 struct constructor_range_stack *range_stack;
8100 char constant;
8101 char simple;
8102 char nonconst;
8103 char implicit;
8104 char erroneous;
8105 char outer;
8106 char incremental;
8107 char designated;
8108 int designator_depth;
8109 };
8110
8111 static struct constructor_stack *constructor_stack;
8112
8113 /* This stack represents designators from some range designator up to
8114 the last designator in the list. */
8115
8116 struct constructor_range_stack
8117 {
8118 struct constructor_range_stack *next, *prev;
8119 struct constructor_stack *stack;
8120 tree range_start;
8121 tree index;
8122 tree range_end;
8123 tree fields;
8124 };
8125
8126 static struct constructor_range_stack *constructor_range_stack;
8127
8128 /* This stack records separate initializers that are nested.
8129 Nested initializers can't happen in ANSI C, but GNU C allows them
8130 in cases like { ... (struct foo) { ... } ... }. */
8131
8132 struct initializer_stack
8133 {
8134 struct initializer_stack *next;
8135 tree decl;
8136 struct constructor_stack *constructor_stack;
8137 struct constructor_range_stack *constructor_range_stack;
8138 vec<constructor_elt, va_gc> *elements;
8139 struct spelling *spelling;
8140 struct spelling *spelling_base;
8141 int spelling_size;
8142 char top_level;
8143 char require_constant_value;
8144 char require_constant_elements;
8145 rich_location *missing_brace_richloc;
8146 };
8147
8148 static struct initializer_stack *initializer_stack;
8149 \f
8150 /* Prepare to parse and output the initializer for variable DECL. */
8151
8152 void
8153 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8154 rich_location *richloc)
8155 {
8156 const char *locus;
8157 struct initializer_stack *p = XNEW (struct initializer_stack);
8158
8159 p->decl = constructor_decl;
8160 p->require_constant_value = require_constant_value;
8161 p->require_constant_elements = require_constant_elements;
8162 p->constructor_stack = constructor_stack;
8163 p->constructor_range_stack = constructor_range_stack;
8164 p->elements = constructor_elements;
8165 p->spelling = spelling;
8166 p->spelling_base = spelling_base;
8167 p->spelling_size = spelling_size;
8168 p->top_level = constructor_top_level;
8169 p->next = initializer_stack;
8170 p->missing_brace_richloc = richloc;
8171 initializer_stack = p;
8172
8173 constructor_decl = decl;
8174 constructor_designated = 0;
8175 constructor_top_level = top_level;
8176
8177 if (decl != NULL_TREE && decl != error_mark_node)
8178 {
8179 require_constant_value = TREE_STATIC (decl);
8180 require_constant_elements
8181 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8182 /* For a scalar, you can always use any value to initialize,
8183 even within braces. */
8184 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8185 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8186 }
8187 else
8188 {
8189 require_constant_value = 0;
8190 require_constant_elements = 0;
8191 locus = _("(anonymous)");
8192 }
8193
8194 constructor_stack = 0;
8195 constructor_range_stack = 0;
8196
8197 found_missing_braces = 0;
8198
8199 spelling_base = 0;
8200 spelling_size = 0;
8201 RESTORE_SPELLING_DEPTH (0);
8202
8203 if (locus)
8204 push_string (locus);
8205 }
8206
8207 void
8208 finish_init (void)
8209 {
8210 struct initializer_stack *p = initializer_stack;
8211
8212 /* Free the whole constructor stack of this initializer. */
8213 while (constructor_stack)
8214 {
8215 struct constructor_stack *q = constructor_stack;
8216 constructor_stack = q->next;
8217 free (q);
8218 }
8219
8220 gcc_assert (!constructor_range_stack);
8221
8222 /* Pop back to the data of the outer initializer (if any). */
8223 free (spelling_base);
8224
8225 constructor_decl = p->decl;
8226 require_constant_value = p->require_constant_value;
8227 require_constant_elements = p->require_constant_elements;
8228 constructor_stack = p->constructor_stack;
8229 constructor_range_stack = p->constructor_range_stack;
8230 constructor_elements = p->elements;
8231 spelling = p->spelling;
8232 spelling_base = p->spelling_base;
8233 spelling_size = p->spelling_size;
8234 constructor_top_level = p->top_level;
8235 initializer_stack = p->next;
8236 free (p);
8237 }
8238 \f
8239 /* Call here when we see the initializer is surrounded by braces.
8240 This is instead of a call to push_init_level;
8241 it is matched by a call to pop_init_level.
8242
8243 TYPE is the type to initialize, for a constructor expression.
8244 For an initializer for a decl, TYPE is zero. */
8245
8246 void
8247 really_start_incremental_init (tree type)
8248 {
8249 struct constructor_stack *p = XNEW (struct constructor_stack);
8250
8251 if (type == NULL_TREE)
8252 type = TREE_TYPE (constructor_decl);
8253
8254 if (VECTOR_TYPE_P (type)
8255 && TYPE_VECTOR_OPAQUE (type))
8256 error ("opaque vector types cannot be initialized");
8257
8258 p->type = constructor_type;
8259 p->fields = constructor_fields;
8260 p->index = constructor_index;
8261 p->max_index = constructor_max_index;
8262 p->unfilled_index = constructor_unfilled_index;
8263 p->unfilled_fields = constructor_unfilled_fields;
8264 p->bit_index = constructor_bit_index;
8265 p->elements = constructor_elements;
8266 p->constant = constructor_constant;
8267 p->simple = constructor_simple;
8268 p->nonconst = constructor_nonconst;
8269 p->erroneous = constructor_erroneous;
8270 p->pending_elts = constructor_pending_elts;
8271 p->depth = constructor_depth;
8272 p->replacement_value.value = 0;
8273 p->replacement_value.original_code = ERROR_MARK;
8274 p->replacement_value.original_type = NULL;
8275 p->implicit = 0;
8276 p->range_stack = 0;
8277 p->outer = 0;
8278 p->incremental = constructor_incremental;
8279 p->designated = constructor_designated;
8280 p->designator_depth = designator_depth;
8281 p->next = 0;
8282 constructor_stack = p;
8283
8284 constructor_constant = 1;
8285 constructor_simple = 1;
8286 constructor_nonconst = 0;
8287 constructor_depth = SPELLING_DEPTH ();
8288 constructor_elements = NULL;
8289 constructor_pending_elts = 0;
8290 constructor_type = type;
8291 constructor_incremental = 1;
8292 constructor_designated = 0;
8293 constructor_zeroinit = 1;
8294 designator_depth = 0;
8295 designator_erroneous = 0;
8296
8297 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8298 {
8299 constructor_fields = TYPE_FIELDS (constructor_type);
8300 /* Skip any nameless bit fields at the beginning. */
8301 while (constructor_fields != NULL_TREE
8302 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8303 constructor_fields = DECL_CHAIN (constructor_fields);
8304
8305 constructor_unfilled_fields = constructor_fields;
8306 constructor_bit_index = bitsize_zero_node;
8307 }
8308 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8309 {
8310 if (TYPE_DOMAIN (constructor_type))
8311 {
8312 constructor_max_index
8313 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8314
8315 /* Detect non-empty initializations of zero-length arrays. */
8316 if (constructor_max_index == NULL_TREE
8317 && TYPE_SIZE (constructor_type))
8318 constructor_max_index = integer_minus_one_node;
8319
8320 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8321 to initialize VLAs will cause a proper error; avoid tree
8322 checking errors as well by setting a safe value. */
8323 if (constructor_max_index
8324 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8325 constructor_max_index = integer_minus_one_node;
8326
8327 constructor_index
8328 = convert (bitsizetype,
8329 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8330 }
8331 else
8332 {
8333 constructor_index = bitsize_zero_node;
8334 constructor_max_index = NULL_TREE;
8335 }
8336
8337 constructor_unfilled_index = constructor_index;
8338 }
8339 else if (VECTOR_TYPE_P (constructor_type))
8340 {
8341 /* Vectors are like simple fixed-size arrays. */
8342 constructor_max_index =
8343 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8344 constructor_index = bitsize_zero_node;
8345 constructor_unfilled_index = constructor_index;
8346 }
8347 else
8348 {
8349 /* Handle the case of int x = {5}; */
8350 constructor_fields = constructor_type;
8351 constructor_unfilled_fields = constructor_type;
8352 }
8353 }
8354 \f
8355 extern location_t last_init_list_comma;
8356
8357 /* Called when we see an open brace for a nested initializer. Finish
8358 off any pending levels with implicit braces. */
8359 void
8360 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8361 {
8362 while (constructor_stack->implicit)
8363 {
8364 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8365 && constructor_fields == NULL_TREE)
8366 process_init_element (input_location,
8367 pop_init_level (loc, 1, braced_init_obstack,
8368 last_init_list_comma),
8369 true, braced_init_obstack);
8370 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8371 && constructor_max_index
8372 && tree_int_cst_lt (constructor_max_index,
8373 constructor_index))
8374 process_init_element (input_location,
8375 pop_init_level (loc, 1, braced_init_obstack,
8376 last_init_list_comma),
8377 true, braced_init_obstack);
8378 else
8379 break;
8380 }
8381 }
8382
8383 /* Push down into a subobject, for initialization.
8384 If this is for an explicit set of braces, IMPLICIT is 0.
8385 If it is because the next element belongs at a lower level,
8386 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8387
8388 void
8389 push_init_level (location_t loc, int implicit,
8390 struct obstack *braced_init_obstack)
8391 {
8392 struct constructor_stack *p;
8393 tree value = NULL_TREE;
8394
8395 /* Unless this is an explicit brace, we need to preserve previous
8396 content if any. */
8397 if (implicit)
8398 {
8399 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8400 value = find_init_member (constructor_fields, braced_init_obstack);
8401 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8402 value = find_init_member (constructor_index, braced_init_obstack);
8403 }
8404
8405 p = XNEW (struct constructor_stack);
8406 p->type = constructor_type;
8407 p->fields = constructor_fields;
8408 p->index = constructor_index;
8409 p->max_index = constructor_max_index;
8410 p->unfilled_index = constructor_unfilled_index;
8411 p->unfilled_fields = constructor_unfilled_fields;
8412 p->bit_index = constructor_bit_index;
8413 p->elements = constructor_elements;
8414 p->constant = constructor_constant;
8415 p->simple = constructor_simple;
8416 p->nonconst = constructor_nonconst;
8417 p->erroneous = constructor_erroneous;
8418 p->pending_elts = constructor_pending_elts;
8419 p->depth = constructor_depth;
8420 p->replacement_value.value = NULL_TREE;
8421 p->replacement_value.original_code = ERROR_MARK;
8422 p->replacement_value.original_type = NULL;
8423 p->implicit = implicit;
8424 p->outer = 0;
8425 p->incremental = constructor_incremental;
8426 p->designated = constructor_designated;
8427 p->designator_depth = designator_depth;
8428 p->next = constructor_stack;
8429 p->range_stack = 0;
8430 constructor_stack = p;
8431
8432 constructor_constant = 1;
8433 constructor_simple = 1;
8434 constructor_nonconst = 0;
8435 constructor_depth = SPELLING_DEPTH ();
8436 constructor_elements = NULL;
8437 constructor_incremental = 1;
8438 constructor_designated = 0;
8439 constructor_pending_elts = 0;
8440 if (!implicit)
8441 {
8442 p->range_stack = constructor_range_stack;
8443 constructor_range_stack = 0;
8444 designator_depth = 0;
8445 designator_erroneous = 0;
8446 }
8447
8448 /* Don't die if an entire brace-pair level is superfluous
8449 in the containing level. */
8450 if (constructor_type == NULL_TREE)
8451 ;
8452 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8453 {
8454 /* Don't die if there are extra init elts at the end. */
8455 if (constructor_fields == NULL_TREE)
8456 constructor_type = NULL_TREE;
8457 else
8458 {
8459 constructor_type = TREE_TYPE (constructor_fields);
8460 push_member_name (constructor_fields);
8461 constructor_depth++;
8462 }
8463 /* If upper initializer is designated, then mark this as
8464 designated too to prevent bogus warnings. */
8465 constructor_designated = p->designated;
8466 }
8467 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8468 {
8469 constructor_type = TREE_TYPE (constructor_type);
8470 push_array_bounds (tree_to_uhwi (constructor_index));
8471 constructor_depth++;
8472 }
8473
8474 if (constructor_type == NULL_TREE)
8475 {
8476 error_init (loc, "extra brace group at end of initializer");
8477 constructor_fields = NULL_TREE;
8478 constructor_unfilled_fields = NULL_TREE;
8479 return;
8480 }
8481
8482 if (value && TREE_CODE (value) == CONSTRUCTOR)
8483 {
8484 constructor_constant = TREE_CONSTANT (value);
8485 constructor_simple = TREE_STATIC (value);
8486 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8487 constructor_elements = CONSTRUCTOR_ELTS (value);
8488 if (!vec_safe_is_empty (constructor_elements)
8489 && (TREE_CODE (constructor_type) == RECORD_TYPE
8490 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8491 set_nonincremental_init (braced_init_obstack);
8492 }
8493
8494 if (implicit == 1)
8495 {
8496 found_missing_braces = 1;
8497 if (initializer_stack->missing_brace_richloc)
8498 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8499 (loc, "{");
8500 }
8501
8502 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8503 {
8504 constructor_fields = TYPE_FIELDS (constructor_type);
8505 /* Skip any nameless bit fields at the beginning. */
8506 while (constructor_fields != NULL_TREE
8507 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8508 constructor_fields = DECL_CHAIN (constructor_fields);
8509
8510 constructor_unfilled_fields = constructor_fields;
8511 constructor_bit_index = bitsize_zero_node;
8512 }
8513 else if (VECTOR_TYPE_P (constructor_type))
8514 {
8515 /* Vectors are like simple fixed-size arrays. */
8516 constructor_max_index =
8517 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8518 constructor_index = bitsize_int (0);
8519 constructor_unfilled_index = constructor_index;
8520 }
8521 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8522 {
8523 if (TYPE_DOMAIN (constructor_type))
8524 {
8525 constructor_max_index
8526 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8527
8528 /* Detect non-empty initializations of zero-length arrays. */
8529 if (constructor_max_index == NULL_TREE
8530 && TYPE_SIZE (constructor_type))
8531 constructor_max_index = integer_minus_one_node;
8532
8533 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8534 to initialize VLAs will cause a proper error; avoid tree
8535 checking errors as well by setting a safe value. */
8536 if (constructor_max_index
8537 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8538 constructor_max_index = integer_minus_one_node;
8539
8540 constructor_index
8541 = convert (bitsizetype,
8542 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8543 }
8544 else
8545 constructor_index = bitsize_zero_node;
8546
8547 constructor_unfilled_index = constructor_index;
8548 if (value && TREE_CODE (value) == STRING_CST)
8549 {
8550 /* We need to split the char/wchar array into individual
8551 characters, so that we don't have to special case it
8552 everywhere. */
8553 set_nonincremental_init_from_string (value, braced_init_obstack);
8554 }
8555 }
8556 else
8557 {
8558 if (constructor_type != error_mark_node)
8559 warning_init (input_location, 0, "braces around scalar initializer");
8560 constructor_fields = constructor_type;
8561 constructor_unfilled_fields = constructor_type;
8562 }
8563 }
8564
8565 /* At the end of an implicit or explicit brace level,
8566 finish up that level of constructor. If a single expression
8567 with redundant braces initialized that level, return the
8568 c_expr structure for that expression. Otherwise, the original_code
8569 element is set to ERROR_MARK.
8570 If we were outputting the elements as they are read, return 0 as the value
8571 from inner levels (process_init_element ignores that),
8572 but return error_mark_node as the value from the outermost level
8573 (that's what we want to put in DECL_INITIAL).
8574 Otherwise, return a CONSTRUCTOR expression as the value. */
8575
8576 struct c_expr
8577 pop_init_level (location_t loc, int implicit,
8578 struct obstack *braced_init_obstack,
8579 location_t insert_before)
8580 {
8581 struct constructor_stack *p;
8582 struct c_expr ret;
8583 ret.value = NULL_TREE;
8584 ret.original_code = ERROR_MARK;
8585 ret.original_type = NULL;
8586
8587 if (implicit == 0)
8588 {
8589 /* When we come to an explicit close brace,
8590 pop any inner levels that didn't have explicit braces. */
8591 while (constructor_stack->implicit)
8592 process_init_element (input_location,
8593 pop_init_level (loc, 1, braced_init_obstack,
8594 insert_before),
8595 true, braced_init_obstack);
8596 gcc_assert (!constructor_range_stack);
8597 }
8598 else
8599 if (initializer_stack->missing_brace_richloc)
8600 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8601 (insert_before, "}");
8602
8603 /* Now output all pending elements. */
8604 constructor_incremental = 1;
8605 output_pending_init_elements (1, braced_init_obstack);
8606
8607 p = constructor_stack;
8608
8609 /* Error for initializing a flexible array member, or a zero-length
8610 array member in an inappropriate context. */
8611 if (constructor_type && constructor_fields
8612 && TREE_CODE (constructor_type) == ARRAY_TYPE
8613 && TYPE_DOMAIN (constructor_type)
8614 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8615 {
8616 /* Silently discard empty initializations. The parser will
8617 already have pedwarned for empty brackets. */
8618 if (integer_zerop (constructor_unfilled_index))
8619 constructor_type = NULL_TREE;
8620 else
8621 {
8622 gcc_assert (!TYPE_SIZE (constructor_type));
8623
8624 if (constructor_depth > 2)
8625 error_init (loc, "initialization of flexible array member in a nested context");
8626 else
8627 pedwarn_init (loc, OPT_Wpedantic,
8628 "initialization of a flexible array member");
8629
8630 /* We have already issued an error message for the existence
8631 of a flexible array member not at the end of the structure.
8632 Discard the initializer so that we do not die later. */
8633 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8634 constructor_type = NULL_TREE;
8635 }
8636 }
8637
8638 switch (vec_safe_length (constructor_elements))
8639 {
8640 case 0:
8641 /* Initialization with { } counts as zeroinit. */
8642 constructor_zeroinit = 1;
8643 break;
8644 case 1:
8645 /* This might be zeroinit as well. */
8646 if (integer_zerop ((*constructor_elements)[0].value))
8647 constructor_zeroinit = 1;
8648 break;
8649 default:
8650 /* If the constructor has more than one element, it can't be { 0 }. */
8651 constructor_zeroinit = 0;
8652 break;
8653 }
8654
8655 /* Warn when some structs are initialized with direct aggregation. */
8656 if (!implicit && found_missing_braces && warn_missing_braces
8657 && !constructor_zeroinit)
8658 {
8659 gcc_assert (initializer_stack->missing_brace_richloc);
8660 warning_at (initializer_stack->missing_brace_richloc,
8661 OPT_Wmissing_braces,
8662 "missing braces around initializer");
8663 }
8664
8665 /* Warn when some struct elements are implicitly initialized to zero. */
8666 if (warn_missing_field_initializers
8667 && constructor_type
8668 && TREE_CODE (constructor_type) == RECORD_TYPE
8669 && constructor_unfilled_fields)
8670 {
8671 /* Do not warn for flexible array members or zero-length arrays. */
8672 while (constructor_unfilled_fields
8673 && (!DECL_SIZE (constructor_unfilled_fields)
8674 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8675 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8676
8677 if (constructor_unfilled_fields
8678 /* Do not warn if this level of the initializer uses member
8679 designators; it is likely to be deliberate. */
8680 && !constructor_designated
8681 /* Do not warn about initializing with { 0 } or with { }. */
8682 && !constructor_zeroinit)
8683 {
8684 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8685 "missing initializer for field %qD of %qT",
8686 constructor_unfilled_fields,
8687 constructor_type))
8688 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8689 "%qD declared here", constructor_unfilled_fields);
8690 }
8691 }
8692
8693 /* Pad out the end of the structure. */
8694 if (p->replacement_value.value)
8695 /* If this closes a superfluous brace pair,
8696 just pass out the element between them. */
8697 ret = p->replacement_value;
8698 else if (constructor_type == NULL_TREE)
8699 ;
8700 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8701 && TREE_CODE (constructor_type) != ARRAY_TYPE
8702 && !VECTOR_TYPE_P (constructor_type))
8703 {
8704 /* A nonincremental scalar initializer--just return
8705 the element, after verifying there is just one. */
8706 if (vec_safe_is_empty (constructor_elements))
8707 {
8708 if (!constructor_erroneous)
8709 error_init (loc, "empty scalar initializer");
8710 ret.value = error_mark_node;
8711 }
8712 else if (vec_safe_length (constructor_elements) != 1)
8713 {
8714 error_init (loc, "extra elements in scalar initializer");
8715 ret.value = (*constructor_elements)[0].value;
8716 }
8717 else
8718 ret.value = (*constructor_elements)[0].value;
8719 }
8720 else
8721 {
8722 if (constructor_erroneous)
8723 ret.value = error_mark_node;
8724 else
8725 {
8726 ret.value = build_constructor (constructor_type,
8727 constructor_elements);
8728 if (constructor_constant)
8729 TREE_CONSTANT (ret.value) = 1;
8730 if (constructor_constant && constructor_simple)
8731 TREE_STATIC (ret.value) = 1;
8732 if (constructor_nonconst)
8733 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8734 }
8735 }
8736
8737 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8738 {
8739 if (constructor_nonconst)
8740 ret.original_code = C_MAYBE_CONST_EXPR;
8741 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8742 ret.original_code = ERROR_MARK;
8743 }
8744
8745 constructor_type = p->type;
8746 constructor_fields = p->fields;
8747 constructor_index = p->index;
8748 constructor_max_index = p->max_index;
8749 constructor_unfilled_index = p->unfilled_index;
8750 constructor_unfilled_fields = p->unfilled_fields;
8751 constructor_bit_index = p->bit_index;
8752 constructor_elements = p->elements;
8753 constructor_constant = p->constant;
8754 constructor_simple = p->simple;
8755 constructor_nonconst = p->nonconst;
8756 constructor_erroneous = p->erroneous;
8757 constructor_incremental = p->incremental;
8758 constructor_designated = p->designated;
8759 designator_depth = p->designator_depth;
8760 constructor_pending_elts = p->pending_elts;
8761 constructor_depth = p->depth;
8762 if (!p->implicit)
8763 constructor_range_stack = p->range_stack;
8764 RESTORE_SPELLING_DEPTH (constructor_depth);
8765
8766 constructor_stack = p->next;
8767 free (p);
8768
8769 if (ret.value == NULL_TREE && constructor_stack == 0)
8770 ret.value = error_mark_node;
8771 return ret;
8772 }
8773
8774 /* Common handling for both array range and field name designators.
8775 ARRAY argument is nonzero for array ranges. Returns false for success. */
8776
8777 static bool
8778 set_designator (location_t loc, bool array,
8779 struct obstack *braced_init_obstack)
8780 {
8781 tree subtype;
8782 enum tree_code subcode;
8783
8784 /* Don't die if an entire brace-pair level is superfluous
8785 in the containing level. */
8786 if (constructor_type == NULL_TREE)
8787 return true;
8788
8789 /* If there were errors in this designator list already, bail out
8790 silently. */
8791 if (designator_erroneous)
8792 return true;
8793
8794 if (!designator_depth)
8795 {
8796 gcc_assert (!constructor_range_stack);
8797
8798 /* Designator list starts at the level of closest explicit
8799 braces. */
8800 while (constructor_stack->implicit)
8801 process_init_element (input_location,
8802 pop_init_level (loc, 1, braced_init_obstack,
8803 last_init_list_comma),
8804 true, braced_init_obstack);
8805 constructor_designated = 1;
8806 return false;
8807 }
8808
8809 switch (TREE_CODE (constructor_type))
8810 {
8811 case RECORD_TYPE:
8812 case UNION_TYPE:
8813 subtype = TREE_TYPE (constructor_fields);
8814 if (subtype != error_mark_node)
8815 subtype = TYPE_MAIN_VARIANT (subtype);
8816 break;
8817 case ARRAY_TYPE:
8818 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8819 break;
8820 default:
8821 gcc_unreachable ();
8822 }
8823
8824 subcode = TREE_CODE (subtype);
8825 if (array && subcode != ARRAY_TYPE)
8826 {
8827 error_init (loc, "array index in non-array initializer");
8828 return true;
8829 }
8830 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8831 {
8832 error_init (loc, "field name not in record or union initializer");
8833 return true;
8834 }
8835
8836 constructor_designated = 1;
8837 finish_implicit_inits (loc, braced_init_obstack);
8838 push_init_level (loc, 2, braced_init_obstack);
8839 return false;
8840 }
8841
8842 /* If there are range designators in designator list, push a new designator
8843 to constructor_range_stack. RANGE_END is end of such stack range or
8844 NULL_TREE if there is no range designator at this level. */
8845
8846 static void
8847 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8848 {
8849 struct constructor_range_stack *p;
8850
8851 p = (struct constructor_range_stack *)
8852 obstack_alloc (braced_init_obstack,
8853 sizeof (struct constructor_range_stack));
8854 p->prev = constructor_range_stack;
8855 p->next = 0;
8856 p->fields = constructor_fields;
8857 p->range_start = constructor_index;
8858 p->index = constructor_index;
8859 p->stack = constructor_stack;
8860 p->range_end = range_end;
8861 if (constructor_range_stack)
8862 constructor_range_stack->next = p;
8863 constructor_range_stack = p;
8864 }
8865
8866 /* Within an array initializer, specify the next index to be initialized.
8867 FIRST is that index. If LAST is nonzero, then initialize a range
8868 of indices, running from FIRST through LAST. */
8869
8870 void
8871 set_init_index (location_t loc, tree first, tree last,
8872 struct obstack *braced_init_obstack)
8873 {
8874 if (set_designator (loc, true, braced_init_obstack))
8875 return;
8876
8877 designator_erroneous = 1;
8878
8879 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8880 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8881 {
8882 error_init (loc, "array index in initializer not of integer type");
8883 return;
8884 }
8885
8886 if (TREE_CODE (first) != INTEGER_CST)
8887 {
8888 first = c_fully_fold (first, false, NULL);
8889 if (TREE_CODE (first) == INTEGER_CST)
8890 pedwarn_init (loc, OPT_Wpedantic,
8891 "array index in initializer is not "
8892 "an integer constant expression");
8893 }
8894
8895 if (last && TREE_CODE (last) != INTEGER_CST)
8896 {
8897 last = c_fully_fold (last, false, NULL);
8898 if (TREE_CODE (last) == INTEGER_CST)
8899 pedwarn_init (loc, OPT_Wpedantic,
8900 "array index in initializer is not "
8901 "an integer constant expression");
8902 }
8903
8904 if (TREE_CODE (first) != INTEGER_CST)
8905 error_init (loc, "nonconstant array index in initializer");
8906 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8907 error_init (loc, "nonconstant array index in initializer");
8908 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8909 error_init (loc, "array index in non-array initializer");
8910 else if (tree_int_cst_sgn (first) == -1)
8911 error_init (loc, "array index in initializer exceeds array bounds");
8912 else if (constructor_max_index
8913 && tree_int_cst_lt (constructor_max_index, first))
8914 error_init (loc, "array index in initializer exceeds array bounds");
8915 else
8916 {
8917 constant_expression_warning (first);
8918 if (last)
8919 constant_expression_warning (last);
8920 constructor_index = convert (bitsizetype, first);
8921 if (tree_int_cst_lt (constructor_index, first))
8922 {
8923 constructor_index = copy_node (constructor_index);
8924 TREE_OVERFLOW (constructor_index) = 1;
8925 }
8926
8927 if (last)
8928 {
8929 if (tree_int_cst_equal (first, last))
8930 last = NULL_TREE;
8931 else if (tree_int_cst_lt (last, first))
8932 {
8933 error_init (loc, "empty index range in initializer");
8934 last = NULL_TREE;
8935 }
8936 else
8937 {
8938 last = convert (bitsizetype, last);
8939 if (constructor_max_index != NULL_TREE
8940 && tree_int_cst_lt (constructor_max_index, last))
8941 {
8942 error_init (loc, "array index range in initializer exceeds "
8943 "array bounds");
8944 last = NULL_TREE;
8945 }
8946 }
8947 }
8948
8949 designator_depth++;
8950 designator_erroneous = 0;
8951 if (constructor_range_stack || last)
8952 push_range_stack (last, braced_init_obstack);
8953 }
8954 }
8955
8956 /* Within a struct initializer, specify the next field to be initialized. */
8957
8958 void
8959 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8960 struct obstack *braced_init_obstack)
8961 {
8962 tree field;
8963
8964 if (set_designator (loc, false, braced_init_obstack))
8965 return;
8966
8967 designator_erroneous = 1;
8968
8969 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8970 {
8971 error_init (loc, "field name not in record or union initializer");
8972 return;
8973 }
8974
8975 field = lookup_field (constructor_type, fieldname);
8976
8977 if (field == NULL_TREE)
8978 {
8979 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8980 if (guessed_id)
8981 {
8982 gcc_rich_location rich_loc (fieldname_loc);
8983 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8984 error_at (&rich_loc,
8985 "%qT has no member named %qE; did you mean %qE?",
8986 constructor_type, fieldname, guessed_id);
8987 }
8988 else
8989 error_at (fieldname_loc, "%qT has no member named %qE",
8990 constructor_type, fieldname);
8991 }
8992 else
8993 do
8994 {
8995 constructor_fields = TREE_VALUE (field);
8996 designator_depth++;
8997 designator_erroneous = 0;
8998 if (constructor_range_stack)
8999 push_range_stack (NULL_TREE, braced_init_obstack);
9000 field = TREE_CHAIN (field);
9001 if (field)
9002 {
9003 if (set_designator (loc, false, braced_init_obstack))
9004 return;
9005 }
9006 }
9007 while (field != NULL_TREE);
9008 }
9009 \f
9010 /* Add a new initializer to the tree of pending initializers. PURPOSE
9011 identifies the initializer, either array index or field in a structure.
9012 VALUE is the value of that index or field. If ORIGTYPE is not
9013 NULL_TREE, it is the original type of VALUE.
9014
9015 IMPLICIT is true if value comes from pop_init_level (1),
9016 the new initializer has been merged with the existing one
9017 and thus no warnings should be emitted about overriding an
9018 existing initializer. */
9019
9020 static void
9021 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9022 bool implicit, struct obstack *braced_init_obstack)
9023 {
9024 struct init_node *p, **q, *r;
9025
9026 q = &constructor_pending_elts;
9027 p = 0;
9028
9029 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9030 {
9031 while (*q != 0)
9032 {
9033 p = *q;
9034 if (tree_int_cst_lt (purpose, p->purpose))
9035 q = &p->left;
9036 else if (tree_int_cst_lt (p->purpose, purpose))
9037 q = &p->right;
9038 else
9039 {
9040 if (!implicit)
9041 {
9042 if (TREE_SIDE_EFFECTS (p->value))
9043 warning_init (loc, OPT_Woverride_init_side_effects,
9044 "initialized field with side-effects "
9045 "overwritten");
9046 else if (warn_override_init)
9047 warning_init (loc, OPT_Woverride_init,
9048 "initialized field overwritten");
9049 }
9050 p->value = value;
9051 p->origtype = origtype;
9052 return;
9053 }
9054 }
9055 }
9056 else
9057 {
9058 tree bitpos;
9059
9060 bitpos = bit_position (purpose);
9061 while (*q != NULL)
9062 {
9063 p = *q;
9064 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9065 q = &p->left;
9066 else if (p->purpose != purpose)
9067 q = &p->right;
9068 else
9069 {
9070 if (!implicit)
9071 {
9072 if (TREE_SIDE_EFFECTS (p->value))
9073 warning_init (loc, OPT_Woverride_init_side_effects,
9074 "initialized field with side-effects "
9075 "overwritten");
9076 else if (warn_override_init)
9077 warning_init (loc, OPT_Woverride_init,
9078 "initialized field overwritten");
9079 }
9080 p->value = value;
9081 p->origtype = origtype;
9082 return;
9083 }
9084 }
9085 }
9086
9087 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9088 sizeof (struct init_node));
9089 r->purpose = purpose;
9090 r->value = value;
9091 r->origtype = origtype;
9092
9093 *q = r;
9094 r->parent = p;
9095 r->left = 0;
9096 r->right = 0;
9097 r->balance = 0;
9098
9099 while (p)
9100 {
9101 struct init_node *s;
9102
9103 if (r == p->left)
9104 {
9105 if (p->balance == 0)
9106 p->balance = -1;
9107 else if (p->balance < 0)
9108 {
9109 if (r->balance < 0)
9110 {
9111 /* L rotation. */
9112 p->left = r->right;
9113 if (p->left)
9114 p->left->parent = p;
9115 r->right = p;
9116
9117 p->balance = 0;
9118 r->balance = 0;
9119
9120 s = p->parent;
9121 p->parent = r;
9122 r->parent = s;
9123 if (s)
9124 {
9125 if (s->left == p)
9126 s->left = r;
9127 else
9128 s->right = r;
9129 }
9130 else
9131 constructor_pending_elts = r;
9132 }
9133 else
9134 {
9135 /* LR rotation. */
9136 struct init_node *t = r->right;
9137
9138 r->right = t->left;
9139 if (r->right)
9140 r->right->parent = r;
9141 t->left = r;
9142
9143 p->left = t->right;
9144 if (p->left)
9145 p->left->parent = p;
9146 t->right = p;
9147
9148 p->balance = t->balance < 0;
9149 r->balance = -(t->balance > 0);
9150 t->balance = 0;
9151
9152 s = p->parent;
9153 p->parent = t;
9154 r->parent = t;
9155 t->parent = s;
9156 if (s)
9157 {
9158 if (s->left == p)
9159 s->left = t;
9160 else
9161 s->right = t;
9162 }
9163 else
9164 constructor_pending_elts = t;
9165 }
9166 break;
9167 }
9168 else
9169 {
9170 /* p->balance == +1; growth of left side balances the node. */
9171 p->balance = 0;
9172 break;
9173 }
9174 }
9175 else /* r == p->right */
9176 {
9177 if (p->balance == 0)
9178 /* Growth propagation from right side. */
9179 p->balance++;
9180 else if (p->balance > 0)
9181 {
9182 if (r->balance > 0)
9183 {
9184 /* R rotation. */
9185 p->right = r->left;
9186 if (p->right)
9187 p->right->parent = p;
9188 r->left = p;
9189
9190 p->balance = 0;
9191 r->balance = 0;
9192
9193 s = p->parent;
9194 p->parent = r;
9195 r->parent = s;
9196 if (s)
9197 {
9198 if (s->left == p)
9199 s->left = r;
9200 else
9201 s->right = r;
9202 }
9203 else
9204 constructor_pending_elts = r;
9205 }
9206 else /* r->balance == -1 */
9207 {
9208 /* RL rotation */
9209 struct init_node *t = r->left;
9210
9211 r->left = t->right;
9212 if (r->left)
9213 r->left->parent = r;
9214 t->right = r;
9215
9216 p->right = t->left;
9217 if (p->right)
9218 p->right->parent = p;
9219 t->left = p;
9220
9221 r->balance = (t->balance < 0);
9222 p->balance = -(t->balance > 0);
9223 t->balance = 0;
9224
9225 s = p->parent;
9226 p->parent = t;
9227 r->parent = t;
9228 t->parent = s;
9229 if (s)
9230 {
9231 if (s->left == p)
9232 s->left = t;
9233 else
9234 s->right = t;
9235 }
9236 else
9237 constructor_pending_elts = t;
9238 }
9239 break;
9240 }
9241 else
9242 {
9243 /* p->balance == -1; growth of right side balances the node. */
9244 p->balance = 0;
9245 break;
9246 }
9247 }
9248
9249 r = p;
9250 p = p->parent;
9251 }
9252 }
9253
9254 /* Build AVL tree from a sorted chain. */
9255
9256 static void
9257 set_nonincremental_init (struct obstack * braced_init_obstack)
9258 {
9259 unsigned HOST_WIDE_INT ix;
9260 tree index, value;
9261
9262 if (TREE_CODE (constructor_type) != RECORD_TYPE
9263 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9264 return;
9265
9266 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9267 add_pending_init (input_location, index, value, NULL_TREE, true,
9268 braced_init_obstack);
9269 constructor_elements = NULL;
9270 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9271 {
9272 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9273 /* Skip any nameless bit fields at the beginning. */
9274 while (constructor_unfilled_fields != NULL_TREE
9275 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9276 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9277
9278 }
9279 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9280 {
9281 if (TYPE_DOMAIN (constructor_type))
9282 constructor_unfilled_index
9283 = convert (bitsizetype,
9284 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9285 else
9286 constructor_unfilled_index = bitsize_zero_node;
9287 }
9288 constructor_incremental = 0;
9289 }
9290
9291 /* Build AVL tree from a string constant. */
9292
9293 static void
9294 set_nonincremental_init_from_string (tree str,
9295 struct obstack * braced_init_obstack)
9296 {
9297 tree value, purpose, type;
9298 HOST_WIDE_INT val[2];
9299 const char *p, *end;
9300 int byte, wchar_bytes, charwidth, bitpos;
9301
9302 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9303
9304 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9305 charwidth = TYPE_PRECISION (char_type_node);
9306 gcc_assert ((size_t) wchar_bytes * charwidth
9307 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9308 type = TREE_TYPE (constructor_type);
9309 p = TREE_STRING_POINTER (str);
9310 end = p + TREE_STRING_LENGTH (str);
9311
9312 for (purpose = bitsize_zero_node;
9313 p < end
9314 && !(constructor_max_index
9315 && tree_int_cst_lt (constructor_max_index, purpose));
9316 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9317 {
9318 if (wchar_bytes == 1)
9319 {
9320 val[0] = (unsigned char) *p++;
9321 val[1] = 0;
9322 }
9323 else
9324 {
9325 val[1] = 0;
9326 val[0] = 0;
9327 for (byte = 0; byte < wchar_bytes; byte++)
9328 {
9329 if (BYTES_BIG_ENDIAN)
9330 bitpos = (wchar_bytes - byte - 1) * charwidth;
9331 else
9332 bitpos = byte * charwidth;
9333 val[bitpos / HOST_BITS_PER_WIDE_INT]
9334 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9335 << (bitpos % HOST_BITS_PER_WIDE_INT);
9336 }
9337 }
9338
9339 if (!TYPE_UNSIGNED (type))
9340 {
9341 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9342 if (bitpos < HOST_BITS_PER_WIDE_INT)
9343 {
9344 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9345 {
9346 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9347 val[1] = -1;
9348 }
9349 }
9350 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9351 {
9352 if (val[0] < 0)
9353 val[1] = -1;
9354 }
9355 else if (val[1] & (HOST_WIDE_INT_1
9356 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9357 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9358 }
9359
9360 value = wide_int_to_tree (type,
9361 wide_int::from_array (val, 2,
9362 HOST_BITS_PER_WIDE_INT * 2));
9363 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9364 braced_init_obstack);
9365 }
9366
9367 constructor_incremental = 0;
9368 }
9369
9370 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9371 not initialized yet. */
9372
9373 static tree
9374 find_init_member (tree field, struct obstack * braced_init_obstack)
9375 {
9376 struct init_node *p;
9377
9378 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9379 {
9380 if (constructor_incremental
9381 && tree_int_cst_lt (field, constructor_unfilled_index))
9382 set_nonincremental_init (braced_init_obstack);
9383
9384 p = constructor_pending_elts;
9385 while (p)
9386 {
9387 if (tree_int_cst_lt (field, p->purpose))
9388 p = p->left;
9389 else if (tree_int_cst_lt (p->purpose, field))
9390 p = p->right;
9391 else
9392 return p->value;
9393 }
9394 }
9395 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9396 {
9397 tree bitpos = bit_position (field);
9398
9399 if (constructor_incremental
9400 && (!constructor_unfilled_fields
9401 || tree_int_cst_lt (bitpos,
9402 bit_position (constructor_unfilled_fields))))
9403 set_nonincremental_init (braced_init_obstack);
9404
9405 p = constructor_pending_elts;
9406 while (p)
9407 {
9408 if (field == p->purpose)
9409 return p->value;
9410 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9411 p = p->left;
9412 else
9413 p = p->right;
9414 }
9415 }
9416 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9417 {
9418 if (!vec_safe_is_empty (constructor_elements)
9419 && (constructor_elements->last ().index == field))
9420 return constructor_elements->last ().value;
9421 }
9422 return NULL_TREE;
9423 }
9424
9425 /* "Output" the next constructor element.
9426 At top level, really output it to assembler code now.
9427 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9428 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9429 TYPE is the data type that the containing data type wants here.
9430 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9431 If VALUE is a string constant, STRICT_STRING is true if it is
9432 unparenthesized or we should not warn here for it being parenthesized.
9433 For other types of VALUE, STRICT_STRING is not used.
9434
9435 PENDING if true means output pending elements that belong
9436 right after this element. (PENDING is normally true;
9437 it is false while outputting pending elements, to avoid recursion.)
9438
9439 IMPLICIT is true if value comes from pop_init_level (1),
9440 the new initializer has been merged with the existing one
9441 and thus no warnings should be emitted about overriding an
9442 existing initializer. */
9443
9444 static void
9445 output_init_element (location_t loc, tree value, tree origtype,
9446 bool strict_string, tree type, tree field, bool pending,
9447 bool implicit, struct obstack * braced_init_obstack)
9448 {
9449 tree semantic_type = NULL_TREE;
9450 bool maybe_const = true;
9451 bool npc;
9452
9453 if (type == error_mark_node || value == error_mark_node)
9454 {
9455 constructor_erroneous = 1;
9456 return;
9457 }
9458 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9459 && (TREE_CODE (value) == STRING_CST
9460 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9461 && !(TREE_CODE (value) == STRING_CST
9462 && TREE_CODE (type) == ARRAY_TYPE
9463 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9464 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9465 TYPE_MAIN_VARIANT (type)))
9466 value = array_to_pointer_conversion (input_location, value);
9467
9468 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9469 && require_constant_value && pending)
9470 {
9471 /* As an extension, allow initializing objects with static storage
9472 duration with compound literals (which are then treated just as
9473 the brace enclosed list they contain). */
9474 if (flag_isoc99)
9475 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9476 "constant");
9477 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9478 value = DECL_INITIAL (decl);
9479 }
9480
9481 npc = null_pointer_constant_p (value);
9482 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9483 {
9484 semantic_type = TREE_TYPE (value);
9485 value = TREE_OPERAND (value, 0);
9486 }
9487 value = c_fully_fold (value, require_constant_value, &maybe_const);
9488
9489 if (value == error_mark_node)
9490 constructor_erroneous = 1;
9491 else if (!TREE_CONSTANT (value))
9492 constructor_constant = 0;
9493 else if (!initializer_constant_valid_p (value,
9494 TREE_TYPE (value),
9495 AGGREGATE_TYPE_P (constructor_type)
9496 && TYPE_REVERSE_STORAGE_ORDER
9497 (constructor_type))
9498 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9499 && DECL_C_BIT_FIELD (field)
9500 && TREE_CODE (value) != INTEGER_CST))
9501 constructor_simple = 0;
9502 if (!maybe_const)
9503 constructor_nonconst = 1;
9504
9505 /* Digest the initializer and issue any errors about incompatible
9506 types before issuing errors about non-constant initializers. */
9507 tree new_value = value;
9508 if (semantic_type)
9509 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9510 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9511 require_constant_value);
9512 if (new_value == error_mark_node)
9513 {
9514 constructor_erroneous = 1;
9515 return;
9516 }
9517 if (require_constant_value || require_constant_elements)
9518 constant_expression_warning (new_value);
9519
9520 /* Proceed to check the constness of the original initializer. */
9521 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9522 {
9523 if (require_constant_value)
9524 {
9525 error_init (loc, "initializer element is not constant");
9526 value = error_mark_node;
9527 }
9528 else if (require_constant_elements)
9529 pedwarn (loc, OPT_Wpedantic,
9530 "initializer element is not computable at load time");
9531 }
9532 else if (!maybe_const
9533 && (require_constant_value || require_constant_elements))
9534 pedwarn_init (loc, OPT_Wpedantic,
9535 "initializer element is not a constant expression");
9536
9537 /* Issue -Wc++-compat warnings about initializing a bitfield with
9538 enum type. */
9539 if (warn_cxx_compat
9540 && field != NULL_TREE
9541 && TREE_CODE (field) == FIELD_DECL
9542 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9543 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9544 != TYPE_MAIN_VARIANT (type))
9545 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9546 {
9547 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9548 if (checktype != error_mark_node
9549 && (TYPE_MAIN_VARIANT (checktype)
9550 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9551 warning_init (loc, OPT_Wc___compat,
9552 "enum conversion in initialization is invalid in C++");
9553 }
9554
9555 /* If this field is empty and does not have side effects (and is not at
9556 the end of structure), don't do anything other than checking the
9557 initializer. */
9558 if (field
9559 && (TREE_TYPE (field) == error_mark_node
9560 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9561 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9562 && !TREE_SIDE_EFFECTS (new_value)
9563 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9564 || DECL_CHAIN (field)))))
9565 return;
9566
9567 /* Finally, set VALUE to the initializer value digested above. */
9568 value = new_value;
9569
9570 /* If this element doesn't come next in sequence,
9571 put it on constructor_pending_elts. */
9572 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9573 && (!constructor_incremental
9574 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9575 {
9576 if (constructor_incremental
9577 && tree_int_cst_lt (field, constructor_unfilled_index))
9578 set_nonincremental_init (braced_init_obstack);
9579
9580 add_pending_init (loc, field, value, origtype, implicit,
9581 braced_init_obstack);
9582 return;
9583 }
9584 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9585 && (!constructor_incremental
9586 || field != constructor_unfilled_fields))
9587 {
9588 /* We do this for records but not for unions. In a union,
9589 no matter which field is specified, it can be initialized
9590 right away since it starts at the beginning of the union. */
9591 if (constructor_incremental)
9592 {
9593 if (!constructor_unfilled_fields)
9594 set_nonincremental_init (braced_init_obstack);
9595 else
9596 {
9597 tree bitpos, unfillpos;
9598
9599 bitpos = bit_position (field);
9600 unfillpos = bit_position (constructor_unfilled_fields);
9601
9602 if (tree_int_cst_lt (bitpos, unfillpos))
9603 set_nonincremental_init (braced_init_obstack);
9604 }
9605 }
9606
9607 add_pending_init (loc, field, value, origtype, implicit,
9608 braced_init_obstack);
9609 return;
9610 }
9611 else if (TREE_CODE (constructor_type) == UNION_TYPE
9612 && !vec_safe_is_empty (constructor_elements))
9613 {
9614 if (!implicit)
9615 {
9616 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9617 warning_init (loc, OPT_Woverride_init_side_effects,
9618 "initialized field with side-effects overwritten");
9619 else if (warn_override_init)
9620 warning_init (loc, OPT_Woverride_init,
9621 "initialized field overwritten");
9622 }
9623
9624 /* We can have just one union field set. */
9625 constructor_elements = NULL;
9626 }
9627
9628 /* Otherwise, output this element either to
9629 constructor_elements or to the assembler file. */
9630
9631 constructor_elt celt = {field, value};
9632 vec_safe_push (constructor_elements, celt);
9633
9634 /* Advance the variable that indicates sequential elements output. */
9635 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9636 constructor_unfilled_index
9637 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9638 bitsize_one_node);
9639 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9640 {
9641 constructor_unfilled_fields
9642 = DECL_CHAIN (constructor_unfilled_fields);
9643
9644 /* Skip any nameless bit fields. */
9645 while (constructor_unfilled_fields != NULL_TREE
9646 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9647 constructor_unfilled_fields =
9648 DECL_CHAIN (constructor_unfilled_fields);
9649 }
9650 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9651 constructor_unfilled_fields = NULL_TREE;
9652
9653 /* Now output any pending elements which have become next. */
9654 if (pending)
9655 output_pending_init_elements (0, braced_init_obstack);
9656 }
9657
9658 /* For two FIELD_DECLs in the same chain, return -1 if field1
9659 comes before field2, 1 if field1 comes after field2 and
9660 0 if field1 == field2. */
9661
9662 static int
9663 init_field_decl_cmp (tree field1, tree field2)
9664 {
9665 if (field1 == field2)
9666 return 0;
9667
9668 tree bitpos1 = bit_position (field1);
9669 tree bitpos2 = bit_position (field2);
9670 if (tree_int_cst_equal (bitpos1, bitpos2))
9671 {
9672 /* If one of the fields has non-zero bitsize, then that
9673 field must be the last one in a sequence of zero
9674 sized fields, fields after it will have bigger
9675 bit_position. */
9676 if (TREE_TYPE (field1) != error_mark_node
9677 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9678 && integer_nonzerop (TREE_TYPE (field1)))
9679 return 1;
9680 if (TREE_TYPE (field2) != error_mark_node
9681 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9682 && integer_nonzerop (TREE_TYPE (field2)))
9683 return -1;
9684 /* Otherwise, fallback to DECL_CHAIN walk to find out
9685 which field comes earlier. Walk chains of both
9686 fields, so that if field1 and field2 are close to each
9687 other in either order, it is found soon even for large
9688 sequences of zero sized fields. */
9689 tree f1 = field1, f2 = field2;
9690 while (1)
9691 {
9692 f1 = DECL_CHAIN (f1);
9693 f2 = DECL_CHAIN (f2);
9694 if (f1 == NULL_TREE)
9695 {
9696 gcc_assert (f2);
9697 return 1;
9698 }
9699 if (f2 == NULL_TREE)
9700 return -1;
9701 if (f1 == field2)
9702 return -1;
9703 if (f2 == field1)
9704 return 1;
9705 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9706 return 1;
9707 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9708 return -1;
9709 }
9710 }
9711 else if (tree_int_cst_lt (bitpos1, bitpos2))
9712 return -1;
9713 else
9714 return 1;
9715 }
9716
9717 /* Output any pending elements which have become next.
9718 As we output elements, constructor_unfilled_{fields,index}
9719 advances, which may cause other elements to become next;
9720 if so, they too are output.
9721
9722 If ALL is 0, we return when there are
9723 no more pending elements to output now.
9724
9725 If ALL is 1, we output space as necessary so that
9726 we can output all the pending elements. */
9727 static void
9728 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9729 {
9730 struct init_node *elt = constructor_pending_elts;
9731 tree next;
9732
9733 retry:
9734
9735 /* Look through the whole pending tree.
9736 If we find an element that should be output now,
9737 output it. Otherwise, set NEXT to the element
9738 that comes first among those still pending. */
9739
9740 next = NULL_TREE;
9741 while (elt)
9742 {
9743 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9744 {
9745 if (tree_int_cst_equal (elt->purpose,
9746 constructor_unfilled_index))
9747 output_init_element (input_location, elt->value, elt->origtype,
9748 true, TREE_TYPE (constructor_type),
9749 constructor_unfilled_index, false, false,
9750 braced_init_obstack);
9751 else if (tree_int_cst_lt (constructor_unfilled_index,
9752 elt->purpose))
9753 {
9754 /* Advance to the next smaller node. */
9755 if (elt->left)
9756 elt = elt->left;
9757 else
9758 {
9759 /* We have reached the smallest node bigger than the
9760 current unfilled index. Fill the space first. */
9761 next = elt->purpose;
9762 break;
9763 }
9764 }
9765 else
9766 {
9767 /* Advance to the next bigger node. */
9768 if (elt->right)
9769 elt = elt->right;
9770 else
9771 {
9772 /* We have reached the biggest node in a subtree. Find
9773 the parent of it, which is the next bigger node. */
9774 while (elt->parent && elt->parent->right == elt)
9775 elt = elt->parent;
9776 elt = elt->parent;
9777 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9778 elt->purpose))
9779 {
9780 next = elt->purpose;
9781 break;
9782 }
9783 }
9784 }
9785 }
9786 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9787 {
9788 /* If the current record is complete we are done. */
9789 if (constructor_unfilled_fields == NULL_TREE)
9790 break;
9791
9792 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9793 elt->purpose);
9794 if (cmp == 0)
9795 output_init_element (input_location, elt->value, elt->origtype,
9796 true, TREE_TYPE (elt->purpose),
9797 elt->purpose, false, false,
9798 braced_init_obstack);
9799 else if (cmp < 0)
9800 {
9801 /* Advance to the next smaller node. */
9802 if (elt->left)
9803 elt = elt->left;
9804 else
9805 {
9806 /* We have reached the smallest node bigger than the
9807 current unfilled field. Fill the space first. */
9808 next = elt->purpose;
9809 break;
9810 }
9811 }
9812 else
9813 {
9814 /* Advance to the next bigger node. */
9815 if (elt->right)
9816 elt = elt->right;
9817 else
9818 {
9819 /* We have reached the biggest node in a subtree. Find
9820 the parent of it, which is the next bigger node. */
9821 while (elt->parent && elt->parent->right == elt)
9822 elt = elt->parent;
9823 elt = elt->parent;
9824 if (elt
9825 && init_field_decl_cmp (constructor_unfilled_fields,
9826 elt->purpose) < 0)
9827 {
9828 next = elt->purpose;
9829 break;
9830 }
9831 }
9832 }
9833 }
9834 }
9835
9836 /* Ordinarily return, but not if we want to output all
9837 and there are elements left. */
9838 if (!(all && next != NULL_TREE))
9839 return;
9840
9841 /* If it's not incremental, just skip over the gap, so that after
9842 jumping to retry we will output the next successive element. */
9843 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9844 constructor_unfilled_fields = next;
9845 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9846 constructor_unfilled_index = next;
9847
9848 /* ELT now points to the node in the pending tree with the next
9849 initializer to output. */
9850 goto retry;
9851 }
9852 \f
9853 /* Add one non-braced element to the current constructor level.
9854 This adjusts the current position within the constructor's type.
9855 This may also start or terminate implicit levels
9856 to handle a partly-braced initializer.
9857
9858 Once this has found the correct level for the new element,
9859 it calls output_init_element.
9860
9861 IMPLICIT is true if value comes from pop_init_level (1),
9862 the new initializer has been merged with the existing one
9863 and thus no warnings should be emitted about overriding an
9864 existing initializer. */
9865
9866 void
9867 process_init_element (location_t loc, struct c_expr value, bool implicit,
9868 struct obstack * braced_init_obstack)
9869 {
9870 tree orig_value = value.value;
9871 int string_flag
9872 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9873 bool strict_string = value.original_code == STRING_CST;
9874 bool was_designated = designator_depth != 0;
9875
9876 designator_depth = 0;
9877 designator_erroneous = 0;
9878
9879 if (!implicit && value.value && !integer_zerop (value.value))
9880 constructor_zeroinit = 0;
9881
9882 /* Handle superfluous braces around string cst as in
9883 char x[] = {"foo"}; */
9884 if (string_flag
9885 && constructor_type
9886 && !was_designated
9887 && TREE_CODE (constructor_type) == ARRAY_TYPE
9888 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9889 && integer_zerop (constructor_unfilled_index))
9890 {
9891 if (constructor_stack->replacement_value.value)
9892 error_init (loc, "excess elements in %<char%> array initializer");
9893 constructor_stack->replacement_value = value;
9894 return;
9895 }
9896
9897 if (constructor_stack->replacement_value.value != NULL_TREE)
9898 {
9899 error_init (loc, "excess elements in struct initializer");
9900 return;
9901 }
9902
9903 /* Ignore elements of a brace group if it is entirely superfluous
9904 and has already been diagnosed. */
9905 if (constructor_type == NULL_TREE)
9906 return;
9907
9908 if (!implicit && warn_designated_init && !was_designated
9909 && TREE_CODE (constructor_type) == RECORD_TYPE
9910 && lookup_attribute ("designated_init",
9911 TYPE_ATTRIBUTES (constructor_type)))
9912 warning_init (loc,
9913 OPT_Wdesignated_init,
9914 "positional initialization of field "
9915 "in %<struct%> declared with %<designated_init%> attribute");
9916
9917 /* If we've exhausted any levels that didn't have braces,
9918 pop them now. */
9919 while (constructor_stack->implicit)
9920 {
9921 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9922 && constructor_fields == NULL_TREE)
9923 process_init_element (loc,
9924 pop_init_level (loc, 1, braced_init_obstack,
9925 last_init_list_comma),
9926 true, braced_init_obstack);
9927 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9928 || VECTOR_TYPE_P (constructor_type))
9929 && constructor_max_index
9930 && tree_int_cst_lt (constructor_max_index,
9931 constructor_index))
9932 process_init_element (loc,
9933 pop_init_level (loc, 1, braced_init_obstack,
9934 last_init_list_comma),
9935 true, braced_init_obstack);
9936 else
9937 break;
9938 }
9939
9940 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9941 if (constructor_range_stack)
9942 {
9943 /* If value is a compound literal and we'll be just using its
9944 content, don't put it into a SAVE_EXPR. */
9945 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9946 || !require_constant_value)
9947 {
9948 tree semantic_type = NULL_TREE;
9949 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9950 {
9951 semantic_type = TREE_TYPE (value.value);
9952 value.value = TREE_OPERAND (value.value, 0);
9953 }
9954 value.value = save_expr (value.value);
9955 if (semantic_type)
9956 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9957 value.value);
9958 }
9959 }
9960
9961 while (1)
9962 {
9963 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9964 {
9965 tree fieldtype;
9966 enum tree_code fieldcode;
9967
9968 if (constructor_fields == NULL_TREE)
9969 {
9970 pedwarn_init (loc, 0, "excess elements in struct initializer");
9971 break;
9972 }
9973
9974 fieldtype = TREE_TYPE (constructor_fields);
9975 if (fieldtype != error_mark_node)
9976 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9977 fieldcode = TREE_CODE (fieldtype);
9978
9979 /* Error for non-static initialization of a flexible array member. */
9980 if (fieldcode == ARRAY_TYPE
9981 && !require_constant_value
9982 && TYPE_SIZE (fieldtype) == NULL_TREE
9983 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9984 {
9985 error_init (loc, "non-static initialization of a flexible "
9986 "array member");
9987 break;
9988 }
9989
9990 /* Error for initialization of a flexible array member with
9991 a string constant if the structure is in an array. E.g.:
9992 struct S { int x; char y[]; };
9993 struct S s[] = { { 1, "foo" } };
9994 is invalid. */
9995 if (string_flag
9996 && fieldcode == ARRAY_TYPE
9997 && constructor_depth > 1
9998 && TYPE_SIZE (fieldtype) == NULL_TREE
9999 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10000 {
10001 bool in_array_p = false;
10002 for (struct constructor_stack *p = constructor_stack;
10003 p && p->type; p = p->next)
10004 if (TREE_CODE (p->type) == ARRAY_TYPE)
10005 {
10006 in_array_p = true;
10007 break;
10008 }
10009 if (in_array_p)
10010 {
10011 error_init (loc, "initialization of flexible array "
10012 "member in a nested context");
10013 break;
10014 }
10015 }
10016
10017 /* Accept a string constant to initialize a subarray. */
10018 if (value.value != NULL_TREE
10019 && fieldcode == ARRAY_TYPE
10020 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10021 && string_flag)
10022 value.value = orig_value;
10023 /* Otherwise, if we have come to a subaggregate,
10024 and we don't have an element of its type, push into it. */
10025 else if (value.value != NULL_TREE
10026 && value.value != error_mark_node
10027 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
10028 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
10029 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
10030 {
10031 push_init_level (loc, 1, braced_init_obstack);
10032 continue;
10033 }
10034
10035 if (value.value)
10036 {
10037 push_member_name (constructor_fields);
10038 output_init_element (loc, value.value, value.original_type,
10039 strict_string, fieldtype,
10040 constructor_fields, true, implicit,
10041 braced_init_obstack);
10042 RESTORE_SPELLING_DEPTH (constructor_depth);
10043 }
10044 else
10045 /* Do the bookkeeping for an element that was
10046 directly output as a constructor. */
10047 {
10048 /* For a record, keep track of end position of last field. */
10049 if (DECL_SIZE (constructor_fields))
10050 constructor_bit_index
10051 = size_binop_loc (input_location, PLUS_EXPR,
10052 bit_position (constructor_fields),
10053 DECL_SIZE (constructor_fields));
10054
10055 /* If the current field was the first one not yet written out,
10056 it isn't now, so update. */
10057 if (constructor_unfilled_fields == constructor_fields)
10058 {
10059 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10060 /* Skip any nameless bit fields. */
10061 while (constructor_unfilled_fields != 0
10062 && (DECL_UNNAMED_BIT_FIELD
10063 (constructor_unfilled_fields)))
10064 constructor_unfilled_fields =
10065 DECL_CHAIN (constructor_unfilled_fields);
10066 }
10067 }
10068
10069 constructor_fields = DECL_CHAIN (constructor_fields);
10070 /* Skip any nameless bit fields at the beginning. */
10071 while (constructor_fields != NULL_TREE
10072 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10073 constructor_fields = DECL_CHAIN (constructor_fields);
10074 }
10075 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10076 {
10077 tree fieldtype;
10078 enum tree_code fieldcode;
10079
10080 if (constructor_fields == NULL_TREE)
10081 {
10082 pedwarn_init (loc, 0,
10083 "excess elements in union initializer");
10084 break;
10085 }
10086
10087 fieldtype = TREE_TYPE (constructor_fields);
10088 if (fieldtype != error_mark_node)
10089 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10090 fieldcode = TREE_CODE (fieldtype);
10091
10092 /* Warn that traditional C rejects initialization of unions.
10093 We skip the warning if the value is zero. This is done
10094 under the assumption that the zero initializer in user
10095 code appears conditioned on e.g. __STDC__ to avoid
10096 "missing initializer" warnings and relies on default
10097 initialization to zero in the traditional C case.
10098 We also skip the warning if the initializer is designated,
10099 again on the assumption that this must be conditional on
10100 __STDC__ anyway (and we've already complained about the
10101 member-designator already). */
10102 if (!in_system_header_at (input_location) && !constructor_designated
10103 && !(value.value && (integer_zerop (value.value)
10104 || real_zerop (value.value))))
10105 warning (OPT_Wtraditional, "traditional C rejects initialization "
10106 "of unions");
10107
10108 /* Accept a string constant to initialize a subarray. */
10109 if (value.value != NULL_TREE
10110 && fieldcode == ARRAY_TYPE
10111 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10112 && string_flag)
10113 value.value = orig_value;
10114 /* Otherwise, if we have come to a subaggregate,
10115 and we don't have an element of its type, push into it. */
10116 else if (value.value != NULL_TREE
10117 && value.value != error_mark_node
10118 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
10119 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
10120 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
10121 {
10122 push_init_level (loc, 1, braced_init_obstack);
10123 continue;
10124 }
10125
10126 if (value.value)
10127 {
10128 push_member_name (constructor_fields);
10129 output_init_element (loc, value.value, value.original_type,
10130 strict_string, fieldtype,
10131 constructor_fields, true, implicit,
10132 braced_init_obstack);
10133 RESTORE_SPELLING_DEPTH (constructor_depth);
10134 }
10135 else
10136 /* Do the bookkeeping for an element that was
10137 directly output as a constructor. */
10138 {
10139 constructor_bit_index = DECL_SIZE (constructor_fields);
10140 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10141 }
10142
10143 constructor_fields = NULL_TREE;
10144 }
10145 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10146 {
10147 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10148 enum tree_code eltcode = TREE_CODE (elttype);
10149
10150 /* Accept a string constant to initialize a subarray. */
10151 if (value.value != NULL_TREE
10152 && eltcode == ARRAY_TYPE
10153 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10154 && string_flag)
10155 value.value = orig_value;
10156 /* Otherwise, if we have come to a subaggregate,
10157 and we don't have an element of its type, push into it. */
10158 else if (value.value != NULL_TREE
10159 && value.value != error_mark_node
10160 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
10161 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
10162 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
10163 {
10164 push_init_level (loc, 1, braced_init_obstack);
10165 continue;
10166 }
10167
10168 if (constructor_max_index != NULL_TREE
10169 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10170 || integer_all_onesp (constructor_max_index)))
10171 {
10172 pedwarn_init (loc, 0,
10173 "excess elements in array initializer");
10174 break;
10175 }
10176
10177 /* Now output the actual element. */
10178 if (value.value)
10179 {
10180 push_array_bounds (tree_to_uhwi (constructor_index));
10181 output_init_element (loc, value.value, value.original_type,
10182 strict_string, elttype,
10183 constructor_index, true, implicit,
10184 braced_init_obstack);
10185 RESTORE_SPELLING_DEPTH (constructor_depth);
10186 }
10187
10188 constructor_index
10189 = size_binop_loc (input_location, PLUS_EXPR,
10190 constructor_index, bitsize_one_node);
10191
10192 if (!value.value)
10193 /* If we are doing the bookkeeping for an element that was
10194 directly output as a constructor, we must update
10195 constructor_unfilled_index. */
10196 constructor_unfilled_index = constructor_index;
10197 }
10198 else if (VECTOR_TYPE_P (constructor_type))
10199 {
10200 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10201
10202 /* Do a basic check of initializer size. Note that vectors
10203 always have a fixed size derived from their type. */
10204 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10205 {
10206 pedwarn_init (loc, 0,
10207 "excess elements in vector initializer");
10208 break;
10209 }
10210
10211 /* Now output the actual element. */
10212 if (value.value)
10213 {
10214 if (TREE_CODE (value.value) == VECTOR_CST)
10215 elttype = TYPE_MAIN_VARIANT (constructor_type);
10216 output_init_element (loc, value.value, value.original_type,
10217 strict_string, elttype,
10218 constructor_index, true, implicit,
10219 braced_init_obstack);
10220 }
10221
10222 constructor_index
10223 = size_binop_loc (input_location,
10224 PLUS_EXPR, constructor_index, bitsize_one_node);
10225
10226 if (!value.value)
10227 /* If we are doing the bookkeeping for an element that was
10228 directly output as a constructor, we must update
10229 constructor_unfilled_index. */
10230 constructor_unfilled_index = constructor_index;
10231 }
10232
10233 /* Handle the sole element allowed in a braced initializer
10234 for a scalar variable. */
10235 else if (constructor_type != error_mark_node
10236 && constructor_fields == NULL_TREE)
10237 {
10238 pedwarn_init (loc, 0,
10239 "excess elements in scalar initializer");
10240 break;
10241 }
10242 else
10243 {
10244 if (value.value)
10245 output_init_element (loc, value.value, value.original_type,
10246 strict_string, constructor_type,
10247 NULL_TREE, true, implicit,
10248 braced_init_obstack);
10249 constructor_fields = NULL_TREE;
10250 }
10251
10252 /* Handle range initializers either at this level or anywhere higher
10253 in the designator stack. */
10254 if (constructor_range_stack)
10255 {
10256 struct constructor_range_stack *p, *range_stack;
10257 int finish = 0;
10258
10259 range_stack = constructor_range_stack;
10260 constructor_range_stack = 0;
10261 while (constructor_stack != range_stack->stack)
10262 {
10263 gcc_assert (constructor_stack->implicit);
10264 process_init_element (loc,
10265 pop_init_level (loc, 1,
10266 braced_init_obstack,
10267 last_init_list_comma),
10268 true, braced_init_obstack);
10269 }
10270 for (p = range_stack;
10271 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10272 p = p->prev)
10273 {
10274 gcc_assert (constructor_stack->implicit);
10275 process_init_element (loc,
10276 pop_init_level (loc, 1,
10277 braced_init_obstack,
10278 last_init_list_comma),
10279 true, braced_init_obstack);
10280 }
10281
10282 p->index = size_binop_loc (input_location,
10283 PLUS_EXPR, p->index, bitsize_one_node);
10284 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10285 finish = 1;
10286
10287 while (1)
10288 {
10289 constructor_index = p->index;
10290 constructor_fields = p->fields;
10291 if (finish && p->range_end && p->index == p->range_start)
10292 {
10293 finish = 0;
10294 p->prev = 0;
10295 }
10296 p = p->next;
10297 if (!p)
10298 break;
10299 finish_implicit_inits (loc, braced_init_obstack);
10300 push_init_level (loc, 2, braced_init_obstack);
10301 p->stack = constructor_stack;
10302 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10303 p->index = p->range_start;
10304 }
10305
10306 if (!finish)
10307 constructor_range_stack = range_stack;
10308 continue;
10309 }
10310
10311 break;
10312 }
10313
10314 constructor_range_stack = 0;
10315 }
10316 \f
10317 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10318 (guaranteed to be 'volatile' or null) and ARGS (represented using
10319 an ASM_EXPR node). */
10320 tree
10321 build_asm_stmt (bool is_volatile, tree args)
10322 {
10323 if (is_volatile)
10324 ASM_VOLATILE_P (args) = 1;
10325 return add_stmt (args);
10326 }
10327
10328 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10329 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10330 SIMPLE indicates whether there was anything at all after the
10331 string in the asm expression -- asm("blah") and asm("blah" : )
10332 are subtly different. We use a ASM_EXPR node to represent this.
10333 LOC is the location of the asm, and IS_INLINE says whether this
10334 is asm inline. */
10335 tree
10336 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10337 tree clobbers, tree labels, bool simple, bool is_inline)
10338 {
10339 tree tail;
10340 tree args;
10341 int i;
10342 const char *constraint;
10343 const char **oconstraints;
10344 bool allows_mem, allows_reg, is_inout;
10345 int ninputs, noutputs;
10346
10347 ninputs = list_length (inputs);
10348 noutputs = list_length (outputs);
10349 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10350
10351 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10352
10353 /* Remove output conversions that change the type but not the mode. */
10354 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10355 {
10356 tree output = TREE_VALUE (tail);
10357
10358 output = c_fully_fold (output, false, NULL, true);
10359
10360 /* ??? Really, this should not be here. Users should be using a
10361 proper lvalue, dammit. But there's a long history of using casts
10362 in the output operands. In cases like longlong.h, this becomes a
10363 primitive form of typechecking -- if the cast can be removed, then
10364 the output operand had a type of the proper width; otherwise we'll
10365 get an error. Gross, but ... */
10366 STRIP_NOPS (output);
10367
10368 if (!lvalue_or_else (loc, output, lv_asm))
10369 output = error_mark_node;
10370
10371 if (output != error_mark_node
10372 && (TREE_READONLY (output)
10373 || TYPE_READONLY (TREE_TYPE (output))
10374 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10375 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10376 readonly_error (loc, output, lv_asm);
10377
10378 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10379 oconstraints[i] = constraint;
10380
10381 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10382 &allows_mem, &allows_reg, &is_inout))
10383 {
10384 /* If the operand is going to end up in memory,
10385 mark it addressable. */
10386 if (!allows_reg && !c_mark_addressable (output))
10387 output = error_mark_node;
10388 if (!(!allows_reg && allows_mem)
10389 && output != error_mark_node
10390 && VOID_TYPE_P (TREE_TYPE (output)))
10391 {
10392 error_at (loc, "invalid use of void expression");
10393 output = error_mark_node;
10394 }
10395 }
10396 else
10397 output = error_mark_node;
10398
10399 TREE_VALUE (tail) = output;
10400 }
10401
10402 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10403 {
10404 tree input;
10405
10406 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10407 input = TREE_VALUE (tail);
10408
10409 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10410 oconstraints, &allows_mem, &allows_reg))
10411 {
10412 /* If the operand is going to end up in memory,
10413 mark it addressable. */
10414 if (!allows_reg && allows_mem)
10415 {
10416 input = c_fully_fold (input, false, NULL, true);
10417
10418 /* Strip the nops as we allow this case. FIXME, this really
10419 should be rejected or made deprecated. */
10420 STRIP_NOPS (input);
10421 if (!c_mark_addressable (input))
10422 input = error_mark_node;
10423 }
10424 else
10425 {
10426 struct c_expr expr;
10427 memset (&expr, 0, sizeof (expr));
10428 expr.value = input;
10429 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10430 input = c_fully_fold (expr.value, false, NULL);
10431
10432 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10433 {
10434 error_at (loc, "invalid use of void expression");
10435 input = error_mark_node;
10436 }
10437 }
10438 }
10439 else
10440 input = error_mark_node;
10441
10442 TREE_VALUE (tail) = input;
10443 }
10444
10445 /* ASMs with labels cannot have outputs. This should have been
10446 enforced by the parser. */
10447 gcc_assert (outputs == NULL || labels == NULL);
10448
10449 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10450
10451 /* asm statements without outputs, including simple ones, are treated
10452 as volatile. */
10453 ASM_INPUT_P (args) = simple;
10454 ASM_VOLATILE_P (args) = (noutputs == 0);
10455 ASM_INLINE_P (args) = is_inline;
10456
10457 return args;
10458 }
10459 \f
10460 /* Generate a goto statement to LABEL. LOC is the location of the
10461 GOTO. */
10462
10463 tree
10464 c_finish_goto_label (location_t loc, tree label)
10465 {
10466 tree decl = lookup_label_for_goto (loc, label);
10467 if (!decl)
10468 return NULL_TREE;
10469 TREE_USED (decl) = 1;
10470 {
10471 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10472 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10473 SET_EXPR_LOCATION (t, loc);
10474 return add_stmt (t);
10475 }
10476 }
10477
10478 /* Generate a computed goto statement to EXPR. LOC is the location of
10479 the GOTO. */
10480
10481 tree
10482 c_finish_goto_ptr (location_t loc, tree expr)
10483 {
10484 tree t;
10485 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10486 expr = c_fully_fold (expr, false, NULL);
10487 expr = convert (ptr_type_node, expr);
10488 t = build1 (GOTO_EXPR, void_type_node, expr);
10489 SET_EXPR_LOCATION (t, loc);
10490 return add_stmt (t);
10491 }
10492
10493 /* Generate a C `return' statement. RETVAL is the expression for what
10494 to return, or a null pointer for `return;' with no value. LOC is
10495 the location of the return statement, or the location of the expression,
10496 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10497 is the original type of RETVAL. */
10498
10499 tree
10500 c_finish_return (location_t loc, tree retval, tree origtype)
10501 {
10502 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10503 bool no_warning = false;
10504 bool npc = false;
10505
10506 /* Use the expansion point to handle cases such as returning NULL
10507 in a function returning void. */
10508 location_t xloc = expansion_point_location_if_in_system_header (loc);
10509
10510 if (TREE_THIS_VOLATILE (current_function_decl))
10511 warning_at (xloc, 0,
10512 "function declared %<noreturn%> has a %<return%> statement");
10513
10514 if (retval)
10515 {
10516 tree semantic_type = NULL_TREE;
10517 npc = null_pointer_constant_p (retval);
10518 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10519 {
10520 semantic_type = TREE_TYPE (retval);
10521 retval = TREE_OPERAND (retval, 0);
10522 }
10523 retval = c_fully_fold (retval, false, NULL);
10524 if (semantic_type)
10525 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10526 }
10527
10528 if (!retval)
10529 {
10530 current_function_returns_null = 1;
10531 if ((warn_return_type >= 0 || flag_isoc99)
10532 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10533 {
10534 bool warned_here;
10535 if (flag_isoc99)
10536 warned_here = pedwarn
10537 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10538 "%<return%> with no value, in function returning non-void");
10539 else
10540 warned_here = warning_at
10541 (loc, OPT_Wreturn_type,
10542 "%<return%> with no value, in function returning non-void");
10543 no_warning = true;
10544 if (warned_here)
10545 inform (DECL_SOURCE_LOCATION (current_function_decl),
10546 "declared here");
10547 }
10548 }
10549 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10550 {
10551 current_function_returns_null = 1;
10552 bool warned_here;
10553 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10554 warned_here = pedwarn
10555 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10556 "%<return%> with a value, in function returning void");
10557 else
10558 warned_here = pedwarn
10559 (xloc, OPT_Wpedantic, "ISO C forbids "
10560 "%<return%> with expression, in function returning void");
10561 if (warned_here)
10562 inform (DECL_SOURCE_LOCATION (current_function_decl),
10563 "declared here");
10564 }
10565 else
10566 {
10567 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10568 retval, origtype, ic_return,
10569 npc, NULL_TREE, NULL_TREE, 0);
10570 tree res = DECL_RESULT (current_function_decl);
10571 tree inner;
10572 bool save;
10573
10574 current_function_returns_value = 1;
10575 if (t == error_mark_node)
10576 return NULL_TREE;
10577
10578 save = in_late_binary_op;
10579 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10580 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10581 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10582 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10583 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10584 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10585 in_late_binary_op = true;
10586 inner = t = convert (TREE_TYPE (res), t);
10587 in_late_binary_op = save;
10588
10589 /* Strip any conversions, additions, and subtractions, and see if
10590 we are returning the address of a local variable. Warn if so. */
10591 while (1)
10592 {
10593 switch (TREE_CODE (inner))
10594 {
10595 CASE_CONVERT:
10596 case NON_LVALUE_EXPR:
10597 case PLUS_EXPR:
10598 case POINTER_PLUS_EXPR:
10599 inner = TREE_OPERAND (inner, 0);
10600 continue;
10601
10602 case MINUS_EXPR:
10603 /* If the second operand of the MINUS_EXPR has a pointer
10604 type (or is converted from it), this may be valid, so
10605 don't give a warning. */
10606 {
10607 tree op1 = TREE_OPERAND (inner, 1);
10608
10609 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10610 && (CONVERT_EXPR_P (op1)
10611 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10612 op1 = TREE_OPERAND (op1, 0);
10613
10614 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10615 break;
10616
10617 inner = TREE_OPERAND (inner, 0);
10618 continue;
10619 }
10620
10621 case ADDR_EXPR:
10622 inner = TREE_OPERAND (inner, 0);
10623
10624 while (REFERENCE_CLASS_P (inner)
10625 && !INDIRECT_REF_P (inner))
10626 inner = TREE_OPERAND (inner, 0);
10627
10628 if (DECL_P (inner)
10629 && !DECL_EXTERNAL (inner)
10630 && !TREE_STATIC (inner)
10631 && DECL_CONTEXT (inner) == current_function_decl
10632 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
10633 {
10634 if (TREE_CODE (inner) == LABEL_DECL)
10635 warning_at (loc, OPT_Wreturn_local_addr,
10636 "function returns address of label");
10637 else
10638 {
10639 warning_at (loc, OPT_Wreturn_local_addr,
10640 "function returns address of local variable");
10641 tree zero = build_zero_cst (TREE_TYPE (res));
10642 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10643 }
10644 }
10645 break;
10646
10647 default:
10648 break;
10649 }
10650
10651 break;
10652 }
10653
10654 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10655 SET_EXPR_LOCATION (retval, loc);
10656
10657 if (warn_sequence_point)
10658 verify_sequence_points (retval);
10659 }
10660
10661 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10662 TREE_NO_WARNING (ret_stmt) |= no_warning;
10663 return add_stmt (ret_stmt);
10664 }
10665 \f
10666 struct c_switch {
10667 /* The SWITCH_EXPR being built. */
10668 tree switch_expr;
10669
10670 /* The original type of the testing expression, i.e. before the
10671 default conversion is applied. */
10672 tree orig_type;
10673
10674 /* A splay-tree mapping the low element of a case range to the high
10675 element, or NULL_TREE if there is no high element. Used to
10676 determine whether or not a new case label duplicates an old case
10677 label. We need a tree, rather than simply a hash table, because
10678 of the GNU case range extension. */
10679 splay_tree cases;
10680
10681 /* The bindings at the point of the switch. This is used for
10682 warnings crossing decls when branching to a case label. */
10683 struct c_spot_bindings *bindings;
10684
10685 /* The next node on the stack. */
10686 struct c_switch *next;
10687
10688 /* Remember whether the controlling expression had boolean type
10689 before integer promotions for the sake of -Wswitch-bool. */
10690 bool bool_cond_p;
10691 };
10692
10693 /* A stack of the currently active switch statements. The innermost
10694 switch statement is on the top of the stack. There is no need to
10695 mark the stack for garbage collection because it is only active
10696 during the processing of the body of a function, and we never
10697 collect at that point. */
10698
10699 struct c_switch *c_switch_stack;
10700
10701 /* Start a C switch statement, testing expression EXP. Return the new
10702 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10703 SWITCH_COND_LOC is the location of the switch's condition.
10704 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10705
10706 tree
10707 c_start_case (location_t switch_loc,
10708 location_t switch_cond_loc,
10709 tree exp, bool explicit_cast_p)
10710 {
10711 tree orig_type = error_mark_node;
10712 bool bool_cond_p = false;
10713 struct c_switch *cs;
10714
10715 if (exp != error_mark_node)
10716 {
10717 orig_type = TREE_TYPE (exp);
10718
10719 if (!INTEGRAL_TYPE_P (orig_type))
10720 {
10721 if (orig_type != error_mark_node)
10722 {
10723 error_at (switch_cond_loc, "switch quantity not an integer");
10724 orig_type = error_mark_node;
10725 }
10726 exp = integer_zero_node;
10727 }
10728 else
10729 {
10730 tree type = TYPE_MAIN_VARIANT (orig_type);
10731 tree e = exp;
10732
10733 /* Warn if the condition has boolean value. */
10734 while (TREE_CODE (e) == COMPOUND_EXPR)
10735 e = TREE_OPERAND (e, 1);
10736
10737 if ((TREE_CODE (type) == BOOLEAN_TYPE
10738 || truth_value_p (TREE_CODE (e)))
10739 /* Explicit cast to int suppresses this warning. */
10740 && !(TREE_CODE (type) == INTEGER_TYPE
10741 && explicit_cast_p))
10742 bool_cond_p = true;
10743
10744 if (!in_system_header_at (input_location)
10745 && (type == long_integer_type_node
10746 || type == long_unsigned_type_node))
10747 warning_at (switch_cond_loc,
10748 OPT_Wtraditional, "%<long%> switch expression not "
10749 "converted to %<int%> in ISO C");
10750
10751 exp = c_fully_fold (exp, false, NULL);
10752 exp = default_conversion (exp);
10753
10754 if (warn_sequence_point)
10755 verify_sequence_points (exp);
10756 }
10757 }
10758
10759 /* Add this new SWITCH_EXPR to the stack. */
10760 cs = XNEW (struct c_switch);
10761 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10762 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10763 cs->orig_type = orig_type;
10764 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10765 cs->bindings = c_get_switch_bindings ();
10766 cs->bool_cond_p = bool_cond_p;
10767 cs->next = c_switch_stack;
10768 c_switch_stack = cs;
10769
10770 return add_stmt (cs->switch_expr);
10771 }
10772
10773 /* Process a case label at location LOC. */
10774
10775 tree
10776 do_case (location_t loc, tree low_value, tree high_value)
10777 {
10778 tree label = NULL_TREE;
10779
10780 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10781 {
10782 low_value = c_fully_fold (low_value, false, NULL);
10783 if (TREE_CODE (low_value) == INTEGER_CST)
10784 pedwarn (loc, OPT_Wpedantic,
10785 "case label is not an integer constant expression");
10786 }
10787
10788 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10789 {
10790 high_value = c_fully_fold (high_value, false, NULL);
10791 if (TREE_CODE (high_value) == INTEGER_CST)
10792 pedwarn (input_location, OPT_Wpedantic,
10793 "case label is not an integer constant expression");
10794 }
10795
10796 if (c_switch_stack == NULL)
10797 {
10798 if (low_value)
10799 error_at (loc, "case label not within a switch statement");
10800 else
10801 error_at (loc, "%<default%> label not within a switch statement");
10802 return NULL_TREE;
10803 }
10804
10805 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10806 EXPR_LOCATION (c_switch_stack->switch_expr),
10807 loc))
10808 return NULL_TREE;
10809
10810 label = c_add_case_label (loc, c_switch_stack->cases,
10811 SWITCH_COND (c_switch_stack->switch_expr),
10812 low_value, high_value);
10813 if (label == error_mark_node)
10814 label = NULL_TREE;
10815 return label;
10816 }
10817
10818 /* Finish the switch statement. TYPE is the original type of the
10819 controlling expression of the switch, or NULL_TREE. */
10820
10821 void
10822 c_finish_case (tree body, tree type)
10823 {
10824 struct c_switch *cs = c_switch_stack;
10825 location_t switch_location;
10826
10827 SWITCH_BODY (cs->switch_expr) = body;
10828
10829 /* Emit warnings as needed. */
10830 switch_location = EXPR_LOCATION (cs->switch_expr);
10831 c_do_switch_warnings (cs->cases, switch_location,
10832 type ? type : TREE_TYPE (cs->switch_expr),
10833 SWITCH_COND (cs->switch_expr), cs->bool_cond_p);
10834 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10835 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10836
10837 /* Pop the stack. */
10838 c_switch_stack = cs->next;
10839 splay_tree_delete (cs->cases);
10840 c_release_switch_bindings (cs->bindings);
10841 XDELETE (cs);
10842 }
10843 \f
10844 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10845 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10846 may be null. */
10847
10848 void
10849 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10850 tree else_block)
10851 {
10852 tree stmt;
10853
10854 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10855 SET_EXPR_LOCATION (stmt, if_locus);
10856 add_stmt (stmt);
10857 }
10858
10859 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10860 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10861 is false for DO loops. INCR is the FOR increment expression. BODY is
10862 the statement controlled by the loop. BLAB is the break label. CLAB is
10863 the continue label. Everything is allowed to be NULL.
10864 COND_LOCUS is the location of the loop condition, INCR_LOCUS is the
10865 location of the FOR increment expression. */
10866
10867 void
10868 c_finish_loop (location_t start_locus, location_t cond_locus, tree cond,
10869 location_t incr_locus, tree incr, tree body, tree blab,
10870 tree clab, bool cond_is_first)
10871 {
10872 tree entry = NULL, exit = NULL, t;
10873
10874 /* If the condition is zero don't generate a loop construct. */
10875 if (cond && integer_zerop (cond))
10876 {
10877 if (cond_is_first)
10878 {
10879 t = build_and_jump (&blab);
10880 SET_EXPR_LOCATION (t, start_locus);
10881 add_stmt (t);
10882 }
10883 }
10884 else
10885 {
10886 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10887
10888 /* If we have an exit condition, then we build an IF with gotos either
10889 out of the loop, or to the top of it. If there's no exit condition,
10890 then we just build a jump back to the top. */
10891 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10892
10893 if (cond && !integer_nonzerop (cond))
10894 {
10895 /* Canonicalize the loop condition to the end. This means
10896 generating a branch to the loop condition. Reuse the
10897 continue label, if possible. */
10898 if (cond_is_first)
10899 {
10900 if (incr || !clab)
10901 {
10902 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10903 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10904 }
10905 else
10906 t = build1 (GOTO_EXPR, void_type_node, clab);
10907 SET_EXPR_LOCATION (t, start_locus);
10908 add_stmt (t);
10909 }
10910
10911 t = build_and_jump (&blab);
10912 exit = fold_build3_loc (cond_is_first ? start_locus : input_location,
10913 COND_EXPR, void_type_node, cond, exit, t);
10914 }
10915 else
10916 {
10917 /* For the backward-goto's location of an unconditional loop
10918 use the beginning of the body, or, if there is none, the
10919 top of the loop. */
10920 location_t loc = EXPR_LOCATION (expr_first (body));
10921 if (loc == UNKNOWN_LOCATION)
10922 loc = start_locus;
10923 SET_EXPR_LOCATION (exit, loc);
10924 }
10925
10926 add_stmt (top);
10927 }
10928
10929 if (body)
10930 add_stmt (body);
10931 if (clab)
10932 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10933 if (incr)
10934 {
10935 if (MAY_HAVE_DEBUG_MARKER_STMTS && incr_locus != UNKNOWN_LOCATION)
10936 {
10937 t = build0 (DEBUG_BEGIN_STMT, void_type_node);
10938 SET_EXPR_LOCATION (t, incr_locus);
10939 add_stmt (t);
10940 }
10941 add_stmt (incr);
10942 }
10943 if (entry)
10944 add_stmt (entry);
10945 if (MAY_HAVE_DEBUG_MARKER_STMTS && cond_locus != UNKNOWN_LOCATION)
10946 {
10947 t = build0 (DEBUG_BEGIN_STMT, void_type_node);
10948 SET_EXPR_LOCATION (t, cond_locus);
10949 add_stmt (t);
10950 }
10951 if (exit)
10952 add_stmt (exit);
10953 if (blab)
10954 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10955 }
10956
10957 tree
10958 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10959 {
10960 bool skip;
10961 tree label = *label_p;
10962
10963 /* In switch statements break is sometimes stylistically used after
10964 a return statement. This can lead to spurious warnings about
10965 control reaching the end of a non-void function when it is
10966 inlined. Note that we are calling block_may_fallthru with
10967 language specific tree nodes; this works because
10968 block_may_fallthru returns true when given something it does not
10969 understand. */
10970 skip = !block_may_fallthru (cur_stmt_list);
10971
10972 if (!label)
10973 {
10974 if (!skip)
10975 *label_p = label = create_artificial_label (loc);
10976 }
10977 else if (TREE_CODE (label) == LABEL_DECL)
10978 ;
10979 else switch (TREE_INT_CST_LOW (label))
10980 {
10981 case 0:
10982 if (is_break)
10983 error_at (loc, "break statement not within loop or switch");
10984 else
10985 error_at (loc, "continue statement not within a loop");
10986 return NULL_TREE;
10987
10988 case 1:
10989 gcc_assert (is_break);
10990 error_at (loc, "break statement used with OpenMP for loop");
10991 return NULL_TREE;
10992
10993 case 2:
10994 if (is_break)
10995 error ("break statement within %<#pragma simd%> loop body");
10996 else
10997 error ("continue statement within %<#pragma simd%> loop body");
10998 return NULL_TREE;
10999
11000 default:
11001 gcc_unreachable ();
11002 }
11003
11004 if (skip)
11005 return NULL_TREE;
11006
11007 if (!is_break)
11008 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
11009
11010 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
11011 }
11012
11013 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11014
11015 static void
11016 emit_side_effect_warnings (location_t loc, tree expr)
11017 {
11018 if (expr == error_mark_node)
11019 ;
11020 else if (!TREE_SIDE_EFFECTS (expr))
11021 {
11022 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
11023 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11024 }
11025 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11026 {
11027 tree r = expr;
11028 location_t cloc = loc;
11029 while (TREE_CODE (r) == COMPOUND_EXPR)
11030 {
11031 if (EXPR_HAS_LOCATION (r))
11032 cloc = EXPR_LOCATION (r);
11033 r = TREE_OPERAND (r, 1);
11034 }
11035 if (!TREE_SIDE_EFFECTS (r)
11036 && !VOID_TYPE_P (TREE_TYPE (r))
11037 && !CONVERT_EXPR_P (r)
11038 && !TREE_NO_WARNING (r)
11039 && !TREE_NO_WARNING (expr))
11040 warning_at (cloc, OPT_Wunused_value,
11041 "right-hand operand of comma expression has no effect");
11042 }
11043 else
11044 warn_if_unused_value (expr, loc);
11045 }
11046
11047 /* Process an expression as if it were a complete statement. Emit
11048 diagnostics, but do not call ADD_STMT. LOC is the location of the
11049 statement. */
11050
11051 tree
11052 c_process_expr_stmt (location_t loc, tree expr)
11053 {
11054 tree exprv;
11055
11056 if (!expr)
11057 return NULL_TREE;
11058
11059 expr = c_fully_fold (expr, false, NULL);
11060
11061 if (warn_sequence_point)
11062 verify_sequence_points (expr);
11063
11064 if (TREE_TYPE (expr) != error_mark_node
11065 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11066 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11067 error_at (loc, "expression statement has incomplete type");
11068
11069 /* If we're not processing a statement expression, warn about unused values.
11070 Warnings for statement expressions will be emitted later, once we figure
11071 out which is the result. */
11072 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11073 && warn_unused_value)
11074 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11075
11076 exprv = expr;
11077 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11078 exprv = TREE_OPERAND (exprv, 1);
11079 while (CONVERT_EXPR_P (exprv))
11080 exprv = TREE_OPERAND (exprv, 0);
11081 if (DECL_P (exprv)
11082 || handled_component_p (exprv)
11083 || TREE_CODE (exprv) == ADDR_EXPR)
11084 mark_exp_read (exprv);
11085
11086 /* If the expression is not of a type to which we cannot assign a line
11087 number, wrap the thing in a no-op NOP_EXPR. */
11088 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11089 {
11090 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11091 SET_EXPR_LOCATION (expr, loc);
11092 }
11093
11094 return expr;
11095 }
11096
11097 /* Emit an expression as a statement. LOC is the location of the
11098 expression. */
11099
11100 tree
11101 c_finish_expr_stmt (location_t loc, tree expr)
11102 {
11103 if (expr)
11104 return add_stmt (c_process_expr_stmt (loc, expr));
11105 else
11106 return NULL;
11107 }
11108
11109 /* Do the opposite and emit a statement as an expression. To begin,
11110 create a new binding level and return it. */
11111
11112 tree
11113 c_begin_stmt_expr (void)
11114 {
11115 tree ret;
11116
11117 /* We must force a BLOCK for this level so that, if it is not expanded
11118 later, there is a way to turn off the entire subtree of blocks that
11119 are contained in it. */
11120 keep_next_level ();
11121 ret = c_begin_compound_stmt (true);
11122
11123 c_bindings_start_stmt_expr (c_switch_stack == NULL
11124 ? NULL
11125 : c_switch_stack->bindings);
11126
11127 /* Mark the current statement list as belonging to a statement list. */
11128 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11129
11130 return ret;
11131 }
11132
11133 /* LOC is the location of the compound statement to which this body
11134 belongs. */
11135
11136 tree
11137 c_finish_stmt_expr (location_t loc, tree body)
11138 {
11139 tree last, type, tmp, val;
11140 tree *last_p;
11141
11142 body = c_end_compound_stmt (loc, body, true);
11143
11144 c_bindings_end_stmt_expr (c_switch_stack == NULL
11145 ? NULL
11146 : c_switch_stack->bindings);
11147
11148 /* Locate the last statement in BODY. See c_end_compound_stmt
11149 about always returning a BIND_EXPR. */
11150 last_p = &BIND_EXPR_BODY (body);
11151 last = BIND_EXPR_BODY (body);
11152
11153 continue_searching:
11154 if (TREE_CODE (last) == STATEMENT_LIST)
11155 {
11156 tree_stmt_iterator l = tsi_last (last);
11157
11158 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11159 tsi_prev (&l);
11160
11161 /* This can happen with degenerate cases like ({ }). No value. */
11162 if (tsi_end_p (l))
11163 return body;
11164
11165 /* If we're supposed to generate side effects warnings, process
11166 all of the statements except the last. */
11167 if (warn_unused_value)
11168 {
11169 for (tree_stmt_iterator i = tsi_start (last);
11170 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11171 {
11172 location_t tloc;
11173 tree t = tsi_stmt (i);
11174
11175 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11176 emit_side_effect_warnings (tloc, t);
11177 }
11178 }
11179 last_p = tsi_stmt_ptr (l);
11180 last = *last_p;
11181 }
11182
11183 /* If the end of the list is exception related, then the list was split
11184 by a call to push_cleanup. Continue searching. */
11185 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11186 || TREE_CODE (last) == TRY_CATCH_EXPR)
11187 {
11188 last_p = &TREE_OPERAND (last, 0);
11189 last = *last_p;
11190 goto continue_searching;
11191 }
11192
11193 if (last == error_mark_node)
11194 return last;
11195
11196 /* In the case that the BIND_EXPR is not necessary, return the
11197 expression out from inside it. */
11198 if ((last == BIND_EXPR_BODY (body)
11199 /* Skip nested debug stmts. */
11200 || last == expr_first (BIND_EXPR_BODY (body)))
11201 && BIND_EXPR_VARS (body) == NULL)
11202 {
11203 /* Even if this looks constant, do not allow it in a constant
11204 expression. */
11205 last = c_wrap_maybe_const (last, true);
11206 /* Do not warn if the return value of a statement expression is
11207 unused. */
11208 TREE_NO_WARNING (last) = 1;
11209 return last;
11210 }
11211
11212 /* Extract the type of said expression. */
11213 type = TREE_TYPE (last);
11214
11215 /* If we're not returning a value at all, then the BIND_EXPR that
11216 we already have is a fine expression to return. */
11217 if (!type || VOID_TYPE_P (type))
11218 return body;
11219
11220 /* Now that we've located the expression containing the value, it seems
11221 silly to make voidify_wrapper_expr repeat the process. Create a
11222 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11223 tmp = create_tmp_var_raw (type);
11224
11225 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11226 tree_expr_nonnegative_p giving up immediately. */
11227 val = last;
11228 if (TREE_CODE (val) == NOP_EXPR
11229 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11230 val = TREE_OPERAND (val, 0);
11231
11232 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11233 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11234
11235 {
11236 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11237 SET_EXPR_LOCATION (t, loc);
11238 return t;
11239 }
11240 }
11241 \f
11242 /* Begin and end compound statements. This is as simple as pushing
11243 and popping new statement lists from the tree. */
11244
11245 tree
11246 c_begin_compound_stmt (bool do_scope)
11247 {
11248 tree stmt = push_stmt_list ();
11249 if (do_scope)
11250 push_scope ();
11251 return stmt;
11252 }
11253
11254 /* End a compound statement. STMT is the statement. LOC is the
11255 location of the compound statement-- this is usually the location
11256 of the opening brace. */
11257
11258 tree
11259 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11260 {
11261 tree block = NULL;
11262
11263 if (do_scope)
11264 {
11265 if (c_dialect_objc ())
11266 objc_clear_super_receiver ();
11267 block = pop_scope ();
11268 }
11269
11270 stmt = pop_stmt_list (stmt);
11271 stmt = c_build_bind_expr (loc, block, stmt);
11272
11273 /* If this compound statement is nested immediately inside a statement
11274 expression, then force a BIND_EXPR to be created. Otherwise we'll
11275 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11276 STATEMENT_LISTs merge, and thus we can lose track of what statement
11277 was really last. */
11278 if (building_stmt_list_p ()
11279 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11280 && TREE_CODE (stmt) != BIND_EXPR)
11281 {
11282 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11283 TREE_SIDE_EFFECTS (stmt) = 1;
11284 SET_EXPR_LOCATION (stmt, loc);
11285 }
11286
11287 return stmt;
11288 }
11289
11290 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11291 when the current scope is exited. EH_ONLY is true when this is not
11292 meant to apply to normal control flow transfer. */
11293
11294 void
11295 push_cleanup (tree decl, tree cleanup, bool eh_only)
11296 {
11297 enum tree_code code;
11298 tree stmt, list;
11299 bool stmt_expr;
11300
11301 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11302 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11303 add_stmt (stmt);
11304 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11305 list = push_stmt_list ();
11306 TREE_OPERAND (stmt, 0) = list;
11307 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11308 }
11309 \f
11310 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11311 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11312
11313 static tree
11314 build_vec_cmp (tree_code code, tree type,
11315 tree arg0, tree arg1)
11316 {
11317 tree zero_vec = build_zero_cst (type);
11318 tree minus_one_vec = build_minus_one_cst (type);
11319 tree cmp_type = build_same_sized_truth_vector_type (type);
11320 tree cmp = build2 (code, cmp_type, arg0, arg1);
11321 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11322 }
11323
11324 /* Build a binary-operation expression without default conversions.
11325 CODE is the kind of expression to build.
11326 LOCATION is the operator's location.
11327 This function differs from `build' in several ways:
11328 the data type of the result is computed and recorded in it,
11329 warnings are generated if arg data types are invalid,
11330 special handling for addition and subtraction of pointers is known,
11331 and some optimization is done (operations on narrow ints
11332 are done in the narrower type when that gives the same result).
11333 Constant folding is also done before the result is returned.
11334
11335 Note that the operands will never have enumeral types, or function
11336 or array types, because either they will have the default conversions
11337 performed or they have both just been converted to some other type in which
11338 the arithmetic is to be done. */
11339
11340 tree
11341 build_binary_op (location_t location, enum tree_code code,
11342 tree orig_op0, tree orig_op1, bool convert_p)
11343 {
11344 tree type0, type1, orig_type0, orig_type1;
11345 tree eptype;
11346 enum tree_code code0, code1;
11347 tree op0, op1;
11348 tree ret = error_mark_node;
11349 const char *invalid_op_diag;
11350 bool op0_int_operands, op1_int_operands;
11351 bool int_const, int_const_or_overflow, int_operands;
11352
11353 /* Expression code to give to the expression when it is built.
11354 Normally this is CODE, which is what the caller asked for,
11355 but in some special cases we change it. */
11356 enum tree_code resultcode = code;
11357
11358 /* Data type in which the computation is to be performed.
11359 In the simplest cases this is the common type of the arguments. */
11360 tree result_type = NULL;
11361
11362 /* When the computation is in excess precision, the type of the
11363 final EXCESS_PRECISION_EXPR. */
11364 tree semantic_result_type = NULL;
11365
11366 /* Nonzero means operands have already been type-converted
11367 in whatever way is necessary.
11368 Zero means they need to be converted to RESULT_TYPE. */
11369 int converted = 0;
11370
11371 /* Nonzero means create the expression with this type, rather than
11372 RESULT_TYPE. */
11373 tree build_type = NULL_TREE;
11374
11375 /* Nonzero means after finally constructing the expression
11376 convert it to this type. */
11377 tree final_type = NULL_TREE;
11378
11379 /* Nonzero if this is an operation like MIN or MAX which can
11380 safely be computed in short if both args are promoted shorts.
11381 Also implies COMMON.
11382 -1 indicates a bitwise operation; this makes a difference
11383 in the exact conditions for when it is safe to do the operation
11384 in a narrower mode. */
11385 int shorten = 0;
11386
11387 /* Nonzero if this is a comparison operation;
11388 if both args are promoted shorts, compare the original shorts.
11389 Also implies COMMON. */
11390 int short_compare = 0;
11391
11392 /* Nonzero if this is a right-shift operation, which can be computed on the
11393 original short and then promoted if the operand is a promoted short. */
11394 int short_shift = 0;
11395
11396 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11397 int common = 0;
11398
11399 /* True means types are compatible as far as ObjC is concerned. */
11400 bool objc_ok;
11401
11402 /* True means this is an arithmetic operation that may need excess
11403 precision. */
11404 bool may_need_excess_precision;
11405
11406 /* True means this is a boolean operation that converts both its
11407 operands to truth-values. */
11408 bool boolean_op = false;
11409
11410 /* Remember whether we're doing / or %. */
11411 bool doing_div_or_mod = false;
11412
11413 /* Remember whether we're doing << or >>. */
11414 bool doing_shift = false;
11415
11416 /* Tree holding instrumentation expression. */
11417 tree instrument_expr = NULL;
11418
11419 if (location == UNKNOWN_LOCATION)
11420 location = input_location;
11421
11422 op0 = orig_op0;
11423 op1 = orig_op1;
11424
11425 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11426 if (op0_int_operands)
11427 op0 = remove_c_maybe_const_expr (op0);
11428 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11429 if (op1_int_operands)
11430 op1 = remove_c_maybe_const_expr (op1);
11431 int_operands = (op0_int_operands && op1_int_operands);
11432 if (int_operands)
11433 {
11434 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11435 && TREE_CODE (orig_op1) == INTEGER_CST);
11436 int_const = (int_const_or_overflow
11437 && !TREE_OVERFLOW (orig_op0)
11438 && !TREE_OVERFLOW (orig_op1));
11439 }
11440 else
11441 int_const = int_const_or_overflow = false;
11442
11443 /* Do not apply default conversion in mixed vector/scalar expression. */
11444 if (convert_p
11445 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11446 {
11447 op0 = default_conversion (op0);
11448 op1 = default_conversion (op1);
11449 }
11450
11451 orig_type0 = type0 = TREE_TYPE (op0);
11452
11453 orig_type1 = type1 = TREE_TYPE (op1);
11454
11455 /* The expression codes of the data types of the arguments tell us
11456 whether the arguments are integers, floating, pointers, etc. */
11457 code0 = TREE_CODE (type0);
11458 code1 = TREE_CODE (type1);
11459
11460 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11461 STRIP_TYPE_NOPS (op0);
11462 STRIP_TYPE_NOPS (op1);
11463
11464 /* If an error was already reported for one of the arguments,
11465 avoid reporting another error. */
11466
11467 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11468 return error_mark_node;
11469
11470 if (code0 == POINTER_TYPE
11471 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11472 return error_mark_node;
11473
11474 if (code1 == POINTER_TYPE
11475 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11476 return error_mark_node;
11477
11478 if ((invalid_op_diag
11479 = targetm.invalid_binary_op (code, type0, type1)))
11480 {
11481 error_at (location, invalid_op_diag);
11482 return error_mark_node;
11483 }
11484
11485 switch (code)
11486 {
11487 case PLUS_EXPR:
11488 case MINUS_EXPR:
11489 case MULT_EXPR:
11490 case TRUNC_DIV_EXPR:
11491 case CEIL_DIV_EXPR:
11492 case FLOOR_DIV_EXPR:
11493 case ROUND_DIV_EXPR:
11494 case EXACT_DIV_EXPR:
11495 may_need_excess_precision = true;
11496 break;
11497
11498 case EQ_EXPR:
11499 case NE_EXPR:
11500 case LE_EXPR:
11501 case GE_EXPR:
11502 case LT_EXPR:
11503 case GT_EXPR:
11504 /* Excess precision for implicit conversions of integers to
11505 floating point in C11 and later. */
11506 may_need_excess_precision = (flag_isoc11
11507 && (ANY_INTEGRAL_TYPE_P (type0)
11508 || ANY_INTEGRAL_TYPE_P (type1)));
11509 break;
11510
11511 default:
11512 may_need_excess_precision = false;
11513 break;
11514 }
11515 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11516 {
11517 op0 = TREE_OPERAND (op0, 0);
11518 type0 = TREE_TYPE (op0);
11519 }
11520 else if (may_need_excess_precision
11521 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11522 {
11523 type0 = eptype;
11524 op0 = convert (eptype, op0);
11525 }
11526 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11527 {
11528 op1 = TREE_OPERAND (op1, 0);
11529 type1 = TREE_TYPE (op1);
11530 }
11531 else if (may_need_excess_precision
11532 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11533 {
11534 type1 = eptype;
11535 op1 = convert (eptype, op1);
11536 }
11537
11538 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11539
11540 /* In case when one of the operands of the binary operation is
11541 a vector and another is a scalar -- convert scalar to vector. */
11542 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11543 {
11544 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11545 true);
11546
11547 switch (convert_flag)
11548 {
11549 case stv_error:
11550 return error_mark_node;
11551 case stv_firstarg:
11552 {
11553 bool maybe_const = true;
11554 tree sc;
11555 sc = c_fully_fold (op0, false, &maybe_const);
11556 sc = save_expr (sc);
11557 sc = convert (TREE_TYPE (type1), sc);
11558 op0 = build_vector_from_val (type1, sc);
11559 if (!maybe_const)
11560 op0 = c_wrap_maybe_const (op0, true);
11561 orig_type0 = type0 = TREE_TYPE (op0);
11562 code0 = TREE_CODE (type0);
11563 converted = 1;
11564 break;
11565 }
11566 case stv_secondarg:
11567 {
11568 bool maybe_const = true;
11569 tree sc;
11570 sc = c_fully_fold (op1, false, &maybe_const);
11571 sc = save_expr (sc);
11572 sc = convert (TREE_TYPE (type0), sc);
11573 op1 = build_vector_from_val (type0, sc);
11574 if (!maybe_const)
11575 op1 = c_wrap_maybe_const (op1, true);
11576 orig_type1 = type1 = TREE_TYPE (op1);
11577 code1 = TREE_CODE (type1);
11578 converted = 1;
11579 break;
11580 }
11581 default:
11582 break;
11583 }
11584 }
11585
11586 switch (code)
11587 {
11588 case PLUS_EXPR:
11589 /* Handle the pointer + int case. */
11590 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11591 {
11592 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11593 goto return_build_binary_op;
11594 }
11595 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11596 {
11597 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11598 goto return_build_binary_op;
11599 }
11600 else
11601 common = 1;
11602 break;
11603
11604 case MINUS_EXPR:
11605 /* Subtraction of two similar pointers.
11606 We must subtract them as integers, then divide by object size. */
11607 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11608 && comp_target_types (location, type0, type1))
11609 {
11610 ret = pointer_diff (location, op0, op1, &instrument_expr);
11611 goto return_build_binary_op;
11612 }
11613 /* Handle pointer minus int. Just like pointer plus int. */
11614 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11615 {
11616 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11617 goto return_build_binary_op;
11618 }
11619 else
11620 common = 1;
11621 break;
11622
11623 case MULT_EXPR:
11624 common = 1;
11625 break;
11626
11627 case TRUNC_DIV_EXPR:
11628 case CEIL_DIV_EXPR:
11629 case FLOOR_DIV_EXPR:
11630 case ROUND_DIV_EXPR:
11631 case EXACT_DIV_EXPR:
11632 doing_div_or_mod = true;
11633 warn_for_div_by_zero (location, op1);
11634
11635 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11636 || code0 == FIXED_POINT_TYPE
11637 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11638 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11639 || code1 == FIXED_POINT_TYPE
11640 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11641 {
11642 enum tree_code tcode0 = code0, tcode1 = code1;
11643
11644 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11645 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11646 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11647 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11648
11649 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11650 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11651 resultcode = RDIV_EXPR;
11652 else
11653 /* Although it would be tempting to shorten always here, that
11654 loses on some targets, since the modulo instruction is
11655 undefined if the quotient can't be represented in the
11656 computation mode. We shorten only if unsigned or if
11657 dividing by something we know != -1. */
11658 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11659 || (TREE_CODE (op1) == INTEGER_CST
11660 && !integer_all_onesp (op1)));
11661 common = 1;
11662 }
11663 break;
11664
11665 case BIT_AND_EXPR:
11666 case BIT_IOR_EXPR:
11667 case BIT_XOR_EXPR:
11668 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11669 shorten = -1;
11670 /* Allow vector types which are not floating point types. */
11671 else if (code0 == VECTOR_TYPE
11672 && code1 == VECTOR_TYPE
11673 && !VECTOR_FLOAT_TYPE_P (type0)
11674 && !VECTOR_FLOAT_TYPE_P (type1))
11675 common = 1;
11676 break;
11677
11678 case TRUNC_MOD_EXPR:
11679 case FLOOR_MOD_EXPR:
11680 doing_div_or_mod = true;
11681 warn_for_div_by_zero (location, op1);
11682
11683 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11684 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11685 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11686 common = 1;
11687 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11688 {
11689 /* Although it would be tempting to shorten always here, that loses
11690 on some targets, since the modulo instruction is undefined if the
11691 quotient can't be represented in the computation mode. We shorten
11692 only if unsigned or if dividing by something we know != -1. */
11693 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11694 || (TREE_CODE (op1) == INTEGER_CST
11695 && !integer_all_onesp (op1)));
11696 common = 1;
11697 }
11698 break;
11699
11700 case TRUTH_ANDIF_EXPR:
11701 case TRUTH_ORIF_EXPR:
11702 case TRUTH_AND_EXPR:
11703 case TRUTH_OR_EXPR:
11704 case TRUTH_XOR_EXPR:
11705 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11706 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11707 || code0 == FIXED_POINT_TYPE)
11708 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11709 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11710 || code1 == FIXED_POINT_TYPE))
11711 {
11712 /* Result of these operations is always an int,
11713 but that does not mean the operands should be
11714 converted to ints! */
11715 result_type = integer_type_node;
11716 if (op0_int_operands)
11717 {
11718 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11719 op0 = remove_c_maybe_const_expr (op0);
11720 }
11721 else
11722 op0 = c_objc_common_truthvalue_conversion (location, op0);
11723 if (op1_int_operands)
11724 {
11725 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11726 op1 = remove_c_maybe_const_expr (op1);
11727 }
11728 else
11729 op1 = c_objc_common_truthvalue_conversion (location, op1);
11730 converted = 1;
11731 boolean_op = true;
11732 }
11733 if (code == TRUTH_ANDIF_EXPR)
11734 {
11735 int_const_or_overflow = (int_operands
11736 && TREE_CODE (orig_op0) == INTEGER_CST
11737 && (op0 == truthvalue_false_node
11738 || TREE_CODE (orig_op1) == INTEGER_CST));
11739 int_const = (int_const_or_overflow
11740 && !TREE_OVERFLOW (orig_op0)
11741 && (op0 == truthvalue_false_node
11742 || !TREE_OVERFLOW (orig_op1)));
11743 }
11744 else if (code == TRUTH_ORIF_EXPR)
11745 {
11746 int_const_or_overflow = (int_operands
11747 && TREE_CODE (orig_op0) == INTEGER_CST
11748 && (op0 == truthvalue_true_node
11749 || TREE_CODE (orig_op1) == INTEGER_CST));
11750 int_const = (int_const_or_overflow
11751 && !TREE_OVERFLOW (orig_op0)
11752 && (op0 == truthvalue_true_node
11753 || !TREE_OVERFLOW (orig_op1)));
11754 }
11755 break;
11756
11757 /* Shift operations: result has same type as first operand;
11758 always convert second operand to int.
11759 Also set SHORT_SHIFT if shifting rightward. */
11760
11761 case RSHIFT_EXPR:
11762 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11763 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11764 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11765 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11766 TYPE_VECTOR_SUBPARTS (type1)))
11767 {
11768 result_type = type0;
11769 converted = 1;
11770 }
11771 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11772 || (code0 == VECTOR_TYPE
11773 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11774 && code1 == INTEGER_TYPE)
11775 {
11776 doing_shift = true;
11777 if (TREE_CODE (op1) == INTEGER_CST)
11778 {
11779 if (tree_int_cst_sgn (op1) < 0)
11780 {
11781 int_const = false;
11782 if (c_inhibit_evaluation_warnings == 0)
11783 warning_at (location, OPT_Wshift_count_negative,
11784 "right shift count is negative");
11785 }
11786 else if (code0 == VECTOR_TYPE)
11787 {
11788 if (compare_tree_int (op1,
11789 TYPE_PRECISION (TREE_TYPE (type0)))
11790 >= 0)
11791 {
11792 int_const = false;
11793 if (c_inhibit_evaluation_warnings == 0)
11794 warning_at (location, OPT_Wshift_count_overflow,
11795 "right shift count >= width of vector element");
11796 }
11797 }
11798 else
11799 {
11800 if (!integer_zerop (op1))
11801 short_shift = 1;
11802
11803 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11804 {
11805 int_const = false;
11806 if (c_inhibit_evaluation_warnings == 0)
11807 warning_at (location, OPT_Wshift_count_overflow,
11808 "right shift count >= width of type");
11809 }
11810 }
11811 }
11812
11813 /* Use the type of the value to be shifted. */
11814 result_type = type0;
11815 /* Avoid converting op1 to result_type later. */
11816 converted = 1;
11817 }
11818 break;
11819
11820 case LSHIFT_EXPR:
11821 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11822 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11823 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11824 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11825 TYPE_VECTOR_SUBPARTS (type1)))
11826 {
11827 result_type = type0;
11828 converted = 1;
11829 }
11830 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11831 || (code0 == VECTOR_TYPE
11832 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11833 && code1 == INTEGER_TYPE)
11834 {
11835 doing_shift = true;
11836 if (TREE_CODE (op0) == INTEGER_CST
11837 && tree_int_cst_sgn (op0) < 0)
11838 {
11839 /* Don't reject a left shift of a negative value in a context
11840 where a constant expression is needed in C90. */
11841 if (flag_isoc99)
11842 int_const = false;
11843 if (c_inhibit_evaluation_warnings == 0)
11844 warning_at (location, OPT_Wshift_negative_value,
11845 "left shift of negative value");
11846 }
11847 if (TREE_CODE (op1) == INTEGER_CST)
11848 {
11849 if (tree_int_cst_sgn (op1) < 0)
11850 {
11851 int_const = false;
11852 if (c_inhibit_evaluation_warnings == 0)
11853 warning_at (location, OPT_Wshift_count_negative,
11854 "left shift count is negative");
11855 }
11856 else if (code0 == VECTOR_TYPE)
11857 {
11858 if (compare_tree_int (op1,
11859 TYPE_PRECISION (TREE_TYPE (type0)))
11860 >= 0)
11861 {
11862 int_const = false;
11863 if (c_inhibit_evaluation_warnings == 0)
11864 warning_at (location, OPT_Wshift_count_overflow,
11865 "left shift count >= width of vector element");
11866 }
11867 }
11868 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11869 {
11870 int_const = false;
11871 if (c_inhibit_evaluation_warnings == 0)
11872 warning_at (location, OPT_Wshift_count_overflow,
11873 "left shift count >= width of type");
11874 }
11875 else if (TREE_CODE (op0) == INTEGER_CST
11876 && maybe_warn_shift_overflow (location, op0, op1)
11877 && flag_isoc99)
11878 int_const = false;
11879 }
11880
11881 /* Use the type of the value to be shifted. */
11882 result_type = type0;
11883 /* Avoid converting op1 to result_type later. */
11884 converted = 1;
11885 }
11886 break;
11887
11888 case EQ_EXPR:
11889 case NE_EXPR:
11890 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11891 {
11892 tree intt;
11893 if (!vector_types_compatible_elements_p (type0, type1))
11894 {
11895 error_at (location, "comparing vectors with different "
11896 "element types");
11897 return error_mark_node;
11898 }
11899
11900 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11901 TYPE_VECTOR_SUBPARTS (type1)))
11902 {
11903 error_at (location, "comparing vectors with different "
11904 "number of elements");
11905 return error_mark_node;
11906 }
11907
11908 /* It's not precisely specified how the usual arithmetic
11909 conversions apply to the vector types. Here, we use
11910 the unsigned type if one of the operands is signed and
11911 the other one is unsigned. */
11912 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11913 {
11914 if (!TYPE_UNSIGNED (type0))
11915 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11916 else
11917 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11918 warning_at (location, OPT_Wsign_compare, "comparison between "
11919 "types %qT and %qT", type0, type1);
11920 }
11921
11922 /* Always construct signed integer vector type. */
11923 intt = c_common_type_for_size (GET_MODE_BITSIZE
11924 (SCALAR_TYPE_MODE
11925 (TREE_TYPE (type0))), 0);
11926 if (!intt)
11927 {
11928 error_at (location, "could not find an integer type "
11929 "of the same size as %qT",
11930 TREE_TYPE (type0));
11931 return error_mark_node;
11932 }
11933 result_type = build_opaque_vector_type (intt,
11934 TYPE_VECTOR_SUBPARTS (type0));
11935 converted = 1;
11936 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11937 goto return_build_binary_op;
11938 }
11939 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11940 warning_at (location,
11941 OPT_Wfloat_equal,
11942 "comparing floating point with %<==%> or %<!=%> is unsafe");
11943 /* Result of comparison is always int,
11944 but don't convert the args to int! */
11945 build_type = integer_type_node;
11946 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11947 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11948 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11949 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11950 short_compare = 1;
11951 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11952 {
11953 if (TREE_CODE (op0) == ADDR_EXPR
11954 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11955 && !from_macro_expansion_at (location))
11956 {
11957 if (code == EQ_EXPR)
11958 warning_at (location,
11959 OPT_Waddress,
11960 "the comparison will always evaluate as %<false%> "
11961 "for the address of %qD will never be NULL",
11962 TREE_OPERAND (op0, 0));
11963 else
11964 warning_at (location,
11965 OPT_Waddress,
11966 "the comparison will always evaluate as %<true%> "
11967 "for the address of %qD will never be NULL",
11968 TREE_OPERAND (op0, 0));
11969 }
11970 result_type = type0;
11971 }
11972 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11973 {
11974 if (TREE_CODE (op1) == ADDR_EXPR
11975 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11976 && !from_macro_expansion_at (location))
11977 {
11978 if (code == EQ_EXPR)
11979 warning_at (location,
11980 OPT_Waddress,
11981 "the comparison will always evaluate as %<false%> "
11982 "for the address of %qD will never be NULL",
11983 TREE_OPERAND (op1, 0));
11984 else
11985 warning_at (location,
11986 OPT_Waddress,
11987 "the comparison will always evaluate as %<true%> "
11988 "for the address of %qD will never be NULL",
11989 TREE_OPERAND (op1, 0));
11990 }
11991 result_type = type1;
11992 }
11993 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11994 {
11995 tree tt0 = TREE_TYPE (type0);
11996 tree tt1 = TREE_TYPE (type1);
11997 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11998 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11999 addr_space_t as_common = ADDR_SPACE_GENERIC;
12000
12001 /* Anything compares with void *. void * compares with anything.
12002 Otherwise, the targets must be compatible
12003 and both must be object or both incomplete. */
12004 if (comp_target_types (location, type0, type1))
12005 result_type = common_pointer_type (type0, type1);
12006 else if (!addr_space_superset (as0, as1, &as_common))
12007 {
12008 error_at (location, "comparison of pointers to "
12009 "disjoint address spaces");
12010 return error_mark_node;
12011 }
12012 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12013 {
12014 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12015 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12016 "comparison of %<void *%> with function pointer");
12017 }
12018 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12019 {
12020 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12021 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12022 "comparison of %<void *%> with function pointer");
12023 }
12024 else
12025 /* Avoid warning about the volatile ObjC EH puts on decls. */
12026 if (!objc_ok)
12027 pedwarn (location, 0,
12028 "comparison of distinct pointer types lacks a cast");
12029
12030 if (result_type == NULL_TREE)
12031 {
12032 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12033 result_type = build_pointer_type
12034 (build_qualified_type (void_type_node, qual));
12035 }
12036 }
12037 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12038 {
12039 result_type = type0;
12040 pedwarn (location, 0, "comparison between pointer and integer");
12041 }
12042 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12043 {
12044 result_type = type1;
12045 pedwarn (location, 0, "comparison between pointer and integer");
12046 }
12047 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12048 || truth_value_p (TREE_CODE (orig_op0)))
12049 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12050 || truth_value_p (TREE_CODE (orig_op1))))
12051 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12052 break;
12053
12054 case LE_EXPR:
12055 case GE_EXPR:
12056 case LT_EXPR:
12057 case GT_EXPR:
12058 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
12059 {
12060 tree intt;
12061 if (!vector_types_compatible_elements_p (type0, type1))
12062 {
12063 error_at (location, "comparing vectors with different "
12064 "element types");
12065 return error_mark_node;
12066 }
12067
12068 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12069 TYPE_VECTOR_SUBPARTS (type1)))
12070 {
12071 error_at (location, "comparing vectors with different "
12072 "number of elements");
12073 return error_mark_node;
12074 }
12075
12076 /* It's not precisely specified how the usual arithmetic
12077 conversions apply to the vector types. Here, we use
12078 the unsigned type if one of the operands is signed and
12079 the other one is unsigned. */
12080 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12081 {
12082 if (!TYPE_UNSIGNED (type0))
12083 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12084 else
12085 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12086 warning_at (location, OPT_Wsign_compare, "comparison between "
12087 "types %qT and %qT", type0, type1);
12088 }
12089
12090 /* Always construct signed integer vector type. */
12091 intt = c_common_type_for_size (GET_MODE_BITSIZE
12092 (SCALAR_TYPE_MODE
12093 (TREE_TYPE (type0))), 0);
12094 if (!intt)
12095 {
12096 error_at (location, "could not find an integer type "
12097 "of the same size as %qT",
12098 TREE_TYPE (type0));
12099 return error_mark_node;
12100 }
12101 result_type = build_opaque_vector_type (intt,
12102 TYPE_VECTOR_SUBPARTS (type0));
12103 converted = 1;
12104 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12105 goto return_build_binary_op;
12106 }
12107 build_type = integer_type_node;
12108 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12109 || code0 == FIXED_POINT_TYPE)
12110 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12111 || code1 == FIXED_POINT_TYPE))
12112 short_compare = 1;
12113 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12114 {
12115 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12116 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12117 addr_space_t as_common;
12118
12119 if (comp_target_types (location, type0, type1))
12120 {
12121 result_type = common_pointer_type (type0, type1);
12122 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12123 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12124 pedwarn (location, 0,
12125 "comparison of complete and incomplete pointers");
12126 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12127 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12128 "ordered comparisons of pointers to functions");
12129 else if (null_pointer_constant_p (orig_op0)
12130 || null_pointer_constant_p (orig_op1))
12131 warning_at (location, OPT_Wextra,
12132 "ordered comparison of pointer with null pointer");
12133
12134 }
12135 else if (!addr_space_superset (as0, as1, &as_common))
12136 {
12137 error_at (location, "comparison of pointers to "
12138 "disjoint address spaces");
12139 return error_mark_node;
12140 }
12141 else
12142 {
12143 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12144 result_type = build_pointer_type
12145 (build_qualified_type (void_type_node, qual));
12146 pedwarn (location, 0,
12147 "comparison of distinct pointer types lacks a cast");
12148 }
12149 }
12150 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12151 {
12152 result_type = type0;
12153 if (pedantic)
12154 pedwarn (location, OPT_Wpedantic,
12155 "ordered comparison of pointer with integer zero");
12156 else if (extra_warnings)
12157 warning_at (location, OPT_Wextra,
12158 "ordered comparison of pointer with integer zero");
12159 }
12160 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12161 {
12162 result_type = type1;
12163 if (pedantic)
12164 pedwarn (location, OPT_Wpedantic,
12165 "ordered comparison of pointer with integer zero");
12166 else if (extra_warnings)
12167 warning_at (location, OPT_Wextra,
12168 "ordered comparison of pointer with integer zero");
12169 }
12170 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12171 {
12172 result_type = type0;
12173 pedwarn (location, 0, "comparison between pointer and integer");
12174 }
12175 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12176 {
12177 result_type = type1;
12178 pedwarn (location, 0, "comparison between pointer and integer");
12179 }
12180
12181 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12182 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12183 {
12184 op0 = save_expr (op0);
12185 op1 = save_expr (op1);
12186
12187 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12188 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12189 }
12190
12191 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12192 || truth_value_p (TREE_CODE (orig_op0)))
12193 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12194 || truth_value_p (TREE_CODE (orig_op1))))
12195 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12196 break;
12197
12198 default:
12199 gcc_unreachable ();
12200 }
12201
12202 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12203 return error_mark_node;
12204
12205 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
12206 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12207 || !vector_types_compatible_elements_p (type0, type1)))
12208 {
12209 gcc_rich_location richloc (location);
12210 maybe_range_label_for_tree_type_mismatch
12211 label_for_op0 (orig_op0, orig_op1),
12212 label_for_op1 (orig_op1, orig_op0);
12213 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12214 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12215 binary_op_error (&richloc, code, type0, type1);
12216 return error_mark_node;
12217 }
12218
12219 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12220 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
12221 &&
12222 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12223 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
12224 {
12225 bool first_complex = (code0 == COMPLEX_TYPE);
12226 bool second_complex = (code1 == COMPLEX_TYPE);
12227 int none_complex = (!first_complex && !second_complex);
12228
12229 if (shorten || common || short_compare)
12230 {
12231 result_type = c_common_type (type0, type1);
12232 do_warn_double_promotion (result_type, type0, type1,
12233 "implicit conversion from %qT to %qT "
12234 "to match other operand of binary "
12235 "expression",
12236 location);
12237 if (result_type == error_mark_node)
12238 return error_mark_node;
12239 }
12240
12241 if (first_complex != second_complex
12242 && (code == PLUS_EXPR
12243 || code == MINUS_EXPR
12244 || code == MULT_EXPR
12245 || (code == TRUNC_DIV_EXPR && first_complex))
12246 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12247 && flag_signed_zeros)
12248 {
12249 /* An operation on mixed real/complex operands must be
12250 handled specially, but the language-independent code can
12251 more easily optimize the plain complex arithmetic if
12252 -fno-signed-zeros. */
12253 tree real_type = TREE_TYPE (result_type);
12254 tree real, imag;
12255 if (type0 != orig_type0 || type1 != orig_type1)
12256 {
12257 gcc_assert (may_need_excess_precision && common);
12258 semantic_result_type = c_common_type (orig_type0, orig_type1);
12259 }
12260 if (first_complex)
12261 {
12262 if (TREE_TYPE (op0) != result_type)
12263 op0 = convert_and_check (location, result_type, op0);
12264 if (TREE_TYPE (op1) != real_type)
12265 op1 = convert_and_check (location, real_type, op1);
12266 }
12267 else
12268 {
12269 if (TREE_TYPE (op0) != real_type)
12270 op0 = convert_and_check (location, real_type, op0);
12271 if (TREE_TYPE (op1) != result_type)
12272 op1 = convert_and_check (location, result_type, op1);
12273 }
12274 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12275 return error_mark_node;
12276 if (first_complex)
12277 {
12278 op0 = save_expr (op0);
12279 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12280 op0, true);
12281 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12282 op0, true);
12283 switch (code)
12284 {
12285 case MULT_EXPR:
12286 case TRUNC_DIV_EXPR:
12287 op1 = save_expr (op1);
12288 imag = build2 (resultcode, real_type, imag, op1);
12289 /* Fall through. */
12290 case PLUS_EXPR:
12291 case MINUS_EXPR:
12292 real = build2 (resultcode, real_type, real, op1);
12293 break;
12294 default:
12295 gcc_unreachable();
12296 }
12297 }
12298 else
12299 {
12300 op1 = save_expr (op1);
12301 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12302 op1, true);
12303 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12304 op1, true);
12305 switch (code)
12306 {
12307 case MULT_EXPR:
12308 op0 = save_expr (op0);
12309 imag = build2 (resultcode, real_type, op0, imag);
12310 /* Fall through. */
12311 case PLUS_EXPR:
12312 real = build2 (resultcode, real_type, op0, real);
12313 break;
12314 case MINUS_EXPR:
12315 real = build2 (resultcode, real_type, op0, real);
12316 imag = build1 (NEGATE_EXPR, real_type, imag);
12317 break;
12318 default:
12319 gcc_unreachable();
12320 }
12321 }
12322 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12323 goto return_build_binary_op;
12324 }
12325
12326 /* For certain operations (which identify themselves by shorten != 0)
12327 if both args were extended from the same smaller type,
12328 do the arithmetic in that type and then extend.
12329
12330 shorten !=0 and !=1 indicates a bitwise operation.
12331 For them, this optimization is safe only if
12332 both args are zero-extended or both are sign-extended.
12333 Otherwise, we might change the result.
12334 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12335 but calculated in (unsigned short) it would be (unsigned short)-1. */
12336
12337 if (shorten && none_complex)
12338 {
12339 final_type = result_type;
12340 result_type = shorten_binary_op (result_type, op0, op1,
12341 shorten == -1);
12342 }
12343
12344 /* Shifts can be shortened if shifting right. */
12345
12346 if (short_shift)
12347 {
12348 int unsigned_arg;
12349 tree arg0 = get_narrower (op0, &unsigned_arg);
12350
12351 final_type = result_type;
12352
12353 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12354 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12355
12356 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12357 && tree_int_cst_sgn (op1) > 0
12358 /* We can shorten only if the shift count is less than the
12359 number of bits in the smaller type size. */
12360 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12361 /* We cannot drop an unsigned shift after sign-extension. */
12362 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12363 {
12364 /* Do an unsigned shift if the operand was zero-extended. */
12365 result_type
12366 = c_common_signed_or_unsigned_type (unsigned_arg,
12367 TREE_TYPE (arg0));
12368 /* Convert value-to-be-shifted to that type. */
12369 if (TREE_TYPE (op0) != result_type)
12370 op0 = convert (result_type, op0);
12371 converted = 1;
12372 }
12373 }
12374
12375 /* Comparison operations are shortened too but differently.
12376 They identify themselves by setting short_compare = 1. */
12377
12378 if (short_compare)
12379 {
12380 /* Don't write &op0, etc., because that would prevent op0
12381 from being kept in a register.
12382 Instead, make copies of the our local variables and
12383 pass the copies by reference, then copy them back afterward. */
12384 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12385 enum tree_code xresultcode = resultcode;
12386 tree val
12387 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12388 &xresultcode);
12389
12390 if (val != NULL_TREE)
12391 {
12392 ret = val;
12393 goto return_build_binary_op;
12394 }
12395
12396 op0 = xop0, op1 = xop1;
12397 converted = 1;
12398 resultcode = xresultcode;
12399
12400 if (c_inhibit_evaluation_warnings == 0)
12401 {
12402 bool op0_maybe_const = true;
12403 bool op1_maybe_const = true;
12404 tree orig_op0_folded, orig_op1_folded;
12405
12406 if (in_late_binary_op)
12407 {
12408 orig_op0_folded = orig_op0;
12409 orig_op1_folded = orig_op1;
12410 }
12411 else
12412 {
12413 /* Fold for the sake of possible warnings, as in
12414 build_conditional_expr. This requires the
12415 "original" values to be folded, not just op0 and
12416 op1. */
12417 c_inhibit_evaluation_warnings++;
12418 op0 = c_fully_fold (op0, require_constant_value,
12419 &op0_maybe_const);
12420 op1 = c_fully_fold (op1, require_constant_value,
12421 &op1_maybe_const);
12422 c_inhibit_evaluation_warnings--;
12423 orig_op0_folded = c_fully_fold (orig_op0,
12424 require_constant_value,
12425 NULL);
12426 orig_op1_folded = c_fully_fold (orig_op1,
12427 require_constant_value,
12428 NULL);
12429 }
12430
12431 if (warn_sign_compare)
12432 warn_for_sign_compare (location, orig_op0_folded,
12433 orig_op1_folded, op0, op1,
12434 result_type, resultcode);
12435 if (!in_late_binary_op && !int_operands)
12436 {
12437 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12438 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12439 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12440 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12441 }
12442 }
12443 }
12444 }
12445
12446 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12447 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12448 Then the expression will be built.
12449 It will be given type FINAL_TYPE if that is nonzero;
12450 otherwise, it will be given type RESULT_TYPE. */
12451
12452 if (!result_type)
12453 {
12454 /* Favor showing any expression locations that are available. */
12455 op_location_t oploc (location, UNKNOWN_LOCATION);
12456 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12457 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12458 return error_mark_node;
12459 }
12460
12461 if (build_type == NULL_TREE)
12462 {
12463 build_type = result_type;
12464 if ((type0 != orig_type0 || type1 != orig_type1)
12465 && !boolean_op)
12466 {
12467 gcc_assert (may_need_excess_precision && common);
12468 semantic_result_type = c_common_type (orig_type0, orig_type1);
12469 }
12470 }
12471
12472 if (!converted)
12473 {
12474 op0 = ep_convert_and_check (location, result_type, op0,
12475 semantic_result_type);
12476 op1 = ep_convert_and_check (location, result_type, op1,
12477 semantic_result_type);
12478
12479 /* This can happen if one operand has a vector type, and the other
12480 has a different type. */
12481 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12482 return error_mark_node;
12483 }
12484
12485 if (sanitize_flags_p ((SANITIZE_SHIFT
12486 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12487 && current_function_decl != NULL_TREE
12488 && (doing_div_or_mod || doing_shift)
12489 && !require_constant_value)
12490 {
12491 /* OP0 and/or OP1 might have side-effects. */
12492 op0 = save_expr (op0);
12493 op1 = save_expr (op1);
12494 op0 = c_fully_fold (op0, false, NULL);
12495 op1 = c_fully_fold (op1, false, NULL);
12496 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12497 | SANITIZE_FLOAT_DIVIDE))))
12498 instrument_expr = ubsan_instrument_division (location, op0, op1);
12499 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12500 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12501 }
12502
12503 /* Treat expressions in initializers specially as they can't trap. */
12504 if (int_const_or_overflow)
12505 ret = (require_constant_value
12506 ? fold_build2_initializer_loc (location, resultcode, build_type,
12507 op0, op1)
12508 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12509 else
12510 ret = build2 (resultcode, build_type, op0, op1);
12511 if (final_type != NULL_TREE)
12512 ret = convert (final_type, ret);
12513
12514 return_build_binary_op:
12515 gcc_assert (ret != error_mark_node);
12516 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12517 ret = (int_operands
12518 ? note_integer_operands (ret)
12519 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12520 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12521 && !in_late_binary_op)
12522 ret = note_integer_operands (ret);
12523 protected_set_expr_location (ret, location);
12524
12525 if (instrument_expr != NULL)
12526 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12527 instrument_expr, ret);
12528
12529 if (semantic_result_type)
12530 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12531 semantic_result_type, ret);
12532
12533 return ret;
12534 }
12535
12536
12537 /* Convert EXPR to be a truth-value, validating its type for this
12538 purpose. LOCATION is the source location for the expression. */
12539
12540 tree
12541 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12542 {
12543 bool int_const, int_operands;
12544
12545 switch (TREE_CODE (TREE_TYPE (expr)))
12546 {
12547 case ARRAY_TYPE:
12548 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12549 return error_mark_node;
12550
12551 case RECORD_TYPE:
12552 error_at (location, "used struct type value where scalar is required");
12553 return error_mark_node;
12554
12555 case UNION_TYPE:
12556 error_at (location, "used union type value where scalar is required");
12557 return error_mark_node;
12558
12559 case VOID_TYPE:
12560 error_at (location, "void value not ignored as it ought to be");
12561 return error_mark_node;
12562
12563 case POINTER_TYPE:
12564 if (reject_gcc_builtin (expr))
12565 return error_mark_node;
12566 break;
12567
12568 case FUNCTION_TYPE:
12569 gcc_unreachable ();
12570
12571 case VECTOR_TYPE:
12572 error_at (location, "used vector type where scalar is required");
12573 return error_mark_node;
12574
12575 default:
12576 break;
12577 }
12578
12579 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12580 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12581 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12582 {
12583 expr = remove_c_maybe_const_expr (expr);
12584 expr = build2 (NE_EXPR, integer_type_node, expr,
12585 convert (TREE_TYPE (expr), integer_zero_node));
12586 expr = note_integer_operands (expr);
12587 }
12588 else
12589 /* ??? Should we also give an error for vectors rather than leaving
12590 those to give errors later? */
12591 expr = c_common_truthvalue_conversion (location, expr);
12592
12593 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12594 {
12595 if (TREE_OVERFLOW (expr))
12596 return expr;
12597 else
12598 return note_integer_operands (expr);
12599 }
12600 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12601 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12602 return expr;
12603 }
12604 \f
12605
12606 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12607 required. */
12608
12609 tree
12610 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12611 {
12612 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12613 {
12614 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12615 /* Executing a compound literal inside a function reinitializes
12616 it. */
12617 if (!TREE_STATIC (decl))
12618 *se = true;
12619 return decl;
12620 }
12621 else
12622 return expr;
12623 }
12624 \f
12625 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12626 statement. LOC is the location of the construct. */
12627
12628 tree
12629 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12630 tree clauses)
12631 {
12632 body = c_end_compound_stmt (loc, body, true);
12633
12634 tree stmt = make_node (code);
12635 TREE_TYPE (stmt) = void_type_node;
12636 OMP_BODY (stmt) = body;
12637 OMP_CLAUSES (stmt) = clauses;
12638 SET_EXPR_LOCATION (stmt, loc);
12639
12640 return add_stmt (stmt);
12641 }
12642
12643 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12644 statement. LOC is the location of the OACC_DATA. */
12645
12646 tree
12647 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12648 {
12649 tree stmt;
12650
12651 block = c_end_compound_stmt (loc, block, true);
12652
12653 stmt = make_node (OACC_DATA);
12654 TREE_TYPE (stmt) = void_type_node;
12655 OACC_DATA_CLAUSES (stmt) = clauses;
12656 OACC_DATA_BODY (stmt) = block;
12657 SET_EXPR_LOCATION (stmt, loc);
12658
12659 return add_stmt (stmt);
12660 }
12661
12662 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12663 statement. LOC is the location of the OACC_HOST_DATA. */
12664
12665 tree
12666 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12667 {
12668 tree stmt;
12669
12670 block = c_end_compound_stmt (loc, block, true);
12671
12672 stmt = make_node (OACC_HOST_DATA);
12673 TREE_TYPE (stmt) = void_type_node;
12674 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12675 OACC_HOST_DATA_BODY (stmt) = block;
12676 SET_EXPR_LOCATION (stmt, loc);
12677
12678 return add_stmt (stmt);
12679 }
12680
12681 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12682
12683 tree
12684 c_begin_omp_parallel (void)
12685 {
12686 tree block;
12687
12688 keep_next_level ();
12689 block = c_begin_compound_stmt (true);
12690
12691 return block;
12692 }
12693
12694 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12695 statement. LOC is the location of the OMP_PARALLEL. */
12696
12697 tree
12698 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12699 {
12700 tree stmt;
12701
12702 block = c_end_compound_stmt (loc, block, true);
12703
12704 stmt = make_node (OMP_PARALLEL);
12705 TREE_TYPE (stmt) = void_type_node;
12706 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12707 OMP_PARALLEL_BODY (stmt) = block;
12708 SET_EXPR_LOCATION (stmt, loc);
12709
12710 return add_stmt (stmt);
12711 }
12712
12713 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12714
12715 tree
12716 c_begin_omp_task (void)
12717 {
12718 tree block;
12719
12720 keep_next_level ();
12721 block = c_begin_compound_stmt (true);
12722
12723 return block;
12724 }
12725
12726 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12727 statement. LOC is the location of the #pragma. */
12728
12729 tree
12730 c_finish_omp_task (location_t loc, tree clauses, tree block)
12731 {
12732 tree stmt;
12733
12734 block = c_end_compound_stmt (loc, block, true);
12735
12736 stmt = make_node (OMP_TASK);
12737 TREE_TYPE (stmt) = void_type_node;
12738 OMP_TASK_CLAUSES (stmt) = clauses;
12739 OMP_TASK_BODY (stmt) = block;
12740 SET_EXPR_LOCATION (stmt, loc);
12741
12742 return add_stmt (stmt);
12743 }
12744
12745 /* Generate GOMP_cancel call for #pragma omp cancel. */
12746
12747 void
12748 c_finish_omp_cancel (location_t loc, tree clauses)
12749 {
12750 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12751 int mask = 0;
12752 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12753 mask = 1;
12754 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12755 mask = 2;
12756 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12757 mask = 4;
12758 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12759 mask = 8;
12760 else
12761 {
12762 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12763 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12764 "clauses");
12765 return;
12766 }
12767 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12768 if (ifc != NULL_TREE)
12769 {
12770 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
12771 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
12772 error_at (OMP_CLAUSE_LOCATION (ifc),
12773 "expected %<cancel%> %<if%> clause modifier");
12774 else
12775 {
12776 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
12777 if (ifc2 != NULL_TREE)
12778 {
12779 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
12780 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
12781 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
12782 error_at (OMP_CLAUSE_LOCATION (ifc2),
12783 "expected %<cancel%> %<if%> clause modifier");
12784 }
12785 }
12786
12787 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12788 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12789 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12790 build_zero_cst (type));
12791 }
12792 else
12793 ifc = boolean_true_node;
12794 tree stmt = build_call_expr_loc (loc, fn, 2,
12795 build_int_cst (integer_type_node, mask),
12796 ifc);
12797 add_stmt (stmt);
12798 }
12799
12800 /* Generate GOMP_cancellation_point call for
12801 #pragma omp cancellation point. */
12802
12803 void
12804 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12805 {
12806 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12807 int mask = 0;
12808 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12809 mask = 1;
12810 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12811 mask = 2;
12812 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12813 mask = 4;
12814 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12815 mask = 8;
12816 else
12817 {
12818 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12819 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12820 "clauses");
12821 return;
12822 }
12823 tree stmt = build_call_expr_loc (loc, fn, 1,
12824 build_int_cst (integer_type_node, mask));
12825 add_stmt (stmt);
12826 }
12827
12828 /* Helper function for handle_omp_array_sections. Called recursively
12829 to handle multiple array-section-subscripts. C is the clause,
12830 T current expression (initially OMP_CLAUSE_DECL), which is either
12831 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12832 expression if specified, TREE_VALUE length expression if specified,
12833 TREE_CHAIN is what it has been specified after, or some decl.
12834 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12835 set to true if any of the array-section-subscript could have length
12836 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12837 first array-section-subscript which is known not to have length
12838 of one. Given say:
12839 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12840 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12841 all are or may have length of 1, array-section-subscript [:2] is the
12842 first one known not to have length 1. For array-section-subscript
12843 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12844 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12845 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12846 case though, as some lengths could be zero. */
12847
12848 static tree
12849 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12850 bool &maybe_zero_len, unsigned int &first_non_one,
12851 enum c_omp_region_type ort)
12852 {
12853 tree ret, low_bound, length, type;
12854 if (TREE_CODE (t) != TREE_LIST)
12855 {
12856 if (error_operand_p (t))
12857 return error_mark_node;
12858 ret = t;
12859 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12860 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12861 {
12862 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12863 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12864 return error_mark_node;
12865 }
12866 if (TREE_CODE (t) == COMPONENT_REF
12867 && ort == C_ORT_OMP
12868 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12869 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12870 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12871 {
12872 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12873 {
12874 error_at (OMP_CLAUSE_LOCATION (c),
12875 "bit-field %qE in %qs clause",
12876 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12877 return error_mark_node;
12878 }
12879 while (TREE_CODE (t) == COMPONENT_REF)
12880 {
12881 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12882 {
12883 error_at (OMP_CLAUSE_LOCATION (c),
12884 "%qE is a member of a union", t);
12885 return error_mark_node;
12886 }
12887 t = TREE_OPERAND (t, 0);
12888 }
12889 }
12890 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12891 {
12892 if (DECL_P (t))
12893 error_at (OMP_CLAUSE_LOCATION (c),
12894 "%qD is not a variable in %qs clause", t,
12895 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12896 else
12897 error_at (OMP_CLAUSE_LOCATION (c),
12898 "%qE is not a variable in %qs clause", t,
12899 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 && TYPE_ATOMIC (TREE_TYPE (t)))
12904 {
12905 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12906 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12907 return error_mark_node;
12908 }
12909 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12910 && VAR_P (t)
12911 && DECL_THREAD_LOCAL_P (t))
12912 {
12913 error_at (OMP_CLAUSE_LOCATION (c),
12914 "%qD is threadprivate variable in %qs clause", t,
12915 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12916 return error_mark_node;
12917 }
12918 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12919 && TYPE_ATOMIC (TREE_TYPE (t))
12920 && POINTER_TYPE_P (TREE_TYPE (t)))
12921 {
12922 /* If the array section is pointer based and the pointer
12923 itself is _Atomic qualified, we need to atomically load
12924 the pointer. */
12925 c_expr expr;
12926 memset (&expr, 0, sizeof (expr));
12927 expr.value = ret;
12928 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12929 expr, false, false);
12930 ret = expr.value;
12931 }
12932 return ret;
12933 }
12934
12935 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12936 maybe_zero_len, first_non_one, ort);
12937 if (ret == error_mark_node || ret == NULL_TREE)
12938 return ret;
12939
12940 type = TREE_TYPE (ret);
12941 low_bound = TREE_PURPOSE (t);
12942 length = TREE_VALUE (t);
12943
12944 if (low_bound == error_mark_node || length == error_mark_node)
12945 return error_mark_node;
12946
12947 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12948 {
12949 error_at (OMP_CLAUSE_LOCATION (c),
12950 "low bound %qE of array section does not have integral type",
12951 low_bound);
12952 return error_mark_node;
12953 }
12954 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12955 {
12956 error_at (OMP_CLAUSE_LOCATION (c),
12957 "length %qE of array section does not have integral type",
12958 length);
12959 return error_mark_node;
12960 }
12961 if (low_bound
12962 && TREE_CODE (low_bound) == INTEGER_CST
12963 && TYPE_PRECISION (TREE_TYPE (low_bound))
12964 > TYPE_PRECISION (sizetype))
12965 low_bound = fold_convert (sizetype, low_bound);
12966 if (length
12967 && TREE_CODE (length) == INTEGER_CST
12968 && TYPE_PRECISION (TREE_TYPE (length))
12969 > TYPE_PRECISION (sizetype))
12970 length = fold_convert (sizetype, length);
12971 if (low_bound == NULL_TREE)
12972 low_bound = integer_zero_node;
12973
12974 if (length != NULL_TREE)
12975 {
12976 if (!integer_nonzerop (length))
12977 {
12978 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12979 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12980 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
12981 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
12982 {
12983 if (integer_zerop (length))
12984 {
12985 error_at (OMP_CLAUSE_LOCATION (c),
12986 "zero length array section in %qs clause",
12987 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12988 return error_mark_node;
12989 }
12990 }
12991 else
12992 maybe_zero_len = true;
12993 }
12994 if (first_non_one == types.length ()
12995 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12996 first_non_one++;
12997 }
12998 if (TREE_CODE (type) == ARRAY_TYPE)
12999 {
13000 if (length == NULL_TREE
13001 && (TYPE_DOMAIN (type) == NULL_TREE
13002 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13003 {
13004 error_at (OMP_CLAUSE_LOCATION (c),
13005 "for unknown bound array type length expression must "
13006 "be specified");
13007 return error_mark_node;
13008 }
13009 if (TREE_CODE (low_bound) == INTEGER_CST
13010 && tree_int_cst_sgn (low_bound) == -1)
13011 {
13012 error_at (OMP_CLAUSE_LOCATION (c),
13013 "negative low bound in array section in %qs clause",
13014 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13015 return error_mark_node;
13016 }
13017 if (length != NULL_TREE
13018 && TREE_CODE (length) == INTEGER_CST
13019 && tree_int_cst_sgn (length) == -1)
13020 {
13021 error_at (OMP_CLAUSE_LOCATION (c),
13022 "negative length in array section in %qs clause",
13023 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13024 return error_mark_node;
13025 }
13026 if (TYPE_DOMAIN (type)
13027 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13028 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13029 == INTEGER_CST)
13030 {
13031 tree size
13032 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13033 size = size_binop (PLUS_EXPR, size, size_one_node);
13034 if (TREE_CODE (low_bound) == INTEGER_CST)
13035 {
13036 if (tree_int_cst_lt (size, low_bound))
13037 {
13038 error_at (OMP_CLAUSE_LOCATION (c),
13039 "low bound %qE above array section size "
13040 "in %qs clause", low_bound,
13041 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13042 return error_mark_node;
13043 }
13044 if (tree_int_cst_equal (size, low_bound))
13045 {
13046 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13047 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13048 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13049 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13050 {
13051 error_at (OMP_CLAUSE_LOCATION (c),
13052 "zero length array section in %qs clause",
13053 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13054 return error_mark_node;
13055 }
13056 maybe_zero_len = true;
13057 }
13058 else if (length == NULL_TREE
13059 && first_non_one == types.length ()
13060 && tree_int_cst_equal
13061 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13062 low_bound))
13063 first_non_one++;
13064 }
13065 else if (length == NULL_TREE)
13066 {
13067 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13068 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13069 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13070 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13071 maybe_zero_len = true;
13072 if (first_non_one == types.length ())
13073 first_non_one++;
13074 }
13075 if (length && TREE_CODE (length) == INTEGER_CST)
13076 {
13077 if (tree_int_cst_lt (size, length))
13078 {
13079 error_at (OMP_CLAUSE_LOCATION (c),
13080 "length %qE above array section size "
13081 "in %qs clause", length,
13082 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13083 return error_mark_node;
13084 }
13085 if (TREE_CODE (low_bound) == INTEGER_CST)
13086 {
13087 tree lbpluslen
13088 = size_binop (PLUS_EXPR,
13089 fold_convert (sizetype, low_bound),
13090 fold_convert (sizetype, length));
13091 if (TREE_CODE (lbpluslen) == INTEGER_CST
13092 && tree_int_cst_lt (size, lbpluslen))
13093 {
13094 error_at (OMP_CLAUSE_LOCATION (c),
13095 "high bound %qE above array section size "
13096 "in %qs clause", lbpluslen,
13097 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13098 return error_mark_node;
13099 }
13100 }
13101 }
13102 }
13103 else if (length == NULL_TREE)
13104 {
13105 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13106 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13107 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13108 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13109 maybe_zero_len = true;
13110 if (first_non_one == types.length ())
13111 first_non_one++;
13112 }
13113
13114 /* For [lb:] we will need to evaluate lb more than once. */
13115 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13116 {
13117 tree lb = save_expr (low_bound);
13118 if (lb != low_bound)
13119 {
13120 TREE_PURPOSE (t) = lb;
13121 low_bound = lb;
13122 }
13123 }
13124 }
13125 else if (TREE_CODE (type) == POINTER_TYPE)
13126 {
13127 if (length == NULL_TREE)
13128 {
13129 error_at (OMP_CLAUSE_LOCATION (c),
13130 "for pointer type length expression must be specified");
13131 return error_mark_node;
13132 }
13133 if (length != NULL_TREE
13134 && TREE_CODE (length) == INTEGER_CST
13135 && tree_int_cst_sgn (length) == -1)
13136 {
13137 error_at (OMP_CLAUSE_LOCATION (c),
13138 "negative length in array section in %qs clause",
13139 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13140 return error_mark_node;
13141 }
13142 /* If there is a pointer type anywhere but in the very first
13143 array-section-subscript, the array section can't be contiguous. */
13144 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13145 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13146 {
13147 error_at (OMP_CLAUSE_LOCATION (c),
13148 "array section is not contiguous in %qs clause",
13149 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13150 return error_mark_node;
13151 }
13152 }
13153 else
13154 {
13155 error_at (OMP_CLAUSE_LOCATION (c),
13156 "%qE does not have pointer or array type", ret);
13157 return error_mark_node;
13158 }
13159 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13160 types.safe_push (TREE_TYPE (ret));
13161 /* We will need to evaluate lb more than once. */
13162 tree lb = save_expr (low_bound);
13163 if (lb != low_bound)
13164 {
13165 TREE_PURPOSE (t) = lb;
13166 low_bound = lb;
13167 }
13168 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13169 return ret;
13170 }
13171
13172 /* Handle array sections for clause C. */
13173
13174 static bool
13175 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13176 {
13177 bool maybe_zero_len = false;
13178 unsigned int first_non_one = 0;
13179 auto_vec<tree, 10> types;
13180 tree *tp = &OMP_CLAUSE_DECL (c);
13181 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13182 && TREE_CODE (*tp) == TREE_LIST
13183 && TREE_PURPOSE (*tp)
13184 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13185 tp = &TREE_VALUE (*tp);
13186 tree first = handle_omp_array_sections_1 (c, *tp, types,
13187 maybe_zero_len, first_non_one,
13188 ort);
13189 if (first == error_mark_node)
13190 return true;
13191 if (first == NULL_TREE)
13192 return false;
13193 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13194 {
13195 tree t = *tp;
13196 tree tem = NULL_TREE;
13197 /* Need to evaluate side effects in the length expressions
13198 if any. */
13199 while (TREE_CODE (t) == TREE_LIST)
13200 {
13201 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13202 {
13203 if (tem == NULL_TREE)
13204 tem = TREE_VALUE (t);
13205 else
13206 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13207 TREE_VALUE (t), tem);
13208 }
13209 t = TREE_CHAIN (t);
13210 }
13211 if (tem)
13212 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13213 first = c_fully_fold (first, false, NULL, true);
13214 *tp = first;
13215 }
13216 else
13217 {
13218 unsigned int num = types.length (), i;
13219 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13220 tree condition = NULL_TREE;
13221
13222 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13223 maybe_zero_len = true;
13224
13225 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13226 t = TREE_CHAIN (t))
13227 {
13228 tree low_bound = TREE_PURPOSE (t);
13229 tree length = TREE_VALUE (t);
13230
13231 i--;
13232 if (low_bound
13233 && TREE_CODE (low_bound) == INTEGER_CST
13234 && TYPE_PRECISION (TREE_TYPE (low_bound))
13235 > TYPE_PRECISION (sizetype))
13236 low_bound = fold_convert (sizetype, low_bound);
13237 if (length
13238 && TREE_CODE (length) == INTEGER_CST
13239 && TYPE_PRECISION (TREE_TYPE (length))
13240 > TYPE_PRECISION (sizetype))
13241 length = fold_convert (sizetype, length);
13242 if (low_bound == NULL_TREE)
13243 low_bound = integer_zero_node;
13244 if (!maybe_zero_len && i > first_non_one)
13245 {
13246 if (integer_nonzerop (low_bound))
13247 goto do_warn_noncontiguous;
13248 if (length != NULL_TREE
13249 && TREE_CODE (length) == INTEGER_CST
13250 && TYPE_DOMAIN (types[i])
13251 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13252 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13253 == INTEGER_CST)
13254 {
13255 tree size;
13256 size = size_binop (PLUS_EXPR,
13257 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13258 size_one_node);
13259 if (!tree_int_cst_equal (length, size))
13260 {
13261 do_warn_noncontiguous:
13262 error_at (OMP_CLAUSE_LOCATION (c),
13263 "array section is not contiguous in %qs "
13264 "clause",
13265 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13266 return true;
13267 }
13268 }
13269 if (length != NULL_TREE
13270 && TREE_SIDE_EFFECTS (length))
13271 {
13272 if (side_effects == NULL_TREE)
13273 side_effects = length;
13274 else
13275 side_effects = build2 (COMPOUND_EXPR,
13276 TREE_TYPE (side_effects),
13277 length, side_effects);
13278 }
13279 }
13280 else
13281 {
13282 tree l;
13283
13284 if (i > first_non_one
13285 && ((length && integer_nonzerop (length))
13286 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13287 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13288 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13289 continue;
13290 if (length)
13291 l = fold_convert (sizetype, length);
13292 else
13293 {
13294 l = size_binop (PLUS_EXPR,
13295 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13296 size_one_node);
13297 l = size_binop (MINUS_EXPR, l,
13298 fold_convert (sizetype, low_bound));
13299 }
13300 if (i > first_non_one)
13301 {
13302 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13303 size_zero_node);
13304 if (condition == NULL_TREE)
13305 condition = l;
13306 else
13307 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13308 l, condition);
13309 }
13310 else if (size == NULL_TREE)
13311 {
13312 size = size_in_bytes (TREE_TYPE (types[i]));
13313 tree eltype = TREE_TYPE (types[num - 1]);
13314 while (TREE_CODE (eltype) == ARRAY_TYPE)
13315 eltype = TREE_TYPE (eltype);
13316 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13317 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13318 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13319 {
13320 if (integer_zerop (size)
13321 || integer_zerop (size_in_bytes (eltype)))
13322 {
13323 error_at (OMP_CLAUSE_LOCATION (c),
13324 "zero length array section in %qs clause",
13325 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13326 return error_mark_node;
13327 }
13328 size = size_binop (EXACT_DIV_EXPR, size,
13329 size_in_bytes (eltype));
13330 }
13331 size = size_binop (MULT_EXPR, size, l);
13332 if (condition)
13333 size = fold_build3 (COND_EXPR, sizetype, condition,
13334 size, size_zero_node);
13335 }
13336 else
13337 size = size_binop (MULT_EXPR, size, l);
13338 }
13339 }
13340 if (side_effects)
13341 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13342 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13343 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13344 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13345 {
13346 size = size_binop (MINUS_EXPR, size, size_one_node);
13347 size = c_fully_fold (size, false, NULL);
13348 size = save_expr (size);
13349 tree index_type = build_index_type (size);
13350 tree eltype = TREE_TYPE (first);
13351 while (TREE_CODE (eltype) == ARRAY_TYPE)
13352 eltype = TREE_TYPE (eltype);
13353 tree type = build_array_type (eltype, index_type);
13354 tree ptype = build_pointer_type (eltype);
13355 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13356 t = build_fold_addr_expr (t);
13357 tree t2 = build_fold_addr_expr (first);
13358 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13359 ptrdiff_type_node, t2);
13360 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13361 ptrdiff_type_node, t2,
13362 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13363 ptrdiff_type_node, t));
13364 t2 = c_fully_fold (t2, false, NULL);
13365 if (tree_fits_shwi_p (t2))
13366 t = build2 (MEM_REF, type, t,
13367 build_int_cst (ptype, tree_to_shwi (t2)));
13368 else
13369 {
13370 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13371 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13372 TREE_TYPE (t), t, t2);
13373 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13374 }
13375 OMP_CLAUSE_DECL (c) = t;
13376 return false;
13377 }
13378 first = c_fully_fold (first, false, NULL);
13379 OMP_CLAUSE_DECL (c) = first;
13380 if (size)
13381 size = c_fully_fold (size, false, NULL);
13382 OMP_CLAUSE_SIZE (c) = size;
13383 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13384 || (TREE_CODE (t) == COMPONENT_REF
13385 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13386 return false;
13387 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13388 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
13389 switch (OMP_CLAUSE_MAP_KIND (c))
13390 {
13391 case GOMP_MAP_ALLOC:
13392 case GOMP_MAP_TO:
13393 case GOMP_MAP_FROM:
13394 case GOMP_MAP_TOFROM:
13395 case GOMP_MAP_ALWAYS_TO:
13396 case GOMP_MAP_ALWAYS_FROM:
13397 case GOMP_MAP_ALWAYS_TOFROM:
13398 case GOMP_MAP_RELEASE:
13399 case GOMP_MAP_DELETE:
13400 case GOMP_MAP_FORCE_TO:
13401 case GOMP_MAP_FORCE_FROM:
13402 case GOMP_MAP_FORCE_TOFROM:
13403 case GOMP_MAP_FORCE_PRESENT:
13404 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13405 break;
13406 default:
13407 break;
13408 }
13409 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13410 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13411 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13412 else if (TREE_CODE (t) == COMPONENT_REF)
13413 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
13414 else
13415 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13416 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13417 && !c_mark_addressable (t))
13418 return false;
13419 OMP_CLAUSE_DECL (c2) = t;
13420 t = build_fold_addr_expr (first);
13421 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13422 tree ptr = OMP_CLAUSE_DECL (c2);
13423 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13424 ptr = build_fold_addr_expr (ptr);
13425 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13426 ptrdiff_type_node, t,
13427 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13428 ptrdiff_type_node, ptr));
13429 t = c_fully_fold (t, false, NULL);
13430 OMP_CLAUSE_SIZE (c2) = t;
13431 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13432 OMP_CLAUSE_CHAIN (c) = c2;
13433 }
13434 return false;
13435 }
13436
13437 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13438 an inline call. But, remap
13439 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13440 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13441
13442 static tree
13443 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13444 tree decl, tree placeholder)
13445 {
13446 copy_body_data id;
13447 hash_map<tree, tree> decl_map;
13448
13449 decl_map.put (omp_decl1, placeholder);
13450 decl_map.put (omp_decl2, decl);
13451 memset (&id, 0, sizeof (id));
13452 id.src_fn = DECL_CONTEXT (omp_decl1);
13453 id.dst_fn = current_function_decl;
13454 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13455 id.decl_map = &decl_map;
13456
13457 id.copy_decl = copy_decl_no_change;
13458 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13459 id.transform_new_cfg = true;
13460 id.transform_return_to_modify = false;
13461 id.transform_lang_insert_block = NULL;
13462 id.eh_lp_nr = 0;
13463 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13464 return stmt;
13465 }
13466
13467 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13468 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13469
13470 static tree
13471 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13472 {
13473 if (*tp == (tree) data)
13474 return *tp;
13475 return NULL_TREE;
13476 }
13477
13478 /* Similarly, but also walk aggregate fields. */
13479
13480 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13481
13482 static tree
13483 c_find_omp_var_r (tree *tp, int *, void *data)
13484 {
13485 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13486 return *tp;
13487 if (RECORD_OR_UNION_TYPE_P (*tp))
13488 {
13489 tree field;
13490 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13491
13492 for (field = TYPE_FIELDS (*tp); field;
13493 field = DECL_CHAIN (field))
13494 if (TREE_CODE (field) == FIELD_DECL)
13495 {
13496 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13497 c_find_omp_var_r, data, pset);
13498 if (ret)
13499 return ret;
13500 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13501 if (ret)
13502 return ret;
13503 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13504 pset);
13505 if (ret)
13506 return ret;
13507 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13508 if (ret)
13509 return ret;
13510 }
13511 }
13512 else if (INTEGRAL_TYPE_P (*tp))
13513 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13514 ((struct c_find_omp_var_s *) data)->pset);
13515 return NULL_TREE;
13516 }
13517
13518 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13519 and clauses containing them should be removed. */
13520
13521 static bool
13522 c_omp_finish_iterators (tree iter)
13523 {
13524 bool ret = false;
13525 for (tree it = iter; it; it = TREE_CHAIN (it))
13526 {
13527 tree var = TREE_VEC_ELT (it, 0);
13528 tree begin = TREE_VEC_ELT (it, 1);
13529 tree end = TREE_VEC_ELT (it, 2);
13530 tree step = TREE_VEC_ELT (it, 3);
13531 tree orig_step;
13532 tree type = TREE_TYPE (var);
13533 location_t loc = DECL_SOURCE_LOCATION (var);
13534 if (type == error_mark_node)
13535 {
13536 ret = true;
13537 continue;
13538 }
13539 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13540 {
13541 error_at (loc, "iterator %qD has neither integral nor pointer type",
13542 var);
13543 ret = true;
13544 continue;
13545 }
13546 else if (TYPE_ATOMIC (type))
13547 {
13548 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13549 ret = true;
13550 continue;
13551 }
13552 else if (TYPE_READONLY (type))
13553 {
13554 error_at (loc, "iterator %qD has const qualified type", var);
13555 ret = true;
13556 continue;
13557 }
13558 else if (step == error_mark_node
13559 || TREE_TYPE (step) == error_mark_node)
13560 {
13561 ret = true;
13562 continue;
13563 }
13564 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13565 {
13566 error_at (EXPR_LOC_OR_LOC (step, loc),
13567 "iterator step with non-integral type");
13568 ret = true;
13569 continue;
13570 }
13571 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13572 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13573 orig_step = save_expr (c_fully_fold (step, false, NULL));
13574 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13575 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13576 if (POINTER_TYPE_P (type))
13577 {
13578 begin = save_expr (begin);
13579 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13580 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13581 fold_convert (sizetype, step),
13582 fold_convert (sizetype, begin));
13583 step = fold_convert (ssizetype, step);
13584 }
13585 if (integer_zerop (step))
13586 {
13587 error_at (loc, "iterator %qD has zero step", var);
13588 ret = true;
13589 continue;
13590 }
13591
13592 if (begin == error_mark_node
13593 || end == error_mark_node
13594 || step == error_mark_node
13595 || orig_step == error_mark_node)
13596 {
13597 ret = true;
13598 continue;
13599 }
13600 hash_set<tree> pset;
13601 tree it2;
13602 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13603 {
13604 tree var2 = TREE_VEC_ELT (it2, 0);
13605 tree begin2 = TREE_VEC_ELT (it2, 1);
13606 tree end2 = TREE_VEC_ELT (it2, 2);
13607 tree step2 = TREE_VEC_ELT (it2, 3);
13608 tree type2 = TREE_TYPE (var2);
13609 location_t loc2 = DECL_SOURCE_LOCATION (var2);
13610 struct c_find_omp_var_s data = { var, &pset };
13611 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13612 {
13613 error_at (loc2,
13614 "type of iterator %qD refers to outer iterator %qD",
13615 var2, var);
13616 break;
13617 }
13618 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13619 {
13620 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13621 "begin expression refers to outer iterator %qD", var);
13622 break;
13623 }
13624 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13625 {
13626 error_at (EXPR_LOC_OR_LOC (end2, loc2),
13627 "end expression refers to outer iterator %qD", var);
13628 break;
13629 }
13630 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13631 {
13632 error_at (EXPR_LOC_OR_LOC (step2, loc2),
13633 "step expression refers to outer iterator %qD", var);
13634 break;
13635 }
13636 }
13637 if (it2)
13638 {
13639 ret = true;
13640 continue;
13641 }
13642 TREE_VEC_ELT (it, 1) = begin;
13643 TREE_VEC_ELT (it, 2) = end;
13644 TREE_VEC_ELT (it, 3) = step;
13645 TREE_VEC_ELT (it, 4) = orig_step;
13646 }
13647 return ret;
13648 }
13649
13650 /* For all elements of CLAUSES, validate them against their constraints.
13651 Remove any elements from the list that are invalid. */
13652
13653 tree
13654 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13655 {
13656 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13657 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
13658 tree c, t, type, *pc;
13659 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13660 bool branch_seen = false;
13661 bool copyprivate_seen = false;
13662 bool linear_variable_step_check = false;
13663 tree *nowait_clause = NULL;
13664 tree ordered_clause = NULL_TREE;
13665 tree schedule_clause = NULL_TREE;
13666 bool oacc_async = false;
13667 tree last_iterators = NULL_TREE;
13668 bool last_iterators_remove = false;
13669 tree *nogroup_seen = NULL;
13670 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
13671 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
13672 int reduction_seen = 0;
13673
13674 bitmap_obstack_initialize (NULL);
13675 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13676 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13677 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13678 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13679 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
13680 bitmap_initialize (&map_head, &bitmap_default_obstack);
13681 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13682 /* If ort == C_ORT_OMP used as nontemporal_head instead. */
13683 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13684
13685 if (ort & C_ORT_ACC)
13686 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13687 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13688 {
13689 oacc_async = true;
13690 break;
13691 }
13692
13693 for (pc = &clauses, c = clauses; c ; c = *pc)
13694 {
13695 bool remove = false;
13696 bool need_complete = false;
13697 bool need_implicitly_determined = false;
13698
13699 switch (OMP_CLAUSE_CODE (c))
13700 {
13701 case OMP_CLAUSE_SHARED:
13702 need_implicitly_determined = true;
13703 goto check_dup_generic;
13704
13705 case OMP_CLAUSE_PRIVATE:
13706 need_complete = true;
13707 need_implicitly_determined = true;
13708 goto check_dup_generic;
13709
13710 case OMP_CLAUSE_REDUCTION:
13711 if (reduction_seen == 0)
13712 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
13713 else if (reduction_seen != -2
13714 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
13715 ? -1 : 1))
13716 {
13717 error_at (OMP_CLAUSE_LOCATION (c),
13718 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
13719 "on the same construct");
13720 reduction_seen = -2;
13721 }
13722 /* FALLTHRU */
13723 case OMP_CLAUSE_IN_REDUCTION:
13724 case OMP_CLAUSE_TASK_REDUCTION:
13725 need_implicitly_determined = true;
13726 t = OMP_CLAUSE_DECL (c);
13727 if (TREE_CODE (t) == TREE_LIST)
13728 {
13729 if (handle_omp_array_sections (c, ort))
13730 {
13731 remove = true;
13732 break;
13733 }
13734
13735 t = OMP_CLAUSE_DECL (c);
13736 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13737 && OMP_CLAUSE_REDUCTION_INSCAN (c))
13738 {
13739 error_at (OMP_CLAUSE_LOCATION (c),
13740 "%<inscan%> %<reduction%> clause with array "
13741 "section");
13742 remove = true;
13743 break;
13744 }
13745 }
13746 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13747 if (t == error_mark_node)
13748 {
13749 remove = true;
13750 break;
13751 }
13752 if (oacc_async)
13753 c_mark_addressable (t);
13754 type = TREE_TYPE (t);
13755 if (TREE_CODE (t) == MEM_REF)
13756 type = TREE_TYPE (type);
13757 if (TREE_CODE (type) == ARRAY_TYPE)
13758 {
13759 tree oatype = type;
13760 gcc_assert (TREE_CODE (t) != MEM_REF);
13761 while (TREE_CODE (type) == ARRAY_TYPE)
13762 type = TREE_TYPE (type);
13763 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13764 {
13765 error_at (OMP_CLAUSE_LOCATION (c),
13766 "%qD in %<reduction%> clause is a zero size array",
13767 t);
13768 remove = true;
13769 break;
13770 }
13771 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13772 TYPE_SIZE_UNIT (type));
13773 if (integer_zerop (size))
13774 {
13775 error_at (OMP_CLAUSE_LOCATION (c),
13776 "%qD in %<reduction%> clause is a zero size array",
13777 t);
13778 remove = true;
13779 break;
13780 }
13781 size = size_binop (MINUS_EXPR, size, size_one_node);
13782 size = save_expr (size);
13783 tree index_type = build_index_type (size);
13784 tree atype = build_array_type (type, index_type);
13785 tree ptype = build_pointer_type (type);
13786 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13787 t = build_fold_addr_expr (t);
13788 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13789 OMP_CLAUSE_DECL (c) = t;
13790 }
13791 if (TYPE_ATOMIC (type))
13792 {
13793 error_at (OMP_CLAUSE_LOCATION (c),
13794 "%<_Atomic%> %qE in %<reduction%> clause", t);
13795 remove = true;
13796 break;
13797 }
13798 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13799 || OMP_CLAUSE_REDUCTION_TASK (c))
13800 {
13801 /* Disallow zero sized or potentially zero sized task
13802 reductions. */
13803 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13804 {
13805 error_at (OMP_CLAUSE_LOCATION (c),
13806 "zero sized type %qT in %qs clause", type,
13807 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13808 remove = true;
13809 break;
13810 }
13811 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
13812 {
13813 error_at (OMP_CLAUSE_LOCATION (c),
13814 "variable sized type %qT in %qs clause", type,
13815 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13816 remove = true;
13817 break;
13818 }
13819 }
13820 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13821 && (FLOAT_TYPE_P (type)
13822 || TREE_CODE (type) == COMPLEX_TYPE))
13823 {
13824 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13825 const char *r_name = NULL;
13826
13827 switch (r_code)
13828 {
13829 case PLUS_EXPR:
13830 case MULT_EXPR:
13831 case MINUS_EXPR:
13832 break;
13833 case MIN_EXPR:
13834 if (TREE_CODE (type) == COMPLEX_TYPE)
13835 r_name = "min";
13836 break;
13837 case MAX_EXPR:
13838 if (TREE_CODE (type) == COMPLEX_TYPE)
13839 r_name = "max";
13840 break;
13841 case BIT_AND_EXPR:
13842 r_name = "&";
13843 break;
13844 case BIT_XOR_EXPR:
13845 r_name = "^";
13846 break;
13847 case BIT_IOR_EXPR:
13848 r_name = "|";
13849 break;
13850 case TRUTH_ANDIF_EXPR:
13851 if (FLOAT_TYPE_P (type))
13852 r_name = "&&";
13853 break;
13854 case TRUTH_ORIF_EXPR:
13855 if (FLOAT_TYPE_P (type))
13856 r_name = "||";
13857 break;
13858 default:
13859 gcc_unreachable ();
13860 }
13861 if (r_name)
13862 {
13863 error_at (OMP_CLAUSE_LOCATION (c),
13864 "%qE has invalid type for %<reduction(%s)%>",
13865 t, r_name);
13866 remove = true;
13867 break;
13868 }
13869 }
13870 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13871 {
13872 error_at (OMP_CLAUSE_LOCATION (c),
13873 "user defined reduction not found for %qE", t);
13874 remove = true;
13875 break;
13876 }
13877 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13878 {
13879 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13880 type = TYPE_MAIN_VARIANT (type);
13881 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13882 VAR_DECL, NULL_TREE, type);
13883 tree decl_placeholder = NULL_TREE;
13884 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13885 DECL_ARTIFICIAL (placeholder) = 1;
13886 DECL_IGNORED_P (placeholder) = 1;
13887 if (TREE_CODE (t) == MEM_REF)
13888 {
13889 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13890 VAR_DECL, NULL_TREE, type);
13891 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13892 DECL_ARTIFICIAL (decl_placeholder) = 1;
13893 DECL_IGNORED_P (decl_placeholder) = 1;
13894 }
13895 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13896 c_mark_addressable (placeholder);
13897 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13898 c_mark_addressable (decl_placeholder ? decl_placeholder
13899 : OMP_CLAUSE_DECL (c));
13900 OMP_CLAUSE_REDUCTION_MERGE (c)
13901 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13902 TREE_VEC_ELT (list, 0),
13903 TREE_VEC_ELT (list, 1),
13904 decl_placeholder ? decl_placeholder
13905 : OMP_CLAUSE_DECL (c), placeholder);
13906 OMP_CLAUSE_REDUCTION_MERGE (c)
13907 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13908 void_type_node, NULL_TREE,
13909 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13910 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13911 if (TREE_VEC_LENGTH (list) == 6)
13912 {
13913 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13914 c_mark_addressable (decl_placeholder ? decl_placeholder
13915 : OMP_CLAUSE_DECL (c));
13916 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13917 c_mark_addressable (placeholder);
13918 tree init = TREE_VEC_ELT (list, 5);
13919 if (init == error_mark_node)
13920 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13921 OMP_CLAUSE_REDUCTION_INIT (c)
13922 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13923 TREE_VEC_ELT (list, 3),
13924 decl_placeholder ? decl_placeholder
13925 : OMP_CLAUSE_DECL (c), placeholder);
13926 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13927 {
13928 tree v = decl_placeholder ? decl_placeholder : t;
13929 OMP_CLAUSE_REDUCTION_INIT (c)
13930 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13931 OMP_CLAUSE_REDUCTION_INIT (c));
13932 }
13933 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13934 c_find_omp_placeholder_r,
13935 placeholder, NULL))
13936 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13937 }
13938 else
13939 {
13940 tree init;
13941 tree v = decl_placeholder ? decl_placeholder : t;
13942 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13943 init = build_constructor (TREE_TYPE (v), NULL);
13944 else
13945 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13946 OMP_CLAUSE_REDUCTION_INIT (c)
13947 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13948 }
13949 OMP_CLAUSE_REDUCTION_INIT (c)
13950 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13951 void_type_node, NULL_TREE,
13952 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13953 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13954 }
13955 if (TREE_CODE (t) == MEM_REF)
13956 {
13957 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13958 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13959 != INTEGER_CST)
13960 {
13961 sorry ("variable length element type in array "
13962 "%<reduction%> clause");
13963 remove = true;
13964 break;
13965 }
13966 t = TREE_OPERAND (t, 0);
13967 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13968 t = TREE_OPERAND (t, 0);
13969 if (TREE_CODE (t) == ADDR_EXPR)
13970 t = TREE_OPERAND (t, 0);
13971 }
13972 goto check_dup_generic_t;
13973
13974 case OMP_CLAUSE_COPYPRIVATE:
13975 copyprivate_seen = true;
13976 if (nowait_clause)
13977 {
13978 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13979 "%<nowait%> clause must not be used together "
13980 "with %<copyprivate%>");
13981 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13982 nowait_clause = NULL;
13983 }
13984 goto check_dup_generic;
13985
13986 case OMP_CLAUSE_COPYIN:
13987 t = OMP_CLAUSE_DECL (c);
13988 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13989 {
13990 error_at (OMP_CLAUSE_LOCATION (c),
13991 "%qE must be %<threadprivate%> for %<copyin%>", t);
13992 remove = true;
13993 break;
13994 }
13995 goto check_dup_generic;
13996
13997 case OMP_CLAUSE_LINEAR:
13998 if (ort != C_ORT_OMP_DECLARE_SIMD)
13999 need_implicitly_determined = true;
14000 t = OMP_CLAUSE_DECL (c);
14001 if (ort != C_ORT_OMP_DECLARE_SIMD
14002 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
14003 {
14004 error_at (OMP_CLAUSE_LOCATION (c),
14005 "modifier should not be specified in %<linear%> "
14006 "clause on %<simd%> or %<for%> constructs");
14007 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14008 }
14009 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14010 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14011 {
14012 error_at (OMP_CLAUSE_LOCATION (c),
14013 "linear clause applied to non-integral non-pointer "
14014 "variable with type %qT", TREE_TYPE (t));
14015 remove = true;
14016 break;
14017 }
14018 if (TYPE_ATOMIC (TREE_TYPE (t)))
14019 {
14020 error_at (OMP_CLAUSE_LOCATION (c),
14021 "%<_Atomic%> %qD in %<linear%> clause", t);
14022 remove = true;
14023 break;
14024 }
14025 if (ort == C_ORT_OMP_DECLARE_SIMD)
14026 {
14027 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14028 if (TREE_CODE (s) == PARM_DECL)
14029 {
14030 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14031 /* map_head bitmap is used as uniform_head if
14032 declare_simd. */
14033 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14034 linear_variable_step_check = true;
14035 goto check_dup_generic;
14036 }
14037 if (TREE_CODE (s) != INTEGER_CST)
14038 {
14039 error_at (OMP_CLAUSE_LOCATION (c),
14040 "%<linear%> clause step %qE is neither constant "
14041 "nor a parameter", s);
14042 remove = true;
14043 break;
14044 }
14045 }
14046 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14047 {
14048 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14049 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14050 OMP_CLAUSE_DECL (c), s);
14051 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14052 sizetype, fold_convert (sizetype, s),
14053 fold_convert
14054 (sizetype, OMP_CLAUSE_DECL (c)));
14055 if (s == error_mark_node)
14056 s = size_one_node;
14057 OMP_CLAUSE_LINEAR_STEP (c) = s;
14058 }
14059 else
14060 OMP_CLAUSE_LINEAR_STEP (c)
14061 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14062 goto check_dup_generic;
14063
14064 check_dup_generic:
14065 t = OMP_CLAUSE_DECL (c);
14066 check_dup_generic_t:
14067 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14068 {
14069 error_at (OMP_CLAUSE_LOCATION (c),
14070 "%qE is not a variable in clause %qs", t,
14071 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14072 remove = true;
14073 }
14074 else if (ort == C_ORT_ACC
14075 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14076 {
14077 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14078 {
14079 error_at (OMP_CLAUSE_LOCATION (c),
14080 "%qD appears more than once in reduction clauses",
14081 t);
14082 remove = true;
14083 }
14084 else
14085 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14086 }
14087 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14088 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14089 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14090 {
14091 error_at (OMP_CLAUSE_LOCATION (c),
14092 "%qE appears more than once in data clauses", t);
14093 remove = true;
14094 }
14095 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14096 && bitmap_bit_p (&map_head, DECL_UID (t)))
14097 {
14098 if (ort == C_ORT_ACC)
14099 error_at (OMP_CLAUSE_LOCATION (c),
14100 "%qD appears more than once in data clauses", t);
14101 else
14102 error_at (OMP_CLAUSE_LOCATION (c),
14103 "%qD appears both in data and map clauses", t);
14104 remove = true;
14105 }
14106 else
14107 bitmap_set_bit (&generic_head, DECL_UID (t));
14108 break;
14109
14110 case OMP_CLAUSE_FIRSTPRIVATE:
14111 t = OMP_CLAUSE_DECL (c);
14112 need_complete = true;
14113 need_implicitly_determined = true;
14114 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14115 {
14116 error_at (OMP_CLAUSE_LOCATION (c),
14117 "%qE is not a variable in clause %<firstprivate%>", t);
14118 remove = true;
14119 }
14120 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14121 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14122 {
14123 error_at (OMP_CLAUSE_LOCATION (c),
14124 "%qE appears more than once in data clauses", t);
14125 remove = true;
14126 }
14127 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14128 {
14129 if (ort == C_ORT_ACC)
14130 error_at (OMP_CLAUSE_LOCATION (c),
14131 "%qD appears more than once in data clauses", t);
14132 else
14133 error_at (OMP_CLAUSE_LOCATION (c),
14134 "%qD appears both in data and map clauses", t);
14135 remove = true;
14136 }
14137 else
14138 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14139 break;
14140
14141 case OMP_CLAUSE_LASTPRIVATE:
14142 t = OMP_CLAUSE_DECL (c);
14143 need_complete = true;
14144 need_implicitly_determined = true;
14145 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14146 {
14147 error_at (OMP_CLAUSE_LOCATION (c),
14148 "%qE is not a variable in clause %<lastprivate%>", t);
14149 remove = true;
14150 }
14151 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14152 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14153 {
14154 error_at (OMP_CLAUSE_LOCATION (c),
14155 "%qE appears more than once in data clauses", t);
14156 remove = true;
14157 }
14158 else
14159 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14160 break;
14161
14162 case OMP_CLAUSE_ALIGNED:
14163 t = OMP_CLAUSE_DECL (c);
14164 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14165 {
14166 error_at (OMP_CLAUSE_LOCATION (c),
14167 "%qE is not a variable in %<aligned%> clause", t);
14168 remove = true;
14169 }
14170 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14171 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14172 {
14173 error_at (OMP_CLAUSE_LOCATION (c),
14174 "%qE in %<aligned%> clause is neither a pointer nor "
14175 "an array", t);
14176 remove = true;
14177 }
14178 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14179 {
14180 error_at (OMP_CLAUSE_LOCATION (c),
14181 "%<_Atomic%> %qD in %<aligned%> clause", t);
14182 remove = true;
14183 break;
14184 }
14185 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14186 {
14187 error_at (OMP_CLAUSE_LOCATION (c),
14188 "%qE appears more than once in %<aligned%> clauses",
14189 t);
14190 remove = true;
14191 }
14192 else
14193 bitmap_set_bit (&aligned_head, DECL_UID (t));
14194 break;
14195
14196 case OMP_CLAUSE_NONTEMPORAL:
14197 t = OMP_CLAUSE_DECL (c);
14198 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14199 {
14200 error_at (OMP_CLAUSE_LOCATION (c),
14201 "%qE is not a variable in %<nontemporal%> clause", t);
14202 remove = true;
14203 }
14204 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14205 {
14206 error_at (OMP_CLAUSE_LOCATION (c),
14207 "%qE appears more than once in %<nontemporal%> "
14208 "clauses", t);
14209 remove = true;
14210 }
14211 else
14212 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14213 break;
14214
14215 case OMP_CLAUSE_DEPEND:
14216 t = OMP_CLAUSE_DECL (c);
14217 if (t == NULL_TREE)
14218 {
14219 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14220 == OMP_CLAUSE_DEPEND_SOURCE);
14221 break;
14222 }
14223 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14224 {
14225 gcc_assert (TREE_CODE (t) == TREE_LIST);
14226 for (; t; t = TREE_CHAIN (t))
14227 {
14228 tree decl = TREE_VALUE (t);
14229 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14230 {
14231 tree offset = TREE_PURPOSE (t);
14232 bool neg = wi::neg_p (wi::to_wide (offset));
14233 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14234 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14235 neg ? MINUS_EXPR : PLUS_EXPR,
14236 decl, offset);
14237 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14238 sizetype,
14239 fold_convert (sizetype, t2),
14240 fold_convert (sizetype, decl));
14241 if (t2 == error_mark_node)
14242 {
14243 remove = true;
14244 break;
14245 }
14246 TREE_PURPOSE (t) = t2;
14247 }
14248 }
14249 break;
14250 }
14251 if (TREE_CODE (t) == TREE_LIST
14252 && TREE_PURPOSE (t)
14253 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14254 {
14255 if (TREE_PURPOSE (t) != last_iterators)
14256 last_iterators_remove
14257 = c_omp_finish_iterators (TREE_PURPOSE (t));
14258 last_iterators = TREE_PURPOSE (t);
14259 t = TREE_VALUE (t);
14260 if (last_iterators_remove)
14261 t = error_mark_node;
14262 }
14263 else
14264 last_iterators = NULL_TREE;
14265 if (TREE_CODE (t) == TREE_LIST)
14266 {
14267 if (handle_omp_array_sections (c, ort))
14268 remove = true;
14269 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14270 {
14271 error_at (OMP_CLAUSE_LOCATION (c),
14272 "%<depend%> clause with %<depobj%> dependence "
14273 "type on array section");
14274 remove = true;
14275 }
14276 break;
14277 }
14278 if (t == error_mark_node)
14279 remove = true;
14280 else if (!lvalue_p (t))
14281 {
14282 error_at (OMP_CLAUSE_LOCATION (c),
14283 "%qE is not lvalue expression nor array section in "
14284 "%<depend%> clause", t);
14285 remove = true;
14286 }
14287 else if (TREE_CODE (t) == COMPONENT_REF
14288 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14289 {
14290 error_at (OMP_CLAUSE_LOCATION (c),
14291 "bit-field %qE in %qs clause", t, "depend");
14292 remove = true;
14293 }
14294 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14295 {
14296 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14297 {
14298 error_at (OMP_CLAUSE_LOCATION (c),
14299 "%qE does not have %<omp_depend_t%> type in "
14300 "%<depend%> clause with %<depobj%> dependence "
14301 "type", t);
14302 remove = true;
14303 }
14304 }
14305 else if (c_omp_depend_t_p (TREE_TYPE (t)))
14306 {
14307 error_at (OMP_CLAUSE_LOCATION (c),
14308 "%qE should not have %<omp_depend_t%> type in "
14309 "%<depend%> clause with dependence type other than "
14310 "%<depobj%>", t);
14311 remove = true;
14312 }
14313 if (!remove)
14314 {
14315 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14316 t, false);
14317 if (addr == error_mark_node)
14318 remove = true;
14319 else
14320 {
14321 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14322 RO_UNARY_STAR);
14323 if (t == error_mark_node)
14324 remove = true;
14325 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14326 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14327 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14328 == TREE_VEC))
14329 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14330 else
14331 OMP_CLAUSE_DECL (c) = t;
14332 }
14333 }
14334 break;
14335
14336 case OMP_CLAUSE_MAP:
14337 case OMP_CLAUSE_TO:
14338 case OMP_CLAUSE_FROM:
14339 case OMP_CLAUSE__CACHE_:
14340 t = OMP_CLAUSE_DECL (c);
14341 if (TREE_CODE (t) == TREE_LIST)
14342 {
14343 if (handle_omp_array_sections (c, ort))
14344 remove = true;
14345 else
14346 {
14347 t = OMP_CLAUSE_DECL (c);
14348 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14349 {
14350 error_at (OMP_CLAUSE_LOCATION (c),
14351 "array section does not have mappable type "
14352 "in %qs clause",
14353 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14354 remove = true;
14355 }
14356 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14357 {
14358 error_at (OMP_CLAUSE_LOCATION (c),
14359 "%<_Atomic%> %qE in %qs clause", t,
14360 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14361 remove = true;
14362 }
14363 while (TREE_CODE (t) == ARRAY_REF)
14364 t = TREE_OPERAND (t, 0);
14365 if (TREE_CODE (t) == COMPONENT_REF
14366 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14367 {
14368 while (TREE_CODE (t) == COMPONENT_REF)
14369 t = TREE_OPERAND (t, 0);
14370 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14371 break;
14372 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14373 {
14374 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14375 error_at (OMP_CLAUSE_LOCATION (c),
14376 "%qD appears more than once in motion "
14377 "clauses", t);
14378 else if (ort == C_ORT_ACC)
14379 error_at (OMP_CLAUSE_LOCATION (c),
14380 "%qD appears more than once in data "
14381 "clauses", t);
14382 else
14383 error_at (OMP_CLAUSE_LOCATION (c),
14384 "%qD appears more than once in map "
14385 "clauses", t);
14386 remove = true;
14387 }
14388 else
14389 {
14390 bitmap_set_bit (&map_head, DECL_UID (t));
14391 bitmap_set_bit (&map_field_head, DECL_UID (t));
14392 }
14393 }
14394 }
14395 break;
14396 }
14397 if (t == error_mark_node)
14398 {
14399 remove = true;
14400 break;
14401 }
14402 if (TREE_CODE (t) == COMPONENT_REF
14403 && (ort & C_ORT_OMP)
14404 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14405 {
14406 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14407 {
14408 error_at (OMP_CLAUSE_LOCATION (c),
14409 "bit-field %qE in %qs clause",
14410 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14411 remove = true;
14412 }
14413 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14414 {
14415 error_at (OMP_CLAUSE_LOCATION (c),
14416 "%qE does not have a mappable type in %qs clause",
14417 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14418 remove = true;
14419 }
14420 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14421 {
14422 error_at (OMP_CLAUSE_LOCATION (c),
14423 "%<_Atomic%> %qE in %qs clause", t,
14424 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14425 remove = true;
14426 }
14427 while (TREE_CODE (t) == COMPONENT_REF)
14428 {
14429 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14430 == UNION_TYPE)
14431 {
14432 error_at (OMP_CLAUSE_LOCATION (c),
14433 "%qE is a member of a union", t);
14434 remove = true;
14435 break;
14436 }
14437 t = TREE_OPERAND (t, 0);
14438 }
14439 if (remove)
14440 break;
14441 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14442 {
14443 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14444 break;
14445 }
14446 }
14447 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14448 {
14449 error_at (OMP_CLAUSE_LOCATION (c),
14450 "%qE is not a variable in %qs clause", t,
14451 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14452 remove = true;
14453 }
14454 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14455 {
14456 error_at (OMP_CLAUSE_LOCATION (c),
14457 "%qD is threadprivate variable 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)
14463 != GOMP_MAP_FIRSTPRIVATE_POINTER))
14464 && !c_mark_addressable (t))
14465 remove = true;
14466 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14467 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
14468 || (OMP_CLAUSE_MAP_KIND (c)
14469 == GOMP_MAP_FIRSTPRIVATE_POINTER)
14470 || (OMP_CLAUSE_MAP_KIND (c)
14471 == GOMP_MAP_FORCE_DEVICEPTR)))
14472 && t == OMP_CLAUSE_DECL (c)
14473 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14474 {
14475 error_at (OMP_CLAUSE_LOCATION (c),
14476 "%qD does not have a mappable type in %qs clause", t,
14477 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14478 remove = true;
14479 }
14480 else if (TREE_TYPE (t) == error_mark_node)
14481 remove = true;
14482 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14483 {
14484 error_at (OMP_CLAUSE_LOCATION (c),
14485 "%<_Atomic%> %qE in %qs clause", t,
14486 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14487 remove = true;
14488 }
14489 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14490 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14491 {
14492 if (bitmap_bit_p (&generic_head, DECL_UID (t))
14493 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14494 {
14495 error_at (OMP_CLAUSE_LOCATION (c),
14496 "%qD appears more than once in data clauses", t);
14497 remove = true;
14498 }
14499 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14500 {
14501 if (ort == C_ORT_ACC)
14502 error_at (OMP_CLAUSE_LOCATION (c),
14503 "%qD appears more than once in data clauses", t);
14504 else
14505 error_at (OMP_CLAUSE_LOCATION (c),
14506 "%qD appears both in data and map clauses", t);
14507 remove = true;
14508 }
14509 else
14510 bitmap_set_bit (&generic_head, DECL_UID (t));
14511 }
14512 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14513 {
14514 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14515 error_at (OMP_CLAUSE_LOCATION (c),
14516 "%qD appears more than once in motion clauses", t);
14517 else if (ort == C_ORT_ACC)
14518 error_at (OMP_CLAUSE_LOCATION (c),
14519 "%qD appears more than once in data clauses", t);
14520 else
14521 error_at (OMP_CLAUSE_LOCATION (c),
14522 "%qD appears more than once in map clauses", t);
14523 remove = true;
14524 }
14525 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14526 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14527 {
14528 if (ort == C_ORT_ACC)
14529 error_at (OMP_CLAUSE_LOCATION (c),
14530 "%qD appears more than once in data clauses", t);
14531 else
14532 error_at (OMP_CLAUSE_LOCATION (c),
14533 "%qD appears both in data and map clauses", t);
14534 remove = true;
14535 }
14536 else
14537 {
14538 bitmap_set_bit (&map_head, DECL_UID (t));
14539 if (t != OMP_CLAUSE_DECL (c)
14540 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14541 bitmap_set_bit (&map_field_head, DECL_UID (t));
14542 }
14543 break;
14544
14545 case OMP_CLAUSE_TO_DECLARE:
14546 case OMP_CLAUSE_LINK:
14547 t = OMP_CLAUSE_DECL (c);
14548 if (TREE_CODE (t) == FUNCTION_DECL
14549 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14550 ;
14551 else if (!VAR_P (t))
14552 {
14553 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14554 error_at (OMP_CLAUSE_LOCATION (c),
14555 "%qE is neither a variable nor a function name in "
14556 "clause %qs", t,
14557 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14558 else
14559 error_at (OMP_CLAUSE_LOCATION (c),
14560 "%qE is not a variable in clause %qs", t,
14561 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14562 remove = true;
14563 }
14564 else if (DECL_THREAD_LOCAL_P (t))
14565 {
14566 error_at (OMP_CLAUSE_LOCATION (c),
14567 "%qD is threadprivate variable in %qs clause", t,
14568 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14569 remove = true;
14570 }
14571 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14572 {
14573 error_at (OMP_CLAUSE_LOCATION (c),
14574 "%qD does not have a mappable type in %qs clause", t,
14575 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14576 remove = true;
14577 }
14578 if (remove)
14579 break;
14580 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
14581 {
14582 error_at (OMP_CLAUSE_LOCATION (c),
14583 "%qE appears more than once on the same "
14584 "%<declare target%> directive", t);
14585 remove = true;
14586 }
14587 else
14588 bitmap_set_bit (&generic_head, DECL_UID (t));
14589 break;
14590
14591 case OMP_CLAUSE_UNIFORM:
14592 t = OMP_CLAUSE_DECL (c);
14593 if (TREE_CODE (t) != PARM_DECL)
14594 {
14595 if (DECL_P (t))
14596 error_at (OMP_CLAUSE_LOCATION (c),
14597 "%qD is not an argument in %<uniform%> clause", t);
14598 else
14599 error_at (OMP_CLAUSE_LOCATION (c),
14600 "%qE is not an argument in %<uniform%> clause", t);
14601 remove = true;
14602 break;
14603 }
14604 /* map_head bitmap is used as uniform_head if declare_simd. */
14605 bitmap_set_bit (&map_head, DECL_UID (t));
14606 goto check_dup_generic;
14607
14608 case OMP_CLAUSE_IS_DEVICE_PTR:
14609 case OMP_CLAUSE_USE_DEVICE_PTR:
14610 t = OMP_CLAUSE_DECL (c);
14611 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
14612 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14613 {
14614 error_at (OMP_CLAUSE_LOCATION (c),
14615 "%qs variable is neither a pointer nor an array",
14616 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14617 remove = true;
14618 }
14619 goto check_dup_generic;
14620
14621 case OMP_CLAUSE_NOWAIT:
14622 if (copyprivate_seen)
14623 {
14624 error_at (OMP_CLAUSE_LOCATION (c),
14625 "%<nowait%> clause must not be used together "
14626 "with %<copyprivate%>");
14627 remove = true;
14628 break;
14629 }
14630 nowait_clause = pc;
14631 pc = &OMP_CLAUSE_CHAIN (c);
14632 continue;
14633
14634 case OMP_CLAUSE_IF:
14635 case OMP_CLAUSE_NUM_THREADS:
14636 case OMP_CLAUSE_NUM_TEAMS:
14637 case OMP_CLAUSE_THREAD_LIMIT:
14638 case OMP_CLAUSE_DEFAULT:
14639 case OMP_CLAUSE_UNTIED:
14640 case OMP_CLAUSE_COLLAPSE:
14641 case OMP_CLAUSE_FINAL:
14642 case OMP_CLAUSE_MERGEABLE:
14643 case OMP_CLAUSE_DEVICE:
14644 case OMP_CLAUSE_DIST_SCHEDULE:
14645 case OMP_CLAUSE_PARALLEL:
14646 case OMP_CLAUSE_FOR:
14647 case OMP_CLAUSE_SECTIONS:
14648 case OMP_CLAUSE_TASKGROUP:
14649 case OMP_CLAUSE_PROC_BIND:
14650 case OMP_CLAUSE_PRIORITY:
14651 case OMP_CLAUSE_GRAINSIZE:
14652 case OMP_CLAUSE_NUM_TASKS:
14653 case OMP_CLAUSE_THREADS:
14654 case OMP_CLAUSE_SIMD:
14655 case OMP_CLAUSE_HINT:
14656 case OMP_CLAUSE_DEFAULTMAP:
14657 case OMP_CLAUSE_NUM_GANGS:
14658 case OMP_CLAUSE_NUM_WORKERS:
14659 case OMP_CLAUSE_VECTOR_LENGTH:
14660 case OMP_CLAUSE_ASYNC:
14661 case OMP_CLAUSE_WAIT:
14662 case OMP_CLAUSE_AUTO:
14663 case OMP_CLAUSE_INDEPENDENT:
14664 case OMP_CLAUSE_SEQ:
14665 case OMP_CLAUSE_GANG:
14666 case OMP_CLAUSE_WORKER:
14667 case OMP_CLAUSE_VECTOR:
14668 case OMP_CLAUSE_TILE:
14669 case OMP_CLAUSE_IF_PRESENT:
14670 case OMP_CLAUSE_FINALIZE:
14671 pc = &OMP_CLAUSE_CHAIN (c);
14672 continue;
14673
14674 case OMP_CLAUSE_NOGROUP:
14675 nogroup_seen = pc;
14676 pc = &OMP_CLAUSE_CHAIN (c);
14677 continue;
14678
14679 case OMP_CLAUSE_SCHEDULE:
14680 schedule_clause = c;
14681 pc = &OMP_CLAUSE_CHAIN (c);
14682 continue;
14683
14684 case OMP_CLAUSE_ORDERED:
14685 ordered_clause = c;
14686 pc = &OMP_CLAUSE_CHAIN (c);
14687 continue;
14688
14689 case OMP_CLAUSE_SAFELEN:
14690 safelen = c;
14691 pc = &OMP_CLAUSE_CHAIN (c);
14692 continue;
14693 case OMP_CLAUSE_SIMDLEN:
14694 simdlen = c;
14695 pc = &OMP_CLAUSE_CHAIN (c);
14696 continue;
14697
14698 case OMP_CLAUSE_INBRANCH:
14699 case OMP_CLAUSE_NOTINBRANCH:
14700 if (branch_seen)
14701 {
14702 error_at (OMP_CLAUSE_LOCATION (c),
14703 "%<inbranch%> clause is incompatible with "
14704 "%<notinbranch%>");
14705 remove = true;
14706 break;
14707 }
14708 branch_seen = true;
14709 pc = &OMP_CLAUSE_CHAIN (c);
14710 continue;
14711
14712 case OMP_CLAUSE_INCLUSIVE:
14713 case OMP_CLAUSE_EXCLUSIVE:
14714 need_complete = true;
14715 need_implicitly_determined = true;
14716 t = OMP_CLAUSE_DECL (c);
14717 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14718 {
14719 error_at (OMP_CLAUSE_LOCATION (c),
14720 "%qE is not a variable in clause %qs", t,
14721 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14722 remove = true;
14723 }
14724 break;
14725
14726 default:
14727 gcc_unreachable ();
14728 }
14729
14730 if (!remove)
14731 {
14732 t = OMP_CLAUSE_DECL (c);
14733
14734 if (need_complete)
14735 {
14736 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14737 if (t == error_mark_node)
14738 remove = true;
14739 }
14740
14741 if (need_implicitly_determined)
14742 {
14743 const char *share_name = NULL;
14744
14745 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14746 share_name = "threadprivate";
14747 else switch (c_omp_predetermined_sharing (t))
14748 {
14749 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
14750 break;
14751 case OMP_CLAUSE_DEFAULT_SHARED:
14752 share_name = "shared";
14753 break;
14754 case OMP_CLAUSE_DEFAULT_PRIVATE:
14755 share_name = "private";
14756 break;
14757 default:
14758 gcc_unreachable ();
14759 }
14760 if (share_name)
14761 {
14762 error_at (OMP_CLAUSE_LOCATION (c),
14763 "%qE is predetermined %qs for %qs",
14764 t, share_name,
14765 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14766 remove = true;
14767 }
14768 else if (TREE_READONLY (t)
14769 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
14770 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
14771 {
14772 error_at (OMP_CLAUSE_LOCATION (c),
14773 "%<const%> qualified %qE may appear only in "
14774 "%<shared%> or %<firstprivate%> clauses", t);
14775 remove = true;
14776 }
14777 }
14778 }
14779
14780 if (remove)
14781 *pc = OMP_CLAUSE_CHAIN (c);
14782 else
14783 pc = &OMP_CLAUSE_CHAIN (c);
14784 }
14785
14786 if (simdlen
14787 && safelen
14788 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
14789 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
14790 {
14791 error_at (OMP_CLAUSE_LOCATION (simdlen),
14792 "%<simdlen%> clause value is bigger than "
14793 "%<safelen%> clause value");
14794 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
14795 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
14796 }
14797
14798 if (ordered_clause
14799 && schedule_clause
14800 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14801 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14802 {
14803 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
14804 "%<nonmonotonic%> schedule modifier specified together "
14805 "with %<ordered%> clause");
14806 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14807 = (enum omp_clause_schedule_kind)
14808 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14809 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14810 }
14811
14812 if (reduction_seen < 0 && ordered_clause)
14813 {
14814 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
14815 "%qs clause specified together with %<inscan%> "
14816 "%<reduction%> clause", "ordered");
14817 reduction_seen = -2;
14818 }
14819
14820 if (reduction_seen < 0 && schedule_clause)
14821 {
14822 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
14823 "%qs clause specified together with %<inscan%> "
14824 "%<reduction%> clause", "schedule");
14825 reduction_seen = -2;
14826 }
14827
14828 if (linear_variable_step_check || reduction_seen == -2)
14829 for (pc = &clauses, c = clauses; c ; c = *pc)
14830 {
14831 bool remove = false;
14832 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14833 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
14834 && !bitmap_bit_p (&map_head,
14835 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
14836 {
14837 error_at (OMP_CLAUSE_LOCATION (c),
14838 "%<linear%> clause step is a parameter %qD not "
14839 "specified in %<uniform%> clause",
14840 OMP_CLAUSE_LINEAR_STEP (c));
14841 remove = true;
14842 }
14843 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14844 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
14845
14846 if (remove)
14847 *pc = OMP_CLAUSE_CHAIN (c);
14848 else
14849 pc = &OMP_CLAUSE_CHAIN (c);
14850 }
14851
14852 if (nogroup_seen && reduction_seen)
14853 {
14854 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
14855 "%<nogroup%> clause must not be used together with "
14856 "%<reduction%> clause");
14857 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
14858 }
14859
14860 bitmap_obstack_release (NULL);
14861 return clauses;
14862 }
14863
14864 /* Return code to initialize DST with a copy constructor from SRC.
14865 C doesn't have copy constructors nor assignment operators, only for
14866 _Atomic vars we need to perform __atomic_load from src into a temporary
14867 followed by __atomic_store of the temporary to dst. */
14868
14869 tree
14870 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
14871 {
14872 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
14873 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14874
14875 location_t loc = OMP_CLAUSE_LOCATION (clause);
14876 tree type = TREE_TYPE (dst);
14877 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
14878 tree tmp = create_tmp_var (nonatomic_type);
14879 tree tmp_addr = build_fold_addr_expr (tmp);
14880 TREE_ADDRESSABLE (tmp) = 1;
14881 TREE_NO_WARNING (tmp) = 1;
14882 tree src_addr = build_fold_addr_expr (src);
14883 tree dst_addr = build_fold_addr_expr (dst);
14884 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14885 vec<tree, va_gc> *params;
14886 /* Expansion of a generic atomic load may require an addition
14887 element, so allocate enough to prevent a resize. */
14888 vec_alloc (params, 4);
14889
14890 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14891 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14892 params->quick_push (src_addr);
14893 params->quick_push (tmp_addr);
14894 params->quick_push (seq_cst);
14895 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14896
14897 vec_alloc (params, 4);
14898
14899 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14900 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14901 params->quick_push (dst_addr);
14902 params->quick_push (tmp_addr);
14903 params->quick_push (seq_cst);
14904 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14905 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14906 }
14907
14908 /* Create a transaction node. */
14909
14910 tree
14911 c_finish_transaction (location_t loc, tree block, int flags)
14912 {
14913 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14914 if (flags & TM_STMT_ATTR_OUTER)
14915 TRANSACTION_EXPR_OUTER (stmt) = 1;
14916 if (flags & TM_STMT_ATTR_RELAXED)
14917 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14918 return add_stmt (stmt);
14919 }
14920
14921 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14922 down to the element type of an array. If ORIG_QUAL_TYPE is not
14923 NULL, then it should be used as the qualified type
14924 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14925 preserve information about the typedef name from which an array
14926 type was derived). */
14927
14928 tree
14929 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14930 size_t orig_qual_indirect)
14931 {
14932 if (type == error_mark_node)
14933 return type;
14934
14935 if (TREE_CODE (type) == ARRAY_TYPE)
14936 {
14937 tree t;
14938 tree element_type = c_build_qualified_type (TREE_TYPE (type),
14939 type_quals, orig_qual_type,
14940 orig_qual_indirect - 1);
14941
14942 /* See if we already have an identically qualified type. */
14943 if (orig_qual_type && orig_qual_indirect == 0)
14944 t = orig_qual_type;
14945 else
14946 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14947 {
14948 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14949 && TYPE_NAME (t) == TYPE_NAME (type)
14950 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14951 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14952 TYPE_ATTRIBUTES (type)))
14953 break;
14954 }
14955 if (!t)
14956 {
14957 tree domain = TYPE_DOMAIN (type);
14958
14959 t = build_variant_type_copy (type);
14960 TREE_TYPE (t) = element_type;
14961
14962 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14963 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14964 SET_TYPE_STRUCTURAL_EQUALITY (t);
14965 else if (TYPE_CANONICAL (element_type) != element_type
14966 || (domain && TYPE_CANONICAL (domain) != domain))
14967 {
14968 tree unqualified_canon
14969 = build_array_type (TYPE_CANONICAL (element_type),
14970 domain? TYPE_CANONICAL (domain)
14971 : NULL_TREE);
14972 if (TYPE_REVERSE_STORAGE_ORDER (type))
14973 {
14974 unqualified_canon
14975 = build_distinct_type_copy (unqualified_canon);
14976 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14977 }
14978 TYPE_CANONICAL (t)
14979 = c_build_qualified_type (unqualified_canon, type_quals);
14980 }
14981 else
14982 TYPE_CANONICAL (t) = t;
14983 }
14984 return t;
14985 }
14986
14987 /* A restrict-qualified pointer type must be a pointer to object or
14988 incomplete type. Note that the use of POINTER_TYPE_P also allows
14989 REFERENCE_TYPEs, which is appropriate for C++. */
14990 if ((type_quals & TYPE_QUAL_RESTRICT)
14991 && (!POINTER_TYPE_P (type)
14992 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14993 {
14994 error ("invalid use of %<restrict%>");
14995 type_quals &= ~TYPE_QUAL_RESTRICT;
14996 }
14997
14998 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14999 ? orig_qual_type
15000 : build_qualified_type (type, type_quals));
15001 /* A variant type does not inherit the list of incomplete vars from the
15002 type main variant. */
15003 if (RECORD_OR_UNION_TYPE_P (var_type)
15004 && TYPE_MAIN_VARIANT (var_type) != var_type)
15005 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15006 return var_type;
15007 }
15008
15009 /* Build a VA_ARG_EXPR for the C parser. */
15010
15011 tree
15012 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15013 {
15014 if (error_operand_p (type))
15015 return error_mark_node;
15016 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15017 order because it takes the address of the expression. */
15018 else if (handled_component_p (expr)
15019 && reverse_storage_order_for_component_p (expr))
15020 {
15021 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15022 return error_mark_node;
15023 }
15024 else if (!COMPLETE_TYPE_P (type))
15025 {
15026 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15027 "type %qT", type);
15028 return error_mark_node;
15029 }
15030 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15031 warning_at (loc2, OPT_Wc___compat,
15032 "C++ requires promoted type, not enum type, in %<va_arg%>");
15033 return build_va_arg (loc2, expr, type);
15034 }
15035
15036 /* Return truthvalue of whether T1 is the same tree structure as T2.
15037 Return 1 if they are the same. Return false if they are different. */
15038
15039 bool
15040 c_tree_equal (tree t1, tree t2)
15041 {
15042 enum tree_code code1, code2;
15043
15044 if (t1 == t2)
15045 return true;
15046 if (!t1 || !t2)
15047 return false;
15048
15049 for (code1 = TREE_CODE (t1);
15050 CONVERT_EXPR_CODE_P (code1)
15051 || code1 == NON_LVALUE_EXPR;
15052 code1 = TREE_CODE (t1))
15053 t1 = TREE_OPERAND (t1, 0);
15054 for (code2 = TREE_CODE (t2);
15055 CONVERT_EXPR_CODE_P (code2)
15056 || code2 == NON_LVALUE_EXPR;
15057 code2 = TREE_CODE (t2))
15058 t2 = TREE_OPERAND (t2, 0);
15059
15060 /* They might have become equal now. */
15061 if (t1 == t2)
15062 return true;
15063
15064 if (code1 != code2)
15065 return false;
15066
15067 switch (code1)
15068 {
15069 case INTEGER_CST:
15070 return wi::to_wide (t1) == wi::to_wide (t2);
15071
15072 case REAL_CST:
15073 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15074
15075 case STRING_CST:
15076 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15077 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15078 TREE_STRING_LENGTH (t1));
15079
15080 case FIXED_CST:
15081 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15082 TREE_FIXED_CST (t2));
15083
15084 case COMPLEX_CST:
15085 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15086 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15087
15088 case VECTOR_CST:
15089 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15090
15091 case CONSTRUCTOR:
15092 /* We need to do this when determining whether or not two
15093 non-type pointer to member function template arguments
15094 are the same. */
15095 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15096 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15097 return false;
15098 {
15099 tree field, value;
15100 unsigned int i;
15101 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15102 {
15103 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15104 if (!c_tree_equal (field, elt2->index)
15105 || !c_tree_equal (value, elt2->value))
15106 return false;
15107 }
15108 }
15109 return true;
15110
15111 case TREE_LIST:
15112 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15113 return false;
15114 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15115 return false;
15116 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15117
15118 case SAVE_EXPR:
15119 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15120
15121 case CALL_EXPR:
15122 {
15123 tree arg1, arg2;
15124 call_expr_arg_iterator iter1, iter2;
15125 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15126 return false;
15127 for (arg1 = first_call_expr_arg (t1, &iter1),
15128 arg2 = first_call_expr_arg (t2, &iter2);
15129 arg1 && arg2;
15130 arg1 = next_call_expr_arg (&iter1),
15131 arg2 = next_call_expr_arg (&iter2))
15132 if (!c_tree_equal (arg1, arg2))
15133 return false;
15134 if (arg1 || arg2)
15135 return false;
15136 return true;
15137 }
15138
15139 case TARGET_EXPR:
15140 {
15141 tree o1 = TREE_OPERAND (t1, 0);
15142 tree o2 = TREE_OPERAND (t2, 0);
15143
15144 /* Special case: if either target is an unallocated VAR_DECL,
15145 it means that it's going to be unified with whatever the
15146 TARGET_EXPR is really supposed to initialize, so treat it
15147 as being equivalent to anything. */
15148 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
15149 && !DECL_RTL_SET_P (o1))
15150 /*Nop*/;
15151 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
15152 && !DECL_RTL_SET_P (o2))
15153 /*Nop*/;
15154 else if (!c_tree_equal (o1, o2))
15155 return false;
15156
15157 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15158 }
15159
15160 case COMPONENT_REF:
15161 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15162 return false;
15163 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15164
15165 case PARM_DECL:
15166 case VAR_DECL:
15167 case CONST_DECL:
15168 case FIELD_DECL:
15169 case FUNCTION_DECL:
15170 case IDENTIFIER_NODE:
15171 case SSA_NAME:
15172 return false;
15173
15174 case TREE_VEC:
15175 {
15176 unsigned ix;
15177 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15178 return false;
15179 for (ix = TREE_VEC_LENGTH (t1); ix--;)
15180 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15181 TREE_VEC_ELT (t2, ix)))
15182 return false;
15183 return true;
15184 }
15185
15186 default:
15187 break;
15188 }
15189
15190 switch (TREE_CODE_CLASS (code1))
15191 {
15192 case tcc_unary:
15193 case tcc_binary:
15194 case tcc_comparison:
15195 case tcc_expression:
15196 case tcc_vl_exp:
15197 case tcc_reference:
15198 case tcc_statement:
15199 {
15200 int i, n = TREE_OPERAND_LENGTH (t1);
15201
15202 switch (code1)
15203 {
15204 case PREINCREMENT_EXPR:
15205 case PREDECREMENT_EXPR:
15206 case POSTINCREMENT_EXPR:
15207 case POSTDECREMENT_EXPR:
15208 n = 1;
15209 break;
15210 case ARRAY_REF:
15211 n = 2;
15212 break;
15213 default:
15214 break;
15215 }
15216
15217 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15218 && n != TREE_OPERAND_LENGTH (t2))
15219 return false;
15220
15221 for (i = 0; i < n; ++i)
15222 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15223 return false;
15224
15225 return true;
15226 }
15227
15228 case tcc_type:
15229 return comptypes (t1, t2);
15230 default:
15231 gcc_unreachable ();
15232 }
15233 /* We can get here with --disable-checking. */
15234 return false;
15235 }
15236
15237 /* Returns true when the function declaration FNDECL is implicit,
15238 introduced as a result of a call to an otherwise undeclared
15239 function, and false otherwise. */
15240
15241 bool
15242 c_decl_implicit (const_tree fndecl)
15243 {
15244 return C_DECL_IMPLICIT (fndecl);
15245 }