]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-typeck.c
Come up with fndecl_built_in_p.
[thirdparty/gcc.git] / gcc / c / c-typeck.c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2018 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 static tree valid_compound_expr_initializer (tree, tree);
102 static void push_string (const char *);
103 static void push_member_name (tree);
104 static int spelling_length (void);
105 static char *print_spelling (char *);
106 static void warning_init (location_t, int, const char *);
107 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
108 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
109 bool, struct obstack *);
110 static void output_pending_init_elements (int, struct obstack *);
111 static bool set_designator (location_t, bool, struct obstack *);
112 static void push_range_stack (tree, struct obstack *);
113 static void add_pending_init (location_t, tree, tree, tree, bool,
114 struct obstack *);
115 static void set_nonincremental_init (struct obstack *);
116 static void set_nonincremental_init_from_string (tree, struct obstack *);
117 static tree find_init_member (tree, struct obstack *);
118 static void readonly_warning (tree, enum lvalue_use);
119 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
120 static void record_maybe_used_decl (tree);
121 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
122 \f
123 /* Return true if EXP is a null pointer constant, false otherwise. */
124
125 static bool
126 null_pointer_constant_p (const_tree expr)
127 {
128 /* This should really operate on c_expr structures, but they aren't
129 yet available everywhere required. */
130 tree type = TREE_TYPE (expr);
131 return (TREE_CODE (expr) == INTEGER_CST
132 && !TREE_OVERFLOW (expr)
133 && integer_zerop (expr)
134 && (INTEGRAL_TYPE_P (type)
135 || (TREE_CODE (type) == POINTER_TYPE
136 && VOID_TYPE_P (TREE_TYPE (type))
137 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
138 }
139
140 /* EXPR may appear in an unevaluated part of an integer constant
141 expression, but not in an evaluated part. Wrap it in a
142 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
143 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
144
145 static tree
146 note_integer_operands (tree expr)
147 {
148 tree ret;
149 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
150 {
151 ret = copy_node (expr);
152 TREE_OVERFLOW (ret) = 1;
153 }
154 else
155 {
156 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
157 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
158 }
159 return ret;
160 }
161
162 /* Having checked whether EXPR may appear in an unevaluated part of an
163 integer constant expression and found that it may, remove any
164 C_MAYBE_CONST_EXPR noting this fact and return the resulting
165 expression. */
166
167 static inline tree
168 remove_c_maybe_const_expr (tree expr)
169 {
170 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
171 return C_MAYBE_CONST_EXPR_EXPR (expr);
172 else
173 return expr;
174 }
175
176 \f/* This is a cache to hold if two types are compatible or not. */
177
178 struct tagged_tu_seen_cache {
179 const struct tagged_tu_seen_cache * next;
180 const_tree t1;
181 const_tree t2;
182 /* The return value of tagged_types_tu_compatible_p if we had seen
183 these two types already. */
184 int val;
185 };
186
187 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
188 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
189
190 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
191 does not have an incomplete type. (That includes void types.)
192 LOC is the location of the use. */
193
194 tree
195 require_complete_type (location_t loc, tree value)
196 {
197 tree type = TREE_TYPE (value);
198
199 if (error_operand_p (value))
200 return error_mark_node;
201
202 /* First, detect a valid value with a complete type. */
203 if (COMPLETE_TYPE_P (type))
204 return value;
205
206 c_incomplete_type_error (loc, value, type);
207 return error_mark_node;
208 }
209
210 /* Print an error message for invalid use of an incomplete type.
211 VALUE is the expression that was used (or 0 if that isn't known)
212 and TYPE is the type that was invalid. LOC is the location for
213 the error. */
214
215 void
216 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
217 {
218 /* Avoid duplicate error message. */
219 if (TREE_CODE (type) == ERROR_MARK)
220 return;
221
222 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
223 error_at (loc, "%qD has an incomplete type %qT", value, type);
224 else
225 {
226 retry:
227 /* We must print an error message. Be clever about what it says. */
228
229 switch (TREE_CODE (type))
230 {
231 case RECORD_TYPE:
232 case UNION_TYPE:
233 case ENUMERAL_TYPE:
234 break;
235
236 case VOID_TYPE:
237 error_at (loc, "invalid use of void expression");
238 return;
239
240 case ARRAY_TYPE:
241 if (TYPE_DOMAIN (type))
242 {
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
244 {
245 error_at (loc, "invalid use of flexible array member");
246 return;
247 }
248 type = TREE_TYPE (type);
249 goto retry;
250 }
251 error_at (loc, "invalid use of array with unspecified bounds");
252 return;
253
254 default:
255 gcc_unreachable ();
256 }
257
258 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
259 error_at (loc, "invalid use of undefined type %qT", type);
260 else
261 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
262 error_at (loc, "invalid use of incomplete typedef %qT", type);
263 }
264 }
265
266 /* Given a type, apply default promotions wrt unnamed function
267 arguments and return the new type. */
268
269 tree
270 c_type_promotes_to (tree type)
271 {
272 tree ret = NULL_TREE;
273
274 if (TYPE_MAIN_VARIANT (type) == float_type_node)
275 ret = double_type_node;
276 else if (c_promoting_integer_type_p (type))
277 {
278 /* Preserve unsignedness if not really getting any wider. */
279 if (TYPE_UNSIGNED (type)
280 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
281 ret = unsigned_type_node;
282 else
283 ret = integer_type_node;
284 }
285
286 if (ret != NULL_TREE)
287 return (TYPE_ATOMIC (type)
288 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
289 : ret);
290
291 return type;
292 }
293
294 /* Return true if between two named address spaces, whether there is a superset
295 named address space that encompasses both address spaces. If there is a
296 superset, return which address space is the superset. */
297
298 static bool
299 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
300 {
301 if (as1 == as2)
302 {
303 *common = as1;
304 return true;
305 }
306 else if (targetm.addr_space.subset_p (as1, as2))
307 {
308 *common = as2;
309 return true;
310 }
311 else if (targetm.addr_space.subset_p (as2, as1))
312 {
313 *common = as1;
314 return true;
315 }
316 else
317 return false;
318 }
319
320 /* Return a variant of TYPE which has all the type qualifiers of LIKE
321 as well as those of TYPE. */
322
323 static tree
324 qualify_type (tree type, tree like)
325 {
326 addr_space_t as_type = TYPE_ADDR_SPACE (type);
327 addr_space_t as_like = TYPE_ADDR_SPACE (like);
328 addr_space_t as_common;
329
330 /* If the two named address spaces are different, determine the common
331 superset address space. If there isn't one, raise an error. */
332 if (!addr_space_superset (as_type, as_like, &as_common))
333 {
334 as_common = as_type;
335 error ("%qT and %qT are in disjoint named address spaces",
336 type, like);
337 }
338
339 return c_build_qualified_type (type,
340 TYPE_QUALS_NO_ADDR_SPACE (type)
341 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
342 | ENCODE_QUAL_ADDR_SPACE (as_common));
343 }
344
345 /* Return true iff the given tree T is a variable length array. */
346
347 bool
348 c_vla_type_p (const_tree t)
349 {
350 if (TREE_CODE (t) == ARRAY_TYPE
351 && C_TYPE_VARIABLE_SIZE (t))
352 return true;
353 return false;
354 }
355 \f
356 /* Return the composite type of two compatible types.
357
358 We assume that comptypes has already been done and returned
359 nonzero; if that isn't so, this may crash. In particular, we
360 assume that qualifiers match. */
361
362 tree
363 composite_type (tree t1, tree t2)
364 {
365 enum tree_code code1;
366 enum tree_code code2;
367 tree attributes;
368
369 /* Save time if the two types are the same. */
370
371 if (t1 == t2) return t1;
372
373 /* If one type is nonsense, use the other. */
374 if (t1 == error_mark_node)
375 return t2;
376 if (t2 == error_mark_node)
377 return t1;
378
379 code1 = TREE_CODE (t1);
380 code2 = TREE_CODE (t2);
381
382 /* Merge the attributes. */
383 attributes = targetm.merge_type_attributes (t1, t2);
384
385 /* If one is an enumerated type and the other is the compatible
386 integer type, the composite type might be either of the two
387 (DR#013 question 3). For consistency, use the enumerated type as
388 the composite type. */
389
390 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
391 return t1;
392 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
393 return t2;
394
395 gcc_assert (code1 == code2);
396
397 switch (code1)
398 {
399 case POINTER_TYPE:
400 /* For two pointers, do this recursively on the target type. */
401 {
402 tree pointed_to_1 = TREE_TYPE (t1);
403 tree pointed_to_2 = TREE_TYPE (t2);
404 tree target = composite_type (pointed_to_1, pointed_to_2);
405 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
406 t1 = build_type_attribute_variant (t1, attributes);
407 return qualify_type (t1, t2);
408 }
409
410 case ARRAY_TYPE:
411 {
412 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
413 int quals;
414 tree unqual_elt;
415 tree d1 = TYPE_DOMAIN (t1);
416 tree d2 = TYPE_DOMAIN (t2);
417 bool d1_variable, d2_variable;
418 bool d1_zero, d2_zero;
419 bool t1_complete, t2_complete;
420
421 /* We should not have any type quals on arrays at all. */
422 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
423 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
424
425 t1_complete = COMPLETE_TYPE_P (t1);
426 t2_complete = COMPLETE_TYPE_P (t2);
427
428 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
429 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
430
431 d1_variable = (!d1_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
434 d2_variable = (!d2_zero
435 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
436 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
437 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
438 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
439
440 /* Save space: see if the result is identical to one of the args. */
441 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
442 && (d2_variable || d2_zero || !d1_variable))
443 return build_type_attribute_variant (t1, attributes);
444 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
445 && (d1_variable || d1_zero || !d2_variable))
446 return build_type_attribute_variant (t2, attributes);
447
448 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
449 return build_type_attribute_variant (t1, attributes);
450 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
451 return build_type_attribute_variant (t2, attributes);
452
453 /* Merge the element types, and have a size if either arg has
454 one. We may have qualifiers on the element types. To set
455 up TYPE_MAIN_VARIANT correctly, we need to form the
456 composite of the unqualified types and add the qualifiers
457 back at the end. */
458 quals = TYPE_QUALS (strip_array_types (elt));
459 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
460 t1 = build_array_type (unqual_elt,
461 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
462 && (d2_variable
463 || d2_zero
464 || !d1_variable))
465 ? t1
466 : t2));
467 /* Ensure a composite type involving a zero-length array type
468 is a zero-length type not an incomplete type. */
469 if (d1_zero && d2_zero
470 && (t1_complete || t2_complete)
471 && !COMPLETE_TYPE_P (t1))
472 {
473 TYPE_SIZE (t1) = bitsize_zero_node;
474 TYPE_SIZE_UNIT (t1) = size_zero_node;
475 }
476 t1 = c_build_qualified_type (t1, quals);
477 return build_type_attribute_variant (t1, attributes);
478 }
479
480 case ENUMERAL_TYPE:
481 case RECORD_TYPE:
482 case UNION_TYPE:
483 if (attributes != NULL)
484 {
485 /* Try harder not to create a new aggregate type. */
486 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
487 return t1;
488 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
489 return t2;
490 }
491 return build_type_attribute_variant (t1, attributes);
492
493 case FUNCTION_TYPE:
494 /* Function types: prefer the one that specified arg types.
495 If both do, merge the arg types. Also merge the return types. */
496 {
497 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
498 tree p1 = TYPE_ARG_TYPES (t1);
499 tree p2 = TYPE_ARG_TYPES (t2);
500 int len;
501 tree newargs, n;
502 int i;
503
504 /* Save space: see if the result is identical to one of the args. */
505 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
506 return build_type_attribute_variant (t1, attributes);
507 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
508 return build_type_attribute_variant (t2, attributes);
509
510 /* Simple way if one arg fails to specify argument types. */
511 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
512 {
513 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
514 t1 = build_type_attribute_variant (t1, attributes);
515 return qualify_type (t1, t2);
516 }
517 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
518 {
519 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
520 t1 = build_type_attribute_variant (t1, attributes);
521 return qualify_type (t1, t2);
522 }
523
524 /* If both args specify argument types, we must merge the two
525 lists, argument by argument. */
526
527 for (len = 0, newargs = p1;
528 newargs && newargs != void_list_node;
529 len++, newargs = TREE_CHAIN (newargs))
530 ;
531
532 for (i = 0; i < len; i++)
533 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
534
535 n = newargs;
536
537 for (; p1 && p1 != void_list_node;
538 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
539 {
540 /* A null type means arg type is not specified.
541 Take whatever the other function type has. */
542 if (TREE_VALUE (p1) == NULL_TREE)
543 {
544 TREE_VALUE (n) = TREE_VALUE (p2);
545 goto parm_done;
546 }
547 if (TREE_VALUE (p2) == NULL_TREE)
548 {
549 TREE_VALUE (n) = TREE_VALUE (p1);
550 goto parm_done;
551 }
552
553 /* Given wait (union {union wait *u; int *i} *)
554 and wait (union wait *),
555 prefer union wait * as type of parm. */
556 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
557 && TREE_VALUE (p1) != TREE_VALUE (p2))
558 {
559 tree memb;
560 tree mv2 = TREE_VALUE (p2);
561 if (mv2 && mv2 != error_mark_node
562 && TREE_CODE (mv2) != ARRAY_TYPE)
563 mv2 = TYPE_MAIN_VARIANT (mv2);
564 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
565 memb; memb = DECL_CHAIN (memb))
566 {
567 tree mv3 = TREE_TYPE (memb);
568 if (mv3 && mv3 != error_mark_node
569 && TREE_CODE (mv3) != ARRAY_TYPE)
570 mv3 = TYPE_MAIN_VARIANT (mv3);
571 if (comptypes (mv3, mv2))
572 {
573 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
574 TREE_VALUE (p2));
575 pedwarn (input_location, OPT_Wpedantic,
576 "function types not truly compatible in ISO C");
577 goto parm_done;
578 }
579 }
580 }
581 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
582 && TREE_VALUE (p2) != TREE_VALUE (p1))
583 {
584 tree memb;
585 tree mv1 = TREE_VALUE (p1);
586 if (mv1 && mv1 != error_mark_node
587 && TREE_CODE (mv1) != ARRAY_TYPE)
588 mv1 = TYPE_MAIN_VARIANT (mv1);
589 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
590 memb; memb = DECL_CHAIN (memb))
591 {
592 tree mv3 = TREE_TYPE (memb);
593 if (mv3 && mv3 != error_mark_node
594 && TREE_CODE (mv3) != ARRAY_TYPE)
595 mv3 = TYPE_MAIN_VARIANT (mv3);
596 if (comptypes (mv3, mv1))
597 {
598 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
599 TREE_VALUE (p1));
600 pedwarn (input_location, OPT_Wpedantic,
601 "function types not truly compatible in ISO C");
602 goto parm_done;
603 }
604 }
605 }
606 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
607 parm_done: ;
608 }
609
610 t1 = build_function_type (valtype, newargs);
611 t1 = qualify_type (t1, t2);
612 }
613 /* FALLTHRU */
614
615 default:
616 return build_type_attribute_variant (t1, attributes);
617 }
618
619 }
620
621 /* Return the type of a conditional expression between pointers to
622 possibly differently qualified versions of compatible types.
623
624 We assume that comp_target_types has already been done and returned
625 nonzero; if that isn't so, this may crash. */
626
627 static tree
628 common_pointer_type (tree t1, tree t2)
629 {
630 tree attributes;
631 tree pointed_to_1, mv1;
632 tree pointed_to_2, mv2;
633 tree target;
634 unsigned target_quals;
635 addr_space_t as1, as2, as_common;
636 int quals1, quals2;
637
638 /* Save time if the two types are the same. */
639
640 if (t1 == t2) return t1;
641
642 /* If one type is nonsense, use the other. */
643 if (t1 == error_mark_node)
644 return t2;
645 if (t2 == error_mark_node)
646 return t1;
647
648 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
649 && TREE_CODE (t2) == POINTER_TYPE);
650
651 /* Merge the attributes. */
652 attributes = targetm.merge_type_attributes (t1, t2);
653
654 /* Find the composite type of the target types, and combine the
655 qualifiers of the two types' targets. Do not lose qualifiers on
656 array element types by taking the TYPE_MAIN_VARIANT. */
657 mv1 = pointed_to_1 = TREE_TYPE (t1);
658 mv2 = pointed_to_2 = TREE_TYPE (t2);
659 if (TREE_CODE (mv1) != ARRAY_TYPE)
660 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
661 if (TREE_CODE (mv2) != ARRAY_TYPE)
662 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
663 target = composite_type (mv1, mv2);
664
665 /* Strip array types to get correct qualifier for pointers to arrays */
666 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
667 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
668
669 /* For function types do not merge const qualifiers, but drop them
670 if used inconsistently. The middle-end uses these to mark const
671 and noreturn functions. */
672 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
673 target_quals = (quals1 & quals2);
674 else
675 target_quals = (quals1 | quals2);
676
677 /* If the two named address spaces are different, determine the common
678 superset address space. This is guaranteed to exist due to the
679 assumption that comp_target_type returned non-zero. */
680 as1 = TYPE_ADDR_SPACE (pointed_to_1);
681 as2 = TYPE_ADDR_SPACE (pointed_to_2);
682 if (!addr_space_superset (as1, as2, &as_common))
683 gcc_unreachable ();
684
685 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
686
687 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
688 return build_type_attribute_variant (t1, attributes);
689 }
690
691 /* Return the common type for two arithmetic types under the usual
692 arithmetic conversions. The default conversions have already been
693 applied, and enumerated types converted to their compatible integer
694 types. The resulting type is unqualified and has no attributes.
695
696 This is the type for the result of most arithmetic operations
697 if the operands have the given two types. */
698
699 static tree
700 c_common_type (tree t1, tree t2)
701 {
702 enum tree_code code1;
703 enum tree_code code2;
704
705 /* If one type is nonsense, use the other. */
706 if (t1 == error_mark_node)
707 return t2;
708 if (t2 == error_mark_node)
709 return t1;
710
711 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
712 t1 = TYPE_MAIN_VARIANT (t1);
713
714 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
715 t2 = TYPE_MAIN_VARIANT (t2);
716
717 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
718 t1 = build_type_attribute_variant (t1, NULL_TREE);
719
720 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
721 t2 = build_type_attribute_variant (t2, NULL_TREE);
722
723 /* Save time if the two types are the same. */
724
725 if (t1 == t2) return t1;
726
727 code1 = TREE_CODE (t1);
728 code2 = TREE_CODE (t2);
729
730 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
731 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
732 || code1 == INTEGER_TYPE);
733 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
734 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
735 || code2 == INTEGER_TYPE);
736
737 /* When one operand is a decimal float type, the other operand cannot be
738 a generic float type or a complex type. We also disallow vector types
739 here. */
740 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
741 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
742 {
743 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
744 {
745 error ("can%'t mix operands of decimal float and vector types");
746 return error_mark_node;
747 }
748 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
749 {
750 error ("can%'t mix operands of decimal float and complex types");
751 return error_mark_node;
752 }
753 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
754 {
755 error ("can%'t mix operands of decimal float and other float types");
756 return error_mark_node;
757 }
758 }
759
760 /* If one type is a vector type, return that type. (How the usual
761 arithmetic conversions apply to the vector types extension is not
762 precisely specified.) */
763 if (code1 == VECTOR_TYPE)
764 return t1;
765
766 if (code2 == VECTOR_TYPE)
767 return t2;
768
769 /* If one type is complex, form the common type of the non-complex
770 components, then make that complex. Use T1 or T2 if it is the
771 required type. */
772 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
773 {
774 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
775 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
776 tree subtype = c_common_type (subtype1, subtype2);
777
778 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
779 return t1;
780 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
781 return t2;
782 else
783 return build_complex_type (subtype);
784 }
785
786 /* If only one is real, use it as the result. */
787
788 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
789 return t1;
790
791 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
792 return t2;
793
794 /* If both are real and either are decimal floating point types, use
795 the decimal floating point type with the greater precision. */
796
797 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
798 {
799 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
801 return dfloat128_type_node;
802 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
804 return dfloat64_type_node;
805 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
806 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
807 return dfloat32_type_node;
808 }
809
810 /* Deal with fixed-point types. */
811 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
812 {
813 unsigned int unsignedp = 0, satp = 0;
814 scalar_mode m1, m2;
815 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
816
817 m1 = SCALAR_TYPE_MODE (t1);
818 m2 = SCALAR_TYPE_MODE (t2);
819
820 /* If one input type is saturating, the result type is saturating. */
821 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
822 satp = 1;
823
824 /* If both fixed-point types are unsigned, the result type is unsigned.
825 When mixing fixed-point and integer types, follow the sign of the
826 fixed-point type.
827 Otherwise, the result type is signed. */
828 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
829 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
830 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
831 && TYPE_UNSIGNED (t1))
832 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t2)))
834 unsignedp = 1;
835
836 /* The result type is signed. */
837 if (unsignedp == 0)
838 {
839 /* If the input type is unsigned, we need to convert to the
840 signed type. */
841 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
842 {
843 enum mode_class mclass = (enum mode_class) 0;
844 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
845 mclass = MODE_FRACT;
846 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
847 mclass = MODE_ACCUM;
848 else
849 gcc_unreachable ();
850 m1 = as_a <scalar_mode>
851 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
852 }
853 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
854 {
855 enum mode_class mclass = (enum mode_class) 0;
856 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
857 mclass = MODE_FRACT;
858 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
859 mclass = MODE_ACCUM;
860 else
861 gcc_unreachable ();
862 m2 = as_a <scalar_mode>
863 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
864 }
865 }
866
867 if (code1 == FIXED_POINT_TYPE)
868 {
869 fbit1 = GET_MODE_FBIT (m1);
870 ibit1 = GET_MODE_IBIT (m1);
871 }
872 else
873 {
874 fbit1 = 0;
875 /* Signed integers need to subtract one sign bit. */
876 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
877 }
878
879 if (code2 == FIXED_POINT_TYPE)
880 {
881 fbit2 = GET_MODE_FBIT (m2);
882 ibit2 = GET_MODE_IBIT (m2);
883 }
884 else
885 {
886 fbit2 = 0;
887 /* Signed integers need to subtract one sign bit. */
888 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
889 }
890
891 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
892 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
893 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
894 satp);
895 }
896
897 /* Both real or both integers; use the one with greater precision. */
898
899 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
900 return t1;
901 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
902 return t2;
903
904 /* Same precision. Prefer long longs to longs to ints when the
905 same precision, following the C99 rules on integer type rank
906 (which are equivalent to the C90 rules for C90 types). */
907
908 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
910 return long_long_unsigned_type_node;
911
912 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
914 {
915 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
916 return long_long_unsigned_type_node;
917 else
918 return long_long_integer_type_node;
919 }
920
921 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
923 return long_unsigned_type_node;
924
925 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
926 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
927 {
928 /* But preserve unsignedness from the other type,
929 since long cannot hold all the values of an unsigned int. */
930 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
931 return long_unsigned_type_node;
932 else
933 return long_integer_type_node;
934 }
935
936 /* For floating types of the same TYPE_PRECISION (which we here
937 assume means either the same set of values, or sets of values
938 neither a subset of the other, with behavior being undefined in
939 the latter case), follow the rules from TS 18661-3: prefer
940 interchange types _FloatN, then standard types long double,
941 double, float, then extended types _FloatNx. For extended types,
942 check them starting with _Float128x as that seems most consistent
943 in spirit with preferring long double to double; for interchange
944 types, also check in that order for consistency although it's not
945 possible for more than one of them to have the same
946 precision. */
947 tree mv1 = TYPE_MAIN_VARIANT (t1);
948 tree mv2 = TYPE_MAIN_VARIANT (t2);
949
950 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
951 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
952 return FLOATN_TYPE_NODE (i);
953
954 /* Likewise, prefer long double to double even if same size. */
955 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
956 return long_double_type_node;
957
958 /* Likewise, prefer double to float even if same size.
959 We got a couple of embedded targets with 32 bit doubles, and the
960 pdp11 might have 64 bit floats. */
961 if (mv1 == double_type_node || mv2 == double_type_node)
962 return double_type_node;
963
964 if (mv1 == float_type_node || mv2 == float_type_node)
965 return float_type_node;
966
967 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
968 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
969 return FLOATNX_TYPE_NODE (i);
970
971 /* Otherwise prefer the unsigned one. */
972
973 if (TYPE_UNSIGNED (t1))
974 return t1;
975 else
976 return t2;
977 }
978 \f
979 /* Wrapper around c_common_type that is used by c-common.c and other
980 front end optimizations that remove promotions. ENUMERAL_TYPEs
981 are allowed here and are converted to their compatible integer types.
982 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
983 preferably a non-Boolean type as the common type. */
984 tree
985 common_type (tree t1, tree t2)
986 {
987 if (TREE_CODE (t1) == ENUMERAL_TYPE)
988 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
989 if (TREE_CODE (t2) == ENUMERAL_TYPE)
990 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
991
992 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
993 if (TREE_CODE (t1) == BOOLEAN_TYPE
994 && TREE_CODE (t2) == BOOLEAN_TYPE)
995 return boolean_type_node;
996
997 /* If either type is BOOLEAN_TYPE, then return the other. */
998 if (TREE_CODE (t1) == BOOLEAN_TYPE)
999 return t2;
1000 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1001 return t1;
1002
1003 return c_common_type (t1, t2);
1004 }
1005
1006 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1007 or various other operations. Return 2 if they are compatible
1008 but a warning may be needed if you use them together. */
1009
1010 int
1011 comptypes (tree type1, tree type2)
1012 {
1013 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1014 int val;
1015
1016 val = comptypes_internal (type1, type2, NULL, NULL);
1017 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1018
1019 return val;
1020 }
1021
1022 /* Like comptypes, but if it returns non-zero because enum and int are
1023 compatible, it sets *ENUM_AND_INT_P to true. */
1024
1025 static int
1026 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1027 {
1028 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1029 int val;
1030
1031 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1032 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1033
1034 return val;
1035 }
1036
1037 /* Like comptypes, but if it returns nonzero for different types, it
1038 sets *DIFFERENT_TYPES_P to true. */
1039
1040 int
1041 comptypes_check_different_types (tree type1, tree type2,
1042 bool *different_types_p)
1043 {
1044 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1045 int val;
1046
1047 val = comptypes_internal (type1, type2, NULL, different_types_p);
1048 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1049
1050 return val;
1051 }
1052 \f
1053 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1054 or various other operations. Return 2 if they are compatible
1055 but a warning may be needed if you use them together. If
1056 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1057 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1058 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1059 NULL, and the types are compatible but different enough not to be
1060 permitted in C11 typedef redeclarations, then this sets
1061 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1062 false, but may or may not be set if the types are incompatible.
1063 This differs from comptypes, in that we don't free the seen
1064 types. */
1065
1066 static int
1067 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1068 bool *different_types_p)
1069 {
1070 const_tree t1 = type1;
1071 const_tree t2 = type2;
1072 int attrval, val;
1073
1074 /* Suppress errors caused by previously reported errors. */
1075
1076 if (t1 == t2 || !t1 || !t2
1077 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1078 return 1;
1079
1080 /* Enumerated types are compatible with integer types, but this is
1081 not transitive: two enumerated types in the same translation unit
1082 are compatible with each other only if they are the same type. */
1083
1084 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1085 {
1086 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1087 if (TREE_CODE (t2) != VOID_TYPE)
1088 {
1089 if (enum_and_int_p != NULL)
1090 *enum_and_int_p = true;
1091 if (different_types_p != NULL)
1092 *different_types_p = true;
1093 }
1094 }
1095 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1096 {
1097 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1098 if (TREE_CODE (t1) != VOID_TYPE)
1099 {
1100 if (enum_and_int_p != NULL)
1101 *enum_and_int_p = true;
1102 if (different_types_p != NULL)
1103 *different_types_p = true;
1104 }
1105 }
1106
1107 if (t1 == t2)
1108 return 1;
1109
1110 /* Different classes of types can't be compatible. */
1111
1112 if (TREE_CODE (t1) != TREE_CODE (t2))
1113 return 0;
1114
1115 /* Qualifiers must match. C99 6.7.3p9 */
1116
1117 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1118 return 0;
1119
1120 /* Allow for two different type nodes which have essentially the same
1121 definition. Note that we already checked for equality of the type
1122 qualifiers (just above). */
1123
1124 if (TREE_CODE (t1) != ARRAY_TYPE
1125 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1126 return 1;
1127
1128 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1129 if (!(attrval = comp_type_attributes (t1, t2)))
1130 return 0;
1131
1132 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1133 val = 0;
1134
1135 switch (TREE_CODE (t1))
1136 {
1137 case INTEGER_TYPE:
1138 case FIXED_POINT_TYPE:
1139 case REAL_TYPE:
1140 /* With these nodes, we can't determine type equivalence by
1141 looking at what is stored in the nodes themselves, because
1142 two nodes might have different TYPE_MAIN_VARIANTs but still
1143 represent the same type. For example, wchar_t and int could
1144 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1145 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1146 and are distinct types. On the other hand, int and the
1147 following typedef
1148
1149 typedef int INT __attribute((may_alias));
1150
1151 have identical properties, different TYPE_MAIN_VARIANTs, but
1152 represent the same type. The canonical type system keeps
1153 track of equivalence in this case, so we fall back on it. */
1154 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1155
1156 case POINTER_TYPE:
1157 /* Do not remove mode information. */
1158 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1159 break;
1160 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1161 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1162 enum_and_int_p, different_types_p));
1163 break;
1164
1165 case FUNCTION_TYPE:
1166 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1167 different_types_p);
1168 break;
1169
1170 case ARRAY_TYPE:
1171 {
1172 tree d1 = TYPE_DOMAIN (t1);
1173 tree d2 = TYPE_DOMAIN (t2);
1174 bool d1_variable, d2_variable;
1175 bool d1_zero, d2_zero;
1176 val = 1;
1177
1178 /* Target types must match incl. qualifiers. */
1179 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1180 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181 enum_and_int_p,
1182 different_types_p)) == 0)
1183 return 0;
1184
1185 if (different_types_p != NULL
1186 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1187 *different_types_p = true;
1188 /* Sizes must match unless one is missing or variable. */
1189 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1190 break;
1191
1192 d1_zero = !TYPE_MAX_VALUE (d1);
1193 d2_zero = !TYPE_MAX_VALUE (d2);
1194
1195 d1_variable = (!d1_zero
1196 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1197 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1198 d2_variable = (!d2_zero
1199 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1200 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1201 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1202 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1203
1204 if (different_types_p != NULL
1205 && d1_variable != d2_variable)
1206 *different_types_p = true;
1207 if (d1_variable || d2_variable)
1208 break;
1209 if (d1_zero && d2_zero)
1210 break;
1211 if (d1_zero || d2_zero
1212 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1213 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1214 val = 0;
1215
1216 break;
1217 }
1218
1219 case ENUMERAL_TYPE:
1220 case RECORD_TYPE:
1221 case UNION_TYPE:
1222 if (val != 1 && !same_translation_unit_p (t1, t2))
1223 {
1224 tree a1 = TYPE_ATTRIBUTES (t1);
1225 tree a2 = TYPE_ATTRIBUTES (t2);
1226
1227 if (! attribute_list_contained (a1, a2)
1228 && ! attribute_list_contained (a2, a1))
1229 break;
1230
1231 if (attrval != 2)
1232 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1233 different_types_p);
1234 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1235 different_types_p);
1236 }
1237 break;
1238
1239 case VECTOR_TYPE:
1240 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1241 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1242 enum_and_int_p, different_types_p));
1243 break;
1244
1245 default:
1246 break;
1247 }
1248 return attrval == 2 && val == 1 ? 2 : val;
1249 }
1250
1251 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1252 their qualifiers, except for named address spaces. If the pointers point to
1253 different named addresses, then we must determine if one address space is a
1254 subset of the other. */
1255
1256 static int
1257 comp_target_types (location_t location, tree ttl, tree ttr)
1258 {
1259 int val;
1260 int val_ped;
1261 tree mvl = TREE_TYPE (ttl);
1262 tree mvr = TREE_TYPE (ttr);
1263 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1264 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1265 addr_space_t as_common;
1266 bool enum_and_int_p;
1267
1268 /* Fail if pointers point to incompatible address spaces. */
1269 if (!addr_space_superset (asl, asr, &as_common))
1270 return 0;
1271
1272 /* For pedantic record result of comptypes on arrays before losing
1273 qualifiers on the element type below. */
1274 val_ped = 1;
1275
1276 if (TREE_CODE (mvl) == ARRAY_TYPE
1277 && TREE_CODE (mvr) == ARRAY_TYPE)
1278 val_ped = comptypes (mvl, mvr);
1279
1280 /* Qualifiers on element types of array types that are
1281 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1282
1283 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1284 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1285 : TYPE_MAIN_VARIANT (mvl));
1286
1287 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1288 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1289 : TYPE_MAIN_VARIANT (mvr));
1290
1291 enum_and_int_p = false;
1292 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1293
1294 if (val == 1 && val_ped != 1)
1295 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1296 "are incompatible in ISO C");
1297
1298 if (val == 2)
1299 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1300
1301 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1302 warning_at (location, OPT_Wc___compat,
1303 "pointer target types incompatible in C++");
1304
1305 return val;
1306 }
1307 \f
1308 /* Subroutines of `comptypes'. */
1309
1310 /* Determine whether two trees derive from the same translation unit.
1311 If the CONTEXT chain ends in a null, that tree's context is still
1312 being parsed, so if two trees have context chains ending in null,
1313 they're in the same translation unit. */
1314
1315 bool
1316 same_translation_unit_p (const_tree t1, const_tree t2)
1317 {
1318 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1319 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1320 {
1321 case tcc_declaration:
1322 t1 = DECL_CONTEXT (t1); break;
1323 case tcc_type:
1324 t1 = TYPE_CONTEXT (t1); break;
1325 case tcc_exceptional:
1326 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1327 default: gcc_unreachable ();
1328 }
1329
1330 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1331 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1332 {
1333 case tcc_declaration:
1334 t2 = DECL_CONTEXT (t2); break;
1335 case tcc_type:
1336 t2 = TYPE_CONTEXT (t2); break;
1337 case tcc_exceptional:
1338 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1339 default: gcc_unreachable ();
1340 }
1341
1342 return t1 == t2;
1343 }
1344
1345 /* Allocate the seen two types, assuming that they are compatible. */
1346
1347 static struct tagged_tu_seen_cache *
1348 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1349 {
1350 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1351 tu->next = tagged_tu_seen_base;
1352 tu->t1 = t1;
1353 tu->t2 = t2;
1354
1355 tagged_tu_seen_base = tu;
1356
1357 /* The C standard says that two structures in different translation
1358 units are compatible with each other only if the types of their
1359 fields are compatible (among other things). We assume that they
1360 are compatible until proven otherwise when building the cache.
1361 An example where this can occur is:
1362 struct a
1363 {
1364 struct a *next;
1365 };
1366 If we are comparing this against a similar struct in another TU,
1367 and did not assume they were compatible, we end up with an infinite
1368 loop. */
1369 tu->val = 1;
1370 return tu;
1371 }
1372
1373 /* Free the seen types until we get to TU_TIL. */
1374
1375 static void
1376 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1377 {
1378 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1379 while (tu != tu_til)
1380 {
1381 const struct tagged_tu_seen_cache *const tu1
1382 = (const struct tagged_tu_seen_cache *) tu;
1383 tu = tu1->next;
1384 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1385 }
1386 tagged_tu_seen_base = tu_til;
1387 }
1388
1389 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1390 compatible. If the two types are not the same (which has been
1391 checked earlier), this can only happen when multiple translation
1392 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1393 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1394 comptypes_internal. */
1395
1396 static int
1397 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1398 bool *enum_and_int_p, bool *different_types_p)
1399 {
1400 tree s1, s2;
1401 bool needs_warning = false;
1402
1403 /* We have to verify that the tags of the types are the same. This
1404 is harder than it looks because this may be a typedef, so we have
1405 to go look at the original type. It may even be a typedef of a
1406 typedef...
1407 In the case of compiler-created builtin structs the TYPE_DECL
1408 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1409 while (TYPE_NAME (t1)
1410 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1411 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1412 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1413
1414 while (TYPE_NAME (t2)
1415 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1416 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1417 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1418
1419 /* C90 didn't have the requirement that the two tags be the same. */
1420 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1421 return 0;
1422
1423 /* C90 didn't say what happened if one or both of the types were
1424 incomplete; we choose to follow C99 rules here, which is that they
1425 are compatible. */
1426 if (TYPE_SIZE (t1) == NULL
1427 || TYPE_SIZE (t2) == NULL)
1428 return 1;
1429
1430 {
1431 const struct tagged_tu_seen_cache * tts_i;
1432 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1433 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1434 return tts_i->val;
1435 }
1436
1437 switch (TREE_CODE (t1))
1438 {
1439 case ENUMERAL_TYPE:
1440 {
1441 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1442 /* Speed up the case where the type values are in the same order. */
1443 tree tv1 = TYPE_VALUES (t1);
1444 tree tv2 = TYPE_VALUES (t2);
1445
1446 if (tv1 == tv2)
1447 {
1448 return 1;
1449 }
1450
1451 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1452 {
1453 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1454 break;
1455 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1456 {
1457 tu->val = 0;
1458 return 0;
1459 }
1460 }
1461
1462 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1463 {
1464 return 1;
1465 }
1466 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1467 {
1468 tu->val = 0;
1469 return 0;
1470 }
1471
1472 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1473 {
1474 tu->val = 0;
1475 return 0;
1476 }
1477
1478 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1479 {
1480 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1481 if (s2 == NULL
1482 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1483 {
1484 tu->val = 0;
1485 return 0;
1486 }
1487 }
1488 return 1;
1489 }
1490
1491 case UNION_TYPE:
1492 {
1493 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1494 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1495 {
1496 tu->val = 0;
1497 return 0;
1498 }
1499
1500 /* Speed up the common case where the fields are in the same order. */
1501 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1502 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1503 {
1504 int result;
1505
1506 if (DECL_NAME (s1) != DECL_NAME (s2))
1507 break;
1508 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1509 enum_and_int_p, different_types_p);
1510
1511 if (result != 1 && !DECL_NAME (s1))
1512 break;
1513 if (result == 0)
1514 {
1515 tu->val = 0;
1516 return 0;
1517 }
1518 if (result == 2)
1519 needs_warning = true;
1520
1521 if (TREE_CODE (s1) == FIELD_DECL
1522 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1523 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1524 {
1525 tu->val = 0;
1526 return 0;
1527 }
1528 }
1529 if (!s1 && !s2)
1530 {
1531 tu->val = needs_warning ? 2 : 1;
1532 return tu->val;
1533 }
1534
1535 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1536 {
1537 bool ok = false;
1538
1539 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1540 if (DECL_NAME (s1) == DECL_NAME (s2))
1541 {
1542 int result;
1543
1544 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1545 enum_and_int_p,
1546 different_types_p);
1547
1548 if (result != 1 && !DECL_NAME (s1))
1549 continue;
1550 if (result == 0)
1551 {
1552 tu->val = 0;
1553 return 0;
1554 }
1555 if (result == 2)
1556 needs_warning = true;
1557
1558 if (TREE_CODE (s1) == FIELD_DECL
1559 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1560 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1561 break;
1562
1563 ok = true;
1564 break;
1565 }
1566 if (!ok)
1567 {
1568 tu->val = 0;
1569 return 0;
1570 }
1571 }
1572 tu->val = needs_warning ? 2 : 10;
1573 return tu->val;
1574 }
1575
1576 case RECORD_TYPE:
1577 {
1578 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1579
1580 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1581 s1 && s2;
1582 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1583 {
1584 int result;
1585 if (TREE_CODE (s1) != TREE_CODE (s2)
1586 || DECL_NAME (s1) != DECL_NAME (s2))
1587 break;
1588 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1589 enum_and_int_p, different_types_p);
1590 if (result == 0)
1591 break;
1592 if (result == 2)
1593 needs_warning = true;
1594
1595 if (TREE_CODE (s1) == FIELD_DECL
1596 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1597 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1598 break;
1599 }
1600 if (s1 && s2)
1601 tu->val = 0;
1602 else
1603 tu->val = needs_warning ? 2 : 1;
1604 return tu->val;
1605 }
1606
1607 default:
1608 gcc_unreachable ();
1609 }
1610 }
1611
1612 /* Return 1 if two function types F1 and F2 are compatible.
1613 If either type specifies no argument types,
1614 the other must specify a fixed number of self-promoting arg types.
1615 Otherwise, if one type specifies only the number of arguments,
1616 the other must specify that number of self-promoting arg types.
1617 Otherwise, the argument types must match.
1618 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1619
1620 static int
1621 function_types_compatible_p (const_tree f1, const_tree f2,
1622 bool *enum_and_int_p, bool *different_types_p)
1623 {
1624 tree args1, args2;
1625 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1626 int val = 1;
1627 int val1;
1628 tree ret1, ret2;
1629
1630 ret1 = TREE_TYPE (f1);
1631 ret2 = TREE_TYPE (f2);
1632
1633 /* 'volatile' qualifiers on a function's return type used to mean
1634 the function is noreturn. */
1635 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1636 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1637 if (TYPE_VOLATILE (ret1))
1638 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1639 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1640 if (TYPE_VOLATILE (ret2))
1641 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1642 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1643 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1644 if (val == 0)
1645 return 0;
1646
1647 args1 = TYPE_ARG_TYPES (f1);
1648 args2 = TYPE_ARG_TYPES (f2);
1649
1650 if (different_types_p != NULL
1651 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1652 *different_types_p = true;
1653
1654 /* An unspecified parmlist matches any specified parmlist
1655 whose argument types don't need default promotions. */
1656
1657 if (args1 == NULL_TREE)
1658 {
1659 if (!self_promoting_args_p (args2))
1660 return 0;
1661 /* If one of these types comes from a non-prototype fn definition,
1662 compare that with the other type's arglist.
1663 If they don't match, ask for a warning (but no error). */
1664 if (TYPE_ACTUAL_ARG_TYPES (f1)
1665 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1666 enum_and_int_p, different_types_p) != 1)
1667 val = 2;
1668 return val;
1669 }
1670 if (args2 == NULL_TREE)
1671 {
1672 if (!self_promoting_args_p (args1))
1673 return 0;
1674 if (TYPE_ACTUAL_ARG_TYPES (f2)
1675 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1676 enum_and_int_p, different_types_p) != 1)
1677 val = 2;
1678 return val;
1679 }
1680
1681 /* Both types have argument lists: compare them and propagate results. */
1682 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1683 different_types_p);
1684 return val1 != 1 ? val1 : val;
1685 }
1686
1687 /* Check two lists of types for compatibility, returning 0 for
1688 incompatible, 1 for compatible, or 2 for compatible with
1689 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1690 comptypes_internal. */
1691
1692 static int
1693 type_lists_compatible_p (const_tree args1, const_tree args2,
1694 bool *enum_and_int_p, bool *different_types_p)
1695 {
1696 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1697 int val = 1;
1698 int newval = 0;
1699
1700 while (1)
1701 {
1702 tree a1, mv1, a2, mv2;
1703 if (args1 == NULL_TREE && args2 == NULL_TREE)
1704 return val;
1705 /* If one list is shorter than the other,
1706 they fail to match. */
1707 if (args1 == NULL_TREE || args2 == NULL_TREE)
1708 return 0;
1709 mv1 = a1 = TREE_VALUE (args1);
1710 mv2 = a2 = TREE_VALUE (args2);
1711 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1712 mv1 = (TYPE_ATOMIC (mv1)
1713 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1714 TYPE_QUAL_ATOMIC)
1715 : TYPE_MAIN_VARIANT (mv1));
1716 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1717 mv2 = (TYPE_ATOMIC (mv2)
1718 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1719 TYPE_QUAL_ATOMIC)
1720 : TYPE_MAIN_VARIANT (mv2));
1721 /* A null pointer instead of a type
1722 means there is supposed to be an argument
1723 but nothing is specified about what type it has.
1724 So match anything that self-promotes. */
1725 if (different_types_p != NULL
1726 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1727 *different_types_p = true;
1728 if (a1 == NULL_TREE)
1729 {
1730 if (c_type_promotes_to (a2) != a2)
1731 return 0;
1732 }
1733 else if (a2 == NULL_TREE)
1734 {
1735 if (c_type_promotes_to (a1) != a1)
1736 return 0;
1737 }
1738 /* If one of the lists has an error marker, ignore this arg. */
1739 else if (TREE_CODE (a1) == ERROR_MARK
1740 || TREE_CODE (a2) == ERROR_MARK)
1741 ;
1742 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1743 different_types_p)))
1744 {
1745 if (different_types_p != NULL)
1746 *different_types_p = true;
1747 /* Allow wait (union {union wait *u; int *i} *)
1748 and wait (union wait *) to be compatible. */
1749 if (TREE_CODE (a1) == UNION_TYPE
1750 && (TYPE_NAME (a1) == NULL_TREE
1751 || TYPE_TRANSPARENT_AGGR (a1))
1752 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1753 && tree_int_cst_equal (TYPE_SIZE (a1),
1754 TYPE_SIZE (a2)))
1755 {
1756 tree memb;
1757 for (memb = TYPE_FIELDS (a1);
1758 memb; memb = DECL_CHAIN (memb))
1759 {
1760 tree mv3 = TREE_TYPE (memb);
1761 if (mv3 && mv3 != error_mark_node
1762 && TREE_CODE (mv3) != ARRAY_TYPE)
1763 mv3 = (TYPE_ATOMIC (mv3)
1764 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1765 TYPE_QUAL_ATOMIC)
1766 : TYPE_MAIN_VARIANT (mv3));
1767 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1768 different_types_p))
1769 break;
1770 }
1771 if (memb == NULL_TREE)
1772 return 0;
1773 }
1774 else if (TREE_CODE (a2) == UNION_TYPE
1775 && (TYPE_NAME (a2) == NULL_TREE
1776 || TYPE_TRANSPARENT_AGGR (a2))
1777 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1778 && tree_int_cst_equal (TYPE_SIZE (a2),
1779 TYPE_SIZE (a1)))
1780 {
1781 tree memb;
1782 for (memb = TYPE_FIELDS (a2);
1783 memb; memb = DECL_CHAIN (memb))
1784 {
1785 tree mv3 = TREE_TYPE (memb);
1786 if (mv3 && mv3 != error_mark_node
1787 && TREE_CODE (mv3) != ARRAY_TYPE)
1788 mv3 = (TYPE_ATOMIC (mv3)
1789 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1790 TYPE_QUAL_ATOMIC)
1791 : TYPE_MAIN_VARIANT (mv3));
1792 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1793 different_types_p))
1794 break;
1795 }
1796 if (memb == NULL_TREE)
1797 return 0;
1798 }
1799 else
1800 return 0;
1801 }
1802
1803 /* comptypes said ok, but record if it said to warn. */
1804 if (newval > val)
1805 val = newval;
1806
1807 args1 = TREE_CHAIN (args1);
1808 args2 = TREE_CHAIN (args2);
1809 }
1810 }
1811 \f
1812 /* Compute the size to increment a pointer by. When a function type or void
1813 type or incomplete type is passed, size_one_node is returned.
1814 This function does not emit any diagnostics; the caller is responsible
1815 for that. */
1816
1817 static tree
1818 c_size_in_bytes (const_tree type)
1819 {
1820 enum tree_code code = TREE_CODE (type);
1821
1822 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1823 || !COMPLETE_TYPE_P (type))
1824 return size_one_node;
1825
1826 /* Convert in case a char is more than one unit. */
1827 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1828 size_int (TYPE_PRECISION (char_type_node)
1829 / BITS_PER_UNIT));
1830 }
1831 \f
1832 /* Return either DECL or its known constant value (if it has one). */
1833
1834 tree
1835 decl_constant_value_1 (tree decl, bool in_init)
1836 {
1837 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1838 TREE_CODE (decl) != PARM_DECL
1839 && !TREE_THIS_VOLATILE (decl)
1840 && TREE_READONLY (decl)
1841 && DECL_INITIAL (decl) != NULL_TREE
1842 && !error_operand_p (DECL_INITIAL (decl))
1843 /* This is invalid if initial value is not constant.
1844 If it has either a function call, a memory reference,
1845 or a variable, then re-evaluating it could give different results. */
1846 && TREE_CONSTANT (DECL_INITIAL (decl))
1847 /* Check for cases where this is sub-optimal, even though valid. */
1848 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1849 return DECL_INITIAL (decl);
1850 return decl;
1851 }
1852
1853 /* Return either DECL or its known constant value (if it has one).
1854 Like the above, but always return decl outside of functions. */
1855
1856 tree
1857 decl_constant_value (tree decl)
1858 {
1859 /* Don't change a variable array bound or initial value to a constant
1860 in a place where a variable is invalid. */
1861 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1862 }
1863
1864 /* Convert the array expression EXP to a pointer. */
1865 static tree
1866 array_to_pointer_conversion (location_t loc, tree exp)
1867 {
1868 tree orig_exp = exp;
1869 tree type = TREE_TYPE (exp);
1870 tree adr;
1871 tree restype = TREE_TYPE (type);
1872 tree ptrtype;
1873
1874 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1875
1876 STRIP_TYPE_NOPS (exp);
1877
1878 if (TREE_NO_WARNING (orig_exp))
1879 TREE_NO_WARNING (exp) = 1;
1880
1881 ptrtype = build_pointer_type (restype);
1882
1883 if (INDIRECT_REF_P (exp))
1884 return convert (ptrtype, TREE_OPERAND (exp, 0));
1885
1886 /* In C++ array compound literals are temporary objects unless they are
1887 const or appear in namespace scope, so they are destroyed too soon
1888 to use them for much of anything (c++/53220). */
1889 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1890 {
1891 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1892 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1893 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1894 "converting an array compound literal to a pointer "
1895 "is ill-formed in C++");
1896 }
1897
1898 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1899 return convert (ptrtype, adr);
1900 }
1901
1902 /* Convert the function expression EXP to a pointer. */
1903 static tree
1904 function_to_pointer_conversion (location_t loc, tree exp)
1905 {
1906 tree orig_exp = exp;
1907
1908 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1909
1910 STRIP_TYPE_NOPS (exp);
1911
1912 if (TREE_NO_WARNING (orig_exp))
1913 TREE_NO_WARNING (exp) = 1;
1914
1915 return build_unary_op (loc, ADDR_EXPR, exp, false);
1916 }
1917
1918 /* Mark EXP as read, not just set, for set but not used -Wunused
1919 warning purposes. */
1920
1921 void
1922 mark_exp_read (tree exp)
1923 {
1924 switch (TREE_CODE (exp))
1925 {
1926 case VAR_DECL:
1927 case PARM_DECL:
1928 DECL_READ_P (exp) = 1;
1929 break;
1930 case ARRAY_REF:
1931 case COMPONENT_REF:
1932 case MODIFY_EXPR:
1933 case REALPART_EXPR:
1934 case IMAGPART_EXPR:
1935 CASE_CONVERT:
1936 case ADDR_EXPR:
1937 case VIEW_CONVERT_EXPR:
1938 mark_exp_read (TREE_OPERAND (exp, 0));
1939 break;
1940 case COMPOUND_EXPR:
1941 case C_MAYBE_CONST_EXPR:
1942 mark_exp_read (TREE_OPERAND (exp, 1));
1943 break;
1944 default:
1945 break;
1946 }
1947 }
1948
1949 /* Perform the default conversion of arrays and functions to pointers.
1950 Return the result of converting EXP. For any other expression, just
1951 return EXP.
1952
1953 LOC is the location of the expression. */
1954
1955 struct c_expr
1956 default_function_array_conversion (location_t loc, struct c_expr exp)
1957 {
1958 tree orig_exp = exp.value;
1959 tree type = TREE_TYPE (exp.value);
1960 enum tree_code code = TREE_CODE (type);
1961
1962 switch (code)
1963 {
1964 case ARRAY_TYPE:
1965 {
1966 bool not_lvalue = false;
1967 bool lvalue_array_p;
1968
1969 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1970 || CONVERT_EXPR_P (exp.value))
1971 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1972 {
1973 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1974 not_lvalue = true;
1975 exp.value = TREE_OPERAND (exp.value, 0);
1976 }
1977
1978 if (TREE_NO_WARNING (orig_exp))
1979 TREE_NO_WARNING (exp.value) = 1;
1980
1981 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1982 if (!flag_isoc99 && !lvalue_array_p)
1983 {
1984 /* Before C99, non-lvalue arrays do not decay to pointers.
1985 Normally, using such an array would be invalid; but it can
1986 be used correctly inside sizeof or as a statement expression.
1987 Thus, do not give an error here; an error will result later. */
1988 return exp;
1989 }
1990
1991 exp.value = array_to_pointer_conversion (loc, exp.value);
1992 }
1993 break;
1994 case FUNCTION_TYPE:
1995 exp.value = function_to_pointer_conversion (loc, exp.value);
1996 break;
1997 default:
1998 break;
1999 }
2000
2001 return exp;
2002 }
2003
2004 struct c_expr
2005 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2006 {
2007 mark_exp_read (exp.value);
2008 return default_function_array_conversion (loc, exp);
2009 }
2010
2011 /* Return whether EXPR should be treated as an atomic lvalue for the
2012 purposes of load and store handling. */
2013
2014 static bool
2015 really_atomic_lvalue (tree expr)
2016 {
2017 if (error_operand_p (expr))
2018 return false;
2019 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2020 return false;
2021 if (!lvalue_p (expr))
2022 return false;
2023
2024 /* Ignore _Atomic on register variables, since their addresses can't
2025 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2026 sequences wouldn't work. Ignore _Atomic on structures containing
2027 bit-fields, since accessing elements of atomic structures or
2028 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2029 it's undefined at translation time or execution time, and the
2030 normal atomic sequences again wouldn't work. */
2031 while (handled_component_p (expr))
2032 {
2033 if (TREE_CODE (expr) == COMPONENT_REF
2034 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2035 return false;
2036 expr = TREE_OPERAND (expr, 0);
2037 }
2038 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2039 return false;
2040 return true;
2041 }
2042
2043 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2044 including converting functions and arrays to pointers if CONVERT_P.
2045 If READ_P, also mark the expression as having been read. */
2046
2047 struct c_expr
2048 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2049 bool convert_p, bool read_p)
2050 {
2051 if (read_p)
2052 mark_exp_read (exp.value);
2053 if (convert_p)
2054 exp = default_function_array_conversion (loc, exp);
2055 if (really_atomic_lvalue (exp.value))
2056 {
2057 vec<tree, va_gc> *params;
2058 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2059 tree expr_type = TREE_TYPE (exp.value);
2060 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2061 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2062
2063 gcc_assert (TYPE_ATOMIC (expr_type));
2064
2065 /* Expansion of a generic atomic load may require an addition
2066 element, so allocate enough to prevent a resize. */
2067 vec_alloc (params, 4);
2068
2069 /* Remove the qualifiers for the rest of the expressions and
2070 create the VAL temp variable to hold the RHS. */
2071 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2072 tmp = create_tmp_var_raw (nonatomic_type);
2073 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2074 TREE_ADDRESSABLE (tmp) = 1;
2075 TREE_NO_WARNING (tmp) = 1;
2076
2077 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2078 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2079 params->quick_push (expr_addr);
2080 params->quick_push (tmp_addr);
2081 params->quick_push (seq_cst);
2082 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2083
2084 /* EXPR is always read. */
2085 mark_exp_read (exp.value);
2086
2087 /* Return tmp which contains the value loaded. */
2088 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2089 NULL_TREE, NULL_TREE);
2090 }
2091 return exp;
2092 }
2093
2094 /* EXP is an expression of integer type. Apply the integer promotions
2095 to it and return the promoted value. */
2096
2097 tree
2098 perform_integral_promotions (tree exp)
2099 {
2100 tree type = TREE_TYPE (exp);
2101 enum tree_code code = TREE_CODE (type);
2102
2103 gcc_assert (INTEGRAL_TYPE_P (type));
2104
2105 /* Normally convert enums to int,
2106 but convert wide enums to something wider. */
2107 if (code == ENUMERAL_TYPE)
2108 {
2109 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2110 TYPE_PRECISION (integer_type_node)),
2111 ((TYPE_PRECISION (type)
2112 >= TYPE_PRECISION (integer_type_node))
2113 && TYPE_UNSIGNED (type)));
2114
2115 return convert (type, exp);
2116 }
2117
2118 /* ??? This should no longer be needed now bit-fields have their
2119 proper types. */
2120 if (TREE_CODE (exp) == COMPONENT_REF
2121 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2122 /* If it's thinner than an int, promote it like a
2123 c_promoting_integer_type_p, otherwise leave it alone. */
2124 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2125 TYPE_PRECISION (integer_type_node)) < 0)
2126 return convert (integer_type_node, exp);
2127
2128 if (c_promoting_integer_type_p (type))
2129 {
2130 /* Preserve unsignedness if not really getting any wider. */
2131 if (TYPE_UNSIGNED (type)
2132 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2133 return convert (unsigned_type_node, exp);
2134
2135 return convert (integer_type_node, exp);
2136 }
2137
2138 return exp;
2139 }
2140
2141
2142 /* Perform default promotions for C data used in expressions.
2143 Enumeral types or short or char are converted to int.
2144 In addition, manifest constants symbols are replaced by their values. */
2145
2146 tree
2147 default_conversion (tree exp)
2148 {
2149 tree orig_exp;
2150 tree type = TREE_TYPE (exp);
2151 enum tree_code code = TREE_CODE (type);
2152 tree promoted_type;
2153
2154 mark_exp_read (exp);
2155
2156 /* Functions and arrays have been converted during parsing. */
2157 gcc_assert (code != FUNCTION_TYPE);
2158 if (code == ARRAY_TYPE)
2159 return exp;
2160
2161 /* Constants can be used directly unless they're not loadable. */
2162 if (TREE_CODE (exp) == CONST_DECL)
2163 exp = DECL_INITIAL (exp);
2164
2165 /* Strip no-op conversions. */
2166 orig_exp = exp;
2167 STRIP_TYPE_NOPS (exp);
2168
2169 if (TREE_NO_WARNING (orig_exp))
2170 TREE_NO_WARNING (exp) = 1;
2171
2172 if (code == VOID_TYPE)
2173 {
2174 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2175 "void value not ignored as it ought to be");
2176 return error_mark_node;
2177 }
2178
2179 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2180 if (exp == error_mark_node)
2181 return error_mark_node;
2182
2183 promoted_type = targetm.promoted_type (type);
2184 if (promoted_type)
2185 return convert (promoted_type, exp);
2186
2187 if (INTEGRAL_TYPE_P (type))
2188 return perform_integral_promotions (exp);
2189
2190 return exp;
2191 }
2192 \f
2193 /* Look up COMPONENT in a structure or union TYPE.
2194
2195 If the component name is not found, returns NULL_TREE. Otherwise,
2196 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2197 stepping down the chain to the component, which is in the last
2198 TREE_VALUE of the list. Normally the list is of length one, but if
2199 the component is embedded within (nested) anonymous structures or
2200 unions, the list steps down the chain to the component. */
2201
2202 static tree
2203 lookup_field (tree type, tree component)
2204 {
2205 tree field;
2206
2207 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2208 to the field elements. Use a binary search on this array to quickly
2209 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2210 will always be set for structures which have many elements.
2211
2212 Duplicate field checking replaces duplicates with NULL_TREE so
2213 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2214 case just iterate using DECL_CHAIN. */
2215
2216 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2217 && !seen_error ())
2218 {
2219 int bot, top, half;
2220 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2221
2222 field = TYPE_FIELDS (type);
2223 bot = 0;
2224 top = TYPE_LANG_SPECIFIC (type)->s->len;
2225 while (top - bot > 1)
2226 {
2227 half = (top - bot + 1) >> 1;
2228 field = field_array[bot+half];
2229
2230 if (DECL_NAME (field) == NULL_TREE)
2231 {
2232 /* Step through all anon unions in linear fashion. */
2233 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2234 {
2235 field = field_array[bot++];
2236 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2237 {
2238 tree anon = lookup_field (TREE_TYPE (field), component);
2239
2240 if (anon)
2241 return tree_cons (NULL_TREE, field, anon);
2242
2243 /* The Plan 9 compiler permits referring
2244 directly to an anonymous struct/union field
2245 using a typedef name. */
2246 if (flag_plan9_extensions
2247 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2248 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2249 == TYPE_DECL)
2250 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2251 == component))
2252 break;
2253 }
2254 }
2255
2256 /* Entire record is only anon unions. */
2257 if (bot > top)
2258 return NULL_TREE;
2259
2260 /* Restart the binary search, with new lower bound. */
2261 continue;
2262 }
2263
2264 if (DECL_NAME (field) == component)
2265 break;
2266 if (DECL_NAME (field) < component)
2267 bot += half;
2268 else
2269 top = bot + half;
2270 }
2271
2272 if (DECL_NAME (field_array[bot]) == component)
2273 field = field_array[bot];
2274 else if (DECL_NAME (field) != component)
2275 return NULL_TREE;
2276 }
2277 else
2278 {
2279 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2280 {
2281 if (DECL_NAME (field) == NULL_TREE
2282 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2283 {
2284 tree anon = lookup_field (TREE_TYPE (field), component);
2285
2286 if (anon)
2287 return tree_cons (NULL_TREE, field, anon);
2288
2289 /* The Plan 9 compiler permits referring directly to an
2290 anonymous struct/union field using a typedef
2291 name. */
2292 if (flag_plan9_extensions
2293 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2294 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2295 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2296 == component))
2297 break;
2298 }
2299
2300 if (DECL_NAME (field) == component)
2301 break;
2302 }
2303
2304 if (field == NULL_TREE)
2305 return NULL_TREE;
2306 }
2307
2308 return tree_cons (NULL_TREE, field, NULL_TREE);
2309 }
2310
2311 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2312
2313 static void
2314 lookup_field_fuzzy_find_candidates (tree type, tree component,
2315 vec<tree> *candidates)
2316 {
2317 tree field;
2318 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2319 {
2320 if (DECL_NAME (field) == NULL_TREE
2321 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2322 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2323 candidates);
2324
2325 if (DECL_NAME (field))
2326 candidates->safe_push (DECL_NAME (field));
2327 }
2328 }
2329
2330 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2331 rather than returning a TREE_LIST for an exact match. */
2332
2333 static tree
2334 lookup_field_fuzzy (tree type, tree component)
2335 {
2336 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2337
2338 /* First, gather a list of candidates. */
2339 auto_vec <tree> candidates;
2340
2341 lookup_field_fuzzy_find_candidates (type, component,
2342 &candidates);
2343
2344 return find_closest_identifier (component, &candidates);
2345 }
2346
2347 /* Support function for build_component_ref's error-handling.
2348
2349 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2350 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2351
2352 static bool
2353 should_suggest_deref_p (tree datum_type)
2354 {
2355 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2356 allows "." for ptrs; we could be handling a failed attempt
2357 to access a property. */
2358 if (c_dialect_objc ())
2359 return false;
2360
2361 /* Only suggest it for pointers... */
2362 if (TREE_CODE (datum_type) != POINTER_TYPE)
2363 return false;
2364
2365 /* ...to structs/unions. */
2366 tree underlying_type = TREE_TYPE (datum_type);
2367 enum tree_code code = TREE_CODE (underlying_type);
2368 if (code == RECORD_TYPE || code == UNION_TYPE)
2369 return true;
2370 else
2371 return false;
2372 }
2373
2374 /* Make an expression to refer to the COMPONENT field of structure or
2375 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2376 location of the COMPONENT_REF. COMPONENT_LOC is the location
2377 of COMPONENT. */
2378
2379 tree
2380 build_component_ref (location_t loc, tree datum, tree component,
2381 location_t component_loc)
2382 {
2383 tree type = TREE_TYPE (datum);
2384 enum tree_code code = TREE_CODE (type);
2385 tree field = NULL;
2386 tree ref;
2387 bool datum_lvalue = lvalue_p (datum);
2388
2389 if (!objc_is_public (datum, component))
2390 return error_mark_node;
2391
2392 /* Detect Objective-C property syntax object.property. */
2393 if (c_dialect_objc ()
2394 && (ref = objc_maybe_build_component_ref (datum, component)))
2395 return ref;
2396
2397 /* See if there is a field or component with name COMPONENT. */
2398
2399 if (code == RECORD_TYPE || code == UNION_TYPE)
2400 {
2401 if (!COMPLETE_TYPE_P (type))
2402 {
2403 c_incomplete_type_error (loc, NULL_TREE, type);
2404 return error_mark_node;
2405 }
2406
2407 field = lookup_field (type, component);
2408
2409 if (!field)
2410 {
2411 tree guessed_id = lookup_field_fuzzy (type, component);
2412 if (guessed_id)
2413 {
2414 /* Attempt to provide a fixit replacement hint, if
2415 we have a valid range for the component. */
2416 location_t reported_loc
2417 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2418 gcc_rich_location rich_loc (reported_loc);
2419 if (component_loc != UNKNOWN_LOCATION)
2420 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2421 error_at (&rich_loc,
2422 "%qT has no member named %qE; did you mean %qE?",
2423 type, component, guessed_id);
2424 }
2425 else
2426 error_at (loc, "%qT has no member named %qE", type, component);
2427 return error_mark_node;
2428 }
2429
2430 /* Accessing elements of atomic structures or unions is undefined
2431 behavior (C11 6.5.2.3#5). */
2432 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2433 {
2434 if (code == RECORD_TYPE)
2435 warning_at (loc, 0, "accessing a member %qE of an atomic "
2436 "structure %qE", component, datum);
2437 else
2438 warning_at (loc, 0, "accessing a member %qE of an atomic "
2439 "union %qE", component, datum);
2440 }
2441
2442 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2443 This might be better solved in future the way the C++ front
2444 end does it - by giving the anonymous entities each a
2445 separate name and type, and then have build_component_ref
2446 recursively call itself. We can't do that here. */
2447 do
2448 {
2449 tree subdatum = TREE_VALUE (field);
2450 int quals;
2451 tree subtype;
2452 bool use_datum_quals;
2453
2454 if (TREE_TYPE (subdatum) == error_mark_node)
2455 return error_mark_node;
2456
2457 /* If this is an rvalue, it does not have qualifiers in C
2458 standard terms and we must avoid propagating such
2459 qualifiers down to a non-lvalue array that is then
2460 converted to a pointer. */
2461 use_datum_quals = (datum_lvalue
2462 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2463
2464 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2465 if (use_datum_quals)
2466 quals |= TYPE_QUALS (TREE_TYPE (datum));
2467 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2468
2469 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2470 NULL_TREE);
2471 SET_EXPR_LOCATION (ref, loc);
2472 if (TREE_READONLY (subdatum)
2473 || (use_datum_quals && TREE_READONLY (datum)))
2474 TREE_READONLY (ref) = 1;
2475 if (TREE_THIS_VOLATILE (subdatum)
2476 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2477 TREE_THIS_VOLATILE (ref) = 1;
2478
2479 if (TREE_DEPRECATED (subdatum))
2480 warn_deprecated_use (subdatum, NULL_TREE);
2481
2482 datum = ref;
2483
2484 field = TREE_CHAIN (field);
2485 }
2486 while (field);
2487
2488 return ref;
2489 }
2490 else if (should_suggest_deref_p (type))
2491 {
2492 /* Special-case the error message for "ptr.field" for the case
2493 where the user has confused "." vs "->". */
2494 rich_location richloc (line_table, loc);
2495 /* "loc" should be the "." token. */
2496 richloc.add_fixit_replace ("->");
2497 error_at (&richloc,
2498 "%qE is a pointer; did you mean to use %<->%>?",
2499 datum);
2500 return error_mark_node;
2501 }
2502 else if (code != ERROR_MARK)
2503 error_at (loc,
2504 "request for member %qE in something not a structure or union",
2505 component);
2506
2507 return error_mark_node;
2508 }
2509 \f
2510 /* Given an expression PTR for a pointer, return an expression
2511 for the value pointed to.
2512 ERRORSTRING is the name of the operator to appear in error messages.
2513
2514 LOC is the location to use for the generated tree. */
2515
2516 tree
2517 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2518 {
2519 tree pointer = default_conversion (ptr);
2520 tree type = TREE_TYPE (pointer);
2521 tree ref;
2522
2523 if (TREE_CODE (type) == POINTER_TYPE)
2524 {
2525 if (CONVERT_EXPR_P (pointer)
2526 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2527 {
2528 /* If a warning is issued, mark it to avoid duplicates from
2529 the backend. This only needs to be done at
2530 warn_strict_aliasing > 2. */
2531 if (warn_strict_aliasing > 2)
2532 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2533 type, TREE_OPERAND (pointer, 0)))
2534 TREE_NO_WARNING (pointer) = 1;
2535 }
2536
2537 if (TREE_CODE (pointer) == ADDR_EXPR
2538 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2539 == TREE_TYPE (type)))
2540 {
2541 ref = TREE_OPERAND (pointer, 0);
2542 protected_set_expr_location (ref, loc);
2543 return ref;
2544 }
2545 else
2546 {
2547 tree t = TREE_TYPE (type);
2548
2549 ref = build1 (INDIRECT_REF, t, pointer);
2550
2551 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2552 {
2553 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2554 {
2555 error_at (loc, "dereferencing pointer to incomplete type "
2556 "%qT", t);
2557 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2558 }
2559 return error_mark_node;
2560 }
2561 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2562 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2563
2564 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2565 so that we get the proper error message if the result is used
2566 to assign to. Also, &* is supposed to be a no-op.
2567 And ANSI C seems to specify that the type of the result
2568 should be the const type. */
2569 /* A de-reference of a pointer to const is not a const. It is valid
2570 to change it via some other pointer. */
2571 TREE_READONLY (ref) = TYPE_READONLY (t);
2572 TREE_SIDE_EFFECTS (ref)
2573 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2574 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2575 protected_set_expr_location (ref, loc);
2576 return ref;
2577 }
2578 }
2579 else if (TREE_CODE (pointer) != ERROR_MARK)
2580 invalid_indirection_error (loc, type, errstring);
2581
2582 return error_mark_node;
2583 }
2584
2585 /* This handles expressions of the form "a[i]", which denotes
2586 an array reference.
2587
2588 This is logically equivalent in C to *(a+i), but we may do it differently.
2589 If A is a variable or a member, we generate a primitive ARRAY_REF.
2590 This avoids forcing the array out of registers, and can work on
2591 arrays that are not lvalues (for example, members of structures returned
2592 by functions).
2593
2594 For vector types, allow vector[i] but not i[vector], and create
2595 *(((type*)&vectortype) + i) for the expression.
2596
2597 LOC is the location to use for the returned expression. */
2598
2599 tree
2600 build_array_ref (location_t loc, tree array, tree index)
2601 {
2602 tree ret;
2603 bool swapped = false;
2604 if (TREE_TYPE (array) == error_mark_node
2605 || TREE_TYPE (index) == error_mark_node)
2606 return error_mark_node;
2607
2608 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2609 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2610 /* Allow vector[index] but not index[vector]. */
2611 && !VECTOR_TYPE_P (TREE_TYPE (array)))
2612 {
2613 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2614 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2615 {
2616 error_at (loc,
2617 "subscripted value is neither array nor pointer nor vector");
2618
2619 return error_mark_node;
2620 }
2621 std::swap (array, index);
2622 swapped = true;
2623 }
2624
2625 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2626 {
2627 error_at (loc, "array subscript is not an integer");
2628 return error_mark_node;
2629 }
2630
2631 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2632 {
2633 error_at (loc, "subscripted value is pointer to function");
2634 return error_mark_node;
2635 }
2636
2637 /* ??? Existing practice has been to warn only when the char
2638 index is syntactically the index, not for char[array]. */
2639 if (!swapped)
2640 warn_array_subscript_with_type_char (loc, index);
2641
2642 /* Apply default promotions *after* noticing character types. */
2643 index = default_conversion (index);
2644 if (index == error_mark_node)
2645 return error_mark_node;
2646
2647 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2648
2649 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2650 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2651
2652 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2653 {
2654 tree rval, type;
2655
2656 /* An array that is indexed by a non-constant
2657 cannot be stored in a register; we must be able to do
2658 address arithmetic on its address.
2659 Likewise an array of elements of variable size. */
2660 if (TREE_CODE (index) != INTEGER_CST
2661 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2662 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2663 {
2664 if (!c_mark_addressable (array, true))
2665 return error_mark_node;
2666 }
2667 /* An array that is indexed by a constant value which is not within
2668 the array bounds cannot be stored in a register either; because we
2669 would get a crash in store_bit_field/extract_bit_field when trying
2670 to access a non-existent part of the register. */
2671 if (TREE_CODE (index) == INTEGER_CST
2672 && TYPE_DOMAIN (TREE_TYPE (array))
2673 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2674 {
2675 if (!c_mark_addressable (array))
2676 return error_mark_node;
2677 }
2678
2679 if ((pedantic || warn_c90_c99_compat)
2680 && ! was_vector)
2681 {
2682 tree foo = array;
2683 while (TREE_CODE (foo) == COMPONENT_REF)
2684 foo = TREE_OPERAND (foo, 0);
2685 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2686 pedwarn (loc, OPT_Wpedantic,
2687 "ISO C forbids subscripting %<register%> array");
2688 else if (!lvalue_p (foo))
2689 pedwarn_c90 (loc, OPT_Wpedantic,
2690 "ISO C90 forbids subscripting non-lvalue "
2691 "array");
2692 }
2693
2694 type = TREE_TYPE (TREE_TYPE (array));
2695 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2696 /* Array ref is const/volatile if the array elements are
2697 or if the array is. */
2698 TREE_READONLY (rval)
2699 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2700 | TREE_READONLY (array));
2701 TREE_SIDE_EFFECTS (rval)
2702 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2703 | TREE_SIDE_EFFECTS (array));
2704 TREE_THIS_VOLATILE (rval)
2705 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2706 /* This was added by rms on 16 Nov 91.
2707 It fixes vol struct foo *a; a->elts[1]
2708 in an inline function.
2709 Hope it doesn't break something else. */
2710 | TREE_THIS_VOLATILE (array));
2711 ret = require_complete_type (loc, rval);
2712 protected_set_expr_location (ret, loc);
2713 if (non_lvalue)
2714 ret = non_lvalue_loc (loc, ret);
2715 return ret;
2716 }
2717 else
2718 {
2719 tree ar = default_conversion (array);
2720
2721 if (ar == error_mark_node)
2722 return ar;
2723
2724 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2725 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2726
2727 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2728 index, false),
2729 RO_ARRAY_INDEXING);
2730 if (non_lvalue)
2731 ret = non_lvalue_loc (loc, ret);
2732 return ret;
2733 }
2734 }
2735 \f
2736 /* Build an external reference to identifier ID. FUN indicates
2737 whether this will be used for a function call. LOC is the source
2738 location of the identifier. This sets *TYPE to the type of the
2739 identifier, which is not the same as the type of the returned value
2740 for CONST_DECLs defined as enum constants. If the type of the
2741 identifier is not available, *TYPE is set to NULL. */
2742 tree
2743 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2744 {
2745 tree ref;
2746 tree decl = lookup_name (id);
2747
2748 /* In Objective-C, an instance variable (ivar) may be preferred to
2749 whatever lookup_name() found. */
2750 decl = objc_lookup_ivar (decl, id);
2751
2752 *type = NULL;
2753 if (decl && decl != error_mark_node)
2754 {
2755 ref = decl;
2756 *type = TREE_TYPE (ref);
2757 }
2758 else if (fun)
2759 /* Implicit function declaration. */
2760 ref = implicitly_declare (loc, id);
2761 else if (decl == error_mark_node)
2762 /* Don't complain about something that's already been
2763 complained about. */
2764 return error_mark_node;
2765 else
2766 {
2767 undeclared_variable (loc, id);
2768 return error_mark_node;
2769 }
2770
2771 if (TREE_TYPE (ref) == error_mark_node)
2772 return error_mark_node;
2773
2774 if (TREE_DEPRECATED (ref))
2775 warn_deprecated_use (ref, NULL_TREE);
2776
2777 /* Recursive call does not count as usage. */
2778 if (ref != current_function_decl)
2779 {
2780 TREE_USED (ref) = 1;
2781 }
2782
2783 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2784 {
2785 if (!in_sizeof && !in_typeof)
2786 C_DECL_USED (ref) = 1;
2787 else if (DECL_INITIAL (ref) == NULL_TREE
2788 && DECL_EXTERNAL (ref)
2789 && !TREE_PUBLIC (ref))
2790 record_maybe_used_decl (ref);
2791 }
2792
2793 if (TREE_CODE (ref) == CONST_DECL)
2794 {
2795 used_types_insert (TREE_TYPE (ref));
2796
2797 if (warn_cxx_compat
2798 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2799 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2800 {
2801 warning_at (loc, OPT_Wc___compat,
2802 ("enum constant defined in struct or union "
2803 "is not visible in C++"));
2804 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2805 }
2806
2807 ref = DECL_INITIAL (ref);
2808 TREE_CONSTANT (ref) = 1;
2809 }
2810 else if (current_function_decl != NULL_TREE
2811 && !DECL_FILE_SCOPE_P (current_function_decl)
2812 && (VAR_OR_FUNCTION_DECL_P (ref)
2813 || TREE_CODE (ref) == PARM_DECL))
2814 {
2815 tree context = decl_function_context (ref);
2816
2817 if (context != NULL_TREE && context != current_function_decl)
2818 DECL_NONLOCAL (ref) = 1;
2819 }
2820 /* C99 6.7.4p3: An inline definition of a function with external
2821 linkage ... shall not contain a reference to an identifier with
2822 internal linkage. */
2823 else if (current_function_decl != NULL_TREE
2824 && DECL_DECLARED_INLINE_P (current_function_decl)
2825 && DECL_EXTERNAL (current_function_decl)
2826 && VAR_OR_FUNCTION_DECL_P (ref)
2827 && (!VAR_P (ref) || TREE_STATIC (ref))
2828 && ! TREE_PUBLIC (ref)
2829 && DECL_CONTEXT (ref) != current_function_decl)
2830 record_inline_static (loc, current_function_decl, ref,
2831 csi_internal);
2832
2833 return ref;
2834 }
2835
2836 /* Record details of decls possibly used inside sizeof or typeof. */
2837 struct maybe_used_decl
2838 {
2839 /* The decl. */
2840 tree decl;
2841 /* The level seen at (in_sizeof + in_typeof). */
2842 int level;
2843 /* The next one at this level or above, or NULL. */
2844 struct maybe_used_decl *next;
2845 };
2846
2847 static struct maybe_used_decl *maybe_used_decls;
2848
2849 /* Record that DECL, an undefined static function reference seen
2850 inside sizeof or typeof, might be used if the operand of sizeof is
2851 a VLA type or the operand of typeof is a variably modified
2852 type. */
2853
2854 static void
2855 record_maybe_used_decl (tree decl)
2856 {
2857 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2858 t->decl = decl;
2859 t->level = in_sizeof + in_typeof;
2860 t->next = maybe_used_decls;
2861 maybe_used_decls = t;
2862 }
2863
2864 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2865 USED is false, just discard them. If it is true, mark them used
2866 (if no longer inside sizeof or typeof) or move them to the next
2867 level up (if still inside sizeof or typeof). */
2868
2869 void
2870 pop_maybe_used (bool used)
2871 {
2872 struct maybe_used_decl *p = maybe_used_decls;
2873 int cur_level = in_sizeof + in_typeof;
2874 while (p && p->level > cur_level)
2875 {
2876 if (used)
2877 {
2878 if (cur_level == 0)
2879 C_DECL_USED (p->decl) = 1;
2880 else
2881 p->level = cur_level;
2882 }
2883 p = p->next;
2884 }
2885 if (!used || cur_level == 0)
2886 maybe_used_decls = p;
2887 }
2888
2889 /* Return the result of sizeof applied to EXPR. */
2890
2891 struct c_expr
2892 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2893 {
2894 struct c_expr ret;
2895 if (expr.value == error_mark_node)
2896 {
2897 ret.value = error_mark_node;
2898 ret.original_code = ERROR_MARK;
2899 ret.original_type = NULL;
2900 pop_maybe_used (false);
2901 }
2902 else
2903 {
2904 bool expr_const_operands = true;
2905
2906 if (TREE_CODE (expr.value) == PARM_DECL
2907 && C_ARRAY_PARAMETER (expr.value))
2908 {
2909 auto_diagnostic_group d;
2910 if (warning_at (loc, OPT_Wsizeof_array_argument,
2911 "%<sizeof%> on array function parameter %qE will "
2912 "return size of %qT", expr.value,
2913 TREE_TYPE (expr.value)))
2914 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2915 }
2916 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2917 &expr_const_operands);
2918 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2919 c_last_sizeof_arg = expr.value;
2920 c_last_sizeof_loc = loc;
2921 ret.original_code = SIZEOF_EXPR;
2922 ret.original_type = NULL;
2923 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2924 {
2925 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2926 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2927 folded_expr, ret.value);
2928 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2929 SET_EXPR_LOCATION (ret.value, loc);
2930 }
2931 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2932 }
2933 return ret;
2934 }
2935
2936 /* Return the result of sizeof applied to T, a structure for the type
2937 name passed to sizeof (rather than the type itself). LOC is the
2938 location of the original expression. */
2939
2940 struct c_expr
2941 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2942 {
2943 tree type;
2944 struct c_expr ret;
2945 tree type_expr = NULL_TREE;
2946 bool type_expr_const = true;
2947 type = groktypename (t, &type_expr, &type_expr_const);
2948 ret.value = c_sizeof (loc, type);
2949 c_last_sizeof_arg = type;
2950 c_last_sizeof_loc = loc;
2951 ret.original_code = SIZEOF_EXPR;
2952 ret.original_type = NULL;
2953 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2954 && c_vla_type_p (type))
2955 {
2956 /* If the type is a [*] array, it is a VLA but is represented as
2957 having a size of zero. In such a case we must ensure that
2958 the result of sizeof does not get folded to a constant by
2959 c_fully_fold, because if the size is evaluated the result is
2960 not constant and so constraints on zero or negative size
2961 arrays must not be applied when this sizeof call is inside
2962 another array declarator. */
2963 if (!type_expr)
2964 type_expr = integer_zero_node;
2965 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2966 type_expr, ret.value);
2967 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2968 }
2969 pop_maybe_used (type != error_mark_node
2970 ? C_TYPE_VARIABLE_SIZE (type) : false);
2971 return ret;
2972 }
2973
2974 /* Build a function call to function FUNCTION with parameters PARAMS.
2975 The function call is at LOC.
2976 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2977 TREE_VALUE of each node is a parameter-expression.
2978 FUNCTION's data type may be a function type or a pointer-to-function. */
2979
2980 tree
2981 build_function_call (location_t loc, tree function, tree params)
2982 {
2983 vec<tree, va_gc> *v;
2984 tree ret;
2985
2986 vec_alloc (v, list_length (params));
2987 for (; params; params = TREE_CHAIN (params))
2988 v->quick_push (TREE_VALUE (params));
2989 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2990 vec_free (v);
2991 return ret;
2992 }
2993
2994 /* Give a note about the location of the declaration of DECL. */
2995
2996 static void
2997 inform_declaration (tree decl)
2998 {
2999 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
3000 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3001 }
3002
3003 /* Build a function call to function FUNCTION with parameters PARAMS.
3004 ORIGTYPES, if not NULL, is a vector of types; each element is
3005 either NULL or the original type of the corresponding element in
3006 PARAMS. The original type may differ from TREE_TYPE of the
3007 parameter for enums. FUNCTION's data type may be a function type
3008 or pointer-to-function. This function changes the elements of
3009 PARAMS. */
3010
3011 tree
3012 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3013 tree function, vec<tree, va_gc> *params,
3014 vec<tree, va_gc> *origtypes)
3015 {
3016 tree fntype, fundecl = NULL_TREE;
3017 tree name = NULL_TREE, result;
3018 tree tem;
3019 int nargs;
3020 tree *argarray;
3021
3022
3023 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3024 STRIP_TYPE_NOPS (function);
3025
3026 /* Convert anything with function type to a pointer-to-function. */
3027 if (TREE_CODE (function) == FUNCTION_DECL)
3028 {
3029 name = DECL_NAME (function);
3030
3031 if (flag_tm)
3032 tm_malloc_replacement (function);
3033 fundecl = function;
3034 /* Atomic functions have type checking/casting already done. They are
3035 often rewritten and don't match the original parameter list. */
3036 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3037 origtypes = NULL;
3038 }
3039 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3040 function = function_to_pointer_conversion (loc, function);
3041
3042 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3043 expressions, like those used for ObjC messenger dispatches. */
3044 if (params && !params->is_empty ())
3045 function = objc_rewrite_function_call (function, (*params)[0]);
3046
3047 function = c_fully_fold (function, false, NULL);
3048
3049 fntype = TREE_TYPE (function);
3050
3051 if (TREE_CODE (fntype) == ERROR_MARK)
3052 return error_mark_node;
3053
3054 if (!(TREE_CODE (fntype) == POINTER_TYPE
3055 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3056 {
3057 if (!flag_diagnostics_show_caret)
3058 error_at (loc,
3059 "called object %qE is not a function or function pointer",
3060 function);
3061 else if (DECL_P (function))
3062 {
3063 error_at (loc,
3064 "called object %qD is not a function or function pointer",
3065 function);
3066 inform_declaration (function);
3067 }
3068 else
3069 error_at (loc,
3070 "called object is not a function or function pointer");
3071 return error_mark_node;
3072 }
3073
3074 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3075 current_function_returns_abnormally = 1;
3076
3077 /* fntype now gets the type of function pointed to. */
3078 fntype = TREE_TYPE (fntype);
3079
3080 /* Convert the parameters to the types declared in the
3081 function prototype, or apply default promotions. */
3082
3083 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3084 origtypes, function, fundecl);
3085 if (nargs < 0)
3086 return error_mark_node;
3087
3088 /* Check that the function is called through a compatible prototype.
3089 If it is not, warn. */
3090 if (CONVERT_EXPR_P (function)
3091 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3092 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3093 && !comptypes (fntype, TREE_TYPE (tem)))
3094 {
3095 tree return_type = TREE_TYPE (fntype);
3096
3097 /* This situation leads to run-time undefined behavior. We can't,
3098 therefore, simply error unless we can prove that all possible
3099 executions of the program must execute the code. */
3100 warning_at (loc, 0, "function called through a non-compatible type");
3101
3102 if (VOID_TYPE_P (return_type)
3103 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3104 pedwarn (loc, 0,
3105 "function with qualified void return type called");
3106 }
3107
3108 argarray = vec_safe_address (params);
3109
3110 /* Check that arguments to builtin functions match the expectations. */
3111 if (fundecl && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL)
3112 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3113 argarray))
3114 return error_mark_node;
3115
3116 /* Check that the arguments to the function are valid. */
3117 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3118 nargs, argarray, &arg_loc);
3119
3120 if (name != NULL_TREE
3121 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3122 {
3123 if (require_constant_value)
3124 result
3125 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3126 function, nargs, argarray);
3127 else
3128 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3129 function, nargs, argarray);
3130 if (TREE_CODE (result) == NOP_EXPR
3131 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3132 STRIP_TYPE_NOPS (result);
3133 }
3134 else
3135 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3136 function, nargs, argarray);
3137 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3138 later. */
3139 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3140 TREE_NO_WARNING (result) = 1;
3141
3142 /* In this improbable scenario, a nested function returns a VM type.
3143 Create a TARGET_EXPR so that the call always has a LHS, much as
3144 what the C++ FE does for functions returning non-PODs. */
3145 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3146 {
3147 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3148 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3149 NULL_TREE, NULL_TREE);
3150 }
3151
3152 if (VOID_TYPE_P (TREE_TYPE (result)))
3153 {
3154 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3155 pedwarn (loc, 0,
3156 "function with qualified void return type called");
3157 return result;
3158 }
3159 return require_complete_type (loc, result);
3160 }
3161
3162 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3163
3164 tree
3165 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3166 tree function, vec<tree, va_gc> *params,
3167 vec<tree, va_gc> *origtypes)
3168 {
3169 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3170 STRIP_TYPE_NOPS (function);
3171
3172 /* Convert anything with function type to a pointer-to-function. */
3173 if (TREE_CODE (function) == FUNCTION_DECL)
3174 {
3175 /* Implement type-directed function overloading for builtins.
3176 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3177 handle all the type checking. The result is a complete expression
3178 that implements this function call. */
3179 tree tem = resolve_overloaded_builtin (loc, function, params);
3180 if (tem)
3181 return tem;
3182 }
3183 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3184 }
3185 \f
3186 /* Convert the argument expressions in the vector VALUES
3187 to the types in the list TYPELIST.
3188
3189 If TYPELIST is exhausted, or when an element has NULL as its type,
3190 perform the default conversions.
3191
3192 ORIGTYPES is the original types of the expressions in VALUES. This
3193 holds the type of enum values which have been converted to integral
3194 types. It may be NULL.
3195
3196 FUNCTION is a tree for the called function. It is used only for
3197 error messages, where it is formatted with %qE.
3198
3199 This is also where warnings about wrong number of args are generated.
3200
3201 ARG_LOC are locations of function arguments (if any).
3202
3203 Returns the actual number of arguments processed (which may be less
3204 than the length of VALUES in some error situations), or -1 on
3205 failure. */
3206
3207 static int
3208 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3209 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3210 tree function, tree fundecl)
3211 {
3212 tree typetail, val;
3213 unsigned int parmnum;
3214 bool error_args = false;
3215 const bool type_generic = fundecl
3216 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3217 bool type_generic_remove_excess_precision = false;
3218 bool type_generic_overflow_p = false;
3219 tree selector;
3220
3221 /* Change pointer to function to the function itself for
3222 diagnostics. */
3223 if (TREE_CODE (function) == ADDR_EXPR
3224 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3225 function = TREE_OPERAND (function, 0);
3226
3227 /* Handle an ObjC selector specially for diagnostics. */
3228 selector = objc_message_selector ();
3229
3230 /* For type-generic built-in functions, determine whether excess
3231 precision should be removed (classification) or not
3232 (comparison). */
3233 if (type_generic
3234 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3235 {
3236 switch (DECL_FUNCTION_CODE (fundecl))
3237 {
3238 case BUILT_IN_ISFINITE:
3239 case BUILT_IN_ISINF:
3240 case BUILT_IN_ISINF_SIGN:
3241 case BUILT_IN_ISNAN:
3242 case BUILT_IN_ISNORMAL:
3243 case BUILT_IN_FPCLASSIFY:
3244 type_generic_remove_excess_precision = true;
3245 break;
3246
3247 case BUILT_IN_ADD_OVERFLOW_P:
3248 case BUILT_IN_SUB_OVERFLOW_P:
3249 case BUILT_IN_MUL_OVERFLOW_P:
3250 /* The last argument of these type-generic builtins
3251 should not be promoted. */
3252 type_generic_overflow_p = true;
3253 break;
3254
3255 default:
3256 break;
3257 }
3258 }
3259
3260 /* Scan the given expressions and types, producing individual
3261 converted arguments. */
3262
3263 for (typetail = typelist, parmnum = 0;
3264 values && values->iterate (parmnum, &val);
3265 ++parmnum)
3266 {
3267 tree type = typetail ? TREE_VALUE (typetail) : 0;
3268 tree valtype = TREE_TYPE (val);
3269 tree rname = function;
3270 int argnum = parmnum + 1;
3271 const char *invalid_func_diag;
3272 bool excess_precision = false;
3273 bool npc;
3274 tree parmval;
3275 /* Some __atomic_* builtins have additional hidden argument at
3276 position 0. */
3277 location_t ploc
3278 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3279 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3280 : input_location;
3281
3282 if (type == void_type_node)
3283 {
3284 if (selector)
3285 error_at (loc, "too many arguments to method %qE", selector);
3286 else
3287 error_at (loc, "too many arguments to function %qE", function);
3288 inform_declaration (fundecl);
3289 return error_args ? -1 : (int) parmnum;
3290 }
3291
3292 if (selector && argnum > 2)
3293 {
3294 rname = selector;
3295 argnum -= 2;
3296 }
3297
3298 npc = null_pointer_constant_p (val);
3299
3300 /* If there is excess precision and a prototype, convert once to
3301 the required type rather than converting via the semantic
3302 type. Likewise without a prototype a float value represented
3303 as long double should be converted once to double. But for
3304 type-generic classification functions excess precision must
3305 be removed here. */
3306 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3307 && (type || !type_generic || !type_generic_remove_excess_precision))
3308 {
3309 val = TREE_OPERAND (val, 0);
3310 excess_precision = true;
3311 }
3312 val = c_fully_fold (val, false, NULL);
3313 STRIP_TYPE_NOPS (val);
3314
3315 val = require_complete_type (ploc, val);
3316
3317 /* Some floating-point arguments must be promoted to double when
3318 no type is specified by a prototype. This applies to
3319 arguments of type float, and to architecture-specific types
3320 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3321 bool promote_float_arg = false;
3322 if (type == NULL_TREE
3323 && TREE_CODE (valtype) == REAL_TYPE
3324 && (TYPE_PRECISION (valtype)
3325 <= TYPE_PRECISION (double_type_node))
3326 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3327 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3328 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3329 {
3330 /* Promote this argument, unless it has a _FloatN or
3331 _FloatNx type. */
3332 promote_float_arg = true;
3333 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3334 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3335 {
3336 promote_float_arg = false;
3337 break;
3338 }
3339 }
3340
3341 if (type != NULL_TREE)
3342 {
3343 /* Formal parm type is specified by a function prototype. */
3344
3345 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3346 {
3347 error_at (ploc, "type of formal parameter %d is incomplete",
3348 parmnum + 1);
3349 parmval = val;
3350 }
3351 else
3352 {
3353 tree origtype;
3354
3355 /* Optionally warn about conversions that
3356 differ from the default conversions. */
3357 if (warn_traditional_conversion || warn_traditional)
3358 {
3359 unsigned int formal_prec = TYPE_PRECISION (type);
3360
3361 if (INTEGRAL_TYPE_P (type)
3362 && TREE_CODE (valtype) == REAL_TYPE)
3363 warning_at (ploc, OPT_Wtraditional_conversion,
3364 "passing argument %d of %qE as integer rather "
3365 "than floating due to prototype",
3366 argnum, rname);
3367 if (INTEGRAL_TYPE_P (type)
3368 && TREE_CODE (valtype) == COMPLEX_TYPE)
3369 warning_at (ploc, OPT_Wtraditional_conversion,
3370 "passing argument %d of %qE as integer rather "
3371 "than complex due to prototype",
3372 argnum, rname);
3373 else if (TREE_CODE (type) == COMPLEX_TYPE
3374 && TREE_CODE (valtype) == REAL_TYPE)
3375 warning_at (ploc, OPT_Wtraditional_conversion,
3376 "passing argument %d of %qE as complex rather "
3377 "than floating due to prototype",
3378 argnum, rname);
3379 else if (TREE_CODE (type) == REAL_TYPE
3380 && INTEGRAL_TYPE_P (valtype))
3381 warning_at (ploc, OPT_Wtraditional_conversion,
3382 "passing argument %d of %qE as floating rather "
3383 "than integer due to prototype",
3384 argnum, rname);
3385 else if (TREE_CODE (type) == COMPLEX_TYPE
3386 && INTEGRAL_TYPE_P (valtype))
3387 warning_at (ploc, OPT_Wtraditional_conversion,
3388 "passing argument %d of %qE as complex rather "
3389 "than integer due to prototype",
3390 argnum, rname);
3391 else if (TREE_CODE (type) == REAL_TYPE
3392 && TREE_CODE (valtype) == COMPLEX_TYPE)
3393 warning_at (ploc, OPT_Wtraditional_conversion,
3394 "passing argument %d of %qE as floating rather "
3395 "than complex due to prototype",
3396 argnum, rname);
3397 /* ??? At some point, messages should be written about
3398 conversions between complex types, but that's too messy
3399 to do now. */
3400 else if (TREE_CODE (type) == REAL_TYPE
3401 && TREE_CODE (valtype) == REAL_TYPE)
3402 {
3403 /* Warn if any argument is passed as `float',
3404 since without a prototype it would be `double'. */
3405 if (formal_prec == TYPE_PRECISION (float_type_node)
3406 && type != dfloat32_type_node)
3407 warning_at (ploc, 0,
3408 "passing argument %d of %qE as %<float%> "
3409 "rather than %<double%> due to prototype",
3410 argnum, rname);
3411
3412 /* Warn if mismatch between argument and prototype
3413 for decimal float types. Warn of conversions with
3414 binary float types and of precision narrowing due to
3415 prototype. */
3416 else if (type != valtype
3417 && (type == dfloat32_type_node
3418 || type == dfloat64_type_node
3419 || type == dfloat128_type_node
3420 || valtype == dfloat32_type_node
3421 || valtype == dfloat64_type_node
3422 || valtype == dfloat128_type_node)
3423 && (formal_prec
3424 <= TYPE_PRECISION (valtype)
3425 || (type == dfloat128_type_node
3426 && (valtype
3427 != dfloat64_type_node
3428 && (valtype
3429 != dfloat32_type_node)))
3430 || (type == dfloat64_type_node
3431 && (valtype
3432 != dfloat32_type_node))))
3433 warning_at (ploc, 0,
3434 "passing argument %d of %qE as %qT "
3435 "rather than %qT due to prototype",
3436 argnum, rname, type, valtype);
3437
3438 }
3439 /* Detect integer changing in width or signedness.
3440 These warnings are only activated with
3441 -Wtraditional-conversion, not with -Wtraditional. */
3442 else if (warn_traditional_conversion
3443 && INTEGRAL_TYPE_P (type)
3444 && INTEGRAL_TYPE_P (valtype))
3445 {
3446 tree would_have_been = default_conversion (val);
3447 tree type1 = TREE_TYPE (would_have_been);
3448
3449 if (val == error_mark_node)
3450 /* VAL could have been of incomplete type. */;
3451 else if (TREE_CODE (type) == ENUMERAL_TYPE
3452 && (TYPE_MAIN_VARIANT (type)
3453 == TYPE_MAIN_VARIANT (valtype)))
3454 /* No warning if function asks for enum
3455 and the actual arg is that enum type. */
3456 ;
3457 else if (formal_prec != TYPE_PRECISION (type1))
3458 warning_at (ploc, OPT_Wtraditional_conversion,
3459 "passing argument %d of %qE "
3460 "with different width due to prototype",
3461 argnum, rname);
3462 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3463 ;
3464 /* Don't complain if the formal parameter type
3465 is an enum, because we can't tell now whether
3466 the value was an enum--even the same enum. */
3467 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3468 ;
3469 else if (TREE_CODE (val) == INTEGER_CST
3470 && int_fits_type_p (val, type))
3471 /* Change in signedness doesn't matter
3472 if a constant value is unaffected. */
3473 ;
3474 /* If the value is extended from a narrower
3475 unsigned type, it doesn't matter whether we
3476 pass it as signed or unsigned; the value
3477 certainly is the same either way. */
3478 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3479 && TYPE_UNSIGNED (valtype))
3480 ;
3481 else if (TYPE_UNSIGNED (type))
3482 warning_at (ploc, OPT_Wtraditional_conversion,
3483 "passing argument %d of %qE "
3484 "as unsigned due to prototype",
3485 argnum, rname);
3486 else
3487 warning_at (ploc, OPT_Wtraditional_conversion,
3488 "passing argument %d of %qE "
3489 "as signed due to prototype",
3490 argnum, rname);
3491 }
3492 }
3493
3494 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3495 sake of better warnings from convert_and_check. */
3496 if (excess_precision)
3497 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3498 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3499 parmval = convert_for_assignment (loc, ploc, type,
3500 val, origtype, ic_argpass,
3501 npc, fundecl, function,
3502 parmnum + 1);
3503
3504 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3505 && INTEGRAL_TYPE_P (type)
3506 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3507 parmval = default_conversion (parmval);
3508 }
3509 }
3510 else if (promote_float_arg)
3511 {
3512 if (type_generic)
3513 parmval = val;
3514 else
3515 {
3516 /* Convert `float' to `double'. */
3517 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3518 warning_at (ploc, OPT_Wdouble_promotion,
3519 "implicit conversion from %qT to %qT when passing "
3520 "argument to function",
3521 valtype, double_type_node);
3522 parmval = convert (double_type_node, val);
3523 }
3524 }
3525 else if ((excess_precision && !type_generic)
3526 || (type_generic_overflow_p && parmnum == 2))
3527 /* A "double" argument with excess precision being passed
3528 without a prototype or in variable arguments.
3529 The last argument of __builtin_*_overflow_p should not be
3530 promoted. */
3531 parmval = convert (valtype, val);
3532 else if ((invalid_func_diag =
3533 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3534 {
3535 error (invalid_func_diag);
3536 return -1;
3537 }
3538 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3539 {
3540 return -1;
3541 }
3542 else
3543 /* Convert `short' and `char' to full-size `int'. */
3544 parmval = default_conversion (val);
3545
3546 (*values)[parmnum] = parmval;
3547 if (parmval == error_mark_node)
3548 error_args = true;
3549
3550 if (typetail)
3551 typetail = TREE_CHAIN (typetail);
3552 }
3553
3554 gcc_assert (parmnum == vec_safe_length (values));
3555
3556 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3557 {
3558 error_at (loc, "too few arguments to function %qE", function);
3559 inform_declaration (fundecl);
3560 return -1;
3561 }
3562
3563 return error_args ? -1 : (int) parmnum;
3564 }
3565 \f
3566 /* This is the entry point used by the parser to build unary operators
3567 in the input. CODE, a tree_code, specifies the unary operator, and
3568 ARG is the operand. For unary plus, the C parser currently uses
3569 CONVERT_EXPR for code.
3570
3571 LOC is the location to use for the tree generated.
3572 */
3573
3574 struct c_expr
3575 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3576 {
3577 struct c_expr result;
3578
3579 result.original_code = code;
3580 result.original_type = NULL;
3581
3582 if (reject_gcc_builtin (arg.value))
3583 {
3584 result.value = error_mark_node;
3585 }
3586 else
3587 {
3588 result.value = build_unary_op (loc, code, arg.value, false);
3589
3590 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3591 overflow_warning (loc, result.value, arg.value);
3592 }
3593
3594 /* We are typically called when parsing a prefix token at LOC acting on
3595 ARG. Reflect this by updating the source range of the result to
3596 start at LOC and end at the end of ARG. */
3597 set_c_expr_source_range (&result,
3598 loc, arg.get_finish ());
3599
3600 return result;
3601 }
3602
3603 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3604
3605 static bool
3606 char_type_p (tree type)
3607 {
3608 return (type == char_type_node
3609 || type == unsigned_char_type_node
3610 || type == signed_char_type_node
3611 || type == char16_type_node
3612 || type == char32_type_node);
3613 }
3614
3615 /* This is the entry point used by the parser to build binary operators
3616 in the input. CODE, a tree_code, specifies the binary operator, and
3617 ARG1 and ARG2 are the operands. In addition to constructing the
3618 expression, we check for operands that were written with other binary
3619 operators in a way that is likely to confuse the user.
3620
3621 LOCATION is the location of the binary operator. */
3622
3623 struct c_expr
3624 parser_build_binary_op (location_t location, enum tree_code code,
3625 struct c_expr arg1, struct c_expr arg2)
3626 {
3627 struct c_expr result;
3628
3629 enum tree_code code1 = arg1.original_code;
3630 enum tree_code code2 = arg2.original_code;
3631 tree type1 = (arg1.original_type
3632 ? arg1.original_type
3633 : TREE_TYPE (arg1.value));
3634 tree type2 = (arg2.original_type
3635 ? arg2.original_type
3636 : TREE_TYPE (arg2.value));
3637
3638 result.value = build_binary_op (location, code,
3639 arg1.value, arg2.value, true);
3640 result.original_code = code;
3641 result.original_type = NULL;
3642
3643 if (TREE_CODE (result.value) == ERROR_MARK)
3644 {
3645 set_c_expr_source_range (&result,
3646 arg1.get_start (),
3647 arg2.get_finish ());
3648 return result;
3649 }
3650
3651 if (location != UNKNOWN_LOCATION)
3652 protected_set_expr_location (result.value, location);
3653
3654 set_c_expr_source_range (&result,
3655 arg1.get_start (),
3656 arg2.get_finish ());
3657
3658 /* Check for cases such as x+y<<z which users are likely
3659 to misinterpret. */
3660 if (warn_parentheses)
3661 warn_about_parentheses (location, code, code1, arg1.value, code2,
3662 arg2.value);
3663
3664 if (warn_logical_op)
3665 warn_logical_operator (location, code, TREE_TYPE (result.value),
3666 code1, arg1.value, code2, arg2.value);
3667
3668 if (warn_tautological_compare)
3669 {
3670 tree lhs = arg1.value;
3671 tree rhs = arg2.value;
3672 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3673 {
3674 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3675 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3676 lhs = NULL_TREE;
3677 else
3678 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3679 }
3680 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3681 {
3682 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3683 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3684 rhs = NULL_TREE;
3685 else
3686 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3687 }
3688 if (lhs != NULL_TREE && rhs != NULL_TREE)
3689 warn_tautological_cmp (location, code, lhs, rhs);
3690 }
3691
3692 if (warn_logical_not_paren
3693 && TREE_CODE_CLASS (code) == tcc_comparison
3694 && code1 == TRUTH_NOT_EXPR
3695 && code2 != TRUTH_NOT_EXPR
3696 /* Avoid warning for !!x == y. */
3697 && (TREE_CODE (arg1.value) != NE_EXPR
3698 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3699 {
3700 /* Avoid warning for !b == y where b has _Bool type. */
3701 tree t = integer_zero_node;
3702 if (TREE_CODE (arg1.value) == EQ_EXPR
3703 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3704 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3705 {
3706 t = TREE_OPERAND (arg1.value, 0);
3707 do
3708 {
3709 if (TREE_TYPE (t) != integer_type_node)
3710 break;
3711 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3712 t = C_MAYBE_CONST_EXPR_EXPR (t);
3713 else if (CONVERT_EXPR_P (t))
3714 t = TREE_OPERAND (t, 0);
3715 else
3716 break;
3717 }
3718 while (1);
3719 }
3720 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3721 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3722 }
3723
3724 /* Warn about comparisons against string literals, with the exception
3725 of testing for equality or inequality of a string literal with NULL. */
3726 if (code == EQ_EXPR || code == NE_EXPR)
3727 {
3728 if ((code1 == STRING_CST
3729 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3730 || (code2 == STRING_CST
3731 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3732 warning_at (location, OPT_Waddress,
3733 "comparison with string literal results in unspecified behavior");
3734 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3735 if (POINTER_TYPE_P (type1)
3736 && null_pointer_constant_p (arg2.value)
3737 && char_type_p (type2))
3738 {
3739 auto_diagnostic_group d;
3740 if (warning_at (location, OPT_Wpointer_compare,
3741 "comparison between pointer and zero character "
3742 "constant"))
3743 inform (arg1.get_start (),
3744 "did you mean to dereference the pointer?");
3745 }
3746 else if (POINTER_TYPE_P (type2)
3747 && null_pointer_constant_p (arg1.value)
3748 && char_type_p (type1))
3749 {
3750 auto_diagnostic_group d;
3751 if (warning_at (location, OPT_Wpointer_compare,
3752 "comparison between pointer and zero character "
3753 "constant"))
3754 inform (arg2.get_start (),
3755 "did you mean to dereference the pointer?");
3756 }
3757 }
3758 else if (TREE_CODE_CLASS (code) == tcc_comparison
3759 && (code1 == STRING_CST || code2 == STRING_CST))
3760 warning_at (location, OPT_Waddress,
3761 "comparison with string literal results in unspecified behavior");
3762
3763 if (TREE_OVERFLOW_P (result.value)
3764 && !TREE_OVERFLOW_P (arg1.value)
3765 && !TREE_OVERFLOW_P (arg2.value))
3766 overflow_warning (location, result.value);
3767
3768 /* Warn about comparisons of different enum types. */
3769 if (warn_enum_compare
3770 && TREE_CODE_CLASS (code) == tcc_comparison
3771 && TREE_CODE (type1) == ENUMERAL_TYPE
3772 && TREE_CODE (type2) == ENUMERAL_TYPE
3773 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3774 warning_at (location, OPT_Wenum_compare,
3775 "comparison between %qT and %qT",
3776 type1, type2);
3777
3778 return result;
3779 }
3780 \f
3781 /* Return a tree for the difference of pointers OP0 and OP1.
3782 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3783 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3784
3785 static tree
3786 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3787 {
3788 tree restype = ptrdiff_type_node;
3789 tree result, inttype;
3790
3791 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3792 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3793 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3794 tree orig_op1 = op1;
3795
3796 /* If the operands point into different address spaces, we need to
3797 explicitly convert them to pointers into the common address space
3798 before we can subtract the numerical address values. */
3799 if (as0 != as1)
3800 {
3801 addr_space_t as_common;
3802 tree common_type;
3803
3804 /* Determine the common superset address space. This is guaranteed
3805 to exist because the caller verified that comp_target_types
3806 returned non-zero. */
3807 if (!addr_space_superset (as0, as1, &as_common))
3808 gcc_unreachable ();
3809
3810 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3811 op0 = convert (common_type, op0);
3812 op1 = convert (common_type, op1);
3813 }
3814
3815 /* Determine integer type result of the subtraction. This will usually
3816 be the same as the result type (ptrdiff_t), but may need to be a wider
3817 type if pointers for the address space are wider than ptrdiff_t. */
3818 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3819 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3820 else
3821 inttype = restype;
3822
3823 if (TREE_CODE (target_type) == VOID_TYPE)
3824 pedwarn (loc, OPT_Wpointer_arith,
3825 "pointer of type %<void *%> used in subtraction");
3826 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3827 pedwarn (loc, OPT_Wpointer_arith,
3828 "pointer to a function used in subtraction");
3829
3830 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3831 {
3832 gcc_assert (current_function_decl != NULL_TREE);
3833
3834 op0 = save_expr (op0);
3835 op1 = save_expr (op1);
3836
3837 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3838 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3839 }
3840
3841 /* First do the subtraction, then build the divide operator
3842 and only convert at the very end.
3843 Do not do default conversions in case restype is a short type. */
3844
3845 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3846 pointers. If some platform cannot provide that, or has a larger
3847 ptrdiff_type to support differences larger than half the address
3848 space, cast the pointers to some larger integer type and do the
3849 computations in that type. */
3850 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3851 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3852 convert (inttype, op1), false);
3853 else
3854 {
3855 /* Cast away qualifiers. */
3856 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
3857 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
3858 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3859 }
3860
3861 /* This generates an error if op1 is pointer to incomplete type. */
3862 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3863 error_at (loc, "arithmetic on pointer to an incomplete type");
3864
3865 op1 = c_size_in_bytes (target_type);
3866
3867 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3868 error_at (loc, "arithmetic on pointer to an empty aggregate");
3869
3870 /* Divide by the size, in easiest possible way. */
3871 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3872 op0, convert (inttype, op1));
3873
3874 /* Convert to final result type if necessary. */
3875 return convert (restype, result);
3876 }
3877 \f
3878 /* Expand atomic compound assignments into an appropriate sequence as
3879 specified by the C11 standard section 6.5.16.2.
3880
3881 _Atomic T1 E1
3882 T2 E2
3883 E1 op= E2
3884
3885 This sequence is used for all types for which these operations are
3886 supported.
3887
3888 In addition, built-in versions of the 'fe' prefixed routines may
3889 need to be invoked for floating point (real, complex or vector) when
3890 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3891
3892 T1 newval;
3893 T1 old;
3894 T1 *addr
3895 T2 val
3896 fenv_t fenv
3897
3898 addr = &E1;
3899 val = (E2);
3900 __atomic_load (addr, &old, SEQ_CST);
3901 feholdexcept (&fenv);
3902 loop:
3903 newval = old op val;
3904 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3905 SEQ_CST))
3906 goto done;
3907 feclearexcept (FE_ALL_EXCEPT);
3908 goto loop:
3909 done:
3910 feupdateenv (&fenv);
3911
3912 The compiler will issue the __atomic_fetch_* built-in when possible,
3913 otherwise it will generate the generic form of the atomic operations.
3914 This requires temp(s) and has their address taken. The atomic processing
3915 is smart enough to figure out when the size of an object can utilize
3916 a lock-free version, and convert the built-in call to the appropriate
3917 lock-free routine. The optimizers will then dispose of any temps that
3918 are no longer required, and lock-free implementations are utilized as
3919 long as there is target support for the required size.
3920
3921 If the operator is NOP_EXPR, then this is a simple assignment, and
3922 an __atomic_store is issued to perform the assignment rather than
3923 the above loop. */
3924
3925 /* Build an atomic assignment at LOC, expanding into the proper
3926 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3927 the result of the operation, unless RETURN_OLD_P, in which case
3928 return the old value of LHS (this is only for postincrement and
3929 postdecrement). */
3930
3931 static tree
3932 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3933 tree rhs, bool return_old_p)
3934 {
3935 tree fndecl, func_call;
3936 vec<tree, va_gc> *params;
3937 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3938 tree old, old_addr;
3939 tree compound_stmt;
3940 tree stmt, goto_stmt;
3941 tree loop_label, loop_decl, done_label, done_decl;
3942
3943 tree lhs_type = TREE_TYPE (lhs);
3944 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
3945 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3946 tree rhs_semantic_type = TREE_TYPE (rhs);
3947 tree nonatomic_rhs_semantic_type;
3948 tree rhs_type;
3949
3950 gcc_assert (TYPE_ATOMIC (lhs_type));
3951
3952 if (return_old_p)
3953 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3954
3955 /* Allocate enough vector items for a compare_exchange. */
3956 vec_alloc (params, 6);
3957
3958 /* Create a compound statement to hold the sequence of statements
3959 with a loop. */
3960 compound_stmt = c_begin_compound_stmt (false);
3961
3962 /* Remove any excess precision (which is only present here in the
3963 case of compound assignments). */
3964 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3965 {
3966 gcc_assert (modifycode != NOP_EXPR);
3967 rhs = TREE_OPERAND (rhs, 0);
3968 }
3969 rhs_type = TREE_TYPE (rhs);
3970
3971 /* Fold the RHS if it hasn't already been folded. */
3972 if (modifycode != NOP_EXPR)
3973 rhs = c_fully_fold (rhs, false, NULL);
3974
3975 /* Remove the qualifiers for the rest of the expressions and create
3976 the VAL temp variable to hold the RHS. */
3977 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3978 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3979 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3980 TYPE_UNQUALIFIED);
3981 val = create_tmp_var_raw (nonatomic_rhs_type);
3982 TREE_ADDRESSABLE (val) = 1;
3983 TREE_NO_WARNING (val) = 1;
3984 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3985 NULL_TREE);
3986 SET_EXPR_LOCATION (rhs, loc);
3987 add_stmt (rhs);
3988
3989 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3990 an atomic_store. */
3991 if (modifycode == NOP_EXPR)
3992 {
3993 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3994 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
3995 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3996 params->quick_push (lhs_addr);
3997 params->quick_push (rhs);
3998 params->quick_push (seq_cst);
3999 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4000 add_stmt (func_call);
4001
4002 /* Finish the compound statement. */
4003 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4004
4005 /* VAL is the value which was stored, return a COMPOUND_STMT of
4006 the statement and that value. */
4007 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4008 }
4009
4010 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4011 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4012 isn't applicable for such builtins. ??? Do we want to handle enums? */
4013 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4014 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4015 {
4016 built_in_function fncode;
4017 switch (modifycode)
4018 {
4019 case PLUS_EXPR:
4020 case POINTER_PLUS_EXPR:
4021 fncode = (return_old_p
4022 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4023 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4024 break;
4025 case MINUS_EXPR:
4026 fncode = (return_old_p
4027 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4028 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4029 break;
4030 case BIT_AND_EXPR:
4031 fncode = (return_old_p
4032 ? BUILT_IN_ATOMIC_FETCH_AND_N
4033 : BUILT_IN_ATOMIC_AND_FETCH_N);
4034 break;
4035 case BIT_IOR_EXPR:
4036 fncode = (return_old_p
4037 ? BUILT_IN_ATOMIC_FETCH_OR_N
4038 : BUILT_IN_ATOMIC_OR_FETCH_N);
4039 break;
4040 case BIT_XOR_EXPR:
4041 fncode = (return_old_p
4042 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4043 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4044 break;
4045 default:
4046 goto cas_loop;
4047 }
4048
4049 /* We can only use "_1" through "_16" variants of the atomic fetch
4050 built-ins. */
4051 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4052 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4053 goto cas_loop;
4054
4055 /* If this is a pointer type, we need to multiply by the size of
4056 the pointer target type. */
4057 if (POINTER_TYPE_P (lhs_type))
4058 {
4059 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4060 /* ??? This would introduce -Wdiscarded-qualifiers
4061 warning: __atomic_fetch_* expect volatile void *
4062 type as the first argument. (Assignments between
4063 atomic and non-atomic objects are OK.) */
4064 || TYPE_RESTRICT (lhs_type))
4065 goto cas_loop;
4066 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4067 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4068 convert (ptrdiff_type_node, rhs),
4069 convert (ptrdiff_type_node, sz));
4070 }
4071
4072 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4073 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4074 fndecl = builtin_decl_explicit (fncode);
4075 params->quick_push (lhs_addr);
4076 params->quick_push (rhs);
4077 params->quick_push (seq_cst);
4078 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4079
4080 newval = create_tmp_var_raw (nonatomic_lhs_type);
4081 TREE_ADDRESSABLE (newval) = 1;
4082 TREE_NO_WARNING (newval) = 1;
4083 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4084 NULL_TREE, NULL_TREE);
4085 SET_EXPR_LOCATION (rhs, loc);
4086 add_stmt (rhs);
4087
4088 /* Finish the compound statement. */
4089 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4090
4091 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4092 the statement and that value. */
4093 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4094 }
4095
4096 cas_loop:
4097 /* Create the variables and labels required for the op= form. */
4098 old = create_tmp_var_raw (nonatomic_lhs_type);
4099 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4100 TREE_ADDRESSABLE (old) = 1;
4101 TREE_NO_WARNING (old) = 1;
4102
4103 newval = create_tmp_var_raw (nonatomic_lhs_type);
4104 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4105 TREE_ADDRESSABLE (newval) = 1;
4106 TREE_NO_WARNING (newval) = 1;
4107
4108 loop_decl = create_artificial_label (loc);
4109 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4110
4111 done_decl = create_artificial_label (loc);
4112 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4113
4114 /* __atomic_load (addr, &old, SEQ_CST). */
4115 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4116 params->quick_push (lhs_addr);
4117 params->quick_push (old_addr);
4118 params->quick_push (seq_cst);
4119 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4120 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4121 NULL_TREE);
4122 add_stmt (old);
4123 params->truncate (0);
4124
4125 /* Create the expressions for floating-point environment
4126 manipulation, if required. */
4127 bool need_fenv = (flag_trapping_math
4128 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4129 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4130 if (need_fenv)
4131 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4132
4133 if (hold_call)
4134 add_stmt (hold_call);
4135
4136 /* loop: */
4137 add_stmt (loop_label);
4138
4139 /* newval = old + val; */
4140 if (rhs_type != rhs_semantic_type)
4141 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4142 rhs = build_binary_op (loc, modifycode, old, val, true);
4143 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4144 {
4145 tree eptype = TREE_TYPE (rhs);
4146 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4147 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4148 }
4149 else
4150 rhs = c_fully_fold (rhs, false, NULL);
4151 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4152 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4153 NULL_TREE, 0);
4154 if (rhs != error_mark_node)
4155 {
4156 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4157 NULL_TREE);
4158 SET_EXPR_LOCATION (rhs, loc);
4159 add_stmt (rhs);
4160 }
4161
4162 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4163 goto done; */
4164 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4165 params->quick_push (lhs_addr);
4166 params->quick_push (old_addr);
4167 params->quick_push (newval_addr);
4168 params->quick_push (integer_zero_node);
4169 params->quick_push (seq_cst);
4170 params->quick_push (seq_cst);
4171 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4172
4173 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4174 SET_EXPR_LOCATION (goto_stmt, loc);
4175
4176 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4177 SET_EXPR_LOCATION (stmt, loc);
4178 add_stmt (stmt);
4179
4180 if (clear_call)
4181 add_stmt (clear_call);
4182
4183 /* goto loop; */
4184 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4185 SET_EXPR_LOCATION (goto_stmt, loc);
4186 add_stmt (goto_stmt);
4187
4188 /* done: */
4189 add_stmt (done_label);
4190
4191 if (update_call)
4192 add_stmt (update_call);
4193
4194 /* Finish the compound statement. */
4195 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4196
4197 /* NEWVAL is the value that was successfully stored, return a
4198 COMPOUND_EXPR of the statement and the appropriate value. */
4199 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4200 return_old_p ? old : newval);
4201 }
4202
4203 /* Construct and perhaps optimize a tree representation
4204 for a unary operation. CODE, a tree_code, specifies the operation
4205 and XARG is the operand.
4206 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4207 promotions (such as from short to int).
4208 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4209 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4210 to pointers in C99.
4211
4212 LOCATION is the location of the operator. */
4213
4214 tree
4215 build_unary_op (location_t location, enum tree_code code, tree xarg,
4216 bool noconvert)
4217 {
4218 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4219 tree arg = xarg;
4220 tree argtype = NULL_TREE;
4221 enum tree_code typecode;
4222 tree val;
4223 tree ret = error_mark_node;
4224 tree eptype = NULL_TREE;
4225 const char *invalid_op_diag;
4226 bool int_operands;
4227
4228 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4229 if (int_operands)
4230 arg = remove_c_maybe_const_expr (arg);
4231
4232 if (code != ADDR_EXPR)
4233 arg = require_complete_type (location, arg);
4234
4235 typecode = TREE_CODE (TREE_TYPE (arg));
4236 if (typecode == ERROR_MARK)
4237 return error_mark_node;
4238 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4239 typecode = INTEGER_TYPE;
4240
4241 if ((invalid_op_diag
4242 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4243 {
4244 error_at (location, invalid_op_diag);
4245 return error_mark_node;
4246 }
4247
4248 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4249 {
4250 eptype = TREE_TYPE (arg);
4251 arg = TREE_OPERAND (arg, 0);
4252 }
4253
4254 switch (code)
4255 {
4256 case CONVERT_EXPR:
4257 /* This is used for unary plus, because a CONVERT_EXPR
4258 is enough to prevent anybody from looking inside for
4259 associativity, but won't generate any code. */
4260 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4261 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4262 || typecode == VECTOR_TYPE))
4263 {
4264 error_at (location, "wrong type argument to unary plus");
4265 return error_mark_node;
4266 }
4267 else if (!noconvert)
4268 arg = default_conversion (arg);
4269 arg = non_lvalue_loc (location, arg);
4270 break;
4271
4272 case NEGATE_EXPR:
4273 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4274 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4275 || typecode == VECTOR_TYPE))
4276 {
4277 error_at (location, "wrong type argument to unary minus");
4278 return error_mark_node;
4279 }
4280 else if (!noconvert)
4281 arg = default_conversion (arg);
4282 break;
4283
4284 case BIT_NOT_EXPR:
4285 /* ~ works on integer types and non float vectors. */
4286 if (typecode == INTEGER_TYPE
4287 || (typecode == VECTOR_TYPE
4288 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4289 {
4290 tree e = arg;
4291
4292 /* Warn if the expression has boolean value. */
4293 while (TREE_CODE (e) == COMPOUND_EXPR)
4294 e = TREE_OPERAND (e, 1);
4295
4296 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4297 || truth_value_p (TREE_CODE (e))))
4298 {
4299 auto_diagnostic_group d;
4300 if (warning_at (location, OPT_Wbool_operation,
4301 "%<~%> on a boolean expression"))
4302 {
4303 gcc_rich_location richloc (location);
4304 richloc.add_fixit_insert_before (location, "!");
4305 inform (&richloc, "did you mean to use logical not?");
4306 }
4307 }
4308 if (!noconvert)
4309 arg = default_conversion (arg);
4310 }
4311 else if (typecode == COMPLEX_TYPE)
4312 {
4313 code = CONJ_EXPR;
4314 pedwarn (location, OPT_Wpedantic,
4315 "ISO C does not support %<~%> for complex conjugation");
4316 if (!noconvert)
4317 arg = default_conversion (arg);
4318 }
4319 else
4320 {
4321 error_at (location, "wrong type argument to bit-complement");
4322 return error_mark_node;
4323 }
4324 break;
4325
4326 case ABS_EXPR:
4327 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4328 {
4329 error_at (location, "wrong type argument to abs");
4330 return error_mark_node;
4331 }
4332 else if (!noconvert)
4333 arg = default_conversion (arg);
4334 break;
4335
4336 case ABSU_EXPR:
4337 if (!(typecode == INTEGER_TYPE))
4338 {
4339 error_at (location, "wrong type argument to absu");
4340 return error_mark_node;
4341 }
4342 else if (!noconvert)
4343 arg = default_conversion (arg);
4344 break;
4345
4346 case CONJ_EXPR:
4347 /* Conjugating a real value is a no-op, but allow it anyway. */
4348 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4349 || typecode == COMPLEX_TYPE))
4350 {
4351 error_at (location, "wrong type argument to conjugation");
4352 return error_mark_node;
4353 }
4354 else if (!noconvert)
4355 arg = default_conversion (arg);
4356 break;
4357
4358 case TRUTH_NOT_EXPR:
4359 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4360 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4361 && typecode != COMPLEX_TYPE)
4362 {
4363 error_at (location,
4364 "wrong type argument to unary exclamation mark");
4365 return error_mark_node;
4366 }
4367 if (int_operands)
4368 {
4369 arg = c_objc_common_truthvalue_conversion (location, xarg);
4370 arg = remove_c_maybe_const_expr (arg);
4371 }
4372 else
4373 arg = c_objc_common_truthvalue_conversion (location, arg);
4374 ret = invert_truthvalue_loc (location, arg);
4375 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4376 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4377 location = EXPR_LOCATION (ret);
4378 goto return_build_unary_op;
4379
4380 case REALPART_EXPR:
4381 case IMAGPART_EXPR:
4382 ret = build_real_imag_expr (location, code, arg);
4383 if (ret == error_mark_node)
4384 return error_mark_node;
4385 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4386 eptype = TREE_TYPE (eptype);
4387 goto return_build_unary_op;
4388
4389 case PREINCREMENT_EXPR:
4390 case POSTINCREMENT_EXPR:
4391 case PREDECREMENT_EXPR:
4392 case POSTDECREMENT_EXPR:
4393
4394 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4395 {
4396 tree inner = build_unary_op (location, code,
4397 C_MAYBE_CONST_EXPR_EXPR (arg),
4398 noconvert);
4399 if (inner == error_mark_node)
4400 return error_mark_node;
4401 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4402 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4403 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4404 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4405 goto return_build_unary_op;
4406 }
4407
4408 /* Complain about anything that is not a true lvalue. In
4409 Objective-C, skip this check for property_refs. */
4410 if (!objc_is_property_ref (arg)
4411 && !lvalue_or_else (location,
4412 arg, ((code == PREINCREMENT_EXPR
4413 || code == POSTINCREMENT_EXPR)
4414 ? lv_increment
4415 : lv_decrement)))
4416 return error_mark_node;
4417
4418 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4419 {
4420 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4421 warning_at (location, OPT_Wc___compat,
4422 "increment of enumeration value is invalid in C++");
4423 else
4424 warning_at (location, OPT_Wc___compat,
4425 "decrement of enumeration value is invalid in C++");
4426 }
4427
4428 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4429 {
4430 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4431 warning_at (location, OPT_Wbool_operation,
4432 "increment of a boolean expression");
4433 else
4434 warning_at (location, OPT_Wbool_operation,
4435 "decrement of a boolean expression");
4436 }
4437
4438 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4439 arg = c_fully_fold (arg, false, NULL, true);
4440
4441 bool atomic_op;
4442 atomic_op = really_atomic_lvalue (arg);
4443
4444 /* Increment or decrement the real part of the value,
4445 and don't change the imaginary part. */
4446 if (typecode == COMPLEX_TYPE)
4447 {
4448 tree real, imag;
4449
4450 pedwarn (location, OPT_Wpedantic,
4451 "ISO C does not support %<++%> and %<--%> on complex types");
4452
4453 if (!atomic_op)
4454 {
4455 arg = stabilize_reference (arg);
4456 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4457 true);
4458 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4459 true);
4460 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4461 if (real == error_mark_node || imag == error_mark_node)
4462 return error_mark_node;
4463 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4464 real, imag);
4465 goto return_build_unary_op;
4466 }
4467 }
4468
4469 /* Report invalid types. */
4470
4471 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4472 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4473 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4474 {
4475 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4476 error_at (location, "wrong type argument to increment");
4477 else
4478 error_at (location, "wrong type argument to decrement");
4479
4480 return error_mark_node;
4481 }
4482
4483 {
4484 tree inc;
4485
4486 argtype = TREE_TYPE (arg);
4487
4488 /* Compute the increment. */
4489
4490 if (typecode == POINTER_TYPE)
4491 {
4492 /* If pointer target is an incomplete type,
4493 we just cannot know how to do the arithmetic. */
4494 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4495 {
4496 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4497 error_at (location,
4498 "increment of pointer to an incomplete type %qT",
4499 TREE_TYPE (argtype));
4500 else
4501 error_at (location,
4502 "decrement of pointer to an incomplete type %qT",
4503 TREE_TYPE (argtype));
4504 }
4505 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4506 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4507 {
4508 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4509 pedwarn (location, OPT_Wpointer_arith,
4510 "wrong type argument to increment");
4511 else
4512 pedwarn (location, OPT_Wpointer_arith,
4513 "wrong type argument to decrement");
4514 }
4515
4516 inc = c_size_in_bytes (TREE_TYPE (argtype));
4517 inc = convert_to_ptrofftype_loc (location, inc);
4518 }
4519 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4520 {
4521 /* For signed fract types, we invert ++ to -- or
4522 -- to ++, and change inc from 1 to -1, because
4523 it is not possible to represent 1 in signed fract constants.
4524 For unsigned fract types, the result always overflows and
4525 we get an undefined (original) or the maximum value. */
4526 if (code == PREINCREMENT_EXPR)
4527 code = PREDECREMENT_EXPR;
4528 else if (code == PREDECREMENT_EXPR)
4529 code = PREINCREMENT_EXPR;
4530 else if (code == POSTINCREMENT_EXPR)
4531 code = POSTDECREMENT_EXPR;
4532 else /* code == POSTDECREMENT_EXPR */
4533 code = POSTINCREMENT_EXPR;
4534
4535 inc = integer_minus_one_node;
4536 inc = convert (argtype, inc);
4537 }
4538 else
4539 {
4540 inc = VECTOR_TYPE_P (argtype)
4541 ? build_one_cst (argtype)
4542 : integer_one_node;
4543 inc = convert (argtype, inc);
4544 }
4545
4546 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4547 need to ask Objective-C to build the increment or decrement
4548 expression for it. */
4549 if (objc_is_property_ref (arg))
4550 return objc_build_incr_expr_for_property_ref (location, code,
4551 arg, inc);
4552
4553 /* Report a read-only lvalue. */
4554 if (TYPE_READONLY (argtype))
4555 {
4556 readonly_error (location, arg,
4557 ((code == PREINCREMENT_EXPR
4558 || code == POSTINCREMENT_EXPR)
4559 ? lv_increment : lv_decrement));
4560 return error_mark_node;
4561 }
4562 else if (TREE_READONLY (arg))
4563 readonly_warning (arg,
4564 ((code == PREINCREMENT_EXPR
4565 || code == POSTINCREMENT_EXPR)
4566 ? lv_increment : lv_decrement));
4567
4568 /* If the argument is atomic, use the special code sequences for
4569 atomic compound assignment. */
4570 if (atomic_op)
4571 {
4572 arg = stabilize_reference (arg);
4573 ret = build_atomic_assign (location, arg,
4574 ((code == PREINCREMENT_EXPR
4575 || code == POSTINCREMENT_EXPR)
4576 ? PLUS_EXPR
4577 : MINUS_EXPR),
4578 (FRACT_MODE_P (TYPE_MODE (argtype))
4579 ? inc
4580 : integer_one_node),
4581 (code == POSTINCREMENT_EXPR
4582 || code == POSTDECREMENT_EXPR));
4583 goto return_build_unary_op;
4584 }
4585
4586 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4587 val = boolean_increment (code, arg);
4588 else
4589 val = build2 (code, TREE_TYPE (arg), arg, inc);
4590 TREE_SIDE_EFFECTS (val) = 1;
4591 if (TREE_CODE (val) != code)
4592 TREE_NO_WARNING (val) = 1;
4593 ret = val;
4594 goto return_build_unary_op;
4595 }
4596
4597 case ADDR_EXPR:
4598 /* Note that this operation never does default_conversion. */
4599
4600 /* The operand of unary '&' must be an lvalue (which excludes
4601 expressions of type void), or, in C99, the result of a [] or
4602 unary '*' operator. */
4603 if (VOID_TYPE_P (TREE_TYPE (arg))
4604 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4605 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4606 pedwarn (location, 0, "taking address of expression of type %<void%>");
4607
4608 /* Let &* cancel out to simplify resulting code. */
4609 if (INDIRECT_REF_P (arg))
4610 {
4611 /* Don't let this be an lvalue. */
4612 if (lvalue_p (TREE_OPERAND (arg, 0)))
4613 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4614 ret = TREE_OPERAND (arg, 0);
4615 goto return_build_unary_op;
4616 }
4617
4618 /* Anything not already handled and not a true memory reference
4619 or a non-lvalue array is an error. */
4620 if (typecode != FUNCTION_TYPE && !noconvert
4621 && !lvalue_or_else (location, arg, lv_addressof))
4622 return error_mark_node;
4623
4624 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4625 folding later. */
4626 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4627 {
4628 tree inner = build_unary_op (location, code,
4629 C_MAYBE_CONST_EXPR_EXPR (arg),
4630 noconvert);
4631 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4632 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4633 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4634 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4635 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4636 goto return_build_unary_op;
4637 }
4638
4639 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4640 argtype = TREE_TYPE (arg);
4641
4642 /* If the lvalue is const or volatile, merge that into the type
4643 to which the address will point. This is only needed
4644 for function types. */
4645 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4646 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4647 && TREE_CODE (argtype) == FUNCTION_TYPE)
4648 {
4649 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4650 int quals = orig_quals;
4651
4652 if (TREE_READONLY (arg))
4653 quals |= TYPE_QUAL_CONST;
4654 if (TREE_THIS_VOLATILE (arg))
4655 quals |= TYPE_QUAL_VOLATILE;
4656
4657 argtype = c_build_qualified_type (argtype, quals);
4658 }
4659
4660 switch (TREE_CODE (arg))
4661 {
4662 case COMPONENT_REF:
4663 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4664 {
4665 error_at (location, "cannot take address of bit-field %qD",
4666 TREE_OPERAND (arg, 1));
4667 return error_mark_node;
4668 }
4669
4670 /* fall through */
4671
4672 case ARRAY_REF:
4673 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4674 {
4675 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4676 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4677 {
4678 error_at (location, "cannot take address of scalar with "
4679 "reverse storage order");
4680 return error_mark_node;
4681 }
4682
4683 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4684 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4685 warning_at (location, OPT_Wscalar_storage_order,
4686 "address of array with reverse scalar storage "
4687 "order requested");
4688 }
4689
4690 default:
4691 break;
4692 }
4693
4694 if (!c_mark_addressable (arg))
4695 return error_mark_node;
4696
4697 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4698 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4699
4700 argtype = build_pointer_type (argtype);
4701
4702 /* ??? Cope with user tricks that amount to offsetof. Delete this
4703 when we have proper support for integer constant expressions. */
4704 val = get_base_address (arg);
4705 if (val && INDIRECT_REF_P (val)
4706 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4707 {
4708 ret = fold_offsetof (arg, argtype);
4709 goto return_build_unary_op;
4710 }
4711
4712 val = build1 (ADDR_EXPR, argtype, arg);
4713
4714 ret = val;
4715 goto return_build_unary_op;
4716
4717 default:
4718 gcc_unreachable ();
4719 }
4720
4721 if (argtype == NULL_TREE)
4722 argtype = TREE_TYPE (arg);
4723 if (TREE_CODE (arg) == INTEGER_CST)
4724 ret = (require_constant_value
4725 ? fold_build1_initializer_loc (location, code, argtype, arg)
4726 : fold_build1_loc (location, code, argtype, arg));
4727 else
4728 ret = build1 (code, argtype, arg);
4729 return_build_unary_op:
4730 gcc_assert (ret != error_mark_node);
4731 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4732 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4733 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4734 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4735 ret = note_integer_operands (ret);
4736 if (eptype)
4737 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4738 protected_set_expr_location (ret, location);
4739 return ret;
4740 }
4741
4742 /* Return nonzero if REF is an lvalue valid for this language.
4743 Lvalues can be assigned, unless their type has TYPE_READONLY.
4744 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4745
4746 bool
4747 lvalue_p (const_tree ref)
4748 {
4749 const enum tree_code code = TREE_CODE (ref);
4750
4751 switch (code)
4752 {
4753 case REALPART_EXPR:
4754 case IMAGPART_EXPR:
4755 case COMPONENT_REF:
4756 return lvalue_p (TREE_OPERAND (ref, 0));
4757
4758 case C_MAYBE_CONST_EXPR:
4759 return lvalue_p (TREE_OPERAND (ref, 1));
4760
4761 case COMPOUND_LITERAL_EXPR:
4762 case STRING_CST:
4763 return true;
4764
4765 case INDIRECT_REF:
4766 case ARRAY_REF:
4767 case VAR_DECL:
4768 case PARM_DECL:
4769 case RESULT_DECL:
4770 case ERROR_MARK:
4771 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4772 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4773
4774 case BIND_EXPR:
4775 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4776
4777 default:
4778 return false;
4779 }
4780 }
4781 \f
4782 /* Give a warning for storing in something that is read-only in GCC
4783 terms but not const in ISO C terms. */
4784
4785 static void
4786 readonly_warning (tree arg, enum lvalue_use use)
4787 {
4788 switch (use)
4789 {
4790 case lv_assign:
4791 warning (0, "assignment of read-only location %qE", arg);
4792 break;
4793 case lv_increment:
4794 warning (0, "increment of read-only location %qE", arg);
4795 break;
4796 case lv_decrement:
4797 warning (0, "decrement of read-only location %qE", arg);
4798 break;
4799 default:
4800 gcc_unreachable ();
4801 }
4802 return;
4803 }
4804
4805
4806 /* Return nonzero if REF is an lvalue valid for this language;
4807 otherwise, print an error message and return zero. USE says
4808 how the lvalue is being used and so selects the error message.
4809 LOCATION is the location at which any error should be reported. */
4810
4811 static int
4812 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4813 {
4814 int win = lvalue_p (ref);
4815
4816 if (!win)
4817 lvalue_error (loc, use);
4818
4819 return win;
4820 }
4821 \f
4822 /* Mark EXP saying that we need to be able to take the
4823 address of it; it should not be allocated in a register.
4824 Returns true if successful. ARRAY_REF_P is true if this
4825 is for ARRAY_REF construction - in that case we don't want
4826 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4827 it is fine to use ARRAY_REFs for vector subscripts on vector
4828 register variables. */
4829
4830 bool
4831 c_mark_addressable (tree exp, bool array_ref_p)
4832 {
4833 tree x = exp;
4834
4835 while (1)
4836 switch (TREE_CODE (x))
4837 {
4838 case VIEW_CONVERT_EXPR:
4839 if (array_ref_p
4840 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4841 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4842 return true;
4843 /* FALLTHRU */
4844 case COMPONENT_REF:
4845 case ADDR_EXPR:
4846 case ARRAY_REF:
4847 case REALPART_EXPR:
4848 case IMAGPART_EXPR:
4849 x = TREE_OPERAND (x, 0);
4850 break;
4851
4852 case COMPOUND_LITERAL_EXPR:
4853 TREE_ADDRESSABLE (x) = 1;
4854 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
4855 return true;
4856
4857 case CONSTRUCTOR:
4858 TREE_ADDRESSABLE (x) = 1;
4859 return true;
4860
4861 case VAR_DECL:
4862 case CONST_DECL:
4863 case PARM_DECL:
4864 case RESULT_DECL:
4865 if (C_DECL_REGISTER (x)
4866 && DECL_NONLOCAL (x))
4867 {
4868 if (TREE_PUBLIC (x) || is_global_var (x))
4869 {
4870 error
4871 ("global register variable %qD used in nested function", x);
4872 return false;
4873 }
4874 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4875 }
4876 else if (C_DECL_REGISTER (x))
4877 {
4878 if (TREE_PUBLIC (x) || is_global_var (x))
4879 error ("address of global register variable %qD requested", x);
4880 else
4881 error ("address of register variable %qD requested", x);
4882 return false;
4883 }
4884
4885 /* FALLTHRU */
4886 case FUNCTION_DECL:
4887 TREE_ADDRESSABLE (x) = 1;
4888 /* FALLTHRU */
4889 default:
4890 return true;
4891 }
4892 }
4893 \f
4894 /* Convert EXPR to TYPE, warning about conversion problems with
4895 constants. SEMANTIC_TYPE is the type this conversion would use
4896 without excess precision. If SEMANTIC_TYPE is NULL, this function
4897 is equivalent to convert_and_check. This function is a wrapper that
4898 handles conversions that may be different than
4899 the usual ones because of excess precision. */
4900
4901 static tree
4902 ep_convert_and_check (location_t loc, tree type, tree expr,
4903 tree semantic_type)
4904 {
4905 if (TREE_TYPE (expr) == type)
4906 return expr;
4907
4908 /* For C11, integer conversions may have results with excess
4909 precision. */
4910 if (flag_isoc11 || !semantic_type)
4911 return convert_and_check (loc, type, expr);
4912
4913 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4914 && TREE_TYPE (expr) != semantic_type)
4915 {
4916 /* For integers, we need to check the real conversion, not
4917 the conversion to the excess precision type. */
4918 expr = convert_and_check (loc, semantic_type, expr);
4919 }
4920 /* Result type is the excess precision type, which should be
4921 large enough, so do not check. */
4922 return convert (type, expr);
4923 }
4924
4925 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4926 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4927 if folded to an integer constant then the unselected half may
4928 contain arbitrary operations not normally permitted in constant
4929 expressions. Set the location of the expression to LOC. */
4930
4931 tree
4932 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4933 tree op1, tree op1_original_type, location_t op1_loc,
4934 tree op2, tree op2_original_type, location_t op2_loc)
4935 {
4936 tree type1;
4937 tree type2;
4938 enum tree_code code1;
4939 enum tree_code code2;
4940 tree result_type = NULL;
4941 tree semantic_result_type = NULL;
4942 tree orig_op1 = op1, orig_op2 = op2;
4943 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4944 bool ifexp_int_operands;
4945 tree ret;
4946
4947 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4948 if (op1_int_operands)
4949 op1 = remove_c_maybe_const_expr (op1);
4950 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4951 if (op2_int_operands)
4952 op2 = remove_c_maybe_const_expr (op2);
4953 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4954 if (ifexp_int_operands)
4955 ifexp = remove_c_maybe_const_expr (ifexp);
4956
4957 /* Promote both alternatives. */
4958
4959 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4960 op1 = default_conversion (op1);
4961 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4962 op2 = default_conversion (op2);
4963
4964 if (TREE_CODE (ifexp) == ERROR_MARK
4965 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4966 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4967 return error_mark_node;
4968
4969 type1 = TREE_TYPE (op1);
4970 code1 = TREE_CODE (type1);
4971 type2 = TREE_TYPE (op2);
4972 code2 = TREE_CODE (type2);
4973
4974 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4975 return error_mark_node;
4976
4977 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4978 return error_mark_node;
4979
4980 /* C90 does not permit non-lvalue arrays in conditional expressions.
4981 In C99 they will be pointers by now. */
4982 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4983 {
4984 error_at (colon_loc, "non-lvalue array in conditional expression");
4985 return error_mark_node;
4986 }
4987
4988 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4989 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4990 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4991 || code1 == COMPLEX_TYPE)
4992 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4993 || code2 == COMPLEX_TYPE))
4994 {
4995 semantic_result_type = c_common_type (type1, type2);
4996 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4997 {
4998 op1 = TREE_OPERAND (op1, 0);
4999 type1 = TREE_TYPE (op1);
5000 gcc_assert (TREE_CODE (type1) == code1);
5001 }
5002 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5003 {
5004 op2 = TREE_OPERAND (op2, 0);
5005 type2 = TREE_TYPE (op2);
5006 gcc_assert (TREE_CODE (type2) == code2);
5007 }
5008 }
5009
5010 if (warn_cxx_compat)
5011 {
5012 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5013 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5014
5015 if (TREE_CODE (t1) == ENUMERAL_TYPE
5016 && TREE_CODE (t2) == ENUMERAL_TYPE
5017 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5018 warning_at (colon_loc, OPT_Wc___compat,
5019 ("different enum types in conditional is "
5020 "invalid in C++: %qT vs %qT"),
5021 t1, t2);
5022 }
5023
5024 /* Quickly detect the usual case where op1 and op2 have the same type
5025 after promotion. */
5026 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5027 {
5028 if (type1 == type2)
5029 result_type = type1;
5030 else
5031 result_type = TYPE_MAIN_VARIANT (type1);
5032 }
5033 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5034 || code1 == COMPLEX_TYPE)
5035 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5036 || code2 == COMPLEX_TYPE))
5037 {
5038 /* In C11, a conditional expression between a floating-point
5039 type and an integer type should convert the integer type to
5040 the evaluation format of the floating-point type, with
5041 possible excess precision. */
5042 tree eptype1 = type1;
5043 tree eptype2 = type2;
5044 if (flag_isoc11)
5045 {
5046 tree eptype;
5047 if (ANY_INTEGRAL_TYPE_P (type1)
5048 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5049 {
5050 eptype2 = eptype;
5051 if (!semantic_result_type)
5052 semantic_result_type = c_common_type (type1, type2);
5053 }
5054 else if (ANY_INTEGRAL_TYPE_P (type2)
5055 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5056 {
5057 eptype1 = eptype;
5058 if (!semantic_result_type)
5059 semantic_result_type = c_common_type (type1, type2);
5060 }
5061 }
5062 result_type = c_common_type (eptype1, eptype2);
5063 if (result_type == error_mark_node)
5064 return error_mark_node;
5065 do_warn_double_promotion (result_type, type1, type2,
5066 "implicit conversion from %qT to %qT to "
5067 "match other result of conditional",
5068 colon_loc);
5069
5070 /* If -Wsign-compare, warn here if type1 and type2 have
5071 different signedness. We'll promote the signed to unsigned
5072 and later code won't know it used to be different.
5073 Do this check on the original types, so that explicit casts
5074 will be considered, but default promotions won't. */
5075 if (c_inhibit_evaluation_warnings == 0)
5076 {
5077 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5078 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5079
5080 if (unsigned_op1 ^ unsigned_op2)
5081 {
5082 bool ovf;
5083
5084 /* Do not warn if the result type is signed, since the
5085 signed type will only be chosen if it can represent
5086 all the values of the unsigned type. */
5087 if (!TYPE_UNSIGNED (result_type))
5088 /* OK */;
5089 else
5090 {
5091 bool op1_maybe_const = true;
5092 bool op2_maybe_const = true;
5093
5094 /* Do not warn if the signed quantity is an
5095 unsuffixed integer literal (or some static
5096 constant expression involving such literals) and
5097 it is non-negative. This warning requires the
5098 operands to be folded for best results, so do
5099 that folding in this case even without
5100 warn_sign_compare to avoid warning options
5101 possibly affecting code generation. */
5102 c_inhibit_evaluation_warnings
5103 += (ifexp == truthvalue_false_node);
5104 op1 = c_fully_fold (op1, require_constant_value,
5105 &op1_maybe_const);
5106 c_inhibit_evaluation_warnings
5107 -= (ifexp == truthvalue_false_node);
5108
5109 c_inhibit_evaluation_warnings
5110 += (ifexp == truthvalue_true_node);
5111 op2 = c_fully_fold (op2, require_constant_value,
5112 &op2_maybe_const);
5113 c_inhibit_evaluation_warnings
5114 -= (ifexp == truthvalue_true_node);
5115
5116 if (warn_sign_compare)
5117 {
5118 if ((unsigned_op2
5119 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5120 || (unsigned_op1
5121 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5122 /* OK */;
5123 else if (unsigned_op2)
5124 warning_at (op1_loc, OPT_Wsign_compare,
5125 "operand of ?: changes signedness from "
5126 "%qT to %qT due to unsignedness of other "
5127 "operand", TREE_TYPE (orig_op1),
5128 TREE_TYPE (orig_op2));
5129 else
5130 warning_at (op2_loc, OPT_Wsign_compare,
5131 "operand of ?: changes signedness from "
5132 "%qT to %qT due to unsignedness of other "
5133 "operand", TREE_TYPE (orig_op2),
5134 TREE_TYPE (orig_op1));
5135 }
5136 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5137 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5138 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5139 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5140 }
5141 }
5142 }
5143 }
5144 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5145 {
5146 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5147 pedwarn (colon_loc, OPT_Wpedantic,
5148 "ISO C forbids conditional expr with only one void side");
5149 result_type = void_type_node;
5150 }
5151 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5152 {
5153 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5154 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5155 addr_space_t as_common;
5156
5157 if (comp_target_types (colon_loc, type1, type2))
5158 result_type = common_pointer_type (type1, type2);
5159 else if (null_pointer_constant_p (orig_op1))
5160 result_type = type2;
5161 else if (null_pointer_constant_p (orig_op2))
5162 result_type = type1;
5163 else if (!addr_space_superset (as1, as2, &as_common))
5164 {
5165 error_at (colon_loc, "pointers to disjoint address spaces "
5166 "used in conditional expression");
5167 return error_mark_node;
5168 }
5169 else if (VOID_TYPE_P (TREE_TYPE (type1))
5170 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5171 {
5172 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5173 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5174 & ~TYPE_QUALS (TREE_TYPE (type1))))
5175 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5176 "pointer to array loses qualifier "
5177 "in conditional expression");
5178
5179 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5180 pedwarn (colon_loc, OPT_Wpedantic,
5181 "ISO C forbids conditional expr between "
5182 "%<void *%> and function pointer");
5183 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5184 TREE_TYPE (type2)));
5185 }
5186 else if (VOID_TYPE_P (TREE_TYPE (type2))
5187 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5188 {
5189 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5190 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5191 & ~TYPE_QUALS (TREE_TYPE (type2))))
5192 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5193 "pointer to array loses qualifier "
5194 "in conditional expression");
5195
5196 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5197 pedwarn (colon_loc, OPT_Wpedantic,
5198 "ISO C forbids conditional expr between "
5199 "%<void *%> and function pointer");
5200 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5201 TREE_TYPE (type1)));
5202 }
5203 /* Objective-C pointer comparisons are a bit more lenient. */
5204 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5205 result_type = objc_common_type (type1, type2);
5206 else
5207 {
5208 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5209
5210 pedwarn (colon_loc, 0,
5211 "pointer type mismatch in conditional expression");
5212 result_type = build_pointer_type
5213 (build_qualified_type (void_type_node, qual));
5214 }
5215 }
5216 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5217 {
5218 if (!null_pointer_constant_p (orig_op2))
5219 pedwarn (colon_loc, 0,
5220 "pointer/integer type mismatch in conditional expression");
5221 else
5222 {
5223 op2 = null_pointer_node;
5224 }
5225 result_type = type1;
5226 }
5227 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5228 {
5229 if (!null_pointer_constant_p (orig_op1))
5230 pedwarn (colon_loc, 0,
5231 "pointer/integer type mismatch in conditional expression");
5232 else
5233 {
5234 op1 = null_pointer_node;
5235 }
5236 result_type = type2;
5237 }
5238
5239 if (!result_type)
5240 {
5241 if (flag_cond_mismatch)
5242 result_type = void_type_node;
5243 else
5244 {
5245 error_at (colon_loc, "type mismatch in conditional expression");
5246 return error_mark_node;
5247 }
5248 }
5249
5250 /* Merge const and volatile flags of the incoming types. */
5251 result_type
5252 = build_type_variant (result_type,
5253 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5254 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5255
5256 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5257 semantic_result_type);
5258 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5259 semantic_result_type);
5260
5261 if (ifexp_bcp && ifexp == truthvalue_true_node)
5262 {
5263 op2_int_operands = true;
5264 op1 = c_fully_fold (op1, require_constant_value, NULL);
5265 }
5266 if (ifexp_bcp && ifexp == truthvalue_false_node)
5267 {
5268 op1_int_operands = true;
5269 op2 = c_fully_fold (op2, require_constant_value, NULL);
5270 }
5271 int_const = int_operands = (ifexp_int_operands
5272 && op1_int_operands
5273 && op2_int_operands);
5274 if (int_operands)
5275 {
5276 int_const = ((ifexp == truthvalue_true_node
5277 && TREE_CODE (orig_op1) == INTEGER_CST
5278 && !TREE_OVERFLOW (orig_op1))
5279 || (ifexp == truthvalue_false_node
5280 && TREE_CODE (orig_op2) == INTEGER_CST
5281 && !TREE_OVERFLOW (orig_op2)));
5282 }
5283
5284 /* Need to convert condition operand into a vector mask. */
5285 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5286 {
5287 tree vectype = TREE_TYPE (ifexp);
5288 tree elem_type = TREE_TYPE (vectype);
5289 tree zero = build_int_cst (elem_type, 0);
5290 tree zero_vec = build_vector_from_val (vectype, zero);
5291 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5292 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5293 }
5294
5295 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5296 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5297 else
5298 {
5299 if (int_operands)
5300 {
5301 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5302 nested inside of the expression. */
5303 op1 = c_fully_fold (op1, false, NULL);
5304 op2 = c_fully_fold (op2, false, NULL);
5305 }
5306 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5307 if (int_operands)
5308 ret = note_integer_operands (ret);
5309 }
5310 if (semantic_result_type)
5311 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5312
5313 protected_set_expr_location (ret, colon_loc);
5314
5315 /* If the OP1 and OP2 are the same and don't have side-effects,
5316 warn here, because the COND_EXPR will be turned into OP1. */
5317 if (warn_duplicated_branches
5318 && TREE_CODE (ret) == COND_EXPR
5319 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5320 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5321 "this condition has identical branches");
5322
5323 return ret;
5324 }
5325 \f
5326 /* Return a compound expression that performs two expressions and
5327 returns the value of the second of them.
5328
5329 LOC is the location of the COMPOUND_EXPR. */
5330
5331 tree
5332 build_compound_expr (location_t loc, tree expr1, tree expr2)
5333 {
5334 bool expr1_int_operands, expr2_int_operands;
5335 tree eptype = NULL_TREE;
5336 tree ret;
5337
5338 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5339 if (expr1_int_operands)
5340 expr1 = remove_c_maybe_const_expr (expr1);
5341 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5342 if (expr2_int_operands)
5343 expr2 = remove_c_maybe_const_expr (expr2);
5344
5345 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5346 expr1 = TREE_OPERAND (expr1, 0);
5347 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5348 {
5349 eptype = TREE_TYPE (expr2);
5350 expr2 = TREE_OPERAND (expr2, 0);
5351 }
5352
5353 if (!TREE_SIDE_EFFECTS (expr1))
5354 {
5355 /* The left-hand operand of a comma expression is like an expression
5356 statement: with -Wunused, we should warn if it doesn't have
5357 any side-effects, unless it was explicitly cast to (void). */
5358 if (warn_unused_value)
5359 {
5360 if (VOID_TYPE_P (TREE_TYPE (expr1))
5361 && CONVERT_EXPR_P (expr1))
5362 ; /* (void) a, b */
5363 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5364 && TREE_CODE (expr1) == COMPOUND_EXPR
5365 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5366 ; /* (void) a, (void) b, c */
5367 else
5368 warning_at (loc, OPT_Wunused_value,
5369 "left-hand operand of comma expression has no effect");
5370 }
5371 }
5372 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5373 && warn_unused_value)
5374 {
5375 tree r = expr1;
5376 location_t cloc = loc;
5377 while (TREE_CODE (r) == COMPOUND_EXPR)
5378 {
5379 if (EXPR_HAS_LOCATION (r))
5380 cloc = EXPR_LOCATION (r);
5381 r = TREE_OPERAND (r, 1);
5382 }
5383 if (!TREE_SIDE_EFFECTS (r)
5384 && !VOID_TYPE_P (TREE_TYPE (r))
5385 && !CONVERT_EXPR_P (r))
5386 warning_at (cloc, OPT_Wunused_value,
5387 "right-hand operand of comma expression has no effect");
5388 }
5389
5390 /* With -Wunused, we should also warn if the left-hand operand does have
5391 side-effects, but computes a value which is not used. For example, in
5392 `foo() + bar(), baz()' the result of the `+' operator is not used,
5393 so we should issue a warning. */
5394 else if (warn_unused_value)
5395 warn_if_unused_value (expr1, loc);
5396
5397 if (expr2 == error_mark_node)
5398 return error_mark_node;
5399
5400 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5401
5402 if (flag_isoc99
5403 && expr1_int_operands
5404 && expr2_int_operands)
5405 ret = note_integer_operands (ret);
5406
5407 if (eptype)
5408 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5409
5410 protected_set_expr_location (ret, loc);
5411 return ret;
5412 }
5413
5414 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5415 which we are casting. OTYPE is the type of the expression being
5416 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5417 of the cast. -Wcast-qual appeared on the command line. Named
5418 address space qualifiers are not handled here, because they result
5419 in different warnings. */
5420
5421 static void
5422 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5423 {
5424 tree in_type = type;
5425 tree in_otype = otype;
5426 int added = 0;
5427 int discarded = 0;
5428 bool is_const;
5429
5430 /* Check that the qualifiers on IN_TYPE are a superset of the
5431 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5432 nodes is uninteresting and we stop as soon as we hit a
5433 non-POINTER_TYPE node on either type. */
5434 do
5435 {
5436 in_otype = TREE_TYPE (in_otype);
5437 in_type = TREE_TYPE (in_type);
5438
5439 /* GNU C allows cv-qualified function types. 'const' means the
5440 function is very pure, 'volatile' means it can't return. We
5441 need to warn when such qualifiers are added, not when they're
5442 taken away. */
5443 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5444 && TREE_CODE (in_type) == FUNCTION_TYPE)
5445 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5446 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5447 else
5448 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5449 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5450 }
5451 while (TREE_CODE (in_type) == POINTER_TYPE
5452 && TREE_CODE (in_otype) == POINTER_TYPE);
5453
5454 if (added)
5455 warning_at (loc, OPT_Wcast_qual,
5456 "cast adds %q#v qualifier to function type", added);
5457
5458 if (discarded)
5459 /* There are qualifiers present in IN_OTYPE that are not present
5460 in IN_TYPE. */
5461 warning_at (loc, OPT_Wcast_qual,
5462 "cast discards %qv qualifier from pointer target type",
5463 discarded);
5464
5465 if (added || discarded)
5466 return;
5467
5468 /* A cast from **T to const **T is unsafe, because it can cause a
5469 const value to be changed with no additional warning. We only
5470 issue this warning if T is the same on both sides, and we only
5471 issue the warning if there are the same number of pointers on
5472 both sides, as otherwise the cast is clearly unsafe anyhow. A
5473 cast is unsafe when a qualifier is added at one level and const
5474 is not present at all outer levels.
5475
5476 To issue this warning, we check at each level whether the cast
5477 adds new qualifiers not already seen. We don't need to special
5478 case function types, as they won't have the same
5479 TYPE_MAIN_VARIANT. */
5480
5481 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5482 return;
5483 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5484 return;
5485
5486 in_type = type;
5487 in_otype = otype;
5488 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5489 do
5490 {
5491 in_type = TREE_TYPE (in_type);
5492 in_otype = TREE_TYPE (in_otype);
5493 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5494 && !is_const)
5495 {
5496 warning_at (loc, OPT_Wcast_qual,
5497 "to be safe all intermediate pointers in cast from "
5498 "%qT to %qT must be %<const%> qualified",
5499 otype, type);
5500 break;
5501 }
5502 if (is_const)
5503 is_const = TYPE_READONLY (in_type);
5504 }
5505 while (TREE_CODE (in_type) == POINTER_TYPE);
5506 }
5507
5508 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5509
5510 static bool
5511 c_safe_arg_type_equiv_p (tree t1, tree t2)
5512 {
5513 t1 = TYPE_MAIN_VARIANT (t1);
5514 t2 = TYPE_MAIN_VARIANT (t2);
5515
5516 if (TREE_CODE (t1) == POINTER_TYPE
5517 && TREE_CODE (t2) == POINTER_TYPE)
5518 return true;
5519
5520 /* The signedness of the parameter matters only when an integral
5521 type smaller than int is promoted to int, otherwise only the
5522 precision of the parameter matters.
5523 This check should make sure that the callee does not see
5524 undefined values in argument registers. */
5525 if (INTEGRAL_TYPE_P (t1)
5526 && INTEGRAL_TYPE_P (t2)
5527 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5528 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5529 || !targetm.calls.promote_prototypes (NULL_TREE)
5530 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5531 return true;
5532
5533 return comptypes (t1, t2);
5534 }
5535
5536 /* Check if a type cast between two function types can be considered safe. */
5537
5538 static bool
5539 c_safe_function_type_cast_p (tree t1, tree t2)
5540 {
5541 if (TREE_TYPE (t1) == void_type_node &&
5542 TYPE_ARG_TYPES (t1) == void_list_node)
5543 return true;
5544
5545 if (TREE_TYPE (t2) == void_type_node &&
5546 TYPE_ARG_TYPES (t2) == void_list_node)
5547 return true;
5548
5549 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5550 return false;
5551
5552 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5553 t1 && t2;
5554 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5555 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5556 return false;
5557
5558 return true;
5559 }
5560
5561 /* Build an expression representing a cast to type TYPE of expression EXPR.
5562 LOC is the location of the cast-- typically the open paren of the cast. */
5563
5564 tree
5565 build_c_cast (location_t loc, tree type, tree expr)
5566 {
5567 tree value;
5568
5569 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5570 expr = TREE_OPERAND (expr, 0);
5571
5572 value = expr;
5573
5574 if (type == error_mark_node || expr == error_mark_node)
5575 return error_mark_node;
5576
5577 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5578 only in <protocol> qualifications. But when constructing cast expressions,
5579 the protocols do matter and must be kept around. */
5580 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5581 return build1 (NOP_EXPR, type, expr);
5582
5583 type = TYPE_MAIN_VARIANT (type);
5584
5585 if (TREE_CODE (type) == ARRAY_TYPE)
5586 {
5587 error_at (loc, "cast specifies array type");
5588 return error_mark_node;
5589 }
5590
5591 if (TREE_CODE (type) == FUNCTION_TYPE)
5592 {
5593 error_at (loc, "cast specifies function type");
5594 return error_mark_node;
5595 }
5596
5597 if (!VOID_TYPE_P (type))
5598 {
5599 value = require_complete_type (loc, value);
5600 if (value == error_mark_node)
5601 return error_mark_node;
5602 }
5603
5604 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5605 {
5606 if (RECORD_OR_UNION_TYPE_P (type))
5607 pedwarn (loc, OPT_Wpedantic,
5608 "ISO C forbids casting nonscalar to the same type");
5609
5610 /* Convert to remove any qualifiers from VALUE's type. */
5611 value = convert (type, value);
5612 }
5613 else if (TREE_CODE (type) == UNION_TYPE)
5614 {
5615 tree field;
5616
5617 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5618 if (TREE_TYPE (field) != error_mark_node
5619 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5620 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5621 break;
5622
5623 if (field)
5624 {
5625 tree t;
5626 bool maybe_const = true;
5627
5628 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5629 t = c_fully_fold (value, false, &maybe_const);
5630 t = build_constructor_single (type, field, t);
5631 if (!maybe_const)
5632 t = c_wrap_maybe_const (t, true);
5633 t = digest_init (loc, type, t,
5634 NULL_TREE, false, true, 0);
5635 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5636 return t;
5637 }
5638 error_at (loc, "cast to union type from type not present in union");
5639 return error_mark_node;
5640 }
5641 else
5642 {
5643 tree otype, ovalue;
5644
5645 if (type == void_type_node)
5646 {
5647 tree t = build1 (CONVERT_EXPR, type, value);
5648 SET_EXPR_LOCATION (t, loc);
5649 return t;
5650 }
5651
5652 otype = TREE_TYPE (value);
5653
5654 /* Optionally warn about potentially worrisome casts. */
5655 if (warn_cast_qual
5656 && TREE_CODE (type) == POINTER_TYPE
5657 && TREE_CODE (otype) == POINTER_TYPE)
5658 handle_warn_cast_qual (loc, type, otype);
5659
5660 /* Warn about conversions between pointers to disjoint
5661 address spaces. */
5662 if (TREE_CODE (type) == POINTER_TYPE
5663 && TREE_CODE (otype) == POINTER_TYPE
5664 && !null_pointer_constant_p (value))
5665 {
5666 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5667 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5668 addr_space_t as_common;
5669
5670 if (!addr_space_superset (as_to, as_from, &as_common))
5671 {
5672 if (ADDR_SPACE_GENERIC_P (as_from))
5673 warning_at (loc, 0, "cast to %s address space pointer "
5674 "from disjoint generic address space pointer",
5675 c_addr_space_name (as_to));
5676
5677 else if (ADDR_SPACE_GENERIC_P (as_to))
5678 warning_at (loc, 0, "cast to generic address space pointer "
5679 "from disjoint %s address space pointer",
5680 c_addr_space_name (as_from));
5681
5682 else
5683 warning_at (loc, 0, "cast to %s address space pointer "
5684 "from disjoint %s address space pointer",
5685 c_addr_space_name (as_to),
5686 c_addr_space_name (as_from));
5687 }
5688 }
5689
5690 /* Warn about possible alignment problems. */
5691 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5692 && TREE_CODE (type) == POINTER_TYPE
5693 && TREE_CODE (otype) == POINTER_TYPE
5694 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5695 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5696 /* Don't warn about opaque types, where the actual alignment
5697 restriction is unknown. */
5698 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5699 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5700 && min_align_of_type (TREE_TYPE (type))
5701 > min_align_of_type (TREE_TYPE (otype)))
5702 warning_at (loc, OPT_Wcast_align,
5703 "cast increases required alignment of target type");
5704
5705 if (TREE_CODE (type) == INTEGER_TYPE
5706 && TREE_CODE (otype) == POINTER_TYPE
5707 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5708 /* Unlike conversion of integers to pointers, where the
5709 warning is disabled for converting constants because
5710 of cases such as SIG_*, warn about converting constant
5711 pointers to integers. In some cases it may cause unwanted
5712 sign extension, and a warning is appropriate. */
5713 warning_at (loc, OPT_Wpointer_to_int_cast,
5714 "cast from pointer to integer of different size");
5715
5716 if (TREE_CODE (value) == CALL_EXPR
5717 && TREE_CODE (type) != TREE_CODE (otype))
5718 warning_at (loc, OPT_Wbad_function_cast,
5719 "cast from function call of type %qT "
5720 "to non-matching type %qT", otype, type);
5721
5722 if (TREE_CODE (type) == POINTER_TYPE
5723 && TREE_CODE (otype) == INTEGER_TYPE
5724 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5725 /* Don't warn about converting any constant. */
5726 && !TREE_CONSTANT (value))
5727 warning_at (loc,
5728 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5729 "of different size");
5730
5731 if (warn_strict_aliasing <= 2)
5732 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
5733
5734 /* If pedantic, warn for conversions between function and object
5735 pointer types, except for converting a null pointer constant
5736 to function pointer type. */
5737 if (pedantic
5738 && TREE_CODE (type) == POINTER_TYPE
5739 && TREE_CODE (otype) == POINTER_TYPE
5740 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5741 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5742 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5743 "conversion of function pointer to object pointer type");
5744
5745 if (pedantic
5746 && TREE_CODE (type) == POINTER_TYPE
5747 && TREE_CODE (otype) == POINTER_TYPE
5748 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5749 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5750 && !null_pointer_constant_p (value))
5751 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5752 "conversion of object pointer to function pointer type");
5753
5754 if (TREE_CODE (type) == POINTER_TYPE
5755 && TREE_CODE (otype) == POINTER_TYPE
5756 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5757 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5758 && !c_safe_function_type_cast_p (TREE_TYPE (type),
5759 TREE_TYPE (otype)))
5760 warning_at (loc, OPT_Wcast_function_type,
5761 "cast between incompatible function types"
5762 " from %qT to %qT", otype, type);
5763
5764 ovalue = value;
5765 value = convert (type, value);
5766
5767 /* Ignore any integer overflow caused by the cast. */
5768 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5769 {
5770 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5771 {
5772 if (!TREE_OVERFLOW (value))
5773 {
5774 /* Avoid clobbering a shared constant. */
5775 value = copy_node (value);
5776 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5777 }
5778 }
5779 else if (TREE_OVERFLOW (value))
5780 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5781 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5782 }
5783 }
5784
5785 /* Don't let a cast be an lvalue. */
5786 if (lvalue_p (value))
5787 value = non_lvalue_loc (loc, value);
5788
5789 /* Don't allow the results of casting to floating-point or complex
5790 types be confused with actual constants, or casts involving
5791 integer and pointer types other than direct integer-to-integer
5792 and integer-to-pointer be confused with integer constant
5793 expressions and null pointer constants. */
5794 if (TREE_CODE (value) == REAL_CST
5795 || TREE_CODE (value) == COMPLEX_CST
5796 || (TREE_CODE (value) == INTEGER_CST
5797 && !((TREE_CODE (expr) == INTEGER_CST
5798 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5799 || TREE_CODE (expr) == REAL_CST
5800 || TREE_CODE (expr) == COMPLEX_CST)))
5801 value = build1 (NOP_EXPR, type, value);
5802
5803 protected_set_expr_location (value, loc);
5804 return value;
5805 }
5806
5807 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5808 location of the open paren of the cast, or the position of the cast
5809 expr. */
5810 tree
5811 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5812 {
5813 tree type;
5814 tree type_expr = NULL_TREE;
5815 bool type_expr_const = true;
5816 tree ret;
5817 int saved_wsp = warn_strict_prototypes;
5818
5819 /* This avoids warnings about unprototyped casts on
5820 integers. E.g. "#define SIG_DFL (void(*)())0". */
5821 if (TREE_CODE (expr) == INTEGER_CST)
5822 warn_strict_prototypes = 0;
5823 type = groktypename (type_name, &type_expr, &type_expr_const);
5824 warn_strict_prototypes = saved_wsp;
5825
5826 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5827 && reject_gcc_builtin (expr))
5828 return error_mark_node;
5829
5830 ret = build_c_cast (loc, type, expr);
5831 if (type_expr)
5832 {
5833 bool inner_expr_const = true;
5834 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5835 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5836 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5837 && inner_expr_const);
5838 SET_EXPR_LOCATION (ret, loc);
5839 }
5840
5841 if (!EXPR_HAS_LOCATION (ret))
5842 protected_set_expr_location (ret, loc);
5843
5844 /* C++ does not permits types to be defined in a cast, but it
5845 allows references to incomplete types. */
5846 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5847 warning_at (loc, OPT_Wc___compat,
5848 "defining a type in a cast is invalid in C++");
5849
5850 return ret;
5851 }
5852 \f
5853 /* Build an assignment expression of lvalue LHS from value RHS.
5854 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5855 may differ from TREE_TYPE (LHS) for an enum bitfield.
5856 MODIFYCODE is the code for a binary operator that we use
5857 to combine the old value of LHS with RHS to get the new value.
5858 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5859 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5860 which may differ from TREE_TYPE (RHS) for an enum value.
5861
5862 LOCATION is the location of the MODIFYCODE operator.
5863 RHS_LOC is the location of the RHS. */
5864
5865 tree
5866 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5867 enum tree_code modifycode,
5868 location_t rhs_loc, tree rhs, tree rhs_origtype)
5869 {
5870 tree result;
5871 tree newrhs;
5872 tree rhseval = NULL_TREE;
5873 tree lhstype = TREE_TYPE (lhs);
5874 tree olhstype = lhstype;
5875 bool npc;
5876 bool is_atomic_op;
5877
5878 /* Types that aren't fully specified cannot be used in assignments. */
5879 lhs = require_complete_type (location, lhs);
5880
5881 /* Avoid duplicate error messages from operands that had errors. */
5882 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5883 return error_mark_node;
5884
5885 /* Ensure an error for assigning a non-lvalue array to an array in
5886 C90. */
5887 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5888 {
5889 error_at (location, "assignment to expression with array type");
5890 return error_mark_node;
5891 }
5892
5893 /* For ObjC properties, defer this check. */
5894 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5895 return error_mark_node;
5896
5897 is_atomic_op = really_atomic_lvalue (lhs);
5898
5899 newrhs = rhs;
5900
5901 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5902 {
5903 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5904 lhs_origtype, modifycode, rhs_loc, rhs,
5905 rhs_origtype);
5906 if (inner == error_mark_node)
5907 return error_mark_node;
5908 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5909 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5910 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5911 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5912 protected_set_expr_location (result, location);
5913 return result;
5914 }
5915
5916 /* If a binary op has been requested, combine the old LHS value with the RHS
5917 producing the value we should actually store into the LHS. */
5918
5919 if (modifycode != NOP_EXPR)
5920 {
5921 lhs = c_fully_fold (lhs, false, NULL, true);
5922 lhs = stabilize_reference (lhs);
5923
5924 /* Construct the RHS for any non-atomic compound assignemnt. */
5925 if (!is_atomic_op)
5926 {
5927 /* If in LHS op= RHS the RHS has side-effects, ensure they
5928 are preevaluated before the rest of the assignment expression's
5929 side-effects, because RHS could contain e.g. function calls
5930 that modify LHS. */
5931 if (TREE_SIDE_EFFECTS (rhs))
5932 {
5933 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5934 newrhs = save_expr (TREE_OPERAND (rhs, 0));
5935 else
5936 newrhs = save_expr (rhs);
5937 rhseval = newrhs;
5938 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5939 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
5940 newrhs);
5941 }
5942 newrhs = build_binary_op (location,
5943 modifycode, lhs, newrhs, true);
5944
5945 /* The original type of the right hand side is no longer
5946 meaningful. */
5947 rhs_origtype = NULL_TREE;
5948 }
5949 }
5950
5951 if (c_dialect_objc ())
5952 {
5953 /* Check if we are modifying an Objective-C property reference;
5954 if so, we need to generate setter calls. */
5955 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5956 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
5957 else
5958 result = objc_maybe_build_modify_expr (lhs, newrhs);
5959 if (result)
5960 goto return_result;
5961
5962 /* Else, do the check that we postponed for Objective-C. */
5963 if (!lvalue_or_else (location, lhs, lv_assign))
5964 return error_mark_node;
5965 }
5966
5967 /* Give an error for storing in something that is 'const'. */
5968
5969 if (TYPE_READONLY (lhstype)
5970 || (RECORD_OR_UNION_TYPE_P (lhstype)
5971 && C_TYPE_FIELDS_READONLY (lhstype)))
5972 {
5973 readonly_error (location, lhs, lv_assign);
5974 return error_mark_node;
5975 }
5976 else if (TREE_READONLY (lhs))
5977 readonly_warning (lhs, lv_assign);
5978
5979 /* If storing into a structure or union member,
5980 it has probably been given type `int'.
5981 Compute the type that would go with
5982 the actual amount of storage the member occupies. */
5983
5984 if (TREE_CODE (lhs) == COMPONENT_REF
5985 && (TREE_CODE (lhstype) == INTEGER_TYPE
5986 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5987 || TREE_CODE (lhstype) == REAL_TYPE
5988 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5989 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5990
5991 /* If storing in a field that is in actuality a short or narrower than one,
5992 we must store in the field in its actual type. */
5993
5994 if (lhstype != TREE_TYPE (lhs))
5995 {
5996 lhs = copy_node (lhs);
5997 TREE_TYPE (lhs) = lhstype;
5998 }
5999
6000 /* Issue -Wc++-compat warnings about an assignment to an enum type
6001 when LHS does not have its original type. This happens for,
6002 e.g., an enum bitfield in a struct. */
6003 if (warn_cxx_compat
6004 && lhs_origtype != NULL_TREE
6005 && lhs_origtype != lhstype
6006 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6007 {
6008 tree checktype = (rhs_origtype != NULL_TREE
6009 ? rhs_origtype
6010 : TREE_TYPE (rhs));
6011 if (checktype != error_mark_node
6012 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6013 || (is_atomic_op && modifycode != NOP_EXPR)))
6014 warning_at (location, OPT_Wc___compat,
6015 "enum conversion in assignment is invalid in C++");
6016 }
6017
6018 /* If the lhs is atomic, remove that qualifier. */
6019 if (is_atomic_op)
6020 {
6021 lhstype = build_qualified_type (lhstype,
6022 (TYPE_QUALS (lhstype)
6023 & ~TYPE_QUAL_ATOMIC));
6024 olhstype = build_qualified_type (olhstype,
6025 (TYPE_QUALS (lhstype)
6026 & ~TYPE_QUAL_ATOMIC));
6027 }
6028
6029 /* Convert new value to destination type. Fold it first, then
6030 restore any excess precision information, for the sake of
6031 conversion warnings. */
6032
6033 if (!(is_atomic_op && modifycode != NOP_EXPR))
6034 {
6035 tree rhs_semantic_type = NULL_TREE;
6036 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6037 {
6038 rhs_semantic_type = TREE_TYPE (newrhs);
6039 newrhs = TREE_OPERAND (newrhs, 0);
6040 }
6041 npc = null_pointer_constant_p (newrhs);
6042 newrhs = c_fully_fold (newrhs, false, NULL);
6043 if (rhs_semantic_type)
6044 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6045 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6046 rhs_origtype, ic_assign, npc,
6047 NULL_TREE, NULL_TREE, 0);
6048 if (TREE_CODE (newrhs) == ERROR_MARK)
6049 return error_mark_node;
6050 }
6051
6052 /* Emit ObjC write barrier, if necessary. */
6053 if (c_dialect_objc () && flag_objc_gc)
6054 {
6055 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6056 if (result)
6057 {
6058 protected_set_expr_location (result, location);
6059 goto return_result;
6060 }
6061 }
6062
6063 /* Scan operands. */
6064
6065 if (is_atomic_op)
6066 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6067 else
6068 {
6069 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6070 TREE_SIDE_EFFECTS (result) = 1;
6071 protected_set_expr_location (result, location);
6072 }
6073
6074 /* If we got the LHS in a different type for storing in,
6075 convert the result back to the nominal type of LHS
6076 so that the value we return always has the same type
6077 as the LHS argument. */
6078
6079 if (olhstype == TREE_TYPE (result))
6080 goto return_result;
6081
6082 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6083 rhs_origtype, ic_assign, false, NULL_TREE,
6084 NULL_TREE, 0);
6085 protected_set_expr_location (result, location);
6086
6087 return_result:
6088 if (rhseval)
6089 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6090 return result;
6091 }
6092 \f
6093 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6094 This is used to implement -fplan9-extensions. */
6095
6096 static bool
6097 find_anonymous_field_with_type (tree struct_type, tree type)
6098 {
6099 tree field;
6100 bool found;
6101
6102 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6103 found = false;
6104 for (field = TYPE_FIELDS (struct_type);
6105 field != NULL_TREE;
6106 field = TREE_CHAIN (field))
6107 {
6108 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6109 ? c_build_qualified_type (TREE_TYPE (field),
6110 TYPE_QUAL_ATOMIC)
6111 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6112 if (DECL_NAME (field) == NULL
6113 && comptypes (type, fieldtype))
6114 {
6115 if (found)
6116 return false;
6117 found = true;
6118 }
6119 else if (DECL_NAME (field) == NULL
6120 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6121 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6122 {
6123 if (found)
6124 return false;
6125 found = true;
6126 }
6127 }
6128 return found;
6129 }
6130
6131 /* RHS is an expression whose type is pointer to struct. If there is
6132 an anonymous field in RHS with type TYPE, then return a pointer to
6133 that field in RHS. This is used with -fplan9-extensions. This
6134 returns NULL if no conversion could be found. */
6135
6136 static tree
6137 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6138 {
6139 tree rhs_struct_type, lhs_main_type;
6140 tree field, found_field;
6141 bool found_sub_field;
6142 tree ret;
6143
6144 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6145 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6146 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6147
6148 gcc_assert (POINTER_TYPE_P (type));
6149 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6150 ? c_build_qualified_type (TREE_TYPE (type),
6151 TYPE_QUAL_ATOMIC)
6152 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6153
6154 found_field = NULL_TREE;
6155 found_sub_field = false;
6156 for (field = TYPE_FIELDS (rhs_struct_type);
6157 field != NULL_TREE;
6158 field = TREE_CHAIN (field))
6159 {
6160 if (DECL_NAME (field) != NULL_TREE
6161 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6162 continue;
6163 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6164 ? c_build_qualified_type (TREE_TYPE (field),
6165 TYPE_QUAL_ATOMIC)
6166 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6167 if (comptypes (lhs_main_type, fieldtype))
6168 {
6169 if (found_field != NULL_TREE)
6170 return NULL_TREE;
6171 found_field = field;
6172 }
6173 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6174 lhs_main_type))
6175 {
6176 if (found_field != NULL_TREE)
6177 return NULL_TREE;
6178 found_field = field;
6179 found_sub_field = true;
6180 }
6181 }
6182
6183 if (found_field == NULL_TREE)
6184 return NULL_TREE;
6185
6186 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6187 build_fold_indirect_ref (rhs), found_field,
6188 NULL_TREE);
6189 ret = build_fold_addr_expr_loc (location, ret);
6190
6191 if (found_sub_field)
6192 {
6193 ret = convert_to_anonymous_field (location, type, ret);
6194 gcc_assert (ret != NULL_TREE);
6195 }
6196
6197 return ret;
6198 }
6199
6200 /* Issue an error message for a bad initializer component.
6201 GMSGID identifies the message.
6202 The component name is taken from the spelling stack. */
6203
6204 static void
6205 error_init (location_t loc, const char *gmsgid)
6206 {
6207 char *ofwhat;
6208
6209 auto_diagnostic_group d;
6210
6211 /* The gmsgid may be a format string with %< and %>. */
6212 error_at (loc, gmsgid);
6213 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6214 if (*ofwhat)
6215 inform (loc, "(near initialization for %qs)", ofwhat);
6216 }
6217
6218 /* Issue a pedantic warning for a bad initializer component. OPT is
6219 the option OPT_* (from options.h) controlling this warning or 0 if
6220 it is unconditionally given. GMSGID identifies the message. The
6221 component name is taken from the spelling stack. */
6222
6223 static void ATTRIBUTE_GCC_DIAG (3,0)
6224 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6225 {
6226 /* Use the location where a macro was expanded rather than where
6227 it was defined to make sure macros defined in system headers
6228 but used incorrectly elsewhere are diagnosed. */
6229 source_location exploc = expansion_point_location_if_in_system_header (loc);
6230 auto_diagnostic_group d;
6231 va_list ap;
6232 va_start (ap, gmsgid);
6233 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6234 va_end (ap);
6235 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6236 if (*ofwhat && warned)
6237 inform (exploc, "(near initialization for %qs)", ofwhat);
6238 }
6239
6240 /* Issue a warning for a bad initializer component.
6241
6242 OPT is the OPT_W* value corresponding to the warning option that
6243 controls this warning. GMSGID identifies the message. The
6244 component name is taken from the spelling stack. */
6245
6246 static void
6247 warning_init (location_t loc, int opt, const char *gmsgid)
6248 {
6249 char *ofwhat;
6250 bool warned;
6251
6252 auto_diagnostic_group d;
6253
6254 /* Use the location where a macro was expanded rather than where
6255 it was defined to make sure macros defined in system headers
6256 but used incorrectly elsewhere are diagnosed. */
6257 source_location exploc = expansion_point_location_if_in_system_header (loc);
6258
6259 /* The gmsgid may be a format string with %< and %>. */
6260 warned = warning_at (exploc, opt, gmsgid);
6261 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6262 if (*ofwhat && warned)
6263 inform (exploc, "(near initialization for %qs)", ofwhat);
6264 }
6265 \f
6266 /* If TYPE is an array type and EXPR is a parenthesized string
6267 constant, warn if pedantic that EXPR is being used to initialize an
6268 object of type TYPE. */
6269
6270 void
6271 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6272 {
6273 if (pedantic
6274 && TREE_CODE (type) == ARRAY_TYPE
6275 && TREE_CODE (expr.value) == STRING_CST
6276 && expr.original_code != STRING_CST)
6277 pedwarn_init (loc, OPT_Wpedantic,
6278 "array initialized from parenthesized string constant");
6279 }
6280
6281 /* Attempt to locate the parameter with the given index within FNDECL,
6282 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6283
6284 static location_t
6285 get_fndecl_argument_location (tree fndecl, int argnum)
6286 {
6287 int i;
6288 tree param;
6289
6290 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6291 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6292 i < argnum && param;
6293 i++, param = TREE_CHAIN (param))
6294 ;
6295
6296 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6297 return DECL_SOURCE_LOCATION (FNDECL). */
6298 if (param == NULL)
6299 return DECL_SOURCE_LOCATION (fndecl);
6300
6301 return DECL_SOURCE_LOCATION (param);
6302 }
6303
6304 /* Issue a note about a mismatching argument for parameter PARMNUM
6305 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6306 Attempt to issue the note at the pertinent parameter of the decl;
6307 failing that issue it at the location of FUNDECL; failing that
6308 issue it at PLOC. */
6309
6310 static void
6311 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6312 tree expected_type, tree actual_type)
6313 {
6314 location_t loc;
6315 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6316 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6317 else
6318 loc = ploc;
6319
6320 inform (loc,
6321 "expected %qT but argument is of type %qT",
6322 expected_type, actual_type);
6323 }
6324
6325 /* Convert value RHS to type TYPE as preparation for an assignment to
6326 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6327 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6328 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6329 constant before any folding.
6330 The real work of conversion is done by `convert'.
6331 The purpose of this function is to generate error messages
6332 for assignments that are not allowed in C.
6333 ERRTYPE says whether it is argument passing, assignment,
6334 initialization or return.
6335
6336 In the following example, '~' denotes where EXPR_LOC and '^' where
6337 LOCATION point to:
6338
6339 f (var); [ic_argpass]
6340 ^ ~~~
6341 x = var; [ic_assign]
6342 ^ ~~~;
6343 int x = var; [ic_init]
6344 ^^^
6345 return x; [ic_return]
6346 ^
6347
6348 FUNCTION is a tree for the function being called.
6349 PARMNUM is the number of the argument, for printing in error messages. */
6350
6351 static tree
6352 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6353 tree rhs, tree origtype, enum impl_conv errtype,
6354 bool null_pointer_constant, tree fundecl,
6355 tree function, int parmnum)
6356 {
6357 enum tree_code codel = TREE_CODE (type);
6358 tree orig_rhs = rhs;
6359 tree rhstype;
6360 enum tree_code coder;
6361 tree rname = NULL_TREE;
6362 bool objc_ok = false;
6363
6364 /* Use the expansion point location to handle cases such as user's
6365 function returning a wrong-type macro defined in a system header. */
6366 location = expansion_point_location_if_in_system_header (location);
6367
6368 if (errtype == ic_argpass)
6369 {
6370 tree selector;
6371 /* Change pointer to function to the function itself for
6372 diagnostics. */
6373 if (TREE_CODE (function) == ADDR_EXPR
6374 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6375 function = TREE_OPERAND (function, 0);
6376
6377 /* Handle an ObjC selector specially for diagnostics. */
6378 selector = objc_message_selector ();
6379 rname = function;
6380 if (selector && parmnum > 2)
6381 {
6382 rname = selector;
6383 parmnum -= 2;
6384 }
6385 }
6386
6387 /* This macro is used to emit diagnostics to ensure that all format
6388 strings are complete sentences, visible to gettext and checked at
6389 compile time. */
6390 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6391 do { \
6392 switch (errtype) \
6393 { \
6394 case ic_argpass: \
6395 { \
6396 auto_diagnostic_group d; \
6397 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6398 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6399 } \
6400 break; \
6401 case ic_assign: \
6402 pedwarn (LOCATION, OPT, AS); \
6403 break; \
6404 case ic_init: \
6405 pedwarn_init (LOCATION, OPT, IN); \
6406 break; \
6407 case ic_return: \
6408 pedwarn (LOCATION, OPT, RE); \
6409 break; \
6410 default: \
6411 gcc_unreachable (); \
6412 } \
6413 } while (0)
6414
6415 /* This macro is used to emit diagnostics to ensure that all format
6416 strings are complete sentences, visible to gettext and checked at
6417 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6418 extra parameter to enumerate qualifiers. */
6419 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6420 do { \
6421 switch (errtype) \
6422 { \
6423 case ic_argpass: \
6424 { \
6425 auto_diagnostic_group d; \
6426 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6427 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6428 } \
6429 break; \
6430 case ic_assign: \
6431 pedwarn (LOCATION, OPT, AS, QUALS); \
6432 break; \
6433 case ic_init: \
6434 pedwarn (LOCATION, OPT, IN, QUALS); \
6435 break; \
6436 case ic_return: \
6437 pedwarn (LOCATION, OPT, RE, QUALS); \
6438 break; \
6439 default: \
6440 gcc_unreachable (); \
6441 } \
6442 } while (0)
6443
6444 /* This macro is used to emit diagnostics to ensure that all format
6445 strings are complete sentences, visible to gettext and checked at
6446 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6447 warning_at instead of pedwarn. */
6448 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6449 do { \
6450 switch (errtype) \
6451 { \
6452 case ic_argpass: \
6453 { \
6454 auto_diagnostic_group d; \
6455 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6456 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6457 } \
6458 break; \
6459 case ic_assign: \
6460 warning_at (LOCATION, OPT, AS, QUALS); \
6461 break; \
6462 case ic_init: \
6463 warning_at (LOCATION, OPT, IN, QUALS); \
6464 break; \
6465 case ic_return: \
6466 warning_at (LOCATION, OPT, RE, QUALS); \
6467 break; \
6468 default: \
6469 gcc_unreachable (); \
6470 } \
6471 } while (0)
6472
6473 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6474 rhs = TREE_OPERAND (rhs, 0);
6475
6476 rhstype = TREE_TYPE (rhs);
6477 coder = TREE_CODE (rhstype);
6478
6479 if (coder == ERROR_MARK)
6480 return error_mark_node;
6481
6482 if (c_dialect_objc ())
6483 {
6484 int parmno;
6485
6486 switch (errtype)
6487 {
6488 case ic_return:
6489 parmno = 0;
6490 break;
6491
6492 case ic_assign:
6493 parmno = -1;
6494 break;
6495
6496 case ic_init:
6497 parmno = -2;
6498 break;
6499
6500 default:
6501 parmno = parmnum;
6502 break;
6503 }
6504
6505 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6506 }
6507
6508 if (warn_cxx_compat)
6509 {
6510 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6511 if (checktype != error_mark_node
6512 && TREE_CODE (type) == ENUMERAL_TYPE
6513 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6514 switch (errtype)
6515 {
6516 case ic_argpass:
6517 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6518 "passing argument %d of %qE is invalid in C++",
6519 parmnum, rname))
6520 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6521 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6522 "expected %qT but argument is of type %qT",
6523 type, rhstype);
6524 break;
6525 case ic_assign:
6526 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6527 "%qT in assignment is invalid in C++", rhstype, type);
6528 break;
6529 case ic_init:
6530 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6531 "%qT to %qT in initialization is invalid in C++",
6532 rhstype, type);
6533 break;
6534 case ic_return:
6535 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6536 "%qT in return is invalid in C++", rhstype, type);
6537 break;
6538 default:
6539 gcc_unreachable ();
6540 }
6541 }
6542
6543 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6544 return rhs;
6545
6546 if (coder == VOID_TYPE)
6547 {
6548 /* Except for passing an argument to an unprototyped function,
6549 this is a constraint violation. When passing an argument to
6550 an unprototyped function, it is compile-time undefined;
6551 making it a constraint in that case was rejected in
6552 DR#252. */
6553 error_at (location, "void value not ignored as it ought to be");
6554 return error_mark_node;
6555 }
6556 rhs = require_complete_type (location, rhs);
6557 if (rhs == error_mark_node)
6558 return error_mark_node;
6559
6560 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6561 return error_mark_node;
6562
6563 /* A non-reference type can convert to a reference. This handles
6564 va_start, va_copy and possibly port built-ins. */
6565 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6566 {
6567 if (!lvalue_p (rhs))
6568 {
6569 error_at (location, "cannot pass rvalue to reference parameter");
6570 return error_mark_node;
6571 }
6572 if (!c_mark_addressable (rhs))
6573 return error_mark_node;
6574 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6575 SET_EXPR_LOCATION (rhs, location);
6576
6577 rhs = convert_for_assignment (location, expr_loc,
6578 build_pointer_type (TREE_TYPE (type)),
6579 rhs, origtype, errtype,
6580 null_pointer_constant, fundecl, function,
6581 parmnum);
6582 if (rhs == error_mark_node)
6583 return error_mark_node;
6584
6585 rhs = build1 (NOP_EXPR, type, rhs);
6586 SET_EXPR_LOCATION (rhs, location);
6587 return rhs;
6588 }
6589 /* Some types can interconvert without explicit casts. */
6590 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6591 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6592 return convert (type, rhs);
6593 /* Arithmetic types all interconvert, and enum is treated like int. */
6594 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6595 || codel == FIXED_POINT_TYPE
6596 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6597 || codel == BOOLEAN_TYPE)
6598 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6599 || coder == FIXED_POINT_TYPE
6600 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6601 || coder == BOOLEAN_TYPE))
6602 {
6603 tree ret;
6604 bool save = in_late_binary_op;
6605 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6606 || (coder == REAL_TYPE
6607 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6608 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6609 in_late_binary_op = true;
6610 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6611 ? expr_loc : location, type, orig_rhs);
6612 in_late_binary_op = save;
6613 return ret;
6614 }
6615
6616 /* Aggregates in different TUs might need conversion. */
6617 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6618 && codel == coder
6619 && comptypes (type, rhstype))
6620 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6621 ? expr_loc : location, type, rhs);
6622
6623 /* Conversion to a transparent union or record from its member types.
6624 This applies only to function arguments. */
6625 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6626 && TYPE_TRANSPARENT_AGGR (type))
6627 && errtype == ic_argpass)
6628 {
6629 tree memb, marginal_memb = NULL_TREE;
6630
6631 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6632 {
6633 tree memb_type = TREE_TYPE (memb);
6634
6635 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6636 TYPE_MAIN_VARIANT (rhstype)))
6637 break;
6638
6639 if (TREE_CODE (memb_type) != POINTER_TYPE)
6640 continue;
6641
6642 if (coder == POINTER_TYPE)
6643 {
6644 tree ttl = TREE_TYPE (memb_type);
6645 tree ttr = TREE_TYPE (rhstype);
6646
6647 /* Any non-function converts to a [const][volatile] void *
6648 and vice versa; otherwise, targets must be the same.
6649 Meanwhile, the lhs target must have all the qualifiers of
6650 the rhs. */
6651 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6652 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6653 || comp_target_types (location, memb_type, rhstype))
6654 {
6655 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6656 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6657 /* If this type won't generate any warnings, use it. */
6658 if (lquals == rquals
6659 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6660 && TREE_CODE (ttl) == FUNCTION_TYPE)
6661 ? ((lquals | rquals) == rquals)
6662 : ((lquals | rquals) == lquals)))
6663 break;
6664
6665 /* Keep looking for a better type, but remember this one. */
6666 if (!marginal_memb)
6667 marginal_memb = memb;
6668 }
6669 }
6670
6671 /* Can convert integer zero to any pointer type. */
6672 if (null_pointer_constant)
6673 {
6674 rhs = null_pointer_node;
6675 break;
6676 }
6677 }
6678
6679 if (memb || marginal_memb)
6680 {
6681 if (!memb)
6682 {
6683 /* We have only a marginally acceptable member type;
6684 it needs a warning. */
6685 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6686 tree ttr = TREE_TYPE (rhstype);
6687
6688 /* Const and volatile mean something different for function
6689 types, so the usual warnings are not appropriate. */
6690 if (TREE_CODE (ttr) == FUNCTION_TYPE
6691 && TREE_CODE (ttl) == FUNCTION_TYPE)
6692 {
6693 /* Because const and volatile on functions are
6694 restrictions that say the function will not do
6695 certain things, it is okay to use a const or volatile
6696 function where an ordinary one is wanted, but not
6697 vice-versa. */
6698 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6699 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6700 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6701 OPT_Wdiscarded_qualifiers,
6702 G_("passing argument %d of %qE "
6703 "makes %q#v qualified function "
6704 "pointer from unqualified"),
6705 G_("assignment makes %q#v qualified "
6706 "function pointer from "
6707 "unqualified"),
6708 G_("initialization makes %q#v qualified "
6709 "function pointer from "
6710 "unqualified"),
6711 G_("return makes %q#v qualified function "
6712 "pointer from unqualified"),
6713 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6714 }
6715 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6716 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6717 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6718 OPT_Wdiscarded_qualifiers,
6719 G_("passing argument %d of %qE discards "
6720 "%qv qualifier from pointer target type"),
6721 G_("assignment discards %qv qualifier "
6722 "from pointer target type"),
6723 G_("initialization discards %qv qualifier "
6724 "from pointer target type"),
6725 G_("return discards %qv qualifier from "
6726 "pointer target type"),
6727 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6728
6729 memb = marginal_memb;
6730 }
6731
6732 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6733 pedwarn (location, OPT_Wpedantic,
6734 "ISO C prohibits argument conversion to union type");
6735
6736 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6737 return build_constructor_single (type, memb, rhs);
6738 }
6739 }
6740
6741 /* Conversions among pointers */
6742 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6743 && (coder == codel))
6744 {
6745 tree ttl = TREE_TYPE (type);
6746 tree ttr = TREE_TYPE (rhstype);
6747 tree mvl = ttl;
6748 tree mvr = ttr;
6749 bool is_opaque_pointer;
6750 int target_cmp = 0; /* Cache comp_target_types () result. */
6751 addr_space_t asl;
6752 addr_space_t asr;
6753
6754 if (TREE_CODE (mvl) != ARRAY_TYPE)
6755 mvl = (TYPE_ATOMIC (mvl)
6756 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6757 TYPE_QUAL_ATOMIC)
6758 : TYPE_MAIN_VARIANT (mvl));
6759 if (TREE_CODE (mvr) != ARRAY_TYPE)
6760 mvr = (TYPE_ATOMIC (mvr)
6761 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6762 TYPE_QUAL_ATOMIC)
6763 : TYPE_MAIN_VARIANT (mvr));
6764 /* Opaque pointers are treated like void pointers. */
6765 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6766
6767 /* The Plan 9 compiler permits a pointer to a struct to be
6768 automatically converted into a pointer to an anonymous field
6769 within the struct. */
6770 if (flag_plan9_extensions
6771 && RECORD_OR_UNION_TYPE_P (mvl)
6772 && RECORD_OR_UNION_TYPE_P (mvr)
6773 && mvl != mvr)
6774 {
6775 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6776 if (new_rhs != NULL_TREE)
6777 {
6778 rhs = new_rhs;
6779 rhstype = TREE_TYPE (rhs);
6780 coder = TREE_CODE (rhstype);
6781 ttr = TREE_TYPE (rhstype);
6782 mvr = TYPE_MAIN_VARIANT (ttr);
6783 }
6784 }
6785
6786 /* C++ does not allow the implicit conversion void* -> T*. However,
6787 for the purpose of reducing the number of false positives, we
6788 tolerate the special case of
6789
6790 int *p = NULL;
6791
6792 where NULL is typically defined in C to be '(void *) 0'. */
6793 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6794 warning_at (errtype == ic_argpass ? expr_loc : location,
6795 OPT_Wc___compat,
6796 "request for implicit conversion "
6797 "from %qT to %qT not permitted in C++", rhstype, type);
6798
6799 /* See if the pointers point to incompatible address spaces. */
6800 asl = TYPE_ADDR_SPACE (ttl);
6801 asr = TYPE_ADDR_SPACE (ttr);
6802 if (!null_pointer_constant_p (rhs)
6803 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6804 {
6805 switch (errtype)
6806 {
6807 case ic_argpass:
6808 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6809 "non-enclosed address space", parmnum, rname);
6810 break;
6811 case ic_assign:
6812 error_at (location, "assignment from pointer to "
6813 "non-enclosed address space");
6814 break;
6815 case ic_init:
6816 error_at (location, "initialization from pointer to "
6817 "non-enclosed address space");
6818 break;
6819 case ic_return:
6820 error_at (location, "return from pointer to "
6821 "non-enclosed address space");
6822 break;
6823 default:
6824 gcc_unreachable ();
6825 }
6826 return error_mark_node;
6827 }
6828
6829 /* Check if the right-hand side has a format attribute but the
6830 left-hand side doesn't. */
6831 if (warn_suggest_attribute_format
6832 && check_missing_format_attribute (type, rhstype))
6833 {
6834 switch (errtype)
6835 {
6836 case ic_argpass:
6837 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6838 "argument %d of %qE might be "
6839 "a candidate for a format attribute",
6840 parmnum, rname);
6841 break;
6842 case ic_assign:
6843 warning_at (location, OPT_Wsuggest_attribute_format,
6844 "assignment left-hand side might be "
6845 "a candidate for a format attribute");
6846 break;
6847 case ic_init:
6848 warning_at (location, OPT_Wsuggest_attribute_format,
6849 "initialization left-hand side might be "
6850 "a candidate for a format attribute");
6851 break;
6852 case ic_return:
6853 warning_at (location, OPT_Wsuggest_attribute_format,
6854 "return type might be "
6855 "a candidate for a format attribute");
6856 break;
6857 default:
6858 gcc_unreachable ();
6859 }
6860 }
6861
6862 /* Any non-function converts to a [const][volatile] void *
6863 and vice versa; otherwise, targets must be the same.
6864 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6865 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6866 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6867 || (target_cmp = comp_target_types (location, type, rhstype))
6868 || is_opaque_pointer
6869 || ((c_common_unsigned_type (mvl)
6870 == c_common_unsigned_type (mvr))
6871 && (c_common_signed_type (mvl)
6872 == c_common_signed_type (mvr))
6873 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6874 {
6875 /* Warn about loss of qualifers from pointers to arrays with
6876 qualifiers on the element type. */
6877 if (TREE_CODE (ttr) == ARRAY_TYPE)
6878 {
6879 ttr = strip_array_types (ttr);
6880 ttl = strip_array_types (ttl);
6881
6882 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6883 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6884 WARNING_FOR_QUALIFIERS (location, expr_loc,
6885 OPT_Wdiscarded_array_qualifiers,
6886 G_("passing argument %d of %qE discards "
6887 "%qv qualifier from pointer target type"),
6888 G_("assignment discards %qv qualifier "
6889 "from pointer target type"),
6890 G_("initialization discards %qv qualifier "
6891 "from pointer target type"),
6892 G_("return discards %qv qualifier from "
6893 "pointer target type"),
6894 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6895 }
6896 else if (pedantic
6897 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6898 ||
6899 (VOID_TYPE_P (ttr)
6900 && !null_pointer_constant
6901 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6902 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6903 G_("ISO C forbids passing argument %d of "
6904 "%qE between function pointer "
6905 "and %<void *%>"),
6906 G_("ISO C forbids assignment between "
6907 "function pointer and %<void *%>"),
6908 G_("ISO C forbids initialization between "
6909 "function pointer and %<void *%>"),
6910 G_("ISO C forbids return between function "
6911 "pointer and %<void *%>"));
6912 /* Const and volatile mean something different for function types,
6913 so the usual warnings are not appropriate. */
6914 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6915 && TREE_CODE (ttl) != FUNCTION_TYPE)
6916 {
6917 /* Don't warn about loss of qualifier for conversions from
6918 qualified void* to pointers to arrays with corresponding
6919 qualifier on the element type. */
6920 if (!pedantic)
6921 ttl = strip_array_types (ttl);
6922
6923 /* Assignments between atomic and non-atomic objects are OK. */
6924 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6925 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6926 {
6927 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6928 OPT_Wdiscarded_qualifiers,
6929 G_("passing argument %d of %qE discards "
6930 "%qv qualifier from pointer target type"),
6931 G_("assignment discards %qv qualifier "
6932 "from pointer target type"),
6933 G_("initialization discards %qv qualifier "
6934 "from pointer target type"),
6935 G_("return discards %qv qualifier from "
6936 "pointer target type"),
6937 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6938 }
6939 /* If this is not a case of ignoring a mismatch in signedness,
6940 no warning. */
6941 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6942 || target_cmp)
6943 ;
6944 /* If there is a mismatch, do warn. */
6945 else if (warn_pointer_sign)
6946 switch (errtype)
6947 {
6948 case ic_argpass:
6949 {
6950 auto_diagnostic_group d;
6951 range_label_for_type_mismatch rhs_label (rhstype, type);
6952 gcc_rich_location richloc (expr_loc, &rhs_label);
6953 if (pedwarn (&richloc, OPT_Wpointer_sign,
6954 "pointer targets in passing argument %d of "
6955 "%qE differ in signedness", parmnum, rname))
6956 inform_for_arg (fundecl, expr_loc, parmnum, type,
6957 rhstype);
6958 }
6959 break;
6960 case ic_assign:
6961 pedwarn (location, OPT_Wpointer_sign,
6962 "pointer targets in assignment from %qT to %qT "
6963 "differ in signedness", rhstype, type);
6964 break;
6965 case ic_init:
6966 pedwarn_init (location, OPT_Wpointer_sign,
6967 "pointer targets in initialization of %qT "
6968 "from %qT differ in signedness", type,
6969 rhstype);
6970 break;
6971 case ic_return:
6972 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6973 "returning %qT from a function with return type "
6974 "%qT differ in signedness", rhstype, type);
6975 break;
6976 default:
6977 gcc_unreachable ();
6978 }
6979 }
6980 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6981 && TREE_CODE (ttr) == FUNCTION_TYPE)
6982 {
6983 /* Because const and volatile on functions are restrictions
6984 that say the function will not do certain things,
6985 it is okay to use a const or volatile function
6986 where an ordinary one is wanted, but not vice-versa. */
6987 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6988 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6989 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6990 OPT_Wdiscarded_qualifiers,
6991 G_("passing argument %d of %qE makes "
6992 "%q#v qualified function pointer "
6993 "from unqualified"),
6994 G_("assignment makes %q#v qualified function "
6995 "pointer from unqualified"),
6996 G_("initialization makes %q#v qualified "
6997 "function pointer from unqualified"),
6998 G_("return makes %q#v qualified function "
6999 "pointer from unqualified"),
7000 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7001 }
7002 }
7003 /* Avoid warning about the volatile ObjC EH puts on decls. */
7004 else if (!objc_ok)
7005 {
7006 switch (errtype)
7007 {
7008 case ic_argpass:
7009 {
7010 auto_diagnostic_group d;
7011 range_label_for_type_mismatch rhs_label (rhstype, type);
7012 gcc_rich_location richloc (expr_loc, &rhs_label);
7013 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7014 "passing argument %d of %qE from incompatible "
7015 "pointer type", parmnum, rname))
7016 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7017 }
7018 break;
7019 case ic_assign:
7020 pedwarn (location, OPT_Wincompatible_pointer_types,
7021 "assignment to %qT from incompatible pointer type %qT",
7022 type, rhstype);
7023 break;
7024 case ic_init:
7025 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7026 "initialization of %qT from incompatible pointer "
7027 "type %qT", type, rhstype);
7028 break;
7029 case ic_return:
7030 pedwarn (location, OPT_Wincompatible_pointer_types,
7031 "returning %qT from a function with incompatible "
7032 "return type %qT", rhstype, type);
7033 break;
7034 default:
7035 gcc_unreachable ();
7036 }
7037 }
7038
7039 return convert (type, rhs);
7040 }
7041 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7042 {
7043 /* ??? This should not be an error when inlining calls to
7044 unprototyped functions. */
7045 error_at (location, "invalid use of non-lvalue array");
7046 return error_mark_node;
7047 }
7048 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7049 {
7050 /* An explicit constant 0 can convert to a pointer,
7051 or one that results from arithmetic, even including
7052 a cast to integer type. */
7053 if (!null_pointer_constant)
7054 switch (errtype)
7055 {
7056 case ic_argpass:
7057 {
7058 auto_diagnostic_group d;
7059 range_label_for_type_mismatch rhs_label (rhstype, type);
7060 gcc_rich_location richloc (expr_loc, &rhs_label);
7061 if (pedwarn (&richloc, OPT_Wint_conversion,
7062 "passing argument %d of %qE makes pointer from "
7063 "integer without a cast", parmnum, rname))
7064 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7065 }
7066 break;
7067 case ic_assign:
7068 pedwarn (location, OPT_Wint_conversion,
7069 "assignment to %qT from %qT makes pointer from integer "
7070 "without a cast", type, rhstype);
7071 break;
7072 case ic_init:
7073 pedwarn_init (location, OPT_Wint_conversion,
7074 "initialization of %qT from %qT makes pointer from "
7075 "integer without a cast", type, rhstype);
7076 break;
7077 case ic_return:
7078 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7079 "function with return type %qT makes pointer from "
7080 "integer without a cast", rhstype, type);
7081 break;
7082 default:
7083 gcc_unreachable ();
7084 }
7085
7086 return convert (type, rhs);
7087 }
7088 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7089 {
7090 switch (errtype)
7091 {
7092 case ic_argpass:
7093 {
7094 auto_diagnostic_group d;
7095 range_label_for_type_mismatch rhs_label (rhstype, type);
7096 gcc_rich_location richloc (expr_loc, &rhs_label);
7097 if (pedwarn (&richloc, OPT_Wint_conversion,
7098 "passing argument %d of %qE makes integer from "
7099 "pointer without a cast", parmnum, rname))
7100 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7101 }
7102 break;
7103 case ic_assign:
7104 pedwarn (location, OPT_Wint_conversion,
7105 "assignment to %qT from %qT makes integer from pointer "
7106 "without a cast", type, rhstype);
7107 break;
7108 case ic_init:
7109 pedwarn_init (location, OPT_Wint_conversion,
7110 "initialization of %qT from %qT makes integer from "
7111 "pointer without a cast", type, rhstype);
7112 break;
7113 case ic_return:
7114 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7115 "function with return type %qT makes integer from "
7116 "pointer without a cast", rhstype, type);
7117 break;
7118 default:
7119 gcc_unreachable ();
7120 }
7121
7122 return convert (type, rhs);
7123 }
7124 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7125 {
7126 tree ret;
7127 bool save = in_late_binary_op;
7128 in_late_binary_op = true;
7129 ret = convert (type, rhs);
7130 in_late_binary_op = save;
7131 return ret;
7132 }
7133
7134 switch (errtype)
7135 {
7136 case ic_argpass:
7137 {
7138 auto_diagnostic_group d;
7139 range_label_for_type_mismatch rhs_label (rhstype, type);
7140 gcc_rich_location richloc (expr_loc, &rhs_label);
7141 error_at (&richloc, "incompatible type for argument %d of %qE", parmnum,
7142 rname);
7143 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7144 }
7145 break;
7146 case ic_assign:
7147 error_at (location, "incompatible types when assigning to type %qT from "
7148 "type %qT", type, rhstype);
7149 break;
7150 case ic_init:
7151 error_at (location,
7152 "incompatible types when initializing type %qT using type %qT",
7153 type, rhstype);
7154 break;
7155 case ic_return:
7156 error_at (location,
7157 "incompatible types when returning type %qT but %qT was "
7158 "expected", rhstype, type);
7159 break;
7160 default:
7161 gcc_unreachable ();
7162 }
7163
7164 return error_mark_node;
7165 }
7166 \f
7167 /* If VALUE is a compound expr all of whose expressions are constant, then
7168 return its value. Otherwise, return error_mark_node.
7169
7170 This is for handling COMPOUND_EXPRs as initializer elements
7171 which is allowed with a warning when -pedantic is specified. */
7172
7173 static tree
7174 valid_compound_expr_initializer (tree value, tree endtype)
7175 {
7176 if (TREE_CODE (value) == COMPOUND_EXPR)
7177 {
7178 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7179 == error_mark_node)
7180 return error_mark_node;
7181 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7182 endtype);
7183 }
7184 else if (!initializer_constant_valid_p (value, endtype))
7185 return error_mark_node;
7186 else
7187 return value;
7188 }
7189 \f
7190 /* Perform appropriate conversions on the initial value of a variable,
7191 store it in the declaration DECL,
7192 and print any error messages that are appropriate.
7193 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7194 If the init is invalid, store an ERROR_MARK.
7195
7196 INIT_LOC is the location of the initial value. */
7197
7198 void
7199 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7200 {
7201 tree value, type;
7202 bool npc = false;
7203
7204 /* If variable's type was invalidly declared, just ignore it. */
7205
7206 type = TREE_TYPE (decl);
7207 if (TREE_CODE (type) == ERROR_MARK)
7208 return;
7209
7210 /* Digest the specified initializer into an expression. */
7211
7212 if (init)
7213 npc = null_pointer_constant_p (init);
7214 value = digest_init (init_loc, type, init, origtype, npc,
7215 true, TREE_STATIC (decl));
7216
7217 /* Store the expression if valid; else report error. */
7218
7219 if (!in_system_header_at (input_location)
7220 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7221 warning (OPT_Wtraditional, "traditional C rejects automatic "
7222 "aggregate initialization");
7223
7224 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7225 DECL_INITIAL (decl) = value;
7226
7227 /* ANSI wants warnings about out-of-range constant initializers. */
7228 STRIP_TYPE_NOPS (value);
7229 if (TREE_STATIC (decl))
7230 constant_expression_warning (value);
7231
7232 /* Check if we need to set array size from compound literal size. */
7233 if (TREE_CODE (type) == ARRAY_TYPE
7234 && TYPE_DOMAIN (type) == NULL_TREE
7235 && value != error_mark_node)
7236 {
7237 tree inside_init = init;
7238
7239 STRIP_TYPE_NOPS (inside_init);
7240 inside_init = fold (inside_init);
7241
7242 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7243 {
7244 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7245
7246 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7247 {
7248 /* For int foo[] = (int [3]){1}; we need to set array size
7249 now since later on array initializer will be just the
7250 brace enclosed list of the compound literal. */
7251 tree etype = strip_array_types (TREE_TYPE (decl));
7252 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7253 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7254 layout_type (type);
7255 layout_decl (cldecl, 0);
7256 TREE_TYPE (decl)
7257 = c_build_qualified_type (type, TYPE_QUALS (etype));
7258 }
7259 }
7260 }
7261 }
7262 \f
7263 /* Methods for storing and printing names for error messages. */
7264
7265 /* Implement a spelling stack that allows components of a name to be pushed
7266 and popped. Each element on the stack is this structure. */
7267
7268 struct spelling
7269 {
7270 int kind;
7271 union
7272 {
7273 unsigned HOST_WIDE_INT i;
7274 const char *s;
7275 } u;
7276 };
7277
7278 #define SPELLING_STRING 1
7279 #define SPELLING_MEMBER 2
7280 #define SPELLING_BOUNDS 3
7281
7282 static struct spelling *spelling; /* Next stack element (unused). */
7283 static struct spelling *spelling_base; /* Spelling stack base. */
7284 static int spelling_size; /* Size of the spelling stack. */
7285
7286 /* Macros to save and restore the spelling stack around push_... functions.
7287 Alternative to SAVE_SPELLING_STACK. */
7288
7289 #define SPELLING_DEPTH() (spelling - spelling_base)
7290 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7291
7292 /* Push an element on the spelling stack with type KIND and assign VALUE
7293 to MEMBER. */
7294
7295 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7296 { \
7297 int depth = SPELLING_DEPTH (); \
7298 \
7299 if (depth >= spelling_size) \
7300 { \
7301 spelling_size += 10; \
7302 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7303 spelling_size); \
7304 RESTORE_SPELLING_DEPTH (depth); \
7305 } \
7306 \
7307 spelling->kind = (KIND); \
7308 spelling->MEMBER = (VALUE); \
7309 spelling++; \
7310 }
7311
7312 /* Push STRING on the stack. Printed literally. */
7313
7314 static void
7315 push_string (const char *string)
7316 {
7317 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7318 }
7319
7320 /* Push a member name on the stack. Printed as '.' STRING. */
7321
7322 static void
7323 push_member_name (tree decl)
7324 {
7325 const char *const string
7326 = (DECL_NAME (decl)
7327 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7328 : _("<anonymous>"));
7329 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7330 }
7331
7332 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7333
7334 static void
7335 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7336 {
7337 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7338 }
7339
7340 /* Compute the maximum size in bytes of the printed spelling. */
7341
7342 static int
7343 spelling_length (void)
7344 {
7345 int size = 0;
7346 struct spelling *p;
7347
7348 for (p = spelling_base; p < spelling; p++)
7349 {
7350 if (p->kind == SPELLING_BOUNDS)
7351 size += 25;
7352 else
7353 size += strlen (p->u.s) + 1;
7354 }
7355
7356 return size;
7357 }
7358
7359 /* Print the spelling to BUFFER and return it. */
7360
7361 static char *
7362 print_spelling (char *buffer)
7363 {
7364 char *d = buffer;
7365 struct spelling *p;
7366
7367 for (p = spelling_base; p < spelling; p++)
7368 if (p->kind == SPELLING_BOUNDS)
7369 {
7370 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7371 d += strlen (d);
7372 }
7373 else
7374 {
7375 const char *s;
7376 if (p->kind == SPELLING_MEMBER)
7377 *d++ = '.';
7378 for (s = p->u.s; (*d = *s++); d++)
7379 ;
7380 }
7381 *d++ = '\0';
7382 return buffer;
7383 }
7384
7385 /* Digest the parser output INIT as an initializer for type TYPE.
7386 Return a C expression of type TYPE to represent the initial value.
7387
7388 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7389
7390 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7391
7392 If INIT is a string constant, STRICT_STRING is true if it is
7393 unparenthesized or we should not warn here for it being parenthesized.
7394 For other types of INIT, STRICT_STRING is not used.
7395
7396 INIT_LOC is the location of the INIT.
7397
7398 REQUIRE_CONSTANT requests an error if non-constant initializers or
7399 elements are seen. */
7400
7401 static tree
7402 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7403 bool null_pointer_constant, bool strict_string,
7404 int require_constant)
7405 {
7406 enum tree_code code = TREE_CODE (type);
7407 tree inside_init = init;
7408 tree semantic_type = NULL_TREE;
7409 bool maybe_const = true;
7410
7411 if (type == error_mark_node
7412 || !init
7413 || error_operand_p (init))
7414 return error_mark_node;
7415
7416 STRIP_TYPE_NOPS (inside_init);
7417
7418 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7419 {
7420 semantic_type = TREE_TYPE (inside_init);
7421 inside_init = TREE_OPERAND (inside_init, 0);
7422 }
7423 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7424
7425 /* Initialization of an array of chars from a string constant
7426 optionally enclosed in braces. */
7427
7428 if (code == ARRAY_TYPE && inside_init
7429 && TREE_CODE (inside_init) == STRING_CST)
7430 {
7431 tree typ1
7432 = (TYPE_ATOMIC (TREE_TYPE (type))
7433 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7434 TYPE_QUAL_ATOMIC)
7435 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7436 /* Note that an array could be both an array of character type
7437 and an array of wchar_t if wchar_t is signed char or unsigned
7438 char. */
7439 bool char_array = (typ1 == char_type_node
7440 || typ1 == signed_char_type_node
7441 || typ1 == unsigned_char_type_node);
7442 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7443 bool char16_array = !!comptypes (typ1, char16_type_node);
7444 bool char32_array = !!comptypes (typ1, char32_type_node);
7445
7446 if (char_array || wchar_array || char16_array || char32_array)
7447 {
7448 struct c_expr expr;
7449 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7450 expr.value = inside_init;
7451 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7452 expr.original_type = NULL;
7453 maybe_warn_string_init (init_loc, type, expr);
7454
7455 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7456 pedwarn_init (init_loc, OPT_Wpedantic,
7457 "initialization of a flexible array member");
7458
7459 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7460 TYPE_MAIN_VARIANT (type)))
7461 return inside_init;
7462
7463 if (char_array)
7464 {
7465 if (typ2 != char_type_node)
7466 {
7467 error_init (init_loc, "char-array initialized from wide "
7468 "string");
7469 return error_mark_node;
7470 }
7471 }
7472 else
7473 {
7474 if (typ2 == char_type_node)
7475 {
7476 error_init (init_loc, "wide character array initialized "
7477 "from non-wide string");
7478 return error_mark_node;
7479 }
7480 else if (!comptypes(typ1, typ2))
7481 {
7482 error_init (init_loc, "wide character array initialized "
7483 "from incompatible wide string");
7484 return error_mark_node;
7485 }
7486 }
7487
7488 TREE_TYPE (inside_init) = type;
7489 if (TYPE_DOMAIN (type) != NULL_TREE
7490 && TYPE_SIZE (type) != NULL_TREE
7491 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7492 {
7493 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7494
7495 /* Subtract the size of a single (possibly wide) character
7496 because it's ok to ignore the terminating null char
7497 that is counted in the length of the constant. */
7498 if (compare_tree_int (TYPE_SIZE_UNIT (type),
7499 (len - (TYPE_PRECISION (typ1)
7500 / BITS_PER_UNIT))) < 0)
7501 pedwarn_init (init_loc, 0,
7502 ("initializer-string for array of chars "
7503 "is too long"));
7504 else if (warn_cxx_compat
7505 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7506 warning_at (init_loc, OPT_Wc___compat,
7507 ("initializer-string for array chars "
7508 "is too long for C++"));
7509 }
7510
7511 return inside_init;
7512 }
7513 else if (INTEGRAL_TYPE_P (typ1))
7514 {
7515 error_init (init_loc, "array of inappropriate type initialized "
7516 "from string constant");
7517 return error_mark_node;
7518 }
7519 }
7520
7521 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7522 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7523 below and handle as a constructor. */
7524 if (code == VECTOR_TYPE
7525 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7526 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7527 && TREE_CONSTANT (inside_init))
7528 {
7529 if (TREE_CODE (inside_init) == VECTOR_CST
7530 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7531 TYPE_MAIN_VARIANT (type)))
7532 return inside_init;
7533
7534 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7535 {
7536 unsigned HOST_WIDE_INT ix;
7537 tree value;
7538 bool constant_p = true;
7539
7540 /* Iterate through elements and check if all constructor
7541 elements are *_CSTs. */
7542 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7543 if (!CONSTANT_CLASS_P (value))
7544 {
7545 constant_p = false;
7546 break;
7547 }
7548
7549 if (constant_p)
7550 return build_vector_from_ctor (type,
7551 CONSTRUCTOR_ELTS (inside_init));
7552 }
7553 }
7554
7555 if (warn_sequence_point)
7556 verify_sequence_points (inside_init);
7557
7558 /* Any type can be initialized
7559 from an expression of the same type, optionally with braces. */
7560
7561 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7562 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7563 TYPE_MAIN_VARIANT (type))
7564 || (code == ARRAY_TYPE
7565 && comptypes (TREE_TYPE (inside_init), type))
7566 || (code == VECTOR_TYPE
7567 && comptypes (TREE_TYPE (inside_init), type))
7568 || (code == POINTER_TYPE
7569 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7570 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7571 TREE_TYPE (type)))))
7572 {
7573 if (code == POINTER_TYPE)
7574 {
7575 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7576 {
7577 if (TREE_CODE (inside_init) == STRING_CST
7578 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7579 inside_init = array_to_pointer_conversion
7580 (init_loc, inside_init);
7581 else
7582 {
7583 error_init (init_loc, "invalid use of non-lvalue array");
7584 return error_mark_node;
7585 }
7586 }
7587 }
7588
7589 if (code == VECTOR_TYPE)
7590 /* Although the types are compatible, we may require a
7591 conversion. */
7592 inside_init = convert (type, inside_init);
7593
7594 if (require_constant
7595 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7596 {
7597 /* As an extension, allow initializing objects with static storage
7598 duration with compound literals (which are then treated just as
7599 the brace enclosed list they contain). Also allow this for
7600 vectors, as we can only assign them with compound literals. */
7601 if (flag_isoc99 && code != VECTOR_TYPE)
7602 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7603 "is not constant");
7604 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7605 inside_init = DECL_INITIAL (decl);
7606 }
7607
7608 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7609 && TREE_CODE (inside_init) != CONSTRUCTOR)
7610 {
7611 error_init (init_loc, "array initialized from non-constant array "
7612 "expression");
7613 return error_mark_node;
7614 }
7615
7616 /* Compound expressions can only occur here if -Wpedantic or
7617 -pedantic-errors is specified. In the later case, we always want
7618 an error. In the former case, we simply want a warning. */
7619 if (require_constant && pedantic
7620 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7621 {
7622 inside_init
7623 = valid_compound_expr_initializer (inside_init,
7624 TREE_TYPE (inside_init));
7625 if (inside_init == error_mark_node)
7626 error_init (init_loc, "initializer element is not constant");
7627 else
7628 pedwarn_init (init_loc, OPT_Wpedantic,
7629 "initializer element is not constant");
7630 if (flag_pedantic_errors)
7631 inside_init = error_mark_node;
7632 }
7633 else if (require_constant
7634 && !initializer_constant_valid_p (inside_init,
7635 TREE_TYPE (inside_init)))
7636 {
7637 error_init (init_loc, "initializer element is not constant");
7638 inside_init = error_mark_node;
7639 }
7640 else if (require_constant && !maybe_const)
7641 pedwarn_init (init_loc, OPT_Wpedantic,
7642 "initializer element is not a constant expression");
7643
7644 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7645 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7646 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7647 type, inside_init, origtype,
7648 ic_init, null_pointer_constant,
7649 NULL_TREE, NULL_TREE, 0);
7650 return inside_init;
7651 }
7652
7653 /* Handle scalar types, including conversions. */
7654
7655 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7656 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7657 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7658 {
7659 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7660 && (TREE_CODE (init) == STRING_CST
7661 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7662 inside_init = init = array_to_pointer_conversion (init_loc, init);
7663 if (semantic_type)
7664 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7665 inside_init);
7666 inside_init
7667 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7668 inside_init, origtype, ic_init,
7669 null_pointer_constant, NULL_TREE, NULL_TREE,
7670 0);
7671
7672 /* Check to see if we have already given an error message. */
7673 if (inside_init == error_mark_node)
7674 ;
7675 else if (require_constant && !TREE_CONSTANT (inside_init))
7676 {
7677 error_init (init_loc, "initializer element is not constant");
7678 inside_init = error_mark_node;
7679 }
7680 else if (require_constant
7681 && !initializer_constant_valid_p (inside_init,
7682 TREE_TYPE (inside_init)))
7683 {
7684 error_init (init_loc, "initializer element is not computable at "
7685 "load time");
7686 inside_init = error_mark_node;
7687 }
7688 else if (require_constant && !maybe_const)
7689 pedwarn_init (init_loc, OPT_Wpedantic,
7690 "initializer element is not a constant expression");
7691
7692 return inside_init;
7693 }
7694
7695 /* Come here only for records and arrays. */
7696
7697 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7698 {
7699 error_init (init_loc, "variable-sized object may not be initialized");
7700 return error_mark_node;
7701 }
7702
7703 error_init (init_loc, "invalid initializer");
7704 return error_mark_node;
7705 }
7706 \f
7707 /* Handle initializers that use braces. */
7708
7709 /* Type of object we are accumulating a constructor for.
7710 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7711 static tree constructor_type;
7712
7713 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7714 left to fill. */
7715 static tree constructor_fields;
7716
7717 /* For an ARRAY_TYPE, this is the specified index
7718 at which to store the next element we get. */
7719 static tree constructor_index;
7720
7721 /* For an ARRAY_TYPE, this is the maximum index. */
7722 static tree constructor_max_index;
7723
7724 /* For a RECORD_TYPE, this is the first field not yet written out. */
7725 static tree constructor_unfilled_fields;
7726
7727 /* For an ARRAY_TYPE, this is the index of the first element
7728 not yet written out. */
7729 static tree constructor_unfilled_index;
7730
7731 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7732 This is so we can generate gaps between fields, when appropriate. */
7733 static tree constructor_bit_index;
7734
7735 /* If we are saving up the elements rather than allocating them,
7736 this is the list of elements so far (in reverse order,
7737 most recent first). */
7738 static vec<constructor_elt, va_gc> *constructor_elements;
7739
7740 /* 1 if constructor should be incrementally stored into a constructor chain,
7741 0 if all the elements should be kept in AVL tree. */
7742 static int constructor_incremental;
7743
7744 /* 1 if so far this constructor's elements are all compile-time constants. */
7745 static int constructor_constant;
7746
7747 /* 1 if so far this constructor's elements are all valid address constants. */
7748 static int constructor_simple;
7749
7750 /* 1 if this constructor has an element that cannot be part of a
7751 constant expression. */
7752 static int constructor_nonconst;
7753
7754 /* 1 if this constructor is erroneous so far. */
7755 static int constructor_erroneous;
7756
7757 /* 1 if this constructor is the universal zero initializer { 0 }. */
7758 static int constructor_zeroinit;
7759
7760 /* Structure for managing pending initializer elements, organized as an
7761 AVL tree. */
7762
7763 struct init_node
7764 {
7765 struct init_node *left, *right;
7766 struct init_node *parent;
7767 int balance;
7768 tree purpose;
7769 tree value;
7770 tree origtype;
7771 };
7772
7773 /* Tree of pending elements at this constructor level.
7774 These are elements encountered out of order
7775 which belong at places we haven't reached yet in actually
7776 writing the output.
7777 Will never hold tree nodes across GC runs. */
7778 static struct init_node *constructor_pending_elts;
7779
7780 /* The SPELLING_DEPTH of this constructor. */
7781 static int constructor_depth;
7782
7783 /* DECL node for which an initializer is being read.
7784 0 means we are reading a constructor expression
7785 such as (struct foo) {...}. */
7786 static tree constructor_decl;
7787
7788 /* Nonzero if this is an initializer for a top-level decl. */
7789 static int constructor_top_level;
7790
7791 /* Nonzero if there were any member designators in this initializer. */
7792 static int constructor_designated;
7793
7794 /* Nesting depth of designator list. */
7795 static int designator_depth;
7796
7797 /* Nonzero if there were diagnosed errors in this designator list. */
7798 static int designator_erroneous;
7799
7800 \f
7801 /* This stack has a level for each implicit or explicit level of
7802 structuring in the initializer, including the outermost one. It
7803 saves the values of most of the variables above. */
7804
7805 struct constructor_range_stack;
7806
7807 struct constructor_stack
7808 {
7809 struct constructor_stack *next;
7810 tree type;
7811 tree fields;
7812 tree index;
7813 tree max_index;
7814 tree unfilled_index;
7815 tree unfilled_fields;
7816 tree bit_index;
7817 vec<constructor_elt, va_gc> *elements;
7818 struct init_node *pending_elts;
7819 int offset;
7820 int depth;
7821 /* If value nonzero, this value should replace the entire
7822 constructor at this level. */
7823 struct c_expr replacement_value;
7824 struct constructor_range_stack *range_stack;
7825 char constant;
7826 char simple;
7827 char nonconst;
7828 char implicit;
7829 char erroneous;
7830 char outer;
7831 char incremental;
7832 char designated;
7833 int designator_depth;
7834 };
7835
7836 static struct constructor_stack *constructor_stack;
7837
7838 /* This stack represents designators from some range designator up to
7839 the last designator in the list. */
7840
7841 struct constructor_range_stack
7842 {
7843 struct constructor_range_stack *next, *prev;
7844 struct constructor_stack *stack;
7845 tree range_start;
7846 tree index;
7847 tree range_end;
7848 tree fields;
7849 };
7850
7851 static struct constructor_range_stack *constructor_range_stack;
7852
7853 /* This stack records separate initializers that are nested.
7854 Nested initializers can't happen in ANSI C, but GNU C allows them
7855 in cases like { ... (struct foo) { ... } ... }. */
7856
7857 struct initializer_stack
7858 {
7859 struct initializer_stack *next;
7860 tree decl;
7861 struct constructor_stack *constructor_stack;
7862 struct constructor_range_stack *constructor_range_stack;
7863 vec<constructor_elt, va_gc> *elements;
7864 struct spelling *spelling;
7865 struct spelling *spelling_base;
7866 int spelling_size;
7867 char top_level;
7868 char require_constant_value;
7869 char require_constant_elements;
7870 rich_location *missing_brace_richloc;
7871 };
7872
7873 static struct initializer_stack *initializer_stack;
7874 \f
7875 /* Prepare to parse and output the initializer for variable DECL. */
7876
7877 void
7878 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7879 rich_location *richloc)
7880 {
7881 const char *locus;
7882 struct initializer_stack *p = XNEW (struct initializer_stack);
7883
7884 p->decl = constructor_decl;
7885 p->require_constant_value = require_constant_value;
7886 p->require_constant_elements = require_constant_elements;
7887 p->constructor_stack = constructor_stack;
7888 p->constructor_range_stack = constructor_range_stack;
7889 p->elements = constructor_elements;
7890 p->spelling = spelling;
7891 p->spelling_base = spelling_base;
7892 p->spelling_size = spelling_size;
7893 p->top_level = constructor_top_level;
7894 p->next = initializer_stack;
7895 p->missing_brace_richloc = richloc;
7896 initializer_stack = p;
7897
7898 constructor_decl = decl;
7899 constructor_designated = 0;
7900 constructor_top_level = top_level;
7901
7902 if (decl != NULL_TREE && decl != error_mark_node)
7903 {
7904 require_constant_value = TREE_STATIC (decl);
7905 require_constant_elements
7906 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7907 /* For a scalar, you can always use any value to initialize,
7908 even within braces. */
7909 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7910 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7911 }
7912 else
7913 {
7914 require_constant_value = 0;
7915 require_constant_elements = 0;
7916 locus = _("(anonymous)");
7917 }
7918
7919 constructor_stack = 0;
7920 constructor_range_stack = 0;
7921
7922 found_missing_braces = 0;
7923
7924 spelling_base = 0;
7925 spelling_size = 0;
7926 RESTORE_SPELLING_DEPTH (0);
7927
7928 if (locus)
7929 push_string (locus);
7930 }
7931
7932 void
7933 finish_init (void)
7934 {
7935 struct initializer_stack *p = initializer_stack;
7936
7937 /* Free the whole constructor stack of this initializer. */
7938 while (constructor_stack)
7939 {
7940 struct constructor_stack *q = constructor_stack;
7941 constructor_stack = q->next;
7942 free (q);
7943 }
7944
7945 gcc_assert (!constructor_range_stack);
7946
7947 /* Pop back to the data of the outer initializer (if any). */
7948 free (spelling_base);
7949
7950 constructor_decl = p->decl;
7951 require_constant_value = p->require_constant_value;
7952 require_constant_elements = p->require_constant_elements;
7953 constructor_stack = p->constructor_stack;
7954 constructor_range_stack = p->constructor_range_stack;
7955 constructor_elements = p->elements;
7956 spelling = p->spelling;
7957 spelling_base = p->spelling_base;
7958 spelling_size = p->spelling_size;
7959 constructor_top_level = p->top_level;
7960 initializer_stack = p->next;
7961 free (p);
7962 }
7963 \f
7964 /* Call here when we see the initializer is surrounded by braces.
7965 This is instead of a call to push_init_level;
7966 it is matched by a call to pop_init_level.
7967
7968 TYPE is the type to initialize, for a constructor expression.
7969 For an initializer for a decl, TYPE is zero. */
7970
7971 void
7972 really_start_incremental_init (tree type)
7973 {
7974 struct constructor_stack *p = XNEW (struct constructor_stack);
7975
7976 if (type == NULL_TREE)
7977 type = TREE_TYPE (constructor_decl);
7978
7979 if (VECTOR_TYPE_P (type)
7980 && TYPE_VECTOR_OPAQUE (type))
7981 error ("opaque vector types cannot be initialized");
7982
7983 p->type = constructor_type;
7984 p->fields = constructor_fields;
7985 p->index = constructor_index;
7986 p->max_index = constructor_max_index;
7987 p->unfilled_index = constructor_unfilled_index;
7988 p->unfilled_fields = constructor_unfilled_fields;
7989 p->bit_index = constructor_bit_index;
7990 p->elements = constructor_elements;
7991 p->constant = constructor_constant;
7992 p->simple = constructor_simple;
7993 p->nonconst = constructor_nonconst;
7994 p->erroneous = constructor_erroneous;
7995 p->pending_elts = constructor_pending_elts;
7996 p->depth = constructor_depth;
7997 p->replacement_value.value = 0;
7998 p->replacement_value.original_code = ERROR_MARK;
7999 p->replacement_value.original_type = NULL;
8000 p->implicit = 0;
8001 p->range_stack = 0;
8002 p->outer = 0;
8003 p->incremental = constructor_incremental;
8004 p->designated = constructor_designated;
8005 p->designator_depth = designator_depth;
8006 p->next = 0;
8007 constructor_stack = p;
8008
8009 constructor_constant = 1;
8010 constructor_simple = 1;
8011 constructor_nonconst = 0;
8012 constructor_depth = SPELLING_DEPTH ();
8013 constructor_elements = NULL;
8014 constructor_pending_elts = 0;
8015 constructor_type = type;
8016 constructor_incremental = 1;
8017 constructor_designated = 0;
8018 constructor_zeroinit = 1;
8019 designator_depth = 0;
8020 designator_erroneous = 0;
8021
8022 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8023 {
8024 constructor_fields = TYPE_FIELDS (constructor_type);
8025 /* Skip any nameless bit fields at the beginning. */
8026 while (constructor_fields != NULL_TREE
8027 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8028 constructor_fields = DECL_CHAIN (constructor_fields);
8029
8030 constructor_unfilled_fields = constructor_fields;
8031 constructor_bit_index = bitsize_zero_node;
8032 }
8033 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8034 {
8035 if (TYPE_DOMAIN (constructor_type))
8036 {
8037 constructor_max_index
8038 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8039
8040 /* Detect non-empty initializations of zero-length arrays. */
8041 if (constructor_max_index == NULL_TREE
8042 && TYPE_SIZE (constructor_type))
8043 constructor_max_index = integer_minus_one_node;
8044
8045 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8046 to initialize VLAs will cause a proper error; avoid tree
8047 checking errors as well by setting a safe value. */
8048 if (constructor_max_index
8049 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8050 constructor_max_index = integer_minus_one_node;
8051
8052 constructor_index
8053 = convert (bitsizetype,
8054 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8055 }
8056 else
8057 {
8058 constructor_index = bitsize_zero_node;
8059 constructor_max_index = NULL_TREE;
8060 }
8061
8062 constructor_unfilled_index = constructor_index;
8063 }
8064 else if (VECTOR_TYPE_P (constructor_type))
8065 {
8066 /* Vectors are like simple fixed-size arrays. */
8067 constructor_max_index =
8068 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8069 constructor_index = bitsize_zero_node;
8070 constructor_unfilled_index = constructor_index;
8071 }
8072 else
8073 {
8074 /* Handle the case of int x = {5}; */
8075 constructor_fields = constructor_type;
8076 constructor_unfilled_fields = constructor_type;
8077 }
8078 }
8079 \f
8080 extern location_t last_init_list_comma;
8081
8082 /* Called when we see an open brace for a nested initializer. Finish
8083 off any pending levels with implicit braces. */
8084 void
8085 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8086 {
8087 while (constructor_stack->implicit)
8088 {
8089 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8090 && constructor_fields == NULL_TREE)
8091 process_init_element (input_location,
8092 pop_init_level (loc, 1, braced_init_obstack,
8093 last_init_list_comma),
8094 true, braced_init_obstack);
8095 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8096 && constructor_max_index
8097 && tree_int_cst_lt (constructor_max_index,
8098 constructor_index))
8099 process_init_element (input_location,
8100 pop_init_level (loc, 1, braced_init_obstack,
8101 last_init_list_comma),
8102 true, braced_init_obstack);
8103 else
8104 break;
8105 }
8106 }
8107
8108 /* Push down into a subobject, for initialization.
8109 If this is for an explicit set of braces, IMPLICIT is 0.
8110 If it is because the next element belongs at a lower level,
8111 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8112
8113 void
8114 push_init_level (location_t loc, int implicit,
8115 struct obstack *braced_init_obstack)
8116 {
8117 struct constructor_stack *p;
8118 tree value = NULL_TREE;
8119
8120 /* Unless this is an explicit brace, we need to preserve previous
8121 content if any. */
8122 if (implicit)
8123 {
8124 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8125 value = find_init_member (constructor_fields, braced_init_obstack);
8126 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8127 value = find_init_member (constructor_index, braced_init_obstack);
8128 }
8129
8130 p = XNEW (struct constructor_stack);
8131 p->type = constructor_type;
8132 p->fields = constructor_fields;
8133 p->index = constructor_index;
8134 p->max_index = constructor_max_index;
8135 p->unfilled_index = constructor_unfilled_index;
8136 p->unfilled_fields = constructor_unfilled_fields;
8137 p->bit_index = constructor_bit_index;
8138 p->elements = constructor_elements;
8139 p->constant = constructor_constant;
8140 p->simple = constructor_simple;
8141 p->nonconst = constructor_nonconst;
8142 p->erroneous = constructor_erroneous;
8143 p->pending_elts = constructor_pending_elts;
8144 p->depth = constructor_depth;
8145 p->replacement_value.value = NULL_TREE;
8146 p->replacement_value.original_code = ERROR_MARK;
8147 p->replacement_value.original_type = NULL;
8148 p->implicit = implicit;
8149 p->outer = 0;
8150 p->incremental = constructor_incremental;
8151 p->designated = constructor_designated;
8152 p->designator_depth = designator_depth;
8153 p->next = constructor_stack;
8154 p->range_stack = 0;
8155 constructor_stack = p;
8156
8157 constructor_constant = 1;
8158 constructor_simple = 1;
8159 constructor_nonconst = 0;
8160 constructor_depth = SPELLING_DEPTH ();
8161 constructor_elements = NULL;
8162 constructor_incremental = 1;
8163 constructor_designated = 0;
8164 constructor_pending_elts = 0;
8165 if (!implicit)
8166 {
8167 p->range_stack = constructor_range_stack;
8168 constructor_range_stack = 0;
8169 designator_depth = 0;
8170 designator_erroneous = 0;
8171 }
8172
8173 /* Don't die if an entire brace-pair level is superfluous
8174 in the containing level. */
8175 if (constructor_type == NULL_TREE)
8176 ;
8177 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8178 {
8179 /* Don't die if there are extra init elts at the end. */
8180 if (constructor_fields == NULL_TREE)
8181 constructor_type = NULL_TREE;
8182 else
8183 {
8184 constructor_type = TREE_TYPE (constructor_fields);
8185 push_member_name (constructor_fields);
8186 constructor_depth++;
8187 }
8188 /* If upper initializer is designated, then mark this as
8189 designated too to prevent bogus warnings. */
8190 constructor_designated = p->designated;
8191 }
8192 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8193 {
8194 constructor_type = TREE_TYPE (constructor_type);
8195 push_array_bounds (tree_to_uhwi (constructor_index));
8196 constructor_depth++;
8197 }
8198
8199 if (constructor_type == NULL_TREE)
8200 {
8201 error_init (loc, "extra brace group at end of initializer");
8202 constructor_fields = NULL_TREE;
8203 constructor_unfilled_fields = NULL_TREE;
8204 return;
8205 }
8206
8207 if (value && TREE_CODE (value) == CONSTRUCTOR)
8208 {
8209 constructor_constant = TREE_CONSTANT (value);
8210 constructor_simple = TREE_STATIC (value);
8211 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8212 constructor_elements = CONSTRUCTOR_ELTS (value);
8213 if (!vec_safe_is_empty (constructor_elements)
8214 && (TREE_CODE (constructor_type) == RECORD_TYPE
8215 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8216 set_nonincremental_init (braced_init_obstack);
8217 }
8218
8219 if (implicit == 1)
8220 {
8221 found_missing_braces = 1;
8222 if (initializer_stack->missing_brace_richloc)
8223 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8224 (loc, "{");
8225 }
8226
8227 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8228 {
8229 constructor_fields = TYPE_FIELDS (constructor_type);
8230 /* Skip any nameless bit fields at the beginning. */
8231 while (constructor_fields != NULL_TREE
8232 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8233 constructor_fields = DECL_CHAIN (constructor_fields);
8234
8235 constructor_unfilled_fields = constructor_fields;
8236 constructor_bit_index = bitsize_zero_node;
8237 }
8238 else if (VECTOR_TYPE_P (constructor_type))
8239 {
8240 /* Vectors are like simple fixed-size arrays. */
8241 constructor_max_index =
8242 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8243 constructor_index = bitsize_int (0);
8244 constructor_unfilled_index = constructor_index;
8245 }
8246 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8247 {
8248 if (TYPE_DOMAIN (constructor_type))
8249 {
8250 constructor_max_index
8251 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8252
8253 /* Detect non-empty initializations of zero-length arrays. */
8254 if (constructor_max_index == NULL_TREE
8255 && TYPE_SIZE (constructor_type))
8256 constructor_max_index = integer_minus_one_node;
8257
8258 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8259 to initialize VLAs will cause a proper error; avoid tree
8260 checking errors as well by setting a safe value. */
8261 if (constructor_max_index
8262 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8263 constructor_max_index = integer_minus_one_node;
8264
8265 constructor_index
8266 = convert (bitsizetype,
8267 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8268 }
8269 else
8270 constructor_index = bitsize_zero_node;
8271
8272 constructor_unfilled_index = constructor_index;
8273 if (value && TREE_CODE (value) == STRING_CST)
8274 {
8275 /* We need to split the char/wchar array into individual
8276 characters, so that we don't have to special case it
8277 everywhere. */
8278 set_nonincremental_init_from_string (value, braced_init_obstack);
8279 }
8280 }
8281 else
8282 {
8283 if (constructor_type != error_mark_node)
8284 warning_init (input_location, 0, "braces around scalar initializer");
8285 constructor_fields = constructor_type;
8286 constructor_unfilled_fields = constructor_type;
8287 }
8288 }
8289
8290 /* At the end of an implicit or explicit brace level,
8291 finish up that level of constructor. If a single expression
8292 with redundant braces initialized that level, return the
8293 c_expr structure for that expression. Otherwise, the original_code
8294 element is set to ERROR_MARK.
8295 If we were outputting the elements as they are read, return 0 as the value
8296 from inner levels (process_init_element ignores that),
8297 but return error_mark_node as the value from the outermost level
8298 (that's what we want to put in DECL_INITIAL).
8299 Otherwise, return a CONSTRUCTOR expression as the value. */
8300
8301 struct c_expr
8302 pop_init_level (location_t loc, int implicit,
8303 struct obstack *braced_init_obstack,
8304 location_t insert_before)
8305 {
8306 struct constructor_stack *p;
8307 struct c_expr ret;
8308 ret.value = NULL_TREE;
8309 ret.original_code = ERROR_MARK;
8310 ret.original_type = NULL;
8311
8312 if (implicit == 0)
8313 {
8314 /* When we come to an explicit close brace,
8315 pop any inner levels that didn't have explicit braces. */
8316 while (constructor_stack->implicit)
8317 process_init_element (input_location,
8318 pop_init_level (loc, 1, braced_init_obstack,
8319 insert_before),
8320 true, braced_init_obstack);
8321 gcc_assert (!constructor_range_stack);
8322 }
8323 else
8324 if (initializer_stack->missing_brace_richloc)
8325 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8326 (insert_before, "}");
8327
8328 /* Now output all pending elements. */
8329 constructor_incremental = 1;
8330 output_pending_init_elements (1, braced_init_obstack);
8331
8332 p = constructor_stack;
8333
8334 /* Error for initializing a flexible array member, or a zero-length
8335 array member in an inappropriate context. */
8336 if (constructor_type && constructor_fields
8337 && TREE_CODE (constructor_type) == ARRAY_TYPE
8338 && TYPE_DOMAIN (constructor_type)
8339 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8340 {
8341 /* Silently discard empty initializations. The parser will
8342 already have pedwarned for empty brackets. */
8343 if (integer_zerop (constructor_unfilled_index))
8344 constructor_type = NULL_TREE;
8345 else
8346 {
8347 gcc_assert (!TYPE_SIZE (constructor_type));
8348
8349 if (constructor_depth > 2)
8350 error_init (loc, "initialization of flexible array member in a nested context");
8351 else
8352 pedwarn_init (loc, OPT_Wpedantic,
8353 "initialization of a flexible array member");
8354
8355 /* We have already issued an error message for the existence
8356 of a flexible array member not at the end of the structure.
8357 Discard the initializer so that we do not die later. */
8358 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8359 constructor_type = NULL_TREE;
8360 }
8361 }
8362
8363 switch (vec_safe_length (constructor_elements))
8364 {
8365 case 0:
8366 /* Initialization with { } counts as zeroinit. */
8367 constructor_zeroinit = 1;
8368 break;
8369 case 1:
8370 /* This might be zeroinit as well. */
8371 if (integer_zerop ((*constructor_elements)[0].value))
8372 constructor_zeroinit = 1;
8373 break;
8374 default:
8375 /* If the constructor has more than one element, it can't be { 0 }. */
8376 constructor_zeroinit = 0;
8377 break;
8378 }
8379
8380 /* Warn when some structs are initialized with direct aggregation. */
8381 if (!implicit && found_missing_braces && warn_missing_braces
8382 && !constructor_zeroinit)
8383 {
8384 gcc_assert (initializer_stack->missing_brace_richloc);
8385 warning_at (initializer_stack->missing_brace_richloc,
8386 OPT_Wmissing_braces,
8387 "missing braces around initializer");
8388 }
8389
8390 /* Warn when some struct elements are implicitly initialized to zero. */
8391 if (warn_missing_field_initializers
8392 && constructor_type
8393 && TREE_CODE (constructor_type) == RECORD_TYPE
8394 && constructor_unfilled_fields)
8395 {
8396 /* Do not warn for flexible array members or zero-length arrays. */
8397 while (constructor_unfilled_fields
8398 && (!DECL_SIZE (constructor_unfilled_fields)
8399 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8400 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8401
8402 if (constructor_unfilled_fields
8403 /* Do not warn if this level of the initializer uses member
8404 designators; it is likely to be deliberate. */
8405 && !constructor_designated
8406 /* Do not warn about initializing with { 0 } or with { }. */
8407 && !constructor_zeroinit)
8408 {
8409 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8410 "missing initializer for field %qD of %qT",
8411 constructor_unfilled_fields,
8412 constructor_type))
8413 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8414 "%qD declared here", constructor_unfilled_fields);
8415 }
8416 }
8417
8418 /* Pad out the end of the structure. */
8419 if (p->replacement_value.value)
8420 /* If this closes a superfluous brace pair,
8421 just pass out the element between them. */
8422 ret = p->replacement_value;
8423 else if (constructor_type == NULL_TREE)
8424 ;
8425 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8426 && TREE_CODE (constructor_type) != ARRAY_TYPE
8427 && !VECTOR_TYPE_P (constructor_type))
8428 {
8429 /* A nonincremental scalar initializer--just return
8430 the element, after verifying there is just one. */
8431 if (vec_safe_is_empty (constructor_elements))
8432 {
8433 if (!constructor_erroneous)
8434 error_init (loc, "empty scalar initializer");
8435 ret.value = error_mark_node;
8436 }
8437 else if (vec_safe_length (constructor_elements) != 1)
8438 {
8439 error_init (loc, "extra elements in scalar initializer");
8440 ret.value = (*constructor_elements)[0].value;
8441 }
8442 else
8443 ret.value = (*constructor_elements)[0].value;
8444 }
8445 else
8446 {
8447 if (constructor_erroneous)
8448 ret.value = error_mark_node;
8449 else
8450 {
8451 ret.value = build_constructor (constructor_type,
8452 constructor_elements);
8453 if (constructor_constant)
8454 TREE_CONSTANT (ret.value) = 1;
8455 if (constructor_constant && constructor_simple)
8456 TREE_STATIC (ret.value) = 1;
8457 if (constructor_nonconst)
8458 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8459 }
8460 }
8461
8462 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8463 {
8464 if (constructor_nonconst)
8465 ret.original_code = C_MAYBE_CONST_EXPR;
8466 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8467 ret.original_code = ERROR_MARK;
8468 }
8469
8470 constructor_type = p->type;
8471 constructor_fields = p->fields;
8472 constructor_index = p->index;
8473 constructor_max_index = p->max_index;
8474 constructor_unfilled_index = p->unfilled_index;
8475 constructor_unfilled_fields = p->unfilled_fields;
8476 constructor_bit_index = p->bit_index;
8477 constructor_elements = p->elements;
8478 constructor_constant = p->constant;
8479 constructor_simple = p->simple;
8480 constructor_nonconst = p->nonconst;
8481 constructor_erroneous = p->erroneous;
8482 constructor_incremental = p->incremental;
8483 constructor_designated = p->designated;
8484 designator_depth = p->designator_depth;
8485 constructor_pending_elts = p->pending_elts;
8486 constructor_depth = p->depth;
8487 if (!p->implicit)
8488 constructor_range_stack = p->range_stack;
8489 RESTORE_SPELLING_DEPTH (constructor_depth);
8490
8491 constructor_stack = p->next;
8492 free (p);
8493
8494 if (ret.value == NULL_TREE && constructor_stack == 0)
8495 ret.value = error_mark_node;
8496 return ret;
8497 }
8498
8499 /* Common handling for both array range and field name designators.
8500 ARRAY argument is nonzero for array ranges. Returns false for success. */
8501
8502 static bool
8503 set_designator (location_t loc, bool array,
8504 struct obstack *braced_init_obstack)
8505 {
8506 tree subtype;
8507 enum tree_code subcode;
8508
8509 /* Don't die if an entire brace-pair level is superfluous
8510 in the containing level. */
8511 if (constructor_type == NULL_TREE)
8512 return true;
8513
8514 /* If there were errors in this designator list already, bail out
8515 silently. */
8516 if (designator_erroneous)
8517 return true;
8518
8519 if (!designator_depth)
8520 {
8521 gcc_assert (!constructor_range_stack);
8522
8523 /* Designator list starts at the level of closest explicit
8524 braces. */
8525 while (constructor_stack->implicit)
8526 process_init_element (input_location,
8527 pop_init_level (loc, 1, braced_init_obstack,
8528 last_init_list_comma),
8529 true, braced_init_obstack);
8530 constructor_designated = 1;
8531 return false;
8532 }
8533
8534 switch (TREE_CODE (constructor_type))
8535 {
8536 case RECORD_TYPE:
8537 case UNION_TYPE:
8538 subtype = TREE_TYPE (constructor_fields);
8539 if (subtype != error_mark_node)
8540 subtype = TYPE_MAIN_VARIANT (subtype);
8541 break;
8542 case ARRAY_TYPE:
8543 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8544 break;
8545 default:
8546 gcc_unreachable ();
8547 }
8548
8549 subcode = TREE_CODE (subtype);
8550 if (array && subcode != ARRAY_TYPE)
8551 {
8552 error_init (loc, "array index in non-array initializer");
8553 return true;
8554 }
8555 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8556 {
8557 error_init (loc, "field name not in record or union initializer");
8558 return true;
8559 }
8560
8561 constructor_designated = 1;
8562 finish_implicit_inits (loc, braced_init_obstack);
8563 push_init_level (loc, 2, braced_init_obstack);
8564 return false;
8565 }
8566
8567 /* If there are range designators in designator list, push a new designator
8568 to constructor_range_stack. RANGE_END is end of such stack range or
8569 NULL_TREE if there is no range designator at this level. */
8570
8571 static void
8572 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8573 {
8574 struct constructor_range_stack *p;
8575
8576 p = (struct constructor_range_stack *)
8577 obstack_alloc (braced_init_obstack,
8578 sizeof (struct constructor_range_stack));
8579 p->prev = constructor_range_stack;
8580 p->next = 0;
8581 p->fields = constructor_fields;
8582 p->range_start = constructor_index;
8583 p->index = constructor_index;
8584 p->stack = constructor_stack;
8585 p->range_end = range_end;
8586 if (constructor_range_stack)
8587 constructor_range_stack->next = p;
8588 constructor_range_stack = p;
8589 }
8590
8591 /* Within an array initializer, specify the next index to be initialized.
8592 FIRST is that index. If LAST is nonzero, then initialize a range
8593 of indices, running from FIRST through LAST. */
8594
8595 void
8596 set_init_index (location_t loc, tree first, tree last,
8597 struct obstack *braced_init_obstack)
8598 {
8599 if (set_designator (loc, true, braced_init_obstack))
8600 return;
8601
8602 designator_erroneous = 1;
8603
8604 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8605 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8606 {
8607 error_init (loc, "array index in initializer not of integer type");
8608 return;
8609 }
8610
8611 if (TREE_CODE (first) != INTEGER_CST)
8612 {
8613 first = c_fully_fold (first, false, NULL);
8614 if (TREE_CODE (first) == INTEGER_CST)
8615 pedwarn_init (loc, OPT_Wpedantic,
8616 "array index in initializer is not "
8617 "an integer constant expression");
8618 }
8619
8620 if (last && TREE_CODE (last) != INTEGER_CST)
8621 {
8622 last = c_fully_fold (last, false, NULL);
8623 if (TREE_CODE (last) == INTEGER_CST)
8624 pedwarn_init (loc, OPT_Wpedantic,
8625 "array index in initializer is not "
8626 "an integer constant expression");
8627 }
8628
8629 if (TREE_CODE (first) != INTEGER_CST)
8630 error_init (loc, "nonconstant array index in initializer");
8631 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8632 error_init (loc, "nonconstant array index in initializer");
8633 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8634 error_init (loc, "array index in non-array initializer");
8635 else if (tree_int_cst_sgn (first) == -1)
8636 error_init (loc, "array index in initializer exceeds array bounds");
8637 else if (constructor_max_index
8638 && tree_int_cst_lt (constructor_max_index, first))
8639 error_init (loc, "array index in initializer exceeds array bounds");
8640 else
8641 {
8642 constant_expression_warning (first);
8643 if (last)
8644 constant_expression_warning (last);
8645 constructor_index = convert (bitsizetype, first);
8646 if (tree_int_cst_lt (constructor_index, first))
8647 {
8648 constructor_index = copy_node (constructor_index);
8649 TREE_OVERFLOW (constructor_index) = 1;
8650 }
8651
8652 if (last)
8653 {
8654 if (tree_int_cst_equal (first, last))
8655 last = NULL_TREE;
8656 else if (tree_int_cst_lt (last, first))
8657 {
8658 error_init (loc, "empty index range in initializer");
8659 last = NULL_TREE;
8660 }
8661 else
8662 {
8663 last = convert (bitsizetype, last);
8664 if (constructor_max_index != NULL_TREE
8665 && tree_int_cst_lt (constructor_max_index, last))
8666 {
8667 error_init (loc, "array index range in initializer exceeds "
8668 "array bounds");
8669 last = NULL_TREE;
8670 }
8671 }
8672 }
8673
8674 designator_depth++;
8675 designator_erroneous = 0;
8676 if (constructor_range_stack || last)
8677 push_range_stack (last, braced_init_obstack);
8678 }
8679 }
8680
8681 /* Within a struct initializer, specify the next field to be initialized. */
8682
8683 void
8684 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8685 struct obstack *braced_init_obstack)
8686 {
8687 tree field;
8688
8689 if (set_designator (loc, false, braced_init_obstack))
8690 return;
8691
8692 designator_erroneous = 1;
8693
8694 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8695 {
8696 error_init (loc, "field name not in record or union initializer");
8697 return;
8698 }
8699
8700 field = lookup_field (constructor_type, fieldname);
8701
8702 if (field == NULL_TREE)
8703 {
8704 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8705 if (guessed_id)
8706 {
8707 gcc_rich_location rich_loc (fieldname_loc);
8708 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8709 error_at (&rich_loc,
8710 "%qT has no member named %qE; did you mean %qE?",
8711 constructor_type, fieldname, guessed_id);
8712 }
8713 else
8714 error_at (fieldname_loc, "%qT has no member named %qE",
8715 constructor_type, fieldname);
8716 }
8717 else
8718 do
8719 {
8720 constructor_fields = TREE_VALUE (field);
8721 designator_depth++;
8722 designator_erroneous = 0;
8723 if (constructor_range_stack)
8724 push_range_stack (NULL_TREE, braced_init_obstack);
8725 field = TREE_CHAIN (field);
8726 if (field)
8727 {
8728 if (set_designator (loc, false, braced_init_obstack))
8729 return;
8730 }
8731 }
8732 while (field != NULL_TREE);
8733 }
8734 \f
8735 /* Add a new initializer to the tree of pending initializers. PURPOSE
8736 identifies the initializer, either array index or field in a structure.
8737 VALUE is the value of that index or field. If ORIGTYPE is not
8738 NULL_TREE, it is the original type of VALUE.
8739
8740 IMPLICIT is true if value comes from pop_init_level (1),
8741 the new initializer has been merged with the existing one
8742 and thus no warnings should be emitted about overriding an
8743 existing initializer. */
8744
8745 static void
8746 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8747 bool implicit, struct obstack *braced_init_obstack)
8748 {
8749 struct init_node *p, **q, *r;
8750
8751 q = &constructor_pending_elts;
8752 p = 0;
8753
8754 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8755 {
8756 while (*q != 0)
8757 {
8758 p = *q;
8759 if (tree_int_cst_lt (purpose, p->purpose))
8760 q = &p->left;
8761 else if (tree_int_cst_lt (p->purpose, purpose))
8762 q = &p->right;
8763 else
8764 {
8765 if (!implicit)
8766 {
8767 if (TREE_SIDE_EFFECTS (p->value))
8768 warning_init (loc, OPT_Woverride_init_side_effects,
8769 "initialized field with side-effects "
8770 "overwritten");
8771 else if (warn_override_init)
8772 warning_init (loc, OPT_Woverride_init,
8773 "initialized field overwritten");
8774 }
8775 p->value = value;
8776 p->origtype = origtype;
8777 return;
8778 }
8779 }
8780 }
8781 else
8782 {
8783 tree bitpos;
8784
8785 bitpos = bit_position (purpose);
8786 while (*q != NULL)
8787 {
8788 p = *q;
8789 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8790 q = &p->left;
8791 else if (p->purpose != purpose)
8792 q = &p->right;
8793 else
8794 {
8795 if (!implicit)
8796 {
8797 if (TREE_SIDE_EFFECTS (p->value))
8798 warning_init (loc, OPT_Woverride_init_side_effects,
8799 "initialized field with side-effects "
8800 "overwritten");
8801 else if (warn_override_init)
8802 warning_init (loc, OPT_Woverride_init,
8803 "initialized field overwritten");
8804 }
8805 p->value = value;
8806 p->origtype = origtype;
8807 return;
8808 }
8809 }
8810 }
8811
8812 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8813 sizeof (struct init_node));
8814 r->purpose = purpose;
8815 r->value = value;
8816 r->origtype = origtype;
8817
8818 *q = r;
8819 r->parent = p;
8820 r->left = 0;
8821 r->right = 0;
8822 r->balance = 0;
8823
8824 while (p)
8825 {
8826 struct init_node *s;
8827
8828 if (r == p->left)
8829 {
8830 if (p->balance == 0)
8831 p->balance = -1;
8832 else if (p->balance < 0)
8833 {
8834 if (r->balance < 0)
8835 {
8836 /* L rotation. */
8837 p->left = r->right;
8838 if (p->left)
8839 p->left->parent = p;
8840 r->right = p;
8841
8842 p->balance = 0;
8843 r->balance = 0;
8844
8845 s = p->parent;
8846 p->parent = r;
8847 r->parent = s;
8848 if (s)
8849 {
8850 if (s->left == p)
8851 s->left = r;
8852 else
8853 s->right = r;
8854 }
8855 else
8856 constructor_pending_elts = r;
8857 }
8858 else
8859 {
8860 /* LR rotation. */
8861 struct init_node *t = r->right;
8862
8863 r->right = t->left;
8864 if (r->right)
8865 r->right->parent = r;
8866 t->left = r;
8867
8868 p->left = t->right;
8869 if (p->left)
8870 p->left->parent = p;
8871 t->right = p;
8872
8873 p->balance = t->balance < 0;
8874 r->balance = -(t->balance > 0);
8875 t->balance = 0;
8876
8877 s = p->parent;
8878 p->parent = t;
8879 r->parent = t;
8880 t->parent = s;
8881 if (s)
8882 {
8883 if (s->left == p)
8884 s->left = t;
8885 else
8886 s->right = t;
8887 }
8888 else
8889 constructor_pending_elts = t;
8890 }
8891 break;
8892 }
8893 else
8894 {
8895 /* p->balance == +1; growth of left side balances the node. */
8896 p->balance = 0;
8897 break;
8898 }
8899 }
8900 else /* r == p->right */
8901 {
8902 if (p->balance == 0)
8903 /* Growth propagation from right side. */
8904 p->balance++;
8905 else if (p->balance > 0)
8906 {
8907 if (r->balance > 0)
8908 {
8909 /* R rotation. */
8910 p->right = r->left;
8911 if (p->right)
8912 p->right->parent = p;
8913 r->left = p;
8914
8915 p->balance = 0;
8916 r->balance = 0;
8917
8918 s = p->parent;
8919 p->parent = r;
8920 r->parent = s;
8921 if (s)
8922 {
8923 if (s->left == p)
8924 s->left = r;
8925 else
8926 s->right = r;
8927 }
8928 else
8929 constructor_pending_elts = r;
8930 }
8931 else /* r->balance == -1 */
8932 {
8933 /* RL rotation */
8934 struct init_node *t = r->left;
8935
8936 r->left = t->right;
8937 if (r->left)
8938 r->left->parent = r;
8939 t->right = r;
8940
8941 p->right = t->left;
8942 if (p->right)
8943 p->right->parent = p;
8944 t->left = p;
8945
8946 r->balance = (t->balance < 0);
8947 p->balance = -(t->balance > 0);
8948 t->balance = 0;
8949
8950 s = p->parent;
8951 p->parent = t;
8952 r->parent = t;
8953 t->parent = s;
8954 if (s)
8955 {
8956 if (s->left == p)
8957 s->left = t;
8958 else
8959 s->right = t;
8960 }
8961 else
8962 constructor_pending_elts = t;
8963 }
8964 break;
8965 }
8966 else
8967 {
8968 /* p->balance == -1; growth of right side balances the node. */
8969 p->balance = 0;
8970 break;
8971 }
8972 }
8973
8974 r = p;
8975 p = p->parent;
8976 }
8977 }
8978
8979 /* Build AVL tree from a sorted chain. */
8980
8981 static void
8982 set_nonincremental_init (struct obstack * braced_init_obstack)
8983 {
8984 unsigned HOST_WIDE_INT ix;
8985 tree index, value;
8986
8987 if (TREE_CODE (constructor_type) != RECORD_TYPE
8988 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8989 return;
8990
8991 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8992 add_pending_init (input_location, index, value, NULL_TREE, true,
8993 braced_init_obstack);
8994 constructor_elements = NULL;
8995 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8996 {
8997 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8998 /* Skip any nameless bit fields at the beginning. */
8999 while (constructor_unfilled_fields != NULL_TREE
9000 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9001 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9002
9003 }
9004 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9005 {
9006 if (TYPE_DOMAIN (constructor_type))
9007 constructor_unfilled_index
9008 = convert (bitsizetype,
9009 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9010 else
9011 constructor_unfilled_index = bitsize_zero_node;
9012 }
9013 constructor_incremental = 0;
9014 }
9015
9016 /* Build AVL tree from a string constant. */
9017
9018 static void
9019 set_nonincremental_init_from_string (tree str,
9020 struct obstack * braced_init_obstack)
9021 {
9022 tree value, purpose, type;
9023 HOST_WIDE_INT val[2];
9024 const char *p, *end;
9025 int byte, wchar_bytes, charwidth, bitpos;
9026
9027 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9028
9029 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9030 charwidth = TYPE_PRECISION (char_type_node);
9031 gcc_assert ((size_t) wchar_bytes * charwidth
9032 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9033 type = TREE_TYPE (constructor_type);
9034 p = TREE_STRING_POINTER (str);
9035 end = p + TREE_STRING_LENGTH (str);
9036
9037 for (purpose = bitsize_zero_node;
9038 p < end
9039 && !(constructor_max_index
9040 && tree_int_cst_lt (constructor_max_index, purpose));
9041 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9042 {
9043 if (wchar_bytes == 1)
9044 {
9045 val[0] = (unsigned char) *p++;
9046 val[1] = 0;
9047 }
9048 else
9049 {
9050 val[1] = 0;
9051 val[0] = 0;
9052 for (byte = 0; byte < wchar_bytes; byte++)
9053 {
9054 if (BYTES_BIG_ENDIAN)
9055 bitpos = (wchar_bytes - byte - 1) * charwidth;
9056 else
9057 bitpos = byte * charwidth;
9058 val[bitpos / HOST_BITS_PER_WIDE_INT]
9059 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9060 << (bitpos % HOST_BITS_PER_WIDE_INT);
9061 }
9062 }
9063
9064 if (!TYPE_UNSIGNED (type))
9065 {
9066 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9067 if (bitpos < HOST_BITS_PER_WIDE_INT)
9068 {
9069 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9070 {
9071 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9072 val[1] = -1;
9073 }
9074 }
9075 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9076 {
9077 if (val[0] < 0)
9078 val[1] = -1;
9079 }
9080 else if (val[1] & (HOST_WIDE_INT_1
9081 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9082 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9083 }
9084
9085 value = wide_int_to_tree (type,
9086 wide_int::from_array (val, 2,
9087 HOST_BITS_PER_WIDE_INT * 2));
9088 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9089 braced_init_obstack);
9090 }
9091
9092 constructor_incremental = 0;
9093 }
9094
9095 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9096 not initialized yet. */
9097
9098 static tree
9099 find_init_member (tree field, struct obstack * braced_init_obstack)
9100 {
9101 struct init_node *p;
9102
9103 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9104 {
9105 if (constructor_incremental
9106 && tree_int_cst_lt (field, constructor_unfilled_index))
9107 set_nonincremental_init (braced_init_obstack);
9108
9109 p = constructor_pending_elts;
9110 while (p)
9111 {
9112 if (tree_int_cst_lt (field, p->purpose))
9113 p = p->left;
9114 else if (tree_int_cst_lt (p->purpose, field))
9115 p = p->right;
9116 else
9117 return p->value;
9118 }
9119 }
9120 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9121 {
9122 tree bitpos = bit_position (field);
9123
9124 if (constructor_incremental
9125 && (!constructor_unfilled_fields
9126 || tree_int_cst_lt (bitpos,
9127 bit_position (constructor_unfilled_fields))))
9128 set_nonincremental_init (braced_init_obstack);
9129
9130 p = constructor_pending_elts;
9131 while (p)
9132 {
9133 if (field == p->purpose)
9134 return p->value;
9135 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9136 p = p->left;
9137 else
9138 p = p->right;
9139 }
9140 }
9141 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9142 {
9143 if (!vec_safe_is_empty (constructor_elements)
9144 && (constructor_elements->last ().index == field))
9145 return constructor_elements->last ().value;
9146 }
9147 return NULL_TREE;
9148 }
9149
9150 /* "Output" the next constructor element.
9151 At top level, really output it to assembler code now.
9152 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9153 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9154 TYPE is the data type that the containing data type wants here.
9155 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9156 If VALUE is a string constant, STRICT_STRING is true if it is
9157 unparenthesized or we should not warn here for it being parenthesized.
9158 For other types of VALUE, STRICT_STRING is not used.
9159
9160 PENDING if true means output pending elements that belong
9161 right after this element. (PENDING is normally true;
9162 it is false while outputting pending elements, to avoid recursion.)
9163
9164 IMPLICIT is true if value comes from pop_init_level (1),
9165 the new initializer has been merged with the existing one
9166 and thus no warnings should be emitted about overriding an
9167 existing initializer. */
9168
9169 static void
9170 output_init_element (location_t loc, tree value, tree origtype,
9171 bool strict_string, tree type, tree field, bool pending,
9172 bool implicit, struct obstack * braced_init_obstack)
9173 {
9174 tree semantic_type = NULL_TREE;
9175 bool maybe_const = true;
9176 bool npc;
9177
9178 if (type == error_mark_node || value == error_mark_node)
9179 {
9180 constructor_erroneous = 1;
9181 return;
9182 }
9183 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9184 && (TREE_CODE (value) == STRING_CST
9185 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9186 && !(TREE_CODE (value) == STRING_CST
9187 && TREE_CODE (type) == ARRAY_TYPE
9188 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9189 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9190 TYPE_MAIN_VARIANT (type)))
9191 value = array_to_pointer_conversion (input_location, value);
9192
9193 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9194 && require_constant_value && pending)
9195 {
9196 /* As an extension, allow initializing objects with static storage
9197 duration with compound literals (which are then treated just as
9198 the brace enclosed list they contain). */
9199 if (flag_isoc99)
9200 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9201 "constant");
9202 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9203 value = DECL_INITIAL (decl);
9204 }
9205
9206 npc = null_pointer_constant_p (value);
9207 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9208 {
9209 semantic_type = TREE_TYPE (value);
9210 value = TREE_OPERAND (value, 0);
9211 }
9212 value = c_fully_fold (value, require_constant_value, &maybe_const);
9213
9214 if (value == error_mark_node)
9215 constructor_erroneous = 1;
9216 else if (!TREE_CONSTANT (value))
9217 constructor_constant = 0;
9218 else if (!initializer_constant_valid_p (value,
9219 TREE_TYPE (value),
9220 AGGREGATE_TYPE_P (constructor_type)
9221 && TYPE_REVERSE_STORAGE_ORDER
9222 (constructor_type))
9223 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9224 && DECL_C_BIT_FIELD (field)
9225 && TREE_CODE (value) != INTEGER_CST))
9226 constructor_simple = 0;
9227 if (!maybe_const)
9228 constructor_nonconst = 1;
9229
9230 /* Digest the initializer and issue any errors about incompatible
9231 types before issuing errors about non-constant initializers. */
9232 tree new_value = value;
9233 if (semantic_type)
9234 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9235 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9236 require_constant_value);
9237 if (new_value == error_mark_node)
9238 {
9239 constructor_erroneous = 1;
9240 return;
9241 }
9242 if (require_constant_value || require_constant_elements)
9243 constant_expression_warning (new_value);
9244
9245 /* Proceed to check the constness of the original initializer. */
9246 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9247 {
9248 if (require_constant_value)
9249 {
9250 error_init (loc, "initializer element is not constant");
9251 value = error_mark_node;
9252 }
9253 else if (require_constant_elements)
9254 pedwarn (loc, OPT_Wpedantic,
9255 "initializer element is not computable at load time");
9256 }
9257 else if (!maybe_const
9258 && (require_constant_value || require_constant_elements))
9259 pedwarn_init (loc, OPT_Wpedantic,
9260 "initializer element is not a constant expression");
9261
9262 /* Issue -Wc++-compat warnings about initializing a bitfield with
9263 enum type. */
9264 if (warn_cxx_compat
9265 && field != NULL_TREE
9266 && TREE_CODE (field) == FIELD_DECL
9267 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9268 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9269 != TYPE_MAIN_VARIANT (type))
9270 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9271 {
9272 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9273 if (checktype != error_mark_node
9274 && (TYPE_MAIN_VARIANT (checktype)
9275 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9276 warning_init (loc, OPT_Wc___compat,
9277 "enum conversion in initialization is invalid in C++");
9278 }
9279
9280 /* If this field is empty and does not have side effects (and is not at
9281 the end of structure), don't do anything other than checking the
9282 initializer. */
9283 if (field
9284 && (TREE_TYPE (field) == error_mark_node
9285 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9286 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9287 && !TREE_SIDE_EFFECTS (new_value)
9288 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9289 || DECL_CHAIN (field)))))
9290 return;
9291
9292 /* Finally, set VALUE to the initializer value digested above. */
9293 value = new_value;
9294
9295 /* If this element doesn't come next in sequence,
9296 put it on constructor_pending_elts. */
9297 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9298 && (!constructor_incremental
9299 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9300 {
9301 if (constructor_incremental
9302 && tree_int_cst_lt (field, constructor_unfilled_index))
9303 set_nonincremental_init (braced_init_obstack);
9304
9305 add_pending_init (loc, field, value, origtype, implicit,
9306 braced_init_obstack);
9307 return;
9308 }
9309 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9310 && (!constructor_incremental
9311 || field != constructor_unfilled_fields))
9312 {
9313 /* We do this for records but not for unions. In a union,
9314 no matter which field is specified, it can be initialized
9315 right away since it starts at the beginning of the union. */
9316 if (constructor_incremental)
9317 {
9318 if (!constructor_unfilled_fields)
9319 set_nonincremental_init (braced_init_obstack);
9320 else
9321 {
9322 tree bitpos, unfillpos;
9323
9324 bitpos = bit_position (field);
9325 unfillpos = bit_position (constructor_unfilled_fields);
9326
9327 if (tree_int_cst_lt (bitpos, unfillpos))
9328 set_nonincremental_init (braced_init_obstack);
9329 }
9330 }
9331
9332 add_pending_init (loc, field, value, origtype, implicit,
9333 braced_init_obstack);
9334 return;
9335 }
9336 else if (TREE_CODE (constructor_type) == UNION_TYPE
9337 && !vec_safe_is_empty (constructor_elements))
9338 {
9339 if (!implicit)
9340 {
9341 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9342 warning_init (loc, OPT_Woverride_init_side_effects,
9343 "initialized field with side-effects overwritten");
9344 else if (warn_override_init)
9345 warning_init (loc, OPT_Woverride_init,
9346 "initialized field overwritten");
9347 }
9348
9349 /* We can have just one union field set. */
9350 constructor_elements = NULL;
9351 }
9352
9353 /* Otherwise, output this element either to
9354 constructor_elements or to the assembler file. */
9355
9356 constructor_elt celt = {field, value};
9357 vec_safe_push (constructor_elements, celt);
9358
9359 /* Advance the variable that indicates sequential elements output. */
9360 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9361 constructor_unfilled_index
9362 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9363 bitsize_one_node);
9364 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9365 {
9366 constructor_unfilled_fields
9367 = DECL_CHAIN (constructor_unfilled_fields);
9368
9369 /* Skip any nameless bit fields. */
9370 while (constructor_unfilled_fields != NULL_TREE
9371 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9372 constructor_unfilled_fields =
9373 DECL_CHAIN (constructor_unfilled_fields);
9374 }
9375 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9376 constructor_unfilled_fields = NULL_TREE;
9377
9378 /* Now output any pending elements which have become next. */
9379 if (pending)
9380 output_pending_init_elements (0, braced_init_obstack);
9381 }
9382
9383 /* For two FIELD_DECLs in the same chain, return -1 if field1
9384 comes before field2, 1 if field1 comes after field2 and
9385 0 if field1 == field2. */
9386
9387 static int
9388 init_field_decl_cmp (tree field1, tree field2)
9389 {
9390 if (field1 == field2)
9391 return 0;
9392
9393 tree bitpos1 = bit_position (field1);
9394 tree bitpos2 = bit_position (field2);
9395 if (tree_int_cst_equal (bitpos1, bitpos2))
9396 {
9397 /* If one of the fields has non-zero bitsize, then that
9398 field must be the last one in a sequence of zero
9399 sized fields, fields after it will have bigger
9400 bit_position. */
9401 if (TREE_TYPE (field1) != error_mark_node
9402 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9403 && integer_nonzerop (TREE_TYPE (field1)))
9404 return 1;
9405 if (TREE_TYPE (field2) != error_mark_node
9406 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9407 && integer_nonzerop (TREE_TYPE (field2)))
9408 return -1;
9409 /* Otherwise, fallback to DECL_CHAIN walk to find out
9410 which field comes earlier. Walk chains of both
9411 fields, so that if field1 and field2 are close to each
9412 other in either order, it is found soon even for large
9413 sequences of zero sized fields. */
9414 tree f1 = field1, f2 = field2;
9415 while (1)
9416 {
9417 f1 = DECL_CHAIN (f1);
9418 f2 = DECL_CHAIN (f2);
9419 if (f1 == NULL_TREE)
9420 {
9421 gcc_assert (f2);
9422 return 1;
9423 }
9424 if (f2 == NULL_TREE)
9425 return -1;
9426 if (f1 == field2)
9427 return -1;
9428 if (f2 == field1)
9429 return 1;
9430 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9431 return 1;
9432 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9433 return -1;
9434 }
9435 }
9436 else if (tree_int_cst_lt (bitpos1, bitpos2))
9437 return -1;
9438 else
9439 return 1;
9440 }
9441
9442 /* Output any pending elements which have become next.
9443 As we output elements, constructor_unfilled_{fields,index}
9444 advances, which may cause other elements to become next;
9445 if so, they too are output.
9446
9447 If ALL is 0, we return when there are
9448 no more pending elements to output now.
9449
9450 If ALL is 1, we output space as necessary so that
9451 we can output all the pending elements. */
9452 static void
9453 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9454 {
9455 struct init_node *elt = constructor_pending_elts;
9456 tree next;
9457
9458 retry:
9459
9460 /* Look through the whole pending tree.
9461 If we find an element that should be output now,
9462 output it. Otherwise, set NEXT to the element
9463 that comes first among those still pending. */
9464
9465 next = NULL_TREE;
9466 while (elt)
9467 {
9468 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9469 {
9470 if (tree_int_cst_equal (elt->purpose,
9471 constructor_unfilled_index))
9472 output_init_element (input_location, elt->value, elt->origtype,
9473 true, TREE_TYPE (constructor_type),
9474 constructor_unfilled_index, false, false,
9475 braced_init_obstack);
9476 else if (tree_int_cst_lt (constructor_unfilled_index,
9477 elt->purpose))
9478 {
9479 /* Advance to the next smaller node. */
9480 if (elt->left)
9481 elt = elt->left;
9482 else
9483 {
9484 /* We have reached the smallest node bigger than the
9485 current unfilled index. Fill the space first. */
9486 next = elt->purpose;
9487 break;
9488 }
9489 }
9490 else
9491 {
9492 /* Advance to the next bigger node. */
9493 if (elt->right)
9494 elt = elt->right;
9495 else
9496 {
9497 /* We have reached the biggest node in a subtree. Find
9498 the parent of it, which is the next bigger node. */
9499 while (elt->parent && elt->parent->right == elt)
9500 elt = elt->parent;
9501 elt = elt->parent;
9502 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9503 elt->purpose))
9504 {
9505 next = elt->purpose;
9506 break;
9507 }
9508 }
9509 }
9510 }
9511 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9512 {
9513 /* If the current record is complete we are done. */
9514 if (constructor_unfilled_fields == NULL_TREE)
9515 break;
9516
9517 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9518 elt->purpose);
9519 if (cmp == 0)
9520 output_init_element (input_location, elt->value, elt->origtype,
9521 true, TREE_TYPE (elt->purpose),
9522 elt->purpose, false, false,
9523 braced_init_obstack);
9524 else if (cmp < 0)
9525 {
9526 /* Advance to the next smaller node. */
9527 if (elt->left)
9528 elt = elt->left;
9529 else
9530 {
9531 /* We have reached the smallest node bigger than the
9532 current unfilled field. Fill the space first. */
9533 next = elt->purpose;
9534 break;
9535 }
9536 }
9537 else
9538 {
9539 /* Advance to the next bigger node. */
9540 if (elt->right)
9541 elt = elt->right;
9542 else
9543 {
9544 /* We have reached the biggest node in a subtree. Find
9545 the parent of it, which is the next bigger node. */
9546 while (elt->parent && elt->parent->right == elt)
9547 elt = elt->parent;
9548 elt = elt->parent;
9549 if (elt
9550 && init_field_decl_cmp (constructor_unfilled_fields,
9551 elt->purpose) < 0)
9552 {
9553 next = elt->purpose;
9554 break;
9555 }
9556 }
9557 }
9558 }
9559 }
9560
9561 /* Ordinarily return, but not if we want to output all
9562 and there are elements left. */
9563 if (!(all && next != NULL_TREE))
9564 return;
9565
9566 /* If it's not incremental, just skip over the gap, so that after
9567 jumping to retry we will output the next successive element. */
9568 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9569 constructor_unfilled_fields = next;
9570 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9571 constructor_unfilled_index = next;
9572
9573 /* ELT now points to the node in the pending tree with the next
9574 initializer to output. */
9575 goto retry;
9576 }
9577 \f
9578 /* Add one non-braced element to the current constructor level.
9579 This adjusts the current position within the constructor's type.
9580 This may also start or terminate implicit levels
9581 to handle a partly-braced initializer.
9582
9583 Once this has found the correct level for the new element,
9584 it calls output_init_element.
9585
9586 IMPLICIT is true if value comes from pop_init_level (1),
9587 the new initializer has been merged with the existing one
9588 and thus no warnings should be emitted about overriding an
9589 existing initializer. */
9590
9591 void
9592 process_init_element (location_t loc, struct c_expr value, bool implicit,
9593 struct obstack * braced_init_obstack)
9594 {
9595 tree orig_value = value.value;
9596 int string_flag
9597 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9598 bool strict_string = value.original_code == STRING_CST;
9599 bool was_designated = designator_depth != 0;
9600
9601 designator_depth = 0;
9602 designator_erroneous = 0;
9603
9604 if (!implicit && value.value && !integer_zerop (value.value))
9605 constructor_zeroinit = 0;
9606
9607 /* Handle superfluous braces around string cst as in
9608 char x[] = {"foo"}; */
9609 if (string_flag
9610 && constructor_type
9611 && !was_designated
9612 && TREE_CODE (constructor_type) == ARRAY_TYPE
9613 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9614 && integer_zerop (constructor_unfilled_index))
9615 {
9616 if (constructor_stack->replacement_value.value)
9617 error_init (loc, "excess elements in char array initializer");
9618 constructor_stack->replacement_value = value;
9619 return;
9620 }
9621
9622 if (constructor_stack->replacement_value.value != NULL_TREE)
9623 {
9624 error_init (loc, "excess elements in struct initializer");
9625 return;
9626 }
9627
9628 /* Ignore elements of a brace group if it is entirely superfluous
9629 and has already been diagnosed. */
9630 if (constructor_type == NULL_TREE)
9631 return;
9632
9633 if (!implicit && warn_designated_init && !was_designated
9634 && TREE_CODE (constructor_type) == RECORD_TYPE
9635 && lookup_attribute ("designated_init",
9636 TYPE_ATTRIBUTES (constructor_type)))
9637 warning_init (loc,
9638 OPT_Wdesignated_init,
9639 "positional initialization of field "
9640 "in %<struct%> declared with %<designated_init%> attribute");
9641
9642 /* If we've exhausted any levels that didn't have braces,
9643 pop them now. */
9644 while (constructor_stack->implicit)
9645 {
9646 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9647 && constructor_fields == NULL_TREE)
9648 process_init_element (loc,
9649 pop_init_level (loc, 1, braced_init_obstack,
9650 last_init_list_comma),
9651 true, braced_init_obstack);
9652 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9653 || VECTOR_TYPE_P (constructor_type))
9654 && constructor_max_index
9655 && tree_int_cst_lt (constructor_max_index,
9656 constructor_index))
9657 process_init_element (loc,
9658 pop_init_level (loc, 1, braced_init_obstack,
9659 last_init_list_comma),
9660 true, braced_init_obstack);
9661 else
9662 break;
9663 }
9664
9665 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9666 if (constructor_range_stack)
9667 {
9668 /* If value is a compound literal and we'll be just using its
9669 content, don't put it into a SAVE_EXPR. */
9670 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9671 || !require_constant_value)
9672 {
9673 tree semantic_type = NULL_TREE;
9674 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9675 {
9676 semantic_type = TREE_TYPE (value.value);
9677 value.value = TREE_OPERAND (value.value, 0);
9678 }
9679 value.value = save_expr (value.value);
9680 if (semantic_type)
9681 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9682 value.value);
9683 }
9684 }
9685
9686 while (1)
9687 {
9688 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9689 {
9690 tree fieldtype;
9691 enum tree_code fieldcode;
9692
9693 if (constructor_fields == NULL_TREE)
9694 {
9695 pedwarn_init (loc, 0, "excess elements in struct initializer");
9696 break;
9697 }
9698
9699 fieldtype = TREE_TYPE (constructor_fields);
9700 if (fieldtype != error_mark_node)
9701 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9702 fieldcode = TREE_CODE (fieldtype);
9703
9704 /* Error for non-static initialization of a flexible array member. */
9705 if (fieldcode == ARRAY_TYPE
9706 && !require_constant_value
9707 && TYPE_SIZE (fieldtype) == NULL_TREE
9708 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9709 {
9710 error_init (loc, "non-static initialization of a flexible "
9711 "array member");
9712 break;
9713 }
9714
9715 /* Error for initialization of a flexible array member with
9716 a string constant if the structure is in an array. E.g.:
9717 struct S { int x; char y[]; };
9718 struct S s[] = { { 1, "foo" } };
9719 is invalid. */
9720 if (string_flag
9721 && fieldcode == ARRAY_TYPE
9722 && constructor_depth > 1
9723 && TYPE_SIZE (fieldtype) == NULL_TREE
9724 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9725 {
9726 bool in_array_p = false;
9727 for (struct constructor_stack *p = constructor_stack;
9728 p && p->type; p = p->next)
9729 if (TREE_CODE (p->type) == ARRAY_TYPE)
9730 {
9731 in_array_p = true;
9732 break;
9733 }
9734 if (in_array_p)
9735 {
9736 error_init (loc, "initialization of flexible array "
9737 "member in a nested context");
9738 break;
9739 }
9740 }
9741
9742 /* Accept a string constant to initialize a subarray. */
9743 if (value.value != NULL_TREE
9744 && fieldcode == ARRAY_TYPE
9745 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9746 && string_flag)
9747 value.value = orig_value;
9748 /* Otherwise, if we have come to a subaggregate,
9749 and we don't have an element of its type, push into it. */
9750 else if (value.value != NULL_TREE
9751 && value.value != error_mark_node
9752 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9753 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9754 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9755 {
9756 push_init_level (loc, 1, braced_init_obstack);
9757 continue;
9758 }
9759
9760 if (value.value)
9761 {
9762 push_member_name (constructor_fields);
9763 output_init_element (loc, value.value, value.original_type,
9764 strict_string, fieldtype,
9765 constructor_fields, true, implicit,
9766 braced_init_obstack);
9767 RESTORE_SPELLING_DEPTH (constructor_depth);
9768 }
9769 else
9770 /* Do the bookkeeping for an element that was
9771 directly output as a constructor. */
9772 {
9773 /* For a record, keep track of end position of last field. */
9774 if (DECL_SIZE (constructor_fields))
9775 constructor_bit_index
9776 = size_binop_loc (input_location, PLUS_EXPR,
9777 bit_position (constructor_fields),
9778 DECL_SIZE (constructor_fields));
9779
9780 /* If the current field was the first one not yet written out,
9781 it isn't now, so update. */
9782 if (constructor_unfilled_fields == constructor_fields)
9783 {
9784 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9785 /* Skip any nameless bit fields. */
9786 while (constructor_unfilled_fields != 0
9787 && (DECL_UNNAMED_BIT_FIELD
9788 (constructor_unfilled_fields)))
9789 constructor_unfilled_fields =
9790 DECL_CHAIN (constructor_unfilled_fields);
9791 }
9792 }
9793
9794 constructor_fields = DECL_CHAIN (constructor_fields);
9795 /* Skip any nameless bit fields at the beginning. */
9796 while (constructor_fields != NULL_TREE
9797 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9798 constructor_fields = DECL_CHAIN (constructor_fields);
9799 }
9800 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9801 {
9802 tree fieldtype;
9803 enum tree_code fieldcode;
9804
9805 if (constructor_fields == NULL_TREE)
9806 {
9807 pedwarn_init (loc, 0,
9808 "excess elements in union initializer");
9809 break;
9810 }
9811
9812 fieldtype = TREE_TYPE (constructor_fields);
9813 if (fieldtype != error_mark_node)
9814 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9815 fieldcode = TREE_CODE (fieldtype);
9816
9817 /* Warn that traditional C rejects initialization of unions.
9818 We skip the warning if the value is zero. This is done
9819 under the assumption that the zero initializer in user
9820 code appears conditioned on e.g. __STDC__ to avoid
9821 "missing initializer" warnings and relies on default
9822 initialization to zero in the traditional C case.
9823 We also skip the warning if the initializer is designated,
9824 again on the assumption that this must be conditional on
9825 __STDC__ anyway (and we've already complained about the
9826 member-designator already). */
9827 if (!in_system_header_at (input_location) && !constructor_designated
9828 && !(value.value && (integer_zerop (value.value)
9829 || real_zerop (value.value))))
9830 warning (OPT_Wtraditional, "traditional C rejects initialization "
9831 "of unions");
9832
9833 /* Accept a string constant to initialize a subarray. */
9834 if (value.value != NULL_TREE
9835 && fieldcode == ARRAY_TYPE
9836 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
9837 && string_flag)
9838 value.value = orig_value;
9839 /* Otherwise, if we have come to a subaggregate,
9840 and we don't have an element of its type, push into it. */
9841 else if (value.value != NULL_TREE
9842 && value.value != error_mark_node
9843 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
9844 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
9845 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
9846 {
9847 push_init_level (loc, 1, braced_init_obstack);
9848 continue;
9849 }
9850
9851 if (value.value)
9852 {
9853 push_member_name (constructor_fields);
9854 output_init_element (loc, value.value, value.original_type,
9855 strict_string, fieldtype,
9856 constructor_fields, true, implicit,
9857 braced_init_obstack);
9858 RESTORE_SPELLING_DEPTH (constructor_depth);
9859 }
9860 else
9861 /* Do the bookkeeping for an element that was
9862 directly output as a constructor. */
9863 {
9864 constructor_bit_index = DECL_SIZE (constructor_fields);
9865 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9866 }
9867
9868 constructor_fields = NULL_TREE;
9869 }
9870 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9871 {
9872 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9873 enum tree_code eltcode = TREE_CODE (elttype);
9874
9875 /* Accept a string constant to initialize a subarray. */
9876 if (value.value != NULL_TREE
9877 && eltcode == ARRAY_TYPE
9878 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9879 && string_flag)
9880 value.value = orig_value;
9881 /* Otherwise, if we have come to a subaggregate,
9882 and we don't have an element of its type, push into it. */
9883 else if (value.value != NULL_TREE
9884 && value.value != error_mark_node
9885 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9886 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9887 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9888 {
9889 push_init_level (loc, 1, braced_init_obstack);
9890 continue;
9891 }
9892
9893 if (constructor_max_index != NULL_TREE
9894 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9895 || integer_all_onesp (constructor_max_index)))
9896 {
9897 pedwarn_init (loc, 0,
9898 "excess elements in array initializer");
9899 break;
9900 }
9901
9902 /* Now output the actual element. */
9903 if (value.value)
9904 {
9905 push_array_bounds (tree_to_uhwi (constructor_index));
9906 output_init_element (loc, value.value, value.original_type,
9907 strict_string, elttype,
9908 constructor_index, true, implicit,
9909 braced_init_obstack);
9910 RESTORE_SPELLING_DEPTH (constructor_depth);
9911 }
9912
9913 constructor_index
9914 = size_binop_loc (input_location, PLUS_EXPR,
9915 constructor_index, bitsize_one_node);
9916
9917 if (!value.value)
9918 /* If we are doing the bookkeeping for an element that was
9919 directly output as a constructor, we must update
9920 constructor_unfilled_index. */
9921 constructor_unfilled_index = constructor_index;
9922 }
9923 else if (VECTOR_TYPE_P (constructor_type))
9924 {
9925 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9926
9927 /* Do a basic check of initializer size. Note that vectors
9928 always have a fixed size derived from their type. */
9929 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9930 {
9931 pedwarn_init (loc, 0,
9932 "excess elements in vector initializer");
9933 break;
9934 }
9935
9936 /* Now output the actual element. */
9937 if (value.value)
9938 {
9939 if (TREE_CODE (value.value) == VECTOR_CST)
9940 elttype = TYPE_MAIN_VARIANT (constructor_type);
9941 output_init_element (loc, value.value, value.original_type,
9942 strict_string, elttype,
9943 constructor_index, true, implicit,
9944 braced_init_obstack);
9945 }
9946
9947 constructor_index
9948 = size_binop_loc (input_location,
9949 PLUS_EXPR, constructor_index, bitsize_one_node);
9950
9951 if (!value.value)
9952 /* If we are doing the bookkeeping for an element that was
9953 directly output as a constructor, we must update
9954 constructor_unfilled_index. */
9955 constructor_unfilled_index = constructor_index;
9956 }
9957
9958 /* Handle the sole element allowed in a braced initializer
9959 for a scalar variable. */
9960 else if (constructor_type != error_mark_node
9961 && constructor_fields == NULL_TREE)
9962 {
9963 pedwarn_init (loc, 0,
9964 "excess elements in scalar initializer");
9965 break;
9966 }
9967 else
9968 {
9969 if (value.value)
9970 output_init_element (loc, value.value, value.original_type,
9971 strict_string, constructor_type,
9972 NULL_TREE, true, implicit,
9973 braced_init_obstack);
9974 constructor_fields = NULL_TREE;
9975 }
9976
9977 /* Handle range initializers either at this level or anywhere higher
9978 in the designator stack. */
9979 if (constructor_range_stack)
9980 {
9981 struct constructor_range_stack *p, *range_stack;
9982 int finish = 0;
9983
9984 range_stack = constructor_range_stack;
9985 constructor_range_stack = 0;
9986 while (constructor_stack != range_stack->stack)
9987 {
9988 gcc_assert (constructor_stack->implicit);
9989 process_init_element (loc,
9990 pop_init_level (loc, 1,
9991 braced_init_obstack,
9992 last_init_list_comma),
9993 true, braced_init_obstack);
9994 }
9995 for (p = range_stack;
9996 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9997 p = p->prev)
9998 {
9999 gcc_assert (constructor_stack->implicit);
10000 process_init_element (loc,
10001 pop_init_level (loc, 1,
10002 braced_init_obstack,
10003 last_init_list_comma),
10004 true, braced_init_obstack);
10005 }
10006
10007 p->index = size_binop_loc (input_location,
10008 PLUS_EXPR, p->index, bitsize_one_node);
10009 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10010 finish = 1;
10011
10012 while (1)
10013 {
10014 constructor_index = p->index;
10015 constructor_fields = p->fields;
10016 if (finish && p->range_end && p->index == p->range_start)
10017 {
10018 finish = 0;
10019 p->prev = 0;
10020 }
10021 p = p->next;
10022 if (!p)
10023 break;
10024 finish_implicit_inits (loc, braced_init_obstack);
10025 push_init_level (loc, 2, braced_init_obstack);
10026 p->stack = constructor_stack;
10027 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10028 p->index = p->range_start;
10029 }
10030
10031 if (!finish)
10032 constructor_range_stack = range_stack;
10033 continue;
10034 }
10035
10036 break;
10037 }
10038
10039 constructor_range_stack = 0;
10040 }
10041 \f
10042 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10043 (guaranteed to be 'volatile' or null) and ARGS (represented using
10044 an ASM_EXPR node). */
10045 tree
10046 build_asm_stmt (tree cv_qualifier, tree args)
10047 {
10048 if (!ASM_VOLATILE_P (args) && cv_qualifier)
10049 ASM_VOLATILE_P (args) = 1;
10050 return add_stmt (args);
10051 }
10052
10053 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10054 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10055 SIMPLE indicates whether there was anything at all after the
10056 string in the asm expression -- asm("blah") and asm("blah" : )
10057 are subtly different. We use a ASM_EXPR node to represent this. */
10058 tree
10059 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10060 tree clobbers, tree labels, bool simple)
10061 {
10062 tree tail;
10063 tree args;
10064 int i;
10065 const char *constraint;
10066 const char **oconstraints;
10067 bool allows_mem, allows_reg, is_inout;
10068 int ninputs, noutputs;
10069
10070 ninputs = list_length (inputs);
10071 noutputs = list_length (outputs);
10072 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10073
10074 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10075
10076 /* Remove output conversions that change the type but not the mode. */
10077 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10078 {
10079 tree output = TREE_VALUE (tail);
10080
10081 output = c_fully_fold (output, false, NULL, true);
10082
10083 /* ??? Really, this should not be here. Users should be using a
10084 proper lvalue, dammit. But there's a long history of using casts
10085 in the output operands. In cases like longlong.h, this becomes a
10086 primitive form of typechecking -- if the cast can be removed, then
10087 the output operand had a type of the proper width; otherwise we'll
10088 get an error. Gross, but ... */
10089 STRIP_NOPS (output);
10090
10091 if (!lvalue_or_else (loc, output, lv_asm))
10092 output = error_mark_node;
10093
10094 if (output != error_mark_node
10095 && (TREE_READONLY (output)
10096 || TYPE_READONLY (TREE_TYPE (output))
10097 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10098 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10099 readonly_error (loc, output, lv_asm);
10100
10101 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10102 oconstraints[i] = constraint;
10103
10104 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10105 &allows_mem, &allows_reg, &is_inout))
10106 {
10107 /* If the operand is going to end up in memory,
10108 mark it addressable. */
10109 if (!allows_reg && !c_mark_addressable (output))
10110 output = error_mark_node;
10111 if (!(!allows_reg && allows_mem)
10112 && output != error_mark_node
10113 && VOID_TYPE_P (TREE_TYPE (output)))
10114 {
10115 error_at (loc, "invalid use of void expression");
10116 output = error_mark_node;
10117 }
10118 }
10119 else
10120 output = error_mark_node;
10121
10122 TREE_VALUE (tail) = output;
10123 }
10124
10125 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10126 {
10127 tree input;
10128
10129 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10130 input = TREE_VALUE (tail);
10131
10132 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10133 oconstraints, &allows_mem, &allows_reg))
10134 {
10135 /* If the operand is going to end up in memory,
10136 mark it addressable. */
10137 if (!allows_reg && allows_mem)
10138 {
10139 input = c_fully_fold (input, false, NULL, true);
10140
10141 /* Strip the nops as we allow this case. FIXME, this really
10142 should be rejected or made deprecated. */
10143 STRIP_NOPS (input);
10144 if (!c_mark_addressable (input))
10145 input = error_mark_node;
10146 }
10147 else
10148 {
10149 struct c_expr expr;
10150 memset (&expr, 0, sizeof (expr));
10151 expr.value = input;
10152 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10153 input = c_fully_fold (expr.value, false, NULL);
10154
10155 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10156 {
10157 error_at (loc, "invalid use of void expression");
10158 input = error_mark_node;
10159 }
10160 }
10161 }
10162 else
10163 input = error_mark_node;
10164
10165 TREE_VALUE (tail) = input;
10166 }
10167
10168 /* ASMs with labels cannot have outputs. This should have been
10169 enforced by the parser. */
10170 gcc_assert (outputs == NULL || labels == NULL);
10171
10172 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10173
10174 /* asm statements without outputs, including simple ones, are treated
10175 as volatile. */
10176 ASM_INPUT_P (args) = simple;
10177 ASM_VOLATILE_P (args) = (noutputs == 0);
10178
10179 return args;
10180 }
10181 \f
10182 /* Generate a goto statement to LABEL. LOC is the location of the
10183 GOTO. */
10184
10185 tree
10186 c_finish_goto_label (location_t loc, tree label)
10187 {
10188 tree decl = lookup_label_for_goto (loc, label);
10189 if (!decl)
10190 return NULL_TREE;
10191 TREE_USED (decl) = 1;
10192 {
10193 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10194 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10195 SET_EXPR_LOCATION (t, loc);
10196 return add_stmt (t);
10197 }
10198 }
10199
10200 /* Generate a computed goto statement to EXPR. LOC is the location of
10201 the GOTO. */
10202
10203 tree
10204 c_finish_goto_ptr (location_t loc, tree expr)
10205 {
10206 tree t;
10207 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10208 expr = c_fully_fold (expr, false, NULL);
10209 expr = convert (ptr_type_node, expr);
10210 t = build1 (GOTO_EXPR, void_type_node, expr);
10211 SET_EXPR_LOCATION (t, loc);
10212 return add_stmt (t);
10213 }
10214
10215 /* Generate a C `return' statement. RETVAL is the expression for what
10216 to return, or a null pointer for `return;' with no value. LOC is
10217 the location of the return statement, or the location of the expression,
10218 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10219 is the original type of RETVAL. */
10220
10221 tree
10222 c_finish_return (location_t loc, tree retval, tree origtype)
10223 {
10224 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10225 bool no_warning = false;
10226 bool npc = false;
10227
10228 /* Use the expansion point to handle cases such as returning NULL
10229 in a function returning void. */
10230 source_location xloc = expansion_point_location_if_in_system_header (loc);
10231
10232 if (TREE_THIS_VOLATILE (current_function_decl))
10233 warning_at (xloc, 0,
10234 "function declared %<noreturn%> has a %<return%> statement");
10235
10236 if (retval)
10237 {
10238 tree semantic_type = NULL_TREE;
10239 npc = null_pointer_constant_p (retval);
10240 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10241 {
10242 semantic_type = TREE_TYPE (retval);
10243 retval = TREE_OPERAND (retval, 0);
10244 }
10245 retval = c_fully_fold (retval, false, NULL);
10246 if (semantic_type)
10247 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10248 }
10249
10250 if (!retval)
10251 {
10252 current_function_returns_null = 1;
10253 if ((warn_return_type >= 0 || flag_isoc99)
10254 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10255 {
10256 bool warned_here;
10257 if (flag_isoc99)
10258 warned_here = pedwarn
10259 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10260 "%<return%> with no value, in function returning non-void");
10261 else
10262 warned_here = warning_at
10263 (loc, OPT_Wreturn_type,
10264 "%<return%> with no value, in function returning non-void");
10265 no_warning = true;
10266 if (warned_here)
10267 inform (DECL_SOURCE_LOCATION (current_function_decl),
10268 "declared here");
10269 }
10270 }
10271 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10272 {
10273 current_function_returns_null = 1;
10274 bool warned_here;
10275 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10276 warned_here = pedwarn
10277 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10278 "%<return%> with a value, in function returning void");
10279 else
10280 warned_here = pedwarn
10281 (xloc, OPT_Wpedantic, "ISO C forbids "
10282 "%<return%> with expression, in function returning void");
10283 if (warned_here)
10284 inform (DECL_SOURCE_LOCATION (current_function_decl),
10285 "declared here");
10286 }
10287 else
10288 {
10289 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10290 retval, origtype, ic_return,
10291 npc, NULL_TREE, NULL_TREE, 0);
10292 tree res = DECL_RESULT (current_function_decl);
10293 tree inner;
10294 bool save;
10295
10296 current_function_returns_value = 1;
10297 if (t == error_mark_node)
10298 return NULL_TREE;
10299
10300 save = in_late_binary_op;
10301 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10302 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10303 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10304 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10305 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10306 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10307 in_late_binary_op = true;
10308 inner = t = convert (TREE_TYPE (res), t);
10309 in_late_binary_op = save;
10310
10311 /* Strip any conversions, additions, and subtractions, and see if
10312 we are returning the address of a local variable. Warn if so. */
10313 while (1)
10314 {
10315 switch (TREE_CODE (inner))
10316 {
10317 CASE_CONVERT:
10318 case NON_LVALUE_EXPR:
10319 case PLUS_EXPR:
10320 case POINTER_PLUS_EXPR:
10321 inner = TREE_OPERAND (inner, 0);
10322 continue;
10323
10324 case MINUS_EXPR:
10325 /* If the second operand of the MINUS_EXPR has a pointer
10326 type (or is converted from it), this may be valid, so
10327 don't give a warning. */
10328 {
10329 tree op1 = TREE_OPERAND (inner, 1);
10330
10331 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10332 && (CONVERT_EXPR_P (op1)
10333 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10334 op1 = TREE_OPERAND (op1, 0);
10335
10336 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10337 break;
10338
10339 inner = TREE_OPERAND (inner, 0);
10340 continue;
10341 }
10342
10343 case ADDR_EXPR:
10344 inner = TREE_OPERAND (inner, 0);
10345
10346 while (REFERENCE_CLASS_P (inner)
10347 && !INDIRECT_REF_P (inner))
10348 inner = TREE_OPERAND (inner, 0);
10349
10350 if (DECL_P (inner)
10351 && !DECL_EXTERNAL (inner)
10352 && !TREE_STATIC (inner)
10353 && DECL_CONTEXT (inner) == current_function_decl)
10354 {
10355 if (TREE_CODE (inner) == LABEL_DECL)
10356 warning_at (loc, OPT_Wreturn_local_addr,
10357 "function returns address of label");
10358 else
10359 {
10360 warning_at (loc, OPT_Wreturn_local_addr,
10361 "function returns address of local variable");
10362 tree zero = build_zero_cst (TREE_TYPE (res));
10363 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10364 }
10365 }
10366 break;
10367
10368 default:
10369 break;
10370 }
10371
10372 break;
10373 }
10374
10375 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10376 SET_EXPR_LOCATION (retval, loc);
10377
10378 if (warn_sequence_point)
10379 verify_sequence_points (retval);
10380 }
10381
10382 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10383 TREE_NO_WARNING (ret_stmt) |= no_warning;
10384 return add_stmt (ret_stmt);
10385 }
10386 \f
10387 struct c_switch {
10388 /* The SWITCH_EXPR being built. */
10389 tree switch_expr;
10390
10391 /* The original type of the testing expression, i.e. before the
10392 default conversion is applied. */
10393 tree orig_type;
10394
10395 /* A splay-tree mapping the low element of a case range to the high
10396 element, or NULL_TREE if there is no high element. Used to
10397 determine whether or not a new case label duplicates an old case
10398 label. We need a tree, rather than simply a hash table, because
10399 of the GNU case range extension. */
10400 splay_tree cases;
10401
10402 /* The bindings at the point of the switch. This is used for
10403 warnings crossing decls when branching to a case label. */
10404 struct c_spot_bindings *bindings;
10405
10406 /* The next node on the stack. */
10407 struct c_switch *next;
10408
10409 /* Remember whether the controlling expression had boolean type
10410 before integer promotions for the sake of -Wswitch-bool. */
10411 bool bool_cond_p;
10412
10413 /* Remember whether there was a case value that is outside the
10414 range of the ORIG_TYPE. */
10415 bool outside_range_p;
10416 };
10417
10418 /* A stack of the currently active switch statements. The innermost
10419 switch statement is on the top of the stack. There is no need to
10420 mark the stack for garbage collection because it is only active
10421 during the processing of the body of a function, and we never
10422 collect at that point. */
10423
10424 struct c_switch *c_switch_stack;
10425
10426 /* Start a C switch statement, testing expression EXP. Return the new
10427 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10428 SWITCH_COND_LOC is the location of the switch's condition.
10429 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10430
10431 tree
10432 c_start_case (location_t switch_loc,
10433 location_t switch_cond_loc,
10434 tree exp, bool explicit_cast_p)
10435 {
10436 tree orig_type = error_mark_node;
10437 bool bool_cond_p = false;
10438 struct c_switch *cs;
10439
10440 if (exp != error_mark_node)
10441 {
10442 orig_type = TREE_TYPE (exp);
10443
10444 if (!INTEGRAL_TYPE_P (orig_type))
10445 {
10446 if (orig_type != error_mark_node)
10447 {
10448 error_at (switch_cond_loc, "switch quantity not an integer");
10449 orig_type = error_mark_node;
10450 }
10451 exp = integer_zero_node;
10452 }
10453 else
10454 {
10455 tree type = TYPE_MAIN_VARIANT (orig_type);
10456 tree e = exp;
10457
10458 /* Warn if the condition has boolean value. */
10459 while (TREE_CODE (e) == COMPOUND_EXPR)
10460 e = TREE_OPERAND (e, 1);
10461
10462 if ((TREE_CODE (type) == BOOLEAN_TYPE
10463 || truth_value_p (TREE_CODE (e)))
10464 /* Explicit cast to int suppresses this warning. */
10465 && !(TREE_CODE (type) == INTEGER_TYPE
10466 && explicit_cast_p))
10467 bool_cond_p = true;
10468
10469 if (!in_system_header_at (input_location)
10470 && (type == long_integer_type_node
10471 || type == long_unsigned_type_node))
10472 warning_at (switch_cond_loc,
10473 OPT_Wtraditional, "%<long%> switch expression not "
10474 "converted to %<int%> in ISO C");
10475
10476 exp = c_fully_fold (exp, false, NULL);
10477 exp = default_conversion (exp);
10478
10479 if (warn_sequence_point)
10480 verify_sequence_points (exp);
10481 }
10482 }
10483
10484 /* Add this new SWITCH_EXPR to the stack. */
10485 cs = XNEW (struct c_switch);
10486 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10487 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10488 cs->orig_type = orig_type;
10489 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10490 cs->bindings = c_get_switch_bindings ();
10491 cs->bool_cond_p = bool_cond_p;
10492 cs->outside_range_p = false;
10493 cs->next = c_switch_stack;
10494 c_switch_stack = cs;
10495
10496 return add_stmt (cs->switch_expr);
10497 }
10498
10499 /* Process a case label at location LOC. */
10500
10501 tree
10502 do_case (location_t loc, tree low_value, tree high_value)
10503 {
10504 tree label = NULL_TREE;
10505
10506 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10507 {
10508 low_value = c_fully_fold (low_value, false, NULL);
10509 if (TREE_CODE (low_value) == INTEGER_CST)
10510 pedwarn (loc, OPT_Wpedantic,
10511 "case label is not an integer constant expression");
10512 }
10513
10514 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10515 {
10516 high_value = c_fully_fold (high_value, false, NULL);
10517 if (TREE_CODE (high_value) == INTEGER_CST)
10518 pedwarn (input_location, OPT_Wpedantic,
10519 "case label is not an integer constant expression");
10520 }
10521
10522 if (c_switch_stack == NULL)
10523 {
10524 if (low_value)
10525 error_at (loc, "case label not within a switch statement");
10526 else
10527 error_at (loc, "%<default%> label not within a switch statement");
10528 return NULL_TREE;
10529 }
10530
10531 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10532 EXPR_LOCATION (c_switch_stack->switch_expr),
10533 loc))
10534 return NULL_TREE;
10535
10536 label = c_add_case_label (loc, c_switch_stack->cases,
10537 SWITCH_COND (c_switch_stack->switch_expr),
10538 c_switch_stack->orig_type,
10539 low_value, high_value,
10540 &c_switch_stack->outside_range_p);
10541 if (label == error_mark_node)
10542 label = NULL_TREE;
10543 return label;
10544 }
10545
10546 /* Finish the switch statement. TYPE is the original type of the
10547 controlling expression of the switch, or NULL_TREE. */
10548
10549 void
10550 c_finish_case (tree body, tree type)
10551 {
10552 struct c_switch *cs = c_switch_stack;
10553 location_t switch_location;
10554
10555 SWITCH_BODY (cs->switch_expr) = body;
10556
10557 /* Emit warnings as needed. */
10558 switch_location = EXPR_LOCATION (cs->switch_expr);
10559 c_do_switch_warnings (cs->cases, switch_location,
10560 type ? type : TREE_TYPE (cs->switch_expr),
10561 SWITCH_COND (cs->switch_expr),
10562 cs->bool_cond_p, cs->outside_range_p);
10563 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10564 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10565
10566 /* Pop the stack. */
10567 c_switch_stack = cs->next;
10568 splay_tree_delete (cs->cases);
10569 c_release_switch_bindings (cs->bindings);
10570 XDELETE (cs);
10571 }
10572 \f
10573 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10574 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10575 may be null. */
10576
10577 void
10578 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10579 tree else_block)
10580 {
10581 tree stmt;
10582
10583 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10584 SET_EXPR_LOCATION (stmt, if_locus);
10585 add_stmt (stmt);
10586 }
10587
10588 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10589 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10590 is false for DO loops. INCR is the FOR increment expression. BODY is
10591 the statement controlled by the loop. BLAB is the break label. CLAB is
10592 the continue label. Everything is allowed to be NULL. */
10593
10594 void
10595 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10596 tree blab, tree clab, bool cond_is_first)
10597 {
10598 tree entry = NULL, exit = NULL, t;
10599
10600 /* If the condition is zero don't generate a loop construct. */
10601 if (cond && integer_zerop (cond))
10602 {
10603 if (cond_is_first)
10604 {
10605 t = build_and_jump (&blab);
10606 SET_EXPR_LOCATION (t, start_locus);
10607 add_stmt (t);
10608 }
10609 }
10610 else
10611 {
10612 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10613
10614 /* If we have an exit condition, then we build an IF with gotos either
10615 out of the loop, or to the top of it. If there's no exit condition,
10616 then we just build a jump back to the top. */
10617 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10618
10619 if (cond && !integer_nonzerop (cond))
10620 {
10621 /* Canonicalize the loop condition to the end. This means
10622 generating a branch to the loop condition. Reuse the
10623 continue label, if possible. */
10624 if (cond_is_first)
10625 {
10626 if (incr || !clab)
10627 {
10628 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10629 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10630 }
10631 else
10632 t = build1 (GOTO_EXPR, void_type_node, clab);
10633 SET_EXPR_LOCATION (t, start_locus);
10634 add_stmt (t);
10635 }
10636
10637 t = build_and_jump (&blab);
10638 if (cond_is_first)
10639 exit = fold_build3_loc (start_locus,
10640 COND_EXPR, void_type_node, cond, exit, t);
10641 else
10642 exit = fold_build3_loc (input_location,
10643 COND_EXPR, void_type_node, cond, exit, t);
10644 }
10645 else
10646 {
10647 /* For the backward-goto's location of an unconditional loop
10648 use the beginning of the body, or, if there is none, the
10649 top of the loop. */
10650 location_t loc = EXPR_LOCATION (expr_first (body));
10651 if (loc == UNKNOWN_LOCATION)
10652 loc = start_locus;
10653 SET_EXPR_LOCATION (exit, loc);
10654 }
10655
10656 add_stmt (top);
10657 }
10658
10659 if (body)
10660 add_stmt (body);
10661 if (clab)
10662 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10663 if (incr)
10664 add_stmt (incr);
10665 if (entry)
10666 add_stmt (entry);
10667 if (exit)
10668 add_stmt (exit);
10669 if (blab)
10670 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10671 }
10672
10673 tree
10674 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10675 {
10676 bool skip;
10677 tree label = *label_p;
10678
10679 /* In switch statements break is sometimes stylistically used after
10680 a return statement. This can lead to spurious warnings about
10681 control reaching the end of a non-void function when it is
10682 inlined. Note that we are calling block_may_fallthru with
10683 language specific tree nodes; this works because
10684 block_may_fallthru returns true when given something it does not
10685 understand. */
10686 skip = !block_may_fallthru (cur_stmt_list);
10687
10688 if (!label)
10689 {
10690 if (!skip)
10691 *label_p = label = create_artificial_label (loc);
10692 }
10693 else if (TREE_CODE (label) == LABEL_DECL)
10694 ;
10695 else switch (TREE_INT_CST_LOW (label))
10696 {
10697 case 0:
10698 if (is_break)
10699 error_at (loc, "break statement not within loop or switch");
10700 else
10701 error_at (loc, "continue statement not within a loop");
10702 return NULL_TREE;
10703
10704 case 1:
10705 gcc_assert (is_break);
10706 error_at (loc, "break statement used with OpenMP for loop");
10707 return NULL_TREE;
10708
10709 case 2:
10710 if (is_break)
10711 error ("break statement within %<#pragma simd%> loop body");
10712 else
10713 error ("continue statement within %<#pragma simd%> loop body");
10714 return NULL_TREE;
10715
10716 default:
10717 gcc_unreachable ();
10718 }
10719
10720 if (skip)
10721 return NULL_TREE;
10722
10723 if (!is_break)
10724 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10725
10726 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10727 }
10728
10729 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10730
10731 static void
10732 emit_side_effect_warnings (location_t loc, tree expr)
10733 {
10734 if (expr == error_mark_node)
10735 ;
10736 else if (!TREE_SIDE_EFFECTS (expr))
10737 {
10738 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
10739 warning_at (loc, OPT_Wunused_value, "statement with no effect");
10740 }
10741 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10742 {
10743 tree r = expr;
10744 location_t cloc = loc;
10745 while (TREE_CODE (r) == COMPOUND_EXPR)
10746 {
10747 if (EXPR_HAS_LOCATION (r))
10748 cloc = EXPR_LOCATION (r);
10749 r = TREE_OPERAND (r, 1);
10750 }
10751 if (!TREE_SIDE_EFFECTS (r)
10752 && !VOID_TYPE_P (TREE_TYPE (r))
10753 && !CONVERT_EXPR_P (r)
10754 && !TREE_NO_WARNING (r)
10755 && !TREE_NO_WARNING (expr))
10756 warning_at (cloc, OPT_Wunused_value,
10757 "right-hand operand of comma expression has no effect");
10758 }
10759 else
10760 warn_if_unused_value (expr, loc);
10761 }
10762
10763 /* Process an expression as if it were a complete statement. Emit
10764 diagnostics, but do not call ADD_STMT. LOC is the location of the
10765 statement. */
10766
10767 tree
10768 c_process_expr_stmt (location_t loc, tree expr)
10769 {
10770 tree exprv;
10771
10772 if (!expr)
10773 return NULL_TREE;
10774
10775 expr = c_fully_fold (expr, false, NULL);
10776
10777 if (warn_sequence_point)
10778 verify_sequence_points (expr);
10779
10780 if (TREE_TYPE (expr) != error_mark_node
10781 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10782 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
10783 error_at (loc, "expression statement has incomplete type");
10784
10785 /* If we're not processing a statement expression, warn about unused values.
10786 Warnings for statement expressions will be emitted later, once we figure
10787 out which is the result. */
10788 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10789 && warn_unused_value)
10790 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
10791
10792 exprv = expr;
10793 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10794 exprv = TREE_OPERAND (exprv, 1);
10795 while (CONVERT_EXPR_P (exprv))
10796 exprv = TREE_OPERAND (exprv, 0);
10797 if (DECL_P (exprv)
10798 || handled_component_p (exprv)
10799 || TREE_CODE (exprv) == ADDR_EXPR)
10800 mark_exp_read (exprv);
10801
10802 /* If the expression is not of a type to which we cannot assign a line
10803 number, wrap the thing in a no-op NOP_EXPR. */
10804 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
10805 {
10806 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10807 SET_EXPR_LOCATION (expr, loc);
10808 }
10809
10810 return expr;
10811 }
10812
10813 /* Emit an expression as a statement. LOC is the location of the
10814 expression. */
10815
10816 tree
10817 c_finish_expr_stmt (location_t loc, tree expr)
10818 {
10819 if (expr)
10820 return add_stmt (c_process_expr_stmt (loc, expr));
10821 else
10822 return NULL;
10823 }
10824
10825 /* Do the opposite and emit a statement as an expression. To begin,
10826 create a new binding level and return it. */
10827
10828 tree
10829 c_begin_stmt_expr (void)
10830 {
10831 tree ret;
10832
10833 /* We must force a BLOCK for this level so that, if it is not expanded
10834 later, there is a way to turn off the entire subtree of blocks that
10835 are contained in it. */
10836 keep_next_level ();
10837 ret = c_begin_compound_stmt (true);
10838
10839 c_bindings_start_stmt_expr (c_switch_stack == NULL
10840 ? NULL
10841 : c_switch_stack->bindings);
10842
10843 /* Mark the current statement list as belonging to a statement list. */
10844 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10845
10846 return ret;
10847 }
10848
10849 /* LOC is the location of the compound statement to which this body
10850 belongs. */
10851
10852 tree
10853 c_finish_stmt_expr (location_t loc, tree body)
10854 {
10855 tree last, type, tmp, val;
10856 tree *last_p;
10857
10858 body = c_end_compound_stmt (loc, body, true);
10859
10860 c_bindings_end_stmt_expr (c_switch_stack == NULL
10861 ? NULL
10862 : c_switch_stack->bindings);
10863
10864 /* Locate the last statement in BODY. See c_end_compound_stmt
10865 about always returning a BIND_EXPR. */
10866 last_p = &BIND_EXPR_BODY (body);
10867 last = BIND_EXPR_BODY (body);
10868
10869 continue_searching:
10870 if (TREE_CODE (last) == STATEMENT_LIST)
10871 {
10872 tree_stmt_iterator l = tsi_last (last);
10873
10874 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
10875 tsi_prev (&l);
10876
10877 /* This can happen with degenerate cases like ({ }). No value. */
10878 if (tsi_end_p (l))
10879 return body;
10880
10881 /* If we're supposed to generate side effects warnings, process
10882 all of the statements except the last. */
10883 if (warn_unused_value)
10884 {
10885 for (tree_stmt_iterator i = tsi_start (last);
10886 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
10887 {
10888 location_t tloc;
10889 tree t = tsi_stmt (i);
10890
10891 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10892 emit_side_effect_warnings (tloc, t);
10893 }
10894 }
10895 last_p = tsi_stmt_ptr (l);
10896 last = *last_p;
10897 }
10898
10899 /* If the end of the list is exception related, then the list was split
10900 by a call to push_cleanup. Continue searching. */
10901 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10902 || TREE_CODE (last) == TRY_CATCH_EXPR)
10903 {
10904 last_p = &TREE_OPERAND (last, 0);
10905 last = *last_p;
10906 goto continue_searching;
10907 }
10908
10909 if (last == error_mark_node)
10910 return last;
10911
10912 /* In the case that the BIND_EXPR is not necessary, return the
10913 expression out from inside it. */
10914 if ((last == BIND_EXPR_BODY (body)
10915 /* Skip nested debug stmts. */
10916 || last == expr_first (BIND_EXPR_BODY (body)))
10917 && BIND_EXPR_VARS (body) == NULL)
10918 {
10919 /* Even if this looks constant, do not allow it in a constant
10920 expression. */
10921 last = c_wrap_maybe_const (last, true);
10922 /* Do not warn if the return value of a statement expression is
10923 unused. */
10924 TREE_NO_WARNING (last) = 1;
10925 return last;
10926 }
10927
10928 /* Extract the type of said expression. */
10929 type = TREE_TYPE (last);
10930
10931 /* If we're not returning a value at all, then the BIND_EXPR that
10932 we already have is a fine expression to return. */
10933 if (!type || VOID_TYPE_P (type))
10934 return body;
10935
10936 /* Now that we've located the expression containing the value, it seems
10937 silly to make voidify_wrapper_expr repeat the process. Create a
10938 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10939 tmp = create_tmp_var_raw (type);
10940
10941 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10942 tree_expr_nonnegative_p giving up immediately. */
10943 val = last;
10944 if (TREE_CODE (val) == NOP_EXPR
10945 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10946 val = TREE_OPERAND (val, 0);
10947
10948 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10949 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10950
10951 {
10952 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10953 SET_EXPR_LOCATION (t, loc);
10954 return t;
10955 }
10956 }
10957 \f
10958 /* Begin and end compound statements. This is as simple as pushing
10959 and popping new statement lists from the tree. */
10960
10961 tree
10962 c_begin_compound_stmt (bool do_scope)
10963 {
10964 tree stmt = push_stmt_list ();
10965 if (do_scope)
10966 push_scope ();
10967 return stmt;
10968 }
10969
10970 /* End a compound statement. STMT is the statement. LOC is the
10971 location of the compound statement-- this is usually the location
10972 of the opening brace. */
10973
10974 tree
10975 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10976 {
10977 tree block = NULL;
10978
10979 if (do_scope)
10980 {
10981 if (c_dialect_objc ())
10982 objc_clear_super_receiver ();
10983 block = pop_scope ();
10984 }
10985
10986 stmt = pop_stmt_list (stmt);
10987 stmt = c_build_bind_expr (loc, block, stmt);
10988
10989 /* If this compound statement is nested immediately inside a statement
10990 expression, then force a BIND_EXPR to be created. Otherwise we'll
10991 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10992 STATEMENT_LISTs merge, and thus we can lose track of what statement
10993 was really last. */
10994 if (building_stmt_list_p ()
10995 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10996 && TREE_CODE (stmt) != BIND_EXPR)
10997 {
10998 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10999 TREE_SIDE_EFFECTS (stmt) = 1;
11000 SET_EXPR_LOCATION (stmt, loc);
11001 }
11002
11003 return stmt;
11004 }
11005
11006 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11007 when the current scope is exited. EH_ONLY is true when this is not
11008 meant to apply to normal control flow transfer. */
11009
11010 void
11011 push_cleanup (tree decl, tree cleanup, bool eh_only)
11012 {
11013 enum tree_code code;
11014 tree stmt, list;
11015 bool stmt_expr;
11016
11017 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11018 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11019 add_stmt (stmt);
11020 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11021 list = push_stmt_list ();
11022 TREE_OPERAND (stmt, 0) = list;
11023 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11024 }
11025 \f
11026 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11027 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11028
11029 static tree
11030 build_vec_cmp (tree_code code, tree type,
11031 tree arg0, tree arg1)
11032 {
11033 tree zero_vec = build_zero_cst (type);
11034 tree minus_one_vec = build_minus_one_cst (type);
11035 tree cmp_type = build_same_sized_truth_vector_type (type);
11036 tree cmp = build2 (code, cmp_type, arg0, arg1);
11037 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11038 }
11039
11040 /* Subclass of range_label for labelling the type of EXPR when reporting
11041 a type mismatch between EXPR and OTHER_EXPR.
11042 Either or both of EXPR and OTHER_EXPR could be NULL. */
11043
11044 class maybe_range_label_for_tree_type_mismatch : public range_label
11045 {
11046 public:
11047 maybe_range_label_for_tree_type_mismatch (tree expr, tree other_expr)
11048 : m_expr (expr), m_other_expr (other_expr)
11049 {
11050 }
11051
11052 label_text get_text () const FINAL OVERRIDE
11053 {
11054 if (m_expr == NULL_TREE
11055 || !EXPR_P (m_expr))
11056 return label_text (NULL, false);
11057 tree expr_type = TREE_TYPE (m_expr);
11058
11059 tree other_type = NULL_TREE;
11060 if (m_other_expr && EXPR_P (m_other_expr))
11061 other_type = TREE_TYPE (m_other_expr);
11062
11063 range_label_for_type_mismatch inner (expr_type, other_type);
11064 return inner.get_text ();
11065 }
11066
11067 private:
11068 tree m_expr;
11069 tree m_other_expr;
11070 };
11071
11072 /* Build a binary-operation expression without default conversions.
11073 CODE is the kind of expression to build.
11074 LOCATION is the operator's location.
11075 This function differs from `build' in several ways:
11076 the data type of the result is computed and recorded in it,
11077 warnings are generated if arg data types are invalid,
11078 special handling for addition and subtraction of pointers is known,
11079 and some optimization is done (operations on narrow ints
11080 are done in the narrower type when that gives the same result).
11081 Constant folding is also done before the result is returned.
11082
11083 Note that the operands will never have enumeral types, or function
11084 or array types, because either they will have the default conversions
11085 performed or they have both just been converted to some other type in which
11086 the arithmetic is to be done. */
11087
11088 tree
11089 build_binary_op (location_t location, enum tree_code code,
11090 tree orig_op0, tree orig_op1, bool convert_p)
11091 {
11092 tree type0, type1, orig_type0, orig_type1;
11093 tree eptype;
11094 enum tree_code code0, code1;
11095 tree op0, op1;
11096 tree ret = error_mark_node;
11097 const char *invalid_op_diag;
11098 bool op0_int_operands, op1_int_operands;
11099 bool int_const, int_const_or_overflow, int_operands;
11100
11101 /* Expression code to give to the expression when it is built.
11102 Normally this is CODE, which is what the caller asked for,
11103 but in some special cases we change it. */
11104 enum tree_code resultcode = code;
11105
11106 /* Data type in which the computation is to be performed.
11107 In the simplest cases this is the common type of the arguments. */
11108 tree result_type = NULL;
11109
11110 /* When the computation is in excess precision, the type of the
11111 final EXCESS_PRECISION_EXPR. */
11112 tree semantic_result_type = NULL;
11113
11114 /* Nonzero means operands have already been type-converted
11115 in whatever way is necessary.
11116 Zero means they need to be converted to RESULT_TYPE. */
11117 int converted = 0;
11118
11119 /* Nonzero means create the expression with this type, rather than
11120 RESULT_TYPE. */
11121 tree build_type = NULL_TREE;
11122
11123 /* Nonzero means after finally constructing the expression
11124 convert it to this type. */
11125 tree final_type = NULL_TREE;
11126
11127 /* Nonzero if this is an operation like MIN or MAX which can
11128 safely be computed in short if both args are promoted shorts.
11129 Also implies COMMON.
11130 -1 indicates a bitwise operation; this makes a difference
11131 in the exact conditions for when it is safe to do the operation
11132 in a narrower mode. */
11133 int shorten = 0;
11134
11135 /* Nonzero if this is a comparison operation;
11136 if both args are promoted shorts, compare the original shorts.
11137 Also implies COMMON. */
11138 int short_compare = 0;
11139
11140 /* Nonzero if this is a right-shift operation, which can be computed on the
11141 original short and then promoted if the operand is a promoted short. */
11142 int short_shift = 0;
11143
11144 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11145 int common = 0;
11146
11147 /* True means types are compatible as far as ObjC is concerned. */
11148 bool objc_ok;
11149
11150 /* True means this is an arithmetic operation that may need excess
11151 precision. */
11152 bool may_need_excess_precision;
11153
11154 /* True means this is a boolean operation that converts both its
11155 operands to truth-values. */
11156 bool boolean_op = false;
11157
11158 /* Remember whether we're doing / or %. */
11159 bool doing_div_or_mod = false;
11160
11161 /* Remember whether we're doing << or >>. */
11162 bool doing_shift = false;
11163
11164 /* Tree holding instrumentation expression. */
11165 tree instrument_expr = NULL;
11166
11167 if (location == UNKNOWN_LOCATION)
11168 location = input_location;
11169
11170 op0 = orig_op0;
11171 op1 = orig_op1;
11172
11173 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11174 if (op0_int_operands)
11175 op0 = remove_c_maybe_const_expr (op0);
11176 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11177 if (op1_int_operands)
11178 op1 = remove_c_maybe_const_expr (op1);
11179 int_operands = (op0_int_operands && op1_int_operands);
11180 if (int_operands)
11181 {
11182 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11183 && TREE_CODE (orig_op1) == INTEGER_CST);
11184 int_const = (int_const_or_overflow
11185 && !TREE_OVERFLOW (orig_op0)
11186 && !TREE_OVERFLOW (orig_op1));
11187 }
11188 else
11189 int_const = int_const_or_overflow = false;
11190
11191 /* Do not apply default conversion in mixed vector/scalar expression. */
11192 if (convert_p
11193 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11194 {
11195 op0 = default_conversion (op0);
11196 op1 = default_conversion (op1);
11197 }
11198
11199 orig_type0 = type0 = TREE_TYPE (op0);
11200
11201 orig_type1 = type1 = TREE_TYPE (op1);
11202
11203 /* The expression codes of the data types of the arguments tell us
11204 whether the arguments are integers, floating, pointers, etc. */
11205 code0 = TREE_CODE (type0);
11206 code1 = TREE_CODE (type1);
11207
11208 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11209 STRIP_TYPE_NOPS (op0);
11210 STRIP_TYPE_NOPS (op1);
11211
11212 /* If an error was already reported for one of the arguments,
11213 avoid reporting another error. */
11214
11215 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11216 return error_mark_node;
11217
11218 if (code0 == POINTER_TYPE
11219 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11220 return error_mark_node;
11221
11222 if (code1 == POINTER_TYPE
11223 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11224 return error_mark_node;
11225
11226 if ((invalid_op_diag
11227 = targetm.invalid_binary_op (code, type0, type1)))
11228 {
11229 error_at (location, invalid_op_diag);
11230 return error_mark_node;
11231 }
11232
11233 switch (code)
11234 {
11235 case PLUS_EXPR:
11236 case MINUS_EXPR:
11237 case MULT_EXPR:
11238 case TRUNC_DIV_EXPR:
11239 case CEIL_DIV_EXPR:
11240 case FLOOR_DIV_EXPR:
11241 case ROUND_DIV_EXPR:
11242 case EXACT_DIV_EXPR:
11243 may_need_excess_precision = true;
11244 break;
11245 default:
11246 may_need_excess_precision = false;
11247 break;
11248 }
11249 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11250 {
11251 op0 = TREE_OPERAND (op0, 0);
11252 type0 = TREE_TYPE (op0);
11253 }
11254 else if (may_need_excess_precision
11255 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11256 {
11257 type0 = eptype;
11258 op0 = convert (eptype, op0);
11259 }
11260 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11261 {
11262 op1 = TREE_OPERAND (op1, 0);
11263 type1 = TREE_TYPE (op1);
11264 }
11265 else if (may_need_excess_precision
11266 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11267 {
11268 type1 = eptype;
11269 op1 = convert (eptype, op1);
11270 }
11271
11272 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11273
11274 /* In case when one of the operands of the binary operation is
11275 a vector and another is a scalar -- convert scalar to vector. */
11276 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11277 {
11278 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11279 true);
11280
11281 switch (convert_flag)
11282 {
11283 case stv_error:
11284 return error_mark_node;
11285 case stv_firstarg:
11286 {
11287 bool maybe_const = true;
11288 tree sc;
11289 sc = c_fully_fold (op0, false, &maybe_const);
11290 sc = save_expr (sc);
11291 sc = convert (TREE_TYPE (type1), sc);
11292 op0 = build_vector_from_val (type1, sc);
11293 if (!maybe_const)
11294 op0 = c_wrap_maybe_const (op0, true);
11295 orig_type0 = type0 = TREE_TYPE (op0);
11296 code0 = TREE_CODE (type0);
11297 converted = 1;
11298 break;
11299 }
11300 case stv_secondarg:
11301 {
11302 bool maybe_const = true;
11303 tree sc;
11304 sc = c_fully_fold (op1, false, &maybe_const);
11305 sc = save_expr (sc);
11306 sc = convert (TREE_TYPE (type0), sc);
11307 op1 = build_vector_from_val (type0, sc);
11308 if (!maybe_const)
11309 op1 = c_wrap_maybe_const (op1, true);
11310 orig_type1 = type1 = TREE_TYPE (op1);
11311 code1 = TREE_CODE (type1);
11312 converted = 1;
11313 break;
11314 }
11315 default:
11316 break;
11317 }
11318 }
11319
11320 switch (code)
11321 {
11322 case PLUS_EXPR:
11323 /* Handle the pointer + int case. */
11324 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11325 {
11326 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11327 goto return_build_binary_op;
11328 }
11329 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11330 {
11331 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11332 goto return_build_binary_op;
11333 }
11334 else
11335 common = 1;
11336 break;
11337
11338 case MINUS_EXPR:
11339 /* Subtraction of two similar pointers.
11340 We must subtract them as integers, then divide by object size. */
11341 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11342 && comp_target_types (location, type0, type1))
11343 {
11344 ret = pointer_diff (location, op0, op1, &instrument_expr);
11345 goto return_build_binary_op;
11346 }
11347 /* Handle pointer minus int. Just like pointer plus int. */
11348 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11349 {
11350 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11351 goto return_build_binary_op;
11352 }
11353 else
11354 common = 1;
11355 break;
11356
11357 case MULT_EXPR:
11358 common = 1;
11359 break;
11360
11361 case TRUNC_DIV_EXPR:
11362 case CEIL_DIV_EXPR:
11363 case FLOOR_DIV_EXPR:
11364 case ROUND_DIV_EXPR:
11365 case EXACT_DIV_EXPR:
11366 doing_div_or_mod = true;
11367 warn_for_div_by_zero (location, op1);
11368
11369 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11370 || code0 == FIXED_POINT_TYPE
11371 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11372 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11373 || code1 == FIXED_POINT_TYPE
11374 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11375 {
11376 enum tree_code tcode0 = code0, tcode1 = code1;
11377
11378 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11379 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11380 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11381 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11382
11383 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11384 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11385 resultcode = RDIV_EXPR;
11386 else
11387 /* Although it would be tempting to shorten always here, that
11388 loses on some targets, since the modulo instruction is
11389 undefined if the quotient can't be represented in the
11390 computation mode. We shorten only if unsigned or if
11391 dividing by something we know != -1. */
11392 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11393 || (TREE_CODE (op1) == INTEGER_CST
11394 && !integer_all_onesp (op1)));
11395 common = 1;
11396 }
11397 break;
11398
11399 case BIT_AND_EXPR:
11400 case BIT_IOR_EXPR:
11401 case BIT_XOR_EXPR:
11402 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11403 shorten = -1;
11404 /* Allow vector types which are not floating point types. */
11405 else if (code0 == VECTOR_TYPE
11406 && code1 == VECTOR_TYPE
11407 && !VECTOR_FLOAT_TYPE_P (type0)
11408 && !VECTOR_FLOAT_TYPE_P (type1))
11409 common = 1;
11410 break;
11411
11412 case TRUNC_MOD_EXPR:
11413 case FLOOR_MOD_EXPR:
11414 doing_div_or_mod = true;
11415 warn_for_div_by_zero (location, op1);
11416
11417 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11418 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11419 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11420 common = 1;
11421 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11422 {
11423 /* Although it would be tempting to shorten always here, that loses
11424 on some targets, since the modulo instruction is undefined if the
11425 quotient can't be represented in the computation mode. We shorten
11426 only if unsigned or if dividing by something we know != -1. */
11427 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11428 || (TREE_CODE (op1) == INTEGER_CST
11429 && !integer_all_onesp (op1)));
11430 common = 1;
11431 }
11432 break;
11433
11434 case TRUTH_ANDIF_EXPR:
11435 case TRUTH_ORIF_EXPR:
11436 case TRUTH_AND_EXPR:
11437 case TRUTH_OR_EXPR:
11438 case TRUTH_XOR_EXPR:
11439 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11440 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11441 || code0 == FIXED_POINT_TYPE)
11442 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11443 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11444 || code1 == FIXED_POINT_TYPE))
11445 {
11446 /* Result of these operations is always an int,
11447 but that does not mean the operands should be
11448 converted to ints! */
11449 result_type = integer_type_node;
11450 if (op0_int_operands)
11451 {
11452 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11453 op0 = remove_c_maybe_const_expr (op0);
11454 }
11455 else
11456 op0 = c_objc_common_truthvalue_conversion (location, op0);
11457 if (op1_int_operands)
11458 {
11459 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11460 op1 = remove_c_maybe_const_expr (op1);
11461 }
11462 else
11463 op1 = c_objc_common_truthvalue_conversion (location, op1);
11464 converted = 1;
11465 boolean_op = true;
11466 }
11467 if (code == TRUTH_ANDIF_EXPR)
11468 {
11469 int_const_or_overflow = (int_operands
11470 && TREE_CODE (orig_op0) == INTEGER_CST
11471 && (op0 == truthvalue_false_node
11472 || TREE_CODE (orig_op1) == INTEGER_CST));
11473 int_const = (int_const_or_overflow
11474 && !TREE_OVERFLOW (orig_op0)
11475 && (op0 == truthvalue_false_node
11476 || !TREE_OVERFLOW (orig_op1)));
11477 }
11478 else if (code == TRUTH_ORIF_EXPR)
11479 {
11480 int_const_or_overflow = (int_operands
11481 && TREE_CODE (orig_op0) == INTEGER_CST
11482 && (op0 == truthvalue_true_node
11483 || TREE_CODE (orig_op1) == INTEGER_CST));
11484 int_const = (int_const_or_overflow
11485 && !TREE_OVERFLOW (orig_op0)
11486 && (op0 == truthvalue_true_node
11487 || !TREE_OVERFLOW (orig_op1)));
11488 }
11489 break;
11490
11491 /* Shift operations: result has same type as first operand;
11492 always convert second operand to int.
11493 Also set SHORT_SHIFT if shifting rightward. */
11494
11495 case RSHIFT_EXPR:
11496 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11497 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11498 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11499 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11500 TYPE_VECTOR_SUBPARTS (type1)))
11501 {
11502 result_type = type0;
11503 converted = 1;
11504 }
11505 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11506 || (code0 == VECTOR_TYPE
11507 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11508 && code1 == INTEGER_TYPE)
11509 {
11510 doing_shift = true;
11511 if (TREE_CODE (op1) == INTEGER_CST)
11512 {
11513 if (tree_int_cst_sgn (op1) < 0)
11514 {
11515 int_const = false;
11516 if (c_inhibit_evaluation_warnings == 0)
11517 warning_at (location, OPT_Wshift_count_negative,
11518 "right shift count is negative");
11519 }
11520 else if (code0 == VECTOR_TYPE)
11521 {
11522 if (compare_tree_int (op1,
11523 TYPE_PRECISION (TREE_TYPE (type0)))
11524 >= 0)
11525 {
11526 int_const = false;
11527 if (c_inhibit_evaluation_warnings == 0)
11528 warning_at (location, OPT_Wshift_count_overflow,
11529 "right shift count >= width of vector element");
11530 }
11531 }
11532 else
11533 {
11534 if (!integer_zerop (op1))
11535 short_shift = 1;
11536
11537 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11538 {
11539 int_const = false;
11540 if (c_inhibit_evaluation_warnings == 0)
11541 warning_at (location, OPT_Wshift_count_overflow,
11542 "right shift count >= width of type");
11543 }
11544 }
11545 }
11546
11547 /* Use the type of the value to be shifted. */
11548 result_type = type0;
11549 /* Avoid converting op1 to result_type later. */
11550 converted = 1;
11551 }
11552 break;
11553
11554 case LSHIFT_EXPR:
11555 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11556 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11557 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11558 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11559 TYPE_VECTOR_SUBPARTS (type1)))
11560 {
11561 result_type = type0;
11562 converted = 1;
11563 }
11564 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11565 || (code0 == VECTOR_TYPE
11566 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11567 && code1 == INTEGER_TYPE)
11568 {
11569 doing_shift = true;
11570 if (TREE_CODE (op0) == INTEGER_CST
11571 && tree_int_cst_sgn (op0) < 0)
11572 {
11573 /* Don't reject a left shift of a negative value in a context
11574 where a constant expression is needed in C90. */
11575 if (flag_isoc99)
11576 int_const = false;
11577 if (c_inhibit_evaluation_warnings == 0)
11578 warning_at (location, OPT_Wshift_negative_value,
11579 "left shift of negative value");
11580 }
11581 if (TREE_CODE (op1) == INTEGER_CST)
11582 {
11583 if (tree_int_cst_sgn (op1) < 0)
11584 {
11585 int_const = false;
11586 if (c_inhibit_evaluation_warnings == 0)
11587 warning_at (location, OPT_Wshift_count_negative,
11588 "left shift count is negative");
11589 }
11590 else if (code0 == VECTOR_TYPE)
11591 {
11592 if (compare_tree_int (op1,
11593 TYPE_PRECISION (TREE_TYPE (type0)))
11594 >= 0)
11595 {
11596 int_const = false;
11597 if (c_inhibit_evaluation_warnings == 0)
11598 warning_at (location, OPT_Wshift_count_overflow,
11599 "left shift count >= width of vector element");
11600 }
11601 }
11602 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11603 {
11604 int_const = false;
11605 if (c_inhibit_evaluation_warnings == 0)
11606 warning_at (location, OPT_Wshift_count_overflow,
11607 "left shift count >= width of type");
11608 }
11609 else if (TREE_CODE (op0) == INTEGER_CST
11610 && maybe_warn_shift_overflow (location, op0, op1)
11611 && flag_isoc99)
11612 int_const = false;
11613 }
11614
11615 /* Use the type of the value to be shifted. */
11616 result_type = type0;
11617 /* Avoid converting op1 to result_type later. */
11618 converted = 1;
11619 }
11620 break;
11621
11622 case EQ_EXPR:
11623 case NE_EXPR:
11624 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11625 {
11626 tree intt;
11627 if (!vector_types_compatible_elements_p (type0, type1))
11628 {
11629 error_at (location, "comparing vectors with different "
11630 "element types");
11631 return error_mark_node;
11632 }
11633
11634 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11635 TYPE_VECTOR_SUBPARTS (type1)))
11636 {
11637 error_at (location, "comparing vectors with different "
11638 "number of elements");
11639 return error_mark_node;
11640 }
11641
11642 /* It's not precisely specified how the usual arithmetic
11643 conversions apply to the vector types. Here, we use
11644 the unsigned type if one of the operands is signed and
11645 the other one is unsigned. */
11646 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11647 {
11648 if (!TYPE_UNSIGNED (type0))
11649 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11650 else
11651 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11652 warning_at (location, OPT_Wsign_compare, "comparison between "
11653 "types %qT and %qT", type0, type1);
11654 }
11655
11656 /* Always construct signed integer vector type. */
11657 intt = c_common_type_for_size (GET_MODE_BITSIZE
11658 (SCALAR_TYPE_MODE
11659 (TREE_TYPE (type0))), 0);
11660 if (!intt)
11661 {
11662 error_at (location, "could not find an integer type "
11663 "of the same size as %qT",
11664 TREE_TYPE (type0));
11665 return error_mark_node;
11666 }
11667 result_type = build_opaque_vector_type (intt,
11668 TYPE_VECTOR_SUBPARTS (type0));
11669 converted = 1;
11670 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11671 goto return_build_binary_op;
11672 }
11673 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11674 warning_at (location,
11675 OPT_Wfloat_equal,
11676 "comparing floating point with == or != is unsafe");
11677 /* Result of comparison is always int,
11678 but don't convert the args to int! */
11679 build_type = integer_type_node;
11680 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11681 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11682 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11683 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11684 short_compare = 1;
11685 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11686 {
11687 if (TREE_CODE (op0) == ADDR_EXPR
11688 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11689 && !from_macro_expansion_at (location))
11690 {
11691 if (code == EQ_EXPR)
11692 warning_at (location,
11693 OPT_Waddress,
11694 "the comparison will always evaluate as %<false%> "
11695 "for the address of %qD will never be NULL",
11696 TREE_OPERAND (op0, 0));
11697 else
11698 warning_at (location,
11699 OPT_Waddress,
11700 "the comparison will always evaluate as %<true%> "
11701 "for the address of %qD will never be NULL",
11702 TREE_OPERAND (op0, 0));
11703 }
11704 result_type = type0;
11705 }
11706 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11707 {
11708 if (TREE_CODE (op1) == ADDR_EXPR
11709 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11710 && !from_macro_expansion_at (location))
11711 {
11712 if (code == EQ_EXPR)
11713 warning_at (location,
11714 OPT_Waddress,
11715 "the comparison will always evaluate as %<false%> "
11716 "for the address of %qD will never be NULL",
11717 TREE_OPERAND (op1, 0));
11718 else
11719 warning_at (location,
11720 OPT_Waddress,
11721 "the comparison will always evaluate as %<true%> "
11722 "for the address of %qD will never be NULL",
11723 TREE_OPERAND (op1, 0));
11724 }
11725 result_type = type1;
11726 }
11727 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11728 {
11729 tree tt0 = TREE_TYPE (type0);
11730 tree tt1 = TREE_TYPE (type1);
11731 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11732 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11733 addr_space_t as_common = ADDR_SPACE_GENERIC;
11734
11735 /* Anything compares with void *. void * compares with anything.
11736 Otherwise, the targets must be compatible
11737 and both must be object or both incomplete. */
11738 if (comp_target_types (location, type0, type1))
11739 result_type = common_pointer_type (type0, type1);
11740 else if (!addr_space_superset (as0, as1, &as_common))
11741 {
11742 error_at (location, "comparison of pointers to "
11743 "disjoint address spaces");
11744 return error_mark_node;
11745 }
11746 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11747 {
11748 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
11749 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11750 "comparison of %<void *%> with function pointer");
11751 }
11752 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
11753 {
11754 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
11755 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11756 "comparison of %<void *%> with function pointer");
11757 }
11758 else
11759 /* Avoid warning about the volatile ObjC EH puts on decls. */
11760 if (!objc_ok)
11761 pedwarn (location, 0,
11762 "comparison of distinct pointer types lacks a cast");
11763
11764 if (result_type == NULL_TREE)
11765 {
11766 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11767 result_type = build_pointer_type
11768 (build_qualified_type (void_type_node, qual));
11769 }
11770 }
11771 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11772 {
11773 result_type = type0;
11774 pedwarn (location, 0, "comparison between pointer and integer");
11775 }
11776 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11777 {
11778 result_type = type1;
11779 pedwarn (location, 0, "comparison between pointer and integer");
11780 }
11781 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11782 || truth_value_p (TREE_CODE (orig_op0)))
11783 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11784 || truth_value_p (TREE_CODE (orig_op1))))
11785 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11786 break;
11787
11788 case LE_EXPR:
11789 case GE_EXPR:
11790 case LT_EXPR:
11791 case GT_EXPR:
11792 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11793 {
11794 tree intt;
11795 if (!vector_types_compatible_elements_p (type0, type1))
11796 {
11797 error_at (location, "comparing vectors with different "
11798 "element types");
11799 return error_mark_node;
11800 }
11801
11802 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11803 TYPE_VECTOR_SUBPARTS (type1)))
11804 {
11805 error_at (location, "comparing vectors with different "
11806 "number of elements");
11807 return error_mark_node;
11808 }
11809
11810 /* It's not precisely specified how the usual arithmetic
11811 conversions apply to the vector types. Here, we use
11812 the unsigned type if one of the operands is signed and
11813 the other one is unsigned. */
11814 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11815 {
11816 if (!TYPE_UNSIGNED (type0))
11817 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11818 else
11819 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11820 warning_at (location, OPT_Wsign_compare, "comparison between "
11821 "types %qT and %qT", type0, type1);
11822 }
11823
11824 /* Always construct signed integer vector type. */
11825 intt = c_common_type_for_size (GET_MODE_BITSIZE
11826 (SCALAR_TYPE_MODE
11827 (TREE_TYPE (type0))), 0);
11828 if (!intt)
11829 {
11830 error_at (location, "could not find an integer type "
11831 "of the same size as %qT",
11832 TREE_TYPE (type0));
11833 return error_mark_node;
11834 }
11835 result_type = build_opaque_vector_type (intt,
11836 TYPE_VECTOR_SUBPARTS (type0));
11837 converted = 1;
11838 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11839 goto return_build_binary_op;
11840 }
11841 build_type = integer_type_node;
11842 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11843 || code0 == FIXED_POINT_TYPE)
11844 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11845 || code1 == FIXED_POINT_TYPE))
11846 short_compare = 1;
11847 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11848 {
11849 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11850 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11851 addr_space_t as_common;
11852
11853 if (comp_target_types (location, type0, type1))
11854 {
11855 result_type = common_pointer_type (type0, type1);
11856 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11857 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
11858 pedwarn (location, 0,
11859 "comparison of complete and incomplete pointers");
11860 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
11861 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
11862 "ordered comparisons of pointers to functions");
11863 else if (null_pointer_constant_p (orig_op0)
11864 || null_pointer_constant_p (orig_op1))
11865 warning_at (location, OPT_Wextra,
11866 "ordered comparison of pointer with null pointer");
11867
11868 }
11869 else if (!addr_space_superset (as0, as1, &as_common))
11870 {
11871 error_at (location, "comparison of pointers to "
11872 "disjoint address spaces");
11873 return error_mark_node;
11874 }
11875 else
11876 {
11877 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11878 result_type = build_pointer_type
11879 (build_qualified_type (void_type_node, qual));
11880 pedwarn (location, 0,
11881 "comparison of distinct pointer types lacks a cast");
11882 }
11883 }
11884 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11885 {
11886 result_type = type0;
11887 if (pedantic)
11888 pedwarn (location, OPT_Wpedantic,
11889 "ordered comparison of pointer with integer zero");
11890 else if (extra_warnings)
11891 warning_at (location, OPT_Wextra,
11892 "ordered comparison of pointer with integer zero");
11893 }
11894 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11895 {
11896 result_type = type1;
11897 if (pedantic)
11898 pedwarn (location, OPT_Wpedantic,
11899 "ordered comparison of pointer with integer zero");
11900 else if (extra_warnings)
11901 warning_at (location, OPT_Wextra,
11902 "ordered comparison of pointer with integer zero");
11903 }
11904 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11905 {
11906 result_type = type0;
11907 pedwarn (location, 0, "comparison between pointer and integer");
11908 }
11909 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11910 {
11911 result_type = type1;
11912 pedwarn (location, 0, "comparison between pointer and integer");
11913 }
11914
11915 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
11916 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
11917 {
11918 op0 = save_expr (op0);
11919 op1 = save_expr (op1);
11920
11921 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
11922 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
11923 }
11924
11925 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11926 || truth_value_p (TREE_CODE (orig_op0)))
11927 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11928 || truth_value_p (TREE_CODE (orig_op1))))
11929 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
11930 break;
11931
11932 default:
11933 gcc_unreachable ();
11934 }
11935
11936 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11937 return error_mark_node;
11938
11939 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11940 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
11941 || !vector_types_compatible_elements_p (type0, type1)))
11942 {
11943 gcc_rich_location richloc (location);
11944 maybe_range_label_for_tree_type_mismatch
11945 label_for_op0 (orig_op0, orig_op1),
11946 label_for_op1 (orig_op1, orig_op0);
11947 richloc.maybe_add_expr (orig_op0, &label_for_op0);
11948 richloc.maybe_add_expr (orig_op1, &label_for_op1);
11949 binary_op_error (&richloc, code, type0, type1);
11950 return error_mark_node;
11951 }
11952
11953 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11954 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
11955 &&
11956 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11957 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11958 {
11959 bool first_complex = (code0 == COMPLEX_TYPE);
11960 bool second_complex = (code1 == COMPLEX_TYPE);
11961 int none_complex = (!first_complex && !second_complex);
11962
11963 if (shorten || common || short_compare)
11964 {
11965 result_type = c_common_type (type0, type1);
11966 do_warn_double_promotion (result_type, type0, type1,
11967 "implicit conversion from %qT to %qT "
11968 "to match other operand of binary "
11969 "expression",
11970 location);
11971 if (result_type == error_mark_node)
11972 return error_mark_node;
11973 }
11974
11975 if (first_complex != second_complex
11976 && (code == PLUS_EXPR
11977 || code == MINUS_EXPR
11978 || code == MULT_EXPR
11979 || (code == TRUNC_DIV_EXPR && first_complex))
11980 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11981 && flag_signed_zeros)
11982 {
11983 /* An operation on mixed real/complex operands must be
11984 handled specially, but the language-independent code can
11985 more easily optimize the plain complex arithmetic if
11986 -fno-signed-zeros. */
11987 tree real_type = TREE_TYPE (result_type);
11988 tree real, imag;
11989 if (type0 != orig_type0 || type1 != orig_type1)
11990 {
11991 gcc_assert (may_need_excess_precision && common);
11992 semantic_result_type = c_common_type (orig_type0, orig_type1);
11993 }
11994 if (first_complex)
11995 {
11996 if (TREE_TYPE (op0) != result_type)
11997 op0 = convert_and_check (location, result_type, op0);
11998 if (TREE_TYPE (op1) != real_type)
11999 op1 = convert_and_check (location, real_type, op1);
12000 }
12001 else
12002 {
12003 if (TREE_TYPE (op0) != real_type)
12004 op0 = convert_and_check (location, real_type, op0);
12005 if (TREE_TYPE (op1) != result_type)
12006 op1 = convert_and_check (location, result_type, op1);
12007 }
12008 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12009 return error_mark_node;
12010 if (first_complex)
12011 {
12012 op0 = save_expr (op0);
12013 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12014 op0, true);
12015 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12016 op0, true);
12017 switch (code)
12018 {
12019 case MULT_EXPR:
12020 case TRUNC_DIV_EXPR:
12021 op1 = save_expr (op1);
12022 imag = build2 (resultcode, real_type, imag, op1);
12023 /* Fall through. */
12024 case PLUS_EXPR:
12025 case MINUS_EXPR:
12026 real = build2 (resultcode, real_type, real, op1);
12027 break;
12028 default:
12029 gcc_unreachable();
12030 }
12031 }
12032 else
12033 {
12034 op1 = save_expr (op1);
12035 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12036 op1, true);
12037 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12038 op1, true);
12039 switch (code)
12040 {
12041 case MULT_EXPR:
12042 op0 = save_expr (op0);
12043 imag = build2 (resultcode, real_type, op0, imag);
12044 /* Fall through. */
12045 case PLUS_EXPR:
12046 real = build2 (resultcode, real_type, op0, real);
12047 break;
12048 case MINUS_EXPR:
12049 real = build2 (resultcode, real_type, op0, real);
12050 imag = build1 (NEGATE_EXPR, real_type, imag);
12051 break;
12052 default:
12053 gcc_unreachable();
12054 }
12055 }
12056 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12057 goto return_build_binary_op;
12058 }
12059
12060 /* For certain operations (which identify themselves by shorten != 0)
12061 if both args were extended from the same smaller type,
12062 do the arithmetic in that type and then extend.
12063
12064 shorten !=0 and !=1 indicates a bitwise operation.
12065 For them, this optimization is safe only if
12066 both args are zero-extended or both are sign-extended.
12067 Otherwise, we might change the result.
12068 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12069 but calculated in (unsigned short) it would be (unsigned short)-1. */
12070
12071 if (shorten && none_complex)
12072 {
12073 final_type = result_type;
12074 result_type = shorten_binary_op (result_type, op0, op1,
12075 shorten == -1);
12076 }
12077
12078 /* Shifts can be shortened if shifting right. */
12079
12080 if (short_shift)
12081 {
12082 int unsigned_arg;
12083 tree arg0 = get_narrower (op0, &unsigned_arg);
12084
12085 final_type = result_type;
12086
12087 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12088 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12089
12090 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12091 && tree_int_cst_sgn (op1) > 0
12092 /* We can shorten only if the shift count is less than the
12093 number of bits in the smaller type size. */
12094 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12095 /* We cannot drop an unsigned shift after sign-extension. */
12096 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12097 {
12098 /* Do an unsigned shift if the operand was zero-extended. */
12099 result_type
12100 = c_common_signed_or_unsigned_type (unsigned_arg,
12101 TREE_TYPE (arg0));
12102 /* Convert value-to-be-shifted to that type. */
12103 if (TREE_TYPE (op0) != result_type)
12104 op0 = convert (result_type, op0);
12105 converted = 1;
12106 }
12107 }
12108
12109 /* Comparison operations are shortened too but differently.
12110 They identify themselves by setting short_compare = 1. */
12111
12112 if (short_compare)
12113 {
12114 /* Don't write &op0, etc., because that would prevent op0
12115 from being kept in a register.
12116 Instead, make copies of the our local variables and
12117 pass the copies by reference, then copy them back afterward. */
12118 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12119 enum tree_code xresultcode = resultcode;
12120 tree val
12121 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12122 &xresultcode);
12123
12124 if (val != NULL_TREE)
12125 {
12126 ret = val;
12127 goto return_build_binary_op;
12128 }
12129
12130 op0 = xop0, op1 = xop1;
12131 converted = 1;
12132 resultcode = xresultcode;
12133
12134 if (c_inhibit_evaluation_warnings == 0)
12135 {
12136 bool op0_maybe_const = true;
12137 bool op1_maybe_const = true;
12138 tree orig_op0_folded, orig_op1_folded;
12139
12140 if (in_late_binary_op)
12141 {
12142 orig_op0_folded = orig_op0;
12143 orig_op1_folded = orig_op1;
12144 }
12145 else
12146 {
12147 /* Fold for the sake of possible warnings, as in
12148 build_conditional_expr. This requires the
12149 "original" values to be folded, not just op0 and
12150 op1. */
12151 c_inhibit_evaluation_warnings++;
12152 op0 = c_fully_fold (op0, require_constant_value,
12153 &op0_maybe_const);
12154 op1 = c_fully_fold (op1, require_constant_value,
12155 &op1_maybe_const);
12156 c_inhibit_evaluation_warnings--;
12157 orig_op0_folded = c_fully_fold (orig_op0,
12158 require_constant_value,
12159 NULL);
12160 orig_op1_folded = c_fully_fold (orig_op1,
12161 require_constant_value,
12162 NULL);
12163 }
12164
12165 if (warn_sign_compare)
12166 warn_for_sign_compare (location, orig_op0_folded,
12167 orig_op1_folded, op0, op1,
12168 result_type, resultcode);
12169 if (!in_late_binary_op && !int_operands)
12170 {
12171 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12172 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12173 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12174 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12175 }
12176 }
12177 }
12178 }
12179
12180 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12181 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12182 Then the expression will be built.
12183 It will be given type FINAL_TYPE if that is nonzero;
12184 otherwise, it will be given type RESULT_TYPE. */
12185
12186 if (!result_type)
12187 {
12188 gcc_rich_location richloc (location);
12189 maybe_range_label_for_tree_type_mismatch
12190 label_for_op0 (orig_op0, orig_op1),
12191 label_for_op1 (orig_op1, orig_op0);
12192 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12193 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12194 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12195 return error_mark_node;
12196 }
12197
12198 if (build_type == NULL_TREE)
12199 {
12200 build_type = result_type;
12201 if ((type0 != orig_type0 || type1 != orig_type1)
12202 && !boolean_op)
12203 {
12204 gcc_assert (may_need_excess_precision && common);
12205 semantic_result_type = c_common_type (orig_type0, orig_type1);
12206 }
12207 }
12208
12209 if (!converted)
12210 {
12211 op0 = ep_convert_and_check (location, result_type, op0,
12212 semantic_result_type);
12213 op1 = ep_convert_and_check (location, result_type, op1,
12214 semantic_result_type);
12215
12216 /* This can happen if one operand has a vector type, and the other
12217 has a different type. */
12218 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12219 return error_mark_node;
12220 }
12221
12222 if (sanitize_flags_p ((SANITIZE_SHIFT
12223 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12224 && current_function_decl != NULL_TREE
12225 && (doing_div_or_mod || doing_shift)
12226 && !require_constant_value)
12227 {
12228 /* OP0 and/or OP1 might have side-effects. */
12229 op0 = save_expr (op0);
12230 op1 = save_expr (op1);
12231 op0 = c_fully_fold (op0, false, NULL);
12232 op1 = c_fully_fold (op1, false, NULL);
12233 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12234 | SANITIZE_FLOAT_DIVIDE))))
12235 instrument_expr = ubsan_instrument_division (location, op0, op1);
12236 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12237 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12238 }
12239
12240 /* Treat expressions in initializers specially as they can't trap. */
12241 if (int_const_or_overflow)
12242 ret = (require_constant_value
12243 ? fold_build2_initializer_loc (location, resultcode, build_type,
12244 op0, op1)
12245 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12246 else
12247 ret = build2 (resultcode, build_type, op0, op1);
12248 if (final_type != NULL_TREE)
12249 ret = convert (final_type, ret);
12250
12251 return_build_binary_op:
12252 gcc_assert (ret != error_mark_node);
12253 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12254 ret = (int_operands
12255 ? note_integer_operands (ret)
12256 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12257 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12258 && !in_late_binary_op)
12259 ret = note_integer_operands (ret);
12260 protected_set_expr_location (ret, location);
12261
12262 if (instrument_expr != NULL)
12263 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12264 instrument_expr, ret);
12265
12266 if (semantic_result_type)
12267 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12268 semantic_result_type, ret);
12269
12270 return ret;
12271 }
12272
12273
12274 /* Convert EXPR to be a truth-value, validating its type for this
12275 purpose. LOCATION is the source location for the expression. */
12276
12277 tree
12278 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12279 {
12280 bool int_const, int_operands;
12281
12282 switch (TREE_CODE (TREE_TYPE (expr)))
12283 {
12284 case ARRAY_TYPE:
12285 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12286 return error_mark_node;
12287
12288 case RECORD_TYPE:
12289 error_at (location, "used struct type value where scalar is required");
12290 return error_mark_node;
12291
12292 case UNION_TYPE:
12293 error_at (location, "used union type value where scalar is required");
12294 return error_mark_node;
12295
12296 case VOID_TYPE:
12297 error_at (location, "void value not ignored as it ought to be");
12298 return error_mark_node;
12299
12300 case POINTER_TYPE:
12301 if (reject_gcc_builtin (expr))
12302 return error_mark_node;
12303 break;
12304
12305 case FUNCTION_TYPE:
12306 gcc_unreachable ();
12307
12308 case VECTOR_TYPE:
12309 error_at (location, "used vector type where scalar is required");
12310 return error_mark_node;
12311
12312 default:
12313 break;
12314 }
12315
12316 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12317 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12318 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12319 {
12320 expr = remove_c_maybe_const_expr (expr);
12321 expr = build2 (NE_EXPR, integer_type_node, expr,
12322 convert (TREE_TYPE (expr), integer_zero_node));
12323 expr = note_integer_operands (expr);
12324 }
12325 else
12326 /* ??? Should we also give an error for vectors rather than leaving
12327 those to give errors later? */
12328 expr = c_common_truthvalue_conversion (location, expr);
12329
12330 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12331 {
12332 if (TREE_OVERFLOW (expr))
12333 return expr;
12334 else
12335 return note_integer_operands (expr);
12336 }
12337 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12338 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12339 return expr;
12340 }
12341 \f
12342
12343 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12344 required. */
12345
12346 tree
12347 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12348 {
12349 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12350 {
12351 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12352 /* Executing a compound literal inside a function reinitializes
12353 it. */
12354 if (!TREE_STATIC (decl))
12355 *se = true;
12356 return decl;
12357 }
12358 else
12359 return expr;
12360 }
12361 \f
12362 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12363 statement. LOC is the location of the construct. */
12364
12365 tree
12366 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12367 tree clauses)
12368 {
12369 body = c_end_compound_stmt (loc, body, true);
12370
12371 tree stmt = make_node (code);
12372 TREE_TYPE (stmt) = void_type_node;
12373 OMP_BODY (stmt) = body;
12374 OMP_CLAUSES (stmt) = clauses;
12375 SET_EXPR_LOCATION (stmt, loc);
12376
12377 return add_stmt (stmt);
12378 }
12379
12380 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12381 statement. LOC is the location of the OACC_DATA. */
12382
12383 tree
12384 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12385 {
12386 tree stmt;
12387
12388 block = c_end_compound_stmt (loc, block, true);
12389
12390 stmt = make_node (OACC_DATA);
12391 TREE_TYPE (stmt) = void_type_node;
12392 OACC_DATA_CLAUSES (stmt) = clauses;
12393 OACC_DATA_BODY (stmt) = block;
12394 SET_EXPR_LOCATION (stmt, loc);
12395
12396 return add_stmt (stmt);
12397 }
12398
12399 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12400 statement. LOC is the location of the OACC_HOST_DATA. */
12401
12402 tree
12403 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12404 {
12405 tree stmt;
12406
12407 block = c_end_compound_stmt (loc, block, true);
12408
12409 stmt = make_node (OACC_HOST_DATA);
12410 TREE_TYPE (stmt) = void_type_node;
12411 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12412 OACC_HOST_DATA_BODY (stmt) = block;
12413 SET_EXPR_LOCATION (stmt, loc);
12414
12415 return add_stmt (stmt);
12416 }
12417
12418 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12419
12420 tree
12421 c_begin_omp_parallel (void)
12422 {
12423 tree block;
12424
12425 keep_next_level ();
12426 block = c_begin_compound_stmt (true);
12427
12428 return block;
12429 }
12430
12431 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12432 statement. LOC is the location of the OMP_PARALLEL. */
12433
12434 tree
12435 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12436 {
12437 tree stmt;
12438
12439 block = c_end_compound_stmt (loc, block, true);
12440
12441 stmt = make_node (OMP_PARALLEL);
12442 TREE_TYPE (stmt) = void_type_node;
12443 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12444 OMP_PARALLEL_BODY (stmt) = block;
12445 SET_EXPR_LOCATION (stmt, loc);
12446
12447 return add_stmt (stmt);
12448 }
12449
12450 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12451
12452 tree
12453 c_begin_omp_task (void)
12454 {
12455 tree block;
12456
12457 keep_next_level ();
12458 block = c_begin_compound_stmt (true);
12459
12460 return block;
12461 }
12462
12463 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12464 statement. LOC is the location of the #pragma. */
12465
12466 tree
12467 c_finish_omp_task (location_t loc, tree clauses, tree block)
12468 {
12469 tree stmt;
12470
12471 block = c_end_compound_stmt (loc, block, true);
12472
12473 stmt = make_node (OMP_TASK);
12474 TREE_TYPE (stmt) = void_type_node;
12475 OMP_TASK_CLAUSES (stmt) = clauses;
12476 OMP_TASK_BODY (stmt) = block;
12477 SET_EXPR_LOCATION (stmt, loc);
12478
12479 return add_stmt (stmt);
12480 }
12481
12482 /* Generate GOMP_cancel call for #pragma omp cancel. */
12483
12484 void
12485 c_finish_omp_cancel (location_t loc, tree clauses)
12486 {
12487 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12488 int mask = 0;
12489 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12490 mask = 1;
12491 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12492 mask = 2;
12493 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12494 mask = 4;
12495 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12496 mask = 8;
12497 else
12498 {
12499 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12500 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12501 "clauses");
12502 return;
12503 }
12504 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12505 if (ifc != NULL_TREE)
12506 {
12507 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12508 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12509 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12510 build_zero_cst (type));
12511 }
12512 else
12513 ifc = boolean_true_node;
12514 tree stmt = build_call_expr_loc (loc, fn, 2,
12515 build_int_cst (integer_type_node, mask),
12516 ifc);
12517 add_stmt (stmt);
12518 }
12519
12520 /* Generate GOMP_cancellation_point call for
12521 #pragma omp cancellation point. */
12522
12523 void
12524 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12525 {
12526 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12527 int mask = 0;
12528 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12529 mask = 1;
12530 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12531 mask = 2;
12532 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12533 mask = 4;
12534 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12535 mask = 8;
12536 else
12537 {
12538 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12539 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12540 "clauses");
12541 return;
12542 }
12543 tree stmt = build_call_expr_loc (loc, fn, 1,
12544 build_int_cst (integer_type_node, mask));
12545 add_stmt (stmt);
12546 }
12547
12548 /* Helper function for handle_omp_array_sections. Called recursively
12549 to handle multiple array-section-subscripts. C is the clause,
12550 T current expression (initially OMP_CLAUSE_DECL), which is either
12551 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12552 expression if specified, TREE_VALUE length expression if specified,
12553 TREE_CHAIN is what it has been specified after, or some decl.
12554 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12555 set to true if any of the array-section-subscript could have length
12556 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12557 first array-section-subscript which is known not to have length
12558 of one. Given say:
12559 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12560 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12561 all are or may have length of 1, array-section-subscript [:2] is the
12562 first one known not to have length 1. For array-section-subscript
12563 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12564 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12565 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12566 case though, as some lengths could be zero. */
12567
12568 static tree
12569 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12570 bool &maybe_zero_len, unsigned int &first_non_one,
12571 enum c_omp_region_type ort)
12572 {
12573 tree ret, low_bound, length, type;
12574 if (TREE_CODE (t) != TREE_LIST)
12575 {
12576 if (error_operand_p (t))
12577 return error_mark_node;
12578 ret = t;
12579 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12580 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12581 {
12582 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12583 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12584 return error_mark_node;
12585 }
12586 if (TREE_CODE (t) == COMPONENT_REF
12587 && ort == C_ORT_OMP
12588 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12589 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12590 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12591 {
12592 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12593 {
12594 error_at (OMP_CLAUSE_LOCATION (c),
12595 "bit-field %qE in %qs clause",
12596 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12597 return error_mark_node;
12598 }
12599 while (TREE_CODE (t) == COMPONENT_REF)
12600 {
12601 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12602 {
12603 error_at (OMP_CLAUSE_LOCATION (c),
12604 "%qE is a member of a union", t);
12605 return error_mark_node;
12606 }
12607 t = TREE_OPERAND (t, 0);
12608 }
12609 }
12610 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12611 {
12612 if (DECL_P (t))
12613 error_at (OMP_CLAUSE_LOCATION (c),
12614 "%qD is not a variable in %qs clause", t,
12615 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12616 else
12617 error_at (OMP_CLAUSE_LOCATION (c),
12618 "%qE is not a variable in %qs clause", t,
12619 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12620 return error_mark_node;
12621 }
12622 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12623 && TYPE_ATOMIC (TREE_TYPE (t)))
12624 {
12625 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12626 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12627 return error_mark_node;
12628 }
12629 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12630 && VAR_P (t)
12631 && DECL_THREAD_LOCAL_P (t))
12632 {
12633 error_at (OMP_CLAUSE_LOCATION (c),
12634 "%qD is threadprivate variable in %qs clause", t,
12635 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12636 return error_mark_node;
12637 }
12638 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12639 && TYPE_ATOMIC (TREE_TYPE (t))
12640 && POINTER_TYPE_P (TREE_TYPE (t)))
12641 {
12642 /* If the array section is pointer based and the pointer
12643 itself is _Atomic qualified, we need to atomically load
12644 the pointer. */
12645 c_expr expr;
12646 memset (&expr, 0, sizeof (expr));
12647 expr.value = ret;
12648 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12649 expr, false, false);
12650 ret = expr.value;
12651 }
12652 return ret;
12653 }
12654
12655 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12656 maybe_zero_len, first_non_one, ort);
12657 if (ret == error_mark_node || ret == NULL_TREE)
12658 return ret;
12659
12660 type = TREE_TYPE (ret);
12661 low_bound = TREE_PURPOSE (t);
12662 length = TREE_VALUE (t);
12663
12664 if (low_bound == error_mark_node || length == error_mark_node)
12665 return error_mark_node;
12666
12667 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12668 {
12669 error_at (OMP_CLAUSE_LOCATION (c),
12670 "low bound %qE of array section does not have integral type",
12671 low_bound);
12672 return error_mark_node;
12673 }
12674 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12675 {
12676 error_at (OMP_CLAUSE_LOCATION (c),
12677 "length %qE of array section does not have integral type",
12678 length);
12679 return error_mark_node;
12680 }
12681 if (low_bound
12682 && TREE_CODE (low_bound) == INTEGER_CST
12683 && TYPE_PRECISION (TREE_TYPE (low_bound))
12684 > TYPE_PRECISION (sizetype))
12685 low_bound = fold_convert (sizetype, low_bound);
12686 if (length
12687 && TREE_CODE (length) == INTEGER_CST
12688 && TYPE_PRECISION (TREE_TYPE (length))
12689 > TYPE_PRECISION (sizetype))
12690 length = fold_convert (sizetype, length);
12691 if (low_bound == NULL_TREE)
12692 low_bound = integer_zero_node;
12693
12694 if (length != NULL_TREE)
12695 {
12696 if (!integer_nonzerop (length))
12697 {
12698 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12699 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12700 {
12701 if (integer_zerop (length))
12702 {
12703 error_at (OMP_CLAUSE_LOCATION (c),
12704 "zero length array section in %qs clause",
12705 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12706 return error_mark_node;
12707 }
12708 }
12709 else
12710 maybe_zero_len = true;
12711 }
12712 if (first_non_one == types.length ()
12713 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12714 first_non_one++;
12715 }
12716 if (TREE_CODE (type) == ARRAY_TYPE)
12717 {
12718 if (length == NULL_TREE
12719 && (TYPE_DOMAIN (type) == NULL_TREE
12720 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12721 {
12722 error_at (OMP_CLAUSE_LOCATION (c),
12723 "for unknown bound array type length expression must "
12724 "be specified");
12725 return error_mark_node;
12726 }
12727 if (TREE_CODE (low_bound) == INTEGER_CST
12728 && tree_int_cst_sgn (low_bound) == -1)
12729 {
12730 error_at (OMP_CLAUSE_LOCATION (c),
12731 "negative low bound in array section in %qs clause",
12732 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12733 return error_mark_node;
12734 }
12735 if (length != NULL_TREE
12736 && TREE_CODE (length) == INTEGER_CST
12737 && tree_int_cst_sgn (length) == -1)
12738 {
12739 error_at (OMP_CLAUSE_LOCATION (c),
12740 "negative length in array section in %qs clause",
12741 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12742 return error_mark_node;
12743 }
12744 if (TYPE_DOMAIN (type)
12745 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12746 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12747 == INTEGER_CST)
12748 {
12749 tree size
12750 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12751 size = size_binop (PLUS_EXPR, size, size_one_node);
12752 if (TREE_CODE (low_bound) == INTEGER_CST)
12753 {
12754 if (tree_int_cst_lt (size, low_bound))
12755 {
12756 error_at (OMP_CLAUSE_LOCATION (c),
12757 "low bound %qE above array section size "
12758 "in %qs clause", low_bound,
12759 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12760 return error_mark_node;
12761 }
12762 if (tree_int_cst_equal (size, low_bound))
12763 {
12764 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12765 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12766 {
12767 error_at (OMP_CLAUSE_LOCATION (c),
12768 "zero length array section in %qs clause",
12769 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12770 return error_mark_node;
12771 }
12772 maybe_zero_len = true;
12773 }
12774 else if (length == NULL_TREE
12775 && first_non_one == types.length ()
12776 && tree_int_cst_equal
12777 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12778 low_bound))
12779 first_non_one++;
12780 }
12781 else if (length == NULL_TREE)
12782 {
12783 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12784 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12785 maybe_zero_len = true;
12786 if (first_non_one == types.length ())
12787 first_non_one++;
12788 }
12789 if (length && TREE_CODE (length) == INTEGER_CST)
12790 {
12791 if (tree_int_cst_lt (size, length))
12792 {
12793 error_at (OMP_CLAUSE_LOCATION (c),
12794 "length %qE above array section size "
12795 "in %qs clause", length,
12796 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12797 return error_mark_node;
12798 }
12799 if (TREE_CODE (low_bound) == INTEGER_CST)
12800 {
12801 tree lbpluslen
12802 = size_binop (PLUS_EXPR,
12803 fold_convert (sizetype, low_bound),
12804 fold_convert (sizetype, length));
12805 if (TREE_CODE (lbpluslen) == INTEGER_CST
12806 && tree_int_cst_lt (size, lbpluslen))
12807 {
12808 error_at (OMP_CLAUSE_LOCATION (c),
12809 "high bound %qE above array section size "
12810 "in %qs clause", lbpluslen,
12811 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12812 return error_mark_node;
12813 }
12814 }
12815 }
12816 }
12817 else if (length == NULL_TREE)
12818 {
12819 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12820 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12821 maybe_zero_len = true;
12822 if (first_non_one == types.length ())
12823 first_non_one++;
12824 }
12825
12826 /* For [lb:] we will need to evaluate lb more than once. */
12827 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12828 {
12829 tree lb = save_expr (low_bound);
12830 if (lb != low_bound)
12831 {
12832 TREE_PURPOSE (t) = lb;
12833 low_bound = lb;
12834 }
12835 }
12836 }
12837 else if (TREE_CODE (type) == POINTER_TYPE)
12838 {
12839 if (length == NULL_TREE)
12840 {
12841 error_at (OMP_CLAUSE_LOCATION (c),
12842 "for pointer type length expression must be specified");
12843 return error_mark_node;
12844 }
12845 if (length != NULL_TREE
12846 && TREE_CODE (length) == INTEGER_CST
12847 && tree_int_cst_sgn (length) == -1)
12848 {
12849 error_at (OMP_CLAUSE_LOCATION (c),
12850 "negative length in array section in %qs clause",
12851 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12852 return error_mark_node;
12853 }
12854 /* If there is a pointer type anywhere but in the very first
12855 array-section-subscript, the array section can't be contiguous. */
12856 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12857 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12858 {
12859 error_at (OMP_CLAUSE_LOCATION (c),
12860 "array section is not contiguous in %qs clause",
12861 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12862 return error_mark_node;
12863 }
12864 }
12865 else
12866 {
12867 error_at (OMP_CLAUSE_LOCATION (c),
12868 "%qE does not have pointer or array type", ret);
12869 return error_mark_node;
12870 }
12871 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12872 types.safe_push (TREE_TYPE (ret));
12873 /* We will need to evaluate lb more than once. */
12874 tree lb = save_expr (low_bound);
12875 if (lb != low_bound)
12876 {
12877 TREE_PURPOSE (t) = lb;
12878 low_bound = lb;
12879 }
12880 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12881 return ret;
12882 }
12883
12884 /* Handle array sections for clause C. */
12885
12886 static bool
12887 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
12888 {
12889 bool maybe_zero_len = false;
12890 unsigned int first_non_one = 0;
12891 auto_vec<tree, 10> types;
12892 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
12893 maybe_zero_len, first_non_one,
12894 ort);
12895 if (first == error_mark_node)
12896 return true;
12897 if (first == NULL_TREE)
12898 return false;
12899 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12900 {
12901 tree t = OMP_CLAUSE_DECL (c);
12902 tree tem = NULL_TREE;
12903 /* Need to evaluate side effects in the length expressions
12904 if any. */
12905 while (TREE_CODE (t) == TREE_LIST)
12906 {
12907 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12908 {
12909 if (tem == NULL_TREE)
12910 tem = TREE_VALUE (t);
12911 else
12912 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12913 TREE_VALUE (t), tem);
12914 }
12915 t = TREE_CHAIN (t);
12916 }
12917 if (tem)
12918 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12919 first = c_fully_fold (first, false, NULL, true);
12920 OMP_CLAUSE_DECL (c) = first;
12921 }
12922 else
12923 {
12924 unsigned int num = types.length (), i;
12925 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12926 tree condition = NULL_TREE;
12927
12928 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12929 maybe_zero_len = true;
12930
12931 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12932 t = TREE_CHAIN (t))
12933 {
12934 tree low_bound = TREE_PURPOSE (t);
12935 tree length = TREE_VALUE (t);
12936
12937 i--;
12938 if (low_bound
12939 && TREE_CODE (low_bound) == INTEGER_CST
12940 && TYPE_PRECISION (TREE_TYPE (low_bound))
12941 > TYPE_PRECISION (sizetype))
12942 low_bound = fold_convert (sizetype, low_bound);
12943 if (length
12944 && TREE_CODE (length) == INTEGER_CST
12945 && TYPE_PRECISION (TREE_TYPE (length))
12946 > TYPE_PRECISION (sizetype))
12947 length = fold_convert (sizetype, length);
12948 if (low_bound == NULL_TREE)
12949 low_bound = integer_zero_node;
12950 if (!maybe_zero_len && i > first_non_one)
12951 {
12952 if (integer_nonzerop (low_bound))
12953 goto do_warn_noncontiguous;
12954 if (length != NULL_TREE
12955 && TREE_CODE (length) == INTEGER_CST
12956 && TYPE_DOMAIN (types[i])
12957 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12958 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12959 == INTEGER_CST)
12960 {
12961 tree size;
12962 size = size_binop (PLUS_EXPR,
12963 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12964 size_one_node);
12965 if (!tree_int_cst_equal (length, size))
12966 {
12967 do_warn_noncontiguous:
12968 error_at (OMP_CLAUSE_LOCATION (c),
12969 "array section is not contiguous in %qs "
12970 "clause",
12971 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12972 return true;
12973 }
12974 }
12975 if (length != NULL_TREE
12976 && TREE_SIDE_EFFECTS (length))
12977 {
12978 if (side_effects == NULL_TREE)
12979 side_effects = length;
12980 else
12981 side_effects = build2 (COMPOUND_EXPR,
12982 TREE_TYPE (side_effects),
12983 length, side_effects);
12984 }
12985 }
12986 else
12987 {
12988 tree l;
12989
12990 if (i > first_non_one
12991 && ((length && integer_nonzerop (length))
12992 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
12993 continue;
12994 if (length)
12995 l = fold_convert (sizetype, length);
12996 else
12997 {
12998 l = size_binop (PLUS_EXPR,
12999 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13000 size_one_node);
13001 l = size_binop (MINUS_EXPR, l,
13002 fold_convert (sizetype, low_bound));
13003 }
13004 if (i > first_non_one)
13005 {
13006 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13007 size_zero_node);
13008 if (condition == NULL_TREE)
13009 condition = l;
13010 else
13011 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13012 l, condition);
13013 }
13014 else if (size == NULL_TREE)
13015 {
13016 size = size_in_bytes (TREE_TYPE (types[i]));
13017 tree eltype = TREE_TYPE (types[num - 1]);
13018 while (TREE_CODE (eltype) == ARRAY_TYPE)
13019 eltype = TREE_TYPE (eltype);
13020 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13021 {
13022 if (integer_zerop (size)
13023 || integer_zerop (size_in_bytes (eltype)))
13024 {
13025 error_at (OMP_CLAUSE_LOCATION (c),
13026 "zero length array section in %qs clause",
13027 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13028 return error_mark_node;
13029 }
13030 size = size_binop (EXACT_DIV_EXPR, size,
13031 size_in_bytes (eltype));
13032 }
13033 size = size_binop (MULT_EXPR, size, l);
13034 if (condition)
13035 size = fold_build3 (COND_EXPR, sizetype, condition,
13036 size, size_zero_node);
13037 }
13038 else
13039 size = size_binop (MULT_EXPR, size, l);
13040 }
13041 }
13042 if (side_effects)
13043 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13044 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13045 {
13046 size = size_binop (MINUS_EXPR, size, size_one_node);
13047 size = c_fully_fold (size, false, NULL);
13048 tree index_type = build_index_type (size);
13049 tree eltype = TREE_TYPE (first);
13050 while (TREE_CODE (eltype) == ARRAY_TYPE)
13051 eltype = TREE_TYPE (eltype);
13052 tree type = build_array_type (eltype, index_type);
13053 tree ptype = build_pointer_type (eltype);
13054 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13055 t = build_fold_addr_expr (t);
13056 tree t2 = build_fold_addr_expr (first);
13057 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13058 ptrdiff_type_node, t2);
13059 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13060 ptrdiff_type_node, t2,
13061 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13062 ptrdiff_type_node, t));
13063 t2 = c_fully_fold (t2, false, NULL);
13064 if (tree_fits_shwi_p (t2))
13065 t = build2 (MEM_REF, type, t,
13066 build_int_cst (ptype, tree_to_shwi (t2)));
13067 else
13068 {
13069 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13070 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13071 TREE_TYPE (t), t, t2);
13072 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13073 }
13074 OMP_CLAUSE_DECL (c) = t;
13075 return false;
13076 }
13077 first = c_fully_fold (first, false, NULL);
13078 OMP_CLAUSE_DECL (c) = first;
13079 if (size)
13080 size = c_fully_fold (size, false, NULL);
13081 OMP_CLAUSE_SIZE (c) = size;
13082 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13083 || (TREE_CODE (t) == COMPONENT_REF
13084 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13085 return false;
13086 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13087 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
13088 switch (OMP_CLAUSE_MAP_KIND (c))
13089 {
13090 case GOMP_MAP_ALLOC:
13091 case GOMP_MAP_TO:
13092 case GOMP_MAP_FROM:
13093 case GOMP_MAP_TOFROM:
13094 case GOMP_MAP_ALWAYS_TO:
13095 case GOMP_MAP_ALWAYS_FROM:
13096 case GOMP_MAP_ALWAYS_TOFROM:
13097 case GOMP_MAP_RELEASE:
13098 case GOMP_MAP_DELETE:
13099 case GOMP_MAP_FORCE_TO:
13100 case GOMP_MAP_FORCE_FROM:
13101 case GOMP_MAP_FORCE_TOFROM:
13102 case GOMP_MAP_FORCE_PRESENT:
13103 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13104 break;
13105 default:
13106 break;
13107 }
13108 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13109 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13110 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13111 else if (TREE_CODE (t) == COMPONENT_REF)
13112 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
13113 else
13114 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13115 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13116 && !c_mark_addressable (t))
13117 return false;
13118 OMP_CLAUSE_DECL (c2) = t;
13119 t = build_fold_addr_expr (first);
13120 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13121 tree ptr = OMP_CLAUSE_DECL (c2);
13122 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13123 ptr = build_fold_addr_expr (ptr);
13124 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13125 ptrdiff_type_node, t,
13126 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13127 ptrdiff_type_node, ptr));
13128 t = c_fully_fold (t, false, NULL);
13129 OMP_CLAUSE_SIZE (c2) = t;
13130 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13131 OMP_CLAUSE_CHAIN (c) = c2;
13132 }
13133 return false;
13134 }
13135
13136 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13137 an inline call. But, remap
13138 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13139 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13140
13141 static tree
13142 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13143 tree decl, tree placeholder)
13144 {
13145 copy_body_data id;
13146 hash_map<tree, tree> decl_map;
13147
13148 decl_map.put (omp_decl1, placeholder);
13149 decl_map.put (omp_decl2, decl);
13150 memset (&id, 0, sizeof (id));
13151 id.src_fn = DECL_CONTEXT (omp_decl1);
13152 id.dst_fn = current_function_decl;
13153 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13154 id.decl_map = &decl_map;
13155
13156 id.copy_decl = copy_decl_no_change;
13157 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13158 id.transform_new_cfg = true;
13159 id.transform_return_to_modify = false;
13160 id.transform_lang_insert_block = NULL;
13161 id.eh_lp_nr = 0;
13162 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13163 return stmt;
13164 }
13165
13166 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13167 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13168
13169 static tree
13170 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13171 {
13172 if (*tp == (tree) data)
13173 return *tp;
13174 return NULL_TREE;
13175 }
13176
13177 /* For all elements of CLAUSES, validate them against their constraints.
13178 Remove any elements from the list that are invalid. */
13179
13180 tree
13181 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13182 {
13183 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13184 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
13185 tree c, t, type, *pc;
13186 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13187 bool branch_seen = false;
13188 bool copyprivate_seen = false;
13189 bool linear_variable_step_check = false;
13190 tree *nowait_clause = NULL;
13191 bool ordered_seen = false;
13192 tree schedule_clause = NULL_TREE;
13193 bool oacc_async = false;
13194
13195 bitmap_obstack_initialize (NULL);
13196 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13197 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13198 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13199 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13200 bitmap_initialize (&map_head, &bitmap_default_obstack);
13201 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13202 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13203
13204 if (ort & C_ORT_ACC)
13205 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13206 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13207 {
13208 oacc_async = true;
13209 break;
13210 }
13211
13212 for (pc = &clauses, c = clauses; c ; c = *pc)
13213 {
13214 bool remove = false;
13215 bool need_complete = false;
13216 bool need_implicitly_determined = false;
13217
13218 switch (OMP_CLAUSE_CODE (c))
13219 {
13220 case OMP_CLAUSE_SHARED:
13221 need_implicitly_determined = true;
13222 goto check_dup_generic;
13223
13224 case OMP_CLAUSE_PRIVATE:
13225 need_complete = true;
13226 need_implicitly_determined = true;
13227 goto check_dup_generic;
13228
13229 case OMP_CLAUSE_REDUCTION:
13230 need_implicitly_determined = true;
13231 t = OMP_CLAUSE_DECL (c);
13232 if (TREE_CODE (t) == TREE_LIST)
13233 {
13234 if (handle_omp_array_sections (c, ort))
13235 {
13236 remove = true;
13237 break;
13238 }
13239
13240 t = OMP_CLAUSE_DECL (c);
13241 }
13242 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13243 if (t == error_mark_node)
13244 {
13245 remove = true;
13246 break;
13247 }
13248 if (oacc_async)
13249 c_mark_addressable (t);
13250 type = TREE_TYPE (t);
13251 if (TREE_CODE (t) == MEM_REF)
13252 type = TREE_TYPE (type);
13253 if (TREE_CODE (type) == ARRAY_TYPE)
13254 {
13255 tree oatype = type;
13256 gcc_assert (TREE_CODE (t) != MEM_REF);
13257 while (TREE_CODE (type) == ARRAY_TYPE)
13258 type = TREE_TYPE (type);
13259 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13260 {
13261 error_at (OMP_CLAUSE_LOCATION (c),
13262 "%qD in %<reduction%> clause is a zero size array",
13263 t);
13264 remove = true;
13265 break;
13266 }
13267 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13268 TYPE_SIZE_UNIT (type));
13269 if (integer_zerop (size))
13270 {
13271 error_at (OMP_CLAUSE_LOCATION (c),
13272 "%qD in %<reduction%> clause is a zero size array",
13273 t);
13274 remove = true;
13275 break;
13276 }
13277 size = size_binop (MINUS_EXPR, size, size_one_node);
13278 tree index_type = build_index_type (size);
13279 tree atype = build_array_type (type, index_type);
13280 tree ptype = build_pointer_type (type);
13281 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13282 t = build_fold_addr_expr (t);
13283 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13284 OMP_CLAUSE_DECL (c) = t;
13285 }
13286 if (TYPE_ATOMIC (type))
13287 {
13288 error_at (OMP_CLAUSE_LOCATION (c),
13289 "%<_Atomic%> %qE in %<reduction%> clause", t);
13290 remove = true;
13291 break;
13292 }
13293 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13294 && (FLOAT_TYPE_P (type)
13295 || TREE_CODE (type) == COMPLEX_TYPE))
13296 {
13297 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13298 const char *r_name = NULL;
13299
13300 switch (r_code)
13301 {
13302 case PLUS_EXPR:
13303 case MULT_EXPR:
13304 case MINUS_EXPR:
13305 break;
13306 case MIN_EXPR:
13307 if (TREE_CODE (type) == COMPLEX_TYPE)
13308 r_name = "min";
13309 break;
13310 case MAX_EXPR:
13311 if (TREE_CODE (type) == COMPLEX_TYPE)
13312 r_name = "max";
13313 break;
13314 case BIT_AND_EXPR:
13315 r_name = "&";
13316 break;
13317 case BIT_XOR_EXPR:
13318 r_name = "^";
13319 break;
13320 case BIT_IOR_EXPR:
13321 r_name = "|";
13322 break;
13323 case TRUTH_ANDIF_EXPR:
13324 if (FLOAT_TYPE_P (type))
13325 r_name = "&&";
13326 break;
13327 case TRUTH_ORIF_EXPR:
13328 if (FLOAT_TYPE_P (type))
13329 r_name = "||";
13330 break;
13331 default:
13332 gcc_unreachable ();
13333 }
13334 if (r_name)
13335 {
13336 error_at (OMP_CLAUSE_LOCATION (c),
13337 "%qE has invalid type for %<reduction(%s)%>",
13338 t, r_name);
13339 remove = true;
13340 break;
13341 }
13342 }
13343 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13344 {
13345 error_at (OMP_CLAUSE_LOCATION (c),
13346 "user defined reduction not found for %qE", t);
13347 remove = true;
13348 break;
13349 }
13350 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13351 {
13352 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13353 type = TYPE_MAIN_VARIANT (type);
13354 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13355 VAR_DECL, NULL_TREE, type);
13356 tree decl_placeholder = NULL_TREE;
13357 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13358 DECL_ARTIFICIAL (placeholder) = 1;
13359 DECL_IGNORED_P (placeholder) = 1;
13360 if (TREE_CODE (t) == MEM_REF)
13361 {
13362 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13363 VAR_DECL, NULL_TREE, type);
13364 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13365 DECL_ARTIFICIAL (decl_placeholder) = 1;
13366 DECL_IGNORED_P (decl_placeholder) = 1;
13367 }
13368 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13369 c_mark_addressable (placeholder);
13370 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13371 c_mark_addressable (decl_placeholder ? decl_placeholder
13372 : OMP_CLAUSE_DECL (c));
13373 OMP_CLAUSE_REDUCTION_MERGE (c)
13374 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13375 TREE_VEC_ELT (list, 0),
13376 TREE_VEC_ELT (list, 1),
13377 decl_placeholder ? decl_placeholder
13378 : OMP_CLAUSE_DECL (c), placeholder);
13379 OMP_CLAUSE_REDUCTION_MERGE (c)
13380 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13381 void_type_node, NULL_TREE,
13382 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13383 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13384 if (TREE_VEC_LENGTH (list) == 6)
13385 {
13386 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13387 c_mark_addressable (decl_placeholder ? decl_placeholder
13388 : OMP_CLAUSE_DECL (c));
13389 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13390 c_mark_addressable (placeholder);
13391 tree init = TREE_VEC_ELT (list, 5);
13392 if (init == error_mark_node)
13393 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13394 OMP_CLAUSE_REDUCTION_INIT (c)
13395 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13396 TREE_VEC_ELT (list, 3),
13397 decl_placeholder ? decl_placeholder
13398 : OMP_CLAUSE_DECL (c), placeholder);
13399 if (TREE_VEC_ELT (list, 5) == error_mark_node)
13400 {
13401 tree v = decl_placeholder ? decl_placeholder : t;
13402 OMP_CLAUSE_REDUCTION_INIT (c)
13403 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13404 OMP_CLAUSE_REDUCTION_INIT (c));
13405 }
13406 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13407 c_find_omp_placeholder_r,
13408 placeholder, NULL))
13409 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13410 }
13411 else
13412 {
13413 tree init;
13414 tree v = decl_placeholder ? decl_placeholder : t;
13415 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13416 init = build_constructor (TREE_TYPE (v), NULL);
13417 else
13418 init = fold_convert (TREE_TYPE (v), integer_zero_node);
13419 OMP_CLAUSE_REDUCTION_INIT (c)
13420 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13421 }
13422 OMP_CLAUSE_REDUCTION_INIT (c)
13423 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13424 void_type_node, NULL_TREE,
13425 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13426 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13427 }
13428 if (TREE_CODE (t) == MEM_REF)
13429 {
13430 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13431 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13432 != INTEGER_CST)
13433 {
13434 sorry ("variable length element type in array "
13435 "%<reduction%> clause");
13436 remove = true;
13437 break;
13438 }
13439 t = TREE_OPERAND (t, 0);
13440 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13441 t = TREE_OPERAND (t, 0);
13442 if (TREE_CODE (t) == ADDR_EXPR)
13443 t = TREE_OPERAND (t, 0);
13444 }
13445 goto check_dup_generic_t;
13446
13447 case OMP_CLAUSE_COPYPRIVATE:
13448 copyprivate_seen = true;
13449 if (nowait_clause)
13450 {
13451 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13452 "%<nowait%> clause must not be used together "
13453 "with %<copyprivate%>");
13454 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13455 nowait_clause = NULL;
13456 }
13457 goto check_dup_generic;
13458
13459 case OMP_CLAUSE_COPYIN:
13460 t = OMP_CLAUSE_DECL (c);
13461 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13462 {
13463 error_at (OMP_CLAUSE_LOCATION (c),
13464 "%qE must be %<threadprivate%> for %<copyin%>", t);
13465 remove = true;
13466 break;
13467 }
13468 goto check_dup_generic;
13469
13470 case OMP_CLAUSE_LINEAR:
13471 if (ort != C_ORT_OMP_DECLARE_SIMD)
13472 need_implicitly_determined = true;
13473 t = OMP_CLAUSE_DECL (c);
13474 if (ort != C_ORT_OMP_DECLARE_SIMD
13475 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13476 {
13477 error_at (OMP_CLAUSE_LOCATION (c),
13478 "modifier should not be specified in %<linear%> "
13479 "clause on %<simd%> or %<for%> constructs");
13480 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13481 }
13482 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13483 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13484 {
13485 error_at (OMP_CLAUSE_LOCATION (c),
13486 "linear clause applied to non-integral non-pointer "
13487 "variable with type %qT", TREE_TYPE (t));
13488 remove = true;
13489 break;
13490 }
13491 if (TYPE_ATOMIC (TREE_TYPE (t)))
13492 {
13493 error_at (OMP_CLAUSE_LOCATION (c),
13494 "%<_Atomic%> %qD in %<linear%> clause", t);
13495 remove = true;
13496 break;
13497 }
13498 if (ort == C_ORT_OMP_DECLARE_SIMD)
13499 {
13500 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13501 if (TREE_CODE (s) == PARM_DECL)
13502 {
13503 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13504 /* map_head bitmap is used as uniform_head if
13505 declare_simd. */
13506 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13507 linear_variable_step_check = true;
13508 goto check_dup_generic;
13509 }
13510 if (TREE_CODE (s) != INTEGER_CST)
13511 {
13512 error_at (OMP_CLAUSE_LOCATION (c),
13513 "%<linear%> clause step %qE is neither constant "
13514 "nor a parameter", s);
13515 remove = true;
13516 break;
13517 }
13518 }
13519 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13520 {
13521 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13522 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13523 OMP_CLAUSE_DECL (c), s);
13524 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13525 sizetype, fold_convert (sizetype, s),
13526 fold_convert
13527 (sizetype, OMP_CLAUSE_DECL (c)));
13528 if (s == error_mark_node)
13529 s = size_one_node;
13530 OMP_CLAUSE_LINEAR_STEP (c) = s;
13531 }
13532 else
13533 OMP_CLAUSE_LINEAR_STEP (c)
13534 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
13535 goto check_dup_generic;
13536
13537 check_dup_generic:
13538 t = OMP_CLAUSE_DECL (c);
13539 check_dup_generic_t:
13540 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13541 {
13542 error_at (OMP_CLAUSE_LOCATION (c),
13543 "%qE is not a variable in clause %qs", t,
13544 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13545 remove = true;
13546 }
13547 else if (ort == C_ORT_ACC
13548 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13549 {
13550 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13551 {
13552 error ("%qD appears more than once in reduction clauses", t);
13553 remove = true;
13554 }
13555 else
13556 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13557 }
13558 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13559 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13560 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13561 {
13562 error_at (OMP_CLAUSE_LOCATION (c),
13563 "%qE appears more than once in data clauses", t);
13564 remove = true;
13565 }
13566 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13567 && bitmap_bit_p (&map_head, DECL_UID (t)))
13568 {
13569 if (ort == C_ORT_ACC)
13570 error ("%qD appears more than once in data clauses", t);
13571 else
13572 error ("%qD appears both in data and map clauses", t);
13573 remove = true;
13574 }
13575 else
13576 bitmap_set_bit (&generic_head, DECL_UID (t));
13577 break;
13578
13579 case OMP_CLAUSE_FIRSTPRIVATE:
13580 t = OMP_CLAUSE_DECL (c);
13581 need_complete = true;
13582 need_implicitly_determined = true;
13583 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13584 {
13585 error_at (OMP_CLAUSE_LOCATION (c),
13586 "%qE is not a variable in clause %<firstprivate%>", t);
13587 remove = true;
13588 }
13589 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13590 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13591 {
13592 error_at (OMP_CLAUSE_LOCATION (c),
13593 "%qE appears more than once in data clauses", t);
13594 remove = true;
13595 }
13596 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13597 {
13598 if (ort == C_ORT_ACC)
13599 error ("%qD appears more than once in data clauses", t);
13600 else
13601 error ("%qD appears both in data and map clauses", t);
13602 remove = true;
13603 }
13604 else
13605 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13606 break;
13607
13608 case OMP_CLAUSE_LASTPRIVATE:
13609 t = OMP_CLAUSE_DECL (c);
13610 need_complete = true;
13611 need_implicitly_determined = true;
13612 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13613 {
13614 error_at (OMP_CLAUSE_LOCATION (c),
13615 "%qE is not a variable in clause %<lastprivate%>", t);
13616 remove = true;
13617 }
13618 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13619 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13620 {
13621 error_at (OMP_CLAUSE_LOCATION (c),
13622 "%qE appears more than once in data clauses", t);
13623 remove = true;
13624 }
13625 else
13626 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13627 break;
13628
13629 case OMP_CLAUSE_ALIGNED:
13630 t = OMP_CLAUSE_DECL (c);
13631 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13632 {
13633 error_at (OMP_CLAUSE_LOCATION (c),
13634 "%qE is not a variable in %<aligned%> clause", t);
13635 remove = true;
13636 }
13637 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13638 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13639 {
13640 error_at (OMP_CLAUSE_LOCATION (c),
13641 "%qE in %<aligned%> clause is neither a pointer nor "
13642 "an array", t);
13643 remove = true;
13644 }
13645 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13646 {
13647 error_at (OMP_CLAUSE_LOCATION (c),
13648 "%<_Atomic%> %qD in %<aligned%> clause", t);
13649 remove = true;
13650 break;
13651 }
13652 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13653 {
13654 error_at (OMP_CLAUSE_LOCATION (c),
13655 "%qE appears more than once in %<aligned%> clauses",
13656 t);
13657 remove = true;
13658 }
13659 else
13660 bitmap_set_bit (&aligned_head, DECL_UID (t));
13661 break;
13662
13663 case OMP_CLAUSE_DEPEND:
13664 t = OMP_CLAUSE_DECL (c);
13665 if (t == NULL_TREE)
13666 {
13667 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13668 == OMP_CLAUSE_DEPEND_SOURCE);
13669 break;
13670 }
13671 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13672 {
13673 gcc_assert (TREE_CODE (t) == TREE_LIST);
13674 for (; t; t = TREE_CHAIN (t))
13675 {
13676 tree decl = TREE_VALUE (t);
13677 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13678 {
13679 tree offset = TREE_PURPOSE (t);
13680 bool neg = wi::neg_p (wi::to_wide (offset));
13681 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13682 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13683 neg ? MINUS_EXPR : PLUS_EXPR,
13684 decl, offset);
13685 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13686 sizetype,
13687 fold_convert (sizetype, t2),
13688 fold_convert (sizetype, decl));
13689 if (t2 == error_mark_node)
13690 {
13691 remove = true;
13692 break;
13693 }
13694 TREE_PURPOSE (t) = t2;
13695 }
13696 }
13697 break;
13698 }
13699 if (TREE_CODE (t) == TREE_LIST)
13700 {
13701 if (handle_omp_array_sections (c, ort))
13702 remove = true;
13703 break;
13704 }
13705 if (t == error_mark_node)
13706 remove = true;
13707 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13708 {
13709 error_at (OMP_CLAUSE_LOCATION (c),
13710 "%qE is not a variable in %<depend%> clause", t);
13711 remove = true;
13712 }
13713 else if (!c_mark_addressable (t))
13714 remove = true;
13715 break;
13716
13717 case OMP_CLAUSE_MAP:
13718 case OMP_CLAUSE_TO:
13719 case OMP_CLAUSE_FROM:
13720 case OMP_CLAUSE__CACHE_:
13721 t = OMP_CLAUSE_DECL (c);
13722 if (TREE_CODE (t) == TREE_LIST)
13723 {
13724 if (handle_omp_array_sections (c, ort))
13725 remove = true;
13726 else
13727 {
13728 t = OMP_CLAUSE_DECL (c);
13729 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13730 {
13731 error_at (OMP_CLAUSE_LOCATION (c),
13732 "array section does not have mappable type "
13733 "in %qs clause",
13734 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13735 remove = true;
13736 }
13737 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13738 {
13739 error_at (OMP_CLAUSE_LOCATION (c),
13740 "%<_Atomic%> %qE in %qs clause", t,
13741 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13742 remove = true;
13743 }
13744 while (TREE_CODE (t) == ARRAY_REF)
13745 t = TREE_OPERAND (t, 0);
13746 if (TREE_CODE (t) == COMPONENT_REF
13747 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13748 {
13749 while (TREE_CODE (t) == COMPONENT_REF)
13750 t = TREE_OPERAND (t, 0);
13751 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13752 break;
13753 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13754 {
13755 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13756 error ("%qD appears more than once in motion"
13757 " clauses", t);
13758 else if (ort == C_ORT_ACC)
13759 error ("%qD appears more than once in data"
13760 " clauses", t);
13761 else
13762 error ("%qD appears more than once in map"
13763 " clauses", t);
13764 remove = true;
13765 }
13766 else
13767 {
13768 bitmap_set_bit (&map_head, DECL_UID (t));
13769 bitmap_set_bit (&map_field_head, DECL_UID (t));
13770 }
13771 }
13772 }
13773 break;
13774 }
13775 if (t == error_mark_node)
13776 {
13777 remove = true;
13778 break;
13779 }
13780 if (TREE_CODE (t) == COMPONENT_REF
13781 && (ort & C_ORT_OMP)
13782 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13783 {
13784 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13785 {
13786 error_at (OMP_CLAUSE_LOCATION (c),
13787 "bit-field %qE in %qs clause",
13788 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13789 remove = true;
13790 }
13791 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13792 {
13793 error_at (OMP_CLAUSE_LOCATION (c),
13794 "%qE does not have a mappable type in %qs clause",
13795 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13796 remove = true;
13797 }
13798 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13799 {
13800 error_at (OMP_CLAUSE_LOCATION (c),
13801 "%<_Atomic%> %qE in %qs clause", t,
13802 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13803 remove = true;
13804 }
13805 while (TREE_CODE (t) == COMPONENT_REF)
13806 {
13807 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13808 == UNION_TYPE)
13809 {
13810 error_at (OMP_CLAUSE_LOCATION (c),
13811 "%qE is a member of a union", t);
13812 remove = true;
13813 break;
13814 }
13815 t = TREE_OPERAND (t, 0);
13816 }
13817 if (remove)
13818 break;
13819 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13820 {
13821 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13822 break;
13823 }
13824 }
13825 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13826 {
13827 error_at (OMP_CLAUSE_LOCATION (c),
13828 "%qE is not a variable in %qs clause", t,
13829 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13830 remove = true;
13831 }
13832 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
13833 {
13834 error_at (OMP_CLAUSE_LOCATION (c),
13835 "%qD is threadprivate variable in %qs clause", t,
13836 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13837 remove = true;
13838 }
13839 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13840 || (OMP_CLAUSE_MAP_KIND (c)
13841 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13842 && !c_mark_addressable (t))
13843 remove = true;
13844 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13845 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13846 || (OMP_CLAUSE_MAP_KIND (c)
13847 == GOMP_MAP_FIRSTPRIVATE_POINTER)
13848 || (OMP_CLAUSE_MAP_KIND (c)
13849 == GOMP_MAP_FORCE_DEVICEPTR)))
13850 && t == OMP_CLAUSE_DECL (c)
13851 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13852 {
13853 error_at (OMP_CLAUSE_LOCATION (c),
13854 "%qD does not have a mappable type in %qs clause", t,
13855 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13856 remove = true;
13857 }
13858 else if (TREE_TYPE (t) == error_mark_node)
13859 remove = true;
13860 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13861 {
13862 error_at (OMP_CLAUSE_LOCATION (c),
13863 "%<_Atomic%> %qE in %qs clause", t,
13864 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13865 remove = true;
13866 }
13867 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13868 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13869 {
13870 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13871 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13872 {
13873 error ("%qD appears more than once in data clauses", t);
13874 remove = true;
13875 }
13876 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13877 {
13878 if (ort == C_ORT_ACC)
13879 error ("%qD appears more than once in data clauses", t);
13880 else
13881 error ("%qD appears both in data and map clauses", t);
13882 remove = true;
13883 }
13884 else
13885 bitmap_set_bit (&generic_head, DECL_UID (t));
13886 }
13887 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13888 {
13889 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13890 error ("%qD appears more than once in motion clauses", t);
13891 else if (ort == C_ORT_ACC)
13892 error ("%qD appears more than once in data clauses", t);
13893 else
13894 error ("%qD appears more than once in map clauses", t);
13895 remove = true;
13896 }
13897 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13898 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13899 {
13900 if (ort == C_ORT_ACC)
13901 error ("%qD appears more than once in data clauses", t);
13902 else
13903 error ("%qD appears both in data and map clauses", t);
13904 remove = true;
13905 }
13906 else
13907 {
13908 bitmap_set_bit (&map_head, DECL_UID (t));
13909 if (t != OMP_CLAUSE_DECL (c)
13910 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13911 bitmap_set_bit (&map_field_head, DECL_UID (t));
13912 }
13913 break;
13914
13915 case OMP_CLAUSE_TO_DECLARE:
13916 case OMP_CLAUSE_LINK:
13917 t = OMP_CLAUSE_DECL (c);
13918 if (TREE_CODE (t) == FUNCTION_DECL
13919 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13920 ;
13921 else if (!VAR_P (t))
13922 {
13923 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13924 error_at (OMP_CLAUSE_LOCATION (c),
13925 "%qE is neither a variable nor a function name in "
13926 "clause %qs", t,
13927 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13928 else
13929 error_at (OMP_CLAUSE_LOCATION (c),
13930 "%qE is not a variable in clause %qs", t,
13931 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13932 remove = true;
13933 }
13934 else if (DECL_THREAD_LOCAL_P (t))
13935 {
13936 error_at (OMP_CLAUSE_LOCATION (c),
13937 "%qD is threadprivate variable in %qs clause", t,
13938 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13939 remove = true;
13940 }
13941 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13942 {
13943 error_at (OMP_CLAUSE_LOCATION (c),
13944 "%qD does not have a mappable type in %qs clause", t,
13945 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13946 remove = true;
13947 }
13948 if (remove)
13949 break;
13950 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13951 {
13952 error_at (OMP_CLAUSE_LOCATION (c),
13953 "%qE appears more than once on the same "
13954 "%<declare target%> directive", t);
13955 remove = true;
13956 }
13957 else
13958 bitmap_set_bit (&generic_head, DECL_UID (t));
13959 break;
13960
13961 case OMP_CLAUSE_UNIFORM:
13962 t = OMP_CLAUSE_DECL (c);
13963 if (TREE_CODE (t) != PARM_DECL)
13964 {
13965 if (DECL_P (t))
13966 error_at (OMP_CLAUSE_LOCATION (c),
13967 "%qD is not an argument in %<uniform%> clause", t);
13968 else
13969 error_at (OMP_CLAUSE_LOCATION (c),
13970 "%qE is not an argument in %<uniform%> clause", t);
13971 remove = true;
13972 break;
13973 }
13974 /* map_head bitmap is used as uniform_head if declare_simd. */
13975 bitmap_set_bit (&map_head, DECL_UID (t));
13976 goto check_dup_generic;
13977
13978 case OMP_CLAUSE_IS_DEVICE_PTR:
13979 case OMP_CLAUSE_USE_DEVICE_PTR:
13980 t = OMP_CLAUSE_DECL (c);
13981 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13982 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13983 {
13984 error_at (OMP_CLAUSE_LOCATION (c),
13985 "%qs variable is neither a pointer nor an array",
13986 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13987 remove = true;
13988 }
13989 goto check_dup_generic;
13990
13991 case OMP_CLAUSE_NOWAIT:
13992 if (copyprivate_seen)
13993 {
13994 error_at (OMP_CLAUSE_LOCATION (c),
13995 "%<nowait%> clause must not be used together "
13996 "with %<copyprivate%>");
13997 remove = true;
13998 break;
13999 }
14000 nowait_clause = pc;
14001 pc = &OMP_CLAUSE_CHAIN (c);
14002 continue;
14003
14004 case OMP_CLAUSE_IF:
14005 case OMP_CLAUSE_NUM_THREADS:
14006 case OMP_CLAUSE_NUM_TEAMS:
14007 case OMP_CLAUSE_THREAD_LIMIT:
14008 case OMP_CLAUSE_DEFAULT:
14009 case OMP_CLAUSE_UNTIED:
14010 case OMP_CLAUSE_COLLAPSE:
14011 case OMP_CLAUSE_FINAL:
14012 case OMP_CLAUSE_MERGEABLE:
14013 case OMP_CLAUSE_DEVICE:
14014 case OMP_CLAUSE_DIST_SCHEDULE:
14015 case OMP_CLAUSE_PARALLEL:
14016 case OMP_CLAUSE_FOR:
14017 case OMP_CLAUSE_SECTIONS:
14018 case OMP_CLAUSE_TASKGROUP:
14019 case OMP_CLAUSE_PROC_BIND:
14020 case OMP_CLAUSE_PRIORITY:
14021 case OMP_CLAUSE_GRAINSIZE:
14022 case OMP_CLAUSE_NUM_TASKS:
14023 case OMP_CLAUSE_NOGROUP:
14024 case OMP_CLAUSE_THREADS:
14025 case OMP_CLAUSE_SIMD:
14026 case OMP_CLAUSE_HINT:
14027 case OMP_CLAUSE_DEFAULTMAP:
14028 case OMP_CLAUSE_NUM_GANGS:
14029 case OMP_CLAUSE_NUM_WORKERS:
14030 case OMP_CLAUSE_VECTOR_LENGTH:
14031 case OMP_CLAUSE_ASYNC:
14032 case OMP_CLAUSE_WAIT:
14033 case OMP_CLAUSE_AUTO:
14034 case OMP_CLAUSE_INDEPENDENT:
14035 case OMP_CLAUSE_SEQ:
14036 case OMP_CLAUSE_GANG:
14037 case OMP_CLAUSE_WORKER:
14038 case OMP_CLAUSE_VECTOR:
14039 case OMP_CLAUSE_TILE:
14040 case OMP_CLAUSE_IF_PRESENT:
14041 case OMP_CLAUSE_FINALIZE:
14042 pc = &OMP_CLAUSE_CHAIN (c);
14043 continue;
14044
14045 case OMP_CLAUSE_SCHEDULE:
14046 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
14047 {
14048 const char *p = NULL;
14049 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
14050 {
14051 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
14052 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
14053 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
14054 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
14055 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
14056 default: gcc_unreachable ();
14057 }
14058 if (p)
14059 {
14060 error_at (OMP_CLAUSE_LOCATION (c),
14061 "%<nonmonotonic%> modifier specified for %qs "
14062 "schedule kind", p);
14063 OMP_CLAUSE_SCHEDULE_KIND (c)
14064 = (enum omp_clause_schedule_kind)
14065 (OMP_CLAUSE_SCHEDULE_KIND (c)
14066 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14067 }
14068 }
14069 schedule_clause = c;
14070 pc = &OMP_CLAUSE_CHAIN (c);
14071 continue;
14072
14073 case OMP_CLAUSE_ORDERED:
14074 ordered_seen = true;
14075 pc = &OMP_CLAUSE_CHAIN (c);
14076 continue;
14077
14078 case OMP_CLAUSE_SAFELEN:
14079 safelen = c;
14080 pc = &OMP_CLAUSE_CHAIN (c);
14081 continue;
14082 case OMP_CLAUSE_SIMDLEN:
14083 simdlen = c;
14084 pc = &OMP_CLAUSE_CHAIN (c);
14085 continue;
14086
14087 case OMP_CLAUSE_INBRANCH:
14088 case OMP_CLAUSE_NOTINBRANCH:
14089 if (branch_seen)
14090 {
14091 error_at (OMP_CLAUSE_LOCATION (c),
14092 "%<inbranch%> clause is incompatible with "
14093 "%<notinbranch%>");
14094 remove = true;
14095 break;
14096 }
14097 branch_seen = true;
14098 pc = &OMP_CLAUSE_CHAIN (c);
14099 continue;
14100
14101 default:
14102 gcc_unreachable ();
14103 }
14104
14105 if (!remove)
14106 {
14107 t = OMP_CLAUSE_DECL (c);
14108
14109 if (need_complete)
14110 {
14111 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14112 if (t == error_mark_node)
14113 remove = true;
14114 }
14115
14116 if (need_implicitly_determined)
14117 {
14118 const char *share_name = NULL;
14119
14120 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14121 share_name = "threadprivate";
14122 else switch (c_omp_predetermined_sharing (t))
14123 {
14124 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
14125 break;
14126 case OMP_CLAUSE_DEFAULT_SHARED:
14127 /* const vars may be specified in firstprivate clause. */
14128 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
14129 && TREE_READONLY (t))
14130 break;
14131 share_name = "shared";
14132 break;
14133 case OMP_CLAUSE_DEFAULT_PRIVATE:
14134 share_name = "private";
14135 break;
14136 default:
14137 gcc_unreachable ();
14138 }
14139 if (share_name)
14140 {
14141 error_at (OMP_CLAUSE_LOCATION (c),
14142 "%qE is predetermined %qs for %qs",
14143 t, share_name,
14144 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14145 remove = true;
14146 }
14147 }
14148 }
14149
14150 if (remove)
14151 *pc = OMP_CLAUSE_CHAIN (c);
14152 else
14153 pc = &OMP_CLAUSE_CHAIN (c);
14154 }
14155
14156 if (simdlen
14157 && safelen
14158 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
14159 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
14160 {
14161 error_at (OMP_CLAUSE_LOCATION (simdlen),
14162 "%<simdlen%> clause value is bigger than "
14163 "%<safelen%> clause value");
14164 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
14165 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
14166 }
14167
14168 if (ordered_seen
14169 && schedule_clause
14170 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14171 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14172 {
14173 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
14174 "%<nonmonotonic%> schedule modifier specified together "
14175 "with %<ordered%> clause");
14176 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14177 = (enum omp_clause_schedule_kind)
14178 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14179 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14180 }
14181
14182 if (linear_variable_step_check)
14183 for (pc = &clauses, c = clauses; c ; c = *pc)
14184 {
14185 bool remove = false;
14186 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14187 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
14188 && !bitmap_bit_p (&map_head,
14189 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
14190 {
14191 error_at (OMP_CLAUSE_LOCATION (c),
14192 "%<linear%> clause step is a parameter %qD not "
14193 "specified in %<uniform%> clause",
14194 OMP_CLAUSE_LINEAR_STEP (c));
14195 remove = true;
14196 }
14197
14198 if (remove)
14199 *pc = OMP_CLAUSE_CHAIN (c);
14200 else
14201 pc = &OMP_CLAUSE_CHAIN (c);
14202 }
14203
14204 bitmap_obstack_release (NULL);
14205 return clauses;
14206 }
14207
14208 /* Return code to initialize DST with a copy constructor from SRC.
14209 C doesn't have copy constructors nor assignment operators, only for
14210 _Atomic vars we need to perform __atomic_load from src into a temporary
14211 followed by __atomic_store of the temporary to dst. */
14212
14213 tree
14214 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
14215 {
14216 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
14217 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14218
14219 location_t loc = OMP_CLAUSE_LOCATION (clause);
14220 tree type = TREE_TYPE (dst);
14221 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
14222 tree tmp = create_tmp_var (nonatomic_type);
14223 tree tmp_addr = build_fold_addr_expr (tmp);
14224 TREE_ADDRESSABLE (tmp) = 1;
14225 TREE_NO_WARNING (tmp) = 1;
14226 tree src_addr = build_fold_addr_expr (src);
14227 tree dst_addr = build_fold_addr_expr (dst);
14228 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14229 vec<tree, va_gc> *params;
14230 /* Expansion of a generic atomic load may require an addition
14231 element, so allocate enough to prevent a resize. */
14232 vec_alloc (params, 4);
14233
14234 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14235 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14236 params->quick_push (src_addr);
14237 params->quick_push (tmp_addr);
14238 params->quick_push (seq_cst);
14239 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14240
14241 vec_alloc (params, 4);
14242
14243 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14244 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14245 params->quick_push (dst_addr);
14246 params->quick_push (tmp_addr);
14247 params->quick_push (seq_cst);
14248 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14249 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14250 }
14251
14252 /* Create a transaction node. */
14253
14254 tree
14255 c_finish_transaction (location_t loc, tree block, int flags)
14256 {
14257 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14258 if (flags & TM_STMT_ATTR_OUTER)
14259 TRANSACTION_EXPR_OUTER (stmt) = 1;
14260 if (flags & TM_STMT_ATTR_RELAXED)
14261 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14262 return add_stmt (stmt);
14263 }
14264
14265 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14266 down to the element type of an array. If ORIG_QUAL_TYPE is not
14267 NULL, then it should be used as the qualified type
14268 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14269 preserve information about the typedef name from which an array
14270 type was derived). */
14271
14272 tree
14273 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14274 size_t orig_qual_indirect)
14275 {
14276 if (type == error_mark_node)
14277 return type;
14278
14279 if (TREE_CODE (type) == ARRAY_TYPE)
14280 {
14281 tree t;
14282 tree element_type = c_build_qualified_type (TREE_TYPE (type),
14283 type_quals, orig_qual_type,
14284 orig_qual_indirect - 1);
14285
14286 /* See if we already have an identically qualified type. */
14287 if (orig_qual_type && orig_qual_indirect == 0)
14288 t = orig_qual_type;
14289 else
14290 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14291 {
14292 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14293 && TYPE_NAME (t) == TYPE_NAME (type)
14294 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14295 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14296 TYPE_ATTRIBUTES (type)))
14297 break;
14298 }
14299 if (!t)
14300 {
14301 tree domain = TYPE_DOMAIN (type);
14302
14303 t = build_variant_type_copy (type);
14304 TREE_TYPE (t) = element_type;
14305
14306 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14307 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14308 SET_TYPE_STRUCTURAL_EQUALITY (t);
14309 else if (TYPE_CANONICAL (element_type) != element_type
14310 || (domain && TYPE_CANONICAL (domain) != domain))
14311 {
14312 tree unqualified_canon
14313 = build_array_type (TYPE_CANONICAL (element_type),
14314 domain? TYPE_CANONICAL (domain)
14315 : NULL_TREE);
14316 if (TYPE_REVERSE_STORAGE_ORDER (type))
14317 {
14318 unqualified_canon
14319 = build_distinct_type_copy (unqualified_canon);
14320 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14321 }
14322 TYPE_CANONICAL (t)
14323 = c_build_qualified_type (unqualified_canon, type_quals);
14324 }
14325 else
14326 TYPE_CANONICAL (t) = t;
14327 }
14328 return t;
14329 }
14330
14331 /* A restrict-qualified pointer type must be a pointer to object or
14332 incomplete type. Note that the use of POINTER_TYPE_P also allows
14333 REFERENCE_TYPEs, which is appropriate for C++. */
14334 if ((type_quals & TYPE_QUAL_RESTRICT)
14335 && (!POINTER_TYPE_P (type)
14336 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14337 {
14338 error ("invalid use of %<restrict%>");
14339 type_quals &= ~TYPE_QUAL_RESTRICT;
14340 }
14341
14342 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14343 ? orig_qual_type
14344 : build_qualified_type (type, type_quals));
14345 /* A variant type does not inherit the list of incomplete vars from the
14346 type main variant. */
14347 if (RECORD_OR_UNION_TYPE_P (var_type)
14348 && TYPE_MAIN_VARIANT (var_type) != var_type)
14349 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14350 return var_type;
14351 }
14352
14353 /* Build a VA_ARG_EXPR for the C parser. */
14354
14355 tree
14356 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14357 {
14358 if (error_operand_p (type))
14359 return error_mark_node;
14360 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14361 order because it takes the address of the expression. */
14362 else if (handled_component_p (expr)
14363 && reverse_storage_order_for_component_p (expr))
14364 {
14365 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14366 return error_mark_node;
14367 }
14368 else if (!COMPLETE_TYPE_P (type))
14369 {
14370 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14371 "type %qT", type);
14372 return error_mark_node;
14373 }
14374 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14375 warning_at (loc2, OPT_Wc___compat,
14376 "C++ requires promoted type, not enum type, in %<va_arg%>");
14377 return build_va_arg (loc2, expr, type);
14378 }
14379
14380 /* Return truthvalue of whether T1 is the same tree structure as T2.
14381 Return 1 if they are the same. Return false if they are different. */
14382
14383 bool
14384 c_tree_equal (tree t1, tree t2)
14385 {
14386 enum tree_code code1, code2;
14387
14388 if (t1 == t2)
14389 return true;
14390 if (!t1 || !t2)
14391 return false;
14392
14393 for (code1 = TREE_CODE (t1);
14394 CONVERT_EXPR_CODE_P (code1)
14395 || code1 == NON_LVALUE_EXPR;
14396 code1 = TREE_CODE (t1))
14397 t1 = TREE_OPERAND (t1, 0);
14398 for (code2 = TREE_CODE (t2);
14399 CONVERT_EXPR_CODE_P (code2)
14400 || code2 == NON_LVALUE_EXPR;
14401 code2 = TREE_CODE (t2))
14402 t2 = TREE_OPERAND (t2, 0);
14403
14404 /* They might have become equal now. */
14405 if (t1 == t2)
14406 return true;
14407
14408 if (code1 != code2)
14409 return false;
14410
14411 switch (code1)
14412 {
14413 case INTEGER_CST:
14414 return wi::to_wide (t1) == wi::to_wide (t2);
14415
14416 case REAL_CST:
14417 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
14418
14419 case STRING_CST:
14420 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14421 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14422 TREE_STRING_LENGTH (t1));
14423
14424 case FIXED_CST:
14425 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14426 TREE_FIXED_CST (t2));
14427
14428 case COMPLEX_CST:
14429 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14430 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14431
14432 case VECTOR_CST:
14433 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14434
14435 case CONSTRUCTOR:
14436 /* We need to do this when determining whether or not two
14437 non-type pointer to member function template arguments
14438 are the same. */
14439 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14440 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14441 return false;
14442 {
14443 tree field, value;
14444 unsigned int i;
14445 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14446 {
14447 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14448 if (!c_tree_equal (field, elt2->index)
14449 || !c_tree_equal (value, elt2->value))
14450 return false;
14451 }
14452 }
14453 return true;
14454
14455 case TREE_LIST:
14456 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14457 return false;
14458 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14459 return false;
14460 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14461
14462 case SAVE_EXPR:
14463 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14464
14465 case CALL_EXPR:
14466 {
14467 tree arg1, arg2;
14468 call_expr_arg_iterator iter1, iter2;
14469 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14470 return false;
14471 for (arg1 = first_call_expr_arg (t1, &iter1),
14472 arg2 = first_call_expr_arg (t2, &iter2);
14473 arg1 && arg2;
14474 arg1 = next_call_expr_arg (&iter1),
14475 arg2 = next_call_expr_arg (&iter2))
14476 if (!c_tree_equal (arg1, arg2))
14477 return false;
14478 if (arg1 || arg2)
14479 return false;
14480 return true;
14481 }
14482
14483 case TARGET_EXPR:
14484 {
14485 tree o1 = TREE_OPERAND (t1, 0);
14486 tree o2 = TREE_OPERAND (t2, 0);
14487
14488 /* Special case: if either target is an unallocated VAR_DECL,
14489 it means that it's going to be unified with whatever the
14490 TARGET_EXPR is really supposed to initialize, so treat it
14491 as being equivalent to anything. */
14492 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
14493 && !DECL_RTL_SET_P (o1))
14494 /*Nop*/;
14495 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
14496 && !DECL_RTL_SET_P (o2))
14497 /*Nop*/;
14498 else if (!c_tree_equal (o1, o2))
14499 return false;
14500
14501 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14502 }
14503
14504 case COMPONENT_REF:
14505 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14506 return false;
14507 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14508
14509 case PARM_DECL:
14510 case VAR_DECL:
14511 case CONST_DECL:
14512 case FIELD_DECL:
14513 case FUNCTION_DECL:
14514 case IDENTIFIER_NODE:
14515 case SSA_NAME:
14516 return false;
14517
14518 case TREE_VEC:
14519 {
14520 unsigned ix;
14521 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14522 return false;
14523 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14524 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14525 TREE_VEC_ELT (t2, ix)))
14526 return false;
14527 return true;
14528 }
14529
14530 default:
14531 break;
14532 }
14533
14534 switch (TREE_CODE_CLASS (code1))
14535 {
14536 case tcc_unary:
14537 case tcc_binary:
14538 case tcc_comparison:
14539 case tcc_expression:
14540 case tcc_vl_exp:
14541 case tcc_reference:
14542 case tcc_statement:
14543 {
14544 int i, n = TREE_OPERAND_LENGTH (t1);
14545
14546 switch (code1)
14547 {
14548 case PREINCREMENT_EXPR:
14549 case PREDECREMENT_EXPR:
14550 case POSTINCREMENT_EXPR:
14551 case POSTDECREMENT_EXPR:
14552 n = 1;
14553 break;
14554 case ARRAY_REF:
14555 n = 2;
14556 break;
14557 default:
14558 break;
14559 }
14560
14561 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14562 && n != TREE_OPERAND_LENGTH (t2))
14563 return false;
14564
14565 for (i = 0; i < n; ++i)
14566 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14567 return false;
14568
14569 return true;
14570 }
14571
14572 case tcc_type:
14573 return comptypes (t1, t2);
14574 default:
14575 gcc_unreachable ();
14576 }
14577 /* We can get here with --disable-checking. */
14578 return false;
14579 }
14580
14581 /* Returns true when the function declaration FNDECL is implicit,
14582 introduced as a result of a call to an otherwise undeclared
14583 function, and false otherwise. */
14584
14585 bool
14586 c_decl_implicit (const_tree fndecl)
14587 {
14588 return C_DECL_IMPLICIT (fndecl);
14589 }