]> gcc.gnu.org Git - gcc.git/blob - gcc/c/c-typeck.cc
openmp: Add support for OpenMP 5.2 linear clause syntax for C/C++
[gcc.git] / gcc / c / c-typeck.cc
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2022 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 conversions. Used to select diagnostic messages
57 and control folding initializers in convert_for_assignment. */
58 enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_init_const,
63 ic_return
64 };
65
66 /* The level of nesting inside "__alignof__". */
67 int in_alignof;
68
69 /* The level of nesting inside "sizeof". */
70 int in_sizeof;
71
72 /* The level of nesting inside "typeof". */
73 int in_typeof;
74
75 /* True when parsing OpenMP loop expressions. */
76 bool c_in_omp_for;
77
78 /* The argument of last parsed sizeof expression, only to be tested
79 if expr.original_code == SIZEOF_EXPR. */
80 tree c_last_sizeof_arg;
81 location_t c_last_sizeof_loc;
82
83 /* Nonzero if we might need to print a "missing braces around
84 initializer" message within this initializer. */
85 static int found_missing_braces;
86
87 static int require_constant_value;
88 static int require_constant_elements;
89
90 static bool null_pointer_constant_p (const_tree);
91 static tree qualify_type (tree, tree);
92 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
93 bool *);
94 static int comp_target_types (location_t, tree, tree);
95 static int function_types_compatible_p (const_tree, const_tree, bool *,
96 bool *);
97 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
98 static tree lookup_field (tree, tree);
99 static int convert_arguments (location_t, vec<location_t>, tree,
100 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
101 tree);
102 static tree pointer_diff (location_t, tree, tree, tree *);
103 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
104 enum impl_conv, bool, tree, tree, int,
105 int = 0);
106 static tree valid_compound_expr_initializer (tree, tree);
107 static void push_string (const char *);
108 static void push_member_name (tree);
109 static int spelling_length (void);
110 static char *print_spelling (char *);
111 static void warning_init (location_t, int, const char *);
112 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
113 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
114 bool, struct obstack *);
115 static void output_pending_init_elements (int, struct obstack *);
116 static bool set_designator (location_t, bool, struct obstack *);
117 static void push_range_stack (tree, struct obstack *);
118 static void add_pending_init (location_t, tree, tree, tree, bool,
119 struct obstack *);
120 static void set_nonincremental_init (struct obstack *);
121 static void set_nonincremental_init_from_string (tree, struct obstack *);
122 static tree find_init_member (tree, struct obstack *);
123 static void readonly_warning (tree, enum lvalue_use);
124 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
125 static void record_maybe_used_decl (tree);
126 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
127 \f
128 /* Return true if EXP is a null pointer constant, false otherwise. */
129
130 static bool
131 null_pointer_constant_p (const_tree expr)
132 {
133 /* This should really operate on c_expr structures, but they aren't
134 yet available everywhere required. */
135 tree type = TREE_TYPE (expr);
136 return (TREE_CODE (expr) == INTEGER_CST
137 && !TREE_OVERFLOW (expr)
138 && integer_zerop (expr)
139 && (INTEGRAL_TYPE_P (type)
140 || (TREE_CODE (type) == POINTER_TYPE
141 && VOID_TYPE_P (TREE_TYPE (type))
142 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
143 }
144
145 /* EXPR may appear in an unevaluated part of an integer constant
146 expression, but not in an evaluated part. Wrap it in a
147 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
148 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
149
150 static tree
151 note_integer_operands (tree expr)
152 {
153 tree ret;
154 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
155 {
156 ret = copy_node (expr);
157 TREE_OVERFLOW (ret) = 1;
158 }
159 else
160 {
161 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
162 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
163 }
164 return ret;
165 }
166
167 /* Having checked whether EXPR may appear in an unevaluated part of an
168 integer constant expression and found that it may, remove any
169 C_MAYBE_CONST_EXPR noting this fact and return the resulting
170 expression. */
171
172 static inline tree
173 remove_c_maybe_const_expr (tree expr)
174 {
175 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
176 return C_MAYBE_CONST_EXPR_EXPR (expr);
177 else
178 return expr;
179 }
180
181 \f/* This is a cache to hold if two types are compatible or not. */
182
183 struct tagged_tu_seen_cache {
184 const struct tagged_tu_seen_cache * next;
185 const_tree t1;
186 const_tree t2;
187 /* The return value of tagged_types_tu_compatible_p if we had seen
188 these two types already. */
189 int val;
190 };
191
192 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
193 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
194
195 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
196 does not have an incomplete type. (That includes void types.)
197 LOC is the location of the use. */
198
199 tree
200 require_complete_type (location_t loc, tree value)
201 {
202 tree type = TREE_TYPE (value);
203
204 if (error_operand_p (value))
205 return error_mark_node;
206
207 /* First, detect a valid value with a complete type. */
208 if (COMPLETE_TYPE_P (type))
209 return value;
210
211 c_incomplete_type_error (loc, value, type);
212 return error_mark_node;
213 }
214
215 /* Print an error message for invalid use of an incomplete type.
216 VALUE is the expression that was used (or 0 if that isn't known)
217 and TYPE is the type that was invalid. LOC is the location for
218 the error. */
219
220 void
221 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
222 {
223 /* Avoid duplicate error message. */
224 if (TREE_CODE (type) == ERROR_MARK)
225 return;
226
227 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
228 error_at (loc, "%qD has an incomplete type %qT", value, type);
229 else
230 {
231 retry:
232 /* We must print an error message. Be clever about what it says. */
233
234 switch (TREE_CODE (type))
235 {
236 case RECORD_TYPE:
237 case UNION_TYPE:
238 case ENUMERAL_TYPE:
239 break;
240
241 case VOID_TYPE:
242 error_at (loc, "invalid use of void expression");
243 return;
244
245 case ARRAY_TYPE:
246 if (TYPE_DOMAIN (type))
247 {
248 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
249 {
250 error_at (loc, "invalid use of flexible array member");
251 return;
252 }
253 type = TREE_TYPE (type);
254 goto retry;
255 }
256 error_at (loc, "invalid use of array with unspecified bounds");
257 return;
258
259 default:
260 gcc_unreachable ();
261 }
262
263 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
264 error_at (loc, "invalid use of undefined type %qT", type);
265 else
266 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
267 error_at (loc, "invalid use of incomplete typedef %qT", type);
268 }
269 }
270
271 /* Given a type, apply default promotions wrt unnamed function
272 arguments and return the new type. */
273
274 tree
275 c_type_promotes_to (tree type)
276 {
277 tree ret = NULL_TREE;
278
279 if (TYPE_MAIN_VARIANT (type) == float_type_node)
280 ret = double_type_node;
281 else if (c_promoting_integer_type_p (type))
282 {
283 /* Preserve unsignedness if not really getting any wider. */
284 if (TYPE_UNSIGNED (type)
285 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
286 ret = unsigned_type_node;
287 else
288 ret = integer_type_node;
289 }
290
291 if (ret != NULL_TREE)
292 return (TYPE_ATOMIC (type)
293 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
294 : ret);
295
296 return type;
297 }
298
299 /* Return true if between two named address spaces, whether there is a superset
300 named address space that encompasses both address spaces. If there is a
301 superset, return which address space is the superset. */
302
303 static bool
304 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
305 {
306 if (as1 == as2)
307 {
308 *common = as1;
309 return true;
310 }
311 else if (targetm.addr_space.subset_p (as1, as2))
312 {
313 *common = as2;
314 return true;
315 }
316 else if (targetm.addr_space.subset_p (as2, as1))
317 {
318 *common = as1;
319 return true;
320 }
321 else
322 return false;
323 }
324
325 /* Return a variant of TYPE which has all the type qualifiers of LIKE
326 as well as those of TYPE. */
327
328 static tree
329 qualify_type (tree type, tree like)
330 {
331 addr_space_t as_type = TYPE_ADDR_SPACE (type);
332 addr_space_t as_like = TYPE_ADDR_SPACE (like);
333 addr_space_t as_common;
334
335 /* If the two named address spaces are different, determine the common
336 superset address space. If there isn't one, raise an error. */
337 if (!addr_space_superset (as_type, as_like, &as_common))
338 {
339 as_common = as_type;
340 error ("%qT and %qT are in disjoint named address spaces",
341 type, like);
342 }
343
344 return c_build_qualified_type (type,
345 TYPE_QUALS_NO_ADDR_SPACE (type)
346 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
347 | ENCODE_QUAL_ADDR_SPACE (as_common));
348 }
349
350 /* Return true iff the given tree T is a variable length array. */
351
352 bool
353 c_vla_type_p (const_tree t)
354 {
355 if (TREE_CODE (t) == ARRAY_TYPE
356 && C_TYPE_VARIABLE_SIZE (t))
357 return true;
358 return false;
359 }
360
361 /* If NTYPE is a type of a non-variadic function with a prototype
362 and OTYPE is a type of a function without a prototype and ATTRS
363 contains attribute format, diagnosess and removes it from ATTRS.
364 Returns the result of build_type_attribute_variant of NTYPE and
365 the (possibly) modified ATTRS. */
366
367 static tree
368 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
369 {
370 if (!prototype_p (otype)
371 && prototype_p (ntype)
372 && lookup_attribute ("format", attrs))
373 {
374 warning_at (input_location, OPT_Wattributes,
375 "%qs attribute cannot be applied to a function that "
376 "does not take variable arguments", "format");
377 attrs = remove_attribute ("format", attrs);
378 }
379 return build_type_attribute_variant (ntype, attrs);
380
381 }
382 /* Return the composite type of two compatible types.
383
384 We assume that comptypes has already been done and returned
385 nonzero; if that isn't so, this may crash. In particular, we
386 assume that qualifiers match. */
387
388 tree
389 composite_type (tree t1, tree t2)
390 {
391 enum tree_code code1;
392 enum tree_code code2;
393 tree attributes;
394
395 /* Save time if the two types are the same. */
396
397 if (t1 == t2) return t1;
398
399 /* If one type is nonsense, use the other. */
400 if (t1 == error_mark_node)
401 return t2;
402 if (t2 == error_mark_node)
403 return t1;
404
405 code1 = TREE_CODE (t1);
406 code2 = TREE_CODE (t2);
407
408 /* Merge the attributes. */
409 attributes = targetm.merge_type_attributes (t1, t2);
410
411 /* If one is an enumerated type and the other is the compatible
412 integer type, the composite type might be either of the two
413 (DR#013 question 3). For consistency, use the enumerated type as
414 the composite type. */
415
416 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
417 return t1;
418 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
419 return t2;
420
421 gcc_assert (code1 == code2);
422
423 switch (code1)
424 {
425 case POINTER_TYPE:
426 /* For two pointers, do this recursively on the target type. */
427 {
428 tree pointed_to_1 = TREE_TYPE (t1);
429 tree pointed_to_2 = TREE_TYPE (t2);
430 tree target = composite_type (pointed_to_1, pointed_to_2);
431 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
432 t1 = build_type_attribute_variant (t1, attributes);
433 return qualify_type (t1, t2);
434 }
435
436 case ARRAY_TYPE:
437 {
438 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
439 int quals;
440 tree unqual_elt;
441 tree d1 = TYPE_DOMAIN (t1);
442 tree d2 = TYPE_DOMAIN (t2);
443 bool d1_variable, d2_variable;
444 bool d1_zero, d2_zero;
445 bool t1_complete, t2_complete;
446
447 /* We should not have any type quals on arrays at all. */
448 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
449 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
450
451 t1_complete = COMPLETE_TYPE_P (t1);
452 t2_complete = COMPLETE_TYPE_P (t2);
453
454 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
455 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
456
457 d1_variable = (!d1_zero
458 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
459 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
460 d2_variable = (!d2_zero
461 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
462 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
463 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
464 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
465
466 /* Save space: see if the result is identical to one of the args. */
467 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
468 && (d2_variable || d2_zero || !d1_variable))
469 return build_type_attribute_variant (t1, attributes);
470 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
471 && (d1_variable || d1_zero || !d2_variable))
472 return build_type_attribute_variant (t2, attributes);
473
474 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
475 return build_type_attribute_variant (t1, attributes);
476 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
477 return build_type_attribute_variant (t2, attributes);
478
479 /* Merge the element types, and have a size if either arg has
480 one. We may have qualifiers on the element types. To set
481 up TYPE_MAIN_VARIANT correctly, we need to form the
482 composite of the unqualified types and add the qualifiers
483 back at the end. */
484 quals = TYPE_QUALS (strip_array_types (elt));
485 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
486 t1 = build_array_type (unqual_elt,
487 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
488 && (d2_variable
489 || d2_zero
490 || !d1_variable))
491 ? t1
492 : t2));
493 /* Ensure a composite type involving a zero-length array type
494 is a zero-length type not an incomplete type. */
495 if (d1_zero && d2_zero
496 && (t1_complete || t2_complete)
497 && !COMPLETE_TYPE_P (t1))
498 {
499 TYPE_SIZE (t1) = bitsize_zero_node;
500 TYPE_SIZE_UNIT (t1) = size_zero_node;
501 }
502 t1 = c_build_qualified_type (t1, quals);
503 return build_type_attribute_variant (t1, attributes);
504 }
505
506 case ENUMERAL_TYPE:
507 case RECORD_TYPE:
508 case UNION_TYPE:
509 if (attributes != NULL)
510 {
511 /* Try harder not to create a new aggregate type. */
512 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
513 return t1;
514 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
515 return t2;
516 }
517 return build_type_attribute_variant (t1, attributes);
518
519 case FUNCTION_TYPE:
520 /* Function types: prefer the one that specified arg types.
521 If both do, merge the arg types. Also merge the return types. */
522 {
523 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
524 tree p1 = TYPE_ARG_TYPES (t1);
525 tree p2 = TYPE_ARG_TYPES (t2);
526 int len;
527 tree newargs, n;
528 int i;
529
530 /* Save space: see if the result is identical to one of the args. */
531 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
532 return build_functype_attribute_variant (t1, t2, attributes);
533 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
534 return build_functype_attribute_variant (t2, t1, attributes);
535
536 /* Simple way if one arg fails to specify argument types. */
537 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
538 {
539 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
540 t1 = build_type_attribute_variant (t1, attributes);
541 return qualify_type (t1, t2);
542 }
543 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
544 {
545 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
546 t1 = build_type_attribute_variant (t1, attributes);
547 return qualify_type (t1, t2);
548 }
549
550 /* If both args specify argument types, we must merge the two
551 lists, argument by argument. */
552
553 for (len = 0, newargs = p1;
554 newargs && newargs != void_list_node;
555 len++, newargs = TREE_CHAIN (newargs))
556 ;
557
558 for (i = 0; i < len; i++)
559 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
560
561 n = newargs;
562
563 for (; p1 && p1 != void_list_node;
564 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
565 {
566 /* A null type means arg type is not specified.
567 Take whatever the other function type has. */
568 if (TREE_VALUE (p1) == NULL_TREE)
569 {
570 TREE_VALUE (n) = TREE_VALUE (p2);
571 goto parm_done;
572 }
573 if (TREE_VALUE (p2) == NULL_TREE)
574 {
575 TREE_VALUE (n) = TREE_VALUE (p1);
576 goto parm_done;
577 }
578
579 /* Given wait (union {union wait *u; int *i} *)
580 and wait (union wait *),
581 prefer union wait * as type of parm. */
582 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
583 && TREE_VALUE (p1) != TREE_VALUE (p2))
584 {
585 tree memb;
586 tree mv2 = TREE_VALUE (p2);
587 if (mv2 && mv2 != error_mark_node
588 && TREE_CODE (mv2) != ARRAY_TYPE)
589 mv2 = TYPE_MAIN_VARIANT (mv2);
590 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
591 memb; memb = DECL_CHAIN (memb))
592 {
593 tree mv3 = TREE_TYPE (memb);
594 if (mv3 && mv3 != error_mark_node
595 && TREE_CODE (mv3) != ARRAY_TYPE)
596 mv3 = TYPE_MAIN_VARIANT (mv3);
597 if (comptypes (mv3, mv2))
598 {
599 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
600 TREE_VALUE (p2));
601 pedwarn (input_location, OPT_Wpedantic,
602 "function types not truly compatible in ISO C");
603 goto parm_done;
604 }
605 }
606 }
607 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
608 && TREE_VALUE (p2) != TREE_VALUE (p1))
609 {
610 tree memb;
611 tree mv1 = TREE_VALUE (p1);
612 if (mv1 && mv1 != error_mark_node
613 && TREE_CODE (mv1) != ARRAY_TYPE)
614 mv1 = TYPE_MAIN_VARIANT (mv1);
615 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
616 memb; memb = DECL_CHAIN (memb))
617 {
618 tree mv3 = TREE_TYPE (memb);
619 if (mv3 && mv3 != error_mark_node
620 && TREE_CODE (mv3) != ARRAY_TYPE)
621 mv3 = TYPE_MAIN_VARIANT (mv3);
622 if (comptypes (mv3, mv1))
623 {
624 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
625 TREE_VALUE (p1));
626 pedwarn (input_location, OPT_Wpedantic,
627 "function types not truly compatible in ISO C");
628 goto parm_done;
629 }
630 }
631 }
632 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
633 parm_done: ;
634 }
635
636 t1 = build_function_type (valtype, newargs);
637 t1 = qualify_type (t1, t2);
638 }
639 /* FALLTHRU */
640
641 default:
642 return build_type_attribute_variant (t1, attributes);
643 }
644
645 }
646
647 /* Return the type of a conditional expression between pointers to
648 possibly differently qualified versions of compatible types.
649
650 We assume that comp_target_types has already been done and returned
651 nonzero; if that isn't so, this may crash. */
652
653 static tree
654 common_pointer_type (tree t1, tree t2)
655 {
656 tree attributes;
657 tree pointed_to_1, mv1;
658 tree pointed_to_2, mv2;
659 tree target;
660 unsigned target_quals;
661 addr_space_t as1, as2, as_common;
662 int quals1, quals2;
663
664 /* Save time if the two types are the same. */
665
666 if (t1 == t2) return t1;
667
668 /* If one type is nonsense, use the other. */
669 if (t1 == error_mark_node)
670 return t2;
671 if (t2 == error_mark_node)
672 return t1;
673
674 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
675 && TREE_CODE (t2) == POINTER_TYPE);
676
677 /* Merge the attributes. */
678 attributes = targetm.merge_type_attributes (t1, t2);
679
680 /* Find the composite type of the target types, and combine the
681 qualifiers of the two types' targets. Do not lose qualifiers on
682 array element types by taking the TYPE_MAIN_VARIANT. */
683 mv1 = pointed_to_1 = TREE_TYPE (t1);
684 mv2 = pointed_to_2 = TREE_TYPE (t2);
685 if (TREE_CODE (mv1) != ARRAY_TYPE)
686 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
687 if (TREE_CODE (mv2) != ARRAY_TYPE)
688 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
689 target = composite_type (mv1, mv2);
690
691 /* Strip array types to get correct qualifier for pointers to arrays */
692 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
693 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
694
695 /* For function types do not merge const qualifiers, but drop them
696 if used inconsistently. The middle-end uses these to mark const
697 and noreturn functions. */
698 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
699 target_quals = (quals1 & quals2);
700 else
701 target_quals = (quals1 | quals2);
702
703 /* If the two named address spaces are different, determine the common
704 superset address space. This is guaranteed to exist due to the
705 assumption that comp_target_type returned non-zero. */
706 as1 = TYPE_ADDR_SPACE (pointed_to_1);
707 as2 = TYPE_ADDR_SPACE (pointed_to_2);
708 if (!addr_space_superset (as1, as2, &as_common))
709 gcc_unreachable ();
710
711 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
712
713 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
714 return build_type_attribute_variant (t1, attributes);
715 }
716
717 /* Return the common type for two arithmetic types under the usual
718 arithmetic conversions. The default conversions have already been
719 applied, and enumerated types converted to their compatible integer
720 types. The resulting type is unqualified and has no attributes.
721
722 This is the type for the result of most arithmetic operations
723 if the operands have the given two types. */
724
725 static tree
726 c_common_type (tree t1, tree t2)
727 {
728 enum tree_code code1;
729 enum tree_code code2;
730
731 /* If one type is nonsense, use the other. */
732 if (t1 == error_mark_node)
733 return t2;
734 if (t2 == error_mark_node)
735 return t1;
736
737 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
738 t1 = TYPE_MAIN_VARIANT (t1);
739
740 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
741 t2 = TYPE_MAIN_VARIANT (t2);
742
743 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
744 {
745 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
746 t1 = build_type_attribute_variant (t1, attrs);
747 }
748
749 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
750 {
751 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
752 t2 = build_type_attribute_variant (t2, attrs);
753 }
754
755 /* Save time if the two types are the same. */
756
757 if (t1 == t2) return t1;
758
759 code1 = TREE_CODE (t1);
760 code2 = TREE_CODE (t2);
761
762 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
763 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
764 || code1 == INTEGER_TYPE);
765 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
766 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
767 || code2 == INTEGER_TYPE);
768
769 /* When one operand is a decimal float type, the other operand cannot be
770 a generic float type or a complex type. We also disallow vector types
771 here. */
772 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
773 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
774 {
775 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
776 {
777 error ("cannot mix operands of decimal floating and vector types");
778 return error_mark_node;
779 }
780 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
781 {
782 error ("cannot mix operands of decimal floating and complex types");
783 return error_mark_node;
784 }
785 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
786 {
787 error ("cannot mix operands of decimal floating "
788 "and other floating types");
789 return error_mark_node;
790 }
791 }
792
793 /* If one type is a vector type, return that type. (How the usual
794 arithmetic conversions apply to the vector types extension is not
795 precisely specified.) */
796 if (code1 == VECTOR_TYPE)
797 return t1;
798
799 if (code2 == VECTOR_TYPE)
800 return t2;
801
802 /* If one type is complex, form the common type of the non-complex
803 components, then make that complex. Use T1 or T2 if it is the
804 required type. */
805 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
806 {
807 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
808 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
809 tree subtype = c_common_type (subtype1, subtype2);
810
811 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
812 return t1;
813 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
814 return t2;
815 else
816 return build_complex_type (subtype);
817 }
818
819 /* If only one is real, use it as the result. */
820
821 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
822 return t1;
823
824 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
825 return t2;
826
827 /* If both are real and either are decimal floating point types, use
828 the decimal floating point type with the greater precision. */
829
830 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
831 {
832 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
833 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
834 return dfloat128_type_node;
835 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
836 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
837 return dfloat64_type_node;
838 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
839 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
840 return dfloat32_type_node;
841 }
842
843 /* Deal with fixed-point types. */
844 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
845 {
846 unsigned int unsignedp = 0, satp = 0;
847 scalar_mode m1, m2;
848 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
849
850 m1 = SCALAR_TYPE_MODE (t1);
851 m2 = SCALAR_TYPE_MODE (t2);
852
853 /* If one input type is saturating, the result type is saturating. */
854 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
855 satp = 1;
856
857 /* If both fixed-point types are unsigned, the result type is unsigned.
858 When mixing fixed-point and integer types, follow the sign of the
859 fixed-point type.
860 Otherwise, the result type is signed. */
861 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
862 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
863 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
864 && TYPE_UNSIGNED (t1))
865 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
866 && TYPE_UNSIGNED (t2)))
867 unsignedp = 1;
868
869 /* The result type is signed. */
870 if (unsignedp == 0)
871 {
872 /* If the input type is unsigned, we need to convert to the
873 signed type. */
874 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
875 {
876 enum mode_class mclass = (enum mode_class) 0;
877 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
878 mclass = MODE_FRACT;
879 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
880 mclass = MODE_ACCUM;
881 else
882 gcc_unreachable ();
883 m1 = as_a <scalar_mode>
884 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
885 }
886 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
887 {
888 enum mode_class mclass = (enum mode_class) 0;
889 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
890 mclass = MODE_FRACT;
891 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
892 mclass = MODE_ACCUM;
893 else
894 gcc_unreachable ();
895 m2 = as_a <scalar_mode>
896 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
897 }
898 }
899
900 if (code1 == FIXED_POINT_TYPE)
901 {
902 fbit1 = GET_MODE_FBIT (m1);
903 ibit1 = GET_MODE_IBIT (m1);
904 }
905 else
906 {
907 fbit1 = 0;
908 /* Signed integers need to subtract one sign bit. */
909 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
910 }
911
912 if (code2 == FIXED_POINT_TYPE)
913 {
914 fbit2 = GET_MODE_FBIT (m2);
915 ibit2 = GET_MODE_IBIT (m2);
916 }
917 else
918 {
919 fbit2 = 0;
920 /* Signed integers need to subtract one sign bit. */
921 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
922 }
923
924 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
925 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
926 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
927 satp);
928 }
929
930 /* Both real or both integers; use the one with greater precision. */
931
932 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
933 return t1;
934 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
935 return t2;
936
937 /* Same precision. Prefer long longs to longs to ints when the
938 same precision, following the C99 rules on integer type rank
939 (which are equivalent to the C90 rules for C90 types). */
940
941 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
942 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
943 return long_long_unsigned_type_node;
944
945 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
946 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
947 {
948 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
949 return long_long_unsigned_type_node;
950 else
951 return long_long_integer_type_node;
952 }
953
954 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
955 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
956 return long_unsigned_type_node;
957
958 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
959 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
960 {
961 /* But preserve unsignedness from the other type,
962 since long cannot hold all the values of an unsigned int. */
963 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
964 return long_unsigned_type_node;
965 else
966 return long_integer_type_node;
967 }
968
969 /* For floating types of the same TYPE_PRECISION (which we here
970 assume means either the same set of values, or sets of values
971 neither a subset of the other, with behavior being undefined in
972 the latter case), follow the rules from TS 18661-3: prefer
973 interchange types _FloatN, then standard types long double,
974 double, float, then extended types _FloatNx. For extended types,
975 check them starting with _Float128x as that seems most consistent
976 in spirit with preferring long double to double; for interchange
977 types, also check in that order for consistency although it's not
978 possible for more than one of them to have the same
979 precision. */
980 tree mv1 = TYPE_MAIN_VARIANT (t1);
981 tree mv2 = TYPE_MAIN_VARIANT (t2);
982
983 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
984 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
985 return FLOATN_TYPE_NODE (i);
986
987 /* Likewise, prefer long double to double even if same size. */
988 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
989 return long_double_type_node;
990
991 /* Likewise, prefer double to float even if same size.
992 We got a couple of embedded targets with 32 bit doubles, and the
993 pdp11 might have 64 bit floats. */
994 if (mv1 == double_type_node || mv2 == double_type_node)
995 return double_type_node;
996
997 if (mv1 == float_type_node || mv2 == float_type_node)
998 return float_type_node;
999
1000 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1001 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1002 return FLOATNX_TYPE_NODE (i);
1003
1004 /* Otherwise prefer the unsigned one. */
1005
1006 if (TYPE_UNSIGNED (t1))
1007 return t1;
1008 else
1009 return t2;
1010 }
1011 \f
1012 /* Wrapper around c_common_type that is used by c-common.cc and other
1013 front end optimizations that remove promotions. ENUMERAL_TYPEs
1014 are allowed here and are converted to their compatible integer types.
1015 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1016 preferably a non-Boolean type as the common type. */
1017 tree
1018 common_type (tree t1, tree t2)
1019 {
1020 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1021 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1022 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1023 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1024
1025 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1026 if (TREE_CODE (t1) == BOOLEAN_TYPE
1027 && TREE_CODE (t2) == BOOLEAN_TYPE)
1028 return boolean_type_node;
1029
1030 /* If either type is BOOLEAN_TYPE, then return the other. */
1031 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1032 return t2;
1033 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1034 return t1;
1035
1036 return c_common_type (t1, t2);
1037 }
1038
1039 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1040 or various other operations. Return 2 if they are compatible
1041 but a warning may be needed if you use them together. */
1042
1043 int
1044 comptypes (tree type1, tree type2)
1045 {
1046 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1047 int val;
1048
1049 val = comptypes_internal (type1, type2, NULL, NULL);
1050 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1051
1052 return val;
1053 }
1054
1055 /* Like comptypes, but if it returns non-zero because enum and int are
1056 compatible, it sets *ENUM_AND_INT_P to true. */
1057
1058 int
1059 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1060 {
1061 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1062 int val;
1063
1064 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1065 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1066
1067 return val;
1068 }
1069
1070 /* Like comptypes, but if it returns nonzero for different types, it
1071 sets *DIFFERENT_TYPES_P to true. */
1072
1073 int
1074 comptypes_check_different_types (tree type1, tree type2,
1075 bool *different_types_p)
1076 {
1077 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1078 int val;
1079
1080 val = comptypes_internal (type1, type2, NULL, different_types_p);
1081 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1082
1083 return val;
1084 }
1085 \f
1086 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1087 or various other operations. Return 2 if they are compatible
1088 but a warning may be needed if you use them together. If
1089 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1090 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1091 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1092 NULL, and the types are compatible but different enough not to be
1093 permitted in C11 typedef redeclarations, then this sets
1094 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1095 false, but may or may not be set if the types are incompatible.
1096 This differs from comptypes, in that we don't free the seen
1097 types. */
1098
1099 static int
1100 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1101 bool *different_types_p)
1102 {
1103 const_tree t1 = type1;
1104 const_tree t2 = type2;
1105 int attrval, val;
1106
1107 /* Suppress errors caused by previously reported errors. */
1108
1109 if (t1 == t2 || !t1 || !t2
1110 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1111 return 1;
1112
1113 /* Enumerated types are compatible with integer types, but this is
1114 not transitive: two enumerated types in the same translation unit
1115 are compatible with each other only if they are the same type. */
1116
1117 if (TREE_CODE (t1) == ENUMERAL_TYPE
1118 && COMPLETE_TYPE_P (t1)
1119 && TREE_CODE (t2) != ENUMERAL_TYPE)
1120 {
1121 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1122 if (TREE_CODE (t2) != VOID_TYPE)
1123 {
1124 if (enum_and_int_p != NULL)
1125 *enum_and_int_p = true;
1126 if (different_types_p != NULL)
1127 *different_types_p = true;
1128 }
1129 }
1130 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1131 && COMPLETE_TYPE_P (t2)
1132 && TREE_CODE (t1) != ENUMERAL_TYPE)
1133 {
1134 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1135 if (TREE_CODE (t1) != VOID_TYPE)
1136 {
1137 if (enum_and_int_p != NULL)
1138 *enum_and_int_p = true;
1139 if (different_types_p != NULL)
1140 *different_types_p = true;
1141 }
1142 }
1143
1144 if (t1 == t2)
1145 return 1;
1146
1147 /* Different classes of types can't be compatible. */
1148
1149 if (TREE_CODE (t1) != TREE_CODE (t2))
1150 return 0;
1151
1152 /* Qualifiers must match. C99 6.7.3p9 */
1153
1154 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1155 return 0;
1156
1157 /* Allow for two different type nodes which have essentially the same
1158 definition. Note that we already checked for equality of the type
1159 qualifiers (just above). */
1160
1161 if (TREE_CODE (t1) != ARRAY_TYPE
1162 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1163 return 1;
1164
1165 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1166 if (!(attrval = comp_type_attributes (t1, t2)))
1167 return 0;
1168
1169 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1170 val = 0;
1171
1172 switch (TREE_CODE (t1))
1173 {
1174 case INTEGER_TYPE:
1175 case FIXED_POINT_TYPE:
1176 case REAL_TYPE:
1177 /* With these nodes, we can't determine type equivalence by
1178 looking at what is stored in the nodes themselves, because
1179 two nodes might have different TYPE_MAIN_VARIANTs but still
1180 represent the same type. For example, wchar_t and int could
1181 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1182 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1183 and are distinct types. On the other hand, int and the
1184 following typedef
1185
1186 typedef int INT __attribute((may_alias));
1187
1188 have identical properties, different TYPE_MAIN_VARIANTs, but
1189 represent the same type. The canonical type system keeps
1190 track of equivalence in this case, so we fall back on it. */
1191 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1192
1193 case POINTER_TYPE:
1194 /* Do not remove mode information. */
1195 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1196 break;
1197 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1198 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1199 enum_and_int_p, different_types_p));
1200 break;
1201
1202 case FUNCTION_TYPE:
1203 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1204 different_types_p);
1205 break;
1206
1207 case ARRAY_TYPE:
1208 {
1209 tree d1 = TYPE_DOMAIN (t1);
1210 tree d2 = TYPE_DOMAIN (t2);
1211 bool d1_variable, d2_variable;
1212 bool d1_zero, d2_zero;
1213 val = 1;
1214
1215 /* Target types must match incl. qualifiers. */
1216 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1217 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1218 enum_and_int_p,
1219 different_types_p)) == 0)
1220 return 0;
1221
1222 if (different_types_p != NULL
1223 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1224 *different_types_p = true;
1225 /* Sizes must match unless one is missing or variable. */
1226 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1227 break;
1228
1229 d1_zero = !TYPE_MAX_VALUE (d1);
1230 d2_zero = !TYPE_MAX_VALUE (d2);
1231
1232 d1_variable = (!d1_zero
1233 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1234 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1235 d2_variable = (!d2_zero
1236 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1237 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1238 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1239 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1240
1241 if (different_types_p != NULL
1242 && d1_variable != d2_variable)
1243 *different_types_p = true;
1244 if (d1_variable || d2_variable)
1245 break;
1246 if (d1_zero && d2_zero)
1247 break;
1248 if (d1_zero || d2_zero
1249 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1250 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1251 val = 0;
1252
1253 break;
1254 }
1255
1256 case ENUMERAL_TYPE:
1257 case RECORD_TYPE:
1258 case UNION_TYPE:
1259 if (val != 1 && !same_translation_unit_p (t1, t2))
1260 {
1261 tree a1 = TYPE_ATTRIBUTES (t1);
1262 tree a2 = TYPE_ATTRIBUTES (t2);
1263
1264 if (! attribute_list_contained (a1, a2)
1265 && ! attribute_list_contained (a2, a1))
1266 break;
1267
1268 if (attrval != 2)
1269 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1270 different_types_p);
1271 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1272 different_types_p);
1273 }
1274 break;
1275
1276 case VECTOR_TYPE:
1277 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1278 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1279 enum_and_int_p, different_types_p));
1280 break;
1281
1282 default:
1283 break;
1284 }
1285 return attrval == 2 && val == 1 ? 2 : val;
1286 }
1287
1288 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1289 their qualifiers, except for named address spaces. If the pointers point to
1290 different named addresses, then we must determine if one address space is a
1291 subset of the other. */
1292
1293 static int
1294 comp_target_types (location_t location, tree ttl, tree ttr)
1295 {
1296 int val;
1297 int val_ped;
1298 tree mvl = TREE_TYPE (ttl);
1299 tree mvr = TREE_TYPE (ttr);
1300 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1301 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1302 addr_space_t as_common;
1303 bool enum_and_int_p;
1304
1305 /* Fail if pointers point to incompatible address spaces. */
1306 if (!addr_space_superset (asl, asr, &as_common))
1307 return 0;
1308
1309 /* For pedantic record result of comptypes on arrays before losing
1310 qualifiers on the element type below. */
1311 val_ped = 1;
1312
1313 if (TREE_CODE (mvl) == ARRAY_TYPE
1314 && TREE_CODE (mvr) == ARRAY_TYPE)
1315 val_ped = comptypes (mvl, mvr);
1316
1317 /* Qualifiers on element types of array types that are
1318 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1319
1320 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1321 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1322 : TYPE_MAIN_VARIANT (mvl));
1323
1324 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1325 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1326 : TYPE_MAIN_VARIANT (mvr));
1327
1328 enum_and_int_p = false;
1329 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1330
1331 if (val == 1 && val_ped != 1)
1332 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1333 "in ISO C before C2X");
1334
1335 if (val == 2)
1336 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1337
1338 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1339 warning_at (location, OPT_Wc___compat,
1340 "pointer target types incompatible in C++");
1341
1342 return val;
1343 }
1344 \f
1345 /* Subroutines of `comptypes'. */
1346
1347 /* Determine whether two trees derive from the same translation unit.
1348 If the CONTEXT chain ends in a null, that tree's context is still
1349 being parsed, so if two trees have context chains ending in null,
1350 they're in the same translation unit. */
1351
1352 bool
1353 same_translation_unit_p (const_tree t1, const_tree t2)
1354 {
1355 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1356 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1357 {
1358 case tcc_declaration:
1359 t1 = DECL_CONTEXT (t1); break;
1360 case tcc_type:
1361 t1 = TYPE_CONTEXT (t1); break;
1362 case tcc_exceptional:
1363 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1364 default: gcc_unreachable ();
1365 }
1366
1367 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1368 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1369 {
1370 case tcc_declaration:
1371 t2 = DECL_CONTEXT (t2); break;
1372 case tcc_type:
1373 t2 = TYPE_CONTEXT (t2); break;
1374 case tcc_exceptional:
1375 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1376 default: gcc_unreachable ();
1377 }
1378
1379 return t1 == t2;
1380 }
1381
1382 /* Allocate the seen two types, assuming that they are compatible. */
1383
1384 static struct tagged_tu_seen_cache *
1385 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1386 {
1387 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1388 tu->next = tagged_tu_seen_base;
1389 tu->t1 = t1;
1390 tu->t2 = t2;
1391
1392 tagged_tu_seen_base = tu;
1393
1394 /* The C standard says that two structures in different translation
1395 units are compatible with each other only if the types of their
1396 fields are compatible (among other things). We assume that they
1397 are compatible until proven otherwise when building the cache.
1398 An example where this can occur is:
1399 struct a
1400 {
1401 struct a *next;
1402 };
1403 If we are comparing this against a similar struct in another TU,
1404 and did not assume they were compatible, we end up with an infinite
1405 loop. */
1406 tu->val = 1;
1407 return tu;
1408 }
1409
1410 /* Free the seen types until we get to TU_TIL. */
1411
1412 static void
1413 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1414 {
1415 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1416 while (tu != tu_til)
1417 {
1418 const struct tagged_tu_seen_cache *const tu1
1419 = (const struct tagged_tu_seen_cache *) tu;
1420 tu = tu1->next;
1421 XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1422 }
1423 tagged_tu_seen_base = tu_til;
1424 }
1425
1426 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1427 compatible. If the two types are not the same (which has been
1428 checked earlier), this can only happen when multiple translation
1429 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1430 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1431 comptypes_internal. */
1432
1433 static int
1434 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1435 bool *enum_and_int_p, bool *different_types_p)
1436 {
1437 tree s1, s2;
1438 bool needs_warning = false;
1439
1440 /* We have to verify that the tags of the types are the same. This
1441 is harder than it looks because this may be a typedef, so we have
1442 to go look at the original type. It may even be a typedef of a
1443 typedef...
1444 In the case of compiler-created builtin structs the TYPE_DECL
1445 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1446 while (TYPE_NAME (t1)
1447 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1448 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1449 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1450
1451 while (TYPE_NAME (t2)
1452 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1453 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1454 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1455
1456 /* C90 didn't have the requirement that the two tags be the same. */
1457 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1458 return 0;
1459
1460 /* C90 didn't say what happened if one or both of the types were
1461 incomplete; we choose to follow C99 rules here, which is that they
1462 are compatible. */
1463 if (TYPE_SIZE (t1) == NULL
1464 || TYPE_SIZE (t2) == NULL)
1465 return 1;
1466
1467 {
1468 const struct tagged_tu_seen_cache * tts_i;
1469 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1470 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1471 return tts_i->val;
1472 }
1473
1474 switch (TREE_CODE (t1))
1475 {
1476 case ENUMERAL_TYPE:
1477 {
1478 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1479 /* Speed up the case where the type values are in the same order. */
1480 tree tv1 = TYPE_VALUES (t1);
1481 tree tv2 = TYPE_VALUES (t2);
1482
1483 if (tv1 == tv2)
1484 {
1485 return 1;
1486 }
1487
1488 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1489 {
1490 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1491 break;
1492 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1493 {
1494 tu->val = 0;
1495 return 0;
1496 }
1497 }
1498
1499 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1500 {
1501 return 1;
1502 }
1503 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1504 {
1505 tu->val = 0;
1506 return 0;
1507 }
1508
1509 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1510 {
1511 tu->val = 0;
1512 return 0;
1513 }
1514
1515 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1516 {
1517 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1518 if (s2 == NULL
1519 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1520 {
1521 tu->val = 0;
1522 return 0;
1523 }
1524 }
1525 return 1;
1526 }
1527
1528 case UNION_TYPE:
1529 {
1530 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1531 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1532 {
1533 tu->val = 0;
1534 return 0;
1535 }
1536
1537 /* Speed up the common case where the fields are in the same order. */
1538 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1539 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1540 {
1541 int result;
1542
1543 if (DECL_NAME (s1) != DECL_NAME (s2))
1544 break;
1545 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1546 enum_and_int_p, different_types_p);
1547
1548 if (result != 1 && !DECL_NAME (s1))
1549 break;
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 {
1562 tu->val = 0;
1563 return 0;
1564 }
1565 }
1566 if (!s1 && !s2)
1567 {
1568 tu->val = needs_warning ? 2 : 1;
1569 return tu->val;
1570 }
1571
1572 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1573 {
1574 bool ok = false;
1575
1576 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1577 if (DECL_NAME (s1) == DECL_NAME (s2))
1578 {
1579 int result;
1580
1581 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1582 enum_and_int_p,
1583 different_types_p);
1584
1585 if (result != 1 && !DECL_NAME (s1))
1586 continue;
1587 if (result == 0)
1588 {
1589 tu->val = 0;
1590 return 0;
1591 }
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 ok = true;
1601 break;
1602 }
1603 if (!ok)
1604 {
1605 tu->val = 0;
1606 return 0;
1607 }
1608 }
1609 tu->val = needs_warning ? 2 : 10;
1610 return tu->val;
1611 }
1612
1613 case RECORD_TYPE:
1614 {
1615 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1616
1617 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1618 s1 && s2;
1619 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1620 {
1621 int result;
1622 if (TREE_CODE (s1) != TREE_CODE (s2)
1623 || DECL_NAME (s1) != DECL_NAME (s2))
1624 break;
1625 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1626 enum_and_int_p, different_types_p);
1627 if (result == 0)
1628 break;
1629 if (result == 2)
1630 needs_warning = true;
1631
1632 if (TREE_CODE (s1) == FIELD_DECL
1633 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1634 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1635 break;
1636 }
1637 if (s1 && s2)
1638 tu->val = 0;
1639 else
1640 tu->val = needs_warning ? 2 : 1;
1641 return tu->val;
1642 }
1643
1644 default:
1645 gcc_unreachable ();
1646 }
1647 }
1648
1649 /* Return 1 if two function types F1 and F2 are compatible.
1650 If either type specifies no argument types,
1651 the other must specify a fixed number of self-promoting arg types.
1652 Otherwise, if one type specifies only the number of arguments,
1653 the other must specify that number of self-promoting arg types.
1654 Otherwise, the argument types must match.
1655 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1656
1657 static int
1658 function_types_compatible_p (const_tree f1, const_tree f2,
1659 bool *enum_and_int_p, bool *different_types_p)
1660 {
1661 tree args1, args2;
1662 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1663 int val = 1;
1664 int val1;
1665 tree ret1, ret2;
1666
1667 ret1 = TREE_TYPE (f1);
1668 ret2 = TREE_TYPE (f2);
1669
1670 /* 'volatile' qualifiers on a function's return type used to mean
1671 the function is noreturn. */
1672 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1673 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1674 if (TYPE_VOLATILE (ret1))
1675 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1676 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1677 if (TYPE_VOLATILE (ret2))
1678 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1679 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1680 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1681 if (val == 0)
1682 return 0;
1683
1684 args1 = TYPE_ARG_TYPES (f1);
1685 args2 = TYPE_ARG_TYPES (f2);
1686
1687 if (different_types_p != NULL
1688 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1689 *different_types_p = true;
1690
1691 /* An unspecified parmlist matches any specified parmlist
1692 whose argument types don't need default promotions. */
1693
1694 if (args1 == NULL_TREE)
1695 {
1696 if (!self_promoting_args_p (args2))
1697 return 0;
1698 /* If one of these types comes from a non-prototype fn definition,
1699 compare that with the other type's arglist.
1700 If they don't match, ask for a warning (but no error). */
1701 if (TYPE_ACTUAL_ARG_TYPES (f1)
1702 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1703 enum_and_int_p, different_types_p) != 1)
1704 val = 2;
1705 return val;
1706 }
1707 if (args2 == NULL_TREE)
1708 {
1709 if (!self_promoting_args_p (args1))
1710 return 0;
1711 if (TYPE_ACTUAL_ARG_TYPES (f2)
1712 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1713 enum_and_int_p, different_types_p) != 1)
1714 val = 2;
1715 return val;
1716 }
1717
1718 /* Both types have argument lists: compare them and propagate results. */
1719 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1720 different_types_p);
1721 return val1 != 1 ? val1 : val;
1722 }
1723
1724 /* Check two lists of types for compatibility, returning 0 for
1725 incompatible, 1 for compatible, or 2 for compatible with
1726 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1727 comptypes_internal. */
1728
1729 static int
1730 type_lists_compatible_p (const_tree args1, const_tree args2,
1731 bool *enum_and_int_p, bool *different_types_p)
1732 {
1733 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1734 int val = 1;
1735 int newval = 0;
1736
1737 while (1)
1738 {
1739 tree a1, mv1, a2, mv2;
1740 if (args1 == NULL_TREE && args2 == NULL_TREE)
1741 return val;
1742 /* If one list is shorter than the other,
1743 they fail to match. */
1744 if (args1 == NULL_TREE || args2 == NULL_TREE)
1745 return 0;
1746 mv1 = a1 = TREE_VALUE (args1);
1747 mv2 = a2 = TREE_VALUE (args2);
1748 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1749 mv1 = (TYPE_ATOMIC (mv1)
1750 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1751 TYPE_QUAL_ATOMIC)
1752 : TYPE_MAIN_VARIANT (mv1));
1753 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1754 mv2 = (TYPE_ATOMIC (mv2)
1755 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1756 TYPE_QUAL_ATOMIC)
1757 : TYPE_MAIN_VARIANT (mv2));
1758 /* A null pointer instead of a type
1759 means there is supposed to be an argument
1760 but nothing is specified about what type it has.
1761 So match anything that self-promotes. */
1762 if (different_types_p != NULL
1763 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1764 *different_types_p = true;
1765 if (a1 == NULL_TREE)
1766 {
1767 if (c_type_promotes_to (a2) != a2)
1768 return 0;
1769 }
1770 else if (a2 == NULL_TREE)
1771 {
1772 if (c_type_promotes_to (a1) != a1)
1773 return 0;
1774 }
1775 /* If one of the lists has an error marker, ignore this arg. */
1776 else if (TREE_CODE (a1) == ERROR_MARK
1777 || TREE_CODE (a2) == ERROR_MARK)
1778 ;
1779 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1780 different_types_p)))
1781 {
1782 if (different_types_p != NULL)
1783 *different_types_p = true;
1784 /* Allow wait (union {union wait *u; int *i} *)
1785 and wait (union wait *) to be compatible. */
1786 if (TREE_CODE (a1) == UNION_TYPE
1787 && (TYPE_NAME (a1) == NULL_TREE
1788 || TYPE_TRANSPARENT_AGGR (a1))
1789 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1790 && tree_int_cst_equal (TYPE_SIZE (a1),
1791 TYPE_SIZE (a2)))
1792 {
1793 tree memb;
1794 for (memb = TYPE_FIELDS (a1);
1795 memb; memb = DECL_CHAIN (memb))
1796 {
1797 tree mv3 = TREE_TYPE (memb);
1798 if (mv3 && mv3 != error_mark_node
1799 && TREE_CODE (mv3) != ARRAY_TYPE)
1800 mv3 = (TYPE_ATOMIC (mv3)
1801 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1802 TYPE_QUAL_ATOMIC)
1803 : TYPE_MAIN_VARIANT (mv3));
1804 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1805 different_types_p))
1806 break;
1807 }
1808 if (memb == NULL_TREE)
1809 return 0;
1810 }
1811 else if (TREE_CODE (a2) == UNION_TYPE
1812 && (TYPE_NAME (a2) == NULL_TREE
1813 || TYPE_TRANSPARENT_AGGR (a2))
1814 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1815 && tree_int_cst_equal (TYPE_SIZE (a2),
1816 TYPE_SIZE (a1)))
1817 {
1818 tree memb;
1819 for (memb = TYPE_FIELDS (a2);
1820 memb; memb = DECL_CHAIN (memb))
1821 {
1822 tree mv3 = TREE_TYPE (memb);
1823 if (mv3 && mv3 != error_mark_node
1824 && TREE_CODE (mv3) != ARRAY_TYPE)
1825 mv3 = (TYPE_ATOMIC (mv3)
1826 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1827 TYPE_QUAL_ATOMIC)
1828 : TYPE_MAIN_VARIANT (mv3));
1829 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1830 different_types_p))
1831 break;
1832 }
1833 if (memb == NULL_TREE)
1834 return 0;
1835 }
1836 else
1837 return 0;
1838 }
1839
1840 /* comptypes said ok, but record if it said to warn. */
1841 if (newval > val)
1842 val = newval;
1843
1844 args1 = TREE_CHAIN (args1);
1845 args2 = TREE_CHAIN (args2);
1846 }
1847 }
1848 \f
1849 /* Compute the size to increment a pointer by. When a function type or void
1850 type or incomplete type is passed, size_one_node is returned.
1851 This function does not emit any diagnostics; the caller is responsible
1852 for that. */
1853
1854 static tree
1855 c_size_in_bytes (const_tree type)
1856 {
1857 enum tree_code code = TREE_CODE (type);
1858
1859 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1860 || !COMPLETE_TYPE_P (type))
1861 return size_one_node;
1862
1863 /* Convert in case a char is more than one unit. */
1864 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1865 size_int (TYPE_PRECISION (char_type_node)
1866 / BITS_PER_UNIT));
1867 }
1868 \f
1869 /* Return either DECL or its known constant value (if it has one). */
1870
1871 tree
1872 decl_constant_value_1 (tree decl, bool in_init)
1873 {
1874 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1875 TREE_CODE (decl) != PARM_DECL
1876 && !TREE_THIS_VOLATILE (decl)
1877 && TREE_READONLY (decl)
1878 && DECL_INITIAL (decl) != NULL_TREE
1879 && !error_operand_p (DECL_INITIAL (decl))
1880 /* This is invalid if initial value is not constant.
1881 If it has either a function call, a memory reference,
1882 or a variable, then re-evaluating it could give different results. */
1883 && TREE_CONSTANT (DECL_INITIAL (decl))
1884 /* Check for cases where this is sub-optimal, even though valid. */
1885 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1886 return DECL_INITIAL (decl);
1887 return decl;
1888 }
1889
1890 /* Return either DECL or its known constant value (if it has one).
1891 Like the above, but always return decl outside of functions. */
1892
1893 tree
1894 decl_constant_value (tree decl)
1895 {
1896 /* Don't change a variable array bound or initial value to a constant
1897 in a place where a variable is invalid. */
1898 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1899 }
1900
1901 /* Convert the array expression EXP to a pointer. */
1902 static tree
1903 array_to_pointer_conversion (location_t loc, tree exp)
1904 {
1905 tree orig_exp = exp;
1906 tree type = TREE_TYPE (exp);
1907 tree adr;
1908 tree restype = TREE_TYPE (type);
1909 tree ptrtype;
1910
1911 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1912
1913 STRIP_TYPE_NOPS (exp);
1914
1915 copy_warning (exp, orig_exp);
1916
1917 ptrtype = build_pointer_type (restype);
1918
1919 if (INDIRECT_REF_P (exp))
1920 return convert (ptrtype, TREE_OPERAND (exp, 0));
1921
1922 /* In C++ array compound literals are temporary objects unless they are
1923 const or appear in namespace scope, so they are destroyed too soon
1924 to use them for much of anything (c++/53220). */
1925 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1926 {
1927 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1928 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1929 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1930 "converting an array compound literal to a pointer "
1931 "is ill-formed in C++");
1932 }
1933
1934 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1935 return convert (ptrtype, adr);
1936 }
1937
1938 /* Convert the function expression EXP to a pointer. */
1939 static tree
1940 function_to_pointer_conversion (location_t loc, tree exp)
1941 {
1942 tree orig_exp = exp;
1943
1944 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1945
1946 STRIP_TYPE_NOPS (exp);
1947
1948 copy_warning (exp, orig_exp);
1949
1950 return build_unary_op (loc, ADDR_EXPR, exp, false);
1951 }
1952
1953 /* Mark EXP as read, not just set, for set but not used -Wunused
1954 warning purposes. */
1955
1956 void
1957 mark_exp_read (tree exp)
1958 {
1959 switch (TREE_CODE (exp))
1960 {
1961 case VAR_DECL:
1962 case PARM_DECL:
1963 DECL_READ_P (exp) = 1;
1964 break;
1965 case ARRAY_REF:
1966 case COMPONENT_REF:
1967 case MODIFY_EXPR:
1968 case REALPART_EXPR:
1969 case IMAGPART_EXPR:
1970 CASE_CONVERT:
1971 case ADDR_EXPR:
1972 case VIEW_CONVERT_EXPR:
1973 mark_exp_read (TREE_OPERAND (exp, 0));
1974 break;
1975 case COMPOUND_EXPR:
1976 /* Pattern match what build_atomic_assign produces with modifycode
1977 NOP_EXPR. */
1978 if (VAR_P (TREE_OPERAND (exp, 1))
1979 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1980 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1981 {
1982 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1983 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1984 if (TREE_CODE (t1) == TARGET_EXPR
1985 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1986 && TREE_CODE (t2) == CALL_EXPR)
1987 {
1988 tree fndecl = get_callee_fndecl (t2);
1989 tree arg = NULL_TREE;
1990 if (fndecl
1991 && TREE_CODE (fndecl) == FUNCTION_DECL
1992 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1993 && call_expr_nargs (t2) >= 2)
1994 switch (DECL_FUNCTION_CODE (fndecl))
1995 {
1996 case BUILT_IN_ATOMIC_STORE:
1997 arg = CALL_EXPR_ARG (t2, 1);
1998 break;
1999 case BUILT_IN_ATOMIC_STORE_1:
2000 case BUILT_IN_ATOMIC_STORE_2:
2001 case BUILT_IN_ATOMIC_STORE_4:
2002 case BUILT_IN_ATOMIC_STORE_8:
2003 case BUILT_IN_ATOMIC_STORE_16:
2004 arg = CALL_EXPR_ARG (t2, 0);
2005 break;
2006 default:
2007 break;
2008 }
2009 if (arg)
2010 {
2011 STRIP_NOPS (arg);
2012 if (TREE_CODE (arg) == ADDR_EXPR
2013 && DECL_P (TREE_OPERAND (arg, 0))
2014 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2015 mark_exp_read (TREE_OPERAND (arg, 0));
2016 }
2017 }
2018 }
2019 /* FALLTHRU */
2020 case C_MAYBE_CONST_EXPR:
2021 mark_exp_read (TREE_OPERAND (exp, 1));
2022 break;
2023 default:
2024 break;
2025 }
2026 }
2027
2028 /* Perform the default conversion of arrays and functions to pointers.
2029 Return the result of converting EXP. For any other expression, just
2030 return EXP.
2031
2032 LOC is the location of the expression. */
2033
2034 struct c_expr
2035 default_function_array_conversion (location_t loc, struct c_expr exp)
2036 {
2037 tree orig_exp = exp.value;
2038 tree type = TREE_TYPE (exp.value);
2039 enum tree_code code = TREE_CODE (type);
2040
2041 switch (code)
2042 {
2043 case ARRAY_TYPE:
2044 {
2045 bool not_lvalue = false;
2046 bool lvalue_array_p;
2047
2048 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2049 || CONVERT_EXPR_P (exp.value))
2050 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2051 {
2052 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2053 not_lvalue = true;
2054 exp.value = TREE_OPERAND (exp.value, 0);
2055 }
2056
2057 copy_warning (exp.value, orig_exp);
2058
2059 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2060 if (!flag_isoc99 && !lvalue_array_p)
2061 {
2062 /* Before C99, non-lvalue arrays do not decay to pointers.
2063 Normally, using such an array would be invalid; but it can
2064 be used correctly inside sizeof or as a statement expression.
2065 Thus, do not give an error here; an error will result later. */
2066 return exp;
2067 }
2068
2069 exp.value = array_to_pointer_conversion (loc, exp.value);
2070 }
2071 break;
2072 case FUNCTION_TYPE:
2073 exp.value = function_to_pointer_conversion (loc, exp.value);
2074 break;
2075 default:
2076 break;
2077 }
2078
2079 return exp;
2080 }
2081
2082 struct c_expr
2083 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2084 {
2085 mark_exp_read (exp.value);
2086 return default_function_array_conversion (loc, exp);
2087 }
2088
2089 /* Return whether EXPR should be treated as an atomic lvalue for the
2090 purposes of load and store handling. */
2091
2092 static bool
2093 really_atomic_lvalue (tree expr)
2094 {
2095 if (error_operand_p (expr))
2096 return false;
2097 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2098 return false;
2099 if (!lvalue_p (expr))
2100 return false;
2101
2102 /* Ignore _Atomic on register variables, since their addresses can't
2103 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2104 sequences wouldn't work. Ignore _Atomic on structures containing
2105 bit-fields, since accessing elements of atomic structures or
2106 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2107 it's undefined at translation time or execution time, and the
2108 normal atomic sequences again wouldn't work. */
2109 while (handled_component_p (expr))
2110 {
2111 if (TREE_CODE (expr) == COMPONENT_REF
2112 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2113 return false;
2114 expr = TREE_OPERAND (expr, 0);
2115 }
2116 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2117 return false;
2118 return true;
2119 }
2120
2121 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2122 including converting functions and arrays to pointers if CONVERT_P.
2123 If READ_P, also mark the expression as having been read. */
2124
2125 struct c_expr
2126 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2127 bool convert_p, bool read_p)
2128 {
2129 if (read_p)
2130 mark_exp_read (exp.value);
2131 if (convert_p)
2132 exp = default_function_array_conversion (loc, exp);
2133 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2134 exp.value = require_complete_type (loc, exp.value);
2135 if (really_atomic_lvalue (exp.value))
2136 {
2137 vec<tree, va_gc> *params;
2138 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2139 tree expr_type = TREE_TYPE (exp.value);
2140 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2141 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2142
2143 gcc_assert (TYPE_ATOMIC (expr_type));
2144
2145 /* Expansion of a generic atomic load may require an addition
2146 element, so allocate enough to prevent a resize. */
2147 vec_alloc (params, 4);
2148
2149 /* Remove the qualifiers for the rest of the expressions and
2150 create the VAL temp variable to hold the RHS. */
2151 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2152 tmp = create_tmp_var_raw (nonatomic_type);
2153 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2154 TREE_ADDRESSABLE (tmp) = 1;
2155 /* Do not disable warnings for TMP even though it's artificial.
2156 -Winvalid-memory-model depends on it. */
2157
2158 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2159 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2160 params->quick_push (expr_addr);
2161 params->quick_push (tmp_addr);
2162 params->quick_push (seq_cst);
2163 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2164
2165 /* EXPR is always read. */
2166 mark_exp_read (exp.value);
2167
2168 /* Return tmp which contains the value loaded. */
2169 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2170 NULL_TREE, NULL_TREE);
2171 }
2172 if (convert_p && !error_operand_p (exp.value)
2173 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2174 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2175 return exp;
2176 }
2177
2178 /* EXP is an expression of integer type. Apply the integer promotions
2179 to it and return the promoted value. */
2180
2181 tree
2182 perform_integral_promotions (tree exp)
2183 {
2184 tree type = TREE_TYPE (exp);
2185 enum tree_code code = TREE_CODE (type);
2186
2187 gcc_assert (INTEGRAL_TYPE_P (type));
2188
2189 /* Normally convert enums to int,
2190 but convert wide enums to something wider. */
2191 if (code == ENUMERAL_TYPE)
2192 {
2193 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2194 TYPE_PRECISION (integer_type_node)),
2195 ((TYPE_PRECISION (type)
2196 >= TYPE_PRECISION (integer_type_node))
2197 && TYPE_UNSIGNED (type)));
2198
2199 return convert (type, exp);
2200 }
2201
2202 /* ??? This should no longer be needed now bit-fields have their
2203 proper types. */
2204 if (TREE_CODE (exp) == COMPONENT_REF
2205 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2206 /* If it's thinner than an int, promote it like a
2207 c_promoting_integer_type_p, otherwise leave it alone. */
2208 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2209 TYPE_PRECISION (integer_type_node)) < 0)
2210 return convert (integer_type_node, exp);
2211
2212 if (c_promoting_integer_type_p (type))
2213 {
2214 /* Preserve unsignedness if not really getting any wider. */
2215 if (TYPE_UNSIGNED (type)
2216 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2217 return convert (unsigned_type_node, exp);
2218
2219 return convert (integer_type_node, exp);
2220 }
2221
2222 return exp;
2223 }
2224
2225
2226 /* Perform default promotions for C data used in expressions.
2227 Enumeral types or short or char are converted to int.
2228 In addition, manifest constants symbols are replaced by their values. */
2229
2230 tree
2231 default_conversion (tree exp)
2232 {
2233 tree orig_exp;
2234 tree type = TREE_TYPE (exp);
2235 enum tree_code code = TREE_CODE (type);
2236 tree promoted_type;
2237
2238 mark_exp_read (exp);
2239
2240 /* Functions and arrays have been converted during parsing. */
2241 gcc_assert (code != FUNCTION_TYPE);
2242 if (code == ARRAY_TYPE)
2243 return exp;
2244
2245 /* Constants can be used directly unless they're not loadable. */
2246 if (TREE_CODE (exp) == CONST_DECL)
2247 exp = DECL_INITIAL (exp);
2248
2249 /* Strip no-op conversions. */
2250 orig_exp = exp;
2251 STRIP_TYPE_NOPS (exp);
2252
2253 copy_warning (exp, orig_exp);
2254
2255 if (code == VOID_TYPE)
2256 {
2257 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2258 "void value not ignored as it ought to be");
2259 return error_mark_node;
2260 }
2261
2262 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2263 if (exp == error_mark_node)
2264 return error_mark_node;
2265
2266 promoted_type = targetm.promoted_type (type);
2267 if (promoted_type)
2268 return convert (promoted_type, exp);
2269
2270 if (INTEGRAL_TYPE_P (type))
2271 return perform_integral_promotions (exp);
2272
2273 return exp;
2274 }
2275 \f
2276 /* Look up COMPONENT in a structure or union TYPE.
2277
2278 If the component name is not found, returns NULL_TREE. Otherwise,
2279 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2280 stepping down the chain to the component, which is in the last
2281 TREE_VALUE of the list. Normally the list is of length one, but if
2282 the component is embedded within (nested) anonymous structures or
2283 unions, the list steps down the chain to the component. */
2284
2285 static tree
2286 lookup_field (tree type, tree component)
2287 {
2288 tree field;
2289
2290 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2291 to the field elements. Use a binary search on this array to quickly
2292 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2293 will always be set for structures which have many elements.
2294
2295 Duplicate field checking replaces duplicates with NULL_TREE so
2296 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2297 case just iterate using DECL_CHAIN. */
2298
2299 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2300 && !seen_error ())
2301 {
2302 int bot, top, half;
2303 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2304
2305 field = TYPE_FIELDS (type);
2306 bot = 0;
2307 top = TYPE_LANG_SPECIFIC (type)->s->len;
2308 while (top - bot > 1)
2309 {
2310 half = (top - bot + 1) >> 1;
2311 field = field_array[bot+half];
2312
2313 if (DECL_NAME (field) == NULL_TREE)
2314 {
2315 /* Step through all anon unions in linear fashion. */
2316 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2317 {
2318 field = field_array[bot++];
2319 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2320 {
2321 tree anon = lookup_field (TREE_TYPE (field), component);
2322
2323 if (anon)
2324 return tree_cons (NULL_TREE, field, anon);
2325
2326 /* The Plan 9 compiler permits referring
2327 directly to an anonymous struct/union field
2328 using a typedef name. */
2329 if (flag_plan9_extensions
2330 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2331 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2332 == TYPE_DECL)
2333 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2334 == component))
2335 break;
2336 }
2337 }
2338
2339 /* Entire record is only anon unions. */
2340 if (bot > top)
2341 return NULL_TREE;
2342
2343 /* Restart the binary search, with new lower bound. */
2344 continue;
2345 }
2346
2347 if (DECL_NAME (field) == component)
2348 break;
2349 if (DECL_NAME (field) < component)
2350 bot += half;
2351 else
2352 top = bot + half;
2353 }
2354
2355 if (DECL_NAME (field_array[bot]) == component)
2356 field = field_array[bot];
2357 else if (DECL_NAME (field) != component)
2358 return NULL_TREE;
2359 }
2360 else
2361 {
2362 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2363 {
2364 if (DECL_NAME (field) == NULL_TREE
2365 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2366 {
2367 tree anon = lookup_field (TREE_TYPE (field), component);
2368
2369 if (anon)
2370 return tree_cons (NULL_TREE, field, anon);
2371
2372 /* The Plan 9 compiler permits referring directly to an
2373 anonymous struct/union field using a typedef
2374 name. */
2375 if (flag_plan9_extensions
2376 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2377 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2378 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2379 == component))
2380 break;
2381 }
2382
2383 if (DECL_NAME (field) == component)
2384 break;
2385 }
2386
2387 if (field == NULL_TREE)
2388 return NULL_TREE;
2389 }
2390
2391 return tree_cons (NULL_TREE, field, NULL_TREE);
2392 }
2393
2394 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2395
2396 static void
2397 lookup_field_fuzzy_find_candidates (tree type, tree component,
2398 vec<tree> *candidates)
2399 {
2400 tree field;
2401 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2402 {
2403 if (DECL_NAME (field) == NULL_TREE
2404 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2405 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2406 candidates);
2407
2408 if (DECL_NAME (field))
2409 candidates->safe_push (DECL_NAME (field));
2410 }
2411 }
2412
2413 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2414 rather than returning a TREE_LIST for an exact match. */
2415
2416 static tree
2417 lookup_field_fuzzy (tree type, tree component)
2418 {
2419 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2420
2421 /* First, gather a list of candidates. */
2422 auto_vec <tree> candidates;
2423
2424 lookup_field_fuzzy_find_candidates (type, component,
2425 &candidates);
2426
2427 return find_closest_identifier (component, &candidates);
2428 }
2429
2430 /* Support function for build_component_ref's error-handling.
2431
2432 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2433 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2434
2435 static bool
2436 should_suggest_deref_p (tree datum_type)
2437 {
2438 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2439 allows "." for ptrs; we could be handling a failed attempt
2440 to access a property. */
2441 if (c_dialect_objc ())
2442 return false;
2443
2444 /* Only suggest it for pointers... */
2445 if (TREE_CODE (datum_type) != POINTER_TYPE)
2446 return false;
2447
2448 /* ...to structs/unions. */
2449 tree underlying_type = TREE_TYPE (datum_type);
2450 enum tree_code code = TREE_CODE (underlying_type);
2451 if (code == RECORD_TYPE || code == UNION_TYPE)
2452 return true;
2453 else
2454 return false;
2455 }
2456
2457 /* Make an expression to refer to the COMPONENT field of structure or
2458 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2459 location of the COMPONENT_REF. COMPONENT_LOC is the location
2460 of COMPONENT. ARROW_LOC is the location of the first -> operand if
2461 it is from -> operator. */
2462
2463 tree
2464 build_component_ref (location_t loc, tree datum, tree component,
2465 location_t component_loc, location_t arrow_loc)
2466 {
2467 tree type = TREE_TYPE (datum);
2468 enum tree_code code = TREE_CODE (type);
2469 tree field = NULL;
2470 tree ref;
2471 bool datum_lvalue = lvalue_p (datum);
2472
2473 if (!objc_is_public (datum, component))
2474 return error_mark_node;
2475
2476 /* Detect Objective-C property syntax object.property. */
2477 if (c_dialect_objc ()
2478 && (ref = objc_maybe_build_component_ref (datum, component)))
2479 return ref;
2480
2481 /* See if there is a field or component with name COMPONENT. */
2482
2483 if (code == RECORD_TYPE || code == UNION_TYPE)
2484 {
2485 if (!COMPLETE_TYPE_P (type))
2486 {
2487 c_incomplete_type_error (loc, NULL_TREE, type);
2488 return error_mark_node;
2489 }
2490
2491 field = lookup_field (type, component);
2492
2493 if (!field)
2494 {
2495 tree guessed_id = lookup_field_fuzzy (type, component);
2496 if (guessed_id)
2497 {
2498 /* Attempt to provide a fixit replacement hint, if
2499 we have a valid range for the component. */
2500 location_t reported_loc
2501 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2502 gcc_rich_location rich_loc (reported_loc);
2503 if (component_loc != UNKNOWN_LOCATION)
2504 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2505 error_at (&rich_loc,
2506 "%qT has no member named %qE; did you mean %qE?",
2507 type, component, guessed_id);
2508 }
2509 else
2510 error_at (loc, "%qT has no member named %qE", type, component);
2511 return error_mark_node;
2512 }
2513
2514 /* Accessing elements of atomic structures or unions is undefined
2515 behavior (C11 6.5.2.3#5). */
2516 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2517 {
2518 if (code == RECORD_TYPE)
2519 warning_at (loc, 0, "accessing a member %qE of an atomic "
2520 "structure %qE", component, datum);
2521 else
2522 warning_at (loc, 0, "accessing a member %qE of an atomic "
2523 "union %qE", component, datum);
2524 }
2525
2526 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2527 This might be better solved in future the way the C++ front
2528 end does it - by giving the anonymous entities each a
2529 separate name and type, and then have build_component_ref
2530 recursively call itself. We can't do that here. */
2531 do
2532 {
2533 tree subdatum = TREE_VALUE (field);
2534 int quals;
2535 tree subtype;
2536 bool use_datum_quals;
2537
2538 if (TREE_TYPE (subdatum) == error_mark_node)
2539 return error_mark_node;
2540
2541 /* If this is an rvalue, it does not have qualifiers in C
2542 standard terms and we must avoid propagating such
2543 qualifiers down to a non-lvalue array that is then
2544 converted to a pointer. */
2545 use_datum_quals = (datum_lvalue
2546 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2547
2548 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2549 if (use_datum_quals)
2550 quals |= TYPE_QUALS (TREE_TYPE (datum));
2551 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2552
2553 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2554 NULL_TREE);
2555 SET_EXPR_LOCATION (ref, loc);
2556 if (TREE_READONLY (subdatum)
2557 || (use_datum_quals && TREE_READONLY (datum)))
2558 TREE_READONLY (ref) = 1;
2559 if (TREE_THIS_VOLATILE (subdatum)
2560 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2561 TREE_THIS_VOLATILE (ref) = 1;
2562
2563 if (TREE_UNAVAILABLE (subdatum))
2564 error_unavailable_use (subdatum, NULL_TREE);
2565 else if (TREE_DEPRECATED (subdatum))
2566 warn_deprecated_use (subdatum, NULL_TREE);
2567
2568 datum = ref;
2569
2570 field = TREE_CHAIN (field);
2571 }
2572 while (field);
2573
2574 return ref;
2575 }
2576 else if (should_suggest_deref_p (type))
2577 {
2578 /* Special-case the error message for "ptr.field" for the case
2579 where the user has confused "." vs "->". */
2580 rich_location richloc (line_table, loc);
2581 if (TREE_CODE (datum) == INDIRECT_REF && arrow_loc != UNKNOWN_LOCATION)
2582 {
2583 richloc.add_fixit_insert_before (arrow_loc, "(*");
2584 richloc.add_fixit_insert_after (arrow_loc, ")");
2585 error_at (&richloc,
2586 "%qE is a pointer to pointer; did you mean to dereference "
2587 "it before applying %<->%> to it?",
2588 TREE_OPERAND (datum, 0));
2589 }
2590 else
2591 {
2592 /* "loc" should be the "." token. */
2593 richloc.add_fixit_replace ("->");
2594 error_at (&richloc,
2595 "%qE is a pointer; did you mean to use %<->%>?",
2596 datum);
2597 }
2598 return error_mark_node;
2599 }
2600 else if (code != ERROR_MARK)
2601 error_at (loc,
2602 "request for member %qE in something not a structure or union",
2603 component);
2604
2605 return error_mark_node;
2606 }
2607 \f
2608 /* Given an expression PTR for a pointer, return an expression
2609 for the value pointed to.
2610 ERRORSTRING is the name of the operator to appear in error messages.
2611
2612 LOC is the location to use for the generated tree. */
2613
2614 tree
2615 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2616 {
2617 tree pointer = default_conversion (ptr);
2618 tree type = TREE_TYPE (pointer);
2619 tree ref;
2620
2621 if (TREE_CODE (type) == POINTER_TYPE)
2622 {
2623 if (CONVERT_EXPR_P (pointer)
2624 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2625 {
2626 /* If a warning is issued, mark it to avoid duplicates from
2627 the backend. This only needs to be done at
2628 warn_strict_aliasing > 2. */
2629 if (warn_strict_aliasing > 2)
2630 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2631 type, TREE_OPERAND (pointer, 0)))
2632 suppress_warning (pointer, OPT_Wstrict_aliasing_);
2633 }
2634
2635 if (TREE_CODE (pointer) == ADDR_EXPR
2636 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2637 == TREE_TYPE (type)))
2638 {
2639 ref = TREE_OPERAND (pointer, 0);
2640 protected_set_expr_location (ref, loc);
2641 return ref;
2642 }
2643 else
2644 {
2645 tree t = TREE_TYPE (type);
2646
2647 ref = build1 (INDIRECT_REF, t, pointer);
2648
2649 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2650 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2651
2652 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2653 so that we get the proper error message if the result is used
2654 to assign to. Also, &* is supposed to be a no-op.
2655 And ANSI C seems to specify that the type of the result
2656 should be the const type. */
2657 /* A de-reference of a pointer to const is not a const. It is valid
2658 to change it via some other pointer. */
2659 TREE_READONLY (ref) = TYPE_READONLY (t);
2660 TREE_SIDE_EFFECTS (ref)
2661 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2662 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2663 protected_set_expr_location (ref, loc);
2664 return ref;
2665 }
2666 }
2667 else if (TREE_CODE (pointer) != ERROR_MARK)
2668 invalid_indirection_error (loc, type, errstring);
2669
2670 return error_mark_node;
2671 }
2672
2673 /* This handles expressions of the form "a[i]", which denotes
2674 an array reference.
2675
2676 This is logically equivalent in C to *(a+i), but we may do it differently.
2677 If A is a variable or a member, we generate a primitive ARRAY_REF.
2678 This avoids forcing the array out of registers, and can work on
2679 arrays that are not lvalues (for example, members of structures returned
2680 by functions).
2681
2682 For vector types, allow vector[i] but not i[vector], and create
2683 *(((type*)&vectortype) + i) for the expression.
2684
2685 LOC is the location to use for the returned expression. */
2686
2687 tree
2688 build_array_ref (location_t loc, tree array, tree index)
2689 {
2690 tree ret;
2691 bool swapped = false;
2692 if (TREE_TYPE (array) == error_mark_node
2693 || TREE_TYPE (index) == error_mark_node)
2694 return error_mark_node;
2695
2696 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2697 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2698 /* Allow vector[index] but not index[vector]. */
2699 && !gnu_vector_type_p (TREE_TYPE (array)))
2700 {
2701 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2702 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2703 {
2704 error_at (loc,
2705 "subscripted value is neither array nor pointer nor vector");
2706
2707 return error_mark_node;
2708 }
2709 std::swap (array, index);
2710 swapped = true;
2711 }
2712
2713 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2714 {
2715 error_at (loc, "array subscript is not an integer");
2716 return error_mark_node;
2717 }
2718
2719 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2720 {
2721 error_at (loc, "subscripted value is pointer to function");
2722 return error_mark_node;
2723 }
2724
2725 /* ??? Existing practice has been to warn only when the char
2726 index is syntactically the index, not for char[array]. */
2727 if (!swapped)
2728 warn_array_subscript_with_type_char (loc, index);
2729
2730 /* Apply default promotions *after* noticing character types. */
2731 index = default_conversion (index);
2732 if (index == error_mark_node)
2733 return error_mark_node;
2734
2735 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2736
2737 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2738 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2739
2740 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2741 {
2742 tree rval, type;
2743
2744 /* An array that is indexed by a non-constant
2745 cannot be stored in a register; we must be able to do
2746 address arithmetic on its address.
2747 Likewise an array of elements of variable size. */
2748 if (TREE_CODE (index) != INTEGER_CST
2749 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2750 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2751 {
2752 if (!c_mark_addressable (array, true))
2753 return error_mark_node;
2754 }
2755 /* An array that is indexed by a constant value which is not within
2756 the array bounds cannot be stored in a register either; because we
2757 would get a crash in store_bit_field/extract_bit_field when trying
2758 to access a non-existent part of the register. */
2759 if (TREE_CODE (index) == INTEGER_CST
2760 && TYPE_DOMAIN (TREE_TYPE (array))
2761 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2762 {
2763 if (!c_mark_addressable (array))
2764 return error_mark_node;
2765 }
2766
2767 if ((pedantic || warn_c90_c99_compat)
2768 && ! was_vector)
2769 {
2770 tree foo = array;
2771 while (TREE_CODE (foo) == COMPONENT_REF)
2772 foo = TREE_OPERAND (foo, 0);
2773 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2774 pedwarn (loc, OPT_Wpedantic,
2775 "ISO C forbids subscripting %<register%> array");
2776 else if (!lvalue_p (foo))
2777 pedwarn_c90 (loc, OPT_Wpedantic,
2778 "ISO C90 forbids subscripting non-lvalue "
2779 "array");
2780 }
2781
2782 type = TREE_TYPE (TREE_TYPE (array));
2783 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2784 /* Array ref is const/volatile if the array elements are
2785 or if the array is. */
2786 TREE_READONLY (rval)
2787 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2788 | TREE_READONLY (array));
2789 TREE_SIDE_EFFECTS (rval)
2790 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2791 | TREE_SIDE_EFFECTS (array));
2792 TREE_THIS_VOLATILE (rval)
2793 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2794 /* This was added by rms on 16 Nov 91.
2795 It fixes vol struct foo *a; a->elts[1]
2796 in an inline function.
2797 Hope it doesn't break something else. */
2798 | TREE_THIS_VOLATILE (array));
2799 ret = require_complete_type (loc, rval);
2800 protected_set_expr_location (ret, loc);
2801 if (non_lvalue)
2802 ret = non_lvalue_loc (loc, ret);
2803 return ret;
2804 }
2805 else
2806 {
2807 tree ar = default_conversion (array);
2808
2809 if (ar == error_mark_node)
2810 return ar;
2811
2812 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2813 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2814
2815 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2816 index, false),
2817 RO_ARRAY_INDEXING);
2818 if (non_lvalue)
2819 ret = non_lvalue_loc (loc, ret);
2820 return ret;
2821 }
2822 }
2823 \f
2824 /* Build an external reference to identifier ID. FUN indicates
2825 whether this will be used for a function call. LOC is the source
2826 location of the identifier. This sets *TYPE to the type of the
2827 identifier, which is not the same as the type of the returned value
2828 for CONST_DECLs defined as enum constants. If the type of the
2829 identifier is not available, *TYPE is set to NULL. */
2830 tree
2831 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2832 {
2833 tree ref;
2834 tree decl = lookup_name (id);
2835
2836 /* In Objective-C, an instance variable (ivar) may be preferred to
2837 whatever lookup_name() found. */
2838 decl = objc_lookup_ivar (decl, id);
2839
2840 *type = NULL;
2841 if (decl && decl != error_mark_node)
2842 {
2843 ref = decl;
2844 *type = TREE_TYPE (ref);
2845 }
2846 else if (fun)
2847 /* Implicit function declaration. */
2848 ref = implicitly_declare (loc, id);
2849 else if (decl == error_mark_node)
2850 /* Don't complain about something that's already been
2851 complained about. */
2852 return error_mark_node;
2853 else
2854 {
2855 undeclared_variable (loc, id);
2856 return error_mark_node;
2857 }
2858
2859 if (TREE_TYPE (ref) == error_mark_node)
2860 return error_mark_node;
2861
2862 if (TREE_UNAVAILABLE (ref))
2863 error_unavailable_use (ref, NULL_TREE);
2864 else if (TREE_DEPRECATED (ref))
2865 warn_deprecated_use (ref, NULL_TREE);
2866
2867 /* Recursive call does not count as usage. */
2868 if (ref != current_function_decl)
2869 {
2870 TREE_USED (ref) = 1;
2871 }
2872
2873 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2874 {
2875 if (!in_sizeof && !in_typeof)
2876 C_DECL_USED (ref) = 1;
2877 else if (DECL_INITIAL (ref) == NULL_TREE
2878 && DECL_EXTERNAL (ref)
2879 && !TREE_PUBLIC (ref))
2880 record_maybe_used_decl (ref);
2881 }
2882
2883 if (TREE_CODE (ref) == CONST_DECL)
2884 {
2885 used_types_insert (TREE_TYPE (ref));
2886
2887 if (warn_cxx_compat
2888 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2889 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2890 {
2891 warning_at (loc, OPT_Wc___compat,
2892 ("enum constant defined in struct or union "
2893 "is not visible in C++"));
2894 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2895 }
2896
2897 ref = DECL_INITIAL (ref);
2898 TREE_CONSTANT (ref) = 1;
2899 }
2900 else if (current_function_decl != NULL_TREE
2901 && !DECL_FILE_SCOPE_P (current_function_decl)
2902 && (VAR_OR_FUNCTION_DECL_P (ref)
2903 || TREE_CODE (ref) == PARM_DECL))
2904 {
2905 tree context = decl_function_context (ref);
2906
2907 if (context != NULL_TREE && context != current_function_decl)
2908 DECL_NONLOCAL (ref) = 1;
2909 }
2910 /* C99 6.7.4p3: An inline definition of a function with external
2911 linkage ... shall not contain a reference to an identifier with
2912 internal linkage. */
2913 else if (current_function_decl != NULL_TREE
2914 && DECL_DECLARED_INLINE_P (current_function_decl)
2915 && DECL_EXTERNAL (current_function_decl)
2916 && VAR_OR_FUNCTION_DECL_P (ref)
2917 && (!VAR_P (ref) || TREE_STATIC (ref))
2918 && ! TREE_PUBLIC (ref)
2919 && DECL_CONTEXT (ref) != current_function_decl)
2920 record_inline_static (loc, current_function_decl, ref,
2921 csi_internal);
2922
2923 return ref;
2924 }
2925
2926 /* Record details of decls possibly used inside sizeof or typeof. */
2927 struct maybe_used_decl
2928 {
2929 /* The decl. */
2930 tree decl;
2931 /* The level seen at (in_sizeof + in_typeof). */
2932 int level;
2933 /* The next one at this level or above, or NULL. */
2934 struct maybe_used_decl *next;
2935 };
2936
2937 static struct maybe_used_decl *maybe_used_decls;
2938
2939 /* Record that DECL, an undefined static function reference seen
2940 inside sizeof or typeof, might be used if the operand of sizeof is
2941 a VLA type or the operand of typeof is a variably modified
2942 type. */
2943
2944 static void
2945 record_maybe_used_decl (tree decl)
2946 {
2947 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2948 t->decl = decl;
2949 t->level = in_sizeof + in_typeof;
2950 t->next = maybe_used_decls;
2951 maybe_used_decls = t;
2952 }
2953
2954 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2955 USED is false, just discard them. If it is true, mark them used
2956 (if no longer inside sizeof or typeof) or move them to the next
2957 level up (if still inside sizeof or typeof). */
2958
2959 void
2960 pop_maybe_used (bool used)
2961 {
2962 struct maybe_used_decl *p = maybe_used_decls;
2963 int cur_level = in_sizeof + in_typeof;
2964 while (p && p->level > cur_level)
2965 {
2966 if (used)
2967 {
2968 if (cur_level == 0)
2969 C_DECL_USED (p->decl) = 1;
2970 else
2971 p->level = cur_level;
2972 }
2973 p = p->next;
2974 }
2975 if (!used || cur_level == 0)
2976 maybe_used_decls = p;
2977 }
2978
2979 /* Return the result of sizeof applied to EXPR. */
2980
2981 struct c_expr
2982 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2983 {
2984 struct c_expr ret;
2985 if (expr.value == error_mark_node)
2986 {
2987 ret.value = error_mark_node;
2988 ret.original_code = ERROR_MARK;
2989 ret.original_type = NULL;
2990 pop_maybe_used (false);
2991 }
2992 else
2993 {
2994 bool expr_const_operands = true;
2995
2996 if (TREE_CODE (expr.value) == PARM_DECL
2997 && C_ARRAY_PARAMETER (expr.value))
2998 {
2999 auto_diagnostic_group d;
3000 if (warning_at (loc, OPT_Wsizeof_array_argument,
3001 "%<sizeof%> on array function parameter %qE will "
3002 "return size of %qT", expr.value,
3003 TREE_TYPE (expr.value)))
3004 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3005 }
3006 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3007 &expr_const_operands);
3008 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3009 c_last_sizeof_arg = expr.value;
3010 c_last_sizeof_loc = loc;
3011 ret.original_code = SIZEOF_EXPR;
3012 ret.original_type = NULL;
3013 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3014 {
3015 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3016 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3017 folded_expr, ret.value);
3018 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3019 SET_EXPR_LOCATION (ret.value, loc);
3020 }
3021 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3022 }
3023 return ret;
3024 }
3025
3026 /* Return the result of sizeof applied to T, a structure for the type
3027 name passed to sizeof (rather than the type itself). LOC is the
3028 location of the original expression. */
3029
3030 struct c_expr
3031 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3032 {
3033 tree type;
3034 struct c_expr ret;
3035 tree type_expr = NULL_TREE;
3036 bool type_expr_const = true;
3037 type = groktypename (t, &type_expr, &type_expr_const);
3038 ret.value = c_sizeof (loc, type);
3039 c_last_sizeof_arg = type;
3040 c_last_sizeof_loc = loc;
3041 ret.original_code = SIZEOF_EXPR;
3042 ret.original_type = NULL;
3043 if (type == error_mark_node)
3044 {
3045 ret.value = error_mark_node;
3046 ret.original_code = ERROR_MARK;
3047 }
3048 else
3049 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3050 && C_TYPE_VARIABLE_SIZE (type))
3051 {
3052 /* If the type is a [*] array, it is a VLA but is represented as
3053 having a size of zero. In such a case we must ensure that
3054 the result of sizeof does not get folded to a constant by
3055 c_fully_fold, because if the size is evaluated the result is
3056 not constant and so constraints on zero or negative size
3057 arrays must not be applied when this sizeof call is inside
3058 another array declarator. */
3059 if (!type_expr)
3060 type_expr = integer_zero_node;
3061 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3062 type_expr, ret.value);
3063 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3064 }
3065 pop_maybe_used (type != error_mark_node
3066 ? C_TYPE_VARIABLE_SIZE (type) : false);
3067 return ret;
3068 }
3069
3070 /* Build a function call to function FUNCTION with parameters PARAMS.
3071 The function call is at LOC.
3072 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3073 TREE_VALUE of each node is a parameter-expression.
3074 FUNCTION's data type may be a function type or a pointer-to-function. */
3075
3076 tree
3077 build_function_call (location_t loc, tree function, tree params)
3078 {
3079 vec<tree, va_gc> *v;
3080 tree ret;
3081
3082 vec_alloc (v, list_length (params));
3083 for (; params; params = TREE_CHAIN (params))
3084 v->quick_push (TREE_VALUE (params));
3085 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3086 vec_free (v);
3087 return ret;
3088 }
3089
3090 /* Give a note about the location of the declaration of DECL. */
3091
3092 static void
3093 inform_declaration (tree decl)
3094 {
3095 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3096 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3097 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3098 }
3099
3100 /* Build a function call to function FUNCTION with parameters PARAMS.
3101 If FUNCTION is the result of resolving an overloaded target built-in,
3102 ORIG_FUNDECL is the original function decl, otherwise it is null.
3103 ORIGTYPES, if not NULL, is a vector of types; each element is
3104 either NULL or the original type of the corresponding element in
3105 PARAMS. The original type may differ from TREE_TYPE of the
3106 parameter for enums. FUNCTION's data type may be a function type
3107 or pointer-to-function. This function changes the elements of
3108 PARAMS. */
3109
3110 tree
3111 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3112 tree function, vec<tree, va_gc> *params,
3113 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3114 {
3115 tree fntype, fundecl = NULL_TREE;
3116 tree name = NULL_TREE, result;
3117 tree tem;
3118 int nargs;
3119 tree *argarray;
3120
3121
3122 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3123 STRIP_TYPE_NOPS (function);
3124
3125 /* Convert anything with function type to a pointer-to-function. */
3126 if (TREE_CODE (function) == FUNCTION_DECL)
3127 {
3128 name = DECL_NAME (function);
3129
3130 if (flag_tm)
3131 tm_malloc_replacement (function);
3132 fundecl = function;
3133 if (!orig_fundecl)
3134 orig_fundecl = fundecl;
3135 /* Atomic functions have type checking/casting already done. They are
3136 often rewritten and don't match the original parameter list. */
3137 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3138 origtypes = NULL;
3139 }
3140 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3141 function = function_to_pointer_conversion (loc, function);
3142
3143 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3144 expressions, like those used for ObjC messenger dispatches. */
3145 if (params && !params->is_empty ())
3146 function = objc_rewrite_function_call (function, (*params)[0]);
3147
3148 function = c_fully_fold (function, false, NULL);
3149
3150 fntype = TREE_TYPE (function);
3151
3152 if (TREE_CODE (fntype) == ERROR_MARK)
3153 return error_mark_node;
3154
3155 if (!(TREE_CODE (fntype) == POINTER_TYPE
3156 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3157 {
3158 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3159 error_at (loc,
3160 "called object %qE is not a function or function pointer",
3161 function);
3162 else if (DECL_P (function))
3163 {
3164 error_at (loc,
3165 "called object %qD is not a function or function pointer",
3166 function);
3167 inform_declaration (function);
3168 }
3169 else
3170 error_at (loc,
3171 "called object is not a function or function pointer");
3172 return error_mark_node;
3173 }
3174
3175 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3176 current_function_returns_abnormally = 1;
3177
3178 /* fntype now gets the type of function pointed to. */
3179 fntype = TREE_TYPE (fntype);
3180
3181 /* Convert the parameters to the types declared in the
3182 function prototype, or apply default promotions. */
3183
3184 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3185 origtypes, function, fundecl);
3186 if (nargs < 0)
3187 return error_mark_node;
3188
3189 /* Check that the function is called through a compatible prototype.
3190 If it is not, warn. */
3191 if (CONVERT_EXPR_P (function)
3192 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3193 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3194 && !comptypes (fntype, TREE_TYPE (tem)))
3195 {
3196 tree return_type = TREE_TYPE (fntype);
3197
3198 /* This situation leads to run-time undefined behavior. We can't,
3199 therefore, simply error unless we can prove that all possible
3200 executions of the program must execute the code. */
3201 warning_at (loc, 0, "function called through a non-compatible type");
3202
3203 if (VOID_TYPE_P (return_type)
3204 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3205 pedwarn (loc, 0,
3206 "function with qualified void return type called");
3207 }
3208
3209 argarray = vec_safe_address (params);
3210
3211 /* Check that arguments to builtin functions match the expectations. */
3212 if (fundecl
3213 && fndecl_built_in_p (fundecl)
3214 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3215 orig_fundecl, nargs, argarray))
3216 return error_mark_node;
3217
3218 /* Check that the arguments to the function are valid. */
3219 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3220 nargs, argarray, &arg_loc);
3221
3222 if (name != NULL_TREE
3223 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3224 {
3225 if (require_constant_value)
3226 result
3227 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3228 function, nargs, argarray);
3229 else
3230 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3231 function, nargs, argarray);
3232 if (TREE_CODE (result) == NOP_EXPR
3233 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3234 STRIP_TYPE_NOPS (result);
3235 }
3236 else
3237 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3238 function, nargs, argarray);
3239 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3240 later. */
3241 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3242 suppress_warning (result, OPT_Wnonnull);
3243
3244 /* In this improbable scenario, a nested function returns a VM type.
3245 Create a TARGET_EXPR so that the call always has a LHS, much as
3246 what the C++ FE does for functions returning non-PODs. */
3247 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3248 {
3249 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3250 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3251 NULL_TREE, NULL_TREE);
3252 }
3253
3254 if (VOID_TYPE_P (TREE_TYPE (result)))
3255 {
3256 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3257 pedwarn (loc, 0,
3258 "function with qualified void return type called");
3259 return result;
3260 }
3261 return require_complete_type (loc, result);
3262 }
3263
3264 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3265
3266 tree
3267 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3268 tree function, vec<tree, va_gc> *params,
3269 vec<tree, va_gc> *origtypes)
3270 {
3271 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3272 STRIP_TYPE_NOPS (function);
3273
3274 /* Convert anything with function type to a pointer-to-function. */
3275 if (TREE_CODE (function) == FUNCTION_DECL)
3276 {
3277 /* Implement type-directed function overloading for builtins.
3278 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3279 handle all the type checking. The result is a complete expression
3280 that implements this function call. */
3281 tree tem = resolve_overloaded_builtin (loc, function, params);
3282 if (tem)
3283 return tem;
3284 }
3285 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3286 }
3287 \f
3288 /* Helper for convert_arguments called to convert the VALue of argument
3289 number ARGNUM from ORIGTYPE to the corresponding parameter number
3290 PARMNUM and TYPE.
3291 PLOC is the location where the conversion is being performed.
3292 FUNCTION and FUNDECL are the same as in convert_arguments.
3293 VALTYPE is the original type of VAL before the conversion and,
3294 for EXCESS_PRECISION_EXPR, the operand of the expression.
3295 NPC is true if VAL represents the null pointer constant (VAL itself
3296 will have been folded to an integer constant).
3297 RNAME is the same as FUNCTION except in Objective C when it's
3298 the function selector.
3299 EXCESS_PRECISION is true when VAL was originally represented
3300 as EXCESS_PRECISION_EXPR.
3301 WARNOPT is the same as in convert_for_assignment. */
3302
3303 static tree
3304 convert_argument (location_t ploc, tree function, tree fundecl,
3305 tree type, tree origtype, tree val, tree valtype,
3306 bool npc, tree rname, int parmnum, int argnum,
3307 bool excess_precision, int warnopt)
3308 {
3309 /* Formal parm type is specified by a function prototype. */
3310
3311 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3312 {
3313 error_at (ploc, "type of formal parameter %d is incomplete",
3314 parmnum + 1);
3315 return val;
3316 }
3317
3318 /* Optionally warn about conversions that differ from the default
3319 conversions. */
3320 if (warn_traditional_conversion || warn_traditional)
3321 {
3322 unsigned int formal_prec = TYPE_PRECISION (type);
3323
3324 if (INTEGRAL_TYPE_P (type)
3325 && TREE_CODE (valtype) == REAL_TYPE)
3326 warning_at (ploc, OPT_Wtraditional_conversion,
3327 "passing argument %d of %qE as integer rather "
3328 "than floating due to prototype",
3329 argnum, rname);
3330 if (INTEGRAL_TYPE_P (type)
3331 && TREE_CODE (valtype) == COMPLEX_TYPE)
3332 warning_at (ploc, OPT_Wtraditional_conversion,
3333 "passing argument %d of %qE as integer rather "
3334 "than complex due to prototype",
3335 argnum, rname);
3336 else if (TREE_CODE (type) == COMPLEX_TYPE
3337 && TREE_CODE (valtype) == REAL_TYPE)
3338 warning_at (ploc, OPT_Wtraditional_conversion,
3339 "passing argument %d of %qE as complex rather "
3340 "than floating due to prototype",
3341 argnum, rname);
3342 else if (TREE_CODE (type) == REAL_TYPE
3343 && INTEGRAL_TYPE_P (valtype))
3344 warning_at (ploc, OPT_Wtraditional_conversion,
3345 "passing argument %d of %qE as floating rather "
3346 "than integer due to prototype",
3347 argnum, rname);
3348 else if (TREE_CODE (type) == COMPLEX_TYPE
3349 && INTEGRAL_TYPE_P (valtype))
3350 warning_at (ploc, OPT_Wtraditional_conversion,
3351 "passing argument %d of %qE as complex rather "
3352 "than integer due to prototype",
3353 argnum, rname);
3354 else if (TREE_CODE (type) == REAL_TYPE
3355 && TREE_CODE (valtype) == COMPLEX_TYPE)
3356 warning_at (ploc, OPT_Wtraditional_conversion,
3357 "passing argument %d of %qE as floating rather "
3358 "than complex due to prototype",
3359 argnum, rname);
3360 /* ??? At some point, messages should be written about
3361 conversions between complex types, but that's too messy
3362 to do now. */
3363 else if (TREE_CODE (type) == REAL_TYPE
3364 && TREE_CODE (valtype) == REAL_TYPE)
3365 {
3366 /* Warn if any argument is passed as `float',
3367 since without a prototype it would be `double'. */
3368 if (formal_prec == TYPE_PRECISION (float_type_node)
3369 && type != dfloat32_type_node)
3370 warning_at (ploc, 0,
3371 "passing argument %d of %qE as %<float%> "
3372 "rather than %<double%> due to prototype",
3373 argnum, rname);
3374
3375 /* Warn if mismatch between argument and prototype
3376 for decimal float types. Warn of conversions with
3377 binary float types and of precision narrowing due to
3378 prototype. */
3379 else if (type != valtype
3380 && (type == dfloat32_type_node
3381 || type == dfloat64_type_node
3382 || type == dfloat128_type_node
3383 || valtype == dfloat32_type_node
3384 || valtype == dfloat64_type_node
3385 || valtype == dfloat128_type_node)
3386 && (formal_prec
3387 <= TYPE_PRECISION (valtype)
3388 || (type == dfloat128_type_node
3389 && (valtype
3390 != dfloat64_type_node
3391 && (valtype
3392 != dfloat32_type_node)))
3393 || (type == dfloat64_type_node
3394 && (valtype
3395 != dfloat32_type_node))))
3396 warning_at (ploc, 0,
3397 "passing argument %d of %qE as %qT "
3398 "rather than %qT due to prototype",
3399 argnum, rname, type, valtype);
3400
3401 }
3402 /* Detect integer changing in width or signedness.
3403 These warnings are only activated with
3404 -Wtraditional-conversion, not with -Wtraditional. */
3405 else if (warn_traditional_conversion
3406 && INTEGRAL_TYPE_P (type)
3407 && INTEGRAL_TYPE_P (valtype))
3408 {
3409 tree would_have_been = default_conversion (val);
3410 tree type1 = TREE_TYPE (would_have_been);
3411
3412 if (val == error_mark_node)
3413 /* VAL could have been of incomplete type. */;
3414 else if (TREE_CODE (type) == ENUMERAL_TYPE
3415 && (TYPE_MAIN_VARIANT (type)
3416 == TYPE_MAIN_VARIANT (valtype)))
3417 /* No warning if function asks for enum
3418 and the actual arg is that enum type. */
3419 ;
3420 else if (formal_prec != TYPE_PRECISION (type1))
3421 warning_at (ploc, OPT_Wtraditional_conversion,
3422 "passing argument %d of %qE "
3423 "with different width due to prototype",
3424 argnum, rname);
3425 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3426 ;
3427 /* Don't complain if the formal parameter type
3428 is an enum, because we can't tell now whether
3429 the value was an enum--even the same enum. */
3430 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3431 ;
3432 else if (TREE_CODE (val) == INTEGER_CST
3433 && int_fits_type_p (val, type))
3434 /* Change in signedness doesn't matter
3435 if a constant value is unaffected. */
3436 ;
3437 /* If the value is extended from a narrower
3438 unsigned type, it doesn't matter whether we
3439 pass it as signed or unsigned; the value
3440 certainly is the same either way. */
3441 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3442 && TYPE_UNSIGNED (valtype))
3443 ;
3444 else if (TYPE_UNSIGNED (type))
3445 warning_at (ploc, OPT_Wtraditional_conversion,
3446 "passing argument %d of %qE "
3447 "as unsigned due to prototype",
3448 argnum, rname);
3449 else
3450 warning_at (ploc, OPT_Wtraditional_conversion,
3451 "passing argument %d of %qE "
3452 "as signed due to prototype",
3453 argnum, rname);
3454 }
3455 }
3456
3457 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3458 sake of better warnings from convert_and_check. */
3459 if (excess_precision)
3460 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3461
3462 tree parmval = convert_for_assignment (ploc, ploc, type,
3463 val, origtype, ic_argpass,
3464 npc, fundecl, function,
3465 parmnum + 1, warnopt);
3466
3467 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3468 && INTEGRAL_TYPE_P (type)
3469 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3470 parmval = default_conversion (parmval);
3471
3472 return parmval;
3473 }
3474
3475 /* Convert the argument expressions in the vector VALUES
3476 to the types in the list TYPELIST.
3477
3478 If TYPELIST is exhausted, or when an element has NULL as its type,
3479 perform the default conversions.
3480
3481 ORIGTYPES is the original types of the expressions in VALUES. This
3482 holds the type of enum values which have been converted to integral
3483 types. It may be NULL.
3484
3485 FUNCTION is a tree for the called function. It is used only for
3486 error messages, where it is formatted with %qE.
3487
3488 This is also where warnings about wrong number of args are generated.
3489
3490 ARG_LOC are locations of function arguments (if any).
3491
3492 Returns the actual number of arguments processed (which may be less
3493 than the length of VALUES in some error situations), or -1 on
3494 failure. */
3495
3496 static int
3497 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3498 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3499 tree function, tree fundecl)
3500 {
3501 unsigned int parmnum;
3502 bool error_args = false;
3503 const bool type_generic = fundecl
3504 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3505 bool type_generic_remove_excess_precision = false;
3506 bool type_generic_overflow_p = false;
3507 tree selector;
3508
3509 /* Change pointer to function to the function itself for
3510 diagnostics. */
3511 if (TREE_CODE (function) == ADDR_EXPR
3512 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3513 function = TREE_OPERAND (function, 0);
3514
3515 /* Handle an ObjC selector specially for diagnostics. */
3516 selector = objc_message_selector ();
3517
3518 /* For a call to a built-in function declared without a prototype,
3519 set to the built-in function's argument list. */
3520 tree builtin_typelist = NULL_TREE;
3521
3522 /* For type-generic built-in functions, determine whether excess
3523 precision should be removed (classification) or not
3524 (comparison). */
3525 if (fundecl
3526 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3527 {
3528 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3529 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3530 {
3531 /* For a call to a built-in function declared without a prototype
3532 use the types of the parameters of the internal built-in to
3533 match those of the arguments to. */
3534 if (tree bdecl = builtin_decl_explicit (code))
3535 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3536 }
3537
3538 /* For type-generic built-in functions, determine whether excess
3539 precision should be removed (classification) or not
3540 (comparison). */
3541 if (type_generic)
3542 switch (code)
3543 {
3544 case BUILT_IN_ISFINITE:
3545 case BUILT_IN_ISINF:
3546 case BUILT_IN_ISINF_SIGN:
3547 case BUILT_IN_ISNAN:
3548 case BUILT_IN_ISNORMAL:
3549 case BUILT_IN_FPCLASSIFY:
3550 type_generic_remove_excess_precision = true;
3551 break;
3552
3553 case BUILT_IN_ADD_OVERFLOW_P:
3554 case BUILT_IN_SUB_OVERFLOW_P:
3555 case BUILT_IN_MUL_OVERFLOW_P:
3556 /* The last argument of these type-generic builtins
3557 should not be promoted. */
3558 type_generic_overflow_p = true;
3559 break;
3560
3561 default:
3562 break;
3563 }
3564 }
3565
3566 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3567 individual converted arguments. */
3568
3569 tree typetail, builtin_typetail, val;
3570 for (typetail = typelist,
3571 builtin_typetail = builtin_typelist,
3572 parmnum = 0;
3573 values && values->iterate (parmnum, &val);
3574 ++parmnum)
3575 {
3576 /* The type of the function parameter (if it was declared with one). */
3577 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3578 /* The type of the built-in function parameter (if the function
3579 is a built-in). Used to detect type incompatibilities in
3580 calls to built-ins declared without a prototype. */
3581 tree builtin_type = (builtin_typetail
3582 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3583 /* The original type of the argument being passed to the function. */
3584 tree valtype = TREE_TYPE (val);
3585 /* The called function (or function selector in Objective C). */
3586 tree rname = function;
3587 int argnum = parmnum + 1;
3588 const char *invalid_func_diag;
3589 /* Set for EXCESS_PRECISION_EXPR arguments. */
3590 bool excess_precision = false;
3591 /* The value of the argument after conversion to the type
3592 of the function parameter it is passed to. */
3593 tree parmval;
3594 /* Some __atomic_* builtins have additional hidden argument at
3595 position 0. */
3596 location_t ploc
3597 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3598 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3599 : input_location;
3600
3601 if (type == void_type_node)
3602 {
3603 if (selector)
3604 error_at (loc, "too many arguments to method %qE", selector);
3605 else
3606 error_at (loc, "too many arguments to function %qE", function);
3607 inform_declaration (fundecl);
3608 return error_args ? -1 : (int) parmnum;
3609 }
3610
3611 if (builtin_type == void_type_node)
3612 {
3613 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3614 "too many arguments to built-in function %qE "
3615 "expecting %d", function, parmnum))
3616 inform_declaration (fundecl);
3617 builtin_typetail = NULL_TREE;
3618 }
3619
3620 if (selector && argnum > 2)
3621 {
3622 rname = selector;
3623 argnum -= 2;
3624 }
3625
3626 /* Determine if VAL is a null pointer constant before folding it. */
3627 bool npc = null_pointer_constant_p (val);
3628
3629 /* If there is excess precision and a prototype, convert once to
3630 the required type rather than converting via the semantic
3631 type. Likewise without a prototype a float value represented
3632 as long double should be converted once to double. But for
3633 type-generic classification functions excess precision must
3634 be removed here. */
3635 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3636 && (type || !type_generic || !type_generic_remove_excess_precision))
3637 {
3638 val = TREE_OPERAND (val, 0);
3639 excess_precision = true;
3640 }
3641 val = c_fully_fold (val, false, NULL);
3642 STRIP_TYPE_NOPS (val);
3643
3644 val = require_complete_type (ploc, val);
3645
3646 /* Some floating-point arguments must be promoted to double when
3647 no type is specified by a prototype. This applies to
3648 arguments of type float, and to architecture-specific types
3649 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3650 bool promote_float_arg = false;
3651 if (type == NULL_TREE
3652 && TREE_CODE (valtype) == REAL_TYPE
3653 && (TYPE_PRECISION (valtype)
3654 <= TYPE_PRECISION (double_type_node))
3655 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3656 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3657 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3658 {
3659 /* Promote this argument, unless it has a _FloatN or
3660 _FloatNx type. */
3661 promote_float_arg = true;
3662 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3663 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3664 {
3665 promote_float_arg = false;
3666 break;
3667 }
3668 }
3669
3670 if (type != NULL_TREE)
3671 {
3672 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3673 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3674 val, valtype, npc, rname, parmnum, argnum,
3675 excess_precision, 0);
3676 }
3677 else if (promote_float_arg)
3678 {
3679 if (type_generic)
3680 parmval = val;
3681 else
3682 {
3683 /* Convert `float' to `double'. */
3684 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3685 warning_at (ploc, OPT_Wdouble_promotion,
3686 "implicit conversion from %qT to %qT when passing "
3687 "argument to function",
3688 valtype, double_type_node);
3689 parmval = convert (double_type_node, val);
3690 }
3691 }
3692 else if ((excess_precision && !type_generic)
3693 || (type_generic_overflow_p && parmnum == 2))
3694 /* A "double" argument with excess precision being passed
3695 without a prototype or in variable arguments.
3696 The last argument of __builtin_*_overflow_p should not be
3697 promoted. */
3698 parmval = convert (valtype, val);
3699 else if ((invalid_func_diag =
3700 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3701 {
3702 error (invalid_func_diag);
3703 return -1;
3704 }
3705 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3706 {
3707 return -1;
3708 }
3709 else
3710 /* Convert `short' and `char' to full-size `int'. */
3711 parmval = default_conversion (val);
3712
3713 (*values)[parmnum] = parmval;
3714 if (parmval == error_mark_node)
3715 error_args = true;
3716
3717 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3718 {
3719 /* For a call to a built-in function declared without a prototype,
3720 perform the conversions from the argument to the expected type
3721 but issue warnings rather than errors for any mismatches.
3722 Ignore the converted argument and use the PARMVAL obtained
3723 above by applying default conversions instead. */
3724 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3725 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3726 val, valtype, npc, rname, parmnum, argnum,
3727 excess_precision,
3728 OPT_Wbuiltin_declaration_mismatch);
3729 }
3730
3731 if (typetail)
3732 typetail = TREE_CHAIN (typetail);
3733
3734 if (builtin_typetail)
3735 builtin_typetail = TREE_CHAIN (builtin_typetail);
3736 }
3737
3738 gcc_assert (parmnum == vec_safe_length (values));
3739
3740 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3741 {
3742 error_at (loc, "too few arguments to function %qE", function);
3743 inform_declaration (fundecl);
3744 return -1;
3745 }
3746
3747 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3748 {
3749 unsigned nargs = parmnum;
3750 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3751 ++nargs;
3752
3753 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3754 "too few arguments to built-in function %qE "
3755 "expecting %u", function, nargs - 1))
3756 inform_declaration (fundecl);
3757 }
3758
3759 return error_args ? -1 : (int) parmnum;
3760 }
3761 \f
3762 /* This is the entry point used by the parser to build unary operators
3763 in the input. CODE, a tree_code, specifies the unary operator, and
3764 ARG is the operand. For unary plus, the C parser currently uses
3765 CONVERT_EXPR for code.
3766
3767 LOC is the location to use for the tree generated.
3768 */
3769
3770 struct c_expr
3771 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3772 {
3773 struct c_expr result;
3774
3775 result.original_code = code;
3776 result.original_type = NULL;
3777
3778 if (reject_gcc_builtin (arg.value))
3779 {
3780 result.value = error_mark_node;
3781 }
3782 else
3783 {
3784 result.value = build_unary_op (loc, code, arg.value, false);
3785
3786 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3787 overflow_warning (loc, result.value, arg.value);
3788 }
3789
3790 /* We are typically called when parsing a prefix token at LOC acting on
3791 ARG. Reflect this by updating the source range of the result to
3792 start at LOC and end at the end of ARG. */
3793 set_c_expr_source_range (&result,
3794 loc, arg.get_finish ());
3795
3796 return result;
3797 }
3798
3799 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3800
3801 bool
3802 char_type_p (tree type)
3803 {
3804 return (type == char_type_node
3805 || type == unsigned_char_type_node
3806 || type == signed_char_type_node
3807 || type == char16_type_node
3808 || type == char32_type_node);
3809 }
3810
3811 /* This is the entry point used by the parser to build binary operators
3812 in the input. CODE, a tree_code, specifies the binary operator, and
3813 ARG1 and ARG2 are the operands. In addition to constructing the
3814 expression, we check for operands that were written with other binary
3815 operators in a way that is likely to confuse the user.
3816
3817 LOCATION is the location of the binary operator. */
3818
3819 struct c_expr
3820 parser_build_binary_op (location_t location, enum tree_code code,
3821 struct c_expr arg1, struct c_expr arg2)
3822 {
3823 struct c_expr result;
3824
3825 enum tree_code code1 = arg1.original_code;
3826 enum tree_code code2 = arg2.original_code;
3827 tree type1 = (arg1.original_type
3828 ? arg1.original_type
3829 : TREE_TYPE (arg1.value));
3830 tree type2 = (arg2.original_type
3831 ? arg2.original_type
3832 : TREE_TYPE (arg2.value));
3833
3834 result.value = build_binary_op (location, code,
3835 arg1.value, arg2.value, true);
3836 result.original_code = code;
3837 result.original_type = NULL;
3838
3839 if (TREE_CODE (result.value) == ERROR_MARK)
3840 {
3841 set_c_expr_source_range (&result,
3842 arg1.get_start (),
3843 arg2.get_finish ());
3844 return result;
3845 }
3846
3847 if (location != UNKNOWN_LOCATION)
3848 protected_set_expr_location (result.value, location);
3849
3850 set_c_expr_source_range (&result,
3851 arg1.get_start (),
3852 arg2.get_finish ());
3853
3854 /* Check for cases such as x+y<<z which users are likely
3855 to misinterpret. */
3856 if (warn_parentheses)
3857 warn_about_parentheses (location, code, code1, arg1.value, code2,
3858 arg2.value);
3859
3860 if (warn_logical_op)
3861 warn_logical_operator (location, code, TREE_TYPE (result.value),
3862 code1, arg1.value, code2, arg2.value);
3863
3864 if (warn_tautological_compare)
3865 {
3866 tree lhs = arg1.value;
3867 tree rhs = arg2.value;
3868 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3869 {
3870 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3871 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3872 lhs = NULL_TREE;
3873 else
3874 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3875 }
3876 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3877 {
3878 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3879 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3880 rhs = NULL_TREE;
3881 else
3882 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3883 }
3884 if (lhs != NULL_TREE && rhs != NULL_TREE)
3885 warn_tautological_cmp (location, code, lhs, rhs);
3886 }
3887
3888 if (warn_logical_not_paren
3889 && TREE_CODE_CLASS (code) == tcc_comparison
3890 && code1 == TRUTH_NOT_EXPR
3891 && code2 != TRUTH_NOT_EXPR
3892 /* Avoid warning for !!x == y. */
3893 && (TREE_CODE (arg1.value) != NE_EXPR
3894 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3895 {
3896 /* Avoid warning for !b == y where b has _Bool type. */
3897 tree t = integer_zero_node;
3898 if (TREE_CODE (arg1.value) == EQ_EXPR
3899 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3900 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3901 {
3902 t = TREE_OPERAND (arg1.value, 0);
3903 do
3904 {
3905 if (TREE_TYPE (t) != integer_type_node)
3906 break;
3907 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3908 t = C_MAYBE_CONST_EXPR_EXPR (t);
3909 else if (CONVERT_EXPR_P (t))
3910 t = TREE_OPERAND (t, 0);
3911 else
3912 break;
3913 }
3914 while (1);
3915 }
3916 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3917 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3918 }
3919
3920 /* Warn about comparisons against string literals, with the exception
3921 of testing for equality or inequality of a string literal with NULL. */
3922 if (code == EQ_EXPR || code == NE_EXPR)
3923 {
3924 if ((code1 == STRING_CST
3925 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3926 || (code2 == STRING_CST
3927 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3928 warning_at (location, OPT_Waddress,
3929 "comparison with string literal results in unspecified behavior");
3930 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3931 if (POINTER_TYPE_P (type1)
3932 && null_pointer_constant_p (arg2.value)
3933 && char_type_p (type2))
3934 {
3935 auto_diagnostic_group d;
3936 if (warning_at (location, OPT_Wpointer_compare,
3937 "comparison between pointer and zero character "
3938 "constant"))
3939 inform (arg1.get_start (),
3940 "did you mean to dereference the pointer?");
3941 }
3942 else if (POINTER_TYPE_P (type2)
3943 && null_pointer_constant_p (arg1.value)
3944 && char_type_p (type1))
3945 {
3946 auto_diagnostic_group d;
3947 if (warning_at (location, OPT_Wpointer_compare,
3948 "comparison between pointer and zero character "
3949 "constant"))
3950 inform (arg2.get_start (),
3951 "did you mean to dereference the pointer?");
3952 }
3953 }
3954 else if (TREE_CODE_CLASS (code) == tcc_comparison
3955 && (code1 == STRING_CST || code2 == STRING_CST))
3956 warning_at (location, OPT_Waddress,
3957 "comparison with string literal results in unspecified "
3958 "behavior");
3959
3960 if (warn_array_compare
3961 && TREE_CODE_CLASS (code) == tcc_comparison
3962 && TREE_CODE (type1) == ARRAY_TYPE
3963 && TREE_CODE (type2) == ARRAY_TYPE)
3964 do_warn_array_compare (location, code, arg1.value, arg2.value);
3965
3966 if (TREE_OVERFLOW_P (result.value)
3967 && !TREE_OVERFLOW_P (arg1.value)
3968 && !TREE_OVERFLOW_P (arg2.value))
3969 overflow_warning (location, result.value);
3970
3971 /* Warn about comparisons of different enum types. */
3972 if (warn_enum_compare
3973 && TREE_CODE_CLASS (code) == tcc_comparison
3974 && TREE_CODE (type1) == ENUMERAL_TYPE
3975 && TREE_CODE (type2) == ENUMERAL_TYPE
3976 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3977 warning_at (location, OPT_Wenum_compare,
3978 "comparison between %qT and %qT",
3979 type1, type2);
3980
3981 return result;
3982 }
3983 \f
3984 /* Return a tree for the difference of pointers OP0 and OP1.
3985 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3986 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3987
3988 static tree
3989 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3990 {
3991 tree restype = ptrdiff_type_node;
3992 tree result, inttype;
3993
3994 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3995 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3996 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3997 tree orig_op0 = op0;
3998 tree orig_op1 = op1;
3999
4000 /* If the operands point into different address spaces, we need to
4001 explicitly convert them to pointers into the common address space
4002 before we can subtract the numerical address values. */
4003 if (as0 != as1)
4004 {
4005 addr_space_t as_common;
4006 tree common_type;
4007
4008 /* Determine the common superset address space. This is guaranteed
4009 to exist because the caller verified that comp_target_types
4010 returned non-zero. */
4011 if (!addr_space_superset (as0, as1, &as_common))
4012 gcc_unreachable ();
4013
4014 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4015 op0 = convert (common_type, op0);
4016 op1 = convert (common_type, op1);
4017 }
4018
4019 /* Determine integer type result of the subtraction. This will usually
4020 be the same as the result type (ptrdiff_t), but may need to be a wider
4021 type if pointers for the address space are wider than ptrdiff_t. */
4022 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4023 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4024 else
4025 inttype = restype;
4026
4027 if (TREE_CODE (target_type) == VOID_TYPE)
4028 pedwarn (loc, OPT_Wpointer_arith,
4029 "pointer of type %<void *%> used in subtraction");
4030 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4031 pedwarn (loc, OPT_Wpointer_arith,
4032 "pointer to a function used in subtraction");
4033
4034 if (current_function_decl != NULL_TREE
4035 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4036 {
4037 op0 = save_expr (op0);
4038 op1 = save_expr (op1);
4039
4040 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4041 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4042 }
4043
4044 /* First do the subtraction, then build the divide operator
4045 and only convert at the very end.
4046 Do not do default conversions in case restype is a short type. */
4047
4048 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4049 pointers. If some platform cannot provide that, or has a larger
4050 ptrdiff_type to support differences larger than half the address
4051 space, cast the pointers to some larger integer type and do the
4052 computations in that type. */
4053 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4054 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4055 convert (inttype, op1), false);
4056 else
4057 {
4058 /* Cast away qualifiers. */
4059 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4060 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4061 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4062 }
4063
4064 /* This generates an error if op1 is pointer to incomplete type. */
4065 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4066 error_at (loc, "arithmetic on pointer to an incomplete type");
4067 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4068 TREE_TYPE (TREE_TYPE (orig_op0))))
4069 verify_type_context (loc, TCTX_POINTER_ARITH,
4070 TREE_TYPE (TREE_TYPE (orig_op1)));
4071
4072 op1 = c_size_in_bytes (target_type);
4073
4074 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4075 error_at (loc, "arithmetic on pointer to an empty aggregate");
4076
4077 /* Divide by the size, in easiest possible way. */
4078 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4079 op0, convert (inttype, op1));
4080
4081 /* Convert to final result type if necessary. */
4082 return convert (restype, result);
4083 }
4084 \f
4085 /* Expand atomic compound assignments into an appropriate sequence as
4086 specified by the C11 standard section 6.5.16.2.
4087
4088 _Atomic T1 E1
4089 T2 E2
4090 E1 op= E2
4091
4092 This sequence is used for all types for which these operations are
4093 supported.
4094
4095 In addition, built-in versions of the 'fe' prefixed routines may
4096 need to be invoked for floating point (real, complex or vector) when
4097 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4098
4099 T1 newval;
4100 T1 old;
4101 T1 *addr
4102 T2 val
4103 fenv_t fenv
4104
4105 addr = &E1;
4106 val = (E2);
4107 __atomic_load (addr, &old, SEQ_CST);
4108 feholdexcept (&fenv);
4109 loop:
4110 newval = old op val;
4111 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4112 SEQ_CST))
4113 goto done;
4114 feclearexcept (FE_ALL_EXCEPT);
4115 goto loop:
4116 done:
4117 feupdateenv (&fenv);
4118
4119 The compiler will issue the __atomic_fetch_* built-in when possible,
4120 otherwise it will generate the generic form of the atomic operations.
4121 This requires temp(s) and has their address taken. The atomic processing
4122 is smart enough to figure out when the size of an object can utilize
4123 a lock-free version, and convert the built-in call to the appropriate
4124 lock-free routine. The optimizers will then dispose of any temps that
4125 are no longer required, and lock-free implementations are utilized as
4126 long as there is target support for the required size.
4127
4128 If the operator is NOP_EXPR, then this is a simple assignment, and
4129 an __atomic_store is issued to perform the assignment rather than
4130 the above loop. */
4131
4132 /* Build an atomic assignment at LOC, expanding into the proper
4133 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4134 the result of the operation, unless RETURN_OLD_P, in which case
4135 return the old value of LHS (this is only for postincrement and
4136 postdecrement). */
4137
4138 static tree
4139 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4140 tree rhs, bool return_old_p)
4141 {
4142 tree fndecl, func_call;
4143 vec<tree, va_gc> *params;
4144 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4145 tree old, old_addr;
4146 tree compound_stmt = NULL_TREE;
4147 tree stmt, goto_stmt;
4148 tree loop_label, loop_decl, done_label, done_decl;
4149
4150 tree lhs_type = TREE_TYPE (lhs);
4151 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4152 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4153 tree rhs_semantic_type = TREE_TYPE (rhs);
4154 tree nonatomic_rhs_semantic_type;
4155 tree rhs_type;
4156
4157 gcc_assert (TYPE_ATOMIC (lhs_type));
4158
4159 if (return_old_p)
4160 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4161
4162 /* Allocate enough vector items for a compare_exchange. */
4163 vec_alloc (params, 6);
4164
4165 /* Create a compound statement to hold the sequence of statements
4166 with a loop. */
4167 if (modifycode != NOP_EXPR)
4168 {
4169 compound_stmt = c_begin_compound_stmt (false);
4170
4171 /* For consistency with build_modify_expr on non-_Atomic,
4172 mark the lhs as read. Also, it would be very hard to match
4173 such expressions in mark_exp_read. */
4174 mark_exp_read (lhs);
4175 }
4176
4177 /* Remove any excess precision (which is only present here in the
4178 case of compound assignments). */
4179 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4180 {
4181 gcc_assert (modifycode != NOP_EXPR);
4182 rhs = TREE_OPERAND (rhs, 0);
4183 }
4184 rhs_type = TREE_TYPE (rhs);
4185
4186 /* Fold the RHS if it hasn't already been folded. */
4187 if (modifycode != NOP_EXPR)
4188 rhs = c_fully_fold (rhs, false, NULL);
4189
4190 /* Remove the qualifiers for the rest of the expressions and create
4191 the VAL temp variable to hold the RHS. */
4192 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4193 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4194 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4195 TYPE_UNQUALIFIED);
4196 val = create_tmp_var_raw (nonatomic_rhs_type);
4197 TREE_ADDRESSABLE (val) = 1;
4198 suppress_warning (val);
4199 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4200 NULL_TREE);
4201 TREE_SIDE_EFFECTS (rhs) = 1;
4202 SET_EXPR_LOCATION (rhs, loc);
4203 if (modifycode != NOP_EXPR)
4204 add_stmt (rhs);
4205
4206 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4207 an atomic_store. */
4208 if (modifycode == NOP_EXPR)
4209 {
4210 compound_stmt = rhs;
4211 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4212 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4213 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4214 params->quick_push (lhs_addr);
4215 params->quick_push (rhs);
4216 params->quick_push (seq_cst);
4217 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4218
4219 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4220 compound_stmt, func_call);
4221
4222 /* VAL is the value which was stored, return a COMPOUND_STMT of
4223 the statement and that value. */
4224 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4225 }
4226
4227 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4228 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4229 isn't applicable for such builtins. ??? Do we want to handle enums? */
4230 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4231 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4232 {
4233 built_in_function fncode;
4234 switch (modifycode)
4235 {
4236 case PLUS_EXPR:
4237 case POINTER_PLUS_EXPR:
4238 fncode = (return_old_p
4239 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4240 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4241 break;
4242 case MINUS_EXPR:
4243 fncode = (return_old_p
4244 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4245 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4246 break;
4247 case BIT_AND_EXPR:
4248 fncode = (return_old_p
4249 ? BUILT_IN_ATOMIC_FETCH_AND_N
4250 : BUILT_IN_ATOMIC_AND_FETCH_N);
4251 break;
4252 case BIT_IOR_EXPR:
4253 fncode = (return_old_p
4254 ? BUILT_IN_ATOMIC_FETCH_OR_N
4255 : BUILT_IN_ATOMIC_OR_FETCH_N);
4256 break;
4257 case BIT_XOR_EXPR:
4258 fncode = (return_old_p
4259 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4260 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4261 break;
4262 default:
4263 goto cas_loop;
4264 }
4265
4266 /* We can only use "_1" through "_16" variants of the atomic fetch
4267 built-ins. */
4268 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4269 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4270 goto cas_loop;
4271
4272 /* If this is a pointer type, we need to multiply by the size of
4273 the pointer target type. */
4274 if (POINTER_TYPE_P (lhs_type))
4275 {
4276 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4277 /* ??? This would introduce -Wdiscarded-qualifiers
4278 warning: __atomic_fetch_* expect volatile void *
4279 type as the first argument. (Assignments between
4280 atomic and non-atomic objects are OK.) */
4281 || TYPE_RESTRICT (lhs_type))
4282 goto cas_loop;
4283 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4284 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4285 convert (ptrdiff_type_node, rhs),
4286 convert (ptrdiff_type_node, sz));
4287 }
4288
4289 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4290 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4291 fndecl = builtin_decl_explicit (fncode);
4292 params->quick_push (lhs_addr);
4293 params->quick_push (rhs);
4294 params->quick_push (seq_cst);
4295 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4296
4297 newval = create_tmp_var_raw (nonatomic_lhs_type);
4298 TREE_ADDRESSABLE (newval) = 1;
4299 suppress_warning (newval);
4300 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4301 NULL_TREE, NULL_TREE);
4302 SET_EXPR_LOCATION (rhs, loc);
4303 add_stmt (rhs);
4304
4305 /* Finish the compound statement. */
4306 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4307
4308 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4309 the statement and that value. */
4310 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4311 }
4312
4313 cas_loop:
4314 /* Create the variables and labels required for the op= form. */
4315 old = create_tmp_var_raw (nonatomic_lhs_type);
4316 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4317 TREE_ADDRESSABLE (old) = 1;
4318 suppress_warning (old);
4319
4320 newval = create_tmp_var_raw (nonatomic_lhs_type);
4321 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4322 TREE_ADDRESSABLE (newval) = 1;
4323 suppress_warning (newval);
4324
4325 loop_decl = create_artificial_label (loc);
4326 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4327
4328 done_decl = create_artificial_label (loc);
4329 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4330
4331 /* __atomic_load (addr, &old, SEQ_CST). */
4332 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4333 params->quick_push (lhs_addr);
4334 params->quick_push (old_addr);
4335 params->quick_push (seq_cst);
4336 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4337 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4338 NULL_TREE);
4339 add_stmt (old);
4340 params->truncate (0);
4341
4342 /* Create the expressions for floating-point environment
4343 manipulation, if required. */
4344 bool need_fenv = (flag_trapping_math
4345 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4346 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4347 if (need_fenv)
4348 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4349
4350 if (hold_call)
4351 add_stmt (hold_call);
4352
4353 /* loop: */
4354 add_stmt (loop_label);
4355
4356 /* newval = old + val; */
4357 if (rhs_type != rhs_semantic_type)
4358 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4359 rhs = build_binary_op (loc, modifycode, old, val, true);
4360 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4361 {
4362 tree eptype = TREE_TYPE (rhs);
4363 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4364 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4365 }
4366 else
4367 rhs = c_fully_fold (rhs, false, NULL);
4368 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4369 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4370 NULL_TREE, 0);
4371 if (rhs != error_mark_node)
4372 {
4373 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4374 NULL_TREE);
4375 SET_EXPR_LOCATION (rhs, loc);
4376 add_stmt (rhs);
4377 }
4378
4379 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4380 goto done; */
4381 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4382 params->quick_push (lhs_addr);
4383 params->quick_push (old_addr);
4384 params->quick_push (newval_addr);
4385 params->quick_push (integer_zero_node);
4386 params->quick_push (seq_cst);
4387 params->quick_push (seq_cst);
4388 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4389
4390 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4391 SET_EXPR_LOCATION (goto_stmt, loc);
4392
4393 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4394 SET_EXPR_LOCATION (stmt, loc);
4395 add_stmt (stmt);
4396
4397 if (clear_call)
4398 add_stmt (clear_call);
4399
4400 /* goto loop; */
4401 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4402 SET_EXPR_LOCATION (goto_stmt, loc);
4403 add_stmt (goto_stmt);
4404
4405 /* done: */
4406 add_stmt (done_label);
4407
4408 if (update_call)
4409 add_stmt (update_call);
4410
4411 /* Finish the compound statement. */
4412 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4413
4414 /* NEWVAL is the value that was successfully stored, return a
4415 COMPOUND_EXPR of the statement and the appropriate value. */
4416 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4417 return_old_p ? old : newval);
4418 }
4419
4420 /* Construct and perhaps optimize a tree representation
4421 for a unary operation. CODE, a tree_code, specifies the operation
4422 and XARG is the operand.
4423 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4424 promotions (such as from short to int).
4425 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4426 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4427 to pointers in C99.
4428
4429 LOCATION is the location of the operator. */
4430
4431 tree
4432 build_unary_op (location_t location, enum tree_code code, tree xarg,
4433 bool noconvert)
4434 {
4435 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4436 tree arg = xarg;
4437 tree argtype = NULL_TREE;
4438 enum tree_code typecode;
4439 tree val;
4440 tree ret = error_mark_node;
4441 tree eptype = NULL_TREE;
4442 const char *invalid_op_diag;
4443 bool int_operands;
4444
4445 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4446 if (int_operands)
4447 arg = remove_c_maybe_const_expr (arg);
4448
4449 if (code != ADDR_EXPR)
4450 arg = require_complete_type (location, arg);
4451
4452 typecode = TREE_CODE (TREE_TYPE (arg));
4453 if (typecode == ERROR_MARK)
4454 return error_mark_node;
4455 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4456 typecode = INTEGER_TYPE;
4457
4458 if ((invalid_op_diag
4459 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4460 {
4461 error_at (location, invalid_op_diag);
4462 return error_mark_node;
4463 }
4464
4465 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4466 {
4467 eptype = TREE_TYPE (arg);
4468 arg = TREE_OPERAND (arg, 0);
4469 }
4470
4471 switch (code)
4472 {
4473 case CONVERT_EXPR:
4474 /* This is used for unary plus, because a CONVERT_EXPR
4475 is enough to prevent anybody from looking inside for
4476 associativity, but won't generate any code. */
4477 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4478 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4479 || gnu_vector_type_p (TREE_TYPE (arg))))
4480 {
4481 error_at (location, "wrong type argument to unary plus");
4482 return error_mark_node;
4483 }
4484 else if (!noconvert)
4485 arg = default_conversion (arg);
4486 arg = non_lvalue_loc (location, arg);
4487 break;
4488
4489 case NEGATE_EXPR:
4490 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4491 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4492 || gnu_vector_type_p (TREE_TYPE (arg))))
4493 {
4494 error_at (location, "wrong type argument to unary minus");
4495 return error_mark_node;
4496 }
4497 else if (!noconvert)
4498 arg = default_conversion (arg);
4499 break;
4500
4501 case BIT_NOT_EXPR:
4502 /* ~ works on integer types and non float vectors. */
4503 if (typecode == INTEGER_TYPE
4504 || (gnu_vector_type_p (TREE_TYPE (arg))
4505 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4506 {
4507 tree e = arg;
4508
4509 /* Warn if the expression has boolean value. */
4510 while (TREE_CODE (e) == COMPOUND_EXPR)
4511 e = TREE_OPERAND (e, 1);
4512
4513 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4514 || truth_value_p (TREE_CODE (e))))
4515 {
4516 auto_diagnostic_group d;
4517 if (warning_at (location, OPT_Wbool_operation,
4518 "%<~%> on a boolean expression"))
4519 {
4520 gcc_rich_location richloc (location);
4521 richloc.add_fixit_insert_before (location, "!");
4522 inform (&richloc, "did you mean to use logical not?");
4523 }
4524 }
4525 if (!noconvert)
4526 arg = default_conversion (arg);
4527 }
4528 else if (typecode == COMPLEX_TYPE)
4529 {
4530 code = CONJ_EXPR;
4531 pedwarn (location, OPT_Wpedantic,
4532 "ISO C does not support %<~%> for complex conjugation");
4533 if (!noconvert)
4534 arg = default_conversion (arg);
4535 }
4536 else
4537 {
4538 error_at (location, "wrong type argument to bit-complement");
4539 return error_mark_node;
4540 }
4541 break;
4542
4543 case ABS_EXPR:
4544 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4545 {
4546 error_at (location, "wrong type argument to abs");
4547 return error_mark_node;
4548 }
4549 else if (!noconvert)
4550 arg = default_conversion (arg);
4551 break;
4552
4553 case ABSU_EXPR:
4554 if (!(typecode == INTEGER_TYPE))
4555 {
4556 error_at (location, "wrong type argument to absu");
4557 return error_mark_node;
4558 }
4559 else if (!noconvert)
4560 arg = default_conversion (arg);
4561 break;
4562
4563 case CONJ_EXPR:
4564 /* Conjugating a real value is a no-op, but allow it anyway. */
4565 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4566 || typecode == COMPLEX_TYPE))
4567 {
4568 error_at (location, "wrong type argument to conjugation");
4569 return error_mark_node;
4570 }
4571 else if (!noconvert)
4572 arg = default_conversion (arg);
4573 break;
4574
4575 case TRUTH_NOT_EXPR:
4576 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4577 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4578 && typecode != COMPLEX_TYPE)
4579 {
4580 error_at (location,
4581 "wrong type argument to unary exclamation mark");
4582 return error_mark_node;
4583 }
4584 if (int_operands)
4585 {
4586 arg = c_objc_common_truthvalue_conversion (location, xarg);
4587 arg = remove_c_maybe_const_expr (arg);
4588 }
4589 else
4590 arg = c_objc_common_truthvalue_conversion (location, arg);
4591 ret = invert_truthvalue_loc (location, arg);
4592 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4593 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4594 location = EXPR_LOCATION (ret);
4595 goto return_build_unary_op;
4596
4597 case REALPART_EXPR:
4598 case IMAGPART_EXPR:
4599 ret = build_real_imag_expr (location, code, arg);
4600 if (ret == error_mark_node)
4601 return error_mark_node;
4602 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4603 eptype = TREE_TYPE (eptype);
4604 goto return_build_unary_op;
4605
4606 case PREINCREMENT_EXPR:
4607 case POSTINCREMENT_EXPR:
4608 case PREDECREMENT_EXPR:
4609 case POSTDECREMENT_EXPR:
4610
4611 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4612 {
4613 tree inner = build_unary_op (location, code,
4614 C_MAYBE_CONST_EXPR_EXPR (arg),
4615 noconvert);
4616 if (inner == error_mark_node)
4617 return error_mark_node;
4618 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4619 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4620 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4621 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4622 goto return_build_unary_op;
4623 }
4624
4625 /* Complain about anything that is not a true lvalue. In
4626 Objective-C, skip this check for property_refs. */
4627 if (!objc_is_property_ref (arg)
4628 && !lvalue_or_else (location,
4629 arg, ((code == PREINCREMENT_EXPR
4630 || code == POSTINCREMENT_EXPR)
4631 ? lv_increment
4632 : lv_decrement)))
4633 return error_mark_node;
4634
4635 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4636 {
4637 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4638 warning_at (location, OPT_Wc___compat,
4639 "increment of enumeration value is invalid in C++");
4640 else
4641 warning_at (location, OPT_Wc___compat,
4642 "decrement of enumeration value is invalid in C++");
4643 }
4644
4645 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4646 {
4647 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4648 warning_at (location, OPT_Wbool_operation,
4649 "increment of a boolean expression");
4650 else
4651 warning_at (location, OPT_Wbool_operation,
4652 "decrement of a boolean expression");
4653 }
4654
4655 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4656 arg = c_fully_fold (arg, false, NULL, true);
4657
4658 bool atomic_op;
4659 atomic_op = really_atomic_lvalue (arg);
4660
4661 /* Increment or decrement the real part of the value,
4662 and don't change the imaginary part. */
4663 if (typecode == COMPLEX_TYPE)
4664 {
4665 tree real, imag;
4666
4667 pedwarn (location, OPT_Wpedantic,
4668 "ISO C does not support %<++%> and %<--%> on complex types");
4669
4670 if (!atomic_op)
4671 {
4672 arg = stabilize_reference (arg);
4673 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4674 true);
4675 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4676 true);
4677 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4678 if (real == error_mark_node || imag == error_mark_node)
4679 return error_mark_node;
4680 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4681 real, imag);
4682 goto return_build_unary_op;
4683 }
4684 }
4685
4686 /* Report invalid types. */
4687
4688 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4689 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4690 && typecode != COMPLEX_TYPE
4691 && !gnu_vector_type_p (TREE_TYPE (arg)))
4692 {
4693 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4694 error_at (location, "wrong type argument to increment");
4695 else
4696 error_at (location, "wrong type argument to decrement");
4697
4698 return error_mark_node;
4699 }
4700
4701 {
4702 tree inc;
4703
4704 argtype = TREE_TYPE (arg);
4705
4706 /* Compute the increment. */
4707
4708 if (typecode == POINTER_TYPE)
4709 {
4710 /* If pointer target is an incomplete type,
4711 we just cannot know how to do the arithmetic. */
4712 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4713 {
4714 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4715 error_at (location,
4716 "increment of pointer to an incomplete type %qT",
4717 TREE_TYPE (argtype));
4718 else
4719 error_at (location,
4720 "decrement of pointer to an incomplete type %qT",
4721 TREE_TYPE (argtype));
4722 }
4723 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4724 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4725 {
4726 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4727 pedwarn (location, OPT_Wpointer_arith,
4728 "wrong type argument to increment");
4729 else
4730 pedwarn (location, OPT_Wpointer_arith,
4731 "wrong type argument to decrement");
4732 }
4733 else
4734 verify_type_context (location, TCTX_POINTER_ARITH,
4735 TREE_TYPE (argtype));
4736
4737 inc = c_size_in_bytes (TREE_TYPE (argtype));
4738 inc = convert_to_ptrofftype_loc (location, inc);
4739 }
4740 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4741 {
4742 /* For signed fract types, we invert ++ to -- or
4743 -- to ++, and change inc from 1 to -1, because
4744 it is not possible to represent 1 in signed fract constants.
4745 For unsigned fract types, the result always overflows and
4746 we get an undefined (original) or the maximum value. */
4747 if (code == PREINCREMENT_EXPR)
4748 code = PREDECREMENT_EXPR;
4749 else if (code == PREDECREMENT_EXPR)
4750 code = PREINCREMENT_EXPR;
4751 else if (code == POSTINCREMENT_EXPR)
4752 code = POSTDECREMENT_EXPR;
4753 else /* code == POSTDECREMENT_EXPR */
4754 code = POSTINCREMENT_EXPR;
4755
4756 inc = integer_minus_one_node;
4757 inc = convert (argtype, inc);
4758 }
4759 else
4760 {
4761 inc = VECTOR_TYPE_P (argtype)
4762 ? build_one_cst (argtype)
4763 : integer_one_node;
4764 inc = convert (argtype, inc);
4765 }
4766
4767 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4768 need to ask Objective-C to build the increment or decrement
4769 expression for it. */
4770 if (objc_is_property_ref (arg))
4771 return objc_build_incr_expr_for_property_ref (location, code,
4772 arg, inc);
4773
4774 /* Report a read-only lvalue. */
4775 if (TYPE_READONLY (argtype))
4776 {
4777 readonly_error (location, arg,
4778 ((code == PREINCREMENT_EXPR
4779 || code == POSTINCREMENT_EXPR)
4780 ? lv_increment : lv_decrement));
4781 return error_mark_node;
4782 }
4783 else if (TREE_READONLY (arg))
4784 readonly_warning (arg,
4785 ((code == PREINCREMENT_EXPR
4786 || code == POSTINCREMENT_EXPR)
4787 ? lv_increment : lv_decrement));
4788
4789 /* If the argument is atomic, use the special code sequences for
4790 atomic compound assignment. */
4791 if (atomic_op)
4792 {
4793 arg = stabilize_reference (arg);
4794 ret = build_atomic_assign (location, arg,
4795 ((code == PREINCREMENT_EXPR
4796 || code == POSTINCREMENT_EXPR)
4797 ? PLUS_EXPR
4798 : MINUS_EXPR),
4799 (FRACT_MODE_P (TYPE_MODE (argtype))
4800 ? inc
4801 : integer_one_node),
4802 (code == POSTINCREMENT_EXPR
4803 || code == POSTDECREMENT_EXPR));
4804 goto return_build_unary_op;
4805 }
4806
4807 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4808 val = boolean_increment (code, arg);
4809 else
4810 val = build2 (code, TREE_TYPE (arg), arg, inc);
4811 TREE_SIDE_EFFECTS (val) = 1;
4812 ret = val;
4813 goto return_build_unary_op;
4814 }
4815
4816 case ADDR_EXPR:
4817 /* Note that this operation never does default_conversion. */
4818
4819 /* The operand of unary '&' must be an lvalue (which excludes
4820 expressions of type void), or, in C99, the result of a [] or
4821 unary '*' operator. */
4822 if (VOID_TYPE_P (TREE_TYPE (arg))
4823 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4824 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4825 pedwarn (location, 0, "taking address of expression of type %<void%>");
4826
4827 /* Let &* cancel out to simplify resulting code. */
4828 if (INDIRECT_REF_P (arg))
4829 {
4830 /* Don't let this be an lvalue. */
4831 if (lvalue_p (TREE_OPERAND (arg, 0)))
4832 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4833 ret = TREE_OPERAND (arg, 0);
4834 goto return_build_unary_op;
4835 }
4836
4837 /* Anything not already handled and not a true memory reference
4838 or a non-lvalue array is an error. */
4839 if (typecode != FUNCTION_TYPE && !noconvert
4840 && !lvalue_or_else (location, arg, lv_addressof))
4841 return error_mark_node;
4842
4843 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4844 folding later. */
4845 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4846 {
4847 tree inner = build_unary_op (location, code,
4848 C_MAYBE_CONST_EXPR_EXPR (arg),
4849 noconvert);
4850 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4851 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4852 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4853 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4854 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4855 goto return_build_unary_op;
4856 }
4857
4858 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4859 argtype = TREE_TYPE (arg);
4860
4861 /* If the lvalue is const or volatile, merge that into the type
4862 to which the address will point. This is only needed
4863 for function types. */
4864 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4865 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4866 && TREE_CODE (argtype) == FUNCTION_TYPE)
4867 {
4868 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4869 int quals = orig_quals;
4870
4871 if (TREE_READONLY (arg))
4872 quals |= TYPE_QUAL_CONST;
4873 if (TREE_THIS_VOLATILE (arg))
4874 quals |= TYPE_QUAL_VOLATILE;
4875
4876 argtype = c_build_qualified_type (argtype, quals);
4877 }
4878
4879 switch (TREE_CODE (arg))
4880 {
4881 case COMPONENT_REF:
4882 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4883 {
4884 error_at (location, "cannot take address of bit-field %qD",
4885 TREE_OPERAND (arg, 1));
4886 return error_mark_node;
4887 }
4888
4889 /* fall through */
4890
4891 case ARRAY_REF:
4892 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4893 {
4894 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4895 && !POINTER_TYPE_P (TREE_TYPE (arg))
4896 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4897 {
4898 error_at (location, "cannot take address of scalar with "
4899 "reverse storage order");
4900 return error_mark_node;
4901 }
4902
4903 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4904 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4905 warning_at (location, OPT_Wscalar_storage_order,
4906 "address of array with reverse scalar storage "
4907 "order requested");
4908 }
4909
4910 default:
4911 break;
4912 }
4913
4914 if (!c_mark_addressable (arg))
4915 return error_mark_node;
4916
4917 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4918 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4919
4920 argtype = build_pointer_type (argtype);
4921
4922 /* ??? Cope with user tricks that amount to offsetof. Delete this
4923 when we have proper support for integer constant expressions. */
4924 val = get_base_address (arg);
4925 if (val && INDIRECT_REF_P (val)
4926 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4927 {
4928 ret = fold_offsetof (arg, argtype);
4929 goto return_build_unary_op;
4930 }
4931
4932 val = build1 (ADDR_EXPR, argtype, arg);
4933
4934 ret = val;
4935 goto return_build_unary_op;
4936
4937 case PAREN_EXPR:
4938 ret = build1 (code, TREE_TYPE (arg), arg);
4939 goto return_build_unary_op;
4940
4941 default:
4942 gcc_unreachable ();
4943 }
4944
4945 if (argtype == NULL_TREE)
4946 argtype = TREE_TYPE (arg);
4947 if (TREE_CODE (arg) == INTEGER_CST)
4948 ret = (require_constant_value
4949 ? fold_build1_initializer_loc (location, code, argtype, arg)
4950 : fold_build1_loc (location, code, argtype, arg));
4951 else
4952 ret = build1 (code, argtype, arg);
4953 return_build_unary_op:
4954 gcc_assert (ret != error_mark_node);
4955 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4956 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4957 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4958 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4959 ret = note_integer_operands (ret);
4960 if (eptype)
4961 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4962 protected_set_expr_location (ret, location);
4963 return ret;
4964 }
4965
4966 /* Return nonzero if REF is an lvalue valid for this language.
4967 Lvalues can be assigned, unless their type has TYPE_READONLY.
4968 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4969
4970 bool
4971 lvalue_p (const_tree ref)
4972 {
4973 const enum tree_code code = TREE_CODE (ref);
4974
4975 switch (code)
4976 {
4977 case REALPART_EXPR:
4978 case IMAGPART_EXPR:
4979 case COMPONENT_REF:
4980 return lvalue_p (TREE_OPERAND (ref, 0));
4981
4982 case C_MAYBE_CONST_EXPR:
4983 return lvalue_p (TREE_OPERAND (ref, 1));
4984
4985 case COMPOUND_LITERAL_EXPR:
4986 case STRING_CST:
4987 return true;
4988
4989 case MEM_REF:
4990 case TARGET_MEM_REF:
4991 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
4992 here as well. */
4993 case INDIRECT_REF:
4994 case ARRAY_REF:
4995 case VAR_DECL:
4996 case PARM_DECL:
4997 case RESULT_DECL:
4998 case ERROR_MARK:
4999 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
5000 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5001
5002 case BIND_EXPR:
5003 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5004
5005 default:
5006 return false;
5007 }
5008 }
5009 \f
5010 /* Give a warning for storing in something that is read-only in GCC
5011 terms but not const in ISO C terms. */
5012
5013 static void
5014 readonly_warning (tree arg, enum lvalue_use use)
5015 {
5016 switch (use)
5017 {
5018 case lv_assign:
5019 warning (0, "assignment of read-only location %qE", arg);
5020 break;
5021 case lv_increment:
5022 warning (0, "increment of read-only location %qE", arg);
5023 break;
5024 case lv_decrement:
5025 warning (0, "decrement of read-only location %qE", arg);
5026 break;
5027 default:
5028 gcc_unreachable ();
5029 }
5030 return;
5031 }
5032
5033
5034 /* Return nonzero if REF is an lvalue valid for this language;
5035 otherwise, print an error message and return zero. USE says
5036 how the lvalue is being used and so selects the error message.
5037 LOCATION is the location at which any error should be reported. */
5038
5039 static int
5040 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5041 {
5042 int win = lvalue_p (ref);
5043
5044 if (!win)
5045 lvalue_error (loc, use);
5046
5047 return win;
5048 }
5049 \f
5050 /* Mark EXP saying that we need to be able to take the
5051 address of it; it should not be allocated in a register.
5052 Returns true if successful. ARRAY_REF_P is true if this
5053 is for ARRAY_REF construction - in that case we don't want
5054 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5055 it is fine to use ARRAY_REFs for vector subscripts on vector
5056 register variables. */
5057
5058 bool
5059 c_mark_addressable (tree exp, bool array_ref_p)
5060 {
5061 tree x = exp;
5062
5063 while (1)
5064 switch (TREE_CODE (x))
5065 {
5066 case VIEW_CONVERT_EXPR:
5067 if (array_ref_p
5068 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5069 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5070 return true;
5071 x = TREE_OPERAND (x, 0);
5072 break;
5073
5074 case COMPONENT_REF:
5075 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5076 {
5077 error ("cannot take address of bit-field %qD",
5078 TREE_OPERAND (x, 1));
5079 return false;
5080 }
5081 /* FALLTHRU */
5082 case ADDR_EXPR:
5083 case ARRAY_REF:
5084 case REALPART_EXPR:
5085 case IMAGPART_EXPR:
5086 x = TREE_OPERAND (x, 0);
5087 break;
5088
5089 case COMPOUND_LITERAL_EXPR:
5090 TREE_ADDRESSABLE (x) = 1;
5091 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5092 return true;
5093
5094 case CONSTRUCTOR:
5095 TREE_ADDRESSABLE (x) = 1;
5096 return true;
5097
5098 case VAR_DECL:
5099 case CONST_DECL:
5100 case PARM_DECL:
5101 case RESULT_DECL:
5102 if (C_DECL_REGISTER (x)
5103 && DECL_NONLOCAL (x))
5104 {
5105 if (TREE_PUBLIC (x) || is_global_var (x))
5106 {
5107 error
5108 ("global register variable %qD used in nested function", x);
5109 return false;
5110 }
5111 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5112 }
5113 else if (C_DECL_REGISTER (x))
5114 {
5115 if (TREE_PUBLIC (x) || is_global_var (x))
5116 error ("address of global register variable %qD requested", x);
5117 else
5118 error ("address of register variable %qD requested", x);
5119 return false;
5120 }
5121
5122 /* FALLTHRU */
5123 case FUNCTION_DECL:
5124 TREE_ADDRESSABLE (x) = 1;
5125 /* FALLTHRU */
5126 default:
5127 return true;
5128 }
5129 }
5130 \f
5131 /* Convert EXPR to TYPE, warning about conversion problems with
5132 constants. SEMANTIC_TYPE is the type this conversion would use
5133 without excess precision. If SEMANTIC_TYPE is NULL, this function
5134 is equivalent to convert_and_check. This function is a wrapper that
5135 handles conversions that may be different than
5136 the usual ones because of excess precision. */
5137
5138 static tree
5139 ep_convert_and_check (location_t loc, tree type, tree expr,
5140 tree semantic_type)
5141 {
5142 if (TREE_TYPE (expr) == type)
5143 return expr;
5144
5145 /* For C11, integer conversions may have results with excess
5146 precision. */
5147 if (flag_isoc11 || !semantic_type)
5148 return convert_and_check (loc, type, expr);
5149
5150 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5151 && TREE_TYPE (expr) != semantic_type)
5152 {
5153 /* For integers, we need to check the real conversion, not
5154 the conversion to the excess precision type. */
5155 expr = convert_and_check (loc, semantic_type, expr);
5156 }
5157 /* Result type is the excess precision type, which should be
5158 large enough, so do not check. */
5159 return convert (type, expr);
5160 }
5161
5162 /* If EXPR refers to a built-in declared without a prototype returns
5163 the actual type of the built-in and, if non-null, set *BLTIN to
5164 a pointer to the built-in. Otherwise return the type of EXPR
5165 and clear *BLTIN if non-null. */
5166
5167 static tree
5168 type_or_builtin_type (tree expr, tree *bltin = NULL)
5169 {
5170 tree dummy;
5171 if (!bltin)
5172 bltin = &dummy;
5173
5174 *bltin = NULL_TREE;
5175
5176 tree type = TREE_TYPE (expr);
5177 if (TREE_CODE (expr) != ADDR_EXPR)
5178 return type;
5179
5180 tree oper = TREE_OPERAND (expr, 0);
5181 if (!DECL_P (oper)
5182 || TREE_CODE (oper) != FUNCTION_DECL
5183 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5184 return type;
5185
5186 built_in_function code = DECL_FUNCTION_CODE (oper);
5187 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5188 return type;
5189
5190 if ((*bltin = builtin_decl_implicit (code)))
5191 type = build_pointer_type (TREE_TYPE (*bltin));
5192
5193 return type;
5194 }
5195
5196 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5197 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5198 if folded to an integer constant then the unselected half may
5199 contain arbitrary operations not normally permitted in constant
5200 expressions. Set the location of the expression to LOC. */
5201
5202 tree
5203 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5204 tree op1, tree op1_original_type, location_t op1_loc,
5205 tree op2, tree op2_original_type, location_t op2_loc)
5206 {
5207 tree type1;
5208 tree type2;
5209 enum tree_code code1;
5210 enum tree_code code2;
5211 tree result_type = NULL;
5212 tree semantic_result_type = NULL;
5213 tree orig_op1 = op1, orig_op2 = op2;
5214 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5215 bool ifexp_int_operands;
5216 tree ret;
5217
5218 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5219 if (op1_int_operands)
5220 op1 = remove_c_maybe_const_expr (op1);
5221 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5222 if (op2_int_operands)
5223 op2 = remove_c_maybe_const_expr (op2);
5224 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5225 if (ifexp_int_operands)
5226 ifexp = remove_c_maybe_const_expr (ifexp);
5227
5228 /* Promote both alternatives. */
5229
5230 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5231 op1 = default_conversion (op1);
5232 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5233 op2 = default_conversion (op2);
5234
5235 if (TREE_CODE (ifexp) == ERROR_MARK
5236 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5237 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5238 return error_mark_node;
5239
5240 tree bltin1 = NULL_TREE;
5241 tree bltin2 = NULL_TREE;
5242 type1 = type_or_builtin_type (op1, &bltin1);
5243 code1 = TREE_CODE (type1);
5244 type2 = type_or_builtin_type (op2, &bltin2);
5245 code2 = TREE_CODE (type2);
5246
5247 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5248 return error_mark_node;
5249
5250 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5251 return error_mark_node;
5252
5253 /* C90 does not permit non-lvalue arrays in conditional expressions.
5254 In C99 they will be pointers by now. */
5255 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5256 {
5257 error_at (colon_loc, "non-lvalue array in conditional expression");
5258 return error_mark_node;
5259 }
5260
5261 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5262 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5263 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5264 || code1 == COMPLEX_TYPE)
5265 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5266 || code2 == COMPLEX_TYPE))
5267 {
5268 semantic_result_type = c_common_type (type1, type2);
5269 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5270 {
5271 op1 = TREE_OPERAND (op1, 0);
5272 type1 = TREE_TYPE (op1);
5273 gcc_assert (TREE_CODE (type1) == code1);
5274 }
5275 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5276 {
5277 op2 = TREE_OPERAND (op2, 0);
5278 type2 = TREE_TYPE (op2);
5279 gcc_assert (TREE_CODE (type2) == code2);
5280 }
5281 }
5282
5283 if (warn_cxx_compat)
5284 {
5285 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5286 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5287
5288 if (TREE_CODE (t1) == ENUMERAL_TYPE
5289 && TREE_CODE (t2) == ENUMERAL_TYPE
5290 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5291 warning_at (colon_loc, OPT_Wc___compat,
5292 ("different enum types in conditional is "
5293 "invalid in C++: %qT vs %qT"),
5294 t1, t2);
5295 }
5296
5297 /* Quickly detect the usual case where op1 and op2 have the same type
5298 after promotion. */
5299 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5300 {
5301 if (type1 == type2)
5302 result_type = type1;
5303 else
5304 result_type = TYPE_MAIN_VARIANT (type1);
5305 }
5306 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5307 || code1 == COMPLEX_TYPE)
5308 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5309 || code2 == COMPLEX_TYPE))
5310 {
5311 /* In C11, a conditional expression between a floating-point
5312 type and an integer type should convert the integer type to
5313 the evaluation format of the floating-point type, with
5314 possible excess precision. */
5315 tree eptype1 = type1;
5316 tree eptype2 = type2;
5317 if (flag_isoc11)
5318 {
5319 tree eptype;
5320 if (ANY_INTEGRAL_TYPE_P (type1)
5321 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5322 {
5323 eptype2 = eptype;
5324 if (!semantic_result_type)
5325 semantic_result_type = c_common_type (type1, type2);
5326 }
5327 else if (ANY_INTEGRAL_TYPE_P (type2)
5328 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5329 {
5330 eptype1 = eptype;
5331 if (!semantic_result_type)
5332 semantic_result_type = c_common_type (type1, type2);
5333 }
5334 }
5335 result_type = c_common_type (eptype1, eptype2);
5336 if (result_type == error_mark_node)
5337 return error_mark_node;
5338 do_warn_double_promotion (result_type, type1, type2,
5339 "implicit conversion from %qT to %qT to "
5340 "match other result of conditional",
5341 colon_loc);
5342
5343 /* If -Wsign-compare, warn here if type1 and type2 have
5344 different signedness. We'll promote the signed to unsigned
5345 and later code won't know it used to be different.
5346 Do this check on the original types, so that explicit casts
5347 will be considered, but default promotions won't. */
5348 if (c_inhibit_evaluation_warnings == 0)
5349 {
5350 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5351 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5352
5353 if (unsigned_op1 ^ unsigned_op2)
5354 {
5355 bool ovf;
5356
5357 /* Do not warn if the result type is signed, since the
5358 signed type will only be chosen if it can represent
5359 all the values of the unsigned type. */
5360 if (!TYPE_UNSIGNED (result_type))
5361 /* OK */;
5362 else
5363 {
5364 bool op1_maybe_const = true;
5365 bool op2_maybe_const = true;
5366
5367 /* Do not warn if the signed quantity is an
5368 unsuffixed integer literal (or some static
5369 constant expression involving such literals) and
5370 it is non-negative. This warning requires the
5371 operands to be folded for best results, so do
5372 that folding in this case even without
5373 warn_sign_compare to avoid warning options
5374 possibly affecting code generation. */
5375 c_inhibit_evaluation_warnings
5376 += (ifexp == truthvalue_false_node);
5377 op1 = c_fully_fold (op1, require_constant_value,
5378 &op1_maybe_const);
5379 c_inhibit_evaluation_warnings
5380 -= (ifexp == truthvalue_false_node);
5381
5382 c_inhibit_evaluation_warnings
5383 += (ifexp == truthvalue_true_node);
5384 op2 = c_fully_fold (op2, require_constant_value,
5385 &op2_maybe_const);
5386 c_inhibit_evaluation_warnings
5387 -= (ifexp == truthvalue_true_node);
5388
5389 if (warn_sign_compare)
5390 {
5391 if ((unsigned_op2
5392 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5393 || (unsigned_op1
5394 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5395 /* OK */;
5396 else if (unsigned_op2)
5397 warning_at (op1_loc, OPT_Wsign_compare,
5398 "operand of %<?:%> changes signedness from "
5399 "%qT to %qT due to unsignedness of other "
5400 "operand", TREE_TYPE (orig_op1),
5401 TREE_TYPE (orig_op2));
5402 else
5403 warning_at (op2_loc, OPT_Wsign_compare,
5404 "operand of %<?:%> changes signedness from "
5405 "%qT to %qT due to unsignedness of other "
5406 "operand", TREE_TYPE (orig_op2),
5407 TREE_TYPE (orig_op1));
5408 }
5409 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5410 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5411 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5412 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5413 }
5414 }
5415 }
5416 }
5417 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5418 {
5419 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5420 pedwarn (colon_loc, OPT_Wpedantic,
5421 "ISO C forbids conditional expr with only one void side");
5422 result_type = void_type_node;
5423 }
5424 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5425 {
5426 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5427 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5428 addr_space_t as_common;
5429
5430 if (comp_target_types (colon_loc, type1, type2))
5431 result_type = common_pointer_type (type1, type2);
5432 else if (null_pointer_constant_p (orig_op1))
5433 result_type = type2;
5434 else if (null_pointer_constant_p (orig_op2))
5435 result_type = type1;
5436 else if (!addr_space_superset (as1, as2, &as_common))
5437 {
5438 error_at (colon_loc, "pointers to disjoint address spaces "
5439 "used in conditional expression");
5440 return error_mark_node;
5441 }
5442 else if ((VOID_TYPE_P (TREE_TYPE (type1))
5443 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5444 || (VOID_TYPE_P (TREE_TYPE (type2))
5445 && !TYPE_ATOMIC (TREE_TYPE (type2))))
5446 {
5447 tree t1 = TREE_TYPE (type1);
5448 tree t2 = TREE_TYPE (type2);
5449 if (!(VOID_TYPE_P (t1)
5450 && !TYPE_ATOMIC (t1)))
5451 {
5452 /* roles are swapped */
5453 t1 = t2;
5454 t2 = TREE_TYPE (type1);
5455 }
5456 tree t2_stripped = strip_array_types (t2);
5457 if ((TREE_CODE (t2) == ARRAY_TYPE)
5458 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5459 {
5460 if (!flag_isoc2x)
5461 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5462 "pointer to array loses qualifier "
5463 "in conditional expression");
5464 else if (warn_c11_c2x_compat > 0)
5465 warning_at (colon_loc, OPT_Wc11_c2x_compat,
5466 "pointer to array loses qualifier "
5467 "in conditional expression in ISO C before C2X");
5468 }
5469 if (TREE_CODE (t2) == FUNCTION_TYPE)
5470 pedwarn (colon_loc, OPT_Wpedantic,
5471 "ISO C forbids conditional expr between "
5472 "%<void *%> and function pointer");
5473 /* for array, use qualifiers of element type */
5474 if (flag_isoc2x)
5475 t2 = t2_stripped;
5476 result_type = build_pointer_type (qualify_type (t1, t2));
5477 }
5478 /* Objective-C pointer comparisons are a bit more lenient. */
5479 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5480 result_type = objc_common_type (type1, type2);
5481 else
5482 {
5483 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5484 if (bltin1 && bltin2)
5485 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5486 "pointer type mismatch between %qT and %qT "
5487 "of %qD and %qD in conditional expression",
5488 type1, type2, bltin1, bltin2);
5489 else
5490 pedwarn (colon_loc, 0,
5491 "pointer type mismatch in conditional expression");
5492 result_type = build_pointer_type
5493 (build_qualified_type (void_type_node, qual));
5494 }
5495 }
5496 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5497 {
5498 if (!null_pointer_constant_p (orig_op2))
5499 pedwarn (colon_loc, 0,
5500 "pointer/integer type mismatch in conditional expression");
5501 else
5502 {
5503 op2 = null_pointer_node;
5504 }
5505 result_type = type1;
5506 }
5507 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5508 {
5509 if (!null_pointer_constant_p (orig_op1))
5510 pedwarn (colon_loc, 0,
5511 "pointer/integer type mismatch in conditional expression");
5512 else
5513 {
5514 op1 = null_pointer_node;
5515 }
5516 result_type = type2;
5517 }
5518
5519 if (!result_type)
5520 {
5521 if (flag_cond_mismatch)
5522 result_type = void_type_node;
5523 else
5524 {
5525 error_at (colon_loc, "type mismatch in conditional expression");
5526 return error_mark_node;
5527 }
5528 }
5529
5530 /* Merge const and volatile flags of the incoming types. */
5531 result_type
5532 = build_type_variant (result_type,
5533 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5534 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5535
5536 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5537 semantic_result_type);
5538 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5539 semantic_result_type);
5540
5541 if (ifexp_bcp && ifexp == truthvalue_true_node)
5542 {
5543 op2_int_operands = true;
5544 op1 = c_fully_fold (op1, require_constant_value, NULL);
5545 }
5546 if (ifexp_bcp && ifexp == truthvalue_false_node)
5547 {
5548 op1_int_operands = true;
5549 op2 = c_fully_fold (op2, require_constant_value, NULL);
5550 }
5551 int_const = int_operands = (ifexp_int_operands
5552 && op1_int_operands
5553 && op2_int_operands);
5554 if (int_operands)
5555 {
5556 int_const = ((ifexp == truthvalue_true_node
5557 && TREE_CODE (orig_op1) == INTEGER_CST
5558 && !TREE_OVERFLOW (orig_op1))
5559 || (ifexp == truthvalue_false_node
5560 && TREE_CODE (orig_op2) == INTEGER_CST
5561 && !TREE_OVERFLOW (orig_op2)));
5562 }
5563
5564 /* Need to convert condition operand into a vector mask. */
5565 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5566 {
5567 tree vectype = TREE_TYPE (ifexp);
5568 tree elem_type = TREE_TYPE (vectype);
5569 tree zero = build_int_cst (elem_type, 0);
5570 tree zero_vec = build_vector_from_val (vectype, zero);
5571 tree cmp_type = truth_type_for (vectype);
5572 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5573 }
5574
5575 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5576 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5577 else
5578 {
5579 if (int_operands)
5580 {
5581 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5582 nested inside of the expression. */
5583 op1 = c_fully_fold (op1, false, NULL);
5584 op2 = c_fully_fold (op2, false, NULL);
5585 }
5586 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5587 if (int_operands)
5588 ret = note_integer_operands (ret);
5589 }
5590 if (semantic_result_type)
5591 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5592
5593 protected_set_expr_location (ret, colon_loc);
5594
5595 /* If the OP1 and OP2 are the same and don't have side-effects,
5596 warn here, because the COND_EXPR will be turned into OP1. */
5597 if (warn_duplicated_branches
5598 && TREE_CODE (ret) == COND_EXPR
5599 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5600 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5601 "this condition has identical branches");
5602
5603 return ret;
5604 }
5605 \f
5606 /* EXPR is an expression, location LOC, whose result is discarded.
5607 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5608 whose right-hand operand is such a call, possibly recursively). */
5609
5610 static void
5611 maybe_warn_nodiscard (location_t loc, tree expr)
5612 {
5613 if (VOID_TYPE_P (TREE_TYPE (expr)))
5614 return;
5615 while (TREE_CODE (expr) == COMPOUND_EXPR)
5616 {
5617 expr = TREE_OPERAND (expr, 1);
5618 if (EXPR_HAS_LOCATION (expr))
5619 loc = EXPR_LOCATION (expr);
5620 }
5621 if (TREE_CODE (expr) != CALL_EXPR)
5622 return;
5623 tree fn = CALL_EXPR_FN (expr);
5624 if (!fn)
5625 return;
5626 tree attr;
5627 if (TREE_CODE (fn) == ADDR_EXPR
5628 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5629 && (attr = lookup_attribute ("nodiscard",
5630 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5631 {
5632 fn = TREE_OPERAND (fn, 0);
5633 tree args = TREE_VALUE (attr);
5634 if (args)
5635 args = TREE_VALUE (args);
5636 auto_diagnostic_group d;
5637 int warned;
5638 if (args)
5639 warned = warning_at (loc, OPT_Wunused_result,
5640 "ignoring return value of %qD, declared with "
5641 "attribute %<nodiscard%>: %E", fn, args);
5642 else
5643 warned = warning_at (loc, OPT_Wunused_result,
5644 "ignoring return value of %qD, declared with "
5645 "attribute %<nodiscard%>", fn);
5646 if (warned)
5647 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5648 }
5649 else
5650 {
5651 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5652 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5653 if (!attr)
5654 return;
5655 tree args = TREE_VALUE (attr);
5656 if (args)
5657 args = TREE_VALUE (args);
5658 auto_diagnostic_group d;
5659 int warned;
5660 if (args)
5661 warned = warning_at (loc, OPT_Wunused_result,
5662 "ignoring return value of type %qT, declared "
5663 "with attribute %<nodiscard%>: %E",
5664 rettype, args);
5665 else
5666 warned = warning_at (loc, OPT_Wunused_result,
5667 "ignoring return value of type %qT, declared "
5668 "with attribute %<nodiscard%>", rettype);
5669 if (warned)
5670 {
5671 if (TREE_CODE (fn) == ADDR_EXPR)
5672 {
5673 fn = TREE_OPERAND (fn, 0);
5674 if (TREE_CODE (fn) == FUNCTION_DECL)
5675 inform (DECL_SOURCE_LOCATION (fn),
5676 "in call to %qD, declared here", fn);
5677 }
5678 }
5679 }
5680 }
5681
5682 /* Return a compound expression that performs two expressions and
5683 returns the value of the second of them.
5684
5685 LOC is the location of the COMPOUND_EXPR. */
5686
5687 tree
5688 build_compound_expr (location_t loc, tree expr1, tree expr2)
5689 {
5690 bool expr1_int_operands, expr2_int_operands;
5691 tree eptype = NULL_TREE;
5692 tree ret;
5693
5694 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5695 if (expr1_int_operands)
5696 expr1 = remove_c_maybe_const_expr (expr1);
5697 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5698 if (expr2_int_operands)
5699 expr2 = remove_c_maybe_const_expr (expr2);
5700
5701 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5702 expr1 = TREE_OPERAND (expr1, 0);
5703 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5704 {
5705 eptype = TREE_TYPE (expr2);
5706 expr2 = TREE_OPERAND (expr2, 0);
5707 }
5708
5709 if (!TREE_SIDE_EFFECTS (expr1))
5710 {
5711 /* The left-hand operand of a comma expression is like an expression
5712 statement: with -Wunused, we should warn if it doesn't have
5713 any side-effects, unless it was explicitly cast to (void). */
5714 if (warn_unused_value)
5715 {
5716 if (VOID_TYPE_P (TREE_TYPE (expr1))
5717 && CONVERT_EXPR_P (expr1))
5718 ; /* (void) a, b */
5719 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5720 && TREE_CODE (expr1) == COMPOUND_EXPR
5721 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5722 ; /* (void) a, (void) b, c */
5723 else
5724 warning_at (loc, OPT_Wunused_value,
5725 "left-hand operand of comma expression has no effect");
5726 }
5727 }
5728 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5729 && warn_unused_value)
5730 {
5731 tree r = expr1;
5732 location_t cloc = loc;
5733 while (TREE_CODE (r) == COMPOUND_EXPR)
5734 {
5735 if (EXPR_HAS_LOCATION (r))
5736 cloc = EXPR_LOCATION (r);
5737 r = TREE_OPERAND (r, 1);
5738 }
5739 if (!TREE_SIDE_EFFECTS (r)
5740 && !VOID_TYPE_P (TREE_TYPE (r))
5741 && !CONVERT_EXPR_P (r))
5742 warning_at (cloc, OPT_Wunused_value,
5743 "right-hand operand of comma expression has no effect");
5744 }
5745
5746 /* With -Wunused, we should also warn if the left-hand operand does have
5747 side-effects, but computes a value which is not used. For example, in
5748 `foo() + bar(), baz()' the result of the `+' operator is not used,
5749 so we should issue a warning. */
5750 else if (warn_unused_value)
5751 warn_if_unused_value (expr1, loc);
5752
5753 maybe_warn_nodiscard (loc, expr1);
5754
5755 if (expr2 == error_mark_node)
5756 return error_mark_node;
5757
5758 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5759
5760 if (flag_isoc99
5761 && expr1_int_operands
5762 && expr2_int_operands)
5763 ret = note_integer_operands (ret);
5764
5765 if (eptype)
5766 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5767
5768 protected_set_expr_location (ret, loc);
5769 return ret;
5770 }
5771
5772 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5773 which we are casting. OTYPE is the type of the expression being
5774 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5775 of the cast. -Wcast-qual appeared on the command line. Named
5776 address space qualifiers are not handled here, because they result
5777 in different warnings. */
5778
5779 static void
5780 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5781 {
5782 tree in_type = type;
5783 tree in_otype = otype;
5784 int added = 0;
5785 int discarded = 0;
5786 bool is_const;
5787
5788 /* Check that the qualifiers on IN_TYPE are a superset of the
5789 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5790 nodes is uninteresting and we stop as soon as we hit a
5791 non-POINTER_TYPE node on either type. */
5792 do
5793 {
5794 in_otype = TREE_TYPE (in_otype);
5795 in_type = TREE_TYPE (in_type);
5796
5797 /* GNU C allows cv-qualified function types. 'const' means the
5798 function is very pure, 'volatile' means it can't return. We
5799 need to warn when such qualifiers are added, not when they're
5800 taken away. */
5801 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5802 && TREE_CODE (in_type) == FUNCTION_TYPE)
5803 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5804 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5805 else
5806 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5807 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5808 }
5809 while (TREE_CODE (in_type) == POINTER_TYPE
5810 && TREE_CODE (in_otype) == POINTER_TYPE);
5811
5812 if (added)
5813 warning_at (loc, OPT_Wcast_qual,
5814 "cast adds %q#v qualifier to function type", added);
5815
5816 if (discarded)
5817 /* There are qualifiers present in IN_OTYPE that are not present
5818 in IN_TYPE. */
5819 warning_at (loc, OPT_Wcast_qual,
5820 "cast discards %qv qualifier from pointer target type",
5821 discarded);
5822
5823 if (added || discarded)
5824 return;
5825
5826 /* A cast from **T to const **T is unsafe, because it can cause a
5827 const value to be changed with no additional warning. We only
5828 issue this warning if T is the same on both sides, and we only
5829 issue the warning if there are the same number of pointers on
5830 both sides, as otherwise the cast is clearly unsafe anyhow. A
5831 cast is unsafe when a qualifier is added at one level and const
5832 is not present at all outer levels.
5833
5834 To issue this warning, we check at each level whether the cast
5835 adds new qualifiers not already seen. We don't need to special
5836 case function types, as they won't have the same
5837 TYPE_MAIN_VARIANT. */
5838
5839 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5840 return;
5841 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5842 return;
5843
5844 in_type = type;
5845 in_otype = otype;
5846 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5847 do
5848 {
5849 in_type = TREE_TYPE (in_type);
5850 in_otype = TREE_TYPE (in_otype);
5851 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5852 && !is_const)
5853 {
5854 warning_at (loc, OPT_Wcast_qual,
5855 "to be safe all intermediate pointers in cast from "
5856 "%qT to %qT must be %<const%> qualified",
5857 otype, type);
5858 break;
5859 }
5860 if (is_const)
5861 is_const = TYPE_READONLY (in_type);
5862 }
5863 while (TREE_CODE (in_type) == POINTER_TYPE);
5864 }
5865
5866 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5867
5868 static bool
5869 c_safe_arg_type_equiv_p (tree t1, tree t2)
5870 {
5871 t1 = TYPE_MAIN_VARIANT (t1);
5872 t2 = TYPE_MAIN_VARIANT (t2);
5873
5874 if (TREE_CODE (t1) == POINTER_TYPE
5875 && TREE_CODE (t2) == POINTER_TYPE)
5876 return true;
5877
5878 /* The signedness of the parameter matters only when an integral
5879 type smaller than int is promoted to int, otherwise only the
5880 precision of the parameter matters.
5881 This check should make sure that the callee does not see
5882 undefined values in argument registers. */
5883 if (INTEGRAL_TYPE_P (t1)
5884 && INTEGRAL_TYPE_P (t2)
5885 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5886 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5887 || !targetm.calls.promote_prototypes (NULL_TREE)
5888 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5889 return true;
5890
5891 return comptypes (t1, t2);
5892 }
5893
5894 /* Check if a type cast between two function types can be considered safe. */
5895
5896 static bool
5897 c_safe_function_type_cast_p (tree t1, tree t2)
5898 {
5899 if (TREE_TYPE (t1) == void_type_node &&
5900 TYPE_ARG_TYPES (t1) == void_list_node)
5901 return true;
5902
5903 if (TREE_TYPE (t2) == void_type_node &&
5904 TYPE_ARG_TYPES (t2) == void_list_node)
5905 return true;
5906
5907 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5908 return false;
5909
5910 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5911 t1 && t2;
5912 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5913 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5914 return false;
5915
5916 return true;
5917 }
5918
5919 /* Build an expression representing a cast to type TYPE of expression EXPR.
5920 LOC is the location of the cast-- typically the open paren of the cast. */
5921
5922 tree
5923 build_c_cast (location_t loc, tree type, tree expr)
5924 {
5925 tree value;
5926
5927 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5928
5929 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5930 expr = TREE_OPERAND (expr, 0);
5931
5932 value = expr;
5933 if (int_operands)
5934 value = remove_c_maybe_const_expr (value);
5935
5936 if (type == error_mark_node || expr == error_mark_node)
5937 return error_mark_node;
5938
5939 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5940 only in <protocol> qualifications. But when constructing cast expressions,
5941 the protocols do matter and must be kept around. */
5942 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5943 return build1 (NOP_EXPR, type, expr);
5944
5945 type = TYPE_MAIN_VARIANT (type);
5946
5947 if (TREE_CODE (type) == ARRAY_TYPE)
5948 {
5949 error_at (loc, "cast specifies array type");
5950 return error_mark_node;
5951 }
5952
5953 if (TREE_CODE (type) == FUNCTION_TYPE)
5954 {
5955 error_at (loc, "cast specifies function type");
5956 return error_mark_node;
5957 }
5958
5959 if (!VOID_TYPE_P (type))
5960 {
5961 value = require_complete_type (loc, value);
5962 if (value == error_mark_node)
5963 return error_mark_node;
5964 }
5965
5966 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5967 {
5968 if (RECORD_OR_UNION_TYPE_P (type))
5969 pedwarn (loc, OPT_Wpedantic,
5970 "ISO C forbids casting nonscalar to the same type");
5971
5972 /* Convert to remove any qualifiers from VALUE's type. */
5973 value = convert (type, value);
5974 }
5975 else if (TREE_CODE (type) == UNION_TYPE)
5976 {
5977 tree field;
5978
5979 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5980 if (TREE_TYPE (field) != error_mark_node
5981 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5982 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5983 break;
5984
5985 if (field)
5986 {
5987 tree t;
5988 bool maybe_const = true;
5989
5990 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5991 t = c_fully_fold (value, false, &maybe_const);
5992 t = build_constructor_single (type, field, t);
5993 if (!maybe_const)
5994 t = c_wrap_maybe_const (t, true);
5995 t = digest_init (loc, type, t,
5996 NULL_TREE, false, true, 0);
5997 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5998 return t;
5999 }
6000 error_at (loc, "cast to union type from type not present in union");
6001 return error_mark_node;
6002 }
6003 else
6004 {
6005 tree otype, ovalue;
6006
6007 if (type == void_type_node)
6008 {
6009 tree t = build1 (CONVERT_EXPR, type, value);
6010 SET_EXPR_LOCATION (t, loc);
6011 return t;
6012 }
6013
6014 otype = TREE_TYPE (value);
6015
6016 /* Optionally warn about potentially worrisome casts. */
6017 if (warn_cast_qual
6018 && TREE_CODE (type) == POINTER_TYPE
6019 && TREE_CODE (otype) == POINTER_TYPE)
6020 handle_warn_cast_qual (loc, type, otype);
6021
6022 /* Warn about conversions between pointers to disjoint
6023 address spaces. */
6024 if (TREE_CODE (type) == POINTER_TYPE
6025 && TREE_CODE (otype) == POINTER_TYPE
6026 && !null_pointer_constant_p (value))
6027 {
6028 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6029 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6030 addr_space_t as_common;
6031
6032 if (!addr_space_superset (as_to, as_from, &as_common))
6033 {
6034 if (ADDR_SPACE_GENERIC_P (as_from))
6035 warning_at (loc, 0, "cast to %s address space pointer "
6036 "from disjoint generic address space pointer",
6037 c_addr_space_name (as_to));
6038
6039 else if (ADDR_SPACE_GENERIC_P (as_to))
6040 warning_at (loc, 0, "cast to generic address space pointer "
6041 "from disjoint %s address space pointer",
6042 c_addr_space_name (as_from));
6043
6044 else
6045 warning_at (loc, 0, "cast to %s address space pointer "
6046 "from disjoint %s address space pointer",
6047 c_addr_space_name (as_to),
6048 c_addr_space_name (as_from));
6049 }
6050 }
6051
6052 /* Warn about possible alignment problems. */
6053 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6054 && TREE_CODE (type) == POINTER_TYPE
6055 && TREE_CODE (otype) == POINTER_TYPE
6056 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6057 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6058 /* Don't warn about opaque types, where the actual alignment
6059 restriction is unknown. */
6060 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6061 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6062 && min_align_of_type (TREE_TYPE (type))
6063 > min_align_of_type (TREE_TYPE (otype)))
6064 warning_at (loc, OPT_Wcast_align,
6065 "cast increases required alignment of target type");
6066
6067 if (TREE_CODE (type) == INTEGER_TYPE
6068 && TREE_CODE (otype) == POINTER_TYPE
6069 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6070 /* Unlike conversion of integers to pointers, where the
6071 warning is disabled for converting constants because
6072 of cases such as SIG_*, warn about converting constant
6073 pointers to integers. In some cases it may cause unwanted
6074 sign extension, and a warning is appropriate. */
6075 warning_at (loc, OPT_Wpointer_to_int_cast,
6076 "cast from pointer to integer of different size");
6077
6078 if (TREE_CODE (value) == CALL_EXPR
6079 && TREE_CODE (type) != TREE_CODE (otype))
6080 warning_at (loc, OPT_Wbad_function_cast,
6081 "cast from function call of type %qT "
6082 "to non-matching type %qT", otype, type);
6083
6084 if (TREE_CODE (type) == POINTER_TYPE
6085 && TREE_CODE (otype) == INTEGER_TYPE
6086 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6087 /* Don't warn about converting any constant. */
6088 && !TREE_CONSTANT (value))
6089 warning_at (loc,
6090 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6091 "of different size");
6092
6093 if (warn_strict_aliasing <= 2)
6094 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6095
6096 /* If pedantic, warn for conversions between function and object
6097 pointer types, except for converting a null pointer constant
6098 to function pointer type. */
6099 if (pedantic
6100 && TREE_CODE (type) == POINTER_TYPE
6101 && TREE_CODE (otype) == POINTER_TYPE
6102 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6103 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6104 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6105 "conversion of function pointer to object pointer type");
6106
6107 if (pedantic
6108 && TREE_CODE (type) == POINTER_TYPE
6109 && TREE_CODE (otype) == POINTER_TYPE
6110 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6111 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6112 && !null_pointer_constant_p (value))
6113 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6114 "conversion of object pointer to function pointer type");
6115
6116 if (TREE_CODE (type) == POINTER_TYPE
6117 && TREE_CODE (otype) == POINTER_TYPE
6118 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6119 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6120 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6121 TREE_TYPE (otype)))
6122 warning_at (loc, OPT_Wcast_function_type,
6123 "cast between incompatible function types"
6124 " from %qT to %qT", otype, type);
6125
6126 ovalue = value;
6127 value = convert (type, value);
6128
6129 /* Ignore any integer overflow caused by the cast. */
6130 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6131 {
6132 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6133 {
6134 if (!TREE_OVERFLOW (value))
6135 {
6136 /* Avoid clobbering a shared constant. */
6137 value = copy_node (value);
6138 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6139 }
6140 }
6141 else if (TREE_OVERFLOW (value))
6142 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6143 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6144 }
6145 }
6146
6147 /* Don't let a cast be an lvalue. */
6148 if (lvalue_p (value))
6149 value = non_lvalue_loc (loc, value);
6150
6151 /* Don't allow the results of casting to floating-point or complex
6152 types be confused with actual constants, or casts involving
6153 integer and pointer types other than direct integer-to-integer
6154 and integer-to-pointer be confused with integer constant
6155 expressions and null pointer constants. */
6156 if (TREE_CODE (value) == REAL_CST
6157 || TREE_CODE (value) == COMPLEX_CST
6158 || (TREE_CODE (value) == INTEGER_CST
6159 && !((TREE_CODE (expr) == INTEGER_CST
6160 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6161 || TREE_CODE (expr) == REAL_CST
6162 || TREE_CODE (expr) == COMPLEX_CST)))
6163 value = build1 (NOP_EXPR, type, value);
6164
6165 /* If the expression has integer operands and so can occur in an
6166 unevaluated part of an integer constant expression, ensure the
6167 return value reflects this. */
6168 if (int_operands
6169 && INTEGRAL_TYPE_P (type)
6170 && value != error_mark_node
6171 && !EXPR_INT_CONST_OPERANDS (value))
6172 value = note_integer_operands (value);
6173
6174 protected_set_expr_location (value, loc);
6175 return value;
6176 }
6177
6178 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6179 location of the open paren of the cast, or the position of the cast
6180 expr. */
6181 tree
6182 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6183 {
6184 tree type;
6185 tree type_expr = NULL_TREE;
6186 bool type_expr_const = true;
6187 tree ret;
6188 int saved_wsp = warn_strict_prototypes;
6189
6190 /* This avoids warnings about unprototyped casts on
6191 integers. E.g. "#define SIG_DFL (void(*)())0". */
6192 if (TREE_CODE (expr) == INTEGER_CST)
6193 warn_strict_prototypes = 0;
6194 type = groktypename (type_name, &type_expr, &type_expr_const);
6195 warn_strict_prototypes = saved_wsp;
6196
6197 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6198 && reject_gcc_builtin (expr))
6199 return error_mark_node;
6200
6201 ret = build_c_cast (loc, type, expr);
6202 if (type_expr)
6203 {
6204 bool inner_expr_const = true;
6205 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6206 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6207 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6208 && inner_expr_const);
6209 SET_EXPR_LOCATION (ret, loc);
6210 }
6211
6212 if (!EXPR_HAS_LOCATION (ret))
6213 protected_set_expr_location (ret, loc);
6214
6215 /* C++ does not permits types to be defined in a cast, but it
6216 allows references to incomplete types. */
6217 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6218 warning_at (loc, OPT_Wc___compat,
6219 "defining a type in a cast is invalid in C++");
6220
6221 return ret;
6222 }
6223 \f
6224 /* Build an assignment expression of lvalue LHS from value RHS.
6225 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6226 may differ from TREE_TYPE (LHS) for an enum bitfield.
6227 MODIFYCODE is the code for a binary operator that we use
6228 to combine the old value of LHS with RHS to get the new value.
6229 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6230 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6231 which may differ from TREE_TYPE (RHS) for an enum value.
6232
6233 LOCATION is the location of the MODIFYCODE operator.
6234 RHS_LOC is the location of the RHS. */
6235
6236 tree
6237 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6238 enum tree_code modifycode,
6239 location_t rhs_loc, tree rhs, tree rhs_origtype)
6240 {
6241 tree result;
6242 tree newrhs;
6243 tree rhseval = NULL_TREE;
6244 tree lhstype = TREE_TYPE (lhs);
6245 tree olhstype = lhstype;
6246 bool npc;
6247 bool is_atomic_op;
6248
6249 /* Types that aren't fully specified cannot be used in assignments. */
6250 lhs = require_complete_type (location, lhs);
6251
6252 /* Avoid duplicate error messages from operands that had errors. */
6253 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6254 return error_mark_node;
6255
6256 /* Ensure an error for assigning a non-lvalue array to an array in
6257 C90. */
6258 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6259 {
6260 error_at (location, "assignment to expression with array type");
6261 return error_mark_node;
6262 }
6263
6264 /* For ObjC properties, defer this check. */
6265 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6266 return error_mark_node;
6267
6268 is_atomic_op = really_atomic_lvalue (lhs);
6269
6270 newrhs = rhs;
6271
6272 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6273 {
6274 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6275 lhs_origtype, modifycode, rhs_loc, rhs,
6276 rhs_origtype);
6277 if (inner == error_mark_node)
6278 return error_mark_node;
6279 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6280 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6281 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6282 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6283 protected_set_expr_location (result, location);
6284 return result;
6285 }
6286
6287 /* If a binary op has been requested, combine the old LHS value with the RHS
6288 producing the value we should actually store into the LHS. */
6289
6290 if (modifycode != NOP_EXPR)
6291 {
6292 lhs = c_fully_fold (lhs, false, NULL, true);
6293 lhs = stabilize_reference (lhs);
6294
6295 /* Construct the RHS for any non-atomic compound assignemnt. */
6296 if (!is_atomic_op)
6297 {
6298 /* If in LHS op= RHS the RHS has side-effects, ensure they
6299 are preevaluated before the rest of the assignment expression's
6300 side-effects, because RHS could contain e.g. function calls
6301 that modify LHS. */
6302 if (TREE_SIDE_EFFECTS (rhs))
6303 {
6304 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6305 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6306 else
6307 newrhs = save_expr (rhs);
6308 rhseval = newrhs;
6309 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6310 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6311 newrhs);
6312 }
6313 newrhs = build_binary_op (location,
6314 modifycode, lhs, newrhs, true);
6315
6316 /* The original type of the right hand side is no longer
6317 meaningful. */
6318 rhs_origtype = NULL_TREE;
6319 }
6320 }
6321
6322 if (c_dialect_objc ())
6323 {
6324 /* Check if we are modifying an Objective-C property reference;
6325 if so, we need to generate setter calls. */
6326 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6327 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6328 else
6329 result = objc_maybe_build_modify_expr (lhs, newrhs);
6330 if (result)
6331 goto return_result;
6332
6333 /* Else, do the check that we postponed for Objective-C. */
6334 if (!lvalue_or_else (location, lhs, lv_assign))
6335 return error_mark_node;
6336 }
6337
6338 /* Give an error for storing in something that is 'const'. */
6339
6340 if (TYPE_READONLY (lhstype)
6341 || (RECORD_OR_UNION_TYPE_P (lhstype)
6342 && C_TYPE_FIELDS_READONLY (lhstype)))
6343 {
6344 readonly_error (location, lhs, lv_assign);
6345 return error_mark_node;
6346 }
6347 else if (TREE_READONLY (lhs))
6348 readonly_warning (lhs, lv_assign);
6349
6350 /* If storing into a structure or union member,
6351 it has probably been given type `int'.
6352 Compute the type that would go with
6353 the actual amount of storage the member occupies. */
6354
6355 if (TREE_CODE (lhs) == COMPONENT_REF
6356 && (TREE_CODE (lhstype) == INTEGER_TYPE
6357 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6358 || TREE_CODE (lhstype) == REAL_TYPE
6359 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6360 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6361
6362 /* If storing in a field that is in actuality a short or narrower than one,
6363 we must store in the field in its actual type. */
6364
6365 if (lhstype != TREE_TYPE (lhs))
6366 {
6367 lhs = copy_node (lhs);
6368 TREE_TYPE (lhs) = lhstype;
6369 }
6370
6371 /* Issue -Wc++-compat warnings about an assignment to an enum type
6372 when LHS does not have its original type. This happens for,
6373 e.g., an enum bitfield in a struct. */
6374 if (warn_cxx_compat
6375 && lhs_origtype != NULL_TREE
6376 && lhs_origtype != lhstype
6377 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6378 {
6379 tree checktype = (rhs_origtype != NULL_TREE
6380 ? rhs_origtype
6381 : TREE_TYPE (rhs));
6382 if (checktype != error_mark_node
6383 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6384 || (is_atomic_op && modifycode != NOP_EXPR)))
6385 warning_at (location, OPT_Wc___compat,
6386 "enum conversion in assignment is invalid in C++");
6387 }
6388
6389 /* Remove qualifiers. */
6390 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6391 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6392
6393 /* Convert new value to destination type. Fold it first, then
6394 restore any excess precision information, for the sake of
6395 conversion warnings. */
6396
6397 if (!(is_atomic_op && modifycode != NOP_EXPR))
6398 {
6399 tree rhs_semantic_type = NULL_TREE;
6400 if (!c_in_omp_for)
6401 {
6402 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6403 {
6404 rhs_semantic_type = TREE_TYPE (newrhs);
6405 newrhs = TREE_OPERAND (newrhs, 0);
6406 }
6407 npc = null_pointer_constant_p (newrhs);
6408 newrhs = c_fully_fold (newrhs, false, NULL);
6409 if (rhs_semantic_type)
6410 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6411 }
6412 else
6413 npc = null_pointer_constant_p (newrhs);
6414 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6415 rhs_origtype, ic_assign, npc,
6416 NULL_TREE, NULL_TREE, 0);
6417 if (TREE_CODE (newrhs) == ERROR_MARK)
6418 return error_mark_node;
6419 }
6420
6421 /* Emit ObjC write barrier, if necessary. */
6422 if (c_dialect_objc () && flag_objc_gc)
6423 {
6424 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6425 if (result)
6426 {
6427 protected_set_expr_location (result, location);
6428 goto return_result;
6429 }
6430 }
6431
6432 /* Scan operands. */
6433
6434 if (is_atomic_op)
6435 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6436 else
6437 {
6438 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6439 TREE_SIDE_EFFECTS (result) = 1;
6440 protected_set_expr_location (result, location);
6441 }
6442
6443 /* If we got the LHS in a different type for storing in,
6444 convert the result back to the nominal type of LHS
6445 so that the value we return always has the same type
6446 as the LHS argument. */
6447
6448 if (olhstype == TREE_TYPE (result))
6449 goto return_result;
6450
6451 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6452 rhs_origtype, ic_assign, false, NULL_TREE,
6453 NULL_TREE, 0);
6454 protected_set_expr_location (result, location);
6455
6456 return_result:
6457 if (rhseval)
6458 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6459 return result;
6460 }
6461 \f
6462 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6463 This is used to implement -fplan9-extensions. */
6464
6465 static bool
6466 find_anonymous_field_with_type (tree struct_type, tree type)
6467 {
6468 tree field;
6469 bool found;
6470
6471 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6472 found = false;
6473 for (field = TYPE_FIELDS (struct_type);
6474 field != NULL_TREE;
6475 field = TREE_CHAIN (field))
6476 {
6477 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6478 ? c_build_qualified_type (TREE_TYPE (field),
6479 TYPE_QUAL_ATOMIC)
6480 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6481 if (DECL_NAME (field) == NULL
6482 && comptypes (type, fieldtype))
6483 {
6484 if (found)
6485 return false;
6486 found = true;
6487 }
6488 else if (DECL_NAME (field) == NULL
6489 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6490 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6491 {
6492 if (found)
6493 return false;
6494 found = true;
6495 }
6496 }
6497 return found;
6498 }
6499
6500 /* RHS is an expression whose type is pointer to struct. If there is
6501 an anonymous field in RHS with type TYPE, then return a pointer to
6502 that field in RHS. This is used with -fplan9-extensions. This
6503 returns NULL if no conversion could be found. */
6504
6505 static tree
6506 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6507 {
6508 tree rhs_struct_type, lhs_main_type;
6509 tree field, found_field;
6510 bool found_sub_field;
6511 tree ret;
6512
6513 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6514 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6515 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6516
6517 gcc_assert (POINTER_TYPE_P (type));
6518 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6519 ? c_build_qualified_type (TREE_TYPE (type),
6520 TYPE_QUAL_ATOMIC)
6521 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6522
6523 found_field = NULL_TREE;
6524 found_sub_field = false;
6525 for (field = TYPE_FIELDS (rhs_struct_type);
6526 field != NULL_TREE;
6527 field = TREE_CHAIN (field))
6528 {
6529 if (DECL_NAME (field) != NULL_TREE
6530 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6531 continue;
6532 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6533 ? c_build_qualified_type (TREE_TYPE (field),
6534 TYPE_QUAL_ATOMIC)
6535 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6536 if (comptypes (lhs_main_type, fieldtype))
6537 {
6538 if (found_field != NULL_TREE)
6539 return NULL_TREE;
6540 found_field = field;
6541 }
6542 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6543 lhs_main_type))
6544 {
6545 if (found_field != NULL_TREE)
6546 return NULL_TREE;
6547 found_field = field;
6548 found_sub_field = true;
6549 }
6550 }
6551
6552 if (found_field == NULL_TREE)
6553 return NULL_TREE;
6554
6555 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6556 build_fold_indirect_ref (rhs), found_field,
6557 NULL_TREE);
6558 ret = build_fold_addr_expr_loc (location, ret);
6559
6560 if (found_sub_field)
6561 {
6562 ret = convert_to_anonymous_field (location, type, ret);
6563 gcc_assert (ret != NULL_TREE);
6564 }
6565
6566 return ret;
6567 }
6568
6569 /* Issue an error message for a bad initializer component.
6570 GMSGID identifies the message.
6571 The component name is taken from the spelling stack. */
6572
6573 static void ATTRIBUTE_GCC_DIAG (2,0)
6574 error_init (location_t loc, const char *gmsgid, ...)
6575 {
6576 char *ofwhat;
6577
6578 auto_diagnostic_group d;
6579
6580 /* The gmsgid may be a format string with %< and %>. */
6581 va_list ap;
6582 va_start (ap, gmsgid);
6583 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6584 va_end (ap);
6585
6586 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6587 if (*ofwhat && warned)
6588 inform (loc, "(near initialization for %qs)", ofwhat);
6589 }
6590
6591 /* Issue a pedantic warning for a bad initializer component. OPT is
6592 the option OPT_* (from options.h) controlling this warning or 0 if
6593 it is unconditionally given. GMSGID identifies the message. The
6594 component name is taken from the spelling stack. */
6595
6596 static void ATTRIBUTE_GCC_DIAG (3,0)
6597 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6598 {
6599 /* Use the location where a macro was expanded rather than where
6600 it was defined to make sure macros defined in system headers
6601 but used incorrectly elsewhere are diagnosed. */
6602 location_t exploc = expansion_point_location_if_in_system_header (loc);
6603 auto_diagnostic_group d;
6604 va_list ap;
6605 va_start (ap, gmsgid);
6606 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6607 va_end (ap);
6608 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6609 if (*ofwhat && warned)
6610 inform (exploc, "(near initialization for %qs)", ofwhat);
6611 }
6612
6613 /* Issue a warning for a bad initializer component.
6614
6615 OPT is the OPT_W* value corresponding to the warning option that
6616 controls this warning. GMSGID identifies the message. The
6617 component name is taken from the spelling stack. */
6618
6619 static void
6620 warning_init (location_t loc, int opt, const char *gmsgid)
6621 {
6622 char *ofwhat;
6623 bool warned;
6624
6625 auto_diagnostic_group d;
6626
6627 /* Use the location where a macro was expanded rather than where
6628 it was defined to make sure macros defined in system headers
6629 but used incorrectly elsewhere are diagnosed. */
6630 location_t exploc = expansion_point_location_if_in_system_header (loc);
6631
6632 /* The gmsgid may be a format string with %< and %>. */
6633 warned = warning_at (exploc, opt, gmsgid);
6634 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6635 if (*ofwhat && warned)
6636 inform (exploc, "(near initialization for %qs)", ofwhat);
6637 }
6638 \f
6639 /* If TYPE is an array type and EXPR is a parenthesized string
6640 constant, warn if pedantic that EXPR is being used to initialize an
6641 object of type TYPE. */
6642
6643 void
6644 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6645 {
6646 if (pedantic
6647 && TREE_CODE (type) == ARRAY_TYPE
6648 && TREE_CODE (expr.value) == STRING_CST
6649 && expr.original_code != STRING_CST)
6650 pedwarn_init (loc, OPT_Wpedantic,
6651 "array initialized from parenthesized string constant");
6652 }
6653
6654 /* Attempt to locate the parameter with the given index within FNDECL,
6655 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6656
6657 static location_t
6658 get_fndecl_argument_location (tree fndecl, int argnum)
6659 {
6660 int i;
6661 tree param;
6662
6663 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6664 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6665 i < argnum && param;
6666 i++, param = TREE_CHAIN (param))
6667 ;
6668
6669 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6670 return DECL_SOURCE_LOCATION (FNDECL). */
6671 if (param == NULL)
6672 return DECL_SOURCE_LOCATION (fndecl);
6673
6674 return DECL_SOURCE_LOCATION (param);
6675 }
6676
6677 /* Issue a note about a mismatching argument for parameter PARMNUM
6678 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6679 Attempt to issue the note at the pertinent parameter of the decl;
6680 failing that issue it at the location of FUNDECL; failing that
6681 issue it at PLOC. */
6682
6683 static void
6684 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6685 tree expected_type, tree actual_type)
6686 {
6687 location_t loc;
6688 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6689 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6690 else
6691 loc = ploc;
6692
6693 inform (loc,
6694 "expected %qT but argument is of type %qT",
6695 expected_type, actual_type);
6696 }
6697
6698 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6699 function FUNDECL declared without prototype to parameter PARMNUM of
6700 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6701
6702 static void
6703 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6704 tree parmtype, tree argtype)
6705 {
6706 tree_code parmcode = TREE_CODE (parmtype);
6707 tree_code argcode = TREE_CODE (argtype);
6708 tree promoted = c_type_promotes_to (argtype);
6709
6710 /* Avoid warning for enum arguments that promote to an integer type
6711 of the same size/mode. */
6712 if (parmcode == INTEGER_TYPE
6713 && argcode == ENUMERAL_TYPE
6714 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6715 return;
6716
6717 if ((parmcode == argcode
6718 || (parmcode == INTEGER_TYPE
6719 && argcode == ENUMERAL_TYPE))
6720 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6721 return;
6722
6723 /* This diagnoses even signed/unsigned mismatches. Those might be
6724 safe in many cases but GCC may emit suboptimal code for them so
6725 warning on those cases drives efficiency improvements. */
6726 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6727 TYPE_MAIN_VARIANT (promoted) == argtype
6728 ? G_("%qD argument %d type is %qT where %qT is expected "
6729 "in a call to built-in function declared without "
6730 "prototype")
6731 : G_("%qD argument %d promotes to %qT where %qT is expected "
6732 "in a call to built-in function declared without "
6733 "prototype"),
6734 fundecl, parmnum, promoted, parmtype))
6735 inform (DECL_SOURCE_LOCATION (fundecl),
6736 "built-in %qD declared here",
6737 fundecl);
6738 }
6739
6740 /* Convert value RHS to type TYPE as preparation for an assignment to
6741 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6742 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6743 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6744 constant before any folding.
6745 The real work of conversion is done by `convert'.
6746 The purpose of this function is to generate error messages
6747 for assignments that are not allowed in C.
6748 ERRTYPE says whether it is argument passing, assignment,
6749 initialization or return.
6750
6751 In the following example, '~' denotes where EXPR_LOC and '^' where
6752 LOCATION point to:
6753
6754 f (var); [ic_argpass]
6755 ^ ~~~
6756 x = var; [ic_assign]
6757 ^ ~~~;
6758 int x = var; [ic_init]
6759 ^^^
6760 return x; [ic_return]
6761 ^
6762
6763 FUNCTION is a tree for the function being called.
6764 PARMNUM is the number of the argument, for printing in error messages.
6765 WARNOPT may be set to a warning option to issue the corresponding warning
6766 rather than an error for invalid conversions. Used for calls to built-in
6767 functions declared without a prototype. */
6768
6769 static tree
6770 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6771 tree rhs, tree origtype, enum impl_conv errtype,
6772 bool null_pointer_constant, tree fundecl,
6773 tree function, int parmnum, int warnopt /* = 0 */)
6774 {
6775 enum tree_code codel = TREE_CODE (type);
6776 tree orig_rhs = rhs;
6777 tree rhstype;
6778 enum tree_code coder;
6779 tree rname = NULL_TREE;
6780 bool objc_ok = false;
6781
6782 /* Use the expansion point location to handle cases such as user's
6783 function returning a wrong-type macro defined in a system header. */
6784 location = expansion_point_location_if_in_system_header (location);
6785
6786 if (errtype == ic_argpass)
6787 {
6788 tree selector;
6789 /* Change pointer to function to the function itself for
6790 diagnostics. */
6791 if (TREE_CODE (function) == ADDR_EXPR
6792 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6793 function = TREE_OPERAND (function, 0);
6794
6795 /* Handle an ObjC selector specially for diagnostics. */
6796 selector = objc_message_selector ();
6797 rname = function;
6798 if (selector && parmnum > 2)
6799 {
6800 rname = selector;
6801 parmnum -= 2;
6802 }
6803 }
6804
6805 /* This macro is used to emit diagnostics to ensure that all format
6806 strings are complete sentences, visible to gettext and checked at
6807 compile time. */
6808 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6809 do { \
6810 switch (errtype) \
6811 { \
6812 case ic_argpass: \
6813 { \
6814 auto_diagnostic_group d; \
6815 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6816 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6817 } \
6818 break; \
6819 case ic_assign: \
6820 pedwarn (LOCATION, OPT, AS); \
6821 break; \
6822 case ic_init: \
6823 case ic_init_const: \
6824 pedwarn_init (LOCATION, OPT, IN); \
6825 break; \
6826 case ic_return: \
6827 pedwarn (LOCATION, OPT, RE); \
6828 break; \
6829 default: \
6830 gcc_unreachable (); \
6831 } \
6832 } while (0)
6833
6834 /* This macro is used to emit diagnostics to ensure that all format
6835 strings are complete sentences, visible to gettext and checked at
6836 compile time. It can be called with 'pedwarn' or 'warning_at'. */
6837 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6838 do { \
6839 switch (errtype) \
6840 { \
6841 case ic_argpass: \
6842 { \
6843 auto_diagnostic_group d; \
6844 if (PEDWARN) { \
6845 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6846 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6847 } else { \
6848 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6849 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6850 } \
6851 } \
6852 break; \
6853 case ic_assign: \
6854 if (PEDWARN) \
6855 pedwarn (LOCATION, OPT, AS, QUALS); \
6856 else \
6857 warning_at (LOCATION, OPT, AS, QUALS); \
6858 break; \
6859 case ic_init: \
6860 case ic_init_const: \
6861 if (PEDWARN) \
6862 pedwarn (LOCATION, OPT, IN, QUALS); \
6863 else \
6864 warning_at (LOCATION, OPT, IN, QUALS); \
6865 break; \
6866 case ic_return: \
6867 if (PEDWARN) \
6868 pedwarn (LOCATION, OPT, RE, QUALS); \
6869 else \
6870 warning_at (LOCATION, OPT, RE, QUALS); \
6871 break; \
6872 default: \
6873 gcc_unreachable (); \
6874 } \
6875 } while (0)
6876
6877 /* This macro is used to emit diagnostics to ensure that all format
6878 strings are complete sentences, visible to gettext and checked at
6879 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6880 extra parameter to enumerate qualifiers. */
6881 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6882 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
6883
6884
6885 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6886 rhs = TREE_OPERAND (rhs, 0);
6887
6888 rhstype = TREE_TYPE (rhs);
6889 coder = TREE_CODE (rhstype);
6890
6891 if (coder == ERROR_MARK)
6892 return error_mark_node;
6893
6894 if (c_dialect_objc ())
6895 {
6896 int parmno;
6897
6898 switch (errtype)
6899 {
6900 case ic_return:
6901 parmno = 0;
6902 break;
6903
6904 case ic_assign:
6905 parmno = -1;
6906 break;
6907
6908 case ic_init:
6909 case ic_init_const:
6910 parmno = -2;
6911 break;
6912
6913 default:
6914 parmno = parmnum;
6915 break;
6916 }
6917
6918 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6919 }
6920
6921 if (warn_cxx_compat)
6922 {
6923 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6924 if (checktype != error_mark_node
6925 && TREE_CODE (type) == ENUMERAL_TYPE
6926 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6927 switch (errtype)
6928 {
6929 case ic_argpass:
6930 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6931 "passing argument %d of %qE is invalid in C++",
6932 parmnum, rname))
6933 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6934 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6935 "expected %qT but argument is of type %qT",
6936 type, rhstype);
6937 break;
6938 case ic_assign:
6939 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6940 "%qT in assignment is invalid in C++", rhstype, type);
6941 break;
6942 case ic_init:
6943 case ic_init_const:
6944 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6945 "%qT to %qT in initialization is invalid in C++",
6946 rhstype, type);
6947 break;
6948 case ic_return:
6949 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6950 "%qT in return is invalid in C++", rhstype, type);
6951 break;
6952 default:
6953 gcc_unreachable ();
6954 }
6955 }
6956
6957 if (warn_enum_conversion)
6958 {
6959 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6960 if (checktype != error_mark_node
6961 && TREE_CODE (checktype) == ENUMERAL_TYPE
6962 && TREE_CODE (type) == ENUMERAL_TYPE
6963 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6964 {
6965 gcc_rich_location loc (location);
6966 warning_at (&loc, OPT_Wenum_conversion,
6967 "implicit conversion from %qT to %qT",
6968 checktype, type);
6969 }
6970 }
6971
6972 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6973 {
6974 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6975 return rhs;
6976 }
6977
6978 if (coder == VOID_TYPE)
6979 {
6980 /* Except for passing an argument to an unprototyped function,
6981 this is a constraint violation. When passing an argument to
6982 an unprototyped function, it is compile-time undefined;
6983 making it a constraint in that case was rejected in
6984 DR#252. */
6985 const char msg[] = "void value not ignored as it ought to be";
6986 if (warnopt)
6987 warning_at (location, warnopt, msg);
6988 else
6989 error_at (location, msg);
6990 return error_mark_node;
6991 }
6992 rhs = require_complete_type (location, rhs);
6993 if (rhs == error_mark_node)
6994 return error_mark_node;
6995
6996 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6997 return error_mark_node;
6998
6999 /* A non-reference type can convert to a reference. This handles
7000 va_start, va_copy and possibly port built-ins. */
7001 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
7002 {
7003 if (!lvalue_p (rhs))
7004 {
7005 const char msg[] = "cannot pass rvalue to reference parameter";
7006 if (warnopt)
7007 warning_at (location, warnopt, msg);
7008 else
7009 error_at (location, msg);
7010 return error_mark_node;
7011 }
7012 if (!c_mark_addressable (rhs))
7013 return error_mark_node;
7014 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
7015 SET_EXPR_LOCATION (rhs, location);
7016
7017 rhs = convert_for_assignment (location, expr_loc,
7018 build_pointer_type (TREE_TYPE (type)),
7019 rhs, origtype, errtype,
7020 null_pointer_constant, fundecl, function,
7021 parmnum, warnopt);
7022 if (rhs == error_mark_node)
7023 return error_mark_node;
7024
7025 rhs = build1 (NOP_EXPR, type, rhs);
7026 SET_EXPR_LOCATION (rhs, location);
7027 return rhs;
7028 }
7029 /* Some types can interconvert without explicit casts. */
7030 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7031 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7032 return convert (type, rhs);
7033 /* Arithmetic types all interconvert, and enum is treated like int. */
7034 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7035 || codel == FIXED_POINT_TYPE
7036 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7037 || codel == BOOLEAN_TYPE)
7038 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7039 || coder == FIXED_POINT_TYPE
7040 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7041 || coder == BOOLEAN_TYPE))
7042 {
7043 if (warnopt && errtype == ic_argpass)
7044 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7045 rhstype);
7046
7047 bool save = in_late_binary_op;
7048 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
7049 || (coder == REAL_TYPE
7050 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7051 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7052 in_late_binary_op = true;
7053 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7054 ? expr_loc : location, type, orig_rhs,
7055 errtype == ic_init_const);
7056 in_late_binary_op = save;
7057 return ret;
7058 }
7059
7060 /* Aggregates in different TUs might need conversion. */
7061 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7062 && codel == coder
7063 && comptypes (type, rhstype))
7064 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7065 ? expr_loc : location, type, rhs);
7066
7067 /* Conversion to a transparent union or record from its member types.
7068 This applies only to function arguments. */
7069 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7070 && TYPE_TRANSPARENT_AGGR (type))
7071 && errtype == ic_argpass)
7072 {
7073 tree memb, marginal_memb = NULL_TREE;
7074
7075 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7076 {
7077 tree memb_type = TREE_TYPE (memb);
7078
7079 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7080 TYPE_MAIN_VARIANT (rhstype)))
7081 break;
7082
7083 if (TREE_CODE (memb_type) != POINTER_TYPE)
7084 continue;
7085
7086 if (coder == POINTER_TYPE)
7087 {
7088 tree ttl = TREE_TYPE (memb_type);
7089 tree ttr = TREE_TYPE (rhstype);
7090
7091 /* Any non-function converts to a [const][volatile] void *
7092 and vice versa; otherwise, targets must be the same.
7093 Meanwhile, the lhs target must have all the qualifiers of
7094 the rhs. */
7095 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7096 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7097 || comp_target_types (location, memb_type, rhstype))
7098 {
7099 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7100 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7101 /* If this type won't generate any warnings, use it. */
7102 if (lquals == rquals
7103 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7104 && TREE_CODE (ttl) == FUNCTION_TYPE)
7105 ? ((lquals | rquals) == rquals)
7106 : ((lquals | rquals) == lquals)))
7107 break;
7108
7109 /* Keep looking for a better type, but remember this one. */
7110 if (!marginal_memb)
7111 marginal_memb = memb;
7112 }
7113 }
7114
7115 /* Can convert integer zero to any pointer type. */
7116 if (null_pointer_constant)
7117 {
7118 rhs = null_pointer_node;
7119 break;
7120 }
7121 }
7122
7123 if (memb || marginal_memb)
7124 {
7125 if (!memb)
7126 {
7127 /* We have only a marginally acceptable member type;
7128 it needs a warning. */
7129 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7130 tree ttr = TREE_TYPE (rhstype);
7131
7132 /* Const and volatile mean something different for function
7133 types, so the usual warnings are not appropriate. */
7134 if (TREE_CODE (ttr) == FUNCTION_TYPE
7135 && TREE_CODE (ttl) == FUNCTION_TYPE)
7136 {
7137 /* Because const and volatile on functions are
7138 restrictions that say the function will not do
7139 certain things, it is okay to use a const or volatile
7140 function where an ordinary one is wanted, but not
7141 vice-versa. */
7142 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7143 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7144 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7145 OPT_Wdiscarded_qualifiers,
7146 G_("passing argument %d of %qE "
7147 "makes %q#v qualified function "
7148 "pointer from unqualified"),
7149 G_("assignment makes %q#v qualified "
7150 "function pointer from "
7151 "unqualified"),
7152 G_("initialization makes %q#v qualified "
7153 "function pointer from "
7154 "unqualified"),
7155 G_("return makes %q#v qualified function "
7156 "pointer from unqualified"),
7157 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7158 }
7159 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7160 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7161 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7162 OPT_Wdiscarded_qualifiers,
7163 G_("passing argument %d of %qE discards "
7164 "%qv qualifier from pointer target type"),
7165 G_("assignment discards %qv qualifier "
7166 "from pointer target type"),
7167 G_("initialization discards %qv qualifier "
7168 "from pointer target type"),
7169 G_("return discards %qv qualifier from "
7170 "pointer target type"),
7171 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7172
7173 memb = marginal_memb;
7174 }
7175
7176 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7177 pedwarn (location, OPT_Wpedantic,
7178 "ISO C prohibits argument conversion to union type");
7179
7180 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7181 return build_constructor_single (type, memb, rhs);
7182 }
7183 }
7184
7185 /* Conversions among pointers */
7186 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7187 && (coder == codel))
7188 {
7189 /* If RHS refers to a built-in declared without a prototype
7190 BLTIN is the declaration of the built-in with a prototype
7191 and RHSTYPE is set to the actual type of the built-in. */
7192 tree bltin;
7193 rhstype = type_or_builtin_type (rhs, &bltin);
7194
7195 tree ttl = TREE_TYPE (type);
7196 tree ttr = TREE_TYPE (rhstype);
7197 tree mvl = ttl;
7198 tree mvr = ttr;
7199 bool is_opaque_pointer;
7200 int target_cmp = 0; /* Cache comp_target_types () result. */
7201 addr_space_t asl;
7202 addr_space_t asr;
7203
7204 if (TREE_CODE (mvl) != ARRAY_TYPE)
7205 mvl = (TYPE_ATOMIC (mvl)
7206 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7207 TYPE_QUAL_ATOMIC)
7208 : TYPE_MAIN_VARIANT (mvl));
7209 if (TREE_CODE (mvr) != ARRAY_TYPE)
7210 mvr = (TYPE_ATOMIC (mvr)
7211 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7212 TYPE_QUAL_ATOMIC)
7213 : TYPE_MAIN_VARIANT (mvr));
7214 /* Opaque pointers are treated like void pointers. */
7215 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7216
7217 /* The Plan 9 compiler permits a pointer to a struct to be
7218 automatically converted into a pointer to an anonymous field
7219 within the struct. */
7220 if (flag_plan9_extensions
7221 && RECORD_OR_UNION_TYPE_P (mvl)
7222 && RECORD_OR_UNION_TYPE_P (mvr)
7223 && mvl != mvr)
7224 {
7225 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7226 if (new_rhs != NULL_TREE)
7227 {
7228 rhs = new_rhs;
7229 rhstype = TREE_TYPE (rhs);
7230 coder = TREE_CODE (rhstype);
7231 ttr = TREE_TYPE (rhstype);
7232 mvr = TYPE_MAIN_VARIANT (ttr);
7233 }
7234 }
7235
7236 /* C++ does not allow the implicit conversion void* -> T*. However,
7237 for the purpose of reducing the number of false positives, we
7238 tolerate the special case of
7239
7240 int *p = NULL;
7241
7242 where NULL is typically defined in C to be '(void *) 0'. */
7243 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7244 warning_at (errtype == ic_argpass ? expr_loc : location,
7245 OPT_Wc___compat,
7246 "request for implicit conversion "
7247 "from %qT to %qT not permitted in C++", rhstype, type);
7248
7249 /* See if the pointers point to incompatible address spaces. */
7250 asl = TYPE_ADDR_SPACE (ttl);
7251 asr = TYPE_ADDR_SPACE (ttr);
7252 if (!null_pointer_constant_p (rhs)
7253 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7254 {
7255 switch (errtype)
7256 {
7257 case ic_argpass:
7258 {
7259 const char msg[] = G_("passing argument %d of %qE from "
7260 "pointer to non-enclosed address space");
7261 if (warnopt)
7262 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7263 else
7264 error_at (expr_loc, msg, parmnum, rname);
7265 break;
7266 }
7267 case ic_assign:
7268 {
7269 const char msg[] = G_("assignment from pointer to "
7270 "non-enclosed address space");
7271 if (warnopt)
7272 warning_at (location, warnopt, msg);
7273 else
7274 error_at (location, msg);
7275 break;
7276 }
7277 case ic_init:
7278 case ic_init_const:
7279 {
7280 const char msg[] = G_("initialization from pointer to "
7281 "non-enclosed address space");
7282 if (warnopt)
7283 warning_at (location, warnopt, msg);
7284 else
7285 error_at (location, msg);
7286 break;
7287 }
7288 case ic_return:
7289 {
7290 const char msg[] = G_("return from pointer to "
7291 "non-enclosed address space");
7292 if (warnopt)
7293 warning_at (location, warnopt, msg);
7294 else
7295 error_at (location, msg);
7296 break;
7297 }
7298 default:
7299 gcc_unreachable ();
7300 }
7301 return error_mark_node;
7302 }
7303
7304 /* Check if the right-hand side has a format attribute but the
7305 left-hand side doesn't. */
7306 if (warn_suggest_attribute_format
7307 && check_missing_format_attribute (type, rhstype))
7308 {
7309 switch (errtype)
7310 {
7311 case ic_argpass:
7312 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7313 "argument %d of %qE might be "
7314 "a candidate for a format attribute",
7315 parmnum, rname);
7316 break;
7317 case ic_assign:
7318 warning_at (location, OPT_Wsuggest_attribute_format,
7319 "assignment left-hand side might be "
7320 "a candidate for a format attribute");
7321 break;
7322 case ic_init:
7323 case ic_init_const:
7324 warning_at (location, OPT_Wsuggest_attribute_format,
7325 "initialization left-hand side might be "
7326 "a candidate for a format attribute");
7327 break;
7328 case ic_return:
7329 warning_at (location, OPT_Wsuggest_attribute_format,
7330 "return type might be "
7331 "a candidate for a format attribute");
7332 break;
7333 default:
7334 gcc_unreachable ();
7335 }
7336 }
7337
7338 /* See if the pointers point to incompatible scalar storage orders. */
7339 if (warn_scalar_storage_order
7340 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7341 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7342 {
7343 tree t;
7344
7345 switch (errtype)
7346 {
7347 case ic_argpass:
7348 /* Do not warn for built-in functions, for example memcpy, since we
7349 control how they behave and they can be useful in this area. */
7350 if (TREE_CODE (rname) != FUNCTION_DECL
7351 || !fndecl_built_in_p (rname))
7352 warning_at (location, OPT_Wscalar_storage_order,
7353 "passing argument %d of %qE from incompatible "
7354 "scalar storage order", parmnum, rname);
7355 break;
7356 case ic_assign:
7357 /* Do not warn if the RHS is a call to a function that returns a
7358 pointer that is not an alias. */
7359 if (TREE_CODE (rhs) != CALL_EXPR
7360 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7361 || !DECL_IS_MALLOC (t))
7362 warning_at (location, OPT_Wscalar_storage_order,
7363 "assignment to %qT from pointer type %qT with "
7364 "incompatible scalar storage order", type, rhstype);
7365 break;
7366 case ic_init:
7367 case ic_init_const:
7368 /* Likewise. */
7369 if (TREE_CODE (rhs) != CALL_EXPR
7370 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7371 || !DECL_IS_MALLOC (t))
7372 warning_at (location, OPT_Wscalar_storage_order,
7373 "initialization of %qT from pointer type %qT with "
7374 "incompatible scalar storage order", type, rhstype);
7375 break;
7376 case ic_return:
7377 warning_at (location, OPT_Wscalar_storage_order,
7378 "returning %qT from pointer type with incompatible "
7379 "scalar storage order %qT", rhstype, type);
7380 break;
7381 default:
7382 gcc_unreachable ();
7383 }
7384 }
7385
7386 /* Any non-function converts to a [const][volatile] void *
7387 and vice versa; otherwise, targets must be the same.
7388 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7389 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7390 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7391 || (target_cmp = comp_target_types (location, type, rhstype))
7392 || is_opaque_pointer
7393 || ((c_common_unsigned_type (mvl)
7394 == c_common_unsigned_type (mvr))
7395 && (c_common_signed_type (mvl)
7396 == c_common_signed_type (mvr))
7397 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7398 {
7399 /* Warn about loss of qualifers from pointers to arrays with
7400 qualifiers on the element type. */
7401 if (TREE_CODE (ttr) == ARRAY_TYPE)
7402 {
7403 ttr = strip_array_types (ttr);
7404 ttl = strip_array_types (ttl);
7405
7406 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7407 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7408 WARNING_FOR_QUALIFIERS (flag_isoc2x,
7409 location, expr_loc,
7410 OPT_Wdiscarded_array_qualifiers,
7411 G_("passing argument %d of %qE discards "
7412 "%qv qualifier from pointer target type"),
7413 G_("assignment discards %qv qualifier "
7414 "from pointer target type"),
7415 G_("initialization discards %qv qualifier "
7416 "from pointer target type"),
7417 G_("return discards %qv qualifier from "
7418 "pointer target type"),
7419 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7420 }
7421 else if (pedantic
7422 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7423 ||
7424 (VOID_TYPE_P (ttr)
7425 && !null_pointer_constant
7426 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7427 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7428 G_("ISO C forbids passing argument %d of "
7429 "%qE between function pointer "
7430 "and %<void *%>"),
7431 G_("ISO C forbids assignment between "
7432 "function pointer and %<void *%>"),
7433 G_("ISO C forbids initialization between "
7434 "function pointer and %<void *%>"),
7435 G_("ISO C forbids return between function "
7436 "pointer and %<void *%>"));
7437 /* Const and volatile mean something different for function types,
7438 so the usual warnings are not appropriate. */
7439 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7440 && TREE_CODE (ttl) != FUNCTION_TYPE)
7441 {
7442 /* Assignments between atomic and non-atomic objects are OK. */
7443 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7444 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7445 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7446 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7447
7448 /* Don't warn about loss of qualifier for conversions from
7449 qualified void* to pointers to arrays with corresponding
7450 qualifier on the element type (except for pedantic before C23). */
7451 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc2x))
7452 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7453 OPT_Wdiscarded_qualifiers,
7454 G_("passing argument %d of %qE discards "
7455 "%qv qualifier from pointer target type"),
7456 G_("assignment discards %qv qualifier "
7457 "from pointer target type"),
7458 G_("initialization discards %qv qualifier "
7459 "from pointer target type"),
7460 G_("return discards %qv qualifier from "
7461 "pointer target type"),
7462 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7463 else if (warn_quals_ped)
7464 pedwarn_c11 (location, OPT_Wc11_c2x_compat,
7465 "array with qualifier on the element is not qualified before C2X");
7466
7467 /* If this is not a case of ignoring a mismatch in signedness,
7468 no warning. */
7469 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7470 || target_cmp)
7471 ;
7472 /* If there is a mismatch, do warn. */
7473 else if (warn_pointer_sign)
7474 switch (errtype)
7475 {
7476 case ic_argpass:
7477 {
7478 auto_diagnostic_group d;
7479 range_label_for_type_mismatch rhs_label (rhstype, type);
7480 gcc_rich_location richloc (expr_loc, &rhs_label);
7481 if (pedwarn (&richloc, OPT_Wpointer_sign,
7482 "pointer targets in passing argument %d of "
7483 "%qE differ in signedness", parmnum, rname))
7484 inform_for_arg (fundecl, expr_loc, parmnum, type,
7485 rhstype);
7486 }
7487 break;
7488 case ic_assign:
7489 pedwarn (location, OPT_Wpointer_sign,
7490 "pointer targets in assignment from %qT to %qT "
7491 "differ in signedness", rhstype, type);
7492 break;
7493 case ic_init:
7494 case ic_init_const:
7495 pedwarn_init (location, OPT_Wpointer_sign,
7496 "pointer targets in initialization of %qT "
7497 "from %qT differ in signedness", type,
7498 rhstype);
7499 break;
7500 case ic_return:
7501 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7502 "returning %qT from a function with return type "
7503 "%qT differ in signedness", rhstype, type);
7504 break;
7505 default:
7506 gcc_unreachable ();
7507 }
7508 }
7509 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7510 && TREE_CODE (ttr) == FUNCTION_TYPE)
7511 {
7512 /* Because const and volatile on functions are restrictions
7513 that say the function will not do certain things,
7514 it is okay to use a const or volatile function
7515 where an ordinary one is wanted, but not vice-versa. */
7516 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7517 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7518 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7519 OPT_Wdiscarded_qualifiers,
7520 G_("passing argument %d of %qE makes "
7521 "%q#v qualified function pointer "
7522 "from unqualified"),
7523 G_("assignment makes %q#v qualified function "
7524 "pointer from unqualified"),
7525 G_("initialization makes %q#v qualified "
7526 "function pointer from unqualified"),
7527 G_("return makes %q#v qualified function "
7528 "pointer from unqualified"),
7529 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7530 }
7531 }
7532 /* Avoid warning about the volatile ObjC EH puts on decls. */
7533 else if (!objc_ok)
7534 {
7535 switch (errtype)
7536 {
7537 case ic_argpass:
7538 {
7539 auto_diagnostic_group d;
7540 range_label_for_type_mismatch rhs_label (rhstype, type);
7541 gcc_rich_location richloc (expr_loc, &rhs_label);
7542 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7543 "passing argument %d of %qE from incompatible "
7544 "pointer type", parmnum, rname))
7545 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7546 }
7547 break;
7548 case ic_assign:
7549 if (bltin)
7550 pedwarn (location, OPT_Wincompatible_pointer_types,
7551 "assignment to %qT from pointer to "
7552 "%qD with incompatible type %qT",
7553 type, bltin, rhstype);
7554 else
7555 pedwarn (location, OPT_Wincompatible_pointer_types,
7556 "assignment to %qT from incompatible pointer type %qT",
7557 type, rhstype);
7558 break;
7559 case ic_init:
7560 case ic_init_const:
7561 if (bltin)
7562 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7563 "initialization of %qT from pointer to "
7564 "%qD with incompatible type %qT",
7565 type, bltin, rhstype);
7566 else
7567 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7568 "initialization of %qT from incompatible "
7569 "pointer type %qT",
7570 type, rhstype);
7571 break;
7572 case ic_return:
7573 if (bltin)
7574 pedwarn (location, OPT_Wincompatible_pointer_types,
7575 "returning pointer to %qD of type %qT from "
7576 "a function with incompatible type %qT",
7577 bltin, rhstype, type);
7578 else
7579 pedwarn (location, OPT_Wincompatible_pointer_types,
7580 "returning %qT from a function with incompatible "
7581 "return type %qT", rhstype, type);
7582 break;
7583 default:
7584 gcc_unreachable ();
7585 }
7586 }
7587
7588 /* If RHS isn't an address, check pointer or array of packed
7589 struct or union. */
7590 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7591
7592 return convert (type, rhs);
7593 }
7594 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7595 {
7596 /* ??? This should not be an error when inlining calls to
7597 unprototyped functions. */
7598 const char msg[] = "invalid use of non-lvalue array";
7599 if (warnopt)
7600 warning_at (location, warnopt, msg);
7601 else
7602 error_at (location, msg);
7603 return error_mark_node;
7604 }
7605 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7606 {
7607 /* An explicit constant 0 can convert to a pointer,
7608 or one that results from arithmetic, even including
7609 a cast to integer type. */
7610 if (!null_pointer_constant)
7611 switch (errtype)
7612 {
7613 case ic_argpass:
7614 {
7615 auto_diagnostic_group d;
7616 range_label_for_type_mismatch rhs_label (rhstype, type);
7617 gcc_rich_location richloc (expr_loc, &rhs_label);
7618 if (pedwarn (&richloc, OPT_Wint_conversion,
7619 "passing argument %d of %qE makes pointer from "
7620 "integer without a cast", parmnum, rname))
7621 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7622 }
7623 break;
7624 case ic_assign:
7625 pedwarn (location, OPT_Wint_conversion,
7626 "assignment to %qT from %qT makes pointer from integer "
7627 "without a cast", type, rhstype);
7628 break;
7629 case ic_init:
7630 case ic_init_const:
7631 pedwarn_init (location, OPT_Wint_conversion,
7632 "initialization of %qT from %qT makes pointer from "
7633 "integer without a cast", type, rhstype);
7634 break;
7635 case ic_return:
7636 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7637 "function with return type %qT makes pointer from "
7638 "integer without a cast", rhstype, type);
7639 break;
7640 default:
7641 gcc_unreachable ();
7642 }
7643
7644 return convert (type, rhs);
7645 }
7646 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7647 {
7648 switch (errtype)
7649 {
7650 case ic_argpass:
7651 {
7652 auto_diagnostic_group d;
7653 range_label_for_type_mismatch rhs_label (rhstype, type);
7654 gcc_rich_location richloc (expr_loc, &rhs_label);
7655 if (pedwarn (&richloc, OPT_Wint_conversion,
7656 "passing argument %d of %qE makes integer from "
7657 "pointer without a cast", parmnum, rname))
7658 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7659 }
7660 break;
7661 case ic_assign:
7662 pedwarn (location, OPT_Wint_conversion,
7663 "assignment to %qT from %qT makes integer from pointer "
7664 "without a cast", type, rhstype);
7665 break;
7666 case ic_init:
7667 case ic_init_const:
7668 pedwarn_init (location, OPT_Wint_conversion,
7669 "initialization of %qT from %qT makes integer from "
7670 "pointer without a cast", type, rhstype);
7671 break;
7672 case ic_return:
7673 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7674 "function with return type %qT makes integer from "
7675 "pointer without a cast", rhstype, type);
7676 break;
7677 default:
7678 gcc_unreachable ();
7679 }
7680
7681 return convert (type, rhs);
7682 }
7683 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7684 {
7685 tree ret;
7686 bool save = in_late_binary_op;
7687 in_late_binary_op = true;
7688 ret = convert (type, rhs);
7689 in_late_binary_op = save;
7690 return ret;
7691 }
7692
7693 switch (errtype)
7694 {
7695 case ic_argpass:
7696 {
7697 auto_diagnostic_group d;
7698 range_label_for_type_mismatch rhs_label (rhstype, type);
7699 gcc_rich_location richloc (expr_loc, &rhs_label);
7700 const char msg[] = G_("incompatible type for argument %d of %qE");
7701 if (warnopt)
7702 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7703 else
7704 error_at (&richloc, msg, parmnum, rname);
7705 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7706 }
7707 break;
7708 case ic_assign:
7709 {
7710 const char msg[]
7711 = G_("incompatible types when assigning to type %qT from type %qT");
7712 if (warnopt)
7713 warning_at (expr_loc, 0, msg, type, rhstype);
7714 else
7715 error_at (expr_loc, msg, type, rhstype);
7716 break;
7717 }
7718 case ic_init:
7719 case ic_init_const:
7720 {
7721 const char msg[]
7722 = G_("incompatible types when initializing type %qT using type %qT");
7723 if (warnopt)
7724 warning_at (location, 0, msg, type, rhstype);
7725 else
7726 error_at (location, msg, type, rhstype);
7727 break;
7728 }
7729 case ic_return:
7730 {
7731 const char msg[]
7732 = G_("incompatible types when returning type %qT but %qT was expected");
7733 if (warnopt)
7734 warning_at (location, 0, msg, rhstype, type);
7735 else
7736 error_at (location, msg, rhstype, type);
7737 break;
7738 }
7739 default:
7740 gcc_unreachable ();
7741 }
7742
7743 return error_mark_node;
7744 }
7745 \f
7746 /* If VALUE is a compound expr all of whose expressions are constant, then
7747 return its value. Otherwise, return error_mark_node.
7748
7749 This is for handling COMPOUND_EXPRs as initializer elements
7750 which is allowed with a warning when -pedantic is specified. */
7751
7752 static tree
7753 valid_compound_expr_initializer (tree value, tree endtype)
7754 {
7755 if (TREE_CODE (value) == COMPOUND_EXPR)
7756 {
7757 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7758 == error_mark_node)
7759 return error_mark_node;
7760 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7761 endtype);
7762 }
7763 else if (!initializer_constant_valid_p (value, endtype))
7764 return error_mark_node;
7765 else
7766 return value;
7767 }
7768 \f
7769 /* Perform appropriate conversions on the initial value of a variable,
7770 store it in the declaration DECL,
7771 and print any error messages that are appropriate.
7772 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7773 If the init is invalid, store an ERROR_MARK.
7774
7775 INIT_LOC is the location of the initial value. */
7776
7777 void
7778 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7779 {
7780 tree value, type;
7781 bool npc = false;
7782
7783 /* If variable's type was invalidly declared, just ignore it. */
7784
7785 type = TREE_TYPE (decl);
7786 if (TREE_CODE (type) == ERROR_MARK)
7787 return;
7788
7789 /* Digest the specified initializer into an expression. */
7790
7791 if (init)
7792 npc = null_pointer_constant_p (init);
7793 value = digest_init (init_loc, type, init, origtype, npc,
7794 true, TREE_STATIC (decl));
7795
7796 /* Store the expression if valid; else report error. */
7797
7798 if (!in_system_header_at (input_location)
7799 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7800 warning (OPT_Wtraditional, "traditional C rejects automatic "
7801 "aggregate initialization");
7802
7803 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7804 DECL_INITIAL (decl) = value;
7805
7806 /* ANSI wants warnings about out-of-range constant initializers. */
7807 STRIP_TYPE_NOPS (value);
7808 if (TREE_STATIC (decl))
7809 constant_expression_warning (value);
7810
7811 /* Check if we need to set array size from compound literal size. */
7812 if (TREE_CODE (type) == ARRAY_TYPE
7813 && TYPE_DOMAIN (type) == NULL_TREE
7814 && value != error_mark_node)
7815 {
7816 tree inside_init = init;
7817
7818 STRIP_TYPE_NOPS (inside_init);
7819 inside_init = fold (inside_init);
7820
7821 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7822 {
7823 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7824
7825 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7826 {
7827 /* For int foo[] = (int [3]){1}; we need to set array size
7828 now since later on array initializer will be just the
7829 brace enclosed list of the compound literal. */
7830 tree etype = strip_array_types (TREE_TYPE (decl));
7831 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7832 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7833 layout_type (type);
7834 layout_decl (cldecl, 0);
7835 TREE_TYPE (decl)
7836 = c_build_qualified_type (type, TYPE_QUALS (etype));
7837 }
7838 }
7839 }
7840 }
7841 \f
7842 /* Methods for storing and printing names for error messages. */
7843
7844 /* Implement a spelling stack that allows components of a name to be pushed
7845 and popped. Each element on the stack is this structure. */
7846
7847 struct spelling
7848 {
7849 int kind;
7850 union
7851 {
7852 unsigned HOST_WIDE_INT i;
7853 const char *s;
7854 } u;
7855 };
7856
7857 #define SPELLING_STRING 1
7858 #define SPELLING_MEMBER 2
7859 #define SPELLING_BOUNDS 3
7860
7861 static struct spelling *spelling; /* Next stack element (unused). */
7862 static struct spelling *spelling_base; /* Spelling stack base. */
7863 static int spelling_size; /* Size of the spelling stack. */
7864
7865 /* Macros to save and restore the spelling stack around push_... functions.
7866 Alternative to SAVE_SPELLING_STACK. */
7867
7868 #define SPELLING_DEPTH() (spelling - spelling_base)
7869 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7870
7871 /* Push an element on the spelling stack with type KIND and assign VALUE
7872 to MEMBER. */
7873
7874 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7875 { \
7876 int depth = SPELLING_DEPTH (); \
7877 \
7878 if (depth >= spelling_size) \
7879 { \
7880 spelling_size += 10; \
7881 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7882 spelling_size); \
7883 RESTORE_SPELLING_DEPTH (depth); \
7884 } \
7885 \
7886 spelling->kind = (KIND); \
7887 spelling->MEMBER = (VALUE); \
7888 spelling++; \
7889 }
7890
7891 /* Push STRING on the stack. Printed literally. */
7892
7893 static void
7894 push_string (const char *string)
7895 {
7896 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7897 }
7898
7899 /* Push a member name on the stack. Printed as '.' STRING. */
7900
7901 static void
7902 push_member_name (tree decl)
7903 {
7904 const char *const string
7905 = (DECL_NAME (decl)
7906 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7907 : _("<anonymous>"));
7908 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7909 }
7910
7911 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7912
7913 static void
7914 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7915 {
7916 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7917 }
7918
7919 /* Compute the maximum size in bytes of the printed spelling. */
7920
7921 static int
7922 spelling_length (void)
7923 {
7924 int size = 0;
7925 struct spelling *p;
7926
7927 for (p = spelling_base; p < spelling; p++)
7928 {
7929 if (p->kind == SPELLING_BOUNDS)
7930 size += 25;
7931 else
7932 size += strlen (p->u.s) + 1;
7933 }
7934
7935 return size;
7936 }
7937
7938 /* Print the spelling to BUFFER and return it. */
7939
7940 static char *
7941 print_spelling (char *buffer)
7942 {
7943 char *d = buffer;
7944 struct spelling *p;
7945
7946 for (p = spelling_base; p < spelling; p++)
7947 if (p->kind == SPELLING_BOUNDS)
7948 {
7949 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7950 d += strlen (d);
7951 }
7952 else
7953 {
7954 const char *s;
7955 if (p->kind == SPELLING_MEMBER)
7956 *d++ = '.';
7957 for (s = p->u.s; (*d = *s++); d++)
7958 ;
7959 }
7960 *d++ = '\0';
7961 return buffer;
7962 }
7963
7964 /* Digest the parser output INIT as an initializer for type TYPE.
7965 Return a C expression of type TYPE to represent the initial value.
7966
7967 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7968
7969 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7970
7971 If INIT is a string constant, STRICT_STRING is true if it is
7972 unparenthesized or we should not warn here for it being parenthesized.
7973 For other types of INIT, STRICT_STRING is not used.
7974
7975 INIT_LOC is the location of the INIT.
7976
7977 REQUIRE_CONSTANT requests an error if non-constant initializers or
7978 elements are seen. */
7979
7980 static tree
7981 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7982 bool null_pointer_constant, bool strict_string,
7983 int require_constant)
7984 {
7985 enum tree_code code = TREE_CODE (type);
7986 tree inside_init = init;
7987 tree semantic_type = NULL_TREE;
7988 bool maybe_const = true;
7989
7990 if (type == error_mark_node
7991 || !init
7992 || error_operand_p (init))
7993 return error_mark_node;
7994
7995 STRIP_TYPE_NOPS (inside_init);
7996
7997 if (!c_in_omp_for)
7998 {
7999 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
8000 {
8001 semantic_type = TREE_TYPE (inside_init);
8002 inside_init = TREE_OPERAND (inside_init, 0);
8003 }
8004 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
8005 }
8006
8007 /* Initialization of an array of chars from a string constant
8008 optionally enclosed in braces. */
8009
8010 if (code == ARRAY_TYPE && inside_init
8011 && TREE_CODE (inside_init) == STRING_CST)
8012 {
8013 tree typ1
8014 = (TYPE_ATOMIC (TREE_TYPE (type))
8015 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
8016 TYPE_QUAL_ATOMIC)
8017 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
8018 /* Note that an array could be both an array of character type
8019 and an array of wchar_t if wchar_t is signed char or unsigned
8020 char. */
8021 bool char_array = (typ1 == char_type_node
8022 || typ1 == signed_char_type_node
8023 || typ1 == unsigned_char_type_node);
8024 bool wchar_array = !!comptypes (typ1, wchar_type_node);
8025 bool char16_array = !!comptypes (typ1, char16_type_node);
8026 bool char32_array = !!comptypes (typ1, char32_type_node);
8027
8028 if (char_array || wchar_array || char16_array || char32_array)
8029 {
8030 struct c_expr expr;
8031 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8032 bool incompat_string_cst = false;
8033 expr.value = inside_init;
8034 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8035 expr.original_type = NULL;
8036 maybe_warn_string_init (init_loc, type, expr);
8037
8038 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8039 pedwarn_init (init_loc, OPT_Wpedantic,
8040 "initialization of a flexible array member");
8041
8042 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8043 TYPE_MAIN_VARIANT (type)))
8044 return inside_init;
8045
8046 if (char_array)
8047 {
8048 if (typ2 != char_type_node)
8049 incompat_string_cst = true;
8050 }
8051 else if (!comptypes (typ1, typ2))
8052 incompat_string_cst = true;
8053
8054 if (incompat_string_cst)
8055 {
8056 error_init (init_loc, "cannot initialize array of %qT from "
8057 "a string literal with type array of %qT",
8058 typ1, typ2);
8059 return error_mark_node;
8060 }
8061
8062 if (TYPE_DOMAIN (type) != NULL_TREE
8063 && TYPE_SIZE (type) != NULL_TREE
8064 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8065 {
8066 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8067 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8068
8069 /* Subtract the size of a single (possibly wide) character
8070 because it's ok to ignore the terminating null char
8071 that is counted in the length of the constant. */
8072 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8073 pedwarn_init (init_loc, 0,
8074 ("initializer-string for array of %qT "
8075 "is too long"), typ1);
8076 else if (warn_cxx_compat
8077 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8078 warning_at (init_loc, OPT_Wc___compat,
8079 ("initializer-string for array of %qT "
8080 "is too long for C++"), typ1);
8081 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8082 {
8083 unsigned HOST_WIDE_INT size
8084 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8085 const char *p = TREE_STRING_POINTER (inside_init);
8086
8087 inside_init = build_string (size, p);
8088 }
8089 }
8090
8091 TREE_TYPE (inside_init) = type;
8092 return inside_init;
8093 }
8094 else if (INTEGRAL_TYPE_P (typ1))
8095 {
8096 error_init (init_loc, "array of inappropriate type initialized "
8097 "from string constant");
8098 return error_mark_node;
8099 }
8100 }
8101
8102 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8103 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8104 below and handle as a constructor. */
8105 if (code == VECTOR_TYPE
8106 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8107 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8108 && TREE_CONSTANT (inside_init))
8109 {
8110 if (TREE_CODE (inside_init) == VECTOR_CST
8111 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8112 TYPE_MAIN_VARIANT (type)))
8113 return inside_init;
8114
8115 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8116 {
8117 unsigned HOST_WIDE_INT ix;
8118 tree value;
8119 bool constant_p = true;
8120
8121 /* Iterate through elements and check if all constructor
8122 elements are *_CSTs. */
8123 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8124 if (!CONSTANT_CLASS_P (value))
8125 {
8126 constant_p = false;
8127 break;
8128 }
8129
8130 if (constant_p)
8131 return build_vector_from_ctor (type,
8132 CONSTRUCTOR_ELTS (inside_init));
8133 }
8134 }
8135
8136 if (warn_sequence_point)
8137 verify_sequence_points (inside_init);
8138
8139 /* Any type can be initialized
8140 from an expression of the same type, optionally with braces. */
8141
8142 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8143 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8144 TYPE_MAIN_VARIANT (type))
8145 || (code == ARRAY_TYPE
8146 && comptypes (TREE_TYPE (inside_init), type))
8147 || (gnu_vector_type_p (type)
8148 && comptypes (TREE_TYPE (inside_init), type))
8149 || (code == POINTER_TYPE
8150 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8151 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8152 TREE_TYPE (type)))))
8153 {
8154 if (code == POINTER_TYPE)
8155 {
8156 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8157 {
8158 if (TREE_CODE (inside_init) == STRING_CST
8159 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8160 inside_init = array_to_pointer_conversion
8161 (init_loc, inside_init);
8162 else
8163 {
8164 error_init (init_loc, "invalid use of non-lvalue array");
8165 return error_mark_node;
8166 }
8167 }
8168 }
8169
8170 if (code == VECTOR_TYPE)
8171 /* Although the types are compatible, we may require a
8172 conversion. */
8173 inside_init = convert (type, inside_init);
8174
8175 if (require_constant
8176 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8177 {
8178 /* As an extension, allow initializing objects with static storage
8179 duration with compound literals (which are then treated just as
8180 the brace enclosed list they contain). Also allow this for
8181 vectors, as we can only assign them with compound literals. */
8182 if (flag_isoc99 && code != VECTOR_TYPE)
8183 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8184 "is not constant");
8185 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8186 inside_init = DECL_INITIAL (decl);
8187 }
8188
8189 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8190 && TREE_CODE (inside_init) != CONSTRUCTOR)
8191 {
8192 error_init (init_loc, "array initialized from non-constant array "
8193 "expression");
8194 return error_mark_node;
8195 }
8196
8197 /* Compound expressions can only occur here if -Wpedantic or
8198 -pedantic-errors is specified. In the later case, we always want
8199 an error. In the former case, we simply want a warning. */
8200 if (require_constant && pedantic
8201 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8202 {
8203 inside_init
8204 = valid_compound_expr_initializer (inside_init,
8205 TREE_TYPE (inside_init));
8206 if (inside_init == error_mark_node)
8207 error_init (init_loc, "initializer element is not constant");
8208 else
8209 pedwarn_init (init_loc, OPT_Wpedantic,
8210 "initializer element is not constant");
8211 if (flag_pedantic_errors)
8212 inside_init = error_mark_node;
8213 }
8214 else if (require_constant
8215 && !initializer_constant_valid_p (inside_init,
8216 TREE_TYPE (inside_init)))
8217 {
8218 error_init (init_loc, "initializer element is not constant");
8219 inside_init = error_mark_node;
8220 }
8221 else if (require_constant && !maybe_const)
8222 pedwarn_init (init_loc, OPT_Wpedantic,
8223 "initializer element is not a constant expression");
8224
8225 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8226 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8227 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8228 type, inside_init, origtype,
8229 (require_constant
8230 ? ic_init_const
8231 : ic_init), null_pointer_constant,
8232 NULL_TREE, NULL_TREE, 0);
8233 return inside_init;
8234 }
8235
8236 /* Handle scalar types, including conversions. */
8237
8238 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8239 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8240 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8241 {
8242 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8243 && (TREE_CODE (init) == STRING_CST
8244 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8245 inside_init = init = array_to_pointer_conversion (init_loc, init);
8246 if (semantic_type)
8247 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8248 inside_init);
8249 inside_init
8250 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8251 inside_init, origtype,
8252 require_constant ? ic_init_const : ic_init,
8253 null_pointer_constant, NULL_TREE, NULL_TREE,
8254 0);
8255
8256 /* Check to see if we have already given an error message. */
8257 if (inside_init == error_mark_node)
8258 ;
8259 else if (require_constant && !TREE_CONSTANT (inside_init))
8260 {
8261 error_init (init_loc, "initializer element is not constant");
8262 inside_init = error_mark_node;
8263 }
8264 else if (require_constant
8265 && !initializer_constant_valid_p (inside_init,
8266 TREE_TYPE (inside_init)))
8267 {
8268 error_init (init_loc, "initializer element is not computable at "
8269 "load time");
8270 inside_init = error_mark_node;
8271 }
8272 else if (require_constant && !maybe_const)
8273 pedwarn_init (init_loc, OPT_Wpedantic,
8274 "initializer element is not a constant expression");
8275
8276 return inside_init;
8277 }
8278
8279 /* Come here only for records and arrays. */
8280
8281 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8282 {
8283 error_init (init_loc, "variable-sized object may not be initialized");
8284 return error_mark_node;
8285 }
8286
8287 error_init (init_loc, "invalid initializer");
8288 return error_mark_node;
8289 }
8290 \f
8291 /* Handle initializers that use braces. */
8292
8293 /* Type of object we are accumulating a constructor for.
8294 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8295 static tree constructor_type;
8296
8297 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8298 left to fill. */
8299 static tree constructor_fields;
8300
8301 /* For an ARRAY_TYPE, this is the specified index
8302 at which to store the next element we get. */
8303 static tree constructor_index;
8304
8305 /* For an ARRAY_TYPE, this is the maximum index. */
8306 static tree constructor_max_index;
8307
8308 /* For a RECORD_TYPE, this is the first field not yet written out. */
8309 static tree constructor_unfilled_fields;
8310
8311 /* For an ARRAY_TYPE, this is the index of the first element
8312 not yet written out. */
8313 static tree constructor_unfilled_index;
8314
8315 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8316 This is so we can generate gaps between fields, when appropriate. */
8317 static tree constructor_bit_index;
8318
8319 /* If we are saving up the elements rather than allocating them,
8320 this is the list of elements so far (in reverse order,
8321 most recent first). */
8322 static vec<constructor_elt, va_gc> *constructor_elements;
8323
8324 /* 1 if constructor should be incrementally stored into a constructor chain,
8325 0 if all the elements should be kept in AVL tree. */
8326 static int constructor_incremental;
8327
8328 /* 1 if so far this constructor's elements are all compile-time constants. */
8329 static int constructor_constant;
8330
8331 /* 1 if so far this constructor's elements are all valid address constants. */
8332 static int constructor_simple;
8333
8334 /* 1 if this constructor has an element that cannot be part of a
8335 constant expression. */
8336 static int constructor_nonconst;
8337
8338 /* 1 if this constructor is erroneous so far. */
8339 static int constructor_erroneous;
8340
8341 /* 1 if this constructor is the universal zero initializer { 0 }. */
8342 static int constructor_zeroinit;
8343
8344 /* Structure for managing pending initializer elements, organized as an
8345 AVL tree. */
8346
8347 struct init_node
8348 {
8349 struct init_node *left, *right;
8350 struct init_node *parent;
8351 int balance;
8352 tree purpose;
8353 tree value;
8354 tree origtype;
8355 };
8356
8357 /* Tree of pending elements at this constructor level.
8358 These are elements encountered out of order
8359 which belong at places we haven't reached yet in actually
8360 writing the output.
8361 Will never hold tree nodes across GC runs. */
8362 static struct init_node *constructor_pending_elts;
8363
8364 /* The SPELLING_DEPTH of this constructor. */
8365 static int constructor_depth;
8366
8367 /* DECL node for which an initializer is being read.
8368 0 means we are reading a constructor expression
8369 such as (struct foo) {...}. */
8370 static tree constructor_decl;
8371
8372 /* Nonzero if this is an initializer for a top-level decl. */
8373 static int constructor_top_level;
8374
8375 /* Nonzero if there were any member designators in this initializer. */
8376 static int constructor_designated;
8377
8378 /* Nesting depth of designator list. */
8379 static int designator_depth;
8380
8381 /* Nonzero if there were diagnosed errors in this designator list. */
8382 static int designator_erroneous;
8383
8384 \f
8385 /* This stack has a level for each implicit or explicit level of
8386 structuring in the initializer, including the outermost one. It
8387 saves the values of most of the variables above. */
8388
8389 struct constructor_range_stack;
8390
8391 struct constructor_stack
8392 {
8393 struct constructor_stack *next;
8394 tree type;
8395 tree fields;
8396 tree index;
8397 tree max_index;
8398 tree unfilled_index;
8399 tree unfilled_fields;
8400 tree bit_index;
8401 vec<constructor_elt, va_gc> *elements;
8402 struct init_node *pending_elts;
8403 int offset;
8404 int depth;
8405 /* If value nonzero, this value should replace the entire
8406 constructor at this level. */
8407 struct c_expr replacement_value;
8408 struct constructor_range_stack *range_stack;
8409 char constant;
8410 char simple;
8411 char nonconst;
8412 char implicit;
8413 char erroneous;
8414 char outer;
8415 char incremental;
8416 char designated;
8417 int designator_depth;
8418 };
8419
8420 static struct constructor_stack *constructor_stack;
8421
8422 /* This stack represents designators from some range designator up to
8423 the last designator in the list. */
8424
8425 struct constructor_range_stack
8426 {
8427 struct constructor_range_stack *next, *prev;
8428 struct constructor_stack *stack;
8429 tree range_start;
8430 tree index;
8431 tree range_end;
8432 tree fields;
8433 };
8434
8435 static struct constructor_range_stack *constructor_range_stack;
8436
8437 /* This stack records separate initializers that are nested.
8438 Nested initializers can't happen in ANSI C, but GNU C allows them
8439 in cases like { ... (struct foo) { ... } ... }. */
8440
8441 struct initializer_stack
8442 {
8443 struct initializer_stack *next;
8444 tree decl;
8445 struct constructor_stack *constructor_stack;
8446 struct constructor_range_stack *constructor_range_stack;
8447 vec<constructor_elt, va_gc> *elements;
8448 struct spelling *spelling;
8449 struct spelling *spelling_base;
8450 int spelling_size;
8451 char top_level;
8452 char require_constant_value;
8453 char require_constant_elements;
8454 char designated;
8455 rich_location *missing_brace_richloc;
8456 };
8457
8458 static struct initializer_stack *initializer_stack;
8459 \f
8460 /* Prepare to parse and output the initializer for variable DECL. */
8461
8462 void
8463 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8464 rich_location *richloc)
8465 {
8466 const char *locus;
8467 struct initializer_stack *p = XNEW (struct initializer_stack);
8468
8469 p->decl = constructor_decl;
8470 p->require_constant_value = require_constant_value;
8471 p->require_constant_elements = require_constant_elements;
8472 p->constructor_stack = constructor_stack;
8473 p->constructor_range_stack = constructor_range_stack;
8474 p->elements = constructor_elements;
8475 p->spelling = spelling;
8476 p->spelling_base = spelling_base;
8477 p->spelling_size = spelling_size;
8478 p->top_level = constructor_top_level;
8479 p->next = initializer_stack;
8480 p->missing_brace_richloc = richloc;
8481 p->designated = constructor_designated;
8482 initializer_stack = p;
8483
8484 constructor_decl = decl;
8485 constructor_designated = 0;
8486 constructor_top_level = top_level;
8487
8488 if (decl != NULL_TREE && decl != error_mark_node)
8489 {
8490 require_constant_value = TREE_STATIC (decl);
8491 require_constant_elements
8492 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8493 /* For a scalar, you can always use any value to initialize,
8494 even within braces. */
8495 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8496 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8497 }
8498 else
8499 {
8500 require_constant_value = 0;
8501 require_constant_elements = 0;
8502 locus = _("(anonymous)");
8503 }
8504
8505 constructor_stack = 0;
8506 constructor_range_stack = 0;
8507
8508 found_missing_braces = 0;
8509
8510 spelling_base = 0;
8511 spelling_size = 0;
8512 RESTORE_SPELLING_DEPTH (0);
8513
8514 if (locus)
8515 push_string (locus);
8516 }
8517
8518 void
8519 finish_init (void)
8520 {
8521 struct initializer_stack *p = initializer_stack;
8522
8523 /* Free the whole constructor stack of this initializer. */
8524 while (constructor_stack)
8525 {
8526 struct constructor_stack *q = constructor_stack;
8527 constructor_stack = q->next;
8528 XDELETE (q);
8529 }
8530
8531 gcc_assert (!constructor_range_stack);
8532
8533 /* Pop back to the data of the outer initializer (if any). */
8534 XDELETE (spelling_base);
8535
8536 constructor_decl = p->decl;
8537 require_constant_value = p->require_constant_value;
8538 require_constant_elements = p->require_constant_elements;
8539 constructor_stack = p->constructor_stack;
8540 constructor_designated = p->designated;
8541 constructor_range_stack = p->constructor_range_stack;
8542 constructor_elements = p->elements;
8543 spelling = p->spelling;
8544 spelling_base = p->spelling_base;
8545 spelling_size = p->spelling_size;
8546 constructor_top_level = p->top_level;
8547 initializer_stack = p->next;
8548 XDELETE (p);
8549 }
8550 \f
8551 /* Call here when we see the initializer is surrounded by braces.
8552 This is instead of a call to push_init_level;
8553 it is matched by a call to pop_init_level.
8554
8555 TYPE is the type to initialize, for a constructor expression.
8556 For an initializer for a decl, TYPE is zero. */
8557
8558 void
8559 really_start_incremental_init (tree type)
8560 {
8561 struct constructor_stack *p = XNEW (struct constructor_stack);
8562
8563 if (type == NULL_TREE)
8564 type = TREE_TYPE (constructor_decl);
8565
8566 if (VECTOR_TYPE_P (type)
8567 && TYPE_VECTOR_OPAQUE (type))
8568 error ("opaque vector types cannot be initialized");
8569
8570 p->type = constructor_type;
8571 p->fields = constructor_fields;
8572 p->index = constructor_index;
8573 p->max_index = constructor_max_index;
8574 p->unfilled_index = constructor_unfilled_index;
8575 p->unfilled_fields = constructor_unfilled_fields;
8576 p->bit_index = constructor_bit_index;
8577 p->elements = constructor_elements;
8578 p->constant = constructor_constant;
8579 p->simple = constructor_simple;
8580 p->nonconst = constructor_nonconst;
8581 p->erroneous = constructor_erroneous;
8582 p->pending_elts = constructor_pending_elts;
8583 p->depth = constructor_depth;
8584 p->replacement_value.value = 0;
8585 p->replacement_value.original_code = ERROR_MARK;
8586 p->replacement_value.original_type = NULL;
8587 p->implicit = 0;
8588 p->range_stack = 0;
8589 p->outer = 0;
8590 p->incremental = constructor_incremental;
8591 p->designated = constructor_designated;
8592 p->designator_depth = designator_depth;
8593 p->next = 0;
8594 constructor_stack = p;
8595
8596 constructor_constant = 1;
8597 constructor_simple = 1;
8598 constructor_nonconst = 0;
8599 constructor_depth = SPELLING_DEPTH ();
8600 constructor_elements = NULL;
8601 constructor_pending_elts = 0;
8602 constructor_type = type;
8603 constructor_incremental = 1;
8604 constructor_designated = 0;
8605 constructor_zeroinit = 1;
8606 designator_depth = 0;
8607 designator_erroneous = 0;
8608
8609 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8610 {
8611 constructor_fields = TYPE_FIELDS (constructor_type);
8612 /* Skip any nameless bit fields at the beginning. */
8613 while (constructor_fields != NULL_TREE
8614 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8615 constructor_fields = DECL_CHAIN (constructor_fields);
8616
8617 constructor_unfilled_fields = constructor_fields;
8618 constructor_bit_index = bitsize_zero_node;
8619 }
8620 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8621 {
8622 if (TYPE_DOMAIN (constructor_type))
8623 {
8624 constructor_max_index
8625 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8626
8627 /* Detect non-empty initializations of zero-length arrays. */
8628 if (constructor_max_index == NULL_TREE
8629 && TYPE_SIZE (constructor_type))
8630 constructor_max_index = integer_minus_one_node;
8631
8632 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8633 to initialize VLAs will cause a proper error; avoid tree
8634 checking errors as well by setting a safe value. */
8635 if (constructor_max_index
8636 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8637 constructor_max_index = integer_minus_one_node;
8638
8639 constructor_index
8640 = convert (bitsizetype,
8641 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8642 }
8643 else
8644 {
8645 constructor_index = bitsize_zero_node;
8646 constructor_max_index = NULL_TREE;
8647 }
8648
8649 constructor_unfilled_index = constructor_index;
8650 }
8651 else if (gnu_vector_type_p (constructor_type))
8652 {
8653 /* Vectors are like simple fixed-size arrays. */
8654 constructor_max_index =
8655 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8656 constructor_index = bitsize_zero_node;
8657 constructor_unfilled_index = constructor_index;
8658 }
8659 else
8660 {
8661 /* Handle the case of int x = {5}; */
8662 constructor_fields = constructor_type;
8663 constructor_unfilled_fields = constructor_type;
8664 }
8665 }
8666 \f
8667 extern location_t last_init_list_comma;
8668
8669 /* Called when we see an open brace for a nested initializer. Finish
8670 off any pending levels with implicit braces. */
8671 void
8672 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8673 {
8674 while (constructor_stack->implicit)
8675 {
8676 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8677 && constructor_fields == NULL_TREE)
8678 process_init_element (input_location,
8679 pop_init_level (loc, 1, braced_init_obstack,
8680 last_init_list_comma),
8681 true, braced_init_obstack);
8682 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8683 && constructor_max_index
8684 && tree_int_cst_lt (constructor_max_index,
8685 constructor_index))
8686 process_init_element (input_location,
8687 pop_init_level (loc, 1, braced_init_obstack,
8688 last_init_list_comma),
8689 true, braced_init_obstack);
8690 else
8691 break;
8692 }
8693 }
8694
8695 /* Push down into a subobject, for initialization.
8696 If this is for an explicit set of braces, IMPLICIT is 0.
8697 If it is because the next element belongs at a lower level,
8698 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8699
8700 void
8701 push_init_level (location_t loc, int implicit,
8702 struct obstack *braced_init_obstack)
8703 {
8704 struct constructor_stack *p;
8705 tree value = NULL_TREE;
8706
8707 /* Unless this is an explicit brace, we need to preserve previous
8708 content if any. */
8709 if (implicit)
8710 {
8711 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8712 value = find_init_member (constructor_fields, braced_init_obstack);
8713 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8714 value = find_init_member (constructor_index, braced_init_obstack);
8715 }
8716
8717 p = XNEW (struct constructor_stack);
8718 p->type = constructor_type;
8719 p->fields = constructor_fields;
8720 p->index = constructor_index;
8721 p->max_index = constructor_max_index;
8722 p->unfilled_index = constructor_unfilled_index;
8723 p->unfilled_fields = constructor_unfilled_fields;
8724 p->bit_index = constructor_bit_index;
8725 p->elements = constructor_elements;
8726 p->constant = constructor_constant;
8727 p->simple = constructor_simple;
8728 p->nonconst = constructor_nonconst;
8729 p->erroneous = constructor_erroneous;
8730 p->pending_elts = constructor_pending_elts;
8731 p->depth = constructor_depth;
8732 p->replacement_value.value = NULL_TREE;
8733 p->replacement_value.original_code = ERROR_MARK;
8734 p->replacement_value.original_type = NULL;
8735 p->implicit = implicit;
8736 p->outer = 0;
8737 p->incremental = constructor_incremental;
8738 p->designated = constructor_designated;
8739 p->designator_depth = designator_depth;
8740 p->next = constructor_stack;
8741 p->range_stack = 0;
8742 constructor_stack = p;
8743
8744 constructor_constant = 1;
8745 constructor_simple = 1;
8746 constructor_nonconst = 0;
8747 constructor_depth = SPELLING_DEPTH ();
8748 constructor_elements = NULL;
8749 constructor_incremental = 1;
8750 /* If the upper initializer is designated, then mark this as
8751 designated too to prevent bogus warnings. */
8752 constructor_designated = p->designated;
8753 constructor_pending_elts = 0;
8754 if (!implicit)
8755 {
8756 p->range_stack = constructor_range_stack;
8757 constructor_range_stack = 0;
8758 designator_depth = 0;
8759 designator_erroneous = 0;
8760 }
8761
8762 /* Don't die if an entire brace-pair level is superfluous
8763 in the containing level. */
8764 if (constructor_type == NULL_TREE)
8765 ;
8766 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8767 {
8768 /* Don't die if there are extra init elts at the end. */
8769 if (constructor_fields == NULL_TREE)
8770 constructor_type = NULL_TREE;
8771 else
8772 {
8773 constructor_type = TREE_TYPE (constructor_fields);
8774 push_member_name (constructor_fields);
8775 constructor_depth++;
8776 }
8777 }
8778 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8779 {
8780 constructor_type = TREE_TYPE (constructor_type);
8781 push_array_bounds (tree_to_uhwi (constructor_index));
8782 constructor_depth++;
8783 }
8784
8785 if (constructor_type == NULL_TREE)
8786 {
8787 error_init (loc, "extra brace group at end of initializer");
8788 constructor_fields = NULL_TREE;
8789 constructor_unfilled_fields = NULL_TREE;
8790 return;
8791 }
8792
8793 if (value && TREE_CODE (value) == CONSTRUCTOR)
8794 {
8795 constructor_constant = TREE_CONSTANT (value);
8796 constructor_simple = TREE_STATIC (value);
8797 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8798 constructor_elements = CONSTRUCTOR_ELTS (value);
8799 if (!vec_safe_is_empty (constructor_elements)
8800 && (TREE_CODE (constructor_type) == RECORD_TYPE
8801 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8802 set_nonincremental_init (braced_init_obstack);
8803 }
8804
8805 if (implicit == 1)
8806 {
8807 found_missing_braces = 1;
8808 if (initializer_stack->missing_brace_richloc)
8809 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8810 (loc, "{");
8811 }
8812
8813 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8814 {
8815 constructor_fields = TYPE_FIELDS (constructor_type);
8816 /* Skip any nameless bit fields at the beginning. */
8817 while (constructor_fields != NULL_TREE
8818 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8819 constructor_fields = DECL_CHAIN (constructor_fields);
8820
8821 constructor_unfilled_fields = constructor_fields;
8822 constructor_bit_index = bitsize_zero_node;
8823 }
8824 else if (gnu_vector_type_p (constructor_type))
8825 {
8826 /* Vectors are like simple fixed-size arrays. */
8827 constructor_max_index =
8828 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8829 constructor_index = bitsize_int (0);
8830 constructor_unfilled_index = constructor_index;
8831 }
8832 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8833 {
8834 if (TYPE_DOMAIN (constructor_type))
8835 {
8836 constructor_max_index
8837 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8838
8839 /* Detect non-empty initializations of zero-length arrays. */
8840 if (constructor_max_index == NULL_TREE
8841 && TYPE_SIZE (constructor_type))
8842 constructor_max_index = integer_minus_one_node;
8843
8844 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8845 to initialize VLAs will cause a proper error; avoid tree
8846 checking errors as well by setting a safe value. */
8847 if (constructor_max_index
8848 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8849 constructor_max_index = integer_minus_one_node;
8850
8851 constructor_index
8852 = convert (bitsizetype,
8853 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8854 }
8855 else
8856 constructor_index = bitsize_zero_node;
8857
8858 constructor_unfilled_index = constructor_index;
8859 if (value && TREE_CODE (value) == STRING_CST)
8860 {
8861 /* We need to split the char/wchar array into individual
8862 characters, so that we don't have to special case it
8863 everywhere. */
8864 set_nonincremental_init_from_string (value, braced_init_obstack);
8865 }
8866 }
8867 else
8868 {
8869 if (constructor_type != error_mark_node)
8870 warning_init (input_location, 0, "braces around scalar initializer");
8871 constructor_fields = constructor_type;
8872 constructor_unfilled_fields = constructor_type;
8873 }
8874 }
8875
8876 /* At the end of an implicit or explicit brace level,
8877 finish up that level of constructor. If a single expression
8878 with redundant braces initialized that level, return the
8879 c_expr structure for that expression. Otherwise, the original_code
8880 element is set to ERROR_MARK.
8881 If we were outputting the elements as they are read, return 0 as the value
8882 from inner levels (process_init_element ignores that),
8883 but return error_mark_node as the value from the outermost level
8884 (that's what we want to put in DECL_INITIAL).
8885 Otherwise, return a CONSTRUCTOR expression as the value. */
8886
8887 struct c_expr
8888 pop_init_level (location_t loc, int implicit,
8889 struct obstack *braced_init_obstack,
8890 location_t insert_before)
8891 {
8892 struct constructor_stack *p;
8893 struct c_expr ret;
8894 ret.value = NULL_TREE;
8895 ret.original_code = ERROR_MARK;
8896 ret.original_type = NULL;
8897
8898 if (implicit == 0)
8899 {
8900 /* When we come to an explicit close brace,
8901 pop any inner levels that didn't have explicit braces. */
8902 while (constructor_stack->implicit)
8903 process_init_element (input_location,
8904 pop_init_level (loc, 1, braced_init_obstack,
8905 insert_before),
8906 true, braced_init_obstack);
8907 gcc_assert (!constructor_range_stack);
8908 }
8909 else
8910 if (initializer_stack->missing_brace_richloc)
8911 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8912 (insert_before, "}");
8913
8914 /* Now output all pending elements. */
8915 constructor_incremental = 1;
8916 output_pending_init_elements (1, braced_init_obstack);
8917
8918 p = constructor_stack;
8919
8920 /* Error for initializing a flexible array member, or a zero-length
8921 array member in an inappropriate context. */
8922 if (constructor_type && constructor_fields
8923 && TREE_CODE (constructor_type) == ARRAY_TYPE
8924 && TYPE_DOMAIN (constructor_type)
8925 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8926 {
8927 /* Silently discard empty initializations. The parser will
8928 already have pedwarned for empty brackets. */
8929 if (integer_zerop (constructor_unfilled_index))
8930 constructor_type = NULL_TREE;
8931 else
8932 {
8933 gcc_assert (!TYPE_SIZE (constructor_type));
8934
8935 if (constructor_depth > 2)
8936 error_init (loc, "initialization of flexible array member in a nested context");
8937 else
8938 pedwarn_init (loc, OPT_Wpedantic,
8939 "initialization of a flexible array member");
8940
8941 /* We have already issued an error message for the existence
8942 of a flexible array member not at the end of the structure.
8943 Discard the initializer so that we do not die later. */
8944 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8945 constructor_type = NULL_TREE;
8946 }
8947 }
8948
8949 switch (vec_safe_length (constructor_elements))
8950 {
8951 case 0:
8952 /* Initialization with { } counts as zeroinit. */
8953 constructor_zeroinit = 1;
8954 break;
8955 case 1:
8956 /* This might be zeroinit as well. */
8957 if (integer_zerop ((*constructor_elements)[0].value))
8958 constructor_zeroinit = 1;
8959 break;
8960 default:
8961 /* If the constructor has more than one element, it can't be { 0 }. */
8962 constructor_zeroinit = 0;
8963 break;
8964 }
8965
8966 /* Warn when some structs are initialized with direct aggregation. */
8967 if (!implicit && found_missing_braces && warn_missing_braces
8968 && !constructor_zeroinit)
8969 {
8970 gcc_assert (initializer_stack->missing_brace_richloc);
8971 warning_at (initializer_stack->missing_brace_richloc,
8972 OPT_Wmissing_braces,
8973 "missing braces around initializer");
8974 }
8975
8976 /* Warn when some struct elements are implicitly initialized to zero. */
8977 if (warn_missing_field_initializers
8978 && constructor_type
8979 && TREE_CODE (constructor_type) == RECORD_TYPE
8980 && constructor_unfilled_fields)
8981 {
8982 /* Do not warn for flexible array members or zero-length arrays. */
8983 while (constructor_unfilled_fields
8984 && (!DECL_SIZE (constructor_unfilled_fields)
8985 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8986 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8987
8988 if (constructor_unfilled_fields
8989 /* Do not warn if this level of the initializer uses member
8990 designators; it is likely to be deliberate. */
8991 && !constructor_designated
8992 /* Do not warn about initializing with { 0 } or with { }. */
8993 && !constructor_zeroinit)
8994 {
8995 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8996 "missing initializer for field %qD of %qT",
8997 constructor_unfilled_fields,
8998 constructor_type))
8999 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9000 "%qD declared here", constructor_unfilled_fields);
9001 }
9002 }
9003
9004 /* Pad out the end of the structure. */
9005 if (p->replacement_value.value)
9006 /* If this closes a superfluous brace pair,
9007 just pass out the element between them. */
9008 ret = p->replacement_value;
9009 else if (constructor_type == NULL_TREE)
9010 ;
9011 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9012 && TREE_CODE (constructor_type) != ARRAY_TYPE
9013 && !gnu_vector_type_p (constructor_type))
9014 {
9015 /* A nonincremental scalar initializer--just return
9016 the element, after verifying there is just one. */
9017 if (vec_safe_is_empty (constructor_elements))
9018 {
9019 if (!constructor_erroneous && constructor_type != error_mark_node)
9020 error_init (loc, "empty scalar initializer");
9021 ret.value = error_mark_node;
9022 }
9023 else if (vec_safe_length (constructor_elements) != 1)
9024 {
9025 error_init (loc, "extra elements in scalar initializer");
9026 ret.value = (*constructor_elements)[0].value;
9027 }
9028 else
9029 ret.value = (*constructor_elements)[0].value;
9030 }
9031 else
9032 {
9033 if (constructor_erroneous)
9034 ret.value = error_mark_node;
9035 else
9036 {
9037 ret.value = build_constructor (constructor_type,
9038 constructor_elements);
9039 if (constructor_constant)
9040 TREE_CONSTANT (ret.value) = 1;
9041 if (constructor_constant && constructor_simple)
9042 TREE_STATIC (ret.value) = 1;
9043 if (constructor_nonconst)
9044 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9045 }
9046 }
9047
9048 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9049 {
9050 if (constructor_nonconst)
9051 ret.original_code = C_MAYBE_CONST_EXPR;
9052 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9053 ret.original_code = ERROR_MARK;
9054 }
9055
9056 constructor_type = p->type;
9057 constructor_fields = p->fields;
9058 constructor_index = p->index;
9059 constructor_max_index = p->max_index;
9060 constructor_unfilled_index = p->unfilled_index;
9061 constructor_unfilled_fields = p->unfilled_fields;
9062 constructor_bit_index = p->bit_index;
9063 constructor_elements = p->elements;
9064 constructor_constant = p->constant;
9065 constructor_simple = p->simple;
9066 constructor_nonconst = p->nonconst;
9067 constructor_erroneous = p->erroneous;
9068 constructor_incremental = p->incremental;
9069 constructor_designated = p->designated;
9070 designator_depth = p->designator_depth;
9071 constructor_pending_elts = p->pending_elts;
9072 constructor_depth = p->depth;
9073 if (!p->implicit)
9074 constructor_range_stack = p->range_stack;
9075 RESTORE_SPELLING_DEPTH (constructor_depth);
9076
9077 constructor_stack = p->next;
9078 XDELETE (p);
9079
9080 if (ret.value == NULL_TREE && constructor_stack == 0)
9081 ret.value = error_mark_node;
9082 return ret;
9083 }
9084
9085 /* Common handling for both array range and field name designators.
9086 ARRAY argument is nonzero for array ranges. Returns false for success. */
9087
9088 static bool
9089 set_designator (location_t loc, bool array,
9090 struct obstack *braced_init_obstack)
9091 {
9092 tree subtype;
9093 enum tree_code subcode;
9094
9095 /* Don't die if an entire brace-pair level is superfluous
9096 in the containing level, or for an erroneous type. */
9097 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9098 return true;
9099
9100 /* If there were errors in this designator list already, bail out
9101 silently. */
9102 if (designator_erroneous)
9103 return true;
9104
9105 /* Likewise for an initializer for a variable-size type. Those are
9106 diagnosed in digest_init. */
9107 if (COMPLETE_TYPE_P (constructor_type)
9108 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9109 return true;
9110
9111 if (!designator_depth)
9112 {
9113 gcc_assert (!constructor_range_stack);
9114
9115 /* Designator list starts at the level of closest explicit
9116 braces. */
9117 while (constructor_stack->implicit)
9118 process_init_element (input_location,
9119 pop_init_level (loc, 1, braced_init_obstack,
9120 last_init_list_comma),
9121 true, braced_init_obstack);
9122 constructor_designated = 1;
9123 return false;
9124 }
9125
9126 switch (TREE_CODE (constructor_type))
9127 {
9128 case RECORD_TYPE:
9129 case UNION_TYPE:
9130 subtype = TREE_TYPE (constructor_fields);
9131 if (subtype != error_mark_node)
9132 subtype = TYPE_MAIN_VARIANT (subtype);
9133 break;
9134 case ARRAY_TYPE:
9135 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9136 break;
9137 default:
9138 gcc_unreachable ();
9139 }
9140
9141 subcode = TREE_CODE (subtype);
9142 if (array && subcode != ARRAY_TYPE)
9143 {
9144 error_init (loc, "array index in non-array initializer");
9145 return true;
9146 }
9147 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9148 {
9149 error_init (loc, "field name not in record or union initializer");
9150 return true;
9151 }
9152
9153 constructor_designated = 1;
9154 finish_implicit_inits (loc, braced_init_obstack);
9155 push_init_level (loc, 2, braced_init_obstack);
9156 return false;
9157 }
9158
9159 /* If there are range designators in designator list, push a new designator
9160 to constructor_range_stack. RANGE_END is end of such stack range or
9161 NULL_TREE if there is no range designator at this level. */
9162
9163 static void
9164 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9165 {
9166 struct constructor_range_stack *p;
9167
9168 p = (struct constructor_range_stack *)
9169 obstack_alloc (braced_init_obstack,
9170 sizeof (struct constructor_range_stack));
9171 p->prev = constructor_range_stack;
9172 p->next = 0;
9173 p->fields = constructor_fields;
9174 p->range_start = constructor_index;
9175 p->index = constructor_index;
9176 p->stack = constructor_stack;
9177 p->range_end = range_end;
9178 if (constructor_range_stack)
9179 constructor_range_stack->next = p;
9180 constructor_range_stack = p;
9181 }
9182
9183 /* Within an array initializer, specify the next index to be initialized.
9184 FIRST is that index. If LAST is nonzero, then initialize a range
9185 of indices, running from FIRST through LAST. */
9186
9187 void
9188 set_init_index (location_t loc, tree first, tree last,
9189 struct obstack *braced_init_obstack)
9190 {
9191 if (set_designator (loc, true, braced_init_obstack))
9192 return;
9193
9194 designator_erroneous = 1;
9195
9196 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9197 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9198 {
9199 error_init (loc, "array index in initializer not of integer type");
9200 return;
9201 }
9202
9203 if (TREE_CODE (first) != INTEGER_CST)
9204 {
9205 first = c_fully_fold (first, false, NULL);
9206 if (TREE_CODE (first) == INTEGER_CST)
9207 pedwarn_init (loc, OPT_Wpedantic,
9208 "array index in initializer is not "
9209 "an integer constant expression");
9210 }
9211
9212 if (last && TREE_CODE (last) != INTEGER_CST)
9213 {
9214 last = c_fully_fold (last, false, NULL);
9215 if (TREE_CODE (last) == INTEGER_CST)
9216 pedwarn_init (loc, OPT_Wpedantic,
9217 "array index in initializer is not "
9218 "an integer constant expression");
9219 }
9220
9221 if (TREE_CODE (first) != INTEGER_CST)
9222 error_init (loc, "nonconstant array index in initializer");
9223 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9224 error_init (loc, "nonconstant array index in initializer");
9225 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9226 error_init (loc, "array index in non-array initializer");
9227 else if (tree_int_cst_sgn (first) == -1)
9228 error_init (loc, "array index in initializer exceeds array bounds");
9229 else if (constructor_max_index
9230 && tree_int_cst_lt (constructor_max_index, first))
9231 error_init (loc, "array index in initializer exceeds array bounds");
9232 else
9233 {
9234 constant_expression_warning (first);
9235 if (last)
9236 constant_expression_warning (last);
9237 constructor_index = convert (bitsizetype, first);
9238 if (tree_int_cst_lt (constructor_index, first))
9239 {
9240 constructor_index = copy_node (constructor_index);
9241 TREE_OVERFLOW (constructor_index) = 1;
9242 }
9243
9244 if (last)
9245 {
9246 if (tree_int_cst_equal (first, last))
9247 last = NULL_TREE;
9248 else if (tree_int_cst_lt (last, first))
9249 {
9250 error_init (loc, "empty index range in initializer");
9251 last = NULL_TREE;
9252 }
9253 else
9254 {
9255 last = convert (bitsizetype, last);
9256 if (constructor_max_index != NULL_TREE
9257 && tree_int_cst_lt (constructor_max_index, last))
9258 {
9259 error_init (loc, "array index range in initializer exceeds "
9260 "array bounds");
9261 last = NULL_TREE;
9262 }
9263 }
9264 }
9265
9266 designator_depth++;
9267 designator_erroneous = 0;
9268 if (constructor_range_stack || last)
9269 push_range_stack (last, braced_init_obstack);
9270 }
9271 }
9272
9273 /* Within a struct initializer, specify the next field to be initialized. */
9274
9275 void
9276 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9277 struct obstack *braced_init_obstack)
9278 {
9279 tree field;
9280
9281 if (set_designator (loc, false, braced_init_obstack))
9282 return;
9283
9284 designator_erroneous = 1;
9285
9286 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9287 {
9288 error_init (loc, "field name not in record or union initializer");
9289 return;
9290 }
9291
9292 field = lookup_field (constructor_type, fieldname);
9293
9294 if (field == NULL_TREE)
9295 {
9296 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9297 if (guessed_id)
9298 {
9299 gcc_rich_location rich_loc (fieldname_loc);
9300 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9301 error_at (&rich_loc,
9302 "%qT has no member named %qE; did you mean %qE?",
9303 constructor_type, fieldname, guessed_id);
9304 }
9305 else
9306 error_at (fieldname_loc, "%qT has no member named %qE",
9307 constructor_type, fieldname);
9308 }
9309 else
9310 do
9311 {
9312 constructor_fields = TREE_VALUE (field);
9313 designator_depth++;
9314 designator_erroneous = 0;
9315 if (constructor_range_stack)
9316 push_range_stack (NULL_TREE, braced_init_obstack);
9317 field = TREE_CHAIN (field);
9318 if (field)
9319 {
9320 if (set_designator (loc, false, braced_init_obstack))
9321 return;
9322 }
9323 }
9324 while (field != NULL_TREE);
9325 }
9326 \f
9327 /* Add a new initializer to the tree of pending initializers. PURPOSE
9328 identifies the initializer, either array index or field in a structure.
9329 VALUE is the value of that index or field. If ORIGTYPE is not
9330 NULL_TREE, it is the original type of VALUE.
9331
9332 IMPLICIT is true if value comes from pop_init_level (1),
9333 the new initializer has been merged with the existing one
9334 and thus no warnings should be emitted about overriding an
9335 existing initializer. */
9336
9337 static void
9338 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9339 bool implicit, struct obstack *braced_init_obstack)
9340 {
9341 struct init_node *p, **q, *r;
9342
9343 q = &constructor_pending_elts;
9344 p = 0;
9345
9346 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9347 {
9348 while (*q != 0)
9349 {
9350 p = *q;
9351 if (tree_int_cst_lt (purpose, p->purpose))
9352 q = &p->left;
9353 else if (tree_int_cst_lt (p->purpose, purpose))
9354 q = &p->right;
9355 else
9356 {
9357 if (!implicit)
9358 {
9359 if (TREE_SIDE_EFFECTS (p->value))
9360 warning_init (loc, OPT_Woverride_init_side_effects,
9361 "initialized field with side-effects "
9362 "overwritten");
9363 else if (warn_override_init)
9364 warning_init (loc, OPT_Woverride_init,
9365 "initialized field overwritten");
9366 }
9367 p->value = value;
9368 p->origtype = origtype;
9369 return;
9370 }
9371 }
9372 }
9373 else
9374 {
9375 tree bitpos;
9376
9377 bitpos = bit_position (purpose);
9378 while (*q != NULL)
9379 {
9380 p = *q;
9381 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9382 q = &p->left;
9383 else if (p->purpose != purpose)
9384 q = &p->right;
9385 else
9386 {
9387 if (!implicit)
9388 {
9389 if (TREE_SIDE_EFFECTS (p->value))
9390 warning_init (loc, OPT_Woverride_init_side_effects,
9391 "initialized field with side-effects "
9392 "overwritten");
9393 else if (warn_override_init)
9394 warning_init (loc, OPT_Woverride_init,
9395 "initialized field overwritten");
9396 }
9397 p->value = value;
9398 p->origtype = origtype;
9399 return;
9400 }
9401 }
9402 }
9403
9404 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9405 sizeof (struct init_node));
9406 r->purpose = purpose;
9407 r->value = value;
9408 r->origtype = origtype;
9409
9410 *q = r;
9411 r->parent = p;
9412 r->left = 0;
9413 r->right = 0;
9414 r->balance = 0;
9415
9416 while (p)
9417 {
9418 struct init_node *s;
9419
9420 if (r == p->left)
9421 {
9422 if (p->balance == 0)
9423 p->balance = -1;
9424 else if (p->balance < 0)
9425 {
9426 if (r->balance < 0)
9427 {
9428 /* L rotation. */
9429 p->left = r->right;
9430 if (p->left)
9431 p->left->parent = p;
9432 r->right = p;
9433
9434 p->balance = 0;
9435 r->balance = 0;
9436
9437 s = p->parent;
9438 p->parent = r;
9439 r->parent = s;
9440 if (s)
9441 {
9442 if (s->left == p)
9443 s->left = r;
9444 else
9445 s->right = r;
9446 }
9447 else
9448 constructor_pending_elts = r;
9449 }
9450 else
9451 {
9452 /* LR rotation. */
9453 struct init_node *t = r->right;
9454
9455 r->right = t->left;
9456 if (r->right)
9457 r->right->parent = r;
9458 t->left = r;
9459
9460 p->left = t->right;
9461 if (p->left)
9462 p->left->parent = p;
9463 t->right = p;
9464
9465 p->balance = t->balance < 0;
9466 r->balance = -(t->balance > 0);
9467 t->balance = 0;
9468
9469 s = p->parent;
9470 p->parent = t;
9471 r->parent = t;
9472 t->parent = s;
9473 if (s)
9474 {
9475 if (s->left == p)
9476 s->left = t;
9477 else
9478 s->right = t;
9479 }
9480 else
9481 constructor_pending_elts = t;
9482 }
9483 break;
9484 }
9485 else
9486 {
9487 /* p->balance == +1; growth of left side balances the node. */
9488 p->balance = 0;
9489 break;
9490 }
9491 }
9492 else /* r == p->right */
9493 {
9494 if (p->balance == 0)
9495 /* Growth propagation from right side. */
9496 p->balance++;
9497 else if (p->balance > 0)
9498 {
9499 if (r->balance > 0)
9500 {
9501 /* R rotation. */
9502 p->right = r->left;
9503 if (p->right)
9504 p->right->parent = p;
9505 r->left = p;
9506
9507 p->balance = 0;
9508 r->balance = 0;
9509
9510 s = p->parent;
9511 p->parent = r;
9512 r->parent = s;
9513 if (s)
9514 {
9515 if (s->left == p)
9516 s->left = r;
9517 else
9518 s->right = r;
9519 }
9520 else
9521 constructor_pending_elts = r;
9522 }
9523 else /* r->balance == -1 */
9524 {
9525 /* RL rotation */
9526 struct init_node *t = r->left;
9527
9528 r->left = t->right;
9529 if (r->left)
9530 r->left->parent = r;
9531 t->right = r;
9532
9533 p->right = t->left;
9534 if (p->right)
9535 p->right->parent = p;
9536 t->left = p;
9537
9538 r->balance = (t->balance < 0);
9539 p->balance = -(t->balance > 0);
9540 t->balance = 0;
9541
9542 s = p->parent;
9543 p->parent = t;
9544 r->parent = t;
9545 t->parent = s;
9546 if (s)
9547 {
9548 if (s->left == p)
9549 s->left = t;
9550 else
9551 s->right = t;
9552 }
9553 else
9554 constructor_pending_elts = t;
9555 }
9556 break;
9557 }
9558 else
9559 {
9560 /* p->balance == -1; growth of right side balances the node. */
9561 p->balance = 0;
9562 break;
9563 }
9564 }
9565
9566 r = p;
9567 p = p->parent;
9568 }
9569 }
9570
9571 /* Build AVL tree from a sorted chain. */
9572
9573 static void
9574 set_nonincremental_init (struct obstack * braced_init_obstack)
9575 {
9576 unsigned HOST_WIDE_INT ix;
9577 tree index, value;
9578
9579 if (TREE_CODE (constructor_type) != RECORD_TYPE
9580 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9581 return;
9582
9583 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9584 add_pending_init (input_location, index, value, NULL_TREE, true,
9585 braced_init_obstack);
9586 constructor_elements = NULL;
9587 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9588 {
9589 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9590 /* Skip any nameless bit fields at the beginning. */
9591 while (constructor_unfilled_fields != NULL_TREE
9592 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9593 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9594
9595 }
9596 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9597 {
9598 if (TYPE_DOMAIN (constructor_type))
9599 constructor_unfilled_index
9600 = convert (bitsizetype,
9601 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9602 else
9603 constructor_unfilled_index = bitsize_zero_node;
9604 }
9605 constructor_incremental = 0;
9606 }
9607
9608 /* Build AVL tree from a string constant. */
9609
9610 static void
9611 set_nonincremental_init_from_string (tree str,
9612 struct obstack * braced_init_obstack)
9613 {
9614 tree value, purpose, type;
9615 HOST_WIDE_INT val[2];
9616 const char *p, *end;
9617 int byte, wchar_bytes, charwidth, bitpos;
9618
9619 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9620
9621 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9622 charwidth = TYPE_PRECISION (char_type_node);
9623 gcc_assert ((size_t) wchar_bytes * charwidth
9624 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9625 type = TREE_TYPE (constructor_type);
9626 p = TREE_STRING_POINTER (str);
9627 end = p + TREE_STRING_LENGTH (str);
9628
9629 for (purpose = bitsize_zero_node;
9630 p < end
9631 && !(constructor_max_index
9632 && tree_int_cst_lt (constructor_max_index, purpose));
9633 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9634 {
9635 if (wchar_bytes == 1)
9636 {
9637 val[0] = (unsigned char) *p++;
9638 val[1] = 0;
9639 }
9640 else
9641 {
9642 val[1] = 0;
9643 val[0] = 0;
9644 for (byte = 0; byte < wchar_bytes; byte++)
9645 {
9646 if (BYTES_BIG_ENDIAN)
9647 bitpos = (wchar_bytes - byte - 1) * charwidth;
9648 else
9649 bitpos = byte * charwidth;
9650 val[bitpos / HOST_BITS_PER_WIDE_INT]
9651 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9652 << (bitpos % HOST_BITS_PER_WIDE_INT);
9653 }
9654 }
9655
9656 if (!TYPE_UNSIGNED (type))
9657 {
9658 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9659 if (bitpos < HOST_BITS_PER_WIDE_INT)
9660 {
9661 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9662 {
9663 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9664 val[1] = -1;
9665 }
9666 }
9667 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9668 {
9669 if (val[0] < 0)
9670 val[1] = -1;
9671 }
9672 else if (val[1] & (HOST_WIDE_INT_1
9673 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9674 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9675 }
9676
9677 value = wide_int_to_tree (type,
9678 wide_int::from_array (val, 2,
9679 HOST_BITS_PER_WIDE_INT * 2));
9680 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9681 braced_init_obstack);
9682 }
9683
9684 constructor_incremental = 0;
9685 }
9686
9687 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9688 not initialized yet. */
9689
9690 static tree
9691 find_init_member (tree field, struct obstack * braced_init_obstack)
9692 {
9693 struct init_node *p;
9694
9695 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9696 {
9697 if (constructor_incremental
9698 && tree_int_cst_lt (field, constructor_unfilled_index))
9699 set_nonincremental_init (braced_init_obstack);
9700
9701 p = constructor_pending_elts;
9702 while (p)
9703 {
9704 if (tree_int_cst_lt (field, p->purpose))
9705 p = p->left;
9706 else if (tree_int_cst_lt (p->purpose, field))
9707 p = p->right;
9708 else
9709 return p->value;
9710 }
9711 }
9712 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9713 {
9714 tree bitpos = bit_position (field);
9715
9716 if (constructor_incremental
9717 && (!constructor_unfilled_fields
9718 || tree_int_cst_lt (bitpos,
9719 bit_position (constructor_unfilled_fields))))
9720 set_nonincremental_init (braced_init_obstack);
9721
9722 p = constructor_pending_elts;
9723 while (p)
9724 {
9725 if (field == p->purpose)
9726 return p->value;
9727 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9728 p = p->left;
9729 else
9730 p = p->right;
9731 }
9732 }
9733 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9734 {
9735 if (!vec_safe_is_empty (constructor_elements)
9736 && (constructor_elements->last ().index == field))
9737 return constructor_elements->last ().value;
9738 }
9739 return NULL_TREE;
9740 }
9741
9742 /* "Output" the next constructor element.
9743 At top level, really output it to assembler code now.
9744 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9745 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9746 TYPE is the data type that the containing data type wants here.
9747 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9748 If VALUE is a string constant, STRICT_STRING is true if it is
9749 unparenthesized or we should not warn here for it being parenthesized.
9750 For other types of VALUE, STRICT_STRING is not used.
9751
9752 PENDING if true means output pending elements that belong
9753 right after this element. (PENDING is normally true;
9754 it is false while outputting pending elements, to avoid recursion.)
9755
9756 IMPLICIT is true if value comes from pop_init_level (1),
9757 the new initializer has been merged with the existing one
9758 and thus no warnings should be emitted about overriding an
9759 existing initializer. */
9760
9761 static void
9762 output_init_element (location_t loc, tree value, tree origtype,
9763 bool strict_string, tree type, tree field, bool pending,
9764 bool implicit, struct obstack * braced_init_obstack)
9765 {
9766 tree semantic_type = NULL_TREE;
9767 bool maybe_const = true;
9768 bool npc;
9769
9770 if (type == error_mark_node || value == error_mark_node)
9771 {
9772 constructor_erroneous = 1;
9773 return;
9774 }
9775 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9776 && (TREE_CODE (value) == STRING_CST
9777 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9778 && !(TREE_CODE (value) == STRING_CST
9779 && TREE_CODE (type) == ARRAY_TYPE
9780 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9781 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9782 TYPE_MAIN_VARIANT (type)))
9783 value = array_to_pointer_conversion (input_location, value);
9784
9785 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9786 && require_constant_value && pending)
9787 {
9788 /* As an extension, allow initializing objects with static storage
9789 duration with compound literals (which are then treated just as
9790 the brace enclosed list they contain). */
9791 if (flag_isoc99)
9792 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9793 "constant");
9794 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9795 value = DECL_INITIAL (decl);
9796 }
9797
9798 npc = null_pointer_constant_p (value);
9799 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9800 {
9801 semantic_type = TREE_TYPE (value);
9802 value = TREE_OPERAND (value, 0);
9803 }
9804 value = c_fully_fold (value, require_constant_value, &maybe_const);
9805
9806 if (value == error_mark_node)
9807 constructor_erroneous = 1;
9808 else if (!TREE_CONSTANT (value))
9809 constructor_constant = 0;
9810 else if (!initializer_constant_valid_p (value,
9811 TREE_TYPE (value),
9812 AGGREGATE_TYPE_P (constructor_type)
9813 && TYPE_REVERSE_STORAGE_ORDER
9814 (constructor_type))
9815 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9816 && DECL_C_BIT_FIELD (field)
9817 && TREE_CODE (value) != INTEGER_CST))
9818 constructor_simple = 0;
9819 if (!maybe_const)
9820 constructor_nonconst = 1;
9821
9822 /* Digest the initializer and issue any errors about incompatible
9823 types before issuing errors about non-constant initializers. */
9824 tree new_value = value;
9825 if (semantic_type)
9826 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9827 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9828 require_constant_value);
9829 if (new_value == error_mark_node)
9830 {
9831 constructor_erroneous = 1;
9832 return;
9833 }
9834 if (require_constant_value || require_constant_elements)
9835 constant_expression_warning (new_value);
9836
9837 /* Proceed to check the constness of the original initializer. */
9838 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9839 {
9840 if (require_constant_value)
9841 {
9842 error_init (loc, "initializer element is not constant");
9843 value = error_mark_node;
9844 }
9845 else if (require_constant_elements)
9846 pedwarn (loc, OPT_Wpedantic,
9847 "initializer element is not computable at load time");
9848 }
9849 else if (!maybe_const
9850 && (require_constant_value || require_constant_elements))
9851 pedwarn_init (loc, OPT_Wpedantic,
9852 "initializer element is not a constant expression");
9853
9854 /* Issue -Wc++-compat warnings about initializing a bitfield with
9855 enum type. */
9856 if (warn_cxx_compat
9857 && field != NULL_TREE
9858 && TREE_CODE (field) == FIELD_DECL
9859 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9860 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9861 != TYPE_MAIN_VARIANT (type))
9862 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9863 {
9864 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9865 if (checktype != error_mark_node
9866 && (TYPE_MAIN_VARIANT (checktype)
9867 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9868 warning_init (loc, OPT_Wc___compat,
9869 "enum conversion in initialization is invalid in C++");
9870 }
9871
9872 /* If this field is empty and does not have side effects (and is not at
9873 the end of structure), don't do anything other than checking the
9874 initializer. */
9875 if (field
9876 && (TREE_TYPE (field) == error_mark_node
9877 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9878 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9879 && !TREE_SIDE_EFFECTS (new_value)
9880 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9881 || DECL_CHAIN (field)))))
9882 return;
9883
9884 /* Finally, set VALUE to the initializer value digested above. */
9885 value = new_value;
9886
9887 /* If this element doesn't come next in sequence,
9888 put it on constructor_pending_elts. */
9889 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9890 && (!constructor_incremental
9891 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9892 {
9893 if (constructor_incremental
9894 && tree_int_cst_lt (field, constructor_unfilled_index))
9895 set_nonincremental_init (braced_init_obstack);
9896
9897 add_pending_init (loc, field, value, origtype, implicit,
9898 braced_init_obstack);
9899 return;
9900 }
9901 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9902 && (!constructor_incremental
9903 || field != constructor_unfilled_fields))
9904 {
9905 /* We do this for records but not for unions. In a union,
9906 no matter which field is specified, it can be initialized
9907 right away since it starts at the beginning of the union. */
9908 if (constructor_incremental)
9909 {
9910 if (!constructor_unfilled_fields)
9911 set_nonincremental_init (braced_init_obstack);
9912 else
9913 {
9914 tree bitpos, unfillpos;
9915
9916 bitpos = bit_position (field);
9917 unfillpos = bit_position (constructor_unfilled_fields);
9918
9919 if (tree_int_cst_lt (bitpos, unfillpos))
9920 set_nonincremental_init (braced_init_obstack);
9921 }
9922 }
9923
9924 add_pending_init (loc, field, value, origtype, implicit,
9925 braced_init_obstack);
9926 return;
9927 }
9928 else if (TREE_CODE (constructor_type) == UNION_TYPE
9929 && !vec_safe_is_empty (constructor_elements))
9930 {
9931 if (!implicit)
9932 {
9933 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9934 warning_init (loc, OPT_Woverride_init_side_effects,
9935 "initialized field with side-effects overwritten");
9936 else if (warn_override_init)
9937 warning_init (loc, OPT_Woverride_init,
9938 "initialized field overwritten");
9939 }
9940
9941 /* We can have just one union field set. */
9942 constructor_elements = NULL;
9943 }
9944
9945 /* Otherwise, output this element either to
9946 constructor_elements or to the assembler file. */
9947
9948 constructor_elt celt = {field, value};
9949 vec_safe_push (constructor_elements, celt);
9950
9951 /* Advance the variable that indicates sequential elements output. */
9952 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9953 constructor_unfilled_index
9954 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9955 bitsize_one_node);
9956 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9957 {
9958 constructor_unfilled_fields
9959 = DECL_CHAIN (constructor_unfilled_fields);
9960
9961 /* Skip any nameless bit fields. */
9962 while (constructor_unfilled_fields != NULL_TREE
9963 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9964 constructor_unfilled_fields =
9965 DECL_CHAIN (constructor_unfilled_fields);
9966 }
9967 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9968 constructor_unfilled_fields = NULL_TREE;
9969
9970 /* Now output any pending elements which have become next. */
9971 if (pending)
9972 output_pending_init_elements (0, braced_init_obstack);
9973 }
9974
9975 /* For two FIELD_DECLs in the same chain, return -1 if field1
9976 comes before field2, 1 if field1 comes after field2 and
9977 0 if field1 == field2. */
9978
9979 static int
9980 init_field_decl_cmp (tree field1, tree field2)
9981 {
9982 if (field1 == field2)
9983 return 0;
9984
9985 tree bitpos1 = bit_position (field1);
9986 tree bitpos2 = bit_position (field2);
9987 if (tree_int_cst_equal (bitpos1, bitpos2))
9988 {
9989 /* If one of the fields has non-zero bitsize, then that
9990 field must be the last one in a sequence of zero
9991 sized fields, fields after it will have bigger
9992 bit_position. */
9993 if (TREE_TYPE (field1) != error_mark_node
9994 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9995 && integer_nonzerop (TREE_TYPE (field1)))
9996 return 1;
9997 if (TREE_TYPE (field2) != error_mark_node
9998 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9999 && integer_nonzerop (TREE_TYPE (field2)))
10000 return -1;
10001 /* Otherwise, fallback to DECL_CHAIN walk to find out
10002 which field comes earlier. Walk chains of both
10003 fields, so that if field1 and field2 are close to each
10004 other in either order, it is found soon even for large
10005 sequences of zero sized fields. */
10006 tree f1 = field1, f2 = field2;
10007 while (1)
10008 {
10009 f1 = DECL_CHAIN (f1);
10010 f2 = DECL_CHAIN (f2);
10011 if (f1 == NULL_TREE)
10012 {
10013 gcc_assert (f2);
10014 return 1;
10015 }
10016 if (f2 == NULL_TREE)
10017 return -1;
10018 if (f1 == field2)
10019 return -1;
10020 if (f2 == field1)
10021 return 1;
10022 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10023 return 1;
10024 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10025 return -1;
10026 }
10027 }
10028 else if (tree_int_cst_lt (bitpos1, bitpos2))
10029 return -1;
10030 else
10031 return 1;
10032 }
10033
10034 /* Output any pending elements which have become next.
10035 As we output elements, constructor_unfilled_{fields,index}
10036 advances, which may cause other elements to become next;
10037 if so, they too are output.
10038
10039 If ALL is 0, we return when there are
10040 no more pending elements to output now.
10041
10042 If ALL is 1, we output space as necessary so that
10043 we can output all the pending elements. */
10044 static void
10045 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10046 {
10047 struct init_node *elt = constructor_pending_elts;
10048 tree next;
10049
10050 retry:
10051
10052 /* Look through the whole pending tree.
10053 If we find an element that should be output now,
10054 output it. Otherwise, set NEXT to the element
10055 that comes first among those still pending. */
10056
10057 next = NULL_TREE;
10058 while (elt)
10059 {
10060 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10061 {
10062 if (tree_int_cst_equal (elt->purpose,
10063 constructor_unfilled_index))
10064 output_init_element (input_location, elt->value, elt->origtype,
10065 true, TREE_TYPE (constructor_type),
10066 constructor_unfilled_index, false, false,
10067 braced_init_obstack);
10068 else if (tree_int_cst_lt (constructor_unfilled_index,
10069 elt->purpose))
10070 {
10071 /* Advance to the next smaller node. */
10072 if (elt->left)
10073 elt = elt->left;
10074 else
10075 {
10076 /* We have reached the smallest node bigger than the
10077 current unfilled index. Fill the space first. */
10078 next = elt->purpose;
10079 break;
10080 }
10081 }
10082 else
10083 {
10084 /* Advance to the next bigger node. */
10085 if (elt->right)
10086 elt = elt->right;
10087 else
10088 {
10089 /* We have reached the biggest node in a subtree. Find
10090 the parent of it, which is the next bigger node. */
10091 while (elt->parent && elt->parent->right == elt)
10092 elt = elt->parent;
10093 elt = elt->parent;
10094 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10095 elt->purpose))
10096 {
10097 next = elt->purpose;
10098 break;
10099 }
10100 }
10101 }
10102 }
10103 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10104 {
10105 /* If the current record is complete we are done. */
10106 if (constructor_unfilled_fields == NULL_TREE)
10107 break;
10108
10109 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10110 elt->purpose);
10111 if (cmp == 0)
10112 output_init_element (input_location, elt->value, elt->origtype,
10113 true, TREE_TYPE (elt->purpose),
10114 elt->purpose, false, false,
10115 braced_init_obstack);
10116 else if (cmp < 0)
10117 {
10118 /* Advance to the next smaller node. */
10119 if (elt->left)
10120 elt = elt->left;
10121 else
10122 {
10123 /* We have reached the smallest node bigger than the
10124 current unfilled field. Fill the space first. */
10125 next = elt->purpose;
10126 break;
10127 }
10128 }
10129 else
10130 {
10131 /* Advance to the next bigger node. */
10132 if (elt->right)
10133 elt = elt->right;
10134 else
10135 {
10136 /* We have reached the biggest node in a subtree. Find
10137 the parent of it, which is the next bigger node. */
10138 while (elt->parent && elt->parent->right == elt)
10139 elt = elt->parent;
10140 elt = elt->parent;
10141 if (elt
10142 && init_field_decl_cmp (constructor_unfilled_fields,
10143 elt->purpose) < 0)
10144 {
10145 next = elt->purpose;
10146 break;
10147 }
10148 }
10149 }
10150 }
10151 }
10152
10153 /* Ordinarily return, but not if we want to output all
10154 and there are elements left. */
10155 if (!(all && next != NULL_TREE))
10156 return;
10157
10158 /* If it's not incremental, just skip over the gap, so that after
10159 jumping to retry we will output the next successive element. */
10160 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10161 constructor_unfilled_fields = next;
10162 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10163 constructor_unfilled_index = next;
10164
10165 /* ELT now points to the node in the pending tree with the next
10166 initializer to output. */
10167 goto retry;
10168 }
10169 \f
10170 /* Expression VALUE coincides with the start of type TYPE in a braced
10171 initializer. Return true if we should treat VALUE as initializing
10172 the first element of TYPE, false if we should treat it as initializing
10173 TYPE as a whole.
10174
10175 If the initializer is clearly invalid, the question becomes:
10176 which choice gives the best error message? */
10177
10178 static bool
10179 initialize_elementwise_p (tree type, tree value)
10180 {
10181 if (type == error_mark_node || value == error_mark_node)
10182 return false;
10183
10184 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10185
10186 tree value_type = TREE_TYPE (value);
10187 if (value_type == error_mark_node)
10188 return false;
10189
10190 /* GNU vectors can be initialized elementwise. However, treat any
10191 kind of vector value as initializing the vector type as a whole,
10192 regardless of whether the value is a GNU vector. Such initializers
10193 are valid if and only if they would have been valid in a non-braced
10194 initializer like:
10195
10196 TYPE foo = VALUE;
10197
10198 so recursing into the vector type would be at best confusing or at
10199 worst wrong. For example, when -flax-vector-conversions is in effect,
10200 it's possible to initialize a V8HI from a V4SI, even though the vectors
10201 have different element types and different numbers of elements. */
10202 if (gnu_vector_type_p (type))
10203 return !VECTOR_TYPE_P (value_type);
10204
10205 if (AGGREGATE_TYPE_P (type))
10206 return type != TYPE_MAIN_VARIANT (value_type);
10207
10208 return false;
10209 }
10210
10211 /* Add one non-braced element to the current constructor level.
10212 This adjusts the current position within the constructor's type.
10213 This may also start or terminate implicit levels
10214 to handle a partly-braced initializer.
10215
10216 Once this has found the correct level for the new element,
10217 it calls output_init_element.
10218
10219 IMPLICIT is true if value comes from pop_init_level (1),
10220 the new initializer has been merged with the existing one
10221 and thus no warnings should be emitted about overriding an
10222 existing initializer. */
10223
10224 void
10225 process_init_element (location_t loc, struct c_expr value, bool implicit,
10226 struct obstack * braced_init_obstack)
10227 {
10228 tree orig_value = value.value;
10229 int string_flag
10230 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10231 bool strict_string = value.original_code == STRING_CST;
10232 bool was_designated = designator_depth != 0;
10233
10234 designator_depth = 0;
10235 designator_erroneous = 0;
10236
10237 if (!implicit && value.value && !integer_zerop (value.value))
10238 constructor_zeroinit = 0;
10239
10240 /* Handle superfluous braces around string cst as in
10241 char x[] = {"foo"}; */
10242 if (string_flag
10243 && constructor_type
10244 && !was_designated
10245 && TREE_CODE (constructor_type) == ARRAY_TYPE
10246 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10247 && integer_zerop (constructor_unfilled_index))
10248 {
10249 if (constructor_stack->replacement_value.value)
10250 error_init (loc, "excess elements in %<char%> array initializer");
10251 constructor_stack->replacement_value = value;
10252 return;
10253 }
10254
10255 if (constructor_stack->replacement_value.value != NULL_TREE)
10256 {
10257 error_init (loc, "excess elements in struct initializer");
10258 return;
10259 }
10260
10261 /* Ignore elements of a brace group if it is entirely superfluous
10262 and has already been diagnosed, or if the type is erroneous. */
10263 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10264 return;
10265
10266 /* Ignore elements of an initializer for a variable-size type.
10267 Those are diagnosed in digest_init. */
10268 if (COMPLETE_TYPE_P (constructor_type)
10269 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10270 return;
10271
10272 if (!implicit && warn_designated_init && !was_designated
10273 && TREE_CODE (constructor_type) == RECORD_TYPE
10274 && lookup_attribute ("designated_init",
10275 TYPE_ATTRIBUTES (constructor_type)))
10276 warning_init (loc,
10277 OPT_Wdesignated_init,
10278 "positional initialization of field "
10279 "in %<struct%> declared with %<designated_init%> attribute");
10280
10281 /* If we've exhausted any levels that didn't have braces,
10282 pop them now. */
10283 while (constructor_stack->implicit)
10284 {
10285 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10286 && constructor_fields == NULL_TREE)
10287 process_init_element (loc,
10288 pop_init_level (loc, 1, braced_init_obstack,
10289 last_init_list_comma),
10290 true, braced_init_obstack);
10291 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10292 || gnu_vector_type_p (constructor_type))
10293 && constructor_max_index
10294 && tree_int_cst_lt (constructor_max_index,
10295 constructor_index))
10296 process_init_element (loc,
10297 pop_init_level (loc, 1, braced_init_obstack,
10298 last_init_list_comma),
10299 true, braced_init_obstack);
10300 else
10301 break;
10302 }
10303
10304 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10305 if (constructor_range_stack)
10306 {
10307 /* If value is a compound literal and we'll be just using its
10308 content, don't put it into a SAVE_EXPR. */
10309 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10310 || !require_constant_value)
10311 {
10312 tree semantic_type = NULL_TREE;
10313 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10314 {
10315 semantic_type = TREE_TYPE (value.value);
10316 value.value = TREE_OPERAND (value.value, 0);
10317 }
10318 value.value = save_expr (value.value);
10319 if (semantic_type)
10320 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10321 value.value);
10322 }
10323 }
10324
10325 while (1)
10326 {
10327 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10328 {
10329 tree fieldtype;
10330 enum tree_code fieldcode;
10331
10332 if (constructor_fields == NULL_TREE)
10333 {
10334 pedwarn_init (loc, 0, "excess elements in struct initializer");
10335 break;
10336 }
10337
10338 fieldtype = TREE_TYPE (constructor_fields);
10339 if (fieldtype != error_mark_node)
10340 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10341 fieldcode = TREE_CODE (fieldtype);
10342
10343 /* Error for non-static initialization of a flexible array member. */
10344 if (fieldcode == ARRAY_TYPE
10345 && !require_constant_value
10346 && TYPE_SIZE (fieldtype) == NULL_TREE
10347 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10348 {
10349 error_init (loc, "non-static initialization of a flexible "
10350 "array member");
10351 break;
10352 }
10353
10354 /* Error for initialization of a flexible array member with
10355 a string constant if the structure is in an array. E.g.:
10356 struct S { int x; char y[]; };
10357 struct S s[] = { { 1, "foo" } };
10358 is invalid. */
10359 if (string_flag
10360 && fieldcode == ARRAY_TYPE
10361 && constructor_depth > 1
10362 && TYPE_SIZE (fieldtype) == NULL_TREE
10363 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10364 {
10365 bool in_array_p = false;
10366 for (struct constructor_stack *p = constructor_stack;
10367 p && p->type; p = p->next)
10368 if (TREE_CODE (p->type) == ARRAY_TYPE)
10369 {
10370 in_array_p = true;
10371 break;
10372 }
10373 if (in_array_p)
10374 {
10375 error_init (loc, "initialization of flexible array "
10376 "member in a nested context");
10377 break;
10378 }
10379 }
10380
10381 /* Accept a string constant to initialize a subarray. */
10382 if (value.value != NULL_TREE
10383 && fieldcode == ARRAY_TYPE
10384 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10385 && string_flag)
10386 value.value = orig_value;
10387 /* Otherwise, if we have come to a subaggregate,
10388 and we don't have an element of its type, push into it. */
10389 else if (value.value != NULL_TREE
10390 && initialize_elementwise_p (fieldtype, value.value))
10391 {
10392 push_init_level (loc, 1, braced_init_obstack);
10393 continue;
10394 }
10395
10396 if (value.value)
10397 {
10398 push_member_name (constructor_fields);
10399 output_init_element (loc, value.value, value.original_type,
10400 strict_string, fieldtype,
10401 constructor_fields, true, implicit,
10402 braced_init_obstack);
10403 RESTORE_SPELLING_DEPTH (constructor_depth);
10404 }
10405 else
10406 /* Do the bookkeeping for an element that was
10407 directly output as a constructor. */
10408 {
10409 /* For a record, keep track of end position of last field. */
10410 if (DECL_SIZE (constructor_fields))
10411 constructor_bit_index
10412 = size_binop_loc (input_location, PLUS_EXPR,
10413 bit_position (constructor_fields),
10414 DECL_SIZE (constructor_fields));
10415
10416 /* If the current field was the first one not yet written out,
10417 it isn't now, so update. */
10418 if (constructor_unfilled_fields == constructor_fields)
10419 {
10420 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10421 /* Skip any nameless bit fields. */
10422 while (constructor_unfilled_fields != 0
10423 && (DECL_UNNAMED_BIT_FIELD
10424 (constructor_unfilled_fields)))
10425 constructor_unfilled_fields =
10426 DECL_CHAIN (constructor_unfilled_fields);
10427 }
10428 }
10429
10430 constructor_fields = DECL_CHAIN (constructor_fields);
10431 /* Skip any nameless bit fields at the beginning. */
10432 while (constructor_fields != NULL_TREE
10433 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10434 constructor_fields = DECL_CHAIN (constructor_fields);
10435 }
10436 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10437 {
10438 tree fieldtype;
10439 enum tree_code fieldcode;
10440
10441 if (constructor_fields == NULL_TREE)
10442 {
10443 pedwarn_init (loc, 0,
10444 "excess elements in union initializer");
10445 break;
10446 }
10447
10448 fieldtype = TREE_TYPE (constructor_fields);
10449 if (fieldtype != error_mark_node)
10450 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10451 fieldcode = TREE_CODE (fieldtype);
10452
10453 /* Warn that traditional C rejects initialization of unions.
10454 We skip the warning if the value is zero. This is done
10455 under the assumption that the zero initializer in user
10456 code appears conditioned on e.g. __STDC__ to avoid
10457 "missing initializer" warnings and relies on default
10458 initialization to zero in the traditional C case.
10459 We also skip the warning if the initializer is designated,
10460 again on the assumption that this must be conditional on
10461 __STDC__ anyway (and we've already complained about the
10462 member-designator already). */
10463 if (!in_system_header_at (input_location) && !constructor_designated
10464 && !(value.value && (integer_zerop (value.value)
10465 || real_zerop (value.value))))
10466 warning (OPT_Wtraditional, "traditional C rejects initialization "
10467 "of unions");
10468
10469 /* Accept a string constant to initialize a subarray. */
10470 if (value.value != NULL_TREE
10471 && fieldcode == ARRAY_TYPE
10472 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10473 && string_flag)
10474 value.value = orig_value;
10475 /* Otherwise, if we have come to a subaggregate,
10476 and we don't have an element of its type, push into it. */
10477 else if (value.value != NULL_TREE
10478 && initialize_elementwise_p (fieldtype, value.value))
10479 {
10480 push_init_level (loc, 1, braced_init_obstack);
10481 continue;
10482 }
10483
10484 if (value.value)
10485 {
10486 push_member_name (constructor_fields);
10487 output_init_element (loc, value.value, value.original_type,
10488 strict_string, fieldtype,
10489 constructor_fields, true, implicit,
10490 braced_init_obstack);
10491 RESTORE_SPELLING_DEPTH (constructor_depth);
10492 }
10493 else
10494 /* Do the bookkeeping for an element that was
10495 directly output as a constructor. */
10496 {
10497 constructor_bit_index = DECL_SIZE (constructor_fields);
10498 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10499 }
10500
10501 constructor_fields = NULL_TREE;
10502 }
10503 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10504 {
10505 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10506 enum tree_code eltcode = TREE_CODE (elttype);
10507
10508 /* Accept a string constant to initialize a subarray. */
10509 if (value.value != NULL_TREE
10510 && eltcode == ARRAY_TYPE
10511 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10512 && string_flag)
10513 value.value = orig_value;
10514 /* Otherwise, if we have come to a subaggregate,
10515 and we don't have an element of its type, push into it. */
10516 else if (value.value != NULL_TREE
10517 && initialize_elementwise_p (elttype, value.value))
10518 {
10519 push_init_level (loc, 1, braced_init_obstack);
10520 continue;
10521 }
10522
10523 if (constructor_max_index != NULL_TREE
10524 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10525 || integer_all_onesp (constructor_max_index)))
10526 {
10527 pedwarn_init (loc, 0,
10528 "excess elements in array initializer");
10529 break;
10530 }
10531
10532 /* Now output the actual element. */
10533 if (value.value)
10534 {
10535 push_array_bounds (tree_to_uhwi (constructor_index));
10536 output_init_element (loc, value.value, value.original_type,
10537 strict_string, elttype,
10538 constructor_index, true, implicit,
10539 braced_init_obstack);
10540 RESTORE_SPELLING_DEPTH (constructor_depth);
10541 }
10542
10543 constructor_index
10544 = size_binop_loc (input_location, PLUS_EXPR,
10545 constructor_index, bitsize_one_node);
10546
10547 if (!value.value)
10548 /* If we are doing the bookkeeping for an element that was
10549 directly output as a constructor, we must update
10550 constructor_unfilled_index. */
10551 constructor_unfilled_index = constructor_index;
10552 }
10553 else if (gnu_vector_type_p (constructor_type))
10554 {
10555 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10556
10557 /* Do a basic check of initializer size. Note that vectors
10558 always have a fixed size derived from their type. */
10559 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10560 {
10561 pedwarn_init (loc, 0,
10562 "excess elements in vector initializer");
10563 break;
10564 }
10565
10566 /* Now output the actual element. */
10567 if (value.value)
10568 {
10569 if (TREE_CODE (value.value) == VECTOR_CST)
10570 elttype = TYPE_MAIN_VARIANT (constructor_type);
10571 output_init_element (loc, value.value, value.original_type,
10572 strict_string, elttype,
10573 constructor_index, true, implicit,
10574 braced_init_obstack);
10575 }
10576
10577 constructor_index
10578 = size_binop_loc (input_location,
10579 PLUS_EXPR, constructor_index, bitsize_one_node);
10580
10581 if (!value.value)
10582 /* If we are doing the bookkeeping for an element that was
10583 directly output as a constructor, we must update
10584 constructor_unfilled_index. */
10585 constructor_unfilled_index = constructor_index;
10586 }
10587
10588 /* Handle the sole element allowed in a braced initializer
10589 for a scalar variable. */
10590 else if (constructor_type != error_mark_node
10591 && constructor_fields == NULL_TREE)
10592 {
10593 pedwarn_init (loc, 0,
10594 "excess elements in scalar initializer");
10595 break;
10596 }
10597 else
10598 {
10599 if (value.value)
10600 output_init_element (loc, value.value, value.original_type,
10601 strict_string, constructor_type,
10602 NULL_TREE, true, implicit,
10603 braced_init_obstack);
10604 constructor_fields = NULL_TREE;
10605 }
10606
10607 /* Handle range initializers either at this level or anywhere higher
10608 in the designator stack. */
10609 if (constructor_range_stack)
10610 {
10611 struct constructor_range_stack *p, *range_stack;
10612 int finish = 0;
10613
10614 range_stack = constructor_range_stack;
10615 constructor_range_stack = 0;
10616 while (constructor_stack != range_stack->stack)
10617 {
10618 gcc_assert (constructor_stack->implicit);
10619 process_init_element (loc,
10620 pop_init_level (loc, 1,
10621 braced_init_obstack,
10622 last_init_list_comma),
10623 true, braced_init_obstack);
10624 }
10625 for (p = range_stack;
10626 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10627 p = p->prev)
10628 {
10629 gcc_assert (constructor_stack->implicit);
10630 process_init_element (loc,
10631 pop_init_level (loc, 1,
10632 braced_init_obstack,
10633 last_init_list_comma),
10634 true, braced_init_obstack);
10635 }
10636
10637 p->index = size_binop_loc (input_location,
10638 PLUS_EXPR, p->index, bitsize_one_node);
10639 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10640 finish = 1;
10641
10642 while (1)
10643 {
10644 constructor_index = p->index;
10645 constructor_fields = p->fields;
10646 if (finish && p->range_end && p->index == p->range_start)
10647 {
10648 finish = 0;
10649 p->prev = 0;
10650 }
10651 p = p->next;
10652 if (!p)
10653 break;
10654 finish_implicit_inits (loc, braced_init_obstack);
10655 push_init_level (loc, 2, braced_init_obstack);
10656 p->stack = constructor_stack;
10657 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10658 p->index = p->range_start;
10659 }
10660
10661 if (!finish)
10662 constructor_range_stack = range_stack;
10663 continue;
10664 }
10665
10666 break;
10667 }
10668
10669 constructor_range_stack = 0;
10670 }
10671 \f
10672 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10673 (guaranteed to be 'volatile' or null) and ARGS (represented using
10674 an ASM_EXPR node). */
10675 tree
10676 build_asm_stmt (bool is_volatile, tree args)
10677 {
10678 if (is_volatile)
10679 ASM_VOLATILE_P (args) = 1;
10680 return add_stmt (args);
10681 }
10682
10683 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10684 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10685 SIMPLE indicates whether there was anything at all after the
10686 string in the asm expression -- asm("blah") and asm("blah" : )
10687 are subtly different. We use a ASM_EXPR node to represent this.
10688 LOC is the location of the asm, and IS_INLINE says whether this
10689 is asm inline. */
10690 tree
10691 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10692 tree clobbers, tree labels, bool simple, bool is_inline)
10693 {
10694 tree tail;
10695 tree args;
10696 int i;
10697 const char *constraint;
10698 const char **oconstraints;
10699 bool allows_mem, allows_reg, is_inout;
10700 int ninputs, noutputs;
10701
10702 ninputs = list_length (inputs);
10703 noutputs = list_length (outputs);
10704 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10705
10706 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10707
10708 /* Remove output conversions that change the type but not the mode. */
10709 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10710 {
10711 tree output = TREE_VALUE (tail);
10712
10713 output = c_fully_fold (output, false, NULL, true);
10714
10715 /* ??? Really, this should not be here. Users should be using a
10716 proper lvalue, dammit. But there's a long history of using casts
10717 in the output operands. In cases like longlong.h, this becomes a
10718 primitive form of typechecking -- if the cast can be removed, then
10719 the output operand had a type of the proper width; otherwise we'll
10720 get an error. Gross, but ... */
10721 STRIP_NOPS (output);
10722
10723 if (!lvalue_or_else (loc, output, lv_asm))
10724 output = error_mark_node;
10725
10726 if (output != error_mark_node
10727 && (TREE_READONLY (output)
10728 || TYPE_READONLY (TREE_TYPE (output))
10729 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10730 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10731 readonly_error (loc, output, lv_asm);
10732
10733 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10734 oconstraints[i] = constraint;
10735
10736 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10737 &allows_mem, &allows_reg, &is_inout))
10738 {
10739 /* If the operand is going to end up in memory,
10740 mark it addressable. */
10741 if (!allows_reg && !c_mark_addressable (output))
10742 output = error_mark_node;
10743 if (!(!allows_reg && allows_mem)
10744 && output != error_mark_node
10745 && VOID_TYPE_P (TREE_TYPE (output)))
10746 {
10747 error_at (loc, "invalid use of void expression");
10748 output = error_mark_node;
10749 }
10750 }
10751 else
10752 output = error_mark_node;
10753
10754 TREE_VALUE (tail) = output;
10755 }
10756
10757 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10758 {
10759 tree input;
10760
10761 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10762 input = TREE_VALUE (tail);
10763
10764 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10765 oconstraints, &allows_mem, &allows_reg))
10766 {
10767 /* If the operand is going to end up in memory,
10768 mark it addressable. */
10769 if (!allows_reg && allows_mem)
10770 {
10771 input = c_fully_fold (input, false, NULL, true);
10772
10773 /* Strip the nops as we allow this case. FIXME, this really
10774 should be rejected or made deprecated. */
10775 STRIP_NOPS (input);
10776 if (!c_mark_addressable (input))
10777 input = error_mark_node;
10778 }
10779 else
10780 {
10781 struct c_expr expr;
10782 memset (&expr, 0, sizeof (expr));
10783 expr.value = input;
10784 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10785 input = c_fully_fold (expr.value, false, NULL);
10786
10787 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10788 {
10789 error_at (loc, "invalid use of void expression");
10790 input = error_mark_node;
10791 }
10792 }
10793 }
10794 else
10795 input = error_mark_node;
10796
10797 TREE_VALUE (tail) = input;
10798 }
10799
10800 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10801
10802 /* asm statements without outputs, including simple ones, are treated
10803 as volatile. */
10804 ASM_INPUT_P (args) = simple;
10805 ASM_VOLATILE_P (args) = (noutputs == 0);
10806 ASM_INLINE_P (args) = is_inline;
10807
10808 return args;
10809 }
10810 \f
10811 /* Generate a goto statement to LABEL. LOC is the location of the
10812 GOTO. */
10813
10814 tree
10815 c_finish_goto_label (location_t loc, tree label)
10816 {
10817 tree decl = lookup_label_for_goto (loc, label);
10818 if (!decl)
10819 return NULL_TREE;
10820 TREE_USED (decl) = 1;
10821 {
10822 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10823 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10824 SET_EXPR_LOCATION (t, loc);
10825 return add_stmt (t);
10826 }
10827 }
10828
10829 /* Generate a computed goto statement to EXPR. LOC is the location of
10830 the GOTO. */
10831
10832 tree
10833 c_finish_goto_ptr (location_t loc, c_expr val)
10834 {
10835 tree expr = val.value;
10836 tree t;
10837 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10838 if (expr != error_mark_node
10839 && !POINTER_TYPE_P (TREE_TYPE (expr))
10840 && !null_pointer_constant_p (expr))
10841 {
10842 error_at (val.get_location (),
10843 "computed goto must be pointer type");
10844 expr = build_zero_cst (ptr_type_node);
10845 }
10846 expr = c_fully_fold (expr, false, NULL);
10847 expr = convert (ptr_type_node, expr);
10848 t = build1 (GOTO_EXPR, void_type_node, expr);
10849 SET_EXPR_LOCATION (t, loc);
10850 return add_stmt (t);
10851 }
10852
10853 /* Generate a C `return' statement. RETVAL is the expression for what
10854 to return, or a null pointer for `return;' with no value. LOC is
10855 the location of the return statement, or the location of the expression,
10856 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10857 is the original type of RETVAL. */
10858
10859 tree
10860 c_finish_return (location_t loc, tree retval, tree origtype)
10861 {
10862 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10863 bool no_warning = false;
10864 bool npc = false;
10865
10866 /* Use the expansion point to handle cases such as returning NULL
10867 in a function returning void. */
10868 location_t xloc = expansion_point_location_if_in_system_header (loc);
10869
10870 if (TREE_THIS_VOLATILE (current_function_decl))
10871 warning_at (xloc, 0,
10872 "function declared %<noreturn%> has a %<return%> statement");
10873
10874 if (retval)
10875 {
10876 tree semantic_type = NULL_TREE;
10877 npc = null_pointer_constant_p (retval);
10878 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10879 {
10880 semantic_type = TREE_TYPE (retval);
10881 retval = TREE_OPERAND (retval, 0);
10882 }
10883 retval = c_fully_fold (retval, false, NULL);
10884 if (semantic_type
10885 && valtype != NULL_TREE
10886 && TREE_CODE (valtype) != VOID_TYPE)
10887 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10888 }
10889
10890 if (!retval)
10891 {
10892 current_function_returns_null = 1;
10893 if ((warn_return_type >= 0 || flag_isoc99)
10894 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10895 {
10896 bool warned_here;
10897 if (flag_isoc99)
10898 warned_here = pedwarn
10899 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10900 "%<return%> with no value, in function returning non-void");
10901 else
10902 warned_here = warning_at
10903 (loc, OPT_Wreturn_type,
10904 "%<return%> with no value, in function returning non-void");
10905 no_warning = true;
10906 if (warned_here)
10907 inform (DECL_SOURCE_LOCATION (current_function_decl),
10908 "declared here");
10909 }
10910 }
10911 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10912 {
10913 current_function_returns_null = 1;
10914 bool warned_here;
10915 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10916 warned_here = pedwarn
10917 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10918 "%<return%> with a value, in function returning void");
10919 else
10920 warned_here = pedwarn
10921 (xloc, OPT_Wpedantic, "ISO C forbids "
10922 "%<return%> with expression, in function returning void");
10923 if (warned_here)
10924 inform (DECL_SOURCE_LOCATION (current_function_decl),
10925 "declared here");
10926 }
10927 else
10928 {
10929 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10930 retval, origtype, ic_return,
10931 npc, NULL_TREE, NULL_TREE, 0);
10932 tree res = DECL_RESULT (current_function_decl);
10933 tree inner;
10934 bool save;
10935
10936 current_function_returns_value = 1;
10937 if (t == error_mark_node)
10938 return NULL_TREE;
10939
10940 save = in_late_binary_op;
10941 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10942 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10943 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10944 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10945 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10946 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10947 in_late_binary_op = true;
10948 inner = t = convert (TREE_TYPE (res), t);
10949 in_late_binary_op = save;
10950
10951 /* Strip any conversions, additions, and subtractions, and see if
10952 we are returning the address of a local variable. Warn if so. */
10953 while (1)
10954 {
10955 switch (TREE_CODE (inner))
10956 {
10957 CASE_CONVERT:
10958 case NON_LVALUE_EXPR:
10959 case PLUS_EXPR:
10960 case POINTER_PLUS_EXPR:
10961 inner = TREE_OPERAND (inner, 0);
10962 continue;
10963
10964 case MINUS_EXPR:
10965 /* If the second operand of the MINUS_EXPR has a pointer
10966 type (or is converted from it), this may be valid, so
10967 don't give a warning. */
10968 {
10969 tree op1 = TREE_OPERAND (inner, 1);
10970
10971 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10972 && (CONVERT_EXPR_P (op1)
10973 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10974 op1 = TREE_OPERAND (op1, 0);
10975
10976 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10977 break;
10978
10979 inner = TREE_OPERAND (inner, 0);
10980 continue;
10981 }
10982
10983 case ADDR_EXPR:
10984 inner = TREE_OPERAND (inner, 0);
10985
10986 while (REFERENCE_CLASS_P (inner)
10987 && !INDIRECT_REF_P (inner))
10988 inner = TREE_OPERAND (inner, 0);
10989
10990 if (DECL_P (inner)
10991 && !DECL_EXTERNAL (inner)
10992 && !TREE_STATIC (inner)
10993 && DECL_CONTEXT (inner) == current_function_decl
10994 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
10995 {
10996 if (TREE_CODE (inner) == LABEL_DECL)
10997 warning_at (loc, OPT_Wreturn_local_addr,
10998 "function returns address of label");
10999 else
11000 {
11001 warning_at (loc, OPT_Wreturn_local_addr,
11002 "function returns address of local variable");
11003 tree zero = build_zero_cst (TREE_TYPE (res));
11004 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11005 }
11006 }
11007 break;
11008
11009 default:
11010 break;
11011 }
11012
11013 break;
11014 }
11015
11016 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11017 SET_EXPR_LOCATION (retval, loc);
11018
11019 if (warn_sequence_point)
11020 verify_sequence_points (retval);
11021 }
11022
11023 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11024 if (no_warning)
11025 suppress_warning (ret_stmt, OPT_Wreturn_type);
11026 return add_stmt (ret_stmt);
11027 }
11028 \f
11029 struct c_switch {
11030 /* The SWITCH_STMT being built. */
11031 tree switch_stmt;
11032
11033 /* The original type of the testing expression, i.e. before the
11034 default conversion is applied. */
11035 tree orig_type;
11036
11037 /* A splay-tree mapping the low element of a case range to the high
11038 element, or NULL_TREE if there is no high element. Used to
11039 determine whether or not a new case label duplicates an old case
11040 label. We need a tree, rather than simply a hash table, because
11041 of the GNU case range extension. */
11042 splay_tree cases;
11043
11044 /* The bindings at the point of the switch. This is used for
11045 warnings crossing decls when branching to a case label. */
11046 struct c_spot_bindings *bindings;
11047
11048 /* Whether the switch includes any break statements. */
11049 bool break_stmt_seen_p;
11050
11051 /* The next node on the stack. */
11052 struct c_switch *next;
11053
11054 /* Remember whether the controlling expression had boolean type
11055 before integer promotions for the sake of -Wswitch-bool. */
11056 bool bool_cond_p;
11057 };
11058
11059 /* A stack of the currently active switch statements. The innermost
11060 switch statement is on the top of the stack. There is no need to
11061 mark the stack for garbage collection because it is only active
11062 during the processing of the body of a function, and we never
11063 collect at that point. */
11064
11065 struct c_switch *c_switch_stack;
11066
11067 /* Start a C switch statement, testing expression EXP. Return the new
11068 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11069 SWITCH_COND_LOC is the location of the switch's condition.
11070 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11071
11072 tree
11073 c_start_switch (location_t switch_loc,
11074 location_t switch_cond_loc,
11075 tree exp, bool explicit_cast_p)
11076 {
11077 tree orig_type = error_mark_node;
11078 bool bool_cond_p = false;
11079 struct c_switch *cs;
11080
11081 if (exp != error_mark_node)
11082 {
11083 orig_type = TREE_TYPE (exp);
11084
11085 if (!INTEGRAL_TYPE_P (orig_type))
11086 {
11087 if (orig_type != error_mark_node)
11088 {
11089 error_at (switch_cond_loc, "switch quantity not an integer");
11090 orig_type = error_mark_node;
11091 }
11092 exp = integer_zero_node;
11093 }
11094 else
11095 {
11096 tree type = TYPE_MAIN_VARIANT (orig_type);
11097 tree e = exp;
11098
11099 /* Warn if the condition has boolean value. */
11100 while (TREE_CODE (e) == COMPOUND_EXPR)
11101 e = TREE_OPERAND (e, 1);
11102
11103 if ((TREE_CODE (type) == BOOLEAN_TYPE
11104 || truth_value_p (TREE_CODE (e)))
11105 /* Explicit cast to int suppresses this warning. */
11106 && !(TREE_CODE (type) == INTEGER_TYPE
11107 && explicit_cast_p))
11108 bool_cond_p = true;
11109
11110 if (!in_system_header_at (input_location)
11111 && (type == long_integer_type_node
11112 || type == long_unsigned_type_node))
11113 warning_at (switch_cond_loc,
11114 OPT_Wtraditional, "%<long%> switch expression not "
11115 "converted to %<int%> in ISO C");
11116
11117 exp = c_fully_fold (exp, false, NULL);
11118 exp = default_conversion (exp);
11119
11120 if (warn_sequence_point)
11121 verify_sequence_points (exp);
11122 }
11123 }
11124
11125 /* Add this new SWITCH_STMT to the stack. */
11126 cs = XNEW (struct c_switch);
11127 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11128 NULL_TREE, orig_type, NULL_TREE);
11129 cs->orig_type = orig_type;
11130 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11131 cs->bindings = c_get_switch_bindings ();
11132 cs->break_stmt_seen_p = false;
11133 cs->bool_cond_p = bool_cond_p;
11134 cs->next = c_switch_stack;
11135 c_switch_stack = cs;
11136
11137 return add_stmt (cs->switch_stmt);
11138 }
11139
11140 /* Process a case label at location LOC. */
11141
11142 tree
11143 do_case (location_t loc, tree low_value, tree high_value)
11144 {
11145 tree label = NULL_TREE;
11146
11147 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11148 {
11149 low_value = c_fully_fold (low_value, false, NULL);
11150 if (TREE_CODE (low_value) == INTEGER_CST)
11151 pedwarn (loc, OPT_Wpedantic,
11152 "case label is not an integer constant expression");
11153 }
11154
11155 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11156 {
11157 high_value = c_fully_fold (high_value, false, NULL);
11158 if (TREE_CODE (high_value) == INTEGER_CST)
11159 pedwarn (input_location, OPT_Wpedantic,
11160 "case label is not an integer constant expression");
11161 }
11162
11163 if (c_switch_stack == NULL)
11164 {
11165 if (low_value)
11166 error_at (loc, "case label not within a switch statement");
11167 else
11168 error_at (loc, "%<default%> label not within a switch statement");
11169 return NULL_TREE;
11170 }
11171
11172 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11173 EXPR_LOCATION (c_switch_stack->switch_stmt),
11174 loc))
11175 return NULL_TREE;
11176
11177 label = c_add_case_label (loc, c_switch_stack->cases,
11178 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11179 low_value, high_value);
11180 if (label == error_mark_node)
11181 label = NULL_TREE;
11182 return label;
11183 }
11184
11185 /* Finish the switch statement. TYPE is the original type of the
11186 controlling expression of the switch, or NULL_TREE. */
11187
11188 void
11189 c_finish_switch (tree body, tree type)
11190 {
11191 struct c_switch *cs = c_switch_stack;
11192 location_t switch_location;
11193
11194 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11195
11196 /* Emit warnings as needed. */
11197 switch_location = EXPR_LOCATION (cs->switch_stmt);
11198 c_do_switch_warnings (cs->cases, switch_location,
11199 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11200 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11201 if (c_switch_covers_all_cases_p (cs->cases,
11202 SWITCH_STMT_TYPE (cs->switch_stmt)))
11203 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11204 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11205
11206 /* Pop the stack. */
11207 c_switch_stack = cs->next;
11208 splay_tree_delete (cs->cases);
11209 c_release_switch_bindings (cs->bindings);
11210 XDELETE (cs);
11211 }
11212 \f
11213 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11214 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11215 may be null. */
11216
11217 void
11218 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11219 tree else_block)
11220 {
11221 tree stmt;
11222
11223 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11224 SET_EXPR_LOCATION (stmt, if_locus);
11225 add_stmt (stmt);
11226 }
11227
11228 tree
11229 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11230 {
11231 /* In switch statements break is sometimes stylistically used after
11232 a return statement. This can lead to spurious warnings about
11233 control reaching the end of a non-void function when it is
11234 inlined. Note that we are calling block_may_fallthru with
11235 language specific tree nodes; this works because
11236 block_may_fallthru returns true when given something it does not
11237 understand. */
11238 bool skip = !block_may_fallthru (cur_stmt_list);
11239
11240 if (is_break)
11241 switch (in_statement)
11242 {
11243 case 0:
11244 error_at (loc, "break statement not within loop or switch");
11245 return NULL_TREE;
11246 case IN_OMP_BLOCK:
11247 error_at (loc, "invalid exit from OpenMP structured block");
11248 return NULL_TREE;
11249 case IN_OMP_FOR:
11250 error_at (loc, "break statement used with OpenMP for loop");
11251 return NULL_TREE;
11252 case IN_ITERATION_STMT:
11253 case IN_OBJC_FOREACH:
11254 break;
11255 default:
11256 gcc_assert (in_statement & IN_SWITCH_STMT);
11257 c_switch_stack->break_stmt_seen_p = true;
11258 break;
11259 }
11260 else
11261 switch (in_statement & ~IN_SWITCH_STMT)
11262 {
11263 case 0:
11264 error_at (loc, "continue statement not within a loop");
11265 return NULL_TREE;
11266 case IN_OMP_BLOCK:
11267 error_at (loc, "invalid exit from OpenMP structured block");
11268 return NULL_TREE;
11269 case IN_ITERATION_STMT:
11270 case IN_OMP_FOR:
11271 case IN_OBJC_FOREACH:
11272 break;
11273 default:
11274 gcc_unreachable ();
11275 }
11276
11277 if (skip)
11278 return NULL_TREE;
11279 else if ((in_statement & IN_OBJC_FOREACH)
11280 && !(is_break && (in_statement & IN_SWITCH_STMT)))
11281 {
11282 /* The foreach expander produces low-level code using gotos instead
11283 of a structured loop construct. */
11284 gcc_assert (label);
11285 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11286 }
11287 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11288 }
11289
11290 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11291
11292 static void
11293 emit_side_effect_warnings (location_t loc, tree expr)
11294 {
11295 maybe_warn_nodiscard (loc, expr);
11296 if (!warn_unused_value)
11297 return;
11298 if (expr == error_mark_node)
11299 ;
11300 else if (!TREE_SIDE_EFFECTS (expr))
11301 {
11302 if (!VOID_TYPE_P (TREE_TYPE (expr))
11303 && !warning_suppressed_p (expr, OPT_Wunused_value))
11304 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11305 }
11306 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11307 {
11308 tree r = expr;
11309 location_t cloc = loc;
11310 while (TREE_CODE (r) == COMPOUND_EXPR)
11311 {
11312 if (EXPR_HAS_LOCATION (r))
11313 cloc = EXPR_LOCATION (r);
11314 r = TREE_OPERAND (r, 1);
11315 }
11316 if (!TREE_SIDE_EFFECTS (r)
11317 && !VOID_TYPE_P (TREE_TYPE (r))
11318 && !CONVERT_EXPR_P (r)
11319 && !warning_suppressed_p (r, OPT_Wunused_value)
11320 && !warning_suppressed_p (expr, OPT_Wunused_value))
11321 warning_at (cloc, OPT_Wunused_value,
11322 "right-hand operand of comma expression has no effect");
11323 }
11324 else
11325 warn_if_unused_value (expr, loc);
11326 }
11327
11328 /* Process an expression as if it were a complete statement. Emit
11329 diagnostics, but do not call ADD_STMT. LOC is the location of the
11330 statement. */
11331
11332 tree
11333 c_process_expr_stmt (location_t loc, tree expr)
11334 {
11335 tree exprv;
11336
11337 if (!expr)
11338 return NULL_TREE;
11339
11340 expr = c_fully_fold (expr, false, NULL);
11341
11342 if (warn_sequence_point)
11343 verify_sequence_points (expr);
11344
11345 if (TREE_TYPE (expr) != error_mark_node
11346 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11347 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11348 error_at (loc, "expression statement has incomplete type");
11349
11350 /* If we're not processing a statement expression, warn about unused values.
11351 Warnings for statement expressions will be emitted later, once we figure
11352 out which is the result. */
11353 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11354 && (warn_unused_value || warn_unused_result))
11355 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11356
11357 exprv = expr;
11358 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11359 exprv = TREE_OPERAND (exprv, 1);
11360 while (CONVERT_EXPR_P (exprv))
11361 exprv = TREE_OPERAND (exprv, 0);
11362 if (DECL_P (exprv)
11363 || handled_component_p (exprv)
11364 || TREE_CODE (exprv) == ADDR_EXPR)
11365 mark_exp_read (exprv);
11366
11367 /* If the expression is not of a type to which we cannot assign a line
11368 number, wrap the thing in a no-op NOP_EXPR. */
11369 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11370 {
11371 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11372 SET_EXPR_LOCATION (expr, loc);
11373 }
11374
11375 return expr;
11376 }
11377
11378 /* Emit an expression as a statement. LOC is the location of the
11379 expression. */
11380
11381 tree
11382 c_finish_expr_stmt (location_t loc, tree expr)
11383 {
11384 if (expr)
11385 return add_stmt (c_process_expr_stmt (loc, expr));
11386 else
11387 return NULL;
11388 }
11389
11390 /* Do the opposite and emit a statement as an expression. To begin,
11391 create a new binding level and return it. */
11392
11393 tree
11394 c_begin_stmt_expr (void)
11395 {
11396 tree ret;
11397
11398 /* We must force a BLOCK for this level so that, if it is not expanded
11399 later, there is a way to turn off the entire subtree of blocks that
11400 are contained in it. */
11401 keep_next_level ();
11402 ret = c_begin_compound_stmt (true);
11403
11404 c_bindings_start_stmt_expr (c_switch_stack == NULL
11405 ? NULL
11406 : c_switch_stack->bindings);
11407
11408 /* Mark the current statement list as belonging to a statement list. */
11409 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11410
11411 return ret;
11412 }
11413
11414 /* LOC is the location of the compound statement to which this body
11415 belongs. */
11416
11417 tree
11418 c_finish_stmt_expr (location_t loc, tree body)
11419 {
11420 tree last, type, tmp, val;
11421 tree *last_p;
11422
11423 body = c_end_compound_stmt (loc, body, true);
11424
11425 c_bindings_end_stmt_expr (c_switch_stack == NULL
11426 ? NULL
11427 : c_switch_stack->bindings);
11428
11429 /* Locate the last statement in BODY. See c_end_compound_stmt
11430 about always returning a BIND_EXPR. */
11431 last_p = &BIND_EXPR_BODY (body);
11432 last = BIND_EXPR_BODY (body);
11433
11434 continue_searching:
11435 if (TREE_CODE (last) == STATEMENT_LIST)
11436 {
11437 tree_stmt_iterator l = tsi_last (last);
11438
11439 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11440 tsi_prev (&l);
11441
11442 /* This can happen with degenerate cases like ({ }). No value. */
11443 if (tsi_end_p (l))
11444 return body;
11445
11446 /* If we're supposed to generate side effects warnings, process
11447 all of the statements except the last. */
11448 if (warn_unused_value || warn_unused_result)
11449 {
11450 for (tree_stmt_iterator i = tsi_start (last);
11451 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11452 {
11453 location_t tloc;
11454 tree t = tsi_stmt (i);
11455
11456 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11457 emit_side_effect_warnings (tloc, t);
11458 }
11459 }
11460 last_p = tsi_stmt_ptr (l);
11461 last = *last_p;
11462 }
11463
11464 /* If the end of the list is exception related, then the list was split
11465 by a call to push_cleanup. Continue searching. */
11466 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11467 || TREE_CODE (last) == TRY_CATCH_EXPR)
11468 {
11469 last_p = &TREE_OPERAND (last, 0);
11470 last = *last_p;
11471 goto continue_searching;
11472 }
11473
11474 if (last == error_mark_node)
11475 return last;
11476
11477 /* In the case that the BIND_EXPR is not necessary, return the
11478 expression out from inside it. */
11479 if ((last == BIND_EXPR_BODY (body)
11480 /* Skip nested debug stmts. */
11481 || last == expr_first (BIND_EXPR_BODY (body)))
11482 && BIND_EXPR_VARS (body) == NULL)
11483 {
11484 /* Even if this looks constant, do not allow it in a constant
11485 expression. */
11486 last = c_wrap_maybe_const (last, true);
11487 /* Do not warn if the return value of a statement expression is
11488 unused. */
11489 suppress_warning (last, OPT_Wunused);
11490 return last;
11491 }
11492
11493 /* Extract the type of said expression. */
11494 type = TREE_TYPE (last);
11495
11496 /* If we're not returning a value at all, then the BIND_EXPR that
11497 we already have is a fine expression to return. */
11498 if (!type || VOID_TYPE_P (type))
11499 return body;
11500
11501 /* Now that we've located the expression containing the value, it seems
11502 silly to make voidify_wrapper_expr repeat the process. Create a
11503 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11504 tmp = create_tmp_var_raw (type);
11505
11506 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11507 tree_expr_nonnegative_p giving up immediately. */
11508 val = last;
11509 if (TREE_CODE (val) == NOP_EXPR
11510 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11511 val = TREE_OPERAND (val, 0);
11512
11513 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11514 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11515
11516 {
11517 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11518 SET_EXPR_LOCATION (t, loc);
11519 return t;
11520 }
11521 }
11522 \f
11523 /* Begin and end compound statements. This is as simple as pushing
11524 and popping new statement lists from the tree. */
11525
11526 tree
11527 c_begin_compound_stmt (bool do_scope)
11528 {
11529 tree stmt = push_stmt_list ();
11530 if (do_scope)
11531 push_scope ();
11532 return stmt;
11533 }
11534
11535 /* End a compound statement. STMT is the statement. LOC is the
11536 location of the compound statement-- this is usually the location
11537 of the opening brace. */
11538
11539 tree
11540 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11541 {
11542 tree block = NULL;
11543
11544 if (do_scope)
11545 {
11546 if (c_dialect_objc ())
11547 objc_clear_super_receiver ();
11548 block = pop_scope ();
11549 }
11550
11551 stmt = pop_stmt_list (stmt);
11552 stmt = c_build_bind_expr (loc, block, stmt);
11553
11554 /* If this compound statement is nested immediately inside a statement
11555 expression, then force a BIND_EXPR to be created. Otherwise we'll
11556 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11557 STATEMENT_LISTs merge, and thus we can lose track of what statement
11558 was really last. */
11559 if (building_stmt_list_p ()
11560 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11561 && TREE_CODE (stmt) != BIND_EXPR)
11562 {
11563 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11564 TREE_SIDE_EFFECTS (stmt) = 1;
11565 SET_EXPR_LOCATION (stmt, loc);
11566 }
11567
11568 return stmt;
11569 }
11570
11571 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11572 when the current scope is exited. EH_ONLY is true when this is not
11573 meant to apply to normal control flow transfer. */
11574
11575 void
11576 push_cleanup (tree decl, tree cleanup, bool eh_only)
11577 {
11578 enum tree_code code;
11579 tree stmt, list;
11580 bool stmt_expr;
11581
11582 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11583 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11584 add_stmt (stmt);
11585 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11586 list = push_stmt_list ();
11587 TREE_OPERAND (stmt, 0) = list;
11588 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11589 }
11590 \f
11591 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11592 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11593
11594 static tree
11595 build_vec_cmp (tree_code code, tree type,
11596 tree arg0, tree arg1)
11597 {
11598 tree zero_vec = build_zero_cst (type);
11599 tree minus_one_vec = build_minus_one_cst (type);
11600 tree cmp_type = truth_type_for (type);
11601 tree cmp = build2 (code, cmp_type, arg0, arg1);
11602 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11603 }
11604
11605 /* Possibly warn about an address of OP never being NULL in a comparison
11606 operation CODE involving null. */
11607
11608 static void
11609 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
11610 {
11611 /* Prevent warnings issued for macro expansion. */
11612 if (!warn_address
11613 || warning_suppressed_p (op, OPT_Waddress)
11614 || from_macro_expansion_at (loc))
11615 return;
11616
11617 if (TREE_CODE (op) == NOP_EXPR)
11618 {
11619 /* Allow casts to intptr_t to suppress the warning. */
11620 tree type = TREE_TYPE (op);
11621 if (TREE_CODE (type) == INTEGER_TYPE)
11622 return;
11623 op = TREE_OPERAND (op, 0);
11624 }
11625
11626 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
11627 {
11628 /* Allow a cast to void* to suppress the warning. */
11629 tree type = TREE_TYPE (TREE_TYPE (op));
11630 if (VOID_TYPE_P (type))
11631 return;
11632
11633 /* Adding any value to a null pointer, including zero, is undefined
11634 in C. This includes the expression &p[0] where p is the null
11635 pointer, although &p[0] will have been folded to p by this point
11636 and so not diagnosed. */
11637 if (code == EQ_EXPR)
11638 warning_at (loc, OPT_Waddress,
11639 "the comparison will always evaluate as %<false%> "
11640 "for the pointer operand in %qE must not be NULL",
11641 op);
11642 else
11643 warning_at (loc, OPT_Waddress,
11644 "the comparison will always evaluate as %<true%> "
11645 "for the pointer operand in %qE must not be NULL",
11646 op);
11647
11648 return;
11649 }
11650
11651 if (TREE_CODE (op) != ADDR_EXPR)
11652 return;
11653
11654 op = TREE_OPERAND (op, 0);
11655
11656 if (TREE_CODE (op) == IMAGPART_EXPR
11657 || TREE_CODE (op) == REALPART_EXPR)
11658 {
11659 /* The address of either complex part may not be null. */
11660 if (code == EQ_EXPR)
11661 warning_at (loc, OPT_Waddress,
11662 "the comparison will always evaluate as %<false%> "
11663 "for the address of %qE will never be NULL",
11664 op);
11665 else
11666 warning_at (loc, OPT_Waddress,
11667 "the comparison will always evaluate as %<true%> "
11668 "for the address of %qE will never be NULL",
11669 op);
11670 return;
11671 }
11672
11673 /* Set to true in the loop below if OP dereferences is operand.
11674 In such a case the ultimate target need not be a decl for
11675 the null [in]equality test to be constant. */
11676 bool deref = false;
11677
11678 /* Get the outermost array or object, or member. */
11679 while (handled_component_p (op))
11680 {
11681 if (TREE_CODE (op) == COMPONENT_REF)
11682 {
11683 /* Get the member (its address is never null). */
11684 op = TREE_OPERAND (op, 1);
11685 break;
11686 }
11687
11688 /* Get the outer array/object to refer to in the warning. */
11689 op = TREE_OPERAND (op, 0);
11690 deref = true;
11691 }
11692
11693 if ((!deref && !decl_with_nonnull_addr_p (op))
11694 || from_macro_expansion_at (loc))
11695 return;
11696
11697 if (code == EQ_EXPR)
11698 warning_at (loc, OPT_Waddress,
11699 "the comparison will always evaluate as %<false%> "
11700 "for the address of %qE will never be NULL",
11701 op);
11702 else
11703 warning_at (loc, OPT_Waddress,
11704 "the comparison will always evaluate as %<true%> "
11705 "for the address of %qE will never be NULL",
11706 op);
11707
11708 if (DECL_P (op))
11709 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
11710 }
11711
11712 /* Build a binary-operation expression without default conversions.
11713 CODE is the kind of expression to build.
11714 LOCATION is the operator's location.
11715 This function differs from `build' in several ways:
11716 the data type of the result is computed and recorded in it,
11717 warnings are generated if arg data types are invalid,
11718 special handling for addition and subtraction of pointers is known,
11719 and some optimization is done (operations on narrow ints
11720 are done in the narrower type when that gives the same result).
11721 Constant folding is also done before the result is returned.
11722
11723 Note that the operands will never have enumeral types, or function
11724 or array types, because either they will have the default conversions
11725 performed or they have both just been converted to some other type in which
11726 the arithmetic is to be done. */
11727
11728 tree
11729 build_binary_op (location_t location, enum tree_code code,
11730 tree orig_op0, tree orig_op1, bool convert_p)
11731 {
11732 tree type0, type1, orig_type0, orig_type1;
11733 tree eptype;
11734 enum tree_code code0, code1;
11735 tree op0, op1;
11736 tree ret = error_mark_node;
11737 const char *invalid_op_diag;
11738 bool op0_int_operands, op1_int_operands;
11739 bool int_const, int_const_or_overflow, int_operands;
11740
11741 /* Expression code to give to the expression when it is built.
11742 Normally this is CODE, which is what the caller asked for,
11743 but in some special cases we change it. */
11744 enum tree_code resultcode = code;
11745
11746 /* Data type in which the computation is to be performed.
11747 In the simplest cases this is the common type of the arguments. */
11748 tree result_type = NULL;
11749
11750 /* When the computation is in excess precision, the type of the
11751 final EXCESS_PRECISION_EXPR. */
11752 tree semantic_result_type = NULL;
11753
11754 /* Nonzero means operands have already been type-converted
11755 in whatever way is necessary.
11756 Zero means they need to be converted to RESULT_TYPE. */
11757 int converted = 0;
11758
11759 /* Nonzero means create the expression with this type, rather than
11760 RESULT_TYPE. */
11761 tree build_type = NULL_TREE;
11762
11763 /* Nonzero means after finally constructing the expression
11764 convert it to this type. */
11765 tree final_type = NULL_TREE;
11766
11767 /* Nonzero if this is an operation like MIN or MAX which can
11768 safely be computed in short if both args are promoted shorts.
11769 Also implies COMMON.
11770 -1 indicates a bitwise operation; this makes a difference
11771 in the exact conditions for when it is safe to do the operation
11772 in a narrower mode. */
11773 int shorten = 0;
11774
11775 /* Nonzero if this is a comparison operation;
11776 if both args are promoted shorts, compare the original shorts.
11777 Also implies COMMON. */
11778 int short_compare = 0;
11779
11780 /* Nonzero if this is a right-shift operation, which can be computed on the
11781 original short and then promoted if the operand is a promoted short. */
11782 int short_shift = 0;
11783
11784 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11785 int common = 0;
11786
11787 /* True means types are compatible as far as ObjC is concerned. */
11788 bool objc_ok;
11789
11790 /* True means this is an arithmetic operation that may need excess
11791 precision. */
11792 bool may_need_excess_precision;
11793
11794 /* True means this is a boolean operation that converts both its
11795 operands to truth-values. */
11796 bool boolean_op = false;
11797
11798 /* Remember whether we're doing / or %. */
11799 bool doing_div_or_mod = false;
11800
11801 /* Remember whether we're doing << or >>. */
11802 bool doing_shift = false;
11803
11804 /* Tree holding instrumentation expression. */
11805 tree instrument_expr = NULL;
11806
11807 if (location == UNKNOWN_LOCATION)
11808 location = input_location;
11809
11810 op0 = orig_op0;
11811 op1 = orig_op1;
11812
11813 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11814 if (op0_int_operands)
11815 op0 = remove_c_maybe_const_expr (op0);
11816 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11817 if (op1_int_operands)
11818 op1 = remove_c_maybe_const_expr (op1);
11819 int_operands = (op0_int_operands && op1_int_operands);
11820 if (int_operands)
11821 {
11822 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11823 && TREE_CODE (orig_op1) == INTEGER_CST);
11824 int_const = (int_const_or_overflow
11825 && !TREE_OVERFLOW (orig_op0)
11826 && !TREE_OVERFLOW (orig_op1));
11827 }
11828 else
11829 int_const = int_const_or_overflow = false;
11830
11831 /* Do not apply default conversion in mixed vector/scalar expression. */
11832 if (convert_p
11833 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11834 {
11835 op0 = default_conversion (op0);
11836 op1 = default_conversion (op1);
11837 }
11838
11839 orig_type0 = type0 = TREE_TYPE (op0);
11840
11841 orig_type1 = type1 = TREE_TYPE (op1);
11842
11843 /* The expression codes of the data types of the arguments tell us
11844 whether the arguments are integers, floating, pointers, etc. */
11845 code0 = TREE_CODE (type0);
11846 code1 = TREE_CODE (type1);
11847
11848 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11849 STRIP_TYPE_NOPS (op0);
11850 STRIP_TYPE_NOPS (op1);
11851
11852 /* If an error was already reported for one of the arguments,
11853 avoid reporting another error. */
11854
11855 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11856 return error_mark_node;
11857
11858 if (code0 == POINTER_TYPE
11859 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11860 return error_mark_node;
11861
11862 if (code1 == POINTER_TYPE
11863 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11864 return error_mark_node;
11865
11866 if ((invalid_op_diag
11867 = targetm.invalid_binary_op (code, type0, type1)))
11868 {
11869 error_at (location, invalid_op_diag);
11870 return error_mark_node;
11871 }
11872
11873 switch (code)
11874 {
11875 case PLUS_EXPR:
11876 case MINUS_EXPR:
11877 case MULT_EXPR:
11878 case TRUNC_DIV_EXPR:
11879 case CEIL_DIV_EXPR:
11880 case FLOOR_DIV_EXPR:
11881 case ROUND_DIV_EXPR:
11882 case EXACT_DIV_EXPR:
11883 may_need_excess_precision = true;
11884 break;
11885
11886 case EQ_EXPR:
11887 case NE_EXPR:
11888 case LE_EXPR:
11889 case GE_EXPR:
11890 case LT_EXPR:
11891 case GT_EXPR:
11892 /* Excess precision for implicit conversions of integers to
11893 floating point in C11 and later. */
11894 may_need_excess_precision = (flag_isoc11
11895 && (ANY_INTEGRAL_TYPE_P (type0)
11896 || ANY_INTEGRAL_TYPE_P (type1)));
11897 break;
11898
11899 default:
11900 may_need_excess_precision = false;
11901 break;
11902 }
11903 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11904 {
11905 op0 = TREE_OPERAND (op0, 0);
11906 type0 = TREE_TYPE (op0);
11907 }
11908 else if (may_need_excess_precision
11909 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11910 {
11911 type0 = eptype;
11912 op0 = convert (eptype, op0);
11913 }
11914 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11915 {
11916 op1 = TREE_OPERAND (op1, 0);
11917 type1 = TREE_TYPE (op1);
11918 }
11919 else if (may_need_excess_precision
11920 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11921 {
11922 type1 = eptype;
11923 op1 = convert (eptype, op1);
11924 }
11925
11926 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11927
11928 /* In case when one of the operands of the binary operation is
11929 a vector and another is a scalar -- convert scalar to vector. */
11930 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11931 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11932 {
11933 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11934 true);
11935
11936 switch (convert_flag)
11937 {
11938 case stv_error:
11939 return error_mark_node;
11940 case stv_firstarg:
11941 {
11942 bool maybe_const = true;
11943 tree sc;
11944 sc = c_fully_fold (op0, false, &maybe_const);
11945 sc = save_expr (sc);
11946 sc = convert (TREE_TYPE (type1), sc);
11947 op0 = build_vector_from_val (type1, sc);
11948 if (!maybe_const)
11949 op0 = c_wrap_maybe_const (op0, true);
11950 orig_type0 = type0 = TREE_TYPE (op0);
11951 code0 = TREE_CODE (type0);
11952 converted = 1;
11953 break;
11954 }
11955 case stv_secondarg:
11956 {
11957 bool maybe_const = true;
11958 tree sc;
11959 sc = c_fully_fold (op1, false, &maybe_const);
11960 sc = save_expr (sc);
11961 sc = convert (TREE_TYPE (type0), sc);
11962 op1 = build_vector_from_val (type0, sc);
11963 if (!maybe_const)
11964 op1 = c_wrap_maybe_const (op1, true);
11965 orig_type1 = type1 = TREE_TYPE (op1);
11966 code1 = TREE_CODE (type1);
11967 converted = 1;
11968 break;
11969 }
11970 default:
11971 break;
11972 }
11973 }
11974
11975 switch (code)
11976 {
11977 case PLUS_EXPR:
11978 /* Handle the pointer + int case. */
11979 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11980 {
11981 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11982 goto return_build_binary_op;
11983 }
11984 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11985 {
11986 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11987 goto return_build_binary_op;
11988 }
11989 else
11990 common = 1;
11991 break;
11992
11993 case MINUS_EXPR:
11994 /* Subtraction of two similar pointers.
11995 We must subtract them as integers, then divide by object size. */
11996 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11997 && comp_target_types (location, type0, type1))
11998 {
11999 ret = pointer_diff (location, op0, op1, &instrument_expr);
12000 goto return_build_binary_op;
12001 }
12002 /* Handle pointer minus int. Just like pointer plus int. */
12003 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12004 {
12005 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12006 goto return_build_binary_op;
12007 }
12008 else
12009 common = 1;
12010 break;
12011
12012 case MULT_EXPR:
12013 common = 1;
12014 break;
12015
12016 case TRUNC_DIV_EXPR:
12017 case CEIL_DIV_EXPR:
12018 case FLOOR_DIV_EXPR:
12019 case ROUND_DIV_EXPR:
12020 case EXACT_DIV_EXPR:
12021 doing_div_or_mod = true;
12022 warn_for_div_by_zero (location, op1);
12023
12024 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12025 || code0 == FIXED_POINT_TYPE
12026 || code0 == COMPLEX_TYPE
12027 || gnu_vector_type_p (type0))
12028 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12029 || code1 == FIXED_POINT_TYPE
12030 || code1 == COMPLEX_TYPE
12031 || gnu_vector_type_p (type1)))
12032 {
12033 enum tree_code tcode0 = code0, tcode1 = code1;
12034
12035 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12036 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12037 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12038 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12039
12040 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
12041 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12042 resultcode = RDIV_EXPR;
12043 else
12044 /* Although it would be tempting to shorten always here, that
12045 loses on some targets, since the modulo instruction is
12046 undefined if the quotient can't be represented in the
12047 computation mode. We shorten only if unsigned or if
12048 dividing by something we know != -1. */
12049 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12050 || (TREE_CODE (op1) == INTEGER_CST
12051 && !integer_all_onesp (op1)));
12052 common = 1;
12053 }
12054 break;
12055
12056 case BIT_AND_EXPR:
12057 case BIT_IOR_EXPR:
12058 case BIT_XOR_EXPR:
12059 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12060 shorten = -1;
12061 /* Allow vector types which are not floating point types. */
12062 else if (gnu_vector_type_p (type0)
12063 && gnu_vector_type_p (type1)
12064 && !VECTOR_FLOAT_TYPE_P (type0)
12065 && !VECTOR_FLOAT_TYPE_P (type1))
12066 common = 1;
12067 break;
12068
12069 case TRUNC_MOD_EXPR:
12070 case FLOOR_MOD_EXPR:
12071 doing_div_or_mod = true;
12072 warn_for_div_by_zero (location, op1);
12073
12074 if (gnu_vector_type_p (type0)
12075 && gnu_vector_type_p (type1)
12076 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12077 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12078 common = 1;
12079 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12080 {
12081 /* Although it would be tempting to shorten always here, that loses
12082 on some targets, since the modulo instruction is undefined if the
12083 quotient can't be represented in the computation mode. We shorten
12084 only if unsigned or if dividing by something we know != -1. */
12085 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12086 || (TREE_CODE (op1) == INTEGER_CST
12087 && !integer_all_onesp (op1)));
12088 common = 1;
12089 }
12090 break;
12091
12092 case TRUTH_ANDIF_EXPR:
12093 case TRUTH_ORIF_EXPR:
12094 case TRUTH_AND_EXPR:
12095 case TRUTH_OR_EXPR:
12096 case TRUTH_XOR_EXPR:
12097 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12098 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12099 || code0 == FIXED_POINT_TYPE)
12100 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12101 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12102 || code1 == FIXED_POINT_TYPE))
12103 {
12104 /* Result of these operations is always an int,
12105 but that does not mean the operands should be
12106 converted to ints! */
12107 result_type = integer_type_node;
12108 if (op0_int_operands)
12109 {
12110 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12111 op0 = remove_c_maybe_const_expr (op0);
12112 }
12113 else
12114 op0 = c_objc_common_truthvalue_conversion (location, op0);
12115 if (op1_int_operands)
12116 {
12117 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12118 op1 = remove_c_maybe_const_expr (op1);
12119 }
12120 else
12121 op1 = c_objc_common_truthvalue_conversion (location, op1);
12122 converted = 1;
12123 boolean_op = true;
12124 }
12125 if (code == TRUTH_ANDIF_EXPR)
12126 {
12127 int_const_or_overflow = (int_operands
12128 && TREE_CODE (orig_op0) == INTEGER_CST
12129 && (op0 == truthvalue_false_node
12130 || TREE_CODE (orig_op1) == INTEGER_CST));
12131 int_const = (int_const_or_overflow
12132 && !TREE_OVERFLOW (orig_op0)
12133 && (op0 == truthvalue_false_node
12134 || !TREE_OVERFLOW (orig_op1)));
12135 }
12136 else if (code == TRUTH_ORIF_EXPR)
12137 {
12138 int_const_or_overflow = (int_operands
12139 && TREE_CODE (orig_op0) == INTEGER_CST
12140 && (op0 == truthvalue_true_node
12141 || TREE_CODE (orig_op1) == INTEGER_CST));
12142 int_const = (int_const_or_overflow
12143 && !TREE_OVERFLOW (orig_op0)
12144 && (op0 == truthvalue_true_node
12145 || !TREE_OVERFLOW (orig_op1)));
12146 }
12147 break;
12148
12149 /* Shift operations: result has same type as first operand;
12150 always convert second operand to int.
12151 Also set SHORT_SHIFT if shifting rightward. */
12152
12153 case RSHIFT_EXPR:
12154 if (gnu_vector_type_p (type0)
12155 && gnu_vector_type_p (type1)
12156 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12157 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12158 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12159 TYPE_VECTOR_SUBPARTS (type1)))
12160 {
12161 result_type = type0;
12162 converted = 1;
12163 }
12164 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12165 || (gnu_vector_type_p (type0)
12166 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12167 && code1 == INTEGER_TYPE)
12168 {
12169 doing_shift = true;
12170 if (TREE_CODE (op1) == INTEGER_CST)
12171 {
12172 if (tree_int_cst_sgn (op1) < 0)
12173 {
12174 int_const = false;
12175 if (c_inhibit_evaluation_warnings == 0)
12176 warning_at (location, OPT_Wshift_count_negative,
12177 "right shift count is negative");
12178 }
12179 else if (code0 == VECTOR_TYPE)
12180 {
12181 if (compare_tree_int (op1,
12182 TYPE_PRECISION (TREE_TYPE (type0)))
12183 >= 0)
12184 {
12185 int_const = false;
12186 if (c_inhibit_evaluation_warnings == 0)
12187 warning_at (location, OPT_Wshift_count_overflow,
12188 "right shift count >= width of vector element");
12189 }
12190 }
12191 else
12192 {
12193 if (!integer_zerop (op1))
12194 short_shift = 1;
12195
12196 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12197 {
12198 int_const = false;
12199 if (c_inhibit_evaluation_warnings == 0)
12200 warning_at (location, OPT_Wshift_count_overflow,
12201 "right shift count >= width of type");
12202 }
12203 }
12204 }
12205
12206 /* Use the type of the value to be shifted. */
12207 result_type = type0;
12208 /* Avoid converting op1 to result_type later. */
12209 converted = 1;
12210 }
12211 break;
12212
12213 case LSHIFT_EXPR:
12214 if (gnu_vector_type_p (type0)
12215 && gnu_vector_type_p (type1)
12216 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12217 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12218 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12219 TYPE_VECTOR_SUBPARTS (type1)))
12220 {
12221 result_type = type0;
12222 converted = 1;
12223 }
12224 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12225 || (gnu_vector_type_p (type0)
12226 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12227 && code1 == INTEGER_TYPE)
12228 {
12229 doing_shift = true;
12230 if (TREE_CODE (op0) == INTEGER_CST
12231 && tree_int_cst_sgn (op0) < 0
12232 && !TYPE_OVERFLOW_WRAPS (type0))
12233 {
12234 /* Don't reject a left shift of a negative value in a context
12235 where a constant expression is needed in C90. */
12236 if (flag_isoc99)
12237 int_const = false;
12238 if (c_inhibit_evaluation_warnings == 0)
12239 warning_at (location, OPT_Wshift_negative_value,
12240 "left shift of negative value");
12241 }
12242 if (TREE_CODE (op1) == INTEGER_CST)
12243 {
12244 if (tree_int_cst_sgn (op1) < 0)
12245 {
12246 int_const = false;
12247 if (c_inhibit_evaluation_warnings == 0)
12248 warning_at (location, OPT_Wshift_count_negative,
12249 "left shift count is negative");
12250 }
12251 else if (code0 == VECTOR_TYPE)
12252 {
12253 if (compare_tree_int (op1,
12254 TYPE_PRECISION (TREE_TYPE (type0)))
12255 >= 0)
12256 {
12257 int_const = false;
12258 if (c_inhibit_evaluation_warnings == 0)
12259 warning_at (location, OPT_Wshift_count_overflow,
12260 "left shift count >= width of vector element");
12261 }
12262 }
12263 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12264 {
12265 int_const = false;
12266 if (c_inhibit_evaluation_warnings == 0)
12267 warning_at (location, OPT_Wshift_count_overflow,
12268 "left shift count >= width of type");
12269 }
12270 else if (TREE_CODE (op0) == INTEGER_CST
12271 && maybe_warn_shift_overflow (location, op0, op1)
12272 && flag_isoc99)
12273 int_const = false;
12274 }
12275
12276 /* Use the type of the value to be shifted. */
12277 result_type = type0;
12278 /* Avoid converting op1 to result_type later. */
12279 converted = 1;
12280 }
12281 break;
12282
12283 case EQ_EXPR:
12284 case NE_EXPR:
12285 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12286 {
12287 tree intt;
12288 if (!vector_types_compatible_elements_p (type0, type1))
12289 {
12290 error_at (location, "comparing vectors with different "
12291 "element types");
12292 return error_mark_node;
12293 }
12294
12295 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12296 TYPE_VECTOR_SUBPARTS (type1)))
12297 {
12298 error_at (location, "comparing vectors with different "
12299 "number of elements");
12300 return error_mark_node;
12301 }
12302
12303 /* It's not precisely specified how the usual arithmetic
12304 conversions apply to the vector types. Here, we use
12305 the unsigned type if one of the operands is signed and
12306 the other one is unsigned. */
12307 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12308 {
12309 if (!TYPE_UNSIGNED (type0))
12310 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12311 else
12312 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12313 warning_at (location, OPT_Wsign_compare, "comparison between "
12314 "types %qT and %qT", type0, type1);
12315 }
12316
12317 /* Always construct signed integer vector type. */
12318 intt = c_common_type_for_size (GET_MODE_BITSIZE
12319 (SCALAR_TYPE_MODE
12320 (TREE_TYPE (type0))), 0);
12321 if (!intt)
12322 {
12323 error_at (location, "could not find an integer type "
12324 "of the same size as %qT",
12325 TREE_TYPE (type0));
12326 return error_mark_node;
12327 }
12328 result_type = build_opaque_vector_type (intt,
12329 TYPE_VECTOR_SUBPARTS (type0));
12330 converted = 1;
12331 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12332 goto return_build_binary_op;
12333 }
12334 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12335 warning_at (location,
12336 OPT_Wfloat_equal,
12337 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12338 /* Result of comparison is always int,
12339 but don't convert the args to int! */
12340 build_type = integer_type_node;
12341 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12342 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12343 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12344 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12345 short_compare = 1;
12346 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12347 {
12348 maybe_warn_for_null_address (location, op0, code);
12349 result_type = type0;
12350 }
12351 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12352 {
12353 maybe_warn_for_null_address (location, op1, code);
12354 result_type = type1;
12355 }
12356 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12357 {
12358 tree tt0 = TREE_TYPE (type0);
12359 tree tt1 = TREE_TYPE (type1);
12360 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12361 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12362 addr_space_t as_common = ADDR_SPACE_GENERIC;
12363
12364 /* Anything compares with void *. void * compares with anything.
12365 Otherwise, the targets must be compatible
12366 and both must be object or both incomplete. */
12367 if (comp_target_types (location, type0, type1))
12368 result_type = common_pointer_type (type0, type1);
12369 else if (!addr_space_superset (as0, as1, &as_common))
12370 {
12371 error_at (location, "comparison of pointers to "
12372 "disjoint address spaces");
12373 return error_mark_node;
12374 }
12375 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12376 {
12377 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12378 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12379 "comparison of %<void *%> with function pointer");
12380 }
12381 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12382 {
12383 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12384 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12385 "comparison of %<void *%> with function pointer");
12386 }
12387 else
12388 /* Avoid warning about the volatile ObjC EH puts on decls. */
12389 if (!objc_ok)
12390 pedwarn (location, 0,
12391 "comparison of distinct pointer types lacks a cast");
12392
12393 if (result_type == NULL_TREE)
12394 {
12395 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12396 result_type = build_pointer_type
12397 (build_qualified_type (void_type_node, qual));
12398 }
12399 }
12400 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12401 {
12402 result_type = type0;
12403 pedwarn (location, 0, "comparison between pointer and integer");
12404 }
12405 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12406 {
12407 result_type = type1;
12408 pedwarn (location, 0, "comparison between pointer and integer");
12409 }
12410 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12411 || truth_value_p (TREE_CODE (orig_op0)))
12412 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12413 || truth_value_p (TREE_CODE (orig_op1))))
12414 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12415 break;
12416
12417 case LE_EXPR:
12418 case GE_EXPR:
12419 case LT_EXPR:
12420 case GT_EXPR:
12421 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12422 {
12423 tree intt;
12424 if (!vector_types_compatible_elements_p (type0, type1))
12425 {
12426 error_at (location, "comparing vectors with different "
12427 "element types");
12428 return error_mark_node;
12429 }
12430
12431 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12432 TYPE_VECTOR_SUBPARTS (type1)))
12433 {
12434 error_at (location, "comparing vectors with different "
12435 "number of elements");
12436 return error_mark_node;
12437 }
12438
12439 /* It's not precisely specified how the usual arithmetic
12440 conversions apply to the vector types. Here, we use
12441 the unsigned type if one of the operands is signed and
12442 the other one is unsigned. */
12443 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12444 {
12445 if (!TYPE_UNSIGNED (type0))
12446 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12447 else
12448 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12449 warning_at (location, OPT_Wsign_compare, "comparison between "
12450 "types %qT and %qT", type0, type1);
12451 }
12452
12453 /* Always construct signed integer vector type. */
12454 intt = c_common_type_for_size (GET_MODE_BITSIZE
12455 (SCALAR_TYPE_MODE
12456 (TREE_TYPE (type0))), 0);
12457 if (!intt)
12458 {
12459 error_at (location, "could not find an integer type "
12460 "of the same size as %qT",
12461 TREE_TYPE (type0));
12462 return error_mark_node;
12463 }
12464 result_type = build_opaque_vector_type (intt,
12465 TYPE_VECTOR_SUBPARTS (type0));
12466 converted = 1;
12467 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12468 goto return_build_binary_op;
12469 }
12470 build_type = integer_type_node;
12471 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12472 || code0 == FIXED_POINT_TYPE)
12473 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12474 || code1 == FIXED_POINT_TYPE))
12475 short_compare = 1;
12476 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12477 {
12478 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12479 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12480 addr_space_t as_common;
12481
12482 if (comp_target_types (location, type0, type1))
12483 {
12484 result_type = common_pointer_type (type0, type1);
12485 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12486 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12487 pedwarn_c99 (location, OPT_Wpedantic,
12488 "comparison of complete and incomplete pointers");
12489 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12490 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12491 "ordered comparisons of pointers to functions");
12492 else if (null_pointer_constant_p (orig_op0)
12493 || null_pointer_constant_p (orig_op1))
12494 warning_at (location, OPT_Wextra,
12495 "ordered comparison of pointer with null pointer");
12496
12497 }
12498 else if (!addr_space_superset (as0, as1, &as_common))
12499 {
12500 error_at (location, "comparison of pointers to "
12501 "disjoint address spaces");
12502 return error_mark_node;
12503 }
12504 else
12505 {
12506 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12507 result_type = build_pointer_type
12508 (build_qualified_type (void_type_node, qual));
12509 pedwarn (location, 0,
12510 "comparison of distinct pointer types lacks a cast");
12511 }
12512 }
12513 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12514 {
12515 result_type = type0;
12516 if (pedantic)
12517 pedwarn (location, OPT_Wpedantic,
12518 "ordered comparison of pointer with integer zero");
12519 else if (extra_warnings)
12520 warning_at (location, OPT_Wextra,
12521 "ordered comparison of pointer with integer zero");
12522 }
12523 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12524 {
12525 result_type = type1;
12526 if (pedantic)
12527 pedwarn (location, OPT_Wpedantic,
12528 "ordered comparison of pointer with integer zero");
12529 else if (extra_warnings)
12530 warning_at (location, OPT_Wextra,
12531 "ordered comparison of pointer with integer zero");
12532 }
12533 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12534 {
12535 result_type = type0;
12536 pedwarn (location, 0, "comparison between pointer and integer");
12537 }
12538 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12539 {
12540 result_type = type1;
12541 pedwarn (location, 0, "comparison between pointer and integer");
12542 }
12543
12544 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12545 && current_function_decl != NULL_TREE
12546 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12547 {
12548 op0 = save_expr (op0);
12549 op1 = save_expr (op1);
12550
12551 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12552 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12553 }
12554
12555 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12556 || truth_value_p (TREE_CODE (orig_op0)))
12557 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12558 || truth_value_p (TREE_CODE (orig_op1))))
12559 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12560 break;
12561
12562 case MIN_EXPR:
12563 case MAX_EXPR:
12564 /* Used for OpenMP atomics. */
12565 gcc_assert (flag_openmp);
12566 common = 1;
12567 break;
12568
12569 default:
12570 gcc_unreachable ();
12571 }
12572
12573 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12574 return error_mark_node;
12575
12576 if (gnu_vector_type_p (type0)
12577 && gnu_vector_type_p (type1)
12578 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12579 || !vector_types_compatible_elements_p (type0, type1)))
12580 {
12581 gcc_rich_location richloc (location);
12582 maybe_range_label_for_tree_type_mismatch
12583 label_for_op0 (orig_op0, orig_op1),
12584 label_for_op1 (orig_op1, orig_op0);
12585 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12586 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12587 binary_op_error (&richloc, code, type0, type1);
12588 return error_mark_node;
12589 }
12590
12591 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12592 || code0 == FIXED_POINT_TYPE
12593 || gnu_vector_type_p (type0))
12594 &&
12595 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12596 || code1 == FIXED_POINT_TYPE
12597 || gnu_vector_type_p (type1)))
12598 {
12599 bool first_complex = (code0 == COMPLEX_TYPE);
12600 bool second_complex = (code1 == COMPLEX_TYPE);
12601 int none_complex = (!first_complex && !second_complex);
12602
12603 if (shorten || common || short_compare)
12604 {
12605 result_type = c_common_type (type0, type1);
12606 do_warn_double_promotion (result_type, type0, type1,
12607 "implicit conversion from %qT to %qT "
12608 "to match other operand of binary "
12609 "expression",
12610 location);
12611 if (result_type == error_mark_node)
12612 return error_mark_node;
12613 }
12614
12615 if (first_complex != second_complex
12616 && (code == PLUS_EXPR
12617 || code == MINUS_EXPR
12618 || code == MULT_EXPR
12619 || (code == TRUNC_DIV_EXPR && first_complex))
12620 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12621 && flag_signed_zeros)
12622 {
12623 /* An operation on mixed real/complex operands must be
12624 handled specially, but the language-independent code can
12625 more easily optimize the plain complex arithmetic if
12626 -fno-signed-zeros. */
12627 tree real_type = TREE_TYPE (result_type);
12628 tree real, imag;
12629 if (type0 != orig_type0 || type1 != orig_type1)
12630 {
12631 gcc_assert (may_need_excess_precision && common);
12632 semantic_result_type = c_common_type (orig_type0, orig_type1);
12633 }
12634 if (first_complex)
12635 {
12636 if (TREE_TYPE (op0) != result_type)
12637 op0 = convert_and_check (location, result_type, op0);
12638 if (TREE_TYPE (op1) != real_type)
12639 op1 = convert_and_check (location, real_type, op1);
12640 }
12641 else
12642 {
12643 if (TREE_TYPE (op0) != real_type)
12644 op0 = convert_and_check (location, real_type, op0);
12645 if (TREE_TYPE (op1) != result_type)
12646 op1 = convert_and_check (location, result_type, op1);
12647 }
12648 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12649 return error_mark_node;
12650 if (first_complex)
12651 {
12652 op0 = save_expr (op0);
12653 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12654 op0, true);
12655 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12656 op0, true);
12657 switch (code)
12658 {
12659 case MULT_EXPR:
12660 case TRUNC_DIV_EXPR:
12661 op1 = save_expr (op1);
12662 imag = build2 (resultcode, real_type, imag, op1);
12663 /* Fall through. */
12664 case PLUS_EXPR:
12665 case MINUS_EXPR:
12666 real = build2 (resultcode, real_type, real, op1);
12667 break;
12668 default:
12669 gcc_unreachable();
12670 }
12671 }
12672 else
12673 {
12674 op1 = save_expr (op1);
12675 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12676 op1, true);
12677 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12678 op1, true);
12679 switch (code)
12680 {
12681 case MULT_EXPR:
12682 op0 = save_expr (op0);
12683 imag = build2 (resultcode, real_type, op0, imag);
12684 /* Fall through. */
12685 case PLUS_EXPR:
12686 real = build2 (resultcode, real_type, op0, real);
12687 break;
12688 case MINUS_EXPR:
12689 real = build2 (resultcode, real_type, op0, real);
12690 imag = build1 (NEGATE_EXPR, real_type, imag);
12691 break;
12692 default:
12693 gcc_unreachable();
12694 }
12695 }
12696 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12697 goto return_build_binary_op;
12698 }
12699
12700 /* For certain operations (which identify themselves by shorten != 0)
12701 if both args were extended from the same smaller type,
12702 do the arithmetic in that type and then extend.
12703
12704 shorten !=0 and !=1 indicates a bitwise operation.
12705 For them, this optimization is safe only if
12706 both args are zero-extended or both are sign-extended.
12707 Otherwise, we might change the result.
12708 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12709 but calculated in (unsigned short) it would be (unsigned short)-1. */
12710
12711 if (shorten && none_complex)
12712 {
12713 final_type = result_type;
12714 result_type = shorten_binary_op (result_type, op0, op1,
12715 shorten == -1);
12716 }
12717
12718 /* Shifts can be shortened if shifting right. */
12719
12720 if (short_shift)
12721 {
12722 int unsigned_arg;
12723 tree arg0 = get_narrower (op0, &unsigned_arg);
12724
12725 final_type = result_type;
12726
12727 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12728 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12729
12730 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12731 && tree_int_cst_sgn (op1) > 0
12732 /* We can shorten only if the shift count is less than the
12733 number of bits in the smaller type size. */
12734 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12735 /* We cannot drop an unsigned shift after sign-extension. */
12736 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12737 {
12738 /* Do an unsigned shift if the operand was zero-extended. */
12739 result_type
12740 = c_common_signed_or_unsigned_type (unsigned_arg,
12741 TREE_TYPE (arg0));
12742 /* Convert value-to-be-shifted to that type. */
12743 if (TREE_TYPE (op0) != result_type)
12744 op0 = convert (result_type, op0);
12745 converted = 1;
12746 }
12747 }
12748
12749 /* Comparison operations are shortened too but differently.
12750 They identify themselves by setting short_compare = 1. */
12751
12752 if (short_compare)
12753 {
12754 /* Don't write &op0, etc., because that would prevent op0
12755 from being kept in a register.
12756 Instead, make copies of the our local variables and
12757 pass the copies by reference, then copy them back afterward. */
12758 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12759 enum tree_code xresultcode = resultcode;
12760 tree val
12761 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12762 &xresultcode);
12763
12764 if (val != NULL_TREE)
12765 {
12766 ret = val;
12767 goto return_build_binary_op;
12768 }
12769
12770 op0 = xop0, op1 = xop1;
12771 converted = 1;
12772 resultcode = xresultcode;
12773
12774 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12775 {
12776 bool op0_maybe_const = true;
12777 bool op1_maybe_const = true;
12778 tree orig_op0_folded, orig_op1_folded;
12779
12780 if (in_late_binary_op)
12781 {
12782 orig_op0_folded = orig_op0;
12783 orig_op1_folded = orig_op1;
12784 }
12785 else
12786 {
12787 /* Fold for the sake of possible warnings, as in
12788 build_conditional_expr. This requires the
12789 "original" values to be folded, not just op0 and
12790 op1. */
12791 c_inhibit_evaluation_warnings++;
12792 op0 = c_fully_fold (op0, require_constant_value,
12793 &op0_maybe_const);
12794 op1 = c_fully_fold (op1, require_constant_value,
12795 &op1_maybe_const);
12796 c_inhibit_evaluation_warnings--;
12797 orig_op0_folded = c_fully_fold (orig_op0,
12798 require_constant_value,
12799 NULL);
12800 orig_op1_folded = c_fully_fold (orig_op1,
12801 require_constant_value,
12802 NULL);
12803 }
12804
12805 if (warn_sign_compare)
12806 warn_for_sign_compare (location, orig_op0_folded,
12807 orig_op1_folded, op0, op1,
12808 result_type, resultcode);
12809 if (!in_late_binary_op && !int_operands)
12810 {
12811 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12812 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12813 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12814 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12815 }
12816 }
12817 }
12818 }
12819
12820 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12821 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12822 Then the expression will be built.
12823 It will be given type FINAL_TYPE if that is nonzero;
12824 otherwise, it will be given type RESULT_TYPE. */
12825
12826 if (!result_type)
12827 {
12828 /* Favor showing any expression locations that are available. */
12829 op_location_t oploc (location, UNKNOWN_LOCATION);
12830 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12831 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12832 return error_mark_node;
12833 }
12834
12835 if (build_type == NULL_TREE)
12836 {
12837 build_type = result_type;
12838 if ((type0 != orig_type0 || type1 != orig_type1)
12839 && !boolean_op)
12840 {
12841 gcc_assert (may_need_excess_precision && common);
12842 semantic_result_type = c_common_type (orig_type0, orig_type1);
12843 }
12844 }
12845
12846 if (!converted)
12847 {
12848 op0 = ep_convert_and_check (location, result_type, op0,
12849 semantic_result_type);
12850 op1 = ep_convert_and_check (location, result_type, op1,
12851 semantic_result_type);
12852
12853 /* This can happen if one operand has a vector type, and the other
12854 has a different type. */
12855 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12856 return error_mark_node;
12857 }
12858
12859 if (sanitize_flags_p ((SANITIZE_SHIFT
12860 | SANITIZE_DIVIDE
12861 | SANITIZE_FLOAT_DIVIDE
12862 | SANITIZE_SI_OVERFLOW))
12863 && current_function_decl != NULL_TREE
12864 && (doing_div_or_mod || doing_shift)
12865 && !require_constant_value)
12866 {
12867 /* OP0 and/or OP1 might have side-effects. */
12868 op0 = save_expr (op0);
12869 op1 = save_expr (op1);
12870 op0 = c_fully_fold (op0, false, NULL);
12871 op1 = c_fully_fold (op1, false, NULL);
12872 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12873 | SANITIZE_FLOAT_DIVIDE
12874 | SANITIZE_SI_OVERFLOW))))
12875 instrument_expr = ubsan_instrument_division (location, op0, op1);
12876 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12877 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12878 }
12879
12880 /* Treat expressions in initializers specially as they can't trap. */
12881 if (int_const_or_overflow)
12882 ret = (require_constant_value
12883 ? fold_build2_initializer_loc (location, resultcode, build_type,
12884 op0, op1)
12885 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12886 else
12887 ret = build2 (resultcode, build_type, op0, op1);
12888 if (final_type != NULL_TREE)
12889 ret = convert (final_type, ret);
12890
12891 return_build_binary_op:
12892 gcc_assert (ret != error_mark_node);
12893 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12894 ret = (int_operands
12895 ? note_integer_operands (ret)
12896 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12897 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12898 && !in_late_binary_op)
12899 ret = note_integer_operands (ret);
12900 protected_set_expr_location (ret, location);
12901
12902 if (instrument_expr != NULL)
12903 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12904 instrument_expr, ret);
12905
12906 if (semantic_result_type)
12907 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12908 semantic_result_type, ret);
12909
12910 return ret;
12911 }
12912
12913
12914 /* Convert EXPR to be a truth-value, validating its type for this
12915 purpose. LOCATION is the source location for the expression. */
12916
12917 tree
12918 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12919 {
12920 bool int_const, int_operands;
12921
12922 switch (TREE_CODE (TREE_TYPE (expr)))
12923 {
12924 case ARRAY_TYPE:
12925 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12926 return error_mark_node;
12927
12928 case RECORD_TYPE:
12929 error_at (location, "used struct type value where scalar is required");
12930 return error_mark_node;
12931
12932 case UNION_TYPE:
12933 error_at (location, "used union type value where scalar is required");
12934 return error_mark_node;
12935
12936 case VOID_TYPE:
12937 error_at (location, "void value not ignored as it ought to be");
12938 return error_mark_node;
12939
12940 case POINTER_TYPE:
12941 if (reject_gcc_builtin (expr))
12942 return error_mark_node;
12943 break;
12944
12945 case FUNCTION_TYPE:
12946 gcc_unreachable ();
12947
12948 case VECTOR_TYPE:
12949 error_at (location, "used vector type where scalar is required");
12950 return error_mark_node;
12951
12952 default:
12953 break;
12954 }
12955
12956 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12957 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12958 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12959 {
12960 expr = remove_c_maybe_const_expr (expr);
12961 expr = build2 (NE_EXPR, integer_type_node, expr,
12962 convert (TREE_TYPE (expr), integer_zero_node));
12963 expr = note_integer_operands (expr);
12964 }
12965 else
12966 /* ??? Should we also give an error for vectors rather than leaving
12967 those to give errors later? */
12968 expr = c_common_truthvalue_conversion (location, expr);
12969
12970 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12971 {
12972 if (TREE_OVERFLOW (expr))
12973 return expr;
12974 else
12975 return note_integer_operands (expr);
12976 }
12977 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12978 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12979 return expr;
12980 }
12981 \f
12982
12983 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12984 required. */
12985
12986 tree
12987 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12988 {
12989 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12990 {
12991 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12992 /* Executing a compound literal inside a function reinitializes
12993 it. */
12994 if (!TREE_STATIC (decl))
12995 *se = true;
12996 return decl;
12997 }
12998 else
12999 return expr;
13000 }
13001 \f
13002 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13003 statement. LOC is the location of the construct. */
13004
13005 tree
13006 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13007 tree clauses)
13008 {
13009 body = c_end_compound_stmt (loc, body, true);
13010
13011 tree stmt = make_node (code);
13012 TREE_TYPE (stmt) = void_type_node;
13013 OMP_BODY (stmt) = body;
13014 OMP_CLAUSES (stmt) = clauses;
13015 SET_EXPR_LOCATION (stmt, loc);
13016
13017 return add_stmt (stmt);
13018 }
13019
13020 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13021 statement. LOC is the location of the OACC_DATA. */
13022
13023 tree
13024 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13025 {
13026 tree stmt;
13027
13028 block = c_end_compound_stmt (loc, block, true);
13029
13030 stmt = make_node (OACC_DATA);
13031 TREE_TYPE (stmt) = void_type_node;
13032 OACC_DATA_CLAUSES (stmt) = clauses;
13033 OACC_DATA_BODY (stmt) = block;
13034 SET_EXPR_LOCATION (stmt, loc);
13035
13036 return add_stmt (stmt);
13037 }
13038
13039 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13040 statement. LOC is the location of the OACC_HOST_DATA. */
13041
13042 tree
13043 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13044 {
13045 tree stmt;
13046
13047 block = c_end_compound_stmt (loc, block, true);
13048
13049 stmt = make_node (OACC_HOST_DATA);
13050 TREE_TYPE (stmt) = void_type_node;
13051 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13052 OACC_HOST_DATA_BODY (stmt) = block;
13053 SET_EXPR_LOCATION (stmt, loc);
13054
13055 return add_stmt (stmt);
13056 }
13057
13058 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13059
13060 tree
13061 c_begin_omp_parallel (void)
13062 {
13063 tree block;
13064
13065 keep_next_level ();
13066 block = c_begin_compound_stmt (true);
13067
13068 return block;
13069 }
13070
13071 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13072 statement. LOC is the location of the OMP_PARALLEL. */
13073
13074 tree
13075 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13076 {
13077 tree stmt;
13078
13079 block = c_end_compound_stmt (loc, block, true);
13080
13081 stmt = make_node (OMP_PARALLEL);
13082 TREE_TYPE (stmt) = void_type_node;
13083 OMP_PARALLEL_CLAUSES (stmt) = clauses;
13084 OMP_PARALLEL_BODY (stmt) = block;
13085 SET_EXPR_LOCATION (stmt, loc);
13086
13087 return add_stmt (stmt);
13088 }
13089
13090 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13091
13092 tree
13093 c_begin_omp_task (void)
13094 {
13095 tree block;
13096
13097 keep_next_level ();
13098 block = c_begin_compound_stmt (true);
13099
13100 return block;
13101 }
13102
13103 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13104 statement. LOC is the location of the #pragma. */
13105
13106 tree
13107 c_finish_omp_task (location_t loc, tree clauses, tree block)
13108 {
13109 tree stmt;
13110
13111 block = c_end_compound_stmt (loc, block, true);
13112
13113 stmt = make_node (OMP_TASK);
13114 TREE_TYPE (stmt) = void_type_node;
13115 OMP_TASK_CLAUSES (stmt) = clauses;
13116 OMP_TASK_BODY (stmt) = block;
13117 SET_EXPR_LOCATION (stmt, loc);
13118
13119 return add_stmt (stmt);
13120 }
13121
13122 /* Generate GOMP_cancel call for #pragma omp cancel. */
13123
13124 void
13125 c_finish_omp_cancel (location_t loc, tree clauses)
13126 {
13127 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13128 int mask = 0;
13129 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13130 mask = 1;
13131 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13132 mask = 2;
13133 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13134 mask = 4;
13135 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13136 mask = 8;
13137 else
13138 {
13139 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13140 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13141 "clauses");
13142 return;
13143 }
13144 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13145 if (ifc != NULL_TREE)
13146 {
13147 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13148 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13149 error_at (OMP_CLAUSE_LOCATION (ifc),
13150 "expected %<cancel%> %<if%> clause modifier");
13151 else
13152 {
13153 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13154 if (ifc2 != NULL_TREE)
13155 {
13156 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13157 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13158 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13159 error_at (OMP_CLAUSE_LOCATION (ifc2),
13160 "expected %<cancel%> %<if%> clause modifier");
13161 }
13162 }
13163
13164 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13165 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13166 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13167 build_zero_cst (type));
13168 }
13169 else
13170 ifc = boolean_true_node;
13171 tree stmt = build_call_expr_loc (loc, fn, 2,
13172 build_int_cst (integer_type_node, mask),
13173 ifc);
13174 add_stmt (stmt);
13175 }
13176
13177 /* Generate GOMP_cancellation_point call for
13178 #pragma omp cancellation point. */
13179
13180 void
13181 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13182 {
13183 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13184 int mask = 0;
13185 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13186 mask = 1;
13187 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13188 mask = 2;
13189 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13190 mask = 4;
13191 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13192 mask = 8;
13193 else
13194 {
13195 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13196 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13197 "clauses");
13198 return;
13199 }
13200 tree stmt = build_call_expr_loc (loc, fn, 1,
13201 build_int_cst (integer_type_node, mask));
13202 add_stmt (stmt);
13203 }
13204
13205 /* Helper function for handle_omp_array_sections. Called recursively
13206 to handle multiple array-section-subscripts. C is the clause,
13207 T current expression (initially OMP_CLAUSE_DECL), which is either
13208 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13209 expression if specified, TREE_VALUE length expression if specified,
13210 TREE_CHAIN is what it has been specified after, or some decl.
13211 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13212 set to true if any of the array-section-subscript could have length
13213 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13214 first array-section-subscript which is known not to have length
13215 of one. Given say:
13216 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13217 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13218 all are or may have length of 1, array-section-subscript [:2] is the
13219 first one known not to have length 1. For array-section-subscript
13220 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13221 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13222 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13223 case though, as some lengths could be zero. */
13224
13225 static tree
13226 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13227 bool &maybe_zero_len, unsigned int &first_non_one,
13228 enum c_omp_region_type ort)
13229 {
13230 tree ret, low_bound, length, type;
13231 if (TREE_CODE (t) != TREE_LIST)
13232 {
13233 if (error_operand_p (t))
13234 return error_mark_node;
13235 ret = t;
13236 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13237 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13238 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13239 {
13240 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13241 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13242 return error_mark_node;
13243 }
13244 while (TREE_CODE (t) == INDIRECT_REF)
13245 {
13246 t = TREE_OPERAND (t, 0);
13247 STRIP_NOPS (t);
13248 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13249 t = TREE_OPERAND (t, 0);
13250 }
13251 while (TREE_CODE (t) == COMPOUND_EXPR)
13252 {
13253 t = TREE_OPERAND (t, 1);
13254 STRIP_NOPS (t);
13255 }
13256 if (TREE_CODE (t) == COMPONENT_REF
13257 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13258 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13259 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13260 {
13261 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13262 {
13263 error_at (OMP_CLAUSE_LOCATION (c),
13264 "bit-field %qE in %qs clause",
13265 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13266 return error_mark_node;
13267 }
13268 while (TREE_CODE (t) == COMPONENT_REF)
13269 {
13270 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13271 {
13272 error_at (OMP_CLAUSE_LOCATION (c),
13273 "%qE is a member of a union", t);
13274 return error_mark_node;
13275 }
13276 t = TREE_OPERAND (t, 0);
13277 while (TREE_CODE (t) == MEM_REF
13278 || TREE_CODE (t) == INDIRECT_REF
13279 || TREE_CODE (t) == ARRAY_REF)
13280 {
13281 t = TREE_OPERAND (t, 0);
13282 STRIP_NOPS (t);
13283 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13284 t = TREE_OPERAND (t, 0);
13285 }
13286 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13287 {
13288 if (maybe_ne (mem_ref_offset (t), 0))
13289 error_at (OMP_CLAUSE_LOCATION (c),
13290 "cannot dereference %qE in %qs clause", t,
13291 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13292 else
13293 t = TREE_OPERAND (t, 0);
13294 }
13295 }
13296 }
13297 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13298 {
13299 if (DECL_P (t))
13300 error_at (OMP_CLAUSE_LOCATION (c),
13301 "%qD is not a variable in %qs clause", t,
13302 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13303 else
13304 error_at (OMP_CLAUSE_LOCATION (c),
13305 "%qE is not a variable in %qs clause", t,
13306 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13307 return error_mark_node;
13308 }
13309 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13310 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13311 && TYPE_ATOMIC (TREE_TYPE (t)))
13312 {
13313 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13314 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13315 return error_mark_node;
13316 }
13317 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13318 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13319 && VAR_P (t)
13320 && DECL_THREAD_LOCAL_P (t))
13321 {
13322 error_at (OMP_CLAUSE_LOCATION (c),
13323 "%qD is threadprivate variable in %qs clause", t,
13324 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13325 return error_mark_node;
13326 }
13327 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13328 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13329 && TYPE_ATOMIC (TREE_TYPE (t))
13330 && POINTER_TYPE_P (TREE_TYPE (t)))
13331 {
13332 /* If the array section is pointer based and the pointer
13333 itself is _Atomic qualified, we need to atomically load
13334 the pointer. */
13335 c_expr expr;
13336 memset (&expr, 0, sizeof (expr));
13337 expr.value = ret;
13338 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13339 expr, false, false);
13340 ret = expr.value;
13341 }
13342 return ret;
13343 }
13344
13345 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13346 maybe_zero_len, first_non_one, ort);
13347 if (ret == error_mark_node || ret == NULL_TREE)
13348 return ret;
13349
13350 type = TREE_TYPE (ret);
13351 low_bound = TREE_PURPOSE (t);
13352 length = TREE_VALUE (t);
13353
13354 if (low_bound == error_mark_node || length == error_mark_node)
13355 return error_mark_node;
13356
13357 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13358 {
13359 error_at (OMP_CLAUSE_LOCATION (c),
13360 "low bound %qE of array section does not have integral type",
13361 low_bound);
13362 return error_mark_node;
13363 }
13364 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13365 {
13366 error_at (OMP_CLAUSE_LOCATION (c),
13367 "length %qE of array section does not have integral type",
13368 length);
13369 return error_mark_node;
13370 }
13371 if (low_bound
13372 && TREE_CODE (low_bound) == INTEGER_CST
13373 && TYPE_PRECISION (TREE_TYPE (low_bound))
13374 > TYPE_PRECISION (sizetype))
13375 low_bound = fold_convert (sizetype, low_bound);
13376 if (length
13377 && TREE_CODE (length) == INTEGER_CST
13378 && TYPE_PRECISION (TREE_TYPE (length))
13379 > TYPE_PRECISION (sizetype))
13380 length = fold_convert (sizetype, length);
13381 if (low_bound == NULL_TREE)
13382 low_bound = integer_zero_node;
13383 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13384 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13385 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13386 {
13387 if (length != integer_one_node)
13388 {
13389 error_at (OMP_CLAUSE_LOCATION (c),
13390 "expected single pointer in %qs clause",
13391 user_omp_clause_code_name (c, ort == C_ORT_ACC));
13392 return error_mark_node;
13393 }
13394 }
13395 if (length != NULL_TREE)
13396 {
13397 if (!integer_nonzerop (length))
13398 {
13399 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13400 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13401 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13402 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13403 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13404 {
13405 if (integer_zerop (length))
13406 {
13407 error_at (OMP_CLAUSE_LOCATION (c),
13408 "zero length array section in %qs clause",
13409 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13410 return error_mark_node;
13411 }
13412 }
13413 else
13414 maybe_zero_len = true;
13415 }
13416 if (first_non_one == types.length ()
13417 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13418 first_non_one++;
13419 }
13420 if (TREE_CODE (type) == ARRAY_TYPE)
13421 {
13422 if (length == NULL_TREE
13423 && (TYPE_DOMAIN (type) == NULL_TREE
13424 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13425 {
13426 error_at (OMP_CLAUSE_LOCATION (c),
13427 "for unknown bound array type length expression must "
13428 "be specified");
13429 return error_mark_node;
13430 }
13431 if (TREE_CODE (low_bound) == INTEGER_CST
13432 && tree_int_cst_sgn (low_bound) == -1)
13433 {
13434 error_at (OMP_CLAUSE_LOCATION (c),
13435 "negative low bound in array section in %qs clause",
13436 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13437 return error_mark_node;
13438 }
13439 if (length != NULL_TREE
13440 && TREE_CODE (length) == INTEGER_CST
13441 && tree_int_cst_sgn (length) == -1)
13442 {
13443 error_at (OMP_CLAUSE_LOCATION (c),
13444 "negative length in array section in %qs clause",
13445 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13446 return error_mark_node;
13447 }
13448 if (TYPE_DOMAIN (type)
13449 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13450 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13451 == INTEGER_CST)
13452 {
13453 tree size
13454 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13455 size = size_binop (PLUS_EXPR, size, size_one_node);
13456 if (TREE_CODE (low_bound) == INTEGER_CST)
13457 {
13458 if (tree_int_cst_lt (size, low_bound))
13459 {
13460 error_at (OMP_CLAUSE_LOCATION (c),
13461 "low bound %qE above array section size "
13462 "in %qs clause", low_bound,
13463 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13464 return error_mark_node;
13465 }
13466 if (tree_int_cst_equal (size, low_bound))
13467 {
13468 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13469 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13470 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13471 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13472 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13473 {
13474 error_at (OMP_CLAUSE_LOCATION (c),
13475 "zero length array section in %qs clause",
13476 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13477 return error_mark_node;
13478 }
13479 maybe_zero_len = true;
13480 }
13481 else if (length == NULL_TREE
13482 && first_non_one == types.length ()
13483 && tree_int_cst_equal
13484 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13485 low_bound))
13486 first_non_one++;
13487 }
13488 else if (length == NULL_TREE)
13489 {
13490 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13491 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13492 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13493 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13494 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13495 maybe_zero_len = true;
13496 if (first_non_one == types.length ())
13497 first_non_one++;
13498 }
13499 if (length && TREE_CODE (length) == INTEGER_CST)
13500 {
13501 if (tree_int_cst_lt (size, length))
13502 {
13503 error_at (OMP_CLAUSE_LOCATION (c),
13504 "length %qE above array section size "
13505 "in %qs clause", length,
13506 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13507 return error_mark_node;
13508 }
13509 if (TREE_CODE (low_bound) == INTEGER_CST)
13510 {
13511 tree lbpluslen
13512 = size_binop (PLUS_EXPR,
13513 fold_convert (sizetype, low_bound),
13514 fold_convert (sizetype, length));
13515 if (TREE_CODE (lbpluslen) == INTEGER_CST
13516 && tree_int_cst_lt (size, lbpluslen))
13517 {
13518 error_at (OMP_CLAUSE_LOCATION (c),
13519 "high bound %qE above array section size "
13520 "in %qs clause", lbpluslen,
13521 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13522 return error_mark_node;
13523 }
13524 }
13525 }
13526 }
13527 else if (length == NULL_TREE)
13528 {
13529 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13530 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13531 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13532 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13533 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13534 maybe_zero_len = true;
13535 if (first_non_one == types.length ())
13536 first_non_one++;
13537 }
13538
13539 /* For [lb:] we will need to evaluate lb more than once. */
13540 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13541 {
13542 tree lb = save_expr (low_bound);
13543 if (lb != low_bound)
13544 {
13545 TREE_PURPOSE (t) = lb;
13546 low_bound = lb;
13547 }
13548 }
13549 }
13550 else if (TREE_CODE (type) == POINTER_TYPE)
13551 {
13552 if (length == NULL_TREE)
13553 {
13554 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13555 error_at (OMP_CLAUSE_LOCATION (c),
13556 "for array function parameter length expression "
13557 "must be specified");
13558 else
13559 error_at (OMP_CLAUSE_LOCATION (c),
13560 "for pointer type length expression must be specified");
13561 return error_mark_node;
13562 }
13563 if (length != NULL_TREE
13564 && TREE_CODE (length) == INTEGER_CST
13565 && tree_int_cst_sgn (length) == -1)
13566 {
13567 error_at (OMP_CLAUSE_LOCATION (c),
13568 "negative length in array section in %qs clause",
13569 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13570 return error_mark_node;
13571 }
13572 /* If there is a pointer type anywhere but in the very first
13573 array-section-subscript, the array section could be non-contiguous. */
13574 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13575 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13576 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13577 {
13578 /* If any prior dimension has a non-one length, then deem this
13579 array section as non-contiguous. */
13580 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
13581 d = TREE_CHAIN (d))
13582 {
13583 tree d_length = TREE_VALUE (d);
13584 if (d_length == NULL_TREE || !integer_onep (d_length))
13585 {
13586 error_at (OMP_CLAUSE_LOCATION (c),
13587 "array section is not contiguous in %qs clause",
13588 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13589 return error_mark_node;
13590 }
13591 }
13592 }
13593 }
13594 else
13595 {
13596 error_at (OMP_CLAUSE_LOCATION (c),
13597 "%qE does not have pointer or array type", ret);
13598 return error_mark_node;
13599 }
13600 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13601 types.safe_push (TREE_TYPE (ret));
13602 /* We will need to evaluate lb more than once. */
13603 tree lb = save_expr (low_bound);
13604 if (lb != low_bound)
13605 {
13606 TREE_PURPOSE (t) = lb;
13607 low_bound = lb;
13608 }
13609 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13610 return ret;
13611 }
13612
13613 /* Handle array sections for clause C. */
13614
13615 static bool
13616 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13617 {
13618 bool maybe_zero_len = false;
13619 unsigned int first_non_one = 0;
13620 auto_vec<tree, 10> types;
13621 tree *tp = &OMP_CLAUSE_DECL (c);
13622 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13623 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13624 && TREE_CODE (*tp) == TREE_LIST
13625 && TREE_PURPOSE (*tp)
13626 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13627 tp = &TREE_VALUE (*tp);
13628 tree first = handle_omp_array_sections_1 (c, *tp, types,
13629 maybe_zero_len, first_non_one,
13630 ort);
13631 if (first == error_mark_node)
13632 return true;
13633 if (first == NULL_TREE)
13634 return false;
13635 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13636 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13637 {
13638 tree t = *tp;
13639 tree tem = NULL_TREE;
13640 /* Need to evaluate side effects in the length expressions
13641 if any. */
13642 while (TREE_CODE (t) == TREE_LIST)
13643 {
13644 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13645 {
13646 if (tem == NULL_TREE)
13647 tem = TREE_VALUE (t);
13648 else
13649 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13650 TREE_VALUE (t), tem);
13651 }
13652 t = TREE_CHAIN (t);
13653 }
13654 if (tem)
13655 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13656 first = c_fully_fold (first, false, NULL, true);
13657 *tp = first;
13658 }
13659 else
13660 {
13661 unsigned int num = types.length (), i;
13662 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13663 tree condition = NULL_TREE;
13664
13665 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13666 maybe_zero_len = true;
13667
13668 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13669 t = TREE_CHAIN (t))
13670 {
13671 tree low_bound = TREE_PURPOSE (t);
13672 tree length = TREE_VALUE (t);
13673
13674 i--;
13675 if (low_bound
13676 && TREE_CODE (low_bound) == INTEGER_CST
13677 && TYPE_PRECISION (TREE_TYPE (low_bound))
13678 > TYPE_PRECISION (sizetype))
13679 low_bound = fold_convert (sizetype, low_bound);
13680 if (length
13681 && TREE_CODE (length) == INTEGER_CST
13682 && TYPE_PRECISION (TREE_TYPE (length))
13683 > TYPE_PRECISION (sizetype))
13684 length = fold_convert (sizetype, length);
13685 if (low_bound == NULL_TREE)
13686 low_bound = integer_zero_node;
13687 if (!maybe_zero_len && i > first_non_one)
13688 {
13689 if (integer_nonzerop (low_bound))
13690 goto do_warn_noncontiguous;
13691 if (length != NULL_TREE
13692 && TREE_CODE (length) == INTEGER_CST
13693 && TYPE_DOMAIN (types[i])
13694 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13695 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13696 == INTEGER_CST)
13697 {
13698 tree size;
13699 size = size_binop (PLUS_EXPR,
13700 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13701 size_one_node);
13702 if (!tree_int_cst_equal (length, size))
13703 {
13704 do_warn_noncontiguous:
13705 error_at (OMP_CLAUSE_LOCATION (c),
13706 "array section is not contiguous in %qs "
13707 "clause",
13708 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13709 return true;
13710 }
13711 }
13712 if (length != NULL_TREE
13713 && TREE_SIDE_EFFECTS (length))
13714 {
13715 if (side_effects == NULL_TREE)
13716 side_effects = length;
13717 else
13718 side_effects = build2 (COMPOUND_EXPR,
13719 TREE_TYPE (side_effects),
13720 length, side_effects);
13721 }
13722 }
13723 else
13724 {
13725 tree l;
13726
13727 if (i > first_non_one
13728 && ((length && integer_nonzerop (length))
13729 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13730 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13731 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13732 continue;
13733 if (length)
13734 l = fold_convert (sizetype, length);
13735 else
13736 {
13737 l = size_binop (PLUS_EXPR,
13738 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13739 size_one_node);
13740 l = size_binop (MINUS_EXPR, l,
13741 fold_convert (sizetype, low_bound));
13742 }
13743 if (i > first_non_one)
13744 {
13745 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13746 size_zero_node);
13747 if (condition == NULL_TREE)
13748 condition = l;
13749 else
13750 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13751 l, condition);
13752 }
13753 else if (size == NULL_TREE)
13754 {
13755 size = size_in_bytes (TREE_TYPE (types[i]));
13756 tree eltype = TREE_TYPE (types[num - 1]);
13757 while (TREE_CODE (eltype) == ARRAY_TYPE)
13758 eltype = TREE_TYPE (eltype);
13759 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13760 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13761 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13762 {
13763 if (integer_zerop (size)
13764 || integer_zerop (size_in_bytes (eltype)))
13765 {
13766 error_at (OMP_CLAUSE_LOCATION (c),
13767 "zero length array section in %qs clause",
13768 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13769 return error_mark_node;
13770 }
13771 size = size_binop (EXACT_DIV_EXPR, size,
13772 size_in_bytes (eltype));
13773 }
13774 size = size_binop (MULT_EXPR, size, l);
13775 if (condition)
13776 size = fold_build3 (COND_EXPR, sizetype, condition,
13777 size, size_zero_node);
13778 }
13779 else
13780 size = size_binop (MULT_EXPR, size, l);
13781 }
13782 }
13783 if (side_effects)
13784 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13785 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13786 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13787 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13788 {
13789 size = size_binop (MINUS_EXPR, size, size_one_node);
13790 size = c_fully_fold (size, false, NULL);
13791 size = save_expr (size);
13792 tree index_type = build_index_type (size);
13793 tree eltype = TREE_TYPE (first);
13794 while (TREE_CODE (eltype) == ARRAY_TYPE)
13795 eltype = TREE_TYPE (eltype);
13796 tree type = build_array_type (eltype, index_type);
13797 tree ptype = build_pointer_type (eltype);
13798 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13799 t = build_fold_addr_expr (t);
13800 tree t2 = build_fold_addr_expr (first);
13801 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13802 ptrdiff_type_node, t2);
13803 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13804 ptrdiff_type_node, t2,
13805 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13806 ptrdiff_type_node, t));
13807 t2 = c_fully_fold (t2, false, NULL);
13808 if (tree_fits_shwi_p (t2))
13809 t = build2 (MEM_REF, type, t,
13810 build_int_cst (ptype, tree_to_shwi (t2)));
13811 else
13812 {
13813 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13814 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13815 TREE_TYPE (t), t, t2);
13816 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13817 }
13818 OMP_CLAUSE_DECL (c) = t;
13819 return false;
13820 }
13821 first = c_fully_fold (first, false, NULL);
13822 OMP_CLAUSE_DECL (c) = first;
13823 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13824 return false;
13825 if (size)
13826 size = c_fully_fold (size, false, NULL);
13827 OMP_CLAUSE_SIZE (c) = size;
13828 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13829 || (TREE_CODE (t) == COMPONENT_REF
13830 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13831 return false;
13832 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13833 switch (OMP_CLAUSE_MAP_KIND (c))
13834 {
13835 case GOMP_MAP_ALLOC:
13836 case GOMP_MAP_IF_PRESENT:
13837 case GOMP_MAP_TO:
13838 case GOMP_MAP_FROM:
13839 case GOMP_MAP_TOFROM:
13840 case GOMP_MAP_ALWAYS_TO:
13841 case GOMP_MAP_ALWAYS_FROM:
13842 case GOMP_MAP_ALWAYS_TOFROM:
13843 case GOMP_MAP_RELEASE:
13844 case GOMP_MAP_DELETE:
13845 case GOMP_MAP_FORCE_TO:
13846 case GOMP_MAP_FORCE_FROM:
13847 case GOMP_MAP_FORCE_TOFROM:
13848 case GOMP_MAP_FORCE_PRESENT:
13849 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13850 break;
13851 default:
13852 break;
13853 }
13854 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13855 if (TREE_CODE (t) == COMPONENT_REF)
13856 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
13857 else
13858 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13859 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
13860 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13861 && !c_mark_addressable (t))
13862 return false;
13863 OMP_CLAUSE_DECL (c2) = t;
13864 t = build_fold_addr_expr (first);
13865 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13866 tree ptr = OMP_CLAUSE_DECL (c2);
13867 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13868 ptr = build_fold_addr_expr (ptr);
13869 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13870 ptrdiff_type_node, t,
13871 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13872 ptrdiff_type_node, ptr));
13873 t = c_fully_fold (t, false, NULL);
13874 OMP_CLAUSE_SIZE (c2) = t;
13875 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13876 OMP_CLAUSE_CHAIN (c) = c2;
13877 }
13878 return false;
13879 }
13880
13881 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13882 an inline call. But, remap
13883 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13884 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13885
13886 static tree
13887 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13888 tree decl, tree placeholder)
13889 {
13890 copy_body_data id;
13891 hash_map<tree, tree> decl_map;
13892
13893 decl_map.put (omp_decl1, placeholder);
13894 decl_map.put (omp_decl2, decl);
13895 memset (&id, 0, sizeof (id));
13896 id.src_fn = DECL_CONTEXT (omp_decl1);
13897 id.dst_fn = current_function_decl;
13898 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13899 id.decl_map = &decl_map;
13900
13901 id.copy_decl = copy_decl_no_change;
13902 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13903 id.transform_new_cfg = true;
13904 id.transform_return_to_modify = false;
13905 id.eh_lp_nr = 0;
13906 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13907 return stmt;
13908 }
13909
13910 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13911 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13912
13913 static tree
13914 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13915 {
13916 if (*tp == (tree) data)
13917 return *tp;
13918 return NULL_TREE;
13919 }
13920
13921 /* Similarly, but also walk aggregate fields. */
13922
13923 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13924
13925 static tree
13926 c_find_omp_var_r (tree *tp, int *, void *data)
13927 {
13928 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13929 return *tp;
13930 if (RECORD_OR_UNION_TYPE_P (*tp))
13931 {
13932 tree field;
13933 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13934
13935 for (field = TYPE_FIELDS (*tp); field;
13936 field = DECL_CHAIN (field))
13937 if (TREE_CODE (field) == FIELD_DECL)
13938 {
13939 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13940 c_find_omp_var_r, data, pset);
13941 if (ret)
13942 return ret;
13943 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13944 if (ret)
13945 return ret;
13946 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13947 pset);
13948 if (ret)
13949 return ret;
13950 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13951 if (ret)
13952 return ret;
13953 }
13954 }
13955 else if (INTEGRAL_TYPE_P (*tp))
13956 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13957 ((struct c_find_omp_var_s *) data)->pset);
13958 return NULL_TREE;
13959 }
13960
13961 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13962 and clauses containing them should be removed. */
13963
13964 static bool
13965 c_omp_finish_iterators (tree iter)
13966 {
13967 bool ret = false;
13968 for (tree it = iter; it; it = TREE_CHAIN (it))
13969 {
13970 tree var = TREE_VEC_ELT (it, 0);
13971 tree begin = TREE_VEC_ELT (it, 1);
13972 tree end = TREE_VEC_ELT (it, 2);
13973 tree step = TREE_VEC_ELT (it, 3);
13974 tree orig_step;
13975 tree type = TREE_TYPE (var);
13976 location_t loc = DECL_SOURCE_LOCATION (var);
13977 if (type == error_mark_node)
13978 {
13979 ret = true;
13980 continue;
13981 }
13982 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13983 {
13984 error_at (loc, "iterator %qD has neither integral nor pointer type",
13985 var);
13986 ret = true;
13987 continue;
13988 }
13989 else if (TYPE_ATOMIC (type))
13990 {
13991 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13992 ret = true;
13993 continue;
13994 }
13995 else if (TYPE_READONLY (type))
13996 {
13997 error_at (loc, "iterator %qD has const qualified type", var);
13998 ret = true;
13999 continue;
14000 }
14001 else if (step == error_mark_node
14002 || TREE_TYPE (step) == error_mark_node)
14003 {
14004 ret = true;
14005 continue;
14006 }
14007 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14008 {
14009 error_at (EXPR_LOC_OR_LOC (step, loc),
14010 "iterator step with non-integral type");
14011 ret = true;
14012 continue;
14013 }
14014 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14015 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14016 orig_step = save_expr (c_fully_fold (step, false, NULL));
14017 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14018 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14019 if (POINTER_TYPE_P (type))
14020 {
14021 begin = save_expr (begin);
14022 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14023 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14024 fold_convert (sizetype, step),
14025 fold_convert (sizetype, begin));
14026 step = fold_convert (ssizetype, step);
14027 }
14028 if (integer_zerop (step))
14029 {
14030 error_at (loc, "iterator %qD has zero step", var);
14031 ret = true;
14032 continue;
14033 }
14034
14035 if (begin == error_mark_node
14036 || end == error_mark_node
14037 || step == error_mark_node
14038 || orig_step == error_mark_node)
14039 {
14040 ret = true;
14041 continue;
14042 }
14043 hash_set<tree> pset;
14044 tree it2;
14045 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14046 {
14047 tree var2 = TREE_VEC_ELT (it2, 0);
14048 tree begin2 = TREE_VEC_ELT (it2, 1);
14049 tree end2 = TREE_VEC_ELT (it2, 2);
14050 tree step2 = TREE_VEC_ELT (it2, 3);
14051 tree type2 = TREE_TYPE (var2);
14052 location_t loc2 = DECL_SOURCE_LOCATION (var2);
14053 struct c_find_omp_var_s data = { var, &pset };
14054 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14055 {
14056 error_at (loc2,
14057 "type of iterator %qD refers to outer iterator %qD",
14058 var2, var);
14059 break;
14060 }
14061 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14062 {
14063 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14064 "begin expression refers to outer iterator %qD", var);
14065 break;
14066 }
14067 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14068 {
14069 error_at (EXPR_LOC_OR_LOC (end2, loc2),
14070 "end expression refers to outer iterator %qD", var);
14071 break;
14072 }
14073 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14074 {
14075 error_at (EXPR_LOC_OR_LOC (step2, loc2),
14076 "step expression refers to outer iterator %qD", var);
14077 break;
14078 }
14079 }
14080 if (it2)
14081 {
14082 ret = true;
14083 continue;
14084 }
14085 TREE_VEC_ELT (it, 1) = begin;
14086 TREE_VEC_ELT (it, 2) = end;
14087 TREE_VEC_ELT (it, 3) = step;
14088 TREE_VEC_ELT (it, 4) = orig_step;
14089 }
14090 return ret;
14091 }
14092
14093 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14094 Return true if an error has been detected. */
14095
14096 static bool
14097 c_oacc_check_attachments (tree c)
14098 {
14099 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14100 return false;
14101
14102 /* OpenACC attach / detach clauses must be pointers. */
14103 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14104 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14105 {
14106 tree t = OMP_CLAUSE_DECL (c);
14107
14108 while (TREE_CODE (t) == TREE_LIST)
14109 t = TREE_CHAIN (t);
14110
14111 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14112 {
14113 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14114 user_omp_clause_code_name (c, true));
14115 return true;
14116 }
14117 }
14118
14119 return false;
14120 }
14121
14122 /* For all elements of CLAUSES, validate them against their constraints.
14123 Remove any elements from the list that are invalid. */
14124
14125 tree
14126 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14127 {
14128 bitmap_head generic_head, firstprivate_head, lastprivate_head;
14129 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14130 bitmap_head oacc_reduction_head, is_on_device_head;
14131 tree c, t, type, *pc;
14132 tree simdlen = NULL_TREE, safelen = NULL_TREE;
14133 bool branch_seen = false;
14134 bool copyprivate_seen = false;
14135 bool mergeable_seen = false;
14136 tree *detach_seen = NULL;
14137 bool linear_variable_step_check = false;
14138 tree *nowait_clause = NULL;
14139 tree ordered_clause = NULL_TREE;
14140 tree schedule_clause = NULL_TREE;
14141 bool oacc_async = false;
14142 bool indir_component_ref_p = false;
14143 tree last_iterators = NULL_TREE;
14144 bool last_iterators_remove = false;
14145 tree *nogroup_seen = NULL;
14146 tree *order_clause = NULL;
14147 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14148 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14149 int reduction_seen = 0;
14150 bool allocate_seen = false;
14151 bool implicit_moved = false;
14152 bool target_in_reduction_seen = false;
14153
14154 bitmap_obstack_initialize (NULL);
14155 bitmap_initialize (&generic_head, &bitmap_default_obstack);
14156 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14157 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14158 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14159 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14160 bitmap_initialize (&map_head, &bitmap_default_obstack);
14161 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14162 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14163 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14164 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14165 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14166 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14167
14168 if (ort & C_ORT_ACC)
14169 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14170 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14171 {
14172 oacc_async = true;
14173 break;
14174 }
14175
14176 for (pc = &clauses, c = clauses; c ; c = *pc)
14177 {
14178 bool remove = false;
14179 bool need_complete = false;
14180 bool need_implicitly_determined = false;
14181
14182 switch (OMP_CLAUSE_CODE (c))
14183 {
14184 case OMP_CLAUSE_SHARED:
14185 need_implicitly_determined = true;
14186 goto check_dup_generic;
14187
14188 case OMP_CLAUSE_PRIVATE:
14189 need_complete = true;
14190 need_implicitly_determined = true;
14191 goto check_dup_generic;
14192
14193 case OMP_CLAUSE_REDUCTION:
14194 if (reduction_seen == 0)
14195 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14196 else if (reduction_seen != -2
14197 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14198 ? -1 : 1))
14199 {
14200 error_at (OMP_CLAUSE_LOCATION (c),
14201 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14202 "on the same construct");
14203 reduction_seen = -2;
14204 }
14205 /* FALLTHRU */
14206 case OMP_CLAUSE_IN_REDUCTION:
14207 case OMP_CLAUSE_TASK_REDUCTION:
14208 need_implicitly_determined = true;
14209 t = OMP_CLAUSE_DECL (c);
14210 if (TREE_CODE (t) == TREE_LIST)
14211 {
14212 if (handle_omp_array_sections (c, ort))
14213 {
14214 remove = true;
14215 break;
14216 }
14217
14218 t = OMP_CLAUSE_DECL (c);
14219 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14220 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14221 {
14222 error_at (OMP_CLAUSE_LOCATION (c),
14223 "%<inscan%> %<reduction%> clause with array "
14224 "section");
14225 remove = true;
14226 break;
14227 }
14228 }
14229 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14230 if (t == error_mark_node)
14231 {
14232 remove = true;
14233 break;
14234 }
14235 if (oacc_async)
14236 c_mark_addressable (t);
14237 type = TREE_TYPE (t);
14238 if (TREE_CODE (t) == MEM_REF)
14239 type = TREE_TYPE (type);
14240 if (TREE_CODE (type) == ARRAY_TYPE)
14241 {
14242 tree oatype = type;
14243 gcc_assert (TREE_CODE (t) != MEM_REF);
14244 while (TREE_CODE (type) == ARRAY_TYPE)
14245 type = TREE_TYPE (type);
14246 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14247 {
14248 error_at (OMP_CLAUSE_LOCATION (c),
14249 "%qD in %<reduction%> clause is a zero size array",
14250 t);
14251 remove = true;
14252 break;
14253 }
14254 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14255 TYPE_SIZE_UNIT (type));
14256 if (integer_zerop (size))
14257 {
14258 error_at (OMP_CLAUSE_LOCATION (c),
14259 "%qD in %<reduction%> clause is a zero size array",
14260 t);
14261 remove = true;
14262 break;
14263 }
14264 size = size_binop (MINUS_EXPR, size, size_one_node);
14265 size = save_expr (size);
14266 tree index_type = build_index_type (size);
14267 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14268 index_type);
14269 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14270 tree ptype = build_pointer_type (type);
14271 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14272 t = build_fold_addr_expr (t);
14273 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14274 OMP_CLAUSE_DECL (c) = t;
14275 }
14276 if (TYPE_ATOMIC (type))
14277 {
14278 error_at (OMP_CLAUSE_LOCATION (c),
14279 "%<_Atomic%> %qE in %<reduction%> clause", t);
14280 remove = true;
14281 break;
14282 }
14283 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14284 || OMP_CLAUSE_REDUCTION_TASK (c))
14285 {
14286 /* Disallow zero sized or potentially zero sized task
14287 reductions. */
14288 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14289 {
14290 error_at (OMP_CLAUSE_LOCATION (c),
14291 "zero sized type %qT in %qs clause", type,
14292 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14293 remove = true;
14294 break;
14295 }
14296 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14297 {
14298 error_at (OMP_CLAUSE_LOCATION (c),
14299 "variable sized type %qT in %qs clause", type,
14300 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14301 remove = true;
14302 break;
14303 }
14304 }
14305 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14306 && (FLOAT_TYPE_P (type)
14307 || TREE_CODE (type) == COMPLEX_TYPE))
14308 {
14309 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14310 const char *r_name = NULL;
14311
14312 switch (r_code)
14313 {
14314 case PLUS_EXPR:
14315 case MULT_EXPR:
14316 case MINUS_EXPR:
14317 case TRUTH_ANDIF_EXPR:
14318 case TRUTH_ORIF_EXPR:
14319 break;
14320 case MIN_EXPR:
14321 if (TREE_CODE (type) == COMPLEX_TYPE)
14322 r_name = "min";
14323 break;
14324 case MAX_EXPR:
14325 if (TREE_CODE (type) == COMPLEX_TYPE)
14326 r_name = "max";
14327 break;
14328 case BIT_AND_EXPR:
14329 r_name = "&";
14330 break;
14331 case BIT_XOR_EXPR:
14332 r_name = "^";
14333 break;
14334 case BIT_IOR_EXPR:
14335 r_name = "|";
14336 break;
14337 default:
14338 gcc_unreachable ();
14339 }
14340 if (r_name)
14341 {
14342 error_at (OMP_CLAUSE_LOCATION (c),
14343 "%qE has invalid type for %<reduction(%s)%>",
14344 t, r_name);
14345 remove = true;
14346 break;
14347 }
14348 }
14349 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14350 {
14351 error_at (OMP_CLAUSE_LOCATION (c),
14352 "user defined reduction not found for %qE", t);
14353 remove = true;
14354 break;
14355 }
14356 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14357 {
14358 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14359 type = TYPE_MAIN_VARIANT (type);
14360 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14361 VAR_DECL, NULL_TREE, type);
14362 tree decl_placeholder = NULL_TREE;
14363 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14364 DECL_ARTIFICIAL (placeholder) = 1;
14365 DECL_IGNORED_P (placeholder) = 1;
14366 if (TREE_CODE (t) == MEM_REF)
14367 {
14368 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14369 VAR_DECL, NULL_TREE, type);
14370 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14371 DECL_ARTIFICIAL (decl_placeholder) = 1;
14372 DECL_IGNORED_P (decl_placeholder) = 1;
14373 }
14374 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14375 c_mark_addressable (placeholder);
14376 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14377 c_mark_addressable (decl_placeholder ? decl_placeholder
14378 : OMP_CLAUSE_DECL (c));
14379 OMP_CLAUSE_REDUCTION_MERGE (c)
14380 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14381 TREE_VEC_ELT (list, 0),
14382 TREE_VEC_ELT (list, 1),
14383 decl_placeholder ? decl_placeholder
14384 : OMP_CLAUSE_DECL (c), placeholder);
14385 OMP_CLAUSE_REDUCTION_MERGE (c)
14386 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14387 void_type_node, NULL_TREE,
14388 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14389 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14390 if (TREE_VEC_LENGTH (list) == 6)
14391 {
14392 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14393 c_mark_addressable (decl_placeholder ? decl_placeholder
14394 : OMP_CLAUSE_DECL (c));
14395 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14396 c_mark_addressable (placeholder);
14397 tree init = TREE_VEC_ELT (list, 5);
14398 if (init == error_mark_node)
14399 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14400 OMP_CLAUSE_REDUCTION_INIT (c)
14401 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14402 TREE_VEC_ELT (list, 3),
14403 decl_placeholder ? decl_placeholder
14404 : OMP_CLAUSE_DECL (c), placeholder);
14405 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14406 {
14407 tree v = decl_placeholder ? decl_placeholder : t;
14408 OMP_CLAUSE_REDUCTION_INIT (c)
14409 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14410 OMP_CLAUSE_REDUCTION_INIT (c));
14411 }
14412 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14413 c_find_omp_placeholder_r,
14414 placeholder, NULL))
14415 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14416 }
14417 else
14418 {
14419 tree init;
14420 tree v = decl_placeholder ? decl_placeholder : t;
14421 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14422 init = build_constructor (TREE_TYPE (v), NULL);
14423 else
14424 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14425 OMP_CLAUSE_REDUCTION_INIT (c)
14426 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14427 }
14428 OMP_CLAUSE_REDUCTION_INIT (c)
14429 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14430 void_type_node, NULL_TREE,
14431 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14432 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14433 }
14434 if (TREE_CODE (t) == MEM_REF)
14435 {
14436 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14437 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14438 != INTEGER_CST)
14439 {
14440 sorry ("variable length element type in array "
14441 "%<reduction%> clause");
14442 remove = true;
14443 break;
14444 }
14445 t = TREE_OPERAND (t, 0);
14446 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14447 t = TREE_OPERAND (t, 0);
14448 if (TREE_CODE (t) == ADDR_EXPR)
14449 t = TREE_OPERAND (t, 0);
14450 }
14451 goto check_dup_generic_t;
14452
14453 case OMP_CLAUSE_COPYPRIVATE:
14454 copyprivate_seen = true;
14455 if (nowait_clause)
14456 {
14457 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14458 "%<nowait%> clause must not be used together "
14459 "with %<copyprivate%>");
14460 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14461 nowait_clause = NULL;
14462 }
14463 goto check_dup_generic;
14464
14465 case OMP_CLAUSE_COPYIN:
14466 t = OMP_CLAUSE_DECL (c);
14467 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14468 {
14469 error_at (OMP_CLAUSE_LOCATION (c),
14470 "%qE must be %<threadprivate%> for %<copyin%>", t);
14471 remove = true;
14472 break;
14473 }
14474 goto check_dup_generic;
14475
14476 case OMP_CLAUSE_LINEAR:
14477 if (ort != C_ORT_OMP_DECLARE_SIMD)
14478 need_implicitly_determined = true;
14479 t = OMP_CLAUSE_DECL (c);
14480 if (ort != C_ORT_OMP_DECLARE_SIMD
14481 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
14482 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
14483 {
14484 error_at (OMP_CLAUSE_LOCATION (c),
14485 "modifier should not be specified in %<linear%> "
14486 "clause on %<simd%> or %<for%> constructs when not "
14487 "using OpenMP 5.2 modifiers");
14488 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14489 }
14490 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14491 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14492 {
14493 error_at (OMP_CLAUSE_LOCATION (c),
14494 "linear clause applied to non-integral non-pointer "
14495 "variable with type %qT", TREE_TYPE (t));
14496 remove = true;
14497 break;
14498 }
14499 if (TYPE_ATOMIC (TREE_TYPE (t)))
14500 {
14501 error_at (OMP_CLAUSE_LOCATION (c),
14502 "%<_Atomic%> %qD in %<linear%> clause", t);
14503 remove = true;
14504 break;
14505 }
14506 if (ort == C_ORT_OMP_DECLARE_SIMD)
14507 {
14508 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14509 if (TREE_CODE (s) == PARM_DECL)
14510 {
14511 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14512 /* map_head bitmap is used as uniform_head if
14513 declare_simd. */
14514 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14515 linear_variable_step_check = true;
14516 goto check_dup_generic;
14517 }
14518 if (TREE_CODE (s) != INTEGER_CST)
14519 {
14520 error_at (OMP_CLAUSE_LOCATION (c),
14521 "%<linear%> clause step %qE is neither constant "
14522 "nor a parameter", s);
14523 remove = true;
14524 break;
14525 }
14526 }
14527 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14528 {
14529 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14530 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14531 OMP_CLAUSE_DECL (c), s);
14532 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14533 sizetype, fold_convert (sizetype, s),
14534 fold_convert
14535 (sizetype, OMP_CLAUSE_DECL (c)));
14536 if (s == error_mark_node)
14537 s = size_one_node;
14538 OMP_CLAUSE_LINEAR_STEP (c) = s;
14539 }
14540 else
14541 OMP_CLAUSE_LINEAR_STEP (c)
14542 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14543 goto check_dup_generic;
14544
14545 check_dup_generic:
14546 t = OMP_CLAUSE_DECL (c);
14547 check_dup_generic_t:
14548 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14549 {
14550 error_at (OMP_CLAUSE_LOCATION (c),
14551 "%qE is not a variable in clause %qs", t,
14552 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14553 remove = true;
14554 }
14555 else if ((ort == C_ORT_ACC
14556 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14557 || (ort == C_ORT_OMP
14558 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14559 || (OMP_CLAUSE_CODE (c)
14560 == OMP_CLAUSE_USE_DEVICE_ADDR)))
14561 || (ort == C_ORT_OMP_TARGET
14562 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14563 {
14564 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14565 && (bitmap_bit_p (&generic_head, DECL_UID (t))
14566 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14567 {
14568 error_at (OMP_CLAUSE_LOCATION (c),
14569 "%qD appears more than once in data-sharing "
14570 "clauses", t);
14571 remove = true;
14572 break;
14573 }
14574 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14575 target_in_reduction_seen = true;
14576 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14577 {
14578 error_at (OMP_CLAUSE_LOCATION (c),
14579 ort == C_ORT_ACC
14580 ? "%qD appears more than once in reduction clauses"
14581 : "%qD appears more than once in data clauses",
14582 t);
14583 remove = true;
14584 }
14585 else
14586 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14587 }
14588 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14589 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14590 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
14591 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14592 {
14593 error_at (OMP_CLAUSE_LOCATION (c),
14594 "%qE appears more than once in data clauses", t);
14595 remove = true;
14596 }
14597 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14598 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
14599 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
14600 && bitmap_bit_p (&map_head, DECL_UID (t)))
14601 {
14602 if (ort == C_ORT_ACC)
14603 error_at (OMP_CLAUSE_LOCATION (c),
14604 "%qD appears more than once in data clauses", t);
14605 else
14606 error_at (OMP_CLAUSE_LOCATION (c),
14607 "%qD appears both in data and map clauses", t);
14608 remove = true;
14609 }
14610 else
14611 bitmap_set_bit (&generic_head, DECL_UID (t));
14612 break;
14613
14614 case OMP_CLAUSE_FIRSTPRIVATE:
14615 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
14616 {
14617 move_implicit:
14618 implicit_moved = true;
14619 /* Move firstprivate and map clauses with
14620 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
14621 clauses chain. */
14622 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
14623 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
14624 while (*pc1)
14625 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
14626 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
14627 {
14628 *pc3 = *pc1;
14629 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
14630 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14631 }
14632 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
14633 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
14634 {
14635 *pc2 = *pc1;
14636 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
14637 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14638 }
14639 else
14640 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
14641 *pc3 = NULL;
14642 *pc2 = cl2;
14643 *pc1 = cl1;
14644 continue;
14645 }
14646 t = OMP_CLAUSE_DECL (c);
14647 need_complete = true;
14648 need_implicitly_determined = true;
14649 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14650 {
14651 error_at (OMP_CLAUSE_LOCATION (c),
14652 "%qE is not a variable in clause %<firstprivate%>", t);
14653 remove = true;
14654 }
14655 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14656 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
14657 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14658 remove = true;
14659 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14660 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14661 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14662 {
14663 error_at (OMP_CLAUSE_LOCATION (c),
14664 "%qE appears more than once in data clauses", t);
14665 remove = true;
14666 }
14667 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14668 {
14669 if (ort == C_ORT_ACC)
14670 error_at (OMP_CLAUSE_LOCATION (c),
14671 "%qD appears more than once in data clauses", t);
14672 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14673 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
14674 /* Silently drop the clause. */;
14675 else
14676 error_at (OMP_CLAUSE_LOCATION (c),
14677 "%qD appears both in data and map clauses", t);
14678 remove = true;
14679 }
14680 else
14681 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14682 break;
14683
14684 case OMP_CLAUSE_LASTPRIVATE:
14685 t = OMP_CLAUSE_DECL (c);
14686 need_complete = true;
14687 need_implicitly_determined = true;
14688 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14689 {
14690 error_at (OMP_CLAUSE_LOCATION (c),
14691 "%qE is not a variable in clause %<lastprivate%>", t);
14692 remove = true;
14693 }
14694 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14695 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14696 {
14697 error_at (OMP_CLAUSE_LOCATION (c),
14698 "%qE appears more than once in data clauses", t);
14699 remove = true;
14700 }
14701 else
14702 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14703 break;
14704
14705 case OMP_CLAUSE_ALIGNED:
14706 t = OMP_CLAUSE_DECL (c);
14707 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14708 {
14709 error_at (OMP_CLAUSE_LOCATION (c),
14710 "%qE is not a variable in %<aligned%> clause", t);
14711 remove = true;
14712 }
14713 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14714 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14715 {
14716 error_at (OMP_CLAUSE_LOCATION (c),
14717 "%qE in %<aligned%> clause is neither a pointer nor "
14718 "an array", t);
14719 remove = true;
14720 }
14721 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14722 {
14723 error_at (OMP_CLAUSE_LOCATION (c),
14724 "%<_Atomic%> %qD in %<aligned%> clause", t);
14725 remove = true;
14726 break;
14727 }
14728 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14729 {
14730 error_at (OMP_CLAUSE_LOCATION (c),
14731 "%qE appears more than once in %<aligned%> clauses",
14732 t);
14733 remove = true;
14734 }
14735 else
14736 bitmap_set_bit (&aligned_head, DECL_UID (t));
14737 break;
14738
14739 case OMP_CLAUSE_NONTEMPORAL:
14740 t = OMP_CLAUSE_DECL (c);
14741 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14742 {
14743 error_at (OMP_CLAUSE_LOCATION (c),
14744 "%qE is not a variable in %<nontemporal%> clause", t);
14745 remove = true;
14746 }
14747 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14748 {
14749 error_at (OMP_CLAUSE_LOCATION (c),
14750 "%qE appears more than once in %<nontemporal%> "
14751 "clauses", t);
14752 remove = true;
14753 }
14754 else
14755 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14756 break;
14757
14758 case OMP_CLAUSE_ALLOCATE:
14759 t = OMP_CLAUSE_DECL (c);
14760 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14761 {
14762 error_at (OMP_CLAUSE_LOCATION (c),
14763 "%qE is not a variable in %<allocate%> clause", t);
14764 remove = true;
14765 }
14766 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14767 {
14768 warning_at (OMP_CLAUSE_LOCATION (c), 0,
14769 "%qE appears more than once in %<allocate%> clauses",
14770 t);
14771 remove = true;
14772 }
14773 else
14774 {
14775 bitmap_set_bit (&aligned_head, DECL_UID (t));
14776 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
14777 allocate_seen = true;
14778 }
14779 break;
14780
14781 case OMP_CLAUSE_DEPEND:
14782 t = OMP_CLAUSE_DECL (c);
14783 if (t == NULL_TREE)
14784 {
14785 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14786 == OMP_CLAUSE_DEPEND_SOURCE);
14787 break;
14788 }
14789 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14790 {
14791 gcc_assert (TREE_CODE (t) == TREE_LIST);
14792 for (; t; t = TREE_CHAIN (t))
14793 {
14794 tree decl = TREE_VALUE (t);
14795 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14796 {
14797 tree offset = TREE_PURPOSE (t);
14798 bool neg = wi::neg_p (wi::to_wide (offset));
14799 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14800 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14801 neg ? MINUS_EXPR : PLUS_EXPR,
14802 decl, offset);
14803 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14804 sizetype,
14805 fold_convert (sizetype, t2),
14806 fold_convert (sizetype, decl));
14807 if (t2 == error_mark_node)
14808 {
14809 remove = true;
14810 break;
14811 }
14812 TREE_PURPOSE (t) = t2;
14813 }
14814 }
14815 break;
14816 }
14817 /* FALLTHRU */
14818 case OMP_CLAUSE_AFFINITY:
14819 t = OMP_CLAUSE_DECL (c);
14820 if (TREE_CODE (t) == TREE_LIST
14821 && TREE_PURPOSE (t)
14822 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14823 {
14824 if (TREE_PURPOSE (t) != last_iterators)
14825 last_iterators_remove
14826 = c_omp_finish_iterators (TREE_PURPOSE (t));
14827 last_iterators = TREE_PURPOSE (t);
14828 t = TREE_VALUE (t);
14829 if (last_iterators_remove)
14830 t = error_mark_node;
14831 }
14832 else
14833 last_iterators = NULL_TREE;
14834 if (TREE_CODE (t) == TREE_LIST)
14835 {
14836 if (handle_omp_array_sections (c, ort))
14837 remove = true;
14838 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14839 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14840 {
14841 error_at (OMP_CLAUSE_LOCATION (c),
14842 "%<depend%> clause with %<depobj%> dependence "
14843 "type on array section");
14844 remove = true;
14845 }
14846 break;
14847 }
14848 if (t == error_mark_node)
14849 remove = true;
14850 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14851 && t == ridpointers[RID_OMP_ALL_MEMORY])
14852 {
14853 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
14854 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
14855 {
14856 error_at (OMP_CLAUSE_LOCATION (c),
14857 "%<omp_all_memory%> used with %<depend%> kind "
14858 "other than %<out%> or %<inout%>");
14859 remove = true;
14860 }
14861 }
14862 else if (!lvalue_p (t))
14863 {
14864 error_at (OMP_CLAUSE_LOCATION (c),
14865 "%qE is not lvalue expression nor array section in "
14866 "%qs clause", t,
14867 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14868 remove = true;
14869 }
14870 else if (TREE_CODE (t) == COMPONENT_REF
14871 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14872 {
14873 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14874 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
14875 error_at (OMP_CLAUSE_LOCATION (c),
14876 "bit-field %qE in %qs clause", t,
14877 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14878 remove = true;
14879 }
14880 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14881 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14882 {
14883 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14884 {
14885 error_at (OMP_CLAUSE_LOCATION (c),
14886 "%qE does not have %<omp_depend_t%> type in "
14887 "%<depend%> clause with %<depobj%> dependence "
14888 "type", t);
14889 remove = true;
14890 }
14891 }
14892 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14893 && c_omp_depend_t_p (TREE_TYPE (t)))
14894 {
14895 error_at (OMP_CLAUSE_LOCATION (c),
14896 "%qE should not have %<omp_depend_t%> type in "
14897 "%<depend%> clause with dependence type other than "
14898 "%<depobj%>", t);
14899 remove = true;
14900 }
14901 if (!remove)
14902 {
14903 if (t == ridpointers[RID_OMP_ALL_MEMORY])
14904 t = null_pointer_node;
14905 else
14906 {
14907 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
14908 ADDR_EXPR, t, false);
14909 if (addr == error_mark_node)
14910 {
14911 remove = true;
14912 break;
14913 }
14914 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14915 RO_UNARY_STAR);
14916 if (t == error_mark_node)
14917 {
14918 remove = true;
14919 break;
14920 }
14921 }
14922 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14923 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14924 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14925 == TREE_VEC))
14926 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14927 else
14928 OMP_CLAUSE_DECL (c) = t;
14929 }
14930 break;
14931
14932 case OMP_CLAUSE_MAP:
14933 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
14934 goto move_implicit;
14935 /* FALLTHRU */
14936 case OMP_CLAUSE_TO:
14937 case OMP_CLAUSE_FROM:
14938 case OMP_CLAUSE__CACHE_:
14939 t = OMP_CLAUSE_DECL (c);
14940 if (TREE_CODE (t) == TREE_LIST)
14941 {
14942 if (handle_omp_array_sections (c, ort))
14943 remove = true;
14944 else
14945 {
14946 t = OMP_CLAUSE_DECL (c);
14947 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14948 {
14949 error_at (OMP_CLAUSE_LOCATION (c),
14950 "array section does not have mappable type "
14951 "in %qs clause",
14952 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14953 remove = true;
14954 }
14955 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14956 {
14957 error_at (OMP_CLAUSE_LOCATION (c),
14958 "%<_Atomic%> %qE in %qs clause", t,
14959 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14960 remove = true;
14961 }
14962 while (TREE_CODE (t) == ARRAY_REF)
14963 t = TREE_OPERAND (t, 0);
14964 if (TREE_CODE (t) == COMPONENT_REF
14965 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14966 {
14967 do
14968 {
14969 t = TREE_OPERAND (t, 0);
14970 if (TREE_CODE (t) == MEM_REF
14971 || TREE_CODE (t) == INDIRECT_REF)
14972 {
14973 t = TREE_OPERAND (t, 0);
14974 STRIP_NOPS (t);
14975 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14976 t = TREE_OPERAND (t, 0);
14977 }
14978 }
14979 while (TREE_CODE (t) == COMPONENT_REF
14980 || TREE_CODE (t) == ARRAY_REF);
14981
14982 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14983 && OMP_CLAUSE_MAP_IMPLICIT (c)
14984 && (bitmap_bit_p (&map_head, DECL_UID (t))
14985 || bitmap_bit_p (&map_field_head, DECL_UID (t))
14986 || bitmap_bit_p (&map_firstprivate_head,
14987 DECL_UID (t))))
14988 {
14989 remove = true;
14990 break;
14991 }
14992 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14993 break;
14994 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14995 {
14996 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14997 error_at (OMP_CLAUSE_LOCATION (c),
14998 "%qD appears more than once in motion "
14999 "clauses", t);
15000 else if (ort == C_ORT_ACC)
15001 error_at (OMP_CLAUSE_LOCATION (c),
15002 "%qD appears more than once in data "
15003 "clauses", t);
15004 else
15005 error_at (OMP_CLAUSE_LOCATION (c),
15006 "%qD appears more than once in map "
15007 "clauses", t);
15008 remove = true;
15009 }
15010 else
15011 {
15012 bitmap_set_bit (&map_head, DECL_UID (t));
15013 bitmap_set_bit (&map_field_head, DECL_UID (t));
15014 }
15015 }
15016 }
15017 if (c_oacc_check_attachments (c))
15018 remove = true;
15019 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15020 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15021 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15022 /* In this case, we have a single array element which is a
15023 pointer, and we already set OMP_CLAUSE_SIZE in
15024 handle_omp_array_sections above. For attach/detach clauses,
15025 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
15026 here. */
15027 OMP_CLAUSE_SIZE (c) = size_zero_node;
15028 break;
15029 }
15030 if (t == error_mark_node)
15031 {
15032 remove = true;
15033 break;
15034 }
15035 /* OpenACC attach / detach clauses must be pointers. */
15036 if (c_oacc_check_attachments (c))
15037 {
15038 remove = true;
15039 break;
15040 }
15041 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15042 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15043 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15044 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15045 bias) to zero here, so it is not set erroneously to the pointer
15046 size later on in gimplify.cc. */
15047 OMP_CLAUSE_SIZE (c) = size_zero_node;
15048 while (TREE_CODE (t) == INDIRECT_REF
15049 || TREE_CODE (t) == ARRAY_REF)
15050 {
15051 t = TREE_OPERAND (t, 0);
15052 STRIP_NOPS (t);
15053 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15054 t = TREE_OPERAND (t, 0);
15055 }
15056 while (TREE_CODE (t) == COMPOUND_EXPR)
15057 {
15058 t = TREE_OPERAND (t, 1);
15059 STRIP_NOPS (t);
15060 }
15061 indir_component_ref_p = false;
15062 if (TREE_CODE (t) == COMPONENT_REF
15063 && (TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
15064 || TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF
15065 || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
15066 {
15067 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
15068 indir_component_ref_p = true;
15069 STRIP_NOPS (t);
15070 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15071 t = TREE_OPERAND (t, 0);
15072 }
15073
15074 if (TREE_CODE (t) == COMPONENT_REF
15075 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
15076 {
15077 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
15078 {
15079 error_at (OMP_CLAUSE_LOCATION (c),
15080 "bit-field %qE in %qs clause",
15081 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15082 remove = true;
15083 }
15084 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
15085 {
15086 error_at (OMP_CLAUSE_LOCATION (c),
15087 "%qE does not have a mappable type in %qs clause",
15088 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15089 remove = true;
15090 }
15091 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15092 {
15093 error_at (OMP_CLAUSE_LOCATION (c),
15094 "%<_Atomic%> %qE in %qs clause", t,
15095 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15096 remove = true;
15097 }
15098 while (TREE_CODE (t) == COMPONENT_REF)
15099 {
15100 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
15101 == UNION_TYPE)
15102 {
15103 error_at (OMP_CLAUSE_LOCATION (c),
15104 "%qE is a member of a union", t);
15105 remove = true;
15106 break;
15107 }
15108 t = TREE_OPERAND (t, 0);
15109 if (TREE_CODE (t) == MEM_REF)
15110 {
15111 if (maybe_ne (mem_ref_offset (t), 0))
15112 error_at (OMP_CLAUSE_LOCATION (c),
15113 "cannot dereference %qE in %qs clause", t,
15114 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15115 else
15116 t = TREE_OPERAND (t, 0);
15117 }
15118 while (TREE_CODE (t) == MEM_REF
15119 || TREE_CODE (t) == INDIRECT_REF
15120 || TREE_CODE (t) == ARRAY_REF)
15121 {
15122 t = TREE_OPERAND (t, 0);
15123 STRIP_NOPS (t);
15124 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15125 t = TREE_OPERAND (t, 0);
15126 }
15127 }
15128 if (remove)
15129 break;
15130 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15131 {
15132 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
15133 || (ort != C_ORT_ACC
15134 && bitmap_bit_p (&map_head, DECL_UID (t))))
15135 break;
15136 }
15137 }
15138 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15139 {
15140 error_at (OMP_CLAUSE_LOCATION (c),
15141 "%qE is not a variable in %qs clause", t,
15142 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15143 remove = true;
15144 }
15145 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15146 {
15147 error_at (OMP_CLAUSE_LOCATION (c),
15148 "%qD is threadprivate variable in %qs clause", t,
15149 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15150 remove = true;
15151 }
15152 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15153 || (OMP_CLAUSE_MAP_KIND (c)
15154 != GOMP_MAP_FIRSTPRIVATE_POINTER))
15155 && !indir_component_ref_p
15156 && !c_mark_addressable (t))
15157 remove = true;
15158 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15159 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15160 || (OMP_CLAUSE_MAP_KIND (c)
15161 == GOMP_MAP_FIRSTPRIVATE_POINTER)
15162 || (OMP_CLAUSE_MAP_KIND (c)
15163 == GOMP_MAP_FORCE_DEVICEPTR)))
15164 && t == OMP_CLAUSE_DECL (c)
15165 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
15166 {
15167 error_at (OMP_CLAUSE_LOCATION (c),
15168 "%qD does not have a mappable type in %qs clause", t,
15169 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15170 remove = true;
15171 }
15172 else if (TREE_TYPE (t) == error_mark_node)
15173 remove = true;
15174 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15175 {
15176 error_at (OMP_CLAUSE_LOCATION (c),
15177 "%<_Atomic%> %qE in %qs clause", t,
15178 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15179 remove = true;
15180 }
15181 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15182 && OMP_CLAUSE_MAP_IMPLICIT (c)
15183 && (bitmap_bit_p (&map_head, DECL_UID (t))
15184 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15185 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15186 remove = true;
15187 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15188 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15189 {
15190 if (bitmap_bit_p (&generic_head, DECL_UID (t))
15191 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15192 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15193 {
15194 error_at (OMP_CLAUSE_LOCATION (c),
15195 "%qD appears more than once in data clauses", t);
15196 remove = true;
15197 }
15198 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15199 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15200 {
15201 if (ort == C_ORT_ACC)
15202 error_at (OMP_CLAUSE_LOCATION (c),
15203 "%qD appears more than once in data clauses", t);
15204 else
15205 error_at (OMP_CLAUSE_LOCATION (c),
15206 "%qD appears both in data and map clauses", t);
15207 remove = true;
15208 }
15209 else
15210 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15211 }
15212 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15213 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15214 {
15215 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15216 error_at (OMP_CLAUSE_LOCATION (c),
15217 "%qD appears more than once in motion clauses", t);
15218 else if (ort == C_ORT_ACC)
15219 error_at (OMP_CLAUSE_LOCATION (c),
15220 "%qD appears more than once in data clauses", t);
15221 else
15222 error_at (OMP_CLAUSE_LOCATION (c),
15223 "%qD appears more than once in map clauses", t);
15224 remove = true;
15225 }
15226 else if (ort == C_ORT_ACC
15227 && bitmap_bit_p (&generic_head, DECL_UID (t)))
15228 {
15229 error_at (OMP_CLAUSE_LOCATION (c),
15230 "%qD appears more than once in data clauses", t);
15231 remove = true;
15232 }
15233 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15234 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15235 {
15236 if (ort == C_ORT_ACC)
15237 error_at (OMP_CLAUSE_LOCATION (c),
15238 "%qD appears more than once in data clauses", t);
15239 else
15240 error_at (OMP_CLAUSE_LOCATION (c),
15241 "%qD appears both in data and map clauses", t);
15242 remove = true;
15243 }
15244 else
15245 {
15246 bitmap_set_bit (&map_head, DECL_UID (t));
15247 if (t != OMP_CLAUSE_DECL (c)
15248 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15249 bitmap_set_bit (&map_field_head, DECL_UID (t));
15250 }
15251 break;
15252
15253 case OMP_CLAUSE_ENTER:
15254 case OMP_CLAUSE_LINK:
15255 t = OMP_CLAUSE_DECL (c);
15256 const char *cname;
15257 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15258 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15259 && OMP_CLAUSE_ENTER_TO (c))
15260 cname = "to";
15261 if (TREE_CODE (t) == FUNCTION_DECL
15262 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15263 ;
15264 else if (!VAR_P (t))
15265 {
15266 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15267 error_at (OMP_CLAUSE_LOCATION (c),
15268 "%qE is neither a variable nor a function name in "
15269 "clause %qs", t, cname);
15270 else
15271 error_at (OMP_CLAUSE_LOCATION (c),
15272 "%qE is not a variable in clause %qs", t, cname);
15273 remove = true;
15274 }
15275 else if (DECL_THREAD_LOCAL_P (t))
15276 {
15277 error_at (OMP_CLAUSE_LOCATION (c),
15278 "%qD is threadprivate variable in %qs clause", t,
15279 cname);
15280 remove = true;
15281 }
15282 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
15283 {
15284 error_at (OMP_CLAUSE_LOCATION (c),
15285 "%qD does not have a mappable type in %qs clause", t,
15286 cname);
15287 remove = true;
15288 }
15289 if (remove)
15290 break;
15291 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15292 {
15293 error_at (OMP_CLAUSE_LOCATION (c),
15294 "%qE appears more than once on the same "
15295 "%<declare target%> directive", t);
15296 remove = true;
15297 }
15298 else
15299 bitmap_set_bit (&generic_head, DECL_UID (t));
15300 break;
15301
15302 case OMP_CLAUSE_UNIFORM:
15303 t = OMP_CLAUSE_DECL (c);
15304 if (TREE_CODE (t) != PARM_DECL)
15305 {
15306 if (DECL_P (t))
15307 error_at (OMP_CLAUSE_LOCATION (c),
15308 "%qD is not an argument in %<uniform%> clause", t);
15309 else
15310 error_at (OMP_CLAUSE_LOCATION (c),
15311 "%qE is not an argument in %<uniform%> clause", t);
15312 remove = true;
15313 break;
15314 }
15315 /* map_head bitmap is used as uniform_head if declare_simd. */
15316 bitmap_set_bit (&map_head, DECL_UID (t));
15317 goto check_dup_generic;
15318
15319 case OMP_CLAUSE_IS_DEVICE_PTR:
15320 case OMP_CLAUSE_USE_DEVICE_PTR:
15321 t = OMP_CLAUSE_DECL (c);
15322 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15323 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15324 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15325 {
15326 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15327 && ort != C_ORT_ACC)
15328 {
15329 error_at (OMP_CLAUSE_LOCATION (c),
15330 "%qs variable is not a pointer",
15331 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15332 remove = true;
15333 }
15334 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15335 {
15336 error_at (OMP_CLAUSE_LOCATION (c),
15337 "%qs variable is neither a pointer nor an array",
15338 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15339 remove = true;
15340 }
15341 }
15342 goto check_dup_generic;
15343
15344 case OMP_CLAUSE_HAS_DEVICE_ADDR:
15345 t = OMP_CLAUSE_DECL (c);
15346 if (TREE_CODE (t) == TREE_LIST)
15347 {
15348 if (handle_omp_array_sections (c, ort))
15349 remove = true;
15350 else
15351 {
15352 t = OMP_CLAUSE_DECL (c);
15353 while (TREE_CODE (t) == ARRAY_REF)
15354 t = TREE_OPERAND (t, 0);
15355 }
15356 }
15357 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15358 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15359 c_mark_addressable (t);
15360 goto check_dup_generic_t;
15361
15362 case OMP_CLAUSE_USE_DEVICE_ADDR:
15363 t = OMP_CLAUSE_DECL (c);
15364 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15365 c_mark_addressable (t);
15366 goto check_dup_generic;
15367
15368 case OMP_CLAUSE_NOWAIT:
15369 if (copyprivate_seen)
15370 {
15371 error_at (OMP_CLAUSE_LOCATION (c),
15372 "%<nowait%> clause must not be used together "
15373 "with %<copyprivate%>");
15374 remove = true;
15375 break;
15376 }
15377 nowait_clause = pc;
15378 pc = &OMP_CLAUSE_CHAIN (c);
15379 continue;
15380
15381 case OMP_CLAUSE_ORDER:
15382 if (ordered_clause)
15383 {
15384 error_at (OMP_CLAUSE_LOCATION (c),
15385 "%<order%> clause must not be used together "
15386 "with %<ordered%>");
15387 remove = true;
15388 break;
15389 }
15390 else if (order_clause)
15391 {
15392 /* Silently remove duplicates. */
15393 remove = true;
15394 break;
15395 }
15396 order_clause = pc;
15397 pc = &OMP_CLAUSE_CHAIN (c);
15398 continue;
15399
15400 case OMP_CLAUSE_DETACH:
15401 t = OMP_CLAUSE_DECL (c);
15402 if (detach_seen)
15403 {
15404 error_at (OMP_CLAUSE_LOCATION (c),
15405 "too many %qs clauses on a task construct",
15406 "detach");
15407 remove = true;
15408 break;
15409 }
15410 detach_seen = pc;
15411 pc = &OMP_CLAUSE_CHAIN (c);
15412 c_mark_addressable (t);
15413 continue;
15414
15415 case OMP_CLAUSE_IF:
15416 case OMP_CLAUSE_NUM_THREADS:
15417 case OMP_CLAUSE_NUM_TEAMS:
15418 case OMP_CLAUSE_THREAD_LIMIT:
15419 case OMP_CLAUSE_DEFAULT:
15420 case OMP_CLAUSE_UNTIED:
15421 case OMP_CLAUSE_COLLAPSE:
15422 case OMP_CLAUSE_FINAL:
15423 case OMP_CLAUSE_DEVICE:
15424 case OMP_CLAUSE_DIST_SCHEDULE:
15425 case OMP_CLAUSE_PARALLEL:
15426 case OMP_CLAUSE_FOR:
15427 case OMP_CLAUSE_SECTIONS:
15428 case OMP_CLAUSE_TASKGROUP:
15429 case OMP_CLAUSE_PROC_BIND:
15430 case OMP_CLAUSE_DEVICE_TYPE:
15431 case OMP_CLAUSE_PRIORITY:
15432 case OMP_CLAUSE_GRAINSIZE:
15433 case OMP_CLAUSE_NUM_TASKS:
15434 case OMP_CLAUSE_THREADS:
15435 case OMP_CLAUSE_SIMD:
15436 case OMP_CLAUSE_HINT:
15437 case OMP_CLAUSE_FILTER:
15438 case OMP_CLAUSE_DEFAULTMAP:
15439 case OMP_CLAUSE_BIND:
15440 case OMP_CLAUSE_NUM_GANGS:
15441 case OMP_CLAUSE_NUM_WORKERS:
15442 case OMP_CLAUSE_VECTOR_LENGTH:
15443 case OMP_CLAUSE_ASYNC:
15444 case OMP_CLAUSE_WAIT:
15445 case OMP_CLAUSE_AUTO:
15446 case OMP_CLAUSE_INDEPENDENT:
15447 case OMP_CLAUSE_SEQ:
15448 case OMP_CLAUSE_GANG:
15449 case OMP_CLAUSE_WORKER:
15450 case OMP_CLAUSE_VECTOR:
15451 case OMP_CLAUSE_TILE:
15452 case OMP_CLAUSE_IF_PRESENT:
15453 case OMP_CLAUSE_FINALIZE:
15454 case OMP_CLAUSE_NOHOST:
15455 pc = &OMP_CLAUSE_CHAIN (c);
15456 continue;
15457
15458 case OMP_CLAUSE_MERGEABLE:
15459 mergeable_seen = true;
15460 pc = &OMP_CLAUSE_CHAIN (c);
15461 continue;
15462
15463 case OMP_CLAUSE_NOGROUP:
15464 nogroup_seen = pc;
15465 pc = &OMP_CLAUSE_CHAIN (c);
15466 continue;
15467
15468 case OMP_CLAUSE_SCHEDULE:
15469 schedule_clause = c;
15470 pc = &OMP_CLAUSE_CHAIN (c);
15471 continue;
15472
15473 case OMP_CLAUSE_ORDERED:
15474 ordered_clause = c;
15475 if (order_clause)
15476 {
15477 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15478 "%<order%> clause must not be used together "
15479 "with %<ordered%>");
15480 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15481 order_clause = NULL;
15482 }
15483 pc = &OMP_CLAUSE_CHAIN (c);
15484 continue;
15485
15486 case OMP_CLAUSE_SAFELEN:
15487 safelen = c;
15488 pc = &OMP_CLAUSE_CHAIN (c);
15489 continue;
15490 case OMP_CLAUSE_SIMDLEN:
15491 simdlen = c;
15492 pc = &OMP_CLAUSE_CHAIN (c);
15493 continue;
15494
15495 case OMP_CLAUSE_INBRANCH:
15496 case OMP_CLAUSE_NOTINBRANCH:
15497 if (branch_seen)
15498 {
15499 error_at (OMP_CLAUSE_LOCATION (c),
15500 "%<inbranch%> clause is incompatible with "
15501 "%<notinbranch%>");
15502 remove = true;
15503 break;
15504 }
15505 branch_seen = true;
15506 pc = &OMP_CLAUSE_CHAIN (c);
15507 continue;
15508
15509 case OMP_CLAUSE_INCLUSIVE:
15510 case OMP_CLAUSE_EXCLUSIVE:
15511 need_complete = true;
15512 need_implicitly_determined = true;
15513 t = OMP_CLAUSE_DECL (c);
15514 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15515 {
15516 error_at (OMP_CLAUSE_LOCATION (c),
15517 "%qE is not a variable in clause %qs", t,
15518 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15519 remove = true;
15520 }
15521 break;
15522
15523 default:
15524 gcc_unreachable ();
15525 }
15526
15527 if (!remove)
15528 {
15529 t = OMP_CLAUSE_DECL (c);
15530
15531 if (need_complete)
15532 {
15533 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15534 if (t == error_mark_node)
15535 remove = true;
15536 }
15537
15538 if (need_implicitly_determined)
15539 {
15540 const char *share_name = NULL;
15541
15542 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15543 share_name = "threadprivate";
15544 else switch (c_omp_predetermined_sharing (t))
15545 {
15546 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15547 break;
15548 case OMP_CLAUSE_DEFAULT_SHARED:
15549 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15550 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15551 && c_omp_predefined_variable (t))
15552 /* The __func__ variable and similar function-local
15553 predefined variables may be listed in a shared or
15554 firstprivate clause. */
15555 break;
15556 share_name = "shared";
15557 break;
15558 case OMP_CLAUSE_DEFAULT_PRIVATE:
15559 share_name = "private";
15560 break;
15561 default:
15562 gcc_unreachable ();
15563 }
15564 if (share_name)
15565 {
15566 error_at (OMP_CLAUSE_LOCATION (c),
15567 "%qE is predetermined %qs for %qs",
15568 t, share_name,
15569 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15570 remove = true;
15571 }
15572 else if (TREE_READONLY (t)
15573 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15574 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15575 {
15576 error_at (OMP_CLAUSE_LOCATION (c),
15577 "%<const%> qualified %qE may appear only in "
15578 "%<shared%> or %<firstprivate%> clauses", t);
15579 remove = true;
15580 }
15581 }
15582 }
15583
15584 if (remove)
15585 *pc = OMP_CLAUSE_CHAIN (c);
15586 else
15587 pc = &OMP_CLAUSE_CHAIN (c);
15588 }
15589
15590 if (simdlen
15591 && safelen
15592 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15593 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15594 {
15595 error_at (OMP_CLAUSE_LOCATION (simdlen),
15596 "%<simdlen%> clause value is bigger than "
15597 "%<safelen%> clause value");
15598 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15599 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15600 }
15601
15602 if (ordered_clause
15603 && schedule_clause
15604 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15605 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15606 {
15607 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15608 "%<nonmonotonic%> schedule modifier specified together "
15609 "with %<ordered%> clause");
15610 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15611 = (enum omp_clause_schedule_kind)
15612 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15613 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15614 }
15615
15616 if (reduction_seen < 0 && ordered_clause)
15617 {
15618 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15619 "%qs clause specified together with %<inscan%> "
15620 "%<reduction%> clause", "ordered");
15621 reduction_seen = -2;
15622 }
15623
15624 if (reduction_seen < 0 && schedule_clause)
15625 {
15626 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15627 "%qs clause specified together with %<inscan%> "
15628 "%<reduction%> clause", "schedule");
15629 reduction_seen = -2;
15630 }
15631
15632 if (linear_variable_step_check
15633 || reduction_seen == -2
15634 || allocate_seen
15635 || target_in_reduction_seen)
15636 for (pc = &clauses, c = clauses; c ; c = *pc)
15637 {
15638 bool remove = false;
15639 if (allocate_seen)
15640 switch (OMP_CLAUSE_CODE (c))
15641 {
15642 case OMP_CLAUSE_REDUCTION:
15643 case OMP_CLAUSE_IN_REDUCTION:
15644 case OMP_CLAUSE_TASK_REDUCTION:
15645 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
15646 {
15647 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
15648 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15649 t = TREE_OPERAND (t, 0);
15650 if (TREE_CODE (t) == ADDR_EXPR
15651 || TREE_CODE (t) == INDIRECT_REF)
15652 t = TREE_OPERAND (t, 0);
15653 if (DECL_P (t))
15654 bitmap_clear_bit (&aligned_head, DECL_UID (t));
15655 break;
15656 }
15657 /* FALLTHRU */
15658 case OMP_CLAUSE_PRIVATE:
15659 case OMP_CLAUSE_FIRSTPRIVATE:
15660 case OMP_CLAUSE_LASTPRIVATE:
15661 case OMP_CLAUSE_LINEAR:
15662 if (DECL_P (OMP_CLAUSE_DECL (c)))
15663 bitmap_clear_bit (&aligned_head,
15664 DECL_UID (OMP_CLAUSE_DECL (c)));
15665 break;
15666 default:
15667 break;
15668 }
15669 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15670 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15671 && !bitmap_bit_p (&map_head,
15672 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15673 {
15674 error_at (OMP_CLAUSE_LOCATION (c),
15675 "%<linear%> clause step is a parameter %qD not "
15676 "specified in %<uniform%> clause",
15677 OMP_CLAUSE_LINEAR_STEP (c));
15678 remove = true;
15679 }
15680 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15681 && reduction_seen == -2)
15682 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15683 if (target_in_reduction_seen
15684 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
15685 {
15686 tree t = OMP_CLAUSE_DECL (c);
15687 while (handled_component_p (t)
15688 || TREE_CODE (t) == INDIRECT_REF
15689 || TREE_CODE (t) == ADDR_EXPR
15690 || TREE_CODE (t) == MEM_REF
15691 || TREE_CODE (t) == NON_LVALUE_EXPR)
15692 t = TREE_OPERAND (t, 0);
15693 if (DECL_P (t)
15694 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15695 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15696 }
15697
15698 if (remove)
15699 *pc = OMP_CLAUSE_CHAIN (c);
15700 else
15701 pc = &OMP_CLAUSE_CHAIN (c);
15702 }
15703
15704 if (allocate_seen)
15705 for (pc = &clauses, c = clauses; c ; c = *pc)
15706 {
15707 bool remove = false;
15708 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
15709 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
15710 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
15711 {
15712 error_at (OMP_CLAUSE_LOCATION (c),
15713 "%qD specified in %<allocate%> clause but not in "
15714 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
15715 remove = true;
15716 }
15717 if (remove)
15718 *pc = OMP_CLAUSE_CHAIN (c);
15719 else
15720 pc = &OMP_CLAUSE_CHAIN (c);
15721 }
15722
15723 if (nogroup_seen && reduction_seen)
15724 {
15725 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15726 "%<nogroup%> clause must not be used together with "
15727 "%<reduction%> clause");
15728 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15729 }
15730
15731 if (detach_seen)
15732 {
15733 if (mergeable_seen)
15734 {
15735 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
15736 "%<detach%> clause must not be used together with "
15737 "%<mergeable%> clause");
15738 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
15739 }
15740 else
15741 {
15742 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
15743
15744 for (pc = &clauses, c = clauses; c ; c = *pc)
15745 {
15746 bool remove = false;
15747 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15748 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15749 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
15750 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
15751 && OMP_CLAUSE_DECL (c) == detach_decl)
15752 {
15753 error_at (OMP_CLAUSE_LOCATION (c),
15754 "the event handle of a %<detach%> clause "
15755 "should not be in a data-sharing clause");
15756 remove = true;
15757 }
15758 if (remove)
15759 *pc = OMP_CLAUSE_CHAIN (c);
15760 else
15761 pc = &OMP_CLAUSE_CHAIN (c);
15762 }
15763 }
15764 }
15765
15766 bitmap_obstack_release (NULL);
15767 return clauses;
15768 }
15769
15770 /* Return code to initialize DST with a copy constructor from SRC.
15771 C doesn't have copy constructors nor assignment operators, only for
15772 _Atomic vars we need to perform __atomic_load from src into a temporary
15773 followed by __atomic_store of the temporary to dst. */
15774
15775 tree
15776 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15777 {
15778 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15779 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15780
15781 location_t loc = OMP_CLAUSE_LOCATION (clause);
15782 tree type = TREE_TYPE (dst);
15783 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15784 tree tmp = create_tmp_var (nonatomic_type);
15785 tree tmp_addr = build_fold_addr_expr (tmp);
15786 TREE_ADDRESSABLE (tmp) = 1;
15787 suppress_warning (tmp);
15788 tree src_addr = build_fold_addr_expr (src);
15789 tree dst_addr = build_fold_addr_expr (dst);
15790 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15791 vec<tree, va_gc> *params;
15792 /* Expansion of a generic atomic load may require an addition
15793 element, so allocate enough to prevent a resize. */
15794 vec_alloc (params, 4);
15795
15796 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
15797 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15798 params->quick_push (src_addr);
15799 params->quick_push (tmp_addr);
15800 params->quick_push (seq_cst);
15801 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15802
15803 vec_alloc (params, 4);
15804
15805 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
15806 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15807 params->quick_push (dst_addr);
15808 params->quick_push (tmp_addr);
15809 params->quick_push (seq_cst);
15810 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15811 return build2 (COMPOUND_EXPR, void_type_node, load, store);
15812 }
15813
15814 /* Create a transaction node. */
15815
15816 tree
15817 c_finish_transaction (location_t loc, tree block, int flags)
15818 {
15819 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15820 if (flags & TM_STMT_ATTR_OUTER)
15821 TRANSACTION_EXPR_OUTER (stmt) = 1;
15822 if (flags & TM_STMT_ATTR_RELAXED)
15823 TRANSACTION_EXPR_RELAXED (stmt) = 1;
15824 return add_stmt (stmt);
15825 }
15826
15827 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15828 down to the element type of an array. If ORIG_QUAL_TYPE is not
15829 NULL, then it should be used as the qualified type
15830 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15831 preserve information about the typedef name from which an array
15832 type was derived). */
15833
15834 tree
15835 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15836 size_t orig_qual_indirect)
15837 {
15838 if (type == error_mark_node)
15839 return type;
15840
15841 if (TREE_CODE (type) == ARRAY_TYPE)
15842 {
15843 tree t;
15844 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15845 type_quals, orig_qual_type,
15846 orig_qual_indirect - 1);
15847
15848 /* See if we already have an identically qualified type. */
15849 if (orig_qual_type && orig_qual_indirect == 0)
15850 t = orig_qual_type;
15851 else
15852 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15853 {
15854 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15855 && TYPE_NAME (t) == TYPE_NAME (type)
15856 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15857 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15858 TYPE_ATTRIBUTES (type)))
15859 break;
15860 }
15861 if (!t)
15862 {
15863 tree domain = TYPE_DOMAIN (type);
15864
15865 t = build_variant_type_copy (type);
15866 TREE_TYPE (t) = element_type;
15867
15868 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15869 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15870 SET_TYPE_STRUCTURAL_EQUALITY (t);
15871 else if (TYPE_CANONICAL (element_type) != element_type
15872 || (domain && TYPE_CANONICAL (domain) != domain))
15873 {
15874 tree unqualified_canon
15875 = build_array_type (TYPE_CANONICAL (element_type),
15876 domain? TYPE_CANONICAL (domain)
15877 : NULL_TREE);
15878 if (TYPE_REVERSE_STORAGE_ORDER (type))
15879 {
15880 unqualified_canon
15881 = build_distinct_type_copy (unqualified_canon);
15882 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15883 }
15884 TYPE_CANONICAL (t)
15885 = c_build_qualified_type (unqualified_canon, type_quals);
15886 }
15887 else
15888 TYPE_CANONICAL (t) = t;
15889 }
15890 return t;
15891 }
15892
15893 /* A restrict-qualified pointer type must be a pointer to object or
15894 incomplete type. Note that the use of POINTER_TYPE_P also allows
15895 REFERENCE_TYPEs, which is appropriate for C++. */
15896 if ((type_quals & TYPE_QUAL_RESTRICT)
15897 && (!POINTER_TYPE_P (type)
15898 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15899 {
15900 error ("invalid use of %<restrict%>");
15901 type_quals &= ~TYPE_QUAL_RESTRICT;
15902 }
15903
15904 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15905 ? orig_qual_type
15906 : build_qualified_type (type, type_quals));
15907 /* A variant type does not inherit the list of incomplete vars from the
15908 type main variant. */
15909 if ((RECORD_OR_UNION_TYPE_P (var_type)
15910 || TREE_CODE (var_type) == ENUMERAL_TYPE)
15911 && TYPE_MAIN_VARIANT (var_type) != var_type)
15912 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15913 return var_type;
15914 }
15915
15916 /* Build a VA_ARG_EXPR for the C parser. */
15917
15918 tree
15919 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15920 {
15921 if (error_operand_p (type))
15922 return error_mark_node;
15923 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15924 order because it takes the address of the expression. */
15925 else if (handled_component_p (expr)
15926 && reverse_storage_order_for_component_p (expr))
15927 {
15928 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15929 return error_mark_node;
15930 }
15931 else if (!COMPLETE_TYPE_P (type))
15932 {
15933 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15934 "type %qT", type);
15935 return error_mark_node;
15936 }
15937 else if (TREE_CODE (type) == FUNCTION_TYPE)
15938 {
15939 error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
15940 type);
15941 return error_mark_node;
15942 }
15943 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15944 warning_at (loc2, OPT_Wc___compat,
15945 "C++ requires promoted type, not enum type, in %<va_arg%>");
15946 return build_va_arg (loc2, expr, type);
15947 }
15948
15949 /* Return truthvalue of whether T1 is the same tree structure as T2.
15950 Return 1 if they are the same. Return false if they are different. */
15951
15952 bool
15953 c_tree_equal (tree t1, tree t2)
15954 {
15955 enum tree_code code1, code2;
15956
15957 if (t1 == t2)
15958 return true;
15959 if (!t1 || !t2)
15960 return false;
15961
15962 for (code1 = TREE_CODE (t1);
15963 CONVERT_EXPR_CODE_P (code1)
15964 || code1 == NON_LVALUE_EXPR;
15965 code1 = TREE_CODE (t1))
15966 t1 = TREE_OPERAND (t1, 0);
15967 for (code2 = TREE_CODE (t2);
15968 CONVERT_EXPR_CODE_P (code2)
15969 || code2 == NON_LVALUE_EXPR;
15970 code2 = TREE_CODE (t2))
15971 t2 = TREE_OPERAND (t2, 0);
15972
15973 /* They might have become equal now. */
15974 if (t1 == t2)
15975 return true;
15976
15977 if (code1 != code2)
15978 return false;
15979
15980 switch (code1)
15981 {
15982 case INTEGER_CST:
15983 return wi::to_wide (t1) == wi::to_wide (t2);
15984
15985 case REAL_CST:
15986 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15987
15988 case STRING_CST:
15989 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15990 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15991 TREE_STRING_LENGTH (t1));
15992
15993 case FIXED_CST:
15994 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15995 TREE_FIXED_CST (t2));
15996
15997 case COMPLEX_CST:
15998 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15999 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16000
16001 case VECTOR_CST:
16002 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16003
16004 case CONSTRUCTOR:
16005 /* We need to do this when determining whether or not two
16006 non-type pointer to member function template arguments
16007 are the same. */
16008 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16009 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16010 return false;
16011 {
16012 tree field, value;
16013 unsigned int i;
16014 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16015 {
16016 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16017 if (!c_tree_equal (field, elt2->index)
16018 || !c_tree_equal (value, elt2->value))
16019 return false;
16020 }
16021 }
16022 return true;
16023
16024 case TREE_LIST:
16025 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16026 return false;
16027 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16028 return false;
16029 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16030
16031 case SAVE_EXPR:
16032 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16033
16034 case CALL_EXPR:
16035 {
16036 tree arg1, arg2;
16037 call_expr_arg_iterator iter1, iter2;
16038 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16039 return false;
16040 for (arg1 = first_call_expr_arg (t1, &iter1),
16041 arg2 = first_call_expr_arg (t2, &iter2);
16042 arg1 && arg2;
16043 arg1 = next_call_expr_arg (&iter1),
16044 arg2 = next_call_expr_arg (&iter2))
16045 if (!c_tree_equal (arg1, arg2))
16046 return false;
16047 if (arg1 || arg2)
16048 return false;
16049 return true;
16050 }
16051
16052 case TARGET_EXPR:
16053 {
16054 tree o1 = TREE_OPERAND (t1, 0);
16055 tree o2 = TREE_OPERAND (t2, 0);
16056
16057 /* Special case: if either target is an unallocated VAR_DECL,
16058 it means that it's going to be unified with whatever the
16059 TARGET_EXPR is really supposed to initialize, so treat it
16060 as being equivalent to anything. */
16061 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16062 && !DECL_RTL_SET_P (o1))
16063 /*Nop*/;
16064 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16065 && !DECL_RTL_SET_P (o2))
16066 /*Nop*/;
16067 else if (!c_tree_equal (o1, o2))
16068 return false;
16069
16070 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16071 }
16072
16073 case COMPONENT_REF:
16074 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16075 return false;
16076 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16077
16078 case PARM_DECL:
16079 case VAR_DECL:
16080 case CONST_DECL:
16081 case FIELD_DECL:
16082 case FUNCTION_DECL:
16083 case IDENTIFIER_NODE:
16084 case SSA_NAME:
16085 return false;
16086
16087 case TREE_VEC:
16088 {
16089 unsigned ix;
16090 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16091 return false;
16092 for (ix = TREE_VEC_LENGTH (t1); ix--;)
16093 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16094 TREE_VEC_ELT (t2, ix)))
16095 return false;
16096 return true;
16097 }
16098
16099 default:
16100 break;
16101 }
16102
16103 switch (TREE_CODE_CLASS (code1))
16104 {
16105 case tcc_unary:
16106 case tcc_binary:
16107 case tcc_comparison:
16108 case tcc_expression:
16109 case tcc_vl_exp:
16110 case tcc_reference:
16111 case tcc_statement:
16112 {
16113 int i, n = TREE_OPERAND_LENGTH (t1);
16114
16115 switch (code1)
16116 {
16117 case PREINCREMENT_EXPR:
16118 case PREDECREMENT_EXPR:
16119 case POSTINCREMENT_EXPR:
16120 case POSTDECREMENT_EXPR:
16121 n = 1;
16122 break;
16123 case ARRAY_REF:
16124 n = 2;
16125 break;
16126 default:
16127 break;
16128 }
16129
16130 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16131 && n != TREE_OPERAND_LENGTH (t2))
16132 return false;
16133
16134 for (i = 0; i < n; ++i)
16135 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16136 return false;
16137
16138 return true;
16139 }
16140
16141 case tcc_type:
16142 return comptypes (t1, t2);
16143 default:
16144 gcc_unreachable ();
16145 }
16146 }
16147
16148 /* Returns true when the function declaration FNDECL is implicit,
16149 introduced as a result of a call to an otherwise undeclared
16150 function, and false otherwise. */
16151
16152 bool
16153 c_decl_implicit (const_tree fndecl)
16154 {
16155 return C_DECL_IMPLICIT (fndecl);
16156 }
This page took 0.767166 seconds and 5 git commands to generate.