runtime: mark go-context.S as no-executable-stack and split-stack supported
[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 If FUNCTION is the result of resolving an overloaded target built-in,
3007 ORIG_FUNDECL is the original function decl, otherwise it is null.
3008 ORIGTYPES, if not NULL, is a vector of types; each element is
3009 either NULL or the original type of the corresponding element in
3010 PARAMS. The original type may differ from TREE_TYPE of the
3011 parameter for enums. FUNCTION's data type may be a function type
3012 or pointer-to-function. This function changes the elements of
3013 PARAMS. */
3014
3015 tree
3016 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3017 tree function, vec<tree, va_gc> *params,
3018 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3019 {
3020 tree fntype, fundecl = NULL_TREE;
3021 tree name = NULL_TREE, result;
3022 tree tem;
3023 int nargs;
3024 tree *argarray;
3025
3026
3027 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3028 STRIP_TYPE_NOPS (function);
3029
3030 /* Convert anything with function type to a pointer-to-function. */
3031 if (TREE_CODE (function) == FUNCTION_DECL)
3032 {
3033 name = DECL_NAME (function);
3034
3035 if (flag_tm)
3036 tm_malloc_replacement (function);
3037 fundecl = function;
3038 if (!orig_fundecl)
3039 orig_fundecl = fundecl;
3040 /* Atomic functions have type checking/casting already done. They are
3041 often rewritten and don't match the original parameter list. */
3042 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3043 origtypes = NULL;
3044 }
3045 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3046 function = function_to_pointer_conversion (loc, function);
3047
3048 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3049 expressions, like those used for ObjC messenger dispatches. */
3050 if (params && !params->is_empty ())
3051 function = objc_rewrite_function_call (function, (*params)[0]);
3052
3053 function = c_fully_fold (function, false, NULL);
3054
3055 fntype = TREE_TYPE (function);
3056
3057 if (TREE_CODE (fntype) == ERROR_MARK)
3058 return error_mark_node;
3059
3060 if (!(TREE_CODE (fntype) == POINTER_TYPE
3061 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3062 {
3063 if (!flag_diagnostics_show_caret)
3064 error_at (loc,
3065 "called object %qE is not a function or function pointer",
3066 function);
3067 else if (DECL_P (function))
3068 {
3069 error_at (loc,
3070 "called object %qD is not a function or function pointer",
3071 function);
3072 inform_declaration (function);
3073 }
3074 else
3075 error_at (loc,
3076 "called object is not a function or function pointer");
3077 return error_mark_node;
3078 }
3079
3080 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3081 current_function_returns_abnormally = 1;
3082
3083 /* fntype now gets the type of function pointed to. */
3084 fntype = TREE_TYPE (fntype);
3085
3086 /* Convert the parameters to the types declared in the
3087 function prototype, or apply default promotions. */
3088
3089 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3090 origtypes, function, fundecl);
3091 if (nargs < 0)
3092 return error_mark_node;
3093
3094 /* Check that the function is called through a compatible prototype.
3095 If it is not, warn. */
3096 if (CONVERT_EXPR_P (function)
3097 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3098 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3099 && !comptypes (fntype, TREE_TYPE (tem)))
3100 {
3101 tree return_type = TREE_TYPE (fntype);
3102
3103 /* This situation leads to run-time undefined behavior. We can't,
3104 therefore, simply error unless we can prove that all possible
3105 executions of the program must execute the code. */
3106 warning_at (loc, 0, "function called through a non-compatible type");
3107
3108 if (VOID_TYPE_P (return_type)
3109 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3110 pedwarn (loc, 0,
3111 "function with qualified void return type called");
3112 }
3113
3114 argarray = vec_safe_address (params);
3115
3116 /* Check that arguments to builtin functions match the expectations. */
3117 if (fundecl
3118 && fndecl_built_in_p (fundecl)
3119 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3120 orig_fundecl, nargs, argarray))
3121 return error_mark_node;
3122
3123 /* Check that the arguments to the function are valid. */
3124 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3125 nargs, argarray, &arg_loc);
3126
3127 if (name != NULL_TREE
3128 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3129 {
3130 if (require_constant_value)
3131 result
3132 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3133 function, nargs, argarray);
3134 else
3135 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3136 function, nargs, argarray);
3137 if (TREE_CODE (result) == NOP_EXPR
3138 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3139 STRIP_TYPE_NOPS (result);
3140 }
3141 else
3142 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3143 function, nargs, argarray);
3144 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3145 later. */
3146 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3147 TREE_NO_WARNING (result) = 1;
3148
3149 /* In this improbable scenario, a nested function returns a VM type.
3150 Create a TARGET_EXPR so that the call always has a LHS, much as
3151 what the C++ FE does for functions returning non-PODs. */
3152 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3153 {
3154 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3155 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3156 NULL_TREE, NULL_TREE);
3157 }
3158
3159 if (VOID_TYPE_P (TREE_TYPE (result)))
3160 {
3161 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3162 pedwarn (loc, 0,
3163 "function with qualified void return type called");
3164 return result;
3165 }
3166 return require_complete_type (loc, result);
3167 }
3168
3169 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3170
3171 tree
3172 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3173 tree function, vec<tree, va_gc> *params,
3174 vec<tree, va_gc> *origtypes)
3175 {
3176 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3177 STRIP_TYPE_NOPS (function);
3178
3179 /* Convert anything with function type to a pointer-to-function. */
3180 if (TREE_CODE (function) == FUNCTION_DECL)
3181 {
3182 /* Implement type-directed function overloading for builtins.
3183 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3184 handle all the type checking. The result is a complete expression
3185 that implements this function call. */
3186 tree tem = resolve_overloaded_builtin (loc, function, params);
3187 if (tem)
3188 return tem;
3189 }
3190 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3191 }
3192 \f
3193 /* Helper for convert_arguments called to convert the VALue of argument
3194 number ARGNUM from ORIGTYPE to the corresponding parameter number
3195 PARMNUM and TYPE.
3196 PLOC is the location where the conversion is being performed.
3197 FUNCTION and FUNDECL are the same as in convert_arguments.
3198 VALTYPE is the original type of VAL before the conversion and,
3199 for EXCESS_PRECISION_EXPR, the operand of the expression.
3200 NPC is true if VAL represents the null pointer constant (VAL itself
3201 will have been folded to an integer constant).
3202 RNAME is the same as FUNCTION except in Objective C when it's
3203 the function selector.
3204 EXCESS_PRECISION is true when VAL was originally represented
3205 as EXCESS_PRECISION_EXPR.
3206 WARNOPT is the same as in convert_for_assignment. */
3207
3208 static tree
3209 convert_argument (location_t ploc, tree function, tree fundecl,
3210 tree type, tree origtype, tree val, tree valtype,
3211 bool npc, tree rname, int parmnum, int argnum,
3212 bool excess_precision, int warnopt)
3213 {
3214 /* Formal parm type is specified by a function prototype. */
3215
3216 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3217 {
3218 error_at (ploc, "type of formal parameter %d is incomplete",
3219 parmnum + 1);
3220 return val;
3221 }
3222
3223 /* Optionally warn about conversions that differ from the default
3224 conversions. */
3225 if (warn_traditional_conversion || warn_traditional)
3226 {
3227 unsigned int formal_prec = TYPE_PRECISION (type);
3228
3229 if (INTEGRAL_TYPE_P (type)
3230 && TREE_CODE (valtype) == REAL_TYPE)
3231 warning_at (ploc, OPT_Wtraditional_conversion,
3232 "passing argument %d of %qE as integer rather "
3233 "than floating due to prototype",
3234 argnum, rname);
3235 if (INTEGRAL_TYPE_P (type)
3236 && TREE_CODE (valtype) == COMPLEX_TYPE)
3237 warning_at (ploc, OPT_Wtraditional_conversion,
3238 "passing argument %d of %qE as integer rather "
3239 "than complex due to prototype",
3240 argnum, rname);
3241 else if (TREE_CODE (type) == COMPLEX_TYPE
3242 && TREE_CODE (valtype) == REAL_TYPE)
3243 warning_at (ploc, OPT_Wtraditional_conversion,
3244 "passing argument %d of %qE as complex rather "
3245 "than floating due to prototype",
3246 argnum, rname);
3247 else if (TREE_CODE (type) == REAL_TYPE
3248 && INTEGRAL_TYPE_P (valtype))
3249 warning_at (ploc, OPT_Wtraditional_conversion,
3250 "passing argument %d of %qE as floating rather "
3251 "than integer due to prototype",
3252 argnum, rname);
3253 else if (TREE_CODE (type) == COMPLEX_TYPE
3254 && INTEGRAL_TYPE_P (valtype))
3255 warning_at (ploc, OPT_Wtraditional_conversion,
3256 "passing argument %d of %qE as complex rather "
3257 "than integer due to prototype",
3258 argnum, rname);
3259 else if (TREE_CODE (type) == REAL_TYPE
3260 && TREE_CODE (valtype) == COMPLEX_TYPE)
3261 warning_at (ploc, OPT_Wtraditional_conversion,
3262 "passing argument %d of %qE as floating rather "
3263 "than complex due to prototype",
3264 argnum, rname);
3265 /* ??? At some point, messages should be written about
3266 conversions between complex types, but that's too messy
3267 to do now. */
3268 else if (TREE_CODE (type) == REAL_TYPE
3269 && TREE_CODE (valtype) == REAL_TYPE)
3270 {
3271 /* Warn if any argument is passed as `float',
3272 since without a prototype it would be `double'. */
3273 if (formal_prec == TYPE_PRECISION (float_type_node)
3274 && type != dfloat32_type_node)
3275 warning_at (ploc, 0,
3276 "passing argument %d of %qE as %<float%> "
3277 "rather than %<double%> due to prototype",
3278 argnum, rname);
3279
3280 /* Warn if mismatch between argument and prototype
3281 for decimal float types. Warn of conversions with
3282 binary float types and of precision narrowing due to
3283 prototype. */
3284 else if (type != valtype
3285 && (type == dfloat32_type_node
3286 || type == dfloat64_type_node
3287 || type == dfloat128_type_node
3288 || valtype == dfloat32_type_node
3289 || valtype == dfloat64_type_node
3290 || valtype == dfloat128_type_node)
3291 && (formal_prec
3292 <= TYPE_PRECISION (valtype)
3293 || (type == dfloat128_type_node
3294 && (valtype
3295 != dfloat64_type_node
3296 && (valtype
3297 != dfloat32_type_node)))
3298 || (type == dfloat64_type_node
3299 && (valtype
3300 != dfloat32_type_node))))
3301 warning_at (ploc, 0,
3302 "passing argument %d of %qE as %qT "
3303 "rather than %qT due to prototype",
3304 argnum, rname, type, valtype);
3305
3306 }
3307 /* Detect integer changing in width or signedness.
3308 These warnings are only activated with
3309 -Wtraditional-conversion, not with -Wtraditional. */
3310 else if (warn_traditional_conversion
3311 && INTEGRAL_TYPE_P (type)
3312 && INTEGRAL_TYPE_P (valtype))
3313 {
3314 tree would_have_been = default_conversion (val);
3315 tree type1 = TREE_TYPE (would_have_been);
3316
3317 if (val == error_mark_node)
3318 /* VAL could have been of incomplete type. */;
3319 else if (TREE_CODE (type) == ENUMERAL_TYPE
3320 && (TYPE_MAIN_VARIANT (type)
3321 == TYPE_MAIN_VARIANT (valtype)))
3322 /* No warning if function asks for enum
3323 and the actual arg is that enum type. */
3324 ;
3325 else if (formal_prec != TYPE_PRECISION (type1))
3326 warning_at (ploc, OPT_Wtraditional_conversion,
3327 "passing argument %d of %qE "
3328 "with different width due to prototype",
3329 argnum, rname);
3330 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3331 ;
3332 /* Don't complain if the formal parameter type
3333 is an enum, because we can't tell now whether
3334 the value was an enum--even the same enum. */
3335 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3336 ;
3337 else if (TREE_CODE (val) == INTEGER_CST
3338 && int_fits_type_p (val, type))
3339 /* Change in signedness doesn't matter
3340 if a constant value is unaffected. */
3341 ;
3342 /* If the value is extended from a narrower
3343 unsigned type, it doesn't matter whether we
3344 pass it as signed or unsigned; the value
3345 certainly is the same either way. */
3346 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3347 && TYPE_UNSIGNED (valtype))
3348 ;
3349 else if (TYPE_UNSIGNED (type))
3350 warning_at (ploc, OPT_Wtraditional_conversion,
3351 "passing argument %d of %qE "
3352 "as unsigned due to prototype",
3353 argnum, rname);
3354 else
3355 warning_at (ploc, OPT_Wtraditional_conversion,
3356 "passing argument %d of %qE "
3357 "as signed due to prototype",
3358 argnum, rname);
3359 }
3360 }
3361
3362 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3363 sake of better warnings from convert_and_check. */
3364 if (excess_precision)
3365 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3366
3367 tree parmval = convert_for_assignment (ploc, ploc, type,
3368 val, origtype, ic_argpass,
3369 npc, fundecl, function,
3370 parmnum + 1, warnopt);
3371
3372 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3373 && INTEGRAL_TYPE_P (type)
3374 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3375 parmval = default_conversion (parmval);
3376
3377 return parmval;
3378 }
3379
3380 /* Convert the argument expressions in the vector VALUES
3381 to the types in the list TYPELIST.
3382
3383 If TYPELIST is exhausted, or when an element has NULL as its type,
3384 perform the default conversions.
3385
3386 ORIGTYPES is the original types of the expressions in VALUES. This
3387 holds the type of enum values which have been converted to integral
3388 types. It may be NULL.
3389
3390 FUNCTION is a tree for the called function. It is used only for
3391 error messages, where it is formatted with %qE.
3392
3393 This is also where warnings about wrong number of args are generated.
3394
3395 ARG_LOC are locations of function arguments (if any).
3396
3397 Returns the actual number of arguments processed (which may be less
3398 than the length of VALUES in some error situations), or -1 on
3399 failure. */
3400
3401 static int
3402 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3403 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3404 tree function, tree fundecl)
3405 {
3406 unsigned int parmnum;
3407 bool error_args = false;
3408 const bool type_generic = fundecl
3409 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3410 bool type_generic_remove_excess_precision = false;
3411 bool type_generic_overflow_p = false;
3412 tree selector;
3413
3414 /* Change pointer to function to the function itself for
3415 diagnostics. */
3416 if (TREE_CODE (function) == ADDR_EXPR
3417 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3418 function = TREE_OPERAND (function, 0);
3419
3420 /* Handle an ObjC selector specially for diagnostics. */
3421 selector = objc_message_selector ();
3422
3423 /* For a call to a built-in function declared without a prototype,
3424 set to the built-in function's argument list. */
3425 tree builtin_typelist = NULL_TREE;
3426
3427 /* For type-generic built-in functions, determine whether excess
3428 precision should be removed (classification) or not
3429 (comparison). */
3430 if (fundecl
3431 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3432 {
3433 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3434 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3435 {
3436 /* For a call to a built-in function declared without a prototype
3437 use the types of the parameters of the internal built-in to
3438 match those of the arguments to. */
3439 if (tree bdecl = builtin_decl_explicit (code))
3440 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3441 }
3442
3443 /* For type-generic built-in functions, determine whether excess
3444 precision should be removed (classification) or not
3445 (comparison). */
3446 if (type_generic)
3447 switch (code)
3448 {
3449 case BUILT_IN_ISFINITE:
3450 case BUILT_IN_ISINF:
3451 case BUILT_IN_ISINF_SIGN:
3452 case BUILT_IN_ISNAN:
3453 case BUILT_IN_ISNORMAL:
3454 case BUILT_IN_FPCLASSIFY:
3455 type_generic_remove_excess_precision = true;
3456 break;
3457
3458 case BUILT_IN_ADD_OVERFLOW_P:
3459 case BUILT_IN_SUB_OVERFLOW_P:
3460 case BUILT_IN_MUL_OVERFLOW_P:
3461 /* The last argument of these type-generic builtins
3462 should not be promoted. */
3463 type_generic_overflow_p = true;
3464 break;
3465
3466 default:
3467 break;
3468 }
3469 }
3470
3471 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3472 individual converted arguments. */
3473
3474 tree typetail, builtin_typetail, val;
3475 for (typetail = typelist,
3476 builtin_typetail = builtin_typelist,
3477 parmnum = 0;
3478 values && values->iterate (parmnum, &val);
3479 ++parmnum)
3480 {
3481 /* The type of the function parameter (if it was declared with one). */
3482 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3483 /* The type of the built-in function parameter (if the function
3484 is a built-in). Used to detect type incompatibilities in
3485 calls to built-ins declared without a prototype. */
3486 tree builtin_type = (builtin_typetail
3487 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3488 /* The original type of the argument being passed to the function. */
3489 tree valtype = TREE_TYPE (val);
3490 /* The called function (or function selector in Objective C). */
3491 tree rname = function;
3492 int argnum = parmnum + 1;
3493 const char *invalid_func_diag;
3494 /* Set for EXCESS_PRECISION_EXPR arguments. */
3495 bool excess_precision = false;
3496 /* The value of the argument after conversion to the type
3497 of the function parameter it is passed to. */
3498 tree parmval;
3499 /* Some __atomic_* builtins have additional hidden argument at
3500 position 0. */
3501 location_t ploc
3502 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3503 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3504 : input_location;
3505
3506 if (type == void_type_node)
3507 {
3508 if (selector)
3509 error_at (loc, "too many arguments to method %qE", selector);
3510 else
3511 error_at (loc, "too many arguments to function %qE", function);
3512 inform_declaration (fundecl);
3513 return error_args ? -1 : (int) parmnum;
3514 }
3515
3516 if (builtin_type == void_type_node)
3517 {
3518 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3519 "too many arguments to built-in function %qE "
3520 "expecting %d", function, parmnum))
3521 inform_declaration (fundecl);
3522 builtin_typetail = NULL_TREE;
3523 }
3524
3525 if (selector && argnum > 2)
3526 {
3527 rname = selector;
3528 argnum -= 2;
3529 }
3530
3531 /* Determine if VAL is a null pointer constant before folding it. */
3532 bool npc = null_pointer_constant_p (val);
3533
3534 /* If there is excess precision and a prototype, convert once to
3535 the required type rather than converting via the semantic
3536 type. Likewise without a prototype a float value represented
3537 as long double should be converted once to double. But for
3538 type-generic classification functions excess precision must
3539 be removed here. */
3540 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3541 && (type || !type_generic || !type_generic_remove_excess_precision))
3542 {
3543 val = TREE_OPERAND (val, 0);
3544 excess_precision = true;
3545 }
3546 val = c_fully_fold (val, false, NULL);
3547 STRIP_TYPE_NOPS (val);
3548
3549 val = require_complete_type (ploc, val);
3550
3551 /* Some floating-point arguments must be promoted to double when
3552 no type is specified by a prototype. This applies to
3553 arguments of type float, and to architecture-specific types
3554 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3555 bool promote_float_arg = false;
3556 if (type == NULL_TREE
3557 && TREE_CODE (valtype) == REAL_TYPE
3558 && (TYPE_PRECISION (valtype)
3559 <= TYPE_PRECISION (double_type_node))
3560 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3561 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3562 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3563 {
3564 /* Promote this argument, unless it has a _FloatN or
3565 _FloatNx type. */
3566 promote_float_arg = true;
3567 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3568 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3569 {
3570 promote_float_arg = false;
3571 break;
3572 }
3573 }
3574
3575 if (type != NULL_TREE)
3576 {
3577 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3578 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3579 val, valtype, npc, rname, parmnum, argnum,
3580 excess_precision, 0);
3581 }
3582 else if (promote_float_arg)
3583 {
3584 if (type_generic)
3585 parmval = val;
3586 else
3587 {
3588 /* Convert `float' to `double'. */
3589 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3590 warning_at (ploc, OPT_Wdouble_promotion,
3591 "implicit conversion from %qT to %qT when passing "
3592 "argument to function",
3593 valtype, double_type_node);
3594 parmval = convert (double_type_node, val);
3595 }
3596 }
3597 else if ((excess_precision && !type_generic)
3598 || (type_generic_overflow_p && parmnum == 2))
3599 /* A "double" argument with excess precision being passed
3600 without a prototype or in variable arguments.
3601 The last argument of __builtin_*_overflow_p should not be
3602 promoted. */
3603 parmval = convert (valtype, val);
3604 else if ((invalid_func_diag =
3605 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3606 {
3607 error (invalid_func_diag);
3608 return -1;
3609 }
3610 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3611 {
3612 return -1;
3613 }
3614 else
3615 /* Convert `short' and `char' to full-size `int'. */
3616 parmval = default_conversion (val);
3617
3618 (*values)[parmnum] = parmval;
3619 if (parmval == error_mark_node)
3620 error_args = true;
3621
3622 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3623 {
3624 /* For a call to a built-in function declared without a prototype,
3625 perform the conversions from the argument to the expected type
3626 but issue warnings rather than errors for any mismatches.
3627 Ignore the converted argument and use the PARMVAL obtained
3628 above by applying default conversions instead. */
3629 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3630 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3631 val, valtype, npc, rname, parmnum, argnum,
3632 excess_precision,
3633 OPT_Wbuiltin_declaration_mismatch);
3634 }
3635
3636 if (typetail)
3637 typetail = TREE_CHAIN (typetail);
3638
3639 if (builtin_typetail)
3640 builtin_typetail = TREE_CHAIN (builtin_typetail);
3641 }
3642
3643 gcc_assert (parmnum == vec_safe_length (values));
3644
3645 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3646 {
3647 error_at (loc, "too few arguments to function %qE", function);
3648 inform_declaration (fundecl);
3649 return -1;
3650 }
3651
3652 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3653 {
3654 unsigned nargs = parmnum;
3655 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3656 ++nargs;
3657
3658 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3659 "too few arguments to built-in function %qE "
3660 "expecting %u", function, nargs - 1))
3661 inform_declaration (fundecl);
3662 }
3663
3664 return error_args ? -1 : (int) parmnum;
3665 }
3666 \f
3667 /* This is the entry point used by the parser to build unary operators
3668 in the input. CODE, a tree_code, specifies the unary operator, and
3669 ARG is the operand. For unary plus, the C parser currently uses
3670 CONVERT_EXPR for code.
3671
3672 LOC is the location to use for the tree generated.
3673 */
3674
3675 struct c_expr
3676 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3677 {
3678 struct c_expr result;
3679
3680 result.original_code = code;
3681 result.original_type = NULL;
3682
3683 if (reject_gcc_builtin (arg.value))
3684 {
3685 result.value = error_mark_node;
3686 }
3687 else
3688 {
3689 result.value = build_unary_op (loc, code, arg.value, false);
3690
3691 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3692 overflow_warning (loc, result.value, arg.value);
3693 }
3694
3695 /* We are typically called when parsing a prefix token at LOC acting on
3696 ARG. Reflect this by updating the source range of the result to
3697 start at LOC and end at the end of ARG. */
3698 set_c_expr_source_range (&result,
3699 loc, arg.get_finish ());
3700
3701 return result;
3702 }
3703
3704 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3705
3706 static bool
3707 char_type_p (tree type)
3708 {
3709 return (type == char_type_node
3710 || type == unsigned_char_type_node
3711 || type == signed_char_type_node
3712 || type == char16_type_node
3713 || type == char32_type_node);
3714 }
3715
3716 /* This is the entry point used by the parser to build binary operators
3717 in the input. CODE, a tree_code, specifies the binary operator, and
3718 ARG1 and ARG2 are the operands. In addition to constructing the
3719 expression, we check for operands that were written with other binary
3720 operators in a way that is likely to confuse the user.
3721
3722 LOCATION is the location of the binary operator. */
3723
3724 struct c_expr
3725 parser_build_binary_op (location_t location, enum tree_code code,
3726 struct c_expr arg1, struct c_expr arg2)
3727 {
3728 struct c_expr result;
3729
3730 enum tree_code code1 = arg1.original_code;
3731 enum tree_code code2 = arg2.original_code;
3732 tree type1 = (arg1.original_type
3733 ? arg1.original_type
3734 : TREE_TYPE (arg1.value));
3735 tree type2 = (arg2.original_type
3736 ? arg2.original_type
3737 : TREE_TYPE (arg2.value));
3738
3739 result.value = build_binary_op (location, code,
3740 arg1.value, arg2.value, true);
3741 result.original_code = code;
3742 result.original_type = NULL;
3743
3744 if (TREE_CODE (result.value) == ERROR_MARK)
3745 {
3746 set_c_expr_source_range (&result,
3747 arg1.get_start (),
3748 arg2.get_finish ());
3749 return result;
3750 }
3751
3752 if (location != UNKNOWN_LOCATION)
3753 protected_set_expr_location (result.value, location);
3754
3755 set_c_expr_source_range (&result,
3756 arg1.get_start (),
3757 arg2.get_finish ());
3758
3759 /* Check for cases such as x+y<<z which users are likely
3760 to misinterpret. */
3761 if (warn_parentheses)
3762 warn_about_parentheses (location, code, code1, arg1.value, code2,
3763 arg2.value);
3764
3765 if (warn_logical_op)
3766 warn_logical_operator (location, code, TREE_TYPE (result.value),
3767 code1, arg1.value, code2, arg2.value);
3768
3769 if (warn_tautological_compare)
3770 {
3771 tree lhs = arg1.value;
3772 tree rhs = arg2.value;
3773 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3774 {
3775 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3776 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3777 lhs = NULL_TREE;
3778 else
3779 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3780 }
3781 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3782 {
3783 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3784 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3785 rhs = NULL_TREE;
3786 else
3787 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3788 }
3789 if (lhs != NULL_TREE && rhs != NULL_TREE)
3790 warn_tautological_cmp (location, code, lhs, rhs);
3791 }
3792
3793 if (warn_logical_not_paren
3794 && TREE_CODE_CLASS (code) == tcc_comparison
3795 && code1 == TRUTH_NOT_EXPR
3796 && code2 != TRUTH_NOT_EXPR
3797 /* Avoid warning for !!x == y. */
3798 && (TREE_CODE (arg1.value) != NE_EXPR
3799 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3800 {
3801 /* Avoid warning for !b == y where b has _Bool type. */
3802 tree t = integer_zero_node;
3803 if (TREE_CODE (arg1.value) == EQ_EXPR
3804 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3805 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3806 {
3807 t = TREE_OPERAND (arg1.value, 0);
3808 do
3809 {
3810 if (TREE_TYPE (t) != integer_type_node)
3811 break;
3812 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3813 t = C_MAYBE_CONST_EXPR_EXPR (t);
3814 else if (CONVERT_EXPR_P (t))
3815 t = TREE_OPERAND (t, 0);
3816 else
3817 break;
3818 }
3819 while (1);
3820 }
3821 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3822 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3823 }
3824
3825 /* Warn about comparisons against string literals, with the exception
3826 of testing for equality or inequality of a string literal with NULL. */
3827 if (code == EQ_EXPR || code == NE_EXPR)
3828 {
3829 if ((code1 == STRING_CST
3830 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3831 || (code2 == STRING_CST
3832 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3833 warning_at (location, OPT_Waddress,
3834 "comparison with string literal results in unspecified behavior");
3835 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3836 if (POINTER_TYPE_P (type1)
3837 && null_pointer_constant_p (arg2.value)
3838 && char_type_p (type2))
3839 {
3840 auto_diagnostic_group d;
3841 if (warning_at (location, OPT_Wpointer_compare,
3842 "comparison between pointer and zero character "
3843 "constant"))
3844 inform (arg1.get_start (),
3845 "did you mean to dereference the pointer?");
3846 }
3847 else if (POINTER_TYPE_P (type2)
3848 && null_pointer_constant_p (arg1.value)
3849 && char_type_p (type1))
3850 {
3851 auto_diagnostic_group d;
3852 if (warning_at (location, OPT_Wpointer_compare,
3853 "comparison between pointer and zero character "
3854 "constant"))
3855 inform (arg2.get_start (),
3856 "did you mean to dereference the pointer?");
3857 }
3858 }
3859 else if (TREE_CODE_CLASS (code) == tcc_comparison
3860 && (code1 == STRING_CST || code2 == STRING_CST))
3861 warning_at (location, OPT_Waddress,
3862 "comparison with string literal results in unspecified behavior");
3863
3864 if (TREE_OVERFLOW_P (result.value)
3865 && !TREE_OVERFLOW_P (arg1.value)
3866 && !TREE_OVERFLOW_P (arg2.value))
3867 overflow_warning (location, result.value);
3868
3869 /* Warn about comparisons of different enum types. */
3870 if (warn_enum_compare
3871 && TREE_CODE_CLASS (code) == tcc_comparison
3872 && TREE_CODE (type1) == ENUMERAL_TYPE
3873 && TREE_CODE (type2) == ENUMERAL_TYPE
3874 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3875 warning_at (location, OPT_Wenum_compare,
3876 "comparison between %qT and %qT",
3877 type1, type2);
3878
3879 return result;
3880 }
3881 \f
3882 /* Return a tree for the difference of pointers OP0 and OP1.
3883 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3884 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3885
3886 static tree
3887 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3888 {
3889 tree restype = ptrdiff_type_node;
3890 tree result, inttype;
3891
3892 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3893 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3894 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3895 tree orig_op1 = op1;
3896
3897 /* If the operands point into different address spaces, we need to
3898 explicitly convert them to pointers into the common address space
3899 before we can subtract the numerical address values. */
3900 if (as0 != as1)
3901 {
3902 addr_space_t as_common;
3903 tree common_type;
3904
3905 /* Determine the common superset address space. This is guaranteed
3906 to exist because the caller verified that comp_target_types
3907 returned non-zero. */
3908 if (!addr_space_superset (as0, as1, &as_common))
3909 gcc_unreachable ();
3910
3911 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3912 op0 = convert (common_type, op0);
3913 op1 = convert (common_type, op1);
3914 }
3915
3916 /* Determine integer type result of the subtraction. This will usually
3917 be the same as the result type (ptrdiff_t), but may need to be a wider
3918 type if pointers for the address space are wider than ptrdiff_t. */
3919 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3920 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3921 else
3922 inttype = restype;
3923
3924 if (TREE_CODE (target_type) == VOID_TYPE)
3925 pedwarn (loc, OPT_Wpointer_arith,
3926 "pointer of type %<void *%> used in subtraction");
3927 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3928 pedwarn (loc, OPT_Wpointer_arith,
3929 "pointer to a function used in subtraction");
3930
3931 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3932 {
3933 gcc_assert (current_function_decl != NULL_TREE);
3934
3935 op0 = save_expr (op0);
3936 op1 = save_expr (op1);
3937
3938 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3939 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3940 }
3941
3942 /* First do the subtraction, then build the divide operator
3943 and only convert at the very end.
3944 Do not do default conversions in case restype is a short type. */
3945
3946 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3947 pointers. If some platform cannot provide that, or has a larger
3948 ptrdiff_type to support differences larger than half the address
3949 space, cast the pointers to some larger integer type and do the
3950 computations in that type. */
3951 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3952 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3953 convert (inttype, op1), false);
3954 else
3955 {
3956 /* Cast away qualifiers. */
3957 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
3958 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
3959 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3960 }
3961
3962 /* This generates an error if op1 is pointer to incomplete type. */
3963 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3964 error_at (loc, "arithmetic on pointer to an incomplete type");
3965
3966 op1 = c_size_in_bytes (target_type);
3967
3968 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3969 error_at (loc, "arithmetic on pointer to an empty aggregate");
3970
3971 /* Divide by the size, in easiest possible way. */
3972 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3973 op0, convert (inttype, op1));
3974
3975 /* Convert to final result type if necessary. */
3976 return convert (restype, result);
3977 }
3978 \f
3979 /* Expand atomic compound assignments into an appropriate sequence as
3980 specified by the C11 standard section 6.5.16.2.
3981
3982 _Atomic T1 E1
3983 T2 E2
3984 E1 op= E2
3985
3986 This sequence is used for all types for which these operations are
3987 supported.
3988
3989 In addition, built-in versions of the 'fe' prefixed routines may
3990 need to be invoked for floating point (real, complex or vector) when
3991 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3992
3993 T1 newval;
3994 T1 old;
3995 T1 *addr
3996 T2 val
3997 fenv_t fenv
3998
3999 addr = &E1;
4000 val = (E2);
4001 __atomic_load (addr, &old, SEQ_CST);
4002 feholdexcept (&fenv);
4003 loop:
4004 newval = old op val;
4005 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4006 SEQ_CST))
4007 goto done;
4008 feclearexcept (FE_ALL_EXCEPT);
4009 goto loop:
4010 done:
4011 feupdateenv (&fenv);
4012
4013 The compiler will issue the __atomic_fetch_* built-in when possible,
4014 otherwise it will generate the generic form of the atomic operations.
4015 This requires temp(s) and has their address taken. The atomic processing
4016 is smart enough to figure out when the size of an object can utilize
4017 a lock-free version, and convert the built-in call to the appropriate
4018 lock-free routine. The optimizers will then dispose of any temps that
4019 are no longer required, and lock-free implementations are utilized as
4020 long as there is target support for the required size.
4021
4022 If the operator is NOP_EXPR, then this is a simple assignment, and
4023 an __atomic_store is issued to perform the assignment rather than
4024 the above loop. */
4025
4026 /* Build an atomic assignment at LOC, expanding into the proper
4027 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4028 the result of the operation, unless RETURN_OLD_P, in which case
4029 return the old value of LHS (this is only for postincrement and
4030 postdecrement). */
4031
4032 static tree
4033 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4034 tree rhs, bool return_old_p)
4035 {
4036 tree fndecl, func_call;
4037 vec<tree, va_gc> *params;
4038 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4039 tree old, old_addr;
4040 tree compound_stmt;
4041 tree stmt, goto_stmt;
4042 tree loop_label, loop_decl, done_label, done_decl;
4043
4044 tree lhs_type = TREE_TYPE (lhs);
4045 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4046 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4047 tree rhs_semantic_type = TREE_TYPE (rhs);
4048 tree nonatomic_rhs_semantic_type;
4049 tree rhs_type;
4050
4051 gcc_assert (TYPE_ATOMIC (lhs_type));
4052
4053 if (return_old_p)
4054 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4055
4056 /* Allocate enough vector items for a compare_exchange. */
4057 vec_alloc (params, 6);
4058
4059 /* Create a compound statement to hold the sequence of statements
4060 with a loop. */
4061 compound_stmt = c_begin_compound_stmt (false);
4062
4063 /* Remove any excess precision (which is only present here in the
4064 case of compound assignments). */
4065 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4066 {
4067 gcc_assert (modifycode != NOP_EXPR);
4068 rhs = TREE_OPERAND (rhs, 0);
4069 }
4070 rhs_type = TREE_TYPE (rhs);
4071
4072 /* Fold the RHS if it hasn't already been folded. */
4073 if (modifycode != NOP_EXPR)
4074 rhs = c_fully_fold (rhs, false, NULL);
4075
4076 /* Remove the qualifiers for the rest of the expressions and create
4077 the VAL temp variable to hold the RHS. */
4078 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4079 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4080 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4081 TYPE_UNQUALIFIED);
4082 val = create_tmp_var_raw (nonatomic_rhs_type);
4083 TREE_ADDRESSABLE (val) = 1;
4084 TREE_NO_WARNING (val) = 1;
4085 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4086 NULL_TREE);
4087 SET_EXPR_LOCATION (rhs, loc);
4088 add_stmt (rhs);
4089
4090 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4091 an atomic_store. */
4092 if (modifycode == NOP_EXPR)
4093 {
4094 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4095 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4096 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4097 params->quick_push (lhs_addr);
4098 params->quick_push (rhs);
4099 params->quick_push (seq_cst);
4100 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4101 add_stmt (func_call);
4102
4103 /* Finish the compound statement. */
4104 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4105
4106 /* VAL is the value which was stored, return a COMPOUND_STMT of
4107 the statement and that value. */
4108 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4109 }
4110
4111 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4112 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4113 isn't applicable for such builtins. ??? Do we want to handle enums? */
4114 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4115 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4116 {
4117 built_in_function fncode;
4118 switch (modifycode)
4119 {
4120 case PLUS_EXPR:
4121 case POINTER_PLUS_EXPR:
4122 fncode = (return_old_p
4123 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4124 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4125 break;
4126 case MINUS_EXPR:
4127 fncode = (return_old_p
4128 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4129 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4130 break;
4131 case BIT_AND_EXPR:
4132 fncode = (return_old_p
4133 ? BUILT_IN_ATOMIC_FETCH_AND_N
4134 : BUILT_IN_ATOMIC_AND_FETCH_N);
4135 break;
4136 case BIT_IOR_EXPR:
4137 fncode = (return_old_p
4138 ? BUILT_IN_ATOMIC_FETCH_OR_N
4139 : BUILT_IN_ATOMIC_OR_FETCH_N);
4140 break;
4141 case BIT_XOR_EXPR:
4142 fncode = (return_old_p
4143 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4144 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4145 break;
4146 default:
4147 goto cas_loop;
4148 }
4149
4150 /* We can only use "_1" through "_16" variants of the atomic fetch
4151 built-ins. */
4152 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4153 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4154 goto cas_loop;
4155
4156 /* If this is a pointer type, we need to multiply by the size of
4157 the pointer target type. */
4158 if (POINTER_TYPE_P (lhs_type))
4159 {
4160 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4161 /* ??? This would introduce -Wdiscarded-qualifiers
4162 warning: __atomic_fetch_* expect volatile void *
4163 type as the first argument. (Assignments between
4164 atomic and non-atomic objects are OK.) */
4165 || TYPE_RESTRICT (lhs_type))
4166 goto cas_loop;
4167 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4168 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4169 convert (ptrdiff_type_node, rhs),
4170 convert (ptrdiff_type_node, sz));
4171 }
4172
4173 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4174 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4175 fndecl = builtin_decl_explicit (fncode);
4176 params->quick_push (lhs_addr);
4177 params->quick_push (rhs);
4178 params->quick_push (seq_cst);
4179 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4180
4181 newval = create_tmp_var_raw (nonatomic_lhs_type);
4182 TREE_ADDRESSABLE (newval) = 1;
4183 TREE_NO_WARNING (newval) = 1;
4184 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4185 NULL_TREE, NULL_TREE);
4186 SET_EXPR_LOCATION (rhs, loc);
4187 add_stmt (rhs);
4188
4189 /* Finish the compound statement. */
4190 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4191
4192 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4193 the statement and that value. */
4194 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4195 }
4196
4197 cas_loop:
4198 /* Create the variables and labels required for the op= form. */
4199 old = create_tmp_var_raw (nonatomic_lhs_type);
4200 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4201 TREE_ADDRESSABLE (old) = 1;
4202 TREE_NO_WARNING (old) = 1;
4203
4204 newval = create_tmp_var_raw (nonatomic_lhs_type);
4205 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4206 TREE_ADDRESSABLE (newval) = 1;
4207 TREE_NO_WARNING (newval) = 1;
4208
4209 loop_decl = create_artificial_label (loc);
4210 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4211
4212 done_decl = create_artificial_label (loc);
4213 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4214
4215 /* __atomic_load (addr, &old, SEQ_CST). */
4216 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4217 params->quick_push (lhs_addr);
4218 params->quick_push (old_addr);
4219 params->quick_push (seq_cst);
4220 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4221 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4222 NULL_TREE);
4223 add_stmt (old);
4224 params->truncate (0);
4225
4226 /* Create the expressions for floating-point environment
4227 manipulation, if required. */
4228 bool need_fenv = (flag_trapping_math
4229 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4230 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4231 if (need_fenv)
4232 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4233
4234 if (hold_call)
4235 add_stmt (hold_call);
4236
4237 /* loop: */
4238 add_stmt (loop_label);
4239
4240 /* newval = old + val; */
4241 if (rhs_type != rhs_semantic_type)
4242 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4243 rhs = build_binary_op (loc, modifycode, old, val, true);
4244 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4245 {
4246 tree eptype = TREE_TYPE (rhs);
4247 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4248 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4249 }
4250 else
4251 rhs = c_fully_fold (rhs, false, NULL);
4252 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4253 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4254 NULL_TREE, 0);
4255 if (rhs != error_mark_node)
4256 {
4257 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4258 NULL_TREE);
4259 SET_EXPR_LOCATION (rhs, loc);
4260 add_stmt (rhs);
4261 }
4262
4263 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4264 goto done; */
4265 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4266 params->quick_push (lhs_addr);
4267 params->quick_push (old_addr);
4268 params->quick_push (newval_addr);
4269 params->quick_push (integer_zero_node);
4270 params->quick_push (seq_cst);
4271 params->quick_push (seq_cst);
4272 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4273
4274 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4275 SET_EXPR_LOCATION (goto_stmt, loc);
4276
4277 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4278 SET_EXPR_LOCATION (stmt, loc);
4279 add_stmt (stmt);
4280
4281 if (clear_call)
4282 add_stmt (clear_call);
4283
4284 /* goto loop; */
4285 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4286 SET_EXPR_LOCATION (goto_stmt, loc);
4287 add_stmt (goto_stmt);
4288
4289 /* done: */
4290 add_stmt (done_label);
4291
4292 if (update_call)
4293 add_stmt (update_call);
4294
4295 /* Finish the compound statement. */
4296 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4297
4298 /* NEWVAL is the value that was successfully stored, return a
4299 COMPOUND_EXPR of the statement and the appropriate value. */
4300 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4301 return_old_p ? old : newval);
4302 }
4303
4304 /* Construct and perhaps optimize a tree representation
4305 for a unary operation. CODE, a tree_code, specifies the operation
4306 and XARG is the operand.
4307 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4308 promotions (such as from short to int).
4309 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4310 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4311 to pointers in C99.
4312
4313 LOCATION is the location of the operator. */
4314
4315 tree
4316 build_unary_op (location_t location, enum tree_code code, tree xarg,
4317 bool noconvert)
4318 {
4319 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4320 tree arg = xarg;
4321 tree argtype = NULL_TREE;
4322 enum tree_code typecode;
4323 tree val;
4324 tree ret = error_mark_node;
4325 tree eptype = NULL_TREE;
4326 const char *invalid_op_diag;
4327 bool int_operands;
4328
4329 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4330 if (int_operands)
4331 arg = remove_c_maybe_const_expr (arg);
4332
4333 if (code != ADDR_EXPR)
4334 arg = require_complete_type (location, arg);
4335
4336 typecode = TREE_CODE (TREE_TYPE (arg));
4337 if (typecode == ERROR_MARK)
4338 return error_mark_node;
4339 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4340 typecode = INTEGER_TYPE;
4341
4342 if ((invalid_op_diag
4343 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4344 {
4345 error_at (location, invalid_op_diag);
4346 return error_mark_node;
4347 }
4348
4349 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4350 {
4351 eptype = TREE_TYPE (arg);
4352 arg = TREE_OPERAND (arg, 0);
4353 }
4354
4355 switch (code)
4356 {
4357 case CONVERT_EXPR:
4358 /* This is used for unary plus, because a CONVERT_EXPR
4359 is enough to prevent anybody from looking inside for
4360 associativity, but won't generate any code. */
4361 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4362 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4363 || typecode == VECTOR_TYPE))
4364 {
4365 error_at (location, "wrong type argument to unary plus");
4366 return error_mark_node;
4367 }
4368 else if (!noconvert)
4369 arg = default_conversion (arg);
4370 arg = non_lvalue_loc (location, arg);
4371 break;
4372
4373 case NEGATE_EXPR:
4374 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4375 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4376 || typecode == VECTOR_TYPE))
4377 {
4378 error_at (location, "wrong type argument to unary minus");
4379 return error_mark_node;
4380 }
4381 else if (!noconvert)
4382 arg = default_conversion (arg);
4383 break;
4384
4385 case BIT_NOT_EXPR:
4386 /* ~ works on integer types and non float vectors. */
4387 if (typecode == INTEGER_TYPE
4388 || (typecode == VECTOR_TYPE
4389 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4390 {
4391 tree e = arg;
4392
4393 /* Warn if the expression has boolean value. */
4394 while (TREE_CODE (e) == COMPOUND_EXPR)
4395 e = TREE_OPERAND (e, 1);
4396
4397 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4398 || truth_value_p (TREE_CODE (e))))
4399 {
4400 auto_diagnostic_group d;
4401 if (warning_at (location, OPT_Wbool_operation,
4402 "%<~%> on a boolean expression"))
4403 {
4404 gcc_rich_location richloc (location);
4405 richloc.add_fixit_insert_before (location, "!");
4406 inform (&richloc, "did you mean to use logical not?");
4407 }
4408 }
4409 if (!noconvert)
4410 arg = default_conversion (arg);
4411 }
4412 else if (typecode == COMPLEX_TYPE)
4413 {
4414 code = CONJ_EXPR;
4415 pedwarn (location, OPT_Wpedantic,
4416 "ISO C does not support %<~%> for complex conjugation");
4417 if (!noconvert)
4418 arg = default_conversion (arg);
4419 }
4420 else
4421 {
4422 error_at (location, "wrong type argument to bit-complement");
4423 return error_mark_node;
4424 }
4425 break;
4426
4427 case ABS_EXPR:
4428 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4429 {
4430 error_at (location, "wrong type argument to abs");
4431 return error_mark_node;
4432 }
4433 else if (!noconvert)
4434 arg = default_conversion (arg);
4435 break;
4436
4437 case ABSU_EXPR:
4438 if (!(typecode == INTEGER_TYPE))
4439 {
4440 error_at (location, "wrong type argument to absu");
4441 return error_mark_node;
4442 }
4443 else if (!noconvert)
4444 arg = default_conversion (arg);
4445 break;
4446
4447 case CONJ_EXPR:
4448 /* Conjugating a real value is a no-op, but allow it anyway. */
4449 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4450 || typecode == COMPLEX_TYPE))
4451 {
4452 error_at (location, "wrong type argument to conjugation");
4453 return error_mark_node;
4454 }
4455 else if (!noconvert)
4456 arg = default_conversion (arg);
4457 break;
4458
4459 case TRUTH_NOT_EXPR:
4460 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4461 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4462 && typecode != COMPLEX_TYPE)
4463 {
4464 error_at (location,
4465 "wrong type argument to unary exclamation mark");
4466 return error_mark_node;
4467 }
4468 if (int_operands)
4469 {
4470 arg = c_objc_common_truthvalue_conversion (location, xarg);
4471 arg = remove_c_maybe_const_expr (arg);
4472 }
4473 else
4474 arg = c_objc_common_truthvalue_conversion (location, arg);
4475 ret = invert_truthvalue_loc (location, arg);
4476 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4477 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4478 location = EXPR_LOCATION (ret);
4479 goto return_build_unary_op;
4480
4481 case REALPART_EXPR:
4482 case IMAGPART_EXPR:
4483 ret = build_real_imag_expr (location, code, arg);
4484 if (ret == error_mark_node)
4485 return error_mark_node;
4486 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4487 eptype = TREE_TYPE (eptype);
4488 goto return_build_unary_op;
4489
4490 case PREINCREMENT_EXPR:
4491 case POSTINCREMENT_EXPR:
4492 case PREDECREMENT_EXPR:
4493 case POSTDECREMENT_EXPR:
4494
4495 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4496 {
4497 tree inner = build_unary_op (location, code,
4498 C_MAYBE_CONST_EXPR_EXPR (arg),
4499 noconvert);
4500 if (inner == error_mark_node)
4501 return error_mark_node;
4502 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4503 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4504 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4505 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4506 goto return_build_unary_op;
4507 }
4508
4509 /* Complain about anything that is not a true lvalue. In
4510 Objective-C, skip this check for property_refs. */
4511 if (!objc_is_property_ref (arg)
4512 && !lvalue_or_else (location,
4513 arg, ((code == PREINCREMENT_EXPR
4514 || code == POSTINCREMENT_EXPR)
4515 ? lv_increment
4516 : lv_decrement)))
4517 return error_mark_node;
4518
4519 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4520 {
4521 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4522 warning_at (location, OPT_Wc___compat,
4523 "increment of enumeration value is invalid in C++");
4524 else
4525 warning_at (location, OPT_Wc___compat,
4526 "decrement of enumeration value is invalid in C++");
4527 }
4528
4529 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4530 {
4531 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4532 warning_at (location, OPT_Wbool_operation,
4533 "increment of a boolean expression");
4534 else
4535 warning_at (location, OPT_Wbool_operation,
4536 "decrement of a boolean expression");
4537 }
4538
4539 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4540 arg = c_fully_fold (arg, false, NULL, true);
4541
4542 bool atomic_op;
4543 atomic_op = really_atomic_lvalue (arg);
4544
4545 /* Increment or decrement the real part of the value,
4546 and don't change the imaginary part. */
4547 if (typecode == COMPLEX_TYPE)
4548 {
4549 tree real, imag;
4550
4551 pedwarn (location, OPT_Wpedantic,
4552 "ISO C does not support %<++%> and %<--%> on complex types");
4553
4554 if (!atomic_op)
4555 {
4556 arg = stabilize_reference (arg);
4557 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4558 true);
4559 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4560 true);
4561 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4562 if (real == error_mark_node || imag == error_mark_node)
4563 return error_mark_node;
4564 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4565 real, imag);
4566 goto return_build_unary_op;
4567 }
4568 }
4569
4570 /* Report invalid types. */
4571
4572 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4573 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4574 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4575 {
4576 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4577 error_at (location, "wrong type argument to increment");
4578 else
4579 error_at (location, "wrong type argument to decrement");
4580
4581 return error_mark_node;
4582 }
4583
4584 {
4585 tree inc;
4586
4587 argtype = TREE_TYPE (arg);
4588
4589 /* Compute the increment. */
4590
4591 if (typecode == POINTER_TYPE)
4592 {
4593 /* If pointer target is an incomplete type,
4594 we just cannot know how to do the arithmetic. */
4595 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4596 {
4597 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4598 error_at (location,
4599 "increment of pointer to an incomplete type %qT",
4600 TREE_TYPE (argtype));
4601 else
4602 error_at (location,
4603 "decrement of pointer to an incomplete type %qT",
4604 TREE_TYPE (argtype));
4605 }
4606 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4607 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4608 {
4609 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4610 pedwarn (location, OPT_Wpointer_arith,
4611 "wrong type argument to increment");
4612 else
4613 pedwarn (location, OPT_Wpointer_arith,
4614 "wrong type argument to decrement");
4615 }
4616
4617 inc = c_size_in_bytes (TREE_TYPE (argtype));
4618 inc = convert_to_ptrofftype_loc (location, inc);
4619 }
4620 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4621 {
4622 /* For signed fract types, we invert ++ to -- or
4623 -- to ++, and change inc from 1 to -1, because
4624 it is not possible to represent 1 in signed fract constants.
4625 For unsigned fract types, the result always overflows and
4626 we get an undefined (original) or the maximum value. */
4627 if (code == PREINCREMENT_EXPR)
4628 code = PREDECREMENT_EXPR;
4629 else if (code == PREDECREMENT_EXPR)
4630 code = PREINCREMENT_EXPR;
4631 else if (code == POSTINCREMENT_EXPR)
4632 code = POSTDECREMENT_EXPR;
4633 else /* code == POSTDECREMENT_EXPR */
4634 code = POSTINCREMENT_EXPR;
4635
4636 inc = integer_minus_one_node;
4637 inc = convert (argtype, inc);
4638 }
4639 else
4640 {
4641 inc = VECTOR_TYPE_P (argtype)
4642 ? build_one_cst (argtype)
4643 : integer_one_node;
4644 inc = convert (argtype, inc);
4645 }
4646
4647 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4648 need to ask Objective-C to build the increment or decrement
4649 expression for it. */
4650 if (objc_is_property_ref (arg))
4651 return objc_build_incr_expr_for_property_ref (location, code,
4652 arg, inc);
4653
4654 /* Report a read-only lvalue. */
4655 if (TYPE_READONLY (argtype))
4656 {
4657 readonly_error (location, arg,
4658 ((code == PREINCREMENT_EXPR
4659 || code == POSTINCREMENT_EXPR)
4660 ? lv_increment : lv_decrement));
4661 return error_mark_node;
4662 }
4663 else if (TREE_READONLY (arg))
4664 readonly_warning (arg,
4665 ((code == PREINCREMENT_EXPR
4666 || code == POSTINCREMENT_EXPR)
4667 ? lv_increment : lv_decrement));
4668
4669 /* If the argument is atomic, use the special code sequences for
4670 atomic compound assignment. */
4671 if (atomic_op)
4672 {
4673 arg = stabilize_reference (arg);
4674 ret = build_atomic_assign (location, arg,
4675 ((code == PREINCREMENT_EXPR
4676 || code == POSTINCREMENT_EXPR)
4677 ? PLUS_EXPR
4678 : MINUS_EXPR),
4679 (FRACT_MODE_P (TYPE_MODE (argtype))
4680 ? inc
4681 : integer_one_node),
4682 (code == POSTINCREMENT_EXPR
4683 || code == POSTDECREMENT_EXPR));
4684 goto return_build_unary_op;
4685 }
4686
4687 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4688 val = boolean_increment (code, arg);
4689 else
4690 val = build2 (code, TREE_TYPE (arg), arg, inc);
4691 TREE_SIDE_EFFECTS (val) = 1;
4692 if (TREE_CODE (val) != code)
4693 TREE_NO_WARNING (val) = 1;
4694 ret = val;
4695 goto return_build_unary_op;
4696 }
4697
4698 case ADDR_EXPR:
4699 /* Note that this operation never does default_conversion. */
4700
4701 /* The operand of unary '&' must be an lvalue (which excludes
4702 expressions of type void), or, in C99, the result of a [] or
4703 unary '*' operator. */
4704 if (VOID_TYPE_P (TREE_TYPE (arg))
4705 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4706 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4707 pedwarn (location, 0, "taking address of expression of type %<void%>");
4708
4709 /* Let &* cancel out to simplify resulting code. */
4710 if (INDIRECT_REF_P (arg))
4711 {
4712 /* Don't let this be an lvalue. */
4713 if (lvalue_p (TREE_OPERAND (arg, 0)))
4714 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4715 ret = TREE_OPERAND (arg, 0);
4716 goto return_build_unary_op;
4717 }
4718
4719 /* Anything not already handled and not a true memory reference
4720 or a non-lvalue array is an error. */
4721 if (typecode != FUNCTION_TYPE && !noconvert
4722 && !lvalue_or_else (location, arg, lv_addressof))
4723 return error_mark_node;
4724
4725 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4726 folding later. */
4727 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4728 {
4729 tree inner = build_unary_op (location, code,
4730 C_MAYBE_CONST_EXPR_EXPR (arg),
4731 noconvert);
4732 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4733 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4734 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4735 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4736 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4737 goto return_build_unary_op;
4738 }
4739
4740 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4741 argtype = TREE_TYPE (arg);
4742
4743 /* If the lvalue is const or volatile, merge that into the type
4744 to which the address will point. This is only needed
4745 for function types. */
4746 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4747 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4748 && TREE_CODE (argtype) == FUNCTION_TYPE)
4749 {
4750 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4751 int quals = orig_quals;
4752
4753 if (TREE_READONLY (arg))
4754 quals |= TYPE_QUAL_CONST;
4755 if (TREE_THIS_VOLATILE (arg))
4756 quals |= TYPE_QUAL_VOLATILE;
4757
4758 argtype = c_build_qualified_type (argtype, quals);
4759 }
4760
4761 switch (TREE_CODE (arg))
4762 {
4763 case COMPONENT_REF:
4764 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4765 {
4766 error_at (location, "cannot take address of bit-field %qD",
4767 TREE_OPERAND (arg, 1));
4768 return error_mark_node;
4769 }
4770
4771 /* fall through */
4772
4773 case ARRAY_REF:
4774 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4775 {
4776 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4777 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4778 {
4779 error_at (location, "cannot take address of scalar with "
4780 "reverse storage order");
4781 return error_mark_node;
4782 }
4783
4784 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4785 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4786 warning_at (location, OPT_Wscalar_storage_order,
4787 "address of array with reverse scalar storage "
4788 "order requested");
4789 }
4790
4791 default:
4792 break;
4793 }
4794
4795 if (!c_mark_addressable (arg))
4796 return error_mark_node;
4797
4798 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4799 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4800
4801 argtype = build_pointer_type (argtype);
4802
4803 /* ??? Cope with user tricks that amount to offsetof. Delete this
4804 when we have proper support for integer constant expressions. */
4805 val = get_base_address (arg);
4806 if (val && INDIRECT_REF_P (val)
4807 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4808 {
4809 ret = fold_offsetof (arg, argtype);
4810 goto return_build_unary_op;
4811 }
4812
4813 val = build1 (ADDR_EXPR, argtype, arg);
4814
4815 ret = val;
4816 goto return_build_unary_op;
4817
4818 default:
4819 gcc_unreachable ();
4820 }
4821
4822 if (argtype == NULL_TREE)
4823 argtype = TREE_TYPE (arg);
4824 if (TREE_CODE (arg) == INTEGER_CST)
4825 ret = (require_constant_value
4826 ? fold_build1_initializer_loc (location, code, argtype, arg)
4827 : fold_build1_loc (location, code, argtype, arg));
4828 else
4829 ret = build1 (code, argtype, arg);
4830 return_build_unary_op:
4831 gcc_assert (ret != error_mark_node);
4832 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4833 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4834 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4835 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4836 ret = note_integer_operands (ret);
4837 if (eptype)
4838 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4839 protected_set_expr_location (ret, location);
4840 return ret;
4841 }
4842
4843 /* Return nonzero if REF is an lvalue valid for this language.
4844 Lvalues can be assigned, unless their type has TYPE_READONLY.
4845 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4846
4847 bool
4848 lvalue_p (const_tree ref)
4849 {
4850 const enum tree_code code = TREE_CODE (ref);
4851
4852 switch (code)
4853 {
4854 case REALPART_EXPR:
4855 case IMAGPART_EXPR:
4856 case COMPONENT_REF:
4857 return lvalue_p (TREE_OPERAND (ref, 0));
4858
4859 case C_MAYBE_CONST_EXPR:
4860 return lvalue_p (TREE_OPERAND (ref, 1));
4861
4862 case COMPOUND_LITERAL_EXPR:
4863 case STRING_CST:
4864 return true;
4865
4866 case INDIRECT_REF:
4867 case ARRAY_REF:
4868 case VAR_DECL:
4869 case PARM_DECL:
4870 case RESULT_DECL:
4871 case ERROR_MARK:
4872 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4873 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4874
4875 case BIND_EXPR:
4876 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4877
4878 default:
4879 return false;
4880 }
4881 }
4882 \f
4883 /* Give a warning for storing in something that is read-only in GCC
4884 terms but not const in ISO C terms. */
4885
4886 static void
4887 readonly_warning (tree arg, enum lvalue_use use)
4888 {
4889 switch (use)
4890 {
4891 case lv_assign:
4892 warning (0, "assignment of read-only location %qE", arg);
4893 break;
4894 case lv_increment:
4895 warning (0, "increment of read-only location %qE", arg);
4896 break;
4897 case lv_decrement:
4898 warning (0, "decrement of read-only location %qE", arg);
4899 break;
4900 default:
4901 gcc_unreachable ();
4902 }
4903 return;
4904 }
4905
4906
4907 /* Return nonzero if REF is an lvalue valid for this language;
4908 otherwise, print an error message and return zero. USE says
4909 how the lvalue is being used and so selects the error message.
4910 LOCATION is the location at which any error should be reported. */
4911
4912 static int
4913 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4914 {
4915 int win = lvalue_p (ref);
4916
4917 if (!win)
4918 lvalue_error (loc, use);
4919
4920 return win;
4921 }
4922 \f
4923 /* Mark EXP saying that we need to be able to take the
4924 address of it; it should not be allocated in a register.
4925 Returns true if successful. ARRAY_REF_P is true if this
4926 is for ARRAY_REF construction - in that case we don't want
4927 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4928 it is fine to use ARRAY_REFs for vector subscripts on vector
4929 register variables. */
4930
4931 bool
4932 c_mark_addressable (tree exp, bool array_ref_p)
4933 {
4934 tree x = exp;
4935
4936 while (1)
4937 switch (TREE_CODE (x))
4938 {
4939 case VIEW_CONVERT_EXPR:
4940 if (array_ref_p
4941 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4942 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4943 return true;
4944 /* FALLTHRU */
4945 case COMPONENT_REF:
4946 case ADDR_EXPR:
4947 case ARRAY_REF:
4948 case REALPART_EXPR:
4949 case IMAGPART_EXPR:
4950 x = TREE_OPERAND (x, 0);
4951 break;
4952
4953 case COMPOUND_LITERAL_EXPR:
4954 TREE_ADDRESSABLE (x) = 1;
4955 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
4956 return true;
4957
4958 case CONSTRUCTOR:
4959 TREE_ADDRESSABLE (x) = 1;
4960 return true;
4961
4962 case VAR_DECL:
4963 case CONST_DECL:
4964 case PARM_DECL:
4965 case RESULT_DECL:
4966 if (C_DECL_REGISTER (x)
4967 && DECL_NONLOCAL (x))
4968 {
4969 if (TREE_PUBLIC (x) || is_global_var (x))
4970 {
4971 error
4972 ("global register variable %qD used in nested function", x);
4973 return false;
4974 }
4975 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4976 }
4977 else if (C_DECL_REGISTER (x))
4978 {
4979 if (TREE_PUBLIC (x) || is_global_var (x))
4980 error ("address of global register variable %qD requested", x);
4981 else
4982 error ("address of register variable %qD requested", x);
4983 return false;
4984 }
4985
4986 /* FALLTHRU */
4987 case FUNCTION_DECL:
4988 TREE_ADDRESSABLE (x) = 1;
4989 /* FALLTHRU */
4990 default:
4991 return true;
4992 }
4993 }
4994 \f
4995 /* Convert EXPR to TYPE, warning about conversion problems with
4996 constants. SEMANTIC_TYPE is the type this conversion would use
4997 without excess precision. If SEMANTIC_TYPE is NULL, this function
4998 is equivalent to convert_and_check. This function is a wrapper that
4999 handles conversions that may be different than
5000 the usual ones because of excess precision. */
5001
5002 static tree
5003 ep_convert_and_check (location_t loc, tree type, tree expr,
5004 tree semantic_type)
5005 {
5006 if (TREE_TYPE (expr) == type)
5007 return expr;
5008
5009 /* For C11, integer conversions may have results with excess
5010 precision. */
5011 if (flag_isoc11 || !semantic_type)
5012 return convert_and_check (loc, type, expr);
5013
5014 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5015 && TREE_TYPE (expr) != semantic_type)
5016 {
5017 /* For integers, we need to check the real conversion, not
5018 the conversion to the excess precision type. */
5019 expr = convert_and_check (loc, semantic_type, expr);
5020 }
5021 /* Result type is the excess precision type, which should be
5022 large enough, so do not check. */
5023 return convert (type, expr);
5024 }
5025
5026 /* If EXPR refers to a built-in declared without a prototype returns
5027 the actual type of the built-in and, if non-null, set *BLTIN to
5028 a pointer to the built-in. Otherwise return the type of EXPR
5029 and clear *BLTIN if non-null. */
5030
5031 static tree
5032 type_or_builtin_type (tree expr, tree *bltin = NULL)
5033 {
5034 tree dummy;
5035 if (!bltin)
5036 bltin = &dummy;
5037
5038 *bltin = NULL_TREE;
5039
5040 tree type = TREE_TYPE (expr);
5041 if (TREE_CODE (expr) != ADDR_EXPR)
5042 return type;
5043
5044 tree oper = TREE_OPERAND (expr, 0);
5045 if (!DECL_P (oper)
5046 || TREE_CODE (oper) != FUNCTION_DECL
5047 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5048 return type;
5049
5050 built_in_function code = DECL_FUNCTION_CODE (oper);
5051 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5052 return type;
5053
5054 if ((*bltin = builtin_decl_implicit (code)))
5055 type = build_pointer_type (TREE_TYPE (*bltin));
5056
5057 return type;
5058 }
5059
5060 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5061 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5062 if folded to an integer constant then the unselected half may
5063 contain arbitrary operations not normally permitted in constant
5064 expressions. Set the location of the expression to LOC. */
5065
5066 tree
5067 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5068 tree op1, tree op1_original_type, location_t op1_loc,
5069 tree op2, tree op2_original_type, location_t op2_loc)
5070 {
5071 tree type1;
5072 tree type2;
5073 enum tree_code code1;
5074 enum tree_code code2;
5075 tree result_type = NULL;
5076 tree semantic_result_type = NULL;
5077 tree orig_op1 = op1, orig_op2 = op2;
5078 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5079 bool ifexp_int_operands;
5080 tree ret;
5081
5082 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5083 if (op1_int_operands)
5084 op1 = remove_c_maybe_const_expr (op1);
5085 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5086 if (op2_int_operands)
5087 op2 = remove_c_maybe_const_expr (op2);
5088 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5089 if (ifexp_int_operands)
5090 ifexp = remove_c_maybe_const_expr (ifexp);
5091
5092 /* Promote both alternatives. */
5093
5094 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5095 op1 = default_conversion (op1);
5096 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5097 op2 = default_conversion (op2);
5098
5099 if (TREE_CODE (ifexp) == ERROR_MARK
5100 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5101 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5102 return error_mark_node;
5103
5104 tree bltin1 = NULL_TREE;
5105 tree bltin2 = NULL_TREE;
5106 type1 = type_or_builtin_type (op1, &bltin1);
5107 code1 = TREE_CODE (type1);
5108 type2 = type_or_builtin_type (op2, &bltin2);
5109 code2 = TREE_CODE (type2);
5110
5111 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5112 return error_mark_node;
5113
5114 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5115 return error_mark_node;
5116
5117 /* C90 does not permit non-lvalue arrays in conditional expressions.
5118 In C99 they will be pointers by now. */
5119 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5120 {
5121 error_at (colon_loc, "non-lvalue array in conditional expression");
5122 return error_mark_node;
5123 }
5124
5125 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5126 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5127 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5128 || code1 == COMPLEX_TYPE)
5129 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5130 || code2 == COMPLEX_TYPE))
5131 {
5132 semantic_result_type = c_common_type (type1, type2);
5133 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5134 {
5135 op1 = TREE_OPERAND (op1, 0);
5136 type1 = TREE_TYPE (op1);
5137 gcc_assert (TREE_CODE (type1) == code1);
5138 }
5139 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5140 {
5141 op2 = TREE_OPERAND (op2, 0);
5142 type2 = TREE_TYPE (op2);
5143 gcc_assert (TREE_CODE (type2) == code2);
5144 }
5145 }
5146
5147 if (warn_cxx_compat)
5148 {
5149 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5150 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5151
5152 if (TREE_CODE (t1) == ENUMERAL_TYPE
5153 && TREE_CODE (t2) == ENUMERAL_TYPE
5154 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5155 warning_at (colon_loc, OPT_Wc___compat,
5156 ("different enum types in conditional is "
5157 "invalid in C++: %qT vs %qT"),
5158 t1, t2);
5159 }
5160
5161 /* Quickly detect the usual case where op1 and op2 have the same type
5162 after promotion. */
5163 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5164 {
5165 if (type1 == type2)
5166 result_type = type1;
5167 else
5168 result_type = TYPE_MAIN_VARIANT (type1);
5169 }
5170 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5171 || code1 == COMPLEX_TYPE)
5172 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5173 || code2 == COMPLEX_TYPE))
5174 {
5175 /* In C11, a conditional expression between a floating-point
5176 type and an integer type should convert the integer type to
5177 the evaluation format of the floating-point type, with
5178 possible excess precision. */
5179 tree eptype1 = type1;
5180 tree eptype2 = type2;
5181 if (flag_isoc11)
5182 {
5183 tree eptype;
5184 if (ANY_INTEGRAL_TYPE_P (type1)
5185 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5186 {
5187 eptype2 = eptype;
5188 if (!semantic_result_type)
5189 semantic_result_type = c_common_type (type1, type2);
5190 }
5191 else if (ANY_INTEGRAL_TYPE_P (type2)
5192 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5193 {
5194 eptype1 = eptype;
5195 if (!semantic_result_type)
5196 semantic_result_type = c_common_type (type1, type2);
5197 }
5198 }
5199 result_type = c_common_type (eptype1, eptype2);
5200 if (result_type == error_mark_node)
5201 return error_mark_node;
5202 do_warn_double_promotion (result_type, type1, type2,
5203 "implicit conversion from %qT to %qT to "
5204 "match other result of conditional",
5205 colon_loc);
5206
5207 /* If -Wsign-compare, warn here if type1 and type2 have
5208 different signedness. We'll promote the signed to unsigned
5209 and later code won't know it used to be different.
5210 Do this check on the original types, so that explicit casts
5211 will be considered, but default promotions won't. */
5212 if (c_inhibit_evaluation_warnings == 0)
5213 {
5214 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5215 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5216
5217 if (unsigned_op1 ^ unsigned_op2)
5218 {
5219 bool ovf;
5220
5221 /* Do not warn if the result type is signed, since the
5222 signed type will only be chosen if it can represent
5223 all the values of the unsigned type. */
5224 if (!TYPE_UNSIGNED (result_type))
5225 /* OK */;
5226 else
5227 {
5228 bool op1_maybe_const = true;
5229 bool op2_maybe_const = true;
5230
5231 /* Do not warn if the signed quantity is an
5232 unsuffixed integer literal (or some static
5233 constant expression involving such literals) and
5234 it is non-negative. This warning requires the
5235 operands to be folded for best results, so do
5236 that folding in this case even without
5237 warn_sign_compare to avoid warning options
5238 possibly affecting code generation. */
5239 c_inhibit_evaluation_warnings
5240 += (ifexp == truthvalue_false_node);
5241 op1 = c_fully_fold (op1, require_constant_value,
5242 &op1_maybe_const);
5243 c_inhibit_evaluation_warnings
5244 -= (ifexp == truthvalue_false_node);
5245
5246 c_inhibit_evaluation_warnings
5247 += (ifexp == truthvalue_true_node);
5248 op2 = c_fully_fold (op2, require_constant_value,
5249 &op2_maybe_const);
5250 c_inhibit_evaluation_warnings
5251 -= (ifexp == truthvalue_true_node);
5252
5253 if (warn_sign_compare)
5254 {
5255 if ((unsigned_op2
5256 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5257 || (unsigned_op1
5258 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5259 /* OK */;
5260 else if (unsigned_op2)
5261 warning_at (op1_loc, OPT_Wsign_compare,
5262 "operand of %<?:%> changes signedness from "
5263 "%qT to %qT due to unsignedness of other "
5264 "operand", TREE_TYPE (orig_op1),
5265 TREE_TYPE (orig_op2));
5266 else
5267 warning_at (op2_loc, OPT_Wsign_compare,
5268 "operand of %<?:%> changes signedness from "
5269 "%qT to %qT due to unsignedness of other "
5270 "operand", TREE_TYPE (orig_op2),
5271 TREE_TYPE (orig_op1));
5272 }
5273 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5274 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5275 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5276 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5277 }
5278 }
5279 }
5280 }
5281 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5282 {
5283 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5284 pedwarn (colon_loc, OPT_Wpedantic,
5285 "ISO C forbids conditional expr with only one void side");
5286 result_type = void_type_node;
5287 }
5288 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5289 {
5290 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5291 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5292 addr_space_t as_common;
5293
5294 if (comp_target_types (colon_loc, type1, type2))
5295 result_type = common_pointer_type (type1, type2);
5296 else if (null_pointer_constant_p (orig_op1))
5297 result_type = type2;
5298 else if (null_pointer_constant_p (orig_op2))
5299 result_type = type1;
5300 else if (!addr_space_superset (as1, as2, &as_common))
5301 {
5302 error_at (colon_loc, "pointers to disjoint address spaces "
5303 "used in conditional expression");
5304 return error_mark_node;
5305 }
5306 else if (VOID_TYPE_P (TREE_TYPE (type1))
5307 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5308 {
5309 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5310 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5311 & ~TYPE_QUALS (TREE_TYPE (type1))))
5312 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5313 "pointer to array loses qualifier "
5314 "in conditional expression");
5315
5316 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5317 pedwarn (colon_loc, OPT_Wpedantic,
5318 "ISO C forbids conditional expr between "
5319 "%<void *%> and function pointer");
5320 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5321 TREE_TYPE (type2)));
5322 }
5323 else if (VOID_TYPE_P (TREE_TYPE (type2))
5324 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5325 {
5326 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5327 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5328 & ~TYPE_QUALS (TREE_TYPE (type2))))
5329 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5330 "pointer to array loses qualifier "
5331 "in conditional expression");
5332
5333 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5334 pedwarn (colon_loc, OPT_Wpedantic,
5335 "ISO C forbids conditional expr between "
5336 "%<void *%> and function pointer");
5337 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5338 TREE_TYPE (type1)));
5339 }
5340 /* Objective-C pointer comparisons are a bit more lenient. */
5341 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5342 result_type = objc_common_type (type1, type2);
5343 else
5344 {
5345 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5346 if (bltin1 && bltin2)
5347 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5348 "pointer type mismatch between %qT and %qT "
5349 "of %qD and %qD in conditional expression",
5350 type1, type2, bltin1, bltin2);
5351 else
5352 pedwarn (colon_loc, 0,
5353 "pointer type mismatch in conditional expression");
5354 result_type = build_pointer_type
5355 (build_qualified_type (void_type_node, qual));
5356 }
5357 }
5358 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5359 {
5360 if (!null_pointer_constant_p (orig_op2))
5361 pedwarn (colon_loc, 0,
5362 "pointer/integer type mismatch in conditional expression");
5363 else
5364 {
5365 op2 = null_pointer_node;
5366 }
5367 result_type = type1;
5368 }
5369 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5370 {
5371 if (!null_pointer_constant_p (orig_op1))
5372 pedwarn (colon_loc, 0,
5373 "pointer/integer type mismatch in conditional expression");
5374 else
5375 {
5376 op1 = null_pointer_node;
5377 }
5378 result_type = type2;
5379 }
5380
5381 if (!result_type)
5382 {
5383 if (flag_cond_mismatch)
5384 result_type = void_type_node;
5385 else
5386 {
5387 error_at (colon_loc, "type mismatch in conditional expression");
5388 return error_mark_node;
5389 }
5390 }
5391
5392 /* Merge const and volatile flags of the incoming types. */
5393 result_type
5394 = build_type_variant (result_type,
5395 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5396 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5397
5398 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5399 semantic_result_type);
5400 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5401 semantic_result_type);
5402
5403 if (ifexp_bcp && ifexp == truthvalue_true_node)
5404 {
5405 op2_int_operands = true;
5406 op1 = c_fully_fold (op1, require_constant_value, NULL);
5407 }
5408 if (ifexp_bcp && ifexp == truthvalue_false_node)
5409 {
5410 op1_int_operands = true;
5411 op2 = c_fully_fold (op2, require_constant_value, NULL);
5412 }
5413 int_const = int_operands = (ifexp_int_operands
5414 && op1_int_operands
5415 && op2_int_operands);
5416 if (int_operands)
5417 {
5418 int_const = ((ifexp == truthvalue_true_node
5419 && TREE_CODE (orig_op1) == INTEGER_CST
5420 && !TREE_OVERFLOW (orig_op1))
5421 || (ifexp == truthvalue_false_node
5422 && TREE_CODE (orig_op2) == INTEGER_CST
5423 && !TREE_OVERFLOW (orig_op2)));
5424 }
5425
5426 /* Need to convert condition operand into a vector mask. */
5427 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5428 {
5429 tree vectype = TREE_TYPE (ifexp);
5430 tree elem_type = TREE_TYPE (vectype);
5431 tree zero = build_int_cst (elem_type, 0);
5432 tree zero_vec = build_vector_from_val (vectype, zero);
5433 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5434 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5435 }
5436
5437 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5438 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5439 else
5440 {
5441 if (int_operands)
5442 {
5443 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5444 nested inside of the expression. */
5445 op1 = c_fully_fold (op1, false, NULL);
5446 op2 = c_fully_fold (op2, false, NULL);
5447 }
5448 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5449 if (int_operands)
5450 ret = note_integer_operands (ret);
5451 }
5452 if (semantic_result_type)
5453 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5454
5455 protected_set_expr_location (ret, colon_loc);
5456
5457 /* If the OP1 and OP2 are the same and don't have side-effects,
5458 warn here, because the COND_EXPR will be turned into OP1. */
5459 if (warn_duplicated_branches
5460 && TREE_CODE (ret) == COND_EXPR
5461 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5462 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5463 "this condition has identical branches");
5464
5465 return ret;
5466 }
5467 \f
5468 /* Return a compound expression that performs two expressions and
5469 returns the value of the second of them.
5470
5471 LOC is the location of the COMPOUND_EXPR. */
5472
5473 tree
5474 build_compound_expr (location_t loc, tree expr1, tree expr2)
5475 {
5476 bool expr1_int_operands, expr2_int_operands;
5477 tree eptype = NULL_TREE;
5478 tree ret;
5479
5480 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5481 if (expr1_int_operands)
5482 expr1 = remove_c_maybe_const_expr (expr1);
5483 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5484 if (expr2_int_operands)
5485 expr2 = remove_c_maybe_const_expr (expr2);
5486
5487 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5488 expr1 = TREE_OPERAND (expr1, 0);
5489 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5490 {
5491 eptype = TREE_TYPE (expr2);
5492 expr2 = TREE_OPERAND (expr2, 0);
5493 }
5494
5495 if (!TREE_SIDE_EFFECTS (expr1))
5496 {
5497 /* The left-hand operand of a comma expression is like an expression
5498 statement: with -Wunused, we should warn if it doesn't have
5499 any side-effects, unless it was explicitly cast to (void). */
5500 if (warn_unused_value)
5501 {
5502 if (VOID_TYPE_P (TREE_TYPE (expr1))
5503 && CONVERT_EXPR_P (expr1))
5504 ; /* (void) a, b */
5505 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5506 && TREE_CODE (expr1) == COMPOUND_EXPR
5507 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5508 ; /* (void) a, (void) b, c */
5509 else
5510 warning_at (loc, OPT_Wunused_value,
5511 "left-hand operand of comma expression has no effect");
5512 }
5513 }
5514 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5515 && warn_unused_value)
5516 {
5517 tree r = expr1;
5518 location_t cloc = loc;
5519 while (TREE_CODE (r) == COMPOUND_EXPR)
5520 {
5521 if (EXPR_HAS_LOCATION (r))
5522 cloc = EXPR_LOCATION (r);
5523 r = TREE_OPERAND (r, 1);
5524 }
5525 if (!TREE_SIDE_EFFECTS (r)
5526 && !VOID_TYPE_P (TREE_TYPE (r))
5527 && !CONVERT_EXPR_P (r))
5528 warning_at (cloc, OPT_Wunused_value,
5529 "right-hand operand of comma expression has no effect");
5530 }
5531
5532 /* With -Wunused, we should also warn if the left-hand operand does have
5533 side-effects, but computes a value which is not used. For example, in
5534 `foo() + bar(), baz()' the result of the `+' operator is not used,
5535 so we should issue a warning. */
5536 else if (warn_unused_value)
5537 warn_if_unused_value (expr1, loc);
5538
5539 if (expr2 == error_mark_node)
5540 return error_mark_node;
5541
5542 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5543
5544 if (flag_isoc99
5545 && expr1_int_operands
5546 && expr2_int_operands)
5547 ret = note_integer_operands (ret);
5548
5549 if (eptype)
5550 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5551
5552 protected_set_expr_location (ret, loc);
5553 return ret;
5554 }
5555
5556 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5557 which we are casting. OTYPE is the type of the expression being
5558 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5559 of the cast. -Wcast-qual appeared on the command line. Named
5560 address space qualifiers are not handled here, because they result
5561 in different warnings. */
5562
5563 static void
5564 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5565 {
5566 tree in_type = type;
5567 tree in_otype = otype;
5568 int added = 0;
5569 int discarded = 0;
5570 bool is_const;
5571
5572 /* Check that the qualifiers on IN_TYPE are a superset of the
5573 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5574 nodes is uninteresting and we stop as soon as we hit a
5575 non-POINTER_TYPE node on either type. */
5576 do
5577 {
5578 in_otype = TREE_TYPE (in_otype);
5579 in_type = TREE_TYPE (in_type);
5580
5581 /* GNU C allows cv-qualified function types. 'const' means the
5582 function is very pure, 'volatile' means it can't return. We
5583 need to warn when such qualifiers are added, not when they're
5584 taken away. */
5585 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5586 && TREE_CODE (in_type) == FUNCTION_TYPE)
5587 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5588 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5589 else
5590 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5591 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5592 }
5593 while (TREE_CODE (in_type) == POINTER_TYPE
5594 && TREE_CODE (in_otype) == POINTER_TYPE);
5595
5596 if (added)
5597 warning_at (loc, OPT_Wcast_qual,
5598 "cast adds %q#v qualifier to function type", added);
5599
5600 if (discarded)
5601 /* There are qualifiers present in IN_OTYPE that are not present
5602 in IN_TYPE. */
5603 warning_at (loc, OPT_Wcast_qual,
5604 "cast discards %qv qualifier from pointer target type",
5605 discarded);
5606
5607 if (added || discarded)
5608 return;
5609
5610 /* A cast from **T to const **T is unsafe, because it can cause a
5611 const value to be changed with no additional warning. We only
5612 issue this warning if T is the same on both sides, and we only
5613 issue the warning if there are the same number of pointers on
5614 both sides, as otherwise the cast is clearly unsafe anyhow. A
5615 cast is unsafe when a qualifier is added at one level and const
5616 is not present at all outer levels.
5617
5618 To issue this warning, we check at each level whether the cast
5619 adds new qualifiers not already seen. We don't need to special
5620 case function types, as they won't have the same
5621 TYPE_MAIN_VARIANT. */
5622
5623 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5624 return;
5625 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5626 return;
5627
5628 in_type = type;
5629 in_otype = otype;
5630 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5631 do
5632 {
5633 in_type = TREE_TYPE (in_type);
5634 in_otype = TREE_TYPE (in_otype);
5635 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5636 && !is_const)
5637 {
5638 warning_at (loc, OPT_Wcast_qual,
5639 "to be safe all intermediate pointers in cast from "
5640 "%qT to %qT must be %<const%> qualified",
5641 otype, type);
5642 break;
5643 }
5644 if (is_const)
5645 is_const = TYPE_READONLY (in_type);
5646 }
5647 while (TREE_CODE (in_type) == POINTER_TYPE);
5648 }
5649
5650 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5651
5652 static bool
5653 c_safe_arg_type_equiv_p (tree t1, tree t2)
5654 {
5655 t1 = TYPE_MAIN_VARIANT (t1);
5656 t2 = TYPE_MAIN_VARIANT (t2);
5657
5658 if (TREE_CODE (t1) == POINTER_TYPE
5659 && TREE_CODE (t2) == POINTER_TYPE)
5660 return true;
5661
5662 /* The signedness of the parameter matters only when an integral
5663 type smaller than int is promoted to int, otherwise only the
5664 precision of the parameter matters.
5665 This check should make sure that the callee does not see
5666 undefined values in argument registers. */
5667 if (INTEGRAL_TYPE_P (t1)
5668 && INTEGRAL_TYPE_P (t2)
5669 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5670 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5671 || !targetm.calls.promote_prototypes (NULL_TREE)
5672 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5673 return true;
5674
5675 return comptypes (t1, t2);
5676 }
5677
5678 /* Check if a type cast between two function types can be considered safe. */
5679
5680 static bool
5681 c_safe_function_type_cast_p (tree t1, tree t2)
5682 {
5683 if (TREE_TYPE (t1) == void_type_node &&
5684 TYPE_ARG_TYPES (t1) == void_list_node)
5685 return true;
5686
5687 if (TREE_TYPE (t2) == void_type_node &&
5688 TYPE_ARG_TYPES (t2) == void_list_node)
5689 return true;
5690
5691 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5692 return false;
5693
5694 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5695 t1 && t2;
5696 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5697 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5698 return false;
5699
5700 return true;
5701 }
5702
5703 /* Build an expression representing a cast to type TYPE of expression EXPR.
5704 LOC is the location of the cast-- typically the open paren of the cast. */
5705
5706 tree
5707 build_c_cast (location_t loc, tree type, tree expr)
5708 {
5709 tree value;
5710
5711 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5712 expr = TREE_OPERAND (expr, 0);
5713
5714 value = expr;
5715
5716 if (type == error_mark_node || expr == error_mark_node)
5717 return error_mark_node;
5718
5719 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5720 only in <protocol> qualifications. But when constructing cast expressions,
5721 the protocols do matter and must be kept around. */
5722 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5723 return build1 (NOP_EXPR, type, expr);
5724
5725 type = TYPE_MAIN_VARIANT (type);
5726
5727 if (TREE_CODE (type) == ARRAY_TYPE)
5728 {
5729 error_at (loc, "cast specifies array type");
5730 return error_mark_node;
5731 }
5732
5733 if (TREE_CODE (type) == FUNCTION_TYPE)
5734 {
5735 error_at (loc, "cast specifies function type");
5736 return error_mark_node;
5737 }
5738
5739 if (!VOID_TYPE_P (type))
5740 {
5741 value = require_complete_type (loc, value);
5742 if (value == error_mark_node)
5743 return error_mark_node;
5744 }
5745
5746 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5747 {
5748 if (RECORD_OR_UNION_TYPE_P (type))
5749 pedwarn (loc, OPT_Wpedantic,
5750 "ISO C forbids casting nonscalar to the same type");
5751
5752 /* Convert to remove any qualifiers from VALUE's type. */
5753 value = convert (type, value);
5754 }
5755 else if (TREE_CODE (type) == UNION_TYPE)
5756 {
5757 tree field;
5758
5759 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5760 if (TREE_TYPE (field) != error_mark_node
5761 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5762 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5763 break;
5764
5765 if (field)
5766 {
5767 tree t;
5768 bool maybe_const = true;
5769
5770 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5771 t = c_fully_fold (value, false, &maybe_const);
5772 t = build_constructor_single (type, field, t);
5773 if (!maybe_const)
5774 t = c_wrap_maybe_const (t, true);
5775 t = digest_init (loc, type, t,
5776 NULL_TREE, false, true, 0);
5777 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5778 return t;
5779 }
5780 error_at (loc, "cast to union type from type not present in union");
5781 return error_mark_node;
5782 }
5783 else
5784 {
5785 tree otype, ovalue;
5786
5787 if (type == void_type_node)
5788 {
5789 tree t = build1 (CONVERT_EXPR, type, value);
5790 SET_EXPR_LOCATION (t, loc);
5791 return t;
5792 }
5793
5794 otype = TREE_TYPE (value);
5795
5796 /* Optionally warn about potentially worrisome casts. */
5797 if (warn_cast_qual
5798 && TREE_CODE (type) == POINTER_TYPE
5799 && TREE_CODE (otype) == POINTER_TYPE)
5800 handle_warn_cast_qual (loc, type, otype);
5801
5802 /* Warn about conversions between pointers to disjoint
5803 address spaces. */
5804 if (TREE_CODE (type) == POINTER_TYPE
5805 && TREE_CODE (otype) == POINTER_TYPE
5806 && !null_pointer_constant_p (value))
5807 {
5808 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5809 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5810 addr_space_t as_common;
5811
5812 if (!addr_space_superset (as_to, as_from, &as_common))
5813 {
5814 if (ADDR_SPACE_GENERIC_P (as_from))
5815 warning_at (loc, 0, "cast to %s address space pointer "
5816 "from disjoint generic address space pointer",
5817 c_addr_space_name (as_to));
5818
5819 else if (ADDR_SPACE_GENERIC_P (as_to))
5820 warning_at (loc, 0, "cast to generic address space pointer "
5821 "from disjoint %s address space pointer",
5822 c_addr_space_name (as_from));
5823
5824 else
5825 warning_at (loc, 0, "cast to %s address space pointer "
5826 "from disjoint %s address space pointer",
5827 c_addr_space_name (as_to),
5828 c_addr_space_name (as_from));
5829 }
5830 }
5831
5832 /* Warn about possible alignment problems. */
5833 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5834 && TREE_CODE (type) == POINTER_TYPE
5835 && TREE_CODE (otype) == POINTER_TYPE
5836 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5837 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5838 /* Don't warn about opaque types, where the actual alignment
5839 restriction is unknown. */
5840 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5841 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5842 && min_align_of_type (TREE_TYPE (type))
5843 > min_align_of_type (TREE_TYPE (otype)))
5844 warning_at (loc, OPT_Wcast_align,
5845 "cast increases required alignment of target type");
5846
5847 if (TREE_CODE (type) == INTEGER_TYPE
5848 && TREE_CODE (otype) == POINTER_TYPE
5849 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5850 /* Unlike conversion of integers to pointers, where the
5851 warning is disabled for converting constants because
5852 of cases such as SIG_*, warn about converting constant
5853 pointers to integers. In some cases it may cause unwanted
5854 sign extension, and a warning is appropriate. */
5855 warning_at (loc, OPT_Wpointer_to_int_cast,
5856 "cast from pointer to integer of different size");
5857
5858 if (TREE_CODE (value) == CALL_EXPR
5859 && TREE_CODE (type) != TREE_CODE (otype))
5860 warning_at (loc, OPT_Wbad_function_cast,
5861 "cast from function call of type %qT "
5862 "to non-matching type %qT", otype, type);
5863
5864 if (TREE_CODE (type) == POINTER_TYPE
5865 && TREE_CODE (otype) == INTEGER_TYPE
5866 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5867 /* Don't warn about converting any constant. */
5868 && !TREE_CONSTANT (value))
5869 warning_at (loc,
5870 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5871 "of different size");
5872
5873 if (warn_strict_aliasing <= 2)
5874 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
5875
5876 /* If pedantic, warn for conversions between function and object
5877 pointer types, except for converting a null pointer constant
5878 to function pointer type. */
5879 if (pedantic
5880 && TREE_CODE (type) == POINTER_TYPE
5881 && TREE_CODE (otype) == POINTER_TYPE
5882 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5883 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5884 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5885 "conversion of function pointer to object pointer type");
5886
5887 if (pedantic
5888 && TREE_CODE (type) == POINTER_TYPE
5889 && TREE_CODE (otype) == POINTER_TYPE
5890 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5891 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5892 && !null_pointer_constant_p (value))
5893 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5894 "conversion of object pointer to function pointer type");
5895
5896 if (TREE_CODE (type) == POINTER_TYPE
5897 && TREE_CODE (otype) == POINTER_TYPE
5898 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5899 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5900 && !c_safe_function_type_cast_p (TREE_TYPE (type),
5901 TREE_TYPE (otype)))
5902 warning_at (loc, OPT_Wcast_function_type,
5903 "cast between incompatible function types"
5904 " from %qT to %qT", otype, type);
5905
5906 ovalue = value;
5907 value = convert (type, value);
5908
5909 /* Ignore any integer overflow caused by the cast. */
5910 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5911 {
5912 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5913 {
5914 if (!TREE_OVERFLOW (value))
5915 {
5916 /* Avoid clobbering a shared constant. */
5917 value = copy_node (value);
5918 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5919 }
5920 }
5921 else if (TREE_OVERFLOW (value))
5922 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5923 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5924 }
5925 }
5926
5927 /* Don't let a cast be an lvalue. */
5928 if (lvalue_p (value))
5929 value = non_lvalue_loc (loc, value);
5930
5931 /* Don't allow the results of casting to floating-point or complex
5932 types be confused with actual constants, or casts involving
5933 integer and pointer types other than direct integer-to-integer
5934 and integer-to-pointer be confused with integer constant
5935 expressions and null pointer constants. */
5936 if (TREE_CODE (value) == REAL_CST
5937 || TREE_CODE (value) == COMPLEX_CST
5938 || (TREE_CODE (value) == INTEGER_CST
5939 && !((TREE_CODE (expr) == INTEGER_CST
5940 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5941 || TREE_CODE (expr) == REAL_CST
5942 || TREE_CODE (expr) == COMPLEX_CST)))
5943 value = build1 (NOP_EXPR, type, value);
5944
5945 protected_set_expr_location (value, loc);
5946 return value;
5947 }
5948
5949 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5950 location of the open paren of the cast, or the position of the cast
5951 expr. */
5952 tree
5953 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5954 {
5955 tree type;
5956 tree type_expr = NULL_TREE;
5957 bool type_expr_const = true;
5958 tree ret;
5959 int saved_wsp = warn_strict_prototypes;
5960
5961 /* This avoids warnings about unprototyped casts on
5962 integers. E.g. "#define SIG_DFL (void(*)())0". */
5963 if (TREE_CODE (expr) == INTEGER_CST)
5964 warn_strict_prototypes = 0;
5965 type = groktypename (type_name, &type_expr, &type_expr_const);
5966 warn_strict_prototypes = saved_wsp;
5967
5968 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5969 && reject_gcc_builtin (expr))
5970 return error_mark_node;
5971
5972 ret = build_c_cast (loc, type, expr);
5973 if (type_expr)
5974 {
5975 bool inner_expr_const = true;
5976 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5977 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5978 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5979 && inner_expr_const);
5980 SET_EXPR_LOCATION (ret, loc);
5981 }
5982
5983 if (!EXPR_HAS_LOCATION (ret))
5984 protected_set_expr_location (ret, loc);
5985
5986 /* C++ does not permits types to be defined in a cast, but it
5987 allows references to incomplete types. */
5988 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5989 warning_at (loc, OPT_Wc___compat,
5990 "defining a type in a cast is invalid in C++");
5991
5992 return ret;
5993 }
5994 \f
5995 /* Build an assignment expression of lvalue LHS from value RHS.
5996 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5997 may differ from TREE_TYPE (LHS) for an enum bitfield.
5998 MODIFYCODE is the code for a binary operator that we use
5999 to combine the old value of LHS with RHS to get the new value.
6000 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6001 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6002 which may differ from TREE_TYPE (RHS) for an enum value.
6003
6004 LOCATION is the location of the MODIFYCODE operator.
6005 RHS_LOC is the location of the RHS. */
6006
6007 tree
6008 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6009 enum tree_code modifycode,
6010 location_t rhs_loc, tree rhs, tree rhs_origtype)
6011 {
6012 tree result;
6013 tree newrhs;
6014 tree rhseval = NULL_TREE;
6015 tree lhstype = TREE_TYPE (lhs);
6016 tree olhstype = lhstype;
6017 bool npc;
6018 bool is_atomic_op;
6019
6020 /* Types that aren't fully specified cannot be used in assignments. */
6021 lhs = require_complete_type (location, lhs);
6022
6023 /* Avoid duplicate error messages from operands that had errors. */
6024 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6025 return error_mark_node;
6026
6027 /* Ensure an error for assigning a non-lvalue array to an array in
6028 C90. */
6029 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6030 {
6031 error_at (location, "assignment to expression with array type");
6032 return error_mark_node;
6033 }
6034
6035 /* For ObjC properties, defer this check. */
6036 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6037 return error_mark_node;
6038
6039 is_atomic_op = really_atomic_lvalue (lhs);
6040
6041 newrhs = rhs;
6042
6043 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6044 {
6045 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6046 lhs_origtype, modifycode, rhs_loc, rhs,
6047 rhs_origtype);
6048 if (inner == error_mark_node)
6049 return error_mark_node;
6050 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6051 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6052 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6053 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6054 protected_set_expr_location (result, location);
6055 return result;
6056 }
6057
6058 /* If a binary op has been requested, combine the old LHS value with the RHS
6059 producing the value we should actually store into the LHS. */
6060
6061 if (modifycode != NOP_EXPR)
6062 {
6063 lhs = c_fully_fold (lhs, false, NULL, true);
6064 lhs = stabilize_reference (lhs);
6065
6066 /* Construct the RHS for any non-atomic compound assignemnt. */
6067 if (!is_atomic_op)
6068 {
6069 /* If in LHS op= RHS the RHS has side-effects, ensure they
6070 are preevaluated before the rest of the assignment expression's
6071 side-effects, because RHS could contain e.g. function calls
6072 that modify LHS. */
6073 if (TREE_SIDE_EFFECTS (rhs))
6074 {
6075 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6076 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6077 else
6078 newrhs = save_expr (rhs);
6079 rhseval = newrhs;
6080 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6081 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6082 newrhs);
6083 }
6084 newrhs = build_binary_op (location,
6085 modifycode, lhs, newrhs, true);
6086
6087 /* The original type of the right hand side is no longer
6088 meaningful. */
6089 rhs_origtype = NULL_TREE;
6090 }
6091 }
6092
6093 if (c_dialect_objc ())
6094 {
6095 /* Check if we are modifying an Objective-C property reference;
6096 if so, we need to generate setter calls. */
6097 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6098 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6099 else
6100 result = objc_maybe_build_modify_expr (lhs, newrhs);
6101 if (result)
6102 goto return_result;
6103
6104 /* Else, do the check that we postponed for Objective-C. */
6105 if (!lvalue_or_else (location, lhs, lv_assign))
6106 return error_mark_node;
6107 }
6108
6109 /* Give an error for storing in something that is 'const'. */
6110
6111 if (TYPE_READONLY (lhstype)
6112 || (RECORD_OR_UNION_TYPE_P (lhstype)
6113 && C_TYPE_FIELDS_READONLY (lhstype)))
6114 {
6115 readonly_error (location, lhs, lv_assign);
6116 return error_mark_node;
6117 }
6118 else if (TREE_READONLY (lhs))
6119 readonly_warning (lhs, lv_assign);
6120
6121 /* If storing into a structure or union member,
6122 it has probably been given type `int'.
6123 Compute the type that would go with
6124 the actual amount of storage the member occupies. */
6125
6126 if (TREE_CODE (lhs) == COMPONENT_REF
6127 && (TREE_CODE (lhstype) == INTEGER_TYPE
6128 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6129 || TREE_CODE (lhstype) == REAL_TYPE
6130 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6131 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6132
6133 /* If storing in a field that is in actuality a short or narrower than one,
6134 we must store in the field in its actual type. */
6135
6136 if (lhstype != TREE_TYPE (lhs))
6137 {
6138 lhs = copy_node (lhs);
6139 TREE_TYPE (lhs) = lhstype;
6140 }
6141
6142 /* Issue -Wc++-compat warnings about an assignment to an enum type
6143 when LHS does not have its original type. This happens for,
6144 e.g., an enum bitfield in a struct. */
6145 if (warn_cxx_compat
6146 && lhs_origtype != NULL_TREE
6147 && lhs_origtype != lhstype
6148 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6149 {
6150 tree checktype = (rhs_origtype != NULL_TREE
6151 ? rhs_origtype
6152 : TREE_TYPE (rhs));
6153 if (checktype != error_mark_node
6154 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6155 || (is_atomic_op && modifycode != NOP_EXPR)))
6156 warning_at (location, OPT_Wc___compat,
6157 "enum conversion in assignment is invalid in C++");
6158 }
6159
6160 /* If the lhs is atomic, remove that qualifier. */
6161 if (is_atomic_op)
6162 {
6163 lhstype = build_qualified_type (lhstype,
6164 (TYPE_QUALS (lhstype)
6165 & ~TYPE_QUAL_ATOMIC));
6166 olhstype = build_qualified_type (olhstype,
6167 (TYPE_QUALS (lhstype)
6168 & ~TYPE_QUAL_ATOMIC));
6169 }
6170
6171 /* Convert new value to destination type. Fold it first, then
6172 restore any excess precision information, for the sake of
6173 conversion warnings. */
6174
6175 if (!(is_atomic_op && modifycode != NOP_EXPR))
6176 {
6177 tree rhs_semantic_type = NULL_TREE;
6178 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6179 {
6180 rhs_semantic_type = TREE_TYPE (newrhs);
6181 newrhs = TREE_OPERAND (newrhs, 0);
6182 }
6183 npc = null_pointer_constant_p (newrhs);
6184 newrhs = c_fully_fold (newrhs, false, NULL);
6185 if (rhs_semantic_type)
6186 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6187 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6188 rhs_origtype, ic_assign, npc,
6189 NULL_TREE, NULL_TREE, 0);
6190 if (TREE_CODE (newrhs) == ERROR_MARK)
6191 return error_mark_node;
6192 }
6193
6194 /* Emit ObjC write barrier, if necessary. */
6195 if (c_dialect_objc () && flag_objc_gc)
6196 {
6197 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6198 if (result)
6199 {
6200 protected_set_expr_location (result, location);
6201 goto return_result;
6202 }
6203 }
6204
6205 /* Scan operands. */
6206
6207 if (is_atomic_op)
6208 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6209 else
6210 {
6211 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6212 TREE_SIDE_EFFECTS (result) = 1;
6213 protected_set_expr_location (result, location);
6214 }
6215
6216 /* If we got the LHS in a different type for storing in,
6217 convert the result back to the nominal type of LHS
6218 so that the value we return always has the same type
6219 as the LHS argument. */
6220
6221 if (olhstype == TREE_TYPE (result))
6222 goto return_result;
6223
6224 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6225 rhs_origtype, ic_assign, false, NULL_TREE,
6226 NULL_TREE, 0);
6227 protected_set_expr_location (result, location);
6228
6229 return_result:
6230 if (rhseval)
6231 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6232 return result;
6233 }
6234 \f
6235 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6236 This is used to implement -fplan9-extensions. */
6237
6238 static bool
6239 find_anonymous_field_with_type (tree struct_type, tree type)
6240 {
6241 tree field;
6242 bool found;
6243
6244 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6245 found = false;
6246 for (field = TYPE_FIELDS (struct_type);
6247 field != NULL_TREE;
6248 field = TREE_CHAIN (field))
6249 {
6250 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6251 ? c_build_qualified_type (TREE_TYPE (field),
6252 TYPE_QUAL_ATOMIC)
6253 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6254 if (DECL_NAME (field) == NULL
6255 && comptypes (type, fieldtype))
6256 {
6257 if (found)
6258 return false;
6259 found = true;
6260 }
6261 else if (DECL_NAME (field) == NULL
6262 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6263 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6264 {
6265 if (found)
6266 return false;
6267 found = true;
6268 }
6269 }
6270 return found;
6271 }
6272
6273 /* RHS is an expression whose type is pointer to struct. If there is
6274 an anonymous field in RHS with type TYPE, then return a pointer to
6275 that field in RHS. This is used with -fplan9-extensions. This
6276 returns NULL if no conversion could be found. */
6277
6278 static tree
6279 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6280 {
6281 tree rhs_struct_type, lhs_main_type;
6282 tree field, found_field;
6283 bool found_sub_field;
6284 tree ret;
6285
6286 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6287 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6288 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6289
6290 gcc_assert (POINTER_TYPE_P (type));
6291 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6292 ? c_build_qualified_type (TREE_TYPE (type),
6293 TYPE_QUAL_ATOMIC)
6294 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6295
6296 found_field = NULL_TREE;
6297 found_sub_field = false;
6298 for (field = TYPE_FIELDS (rhs_struct_type);
6299 field != NULL_TREE;
6300 field = TREE_CHAIN (field))
6301 {
6302 if (DECL_NAME (field) != NULL_TREE
6303 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6304 continue;
6305 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6306 ? c_build_qualified_type (TREE_TYPE (field),
6307 TYPE_QUAL_ATOMIC)
6308 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6309 if (comptypes (lhs_main_type, fieldtype))
6310 {
6311 if (found_field != NULL_TREE)
6312 return NULL_TREE;
6313 found_field = field;
6314 }
6315 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6316 lhs_main_type))
6317 {
6318 if (found_field != NULL_TREE)
6319 return NULL_TREE;
6320 found_field = field;
6321 found_sub_field = true;
6322 }
6323 }
6324
6325 if (found_field == NULL_TREE)
6326 return NULL_TREE;
6327
6328 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6329 build_fold_indirect_ref (rhs), found_field,
6330 NULL_TREE);
6331 ret = build_fold_addr_expr_loc (location, ret);
6332
6333 if (found_sub_field)
6334 {
6335 ret = convert_to_anonymous_field (location, type, ret);
6336 gcc_assert (ret != NULL_TREE);
6337 }
6338
6339 return ret;
6340 }
6341
6342 /* Issue an error message for a bad initializer component.
6343 GMSGID identifies the message.
6344 The component name is taken from the spelling stack. */
6345
6346 static void ATTRIBUTE_GCC_DIAG (2,0)
6347 error_init (location_t loc, const char *gmsgid, ...)
6348 {
6349 char *ofwhat;
6350
6351 auto_diagnostic_group d;
6352
6353 /* The gmsgid may be a format string with %< and %>. */
6354 va_list ap;
6355 va_start (ap, gmsgid);
6356 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6357 va_end (ap);
6358
6359 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6360 if (*ofwhat && warned)
6361 inform (loc, "(near initialization for %qs)", ofwhat);
6362 }
6363
6364 /* Issue a pedantic warning for a bad initializer component. OPT is
6365 the option OPT_* (from options.h) controlling this warning or 0 if
6366 it is unconditionally given. GMSGID identifies the message. The
6367 component name is taken from the spelling stack. */
6368
6369 static void ATTRIBUTE_GCC_DIAG (3,0)
6370 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6371 {
6372 /* Use the location where a macro was expanded rather than where
6373 it was defined to make sure macros defined in system headers
6374 but used incorrectly elsewhere are diagnosed. */
6375 location_t exploc = expansion_point_location_if_in_system_header (loc);
6376 auto_diagnostic_group d;
6377 va_list ap;
6378 va_start (ap, gmsgid);
6379 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6380 va_end (ap);
6381 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6382 if (*ofwhat && warned)
6383 inform (exploc, "(near initialization for %qs)", ofwhat);
6384 }
6385
6386 /* Issue a warning for a bad initializer component.
6387
6388 OPT is the OPT_W* value corresponding to the warning option that
6389 controls this warning. GMSGID identifies the message. The
6390 component name is taken from the spelling stack. */
6391
6392 static void
6393 warning_init (location_t loc, int opt, const char *gmsgid)
6394 {
6395 char *ofwhat;
6396 bool warned;
6397
6398 auto_diagnostic_group d;
6399
6400 /* Use the location where a macro was expanded rather than where
6401 it was defined to make sure macros defined in system headers
6402 but used incorrectly elsewhere are diagnosed. */
6403 location_t exploc = expansion_point_location_if_in_system_header (loc);
6404
6405 /* The gmsgid may be a format string with %< and %>. */
6406 warned = warning_at (exploc, opt, gmsgid);
6407 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6408 if (*ofwhat && warned)
6409 inform (exploc, "(near initialization for %qs)", ofwhat);
6410 }
6411 \f
6412 /* If TYPE is an array type and EXPR is a parenthesized string
6413 constant, warn if pedantic that EXPR is being used to initialize an
6414 object of type TYPE. */
6415
6416 void
6417 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6418 {
6419 if (pedantic
6420 && TREE_CODE (type) == ARRAY_TYPE
6421 && TREE_CODE (expr.value) == STRING_CST
6422 && expr.original_code != STRING_CST)
6423 pedwarn_init (loc, OPT_Wpedantic,
6424 "array initialized from parenthesized string constant");
6425 }
6426
6427 /* Attempt to locate the parameter with the given index within FNDECL,
6428 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6429
6430 static location_t
6431 get_fndecl_argument_location (tree fndecl, int argnum)
6432 {
6433 int i;
6434 tree param;
6435
6436 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6437 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6438 i < argnum && param;
6439 i++, param = TREE_CHAIN (param))
6440 ;
6441
6442 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6443 return DECL_SOURCE_LOCATION (FNDECL). */
6444 if (param == NULL)
6445 return DECL_SOURCE_LOCATION (fndecl);
6446
6447 return DECL_SOURCE_LOCATION (param);
6448 }
6449
6450 /* Issue a note about a mismatching argument for parameter PARMNUM
6451 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6452 Attempt to issue the note at the pertinent parameter of the decl;
6453 failing that issue it at the location of FUNDECL; failing that
6454 issue it at PLOC. */
6455
6456 static void
6457 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6458 tree expected_type, tree actual_type)
6459 {
6460 location_t loc;
6461 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6462 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6463 else
6464 loc = ploc;
6465
6466 inform (loc,
6467 "expected %qT but argument is of type %qT",
6468 expected_type, actual_type);
6469 }
6470
6471 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6472 function FUNDECL declared without prototype to parameter PARMNUM of
6473 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6474
6475 static void
6476 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6477 tree parmtype, tree argtype)
6478 {
6479 tree_code parmcode = TREE_CODE (parmtype);
6480 tree_code argcode = TREE_CODE (argtype);
6481 tree promoted = c_type_promotes_to (argtype);
6482
6483 /* Avoid warning for enum arguments that promote to an integer type
6484 of the same size/mode. */
6485 if (parmcode == INTEGER_TYPE
6486 && argcode == ENUMERAL_TYPE
6487 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6488 return;
6489
6490 if ((parmcode == argcode
6491 || (parmcode == INTEGER_TYPE
6492 && argcode == ENUMERAL_TYPE))
6493 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6494 return;
6495
6496 /* This diagnoses even signed/unsigned mismatches. Those might be
6497 safe in many cases but GCC may emit suboptimal code for them so
6498 warning on those cases drives efficiency improvements. */
6499 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6500 TYPE_MAIN_VARIANT (promoted) == argtype
6501 ? G_("%qD argument %d type is %qT where %qT is expected "
6502 "in a call to built-in function declared without "
6503 "prototype")
6504 : G_("%qD argument %d promotes to %qT where %qT is expected "
6505 "in a call to built-in function declared without "
6506 "prototype"),
6507 fundecl, parmnum, promoted, parmtype))
6508 inform (DECL_SOURCE_LOCATION (fundecl),
6509 "built-in %qD declared here",
6510 fundecl);
6511 }
6512
6513 /* Convert value RHS to type TYPE as preparation for an assignment to
6514 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6515 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6516 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6517 constant before any folding.
6518 The real work of conversion is done by `convert'.
6519 The purpose of this function is to generate error messages
6520 for assignments that are not allowed in C.
6521 ERRTYPE says whether it is argument passing, assignment,
6522 initialization or return.
6523
6524 In the following example, '~' denotes where EXPR_LOC and '^' where
6525 LOCATION point to:
6526
6527 f (var); [ic_argpass]
6528 ^ ~~~
6529 x = var; [ic_assign]
6530 ^ ~~~;
6531 int x = var; [ic_init]
6532 ^^^
6533 return x; [ic_return]
6534 ^
6535
6536 FUNCTION is a tree for the function being called.
6537 PARMNUM is the number of the argument, for printing in error messages.
6538 WARNOPT may be set to a warning option to issue the corresponding warning
6539 rather than an error for invalid conversions. Used for calls to built-in
6540 functions declared without a prototype. */
6541
6542 static tree
6543 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6544 tree rhs, tree origtype, enum impl_conv errtype,
6545 bool null_pointer_constant, tree fundecl,
6546 tree function, int parmnum, int warnopt /* = 0 */)
6547 {
6548 enum tree_code codel = TREE_CODE (type);
6549 tree orig_rhs = rhs;
6550 tree rhstype;
6551 enum tree_code coder;
6552 tree rname = NULL_TREE;
6553 bool objc_ok = false;
6554
6555 /* Use the expansion point location to handle cases such as user's
6556 function returning a wrong-type macro defined in a system header. */
6557 location = expansion_point_location_if_in_system_header (location);
6558
6559 if (errtype == ic_argpass)
6560 {
6561 tree selector;
6562 /* Change pointer to function to the function itself for
6563 diagnostics. */
6564 if (TREE_CODE (function) == ADDR_EXPR
6565 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6566 function = TREE_OPERAND (function, 0);
6567
6568 /* Handle an ObjC selector specially for diagnostics. */
6569 selector = objc_message_selector ();
6570 rname = function;
6571 if (selector && parmnum > 2)
6572 {
6573 rname = selector;
6574 parmnum -= 2;
6575 }
6576 }
6577
6578 /* This macro is used to emit diagnostics to ensure that all format
6579 strings are complete sentences, visible to gettext and checked at
6580 compile time. */
6581 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6582 do { \
6583 switch (errtype) \
6584 { \
6585 case ic_argpass: \
6586 { \
6587 auto_diagnostic_group d; \
6588 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6589 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6590 } \
6591 break; \
6592 case ic_assign: \
6593 pedwarn (LOCATION, OPT, AS); \
6594 break; \
6595 case ic_init: \
6596 pedwarn_init (LOCATION, OPT, IN); \
6597 break; \
6598 case ic_return: \
6599 pedwarn (LOCATION, OPT, RE); \
6600 break; \
6601 default: \
6602 gcc_unreachable (); \
6603 } \
6604 } while (0)
6605
6606 /* This macro is used to emit diagnostics to ensure that all format
6607 strings are complete sentences, visible to gettext and checked at
6608 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6609 extra parameter to enumerate qualifiers. */
6610 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6611 do { \
6612 switch (errtype) \
6613 { \
6614 case ic_argpass: \
6615 { \
6616 auto_diagnostic_group d; \
6617 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6618 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6619 } \
6620 break; \
6621 case ic_assign: \
6622 pedwarn (LOCATION, OPT, AS, QUALS); \
6623 break; \
6624 case ic_init: \
6625 pedwarn (LOCATION, OPT, IN, QUALS); \
6626 break; \
6627 case ic_return: \
6628 pedwarn (LOCATION, OPT, RE, QUALS); \
6629 break; \
6630 default: \
6631 gcc_unreachable (); \
6632 } \
6633 } while (0)
6634
6635 /* This macro is used to emit diagnostics to ensure that all format
6636 strings are complete sentences, visible to gettext and checked at
6637 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6638 warning_at instead of pedwarn. */
6639 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6640 do { \
6641 switch (errtype) \
6642 { \
6643 case ic_argpass: \
6644 { \
6645 auto_diagnostic_group d; \
6646 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6647 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6648 } \
6649 break; \
6650 case ic_assign: \
6651 warning_at (LOCATION, OPT, AS, QUALS); \
6652 break; \
6653 case ic_init: \
6654 warning_at (LOCATION, OPT, IN, QUALS); \
6655 break; \
6656 case ic_return: \
6657 warning_at (LOCATION, OPT, RE, QUALS); \
6658 break; \
6659 default: \
6660 gcc_unreachable (); \
6661 } \
6662 } while (0)
6663
6664 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6665 rhs = TREE_OPERAND (rhs, 0);
6666
6667 rhstype = TREE_TYPE (rhs);
6668 coder = TREE_CODE (rhstype);
6669
6670 if (coder == ERROR_MARK)
6671 return error_mark_node;
6672
6673 if (c_dialect_objc ())
6674 {
6675 int parmno;
6676
6677 switch (errtype)
6678 {
6679 case ic_return:
6680 parmno = 0;
6681 break;
6682
6683 case ic_assign:
6684 parmno = -1;
6685 break;
6686
6687 case ic_init:
6688 parmno = -2;
6689 break;
6690
6691 default:
6692 parmno = parmnum;
6693 break;
6694 }
6695
6696 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6697 }
6698
6699 if (warn_cxx_compat)
6700 {
6701 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6702 if (checktype != error_mark_node
6703 && TREE_CODE (type) == ENUMERAL_TYPE
6704 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6705 switch (errtype)
6706 {
6707 case ic_argpass:
6708 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6709 "passing argument %d of %qE is invalid in C++",
6710 parmnum, rname))
6711 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6712 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6713 "expected %qT but argument is of type %qT",
6714 type, rhstype);
6715 break;
6716 case ic_assign:
6717 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6718 "%qT in assignment is invalid in C++", rhstype, type);
6719 break;
6720 case ic_init:
6721 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6722 "%qT to %qT in initialization is invalid in C++",
6723 rhstype, type);
6724 break;
6725 case ic_return:
6726 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6727 "%qT in return is invalid in C++", rhstype, type);
6728 break;
6729 default:
6730 gcc_unreachable ();
6731 }
6732 }
6733
6734 if (warn_enum_conversion)
6735 {
6736 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6737 if (checktype != error_mark_node
6738 && TREE_CODE (checktype) == ENUMERAL_TYPE
6739 && TREE_CODE (type) == ENUMERAL_TYPE
6740 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6741 {
6742 gcc_rich_location loc (location);
6743 warning_at (&loc, OPT_Wenum_conversion,
6744 "implicit conversion from %qT to %qT",
6745 checktype, type);
6746 }
6747 }
6748
6749 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6750 {
6751 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6752 return rhs;
6753 }
6754
6755 if (coder == VOID_TYPE)
6756 {
6757 /* Except for passing an argument to an unprototyped function,
6758 this is a constraint violation. When passing an argument to
6759 an unprototyped function, it is compile-time undefined;
6760 making it a constraint in that case was rejected in
6761 DR#252. */
6762 const char msg[] = "void value not ignored as it ought to be";
6763 if (warnopt)
6764 warning_at (location, warnopt, msg);
6765 else
6766 error_at (location, msg);
6767 return error_mark_node;
6768 }
6769 rhs = require_complete_type (location, rhs);
6770 if (rhs == error_mark_node)
6771 return error_mark_node;
6772
6773 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6774 return error_mark_node;
6775
6776 /* A non-reference type can convert to a reference. This handles
6777 va_start, va_copy and possibly port built-ins. */
6778 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6779 {
6780 if (!lvalue_p (rhs))
6781 {
6782 const char msg[] = "cannot pass rvalue to reference parameter";
6783 if (warnopt)
6784 warning_at (location, warnopt, msg);
6785 else
6786 error_at (location, msg);
6787 return error_mark_node;
6788 }
6789 if (!c_mark_addressable (rhs))
6790 return error_mark_node;
6791 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6792 SET_EXPR_LOCATION (rhs, location);
6793
6794 rhs = convert_for_assignment (location, expr_loc,
6795 build_pointer_type (TREE_TYPE (type)),
6796 rhs, origtype, errtype,
6797 null_pointer_constant, fundecl, function,
6798 parmnum, warnopt);
6799 if (rhs == error_mark_node)
6800 return error_mark_node;
6801
6802 rhs = build1 (NOP_EXPR, type, rhs);
6803 SET_EXPR_LOCATION (rhs, location);
6804 return rhs;
6805 }
6806 /* Some types can interconvert without explicit casts. */
6807 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6808 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6809 return convert (type, rhs);
6810 /* Arithmetic types all interconvert, and enum is treated like int. */
6811 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6812 || codel == FIXED_POINT_TYPE
6813 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6814 || codel == BOOLEAN_TYPE)
6815 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6816 || coder == FIXED_POINT_TYPE
6817 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6818 || coder == BOOLEAN_TYPE))
6819 {
6820 if (warnopt && errtype == ic_argpass)
6821 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
6822 rhstype);
6823
6824 bool save = in_late_binary_op;
6825 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6826 || (coder == REAL_TYPE
6827 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6828 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6829 in_late_binary_op = true;
6830 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6831 ? expr_loc : location, type, orig_rhs);
6832 in_late_binary_op = save;
6833 return ret;
6834 }
6835
6836 /* Aggregates in different TUs might need conversion. */
6837 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6838 && codel == coder
6839 && comptypes (type, rhstype))
6840 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6841 ? expr_loc : location, type, rhs);
6842
6843 /* Conversion to a transparent union or record from its member types.
6844 This applies only to function arguments. */
6845 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6846 && TYPE_TRANSPARENT_AGGR (type))
6847 && errtype == ic_argpass)
6848 {
6849 tree memb, marginal_memb = NULL_TREE;
6850
6851 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6852 {
6853 tree memb_type = TREE_TYPE (memb);
6854
6855 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6856 TYPE_MAIN_VARIANT (rhstype)))
6857 break;
6858
6859 if (TREE_CODE (memb_type) != POINTER_TYPE)
6860 continue;
6861
6862 if (coder == POINTER_TYPE)
6863 {
6864 tree ttl = TREE_TYPE (memb_type);
6865 tree ttr = TREE_TYPE (rhstype);
6866
6867 /* Any non-function converts to a [const][volatile] void *
6868 and vice versa; otherwise, targets must be the same.
6869 Meanwhile, the lhs target must have all the qualifiers of
6870 the rhs. */
6871 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6872 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6873 || comp_target_types (location, memb_type, rhstype))
6874 {
6875 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6876 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6877 /* If this type won't generate any warnings, use it. */
6878 if (lquals == rquals
6879 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6880 && TREE_CODE (ttl) == FUNCTION_TYPE)
6881 ? ((lquals | rquals) == rquals)
6882 : ((lquals | rquals) == lquals)))
6883 break;
6884
6885 /* Keep looking for a better type, but remember this one. */
6886 if (!marginal_memb)
6887 marginal_memb = memb;
6888 }
6889 }
6890
6891 /* Can convert integer zero to any pointer type. */
6892 if (null_pointer_constant)
6893 {
6894 rhs = null_pointer_node;
6895 break;
6896 }
6897 }
6898
6899 if (memb || marginal_memb)
6900 {
6901 if (!memb)
6902 {
6903 /* We have only a marginally acceptable member type;
6904 it needs a warning. */
6905 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6906 tree ttr = TREE_TYPE (rhstype);
6907
6908 /* Const and volatile mean something different for function
6909 types, so the usual warnings are not appropriate. */
6910 if (TREE_CODE (ttr) == FUNCTION_TYPE
6911 && TREE_CODE (ttl) == FUNCTION_TYPE)
6912 {
6913 /* Because const and volatile on functions are
6914 restrictions that say the function will not do
6915 certain things, it is okay to use a const or volatile
6916 function where an ordinary one is wanted, but not
6917 vice-versa. */
6918 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6919 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6920 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6921 OPT_Wdiscarded_qualifiers,
6922 G_("passing argument %d of %qE "
6923 "makes %q#v qualified function "
6924 "pointer from unqualified"),
6925 G_("assignment makes %q#v qualified "
6926 "function pointer from "
6927 "unqualified"),
6928 G_("initialization makes %q#v qualified "
6929 "function pointer from "
6930 "unqualified"),
6931 G_("return makes %q#v qualified function "
6932 "pointer from unqualified"),
6933 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6934 }
6935 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6936 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6937 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6938 OPT_Wdiscarded_qualifiers,
6939 G_("passing argument %d of %qE discards "
6940 "%qv qualifier from pointer target type"),
6941 G_("assignment discards %qv qualifier "
6942 "from pointer target type"),
6943 G_("initialization discards %qv qualifier "
6944 "from pointer target type"),
6945 G_("return discards %qv qualifier from "
6946 "pointer target type"),
6947 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6948
6949 memb = marginal_memb;
6950 }
6951
6952 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6953 pedwarn (location, OPT_Wpedantic,
6954 "ISO C prohibits argument conversion to union type");
6955
6956 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6957 return build_constructor_single (type, memb, rhs);
6958 }
6959 }
6960
6961 /* Conversions among pointers */
6962 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6963 && (coder == codel))
6964 {
6965 /* If RHS refers to a built-in declared without a prototype
6966 BLTIN is the declaration of the built-in with a prototype
6967 and RHSTYPE is set to the actual type of the built-in. */
6968 tree bltin;
6969 rhstype = type_or_builtin_type (rhs, &bltin);
6970
6971 tree ttl = TREE_TYPE (type);
6972 tree ttr = TREE_TYPE (rhstype);
6973 tree mvl = ttl;
6974 tree mvr = ttr;
6975 bool is_opaque_pointer;
6976 int target_cmp = 0; /* Cache comp_target_types () result. */
6977 addr_space_t asl;
6978 addr_space_t asr;
6979
6980 if (TREE_CODE (mvl) != ARRAY_TYPE)
6981 mvl = (TYPE_ATOMIC (mvl)
6982 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6983 TYPE_QUAL_ATOMIC)
6984 : TYPE_MAIN_VARIANT (mvl));
6985 if (TREE_CODE (mvr) != ARRAY_TYPE)
6986 mvr = (TYPE_ATOMIC (mvr)
6987 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6988 TYPE_QUAL_ATOMIC)
6989 : TYPE_MAIN_VARIANT (mvr));
6990 /* Opaque pointers are treated like void pointers. */
6991 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6992
6993 /* The Plan 9 compiler permits a pointer to a struct to be
6994 automatically converted into a pointer to an anonymous field
6995 within the struct. */
6996 if (flag_plan9_extensions
6997 && RECORD_OR_UNION_TYPE_P (mvl)
6998 && RECORD_OR_UNION_TYPE_P (mvr)
6999 && mvl != mvr)
7000 {
7001 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7002 if (new_rhs != NULL_TREE)
7003 {
7004 rhs = new_rhs;
7005 rhstype = TREE_TYPE (rhs);
7006 coder = TREE_CODE (rhstype);
7007 ttr = TREE_TYPE (rhstype);
7008 mvr = TYPE_MAIN_VARIANT (ttr);
7009 }
7010 }
7011
7012 /* C++ does not allow the implicit conversion void* -> T*. However,
7013 for the purpose of reducing the number of false positives, we
7014 tolerate the special case of
7015
7016 int *p = NULL;
7017
7018 where NULL is typically defined in C to be '(void *) 0'. */
7019 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7020 warning_at (errtype == ic_argpass ? expr_loc : location,
7021 OPT_Wc___compat,
7022 "request for implicit conversion "
7023 "from %qT to %qT not permitted in C++", rhstype, type);
7024
7025 /* See if the pointers point to incompatible address spaces. */
7026 asl = TYPE_ADDR_SPACE (ttl);
7027 asr = TYPE_ADDR_SPACE (ttr);
7028 if (!null_pointer_constant_p (rhs)
7029 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7030 {
7031 switch (errtype)
7032 {
7033 case ic_argpass:
7034 {
7035 const char msg[] = G_("passing argument %d of %qE from "
7036 "pointer to non-enclosed address space");
7037 if (warnopt)
7038 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7039 else
7040 error_at (expr_loc, msg, parmnum, rname);
7041 break;
7042 }
7043 case ic_assign:
7044 {
7045 const char msg[] = G_("assignment 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 case ic_init:
7054 {
7055 const char msg[] = G_("initialization from pointer to "
7056 "non-enclosed address space");
7057 if (warnopt)
7058 warning_at (location, warnopt, msg);
7059 else
7060 error_at (location, msg);
7061 break;
7062 }
7063 case ic_return:
7064 {
7065 const char msg[] = G_("return from pointer to "
7066 "non-enclosed address space");
7067 if (warnopt)
7068 warning_at (location, warnopt, msg);
7069 else
7070 error_at (location, msg);
7071 break;
7072 }
7073 default:
7074 gcc_unreachable ();
7075 }
7076 return error_mark_node;
7077 }
7078
7079 /* Check if the right-hand side has a format attribute but the
7080 left-hand side doesn't. */
7081 if (warn_suggest_attribute_format
7082 && check_missing_format_attribute (type, rhstype))
7083 {
7084 switch (errtype)
7085 {
7086 case ic_argpass:
7087 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7088 "argument %d of %qE might be "
7089 "a candidate for a format attribute",
7090 parmnum, rname);
7091 break;
7092 case ic_assign:
7093 warning_at (location, OPT_Wsuggest_attribute_format,
7094 "assignment left-hand side might be "
7095 "a candidate for a format attribute");
7096 break;
7097 case ic_init:
7098 warning_at (location, OPT_Wsuggest_attribute_format,
7099 "initialization left-hand side might be "
7100 "a candidate for a format attribute");
7101 break;
7102 case ic_return:
7103 warning_at (location, OPT_Wsuggest_attribute_format,
7104 "return type might be "
7105 "a candidate for a format attribute");
7106 break;
7107 default:
7108 gcc_unreachable ();
7109 }
7110 }
7111
7112 /* Any non-function converts to a [const][volatile] void *
7113 and vice versa; otherwise, targets must be the same.
7114 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7115 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7116 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7117 || (target_cmp = comp_target_types (location, type, rhstype))
7118 || is_opaque_pointer
7119 || ((c_common_unsigned_type (mvl)
7120 == c_common_unsigned_type (mvr))
7121 && (c_common_signed_type (mvl)
7122 == c_common_signed_type (mvr))
7123 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7124 {
7125 /* Warn about loss of qualifers from pointers to arrays with
7126 qualifiers on the element type. */
7127 if (TREE_CODE (ttr) == ARRAY_TYPE)
7128 {
7129 ttr = strip_array_types (ttr);
7130 ttl = strip_array_types (ttl);
7131
7132 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7133 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7134 WARNING_FOR_QUALIFIERS (location, expr_loc,
7135 OPT_Wdiscarded_array_qualifiers,
7136 G_("passing argument %d of %qE discards "
7137 "%qv qualifier from pointer target type"),
7138 G_("assignment discards %qv qualifier "
7139 "from pointer target type"),
7140 G_("initialization discards %qv qualifier "
7141 "from pointer target type"),
7142 G_("return discards %qv qualifier from "
7143 "pointer target type"),
7144 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7145 }
7146 else if (pedantic
7147 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7148 ||
7149 (VOID_TYPE_P (ttr)
7150 && !null_pointer_constant
7151 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7152 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7153 G_("ISO C forbids passing argument %d of "
7154 "%qE between function pointer "
7155 "and %<void *%>"),
7156 G_("ISO C forbids assignment between "
7157 "function pointer and %<void *%>"),
7158 G_("ISO C forbids initialization between "
7159 "function pointer and %<void *%>"),
7160 G_("ISO C forbids return between function "
7161 "pointer and %<void *%>"));
7162 /* Const and volatile mean something different for function types,
7163 so the usual warnings are not appropriate. */
7164 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7165 && TREE_CODE (ttl) != FUNCTION_TYPE)
7166 {
7167 /* Don't warn about loss of qualifier for conversions from
7168 qualified void* to pointers to arrays with corresponding
7169 qualifier on the element type. */
7170 if (!pedantic)
7171 ttl = strip_array_types (ttl);
7172
7173 /* Assignments between atomic and non-atomic objects are OK. */
7174 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7175 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7176 {
7177 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7178 OPT_Wdiscarded_qualifiers,
7179 G_("passing argument %d of %qE discards "
7180 "%qv qualifier from pointer target type"),
7181 G_("assignment discards %qv qualifier "
7182 "from pointer target type"),
7183 G_("initialization discards %qv qualifier "
7184 "from pointer target type"),
7185 G_("return discards %qv qualifier from "
7186 "pointer target type"),
7187 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7188 }
7189 /* If this is not a case of ignoring a mismatch in signedness,
7190 no warning. */
7191 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7192 || target_cmp)
7193 ;
7194 /* If there is a mismatch, do warn. */
7195 else if (warn_pointer_sign)
7196 switch (errtype)
7197 {
7198 case ic_argpass:
7199 {
7200 auto_diagnostic_group d;
7201 range_label_for_type_mismatch rhs_label (rhstype, type);
7202 gcc_rich_location richloc (expr_loc, &rhs_label);
7203 if (pedwarn (&richloc, OPT_Wpointer_sign,
7204 "pointer targets in passing argument %d of "
7205 "%qE differ in signedness", parmnum, rname))
7206 inform_for_arg (fundecl, expr_loc, parmnum, type,
7207 rhstype);
7208 }
7209 break;
7210 case ic_assign:
7211 pedwarn (location, OPT_Wpointer_sign,
7212 "pointer targets in assignment from %qT to %qT "
7213 "differ in signedness", rhstype, type);
7214 break;
7215 case ic_init:
7216 pedwarn_init (location, OPT_Wpointer_sign,
7217 "pointer targets in initialization of %qT "
7218 "from %qT differ in signedness", type,
7219 rhstype);
7220 break;
7221 case ic_return:
7222 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7223 "returning %qT from a function with return type "
7224 "%qT differ in signedness", rhstype, type);
7225 break;
7226 default:
7227 gcc_unreachable ();
7228 }
7229 }
7230 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7231 && TREE_CODE (ttr) == FUNCTION_TYPE)
7232 {
7233 /* Because const and volatile on functions are restrictions
7234 that say the function will not do certain things,
7235 it is okay to use a const or volatile function
7236 where an ordinary one is wanted, but not vice-versa. */
7237 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7238 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7239 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7240 OPT_Wdiscarded_qualifiers,
7241 G_("passing argument %d of %qE makes "
7242 "%q#v qualified function pointer "
7243 "from unqualified"),
7244 G_("assignment makes %q#v qualified function "
7245 "pointer from unqualified"),
7246 G_("initialization makes %q#v qualified "
7247 "function pointer from unqualified"),
7248 G_("return makes %q#v qualified function "
7249 "pointer from unqualified"),
7250 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7251 }
7252 }
7253 /* Avoid warning about the volatile ObjC EH puts on decls. */
7254 else if (!objc_ok)
7255 {
7256 switch (errtype)
7257 {
7258 case ic_argpass:
7259 {
7260 auto_diagnostic_group d;
7261 range_label_for_type_mismatch rhs_label (rhstype, type);
7262 gcc_rich_location richloc (expr_loc, &rhs_label);
7263 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7264 "passing argument %d of %qE from incompatible "
7265 "pointer type", parmnum, rname))
7266 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7267 }
7268 break;
7269 case ic_assign:
7270 if (bltin)
7271 pedwarn (location, OPT_Wincompatible_pointer_types,
7272 "assignment to %qT from pointer to "
7273 "%qD with incompatible type %qT",
7274 type, bltin, rhstype);
7275 else
7276 pedwarn (location, OPT_Wincompatible_pointer_types,
7277 "assignment to %qT from incompatible pointer type %qT",
7278 type, rhstype);
7279 break;
7280 case ic_init:
7281 if (bltin)
7282 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7283 "initialization of %qT from pointer to "
7284 "%qD with incompatible type %qT",
7285 type, bltin, rhstype);
7286 else
7287 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7288 "initialization of %qT from incompatible "
7289 "pointer type %qT",
7290 type, rhstype);
7291 break;
7292 case ic_return:
7293 if (bltin)
7294 pedwarn (location, OPT_Wincompatible_pointer_types,
7295 "returning pointer to %qD of type %qT from "
7296 "a function with incompatible type %qT",
7297 bltin, rhstype, type);
7298 else
7299 pedwarn (location, OPT_Wincompatible_pointer_types,
7300 "returning %qT from a function with incompatible "
7301 "return type %qT", rhstype, type);
7302 break;
7303 default:
7304 gcc_unreachable ();
7305 }
7306 }
7307
7308 /* If RHS isn't an address, check pointer or array of packed
7309 struct or union. */
7310 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7311
7312 return convert (type, rhs);
7313 }
7314 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7315 {
7316 /* ??? This should not be an error when inlining calls to
7317 unprototyped functions. */
7318 const char msg[] = "invalid use of non-lvalue array";
7319 if (warnopt)
7320 warning_at (location, warnopt, msg);
7321 else
7322 error_at (location, msg);
7323 return error_mark_node;
7324 }
7325 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7326 {
7327 /* An explicit constant 0 can convert to a pointer,
7328 or one that results from arithmetic, even including
7329 a cast to integer type. */
7330 if (!null_pointer_constant)
7331 switch (errtype)
7332 {
7333 case ic_argpass:
7334 {
7335 auto_diagnostic_group d;
7336 range_label_for_type_mismatch rhs_label (rhstype, type);
7337 gcc_rich_location richloc (expr_loc, &rhs_label);
7338 if (pedwarn (&richloc, OPT_Wint_conversion,
7339 "passing argument %d of %qE makes pointer from "
7340 "integer without a cast", parmnum, rname))
7341 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7342 }
7343 break;
7344 case ic_assign:
7345 pedwarn (location, OPT_Wint_conversion,
7346 "assignment to %qT from %qT makes pointer from integer "
7347 "without a cast", type, rhstype);
7348 break;
7349 case ic_init:
7350 pedwarn_init (location, OPT_Wint_conversion,
7351 "initialization of %qT from %qT makes pointer from "
7352 "integer without a cast", type, rhstype);
7353 break;
7354 case ic_return:
7355 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7356 "function with return type %qT makes pointer from "
7357 "integer without a cast", rhstype, type);
7358 break;
7359 default:
7360 gcc_unreachable ();
7361 }
7362
7363 return convert (type, rhs);
7364 }
7365 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7366 {
7367 switch (errtype)
7368 {
7369 case ic_argpass:
7370 {
7371 auto_diagnostic_group d;
7372 range_label_for_type_mismatch rhs_label (rhstype, type);
7373 gcc_rich_location richloc (expr_loc, &rhs_label);
7374 if (pedwarn (&richloc, OPT_Wint_conversion,
7375 "passing argument %d of %qE makes integer from "
7376 "pointer without a cast", parmnum, rname))
7377 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7378 }
7379 break;
7380 case ic_assign:
7381 pedwarn (location, OPT_Wint_conversion,
7382 "assignment to %qT from %qT makes integer from pointer "
7383 "without a cast", type, rhstype);
7384 break;
7385 case ic_init:
7386 pedwarn_init (location, OPT_Wint_conversion,
7387 "initialization of %qT from %qT makes integer from "
7388 "pointer without a cast", type, rhstype);
7389 break;
7390 case ic_return:
7391 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7392 "function with return type %qT makes integer from "
7393 "pointer without a cast", rhstype, type);
7394 break;
7395 default:
7396 gcc_unreachable ();
7397 }
7398
7399 return convert (type, rhs);
7400 }
7401 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7402 {
7403 tree ret;
7404 bool save = in_late_binary_op;
7405 in_late_binary_op = true;
7406 ret = convert (type, rhs);
7407 in_late_binary_op = save;
7408 return ret;
7409 }
7410
7411 switch (errtype)
7412 {
7413 case ic_argpass:
7414 {
7415 auto_diagnostic_group d;
7416 range_label_for_type_mismatch rhs_label (rhstype, type);
7417 gcc_rich_location richloc (expr_loc, &rhs_label);
7418 const char msg[] = G_("incompatible type for argument %d of %qE");
7419 if (warnopt)
7420 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7421 else
7422 error_at (&richloc, msg, parmnum, rname);
7423 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7424 }
7425 break;
7426 case ic_assign:
7427 {
7428 const char msg[]
7429 = G_("incompatible types when assigning to type %qT from type %qT");
7430 if (warnopt)
7431 warning_at (expr_loc, 0, msg, type, rhstype);
7432 else
7433 error_at (expr_loc, msg, type, rhstype);
7434 break;
7435 }
7436 case ic_init:
7437 {
7438 const char msg[]
7439 = G_("incompatible types when initializing type %qT using type %qT");
7440 if (warnopt)
7441 warning_at (location, 0, msg, type, rhstype);
7442 else
7443 error_at (location, msg, type, rhstype);
7444 break;
7445 }
7446 case ic_return:
7447 {
7448 const char msg[]
7449 = G_("incompatible types when returning type %qT but %qT was expected");
7450 if (warnopt)
7451 warning_at (location, 0, msg, rhstype, type);
7452 else
7453 error_at (location, msg, rhstype, type);
7454 break;
7455 }
7456 default:
7457 gcc_unreachable ();
7458 }
7459
7460 return error_mark_node;
7461 }
7462 \f
7463 /* If VALUE is a compound expr all of whose expressions are constant, then
7464 return its value. Otherwise, return error_mark_node.
7465
7466 This is for handling COMPOUND_EXPRs as initializer elements
7467 which is allowed with a warning when -pedantic is specified. */
7468
7469 static tree
7470 valid_compound_expr_initializer (tree value, tree endtype)
7471 {
7472 if (TREE_CODE (value) == COMPOUND_EXPR)
7473 {
7474 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7475 == error_mark_node)
7476 return error_mark_node;
7477 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7478 endtype);
7479 }
7480 else if (!initializer_constant_valid_p (value, endtype))
7481 return error_mark_node;
7482 else
7483 return value;
7484 }
7485 \f
7486 /* Perform appropriate conversions on the initial value of a variable,
7487 store it in the declaration DECL,
7488 and print any error messages that are appropriate.
7489 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7490 If the init is invalid, store an ERROR_MARK.
7491
7492 INIT_LOC is the location of the initial value. */
7493
7494 void
7495 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7496 {
7497 tree value, type;
7498 bool npc = false;
7499
7500 /* If variable's type was invalidly declared, just ignore it. */
7501
7502 type = TREE_TYPE (decl);
7503 if (TREE_CODE (type) == ERROR_MARK)
7504 return;
7505
7506 /* Digest the specified initializer into an expression. */
7507
7508 if (init)
7509 npc = null_pointer_constant_p (init);
7510 value = digest_init (init_loc, type, init, origtype, npc,
7511 true, TREE_STATIC (decl));
7512
7513 /* Store the expression if valid; else report error. */
7514
7515 if (!in_system_header_at (input_location)
7516 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7517 warning (OPT_Wtraditional, "traditional C rejects automatic "
7518 "aggregate initialization");
7519
7520 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7521 DECL_INITIAL (decl) = value;
7522
7523 /* ANSI wants warnings about out-of-range constant initializers. */
7524 STRIP_TYPE_NOPS (value);
7525 if (TREE_STATIC (decl))
7526 constant_expression_warning (value);
7527
7528 /* Check if we need to set array size from compound literal size. */
7529 if (TREE_CODE (type) == ARRAY_TYPE
7530 && TYPE_DOMAIN (type) == NULL_TREE
7531 && value != error_mark_node)
7532 {
7533 tree inside_init = init;
7534
7535 STRIP_TYPE_NOPS (inside_init);
7536 inside_init = fold (inside_init);
7537
7538 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7539 {
7540 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7541
7542 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7543 {
7544 /* For int foo[] = (int [3]){1}; we need to set array size
7545 now since later on array initializer will be just the
7546 brace enclosed list of the compound literal. */
7547 tree etype = strip_array_types (TREE_TYPE (decl));
7548 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7549 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7550 layout_type (type);
7551 layout_decl (cldecl, 0);
7552 TREE_TYPE (decl)
7553 = c_build_qualified_type (type, TYPE_QUALS (etype));
7554 }
7555 }
7556 }
7557 }
7558 \f
7559 /* Methods for storing and printing names for error messages. */
7560
7561 /* Implement a spelling stack that allows components of a name to be pushed
7562 and popped. Each element on the stack is this structure. */
7563
7564 struct spelling
7565 {
7566 int kind;
7567 union
7568 {
7569 unsigned HOST_WIDE_INT i;
7570 const char *s;
7571 } u;
7572 };
7573
7574 #define SPELLING_STRING 1
7575 #define SPELLING_MEMBER 2
7576 #define SPELLING_BOUNDS 3
7577
7578 static struct spelling *spelling; /* Next stack element (unused). */
7579 static struct spelling *spelling_base; /* Spelling stack base. */
7580 static int spelling_size; /* Size of the spelling stack. */
7581
7582 /* Macros to save and restore the spelling stack around push_... functions.
7583 Alternative to SAVE_SPELLING_STACK. */
7584
7585 #define SPELLING_DEPTH() (spelling - spelling_base)
7586 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7587
7588 /* Push an element on the spelling stack with type KIND and assign VALUE
7589 to MEMBER. */
7590
7591 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7592 { \
7593 int depth = SPELLING_DEPTH (); \
7594 \
7595 if (depth >= spelling_size) \
7596 { \
7597 spelling_size += 10; \
7598 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7599 spelling_size); \
7600 RESTORE_SPELLING_DEPTH (depth); \
7601 } \
7602 \
7603 spelling->kind = (KIND); \
7604 spelling->MEMBER = (VALUE); \
7605 spelling++; \
7606 }
7607
7608 /* Push STRING on the stack. Printed literally. */
7609
7610 static void
7611 push_string (const char *string)
7612 {
7613 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7614 }
7615
7616 /* Push a member name on the stack. Printed as '.' STRING. */
7617
7618 static void
7619 push_member_name (tree decl)
7620 {
7621 const char *const string
7622 = (DECL_NAME (decl)
7623 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7624 : _("<anonymous>"));
7625 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7626 }
7627
7628 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7629
7630 static void
7631 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7632 {
7633 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7634 }
7635
7636 /* Compute the maximum size in bytes of the printed spelling. */
7637
7638 static int
7639 spelling_length (void)
7640 {
7641 int size = 0;
7642 struct spelling *p;
7643
7644 for (p = spelling_base; p < spelling; p++)
7645 {
7646 if (p->kind == SPELLING_BOUNDS)
7647 size += 25;
7648 else
7649 size += strlen (p->u.s) + 1;
7650 }
7651
7652 return size;
7653 }
7654
7655 /* Print the spelling to BUFFER and return it. */
7656
7657 static char *
7658 print_spelling (char *buffer)
7659 {
7660 char *d = buffer;
7661 struct spelling *p;
7662
7663 for (p = spelling_base; p < spelling; p++)
7664 if (p->kind == SPELLING_BOUNDS)
7665 {
7666 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7667 d += strlen (d);
7668 }
7669 else
7670 {
7671 const char *s;
7672 if (p->kind == SPELLING_MEMBER)
7673 *d++ = '.';
7674 for (s = p->u.s; (*d = *s++); d++)
7675 ;
7676 }
7677 *d++ = '\0';
7678 return buffer;
7679 }
7680
7681 /* Digest the parser output INIT as an initializer for type TYPE.
7682 Return a C expression of type TYPE to represent the initial value.
7683
7684 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7685
7686 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7687
7688 If INIT is a string constant, STRICT_STRING is true if it is
7689 unparenthesized or we should not warn here for it being parenthesized.
7690 For other types of INIT, STRICT_STRING is not used.
7691
7692 INIT_LOC is the location of the INIT.
7693
7694 REQUIRE_CONSTANT requests an error if non-constant initializers or
7695 elements are seen. */
7696
7697 static tree
7698 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7699 bool null_pointer_constant, bool strict_string,
7700 int require_constant)
7701 {
7702 enum tree_code code = TREE_CODE (type);
7703 tree inside_init = init;
7704 tree semantic_type = NULL_TREE;
7705 bool maybe_const = true;
7706
7707 if (type == error_mark_node
7708 || !init
7709 || error_operand_p (init))
7710 return error_mark_node;
7711
7712 STRIP_TYPE_NOPS (inside_init);
7713
7714 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7715 {
7716 semantic_type = TREE_TYPE (inside_init);
7717 inside_init = TREE_OPERAND (inside_init, 0);
7718 }
7719 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7720
7721 /* Initialization of an array of chars from a string constant
7722 optionally enclosed in braces. */
7723
7724 if (code == ARRAY_TYPE && inside_init
7725 && TREE_CODE (inside_init) == STRING_CST)
7726 {
7727 tree typ1
7728 = (TYPE_ATOMIC (TREE_TYPE (type))
7729 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7730 TYPE_QUAL_ATOMIC)
7731 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7732 /* Note that an array could be both an array of character type
7733 and an array of wchar_t if wchar_t is signed char or unsigned
7734 char. */
7735 bool char_array = (typ1 == char_type_node
7736 || typ1 == signed_char_type_node
7737 || typ1 == unsigned_char_type_node);
7738 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7739 bool char16_array = !!comptypes (typ1, char16_type_node);
7740 bool char32_array = !!comptypes (typ1, char32_type_node);
7741
7742 if (char_array || wchar_array || char16_array || char32_array)
7743 {
7744 struct c_expr expr;
7745 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7746 bool incompat_string_cst = false;
7747 expr.value = inside_init;
7748 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7749 expr.original_type = NULL;
7750 maybe_warn_string_init (init_loc, type, expr);
7751
7752 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7753 pedwarn_init (init_loc, OPT_Wpedantic,
7754 "initialization of a flexible array member");
7755
7756 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7757 TYPE_MAIN_VARIANT (type)))
7758 return inside_init;
7759
7760 if (char_array)
7761 {
7762 if (typ2 != char_type_node)
7763 incompat_string_cst = true;
7764 }
7765 else if (!comptypes (typ1, typ2))
7766 incompat_string_cst = true;
7767
7768 if (incompat_string_cst)
7769 {
7770 error_init (init_loc, "cannot initialize array of %qT from "
7771 "a string literal with type array of %qT",
7772 typ1, typ2);
7773 return error_mark_node;
7774 }
7775
7776 if (TYPE_DOMAIN (type) != NULL_TREE
7777 && TYPE_SIZE (type) != NULL_TREE
7778 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7779 {
7780 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7781 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
7782
7783 /* Subtract the size of a single (possibly wide) character
7784 because it's ok to ignore the terminating null char
7785 that is counted in the length of the constant. */
7786 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
7787 pedwarn_init (init_loc, 0,
7788 ("initializer-string for array of %qT "
7789 "is too long"), typ1);
7790 else if (warn_cxx_compat
7791 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7792 warning_at (init_loc, OPT_Wc___compat,
7793 ("initializer-string for array of %qT "
7794 "is too long for C++"), typ1);
7795 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7796 {
7797 unsigned HOST_WIDE_INT size
7798 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7799 const char *p = TREE_STRING_POINTER (inside_init);
7800
7801 inside_init = build_string (size, p);
7802 }
7803 }
7804
7805 TREE_TYPE (inside_init) = type;
7806 return inside_init;
7807 }
7808 else if (INTEGRAL_TYPE_P (typ1))
7809 {
7810 error_init (init_loc, "array of inappropriate type initialized "
7811 "from string constant");
7812 return error_mark_node;
7813 }
7814 }
7815
7816 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7817 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7818 below and handle as a constructor. */
7819 if (code == VECTOR_TYPE
7820 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7821 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7822 && TREE_CONSTANT (inside_init))
7823 {
7824 if (TREE_CODE (inside_init) == VECTOR_CST
7825 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7826 TYPE_MAIN_VARIANT (type)))
7827 return inside_init;
7828
7829 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7830 {
7831 unsigned HOST_WIDE_INT ix;
7832 tree value;
7833 bool constant_p = true;
7834
7835 /* Iterate through elements and check if all constructor
7836 elements are *_CSTs. */
7837 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7838 if (!CONSTANT_CLASS_P (value))
7839 {
7840 constant_p = false;
7841 break;
7842 }
7843
7844 if (constant_p)
7845 return build_vector_from_ctor (type,
7846 CONSTRUCTOR_ELTS (inside_init));
7847 }
7848 }
7849
7850 if (warn_sequence_point)
7851 verify_sequence_points (inside_init);
7852
7853 /* Any type can be initialized
7854 from an expression of the same type, optionally with braces. */
7855
7856 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7857 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7858 TYPE_MAIN_VARIANT (type))
7859 || (code == ARRAY_TYPE
7860 && comptypes (TREE_TYPE (inside_init), type))
7861 || (code == VECTOR_TYPE
7862 && comptypes (TREE_TYPE (inside_init), type))
7863 || (code == POINTER_TYPE
7864 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7865 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7866 TREE_TYPE (type)))))
7867 {
7868 if (code == POINTER_TYPE)
7869 {
7870 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7871 {
7872 if (TREE_CODE (inside_init) == STRING_CST
7873 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7874 inside_init = array_to_pointer_conversion
7875 (init_loc, inside_init);
7876 else
7877 {
7878 error_init (init_loc, "invalid use of non-lvalue array");
7879 return error_mark_node;
7880 }
7881 }
7882 }
7883
7884 if (code == VECTOR_TYPE)
7885 /* Although the types are compatible, we may require a
7886 conversion. */
7887 inside_init = convert (type, inside_init);
7888
7889 if (require_constant
7890 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7891 {
7892 /* As an extension, allow initializing objects with static storage
7893 duration with compound literals (which are then treated just as
7894 the brace enclosed list they contain). Also allow this for
7895 vectors, as we can only assign them with compound literals. */
7896 if (flag_isoc99 && code != VECTOR_TYPE)
7897 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7898 "is not constant");
7899 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7900 inside_init = DECL_INITIAL (decl);
7901 }
7902
7903 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7904 && TREE_CODE (inside_init) != CONSTRUCTOR)
7905 {
7906 error_init (init_loc, "array initialized from non-constant array "
7907 "expression");
7908 return error_mark_node;
7909 }
7910
7911 /* Compound expressions can only occur here if -Wpedantic or
7912 -pedantic-errors is specified. In the later case, we always want
7913 an error. In the former case, we simply want a warning. */
7914 if (require_constant && pedantic
7915 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7916 {
7917 inside_init
7918 = valid_compound_expr_initializer (inside_init,
7919 TREE_TYPE (inside_init));
7920 if (inside_init == error_mark_node)
7921 error_init (init_loc, "initializer element is not constant");
7922 else
7923 pedwarn_init (init_loc, OPT_Wpedantic,
7924 "initializer element is not constant");
7925 if (flag_pedantic_errors)
7926 inside_init = error_mark_node;
7927 }
7928 else if (require_constant
7929 && !initializer_constant_valid_p (inside_init,
7930 TREE_TYPE (inside_init)))
7931 {
7932 error_init (init_loc, "initializer element is not constant");
7933 inside_init = error_mark_node;
7934 }
7935 else if (require_constant && !maybe_const)
7936 pedwarn_init (init_loc, OPT_Wpedantic,
7937 "initializer element is not a constant expression");
7938
7939 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7940 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7941 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7942 type, inside_init, origtype,
7943 ic_init, null_pointer_constant,
7944 NULL_TREE, NULL_TREE, 0);
7945 return inside_init;
7946 }
7947
7948 /* Handle scalar types, including conversions. */
7949
7950 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7951 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7952 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7953 {
7954 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7955 && (TREE_CODE (init) == STRING_CST
7956 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7957 inside_init = init = array_to_pointer_conversion (init_loc, init);
7958 if (semantic_type)
7959 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7960 inside_init);
7961 inside_init
7962 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7963 inside_init, origtype, ic_init,
7964 null_pointer_constant, NULL_TREE, NULL_TREE,
7965 0);
7966
7967 /* Check to see if we have already given an error message. */
7968 if (inside_init == error_mark_node)
7969 ;
7970 else if (require_constant && !TREE_CONSTANT (inside_init))
7971 {
7972 error_init (init_loc, "initializer element is not constant");
7973 inside_init = error_mark_node;
7974 }
7975 else if (require_constant
7976 && !initializer_constant_valid_p (inside_init,
7977 TREE_TYPE (inside_init)))
7978 {
7979 error_init (init_loc, "initializer element is not computable at "
7980 "load time");
7981 inside_init = error_mark_node;
7982 }
7983 else if (require_constant && !maybe_const)
7984 pedwarn_init (init_loc, OPT_Wpedantic,
7985 "initializer element is not a constant expression");
7986
7987 return inside_init;
7988 }
7989
7990 /* Come here only for records and arrays. */
7991
7992 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7993 {
7994 error_init (init_loc, "variable-sized object may not be initialized");
7995 return error_mark_node;
7996 }
7997
7998 error_init (init_loc, "invalid initializer");
7999 return error_mark_node;
8000 }
8001 \f
8002 /* Handle initializers that use braces. */
8003
8004 /* Type of object we are accumulating a constructor for.
8005 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8006 static tree constructor_type;
8007
8008 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8009 left to fill. */
8010 static tree constructor_fields;
8011
8012 /* For an ARRAY_TYPE, this is the specified index
8013 at which to store the next element we get. */
8014 static tree constructor_index;
8015
8016 /* For an ARRAY_TYPE, this is the maximum index. */
8017 static tree constructor_max_index;
8018
8019 /* For a RECORD_TYPE, this is the first field not yet written out. */
8020 static tree constructor_unfilled_fields;
8021
8022 /* For an ARRAY_TYPE, this is the index of the first element
8023 not yet written out. */
8024 static tree constructor_unfilled_index;
8025
8026 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8027 This is so we can generate gaps between fields, when appropriate. */
8028 static tree constructor_bit_index;
8029
8030 /* If we are saving up the elements rather than allocating them,
8031 this is the list of elements so far (in reverse order,
8032 most recent first). */
8033 static vec<constructor_elt, va_gc> *constructor_elements;
8034
8035 /* 1 if constructor should be incrementally stored into a constructor chain,
8036 0 if all the elements should be kept in AVL tree. */
8037 static int constructor_incremental;
8038
8039 /* 1 if so far this constructor's elements are all compile-time constants. */
8040 static int constructor_constant;
8041
8042 /* 1 if so far this constructor's elements are all valid address constants. */
8043 static int constructor_simple;
8044
8045 /* 1 if this constructor has an element that cannot be part of a
8046 constant expression. */
8047 static int constructor_nonconst;
8048
8049 /* 1 if this constructor is erroneous so far. */
8050 static int constructor_erroneous;
8051
8052 /* 1 if this constructor is the universal zero initializer { 0 }. */
8053 static int constructor_zeroinit;
8054
8055 /* Structure for managing pending initializer elements, organized as an
8056 AVL tree. */
8057
8058 struct init_node
8059 {
8060 struct init_node *left, *right;
8061 struct init_node *parent;
8062 int balance;
8063 tree purpose;
8064 tree value;
8065 tree origtype;
8066 };
8067
8068 /* Tree of pending elements at this constructor level.
8069 These are elements encountered out of order
8070 which belong at places we haven't reached yet in actually
8071 writing the output.
8072 Will never hold tree nodes across GC runs. */
8073 static struct init_node *constructor_pending_elts;
8074
8075 /* The SPELLING_DEPTH of this constructor. */
8076 static int constructor_depth;
8077
8078 /* DECL node for which an initializer is being read.
8079 0 means we are reading a constructor expression
8080 such as (struct foo) {...}. */
8081 static tree constructor_decl;
8082
8083 /* Nonzero if this is an initializer for a top-level decl. */
8084 static int constructor_top_level;
8085
8086 /* Nonzero if there were any member designators in this initializer. */
8087 static int constructor_designated;
8088
8089 /* Nesting depth of designator list. */
8090 static int designator_depth;
8091
8092 /* Nonzero if there were diagnosed errors in this designator list. */
8093 static int designator_erroneous;
8094
8095 \f
8096 /* This stack has a level for each implicit or explicit level of
8097 structuring in the initializer, including the outermost one. It
8098 saves the values of most of the variables above. */
8099
8100 struct constructor_range_stack;
8101
8102 struct constructor_stack
8103 {
8104 struct constructor_stack *next;
8105 tree type;
8106 tree fields;
8107 tree index;
8108 tree max_index;
8109 tree unfilled_index;
8110 tree unfilled_fields;
8111 tree bit_index;
8112 vec<constructor_elt, va_gc> *elements;
8113 struct init_node *pending_elts;
8114 int offset;
8115 int depth;
8116 /* If value nonzero, this value should replace the entire
8117 constructor at this level. */
8118 struct c_expr replacement_value;
8119 struct constructor_range_stack *range_stack;
8120 char constant;
8121 char simple;
8122 char nonconst;
8123 char implicit;
8124 char erroneous;
8125 char outer;
8126 char incremental;
8127 char designated;
8128 int designator_depth;
8129 };
8130
8131 static struct constructor_stack *constructor_stack;
8132
8133 /* This stack represents designators from some range designator up to
8134 the last designator in the list. */
8135
8136 struct constructor_range_stack
8137 {
8138 struct constructor_range_stack *next, *prev;
8139 struct constructor_stack *stack;
8140 tree range_start;
8141 tree index;
8142 tree range_end;
8143 tree fields;
8144 };
8145
8146 static struct constructor_range_stack *constructor_range_stack;
8147
8148 /* This stack records separate initializers that are nested.
8149 Nested initializers can't happen in ANSI C, but GNU C allows them
8150 in cases like { ... (struct foo) { ... } ... }. */
8151
8152 struct initializer_stack
8153 {
8154 struct initializer_stack *next;
8155 tree decl;
8156 struct constructor_stack *constructor_stack;
8157 struct constructor_range_stack *constructor_range_stack;
8158 vec<constructor_elt, va_gc> *elements;
8159 struct spelling *spelling;
8160 struct spelling *spelling_base;
8161 int spelling_size;
8162 char top_level;
8163 char require_constant_value;
8164 char require_constant_elements;
8165 rich_location *missing_brace_richloc;
8166 };
8167
8168 static struct initializer_stack *initializer_stack;
8169 \f
8170 /* Prepare to parse and output the initializer for variable DECL. */
8171
8172 void
8173 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8174 rich_location *richloc)
8175 {
8176 const char *locus;
8177 struct initializer_stack *p = XNEW (struct initializer_stack);
8178
8179 p->decl = constructor_decl;
8180 p->require_constant_value = require_constant_value;
8181 p->require_constant_elements = require_constant_elements;
8182 p->constructor_stack = constructor_stack;
8183 p->constructor_range_stack = constructor_range_stack;
8184 p->elements = constructor_elements;
8185 p->spelling = spelling;
8186 p->spelling_base = spelling_base;
8187 p->spelling_size = spelling_size;
8188 p->top_level = constructor_top_level;
8189 p->next = initializer_stack;
8190 p->missing_brace_richloc = richloc;
8191 initializer_stack = p;
8192
8193 constructor_decl = decl;
8194 constructor_designated = 0;
8195 constructor_top_level = top_level;
8196
8197 if (decl != NULL_TREE && decl != error_mark_node)
8198 {
8199 require_constant_value = TREE_STATIC (decl);
8200 require_constant_elements
8201 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8202 /* For a scalar, you can always use any value to initialize,
8203 even within braces. */
8204 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8205 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8206 }
8207 else
8208 {
8209 require_constant_value = 0;
8210 require_constant_elements = 0;
8211 locus = _("(anonymous)");
8212 }
8213
8214 constructor_stack = 0;
8215 constructor_range_stack = 0;
8216
8217 found_missing_braces = 0;
8218
8219 spelling_base = 0;
8220 spelling_size = 0;
8221 RESTORE_SPELLING_DEPTH (0);
8222
8223 if (locus)
8224 push_string (locus);
8225 }
8226
8227 void
8228 finish_init (void)
8229 {
8230 struct initializer_stack *p = initializer_stack;
8231
8232 /* Free the whole constructor stack of this initializer. */
8233 while (constructor_stack)
8234 {
8235 struct constructor_stack *q = constructor_stack;
8236 constructor_stack = q->next;
8237 free (q);
8238 }
8239
8240 gcc_assert (!constructor_range_stack);
8241
8242 /* Pop back to the data of the outer initializer (if any). */
8243 free (spelling_base);
8244
8245 constructor_decl = p->decl;
8246 require_constant_value = p->require_constant_value;
8247 require_constant_elements = p->require_constant_elements;
8248 constructor_stack = p->constructor_stack;
8249 constructor_range_stack = p->constructor_range_stack;
8250 constructor_elements = p->elements;
8251 spelling = p->spelling;
8252 spelling_base = p->spelling_base;
8253 spelling_size = p->spelling_size;
8254 constructor_top_level = p->top_level;
8255 initializer_stack = p->next;
8256 free (p);
8257 }
8258 \f
8259 /* Call here when we see the initializer is surrounded by braces.
8260 This is instead of a call to push_init_level;
8261 it is matched by a call to pop_init_level.
8262
8263 TYPE is the type to initialize, for a constructor expression.
8264 For an initializer for a decl, TYPE is zero. */
8265
8266 void
8267 really_start_incremental_init (tree type)
8268 {
8269 struct constructor_stack *p = XNEW (struct constructor_stack);
8270
8271 if (type == NULL_TREE)
8272 type = TREE_TYPE (constructor_decl);
8273
8274 if (VECTOR_TYPE_P (type)
8275 && TYPE_VECTOR_OPAQUE (type))
8276 error ("opaque vector types cannot be initialized");
8277
8278 p->type = constructor_type;
8279 p->fields = constructor_fields;
8280 p->index = constructor_index;
8281 p->max_index = constructor_max_index;
8282 p->unfilled_index = constructor_unfilled_index;
8283 p->unfilled_fields = constructor_unfilled_fields;
8284 p->bit_index = constructor_bit_index;
8285 p->elements = constructor_elements;
8286 p->constant = constructor_constant;
8287 p->simple = constructor_simple;
8288 p->nonconst = constructor_nonconst;
8289 p->erroneous = constructor_erroneous;
8290 p->pending_elts = constructor_pending_elts;
8291 p->depth = constructor_depth;
8292 p->replacement_value.value = 0;
8293 p->replacement_value.original_code = ERROR_MARK;
8294 p->replacement_value.original_type = NULL;
8295 p->implicit = 0;
8296 p->range_stack = 0;
8297 p->outer = 0;
8298 p->incremental = constructor_incremental;
8299 p->designated = constructor_designated;
8300 p->designator_depth = designator_depth;
8301 p->next = 0;
8302 constructor_stack = p;
8303
8304 constructor_constant = 1;
8305 constructor_simple = 1;
8306 constructor_nonconst = 0;
8307 constructor_depth = SPELLING_DEPTH ();
8308 constructor_elements = NULL;
8309 constructor_pending_elts = 0;
8310 constructor_type = type;
8311 constructor_incremental = 1;
8312 constructor_designated = 0;
8313 constructor_zeroinit = 1;
8314 designator_depth = 0;
8315 designator_erroneous = 0;
8316
8317 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8318 {
8319 constructor_fields = TYPE_FIELDS (constructor_type);
8320 /* Skip any nameless bit fields at the beginning. */
8321 while (constructor_fields != NULL_TREE
8322 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8323 constructor_fields = DECL_CHAIN (constructor_fields);
8324
8325 constructor_unfilled_fields = constructor_fields;
8326 constructor_bit_index = bitsize_zero_node;
8327 }
8328 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8329 {
8330 if (TYPE_DOMAIN (constructor_type))
8331 {
8332 constructor_max_index
8333 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8334
8335 /* Detect non-empty initializations of zero-length arrays. */
8336 if (constructor_max_index == NULL_TREE
8337 && TYPE_SIZE (constructor_type))
8338 constructor_max_index = integer_minus_one_node;
8339
8340 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8341 to initialize VLAs will cause a proper error; avoid tree
8342 checking errors as well by setting a safe value. */
8343 if (constructor_max_index
8344 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8345 constructor_max_index = integer_minus_one_node;
8346
8347 constructor_index
8348 = convert (bitsizetype,
8349 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8350 }
8351 else
8352 {
8353 constructor_index = bitsize_zero_node;
8354 constructor_max_index = NULL_TREE;
8355 }
8356
8357 constructor_unfilled_index = constructor_index;
8358 }
8359 else if (VECTOR_TYPE_P (constructor_type))
8360 {
8361 /* Vectors are like simple fixed-size arrays. */
8362 constructor_max_index =
8363 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8364 constructor_index = bitsize_zero_node;
8365 constructor_unfilled_index = constructor_index;
8366 }
8367 else
8368 {
8369 /* Handle the case of int x = {5}; */
8370 constructor_fields = constructor_type;
8371 constructor_unfilled_fields = constructor_type;
8372 }
8373 }
8374 \f
8375 extern location_t last_init_list_comma;
8376
8377 /* Called when we see an open brace for a nested initializer. Finish
8378 off any pending levels with implicit braces. */
8379 void
8380 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8381 {
8382 while (constructor_stack->implicit)
8383 {
8384 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8385 && constructor_fields == NULL_TREE)
8386 process_init_element (input_location,
8387 pop_init_level (loc, 1, braced_init_obstack,
8388 last_init_list_comma),
8389 true, braced_init_obstack);
8390 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8391 && constructor_max_index
8392 && tree_int_cst_lt (constructor_max_index,
8393 constructor_index))
8394 process_init_element (input_location,
8395 pop_init_level (loc, 1, braced_init_obstack,
8396 last_init_list_comma),
8397 true, braced_init_obstack);
8398 else
8399 break;
8400 }
8401 }
8402
8403 /* Push down into a subobject, for initialization.
8404 If this is for an explicit set of braces, IMPLICIT is 0.
8405 If it is because the next element belongs at a lower level,
8406 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8407
8408 void
8409 push_init_level (location_t loc, int implicit,
8410 struct obstack *braced_init_obstack)
8411 {
8412 struct constructor_stack *p;
8413 tree value = NULL_TREE;
8414
8415 /* Unless this is an explicit brace, we need to preserve previous
8416 content if any. */
8417 if (implicit)
8418 {
8419 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8420 value = find_init_member (constructor_fields, braced_init_obstack);
8421 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8422 value = find_init_member (constructor_index, braced_init_obstack);
8423 }
8424
8425 p = XNEW (struct constructor_stack);
8426 p->type = constructor_type;
8427 p->fields = constructor_fields;
8428 p->index = constructor_index;
8429 p->max_index = constructor_max_index;
8430 p->unfilled_index = constructor_unfilled_index;
8431 p->unfilled_fields = constructor_unfilled_fields;
8432 p->bit_index = constructor_bit_index;
8433 p->elements = constructor_elements;
8434 p->constant = constructor_constant;
8435 p->simple = constructor_simple;
8436 p->nonconst = constructor_nonconst;
8437 p->erroneous = constructor_erroneous;
8438 p->pending_elts = constructor_pending_elts;
8439 p->depth = constructor_depth;
8440 p->replacement_value.value = NULL_TREE;
8441 p->replacement_value.original_code = ERROR_MARK;
8442 p->replacement_value.original_type = NULL;
8443 p->implicit = implicit;
8444 p->outer = 0;
8445 p->incremental = constructor_incremental;
8446 p->designated = constructor_designated;
8447 p->designator_depth = designator_depth;
8448 p->next = constructor_stack;
8449 p->range_stack = 0;
8450 constructor_stack = p;
8451
8452 constructor_constant = 1;
8453 constructor_simple = 1;
8454 constructor_nonconst = 0;
8455 constructor_depth = SPELLING_DEPTH ();
8456 constructor_elements = NULL;
8457 constructor_incremental = 1;
8458 constructor_designated = 0;
8459 constructor_pending_elts = 0;
8460 if (!implicit)
8461 {
8462 p->range_stack = constructor_range_stack;
8463 constructor_range_stack = 0;
8464 designator_depth = 0;
8465 designator_erroneous = 0;
8466 }
8467
8468 /* Don't die if an entire brace-pair level is superfluous
8469 in the containing level. */
8470 if (constructor_type == NULL_TREE)
8471 ;
8472 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8473 {
8474 /* Don't die if there are extra init elts at the end. */
8475 if (constructor_fields == NULL_TREE)
8476 constructor_type = NULL_TREE;
8477 else
8478 {
8479 constructor_type = TREE_TYPE (constructor_fields);
8480 push_member_name (constructor_fields);
8481 constructor_depth++;
8482 }
8483 /* If upper initializer is designated, then mark this as
8484 designated too to prevent bogus warnings. */
8485 constructor_designated = p->designated;
8486 }
8487 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8488 {
8489 constructor_type = TREE_TYPE (constructor_type);
8490 push_array_bounds (tree_to_uhwi (constructor_index));
8491 constructor_depth++;
8492 }
8493
8494 if (constructor_type == NULL_TREE)
8495 {
8496 error_init (loc, "extra brace group at end of initializer");
8497 constructor_fields = NULL_TREE;
8498 constructor_unfilled_fields = NULL_TREE;
8499 return;
8500 }
8501
8502 if (value && TREE_CODE (value) == CONSTRUCTOR)
8503 {
8504 constructor_constant = TREE_CONSTANT (value);
8505 constructor_simple = TREE_STATIC (value);
8506 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8507 constructor_elements = CONSTRUCTOR_ELTS (value);
8508 if (!vec_safe_is_empty (constructor_elements)
8509 && (TREE_CODE (constructor_type) == RECORD_TYPE
8510 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8511 set_nonincremental_init (braced_init_obstack);
8512 }
8513
8514 if (implicit == 1)
8515 {
8516 found_missing_braces = 1;
8517 if (initializer_stack->missing_brace_richloc)
8518 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8519 (loc, "{");
8520 }
8521
8522 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8523 {
8524 constructor_fields = TYPE_FIELDS (constructor_type);
8525 /* Skip any nameless bit fields at the beginning. */
8526 while (constructor_fields != NULL_TREE
8527 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8528 constructor_fields = DECL_CHAIN (constructor_fields);
8529
8530 constructor_unfilled_fields = constructor_fields;
8531 constructor_bit_index = bitsize_zero_node;
8532 }
8533 else if (VECTOR_TYPE_P (constructor_type))
8534 {
8535 /* Vectors are like simple fixed-size arrays. */
8536 constructor_max_index =
8537 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8538 constructor_index = bitsize_int (0);
8539 constructor_unfilled_index = constructor_index;
8540 }
8541 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8542 {
8543 if (TYPE_DOMAIN (constructor_type))
8544 {
8545 constructor_max_index
8546 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8547
8548 /* Detect non-empty initializations of zero-length arrays. */
8549 if (constructor_max_index == NULL_TREE
8550 && TYPE_SIZE (constructor_type))
8551 constructor_max_index = integer_minus_one_node;
8552
8553 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8554 to initialize VLAs will cause a proper error; avoid tree
8555 checking errors as well by setting a safe value. */
8556 if (constructor_max_index
8557 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8558 constructor_max_index = integer_minus_one_node;
8559
8560 constructor_index
8561 = convert (bitsizetype,
8562 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8563 }
8564 else
8565 constructor_index = bitsize_zero_node;
8566
8567 constructor_unfilled_index = constructor_index;
8568 if (value && TREE_CODE (value) == STRING_CST)
8569 {
8570 /* We need to split the char/wchar array into individual
8571 characters, so that we don't have to special case it
8572 everywhere. */
8573 set_nonincremental_init_from_string (value, braced_init_obstack);
8574 }
8575 }
8576 else
8577 {
8578 if (constructor_type != error_mark_node)
8579 warning_init (input_location, 0, "braces around scalar initializer");
8580 constructor_fields = constructor_type;
8581 constructor_unfilled_fields = constructor_type;
8582 }
8583 }
8584
8585 /* At the end of an implicit or explicit brace level,
8586 finish up that level of constructor. If a single expression
8587 with redundant braces initialized that level, return the
8588 c_expr structure for that expression. Otherwise, the original_code
8589 element is set to ERROR_MARK.
8590 If we were outputting the elements as they are read, return 0 as the value
8591 from inner levels (process_init_element ignores that),
8592 but return error_mark_node as the value from the outermost level
8593 (that's what we want to put in DECL_INITIAL).
8594 Otherwise, return a CONSTRUCTOR expression as the value. */
8595
8596 struct c_expr
8597 pop_init_level (location_t loc, int implicit,
8598 struct obstack *braced_init_obstack,
8599 location_t insert_before)
8600 {
8601 struct constructor_stack *p;
8602 struct c_expr ret;
8603 ret.value = NULL_TREE;
8604 ret.original_code = ERROR_MARK;
8605 ret.original_type = NULL;
8606
8607 if (implicit == 0)
8608 {
8609 /* When we come to an explicit close brace,
8610 pop any inner levels that didn't have explicit braces. */
8611 while (constructor_stack->implicit)
8612 process_init_element (input_location,
8613 pop_init_level (loc, 1, braced_init_obstack,
8614 insert_before),
8615 true, braced_init_obstack);
8616 gcc_assert (!constructor_range_stack);
8617 }
8618 else
8619 if (initializer_stack->missing_brace_richloc)
8620 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8621 (insert_before, "}");
8622
8623 /* Now output all pending elements. */
8624 constructor_incremental = 1;
8625 output_pending_init_elements (1, braced_init_obstack);
8626
8627 p = constructor_stack;
8628
8629 /* Error for initializing a flexible array member, or a zero-length
8630 array member in an inappropriate context. */
8631 if (constructor_type && constructor_fields
8632 && TREE_CODE (constructor_type) == ARRAY_TYPE
8633 && TYPE_DOMAIN (constructor_type)
8634 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8635 {
8636 /* Silently discard empty initializations. The parser will
8637 already have pedwarned for empty brackets. */
8638 if (integer_zerop (constructor_unfilled_index))
8639 constructor_type = NULL_TREE;
8640 else
8641 {
8642 gcc_assert (!TYPE_SIZE (constructor_type));
8643
8644 if (constructor_depth > 2)
8645 error_init (loc, "initialization of flexible array member in a nested context");
8646 else
8647 pedwarn_init (loc, OPT_Wpedantic,
8648 "initialization of a flexible array member");
8649
8650 /* We have already issued an error message for the existence
8651 of a flexible array member not at the end of the structure.
8652 Discard the initializer so that we do not die later. */
8653 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8654 constructor_type = NULL_TREE;
8655 }
8656 }
8657
8658 switch (vec_safe_length (constructor_elements))
8659 {
8660 case 0:
8661 /* Initialization with { } counts as zeroinit. */
8662 constructor_zeroinit = 1;
8663 break;
8664 case 1:
8665 /* This might be zeroinit as well. */
8666 if (integer_zerop ((*constructor_elements)[0].value))
8667 constructor_zeroinit = 1;
8668 break;
8669 default:
8670 /* If the constructor has more than one element, it can't be { 0 }. */
8671 constructor_zeroinit = 0;
8672 break;
8673 }
8674
8675 /* Warn when some structs are initialized with direct aggregation. */
8676 if (!implicit && found_missing_braces && warn_missing_braces
8677 && !constructor_zeroinit)
8678 {
8679 gcc_assert (initializer_stack->missing_brace_richloc);
8680 warning_at (initializer_stack->missing_brace_richloc,
8681 OPT_Wmissing_braces,
8682 "missing braces around initializer");
8683 }
8684
8685 /* Warn when some struct elements are implicitly initialized to zero. */
8686 if (warn_missing_field_initializers
8687 && constructor_type
8688 && TREE_CODE (constructor_type) == RECORD_TYPE
8689 && constructor_unfilled_fields)
8690 {
8691 /* Do not warn for flexible array members or zero-length arrays. */
8692 while (constructor_unfilled_fields
8693 && (!DECL_SIZE (constructor_unfilled_fields)
8694 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8695 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8696
8697 if (constructor_unfilled_fields
8698 /* Do not warn if this level of the initializer uses member
8699 designators; it is likely to be deliberate. */
8700 && !constructor_designated
8701 /* Do not warn about initializing with { 0 } or with { }. */
8702 && !constructor_zeroinit)
8703 {
8704 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8705 "missing initializer for field %qD of %qT",
8706 constructor_unfilled_fields,
8707 constructor_type))
8708 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8709 "%qD declared here", constructor_unfilled_fields);
8710 }
8711 }
8712
8713 /* Pad out the end of the structure. */
8714 if (p->replacement_value.value)
8715 /* If this closes a superfluous brace pair,
8716 just pass out the element between them. */
8717 ret = p->replacement_value;
8718 else if (constructor_type == NULL_TREE)
8719 ;
8720 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8721 && TREE_CODE (constructor_type) != ARRAY_TYPE
8722 && !VECTOR_TYPE_P (constructor_type))
8723 {
8724 /* A nonincremental scalar initializer--just return
8725 the element, after verifying there is just one. */
8726 if (vec_safe_is_empty (constructor_elements))
8727 {
8728 if (!constructor_erroneous)
8729 error_init (loc, "empty scalar initializer");
8730 ret.value = error_mark_node;
8731 }
8732 else if (vec_safe_length (constructor_elements) != 1)
8733 {
8734 error_init (loc, "extra elements in scalar initializer");
8735 ret.value = (*constructor_elements)[0].value;
8736 }
8737 else
8738 ret.value = (*constructor_elements)[0].value;
8739 }
8740 else
8741 {
8742 if (constructor_erroneous)
8743 ret.value = error_mark_node;
8744 else
8745 {
8746 ret.value = build_constructor (constructor_type,
8747 constructor_elements);
8748 if (constructor_constant)
8749 TREE_CONSTANT (ret.value) = 1;
8750 if (constructor_constant && constructor_simple)
8751 TREE_STATIC (ret.value) = 1;
8752 if (constructor_nonconst)
8753 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8754 }
8755 }
8756
8757 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8758 {
8759 if (constructor_nonconst)
8760 ret.original_code = C_MAYBE_CONST_EXPR;
8761 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8762 ret.original_code = ERROR_MARK;
8763 }
8764
8765 constructor_type = p->type;
8766 constructor_fields = p->fields;
8767 constructor_index = p->index;
8768 constructor_max_index = p->max_index;
8769 constructor_unfilled_index = p->unfilled_index;
8770 constructor_unfilled_fields = p->unfilled_fields;
8771 constructor_bit_index = p->bit_index;
8772 constructor_elements = p->elements;
8773 constructor_constant = p->constant;
8774 constructor_simple = p->simple;
8775 constructor_nonconst = p->nonconst;
8776 constructor_erroneous = p->erroneous;
8777 constructor_incremental = p->incremental;
8778 constructor_designated = p->designated;
8779 designator_depth = p->designator_depth;
8780 constructor_pending_elts = p->pending_elts;
8781 constructor_depth = p->depth;
8782 if (!p->implicit)
8783 constructor_range_stack = p->range_stack;
8784 RESTORE_SPELLING_DEPTH (constructor_depth);
8785
8786 constructor_stack = p->next;
8787 free (p);
8788
8789 if (ret.value == NULL_TREE && constructor_stack == 0)
8790 ret.value = error_mark_node;
8791 return ret;
8792 }
8793
8794 /* Common handling for both array range and field name designators.
8795 ARRAY argument is nonzero for array ranges. Returns false for success. */
8796
8797 static bool
8798 set_designator (location_t loc, bool array,
8799 struct obstack *braced_init_obstack)
8800 {
8801 tree subtype;
8802 enum tree_code subcode;
8803
8804 /* Don't die if an entire brace-pair level is superfluous
8805 in the containing level. */
8806 if (constructor_type == NULL_TREE)
8807 return true;
8808
8809 /* If there were errors in this designator list already, bail out
8810 silently. */
8811 if (designator_erroneous)
8812 return true;
8813
8814 if (!designator_depth)
8815 {
8816 gcc_assert (!constructor_range_stack);
8817
8818 /* Designator list starts at the level of closest explicit
8819 braces. */
8820 while (constructor_stack->implicit)
8821 process_init_element (input_location,
8822 pop_init_level (loc, 1, braced_init_obstack,
8823 last_init_list_comma),
8824 true, braced_init_obstack);
8825 constructor_designated = 1;
8826 return false;
8827 }
8828
8829 switch (TREE_CODE (constructor_type))
8830 {
8831 case RECORD_TYPE:
8832 case UNION_TYPE:
8833 subtype = TREE_TYPE (constructor_fields);
8834 if (subtype != error_mark_node)
8835 subtype = TYPE_MAIN_VARIANT (subtype);
8836 break;
8837 case ARRAY_TYPE:
8838 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8839 break;
8840 default:
8841 gcc_unreachable ();
8842 }
8843
8844 subcode = TREE_CODE (subtype);
8845 if (array && subcode != ARRAY_TYPE)
8846 {
8847 error_init (loc, "array index in non-array initializer");
8848 return true;
8849 }
8850 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8851 {
8852 error_init (loc, "field name not in record or union initializer");
8853 return true;
8854 }
8855
8856 constructor_designated = 1;
8857 finish_implicit_inits (loc, braced_init_obstack);
8858 push_init_level (loc, 2, braced_init_obstack);
8859 return false;
8860 }
8861
8862 /* If there are range designators in designator list, push a new designator
8863 to constructor_range_stack. RANGE_END is end of such stack range or
8864 NULL_TREE if there is no range designator at this level. */
8865
8866 static void
8867 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8868 {
8869 struct constructor_range_stack *p;
8870
8871 p = (struct constructor_range_stack *)
8872 obstack_alloc (braced_init_obstack,
8873 sizeof (struct constructor_range_stack));
8874 p->prev = constructor_range_stack;
8875 p->next = 0;
8876 p->fields = constructor_fields;
8877 p->range_start = constructor_index;
8878 p->index = constructor_index;
8879 p->stack = constructor_stack;
8880 p->range_end = range_end;
8881 if (constructor_range_stack)
8882 constructor_range_stack->next = p;
8883 constructor_range_stack = p;
8884 }
8885
8886 /* Within an array initializer, specify the next index to be initialized.
8887 FIRST is that index. If LAST is nonzero, then initialize a range
8888 of indices, running from FIRST through LAST. */
8889
8890 void
8891 set_init_index (location_t loc, tree first, tree last,
8892 struct obstack *braced_init_obstack)
8893 {
8894 if (set_designator (loc, true, braced_init_obstack))
8895 return;
8896
8897 designator_erroneous = 1;
8898
8899 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8900 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8901 {
8902 error_init (loc, "array index in initializer not of integer type");
8903 return;
8904 }
8905
8906 if (TREE_CODE (first) != INTEGER_CST)
8907 {
8908 first = c_fully_fold (first, false, NULL);
8909 if (TREE_CODE (first) == INTEGER_CST)
8910 pedwarn_init (loc, OPT_Wpedantic,
8911 "array index in initializer is not "
8912 "an integer constant expression");
8913 }
8914
8915 if (last && TREE_CODE (last) != INTEGER_CST)
8916 {
8917 last = c_fully_fold (last, false, NULL);
8918 if (TREE_CODE (last) == INTEGER_CST)
8919 pedwarn_init (loc, OPT_Wpedantic,
8920 "array index in initializer is not "
8921 "an integer constant expression");
8922 }
8923
8924 if (TREE_CODE (first) != INTEGER_CST)
8925 error_init (loc, "nonconstant array index in initializer");
8926 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8927 error_init (loc, "nonconstant array index in initializer");
8928 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8929 error_init (loc, "array index in non-array initializer");
8930 else if (tree_int_cst_sgn (first) == -1)
8931 error_init (loc, "array index in initializer exceeds array bounds");
8932 else if (constructor_max_index
8933 && tree_int_cst_lt (constructor_max_index, first))
8934 error_init (loc, "array index in initializer exceeds array bounds");
8935 else
8936 {
8937 constant_expression_warning (first);
8938 if (last)
8939 constant_expression_warning (last);
8940 constructor_index = convert (bitsizetype, first);
8941 if (tree_int_cst_lt (constructor_index, first))
8942 {
8943 constructor_index = copy_node (constructor_index);
8944 TREE_OVERFLOW (constructor_index) = 1;
8945 }
8946
8947 if (last)
8948 {
8949 if (tree_int_cst_equal (first, last))
8950 last = NULL_TREE;
8951 else if (tree_int_cst_lt (last, first))
8952 {
8953 error_init (loc, "empty index range in initializer");
8954 last = NULL_TREE;
8955 }
8956 else
8957 {
8958 last = convert (bitsizetype, last);
8959 if (constructor_max_index != NULL_TREE
8960 && tree_int_cst_lt (constructor_max_index, last))
8961 {
8962 error_init (loc, "array index range in initializer exceeds "
8963 "array bounds");
8964 last = NULL_TREE;
8965 }
8966 }
8967 }
8968
8969 designator_depth++;
8970 designator_erroneous = 0;
8971 if (constructor_range_stack || last)
8972 push_range_stack (last, braced_init_obstack);
8973 }
8974 }
8975
8976 /* Within a struct initializer, specify the next field to be initialized. */
8977
8978 void
8979 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8980 struct obstack *braced_init_obstack)
8981 {
8982 tree field;
8983
8984 if (set_designator (loc, false, braced_init_obstack))
8985 return;
8986
8987 designator_erroneous = 1;
8988
8989 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8990 {
8991 error_init (loc, "field name not in record or union initializer");
8992 return;
8993 }
8994
8995 field = lookup_field (constructor_type, fieldname);
8996
8997 if (field == NULL_TREE)
8998 {
8999 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9000 if (guessed_id)
9001 {
9002 gcc_rich_location rich_loc (fieldname_loc);
9003 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9004 error_at (&rich_loc,
9005 "%qT has no member named %qE; did you mean %qE?",
9006 constructor_type, fieldname, guessed_id);
9007 }
9008 else
9009 error_at (fieldname_loc, "%qT has no member named %qE",
9010 constructor_type, fieldname);
9011 }
9012 else
9013 do
9014 {
9015 constructor_fields = TREE_VALUE (field);
9016 designator_depth++;
9017 designator_erroneous = 0;
9018 if (constructor_range_stack)
9019 push_range_stack (NULL_TREE, braced_init_obstack);
9020 field = TREE_CHAIN (field);
9021 if (field)
9022 {
9023 if (set_designator (loc, false, braced_init_obstack))
9024 return;
9025 }
9026 }
9027 while (field != NULL_TREE);
9028 }
9029 \f
9030 /* Add a new initializer to the tree of pending initializers. PURPOSE
9031 identifies the initializer, either array index or field in a structure.
9032 VALUE is the value of that index or field. If ORIGTYPE is not
9033 NULL_TREE, it is the original type of VALUE.
9034
9035 IMPLICIT is true if value comes from pop_init_level (1),
9036 the new initializer has been merged with the existing one
9037 and thus no warnings should be emitted about overriding an
9038 existing initializer. */
9039
9040 static void
9041 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9042 bool implicit, struct obstack *braced_init_obstack)
9043 {
9044 struct init_node *p, **q, *r;
9045
9046 q = &constructor_pending_elts;
9047 p = 0;
9048
9049 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9050 {
9051 while (*q != 0)
9052 {
9053 p = *q;
9054 if (tree_int_cst_lt (purpose, p->purpose))
9055 q = &p->left;
9056 else if (tree_int_cst_lt (p->purpose, purpose))
9057 q = &p->right;
9058 else
9059 {
9060 if (!implicit)
9061 {
9062 if (TREE_SIDE_EFFECTS (p->value))
9063 warning_init (loc, OPT_Woverride_init_side_effects,
9064 "initialized field with side-effects "
9065 "overwritten");
9066 else if (warn_override_init)
9067 warning_init (loc, OPT_Woverride_init,
9068 "initialized field overwritten");
9069 }
9070 p->value = value;
9071 p->origtype = origtype;
9072 return;
9073 }
9074 }
9075 }
9076 else
9077 {
9078 tree bitpos;
9079
9080 bitpos = bit_position (purpose);
9081 while (*q != NULL)
9082 {
9083 p = *q;
9084 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9085 q = &p->left;
9086 else if (p->purpose != purpose)
9087 q = &p->right;
9088 else
9089 {
9090 if (!implicit)
9091 {
9092 if (TREE_SIDE_EFFECTS (p->value))
9093 warning_init (loc, OPT_Woverride_init_side_effects,
9094 "initialized field with side-effects "
9095 "overwritten");
9096 else if (warn_override_init)
9097 warning_init (loc, OPT_Woverride_init,
9098 "initialized field overwritten");
9099 }
9100 p->value = value;
9101 p->origtype = origtype;
9102 return;
9103 }
9104 }
9105 }
9106
9107 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9108 sizeof (struct init_node));
9109 r->purpose = purpose;
9110 r->value = value;
9111 r->origtype = origtype;
9112
9113 *q = r;
9114 r->parent = p;
9115 r->left = 0;
9116 r->right = 0;
9117 r->balance = 0;
9118
9119 while (p)
9120 {
9121 struct init_node *s;
9122
9123 if (r == p->left)
9124 {
9125 if (p->balance == 0)
9126 p->balance = -1;
9127 else if (p->balance < 0)
9128 {
9129 if (r->balance < 0)
9130 {
9131 /* L rotation. */
9132 p->left = r->right;
9133 if (p->left)
9134 p->left->parent = p;
9135 r->right = p;
9136
9137 p->balance = 0;
9138 r->balance = 0;
9139
9140 s = p->parent;
9141 p->parent = r;
9142 r->parent = s;
9143 if (s)
9144 {
9145 if (s->left == p)
9146 s->left = r;
9147 else
9148 s->right = r;
9149 }
9150 else
9151 constructor_pending_elts = r;
9152 }
9153 else
9154 {
9155 /* LR rotation. */
9156 struct init_node *t = r->right;
9157
9158 r->right = t->left;
9159 if (r->right)
9160 r->right->parent = r;
9161 t->left = r;
9162
9163 p->left = t->right;
9164 if (p->left)
9165 p->left->parent = p;
9166 t->right = p;
9167
9168 p->balance = t->balance < 0;
9169 r->balance = -(t->balance > 0);
9170 t->balance = 0;
9171
9172 s = p->parent;
9173 p->parent = t;
9174 r->parent = t;
9175 t->parent = s;
9176 if (s)
9177 {
9178 if (s->left == p)
9179 s->left = t;
9180 else
9181 s->right = t;
9182 }
9183 else
9184 constructor_pending_elts = t;
9185 }
9186 break;
9187 }
9188 else
9189 {
9190 /* p->balance == +1; growth of left side balances the node. */
9191 p->balance = 0;
9192 break;
9193 }
9194 }
9195 else /* r == p->right */
9196 {
9197 if (p->balance == 0)
9198 /* Growth propagation from right side. */
9199 p->balance++;
9200 else if (p->balance > 0)
9201 {
9202 if (r->balance > 0)
9203 {
9204 /* R rotation. */
9205 p->right = r->left;
9206 if (p->right)
9207 p->right->parent = p;
9208 r->left = p;
9209
9210 p->balance = 0;
9211 r->balance = 0;
9212
9213 s = p->parent;
9214 p->parent = r;
9215 r->parent = s;
9216 if (s)
9217 {
9218 if (s->left == p)
9219 s->left = r;
9220 else
9221 s->right = r;
9222 }
9223 else
9224 constructor_pending_elts = r;
9225 }
9226 else /* r->balance == -1 */
9227 {
9228 /* RL rotation */
9229 struct init_node *t = r->left;
9230
9231 r->left = t->right;
9232 if (r->left)
9233 r->left->parent = r;
9234 t->right = r;
9235
9236 p->right = t->left;
9237 if (p->right)
9238 p->right->parent = p;
9239 t->left = p;
9240
9241 r->balance = (t->balance < 0);
9242 p->balance = -(t->balance > 0);
9243 t->balance = 0;
9244
9245 s = p->parent;
9246 p->parent = t;
9247 r->parent = t;
9248 t->parent = s;
9249 if (s)
9250 {
9251 if (s->left == p)
9252 s->left = t;
9253 else
9254 s->right = t;
9255 }
9256 else
9257 constructor_pending_elts = t;
9258 }
9259 break;
9260 }
9261 else
9262 {
9263 /* p->balance == -1; growth of right side balances the node. */
9264 p->balance = 0;
9265 break;
9266 }
9267 }
9268
9269 r = p;
9270 p = p->parent;
9271 }
9272 }
9273
9274 /* Build AVL tree from a sorted chain. */
9275
9276 static void
9277 set_nonincremental_init (struct obstack * braced_init_obstack)
9278 {
9279 unsigned HOST_WIDE_INT ix;
9280 tree index, value;
9281
9282 if (TREE_CODE (constructor_type) != RECORD_TYPE
9283 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9284 return;
9285
9286 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9287 add_pending_init (input_location, index, value, NULL_TREE, true,
9288 braced_init_obstack);
9289 constructor_elements = NULL;
9290 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9291 {
9292 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9293 /* Skip any nameless bit fields at the beginning. */
9294 while (constructor_unfilled_fields != NULL_TREE
9295 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9296 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9297
9298 }
9299 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9300 {
9301 if (TYPE_DOMAIN (constructor_type))
9302 constructor_unfilled_index
9303 = convert (bitsizetype,
9304 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9305 else
9306 constructor_unfilled_index = bitsize_zero_node;
9307 }
9308 constructor_incremental = 0;
9309 }
9310
9311 /* Build AVL tree from a string constant. */
9312
9313 static void
9314 set_nonincremental_init_from_string (tree str,
9315 struct obstack * braced_init_obstack)
9316 {
9317 tree value, purpose, type;
9318 HOST_WIDE_INT val[2];
9319 const char *p, *end;
9320 int byte, wchar_bytes, charwidth, bitpos;
9321
9322 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9323
9324 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9325 charwidth = TYPE_PRECISION (char_type_node);
9326 gcc_assert ((size_t) wchar_bytes * charwidth
9327 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9328 type = TREE_TYPE (constructor_type);
9329 p = TREE_STRING_POINTER (str);
9330 end = p + TREE_STRING_LENGTH (str);
9331
9332 for (purpose = bitsize_zero_node;
9333 p < end
9334 && !(constructor_max_index
9335 && tree_int_cst_lt (constructor_max_index, purpose));
9336 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9337 {
9338 if (wchar_bytes == 1)
9339 {
9340 val[0] = (unsigned char) *p++;
9341 val[1] = 0;
9342 }
9343 else
9344 {
9345 val[1] = 0;
9346 val[0] = 0;
9347 for (byte = 0; byte < wchar_bytes; byte++)
9348 {
9349 if (BYTES_BIG_ENDIAN)
9350 bitpos = (wchar_bytes - byte - 1) * charwidth;
9351 else
9352 bitpos = byte * charwidth;
9353 val[bitpos / HOST_BITS_PER_WIDE_INT]
9354 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9355 << (bitpos % HOST_BITS_PER_WIDE_INT);
9356 }
9357 }
9358
9359 if (!TYPE_UNSIGNED (type))
9360 {
9361 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9362 if (bitpos < HOST_BITS_PER_WIDE_INT)
9363 {
9364 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9365 {
9366 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9367 val[1] = -1;
9368 }
9369 }
9370 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9371 {
9372 if (val[0] < 0)
9373 val[1] = -1;
9374 }
9375 else if (val[1] & (HOST_WIDE_INT_1
9376 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9377 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9378 }
9379
9380 value = wide_int_to_tree (type,
9381 wide_int::from_array (val, 2,
9382 HOST_BITS_PER_WIDE_INT * 2));
9383 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9384 braced_init_obstack);
9385 }
9386
9387 constructor_incremental = 0;
9388 }
9389
9390 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9391 not initialized yet. */
9392
9393 static tree
9394 find_init_member (tree field, struct obstack * braced_init_obstack)
9395 {
9396 struct init_node *p;
9397
9398 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9399 {
9400 if (constructor_incremental
9401 && tree_int_cst_lt (field, constructor_unfilled_index))
9402 set_nonincremental_init (braced_init_obstack);
9403
9404 p = constructor_pending_elts;
9405 while (p)
9406 {
9407 if (tree_int_cst_lt (field, p->purpose))
9408 p = p->left;
9409 else if (tree_int_cst_lt (p->purpose, field))
9410 p = p->right;
9411 else
9412 return p->value;
9413 }
9414 }
9415 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9416 {
9417 tree bitpos = bit_position (field);
9418
9419 if (constructor_incremental
9420 && (!constructor_unfilled_fields
9421 || tree_int_cst_lt (bitpos,
9422 bit_position (constructor_unfilled_fields))))
9423 set_nonincremental_init (braced_init_obstack);
9424
9425 p = constructor_pending_elts;
9426 while (p)
9427 {
9428 if (field == p->purpose)
9429 return p->value;
9430 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9431 p = p->left;
9432 else
9433 p = p->right;
9434 }
9435 }
9436 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9437 {
9438 if (!vec_safe_is_empty (constructor_elements)
9439 && (constructor_elements->last ().index == field))
9440 return constructor_elements->last ().value;
9441 }
9442 return NULL_TREE;
9443 }
9444
9445 /* "Output" the next constructor element.
9446 At top level, really output it to assembler code now.
9447 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9448 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9449 TYPE is the data type that the containing data type wants here.
9450 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9451 If VALUE is a string constant, STRICT_STRING is true if it is
9452 unparenthesized or we should not warn here for it being parenthesized.
9453 For other types of VALUE, STRICT_STRING is not used.
9454
9455 PENDING if true means output pending elements that belong
9456 right after this element. (PENDING is normally true;
9457 it is false while outputting pending elements, to avoid recursion.)
9458
9459 IMPLICIT is true if value comes from pop_init_level (1),
9460 the new initializer has been merged with the existing one
9461 and thus no warnings should be emitted about overriding an
9462 existing initializer. */
9463
9464 static void
9465 output_init_element (location_t loc, tree value, tree origtype,
9466 bool strict_string, tree type, tree field, bool pending,
9467 bool implicit, struct obstack * braced_init_obstack)
9468 {
9469 tree semantic_type = NULL_TREE;
9470 bool maybe_const = true;
9471 bool npc;
9472
9473 if (type == error_mark_node || value == error_mark_node)
9474 {
9475 constructor_erroneous = 1;
9476 return;
9477 }
9478 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9479 && (TREE_CODE (value) == STRING_CST
9480 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9481 && !(TREE_CODE (value) == STRING_CST
9482 && TREE_CODE (type) == ARRAY_TYPE
9483 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9484 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9485 TYPE_MAIN_VARIANT (type)))
9486 value = array_to_pointer_conversion (input_location, value);
9487
9488 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9489 && require_constant_value && pending)
9490 {
9491 /* As an extension, allow initializing objects with static storage
9492 duration with compound literals (which are then treated just as
9493 the brace enclosed list they contain). */
9494 if (flag_isoc99)
9495 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9496 "constant");
9497 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9498 value = DECL_INITIAL (decl);
9499 }
9500
9501 npc = null_pointer_constant_p (value);
9502 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9503 {
9504 semantic_type = TREE_TYPE (value);
9505 value = TREE_OPERAND (value, 0);
9506 }
9507 value = c_fully_fold (value, require_constant_value, &maybe_const);
9508
9509 if (value == error_mark_node)
9510 constructor_erroneous = 1;
9511 else if (!TREE_CONSTANT (value))
9512 constructor_constant = 0;
9513 else if (!initializer_constant_valid_p (value,
9514 TREE_TYPE (value),
9515 AGGREGATE_TYPE_P (constructor_type)
9516 && TYPE_REVERSE_STORAGE_ORDER
9517 (constructor_type))
9518 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9519 && DECL_C_BIT_FIELD (field)
9520 && TREE_CODE (value) != INTEGER_CST))
9521 constructor_simple = 0;
9522 if (!maybe_const)
9523 constructor_nonconst = 1;
9524
9525 /* Digest the initializer and issue any errors about incompatible
9526 types before issuing errors about non-constant initializers. */
9527 tree new_value = value;
9528 if (semantic_type)
9529 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9530 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9531 require_constant_value);
9532 if (new_value == error_mark_node)
9533 {
9534 constructor_erroneous = 1;
9535 return;
9536 }
9537 if (require_constant_value || require_constant_elements)
9538 constant_expression_warning (new_value);
9539
9540 /* Proceed to check the constness of the original initializer. */
9541 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9542 {
9543 if (require_constant_value)
9544 {
9545 error_init (loc, "initializer element is not constant");
9546 value = error_mark_node;
9547 }
9548 else if (require_constant_elements)
9549 pedwarn (loc, OPT_Wpedantic,
9550 "initializer element is not computable at load time");
9551 }
9552 else if (!maybe_const
9553 && (require_constant_value || require_constant_elements))
9554 pedwarn_init (loc, OPT_Wpedantic,
9555 "initializer element is not a constant expression");
9556
9557 /* Issue -Wc++-compat warnings about initializing a bitfield with
9558 enum type. */
9559 if (warn_cxx_compat
9560 && field != NULL_TREE
9561 && TREE_CODE (field) == FIELD_DECL
9562 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9563 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9564 != TYPE_MAIN_VARIANT (type))
9565 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9566 {
9567 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9568 if (checktype != error_mark_node
9569 && (TYPE_MAIN_VARIANT (checktype)
9570 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9571 warning_init (loc, OPT_Wc___compat,
9572 "enum conversion in initialization is invalid in C++");
9573 }
9574
9575 /* If this field is empty and does not have side effects (and is not at
9576 the end of structure), don't do anything other than checking the
9577 initializer. */
9578 if (field
9579 && (TREE_TYPE (field) == error_mark_node
9580 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9581 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9582 && !TREE_SIDE_EFFECTS (new_value)
9583 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9584 || DECL_CHAIN (field)))))
9585 return;
9586
9587 /* Finally, set VALUE to the initializer value digested above. */
9588 value = new_value;
9589
9590 /* If this element doesn't come next in sequence,
9591 put it on constructor_pending_elts. */
9592 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9593 && (!constructor_incremental
9594 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9595 {
9596 if (constructor_incremental
9597 && tree_int_cst_lt (field, constructor_unfilled_index))
9598 set_nonincremental_init (braced_init_obstack);
9599
9600 add_pending_init (loc, field, value, origtype, implicit,
9601 braced_init_obstack);
9602 return;
9603 }
9604 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9605 && (!constructor_incremental
9606 || field != constructor_unfilled_fields))
9607 {
9608 /* We do this for records but not for unions. In a union,
9609 no matter which field is specified, it can be initialized
9610 right away since it starts at the beginning of the union. */
9611 if (constructor_incremental)
9612 {
9613 if (!constructor_unfilled_fields)
9614 set_nonincremental_init (braced_init_obstack);
9615 else
9616 {
9617 tree bitpos, unfillpos;
9618
9619 bitpos = bit_position (field);
9620 unfillpos = bit_position (constructor_unfilled_fields);
9621
9622 if (tree_int_cst_lt (bitpos, unfillpos))
9623 set_nonincremental_init (braced_init_obstack);
9624 }
9625 }
9626
9627 add_pending_init (loc, field, value, origtype, implicit,
9628 braced_init_obstack);
9629 return;
9630 }
9631 else if (TREE_CODE (constructor_type) == UNION_TYPE
9632 && !vec_safe_is_empty (constructor_elements))
9633 {
9634 if (!implicit)
9635 {
9636 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9637 warning_init (loc, OPT_Woverride_init_side_effects,
9638 "initialized field with side-effects overwritten");
9639 else if (warn_override_init)
9640 warning_init (loc, OPT_Woverride_init,
9641 "initialized field overwritten");
9642 }
9643
9644 /* We can have just one union field set. */
9645 constructor_elements = NULL;
9646 }
9647
9648 /* Otherwise, output this element either to
9649 constructor_elements or to the assembler file. */
9650
9651 constructor_elt celt = {field, value};
9652 vec_safe_push (constructor_elements, celt);
9653
9654 /* Advance the variable that indicates sequential elements output. */
9655 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9656 constructor_unfilled_index
9657 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9658 bitsize_one_node);
9659 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9660 {
9661 constructor_unfilled_fields
9662 = DECL_CHAIN (constructor_unfilled_fields);
9663
9664 /* Skip any nameless bit fields. */
9665 while (constructor_unfilled_fields != NULL_TREE
9666 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9667 constructor_unfilled_fields =
9668 DECL_CHAIN (constructor_unfilled_fields);
9669 }
9670 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9671 constructor_unfilled_fields = NULL_TREE;
9672
9673 /* Now output any pending elements which have become next. */
9674 if (pending)
9675 output_pending_init_elements (0, braced_init_obstack);
9676 }
9677
9678 /* For two FIELD_DECLs in the same chain, return -1 if field1
9679 comes before field2, 1 if field1 comes after field2 and
9680 0 if field1 == field2. */
9681
9682 static int
9683 init_field_decl_cmp (tree field1, tree field2)
9684 {
9685 if (field1 == field2)
9686 return 0;
9687
9688 tree bitpos1 = bit_position (field1);
9689 tree bitpos2 = bit_position (field2);
9690 if (tree_int_cst_equal (bitpos1, bitpos2))
9691 {
9692 /* If one of the fields has non-zero bitsize, then that
9693 field must be the last one in a sequence of zero
9694 sized fields, fields after it will have bigger
9695 bit_position. */
9696 if (TREE_TYPE (field1) != error_mark_node
9697 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9698 && integer_nonzerop (TREE_TYPE (field1)))
9699 return 1;
9700 if (TREE_TYPE (field2) != error_mark_node
9701 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9702 && integer_nonzerop (TREE_TYPE (field2)))
9703 return -1;
9704 /* Otherwise, fallback to DECL_CHAIN walk to find out
9705 which field comes earlier. Walk chains of both
9706 fields, so that if field1 and field2 are close to each
9707 other in either order, it is found soon even for large
9708 sequences of zero sized fields. */
9709 tree f1 = field1, f2 = field2;
9710 while (1)
9711 {
9712 f1 = DECL_CHAIN (f1);
9713 f2 = DECL_CHAIN (f2);
9714 if (f1 == NULL_TREE)
9715 {
9716 gcc_assert (f2);
9717 return 1;
9718 }
9719 if (f2 == NULL_TREE)
9720 return -1;
9721 if (f1 == field2)
9722 return -1;
9723 if (f2 == field1)
9724 return 1;
9725 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9726 return 1;
9727 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9728 return -1;
9729 }
9730 }
9731 else if (tree_int_cst_lt (bitpos1, bitpos2))
9732 return -1;
9733 else
9734 return 1;
9735 }
9736
9737 /* Output any pending elements which have become next.
9738 As we output elements, constructor_unfilled_{fields,index}
9739 advances, which may cause other elements to become next;
9740 if so, they too are output.
9741
9742 If ALL is 0, we return when there are
9743 no more pending elements to output now.
9744
9745 If ALL is 1, we output space as necessary so that
9746 we can output all the pending elements. */
9747 static void
9748 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9749 {
9750 struct init_node *elt = constructor_pending_elts;
9751 tree next;
9752
9753 retry:
9754
9755 /* Look through the whole pending tree.
9756 If we find an element that should be output now,
9757 output it. Otherwise, set NEXT to the element
9758 that comes first among those still pending. */
9759
9760 next = NULL_TREE;
9761 while (elt)
9762 {
9763 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9764 {
9765 if (tree_int_cst_equal (elt->purpose,
9766 constructor_unfilled_index))
9767 output_init_element (input_location, elt->value, elt->origtype,
9768 true, TREE_TYPE (constructor_type),
9769 constructor_unfilled_index, false, false,
9770 braced_init_obstack);
9771 else if (tree_int_cst_lt (constructor_unfilled_index,
9772 elt->purpose))
9773 {
9774 /* Advance to the next smaller node. */
9775 if (elt->left)
9776 elt = elt->left;
9777 else
9778 {
9779 /* We have reached the smallest node bigger than the
9780 current unfilled index. Fill the space first. */
9781 next = elt->purpose;
9782 break;
9783 }
9784 }
9785 else
9786 {
9787 /* Advance to the next bigger node. */
9788 if (elt->right)
9789 elt = elt->right;
9790 else
9791 {
9792 /* We have reached the biggest node in a subtree. Find
9793 the parent of it, which is the next bigger node. */
9794 while (elt->parent && elt->parent->right == elt)
9795 elt = elt->parent;
9796 elt = elt->parent;
9797 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9798 elt->purpose))
9799 {
9800 next = elt->purpose;
9801 break;
9802 }
9803 }
9804 }
9805 }
9806 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9807 {
9808 /* If the current record is complete we are done. */
9809 if (constructor_unfilled_fields == NULL_TREE)
9810 break;
9811
9812 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9813 elt->purpose);
9814 if (cmp == 0)
9815 output_init_element (input_location, elt->value, elt->origtype,
9816 true, TREE_TYPE (elt->purpose),
9817 elt->purpose, false, false,
9818 braced_init_obstack);
9819 else if (cmp < 0)
9820 {
9821 /* Advance to the next smaller node. */
9822 if (elt->left)
9823 elt = elt->left;
9824 else
9825 {
9826 /* We have reached the smallest node bigger than the
9827 current unfilled field. Fill the space first. */
9828 next = elt->purpose;
9829 break;
9830 }
9831 }
9832 else
9833 {
9834 /* Advance to the next bigger node. */
9835 if (elt->right)
9836 elt = elt->right;
9837 else
9838 {
9839 /* We have reached the biggest node in a subtree. Find
9840 the parent of it, which is the next bigger node. */
9841 while (elt->parent && elt->parent->right == elt)
9842 elt = elt->parent;
9843 elt = elt->parent;
9844 if (elt
9845 && init_field_decl_cmp (constructor_unfilled_fields,
9846 elt->purpose) < 0)
9847 {
9848 next = elt->purpose;
9849 break;
9850 }
9851 }
9852 }
9853 }
9854 }
9855
9856 /* Ordinarily return, but not if we want to output all
9857 and there are elements left. */
9858 if (!(all && next != NULL_TREE))
9859 return;
9860
9861 /* If it's not incremental, just skip over the gap, so that after
9862 jumping to retry we will output the next successive element. */
9863 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9864 constructor_unfilled_fields = next;
9865 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9866 constructor_unfilled_index = next;
9867
9868 /* ELT now points to the node in the pending tree with the next
9869 initializer to output. */
9870 goto retry;
9871 }
9872 \f
9873 /* Add one non-braced element to the current constructor level.
9874 This adjusts the current position within the constructor's type.
9875 This may also start or terminate implicit levels
9876 to handle a partly-braced initializer.
9877
9878 Once this has found the correct level for the new element,
9879 it calls output_init_element.
9880
9881 IMPLICIT is true if value comes from pop_init_level (1),
9882 the new initializer has been merged with the existing one
9883 and thus no warnings should be emitted about overriding an
9884 existing initializer. */
9885
9886 void
9887 process_init_element (location_t loc, struct c_expr value, bool implicit,
9888 struct obstack * braced_init_obstack)
9889 {
9890 tree orig_value = value.value;
9891 int string_flag
9892 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9893 bool strict_string = value.original_code == STRING_CST;
9894 bool was_designated = designator_depth != 0;
9895
9896 designator_depth = 0;
9897 designator_erroneous = 0;
9898
9899 if (!implicit && value.value && !integer_zerop (value.value))
9900 constructor_zeroinit = 0;
9901
9902 /* Handle superfluous braces around string cst as in
9903 char x[] = {"foo"}; */
9904 if (string_flag
9905 && constructor_type
9906 && !was_designated
9907 && TREE_CODE (constructor_type) == ARRAY_TYPE
9908 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9909 && integer_zerop (constructor_unfilled_index))
9910 {
9911 if (constructor_stack->replacement_value.value)
9912 error_init (loc, "excess elements in %<char%> array initializer");
9913 constructor_stack->replacement_value = value;
9914 return;
9915 }
9916
9917 if (constructor_stack->replacement_value.value != NULL_TREE)
9918 {
9919 error_init (loc, "excess elements in struct initializer");
9920 return;
9921 }
9922
9923 /* Ignore elements of a brace group if it is entirely superfluous
9924 and has already been diagnosed. */
9925 if (constructor_type == NULL_TREE)
9926 return;
9927
9928 if (!implicit && warn_designated_init && !was_designated
9929 && TREE_CODE (constructor_type) == RECORD_TYPE
9930 && lookup_attribute ("designated_init",
9931 TYPE_ATTRIBUTES (constructor_type)))
9932 warning_init (loc,
9933 OPT_Wdesignated_init,
9934 "positional initialization of field "
9935 "in %<struct%> declared with %<designated_init%> attribute");
9936
9937 /* If we've exhausted any levels that didn't have braces,
9938 pop them now. */
9939 while (constructor_stack->implicit)
9940 {
9941 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9942 && constructor_fields == NULL_TREE)
9943 process_init_element (loc,
9944 pop_init_level (loc, 1, braced_init_obstack,
9945 last_init_list_comma),
9946 true, braced_init_obstack);
9947 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9948 || VECTOR_TYPE_P (constructor_type))
9949 && constructor_max_index
9950 && tree_int_cst_lt (constructor_max_index,
9951 constructor_index))
9952 process_init_element (loc,
9953 pop_init_level (loc, 1, braced_init_obstack,
9954 last_init_list_comma),
9955 true, braced_init_obstack);
9956 else
9957 break;
9958 }
9959
9960 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9961 if (constructor_range_stack)
9962 {
9963 /* If value is a compound literal and we'll be just using its
9964 content, don't put it into a SAVE_EXPR. */
9965 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9966 || !require_constant_value)
9967 {
9968 tree semantic_type = NULL_TREE;
9969 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9970 {
9971 semantic_type = TREE_TYPE (value.value);
9972 value.value = TREE_OPERAND (value.value, 0);
9973 }
9974 value.value = save_expr (value.value);
9975 if (semantic_type)
9976 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9977 value.value);
9978 }
9979 }
9980
9981 while (1)
9982 {
9983 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9984 {
9985 tree fieldtype;
9986 enum tree_code fieldcode;
9987
9988 if (constructor_fields == NULL_TREE)
9989 {
9990 pedwarn_init (loc, 0, "excess elements in struct initializer");
9991 break;
9992 }
9993
9994 fieldtype = TREE_TYPE (constructor_fields);
9995 if (fieldtype != error_mark_node)
9996 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9997 fieldcode = TREE_CODE (fieldtype);
9998
9999 /* Error for non-static initialization of a flexible array member. */
10000 if (fieldcode == ARRAY_TYPE
10001 && !require_constant_value
10002 && TYPE_SIZE (fieldtype) == NULL_TREE
10003 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10004 {
10005 error_init (loc, "non-static initialization of a flexible "
10006 "array member");
10007 break;
10008 }
10009
10010 /* Error for initialization of a flexible array member with
10011 a string constant if the structure is in an array. E.g.:
10012 struct S { int x; char y[]; };
10013 struct S s[] = { { 1, "foo" } };
10014 is invalid. */
10015 if (string_flag
10016 && fieldcode == ARRAY_TYPE
10017 && constructor_depth > 1
10018 && TYPE_SIZE (fieldtype) == NULL_TREE
10019 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10020 {
10021 bool in_array_p = false;
10022 for (struct constructor_stack *p = constructor_stack;
10023 p && p->type; p = p->next)
10024 if (TREE_CODE (p->type) == ARRAY_TYPE)
10025 {
10026 in_array_p = true;
10027 break;
10028 }
10029 if (in_array_p)
10030 {
10031 error_init (loc, "initialization of flexible array "
10032 "member in a nested context");
10033 break;
10034 }
10035 }
10036
10037 /* Accept a string constant to initialize a subarray. */
10038 if (value.value != NULL_TREE
10039 && fieldcode == ARRAY_TYPE
10040 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10041 && string_flag)
10042 value.value = orig_value;
10043 /* Otherwise, if we have come to a subaggregate,
10044 and we don't have an element of its type, push into it. */
10045 else if (value.value != NULL_TREE
10046 && value.value != error_mark_node
10047 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
10048 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
10049 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
10050 {
10051 push_init_level (loc, 1, braced_init_obstack);
10052 continue;
10053 }
10054
10055 if (value.value)
10056 {
10057 push_member_name (constructor_fields);
10058 output_init_element (loc, value.value, value.original_type,
10059 strict_string, fieldtype,
10060 constructor_fields, true, implicit,
10061 braced_init_obstack);
10062 RESTORE_SPELLING_DEPTH (constructor_depth);
10063 }
10064 else
10065 /* Do the bookkeeping for an element that was
10066 directly output as a constructor. */
10067 {
10068 /* For a record, keep track of end position of last field. */
10069 if (DECL_SIZE (constructor_fields))
10070 constructor_bit_index
10071 = size_binop_loc (input_location, PLUS_EXPR,
10072 bit_position (constructor_fields),
10073 DECL_SIZE (constructor_fields));
10074
10075 /* If the current field was the first one not yet written out,
10076 it isn't now, so update. */
10077 if (constructor_unfilled_fields == constructor_fields)
10078 {
10079 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10080 /* Skip any nameless bit fields. */
10081 while (constructor_unfilled_fields != 0
10082 && (DECL_UNNAMED_BIT_FIELD
10083 (constructor_unfilled_fields)))
10084 constructor_unfilled_fields =
10085 DECL_CHAIN (constructor_unfilled_fields);
10086 }
10087 }
10088
10089 constructor_fields = DECL_CHAIN (constructor_fields);
10090 /* Skip any nameless bit fields at the beginning. */
10091 while (constructor_fields != NULL_TREE
10092 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10093 constructor_fields = DECL_CHAIN (constructor_fields);
10094 }
10095 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10096 {
10097 tree fieldtype;
10098 enum tree_code fieldcode;
10099
10100 if (constructor_fields == NULL_TREE)
10101 {
10102 pedwarn_init (loc, 0,
10103 "excess elements in union initializer");
10104 break;
10105 }
10106
10107 fieldtype = TREE_TYPE (constructor_fields);
10108 if (fieldtype != error_mark_node)
10109 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10110 fieldcode = TREE_CODE (fieldtype);
10111
10112 /* Warn that traditional C rejects initialization of unions.
10113 We skip the warning if the value is zero. This is done
10114 under the assumption that the zero initializer in user
10115 code appears conditioned on e.g. __STDC__ to avoid
10116 "missing initializer" warnings and relies on default
10117 initialization to zero in the traditional C case.
10118 We also skip the warning if the initializer is designated,
10119 again on the assumption that this must be conditional on
10120 __STDC__ anyway (and we've already complained about the
10121 member-designator already). */
10122 if (!in_system_header_at (input_location) && !constructor_designated
10123 && !(value.value && (integer_zerop (value.value)
10124 || real_zerop (value.value))))
10125 warning (OPT_Wtraditional, "traditional C rejects initialization "
10126 "of unions");
10127
10128 /* Accept a string constant to initialize a subarray. */
10129 if (value.value != NULL_TREE
10130 && fieldcode == ARRAY_TYPE
10131 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10132 && string_flag)
10133 value.value = orig_value;
10134 /* Otherwise, if we have come to a subaggregate,
10135 and we don't have an element of its type, push into it. */
10136 else if (value.value != NULL_TREE
10137 && value.value != error_mark_node
10138 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
10139 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
10140 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
10141 {
10142 push_init_level (loc, 1, braced_init_obstack);
10143 continue;
10144 }
10145
10146 if (value.value)
10147 {
10148 push_member_name (constructor_fields);
10149 output_init_element (loc, value.value, value.original_type,
10150 strict_string, fieldtype,
10151 constructor_fields, true, implicit,
10152 braced_init_obstack);
10153 RESTORE_SPELLING_DEPTH (constructor_depth);
10154 }
10155 else
10156 /* Do the bookkeeping for an element that was
10157 directly output as a constructor. */
10158 {
10159 constructor_bit_index = DECL_SIZE (constructor_fields);
10160 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10161 }
10162
10163 constructor_fields = NULL_TREE;
10164 }
10165 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10166 {
10167 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10168 enum tree_code eltcode = TREE_CODE (elttype);
10169
10170 /* Accept a string constant to initialize a subarray. */
10171 if (value.value != NULL_TREE
10172 && eltcode == ARRAY_TYPE
10173 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10174 && string_flag)
10175 value.value = orig_value;
10176 /* Otherwise, if we have come to a subaggregate,
10177 and we don't have an element of its type, push into it. */
10178 else if (value.value != NULL_TREE
10179 && value.value != error_mark_node
10180 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
10181 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
10182 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
10183 {
10184 push_init_level (loc, 1, braced_init_obstack);
10185 continue;
10186 }
10187
10188 if (constructor_max_index != NULL_TREE
10189 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10190 || integer_all_onesp (constructor_max_index)))
10191 {
10192 pedwarn_init (loc, 0,
10193 "excess elements in array initializer");
10194 break;
10195 }
10196
10197 /* Now output the actual element. */
10198 if (value.value)
10199 {
10200 push_array_bounds (tree_to_uhwi (constructor_index));
10201 output_init_element (loc, value.value, value.original_type,
10202 strict_string, elttype,
10203 constructor_index, true, implicit,
10204 braced_init_obstack);
10205 RESTORE_SPELLING_DEPTH (constructor_depth);
10206 }
10207
10208 constructor_index
10209 = size_binop_loc (input_location, PLUS_EXPR,
10210 constructor_index, bitsize_one_node);
10211
10212 if (!value.value)
10213 /* If we are doing the bookkeeping for an element that was
10214 directly output as a constructor, we must update
10215 constructor_unfilled_index. */
10216 constructor_unfilled_index = constructor_index;
10217 }
10218 else if (VECTOR_TYPE_P (constructor_type))
10219 {
10220 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10221
10222 /* Do a basic check of initializer size. Note that vectors
10223 always have a fixed size derived from their type. */
10224 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10225 {
10226 pedwarn_init (loc, 0,
10227 "excess elements in vector initializer");
10228 break;
10229 }
10230
10231 /* Now output the actual element. */
10232 if (value.value)
10233 {
10234 if (TREE_CODE (value.value) == VECTOR_CST)
10235 elttype = TYPE_MAIN_VARIANT (constructor_type);
10236 output_init_element (loc, value.value, value.original_type,
10237 strict_string, elttype,
10238 constructor_index, true, implicit,
10239 braced_init_obstack);
10240 }
10241
10242 constructor_index
10243 = size_binop_loc (input_location,
10244 PLUS_EXPR, constructor_index, bitsize_one_node);
10245
10246 if (!value.value)
10247 /* If we are doing the bookkeeping for an element that was
10248 directly output as a constructor, we must update
10249 constructor_unfilled_index. */
10250 constructor_unfilled_index = constructor_index;
10251 }
10252
10253 /* Handle the sole element allowed in a braced initializer
10254 for a scalar variable. */
10255 else if (constructor_type != error_mark_node
10256 && constructor_fields == NULL_TREE)
10257 {
10258 pedwarn_init (loc, 0,
10259 "excess elements in scalar initializer");
10260 break;
10261 }
10262 else
10263 {
10264 if (value.value)
10265 output_init_element (loc, value.value, value.original_type,
10266 strict_string, constructor_type,
10267 NULL_TREE, true, implicit,
10268 braced_init_obstack);
10269 constructor_fields = NULL_TREE;
10270 }
10271
10272 /* Handle range initializers either at this level or anywhere higher
10273 in the designator stack. */
10274 if (constructor_range_stack)
10275 {
10276 struct constructor_range_stack *p, *range_stack;
10277 int finish = 0;
10278
10279 range_stack = constructor_range_stack;
10280 constructor_range_stack = 0;
10281 while (constructor_stack != range_stack->stack)
10282 {
10283 gcc_assert (constructor_stack->implicit);
10284 process_init_element (loc,
10285 pop_init_level (loc, 1,
10286 braced_init_obstack,
10287 last_init_list_comma),
10288 true, braced_init_obstack);
10289 }
10290 for (p = range_stack;
10291 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10292 p = p->prev)
10293 {
10294 gcc_assert (constructor_stack->implicit);
10295 process_init_element (loc,
10296 pop_init_level (loc, 1,
10297 braced_init_obstack,
10298 last_init_list_comma),
10299 true, braced_init_obstack);
10300 }
10301
10302 p->index = size_binop_loc (input_location,
10303 PLUS_EXPR, p->index, bitsize_one_node);
10304 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10305 finish = 1;
10306
10307 while (1)
10308 {
10309 constructor_index = p->index;
10310 constructor_fields = p->fields;
10311 if (finish && p->range_end && p->index == p->range_start)
10312 {
10313 finish = 0;
10314 p->prev = 0;
10315 }
10316 p = p->next;
10317 if (!p)
10318 break;
10319 finish_implicit_inits (loc, braced_init_obstack);
10320 push_init_level (loc, 2, braced_init_obstack);
10321 p->stack = constructor_stack;
10322 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10323 p->index = p->range_start;
10324 }
10325
10326 if (!finish)
10327 constructor_range_stack = range_stack;
10328 continue;
10329 }
10330
10331 break;
10332 }
10333
10334 constructor_range_stack = 0;
10335 }
10336 \f
10337 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10338 (guaranteed to be 'volatile' or null) and ARGS (represented using
10339 an ASM_EXPR node). */
10340 tree
10341 build_asm_stmt (bool is_volatile, tree args)
10342 {
10343 if (is_volatile)
10344 ASM_VOLATILE_P (args) = 1;
10345 return add_stmt (args);
10346 }
10347
10348 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10349 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10350 SIMPLE indicates whether there was anything at all after the
10351 string in the asm expression -- asm("blah") and asm("blah" : )
10352 are subtly different. We use a ASM_EXPR node to represent this.
10353 LOC is the location of the asm, and IS_INLINE says whether this
10354 is asm inline. */
10355 tree
10356 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10357 tree clobbers, tree labels, bool simple, bool is_inline)
10358 {
10359 tree tail;
10360 tree args;
10361 int i;
10362 const char *constraint;
10363 const char **oconstraints;
10364 bool allows_mem, allows_reg, is_inout;
10365 int ninputs, noutputs;
10366
10367 ninputs = list_length (inputs);
10368 noutputs = list_length (outputs);
10369 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10370
10371 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10372
10373 /* Remove output conversions that change the type but not the mode. */
10374 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10375 {
10376 tree output = TREE_VALUE (tail);
10377
10378 output = c_fully_fold (output, false, NULL, true);
10379
10380 /* ??? Really, this should not be here. Users should be using a
10381 proper lvalue, dammit. But there's a long history of using casts
10382 in the output operands. In cases like longlong.h, this becomes a
10383 primitive form of typechecking -- if the cast can be removed, then
10384 the output operand had a type of the proper width; otherwise we'll
10385 get an error. Gross, but ... */
10386 STRIP_NOPS (output);
10387
10388 if (!lvalue_or_else (loc, output, lv_asm))
10389 output = error_mark_node;
10390
10391 if (output != error_mark_node
10392 && (TREE_READONLY (output)
10393 || TYPE_READONLY (TREE_TYPE (output))
10394 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10395 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10396 readonly_error (loc, output, lv_asm);
10397
10398 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10399 oconstraints[i] = constraint;
10400
10401 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10402 &allows_mem, &allows_reg, &is_inout))
10403 {
10404 /* If the operand is going to end up in memory,
10405 mark it addressable. */
10406 if (!allows_reg && !c_mark_addressable (output))
10407 output = error_mark_node;
10408 if (!(!allows_reg && allows_mem)
10409 && output != error_mark_node
10410 && VOID_TYPE_P (TREE_TYPE (output)))
10411 {
10412 error_at (loc, "invalid use of void expression");
10413 output = error_mark_node;
10414 }
10415 }
10416 else
10417 output = error_mark_node;
10418
10419 TREE_VALUE (tail) = output;
10420 }
10421
10422 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10423 {
10424 tree input;
10425
10426 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10427 input = TREE_VALUE (tail);
10428
10429 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10430 oconstraints, &allows_mem, &allows_reg))
10431 {
10432 /* If the operand is going to end up in memory,
10433 mark it addressable. */
10434 if (!allows_reg && allows_mem)
10435 {
10436 input = c_fully_fold (input, false, NULL, true);
10437
10438 /* Strip the nops as we allow this case. FIXME, this really
10439 should be rejected or made deprecated. */
10440 STRIP_NOPS (input);
10441 if (!c_mark_addressable (input))
10442 input = error_mark_node;
10443 }
10444 else
10445 {
10446 struct c_expr expr;
10447 memset (&expr, 0, sizeof (expr));
10448 expr.value = input;
10449 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10450 input = c_fully_fold (expr.value, false, NULL);
10451
10452 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10453 {
10454 error_at (loc, "invalid use of void expression");
10455 input = error_mark_node;
10456 }
10457 }
10458 }
10459 else
10460 input = error_mark_node;
10461
10462 TREE_VALUE (tail) = input;
10463 }
10464
10465 /* ASMs with labels cannot have outputs. This should have been
10466 enforced by the parser. */
10467 gcc_assert (outputs == NULL || labels == NULL);
10468
10469 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10470
10471 /* asm statements without outputs, including simple ones, are treated
10472 as volatile. */
10473 ASM_INPUT_P (args) = simple;
10474 ASM_VOLATILE_P (args) = (noutputs == 0);
10475 ASM_INLINE_P (args) = is_inline;
10476
10477 return args;
10478 }
10479 \f
10480 /* Generate a goto statement to LABEL. LOC is the location of the
10481 GOTO. */
10482
10483 tree
10484 c_finish_goto_label (location_t loc, tree label)
10485 {
10486 tree decl = lookup_label_for_goto (loc, label);
10487 if (!decl)
10488 return NULL_TREE;
10489 TREE_USED (decl) = 1;
10490 {
10491 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10492 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10493 SET_EXPR_LOCATION (t, loc);
10494 return add_stmt (t);
10495 }
10496 }
10497
10498 /* Generate a computed goto statement to EXPR. LOC is the location of
10499 the GOTO. */
10500
10501 tree
10502 c_finish_goto_ptr (location_t loc, tree expr)
10503 {
10504 tree t;
10505 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10506 expr = c_fully_fold (expr, false, NULL);
10507 expr = convert (ptr_type_node, expr);
10508 t = build1 (GOTO_EXPR, void_type_node, expr);
10509 SET_EXPR_LOCATION (t, loc);
10510 return add_stmt (t);
10511 }
10512
10513 /* Generate a C `return' statement. RETVAL is the expression for what
10514 to return, or a null pointer for `return;' with no value. LOC is
10515 the location of the return statement, or the location of the expression,
10516 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10517 is the original type of RETVAL. */
10518
10519 tree
10520 c_finish_return (location_t loc, tree retval, tree origtype)
10521 {
10522 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10523 bool no_warning = false;
10524 bool npc = false;
10525
10526 /* Use the expansion point to handle cases such as returning NULL
10527 in a function returning void. */
10528 location_t xloc = expansion_point_location_if_in_system_header (loc);
10529
10530 if (TREE_THIS_VOLATILE (current_function_decl))
10531 warning_at (xloc, 0,
10532 "function declared %<noreturn%> has a %<return%> statement");
10533
10534 if (retval)
10535 {
10536 tree semantic_type = NULL_TREE;
10537 npc = null_pointer_constant_p (retval);
10538 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10539 {
10540 semantic_type = TREE_TYPE (retval);
10541 retval = TREE_OPERAND (retval, 0);
10542 }
10543 retval = c_fully_fold (retval, false, NULL);
10544 if (semantic_type)
10545 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10546 }
10547
10548 if (!retval)
10549 {
10550 current_function_returns_null = 1;
10551 if ((warn_return_type >= 0 || flag_isoc99)
10552 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10553 {
10554 bool warned_here;
10555 if (flag_isoc99)
10556 warned_here = pedwarn
10557 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10558 "%<return%> with no value, in function returning non-void");
10559 else
10560 warned_here = warning_at
10561 (loc, OPT_Wreturn_type,
10562 "%<return%> with no value, in function returning non-void");
10563 no_warning = true;
10564 if (warned_here)
10565 inform (DECL_SOURCE_LOCATION (current_function_decl),
10566 "declared here");
10567 }
10568 }
10569 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10570 {
10571 current_function_returns_null = 1;
10572 bool warned_here;
10573 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10574 warned_here = pedwarn
10575 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10576 "%<return%> with a value, in function returning void");
10577 else
10578 warned_here = pedwarn
10579 (xloc, OPT_Wpedantic, "ISO C forbids "
10580 "%<return%> with expression, in function returning void");
10581 if (warned_here)
10582 inform (DECL_SOURCE_LOCATION (current_function_decl),
10583 "declared here");
10584 }
10585 else
10586 {
10587 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10588 retval, origtype, ic_return,
10589 npc, NULL_TREE, NULL_TREE, 0);
10590 tree res = DECL_RESULT (current_function_decl);
10591 tree inner;
10592 bool save;
10593
10594 current_function_returns_value = 1;
10595 if (t == error_mark_node)
10596 return NULL_TREE;
10597
10598 save = in_late_binary_op;
10599 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10600 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10601 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10602 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10603 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10604 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10605 in_late_binary_op = true;
10606 inner = t = convert (TREE_TYPE (res), t);
10607 in_late_binary_op = save;
10608
10609 /* Strip any conversions, additions, and subtractions, and see if
10610 we are returning the address of a local variable. Warn if so. */
10611 while (1)
10612 {
10613 switch (TREE_CODE (inner))
10614 {
10615 CASE_CONVERT:
10616 case NON_LVALUE_EXPR:
10617 case PLUS_EXPR:
10618 case POINTER_PLUS_EXPR:
10619 inner = TREE_OPERAND (inner, 0);
10620 continue;
10621
10622 case MINUS_EXPR:
10623 /* If the second operand of the MINUS_EXPR has a pointer
10624 type (or is converted from it), this may be valid, so
10625 don't give a warning. */
10626 {
10627 tree op1 = TREE_OPERAND (inner, 1);
10628
10629 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10630 && (CONVERT_EXPR_P (op1)
10631 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10632 op1 = TREE_OPERAND (op1, 0);
10633
10634 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10635 break;
10636
10637 inner = TREE_OPERAND (inner, 0);
10638 continue;
10639 }
10640
10641 case ADDR_EXPR:
10642 inner = TREE_OPERAND (inner, 0);
10643
10644 while (REFERENCE_CLASS_P (inner)
10645 && !INDIRECT_REF_P (inner))
10646 inner = TREE_OPERAND (inner, 0);
10647
10648 if (DECL_P (inner)
10649 && !DECL_EXTERNAL (inner)
10650 && !TREE_STATIC (inner)
10651 && DECL_CONTEXT (inner) == current_function_decl
10652 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
10653 {
10654 if (TREE_CODE (inner) == LABEL_DECL)
10655 warning_at (loc, OPT_Wreturn_local_addr,
10656 "function returns address of label");
10657 else
10658 {
10659 warning_at (loc, OPT_Wreturn_local_addr,
10660 "function returns address of local variable");
10661 tree zero = build_zero_cst (TREE_TYPE (res));
10662 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10663 }
10664 }
10665 break;
10666
10667 default:
10668 break;
10669 }
10670
10671 break;
10672 }
10673
10674 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10675 SET_EXPR_LOCATION (retval, loc);
10676
10677 if (warn_sequence_point)
10678 verify_sequence_points (retval);
10679 }
10680
10681 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10682 TREE_NO_WARNING (ret_stmt) |= no_warning;
10683 return add_stmt (ret_stmt);
10684 }
10685 \f
10686 struct c_switch {
10687 /* The SWITCH_EXPR being built. */
10688 tree switch_expr;
10689
10690 /* The original type of the testing expression, i.e. before the
10691 default conversion is applied. */
10692 tree orig_type;
10693
10694 /* A splay-tree mapping the low element of a case range to the high
10695 element, or NULL_TREE if there is no high element. Used to
10696 determine whether or not a new case label duplicates an old case
10697 label. We need a tree, rather than simply a hash table, because
10698 of the GNU case range extension. */
10699 splay_tree cases;
10700
10701 /* The bindings at the point of the switch. This is used for
10702 warnings crossing decls when branching to a case label. */
10703 struct c_spot_bindings *bindings;
10704
10705 /* The next node on the stack. */
10706 struct c_switch *next;
10707
10708 /* Remember whether the controlling expression had boolean type
10709 before integer promotions for the sake of -Wswitch-bool. */
10710 bool bool_cond_p;
10711 };
10712
10713 /* A stack of the currently active switch statements. The innermost
10714 switch statement is on the top of the stack. There is no need to
10715 mark the stack for garbage collection because it is only active
10716 during the processing of the body of a function, and we never
10717 collect at that point. */
10718
10719 struct c_switch *c_switch_stack;
10720
10721 /* Start a C switch statement, testing expression EXP. Return the new
10722 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10723 SWITCH_COND_LOC is the location of the switch's condition.
10724 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10725
10726 tree
10727 c_start_case (location_t switch_loc,
10728 location_t switch_cond_loc,
10729 tree exp, bool explicit_cast_p)
10730 {
10731 tree orig_type = error_mark_node;
10732 bool bool_cond_p = false;
10733 struct c_switch *cs;
10734
10735 if (exp != error_mark_node)
10736 {
10737 orig_type = TREE_TYPE (exp);
10738
10739 if (!INTEGRAL_TYPE_P (orig_type))
10740 {
10741 if (orig_type != error_mark_node)
10742 {
10743 error_at (switch_cond_loc, "switch quantity not an integer");
10744 orig_type = error_mark_node;
10745 }
10746 exp = integer_zero_node;
10747 }
10748 else
10749 {
10750 tree type = TYPE_MAIN_VARIANT (orig_type);
10751 tree e = exp;
10752
10753 /* Warn if the condition has boolean value. */
10754 while (TREE_CODE (e) == COMPOUND_EXPR)
10755 e = TREE_OPERAND (e, 1);
10756
10757 if ((TREE_CODE (type) == BOOLEAN_TYPE
10758 || truth_value_p (TREE_CODE (e)))
10759 /* Explicit cast to int suppresses this warning. */
10760 && !(TREE_CODE (type) == INTEGER_TYPE
10761 && explicit_cast_p))
10762 bool_cond_p = true;
10763
10764 if (!in_system_header_at (input_location)
10765 && (type == long_integer_type_node
10766 || type == long_unsigned_type_node))
10767 warning_at (switch_cond_loc,
10768 OPT_Wtraditional, "%<long%> switch expression not "
10769 "converted to %<int%> in ISO C");
10770
10771 exp = c_fully_fold (exp, false, NULL);
10772 exp = default_conversion (exp);
10773
10774 if (warn_sequence_point)
10775 verify_sequence_points (exp);
10776 }
10777 }
10778
10779 /* Add this new SWITCH_EXPR to the stack. */
10780 cs = XNEW (struct c_switch);
10781 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10782 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10783 cs->orig_type = orig_type;
10784 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10785 cs->bindings = c_get_switch_bindings ();
10786 cs->bool_cond_p = bool_cond_p;
10787 cs->next = c_switch_stack;
10788 c_switch_stack = cs;
10789
10790 return add_stmt (cs->switch_expr);
10791 }
10792
10793 /* Process a case label at location LOC. */
10794
10795 tree
10796 do_case (location_t loc, tree low_value, tree high_value)
10797 {
10798 tree label = NULL_TREE;
10799
10800 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10801 {
10802 low_value = c_fully_fold (low_value, false, NULL);
10803 if (TREE_CODE (low_value) == INTEGER_CST)
10804 pedwarn (loc, OPT_Wpedantic,
10805 "case label is not an integer constant expression");
10806 }
10807
10808 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10809 {
10810 high_value = c_fully_fold (high_value, false, NULL);
10811 if (TREE_CODE (high_value) == INTEGER_CST)
10812 pedwarn (input_location, OPT_Wpedantic,
10813 "case label is not an integer constant expression");
10814 }
10815
10816 if (c_switch_stack == NULL)
10817 {
10818 if (low_value)
10819 error_at (loc, "case label not within a switch statement");
10820 else
10821 error_at (loc, "%<default%> label not within a switch statement");
10822 return NULL_TREE;
10823 }
10824
10825 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10826 EXPR_LOCATION (c_switch_stack->switch_expr),
10827 loc))
10828 return NULL_TREE;
10829
10830 label = c_add_case_label (loc, c_switch_stack->cases,
10831 SWITCH_COND (c_switch_stack->switch_expr),
10832 low_value, high_value);
10833 if (label == error_mark_node)
10834 label = NULL_TREE;
10835 return label;
10836 }
10837
10838 /* Finish the switch statement. TYPE is the original type of the
10839 controlling expression of the switch, or NULL_TREE. */
10840
10841 void
10842 c_finish_case (tree body, tree type)
10843 {
10844 struct c_switch *cs = c_switch_stack;
10845 location_t switch_location;
10846
10847 SWITCH_BODY (cs->switch_expr) = body;
10848
10849 /* Emit warnings as needed. */
10850 switch_location = EXPR_LOCATION (cs->switch_expr);
10851 c_do_switch_warnings (cs->cases, switch_location,
10852 type ? type : TREE_TYPE (cs->switch_expr),
10853 SWITCH_COND (cs->switch_expr), cs->bool_cond_p);
10854 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10855 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10856
10857 /* Pop the stack. */
10858 c_switch_stack = cs->next;
10859 splay_tree_delete (cs->cases);
10860 c_release_switch_bindings (cs->bindings);
10861 XDELETE (cs);
10862 }
10863 \f
10864 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10865 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10866 may be null. */
10867
10868 void
10869 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10870 tree else_block)
10871 {
10872 tree stmt;
10873
10874 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10875 SET_EXPR_LOCATION (stmt, if_locus);
10876 add_stmt (stmt);
10877 }
10878
10879 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10880 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10881 is false for DO loops. INCR is the FOR increment expression. BODY is
10882 the statement controlled by the loop. BLAB is the break label. CLAB is
10883 the continue label. Everything is allowed to be NULL.
10884 COND_LOCUS is the location of the loop condition, INCR_LOCUS is the
10885 location of the FOR increment expression. */
10886
10887 void
10888 c_finish_loop (location_t start_locus, location_t cond_locus, tree cond,
10889 location_t incr_locus, tree incr, tree body, tree blab,
10890 tree clab, bool cond_is_first)
10891 {
10892 tree entry = NULL, exit = NULL, t;
10893
10894 /* If the condition is zero don't generate a loop construct. */
10895 if (cond && integer_zerop (cond))
10896 {
10897 if (cond_is_first)
10898 {
10899 t = build_and_jump (&blab);
10900 SET_EXPR_LOCATION (t, start_locus);
10901 add_stmt (t);
10902 }
10903 }
10904 else
10905 {
10906 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10907
10908 /* If we have an exit condition, then we build an IF with gotos either
10909 out of the loop, or to the top of it. If there's no exit condition,
10910 then we just build a jump back to the top. */
10911 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10912
10913 if (cond && !integer_nonzerop (cond))
10914 {
10915 /* Canonicalize the loop condition to the end. This means
10916 generating a branch to the loop condition. Reuse the
10917 continue label, if possible. */
10918 if (cond_is_first)
10919 {
10920 if (incr || !clab)
10921 {
10922 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10923 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10924 }
10925 else
10926 t = build1 (GOTO_EXPR, void_type_node, clab);
10927 SET_EXPR_LOCATION (t, start_locus);
10928 add_stmt (t);
10929 }
10930
10931 t = build_and_jump (&blab);
10932 exit = fold_build3_loc (cond_is_first ? start_locus : input_location,
10933 COND_EXPR, void_type_node, cond, exit, t);
10934 }
10935 else
10936 {
10937 /* For the backward-goto's location of an unconditional loop
10938 use the beginning of the body, or, if there is none, the
10939 top of the loop. */
10940 location_t loc = EXPR_LOCATION (expr_first (body));
10941 if (loc == UNKNOWN_LOCATION)
10942 loc = start_locus;
10943 SET_EXPR_LOCATION (exit, loc);
10944 }
10945
10946 add_stmt (top);
10947 }
10948
10949 if (body)
10950 add_stmt (body);
10951 if (clab)
10952 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10953 if (incr)
10954 {
10955 if (MAY_HAVE_DEBUG_MARKER_STMTS && incr_locus != UNKNOWN_LOCATION)
10956 {
10957 t = build0 (DEBUG_BEGIN_STMT, void_type_node);
10958 SET_EXPR_LOCATION (t, incr_locus);
10959 add_stmt (t);
10960 }
10961 add_stmt (incr);
10962 }
10963 if (entry)
10964 add_stmt (entry);
10965 if (MAY_HAVE_DEBUG_MARKER_STMTS && cond_locus != UNKNOWN_LOCATION)
10966 {
10967 t = build0 (DEBUG_BEGIN_STMT, void_type_node);
10968 SET_EXPR_LOCATION (t, cond_locus);
10969 add_stmt (t);
10970 }
10971 if (exit)
10972 add_stmt (exit);
10973 if (blab)
10974 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10975 }
10976
10977 tree
10978 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10979 {
10980 bool skip;
10981 tree label = *label_p;
10982
10983 /* In switch statements break is sometimes stylistically used after
10984 a return statement. This can lead to spurious warnings about
10985 control reaching the end of a non-void function when it is
10986 inlined. Note that we are calling block_may_fallthru with
10987 language specific tree nodes; this works because
10988 block_may_fallthru returns true when given something it does not
10989 understand. */
10990 skip = !block_may_fallthru (cur_stmt_list);
10991
10992 if (!label)
10993 {
10994 if (!skip)
10995 *label_p = label = create_artificial_label (loc);
10996 }
10997 else if (TREE_CODE (label) == LABEL_DECL)
10998 ;
10999 else switch (TREE_INT_CST_LOW (label))
11000 {
11001 case 0:
11002 if (is_break)
11003 error_at (loc, "break statement not within loop or switch");
11004 else
11005 error_at (loc, "continue statement not within a loop");
11006 return NULL_TREE;
11007
11008 case 1:
11009 gcc_assert (is_break);
11010 error_at (loc, "break statement used with OpenMP for loop");
11011 return NULL_TREE;
11012
11013 case 2:
11014 if (is_break)
11015 error ("break statement within %<#pragma simd%> loop body");
11016 else
11017 error ("continue statement within %<#pragma simd%> loop body");
11018 return NULL_TREE;
11019
11020 default:
11021 gcc_unreachable ();
11022 }
11023
11024 if (skip)
11025 return NULL_TREE;
11026
11027 if (!is_break)
11028 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
11029
11030 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
11031 }
11032
11033 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11034
11035 static void
11036 emit_side_effect_warnings (location_t loc, tree expr)
11037 {
11038 if (expr == error_mark_node)
11039 ;
11040 else if (!TREE_SIDE_EFFECTS (expr))
11041 {
11042 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
11043 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11044 }
11045 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11046 {
11047 tree r = expr;
11048 location_t cloc = loc;
11049 while (TREE_CODE (r) == COMPOUND_EXPR)
11050 {
11051 if (EXPR_HAS_LOCATION (r))
11052 cloc = EXPR_LOCATION (r);
11053 r = TREE_OPERAND (r, 1);
11054 }
11055 if (!TREE_SIDE_EFFECTS (r)
11056 && !VOID_TYPE_P (TREE_TYPE (r))
11057 && !CONVERT_EXPR_P (r)
11058 && !TREE_NO_WARNING (r)
11059 && !TREE_NO_WARNING (expr))
11060 warning_at (cloc, OPT_Wunused_value,
11061 "right-hand operand of comma expression has no effect");
11062 }
11063 else
11064 warn_if_unused_value (expr, loc);
11065 }
11066
11067 /* Process an expression as if it were a complete statement. Emit
11068 diagnostics, but do not call ADD_STMT. LOC is the location of the
11069 statement. */
11070
11071 tree
11072 c_process_expr_stmt (location_t loc, tree expr)
11073 {
11074 tree exprv;
11075
11076 if (!expr)
11077 return NULL_TREE;
11078
11079 expr = c_fully_fold (expr, false, NULL);
11080
11081 if (warn_sequence_point)
11082 verify_sequence_points (expr);
11083
11084 if (TREE_TYPE (expr) != error_mark_node
11085 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11086 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11087 error_at (loc, "expression statement has incomplete type");
11088
11089 /* If we're not processing a statement expression, warn about unused values.
11090 Warnings for statement expressions will be emitted later, once we figure
11091 out which is the result. */
11092 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11093 && warn_unused_value)
11094 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11095
11096 exprv = expr;
11097 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11098 exprv = TREE_OPERAND (exprv, 1);
11099 while (CONVERT_EXPR_P (exprv))
11100 exprv = TREE_OPERAND (exprv, 0);
11101 if (DECL_P (exprv)
11102 || handled_component_p (exprv)
11103 || TREE_CODE (exprv) == ADDR_EXPR)
11104 mark_exp_read (exprv);
11105
11106 /* If the expression is not of a type to which we cannot assign a line
11107 number, wrap the thing in a no-op NOP_EXPR. */
11108 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11109 {
11110 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11111 SET_EXPR_LOCATION (expr, loc);
11112 }
11113
11114 return expr;
11115 }
11116
11117 /* Emit an expression as a statement. LOC is the location of the
11118 expression. */
11119
11120 tree
11121 c_finish_expr_stmt (location_t loc, tree expr)
11122 {
11123 if (expr)
11124 return add_stmt (c_process_expr_stmt (loc, expr));
11125 else
11126 return NULL;
11127 }
11128
11129 /* Do the opposite and emit a statement as an expression. To begin,
11130 create a new binding level and return it. */
11131
11132 tree
11133 c_begin_stmt_expr (void)
11134 {
11135 tree ret;
11136
11137 /* We must force a BLOCK for this level so that, if it is not expanded
11138 later, there is a way to turn off the entire subtree of blocks that
11139 are contained in it. */
11140 keep_next_level ();
11141 ret = c_begin_compound_stmt (true);
11142
11143 c_bindings_start_stmt_expr (c_switch_stack == NULL
11144 ? NULL
11145 : c_switch_stack->bindings);
11146
11147 /* Mark the current statement list as belonging to a statement list. */
11148 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11149
11150 return ret;
11151 }
11152
11153 /* LOC is the location of the compound statement to which this body
11154 belongs. */
11155
11156 tree
11157 c_finish_stmt_expr (location_t loc, tree body)
11158 {
11159 tree last, type, tmp, val;
11160 tree *last_p;
11161
11162 body = c_end_compound_stmt (loc, body, true);
11163
11164 c_bindings_end_stmt_expr (c_switch_stack == NULL
11165 ? NULL
11166 : c_switch_stack->bindings);
11167
11168 /* Locate the last statement in BODY. See c_end_compound_stmt
11169 about always returning a BIND_EXPR. */
11170 last_p = &BIND_EXPR_BODY (body);
11171 last = BIND_EXPR_BODY (body);
11172
11173 continue_searching:
11174 if (TREE_CODE (last) == STATEMENT_LIST)
11175 {
11176 tree_stmt_iterator l = tsi_last (last);
11177
11178 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11179 tsi_prev (&l);
11180
11181 /* This can happen with degenerate cases like ({ }). No value. */
11182 if (tsi_end_p (l))
11183 return body;
11184
11185 /* If we're supposed to generate side effects warnings, process
11186 all of the statements except the last. */
11187 if (warn_unused_value)
11188 {
11189 for (tree_stmt_iterator i = tsi_start (last);
11190 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11191 {
11192 location_t tloc;
11193 tree t = tsi_stmt (i);
11194
11195 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11196 emit_side_effect_warnings (tloc, t);
11197 }
11198 }
11199 last_p = tsi_stmt_ptr (l);
11200 last = *last_p;
11201 }
11202
11203 /* If the end of the list is exception related, then the list was split
11204 by a call to push_cleanup. Continue searching. */
11205 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11206 || TREE_CODE (last) == TRY_CATCH_EXPR)
11207 {
11208 last_p = &TREE_OPERAND (last, 0);
11209 last = *last_p;
11210 goto continue_searching;
11211 }
11212
11213 if (last == error_mark_node)
11214 return last;
11215
11216 /* In the case that the BIND_EXPR is not necessary, return the
11217 expression out from inside it. */
11218 if ((last == BIND_EXPR_BODY (body)
11219 /* Skip nested debug stmts. */
11220 || last == expr_first (BIND_EXPR_BODY (body)))
11221 && BIND_EXPR_VARS (body) == NULL)
11222 {
11223 /* Even if this looks constant, do not allow it in a constant
11224 expression. */
11225 last = c_wrap_maybe_const (last, true);
11226 /* Do not warn if the return value of a statement expression is
11227 unused. */
11228 TREE_NO_WARNING (last) = 1;
11229 return last;
11230 }
11231
11232 /* Extract the type of said expression. */
11233 type = TREE_TYPE (last);
11234
11235 /* If we're not returning a value at all, then the BIND_EXPR that
11236 we already have is a fine expression to return. */
11237 if (!type || VOID_TYPE_P (type))
11238 return body;
11239
11240 /* Now that we've located the expression containing the value, it seems
11241 silly to make voidify_wrapper_expr repeat the process. Create a
11242 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11243 tmp = create_tmp_var_raw (type);
11244
11245 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11246 tree_expr_nonnegative_p giving up immediately. */
11247 val = last;
11248 if (TREE_CODE (val) == NOP_EXPR
11249 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11250 val = TREE_OPERAND (val, 0);
11251
11252 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11253 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11254
11255 {
11256 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11257 SET_EXPR_LOCATION (t, loc);
11258 return t;
11259 }
11260 }
11261 \f
11262 /* Begin and end compound statements. This is as simple as pushing
11263 and popping new statement lists from the tree. */
11264
11265 tree
11266 c_begin_compound_stmt (bool do_scope)
11267 {
11268 tree stmt = push_stmt_list ();
11269 if (do_scope)
11270 push_scope ();
11271 return stmt;
11272 }
11273
11274 /* End a compound statement. STMT is the statement. LOC is the
11275 location of the compound statement-- this is usually the location
11276 of the opening brace. */
11277
11278 tree
11279 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11280 {
11281 tree block = NULL;
11282
11283 if (do_scope)
11284 {
11285 if (c_dialect_objc ())
11286 objc_clear_super_receiver ();
11287 block = pop_scope ();
11288 }
11289
11290 stmt = pop_stmt_list (stmt);
11291 stmt = c_build_bind_expr (loc, block, stmt);
11292
11293 /* If this compound statement is nested immediately inside a statement
11294 expression, then force a BIND_EXPR to be created. Otherwise we'll
11295 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11296 STATEMENT_LISTs merge, and thus we can lose track of what statement
11297 was really last. */
11298 if (building_stmt_list_p ()
11299 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11300 && TREE_CODE (stmt) != BIND_EXPR)
11301 {
11302 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11303 TREE_SIDE_EFFECTS (stmt) = 1;
11304 SET_EXPR_LOCATION (stmt, loc);
11305 }
11306
11307 return stmt;
11308 }
11309
11310 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11311 when the current scope is exited. EH_ONLY is true when this is not
11312 meant to apply to normal control flow transfer. */
11313
11314 void
11315 push_cleanup (tree decl, tree cleanup, bool eh_only)
11316 {
11317 enum tree_code code;
11318 tree stmt, list;
11319 bool stmt_expr;
11320
11321 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11322 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11323 add_stmt (stmt);
11324 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11325 list = push_stmt_list ();
11326 TREE_OPERAND (stmt, 0) = list;
11327 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11328 }
11329 \f
11330 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11331 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11332
11333 static tree
11334 build_vec_cmp (tree_code code, tree type,
11335 tree arg0, tree arg1)
11336 {
11337 tree zero_vec = build_zero_cst (type);
11338 tree minus_one_vec = build_minus_one_cst (type);
11339 tree cmp_type = build_same_sized_truth_vector_type (type);
11340 tree cmp = build2 (code, cmp_type, arg0, arg1);
11341 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11342 }
11343
11344 /* Build a binary-operation expression without default conversions.
11345 CODE is the kind of expression to build.
11346 LOCATION is the operator's location.
11347 This function differs from `build' in several ways:
11348 the data type of the result is computed and recorded in it,
11349 warnings are generated if arg data types are invalid,
11350 special handling for addition and subtraction of pointers is known,
11351 and some optimization is done (operations on narrow ints
11352 are done in the narrower type when that gives the same result).
11353 Constant folding is also done before the result is returned.
11354
11355 Note that the operands will never have enumeral types, or function
11356 or array types, because either they will have the default conversions
11357 performed or they have both just been converted to some other type in which
11358 the arithmetic is to be done. */
11359
11360 tree
11361 build_binary_op (location_t location, enum tree_code code,
11362 tree orig_op0, tree orig_op1, bool convert_p)
11363 {
11364 tree type0, type1, orig_type0, orig_type1;
11365 tree eptype;
11366 enum tree_code code0, code1;
11367 tree op0, op1;
11368 tree ret = error_mark_node;
11369 const char *invalid_op_diag;
11370 bool op0_int_operands, op1_int_operands;
11371 bool int_const, int_const_or_overflow, int_operands;
11372
11373 /* Expression code to give to the expression when it is built.
11374 Normally this is CODE, which is what the caller asked for,
11375 but in some special cases we change it. */
11376 enum tree_code resultcode = code;
11377
11378 /* Data type in which the computation is to be performed.
11379 In the simplest cases this is the common type of the arguments. */
11380 tree result_type = NULL;
11381
11382 /* When the computation is in excess precision, the type of the
11383 final EXCESS_PRECISION_EXPR. */
11384 tree semantic_result_type = NULL;
11385
11386 /* Nonzero means operands have already been type-converted
11387 in whatever way is necessary.
11388 Zero means they need to be converted to RESULT_TYPE. */
11389 int converted = 0;
11390
11391 /* Nonzero means create the expression with this type, rather than
11392 RESULT_TYPE. */
11393 tree build_type = NULL_TREE;
11394
11395 /* Nonzero means after finally constructing the expression
11396 convert it to this type. */
11397 tree final_type = NULL_TREE;
11398
11399 /* Nonzero if this is an operation like MIN or MAX which can
11400 safely be computed in short if both args are promoted shorts.
11401 Also implies COMMON.
11402 -1 indicates a bitwise operation; this makes a difference
11403 in the exact conditions for when it is safe to do the operation
11404 in a narrower mode. */
11405 int shorten = 0;
11406
11407 /* Nonzero if this is a comparison operation;
11408 if both args are promoted shorts, compare the original shorts.
11409 Also implies COMMON. */
11410 int short_compare = 0;
11411
11412 /* Nonzero if this is a right-shift operation, which can be computed on the
11413 original short and then promoted if the operand is a promoted short. */
11414 int short_shift = 0;
11415
11416 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11417 int common = 0;
11418
11419 /* True means types are compatible as far as ObjC is concerned. */
11420 bool objc_ok;
11421
11422 /* True means this is an arithmetic operation that may need excess
11423 precision. */
11424 bool may_need_excess_precision;
11425
11426 /* True means this is a boolean operation that converts both its
11427 operands to truth-values. */
11428 bool boolean_op = false;
11429
11430 /* Remember whether we're doing / or %. */
11431 bool doing_div_or_mod = false;
11432
11433 /* Remember whether we're doing << or >>. */
11434 bool doing_shift = false;
11435
11436 /* Tree holding instrumentation expression. */
11437 tree instrument_expr = NULL;
11438
11439 if (location == UNKNOWN_LOCATION)
11440 location = input_location;
11441
11442 op0 = orig_op0;
11443 op1 = orig_op1;
11444
11445 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11446 if (op0_int_operands)
11447 op0 = remove_c_maybe_const_expr (op0);
11448 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11449 if (op1_int_operands)
11450 op1 = remove_c_maybe_const_expr (op1);
11451 int_operands = (op0_int_operands && op1_int_operands);
11452 if (int_operands)
11453 {
11454 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11455 && TREE_CODE (orig_op1) == INTEGER_CST);
11456 int_const = (int_const_or_overflow
11457 && !TREE_OVERFLOW (orig_op0)
11458 && !TREE_OVERFLOW (orig_op1));
11459 }
11460 else
11461 int_const = int_const_or_overflow = false;
11462
11463 /* Do not apply default conversion in mixed vector/scalar expression. */
11464 if (convert_p
11465 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11466 {
11467 op0 = default_conversion (op0);
11468 op1 = default_conversion (op1);
11469 }
11470
11471 orig_type0 = type0 = TREE_TYPE (op0);
11472
11473 orig_type1 = type1 = TREE_TYPE (op1);
11474
11475 /* The expression codes of the data types of the arguments tell us
11476 whether the arguments are integers, floating, pointers, etc. */
11477 code0 = TREE_CODE (type0);
11478 code1 = TREE_CODE (type1);
11479
11480 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11481 STRIP_TYPE_NOPS (op0);
11482 STRIP_TYPE_NOPS (op1);
11483
11484 /* If an error was already reported for one of the arguments,
11485 avoid reporting another error. */
11486
11487 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11488 return error_mark_node;
11489
11490 if (code0 == POINTER_TYPE
11491 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11492 return error_mark_node;
11493
11494 if (code1 == POINTER_TYPE
11495 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11496 return error_mark_node;
11497
11498 if ((invalid_op_diag
11499 = targetm.invalid_binary_op (code, type0, type1)))
11500 {
11501 error_at (location, invalid_op_diag);
11502 return error_mark_node;
11503 }
11504
11505 switch (code)
11506 {
11507 case PLUS_EXPR:
11508 case MINUS_EXPR:
11509 case MULT_EXPR:
11510 case TRUNC_DIV_EXPR:
11511 case CEIL_DIV_EXPR:
11512 case FLOOR_DIV_EXPR:
11513 case ROUND_DIV_EXPR:
11514 case EXACT_DIV_EXPR:
11515 may_need_excess_precision = true;
11516 break;
11517
11518 case EQ_EXPR:
11519 case NE_EXPR:
11520 case LE_EXPR:
11521 case GE_EXPR:
11522 case LT_EXPR:
11523 case GT_EXPR:
11524 /* Excess precision for implicit conversions of integers to
11525 floating point in C11 and later. */
11526 may_need_excess_precision = (flag_isoc11
11527 && (ANY_INTEGRAL_TYPE_P (type0)
11528 || ANY_INTEGRAL_TYPE_P (type1)));
11529 break;
11530
11531 default:
11532 may_need_excess_precision = false;
11533 break;
11534 }
11535 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11536 {
11537 op0 = TREE_OPERAND (op0, 0);
11538 type0 = TREE_TYPE (op0);
11539 }
11540 else if (may_need_excess_precision
11541 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11542 {
11543 type0 = eptype;
11544 op0 = convert (eptype, op0);
11545 }
11546 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11547 {
11548 op1 = TREE_OPERAND (op1, 0);
11549 type1 = TREE_TYPE (op1);
11550 }
11551 else if (may_need_excess_precision
11552 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11553 {
11554 type1 = eptype;
11555 op1 = convert (eptype, op1);
11556 }
11557
11558 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11559
11560 /* In case when one of the operands of the binary operation is
11561 a vector and another is a scalar -- convert scalar to vector. */
11562 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11563 {
11564 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11565 true);
11566
11567 switch (convert_flag)
11568 {
11569 case stv_error:
11570 return error_mark_node;
11571 case stv_firstarg:
11572 {
11573 bool maybe_const = true;
11574 tree sc;
11575 sc = c_fully_fold (op0, false, &maybe_const);
11576 sc = save_expr (sc);
11577 sc = convert (TREE_TYPE (type1), sc);
11578 op0 = build_vector_from_val (type1, sc);
11579 if (!maybe_const)
11580 op0 = c_wrap_maybe_const (op0, true);
11581 orig_type0 = type0 = TREE_TYPE (op0);
11582 code0 = TREE_CODE (type0);
11583 converted = 1;
11584 break;
11585 }
11586 case stv_secondarg:
11587 {
11588 bool maybe_const = true;
11589 tree sc;
11590 sc = c_fully_fold (op1, false, &maybe_const);
11591 sc = save_expr (sc);
11592 sc = convert (TREE_TYPE (type0), sc);
11593 op1 = build_vector_from_val (type0, sc);
11594 if (!maybe_const)
11595 op1 = c_wrap_maybe_const (op1, true);
11596 orig_type1 = type1 = TREE_TYPE (op1);
11597 code1 = TREE_CODE (type1);
11598 converted = 1;
11599 break;
11600 }
11601 default:
11602 break;
11603 }
11604 }
11605
11606 switch (code)
11607 {
11608 case PLUS_EXPR:
11609 /* Handle the pointer + int case. */
11610 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11611 {
11612 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11613 goto return_build_binary_op;
11614 }
11615 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11616 {
11617 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11618 goto return_build_binary_op;
11619 }
11620 else
11621 common = 1;
11622 break;
11623
11624 case MINUS_EXPR:
11625 /* Subtraction of two similar pointers.
11626 We must subtract them as integers, then divide by object size. */
11627 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11628 && comp_target_types (location, type0, type1))
11629 {
11630 ret = pointer_diff (location, op0, op1, &instrument_expr);
11631 goto return_build_binary_op;
11632 }
11633 /* Handle pointer minus int. Just like pointer plus int. */
11634 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11635 {
11636 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11637 goto return_build_binary_op;
11638 }
11639 else
11640 common = 1;
11641 break;
11642
11643 case MULT_EXPR:
11644 common = 1;
11645 break;
11646
11647 case TRUNC_DIV_EXPR:
11648 case CEIL_DIV_EXPR:
11649 case FLOOR_DIV_EXPR:
11650 case ROUND_DIV_EXPR:
11651 case EXACT_DIV_EXPR:
11652 doing_div_or_mod = true;
11653 warn_for_div_by_zero (location, op1);
11654
11655 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11656 || code0 == FIXED_POINT_TYPE
11657 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11658 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11659 || code1 == FIXED_POINT_TYPE
11660 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11661 {
11662 enum tree_code tcode0 = code0, tcode1 = code1;
11663
11664 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11665 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11666 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11667 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11668
11669 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11670 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11671 resultcode = RDIV_EXPR;
11672 else
11673 /* Although it would be tempting to shorten always here, that
11674 loses on some targets, since the modulo instruction is
11675 undefined if the quotient can't be represented in the
11676 computation mode. We shorten only if unsigned or if
11677 dividing by something we know != -1. */
11678 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11679 || (TREE_CODE (op1) == INTEGER_CST
11680 && !integer_all_onesp (op1)));
11681 common = 1;
11682 }
11683 break;
11684
11685 case BIT_AND_EXPR:
11686 case BIT_IOR_EXPR:
11687 case BIT_XOR_EXPR:
11688 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11689 shorten = -1;
11690 /* Allow vector types which are not floating point types. */
11691 else if (code0 == VECTOR_TYPE
11692 && code1 == VECTOR_TYPE
11693 && !VECTOR_FLOAT_TYPE_P (type0)
11694 && !VECTOR_FLOAT_TYPE_P (type1))
11695 common = 1;
11696 break;
11697
11698 case TRUNC_MOD_EXPR:
11699 case FLOOR_MOD_EXPR:
11700 doing_div_or_mod = true;
11701 warn_for_div_by_zero (location, op1);
11702
11703 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11704 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11705 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11706 common = 1;
11707 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11708 {
11709 /* Although it would be tempting to shorten always here, that loses
11710 on some targets, since the modulo instruction is undefined if the
11711 quotient can't be represented in the computation mode. We shorten
11712 only if unsigned or if dividing by something we know != -1. */
11713 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11714 || (TREE_CODE (op1) == INTEGER_CST
11715 && !integer_all_onesp (op1)));
11716 common = 1;
11717 }
11718 break;
11719
11720 case TRUTH_ANDIF_EXPR:
11721 case TRUTH_ORIF_EXPR:
11722 case TRUTH_AND_EXPR:
11723 case TRUTH_OR_EXPR:
11724 case TRUTH_XOR_EXPR:
11725 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11726 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11727 || code0 == FIXED_POINT_TYPE)
11728 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11729 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11730 || code1 == FIXED_POINT_TYPE))
11731 {
11732 /* Result of these operations is always an int,
11733 but that does not mean the operands should be
11734 converted to ints! */
11735 result_type = integer_type_node;
11736 if (op0_int_operands)
11737 {
11738 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11739 op0 = remove_c_maybe_const_expr (op0);
11740 }
11741 else
11742 op0 = c_objc_common_truthvalue_conversion (location, op0);
11743 if (op1_int_operands)
11744 {
11745 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11746 op1 = remove_c_maybe_const_expr (op1);
11747 }
11748 else
11749 op1 = c_objc_common_truthvalue_conversion (location, op1);
11750 converted = 1;
11751 boolean_op = true;
11752 }
11753 if (code == TRUTH_ANDIF_EXPR)
11754 {
11755 int_const_or_overflow = (int_operands
11756 && TREE_CODE (orig_op0) == INTEGER_CST
11757 && (op0 == truthvalue_false_node
11758 || TREE_CODE (orig_op1) == INTEGER_CST));
11759 int_const = (int_const_or_overflow
11760 && !TREE_OVERFLOW (orig_op0)
11761 && (op0 == truthvalue_false_node
11762 || !TREE_OVERFLOW (orig_op1)));
11763 }
11764 else if (code == TRUTH_ORIF_EXPR)
11765 {
11766 int_const_or_overflow = (int_operands
11767 && TREE_CODE (orig_op0) == INTEGER_CST
11768 && (op0 == truthvalue_true_node
11769 || TREE_CODE (orig_op1) == INTEGER_CST));
11770 int_const = (int_const_or_overflow
11771 && !TREE_OVERFLOW (orig_op0)
11772 && (op0 == truthvalue_true_node
11773 || !TREE_OVERFLOW (orig_op1)));
11774 }
11775 break;
11776
11777 /* Shift operations: result has same type as first operand;
11778 always convert second operand to int.
11779 Also set SHORT_SHIFT if shifting rightward. */
11780
11781 case RSHIFT_EXPR:
11782 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11783 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11784 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11785 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11786 TYPE_VECTOR_SUBPARTS (type1)))
11787 {
11788 result_type = type0;
11789 converted = 1;
11790 }
11791 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11792 || (code0 == VECTOR_TYPE
11793 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11794 && code1 == INTEGER_TYPE)
11795 {
11796 doing_shift = true;
11797 if (TREE_CODE (op1) == INTEGER_CST)
11798 {
11799 if (tree_int_cst_sgn (op1) < 0)
11800 {
11801 int_const = false;
11802 if (c_inhibit_evaluation_warnings == 0)
11803 warning_at (location, OPT_Wshift_count_negative,
11804 "right shift count is negative");
11805 }
11806 else if (code0 == VECTOR_TYPE)
11807 {
11808 if (compare_tree_int (op1,
11809 TYPE_PRECISION (TREE_TYPE (type0)))
11810 >= 0)
11811 {
11812 int_const = false;
11813 if (c_inhibit_evaluation_warnings == 0)
11814 warning_at (location, OPT_Wshift_count_overflow,
11815 "right shift count >= width of vector element");
11816 }
11817 }
11818 else
11819 {
11820 if (!integer_zerop (op1))
11821 short_shift = 1;
11822
11823 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11824 {
11825 int_const = false;
11826 if (c_inhibit_evaluation_warnings == 0)
11827 warning_at (location, OPT_Wshift_count_overflow,
11828 "right shift count >= width of type");
11829 }
11830 }
11831 }
11832
11833 /* Use the type of the value to be shifted. */
11834 result_type = type0;
11835 /* Avoid converting op1 to result_type later. */
11836 converted = 1;
11837 }
11838 break;
11839
11840 case LSHIFT_EXPR:
11841 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11842 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11843 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11844 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11845 TYPE_VECTOR_SUBPARTS (type1)))
11846 {
11847 result_type = type0;
11848 converted = 1;
11849 }
11850 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11851 || (code0 == VECTOR_TYPE
11852 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11853 && code1 == INTEGER_TYPE)
11854 {
11855 doing_shift = true;
11856 if (TREE_CODE (op0) == INTEGER_CST
11857 && tree_int_cst_sgn (op0) < 0)
11858 {
11859 /* Don't reject a left shift of a negative value in a context
11860 where a constant expression is needed in C90. */
11861 if (flag_isoc99)
11862 int_const = false;
11863 if (c_inhibit_evaluation_warnings == 0)
11864 warning_at (location, OPT_Wshift_negative_value,
11865 "left shift of negative value");
11866 }
11867 if (TREE_CODE (op1) == INTEGER_CST)
11868 {
11869 if (tree_int_cst_sgn (op1) < 0)
11870 {
11871 int_const = false;
11872 if (c_inhibit_evaluation_warnings == 0)
11873 warning_at (location, OPT_Wshift_count_negative,
11874 "left shift count is negative");
11875 }
11876 else if (code0 == VECTOR_TYPE)
11877 {
11878 if (compare_tree_int (op1,
11879 TYPE_PRECISION (TREE_TYPE (type0)))
11880 >= 0)
11881 {
11882 int_const = false;
11883 if (c_inhibit_evaluation_warnings == 0)
11884 warning_at (location, OPT_Wshift_count_overflow,
11885 "left shift count >= width of vector element");
11886 }
11887 }
11888 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11889 {
11890 int_const = false;
11891 if (c_inhibit_evaluation_warnings == 0)
11892 warning_at (location, OPT_Wshift_count_overflow,
11893 "left shift count >= width of type");
11894 }
11895 else if (TREE_CODE (op0) == INTEGER_CST
11896 && maybe_warn_shift_overflow (location, op0, op1)
11897 && flag_isoc99)
11898 int_const = false;
11899 }
11900
11901 /* Use the type of the value to be shifted. */
11902 result_type = type0;
11903 /* Avoid converting op1 to result_type later. */
11904 converted = 1;
11905 }
11906 break;
11907
11908 case EQ_EXPR:
11909 case NE_EXPR:
11910 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11911 {
11912 tree intt;
11913 if (!vector_types_compatible_elements_p (type0, type1))
11914 {
11915 error_at (location, "comparing vectors with different "
11916 "element types");
11917 return error_mark_node;
11918 }
11919
11920 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11921 TYPE_VECTOR_SUBPARTS (type1)))
11922 {
11923 error_at (location, "comparing vectors with different "
11924 "number of elements");
11925 return error_mark_node;
11926 }
11927
11928 /* It's not precisely specified how the usual arithmetic
11929 conversions apply to the vector types. Here, we use
11930 the unsigned type if one of the operands is signed and
11931 the other one is unsigned. */
11932 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11933 {
11934 if (!TYPE_UNSIGNED (type0))
11935 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11936 else
11937 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11938 warning_at (location, OPT_Wsign_compare, "comparison between "
11939 "types %qT and %qT", type0, type1);
11940 }
11941
11942 /* Always construct signed integer vector type. */
11943 intt = c_common_type_for_size (GET_MODE_BITSIZE
11944 (SCALAR_TYPE_MODE
11945 (TREE_TYPE (type0))), 0);
11946 if (!intt)
11947 {
11948 error_at (location, "could not find an integer type "
11949 "of the same size as %qT",
11950 TREE_TYPE (type0));
11951 return error_mark_node;
11952 }
11953 result_type = build_opaque_vector_type (intt,
11954 TYPE_VECTOR_SUBPARTS (type0));
11955 converted = 1;
11956 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11957 goto return_build_binary_op;
11958 }
11959 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11960 warning_at (location,
11961 OPT_Wfloat_equal,
11962 "comparing floating-point with %<==%> or %<!=%> is unsafe");
11963 /* Result of comparison is always int,
11964 but don't convert the args to int! */
11965 build_type = integer_type_node;
11966 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11967 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11968 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11969 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11970 short_compare = 1;
11971 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11972 {
11973 if (TREE_CODE (op0) == ADDR_EXPR
11974 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11975 && !from_macro_expansion_at (location))
11976 {
11977 if (code == EQ_EXPR)
11978 warning_at (location,
11979 OPT_Waddress,
11980 "the comparison will always evaluate as %<false%> "
11981 "for the address of %qD will never be NULL",
11982 TREE_OPERAND (op0, 0));
11983 else
11984 warning_at (location,
11985 OPT_Waddress,
11986 "the comparison will always evaluate as %<true%> "
11987 "for the address of %qD will never be NULL",
11988 TREE_OPERAND (op0, 0));
11989 }
11990 result_type = type0;
11991 }
11992 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11993 {
11994 if (TREE_CODE (op1) == ADDR_EXPR
11995 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11996 && !from_macro_expansion_at (location))
11997 {
11998 if (code == EQ_EXPR)
11999 warning_at (location,
12000 OPT_Waddress,
12001 "the comparison will always evaluate as %<false%> "
12002 "for the address of %qD will never be NULL",
12003 TREE_OPERAND (op1, 0));
12004 else
12005 warning_at (location,
12006 OPT_Waddress,
12007 "the comparison will always evaluate as %<true%> "
12008 "for the address of %qD will never be NULL",
12009 TREE_OPERAND (op1, 0));
12010 }
12011 result_type = type1;
12012 }
12013 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12014 {
12015 tree tt0 = TREE_TYPE (type0);
12016 tree tt1 = TREE_TYPE (type1);
12017 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12018 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12019 addr_space_t as_common = ADDR_SPACE_GENERIC;
12020
12021 /* Anything compares with void *. void * compares with anything.
12022 Otherwise, the targets must be compatible
12023 and both must be object or both incomplete. */
12024 if (comp_target_types (location, type0, type1))
12025 result_type = common_pointer_type (type0, type1);
12026 else if (!addr_space_superset (as0, as1, &as_common))
12027 {
12028 error_at (location, "comparison of pointers to "
12029 "disjoint address spaces");
12030 return error_mark_node;
12031 }
12032 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12033 {
12034 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12035 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12036 "comparison of %<void *%> with function pointer");
12037 }
12038 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12039 {
12040 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12041 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12042 "comparison of %<void *%> with function pointer");
12043 }
12044 else
12045 /* Avoid warning about the volatile ObjC EH puts on decls. */
12046 if (!objc_ok)
12047 pedwarn (location, 0,
12048 "comparison of distinct pointer types lacks a cast");
12049
12050 if (result_type == NULL_TREE)
12051 {
12052 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12053 result_type = build_pointer_type
12054 (build_qualified_type (void_type_node, qual));
12055 }
12056 }
12057 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12058 {
12059 result_type = type0;
12060 pedwarn (location, 0, "comparison between pointer and integer");
12061 }
12062 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12063 {
12064 result_type = type1;
12065 pedwarn (location, 0, "comparison between pointer and integer");
12066 }
12067 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12068 || truth_value_p (TREE_CODE (orig_op0)))
12069 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12070 || truth_value_p (TREE_CODE (orig_op1))))
12071 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12072 break;
12073
12074 case LE_EXPR:
12075 case GE_EXPR:
12076 case LT_EXPR:
12077 case GT_EXPR:
12078 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
12079 {
12080 tree intt;
12081 if (!vector_types_compatible_elements_p (type0, type1))
12082 {
12083 error_at (location, "comparing vectors with different "
12084 "element types");
12085 return error_mark_node;
12086 }
12087
12088 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12089 TYPE_VECTOR_SUBPARTS (type1)))
12090 {
12091 error_at (location, "comparing vectors with different "
12092 "number of elements");
12093 return error_mark_node;
12094 }
12095
12096 /* It's not precisely specified how the usual arithmetic
12097 conversions apply to the vector types. Here, we use
12098 the unsigned type if one of the operands is signed and
12099 the other one is unsigned. */
12100 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12101 {
12102 if (!TYPE_UNSIGNED (type0))
12103 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12104 else
12105 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12106 warning_at (location, OPT_Wsign_compare, "comparison between "
12107 "types %qT and %qT", type0, type1);
12108 }
12109
12110 /* Always construct signed integer vector type. */
12111 intt = c_common_type_for_size (GET_MODE_BITSIZE
12112 (SCALAR_TYPE_MODE
12113 (TREE_TYPE (type0))), 0);
12114 if (!intt)
12115 {
12116 error_at (location, "could not find an integer type "
12117 "of the same size as %qT",
12118 TREE_TYPE (type0));
12119 return error_mark_node;
12120 }
12121 result_type = build_opaque_vector_type (intt,
12122 TYPE_VECTOR_SUBPARTS (type0));
12123 converted = 1;
12124 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12125 goto return_build_binary_op;
12126 }
12127 build_type = integer_type_node;
12128 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12129 || code0 == FIXED_POINT_TYPE)
12130 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12131 || code1 == FIXED_POINT_TYPE))
12132 short_compare = 1;
12133 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12134 {
12135 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12136 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12137 addr_space_t as_common;
12138
12139 if (comp_target_types (location, type0, type1))
12140 {
12141 result_type = common_pointer_type (type0, type1);
12142 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12143 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12144 pedwarn (location, 0,
12145 "comparison of complete and incomplete pointers");
12146 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12147 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12148 "ordered comparisons of pointers to functions");
12149 else if (null_pointer_constant_p (orig_op0)
12150 || null_pointer_constant_p (orig_op1))
12151 warning_at (location, OPT_Wextra,
12152 "ordered comparison of pointer with null pointer");
12153
12154 }
12155 else if (!addr_space_superset (as0, as1, &as_common))
12156 {
12157 error_at (location, "comparison of pointers to "
12158 "disjoint address spaces");
12159 return error_mark_node;
12160 }
12161 else
12162 {
12163 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12164 result_type = build_pointer_type
12165 (build_qualified_type (void_type_node, qual));
12166 pedwarn (location, 0,
12167 "comparison of distinct pointer types lacks a cast");
12168 }
12169 }
12170 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12171 {
12172 result_type = type0;
12173 if (pedantic)
12174 pedwarn (location, OPT_Wpedantic,
12175 "ordered comparison of pointer with integer zero");
12176 else if (extra_warnings)
12177 warning_at (location, OPT_Wextra,
12178 "ordered comparison of pointer with integer zero");
12179 }
12180 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12181 {
12182 result_type = type1;
12183 if (pedantic)
12184 pedwarn (location, OPT_Wpedantic,
12185 "ordered comparison of pointer with integer zero");
12186 else if (extra_warnings)
12187 warning_at (location, OPT_Wextra,
12188 "ordered comparison of pointer with integer zero");
12189 }
12190 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12191 {
12192 result_type = type0;
12193 pedwarn (location, 0, "comparison between pointer and integer");
12194 }
12195 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12196 {
12197 result_type = type1;
12198 pedwarn (location, 0, "comparison between pointer and integer");
12199 }
12200
12201 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12202 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12203 {
12204 op0 = save_expr (op0);
12205 op1 = save_expr (op1);
12206
12207 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12208 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12209 }
12210
12211 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12212 || truth_value_p (TREE_CODE (orig_op0)))
12213 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12214 || truth_value_p (TREE_CODE (orig_op1))))
12215 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12216 break;
12217
12218 default:
12219 gcc_unreachable ();
12220 }
12221
12222 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12223 return error_mark_node;
12224
12225 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
12226 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12227 || !vector_types_compatible_elements_p (type0, type1)))
12228 {
12229 gcc_rich_location richloc (location);
12230 maybe_range_label_for_tree_type_mismatch
12231 label_for_op0 (orig_op0, orig_op1),
12232 label_for_op1 (orig_op1, orig_op0);
12233 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12234 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12235 binary_op_error (&richloc, code, type0, type1);
12236 return error_mark_node;
12237 }
12238
12239 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12240 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
12241 &&
12242 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12243 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
12244 {
12245 bool first_complex = (code0 == COMPLEX_TYPE);
12246 bool second_complex = (code1 == COMPLEX_TYPE);
12247 int none_complex = (!first_complex && !second_complex);
12248
12249 if (shorten || common || short_compare)
12250 {
12251 result_type = c_common_type (type0, type1);
12252 do_warn_double_promotion (result_type, type0, type1,
12253 "implicit conversion from %qT to %qT "
12254 "to match other operand of binary "
12255 "expression",
12256 location);
12257 if (result_type == error_mark_node)
12258 return error_mark_node;
12259 }
12260
12261 if (first_complex != second_complex
12262 && (code == PLUS_EXPR
12263 || code == MINUS_EXPR
12264 || code == MULT_EXPR
12265 || (code == TRUNC_DIV_EXPR && first_complex))
12266 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12267 && flag_signed_zeros)
12268 {
12269 /* An operation on mixed real/complex operands must be
12270 handled specially, but the language-independent code can
12271 more easily optimize the plain complex arithmetic if
12272 -fno-signed-zeros. */
12273 tree real_type = TREE_TYPE (result_type);
12274 tree real, imag;
12275 if (type0 != orig_type0 || type1 != orig_type1)
12276 {
12277 gcc_assert (may_need_excess_precision && common);
12278 semantic_result_type = c_common_type (orig_type0, orig_type1);
12279 }
12280 if (first_complex)
12281 {
12282 if (TREE_TYPE (op0) != result_type)
12283 op0 = convert_and_check (location, result_type, op0);
12284 if (TREE_TYPE (op1) != real_type)
12285 op1 = convert_and_check (location, real_type, op1);
12286 }
12287 else
12288 {
12289 if (TREE_TYPE (op0) != real_type)
12290 op0 = convert_and_check (location, real_type, op0);
12291 if (TREE_TYPE (op1) != result_type)
12292 op1 = convert_and_check (location, result_type, op1);
12293 }
12294 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12295 return error_mark_node;
12296 if (first_complex)
12297 {
12298 op0 = save_expr (op0);
12299 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12300 op0, true);
12301 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12302 op0, true);
12303 switch (code)
12304 {
12305 case MULT_EXPR:
12306 case TRUNC_DIV_EXPR:
12307 op1 = save_expr (op1);
12308 imag = build2 (resultcode, real_type, imag, op1);
12309 /* Fall through. */
12310 case PLUS_EXPR:
12311 case MINUS_EXPR:
12312 real = build2 (resultcode, real_type, real, op1);
12313 break;
12314 default:
12315 gcc_unreachable();
12316 }
12317 }
12318 else
12319 {
12320 op1 = save_expr (op1);
12321 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12322 op1, true);
12323 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12324 op1, true);
12325 switch (code)
12326 {
12327 case MULT_EXPR:
12328 op0 = save_expr (op0);
12329 imag = build2 (resultcode, real_type, op0, imag);
12330 /* Fall through. */
12331 case PLUS_EXPR:
12332 real = build2 (resultcode, real_type, op0, real);
12333 break;
12334 case MINUS_EXPR:
12335 real = build2 (resultcode, real_type, op0, real);
12336 imag = build1 (NEGATE_EXPR, real_type, imag);
12337 break;
12338 default:
12339 gcc_unreachable();
12340 }
12341 }
12342 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12343 goto return_build_binary_op;
12344 }
12345
12346 /* For certain operations (which identify themselves by shorten != 0)
12347 if both args were extended from the same smaller type,
12348 do the arithmetic in that type and then extend.
12349
12350 shorten !=0 and !=1 indicates a bitwise operation.
12351 For them, this optimization is safe only if
12352 both args are zero-extended or both are sign-extended.
12353 Otherwise, we might change the result.
12354 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12355 but calculated in (unsigned short) it would be (unsigned short)-1. */
12356
12357 if (shorten && none_complex)
12358 {
12359 final_type = result_type;
12360 result_type = shorten_binary_op (result_type, op0, op1,
12361 shorten == -1);
12362 }
12363
12364 /* Shifts can be shortened if shifting right. */
12365
12366 if (short_shift)
12367 {
12368 int unsigned_arg;
12369 tree arg0 = get_narrower (op0, &unsigned_arg);
12370
12371 final_type = result_type;
12372
12373 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12374 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12375
12376 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12377 && tree_int_cst_sgn (op1) > 0
12378 /* We can shorten only if the shift count is less than the
12379 number of bits in the smaller type size. */
12380 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12381 /* We cannot drop an unsigned shift after sign-extension. */
12382 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12383 {
12384 /* Do an unsigned shift if the operand was zero-extended. */
12385 result_type
12386 = c_common_signed_or_unsigned_type (unsigned_arg,
12387 TREE_TYPE (arg0));
12388 /* Convert value-to-be-shifted to that type. */
12389 if (TREE_TYPE (op0) != result_type)
12390 op0 = convert (result_type, op0);
12391 converted = 1;
12392 }
12393 }
12394
12395 /* Comparison operations are shortened too but differently.
12396 They identify themselves by setting short_compare = 1. */
12397
12398 if (short_compare)
12399 {
12400 /* Don't write &op0, etc., because that would prevent op0
12401 from being kept in a register.
12402 Instead, make copies of the our local variables and
12403 pass the copies by reference, then copy them back afterward. */
12404 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12405 enum tree_code xresultcode = resultcode;
12406 tree val
12407 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12408 &xresultcode);
12409
12410 if (val != NULL_TREE)
12411 {
12412 ret = val;
12413 goto return_build_binary_op;
12414 }
12415
12416 op0 = xop0, op1 = xop1;
12417 converted = 1;
12418 resultcode = xresultcode;
12419
12420 if (c_inhibit_evaluation_warnings == 0)
12421 {
12422 bool op0_maybe_const = true;
12423 bool op1_maybe_const = true;
12424 tree orig_op0_folded, orig_op1_folded;
12425
12426 if (in_late_binary_op)
12427 {
12428 orig_op0_folded = orig_op0;
12429 orig_op1_folded = orig_op1;
12430 }
12431 else
12432 {
12433 /* Fold for the sake of possible warnings, as in
12434 build_conditional_expr. This requires the
12435 "original" values to be folded, not just op0 and
12436 op1. */
12437 c_inhibit_evaluation_warnings++;
12438 op0 = c_fully_fold (op0, require_constant_value,
12439 &op0_maybe_const);
12440 op1 = c_fully_fold (op1, require_constant_value,
12441 &op1_maybe_const);
12442 c_inhibit_evaluation_warnings--;
12443 orig_op0_folded = c_fully_fold (orig_op0,
12444 require_constant_value,
12445 NULL);
12446 orig_op1_folded = c_fully_fold (orig_op1,
12447 require_constant_value,
12448 NULL);
12449 }
12450
12451 if (warn_sign_compare)
12452 warn_for_sign_compare (location, orig_op0_folded,
12453 orig_op1_folded, op0, op1,
12454 result_type, resultcode);
12455 if (!in_late_binary_op && !int_operands)
12456 {
12457 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12458 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12459 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12460 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12461 }
12462 }
12463 }
12464 }
12465
12466 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12467 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12468 Then the expression will be built.
12469 It will be given type FINAL_TYPE if that is nonzero;
12470 otherwise, it will be given type RESULT_TYPE. */
12471
12472 if (!result_type)
12473 {
12474 /* Favor showing any expression locations that are available. */
12475 op_location_t oploc (location, UNKNOWN_LOCATION);
12476 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12477 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12478 return error_mark_node;
12479 }
12480
12481 if (build_type == NULL_TREE)
12482 {
12483 build_type = result_type;
12484 if ((type0 != orig_type0 || type1 != orig_type1)
12485 && !boolean_op)
12486 {
12487 gcc_assert (may_need_excess_precision && common);
12488 semantic_result_type = c_common_type (orig_type0, orig_type1);
12489 }
12490 }
12491
12492 if (!converted)
12493 {
12494 op0 = ep_convert_and_check (location, result_type, op0,
12495 semantic_result_type);
12496 op1 = ep_convert_and_check (location, result_type, op1,
12497 semantic_result_type);
12498
12499 /* This can happen if one operand has a vector type, and the other
12500 has a different type. */
12501 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12502 return error_mark_node;
12503 }
12504
12505 if (sanitize_flags_p ((SANITIZE_SHIFT
12506 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12507 && current_function_decl != NULL_TREE
12508 && (doing_div_or_mod || doing_shift)
12509 && !require_constant_value)
12510 {
12511 /* OP0 and/or OP1 might have side-effects. */
12512 op0 = save_expr (op0);
12513 op1 = save_expr (op1);
12514 op0 = c_fully_fold (op0, false, NULL);
12515 op1 = c_fully_fold (op1, false, NULL);
12516 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12517 | SANITIZE_FLOAT_DIVIDE))))
12518 instrument_expr = ubsan_instrument_division (location, op0, op1);
12519 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12520 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12521 }
12522
12523 /* Treat expressions in initializers specially as they can't trap. */
12524 if (int_const_or_overflow)
12525 ret = (require_constant_value
12526 ? fold_build2_initializer_loc (location, resultcode, build_type,
12527 op0, op1)
12528 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12529 else
12530 ret = build2 (resultcode, build_type, op0, op1);
12531 if (final_type != NULL_TREE)
12532 ret = convert (final_type, ret);
12533
12534 return_build_binary_op:
12535 gcc_assert (ret != error_mark_node);
12536 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12537 ret = (int_operands
12538 ? note_integer_operands (ret)
12539 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12540 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12541 && !in_late_binary_op)
12542 ret = note_integer_operands (ret);
12543 protected_set_expr_location (ret, location);
12544
12545 if (instrument_expr != NULL)
12546 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12547 instrument_expr, ret);
12548
12549 if (semantic_result_type)
12550 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12551 semantic_result_type, ret);
12552
12553 return ret;
12554 }
12555
12556
12557 /* Convert EXPR to be a truth-value, validating its type for this
12558 purpose. LOCATION is the source location for the expression. */
12559
12560 tree
12561 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12562 {
12563 bool int_const, int_operands;
12564
12565 switch (TREE_CODE (TREE_TYPE (expr)))
12566 {
12567 case ARRAY_TYPE:
12568 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12569 return error_mark_node;
12570
12571 case RECORD_TYPE:
12572 error_at (location, "used struct type value where scalar is required");
12573 return error_mark_node;
12574
12575 case UNION_TYPE:
12576 error_at (location, "used union type value where scalar is required");
12577 return error_mark_node;
12578
12579 case VOID_TYPE:
12580 error_at (location, "void value not ignored as it ought to be");
12581 return error_mark_node;
12582
12583 case POINTER_TYPE:
12584 if (reject_gcc_builtin (expr))
12585 return error_mark_node;
12586 break;
12587
12588 case FUNCTION_TYPE:
12589 gcc_unreachable ();
12590
12591 case VECTOR_TYPE:
12592 error_at (location, "used vector type where scalar is required");
12593 return error_mark_node;
12594
12595 default:
12596 break;
12597 }
12598
12599 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12600 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12601 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12602 {
12603 expr = remove_c_maybe_const_expr (expr);
12604 expr = build2 (NE_EXPR, integer_type_node, expr,
12605 convert (TREE_TYPE (expr), integer_zero_node));
12606 expr = note_integer_operands (expr);
12607 }
12608 else
12609 /* ??? Should we also give an error for vectors rather than leaving
12610 those to give errors later? */
12611 expr = c_common_truthvalue_conversion (location, expr);
12612
12613 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12614 {
12615 if (TREE_OVERFLOW (expr))
12616 return expr;
12617 else
12618 return note_integer_operands (expr);
12619 }
12620 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12621 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12622 return expr;
12623 }
12624 \f
12625
12626 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12627 required. */
12628
12629 tree
12630 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12631 {
12632 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12633 {
12634 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12635 /* Executing a compound literal inside a function reinitializes
12636 it. */
12637 if (!TREE_STATIC (decl))
12638 *se = true;
12639 return decl;
12640 }
12641 else
12642 return expr;
12643 }
12644 \f
12645 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12646 statement. LOC is the location of the construct. */
12647
12648 tree
12649 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12650 tree clauses)
12651 {
12652 body = c_end_compound_stmt (loc, body, true);
12653
12654 tree stmt = make_node (code);
12655 TREE_TYPE (stmt) = void_type_node;
12656 OMP_BODY (stmt) = body;
12657 OMP_CLAUSES (stmt) = clauses;
12658 SET_EXPR_LOCATION (stmt, loc);
12659
12660 return add_stmt (stmt);
12661 }
12662
12663 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12664 statement. LOC is the location of the OACC_DATA. */
12665
12666 tree
12667 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12668 {
12669 tree stmt;
12670
12671 block = c_end_compound_stmt (loc, block, true);
12672
12673 stmt = make_node (OACC_DATA);
12674 TREE_TYPE (stmt) = void_type_node;
12675 OACC_DATA_CLAUSES (stmt) = clauses;
12676 OACC_DATA_BODY (stmt) = block;
12677 SET_EXPR_LOCATION (stmt, loc);
12678
12679 return add_stmt (stmt);
12680 }
12681
12682 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12683 statement. LOC is the location of the OACC_HOST_DATA. */
12684
12685 tree
12686 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12687 {
12688 tree stmt;
12689
12690 block = c_end_compound_stmt (loc, block, true);
12691
12692 stmt = make_node (OACC_HOST_DATA);
12693 TREE_TYPE (stmt) = void_type_node;
12694 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12695 OACC_HOST_DATA_BODY (stmt) = block;
12696 SET_EXPR_LOCATION (stmt, loc);
12697
12698 return add_stmt (stmt);
12699 }
12700
12701 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12702
12703 tree
12704 c_begin_omp_parallel (void)
12705 {
12706 tree block;
12707
12708 keep_next_level ();
12709 block = c_begin_compound_stmt (true);
12710
12711 return block;
12712 }
12713
12714 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12715 statement. LOC is the location of the OMP_PARALLEL. */
12716
12717 tree
12718 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12719 {
12720 tree stmt;
12721
12722 block = c_end_compound_stmt (loc, block, true);
12723
12724 stmt = make_node (OMP_PARALLEL);
12725 TREE_TYPE (stmt) = void_type_node;
12726 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12727 OMP_PARALLEL_BODY (stmt) = block;
12728 SET_EXPR_LOCATION (stmt, loc);
12729
12730 return add_stmt (stmt);
12731 }
12732
12733 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12734
12735 tree
12736 c_begin_omp_task (void)
12737 {
12738 tree block;
12739
12740 keep_next_level ();
12741 block = c_begin_compound_stmt (true);
12742
12743 return block;
12744 }
12745
12746 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12747 statement. LOC is the location of the #pragma. */
12748
12749 tree
12750 c_finish_omp_task (location_t loc, tree clauses, tree block)
12751 {
12752 tree stmt;
12753
12754 block = c_end_compound_stmt (loc, block, true);
12755
12756 stmt = make_node (OMP_TASK);
12757 TREE_TYPE (stmt) = void_type_node;
12758 OMP_TASK_CLAUSES (stmt) = clauses;
12759 OMP_TASK_BODY (stmt) = block;
12760 SET_EXPR_LOCATION (stmt, loc);
12761
12762 return add_stmt (stmt);
12763 }
12764
12765 /* Generate GOMP_cancel call for #pragma omp cancel. */
12766
12767 void
12768 c_finish_omp_cancel (location_t loc, tree clauses)
12769 {
12770 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12771 int mask = 0;
12772 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12773 mask = 1;
12774 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12775 mask = 2;
12776 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12777 mask = 4;
12778 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12779 mask = 8;
12780 else
12781 {
12782 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12783 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12784 "clauses");
12785 return;
12786 }
12787 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12788 if (ifc != NULL_TREE)
12789 {
12790 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
12791 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
12792 error_at (OMP_CLAUSE_LOCATION (ifc),
12793 "expected %<cancel%> %<if%> clause modifier");
12794 else
12795 {
12796 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
12797 if (ifc2 != NULL_TREE)
12798 {
12799 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
12800 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
12801 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
12802 error_at (OMP_CLAUSE_LOCATION (ifc2),
12803 "expected %<cancel%> %<if%> clause modifier");
12804 }
12805 }
12806
12807 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12808 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12809 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12810 build_zero_cst (type));
12811 }
12812 else
12813 ifc = boolean_true_node;
12814 tree stmt = build_call_expr_loc (loc, fn, 2,
12815 build_int_cst (integer_type_node, mask),
12816 ifc);
12817 add_stmt (stmt);
12818 }
12819
12820 /* Generate GOMP_cancellation_point call for
12821 #pragma omp cancellation point. */
12822
12823 void
12824 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12825 {
12826 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12827 int mask = 0;
12828 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12829 mask = 1;
12830 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12831 mask = 2;
12832 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12833 mask = 4;
12834 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12835 mask = 8;
12836 else
12837 {
12838 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12839 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12840 "clauses");
12841 return;
12842 }
12843 tree stmt = build_call_expr_loc (loc, fn, 1,
12844 build_int_cst (integer_type_node, mask));
12845 add_stmt (stmt);
12846 }
12847
12848 /* Helper function for handle_omp_array_sections. Called recursively
12849 to handle multiple array-section-subscripts. C is the clause,
12850 T current expression (initially OMP_CLAUSE_DECL), which is either
12851 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12852 expression if specified, TREE_VALUE length expression if specified,
12853 TREE_CHAIN is what it has been specified after, or some decl.
12854 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12855 set to true if any of the array-section-subscript could have length
12856 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12857 first array-section-subscript which is known not to have length
12858 of one. Given say:
12859 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12860 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12861 all are or may have length of 1, array-section-subscript [:2] is the
12862 first one known not to have length 1. For array-section-subscript
12863 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12864 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12865 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12866 case though, as some lengths could be zero. */
12867
12868 static tree
12869 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12870 bool &maybe_zero_len, unsigned int &first_non_one,
12871 enum c_omp_region_type ort)
12872 {
12873 tree ret, low_bound, length, type;
12874 if (TREE_CODE (t) != TREE_LIST)
12875 {
12876 if (error_operand_p (t))
12877 return error_mark_node;
12878 ret = t;
12879 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12880 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12881 {
12882 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12883 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12884 return error_mark_node;
12885 }
12886 if (TREE_CODE (t) == COMPONENT_REF
12887 && ort == C_ORT_OMP
12888 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12889 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12890 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12891 {
12892 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12893 {
12894 error_at (OMP_CLAUSE_LOCATION (c),
12895 "bit-field %qE in %qs clause",
12896 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12897 return error_mark_node;
12898 }
12899 while (TREE_CODE (t) == COMPONENT_REF)
12900 {
12901 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12902 {
12903 error_at (OMP_CLAUSE_LOCATION (c),
12904 "%qE is a member of a union", t);
12905 return error_mark_node;
12906 }
12907 t = TREE_OPERAND (t, 0);
12908 }
12909 }
12910 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12911 {
12912 if (DECL_P (t))
12913 error_at (OMP_CLAUSE_LOCATION (c),
12914 "%qD is not a variable in %qs clause", t,
12915 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12916 else
12917 error_at (OMP_CLAUSE_LOCATION (c),
12918 "%qE is not a variable in %qs clause", t,
12919 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12920 return error_mark_node;
12921 }
12922 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12923 && TYPE_ATOMIC (TREE_TYPE (t)))
12924 {
12925 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12926 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12927 return error_mark_node;
12928 }
12929 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12930 && VAR_P (t)
12931 && DECL_THREAD_LOCAL_P (t))
12932 {
12933 error_at (OMP_CLAUSE_LOCATION (c),
12934 "%qD is threadprivate variable in %qs clause", t,
12935 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12936 return error_mark_node;
12937 }
12938 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12939 && TYPE_ATOMIC (TREE_TYPE (t))
12940 && POINTER_TYPE_P (TREE_TYPE (t)))
12941 {
12942 /* If the array section is pointer based and the pointer
12943 itself is _Atomic qualified, we need to atomically load
12944 the pointer. */
12945 c_expr expr;
12946 memset (&expr, 0, sizeof (expr));
12947 expr.value = ret;
12948 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12949 expr, false, false);
12950 ret = expr.value;
12951 }
12952 return ret;
12953 }
12954
12955 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12956 maybe_zero_len, first_non_one, ort);
12957 if (ret == error_mark_node || ret == NULL_TREE)
12958 return ret;
12959
12960 type = TREE_TYPE (ret);
12961 low_bound = TREE_PURPOSE (t);
12962 length = TREE_VALUE (t);
12963
12964 if (low_bound == error_mark_node || length == error_mark_node)
12965 return error_mark_node;
12966
12967 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12968 {
12969 error_at (OMP_CLAUSE_LOCATION (c),
12970 "low bound %qE of array section does not have integral type",
12971 low_bound);
12972 return error_mark_node;
12973 }
12974 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12975 {
12976 error_at (OMP_CLAUSE_LOCATION (c),
12977 "length %qE of array section does not have integral type",
12978 length);
12979 return error_mark_node;
12980 }
12981 if (low_bound
12982 && TREE_CODE (low_bound) == INTEGER_CST
12983 && TYPE_PRECISION (TREE_TYPE (low_bound))
12984 > TYPE_PRECISION (sizetype))
12985 low_bound = fold_convert (sizetype, low_bound);
12986 if (length
12987 && TREE_CODE (length) == INTEGER_CST
12988 && TYPE_PRECISION (TREE_TYPE (length))
12989 > TYPE_PRECISION (sizetype))
12990 length = fold_convert (sizetype, length);
12991 if (low_bound == NULL_TREE)
12992 low_bound = integer_zero_node;
12993
12994 if (length != NULL_TREE)
12995 {
12996 if (!integer_nonzerop (length))
12997 {
12998 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12999 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13000 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13001 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13002 {
13003 if (integer_zerop (length))
13004 {
13005 error_at (OMP_CLAUSE_LOCATION (c),
13006 "zero length array section in %qs clause",
13007 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13008 return error_mark_node;
13009 }
13010 }
13011 else
13012 maybe_zero_len = true;
13013 }
13014 if (first_non_one == types.length ()
13015 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13016 first_non_one++;
13017 }
13018 if (TREE_CODE (type) == ARRAY_TYPE)
13019 {
13020 if (length == NULL_TREE
13021 && (TYPE_DOMAIN (type) == NULL_TREE
13022 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13023 {
13024 error_at (OMP_CLAUSE_LOCATION (c),
13025 "for unknown bound array type length expression must "
13026 "be specified");
13027 return error_mark_node;
13028 }
13029 if (TREE_CODE (low_bound) == INTEGER_CST
13030 && tree_int_cst_sgn (low_bound) == -1)
13031 {
13032 error_at (OMP_CLAUSE_LOCATION (c),
13033 "negative low bound in array section in %qs clause",
13034 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13035 return error_mark_node;
13036 }
13037 if (length != NULL_TREE
13038 && TREE_CODE (length) == INTEGER_CST
13039 && tree_int_cst_sgn (length) == -1)
13040 {
13041 error_at (OMP_CLAUSE_LOCATION (c),
13042 "negative length in array section in %qs clause",
13043 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13044 return error_mark_node;
13045 }
13046 if (TYPE_DOMAIN (type)
13047 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13048 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13049 == INTEGER_CST)
13050 {
13051 tree size
13052 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13053 size = size_binop (PLUS_EXPR, size, size_one_node);
13054 if (TREE_CODE (low_bound) == INTEGER_CST)
13055 {
13056 if (tree_int_cst_lt (size, low_bound))
13057 {
13058 error_at (OMP_CLAUSE_LOCATION (c),
13059 "low bound %qE above array section size "
13060 "in %qs clause", low_bound,
13061 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13062 return error_mark_node;
13063 }
13064 if (tree_int_cst_equal (size, low_bound))
13065 {
13066 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13067 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13068 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13069 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13070 {
13071 error_at (OMP_CLAUSE_LOCATION (c),
13072 "zero length array section in %qs clause",
13073 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13074 return error_mark_node;
13075 }
13076 maybe_zero_len = true;
13077 }
13078 else if (length == NULL_TREE
13079 && first_non_one == types.length ()
13080 && tree_int_cst_equal
13081 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13082 low_bound))
13083 first_non_one++;
13084 }
13085 else if (length == NULL_TREE)
13086 {
13087 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13088 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13089 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13090 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13091 maybe_zero_len = true;
13092 if (first_non_one == types.length ())
13093 first_non_one++;
13094 }
13095 if (length && TREE_CODE (length) == INTEGER_CST)
13096 {
13097 if (tree_int_cst_lt (size, length))
13098 {
13099 error_at (OMP_CLAUSE_LOCATION (c),
13100 "length %qE above array section size "
13101 "in %qs clause", length,
13102 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13103 return error_mark_node;
13104 }
13105 if (TREE_CODE (low_bound) == INTEGER_CST)
13106 {
13107 tree lbpluslen
13108 = size_binop (PLUS_EXPR,
13109 fold_convert (sizetype, low_bound),
13110 fold_convert (sizetype, length));
13111 if (TREE_CODE (lbpluslen) == INTEGER_CST
13112 && tree_int_cst_lt (size, lbpluslen))
13113 {
13114 error_at (OMP_CLAUSE_LOCATION (c),
13115 "high bound %qE above array section size "
13116 "in %qs clause", lbpluslen,
13117 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13118 return error_mark_node;
13119 }
13120 }
13121 }
13122 }
13123 else if (length == NULL_TREE)
13124 {
13125 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13126 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13127 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13128 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13129 maybe_zero_len = true;
13130 if (first_non_one == types.length ())
13131 first_non_one++;
13132 }
13133
13134 /* For [lb:] we will need to evaluate lb more than once. */
13135 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13136 {
13137 tree lb = save_expr (low_bound);
13138 if (lb != low_bound)
13139 {
13140 TREE_PURPOSE (t) = lb;
13141 low_bound = lb;
13142 }
13143 }
13144 }
13145 else if (TREE_CODE (type) == POINTER_TYPE)
13146 {
13147 if (length == NULL_TREE)
13148 {
13149 error_at (OMP_CLAUSE_LOCATION (c),
13150 "for pointer type length expression must be specified");
13151 return error_mark_node;
13152 }
13153 if (length != NULL_TREE
13154 && TREE_CODE (length) == INTEGER_CST
13155 && tree_int_cst_sgn (length) == -1)
13156 {
13157 error_at (OMP_CLAUSE_LOCATION (c),
13158 "negative length in array section in %qs clause",
13159 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13160 return error_mark_node;
13161 }
13162 /* If there is a pointer type anywhere but in the very first
13163 array-section-subscript, the array section can't be contiguous. */
13164 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13165 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13166 {
13167 error_at (OMP_CLAUSE_LOCATION (c),
13168 "array section is not contiguous in %qs clause",
13169 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13170 return error_mark_node;
13171 }
13172 }
13173 else
13174 {
13175 error_at (OMP_CLAUSE_LOCATION (c),
13176 "%qE does not have pointer or array type", ret);
13177 return error_mark_node;
13178 }
13179 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13180 types.safe_push (TREE_TYPE (ret));
13181 /* We will need to evaluate lb more than once. */
13182 tree lb = save_expr (low_bound);
13183 if (lb != low_bound)
13184 {
13185 TREE_PURPOSE (t) = lb;
13186 low_bound = lb;
13187 }
13188 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13189 return ret;
13190 }
13191
13192 /* Handle array sections for clause C. */
13193
13194 static bool
13195 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13196 {
13197 bool maybe_zero_len = false;
13198 unsigned int first_non_one = 0;
13199 auto_vec<tree, 10> types;
13200 tree *tp = &OMP_CLAUSE_DECL (c);
13201 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13202 && TREE_CODE (*tp) == TREE_LIST
13203 && TREE_PURPOSE (*tp)
13204 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13205 tp = &TREE_VALUE (*tp);
13206 tree first = handle_omp_array_sections_1 (c, *tp, types,
13207 maybe_zero_len, first_non_one,
13208 ort);
13209 if (first == error_mark_node)
13210 return true;
13211 if (first == NULL_TREE)
13212 return false;
13213 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13214 {
13215 tree t = *tp;
13216 tree tem = NULL_TREE;
13217 /* Need to evaluate side effects in the length expressions
13218 if any. */
13219 while (TREE_CODE (t) == TREE_LIST)
13220 {
13221 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13222 {
13223 if (tem == NULL_TREE)
13224 tem = TREE_VALUE (t);
13225 else
13226 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13227 TREE_VALUE (t), tem);
13228 }
13229 t = TREE_CHAIN (t);
13230 }
13231 if (tem)
13232 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13233 first = c_fully_fold (first, false, NULL, true);
13234 *tp = first;
13235 }
13236 else
13237 {
13238 unsigned int num = types.length (), i;
13239 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13240 tree condition = NULL_TREE;
13241
13242 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13243 maybe_zero_len = true;
13244
13245 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13246 t = TREE_CHAIN (t))
13247 {
13248 tree low_bound = TREE_PURPOSE (t);
13249 tree length = TREE_VALUE (t);
13250
13251 i--;
13252 if (low_bound
13253 && TREE_CODE (low_bound) == INTEGER_CST
13254 && TYPE_PRECISION (TREE_TYPE (low_bound))
13255 > TYPE_PRECISION (sizetype))
13256 low_bound = fold_convert (sizetype, low_bound);
13257 if (length
13258 && TREE_CODE (length) == INTEGER_CST
13259 && TYPE_PRECISION (TREE_TYPE (length))
13260 > TYPE_PRECISION (sizetype))
13261 length = fold_convert (sizetype, length);
13262 if (low_bound == NULL_TREE)
13263 low_bound = integer_zero_node;
13264 if (!maybe_zero_len && i > first_non_one)
13265 {
13266 if (integer_nonzerop (low_bound))
13267 goto do_warn_noncontiguous;
13268 if (length != NULL_TREE
13269 && TREE_CODE (length) == INTEGER_CST
13270 && TYPE_DOMAIN (types[i])
13271 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13272 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13273 == INTEGER_CST)
13274 {
13275 tree size;
13276 size = size_binop (PLUS_EXPR,
13277 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13278 size_one_node);
13279 if (!tree_int_cst_equal (length, size))
13280 {
13281 do_warn_noncontiguous:
13282 error_at (OMP_CLAUSE_LOCATION (c),
13283 "array section is not contiguous in %qs "
13284 "clause",
13285 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13286 return true;
13287 }
13288 }
13289 if (length != NULL_TREE
13290 && TREE_SIDE_EFFECTS (length))
13291 {
13292 if (side_effects == NULL_TREE)
13293 side_effects = length;
13294 else
13295 side_effects = build2 (COMPOUND_EXPR,
13296 TREE_TYPE (side_effects),
13297 length, side_effects);
13298 }
13299 }
13300 else
13301 {
13302 tree l;
13303
13304 if (i > first_non_one
13305 && ((length && integer_nonzerop (length))
13306 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13307 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13308 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13309 continue;
13310 if (length)
13311 l = fold_convert (sizetype, length);
13312 else
13313 {
13314 l = size_binop (PLUS_EXPR,
13315 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13316 size_one_node);
13317 l = size_binop (MINUS_EXPR, l,
13318 fold_convert (sizetype, low_bound));
13319 }
13320 if (i > first_non_one)
13321 {
13322 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13323 size_zero_node);
13324 if (condition == NULL_TREE)
13325 condition = l;
13326 else
13327 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13328 l, condition);
13329 }
13330 else if (size == NULL_TREE)
13331 {
13332 size = size_in_bytes (TREE_TYPE (types[i]));
13333 tree eltype = TREE_TYPE (types[num - 1]);
13334 while (TREE_CODE (eltype) == ARRAY_TYPE)
13335 eltype = TREE_TYPE (eltype);
13336 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13337 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13338 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13339 {
13340 if (integer_zerop (size)
13341 || integer_zerop (size_in_bytes (eltype)))
13342 {
13343 error_at (OMP_CLAUSE_LOCATION (c),
13344 "zero length array section in %qs clause",
13345 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13346 return error_mark_node;
13347 }
13348 size = size_binop (EXACT_DIV_EXPR, size,
13349 size_in_bytes (eltype));
13350 }
13351 size = size_binop (MULT_EXPR, size, l);
13352 if (condition)
13353 size = fold_build3 (COND_EXPR, sizetype, condition,
13354 size, size_zero_node);
13355 }
13356 else
13357 size = size_binop (MULT_EXPR, size, l);
13358 }
13359 }
13360 if (side_effects)
13361 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13362 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13363 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13364 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13365 {
13366 size = size_binop (MINUS_EXPR, size, size_one_node);
13367 size = c_fully_fold (size, false, NULL);
13368 size = save_expr (size);
13369 tree index_type = build_index_type (size);
13370 tree eltype = TREE_TYPE (first);
13371 while (TREE_CODE (eltype) == ARRAY_TYPE)
13372 eltype = TREE_TYPE (eltype);
13373 tree type = build_array_type (eltype, index_type);
13374 tree ptype = build_pointer_type (eltype);
13375 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13376 t = build_fold_addr_expr (t);
13377 tree t2 = build_fold_addr_expr (first);
13378 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13379 ptrdiff_type_node, t2);
13380 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13381 ptrdiff_type_node, t2,
13382 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13383 ptrdiff_type_node, t));
13384 t2 = c_fully_fold (t2, false, NULL);
13385 if (tree_fits_shwi_p (t2))
13386 t = build2 (MEM_REF, type, t,
13387 build_int_cst (ptype, tree_to_shwi (t2)));
13388 else
13389 {
13390 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13391 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13392 TREE_TYPE (t), t, t2);
13393 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13394 }
13395 OMP_CLAUSE_DECL (c) = t;
13396 return false;
13397 }
13398 first = c_fully_fold (first, false, NULL);
13399 OMP_CLAUSE_DECL (c) = first;
13400 if (size)
13401 size = c_fully_fold (size, false, NULL);
13402 OMP_CLAUSE_SIZE (c) = size;
13403 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13404 || (TREE_CODE (t) == COMPONENT_REF
13405 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13406 return false;
13407 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13408 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
13409 switch (OMP_CLAUSE_MAP_KIND (c))
13410 {
13411 case GOMP_MAP_ALLOC:
13412 case GOMP_MAP_TO:
13413 case GOMP_MAP_FROM:
13414 case GOMP_MAP_TOFROM:
13415 case GOMP_MAP_ALWAYS_TO:
13416 case GOMP_MAP_ALWAYS_FROM:
13417 case GOMP_MAP_ALWAYS_TOFROM:
13418 case GOMP_MAP_RELEASE:
13419 case GOMP_MAP_DELETE:
13420 case GOMP_MAP_FORCE_TO:
13421 case GOMP_MAP_FORCE_FROM:
13422 case GOMP_MAP_FORCE_TOFROM:
13423 case GOMP_MAP_FORCE_PRESENT:
13424 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13425 break;
13426 default:
13427 break;
13428 }
13429 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13430 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13431 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13432 else if (TREE_CODE (t) == COMPONENT_REF)
13433 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
13434 else
13435 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13436 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13437 && !c_mark_addressable (t))
13438 return false;
13439 OMP_CLAUSE_DECL (c2) = t;
13440 t = build_fold_addr_expr (first);
13441 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13442 tree ptr = OMP_CLAUSE_DECL (c2);
13443 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13444 ptr = build_fold_addr_expr (ptr);
13445 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13446 ptrdiff_type_node, t,
13447 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13448 ptrdiff_type_node, ptr));
13449 t = c_fully_fold (t, false, NULL);
13450 OMP_CLAUSE_SIZE (c2) = t;
13451 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13452 OMP_CLAUSE_CHAIN (c) = c2;
13453 }
13454 return false;
13455 }
13456
13457 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13458 an inline call. But, remap
13459 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13460 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13461
13462 static tree
13463 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13464 tree decl, tree placeholder)
13465 {
13466 copy_body_data id;
13467 hash_map<tree, tree> decl_map;
13468
13469 decl_map.put (omp_decl1, placeholder);
13470 decl_map.put (omp_decl2, decl);
13471 memset (&id, 0, sizeof (id));
13472 id.src_fn = DECL_CONTEXT (omp_decl1);
13473 id.dst_fn = current_function_decl;
13474 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13475 id.decl_map = &decl_map;
13476
13477 id.copy_decl = copy_decl_no_change;
13478 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13479 id.transform_new_cfg = true;
13480 id.transform_return_to_modify = false;
13481 id.transform_lang_insert_block = NULL;
13482 id.eh_lp_nr = 0;
13483 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13484 return stmt;
13485 }
13486
13487 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13488 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13489
13490 static tree
13491 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13492 {
13493 if (*tp == (tree) data)
13494 return *tp;
13495 return NULL_TREE;
13496 }
13497
13498 /* Similarly, but also walk aggregate fields. */
13499
13500 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13501
13502 static tree
13503 c_find_omp_var_r (tree *tp, int *, void *data)
13504 {
13505 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13506 return *tp;
13507 if (RECORD_OR_UNION_TYPE_P (*tp))
13508 {
13509 tree field;
13510 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13511
13512 for (field = TYPE_FIELDS (*tp); field;
13513 field = DECL_CHAIN (field))
13514 if (TREE_CODE (field) == FIELD_DECL)
13515 {
13516 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13517 c_find_omp_var_r, data, pset);
13518 if (ret)
13519 return ret;
13520 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13521 if (ret)
13522 return ret;
13523 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13524 pset);
13525 if (ret)
13526 return ret;
13527 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13528 if (ret)
13529 return ret;
13530 }
13531 }
13532 else if (INTEGRAL_TYPE_P (*tp))
13533 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13534 ((struct c_find_omp_var_s *) data)->pset);
13535 return NULL_TREE;
13536 }
13537
13538 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13539 and clauses containing them should be removed. */
13540
13541 static bool
13542 c_omp_finish_iterators (tree iter)
13543 {
13544 bool ret = false;
13545 for (tree it = iter; it; it = TREE_CHAIN (it))
13546 {
13547 tree var = TREE_VEC_ELT (it, 0);
13548 tree begin = TREE_VEC_ELT (it, 1);
13549 tree end = TREE_VEC_ELT (it, 2);
13550 tree step = TREE_VEC_ELT (it, 3);
13551 tree orig_step;
13552 tree type = TREE_TYPE (var);
13553 location_t loc = DECL_SOURCE_LOCATION (var);
13554 if (type == error_mark_node)
13555 {
13556 ret = true;
13557 continue;
13558 }
13559 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13560 {
13561 error_at (loc, "iterator %qD has neither integral nor pointer type",
13562 var);
13563 ret = true;
13564 continue;
13565 }
13566 else if (TYPE_ATOMIC (type))
13567 {
13568 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13569 ret = true;
13570 continue;
13571 }
13572 else if (TYPE_READONLY (type))
13573 {
13574 error_at (loc, "iterator %qD has const qualified type", var);
13575 ret = true;
13576 continue;
13577 }
13578 else if (step == error_mark_node
13579 || TREE_TYPE (step) == error_mark_node)
13580 {
13581 ret = true;
13582 continue;
13583 }
13584 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13585 {
13586 error_at (EXPR_LOC_OR_LOC (step, loc),
13587 "iterator step with non-integral type");
13588 ret = true;
13589 continue;
13590 }
13591 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13592 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13593 orig_step = save_expr (c_fully_fold (step, false, NULL));
13594 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13595 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13596 if (POINTER_TYPE_P (type))
13597 {
13598 begin = save_expr (begin);
13599 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13600 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13601 fold_convert (sizetype, step),
13602 fold_convert (sizetype, begin));
13603 step = fold_convert (ssizetype, step);
13604 }
13605 if (integer_zerop (step))
13606 {
13607 error_at (loc, "iterator %qD has zero step", var);
13608 ret = true;
13609 continue;
13610 }
13611
13612 if (begin == error_mark_node
13613 || end == error_mark_node
13614 || step == error_mark_node
13615 || orig_step == error_mark_node)
13616 {
13617 ret = true;
13618 continue;
13619 }
13620 hash_set<tree> pset;
13621 tree it2;
13622 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13623 {
13624 tree var2 = TREE_VEC_ELT (it2, 0);
13625 tree begin2 = TREE_VEC_ELT (it2, 1);
13626 tree end2 = TREE_VEC_ELT (it2, 2);
13627 tree step2 = TREE_VEC_ELT (it2, 3);
13628 tree type2 = TREE_TYPE (var2);
13629 location_t loc2 = DECL_SOURCE_LOCATION (var2);
13630 struct c_find_omp_var_s data = { var, &pset };
13631 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13632 {
13633 error_at (loc2,
13634 "type of iterator %qD refers to outer iterator %qD",
13635 var2, var);
13636 break;
13637 }
13638 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13639 {
13640 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13641 "begin expression refers to outer iterator %qD", var);
13642 break;
13643 }
13644 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13645 {
13646 error_at (EXPR_LOC_OR_LOC (end2, loc2),
13647 "end expression refers to outer iterator %qD", var);
13648 break;
13649 }
13650 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13651 {
13652 error_at (EXPR_LOC_OR_LOC (step2, loc2),
13653 "step expression refers to outer iterator %qD", var);
13654 break;
13655 }
13656 }
13657 if (it2)
13658 {
13659 ret = true;
13660 continue;
13661 }
13662 TREE_VEC_ELT (it, 1) = begin;
13663 TREE_VEC_ELT (it, 2) = end;
13664 TREE_VEC_ELT (it, 3) = step;
13665 TREE_VEC_ELT (it, 4) = orig_step;
13666 }
13667 return ret;
13668 }
13669
13670 /* For all elements of CLAUSES, validate them against their constraints.
13671 Remove any elements from the list that are invalid. */
13672
13673 tree
13674 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13675 {
13676 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13677 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
13678 tree c, t, type, *pc;
13679 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13680 bool branch_seen = false;
13681 bool copyprivate_seen = false;
13682 bool linear_variable_step_check = false;
13683 tree *nowait_clause = NULL;
13684 tree ordered_clause = NULL_TREE;
13685 tree schedule_clause = NULL_TREE;
13686 bool oacc_async = false;
13687 tree last_iterators = NULL_TREE;
13688 bool last_iterators_remove = false;
13689 tree *nogroup_seen = NULL;
13690 tree *order_clause = NULL;
13691 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
13692 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
13693 int reduction_seen = 0;
13694
13695 bitmap_obstack_initialize (NULL);
13696 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13697 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13698 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13699 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13700 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
13701 bitmap_initialize (&map_head, &bitmap_default_obstack);
13702 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13703 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
13704 instead. */
13705 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13706
13707 if (ort & C_ORT_ACC)
13708 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13709 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13710 {
13711 oacc_async = true;
13712 break;
13713 }
13714
13715 for (pc = &clauses, c = clauses; c ; c = *pc)
13716 {
13717 bool remove = false;
13718 bool need_complete = false;
13719 bool need_implicitly_determined = false;
13720
13721 switch (OMP_CLAUSE_CODE (c))
13722 {
13723 case OMP_CLAUSE_SHARED:
13724 need_implicitly_determined = true;
13725 goto check_dup_generic;
13726
13727 case OMP_CLAUSE_PRIVATE:
13728 need_complete = true;
13729 need_implicitly_determined = true;
13730 goto check_dup_generic;
13731
13732 case OMP_CLAUSE_REDUCTION:
13733 if (reduction_seen == 0)
13734 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
13735 else if (reduction_seen != -2
13736 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
13737 ? -1 : 1))
13738 {
13739 error_at (OMP_CLAUSE_LOCATION (c),
13740 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
13741 "on the same construct");
13742 reduction_seen = -2;
13743 }
13744 /* FALLTHRU */
13745 case OMP_CLAUSE_IN_REDUCTION:
13746 case OMP_CLAUSE_TASK_REDUCTION:
13747 need_implicitly_determined = true;
13748 t = OMP_CLAUSE_DECL (c);
13749 if (TREE_CODE (t) == TREE_LIST)
13750 {
13751 if (handle_omp_array_sections (c, ort))
13752 {
13753 remove = true;
13754 break;
13755 }
13756
13757 t = OMP_CLAUSE_DECL (c);
13758 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13759 && OMP_CLAUSE_REDUCTION_INSCAN (c))
13760 {
13761 error_at (OMP_CLAUSE_LOCATION (c),
13762 "%<inscan%> %<reduction%> clause with array "
13763 "section");
13764 remove = true;
13765 break;
13766 }
13767 }
13768 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13769 if (t == error_mark_node)
13770 {
13771 remove = true;
13772 break;
13773 }
13774 if (oacc_async)
13775 c_mark_addressable (t);
13776 type = TREE_TYPE (t);
13777 if (TREE_CODE (t) == MEM_REF)
13778 type = TREE_TYPE (type);
13779 if (TREE_CODE (type) == ARRAY_TYPE)
13780 {
13781 tree oatype = type;
13782 gcc_assert (TREE_CODE (t) != MEM_REF);
13783 while (TREE_CODE (type) == ARRAY_TYPE)
13784 type = TREE_TYPE (type);
13785 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13786 {
13787 error_at (OMP_CLAUSE_LOCATION (c),
13788 "%qD in %<reduction%> clause is a zero size array",
13789 t);
13790 remove = true;
13791 break;
13792 }
13793 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13794 TYPE_SIZE_UNIT (type));
13795 if (integer_zerop (size))
13796 {
13797 error_at (OMP_CLAUSE_LOCATION (c),
13798 "%qD in %<reduction%> clause is a zero size array",
13799 t);
13800 remove = true;
13801 break;
13802 }
13803 size = size_binop (MINUS_EXPR, size, size_one_node);
13804 size = save_expr (size);
13805 tree index_type = build_index_type (size);
13806 tree atype = build_array_type (type, index_type);
13807 tree ptype = build_pointer_type (type);
13808 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13809 t = build_fold_addr_expr (t);
13810 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13811 OMP_CLAUSE_DECL (c) = t;
13812 }
13813 if (TYPE_ATOMIC (type))
13814 {
13815 error_at (OMP_CLAUSE_LOCATION (c),
13816 "%<_Atomic%> %qE in %<reduction%> clause", t);
13817 remove = true;
13818 break;
13819 }
13820 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13821 || OMP_CLAUSE_REDUCTION_TASK (c))
13822 {
13823 /* Disallow zero sized or potentially zero sized task
13824 reductions. */
13825 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13826 {
13827 error_at (OMP_CLAUSE_LOCATION (c),
13828 "zero sized type %qT in %qs clause", type,
13829 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13830 remove = true;
13831 break;
13832 }
13833 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
13834 {
13835 error_at (OMP_CLAUSE_LOCATION (c),
13836 "variable sized type %qT in %qs clause", type,
13837 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13838 remove = true;
13839 break;
13840 }
13841 }
13842 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13843 && (FLOAT_TYPE_P (type)
13844 || TREE_CODE (type) == COMPLEX_TYPE))
13845 {
13846 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13847 const char *r_name = NULL;
13848
13849 switch (r_code)
13850 {
13851 case PLUS_EXPR:
13852 case MULT_EXPR:
13853 case MINUS_EXPR:
13854 break;
13855 case MIN_EXPR:
13856 if (TREE_CODE (type) == COMPLEX_TYPE)
13857 r_name = "min";
13858 break;
13859 case MAX_EXPR:
13860 if (TREE_CODE (type) == COMPLEX_TYPE)
13861 r_name = "max";
13862 break;
13863 case BIT_AND_EXPR:
13864 r_name = "&";
13865 break;
13866 case BIT_XOR_EXPR:
13867 r_name = "^";
13868 break;
13869 case BIT_IOR_EXPR:
13870 r_name = "|";
13871 break;
13872 case TRUTH_ANDIF_EXPR:
13873 if (FLOAT_TYPE_P (type))
13874 r_name = "&&";
13875 break;
13876 case TRUTH_ORIF_EXPR:
13877 if (FLOAT_TYPE_P (type))
13878 r_name = "||";
13879 break;
13880 default:
13881 gcc_unreachable ();
13882 }
13883 if (r_name)
13884 {
13885 error_at (OMP_CLAUSE_LOCATION (c),
13886 "%qE has invalid type for %<reduction(%s)%>",
13887 t, r_name);
13888 remove = true;
13889 break;
13890 }
13891 }
13892 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13893 {
13894 error_at (OMP_CLAUSE_LOCATION (c),
13895 "user defined reduction not found for %qE", t);
13896 remove = true;
13897 break;
13898 }
13899 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13900 {
13901 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13902 type = TYPE_MAIN_VARIANT (type);
13903 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13904 VAR_DECL, NULL_TREE, type);
13905 tree decl_placeholder = NULL_TREE;
13906 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13907 DECL_ARTIFICIAL (placeholder) = 1;
13908 DECL_IGNORED_P (placeholder) = 1;
13909 if (TREE_CODE (t) == MEM_REF)
13910 {
13911 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13912 VAR_DECL, NULL_TREE, type);
13913 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13914 DECL_ARTIFICIAL (decl_placeholder) = 1;
13915 DECL_IGNORED_P (decl_placeholder) = 1;
13916 }
13917 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13918 c_mark_addressable (placeholder);
13919 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13920 c_mark_addressable (decl_placeholder ? decl_placeholder
13921 : OMP_CLAUSE_DECL (c));
13922 OMP_CLAUSE_REDUCTION_MERGE (c)
13923 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13924 TREE_VEC_ELT (list, 0),
13925 TREE_VEC_ELT (list, 1),
13926 decl_placeholder ? decl_placeholder
13927 : OMP_CLAUSE_DECL (c), placeholder);
13928 OMP_CLAUSE_REDUCTION_MERGE (c)
13929 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13930 void_type_node, NULL_TREE,
13931 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13932 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13933 if (TREE_VEC_LENGTH (list) == 6)
13934 {
13935 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13936 c_mark_addressable (decl_placeholder ? decl_placeholder
13937 : OMP_CLAUSE_DECL (c));
13938 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13939 c_mark_addressable (placeholder);
13940 tree init = TREE_VEC_ELT (list, 5);
13941 if (init == error_mark_node)
13942 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13943 OMP_CLAUSE_REDUCTION_INIT (c)
13944 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13945 TREE_VEC_ELT (list, 3),
13946 decl_placeholder ? decl_placeholder
13947 : OMP_CLAUSE_DECL (c), placeholder);
13948 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13949 {
13950 tree v = decl_placeholder ? decl_placeholder : t;
13951 OMP_CLAUSE_REDUCTION_INIT (c)
13952 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13953 OMP_CLAUSE_REDUCTION_INIT (c));
13954 }
13955 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13956 c_find_omp_placeholder_r,
13957 placeholder, NULL))
13958 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13959 }
13960 else
13961 {
13962 tree init;
13963 tree v = decl_placeholder ? decl_placeholder : t;
13964 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13965 init = build_constructor (TREE_TYPE (v), NULL);
13966 else
13967 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13968 OMP_CLAUSE_REDUCTION_INIT (c)
13969 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13970 }
13971 OMP_CLAUSE_REDUCTION_INIT (c)
13972 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13973 void_type_node, NULL_TREE,
13974 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13975 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13976 }
13977 if (TREE_CODE (t) == MEM_REF)
13978 {
13979 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13980 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13981 != INTEGER_CST)
13982 {
13983 sorry ("variable length element type in array "
13984 "%<reduction%> clause");
13985 remove = true;
13986 break;
13987 }
13988 t = TREE_OPERAND (t, 0);
13989 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13990 t = TREE_OPERAND (t, 0);
13991 if (TREE_CODE (t) == ADDR_EXPR)
13992 t = TREE_OPERAND (t, 0);
13993 }
13994 goto check_dup_generic_t;
13995
13996 case OMP_CLAUSE_COPYPRIVATE:
13997 copyprivate_seen = true;
13998 if (nowait_clause)
13999 {
14000 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14001 "%<nowait%> clause must not be used together "
14002 "with %<copyprivate%>");
14003 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14004 nowait_clause = NULL;
14005 }
14006 goto check_dup_generic;
14007
14008 case OMP_CLAUSE_COPYIN:
14009 t = OMP_CLAUSE_DECL (c);
14010 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14011 {
14012 error_at (OMP_CLAUSE_LOCATION (c),
14013 "%qE must be %<threadprivate%> for %<copyin%>", t);
14014 remove = true;
14015 break;
14016 }
14017 goto check_dup_generic;
14018
14019 case OMP_CLAUSE_LINEAR:
14020 if (ort != C_ORT_OMP_DECLARE_SIMD)
14021 need_implicitly_determined = true;
14022 t = OMP_CLAUSE_DECL (c);
14023 if (ort != C_ORT_OMP_DECLARE_SIMD
14024 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
14025 {
14026 error_at (OMP_CLAUSE_LOCATION (c),
14027 "modifier should not be specified in %<linear%> "
14028 "clause on %<simd%> or %<for%> constructs");
14029 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14030 }
14031 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14032 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14033 {
14034 error_at (OMP_CLAUSE_LOCATION (c),
14035 "linear clause applied to non-integral non-pointer "
14036 "variable with type %qT", TREE_TYPE (t));
14037 remove = true;
14038 break;
14039 }
14040 if (TYPE_ATOMIC (TREE_TYPE (t)))
14041 {
14042 error_at (OMP_CLAUSE_LOCATION (c),
14043 "%<_Atomic%> %qD in %<linear%> clause", t);
14044 remove = true;
14045 break;
14046 }
14047 if (ort == C_ORT_OMP_DECLARE_SIMD)
14048 {
14049 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14050 if (TREE_CODE (s) == PARM_DECL)
14051 {
14052 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14053 /* map_head bitmap is used as uniform_head if
14054 declare_simd. */
14055 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14056 linear_variable_step_check = true;
14057 goto check_dup_generic;
14058 }
14059 if (TREE_CODE (s) != INTEGER_CST)
14060 {
14061 error_at (OMP_CLAUSE_LOCATION (c),
14062 "%<linear%> clause step %qE is neither constant "
14063 "nor a parameter", s);
14064 remove = true;
14065 break;
14066 }
14067 }
14068 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14069 {
14070 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14071 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14072 OMP_CLAUSE_DECL (c), s);
14073 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14074 sizetype, fold_convert (sizetype, s),
14075 fold_convert
14076 (sizetype, OMP_CLAUSE_DECL (c)));
14077 if (s == error_mark_node)
14078 s = size_one_node;
14079 OMP_CLAUSE_LINEAR_STEP (c) = s;
14080 }
14081 else
14082 OMP_CLAUSE_LINEAR_STEP (c)
14083 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14084 goto check_dup_generic;
14085
14086 check_dup_generic:
14087 t = OMP_CLAUSE_DECL (c);
14088 check_dup_generic_t:
14089 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14090 {
14091 error_at (OMP_CLAUSE_LOCATION (c),
14092 "%qE is not a variable in clause %qs", t,
14093 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14094 remove = true;
14095 }
14096 else if ((ort == C_ORT_ACC
14097 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14098 || (ort == C_ORT_OMP
14099 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14100 || (OMP_CLAUSE_CODE (c)
14101 == OMP_CLAUSE_USE_DEVICE_ADDR))))
14102 {
14103 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14104 {
14105 error_at (OMP_CLAUSE_LOCATION (c),
14106 ort == C_ORT_ACC
14107 ? "%qD appears more than once in reduction clauses"
14108 : "%qD appears more than once in data clauses",
14109 t);
14110 remove = true;
14111 }
14112 else
14113 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14114 }
14115 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14116 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14117 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14118 {
14119 error_at (OMP_CLAUSE_LOCATION (c),
14120 "%qE appears more than once in data clauses", t);
14121 remove = true;
14122 }
14123 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14124 && bitmap_bit_p (&map_head, DECL_UID (t)))
14125 {
14126 if (ort == C_ORT_ACC)
14127 error_at (OMP_CLAUSE_LOCATION (c),
14128 "%qD appears more than once in data clauses", t);
14129 else
14130 error_at (OMP_CLAUSE_LOCATION (c),
14131 "%qD appears both in data and map clauses", t);
14132 remove = true;
14133 }
14134 else
14135 bitmap_set_bit (&generic_head, DECL_UID (t));
14136 break;
14137
14138 case OMP_CLAUSE_FIRSTPRIVATE:
14139 t = OMP_CLAUSE_DECL (c);
14140 need_complete = true;
14141 need_implicitly_determined = true;
14142 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14143 {
14144 error_at (OMP_CLAUSE_LOCATION (c),
14145 "%qE is not a variable in clause %<firstprivate%>", t);
14146 remove = true;
14147 }
14148 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14149 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14150 {
14151 error_at (OMP_CLAUSE_LOCATION (c),
14152 "%qE appears more than once in data clauses", t);
14153 remove = true;
14154 }
14155 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14156 {
14157 if (ort == C_ORT_ACC)
14158 error_at (OMP_CLAUSE_LOCATION (c),
14159 "%qD appears more than once in data clauses", t);
14160 else
14161 error_at (OMP_CLAUSE_LOCATION (c),
14162 "%qD appears both in data and map clauses", t);
14163 remove = true;
14164 }
14165 else
14166 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14167 break;
14168
14169 case OMP_CLAUSE_LASTPRIVATE:
14170 t = OMP_CLAUSE_DECL (c);
14171 need_complete = true;
14172 need_implicitly_determined = true;
14173 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14174 {
14175 error_at (OMP_CLAUSE_LOCATION (c),
14176 "%qE is not a variable in clause %<lastprivate%>", t);
14177 remove = true;
14178 }
14179 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14180 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14181 {
14182 error_at (OMP_CLAUSE_LOCATION (c),
14183 "%qE appears more than once in data clauses", t);
14184 remove = true;
14185 }
14186 else
14187 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14188 break;
14189
14190 case OMP_CLAUSE_ALIGNED:
14191 t = OMP_CLAUSE_DECL (c);
14192 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14193 {
14194 error_at (OMP_CLAUSE_LOCATION (c),
14195 "%qE is not a variable in %<aligned%> clause", t);
14196 remove = true;
14197 }
14198 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14199 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14200 {
14201 error_at (OMP_CLAUSE_LOCATION (c),
14202 "%qE in %<aligned%> clause is neither a pointer nor "
14203 "an array", t);
14204 remove = true;
14205 }
14206 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14207 {
14208 error_at (OMP_CLAUSE_LOCATION (c),
14209 "%<_Atomic%> %qD in %<aligned%> clause", t);
14210 remove = true;
14211 break;
14212 }
14213 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14214 {
14215 error_at (OMP_CLAUSE_LOCATION (c),
14216 "%qE appears more than once in %<aligned%> clauses",
14217 t);
14218 remove = true;
14219 }
14220 else
14221 bitmap_set_bit (&aligned_head, DECL_UID (t));
14222 break;
14223
14224 case OMP_CLAUSE_NONTEMPORAL:
14225 t = OMP_CLAUSE_DECL (c);
14226 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14227 {
14228 error_at (OMP_CLAUSE_LOCATION (c),
14229 "%qE is not a variable in %<nontemporal%> clause", t);
14230 remove = true;
14231 }
14232 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14233 {
14234 error_at (OMP_CLAUSE_LOCATION (c),
14235 "%qE appears more than once in %<nontemporal%> "
14236 "clauses", t);
14237 remove = true;
14238 }
14239 else
14240 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14241 break;
14242
14243 case OMP_CLAUSE_DEPEND:
14244 t = OMP_CLAUSE_DECL (c);
14245 if (t == NULL_TREE)
14246 {
14247 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14248 == OMP_CLAUSE_DEPEND_SOURCE);
14249 break;
14250 }
14251 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14252 {
14253 gcc_assert (TREE_CODE (t) == TREE_LIST);
14254 for (; t; t = TREE_CHAIN (t))
14255 {
14256 tree decl = TREE_VALUE (t);
14257 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14258 {
14259 tree offset = TREE_PURPOSE (t);
14260 bool neg = wi::neg_p (wi::to_wide (offset));
14261 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14262 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14263 neg ? MINUS_EXPR : PLUS_EXPR,
14264 decl, offset);
14265 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14266 sizetype,
14267 fold_convert (sizetype, t2),
14268 fold_convert (sizetype, decl));
14269 if (t2 == error_mark_node)
14270 {
14271 remove = true;
14272 break;
14273 }
14274 TREE_PURPOSE (t) = t2;
14275 }
14276 }
14277 break;
14278 }
14279 if (TREE_CODE (t) == TREE_LIST
14280 && TREE_PURPOSE (t)
14281 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14282 {
14283 if (TREE_PURPOSE (t) != last_iterators)
14284 last_iterators_remove
14285 = c_omp_finish_iterators (TREE_PURPOSE (t));
14286 last_iterators = TREE_PURPOSE (t);
14287 t = TREE_VALUE (t);
14288 if (last_iterators_remove)
14289 t = error_mark_node;
14290 }
14291 else
14292 last_iterators = NULL_TREE;
14293 if (TREE_CODE (t) == TREE_LIST)
14294 {
14295 if (handle_omp_array_sections (c, ort))
14296 remove = true;
14297 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14298 {
14299 error_at (OMP_CLAUSE_LOCATION (c),
14300 "%<depend%> clause with %<depobj%> dependence "
14301 "type on array section");
14302 remove = true;
14303 }
14304 break;
14305 }
14306 if (t == error_mark_node)
14307 remove = true;
14308 else if (!lvalue_p (t))
14309 {
14310 error_at (OMP_CLAUSE_LOCATION (c),
14311 "%qE is not lvalue expression nor array section in "
14312 "%<depend%> clause", t);
14313 remove = true;
14314 }
14315 else if (TREE_CODE (t) == COMPONENT_REF
14316 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14317 {
14318 error_at (OMP_CLAUSE_LOCATION (c),
14319 "bit-field %qE in %qs clause", t, "depend");
14320 remove = true;
14321 }
14322 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14323 {
14324 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14325 {
14326 error_at (OMP_CLAUSE_LOCATION (c),
14327 "%qE does not have %<omp_depend_t%> type in "
14328 "%<depend%> clause with %<depobj%> dependence "
14329 "type", t);
14330 remove = true;
14331 }
14332 }
14333 else if (c_omp_depend_t_p (TREE_TYPE (t)))
14334 {
14335 error_at (OMP_CLAUSE_LOCATION (c),
14336 "%qE should not have %<omp_depend_t%> type in "
14337 "%<depend%> clause with dependence type other than "
14338 "%<depobj%>", t);
14339 remove = true;
14340 }
14341 if (!remove)
14342 {
14343 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14344 t, false);
14345 if (addr == error_mark_node)
14346 remove = true;
14347 else
14348 {
14349 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14350 RO_UNARY_STAR);
14351 if (t == error_mark_node)
14352 remove = true;
14353 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14354 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14355 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14356 == TREE_VEC))
14357 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14358 else
14359 OMP_CLAUSE_DECL (c) = t;
14360 }
14361 }
14362 break;
14363
14364 case OMP_CLAUSE_MAP:
14365 case OMP_CLAUSE_TO:
14366 case OMP_CLAUSE_FROM:
14367 case OMP_CLAUSE__CACHE_:
14368 t = OMP_CLAUSE_DECL (c);
14369 if (TREE_CODE (t) == TREE_LIST)
14370 {
14371 if (handle_omp_array_sections (c, ort))
14372 remove = true;
14373 else
14374 {
14375 t = OMP_CLAUSE_DECL (c);
14376 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14377 {
14378 error_at (OMP_CLAUSE_LOCATION (c),
14379 "array section does not have mappable type "
14380 "in %qs clause",
14381 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14382 remove = true;
14383 }
14384 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14385 {
14386 error_at (OMP_CLAUSE_LOCATION (c),
14387 "%<_Atomic%> %qE in %qs clause", t,
14388 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14389 remove = true;
14390 }
14391 while (TREE_CODE (t) == ARRAY_REF)
14392 t = TREE_OPERAND (t, 0);
14393 if (TREE_CODE (t) == COMPONENT_REF
14394 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14395 {
14396 while (TREE_CODE (t) == COMPONENT_REF)
14397 t = TREE_OPERAND (t, 0);
14398 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14399 break;
14400 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14401 {
14402 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14403 error_at (OMP_CLAUSE_LOCATION (c),
14404 "%qD appears more than once in motion "
14405 "clauses", t);
14406 else if (ort == C_ORT_ACC)
14407 error_at (OMP_CLAUSE_LOCATION (c),
14408 "%qD appears more than once in data "
14409 "clauses", t);
14410 else
14411 error_at (OMP_CLAUSE_LOCATION (c),
14412 "%qD appears more than once in map "
14413 "clauses", t);
14414 remove = true;
14415 }
14416 else
14417 {
14418 bitmap_set_bit (&map_head, DECL_UID (t));
14419 bitmap_set_bit (&map_field_head, DECL_UID (t));
14420 }
14421 }
14422 }
14423 break;
14424 }
14425 if (t == error_mark_node)
14426 {
14427 remove = true;
14428 break;
14429 }
14430 if (TREE_CODE (t) == COMPONENT_REF
14431 && (ort & C_ORT_OMP)
14432 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14433 {
14434 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14435 {
14436 error_at (OMP_CLAUSE_LOCATION (c),
14437 "bit-field %qE in %qs clause",
14438 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14439 remove = true;
14440 }
14441 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14442 {
14443 error_at (OMP_CLAUSE_LOCATION (c),
14444 "%qE does not have a mappable type in %qs clause",
14445 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14446 remove = true;
14447 }
14448 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14449 {
14450 error_at (OMP_CLAUSE_LOCATION (c),
14451 "%<_Atomic%> %qE in %qs clause", t,
14452 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14453 remove = true;
14454 }
14455 while (TREE_CODE (t) == COMPONENT_REF)
14456 {
14457 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14458 == UNION_TYPE)
14459 {
14460 error_at (OMP_CLAUSE_LOCATION (c),
14461 "%qE is a member of a union", t);
14462 remove = true;
14463 break;
14464 }
14465 t = TREE_OPERAND (t, 0);
14466 }
14467 if (remove)
14468 break;
14469 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14470 {
14471 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14472 break;
14473 }
14474 }
14475 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14476 {
14477 error_at (OMP_CLAUSE_LOCATION (c),
14478 "%qE is not a variable in %qs clause", t,
14479 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14480 remove = true;
14481 }
14482 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14483 {
14484 error_at (OMP_CLAUSE_LOCATION (c),
14485 "%qD is threadprivate variable 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)
14491 != GOMP_MAP_FIRSTPRIVATE_POINTER))
14492 && !c_mark_addressable (t))
14493 remove = true;
14494 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14495 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
14496 || (OMP_CLAUSE_MAP_KIND (c)
14497 == GOMP_MAP_FIRSTPRIVATE_POINTER)
14498 || (OMP_CLAUSE_MAP_KIND (c)
14499 == GOMP_MAP_FORCE_DEVICEPTR)))
14500 && t == OMP_CLAUSE_DECL (c)
14501 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14502 {
14503 error_at (OMP_CLAUSE_LOCATION (c),
14504 "%qD does not have a mappable type in %qs clause", t,
14505 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14506 remove = true;
14507 }
14508 else if (TREE_TYPE (t) == error_mark_node)
14509 remove = true;
14510 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14511 {
14512 error_at (OMP_CLAUSE_LOCATION (c),
14513 "%<_Atomic%> %qE in %qs clause", t,
14514 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14515 remove = true;
14516 }
14517 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14518 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14519 {
14520 if (bitmap_bit_p (&generic_head, DECL_UID (t))
14521 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14522 {
14523 error_at (OMP_CLAUSE_LOCATION (c),
14524 "%qD appears more than once in data clauses", t);
14525 remove = true;
14526 }
14527 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14528 {
14529 if (ort == C_ORT_ACC)
14530 error_at (OMP_CLAUSE_LOCATION (c),
14531 "%qD appears more than once in data clauses", t);
14532 else
14533 error_at (OMP_CLAUSE_LOCATION (c),
14534 "%qD appears both in data and map clauses", t);
14535 remove = true;
14536 }
14537 else
14538 bitmap_set_bit (&generic_head, DECL_UID (t));
14539 }
14540 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14541 {
14542 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14543 error_at (OMP_CLAUSE_LOCATION (c),
14544 "%qD appears more than once in motion clauses", t);
14545 else if (ort == C_ORT_ACC)
14546 error_at (OMP_CLAUSE_LOCATION (c),
14547 "%qD appears more than once in data clauses", t);
14548 else
14549 error_at (OMP_CLAUSE_LOCATION (c),
14550 "%qD appears more than once in map clauses", t);
14551 remove = true;
14552 }
14553 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14554 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14555 {
14556 if (ort == C_ORT_ACC)
14557 error_at (OMP_CLAUSE_LOCATION (c),
14558 "%qD appears more than once in data clauses", t);
14559 else
14560 error_at (OMP_CLAUSE_LOCATION (c),
14561 "%qD appears both in data and map clauses", t);
14562 remove = true;
14563 }
14564 else
14565 {
14566 bitmap_set_bit (&map_head, DECL_UID (t));
14567 if (t != OMP_CLAUSE_DECL (c)
14568 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14569 bitmap_set_bit (&map_field_head, DECL_UID (t));
14570 }
14571 break;
14572
14573 case OMP_CLAUSE_TO_DECLARE:
14574 case OMP_CLAUSE_LINK:
14575 t = OMP_CLAUSE_DECL (c);
14576 if (TREE_CODE (t) == FUNCTION_DECL
14577 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14578 ;
14579 else if (!VAR_P (t))
14580 {
14581 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14582 error_at (OMP_CLAUSE_LOCATION (c),
14583 "%qE is neither a variable nor a function name in "
14584 "clause %qs", t,
14585 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14586 else
14587 error_at (OMP_CLAUSE_LOCATION (c),
14588 "%qE is not a variable in clause %qs", t,
14589 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14590 remove = true;
14591 }
14592 else if (DECL_THREAD_LOCAL_P (t))
14593 {
14594 error_at (OMP_CLAUSE_LOCATION (c),
14595 "%qD is threadprivate variable in %qs clause", t,
14596 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14597 remove = true;
14598 }
14599 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14600 {
14601 error_at (OMP_CLAUSE_LOCATION (c),
14602 "%qD does not have a mappable type in %qs clause", t,
14603 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14604 remove = true;
14605 }
14606 if (remove)
14607 break;
14608 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
14609 {
14610 error_at (OMP_CLAUSE_LOCATION (c),
14611 "%qE appears more than once on the same "
14612 "%<declare target%> directive", t);
14613 remove = true;
14614 }
14615 else
14616 bitmap_set_bit (&generic_head, DECL_UID (t));
14617 break;
14618
14619 case OMP_CLAUSE_UNIFORM:
14620 t = OMP_CLAUSE_DECL (c);
14621 if (TREE_CODE (t) != PARM_DECL)
14622 {
14623 if (DECL_P (t))
14624 error_at (OMP_CLAUSE_LOCATION (c),
14625 "%qD is not an argument in %<uniform%> clause", t);
14626 else
14627 error_at (OMP_CLAUSE_LOCATION (c),
14628 "%qE is not an argument in %<uniform%> clause", t);
14629 remove = true;
14630 break;
14631 }
14632 /* map_head bitmap is used as uniform_head if declare_simd. */
14633 bitmap_set_bit (&map_head, DECL_UID (t));
14634 goto check_dup_generic;
14635
14636 case OMP_CLAUSE_IS_DEVICE_PTR:
14637 case OMP_CLAUSE_USE_DEVICE_PTR:
14638 t = OMP_CLAUSE_DECL (c);
14639 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14640 {
14641 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14642 && ort == C_ORT_OMP)
14643 {
14644 error_at (OMP_CLAUSE_LOCATION (c),
14645 "%qs variable is not a pointer",
14646 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14647 remove = true;
14648 }
14649 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14650 {
14651 error_at (OMP_CLAUSE_LOCATION (c),
14652 "%qs variable is neither a pointer nor an array",
14653 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14654 remove = true;
14655 }
14656 }
14657 goto check_dup_generic;
14658
14659 case OMP_CLAUSE_USE_DEVICE_ADDR:
14660 t = OMP_CLAUSE_DECL (c);
14661 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14662 c_mark_addressable (t);
14663 goto check_dup_generic;
14664
14665 case OMP_CLAUSE_NOWAIT:
14666 if (copyprivate_seen)
14667 {
14668 error_at (OMP_CLAUSE_LOCATION (c),
14669 "%<nowait%> clause must not be used together "
14670 "with %<copyprivate%>");
14671 remove = true;
14672 break;
14673 }
14674 nowait_clause = pc;
14675 pc = &OMP_CLAUSE_CHAIN (c);
14676 continue;
14677
14678 case OMP_CLAUSE_ORDER:
14679 if (ordered_clause)
14680 {
14681 error_at (OMP_CLAUSE_LOCATION (c),
14682 "%<order%> clause must not be used together "
14683 "with %<ordered%>");
14684 remove = true;
14685 break;
14686 }
14687 else if (order_clause)
14688 {
14689 /* Silently remove duplicates. */
14690 remove = true;
14691 break;
14692 }
14693 order_clause = pc;
14694 pc = &OMP_CLAUSE_CHAIN (c);
14695 continue;
14696
14697 case OMP_CLAUSE_IF:
14698 case OMP_CLAUSE_NUM_THREADS:
14699 case OMP_CLAUSE_NUM_TEAMS:
14700 case OMP_CLAUSE_THREAD_LIMIT:
14701 case OMP_CLAUSE_DEFAULT:
14702 case OMP_CLAUSE_UNTIED:
14703 case OMP_CLAUSE_COLLAPSE:
14704 case OMP_CLAUSE_FINAL:
14705 case OMP_CLAUSE_MERGEABLE:
14706 case OMP_CLAUSE_DEVICE:
14707 case OMP_CLAUSE_DIST_SCHEDULE:
14708 case OMP_CLAUSE_PARALLEL:
14709 case OMP_CLAUSE_FOR:
14710 case OMP_CLAUSE_SECTIONS:
14711 case OMP_CLAUSE_TASKGROUP:
14712 case OMP_CLAUSE_PROC_BIND:
14713 case OMP_CLAUSE_DEVICE_TYPE:
14714 case OMP_CLAUSE_PRIORITY:
14715 case OMP_CLAUSE_GRAINSIZE:
14716 case OMP_CLAUSE_NUM_TASKS:
14717 case OMP_CLAUSE_THREADS:
14718 case OMP_CLAUSE_SIMD:
14719 case OMP_CLAUSE_HINT:
14720 case OMP_CLAUSE_DEFAULTMAP:
14721 case OMP_CLAUSE_BIND:
14722 case OMP_CLAUSE_NUM_GANGS:
14723 case OMP_CLAUSE_NUM_WORKERS:
14724 case OMP_CLAUSE_VECTOR_LENGTH:
14725 case OMP_CLAUSE_ASYNC:
14726 case OMP_CLAUSE_WAIT:
14727 case OMP_CLAUSE_AUTO:
14728 case OMP_CLAUSE_INDEPENDENT:
14729 case OMP_CLAUSE_SEQ:
14730 case OMP_CLAUSE_GANG:
14731 case OMP_CLAUSE_WORKER:
14732 case OMP_CLAUSE_VECTOR:
14733 case OMP_CLAUSE_TILE:
14734 case OMP_CLAUSE_IF_PRESENT:
14735 case OMP_CLAUSE_FINALIZE:
14736 pc = &OMP_CLAUSE_CHAIN (c);
14737 continue;
14738
14739 case OMP_CLAUSE_NOGROUP:
14740 nogroup_seen = pc;
14741 pc = &OMP_CLAUSE_CHAIN (c);
14742 continue;
14743
14744 case OMP_CLAUSE_SCHEDULE:
14745 schedule_clause = c;
14746 pc = &OMP_CLAUSE_CHAIN (c);
14747 continue;
14748
14749 case OMP_CLAUSE_ORDERED:
14750 ordered_clause = c;
14751 if (order_clause)
14752 {
14753 error_at (OMP_CLAUSE_LOCATION (*order_clause),
14754 "%<order%> clause must not be used together "
14755 "with %<ordered%>");
14756 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
14757 order_clause = NULL;
14758 }
14759 pc = &OMP_CLAUSE_CHAIN (c);
14760 continue;
14761
14762 case OMP_CLAUSE_SAFELEN:
14763 safelen = c;
14764 pc = &OMP_CLAUSE_CHAIN (c);
14765 continue;
14766 case OMP_CLAUSE_SIMDLEN:
14767 simdlen = c;
14768 pc = &OMP_CLAUSE_CHAIN (c);
14769 continue;
14770
14771 case OMP_CLAUSE_INBRANCH:
14772 case OMP_CLAUSE_NOTINBRANCH:
14773 if (branch_seen)
14774 {
14775 error_at (OMP_CLAUSE_LOCATION (c),
14776 "%<inbranch%> clause is incompatible with "
14777 "%<notinbranch%>");
14778 remove = true;
14779 break;
14780 }
14781 branch_seen = true;
14782 pc = &OMP_CLAUSE_CHAIN (c);
14783 continue;
14784
14785 case OMP_CLAUSE_INCLUSIVE:
14786 case OMP_CLAUSE_EXCLUSIVE:
14787 need_complete = true;
14788 need_implicitly_determined = true;
14789 t = OMP_CLAUSE_DECL (c);
14790 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14791 {
14792 error_at (OMP_CLAUSE_LOCATION (c),
14793 "%qE is not a variable in clause %qs", t,
14794 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14795 remove = true;
14796 }
14797 break;
14798
14799 default:
14800 gcc_unreachable ();
14801 }
14802
14803 if (!remove)
14804 {
14805 t = OMP_CLAUSE_DECL (c);
14806
14807 if (need_complete)
14808 {
14809 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14810 if (t == error_mark_node)
14811 remove = true;
14812 }
14813
14814 if (need_implicitly_determined)
14815 {
14816 const char *share_name = NULL;
14817
14818 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14819 share_name = "threadprivate";
14820 else switch (c_omp_predetermined_sharing (t))
14821 {
14822 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
14823 break;
14824 case OMP_CLAUSE_DEFAULT_SHARED:
14825 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
14826 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
14827 && c_omp_predefined_variable (t))
14828 /* The __func__ variable and similar function-local
14829 predefined variables may be listed in a shared or
14830 firstprivate clause. */
14831 break;
14832 share_name = "shared";
14833 break;
14834 case OMP_CLAUSE_DEFAULT_PRIVATE:
14835 share_name = "private";
14836 break;
14837 default:
14838 gcc_unreachable ();
14839 }
14840 if (share_name)
14841 {
14842 error_at (OMP_CLAUSE_LOCATION (c),
14843 "%qE is predetermined %qs for %qs",
14844 t, share_name,
14845 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14846 remove = true;
14847 }
14848 else if (TREE_READONLY (t)
14849 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
14850 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
14851 {
14852 error_at (OMP_CLAUSE_LOCATION (c),
14853 "%<const%> qualified %qE may appear only in "
14854 "%<shared%> or %<firstprivate%> clauses", t);
14855 remove = true;
14856 }
14857 }
14858 }
14859
14860 if (remove)
14861 *pc = OMP_CLAUSE_CHAIN (c);
14862 else
14863 pc = &OMP_CLAUSE_CHAIN (c);
14864 }
14865
14866 if (simdlen
14867 && safelen
14868 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
14869 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
14870 {
14871 error_at (OMP_CLAUSE_LOCATION (simdlen),
14872 "%<simdlen%> clause value is bigger than "
14873 "%<safelen%> clause value");
14874 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
14875 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
14876 }
14877
14878 if (ordered_clause
14879 && schedule_clause
14880 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14881 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14882 {
14883 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
14884 "%<nonmonotonic%> schedule modifier specified together "
14885 "with %<ordered%> clause");
14886 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14887 = (enum omp_clause_schedule_kind)
14888 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14889 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14890 }
14891
14892 if (reduction_seen < 0 && ordered_clause)
14893 {
14894 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
14895 "%qs clause specified together with %<inscan%> "
14896 "%<reduction%> clause", "ordered");
14897 reduction_seen = -2;
14898 }
14899
14900 if (reduction_seen < 0 && schedule_clause)
14901 {
14902 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
14903 "%qs clause specified together with %<inscan%> "
14904 "%<reduction%> clause", "schedule");
14905 reduction_seen = -2;
14906 }
14907
14908 if (linear_variable_step_check || reduction_seen == -2)
14909 for (pc = &clauses, c = clauses; c ; c = *pc)
14910 {
14911 bool remove = false;
14912 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14913 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
14914 && !bitmap_bit_p (&map_head,
14915 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
14916 {
14917 error_at (OMP_CLAUSE_LOCATION (c),
14918 "%<linear%> clause step is a parameter %qD not "
14919 "specified in %<uniform%> clause",
14920 OMP_CLAUSE_LINEAR_STEP (c));
14921 remove = true;
14922 }
14923 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14924 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
14925
14926 if (remove)
14927 *pc = OMP_CLAUSE_CHAIN (c);
14928 else
14929 pc = &OMP_CLAUSE_CHAIN (c);
14930 }
14931
14932 if (nogroup_seen && reduction_seen)
14933 {
14934 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
14935 "%<nogroup%> clause must not be used together with "
14936 "%<reduction%> clause");
14937 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
14938 }
14939
14940 bitmap_obstack_release (NULL);
14941 return clauses;
14942 }
14943
14944 /* Return code to initialize DST with a copy constructor from SRC.
14945 C doesn't have copy constructors nor assignment operators, only for
14946 _Atomic vars we need to perform __atomic_load from src into a temporary
14947 followed by __atomic_store of the temporary to dst. */
14948
14949 tree
14950 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
14951 {
14952 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
14953 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14954
14955 location_t loc = OMP_CLAUSE_LOCATION (clause);
14956 tree type = TREE_TYPE (dst);
14957 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
14958 tree tmp = create_tmp_var (nonatomic_type);
14959 tree tmp_addr = build_fold_addr_expr (tmp);
14960 TREE_ADDRESSABLE (tmp) = 1;
14961 TREE_NO_WARNING (tmp) = 1;
14962 tree src_addr = build_fold_addr_expr (src);
14963 tree dst_addr = build_fold_addr_expr (dst);
14964 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14965 vec<tree, va_gc> *params;
14966 /* Expansion of a generic atomic load may require an addition
14967 element, so allocate enough to prevent a resize. */
14968 vec_alloc (params, 4);
14969
14970 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14971 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14972 params->quick_push (src_addr);
14973 params->quick_push (tmp_addr);
14974 params->quick_push (seq_cst);
14975 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14976
14977 vec_alloc (params, 4);
14978
14979 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14980 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14981 params->quick_push (dst_addr);
14982 params->quick_push (tmp_addr);
14983 params->quick_push (seq_cst);
14984 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14985 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14986 }
14987
14988 /* Create a transaction node. */
14989
14990 tree
14991 c_finish_transaction (location_t loc, tree block, int flags)
14992 {
14993 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14994 if (flags & TM_STMT_ATTR_OUTER)
14995 TRANSACTION_EXPR_OUTER (stmt) = 1;
14996 if (flags & TM_STMT_ATTR_RELAXED)
14997 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14998 return add_stmt (stmt);
14999 }
15000
15001 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15002 down to the element type of an array. If ORIG_QUAL_TYPE is not
15003 NULL, then it should be used as the qualified type
15004 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15005 preserve information about the typedef name from which an array
15006 type was derived). */
15007
15008 tree
15009 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15010 size_t orig_qual_indirect)
15011 {
15012 if (type == error_mark_node)
15013 return type;
15014
15015 if (TREE_CODE (type) == ARRAY_TYPE)
15016 {
15017 tree t;
15018 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15019 type_quals, orig_qual_type,
15020 orig_qual_indirect - 1);
15021
15022 /* See if we already have an identically qualified type. */
15023 if (orig_qual_type && orig_qual_indirect == 0)
15024 t = orig_qual_type;
15025 else
15026 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15027 {
15028 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15029 && TYPE_NAME (t) == TYPE_NAME (type)
15030 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15031 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15032 TYPE_ATTRIBUTES (type)))
15033 break;
15034 }
15035 if (!t)
15036 {
15037 tree domain = TYPE_DOMAIN (type);
15038
15039 t = build_variant_type_copy (type);
15040 TREE_TYPE (t) = element_type;
15041
15042 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15043 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15044 SET_TYPE_STRUCTURAL_EQUALITY (t);
15045 else if (TYPE_CANONICAL (element_type) != element_type
15046 || (domain && TYPE_CANONICAL (domain) != domain))
15047 {
15048 tree unqualified_canon
15049 = build_array_type (TYPE_CANONICAL (element_type),
15050 domain? TYPE_CANONICAL (domain)
15051 : NULL_TREE);
15052 if (TYPE_REVERSE_STORAGE_ORDER (type))
15053 {
15054 unqualified_canon
15055 = build_distinct_type_copy (unqualified_canon);
15056 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15057 }
15058 TYPE_CANONICAL (t)
15059 = c_build_qualified_type (unqualified_canon, type_quals);
15060 }
15061 else
15062 TYPE_CANONICAL (t) = t;
15063 }
15064 return t;
15065 }
15066
15067 /* A restrict-qualified pointer type must be a pointer to object or
15068 incomplete type. Note that the use of POINTER_TYPE_P also allows
15069 REFERENCE_TYPEs, which is appropriate for C++. */
15070 if ((type_quals & TYPE_QUAL_RESTRICT)
15071 && (!POINTER_TYPE_P (type)
15072 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15073 {
15074 error ("invalid use of %<restrict%>");
15075 type_quals &= ~TYPE_QUAL_RESTRICT;
15076 }
15077
15078 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15079 ? orig_qual_type
15080 : build_qualified_type (type, type_quals));
15081 /* A variant type does not inherit the list of incomplete vars from the
15082 type main variant. */
15083 if (RECORD_OR_UNION_TYPE_P (var_type)
15084 && TYPE_MAIN_VARIANT (var_type) != var_type)
15085 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15086 return var_type;
15087 }
15088
15089 /* Build a VA_ARG_EXPR for the C parser. */
15090
15091 tree
15092 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15093 {
15094 if (error_operand_p (type))
15095 return error_mark_node;
15096 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15097 order because it takes the address of the expression. */
15098 else if (handled_component_p (expr)
15099 && reverse_storage_order_for_component_p (expr))
15100 {
15101 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15102 return error_mark_node;
15103 }
15104 else if (!COMPLETE_TYPE_P (type))
15105 {
15106 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15107 "type %qT", type);
15108 return error_mark_node;
15109 }
15110 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15111 warning_at (loc2, OPT_Wc___compat,
15112 "C++ requires promoted type, not enum type, in %<va_arg%>");
15113 return build_va_arg (loc2, expr, type);
15114 }
15115
15116 /* Return truthvalue of whether T1 is the same tree structure as T2.
15117 Return 1 if they are the same. Return false if they are different. */
15118
15119 bool
15120 c_tree_equal (tree t1, tree t2)
15121 {
15122 enum tree_code code1, code2;
15123
15124 if (t1 == t2)
15125 return true;
15126 if (!t1 || !t2)
15127 return false;
15128
15129 for (code1 = TREE_CODE (t1);
15130 CONVERT_EXPR_CODE_P (code1)
15131 || code1 == NON_LVALUE_EXPR;
15132 code1 = TREE_CODE (t1))
15133 t1 = TREE_OPERAND (t1, 0);
15134 for (code2 = TREE_CODE (t2);
15135 CONVERT_EXPR_CODE_P (code2)
15136 || code2 == NON_LVALUE_EXPR;
15137 code2 = TREE_CODE (t2))
15138 t2 = TREE_OPERAND (t2, 0);
15139
15140 /* They might have become equal now. */
15141 if (t1 == t2)
15142 return true;
15143
15144 if (code1 != code2)
15145 return false;
15146
15147 switch (code1)
15148 {
15149 case INTEGER_CST:
15150 return wi::to_wide (t1) == wi::to_wide (t2);
15151
15152 case REAL_CST:
15153 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15154
15155 case STRING_CST:
15156 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15157 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15158 TREE_STRING_LENGTH (t1));
15159
15160 case FIXED_CST:
15161 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15162 TREE_FIXED_CST (t2));
15163
15164 case COMPLEX_CST:
15165 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15166 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15167
15168 case VECTOR_CST:
15169 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15170
15171 case CONSTRUCTOR:
15172 /* We need to do this when determining whether or not two
15173 non-type pointer to member function template arguments
15174 are the same. */
15175 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15176 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15177 return false;
15178 {
15179 tree field, value;
15180 unsigned int i;
15181 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15182 {
15183 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15184 if (!c_tree_equal (field, elt2->index)
15185 || !c_tree_equal (value, elt2->value))
15186 return false;
15187 }
15188 }
15189 return true;
15190
15191 case TREE_LIST:
15192 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15193 return false;
15194 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15195 return false;
15196 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15197
15198 case SAVE_EXPR:
15199 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15200
15201 case CALL_EXPR:
15202 {
15203 tree arg1, arg2;
15204 call_expr_arg_iterator iter1, iter2;
15205 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15206 return false;
15207 for (arg1 = first_call_expr_arg (t1, &iter1),
15208 arg2 = first_call_expr_arg (t2, &iter2);
15209 arg1 && arg2;
15210 arg1 = next_call_expr_arg (&iter1),
15211 arg2 = next_call_expr_arg (&iter2))
15212 if (!c_tree_equal (arg1, arg2))
15213 return false;
15214 if (arg1 || arg2)
15215 return false;
15216 return true;
15217 }
15218
15219 case TARGET_EXPR:
15220 {
15221 tree o1 = TREE_OPERAND (t1, 0);
15222 tree o2 = TREE_OPERAND (t2, 0);
15223
15224 /* Special case: if either target is an unallocated VAR_DECL,
15225 it means that it's going to be unified with whatever the
15226 TARGET_EXPR is really supposed to initialize, so treat it
15227 as being equivalent to anything. */
15228 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
15229 && !DECL_RTL_SET_P (o1))
15230 /*Nop*/;
15231 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
15232 && !DECL_RTL_SET_P (o2))
15233 /*Nop*/;
15234 else if (!c_tree_equal (o1, o2))
15235 return false;
15236
15237 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15238 }
15239
15240 case COMPONENT_REF:
15241 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15242 return false;
15243 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15244
15245 case PARM_DECL:
15246 case VAR_DECL:
15247 case CONST_DECL:
15248 case FIELD_DECL:
15249 case FUNCTION_DECL:
15250 case IDENTIFIER_NODE:
15251 case SSA_NAME:
15252 return false;
15253
15254 case TREE_VEC:
15255 {
15256 unsigned ix;
15257 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15258 return false;
15259 for (ix = TREE_VEC_LENGTH (t1); ix--;)
15260 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15261 TREE_VEC_ELT (t2, ix)))
15262 return false;
15263 return true;
15264 }
15265
15266 default:
15267 break;
15268 }
15269
15270 switch (TREE_CODE_CLASS (code1))
15271 {
15272 case tcc_unary:
15273 case tcc_binary:
15274 case tcc_comparison:
15275 case tcc_expression:
15276 case tcc_vl_exp:
15277 case tcc_reference:
15278 case tcc_statement:
15279 {
15280 int i, n = TREE_OPERAND_LENGTH (t1);
15281
15282 switch (code1)
15283 {
15284 case PREINCREMENT_EXPR:
15285 case PREDECREMENT_EXPR:
15286 case POSTINCREMENT_EXPR:
15287 case POSTDECREMENT_EXPR:
15288 n = 1;
15289 break;
15290 case ARRAY_REF:
15291 n = 2;
15292 break;
15293 default:
15294 break;
15295 }
15296
15297 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15298 && n != TREE_OPERAND_LENGTH (t2))
15299 return false;
15300
15301 for (i = 0; i < n; ++i)
15302 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15303 return false;
15304
15305 return true;
15306 }
15307
15308 case tcc_type:
15309 return comptypes (t1, t2);
15310 default:
15311 gcc_unreachable ();
15312 }
15313 /* We can get here with --disable-checking. */
15314 return false;
15315 }
15316
15317 /* Returns true when the function declaration FNDECL is implicit,
15318 introduced as a result of a call to an otherwise undeclared
15319 function, and false otherwise. */
15320
15321 bool
15322 c_decl_implicit (const_tree fndecl)
15323 {
15324 return C_DECL_IMPLICIT (fndecl);
15325 }