]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-typeck.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / c / c-typeck.cc
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2024 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 #include "realmpfr.h"
56
57 /* Possible cases of implicit conversions. Used to select diagnostic messages
58 and control folding initializers in convert_for_assignment. */
59 enum impl_conv {
60 ic_argpass,
61 ic_assign,
62 ic_init,
63 ic_init_const,
64 ic_return
65 };
66
67 /* The level of nesting inside "__alignof__". */
68 int in_alignof;
69
70 /* The level of nesting inside "sizeof". */
71 int in_sizeof;
72
73 /* The level of nesting inside "typeof". */
74 int in_typeof;
75
76 /* True when parsing OpenMP loop expressions. */
77 bool c_in_omp_for;
78
79 /* The argument of last parsed sizeof expression, only to be tested
80 if expr.original_code == SIZEOF_EXPR. */
81 tree c_last_sizeof_arg;
82 location_t c_last_sizeof_loc;
83
84 /* Nonzero if we might need to print a "missing braces around
85 initializer" message within this initializer. */
86 static int found_missing_braces;
87
88 static bool require_constant_value;
89 static bool require_constant_elements;
90 static bool require_constexpr_value;
91
92 static tree qualify_type (tree, tree);
93 struct comptypes_data;
94 static bool tagged_types_tu_compatible_p (const_tree, const_tree,
95 struct comptypes_data *);
96 static bool comp_target_types (location_t, tree, tree);
97 static bool function_types_compatible_p (const_tree, const_tree,
98 struct comptypes_data *);
99 static bool type_lists_compatible_p (const_tree, const_tree,
100 struct comptypes_data *);
101 static tree lookup_field (tree, tree);
102 static int convert_arguments (location_t, vec<location_t>, tree,
103 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
104 tree);
105 static tree pointer_diff (location_t, tree, tree, tree *);
106 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
107 enum impl_conv, bool, tree, tree, int,
108 int = 0);
109 static tree valid_compound_expr_initializer (tree, tree);
110 static void push_string (const char *);
111 static void push_member_name (tree);
112 static int spelling_length (void);
113 static char *print_spelling (char *);
114 static void warning_init (location_t, int, const char *);
115 static tree digest_init (location_t, tree, tree, tree, bool, bool, bool, bool,
116 bool, bool);
117 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
118 bool, struct obstack *);
119 static void output_pending_init_elements (int, struct obstack *);
120 static bool set_designator (location_t, bool, struct obstack *);
121 static void push_range_stack (tree, struct obstack *);
122 static void add_pending_init (location_t, tree, tree, tree, bool,
123 struct obstack *);
124 static void set_nonincremental_init (struct obstack *);
125 static void set_nonincremental_init_from_string (tree, struct obstack *);
126 static tree find_init_member (tree, struct obstack *);
127 static void readonly_warning (tree, enum lvalue_use);
128 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
129 static void record_maybe_used_decl (tree);
130 static bool comptypes_internal (const_tree, const_tree,
131 struct comptypes_data *data);
132 \f
133 /* Return true if EXP is a null pointer constant, false otherwise. */
134
135 bool
136 null_pointer_constant_p (const_tree expr)
137 {
138 /* This should really operate on c_expr structures, but they aren't
139 yet available everywhere required. */
140 tree type = TREE_TYPE (expr);
141
142 /* An integer constant expression with the value 0, such an expression
143 cast to type void*, or the predefined constant nullptr, are a null
144 pointer constant. */
145 if (expr == nullptr_node)
146 return true;
147
148 return (TREE_CODE (expr) == INTEGER_CST
149 && !TREE_OVERFLOW (expr)
150 && integer_zerop (expr)
151 && (INTEGRAL_TYPE_P (type)
152 || (TREE_CODE (type) == POINTER_TYPE
153 && VOID_TYPE_P (TREE_TYPE (type))
154 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
155 }
156
157 /* EXPR may appear in an unevaluated part of an integer constant
158 expression, but not in an evaluated part. Wrap it in a
159 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
160 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
161
162 static tree
163 note_integer_operands (tree expr)
164 {
165 tree ret;
166 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
167 {
168 ret = copy_node (expr);
169 TREE_OVERFLOW (ret) = 1;
170 }
171 else
172 {
173 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
174 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
175 }
176 return ret;
177 }
178
179 /* Having checked whether EXPR may appear in an unevaluated part of an
180 integer constant expression and found that it may, remove any
181 C_MAYBE_CONST_EXPR noting this fact and return the resulting
182 expression. */
183
184 static inline tree
185 remove_c_maybe_const_expr (tree expr)
186 {
187 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
188 return C_MAYBE_CONST_EXPR_EXPR (expr);
189 else
190 return expr;
191 }
192
193 \f/* This is a cache to hold if two types are seen. */
194
195 struct tagged_tu_seen_cache {
196 const struct tagged_tu_seen_cache * next;
197 const_tree t1;
198 const_tree t2;
199 };
200
201 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
202 does not have an incomplete type. (That includes void types.)
203 LOC is the location of the use. */
204
205 tree
206 require_complete_type (location_t loc, tree value)
207 {
208 tree type = TREE_TYPE (value);
209
210 if (error_operand_p (value))
211 return error_mark_node;
212
213 /* First, detect a valid value with a complete type. */
214 if (COMPLETE_TYPE_P (type))
215 return value;
216
217 c_incomplete_type_error (loc, value, type);
218 return error_mark_node;
219 }
220
221 /* Print an error message for invalid use of an incomplete type.
222 VALUE is the expression that was used (or 0 if that isn't known)
223 and TYPE is the type that was invalid. LOC is the location for
224 the error. */
225
226 void
227 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
228 {
229 /* Avoid duplicate error message. */
230 if (TREE_CODE (type) == ERROR_MARK)
231 return;
232
233 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
234 error_at (loc, "%qD has an incomplete type %qT", value, type);
235 else
236 {
237 retry:
238 /* We must print an error message. Be clever about what it says. */
239
240 switch (TREE_CODE (type))
241 {
242 case RECORD_TYPE:
243 case UNION_TYPE:
244 case ENUMERAL_TYPE:
245 break;
246
247 case VOID_TYPE:
248 error_at (loc, "invalid use of void expression");
249 return;
250
251 case ARRAY_TYPE:
252 if (TYPE_DOMAIN (type))
253 {
254 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
255 {
256 error_at (loc, "invalid use of flexible array member");
257 return;
258 }
259 type = TREE_TYPE (type);
260 goto retry;
261 }
262 error_at (loc, "invalid use of array with unspecified bounds");
263 return;
264
265 default:
266 gcc_unreachable ();
267 }
268
269 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
270 error_at (loc, "invalid use of undefined type %qT", type);
271 else
272 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
273 error_at (loc, "invalid use of incomplete typedef %qT", type);
274 }
275 }
276
277 /* Given a type, apply default promotions wrt unnamed function
278 arguments and return the new type. */
279
280 tree
281 c_type_promotes_to (tree type)
282 {
283 tree ret = NULL_TREE;
284
285 if (TYPE_MAIN_VARIANT (type) == float_type_node)
286 ret = double_type_node;
287 else if (c_promoting_integer_type_p (type))
288 {
289 /* Preserve unsignedness if not really getting any wider. */
290 if (TYPE_UNSIGNED (type)
291 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
292 ret = unsigned_type_node;
293 else
294 ret = integer_type_node;
295 }
296
297 if (ret != NULL_TREE)
298 return (TYPE_ATOMIC (type)
299 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
300 : ret);
301
302 return type;
303 }
304
305 /* Return true if between two named address spaces, whether there is a superset
306 named address space that encompasses both address spaces. If there is a
307 superset, return which address space is the superset. */
308
309 static bool
310 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
311 {
312 if (as1 == as2)
313 {
314 *common = as1;
315 return true;
316 }
317 else if (targetm.addr_space.subset_p (as1, as2))
318 {
319 *common = as2;
320 return true;
321 }
322 else if (targetm.addr_space.subset_p (as2, as1))
323 {
324 *common = as1;
325 return true;
326 }
327 else
328 return false;
329 }
330
331 /* Return a variant of TYPE which has all the type qualifiers of LIKE
332 as well as those of TYPE. */
333
334 static tree
335 qualify_type (tree type, tree like)
336 {
337 addr_space_t as_type = TYPE_ADDR_SPACE (type);
338 addr_space_t as_like = TYPE_ADDR_SPACE (like);
339 addr_space_t as_common;
340
341 /* If the two named address spaces are different, determine the common
342 superset address space. If there isn't one, raise an error. */
343 if (!addr_space_superset (as_type, as_like, &as_common))
344 {
345 as_common = as_type;
346 error ("%qT and %qT are in disjoint named address spaces",
347 type, like);
348 }
349
350 return c_build_qualified_type (type,
351 TYPE_QUALS_NO_ADDR_SPACE (type)
352 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
353 | ENCODE_QUAL_ADDR_SPACE (as_common));
354 }
355
356
357 /* If NTYPE is a type of a non-variadic function with a prototype
358 and OTYPE is a type of a function without a prototype and ATTRS
359 contains attribute format, diagnosess and removes it from ATTRS.
360 Returns the result of build_type_attribute_variant of NTYPE and
361 the (possibly) modified ATTRS. */
362
363 static tree
364 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
365 {
366 if (!prototype_p (otype)
367 && prototype_p (ntype)
368 && lookup_attribute ("format", attrs))
369 {
370 warning_at (input_location, OPT_Wattributes,
371 "%qs attribute cannot be applied to a function that "
372 "does not take variable arguments", "format");
373 attrs = remove_attribute ("format", attrs);
374 }
375 return build_type_attribute_variant (ntype, attrs);
376
377 }
378 /* Return the composite type of two compatible types.
379
380 We assume that comptypes has already been done and returned
381 nonzero; if that isn't so, this may crash. In particular, we
382 assume that qualifiers match. */
383
384 struct composite_cache {
385 tree t1;
386 tree t2;
387 tree composite;
388 struct composite_cache* next;
389 };
390
391 tree
392 composite_type_internal (tree t1, tree t2, struct composite_cache* cache)
393 {
394 enum tree_code code1;
395 enum tree_code code2;
396 tree attributes;
397
398 /* Save time if the two types are the same. */
399
400 if (t1 == t2) return t1;
401
402 /* If one type is nonsense, use the other. */
403 if (t1 == error_mark_node)
404 return t2;
405 if (t2 == error_mark_node)
406 return t1;
407
408 code1 = TREE_CODE (t1);
409 code2 = TREE_CODE (t2);
410
411 /* Merge the attributes. */
412 attributes = targetm.merge_type_attributes (t1, t2);
413
414 /* If one is an enumerated type and the other is the compatible
415 integer type, the composite type might be either of the two
416 (DR#013 question 3). For consistency, use the enumerated type as
417 the composite type. */
418
419 if (code1 == ENUMERAL_TYPE
420 && (code2 == INTEGER_TYPE
421 || code2 == BOOLEAN_TYPE))
422 return t1;
423 if (code2 == ENUMERAL_TYPE
424 && (code1 == INTEGER_TYPE
425 || code1 == BOOLEAN_TYPE))
426 return t2;
427
428 gcc_assert (code1 == code2);
429
430 switch (code1)
431 {
432 case POINTER_TYPE:
433 /* For two pointers, do this recursively on the target type. */
434 {
435 tree pointed_to_1 = TREE_TYPE (t1);
436 tree pointed_to_2 = TREE_TYPE (t2);
437 tree target = composite_type_internal (pointed_to_1,
438 pointed_to_2, cache);
439 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
440 t1 = build_type_attribute_variant (t1, attributes);
441 return qualify_type (t1, t2);
442 }
443
444 case ARRAY_TYPE:
445 {
446 tree elt = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
447 cache);
448 int quals;
449 tree unqual_elt;
450 tree d1 = TYPE_DOMAIN (t1);
451 tree d2 = TYPE_DOMAIN (t2);
452 bool d1_variable, d2_variable;
453 bool d1_zero, d2_zero;
454 bool t1_complete, t2_complete;
455
456 /* We should not have any type quals on arrays at all. */
457 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
458 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
459
460 t1_complete = COMPLETE_TYPE_P (t1);
461 t2_complete = COMPLETE_TYPE_P (t2);
462
463 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
464 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
465
466 d1_variable = (!d1_zero
467 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
468 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
469 d2_variable = (!d2_zero
470 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
471 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
472 d1_variable = d1_variable || (d1_zero && C_TYPE_VARIABLE_SIZE (t1));
473 d2_variable = d2_variable || (d2_zero && C_TYPE_VARIABLE_SIZE (t2));
474
475 /* Save space: see if the result is identical to one of the args. */
476 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
477 && (d2_variable || d2_zero || !d1_variable))
478 return build_type_attribute_variant (t1, attributes);
479 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
480 && (d1_variable || d1_zero || !d2_variable))
481 return build_type_attribute_variant (t2, attributes);
482
483 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
484 return build_type_attribute_variant (t1, attributes);
485 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
486 return build_type_attribute_variant (t2, attributes);
487
488 /* Merge the element types, and have a size if either arg has
489 one. We may have qualifiers on the element types. To set
490 up TYPE_MAIN_VARIANT correctly, we need to form the
491 composite of the unqualified types and add the qualifiers
492 back at the end. */
493 quals = TYPE_QUALS (strip_array_types (elt));
494 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
495 t1 = build_array_type (unqual_elt,
496 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
497 && (d2_variable
498 || d2_zero
499 || !d1_variable))
500 ? t1
501 : t2));
502 /* Ensure a composite type involving a zero-length array type
503 is a zero-length type not an incomplete type. */
504 if (d1_zero && d2_zero
505 && (t1_complete || t2_complete)
506 && !COMPLETE_TYPE_P (t1))
507 {
508 TYPE_SIZE (t1) = bitsize_zero_node;
509 TYPE_SIZE_UNIT (t1) = size_zero_node;
510 }
511 t1 = c_build_qualified_type (t1, quals);
512 return build_type_attribute_variant (t1, attributes);
513 }
514
515 case RECORD_TYPE:
516 case UNION_TYPE:
517 if (flag_isoc23 && !comptypes_same_p (t1, t2))
518 {
519 gcc_checking_assert (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2));
520 gcc_checking_assert (!TYPE_NAME (t1) || comptypes (t1, t2));
521
522 /* If a composite type for these two types is already under
523 construction, return it. */
524
525 for (struct composite_cache *c = cache; c != NULL; c = c->next)
526 if (c->t1 == t1 && c->t2 == t2)
527 return c->composite;
528
529 /* Otherwise, create a new type node and link it into the cache. */
530
531 tree n = make_node (code1);
532 TYPE_NAME (n) = TYPE_NAME (t1);
533
534 struct composite_cache cache2 = { t1, t2, n, cache };
535 cache = &cache2;
536
537 tree f1 = TYPE_FIELDS (t1);
538 tree f2 = TYPE_FIELDS (t2);
539 tree fields = NULL_TREE;
540
541 for (tree a = f1, b = f2; a && b;
542 a = DECL_CHAIN (a), b = DECL_CHAIN (b))
543 {
544 tree ta = TREE_TYPE (a);
545 tree tb = TREE_TYPE (b);
546
547 if (DECL_C_BIT_FIELD (a))
548 {
549 ta = DECL_BIT_FIELD_TYPE (a);
550 tb = DECL_BIT_FIELD_TYPE (b);
551 }
552
553 gcc_assert (DECL_NAME (a) == DECL_NAME (b));
554 gcc_checking_assert (!DECL_NAME (a) || comptypes (ta, tb));
555
556 tree t = composite_type_internal (ta, tb, cache);
557 tree f = build_decl (input_location, FIELD_DECL, DECL_NAME (a), t);
558
559 DECL_PACKED (f) = DECL_PACKED (a);
560 SET_DECL_ALIGN (f, DECL_ALIGN (a));
561 DECL_ATTRIBUTES (f) = DECL_ATTRIBUTES (a);
562 C_DECL_VARIABLE_SIZE (f) = C_TYPE_VARIABLE_SIZE (t);
563
564 finish_decl (f, input_location, NULL, NULL, NULL);
565
566 if (DECL_C_BIT_FIELD (a))
567 {
568 /* This will be processed by finish_struct. */
569 SET_DECL_C_BIT_FIELD (f);
570 DECL_INITIAL (f) = build_int_cst (integer_type_node,
571 tree_to_uhwi (DECL_SIZE (a)));
572 DECL_NONADDRESSABLE_P (f) = true;
573 DECL_PADDING_P (f) = !DECL_NAME (a);
574 }
575
576 DECL_CHAIN (f) = fields;
577 fields = f;
578 }
579
580 fields = nreverse (fields);
581
582 /* Setup the struct/union type. Because we inherit all variably
583 modified components, we can ignore the size expression. */
584 tree expr = NULL_TREE;
585 n = finish_struct(input_location, n, fields, attributes, NULL, &expr);
586
587 n = qualify_type (n, t1);
588
589 gcc_checking_assert (!TYPE_NAME (n) || comptypes (n, t1));
590 gcc_checking_assert (!TYPE_NAME (n) || comptypes (n, t2));
591
592 return n;
593 }
594 /* FALLTHRU */
595 case ENUMERAL_TYPE:
596 if (attributes != NULL)
597 {
598 /* Try harder not to create a new aggregate type. */
599 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
600 return t1;
601 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
602 return t2;
603 }
604 return build_type_attribute_variant (t1, attributes);
605
606 case FUNCTION_TYPE:
607 /* Function types: prefer the one that specified arg types.
608 If both do, merge the arg types. Also merge the return types. */
609 {
610 tree valtype = composite_type_internal (TREE_TYPE (t1),
611 TREE_TYPE (t2), cache);
612 tree p1 = TYPE_ARG_TYPES (t1);
613 tree p2 = TYPE_ARG_TYPES (t2);
614 int len;
615 tree newargs, n;
616 int i;
617
618 /* Save space: see if the result is identical to one of the args. */
619 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
620 return build_functype_attribute_variant (t1, t2, attributes);
621 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
622 return build_functype_attribute_variant (t2, t1, attributes);
623
624 /* Simple way if one arg fails to specify argument types. */
625 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
626 {
627 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2),
628 TYPE_NO_NAMED_ARGS_STDARG_P (t2));
629 t1 = build_type_attribute_variant (t1, attributes);
630 return qualify_type (t1, t2);
631 }
632 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
633 {
634 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1),
635 TYPE_NO_NAMED_ARGS_STDARG_P (t1));
636 t1 = build_type_attribute_variant (t1, attributes);
637 return qualify_type (t1, t2);
638 }
639
640 /* If both args specify argument types, we must merge the two
641 lists, argument by argument. */
642
643 for (len = 0, newargs = p1;
644 newargs && newargs != void_list_node;
645 len++, newargs = TREE_CHAIN (newargs))
646 ;
647
648 for (i = 0; i < len; i++)
649 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
650
651 n = newargs;
652
653 for (; p1 && p1 != void_list_node;
654 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
655 {
656 tree mv1 = TREE_VALUE (p1);
657 if (mv1 && mv1 != error_mark_node
658 && TREE_CODE (mv1) != ARRAY_TYPE)
659 mv1 = TYPE_MAIN_VARIANT (mv1);
660
661 tree mv2 = TREE_VALUE (p2);
662 if (mv2 && mv2 != error_mark_node
663 && TREE_CODE (mv2) != ARRAY_TYPE)
664 mv2 = TYPE_MAIN_VARIANT (mv2);
665
666 /* A null type means arg type is not specified.
667 Take whatever the other function type has. */
668 if (TREE_VALUE (p1) == NULL_TREE)
669 {
670 TREE_VALUE (n) = TREE_VALUE (p2);
671 goto parm_done;
672 }
673 if (TREE_VALUE (p2) == NULL_TREE)
674 {
675 TREE_VALUE (n) = TREE_VALUE (p1);
676 goto parm_done;
677 }
678
679 /* Given wait (union {union wait *u; int *i} *)
680 and wait (union wait *),
681 prefer union wait * as type of parm. */
682 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
683 && TREE_VALUE (p1) != TREE_VALUE (p2))
684 {
685 tree memb;
686 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
687 memb; memb = DECL_CHAIN (memb))
688 {
689 tree mv3 = TREE_TYPE (memb);
690 if (mv3 && mv3 != error_mark_node
691 && TREE_CODE (mv3) != ARRAY_TYPE)
692 mv3 = TYPE_MAIN_VARIANT (mv3);
693 if (comptypes (mv3, mv2))
694 {
695 TREE_VALUE (n) = composite_type_internal (TREE_TYPE (memb),
696 TREE_VALUE (p2),
697 cache);
698 pedwarn (input_location, OPT_Wpedantic,
699 "function types not truly compatible in ISO C");
700 goto parm_done;
701 }
702 }
703 }
704 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
705 && TREE_VALUE (p2) != TREE_VALUE (p1))
706 {
707 tree memb;
708 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
709 memb; memb = DECL_CHAIN (memb))
710 {
711 tree mv3 = TREE_TYPE (memb);
712 if (mv3 && mv3 != error_mark_node
713 && TREE_CODE (mv3) != ARRAY_TYPE)
714 mv3 = TYPE_MAIN_VARIANT (mv3);
715 if (comptypes (mv3, mv1))
716 {
717 TREE_VALUE (n)
718 = composite_type_internal (TREE_TYPE (memb),
719 TREE_VALUE (p1),
720 cache);
721 pedwarn (input_location, OPT_Wpedantic,
722 "function types not truly compatible in ISO C");
723 goto parm_done;
724 }
725 }
726 }
727 TREE_VALUE (n) = composite_type_internal (mv1, mv2, cache);
728 parm_done: ;
729 }
730
731 t1 = build_function_type (valtype, newargs);
732 t1 = qualify_type (t1, t2);
733 }
734 /* FALLTHRU */
735
736 default:
737 return build_type_attribute_variant (t1, attributes);
738 }
739 }
740
741 tree
742 composite_type (tree t1, tree t2)
743 {
744 struct composite_cache cache = { };
745 return composite_type_internal (t1, t2, &cache);
746 }
747
748 /* Return the type of a conditional expression between pointers to
749 possibly differently qualified versions of compatible types.
750
751 We assume that comp_target_types has already been done and returned
752 true; if that isn't so, this may crash. */
753
754 static tree
755 common_pointer_type (tree t1, tree t2)
756 {
757 tree attributes;
758 tree pointed_to_1, mv1;
759 tree pointed_to_2, mv2;
760 tree target;
761 unsigned target_quals;
762 addr_space_t as1, as2, as_common;
763 int quals1, quals2;
764
765 /* Save time if the two types are the same. */
766
767 if (t1 == t2) return t1;
768
769 /* If one type is nonsense, use the other. */
770 if (t1 == error_mark_node)
771 return t2;
772 if (t2 == error_mark_node)
773 return t1;
774
775 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
776 && TREE_CODE (t2) == POINTER_TYPE);
777
778 /* Merge the attributes. */
779 attributes = targetm.merge_type_attributes (t1, t2);
780
781 /* Find the composite type of the target types, and combine the
782 qualifiers of the two types' targets. Do not lose qualifiers on
783 array element types by taking the TYPE_MAIN_VARIANT. */
784 mv1 = pointed_to_1 = TREE_TYPE (t1);
785 mv2 = pointed_to_2 = TREE_TYPE (t2);
786 if (TREE_CODE (mv1) != ARRAY_TYPE)
787 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
788 if (TREE_CODE (mv2) != ARRAY_TYPE)
789 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
790 target = composite_type (mv1, mv2);
791
792 /* Strip array types to get correct qualifier for pointers to arrays */
793 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
794 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
795
796 /* For function types do not merge const qualifiers, but drop them
797 if used inconsistently. The middle-end uses these to mark const
798 and noreturn functions. */
799 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
800 target_quals = (quals1 & quals2);
801 else
802 target_quals = (quals1 | quals2);
803
804 /* If the two named address spaces are different, determine the common
805 superset address space. This is guaranteed to exist due to the
806 assumption that comp_target_type returned true. */
807 as1 = TYPE_ADDR_SPACE (pointed_to_1);
808 as2 = TYPE_ADDR_SPACE (pointed_to_2);
809 if (!addr_space_superset (as1, as2, &as_common))
810 gcc_unreachable ();
811
812 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
813
814 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
815 return build_type_attribute_variant (t1, attributes);
816 }
817
818 /* Return the common type for two arithmetic types under the usual
819 arithmetic conversions. The default conversions have already been
820 applied, and enumerated types converted to their compatible integer
821 types. The resulting type is unqualified and has no attributes.
822
823 This is the type for the result of most arithmetic operations
824 if the operands have the given two types. */
825
826 static tree
827 c_common_type (tree t1, tree t2)
828 {
829 enum tree_code code1;
830 enum tree_code code2;
831
832 /* If one type is nonsense, use the other. */
833 if (t1 == error_mark_node)
834 return t2;
835 if (t2 == error_mark_node)
836 return t1;
837
838 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
839 t1 = TYPE_MAIN_VARIANT (t1);
840
841 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
842 t2 = TYPE_MAIN_VARIANT (t2);
843
844 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
845 {
846 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
847 t1 = build_type_attribute_variant (t1, attrs);
848 }
849
850 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
851 {
852 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
853 t2 = build_type_attribute_variant (t2, attrs);
854 }
855
856 /* Save time if the two types are the same. */
857
858 if (t1 == t2) return t1;
859
860 code1 = TREE_CODE (t1);
861 code2 = TREE_CODE (t2);
862
863 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
864 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
865 || code1 == INTEGER_TYPE || code1 == BITINT_TYPE);
866 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
867 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
868 || code2 == INTEGER_TYPE || code2 == BITINT_TYPE);
869
870 /* When one operand is a decimal float type, the other operand cannot be
871 a generic float type or a complex type. We also disallow vector types
872 here. */
873 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
874 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
875 {
876 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
877 {
878 error ("cannot mix operands of decimal floating and vector types");
879 return error_mark_node;
880 }
881 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
882 {
883 error ("cannot mix operands of decimal floating and complex types");
884 return error_mark_node;
885 }
886 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
887 {
888 error ("cannot mix operands of decimal floating "
889 "and other floating types");
890 return error_mark_node;
891 }
892 }
893
894 /* If one type is a vector type, return that type. (How the usual
895 arithmetic conversions apply to the vector types extension is not
896 precisely specified.) */
897 if (code1 == VECTOR_TYPE)
898 return t1;
899
900 if (code2 == VECTOR_TYPE)
901 return t2;
902
903 /* If one type is complex, form the common type of the non-complex
904 components, then make that complex. Use T1 or T2 if it is the
905 required type. */
906 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
907 {
908 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
909 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
910 tree subtype = c_common_type (subtype1, subtype2);
911
912 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
913 return t1;
914 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
915 return t2;
916 else if (TREE_CODE (subtype) == BITINT_TYPE)
917 {
918 sorry ("%<_Complex _BitInt(%d)%> unsupported",
919 TYPE_PRECISION (subtype));
920 return code1 == COMPLEX_TYPE ? t1 : t2;
921 }
922 else
923 return build_complex_type (subtype);
924 }
925
926 /* If only one is real, use it as the result. */
927
928 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
929 return t1;
930
931 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
932 return t2;
933
934 /* If both are real and either are decimal floating point types, use
935 the decimal floating point type with the greater precision. */
936
937 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
938 {
939 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
940 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
941 return dfloat128_type_node;
942 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
943 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
944 return dfloat64_type_node;
945 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
946 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
947 return dfloat32_type_node;
948 }
949
950 /* Deal with fixed-point types. */
951 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
952 {
953 unsigned int unsignedp = 0, satp = 0;
954 scalar_mode m1, m2;
955 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
956
957 m1 = SCALAR_TYPE_MODE (t1);
958 m2 = SCALAR_TYPE_MODE (t2);
959
960 /* If one input type is saturating, the result type is saturating. */
961 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
962 satp = 1;
963
964 /* If both fixed-point types are unsigned, the result type is unsigned.
965 When mixing fixed-point and integer types, follow the sign of the
966 fixed-point type.
967 Otherwise, the result type is signed. */
968 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
969 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
970 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
971 && TYPE_UNSIGNED (t1))
972 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
973 && TYPE_UNSIGNED (t2)))
974 unsignedp = 1;
975
976 /* The result type is signed. */
977 if (unsignedp == 0)
978 {
979 /* If the input type is unsigned, we need to convert to the
980 signed type. */
981 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
982 {
983 enum mode_class mclass = (enum mode_class) 0;
984 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
985 mclass = MODE_FRACT;
986 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
987 mclass = MODE_ACCUM;
988 else
989 gcc_unreachable ();
990 m1 = as_a <scalar_mode>
991 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
992 }
993 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
994 {
995 enum mode_class mclass = (enum mode_class) 0;
996 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
997 mclass = MODE_FRACT;
998 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
999 mclass = MODE_ACCUM;
1000 else
1001 gcc_unreachable ();
1002 m2 = as_a <scalar_mode>
1003 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
1004 }
1005 }
1006
1007 if (code1 == FIXED_POINT_TYPE)
1008 {
1009 fbit1 = GET_MODE_FBIT (m1);
1010 ibit1 = GET_MODE_IBIT (m1);
1011 }
1012 else
1013 {
1014 fbit1 = 0;
1015 /* Signed integers need to subtract one sign bit. */
1016 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
1017 }
1018
1019 if (code2 == FIXED_POINT_TYPE)
1020 {
1021 fbit2 = GET_MODE_FBIT (m2);
1022 ibit2 = GET_MODE_IBIT (m2);
1023 }
1024 else
1025 {
1026 fbit2 = 0;
1027 /* Signed integers need to subtract one sign bit. */
1028 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
1029 }
1030
1031 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
1032 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
1033 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
1034 satp);
1035 }
1036
1037 /* Both real or both integers; use the one with greater precision. */
1038
1039 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
1040 return t1;
1041 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
1042 return t2;
1043
1044 /* Same precision. Prefer long longs to longs to ints when the
1045 same precision, following the C99 rules on integer type rank
1046 (which are equivalent to the C90 rules for C90 types). */
1047
1048 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
1049 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
1050 return long_long_unsigned_type_node;
1051
1052 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
1053 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
1054 {
1055 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1056 return long_long_unsigned_type_node;
1057 else
1058 return long_long_integer_type_node;
1059 }
1060
1061 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
1062 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
1063 return long_unsigned_type_node;
1064
1065 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
1066 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
1067 {
1068 /* But preserve unsignedness from the other type,
1069 since long cannot hold all the values of an unsigned int. */
1070 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1071 return long_unsigned_type_node;
1072 else
1073 return long_integer_type_node;
1074 }
1075
1076 /* For floating types of the same TYPE_PRECISION (which we here
1077 assume means either the same set of values, or sets of values
1078 neither a subset of the other, with behavior being undefined in
1079 the latter case), follow the rules from TS 18661-3: prefer
1080 interchange types _FloatN, then standard types long double,
1081 double, float, then extended types _FloatNx. For extended types,
1082 check them starting with _Float128x as that seems most consistent
1083 in spirit with preferring long double to double; for interchange
1084 types, also check in that order for consistency although it's not
1085 possible for more than one of them to have the same
1086 precision. */
1087 tree mv1 = TYPE_MAIN_VARIANT (t1);
1088 tree mv2 = TYPE_MAIN_VARIANT (t2);
1089
1090 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
1091 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
1092 return FLOATN_TYPE_NODE (i);
1093
1094 /* Likewise, prefer long double to double even if same size. */
1095 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1096 return long_double_type_node;
1097
1098 /* Likewise, prefer double to float even if same size.
1099 We got a couple of embedded targets with 32 bit doubles, and the
1100 pdp11 might have 64 bit floats. */
1101 if (mv1 == double_type_node || mv2 == double_type_node)
1102 return double_type_node;
1103
1104 if (mv1 == float_type_node || mv2 == float_type_node)
1105 return float_type_node;
1106
1107 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1108 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1109 return FLOATNX_TYPE_NODE (i);
1110
1111 if ((code1 == BITINT_TYPE || code2 == BITINT_TYPE) && code1 != code2)
1112 {
1113 /* Prefer any other integral types over bit-precise integer types. */
1114 if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2))
1115 return code1 == BITINT_TYPE ? t2 : t1;
1116 /* If BITINT_TYPE is unsigned and the other type is signed
1117 non-BITINT_TYPE with the same precision, the latter has higher rank.
1118 In that case:
1119 Otherwise, both operands are converted to the unsigned integer type
1120 corresponding to the type of the operand with signed integer type. */
1121 if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2))
1122 return c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
1123 }
1124
1125 /* Otherwise prefer the unsigned one. */
1126
1127 if (TYPE_UNSIGNED (t1))
1128 return t1;
1129 else
1130 return t2;
1131 }
1132 \f
1133 /* Wrapper around c_common_type that is used by c-common.cc and other
1134 front end optimizations that remove promotions. ENUMERAL_TYPEs
1135 are allowed here and are converted to their compatible integer types.
1136 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1137 preferably a non-Boolean type as the common type. */
1138 tree
1139 common_type (tree t1, tree t2)
1140 {
1141 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1142 t1 = ENUM_UNDERLYING_TYPE (t1);
1143 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1144 t2 = ENUM_UNDERLYING_TYPE (t2);
1145
1146 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1147 if (TREE_CODE (t1) == BOOLEAN_TYPE
1148 && TREE_CODE (t2) == BOOLEAN_TYPE)
1149 return boolean_type_node;
1150
1151 /* If either type is BOOLEAN_TYPE, then return the other. */
1152 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1153 return t2;
1154 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1155 return t1;
1156
1157 return c_common_type (t1, t2);
1158 }
1159
1160 struct comptypes_data {
1161 bool enum_and_int_p;
1162 bool different_types_p;
1163 bool warning_needed;
1164 bool anon_field;
1165 bool equiv;
1166
1167 const struct tagged_tu_seen_cache* cache;
1168 };
1169
1170 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1171 or various other operations. Return 2 if they are compatible
1172 but a warning may be needed if you use them together. */
1173
1174 int
1175 comptypes (tree type1, tree type2)
1176 {
1177 struct comptypes_data data = { };
1178 bool ret = comptypes_internal (type1, type2, &data);
1179
1180 return ret ? (data.warning_needed ? 2 : 1) : 0;
1181 }
1182
1183
1184 /* Like comptypes, but it returns non-zero only for identical
1185 types. */
1186
1187 bool
1188 comptypes_same_p (tree type1, tree type2)
1189 {
1190 struct comptypes_data data = { };
1191 bool ret = comptypes_internal (type1, type2, &data);
1192
1193 if (data.different_types_p)
1194 return false;
1195
1196 return ret;
1197 }
1198
1199
1200 /* Like comptypes, but if it returns non-zero because enum and int are
1201 compatible, it sets *ENUM_AND_INT_P to true. */
1202
1203 int
1204 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1205 {
1206 struct comptypes_data data = { };
1207 bool ret = comptypes_internal (type1, type2, &data);
1208 *enum_and_int_p = data.enum_and_int_p;
1209
1210 return ret ? (data.warning_needed ? 2 : 1) : 0;
1211 }
1212
1213 /* Like comptypes, but if it returns nonzero for different types, it
1214 sets *DIFFERENT_TYPES_P to true. */
1215
1216 int
1217 comptypes_check_different_types (tree type1, tree type2,
1218 bool *different_types_p)
1219 {
1220 struct comptypes_data data = { };
1221 bool ret = comptypes_internal (type1, type2, &data);
1222 *different_types_p = data.different_types_p;
1223
1224 return ret ? (data.warning_needed ? 2 : 1) : 0;
1225 }
1226
1227
1228 /* Like comptypes, but if it returns nonzero for struct and union
1229 types considered equivalent for aliasing purposes. */
1230
1231 bool
1232 comptypes_equiv_p (tree type1, tree type2)
1233 {
1234 struct comptypes_data data = { };
1235 data.equiv = true;
1236 bool ret = comptypes_internal (type1, type2, &data);
1237
1238 return ret;
1239 }
1240
1241 \f
1242 /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1243 or various other operations. If they are compatible but a warning may
1244 be needed if you use them together, 'warning_needed' in DATA is set.
1245 If one type is an enum and the other a compatible integer type, then
1246 this sets 'enum_and_int_p' in DATA to true (it is never set to
1247 false). If the types are compatible but different enough not to be
1248 permitted in C11 typedef redeclarations, then this sets
1249 'different_types_p' in DATA to true; it is never set to
1250 false, but may or may not be set if the types are incompatible.
1251 This differs from comptypes, in that we don't free the seen
1252 types. */
1253
1254 static bool
1255 comptypes_internal (const_tree type1, const_tree type2,
1256 struct comptypes_data *data)
1257 {
1258 const_tree t1 = type1;
1259 const_tree t2 = type2;
1260
1261 /* Suppress errors caused by previously reported errors. */
1262
1263 if (t1 == t2 || !t1 || !t2
1264 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1265 return true;
1266
1267 /* Enumerated types are compatible with integer types, but this is
1268 not transitive: two enumerated types in the same translation unit
1269 are compatible with each other only if they are the same type. */
1270
1271 if (TREE_CODE (t1) == ENUMERAL_TYPE
1272 && COMPLETE_TYPE_P (t1)
1273 && TREE_CODE (t2) != ENUMERAL_TYPE)
1274 {
1275 t1 = ENUM_UNDERLYING_TYPE (t1);
1276 if (TREE_CODE (t2) != VOID_TYPE)
1277 {
1278 data->enum_and_int_p = true;
1279 data->different_types_p = true;
1280 }
1281 }
1282 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1283 && COMPLETE_TYPE_P (t2)
1284 && TREE_CODE (t1) != ENUMERAL_TYPE)
1285 {
1286 t2 = ENUM_UNDERLYING_TYPE (t2);
1287 if (TREE_CODE (t1) != VOID_TYPE)
1288 {
1289 data->enum_and_int_p = true;
1290 data->different_types_p = true;
1291 }
1292 }
1293
1294 if (t1 == t2)
1295 return true;
1296
1297 /* Different classes of types can't be compatible. */
1298
1299 if (TREE_CODE (t1) != TREE_CODE (t2))
1300 return false;
1301
1302 /* Qualifiers must match. C99 6.7.3p9 */
1303
1304 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1305 return false;
1306
1307 /* Allow for two different type nodes which have essentially the same
1308 definition. Note that we already checked for equality of the type
1309 qualifiers (just above). */
1310
1311 if (TREE_CODE (t1) != ARRAY_TYPE
1312 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1313 return true;
1314
1315 int attrval;
1316
1317 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1318 if (!(attrval = comp_type_attributes (t1, t2)))
1319 return false;
1320
1321 if (2 == attrval)
1322 data->warning_needed = true;
1323
1324 switch (TREE_CODE (t1))
1325 {
1326 case INTEGER_TYPE:
1327 case FIXED_POINT_TYPE:
1328 case REAL_TYPE:
1329 case BITINT_TYPE:
1330 /* With these nodes, we can't determine type equivalence by
1331 looking at what is stored in the nodes themselves, because
1332 two nodes might have different TYPE_MAIN_VARIANTs but still
1333 represent the same type. For example, wchar_t and int could
1334 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1335 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1336 and are distinct types. On the other hand, int and the
1337 following typedef
1338
1339 typedef int INT __attribute((may_alias));
1340
1341 have identical properties, different TYPE_MAIN_VARIANTs, but
1342 represent the same type. The canonical type system keeps
1343 track of equivalence in this case, so we fall back on it. */
1344 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1345
1346 case POINTER_TYPE:
1347 /* Do not remove mode information. */
1348 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1349 return false;
1350 return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1351
1352 case FUNCTION_TYPE:
1353 return function_types_compatible_p (t1, t2, data);
1354
1355 case ARRAY_TYPE:
1356 {
1357 tree d1 = TYPE_DOMAIN (t1);
1358 tree d2 = TYPE_DOMAIN (t2);
1359 bool d1_variable, d2_variable;
1360 bool d1_zero, d2_zero;
1361
1362 /* Target types must match incl. qualifiers. */
1363 if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data))
1364 return false;
1365
1366 if ((d1 == NULL_TREE) != (d2 == NULL_TREE))
1367 data->different_types_p = true;
1368 /* Ignore size mismatches. */
1369 if (data->equiv)
1370 return true;
1371 /* Sizes must match unless one is missing or variable. */
1372 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1373 return true;
1374
1375 d1_zero = !TYPE_MAX_VALUE (d1);
1376 d2_zero = !TYPE_MAX_VALUE (d2);
1377
1378 d1_variable = (!d1_zero
1379 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1380 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1381 d2_variable = (!d2_zero
1382 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1383 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1384 d1_variable = d1_variable || (d1_zero && C_TYPE_VARIABLE_SIZE (t1));
1385 d2_variable = d2_variable || (d2_zero && C_TYPE_VARIABLE_SIZE (t2));
1386
1387 if (d1_variable != d2_variable)
1388 data->different_types_p = true;
1389 if (d1_variable || d2_variable)
1390 return true;
1391 if (d1_zero && d2_zero)
1392 return true;
1393 if (d1_zero || d2_zero
1394 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1395 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1396 return false;
1397
1398 return true;
1399 }
1400
1401 case ENUMERAL_TYPE:
1402 case RECORD_TYPE:
1403 case UNION_TYPE:
1404
1405 if (!flag_isoc23)
1406 return false;
1407
1408 return tagged_types_tu_compatible_p (t1, t2, data);
1409
1410 case VECTOR_TYPE:
1411 return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1412 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1413
1414 default:
1415 return false;
1416 }
1417 gcc_unreachable ();
1418 }
1419
1420 /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring
1421 their qualifiers, except for named address spaces. If the pointers point to
1422 different named addresses, then we must determine if one address space is a
1423 subset of the other. */
1424
1425 static bool
1426 comp_target_types (location_t location, tree ttl, tree ttr)
1427 {
1428 int val;
1429 int val_ped;
1430 tree mvl = TREE_TYPE (ttl);
1431 tree mvr = TREE_TYPE (ttr);
1432 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1433 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1434 addr_space_t as_common;
1435 bool enum_and_int_p;
1436
1437 /* Fail if pointers point to incompatible address spaces. */
1438 if (!addr_space_superset (asl, asr, &as_common))
1439 return 0;
1440
1441 /* For pedantic record result of comptypes on arrays before losing
1442 qualifiers on the element type below. */
1443 val_ped = 1;
1444
1445 if (TREE_CODE (mvl) == ARRAY_TYPE
1446 && TREE_CODE (mvr) == ARRAY_TYPE)
1447 val_ped = comptypes (mvl, mvr);
1448
1449 /* Qualifiers on element types of array types that are
1450 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1451
1452 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1453 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1454 : TYPE_MAIN_VARIANT (mvl));
1455
1456 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1457 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1458 : TYPE_MAIN_VARIANT (mvr));
1459
1460 enum_and_int_p = false;
1461 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1462
1463 if (val == 1 && val_ped != 1)
1464 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1465 "in ISO C before C23");
1466
1467 if (val == 2)
1468 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1469
1470 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1471 warning_at (location, OPT_Wc___compat,
1472 "pointer target types incompatible in C++");
1473
1474 return val;
1475 }
1476 \f
1477 /* Subroutines of `comptypes'. */
1478
1479 /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are
1480 compatible. The two types are not the same (which has been
1481 checked earlier in comptypes_internal). */
1482
1483 static bool
1484 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1485 struct comptypes_data *data)
1486 {
1487 tree s1, s2;
1488
1489 /* We have to verify that the tags of the types are the same. This
1490 is harder than it looks because this may be a typedef, so we have
1491 to go look at the original type. It may even be a typedef of a
1492 typedef...
1493 In the case of compiler-created builtin structs the TYPE_DECL
1494 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1495 while (TYPE_NAME (t1)
1496 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1497 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1498 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1499
1500 while (TYPE_NAME (t2)
1501 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1502 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1503 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1504
1505 if (TYPE_NAME (t1) != TYPE_NAME (t2))
1506 return false;
1507
1508 if (!data->anon_field && NULL_TREE == TYPE_NAME (t1))
1509 return false;
1510
1511 if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2))
1512 data->different_types_p = true;
1513
1514 /* Incomplete types are incompatible inside a TU. */
1515 if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL)
1516 return false;
1517
1518 if (ENUMERAL_TYPE != TREE_CODE (t1)
1519 && (TYPE_REVERSE_STORAGE_ORDER (t1)
1520 != TYPE_REVERSE_STORAGE_ORDER (t2)))
1521 return false;
1522
1523 /* For types already being looked at in some active
1524 invocation of this function, assume compatibility.
1525 The cache is built as a linked list on the stack
1526 with the head of the list passed downwards. */
1527 for (const struct tagged_tu_seen_cache *t = data->cache;
1528 t != NULL; t = t->next)
1529 if (t->t1 == t1 && t->t2 == t2)
1530 return true;
1531
1532 const struct tagged_tu_seen_cache entry = { data->cache, t1, t2 };
1533
1534 switch (TREE_CODE (t1))
1535 {
1536 case ENUMERAL_TYPE:
1537 {
1538 if (!comptypes (ENUM_UNDERLYING_TYPE (t1), ENUM_UNDERLYING_TYPE (t2)))
1539 return false;
1540
1541 /* Speed up the case where the type values are in the same order. */
1542 tree tv1 = TYPE_VALUES (t1);
1543 tree tv2 = TYPE_VALUES (t2);
1544
1545 if (tv1 == tv2)
1546 return true;
1547
1548 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1549 {
1550 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1551 break;
1552
1553 if (simple_cst_equal (DECL_INITIAL (TREE_VALUE (tv1)),
1554 DECL_INITIAL (TREE_VALUE (tv2))) != 1)
1555 break;
1556 }
1557
1558 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1559 return true;
1560
1561 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1562 return false;
1563
1564 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1565 return false;
1566
1567 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1568 {
1569 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1570
1571 if (s2 == NULL
1572 || simple_cst_equal (DECL_INITIAL (TREE_VALUE (s1)),
1573 DECL_INITIAL (TREE_VALUE (s2))) != 1)
1574 return false;
1575 }
1576
1577 return true;
1578 }
1579
1580 case UNION_TYPE:
1581 case RECORD_TYPE:
1582
1583 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1584 return false;
1585
1586 if (data->equiv && (C_TYPE_VARIABLE_SIZE (t1) || C_TYPE_VARIABLE_SIZE (t2)))
1587 return false;
1588
1589 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1590 s1 && s2;
1591 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1592 {
1593 gcc_assert (TREE_CODE (s1) == FIELD_DECL);
1594 gcc_assert (TREE_CODE (s2) == FIELD_DECL);
1595
1596 if (DECL_NAME (s1) != DECL_NAME (s2))
1597 return false;
1598
1599 if (DECL_ALIGN (s1) != DECL_ALIGN (s2))
1600 return false;
1601
1602 data->anon_field = !DECL_NAME (s1);
1603
1604 data->cache = &entry;
1605 if (!comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), data))
1606 return false;
1607
1608 tree st1 = TYPE_SIZE (TREE_TYPE (s1));
1609 tree st2 = TYPE_SIZE (TREE_TYPE (s2));
1610
1611 if (data->equiv
1612 && st1 && TREE_CODE (st1) == INTEGER_CST
1613 && st2 && TREE_CODE (st2) == INTEGER_CST
1614 && !tree_int_cst_equal (st1, st2))
1615 return false;
1616 }
1617 return true;
1618
1619 default:
1620 gcc_unreachable ();
1621 }
1622 }
1623
1624 /* Return true if two function types F1 and F2 are compatible.
1625 If either type specifies no argument types,
1626 the other must specify a fixed number of self-promoting arg types.
1627 Otherwise, if one type specifies only the number of arguments,
1628 the other must specify that number of self-promoting arg types.
1629 Otherwise, the argument types must match. */
1630
1631 static bool
1632 function_types_compatible_p (const_tree f1, const_tree f2,
1633 struct comptypes_data *data)
1634 {
1635 tree args1, args2;
1636 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1637 int val = 1;
1638 int val1;
1639 tree ret1, ret2;
1640
1641 ret1 = TREE_TYPE (f1);
1642 ret2 = TREE_TYPE (f2);
1643
1644 /* 'volatile' qualifiers on a function's return type used to mean
1645 the function is noreturn. */
1646 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1647 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1648 if (TYPE_VOLATILE (ret1))
1649 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1650 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1651 if (TYPE_VOLATILE (ret2))
1652 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1653 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1654 val = comptypes_internal (ret1, ret2, data);
1655 if (val == 0)
1656 return 0;
1657
1658 args1 = TYPE_ARG_TYPES (f1);
1659 args2 = TYPE_ARG_TYPES (f2);
1660
1661 if ((args1 == NULL_TREE) != (args2 == NULL_TREE))
1662 data->different_types_p = true;
1663
1664 /* An unspecified parmlist matches any specified parmlist
1665 whose argument types don't need default promotions. */
1666
1667 if (args1 == NULL_TREE)
1668 {
1669 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1670 return 0;
1671 if (!self_promoting_args_p (args2))
1672 return 0;
1673 /* If one of these types comes from a non-prototype fn definition,
1674 compare that with the other type's arglist.
1675 If they don't match, ask for a warning (but no error). */
1676 if (TYPE_ACTUAL_ARG_TYPES (f1)
1677 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1678 data) != 1)
1679 {
1680 val = 1;
1681 data->warning_needed = true;
1682 }
1683 return val;
1684 }
1685 if (args2 == NULL_TREE)
1686 {
1687 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1688 return 0;
1689 if (!self_promoting_args_p (args1))
1690 return 0;
1691 if (TYPE_ACTUAL_ARG_TYPES (f2)
1692 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1693 data) != 1)
1694 {
1695 val = 1;
1696 data->warning_needed = true;
1697 }
1698 return val;
1699 }
1700
1701 /* Both types have argument lists: compare them and propagate results. */
1702 val1 = type_lists_compatible_p (args1, args2, data);
1703 return val1;
1704 }
1705
1706 /* Check two lists of types for compatibility, returning false for
1707 incompatible, true for compatible. */
1708
1709 static bool
1710 type_lists_compatible_p (const_tree args1, const_tree args2,
1711 struct comptypes_data *data)
1712 {
1713 while (1)
1714 {
1715 tree a1, mv1, a2, mv2;
1716 if (args1 == NULL_TREE && args2 == NULL_TREE)
1717 return true;
1718 /* If one list is shorter than the other,
1719 they fail to match. */
1720 if (args1 == NULL_TREE || args2 == NULL_TREE)
1721 return 0;
1722 mv1 = a1 = TREE_VALUE (args1);
1723 mv2 = a2 = TREE_VALUE (args2);
1724 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1725 mv1 = (TYPE_ATOMIC (mv1)
1726 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1727 TYPE_QUAL_ATOMIC)
1728 : TYPE_MAIN_VARIANT (mv1));
1729 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1730 mv2 = (TYPE_ATOMIC (mv2)
1731 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1732 TYPE_QUAL_ATOMIC)
1733 : TYPE_MAIN_VARIANT (mv2));
1734 /* A null pointer instead of a type
1735 means there is supposed to be an argument
1736 but nothing is specified about what type it has.
1737 So match anything that self-promotes. */
1738 if ((a1 == NULL_TREE) != (a2 == NULL_TREE))
1739 data->different_types_p = true;
1740 if (a1 == NULL_TREE)
1741 {
1742 if (c_type_promotes_to (a2) != a2)
1743 return 0;
1744 }
1745 else if (a2 == NULL_TREE)
1746 {
1747 if (c_type_promotes_to (a1) != a1)
1748 return 0;
1749 }
1750 /* If one of the lists has an error marker, ignore this arg. */
1751 else if (TREE_CODE (a1) == ERROR_MARK
1752 || TREE_CODE (a2) == ERROR_MARK)
1753 ;
1754 else if (!comptypes_internal (mv1, mv2, data))
1755 {
1756 data->different_types_p = true;
1757 /* Allow wait (union {union wait *u; int *i} *)
1758 and wait (union wait *) to be compatible. */
1759 if (TREE_CODE (a1) == UNION_TYPE
1760 && (TYPE_NAME (a1) == NULL_TREE
1761 || TYPE_TRANSPARENT_AGGR (a1))
1762 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1763 && tree_int_cst_equal (TYPE_SIZE (a1),
1764 TYPE_SIZE (a2)))
1765 {
1766 tree memb;
1767 for (memb = TYPE_FIELDS (a1);
1768 memb; memb = DECL_CHAIN (memb))
1769 {
1770 tree mv3 = TREE_TYPE (memb);
1771 if (mv3 && mv3 != error_mark_node
1772 && TREE_CODE (mv3) != ARRAY_TYPE)
1773 mv3 = (TYPE_ATOMIC (mv3)
1774 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1775 TYPE_QUAL_ATOMIC)
1776 : TYPE_MAIN_VARIANT (mv3));
1777 if (comptypes_internal (mv3, mv2, data))
1778 break;
1779 }
1780 if (memb == NULL_TREE)
1781 return 0;
1782 }
1783 else if (TREE_CODE (a2) == UNION_TYPE
1784 && (TYPE_NAME (a2) == NULL_TREE
1785 || TYPE_TRANSPARENT_AGGR (a2))
1786 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1787 && tree_int_cst_equal (TYPE_SIZE (a2),
1788 TYPE_SIZE (a1)))
1789 {
1790 tree memb;
1791 for (memb = TYPE_FIELDS (a2);
1792 memb; memb = DECL_CHAIN (memb))
1793 {
1794 tree mv3 = TREE_TYPE (memb);
1795 if (mv3 && mv3 != error_mark_node
1796 && TREE_CODE (mv3) != ARRAY_TYPE)
1797 mv3 = (TYPE_ATOMIC (mv3)
1798 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1799 TYPE_QUAL_ATOMIC)
1800 : TYPE_MAIN_VARIANT (mv3));
1801 if (comptypes_internal (mv3, mv1, data))
1802 break;
1803 }
1804 if (memb == NULL_TREE)
1805 return 0;
1806 }
1807 else
1808 return 0;
1809 }
1810
1811 args1 = TREE_CHAIN (args1);
1812 args2 = TREE_CHAIN (args2);
1813 }
1814 }
1815 \f
1816 /* Compute the size to increment a pointer by. When a function type or void
1817 type or incomplete type is passed, size_one_node is returned.
1818 This function does not emit any diagnostics; the caller is responsible
1819 for that. */
1820
1821 static tree
1822 c_size_in_bytes (const_tree type)
1823 {
1824 enum tree_code code = TREE_CODE (type);
1825
1826 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1827 || !COMPLETE_TYPE_P (type))
1828 return size_one_node;
1829
1830 /* Convert in case a char is more than one unit. */
1831 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1832 size_int (TYPE_PRECISION (char_type_node)
1833 / BITS_PER_UNIT));
1834 }
1835 \f
1836 /* Return either DECL or its known constant value (if it has one). */
1837
1838 tree
1839 decl_constant_value_1 (tree decl, bool in_init)
1840 {
1841 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1842 TREE_CODE (decl) != PARM_DECL
1843 && !TREE_THIS_VOLATILE (decl)
1844 && TREE_READONLY (decl)
1845 && DECL_INITIAL (decl) != NULL_TREE
1846 && !error_operand_p (DECL_INITIAL (decl))
1847 /* This is invalid if initial value is not constant.
1848 If it has either a function call, a memory reference,
1849 or a variable, then re-evaluating it could give different results. */
1850 && TREE_CONSTANT (DECL_INITIAL (decl))
1851 /* Check for cases where this is sub-optimal, even though valid. */
1852 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1853 return DECL_INITIAL (decl);
1854 return decl;
1855 }
1856
1857 /* Return either DECL or its known constant value (if it has one).
1858 Like the above, but always return decl outside of functions. */
1859
1860 tree
1861 decl_constant_value (tree decl)
1862 {
1863 /* Don't change a variable array bound or initial value to a constant
1864 in a place where a variable is invalid. */
1865 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1866 }
1867
1868 /* Convert the array expression EXP to a pointer. */
1869 static tree
1870 array_to_pointer_conversion (location_t loc, tree exp)
1871 {
1872 tree orig_exp = exp;
1873 tree type = TREE_TYPE (exp);
1874 tree adr;
1875 tree restype = TREE_TYPE (type);
1876 tree ptrtype;
1877
1878 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1879
1880 STRIP_TYPE_NOPS (exp);
1881
1882 copy_warning (exp, orig_exp);
1883
1884 ptrtype = build_pointer_type (restype);
1885
1886 if (INDIRECT_REF_P (exp))
1887 return convert (ptrtype, TREE_OPERAND (exp, 0));
1888
1889 /* In C++ array compound literals are temporary objects unless they are
1890 const or appear in namespace scope, so they are destroyed too soon
1891 to use them for much of anything (c++/53220). */
1892 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1893 {
1894 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1895 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1896 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1897 "converting an array compound literal to a pointer "
1898 "leads to a dangling pointer in C++");
1899 }
1900
1901 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1902 return convert (ptrtype, adr);
1903 }
1904
1905 /* Convert the function expression EXP to a pointer. */
1906 static tree
1907 function_to_pointer_conversion (location_t loc, tree exp)
1908 {
1909 tree orig_exp = exp;
1910
1911 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1912
1913 STRIP_TYPE_NOPS (exp);
1914
1915 copy_warning (exp, orig_exp);
1916
1917 return build_unary_op (loc, ADDR_EXPR, exp, false);
1918 }
1919
1920 /* Mark EXP as read, not just set, for set but not used -Wunused
1921 warning purposes. */
1922
1923 void
1924 mark_exp_read (tree exp)
1925 {
1926 switch (TREE_CODE (exp))
1927 {
1928 case VAR_DECL:
1929 case PARM_DECL:
1930 DECL_READ_P (exp) = 1;
1931 break;
1932 case ARRAY_REF:
1933 case COMPONENT_REF:
1934 case MODIFY_EXPR:
1935 case REALPART_EXPR:
1936 case IMAGPART_EXPR:
1937 CASE_CONVERT:
1938 case ADDR_EXPR:
1939 case VIEW_CONVERT_EXPR:
1940 mark_exp_read (TREE_OPERAND (exp, 0));
1941 break;
1942 case COMPOUND_EXPR:
1943 /* Pattern match what build_atomic_assign produces with modifycode
1944 NOP_EXPR. */
1945 if (VAR_P (TREE_OPERAND (exp, 1))
1946 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1947 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1948 {
1949 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1950 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1951 if (TREE_CODE (t1) == TARGET_EXPR
1952 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1953 && TREE_CODE (t2) == CALL_EXPR)
1954 {
1955 tree fndecl = get_callee_fndecl (t2);
1956 tree arg = NULL_TREE;
1957 if (fndecl
1958 && TREE_CODE (fndecl) == FUNCTION_DECL
1959 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1960 && call_expr_nargs (t2) >= 2)
1961 switch (DECL_FUNCTION_CODE (fndecl))
1962 {
1963 case BUILT_IN_ATOMIC_STORE:
1964 arg = CALL_EXPR_ARG (t2, 1);
1965 break;
1966 case BUILT_IN_ATOMIC_STORE_1:
1967 case BUILT_IN_ATOMIC_STORE_2:
1968 case BUILT_IN_ATOMIC_STORE_4:
1969 case BUILT_IN_ATOMIC_STORE_8:
1970 case BUILT_IN_ATOMIC_STORE_16:
1971 arg = CALL_EXPR_ARG (t2, 0);
1972 break;
1973 default:
1974 break;
1975 }
1976 if (arg)
1977 {
1978 STRIP_NOPS (arg);
1979 if (TREE_CODE (arg) == ADDR_EXPR
1980 && DECL_P (TREE_OPERAND (arg, 0))
1981 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
1982 mark_exp_read (TREE_OPERAND (arg, 0));
1983 }
1984 }
1985 }
1986 /* FALLTHRU */
1987 case C_MAYBE_CONST_EXPR:
1988 mark_exp_read (TREE_OPERAND (exp, 1));
1989 break;
1990 default:
1991 break;
1992 }
1993 }
1994
1995 /* Perform the default conversion of arrays and functions to pointers.
1996 Return the result of converting EXP. For any other expression, just
1997 return EXP.
1998
1999 LOC is the location of the expression. */
2000
2001 struct c_expr
2002 default_function_array_conversion (location_t loc, struct c_expr exp)
2003 {
2004 tree orig_exp = exp.value;
2005 tree type = TREE_TYPE (exp.value);
2006 enum tree_code code = TREE_CODE (type);
2007
2008 switch (code)
2009 {
2010 case ARRAY_TYPE:
2011 {
2012 bool not_lvalue = false;
2013 bool lvalue_array_p;
2014
2015 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2016 || CONVERT_EXPR_P (exp.value))
2017 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2018 {
2019 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2020 not_lvalue = true;
2021 exp.value = TREE_OPERAND (exp.value, 0);
2022 }
2023
2024 copy_warning (exp.value, orig_exp);
2025
2026 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2027 if (!flag_isoc99 && !lvalue_array_p)
2028 {
2029 /* Before C99, non-lvalue arrays do not decay to pointers.
2030 Normally, using such an array would be invalid; but it can
2031 be used correctly inside sizeof or as a statement expression.
2032 Thus, do not give an error here; an error will result later. */
2033 return exp;
2034 }
2035
2036 exp.value = array_to_pointer_conversion (loc, exp.value);
2037 }
2038 break;
2039 case FUNCTION_TYPE:
2040 exp.value = function_to_pointer_conversion (loc, exp.value);
2041 break;
2042 default:
2043 break;
2044 }
2045
2046 return exp;
2047 }
2048
2049 struct c_expr
2050 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2051 {
2052 mark_exp_read (exp.value);
2053 return default_function_array_conversion (loc, exp);
2054 }
2055
2056 /* Return whether EXPR should be treated as an atomic lvalue for the
2057 purposes of load and store handling. */
2058
2059 static bool
2060 really_atomic_lvalue (tree expr)
2061 {
2062 if (error_operand_p (expr))
2063 return false;
2064 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2065 return false;
2066 if (!lvalue_p (expr))
2067 return false;
2068
2069 /* Ignore _Atomic on register variables, since their addresses can't
2070 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2071 sequences wouldn't work. Ignore _Atomic on structures containing
2072 bit-fields, since accessing elements of atomic structures or
2073 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2074 it's undefined at translation time or execution time, and the
2075 normal atomic sequences again wouldn't work. */
2076 while (handled_component_p (expr))
2077 {
2078 if (TREE_CODE (expr) == COMPONENT_REF
2079 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2080 return false;
2081 expr = TREE_OPERAND (expr, 0);
2082 }
2083 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2084 return false;
2085 return true;
2086 }
2087
2088 /* If EXPR is a named constant (C23) derived from a constexpr variable
2089 - that is, a reference to such a variable, or a member extracted by
2090 a sequence of structure and union (but not array) member accesses
2091 (where union member accesses must access the same member as
2092 initialized) - then return the corresponding initializer;
2093 otherwise, return NULL_TREE. */
2094
2095 static tree
2096 maybe_get_constexpr_init (tree expr)
2097 {
2098 tree decl = NULL_TREE;
2099 if (TREE_CODE (expr) == VAR_DECL)
2100 decl = expr;
2101 else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2102 decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2103 if (decl
2104 && C_DECL_DECLARED_CONSTEXPR (decl)
2105 && DECL_INITIAL (decl) != NULL_TREE
2106 && !error_operand_p (DECL_INITIAL (decl)))
2107 return DECL_INITIAL (decl);
2108 if (TREE_CODE (expr) != COMPONENT_REF)
2109 return NULL_TREE;
2110 tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2111 if (inner == NULL_TREE)
2112 return NULL_TREE;
2113 while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2114 && !error_operand_p (inner)
2115 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2116 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2117 inner = TREE_OPERAND (inner, 0);
2118 if (TREE_CODE (inner) != CONSTRUCTOR)
2119 return NULL_TREE;
2120 tree field = TREE_OPERAND (expr, 1);
2121 unsigned HOST_WIDE_INT cidx;
2122 tree cfield, cvalue;
2123 bool have_other_init = false;
2124 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2125 {
2126 if (cfield == field)
2127 return cvalue;
2128 have_other_init = true;
2129 }
2130 if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2131 && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2132 return NULL_TREE;
2133 /* Return a default initializer. */
2134 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2135 return build_constructor (TREE_TYPE (expr), NULL);
2136 return build_zero_cst (TREE_TYPE (expr));
2137 }
2138
2139 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2140 including converting functions and arrays to pointers if CONVERT_P.
2141 If READ_P, also mark the expression as having been read. If
2142 FOR_INIT, constexpr expressions of structure and union type should
2143 be replaced by the corresponding CONSTRUCTOR; otherwise, only
2144 constexpr scalars (including elements of structures and unions) are
2145 replaced by their initializers. */
2146
2147 struct c_expr
2148 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2149 bool convert_p, bool read_p, bool for_init)
2150 {
2151 bool force_non_npc = false;
2152 if (read_p)
2153 mark_exp_read (exp.value);
2154 if (convert_p)
2155 exp = default_function_array_conversion (loc, exp);
2156 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2157 exp.value = require_complete_type (loc, exp.value);
2158 if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2159 {
2160 tree init = maybe_get_constexpr_init (exp.value);
2161 if (init != NULL_TREE)
2162 {
2163 /* A named constant of pointer type or type nullptr_t is not
2164 a null pointer constant even if the initializer is
2165 one. */
2166 if (TREE_CODE (init) == INTEGER_CST
2167 && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2168 && integer_zerop (init))
2169 force_non_npc = true;
2170 exp.value = init;
2171 }
2172 }
2173 if (really_atomic_lvalue (exp.value))
2174 {
2175 vec<tree, va_gc> *params;
2176 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2177 tree expr_type = TREE_TYPE (exp.value);
2178 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2179 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2180
2181 gcc_assert (TYPE_ATOMIC (expr_type));
2182
2183 /* Expansion of a generic atomic load may require an addition
2184 element, so allocate enough to prevent a resize. */
2185 vec_alloc (params, 4);
2186
2187 /* Remove the qualifiers for the rest of the expressions and
2188 create the VAL temp variable to hold the RHS. */
2189 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2190 tmp = create_tmp_var_raw (nonatomic_type);
2191 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2192 TREE_ADDRESSABLE (tmp) = 1;
2193 /* Do not disable warnings for TMP even though it's artificial.
2194 -Winvalid-memory-model depends on it. */
2195
2196 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2197 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2198 params->quick_push (expr_addr);
2199 params->quick_push (tmp_addr);
2200 params->quick_push (seq_cst);
2201 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2202
2203 /* EXPR is always read. */
2204 mark_exp_read (exp.value);
2205
2206 /* Return tmp which contains the value loaded. */
2207 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2208 NULL_TREE, NULL_TREE);
2209 }
2210 if (convert_p && !error_operand_p (exp.value)
2211 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2212 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2213 if (force_non_npc)
2214 exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2215
2216 {
2217 tree false_value, true_value;
2218 if (convert_p && !error_operand_p (exp.value)
2219 && c_hardbool_type_attr (TREE_TYPE (exp.value),
2220 &false_value, &true_value))
2221 {
2222 tree t = save_expr (exp.value);
2223
2224 mark_exp_read (exp.value);
2225
2226 tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
2227 tree expr = build_call_expr_loc (loc, trapfn, 0);
2228 expr = build_compound_expr (loc, expr, boolean_true_node);
2229 expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2230 fold_build2_loc (loc, NE_EXPR,
2231 boolean_type_node,
2232 t, true_value),
2233 expr, boolean_true_node);
2234 expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2235 fold_build2_loc (loc, NE_EXPR,
2236 boolean_type_node,
2237 t, false_value),
2238 expr, boolean_false_node);
2239
2240 exp.value = expr;
2241 }
2242 }
2243
2244 return exp;
2245 }
2246
2247 /* EXP is an expression of integer type. Apply the integer promotions
2248 to it and return the promoted value. */
2249
2250 tree
2251 perform_integral_promotions (tree exp)
2252 {
2253 tree type = TREE_TYPE (exp);
2254 enum tree_code code = TREE_CODE (type);
2255
2256 gcc_assert (INTEGRAL_TYPE_P (type));
2257
2258 /* Convert enums to the result of applying the integer promotions to
2259 their underlying type. */
2260 if (code == ENUMERAL_TYPE)
2261 {
2262 type = ENUM_UNDERLYING_TYPE (type);
2263 if (c_promoting_integer_type_p (type))
2264 {
2265 if (TYPE_UNSIGNED (type)
2266 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2267 type = unsigned_type_node;
2268 else
2269 type = integer_type_node;
2270 }
2271
2272 return convert (type, exp);
2273 }
2274
2275 /* ??? This should no longer be needed now bit-fields have their
2276 proper types. */
2277 if (TREE_CODE (exp) == COMPONENT_REF
2278 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
2279 {
2280 if (TREE_CODE (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)))
2281 == BITINT_TYPE)
2282 return convert (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)), exp);
2283 /* If it's thinner than an int, promote it like a
2284 c_promoting_integer_type_p, otherwise leave it alone. */
2285 if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2286 TYPE_PRECISION (integer_type_node)) < 0)
2287 return convert (integer_type_node, exp);
2288 }
2289
2290 if (c_promoting_integer_type_p (type))
2291 {
2292 /* Preserve unsignedness if not really getting any wider. */
2293 if (TYPE_UNSIGNED (type)
2294 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2295 return convert (unsigned_type_node, exp);
2296
2297 return convert (integer_type_node, exp);
2298 }
2299
2300 return exp;
2301 }
2302
2303
2304 /* Perform default promotions for C data used in expressions.
2305 Enumeral types or short or char are converted to int.
2306 In addition, manifest constants symbols are replaced by their values. */
2307
2308 tree
2309 default_conversion (tree exp)
2310 {
2311 tree orig_exp;
2312 tree type = TREE_TYPE (exp);
2313 enum tree_code code = TREE_CODE (type);
2314 tree promoted_type;
2315
2316 mark_exp_read (exp);
2317
2318 /* Functions and arrays have been converted during parsing. */
2319 gcc_assert (code != FUNCTION_TYPE);
2320 if (code == ARRAY_TYPE)
2321 return exp;
2322
2323 /* Constants can be used directly unless they're not loadable. */
2324 if (TREE_CODE (exp) == CONST_DECL)
2325 exp = DECL_INITIAL (exp);
2326
2327 /* Strip no-op conversions. */
2328 orig_exp = exp;
2329 STRIP_TYPE_NOPS (exp);
2330
2331 copy_warning (exp, orig_exp);
2332
2333 if (code == VOID_TYPE)
2334 {
2335 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2336 "void value not ignored as it ought to be");
2337 return error_mark_node;
2338 }
2339
2340 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2341 if (exp == error_mark_node)
2342 return error_mark_node;
2343
2344 promoted_type = targetm.promoted_type (type);
2345 if (promoted_type)
2346 return convert (promoted_type, exp);
2347
2348 if (INTEGRAL_TYPE_P (type))
2349 return perform_integral_promotions (exp);
2350
2351 return exp;
2352 }
2353 \f
2354 /* Look up COMPONENT in a structure or union TYPE.
2355
2356 If the component name is not found, returns NULL_TREE. Otherwise,
2357 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2358 stepping down the chain to the component, which is in the last
2359 TREE_VALUE of the list. Normally the list is of length one, but if
2360 the component is embedded within (nested) anonymous structures or
2361 unions, the list steps down the chain to the component. */
2362
2363 static tree
2364 lookup_field (tree type, tree component)
2365 {
2366 tree field;
2367
2368 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2369 to the field elements. Use a binary search on this array to quickly
2370 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2371 will always be set for structures which have many elements.
2372
2373 Duplicate field checking replaces duplicates with NULL_TREE so
2374 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2375 case just iterate using DECL_CHAIN. */
2376
2377 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2378 && !seen_error ())
2379 {
2380 int bot, top, half;
2381 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2382
2383 field = TYPE_FIELDS (type);
2384 bot = 0;
2385 top = TYPE_LANG_SPECIFIC (type)->s->len;
2386 while (top - bot > 1)
2387 {
2388 half = (top - bot + 1) >> 1;
2389 field = field_array[bot+half];
2390
2391 if (DECL_NAME (field) == NULL_TREE)
2392 {
2393 /* Step through all anon unions in linear fashion. */
2394 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2395 {
2396 field = field_array[bot++];
2397 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2398 {
2399 tree anon = lookup_field (TREE_TYPE (field), component);
2400
2401 if (anon)
2402 return tree_cons (NULL_TREE, field, anon);
2403
2404 /* The Plan 9 compiler permits referring
2405 directly to an anonymous struct/union field
2406 using a typedef name. */
2407 if (flag_plan9_extensions
2408 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2409 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2410 == TYPE_DECL)
2411 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2412 == component))
2413 break;
2414 }
2415 }
2416
2417 /* Entire record is only anon unions. */
2418 if (bot > top)
2419 return NULL_TREE;
2420
2421 /* Restart the binary search, with new lower bound. */
2422 continue;
2423 }
2424
2425 if (DECL_NAME (field) == component)
2426 break;
2427 if (DECL_NAME (field) < component)
2428 bot += half;
2429 else
2430 top = bot + half;
2431 }
2432
2433 if (DECL_NAME (field_array[bot]) == component)
2434 field = field_array[bot];
2435 else if (DECL_NAME (field) != component)
2436 return NULL_TREE;
2437 }
2438 else
2439 {
2440 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2441 {
2442 if (DECL_NAME (field) == NULL_TREE
2443 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2444 {
2445 tree anon = lookup_field (TREE_TYPE (field), component);
2446
2447 if (anon)
2448 return tree_cons (NULL_TREE, field, anon);
2449
2450 /* The Plan 9 compiler permits referring directly to an
2451 anonymous struct/union field using a typedef
2452 name. */
2453 if (flag_plan9_extensions
2454 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2455 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2456 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2457 == component))
2458 break;
2459 }
2460
2461 if (DECL_NAME (field) == component)
2462 break;
2463 }
2464
2465 if (field == NULL_TREE)
2466 return NULL_TREE;
2467 }
2468
2469 return tree_cons (NULL_TREE, field, NULL_TREE);
2470 }
2471
2472 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2473
2474 static void
2475 lookup_field_fuzzy_find_candidates (tree type, tree component,
2476 vec<tree> *candidates)
2477 {
2478 tree field;
2479 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2480 {
2481 if (DECL_NAME (field) == NULL_TREE
2482 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2483 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2484 candidates);
2485
2486 if (DECL_NAME (field))
2487 candidates->safe_push (DECL_NAME (field));
2488 }
2489 }
2490
2491 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2492 rather than returning a TREE_LIST for an exact match. */
2493
2494 static tree
2495 lookup_field_fuzzy (tree type, tree component)
2496 {
2497 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2498
2499 /* First, gather a list of candidates. */
2500 auto_vec <tree> candidates;
2501
2502 lookup_field_fuzzy_find_candidates (type, component,
2503 &candidates);
2504
2505 return find_closest_identifier (component, &candidates);
2506 }
2507
2508 /* Support function for build_component_ref's error-handling.
2509
2510 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2511 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2512
2513 static bool
2514 should_suggest_deref_p (tree datum_type)
2515 {
2516 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2517 allows "." for ptrs; we could be handling a failed attempt
2518 to access a property. */
2519 if (c_dialect_objc ())
2520 return false;
2521
2522 /* Only suggest it for pointers... */
2523 if (TREE_CODE (datum_type) != POINTER_TYPE)
2524 return false;
2525
2526 /* ...to structs/unions. */
2527 tree underlying_type = TREE_TYPE (datum_type);
2528 enum tree_code code = TREE_CODE (underlying_type);
2529 if (code == RECORD_TYPE || code == UNION_TYPE)
2530 return true;
2531 else
2532 return false;
2533 }
2534
2535 /* Make an expression to refer to the COMPONENT field of structure or
2536 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2537 location of the COMPONENT_REF. COMPONENT_LOC is the location
2538 of COMPONENT. ARROW_LOC is the location of the first -> operand if
2539 it is from -> operator. */
2540
2541 tree
2542 build_component_ref (location_t loc, tree datum, tree component,
2543 location_t component_loc, location_t arrow_loc)
2544 {
2545 tree type = TREE_TYPE (datum);
2546 enum tree_code code = TREE_CODE (type);
2547 tree field = NULL;
2548 tree ref;
2549 bool datum_lvalue = lvalue_p (datum);
2550
2551 if (!objc_is_public (datum, component))
2552 return error_mark_node;
2553
2554 /* Detect Objective-C property syntax object.property. */
2555 if (c_dialect_objc ()
2556 && (ref = objc_maybe_build_component_ref (datum, component)))
2557 return ref;
2558
2559 /* See if there is a field or component with name COMPONENT. */
2560
2561 if (code == RECORD_TYPE || code == UNION_TYPE)
2562 {
2563 if (!COMPLETE_TYPE_P (type))
2564 {
2565 c_incomplete_type_error (loc, NULL_TREE, type);
2566 return error_mark_node;
2567 }
2568
2569 field = lookup_field (type, component);
2570
2571 if (!field)
2572 {
2573 tree guessed_id = lookup_field_fuzzy (type, component);
2574 if (guessed_id)
2575 {
2576 /* Attempt to provide a fixit replacement hint, if
2577 we have a valid range for the component. */
2578 location_t reported_loc
2579 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2580 gcc_rich_location rich_loc (reported_loc);
2581 if (component_loc != UNKNOWN_LOCATION)
2582 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2583 error_at (&rich_loc,
2584 "%qT has no member named %qE; did you mean %qE?",
2585 type, component, guessed_id);
2586 }
2587 else
2588 error_at (loc, "%qT has no member named %qE", type, component);
2589 return error_mark_node;
2590 }
2591
2592 /* Accessing elements of atomic structures or unions is undefined
2593 behavior (C11 6.5.2.3#5). */
2594 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2595 {
2596 if (code == RECORD_TYPE)
2597 warning_at (loc, 0, "accessing a member %qE of an atomic "
2598 "structure %qE", component, datum);
2599 else
2600 warning_at (loc, 0, "accessing a member %qE of an atomic "
2601 "union %qE", component, datum);
2602 }
2603
2604 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2605 This might be better solved in future the way the C++ front
2606 end does it - by giving the anonymous entities each a
2607 separate name and type, and then have build_component_ref
2608 recursively call itself. We can't do that here. */
2609 do
2610 {
2611 tree subdatum = TREE_VALUE (field);
2612 int quals;
2613 tree subtype;
2614 bool use_datum_quals;
2615
2616 if (TREE_TYPE (subdatum) == error_mark_node)
2617 return error_mark_node;
2618
2619 /* If this is an rvalue, it does not have qualifiers in C
2620 standard terms and we must avoid propagating such
2621 qualifiers down to a non-lvalue array that is then
2622 converted to a pointer. */
2623 use_datum_quals = (datum_lvalue
2624 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2625
2626 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2627 if (use_datum_quals)
2628 quals |= TYPE_QUALS (TREE_TYPE (datum));
2629 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2630
2631 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2632 NULL_TREE);
2633 SET_EXPR_LOCATION (ref, loc);
2634 if (TREE_READONLY (subdatum)
2635 || (use_datum_quals && TREE_READONLY (datum)))
2636 TREE_READONLY (ref) = 1;
2637 if (TREE_THIS_VOLATILE (subdatum)
2638 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2639 TREE_THIS_VOLATILE (ref) = 1;
2640
2641 if (TREE_UNAVAILABLE (subdatum))
2642 error_unavailable_use (subdatum, NULL_TREE);
2643 else if (TREE_DEPRECATED (subdatum))
2644 warn_deprecated_use (subdatum, NULL_TREE);
2645
2646 datum = ref;
2647
2648 field = TREE_CHAIN (field);
2649 }
2650 while (field);
2651
2652 return ref;
2653 }
2654 else if (should_suggest_deref_p (type))
2655 {
2656 /* Special-case the error message for "ptr.field" for the case
2657 where the user has confused "." vs "->". */
2658 rich_location richloc (line_table, loc);
2659 if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
2660 {
2661 richloc.add_fixit_insert_before (arrow_loc, "(*");
2662 richloc.add_fixit_insert_after (arrow_loc, ")");
2663 error_at (&richloc,
2664 "%qE is a pointer to pointer; did you mean to dereference "
2665 "it before applying %<->%> to it?",
2666 TREE_OPERAND (datum, 0));
2667 }
2668 else
2669 {
2670 /* "loc" should be the "." token. */
2671 richloc.add_fixit_replace ("->");
2672 error_at (&richloc,
2673 "%qE is a pointer; did you mean to use %<->%>?",
2674 datum);
2675 }
2676 return error_mark_node;
2677 }
2678 else if (code != ERROR_MARK)
2679 error_at (loc,
2680 "request for member %qE in something not a structure or union",
2681 component);
2682
2683 return error_mark_node;
2684 }
2685 \f
2686 /* Given an expression PTR for a pointer, return an expression
2687 for the value pointed to.
2688 ERRORSTRING is the name of the operator to appear in error messages.
2689
2690 LOC is the location to use for the generated tree. */
2691
2692 tree
2693 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2694 {
2695 tree pointer = default_conversion (ptr);
2696 tree type = TREE_TYPE (pointer);
2697 tree ref;
2698
2699 if (TREE_CODE (type) == POINTER_TYPE)
2700 {
2701 if (CONVERT_EXPR_P (pointer)
2702 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2703 {
2704 /* If a warning is issued, mark it to avoid duplicates from
2705 the backend. This only needs to be done at
2706 warn_strict_aliasing > 2. */
2707 if (warn_strict_aliasing > 2)
2708 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2709 type, TREE_OPERAND (pointer, 0)))
2710 suppress_warning (pointer, OPT_Wstrict_aliasing_);
2711 }
2712
2713 if (TREE_CODE (pointer) == ADDR_EXPR
2714 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2715 == TREE_TYPE (type)))
2716 {
2717 ref = TREE_OPERAND (pointer, 0);
2718 protected_set_expr_location (ref, loc);
2719 return ref;
2720 }
2721 else
2722 {
2723 tree t = TREE_TYPE (type);
2724
2725 ref = build1 (INDIRECT_REF, t, pointer);
2726
2727 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2728 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2729
2730 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2731 so that we get the proper error message if the result is used
2732 to assign to. Also, &* is supposed to be a no-op.
2733 And ANSI C seems to specify that the type of the result
2734 should be the const type. */
2735 /* A de-reference of a pointer to const is not a const. It is valid
2736 to change it via some other pointer. */
2737 TREE_READONLY (ref) = TYPE_READONLY (t);
2738 TREE_SIDE_EFFECTS (ref)
2739 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2740 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2741 protected_set_expr_location (ref, loc);
2742 return ref;
2743 }
2744 }
2745 else if (TREE_CODE (pointer) != ERROR_MARK)
2746 invalid_indirection_error (loc, type, errstring);
2747
2748 return error_mark_node;
2749 }
2750
2751 /* This handles expressions of the form "a[i]", which denotes
2752 an array reference.
2753
2754 This is logically equivalent in C to *(a+i), but we may do it differently.
2755 If A is a variable or a member, we generate a primitive ARRAY_REF.
2756 This avoids forcing the array out of registers, and can work on
2757 arrays that are not lvalues (for example, members of structures returned
2758 by functions).
2759
2760 For vector types, allow vector[i] but not i[vector], and create
2761 *(((type*)&vectortype) + i) for the expression.
2762
2763 LOC is the location to use for the returned expression. */
2764
2765 tree
2766 build_array_ref (location_t loc, tree array, tree index)
2767 {
2768 tree ret;
2769 bool swapped = false;
2770 if (TREE_TYPE (array) == error_mark_node
2771 || TREE_TYPE (index) == error_mark_node)
2772 return error_mark_node;
2773
2774 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2775 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2776 /* Allow vector[index] but not index[vector]. */
2777 && !gnu_vector_type_p (TREE_TYPE (array)))
2778 {
2779 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2780 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2781 {
2782 error_at (loc,
2783 "subscripted value is neither array nor pointer nor vector");
2784
2785 return error_mark_node;
2786 }
2787 std::swap (array, index);
2788 swapped = true;
2789 }
2790
2791 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2792 {
2793 error_at (loc, "array subscript is not an integer");
2794 return error_mark_node;
2795 }
2796
2797 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2798 {
2799 error_at (loc, "subscripted value is pointer to function");
2800 return error_mark_node;
2801 }
2802
2803 /* ??? Existing practice has been to warn only when the char
2804 index is syntactically the index, not for char[array]. */
2805 if (!swapped)
2806 warn_array_subscript_with_type_char (loc, index);
2807
2808 /* Apply default promotions *after* noticing character types. */
2809 index = default_conversion (index);
2810 if (index == error_mark_node)
2811 return error_mark_node;
2812
2813 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
2814 || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE);
2815
2816 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2817 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2818
2819 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2820 {
2821 tree rval, type;
2822
2823 /* An array that is indexed by a non-constant
2824 cannot be stored in a register; we must be able to do
2825 address arithmetic on its address.
2826 Likewise an array of elements of variable size. */
2827 if (TREE_CODE (index) != INTEGER_CST
2828 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2829 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2830 {
2831 if (!c_mark_addressable (array, true))
2832 return error_mark_node;
2833 }
2834 /* An array that is indexed by a constant value which is not within
2835 the array bounds cannot be stored in a register either; because we
2836 would get a crash in store_bit_field/extract_bit_field when trying
2837 to access a non-existent part of the register. */
2838 if (TREE_CODE (index) == INTEGER_CST
2839 && TYPE_DOMAIN (TREE_TYPE (array))
2840 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2841 {
2842 if (!c_mark_addressable (array))
2843 return error_mark_node;
2844 }
2845
2846 if ((pedantic || warn_c90_c99_compat)
2847 && ! was_vector)
2848 {
2849 tree foo = array;
2850 while (TREE_CODE (foo) == COMPONENT_REF)
2851 foo = TREE_OPERAND (foo, 0);
2852 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2853 pedwarn (loc, OPT_Wpedantic,
2854 "ISO C forbids subscripting %<register%> array");
2855 else if (!lvalue_p (foo))
2856 pedwarn_c90 (loc, OPT_Wpedantic,
2857 "ISO C90 forbids subscripting non-lvalue "
2858 "array");
2859 }
2860
2861 type = TREE_TYPE (TREE_TYPE (array));
2862 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2863 /* Array ref is const/volatile if the array elements are
2864 or if the array is. */
2865 TREE_READONLY (rval)
2866 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2867 | TREE_READONLY (array));
2868 TREE_SIDE_EFFECTS (rval)
2869 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2870 | TREE_SIDE_EFFECTS (array));
2871 TREE_THIS_VOLATILE (rval)
2872 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2873 /* This was added by rms on 16 Nov 91.
2874 It fixes vol struct foo *a; a->elts[1]
2875 in an inline function.
2876 Hope it doesn't break something else. */
2877 | TREE_THIS_VOLATILE (array));
2878 ret = require_complete_type (loc, rval);
2879 protected_set_expr_location (ret, loc);
2880 if (non_lvalue)
2881 ret = non_lvalue_loc (loc, ret);
2882 return ret;
2883 }
2884 else
2885 {
2886 tree ar = default_conversion (array);
2887
2888 if (ar == error_mark_node)
2889 return ar;
2890
2891 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2892 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2893
2894 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2895 index, false),
2896 RO_ARRAY_INDEXING);
2897 if (non_lvalue)
2898 ret = non_lvalue_loc (loc, ret);
2899 return ret;
2900 }
2901 }
2902 \f
2903 /* Build an external reference to identifier ID. FUN indicates
2904 whether this will be used for a function call. LOC is the source
2905 location of the identifier. This sets *TYPE to the type of the
2906 identifier, which is not the same as the type of the returned value
2907 for CONST_DECLs defined as enum constants. If the type of the
2908 identifier is not available, *TYPE is set to NULL. */
2909 tree
2910 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2911 {
2912 tree ref;
2913 tree decl = lookup_name (id);
2914
2915 /* In Objective-C, an instance variable (ivar) may be preferred to
2916 whatever lookup_name() found. */
2917 decl = objc_lookup_ivar (decl, id);
2918
2919 *type = NULL;
2920 if (decl && decl != error_mark_node)
2921 {
2922 ref = decl;
2923 *type = TREE_TYPE (ref);
2924 if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
2925 error_at (loc, "underspecified %qD referenced in its initializer",
2926 decl);
2927 }
2928 else if (fun)
2929 /* Implicit function declaration. */
2930 ref = implicitly_declare (loc, id);
2931 else if (decl == error_mark_node)
2932 /* Don't complain about something that's already been
2933 complained about. */
2934 return error_mark_node;
2935 else
2936 {
2937 undeclared_variable (loc, id);
2938 return error_mark_node;
2939 }
2940
2941 if (TREE_TYPE (ref) == error_mark_node)
2942 return error_mark_node;
2943
2944 if (TREE_UNAVAILABLE (ref))
2945 error_unavailable_use (ref, NULL_TREE);
2946 else if (TREE_DEPRECATED (ref))
2947 warn_deprecated_use (ref, NULL_TREE);
2948
2949 /* Recursive call does not count as usage. */
2950 if (ref != current_function_decl)
2951 {
2952 TREE_USED (ref) = 1;
2953 }
2954
2955 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2956 {
2957 if (!in_sizeof && !in_typeof)
2958 C_DECL_USED (ref) = 1;
2959 else if (DECL_INITIAL (ref) == NULL_TREE
2960 && DECL_EXTERNAL (ref)
2961 && !TREE_PUBLIC (ref))
2962 record_maybe_used_decl (ref);
2963 }
2964
2965 if (TREE_CODE (ref) == CONST_DECL)
2966 {
2967 used_types_insert (TREE_TYPE (ref));
2968
2969 if (warn_cxx_compat
2970 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2971 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2972 {
2973 warning_at (loc, OPT_Wc___compat,
2974 ("enum constant defined in struct or union "
2975 "is not visible in C++"));
2976 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2977 }
2978
2979 ref = DECL_INITIAL (ref);
2980 TREE_CONSTANT (ref) = 1;
2981 }
2982 else if (current_function_decl != NULL_TREE
2983 && !DECL_FILE_SCOPE_P (current_function_decl)
2984 && (VAR_OR_FUNCTION_DECL_P (ref)
2985 || TREE_CODE (ref) == PARM_DECL))
2986 {
2987 tree context = decl_function_context (ref);
2988
2989 if (context != NULL_TREE && context != current_function_decl)
2990 DECL_NONLOCAL (ref) = 1;
2991 }
2992 /* C99 6.7.4p3: An inline definition of a function with external
2993 linkage ... shall not contain a reference to an identifier with
2994 internal linkage. */
2995 else if (current_function_decl != NULL_TREE
2996 && DECL_DECLARED_INLINE_P (current_function_decl)
2997 && DECL_EXTERNAL (current_function_decl)
2998 && VAR_OR_FUNCTION_DECL_P (ref)
2999 && (!VAR_P (ref) || TREE_STATIC (ref))
3000 && ! TREE_PUBLIC (ref)
3001 && DECL_CONTEXT (ref) != current_function_decl)
3002 record_inline_static (loc, current_function_decl, ref,
3003 csi_internal);
3004
3005 return ref;
3006 }
3007
3008 /* Record details of decls possibly used inside sizeof or typeof. */
3009 struct maybe_used_decl
3010 {
3011 /* The decl. */
3012 tree decl;
3013 /* The level seen at (in_sizeof + in_typeof). */
3014 int level;
3015 /* The next one at this level or above, or NULL. */
3016 struct maybe_used_decl *next;
3017 };
3018
3019 static struct maybe_used_decl *maybe_used_decls;
3020
3021 /* Record that DECL, an undefined static function reference seen
3022 inside sizeof or typeof, might be used if the operand of sizeof is
3023 a VLA type or the operand of typeof is a variably modified
3024 type. */
3025
3026 static void
3027 record_maybe_used_decl (tree decl)
3028 {
3029 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3030 t->decl = decl;
3031 t->level = in_sizeof + in_typeof;
3032 t->next = maybe_used_decls;
3033 maybe_used_decls = t;
3034 }
3035
3036 /* Pop the stack of decls possibly used inside sizeof or typeof. If
3037 USED is false, just discard them. If it is true, mark them used
3038 (if no longer inside sizeof or typeof) or move them to the next
3039 level up (if still inside sizeof or typeof). */
3040
3041 void
3042 pop_maybe_used (bool used)
3043 {
3044 struct maybe_used_decl *p = maybe_used_decls;
3045 int cur_level = in_sizeof + in_typeof;
3046 while (p && p->level > cur_level)
3047 {
3048 if (used)
3049 {
3050 if (cur_level == 0)
3051 C_DECL_USED (p->decl) = 1;
3052 else
3053 p->level = cur_level;
3054 }
3055 p = p->next;
3056 }
3057 if (!used || cur_level == 0)
3058 maybe_used_decls = p;
3059 }
3060
3061 /* Return the result of sizeof applied to EXPR. */
3062
3063 struct c_expr
3064 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
3065 {
3066 struct c_expr ret;
3067 if (expr.value == error_mark_node)
3068 {
3069 ret.value = error_mark_node;
3070 ret.original_code = ERROR_MARK;
3071 ret.original_type = NULL;
3072 ret.m_decimal = 0;
3073 pop_maybe_used (false);
3074 }
3075 else
3076 {
3077 bool expr_const_operands = true;
3078
3079 if (TREE_CODE (expr.value) == PARM_DECL
3080 && C_ARRAY_PARAMETER (expr.value))
3081 {
3082 auto_diagnostic_group d;
3083 if (warning_at (loc, OPT_Wsizeof_array_argument,
3084 "%<sizeof%> on array function parameter %qE will "
3085 "return size of %qT", expr.value,
3086 TREE_TYPE (expr.value)))
3087 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3088 }
3089 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3090 &expr_const_operands);
3091 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3092 c_last_sizeof_arg = expr.value;
3093 c_last_sizeof_loc = loc;
3094 ret.original_code = SIZEOF_EXPR;
3095 ret.original_type = NULL;
3096 ret.m_decimal = 0;
3097 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3098 {
3099 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3100 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3101 folded_expr, ret.value);
3102 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3103 SET_EXPR_LOCATION (ret.value, loc);
3104 }
3105 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3106 }
3107 return ret;
3108 }
3109
3110 /* Return the result of sizeof applied to T, a structure for the type
3111 name passed to sizeof (rather than the type itself). LOC is the
3112 location of the original expression. */
3113
3114 struct c_expr
3115 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3116 {
3117 tree type;
3118 struct c_expr ret;
3119 tree type_expr = NULL_TREE;
3120 bool type_expr_const = true;
3121 type = groktypename (t, &type_expr, &type_expr_const);
3122 ret.value = c_sizeof (loc, type);
3123 c_last_sizeof_arg = type;
3124 c_last_sizeof_loc = loc;
3125 ret.original_code = SIZEOF_EXPR;
3126 ret.original_type = NULL;
3127 ret.m_decimal = 0;
3128 if (type == error_mark_node)
3129 {
3130 ret.value = error_mark_node;
3131 ret.original_code = ERROR_MARK;
3132 }
3133 else
3134 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3135 && C_TYPE_VARIABLE_SIZE (type))
3136 {
3137 /* If the type is a [*] array, it is a VLA but is represented as
3138 having a size of zero. In such a case we must ensure that
3139 the result of sizeof does not get folded to a constant by
3140 c_fully_fold, because if the size is evaluated the result is
3141 not constant and so constraints on zero or negative size
3142 arrays must not be applied when this sizeof call is inside
3143 another array declarator. */
3144 if (!type_expr)
3145 type_expr = integer_zero_node;
3146 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3147 type_expr, ret.value);
3148 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3149 }
3150 pop_maybe_used (type != error_mark_node
3151 ? C_TYPE_VARIABLE_SIZE (type) : false);
3152 return ret;
3153 }
3154
3155 /* Build a function call to function FUNCTION with parameters PARAMS.
3156 The function call is at LOC.
3157 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3158 TREE_VALUE of each node is a parameter-expression.
3159 FUNCTION's data type may be a function type or a pointer-to-function. */
3160
3161 tree
3162 build_function_call (location_t loc, tree function, tree params)
3163 {
3164 vec<tree, va_gc> *v;
3165 tree ret;
3166
3167 vec_alloc (v, list_length (params));
3168 for (; params; params = TREE_CHAIN (params))
3169 v->quick_push (TREE_VALUE (params));
3170 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3171 vec_free (v);
3172 return ret;
3173 }
3174
3175 /* Give a note about the location of the declaration of DECL. */
3176
3177 static void
3178 inform_declaration (tree decl)
3179 {
3180 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3181 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3182 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3183 }
3184
3185 /* Build a function call to function FUNCTION with parameters PARAMS.
3186 If FUNCTION is the result of resolving an overloaded target built-in,
3187 ORIG_FUNDECL is the original function decl, otherwise it is null.
3188 ORIGTYPES, if not NULL, is a vector of types; each element is
3189 either NULL or the original type of the corresponding element in
3190 PARAMS. The original type may differ from TREE_TYPE of the
3191 parameter for enums. FUNCTION's data type may be a function type
3192 or pointer-to-function. This function changes the elements of
3193 PARAMS. */
3194
3195 tree
3196 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3197 tree function, vec<tree, va_gc> *params,
3198 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3199 {
3200 tree fntype, fundecl = NULL_TREE;
3201 tree name = NULL_TREE, result;
3202 tree tem;
3203 int nargs;
3204 tree *argarray;
3205
3206
3207 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3208 STRIP_TYPE_NOPS (function);
3209
3210 /* Convert anything with function type to a pointer-to-function. */
3211 if (TREE_CODE (function) == FUNCTION_DECL)
3212 {
3213 name = DECL_NAME (function);
3214
3215 if (flag_tm)
3216 tm_malloc_replacement (function);
3217 fundecl = function;
3218 if (!orig_fundecl)
3219 orig_fundecl = fundecl;
3220 /* Atomic functions have type checking/casting already done. They are
3221 often rewritten and don't match the original parameter list. */
3222 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3223 origtypes = NULL;
3224 }
3225 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3226 function = function_to_pointer_conversion (loc, function);
3227
3228 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3229 expressions, like those used for ObjC messenger dispatches. */
3230 if (params && !params->is_empty ())
3231 function = objc_rewrite_function_call (function, (*params)[0]);
3232
3233 function = c_fully_fold (function, false, NULL);
3234
3235 fntype = TREE_TYPE (function);
3236
3237 if (TREE_CODE (fntype) == ERROR_MARK)
3238 return error_mark_node;
3239
3240 if (!(TREE_CODE (fntype) == POINTER_TYPE
3241 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3242 {
3243 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3244 error_at (loc,
3245 "called object %qE is not a function or function pointer",
3246 function);
3247 else if (DECL_P (function))
3248 {
3249 error_at (loc,
3250 "called object %qD is not a function or function pointer",
3251 function);
3252 inform_declaration (function);
3253 }
3254 else
3255 error_at (loc,
3256 "called object is not a function or function pointer");
3257 return error_mark_node;
3258 }
3259
3260 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3261 current_function_returns_abnormally = 1;
3262
3263 /* fntype now gets the type of function pointed to. */
3264 fntype = TREE_TYPE (fntype);
3265 tree return_type = TREE_TYPE (fntype);
3266
3267 /* Convert the parameters to the types declared in the
3268 function prototype, or apply default promotions. */
3269
3270 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3271 origtypes, function, fundecl);
3272 if (nargs < 0)
3273 return error_mark_node;
3274
3275 /* Check that the function is called through a compatible prototype.
3276 If it is not, warn. */
3277 if (CONVERT_EXPR_P (function)
3278 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3279 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3280 && !comptypes (fntype, TREE_TYPE (tem)))
3281 {
3282 /* This situation leads to run-time undefined behavior. We can't,
3283 therefore, simply error unless we can prove that all possible
3284 executions of the program must execute the code. */
3285 warning_at (loc, 0, "function called through a non-compatible type");
3286
3287 if (VOID_TYPE_P (return_type)
3288 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3289 pedwarn (loc, 0,
3290 "function with qualified void return type called");
3291 }
3292
3293 argarray = vec_safe_address (params);
3294
3295 /* Check that arguments to builtin functions match the expectations. */
3296 if (fundecl
3297 && fndecl_built_in_p (fundecl)
3298 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3299 orig_fundecl, nargs, argarray))
3300 return error_mark_node;
3301
3302 /* Check that the arguments to the function are valid. */
3303 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3304 nargs, argarray, &arg_loc);
3305
3306 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
3307 && !VOID_TYPE_P (return_type))
3308 return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
3309 if (name != NULL_TREE
3310 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3311 {
3312 if (require_constant_value)
3313 result
3314 = fold_build_call_array_initializer_loc (loc, return_type,
3315 function, nargs, argarray);
3316 else
3317 result = fold_build_call_array_loc (loc, return_type,
3318 function, nargs, argarray);
3319 if (TREE_CODE (result) == NOP_EXPR
3320 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3321 STRIP_TYPE_NOPS (result);
3322 }
3323 else
3324 result = build_call_array_loc (loc, return_type,
3325 function, nargs, argarray);
3326 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3327 later. */
3328 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3329 suppress_warning (result, OPT_Wnonnull);
3330
3331 /* In this improbable scenario, a nested function returns a VM type.
3332 Create a TARGET_EXPR so that the call always has a LHS, much as
3333 what the C++ FE does for functions returning non-PODs. */
3334 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (fntype)))
3335 {
3336 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3337 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3338 NULL_TREE, NULL_TREE);
3339 }
3340
3341 if (VOID_TYPE_P (TREE_TYPE (result)))
3342 {
3343 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3344 pedwarn (loc, 0,
3345 "function with qualified void return type called");
3346 return result;
3347 }
3348 return require_complete_type (loc, result);
3349 }
3350
3351 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3352
3353 tree
3354 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3355 tree function, vec<tree, va_gc> *params,
3356 vec<tree, va_gc> *origtypes)
3357 {
3358 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3359 STRIP_TYPE_NOPS (function);
3360
3361 /* Convert anything with function type to a pointer-to-function. */
3362 if (TREE_CODE (function) == FUNCTION_DECL)
3363 {
3364 /* Implement type-directed function overloading for builtins.
3365 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3366 handle all the type checking. The result is a complete expression
3367 that implements this function call. */
3368 tree tem = resolve_overloaded_builtin (loc, function, params);
3369 if (tem)
3370 return tem;
3371 }
3372 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3373 }
3374 \f
3375 /* Helper for convert_arguments called to convert the VALue of argument
3376 number ARGNUM from ORIGTYPE to the corresponding parameter number
3377 PARMNUM and TYPE.
3378 PLOC is the location where the conversion is being performed.
3379 FUNCTION and FUNDECL are the same as in convert_arguments.
3380 VALTYPE is the original type of VAL before the conversion and,
3381 for EXCESS_PRECISION_EXPR, the operand of the expression.
3382 NPC is true if VAL represents the null pointer constant (VAL itself
3383 will have been folded to an integer constant).
3384 RNAME is the same as FUNCTION except in Objective C when it's
3385 the function selector.
3386 EXCESS_PRECISION is true when VAL was originally represented
3387 as EXCESS_PRECISION_EXPR.
3388 WARNOPT is the same as in convert_for_assignment. */
3389
3390 static tree
3391 convert_argument (location_t ploc, tree function, tree fundecl,
3392 tree type, tree origtype, tree val, tree valtype,
3393 bool npc, tree rname, int parmnum, int argnum,
3394 bool excess_precision, int warnopt)
3395 {
3396 /* Formal parm type is specified by a function prototype. */
3397
3398 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3399 {
3400 error_at (ploc, "type of formal parameter %d is incomplete",
3401 parmnum + 1);
3402 return error_mark_node;
3403 }
3404
3405 /* Optionally warn about conversions that differ from the default
3406 conversions. */
3407 if (warn_traditional_conversion || warn_traditional)
3408 {
3409 if (INTEGRAL_TYPE_P (type)
3410 && SCALAR_FLOAT_TYPE_P (valtype))
3411 warning_at (ploc, OPT_Wtraditional_conversion,
3412 "passing argument %d of %qE as integer rather "
3413 "than floating due to prototype",
3414 argnum, rname);
3415 if (INTEGRAL_TYPE_P (type)
3416 && TREE_CODE (valtype) == COMPLEX_TYPE)
3417 warning_at (ploc, OPT_Wtraditional_conversion,
3418 "passing argument %d of %qE as integer rather "
3419 "than complex due to prototype",
3420 argnum, rname);
3421 else if (TREE_CODE (type) == COMPLEX_TYPE
3422 && SCALAR_FLOAT_TYPE_P (valtype))
3423 warning_at (ploc, OPT_Wtraditional_conversion,
3424 "passing argument %d of %qE as complex rather "
3425 "than floating due to prototype",
3426 argnum, rname);
3427 else if (SCALAR_FLOAT_TYPE_P (type)
3428 && INTEGRAL_TYPE_P (valtype))
3429 warning_at (ploc, OPT_Wtraditional_conversion,
3430 "passing argument %d of %qE as floating rather "
3431 "than integer due to prototype",
3432 argnum, rname);
3433 else if (TREE_CODE (type) == COMPLEX_TYPE
3434 && INTEGRAL_TYPE_P (valtype))
3435 warning_at (ploc, OPT_Wtraditional_conversion,
3436 "passing argument %d of %qE as complex rather "
3437 "than integer due to prototype",
3438 argnum, rname);
3439 else if (SCALAR_FLOAT_TYPE_P (type)
3440 && TREE_CODE (valtype) == COMPLEX_TYPE)
3441 warning_at (ploc, OPT_Wtraditional_conversion,
3442 "passing argument %d of %qE as floating rather "
3443 "than complex due to prototype",
3444 argnum, rname);
3445 /* ??? At some point, messages should be written about
3446 conversions between complex types, but that's too messy
3447 to do now. */
3448 else if (SCALAR_FLOAT_TYPE_P (type)
3449 && SCALAR_FLOAT_TYPE_P (valtype))
3450 {
3451 unsigned int formal_prec = TYPE_PRECISION (type);
3452
3453 /* Warn if any argument is passed as `float',
3454 since without a prototype it would be `double'. */
3455 if (formal_prec == TYPE_PRECISION (float_type_node)
3456 && type != dfloat32_type_node)
3457 warning_at (ploc, 0,
3458 "passing argument %d of %qE as %<float%> "
3459 "rather than %<double%> due to prototype",
3460 argnum, rname);
3461
3462 /* Warn if mismatch between argument and prototype
3463 for decimal float types. Warn of conversions with
3464 binary float types and of precision narrowing due to
3465 prototype. */
3466 else if (type != valtype
3467 && (type == dfloat32_type_node
3468 || type == dfloat64_type_node
3469 || type == dfloat128_type_node
3470 || valtype == dfloat32_type_node
3471 || valtype == dfloat64_type_node
3472 || valtype == dfloat128_type_node)
3473 && (formal_prec
3474 <= TYPE_PRECISION (valtype)
3475 || (type == dfloat128_type_node
3476 && (valtype
3477 != dfloat64_type_node
3478 && (valtype
3479 != dfloat32_type_node)))
3480 || (type == dfloat64_type_node
3481 && (valtype
3482 != dfloat32_type_node))))
3483 warning_at (ploc, 0,
3484 "passing argument %d of %qE as %qT "
3485 "rather than %qT due to prototype",
3486 argnum, rname, type, valtype);
3487
3488 }
3489 /* Detect integer changing in width or signedness.
3490 These warnings are only activated with
3491 -Wtraditional-conversion, not with -Wtraditional. */
3492 else if (warn_traditional_conversion
3493 && INTEGRAL_TYPE_P (type)
3494 && INTEGRAL_TYPE_P (valtype))
3495 {
3496 unsigned int formal_prec = TYPE_PRECISION (type);
3497 tree would_have_been = default_conversion (val);
3498 tree type1 = TREE_TYPE (would_have_been);
3499
3500 if (val == error_mark_node)
3501 /* VAL could have been of incomplete type. */;
3502 else if (TREE_CODE (type) == ENUMERAL_TYPE
3503 && (TYPE_MAIN_VARIANT (type)
3504 == TYPE_MAIN_VARIANT (valtype)))
3505 /* No warning if function asks for enum
3506 and the actual arg is that enum type. */
3507 ;
3508 else if (formal_prec != TYPE_PRECISION (type1))
3509 warning_at (ploc, OPT_Wtraditional_conversion,
3510 "passing argument %d of %qE "
3511 "with different width due to prototype",
3512 argnum, rname);
3513 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3514 ;
3515 /* Don't complain if the formal parameter type
3516 is an enum, because we can't tell now whether
3517 the value was an enum--even the same enum. */
3518 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3519 ;
3520 else if (TREE_CODE (val) == INTEGER_CST
3521 && int_fits_type_p (val, type))
3522 /* Change in signedness doesn't matter
3523 if a constant value is unaffected. */
3524 ;
3525 /* If the value is extended from a narrower
3526 unsigned type, it doesn't matter whether we
3527 pass it as signed or unsigned; the value
3528 certainly is the same either way. */
3529 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3530 && TYPE_UNSIGNED (valtype))
3531 ;
3532 else if (TYPE_UNSIGNED (type))
3533 warning_at (ploc, OPT_Wtraditional_conversion,
3534 "passing argument %d of %qE "
3535 "as unsigned due to prototype",
3536 argnum, rname);
3537 else
3538 warning_at (ploc, OPT_Wtraditional_conversion,
3539 "passing argument %d of %qE "
3540 "as signed due to prototype",
3541 argnum, rname);
3542 }
3543 }
3544
3545 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3546 sake of better warnings from convert_and_check. */
3547 if (excess_precision)
3548 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3549
3550 tree parmval = convert_for_assignment (ploc, ploc, type,
3551 val, origtype, ic_argpass,
3552 npc, fundecl, function,
3553 parmnum + 1, warnopt);
3554
3555 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3556 && INTEGRAL_TYPE_P (type)
3557 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3558 parmval = default_conversion (parmval);
3559
3560 return parmval;
3561 }
3562
3563 /* Convert the argument expressions in the vector VALUES
3564 to the types in the list TYPELIST.
3565
3566 If TYPELIST is exhausted, or when an element has NULL as its type,
3567 perform the default conversions.
3568
3569 ORIGTYPES is the original types of the expressions in VALUES. This
3570 holds the type of enum values which have been converted to integral
3571 types. It may be NULL.
3572
3573 FUNCTION is a tree for the called function. It is used only for
3574 error messages, where it is formatted with %qE.
3575
3576 This is also where warnings about wrong number of args are generated.
3577
3578 ARG_LOC are locations of function arguments (if any).
3579
3580 Returns the actual number of arguments processed (which may be less
3581 than the length of VALUES in some error situations), or -1 on
3582 failure. */
3583
3584 static int
3585 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3586 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3587 tree function, tree fundecl)
3588 {
3589 unsigned int parmnum;
3590 bool error_args = false;
3591 const bool type_generic = fundecl
3592 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3593 bool type_generic_remove_excess_precision = false;
3594 bool type_generic_overflow_p = false;
3595 bool type_generic_bit_query = false;
3596 tree selector;
3597
3598 /* Change pointer to function to the function itself for
3599 diagnostics. */
3600 if (TREE_CODE (function) == ADDR_EXPR
3601 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3602 function = TREE_OPERAND (function, 0);
3603
3604 /* Handle an ObjC selector specially for diagnostics. */
3605 selector = objc_message_selector ();
3606
3607 /* For a call to a built-in function declared without a prototype,
3608 set to the built-in function's argument list. */
3609 tree builtin_typelist = NULL_TREE;
3610
3611 /* For type-generic built-in functions, determine whether excess
3612 precision should be removed (classification) or not
3613 (comparison). */
3614 if (fundecl
3615 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3616 {
3617 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3618 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3619 {
3620 /* For a call to a built-in function declared without a prototype
3621 use the types of the parameters of the internal built-in to
3622 match those of the arguments to. */
3623 if (tree bdecl = builtin_decl_explicit (code))
3624 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3625 }
3626
3627 /* For type-generic built-in functions, determine whether excess
3628 precision should be removed (classification) or not
3629 (comparison). */
3630 if (type_generic)
3631 switch (code)
3632 {
3633 case BUILT_IN_ISFINITE:
3634 case BUILT_IN_ISINF:
3635 case BUILT_IN_ISINF_SIGN:
3636 case BUILT_IN_ISNAN:
3637 case BUILT_IN_ISNORMAL:
3638 case BUILT_IN_ISSIGNALING:
3639 case BUILT_IN_FPCLASSIFY:
3640 type_generic_remove_excess_precision = true;
3641 break;
3642
3643 case BUILT_IN_ADD_OVERFLOW_P:
3644 case BUILT_IN_SUB_OVERFLOW_P:
3645 case BUILT_IN_MUL_OVERFLOW_P:
3646 /* The last argument of these type-generic builtins
3647 should not be promoted. */
3648 type_generic_overflow_p = true;
3649 break;
3650
3651 case BUILT_IN_CLZG:
3652 case BUILT_IN_CTZG:
3653 case BUILT_IN_CLRSBG:
3654 case BUILT_IN_FFSG:
3655 case BUILT_IN_PARITYG:
3656 case BUILT_IN_POPCOUNTG:
3657 /* The first argument of these type-generic builtins
3658 should not be promoted. */
3659 type_generic_bit_query = true;
3660 break;
3661
3662 default:
3663 break;
3664 }
3665 }
3666
3667 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3668 individual converted arguments. */
3669
3670 tree typetail, builtin_typetail, val;
3671 for (typetail = typelist,
3672 builtin_typetail = builtin_typelist,
3673 parmnum = 0;
3674 values && values->iterate (parmnum, &val);
3675 ++parmnum)
3676 {
3677 /* The type of the function parameter (if it was declared with one). */
3678 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3679 /* The type of the built-in function parameter (if the function
3680 is a built-in). Used to detect type incompatibilities in
3681 calls to built-ins declared without a prototype. */
3682 tree builtin_type = (builtin_typetail
3683 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3684 /* The original type of the argument being passed to the function. */
3685 tree valtype = TREE_TYPE (val);
3686 /* The called function (or function selector in Objective C). */
3687 tree rname = function;
3688 int argnum = parmnum + 1;
3689 const char *invalid_func_diag;
3690 /* Set for EXCESS_PRECISION_EXPR arguments. */
3691 bool excess_precision = false;
3692 /* The value of the argument after conversion to the type
3693 of the function parameter it is passed to. */
3694 tree parmval;
3695 /* Some __atomic_* builtins have additional hidden argument at
3696 position 0. */
3697 location_t ploc
3698 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3699 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3700 : input_location;
3701
3702 if (type == void_type_node)
3703 {
3704 if (selector)
3705 error_at (loc, "too many arguments to method %qE", selector);
3706 else
3707 error_at (loc, "too many arguments to function %qE", function);
3708 inform_declaration (fundecl);
3709 return error_args ? -1 : (int) parmnum;
3710 }
3711
3712 if (builtin_type == void_type_node)
3713 {
3714 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3715 "too many arguments to built-in function %qE "
3716 "expecting %d", function, parmnum))
3717 inform_declaration (fundecl);
3718 builtin_typetail = NULL_TREE;
3719 }
3720
3721 if (selector && argnum > 2)
3722 {
3723 rname = selector;
3724 argnum -= 2;
3725 }
3726
3727 /* Determine if VAL is a null pointer constant before folding it. */
3728 bool npc = null_pointer_constant_p (val);
3729
3730 /* If there is excess precision and a prototype, convert once to
3731 the required type rather than converting via the semantic
3732 type. Likewise without a prototype a float value represented
3733 as long double should be converted once to double. But for
3734 type-generic classification functions excess precision must
3735 be removed here. */
3736 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3737 && (type || !type_generic || !type_generic_remove_excess_precision))
3738 {
3739 val = TREE_OPERAND (val, 0);
3740 excess_precision = true;
3741 }
3742 val = c_fully_fold (val, false, NULL);
3743 STRIP_TYPE_NOPS (val);
3744
3745 val = require_complete_type (ploc, val);
3746
3747 /* Some floating-point arguments must be promoted to double when
3748 no type is specified by a prototype. This applies to
3749 arguments of type float, and to architecture-specific types
3750 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3751 bool promote_float_arg = false;
3752 if (type == NULL_TREE
3753 && TREE_CODE (valtype) == REAL_TYPE
3754 && (TYPE_PRECISION (valtype)
3755 <= TYPE_PRECISION (double_type_node))
3756 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3757 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3758 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3759 {
3760 /* Promote this argument, unless it has a _FloatN or
3761 _FloatNx type. */
3762 promote_float_arg = true;
3763 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3764 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3765 {
3766 promote_float_arg = false;
3767 break;
3768 }
3769 /* Don't promote __bf16 either. */
3770 if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
3771 promote_float_arg = false;
3772 }
3773
3774 if (type != NULL_TREE)
3775 {
3776 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3777 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3778 val, valtype, npc, rname, parmnum, argnum,
3779 excess_precision, 0);
3780 }
3781 else if (promote_float_arg)
3782 {
3783 if (type_generic)
3784 parmval = val;
3785 else
3786 {
3787 /* Convert `float' to `double'. */
3788 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3789 warning_at (ploc, OPT_Wdouble_promotion,
3790 "implicit conversion from %qT to %qT when passing "
3791 "argument to function",
3792 valtype, double_type_node);
3793 parmval = convert (double_type_node, val);
3794 }
3795 }
3796 else if ((excess_precision && !type_generic)
3797 || (type_generic_overflow_p && parmnum == 2)
3798 || (type_generic_bit_query && parmnum == 0))
3799 /* A "double" argument with excess precision being passed
3800 without a prototype or in variable arguments.
3801 The last argument of __builtin_*_overflow_p should not be
3802 promoted, similarly the first argument of
3803 __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
3804 parmval = convert (valtype, val);
3805 else if ((invalid_func_diag =
3806 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3807 {
3808 error (invalid_func_diag);
3809 return -1;
3810 }
3811 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3812 {
3813 return -1;
3814 }
3815 else
3816 /* Convert `short' and `char' to full-size `int'. */
3817 parmval = default_conversion (val);
3818
3819 (*values)[parmnum] = parmval;
3820 if (parmval == error_mark_node)
3821 error_args = true;
3822
3823 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3824 {
3825 /* For a call to a built-in function declared without a prototype,
3826 perform the conversions from the argument to the expected type
3827 but issue warnings rather than errors for any mismatches.
3828 Ignore the converted argument and use the PARMVAL obtained
3829 above by applying default conversions instead. */
3830 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3831 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3832 val, valtype, npc, rname, parmnum, argnum,
3833 excess_precision,
3834 OPT_Wbuiltin_declaration_mismatch);
3835 }
3836
3837 if (typetail)
3838 typetail = TREE_CHAIN (typetail);
3839
3840 if (builtin_typetail)
3841 builtin_typetail = TREE_CHAIN (builtin_typetail);
3842 }
3843
3844 gcc_assert (parmnum == vec_safe_length (values));
3845
3846 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3847 {
3848 error_at (loc, "too few arguments to function %qE", function);
3849 inform_declaration (fundecl);
3850 return -1;
3851 }
3852
3853 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3854 {
3855 unsigned nargs = parmnum;
3856 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3857 ++nargs;
3858
3859 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3860 "too few arguments to built-in function %qE "
3861 "expecting %u", function, nargs - 1))
3862 inform_declaration (fundecl);
3863 }
3864
3865 return error_args ? -1 : (int) parmnum;
3866 }
3867 \f
3868 /* This is the entry point used by the parser to build unary operators
3869 in the input. CODE, a tree_code, specifies the unary operator, and
3870 ARG is the operand. For unary plus, the C parser currently uses
3871 CONVERT_EXPR for code.
3872
3873 LOC is the location to use for the tree generated.
3874 */
3875
3876 struct c_expr
3877 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3878 {
3879 struct c_expr result;
3880
3881 result.original_code = code;
3882 result.original_type = NULL;
3883 result.m_decimal = 0;
3884
3885 if (reject_gcc_builtin (arg.value))
3886 {
3887 result.value = error_mark_node;
3888 }
3889 else
3890 {
3891 result.value = build_unary_op (loc, code, arg.value, false);
3892
3893 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3894 overflow_warning (loc, result.value, arg.value);
3895 }
3896
3897 /* We are typically called when parsing a prefix token at LOC acting on
3898 ARG. Reflect this by updating the source range of the result to
3899 start at LOC and end at the end of ARG. */
3900 set_c_expr_source_range (&result,
3901 loc, arg.get_finish ());
3902
3903 return result;
3904 }
3905
3906 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3907
3908 bool
3909 char_type_p (tree type)
3910 {
3911 return (type == char_type_node
3912 || type == unsigned_char_type_node
3913 || type == signed_char_type_node
3914 || type == char16_type_node
3915 || type == char32_type_node);
3916 }
3917
3918 /* This is the entry point used by the parser to build binary operators
3919 in the input. CODE, a tree_code, specifies the binary operator, and
3920 ARG1 and ARG2 are the operands. In addition to constructing the
3921 expression, we check for operands that were written with other binary
3922 operators in a way that is likely to confuse the user.
3923
3924 LOCATION is the location of the binary operator. */
3925
3926 struct c_expr
3927 parser_build_binary_op (location_t location, enum tree_code code,
3928 struct c_expr arg1, struct c_expr arg2)
3929 {
3930 struct c_expr result;
3931 result.m_decimal = 0;
3932
3933 enum tree_code code1 = arg1.original_code;
3934 enum tree_code code2 = arg2.original_code;
3935 tree type1 = (arg1.original_type
3936 ? arg1.original_type
3937 : TREE_TYPE (arg1.value));
3938 tree type2 = (arg2.original_type
3939 ? arg2.original_type
3940 : TREE_TYPE (arg2.value));
3941
3942 result.value = build_binary_op (location, code,
3943 arg1.value, arg2.value, true);
3944 result.original_code = code;
3945 result.original_type = NULL;
3946 result.m_decimal = 0;
3947
3948 if (TREE_CODE (result.value) == ERROR_MARK)
3949 {
3950 set_c_expr_source_range (&result,
3951 arg1.get_start (),
3952 arg2.get_finish ());
3953 return result;
3954 }
3955
3956 if (location != UNKNOWN_LOCATION)
3957 protected_set_expr_location (result.value, location);
3958
3959 set_c_expr_source_range (&result,
3960 arg1.get_start (),
3961 arg2.get_finish ());
3962
3963 /* Check for cases such as x+y<<z which users are likely
3964 to misinterpret. */
3965 if (warn_parentheses)
3966 warn_about_parentheses (location, code, code1, arg1.value, code2,
3967 arg2.value);
3968
3969 if (warn_logical_op)
3970 warn_logical_operator (location, code, TREE_TYPE (result.value),
3971 code1, arg1.value, code2, arg2.value);
3972
3973 if (warn_tautological_compare)
3974 {
3975 tree lhs = arg1.value;
3976 tree rhs = arg2.value;
3977 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3978 {
3979 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3980 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3981 lhs = NULL_TREE;
3982 else
3983 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3984 }
3985 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3986 {
3987 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3988 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3989 rhs = NULL_TREE;
3990 else
3991 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3992 }
3993 if (lhs != NULL_TREE && rhs != NULL_TREE)
3994 warn_tautological_cmp (location, code, lhs, rhs);
3995 }
3996
3997 if (warn_logical_not_paren
3998 && TREE_CODE_CLASS (code) == tcc_comparison
3999 && code1 == TRUTH_NOT_EXPR
4000 && code2 != TRUTH_NOT_EXPR
4001 /* Avoid warning for !!x == y. */
4002 && (TREE_CODE (arg1.value) != NE_EXPR
4003 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
4004 {
4005 /* Avoid warning for !b == y where b has _Bool type. */
4006 tree t = integer_zero_node;
4007 if (TREE_CODE (arg1.value) == EQ_EXPR
4008 && integer_zerop (TREE_OPERAND (arg1.value, 1))
4009 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
4010 {
4011 t = TREE_OPERAND (arg1.value, 0);
4012 do
4013 {
4014 if (TREE_TYPE (t) != integer_type_node)
4015 break;
4016 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
4017 t = C_MAYBE_CONST_EXPR_EXPR (t);
4018 else if (CONVERT_EXPR_P (t))
4019 t = TREE_OPERAND (t, 0);
4020 else
4021 break;
4022 }
4023 while (1);
4024 }
4025 if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
4026 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
4027 }
4028
4029 /* Warn about comparisons against string literals, with the exception
4030 of testing for equality or inequality of a string literal with NULL. */
4031 if (code == EQ_EXPR || code == NE_EXPR)
4032 {
4033 if ((code1 == STRING_CST
4034 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
4035 || (code2 == STRING_CST
4036 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
4037 warning_at (location, OPT_Waddress,
4038 "comparison with string literal results in unspecified behavior");
4039 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
4040 if (POINTER_TYPE_P (type1)
4041 && null_pointer_constant_p (arg2.value)
4042 && char_type_p (type2))
4043 {
4044 auto_diagnostic_group d;
4045 if (warning_at (location, OPT_Wpointer_compare,
4046 "comparison between pointer and zero character "
4047 "constant"))
4048 inform (arg1.get_start (),
4049 "did you mean to dereference the pointer?");
4050 }
4051 else if (POINTER_TYPE_P (type2)
4052 && null_pointer_constant_p (arg1.value)
4053 && char_type_p (type1))
4054 {
4055 auto_diagnostic_group d;
4056 if (warning_at (location, OPT_Wpointer_compare,
4057 "comparison between pointer and zero character "
4058 "constant"))
4059 inform (arg2.get_start (),
4060 "did you mean to dereference the pointer?");
4061 }
4062 }
4063 else if (TREE_CODE_CLASS (code) == tcc_comparison
4064 && (code1 == STRING_CST || code2 == STRING_CST))
4065 warning_at (location, OPT_Waddress,
4066 "comparison with string literal results in unspecified "
4067 "behavior");
4068
4069 if (warn_array_compare
4070 && TREE_CODE_CLASS (code) == tcc_comparison
4071 && TREE_CODE (type1) == ARRAY_TYPE
4072 && TREE_CODE (type2) == ARRAY_TYPE)
4073 do_warn_array_compare (location, code, arg1.value, arg2.value);
4074
4075 if (TREE_OVERFLOW_P (result.value)
4076 && !TREE_OVERFLOW_P (arg1.value)
4077 && !TREE_OVERFLOW_P (arg2.value))
4078 overflow_warning (location, result.value);
4079
4080 /* Warn about comparisons of different enum types. */
4081 if (warn_enum_compare
4082 && TREE_CODE_CLASS (code) == tcc_comparison
4083 && TREE_CODE (type1) == ENUMERAL_TYPE
4084 && TREE_CODE (type2) == ENUMERAL_TYPE
4085 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
4086 warning_at (location, OPT_Wenum_compare,
4087 "comparison between %qT and %qT",
4088 type1, type2);
4089
4090 if (warn_xor_used_as_pow
4091 && code == BIT_XOR_EXPR
4092 && arg1.m_decimal
4093 && arg2.m_decimal)
4094 check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
4095 location,
4096 arg2.get_location (), arg2.value);
4097
4098 return result;
4099 }
4100 \f
4101 /* Return a tree for the difference of pointers OP0 and OP1.
4102 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
4103 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
4104
4105 static tree
4106 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
4107 {
4108 tree restype = ptrdiff_type_node;
4109 tree result, inttype;
4110
4111 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
4112 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
4113 tree target_type = TREE_TYPE (TREE_TYPE (op0));
4114 tree orig_op0 = op0;
4115 tree orig_op1 = op1;
4116
4117 /* If the operands point into different address spaces, we need to
4118 explicitly convert them to pointers into the common address space
4119 before we can subtract the numerical address values. */
4120 if (as0 != as1)
4121 {
4122 addr_space_t as_common;
4123 tree common_type;
4124
4125 /* Determine the common superset address space. This is guaranteed
4126 to exist because the caller verified that comp_target_types
4127 returned non-zero. */
4128 if (!addr_space_superset (as0, as1, &as_common))
4129 gcc_unreachable ();
4130
4131 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4132 op0 = convert (common_type, op0);
4133 op1 = convert (common_type, op1);
4134 }
4135
4136 /* Determine integer type result of the subtraction. This will usually
4137 be the same as the result type (ptrdiff_t), but may need to be a wider
4138 type if pointers for the address space are wider than ptrdiff_t. */
4139 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4140 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4141 else
4142 inttype = restype;
4143
4144 if (VOID_TYPE_P (target_type))
4145 pedwarn (loc, OPT_Wpointer_arith,
4146 "pointer of type %<void *%> used in subtraction");
4147 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4148 pedwarn (loc, OPT_Wpointer_arith,
4149 "pointer to a function used in subtraction");
4150
4151 if (current_function_decl != NULL_TREE
4152 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4153 {
4154 op0 = save_expr (op0);
4155 op1 = save_expr (op1);
4156
4157 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4158 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4159 }
4160
4161 /* First do the subtraction, then build the divide operator
4162 and only convert at the very end.
4163 Do not do default conversions in case restype is a short type. */
4164
4165 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4166 pointers. If some platform cannot provide that, or has a larger
4167 ptrdiff_type to support differences larger than half the address
4168 space, cast the pointers to some larger integer type and do the
4169 computations in that type. */
4170 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4171 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4172 convert (inttype, op1), false);
4173 else
4174 {
4175 /* Cast away qualifiers. */
4176 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4177 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4178 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4179 }
4180
4181 /* This generates an error if op1 is pointer to incomplete type. */
4182 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4183 error_at (loc, "arithmetic on pointer to an incomplete type");
4184 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4185 TREE_TYPE (TREE_TYPE (orig_op0))))
4186 verify_type_context (loc, TCTX_POINTER_ARITH,
4187 TREE_TYPE (TREE_TYPE (orig_op1)));
4188
4189 op1 = c_size_in_bytes (target_type);
4190
4191 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4192 error_at (loc, "arithmetic on pointer to an empty aggregate");
4193
4194 /* Divide by the size, in easiest possible way. */
4195 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4196 op0, convert (inttype, op1));
4197
4198 /* Convert to final result type if necessary. */
4199 return convert (restype, result);
4200 }
4201 \f
4202 /* Expand atomic compound assignments into an appropriate sequence as
4203 specified by the C11 standard section 6.5.16.2.
4204
4205 _Atomic T1 E1
4206 T2 E2
4207 E1 op= E2
4208
4209 This sequence is used for all types for which these operations are
4210 supported.
4211
4212 In addition, built-in versions of the 'fe' prefixed routines may
4213 need to be invoked for floating point (real, complex or vector) when
4214 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4215
4216 T1 newval;
4217 T1 old;
4218 T1 *addr
4219 T2 val
4220 fenv_t fenv
4221
4222 addr = &E1;
4223 val = (E2);
4224 __atomic_load (addr, &old, SEQ_CST);
4225 feholdexcept (&fenv);
4226 loop:
4227 newval = old op val;
4228 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4229 SEQ_CST))
4230 goto done;
4231 feclearexcept (FE_ALL_EXCEPT);
4232 goto loop:
4233 done:
4234 feupdateenv (&fenv);
4235
4236 The compiler will issue the __atomic_fetch_* built-in when possible,
4237 otherwise it will generate the generic form of the atomic operations.
4238 This requires temp(s) and has their address taken. The atomic processing
4239 is smart enough to figure out when the size of an object can utilize
4240 a lock-free version, and convert the built-in call to the appropriate
4241 lock-free routine. The optimizers will then dispose of any temps that
4242 are no longer required, and lock-free implementations are utilized as
4243 long as there is target support for the required size.
4244
4245 If the operator is NOP_EXPR, then this is a simple assignment, and
4246 an __atomic_store is issued to perform the assignment rather than
4247 the above loop. */
4248
4249 /* Build an atomic assignment at LOC, expanding into the proper
4250 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4251 the result of the operation, unless RETURN_OLD_P, in which case
4252 return the old value of LHS (this is only for postincrement and
4253 postdecrement). */
4254
4255 static tree
4256 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4257 tree rhs, bool return_old_p)
4258 {
4259 tree fndecl, func_call;
4260 vec<tree, va_gc> *params;
4261 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4262 tree old, old_addr;
4263 tree compound_stmt = NULL_TREE;
4264 tree stmt, goto_stmt;
4265 tree loop_label, loop_decl, done_label, done_decl;
4266
4267 tree lhs_type = TREE_TYPE (lhs);
4268 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4269 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4270 tree rhs_semantic_type = TREE_TYPE (rhs);
4271 tree nonatomic_rhs_semantic_type;
4272 tree rhs_type;
4273
4274 gcc_assert (TYPE_ATOMIC (lhs_type));
4275
4276 if (return_old_p)
4277 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4278
4279 /* Allocate enough vector items for a compare_exchange. */
4280 vec_alloc (params, 6);
4281
4282 /* Create a compound statement to hold the sequence of statements
4283 with a loop. */
4284 if (modifycode != NOP_EXPR)
4285 {
4286 compound_stmt = c_begin_compound_stmt (false);
4287
4288 /* For consistency with build_modify_expr on non-_Atomic,
4289 mark the lhs as read. Also, it would be very hard to match
4290 such expressions in mark_exp_read. */
4291 mark_exp_read (lhs);
4292 }
4293
4294 /* Remove any excess precision (which is only present here in the
4295 case of compound assignments). */
4296 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4297 {
4298 gcc_assert (modifycode != NOP_EXPR);
4299 rhs = TREE_OPERAND (rhs, 0);
4300 }
4301 rhs_type = TREE_TYPE (rhs);
4302
4303 /* Fold the RHS if it hasn't already been folded. */
4304 if (modifycode != NOP_EXPR)
4305 rhs = c_fully_fold (rhs, false, NULL);
4306
4307 /* Remove the qualifiers for the rest of the expressions and create
4308 the VAL temp variable to hold the RHS. */
4309 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4310 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4311 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4312 TYPE_UNQUALIFIED);
4313 val = create_tmp_var_raw (nonatomic_rhs_type);
4314 TREE_ADDRESSABLE (val) = 1;
4315 suppress_warning (val);
4316 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4317 NULL_TREE);
4318 TREE_SIDE_EFFECTS (rhs) = 1;
4319 SET_EXPR_LOCATION (rhs, loc);
4320 if (modifycode != NOP_EXPR)
4321 add_stmt (rhs);
4322
4323 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4324 an atomic_store. */
4325 if (modifycode == NOP_EXPR)
4326 {
4327 compound_stmt = rhs;
4328 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4329 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4330 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4331 params->quick_push (lhs_addr);
4332 params->quick_push (rhs);
4333 params->quick_push (seq_cst);
4334 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4335
4336 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4337 compound_stmt, func_call);
4338
4339 /* VAL is the value which was stored, return a COMPOUND_STMT of
4340 the statement and that value. */
4341 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4342 }
4343
4344 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4345 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4346 isn't applicable for such builtins. ??? Do we want to handle enums? */
4347 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4348 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4349 {
4350 built_in_function fncode;
4351 switch (modifycode)
4352 {
4353 case PLUS_EXPR:
4354 case POINTER_PLUS_EXPR:
4355 fncode = (return_old_p
4356 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4357 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4358 break;
4359 case MINUS_EXPR:
4360 fncode = (return_old_p
4361 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4362 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4363 break;
4364 case BIT_AND_EXPR:
4365 fncode = (return_old_p
4366 ? BUILT_IN_ATOMIC_FETCH_AND_N
4367 : BUILT_IN_ATOMIC_AND_FETCH_N);
4368 break;
4369 case BIT_IOR_EXPR:
4370 fncode = (return_old_p
4371 ? BUILT_IN_ATOMIC_FETCH_OR_N
4372 : BUILT_IN_ATOMIC_OR_FETCH_N);
4373 break;
4374 case BIT_XOR_EXPR:
4375 fncode = (return_old_p
4376 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4377 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4378 break;
4379 default:
4380 goto cas_loop;
4381 }
4382
4383 /* We can only use "_1" through "_16" variants of the atomic fetch
4384 built-ins. */
4385 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4386 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4387 goto cas_loop;
4388
4389 /* If this is a pointer type, we need to multiply by the size of
4390 the pointer target type. */
4391 if (POINTER_TYPE_P (lhs_type))
4392 {
4393 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4394 /* ??? This would introduce -Wdiscarded-qualifiers
4395 warning: __atomic_fetch_* expect volatile void *
4396 type as the first argument. (Assignments between
4397 atomic and non-atomic objects are OK.) */
4398 || TYPE_RESTRICT (lhs_type))
4399 goto cas_loop;
4400 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4401 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4402 convert (ptrdiff_type_node, rhs),
4403 convert (ptrdiff_type_node, sz));
4404 }
4405
4406 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4407 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4408 fndecl = builtin_decl_explicit (fncode);
4409 params->quick_push (lhs_addr);
4410 params->quick_push (rhs);
4411 params->quick_push (seq_cst);
4412 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4413
4414 newval = create_tmp_var_raw (nonatomic_lhs_type);
4415 TREE_ADDRESSABLE (newval) = 1;
4416 suppress_warning (newval);
4417 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4418 NULL_TREE, NULL_TREE);
4419 SET_EXPR_LOCATION (rhs, loc);
4420 add_stmt (rhs);
4421
4422 /* Finish the compound statement. */
4423 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4424
4425 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4426 the statement and that value. */
4427 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4428 }
4429
4430 cas_loop:
4431 /* Create the variables and labels required for the op= form. */
4432 old = create_tmp_var_raw (nonatomic_lhs_type);
4433 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4434 TREE_ADDRESSABLE (old) = 1;
4435 suppress_warning (old);
4436
4437 newval = create_tmp_var_raw (nonatomic_lhs_type);
4438 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4439 TREE_ADDRESSABLE (newval) = 1;
4440 suppress_warning (newval);
4441
4442 loop_decl = create_artificial_label (loc);
4443 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4444
4445 done_decl = create_artificial_label (loc);
4446 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4447
4448 /* __atomic_load (addr, &old, SEQ_CST). */
4449 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4450 params->quick_push (lhs_addr);
4451 params->quick_push (old_addr);
4452 params->quick_push (seq_cst);
4453 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4454 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4455 NULL_TREE);
4456 add_stmt (old);
4457 params->truncate (0);
4458
4459 /* Create the expressions for floating-point environment
4460 manipulation, if required. */
4461 bool need_fenv = (flag_trapping_math
4462 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4463 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4464 if (need_fenv)
4465 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4466
4467 if (hold_call)
4468 add_stmt (hold_call);
4469
4470 /* loop: */
4471 add_stmt (loop_label);
4472
4473 /* newval = old + val; */
4474 if (rhs_type != rhs_semantic_type)
4475 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4476 rhs = build_binary_op (loc, modifycode, old, val, true);
4477 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4478 {
4479 tree eptype = TREE_TYPE (rhs);
4480 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4481 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4482 }
4483 else
4484 rhs = c_fully_fold (rhs, false, NULL);
4485 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4486 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4487 NULL_TREE, 0);
4488 if (rhs != error_mark_node)
4489 {
4490 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4491 NULL_TREE);
4492 SET_EXPR_LOCATION (rhs, loc);
4493 add_stmt (rhs);
4494 }
4495
4496 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4497 goto done; */
4498 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4499 params->quick_push (lhs_addr);
4500 params->quick_push (old_addr);
4501 params->quick_push (newval_addr);
4502 params->quick_push (integer_zero_node);
4503 params->quick_push (seq_cst);
4504 params->quick_push (seq_cst);
4505 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4506
4507 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4508 SET_EXPR_LOCATION (goto_stmt, loc);
4509
4510 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4511 SET_EXPR_LOCATION (stmt, loc);
4512 add_stmt (stmt);
4513
4514 if (clear_call)
4515 add_stmt (clear_call);
4516
4517 /* goto loop; */
4518 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4519 SET_EXPR_LOCATION (goto_stmt, loc);
4520 add_stmt (goto_stmt);
4521
4522 /* done: */
4523 add_stmt (done_label);
4524
4525 if (update_call)
4526 add_stmt (update_call);
4527
4528 /* Finish the compound statement. */
4529 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4530
4531 /* NEWVAL is the value that was successfully stored, return a
4532 COMPOUND_EXPR of the statement and the appropriate value. */
4533 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4534 return_old_p ? old : newval);
4535 }
4536
4537 /* Construct and perhaps optimize a tree representation
4538 for a unary operation. CODE, a tree_code, specifies the operation
4539 and XARG is the operand.
4540 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4541 promotions (such as from short to int).
4542 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4543 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4544 to pointers in C99.
4545
4546 LOCATION is the location of the operator. */
4547
4548 tree
4549 build_unary_op (location_t location, enum tree_code code, tree xarg,
4550 bool noconvert)
4551 {
4552 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4553 tree arg = xarg;
4554 tree argtype = NULL_TREE;
4555 enum tree_code typecode;
4556 tree val;
4557 tree ret = error_mark_node;
4558 tree eptype = NULL_TREE;
4559 const char *invalid_op_diag;
4560 bool int_operands;
4561
4562 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4563 if (int_operands)
4564 arg = remove_c_maybe_const_expr (arg);
4565
4566 if (code != ADDR_EXPR)
4567 arg = require_complete_type (location, arg);
4568
4569 typecode = TREE_CODE (TREE_TYPE (arg));
4570 if (typecode == ERROR_MARK)
4571 return error_mark_node;
4572 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4573 typecode = INTEGER_TYPE;
4574
4575 if ((invalid_op_diag
4576 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4577 {
4578 error_at (location, invalid_op_diag);
4579 return error_mark_node;
4580 }
4581
4582 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4583 {
4584 eptype = TREE_TYPE (arg);
4585 arg = TREE_OPERAND (arg, 0);
4586 }
4587
4588 switch (code)
4589 {
4590 case CONVERT_EXPR:
4591 /* This is used for unary plus, because a CONVERT_EXPR
4592 is enough to prevent anybody from looking inside for
4593 associativity, but won't generate any code. */
4594 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4595 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4596 || typecode == BITINT_TYPE
4597 || gnu_vector_type_p (TREE_TYPE (arg))))
4598 {
4599 error_at (location, "wrong type argument to unary plus");
4600 return error_mark_node;
4601 }
4602 else if (!noconvert)
4603 arg = default_conversion (arg);
4604 arg = non_lvalue_loc (location, arg);
4605 break;
4606
4607 case NEGATE_EXPR:
4608 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4609 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4610 || typecode == BITINT_TYPE
4611 || gnu_vector_type_p (TREE_TYPE (arg))))
4612 {
4613 error_at (location, "wrong type argument to unary minus");
4614 return error_mark_node;
4615 }
4616 else if (!noconvert)
4617 arg = default_conversion (arg);
4618 break;
4619
4620 case BIT_NOT_EXPR:
4621 /* ~ works on integer types and non float vectors. */
4622 if (typecode == INTEGER_TYPE
4623 || typecode == BITINT_TYPE
4624 || (gnu_vector_type_p (TREE_TYPE (arg))
4625 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4626 {
4627 tree e = arg;
4628
4629 /* Warn if the expression has boolean value. */
4630 while (TREE_CODE (e) == COMPOUND_EXPR)
4631 e = TREE_OPERAND (e, 1);
4632
4633 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
4634 || truth_value_p (TREE_CODE (e))))
4635 {
4636 auto_diagnostic_group d;
4637 if (warning_at (location, OPT_Wbool_operation,
4638 "%<~%> on a boolean expression"))
4639 {
4640 gcc_rich_location richloc (location);
4641 richloc.add_fixit_insert_before (location, "!");
4642 inform (&richloc, "did you mean to use logical not?");
4643 }
4644 }
4645 if (!noconvert)
4646 arg = default_conversion (arg);
4647 }
4648 else if (typecode == COMPLEX_TYPE)
4649 {
4650 code = CONJ_EXPR;
4651 pedwarn (location, OPT_Wpedantic,
4652 "ISO C does not support %<~%> for complex conjugation");
4653 if (!noconvert)
4654 arg = default_conversion (arg);
4655 }
4656 else
4657 {
4658 error_at (location, "wrong type argument to bit-complement");
4659 return error_mark_node;
4660 }
4661 break;
4662
4663 case ABS_EXPR:
4664 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4665 {
4666 error_at (location, "wrong type argument to abs");
4667 return error_mark_node;
4668 }
4669 else if (!noconvert)
4670 arg = default_conversion (arg);
4671 break;
4672
4673 case ABSU_EXPR:
4674 if (!(typecode == INTEGER_TYPE))
4675 {
4676 error_at (location, "wrong type argument to absu");
4677 return error_mark_node;
4678 }
4679 else if (!noconvert)
4680 arg = default_conversion (arg);
4681 break;
4682
4683 case CONJ_EXPR:
4684 /* Conjugating a real value is a no-op, but allow it anyway. */
4685 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4686 || typecode == COMPLEX_TYPE))
4687 {
4688 error_at (location, "wrong type argument to conjugation");
4689 return error_mark_node;
4690 }
4691 else if (!noconvert)
4692 arg = default_conversion (arg);
4693 break;
4694
4695 case TRUTH_NOT_EXPR:
4696 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4697 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4698 && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE
4699 && typecode != BITINT_TYPE)
4700 {
4701 error_at (location,
4702 "wrong type argument to unary exclamation mark");
4703 return error_mark_node;
4704 }
4705 if (int_operands)
4706 {
4707 arg = c_objc_common_truthvalue_conversion (location, xarg);
4708 arg = remove_c_maybe_const_expr (arg);
4709 }
4710 else
4711 arg = c_objc_common_truthvalue_conversion (location, arg);
4712 ret = invert_truthvalue_loc (location, arg);
4713 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4714 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4715 location = EXPR_LOCATION (ret);
4716 goto return_build_unary_op;
4717
4718 case REALPART_EXPR:
4719 case IMAGPART_EXPR:
4720 ret = build_real_imag_expr (location, code, arg);
4721 if (ret == error_mark_node)
4722 return error_mark_node;
4723 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4724 eptype = TREE_TYPE (eptype);
4725 goto return_build_unary_op;
4726
4727 case PREINCREMENT_EXPR:
4728 case POSTINCREMENT_EXPR:
4729 case PREDECREMENT_EXPR:
4730 case POSTDECREMENT_EXPR:
4731
4732 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4733 {
4734 tree inner = build_unary_op (location, code,
4735 C_MAYBE_CONST_EXPR_EXPR (arg),
4736 noconvert);
4737 if (inner == error_mark_node)
4738 return error_mark_node;
4739 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4740 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4741 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4742 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4743 goto return_build_unary_op;
4744 }
4745
4746 /* Complain about anything that is not a true lvalue. In
4747 Objective-C, skip this check for property_refs. */
4748 if (!objc_is_property_ref (arg)
4749 && !lvalue_or_else (location,
4750 arg, ((code == PREINCREMENT_EXPR
4751 || code == POSTINCREMENT_EXPR)
4752 ? lv_increment
4753 : lv_decrement)))
4754 return error_mark_node;
4755
4756 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4757 {
4758 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4759 warning_at (location, OPT_Wc___compat,
4760 "increment of enumeration value is invalid in C++");
4761 else
4762 warning_at (location, OPT_Wc___compat,
4763 "decrement of enumeration value is invalid in C++");
4764 }
4765
4766 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4767 {
4768 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4769 warning_at (location, OPT_Wbool_operation,
4770 "increment of a boolean expression");
4771 else
4772 warning_at (location, OPT_Wbool_operation,
4773 "decrement of a boolean expression");
4774 }
4775
4776 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4777 arg = c_fully_fold (arg, false, NULL, true);
4778
4779 bool atomic_op;
4780 atomic_op = really_atomic_lvalue (arg);
4781
4782 /* Increment or decrement the real part of the value,
4783 and don't change the imaginary part. */
4784 if (typecode == COMPLEX_TYPE)
4785 {
4786 tree real, imag;
4787
4788 pedwarn (location, OPT_Wpedantic,
4789 "ISO C does not support %<++%> and %<--%> on complex types");
4790
4791 if (!atomic_op)
4792 {
4793 arg = stabilize_reference (arg);
4794 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4795 true);
4796 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4797 true);
4798 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4799 if (real == error_mark_node || imag == error_mark_node)
4800 return error_mark_node;
4801 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4802 real, imag);
4803 goto return_build_unary_op;
4804 }
4805 }
4806
4807 /* Report invalid types. */
4808
4809 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4810 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4811 && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE
4812 && !gnu_vector_type_p (TREE_TYPE (arg)))
4813 {
4814 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4815 error_at (location, "wrong type argument to increment");
4816 else
4817 error_at (location, "wrong type argument to decrement");
4818
4819 return error_mark_node;
4820 }
4821
4822 {
4823 tree inc;
4824
4825 argtype = TREE_TYPE (arg);
4826
4827 /* Compute the increment. */
4828
4829 if (typecode == POINTER_TYPE)
4830 {
4831 /* If pointer target is an incomplete type,
4832 we just cannot know how to do the arithmetic. */
4833 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4834 {
4835 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4836 error_at (location,
4837 "increment of pointer to an incomplete type %qT",
4838 TREE_TYPE (argtype));
4839 else
4840 error_at (location,
4841 "decrement of pointer to an incomplete type %qT",
4842 TREE_TYPE (argtype));
4843 }
4844 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4845 || VOID_TYPE_P (TREE_TYPE (argtype)))
4846 {
4847 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4848 pedwarn (location, OPT_Wpointer_arith,
4849 "wrong type argument to increment");
4850 else
4851 pedwarn (location, OPT_Wpointer_arith,
4852 "wrong type argument to decrement");
4853 }
4854 else
4855 verify_type_context (location, TCTX_POINTER_ARITH,
4856 TREE_TYPE (argtype));
4857
4858 inc = c_size_in_bytes (TREE_TYPE (argtype));
4859 inc = convert_to_ptrofftype_loc (location, inc);
4860 }
4861 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4862 {
4863 /* For signed fract types, we invert ++ to -- or
4864 -- to ++, and change inc from 1 to -1, because
4865 it is not possible to represent 1 in signed fract constants.
4866 For unsigned fract types, the result always overflows and
4867 we get an undefined (original) or the maximum value. */
4868 if (code == PREINCREMENT_EXPR)
4869 code = PREDECREMENT_EXPR;
4870 else if (code == PREDECREMENT_EXPR)
4871 code = PREINCREMENT_EXPR;
4872 else if (code == POSTINCREMENT_EXPR)
4873 code = POSTDECREMENT_EXPR;
4874 else /* code == POSTDECREMENT_EXPR */
4875 code = POSTINCREMENT_EXPR;
4876
4877 inc = integer_minus_one_node;
4878 inc = convert (argtype, inc);
4879 }
4880 else
4881 {
4882 inc = VECTOR_TYPE_P (argtype)
4883 ? build_one_cst (argtype)
4884 : integer_one_node;
4885 inc = convert (argtype, inc);
4886 }
4887
4888 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4889 need to ask Objective-C to build the increment or decrement
4890 expression for it. */
4891 if (objc_is_property_ref (arg))
4892 return objc_build_incr_expr_for_property_ref (location, code,
4893 arg, inc);
4894
4895 /* Report a read-only lvalue. */
4896 if (TYPE_READONLY (argtype))
4897 {
4898 readonly_error (location, arg,
4899 ((code == PREINCREMENT_EXPR
4900 || code == POSTINCREMENT_EXPR)
4901 ? lv_increment : lv_decrement));
4902 return error_mark_node;
4903 }
4904 else if (TREE_READONLY (arg))
4905 readonly_warning (arg,
4906 ((code == PREINCREMENT_EXPR
4907 || code == POSTINCREMENT_EXPR)
4908 ? lv_increment : lv_decrement));
4909
4910 /* If the argument is atomic, use the special code sequences for
4911 atomic compound assignment. */
4912 if (atomic_op)
4913 {
4914 arg = stabilize_reference (arg);
4915 ret = build_atomic_assign (location, arg,
4916 ((code == PREINCREMENT_EXPR
4917 || code == POSTINCREMENT_EXPR)
4918 ? PLUS_EXPR
4919 : MINUS_EXPR),
4920 (FRACT_MODE_P (TYPE_MODE (argtype))
4921 ? inc
4922 : integer_one_node),
4923 (code == POSTINCREMENT_EXPR
4924 || code == POSTDECREMENT_EXPR));
4925 goto return_build_unary_op;
4926 }
4927
4928 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4929 val = boolean_increment (code, arg);
4930 else
4931 val = build2 (code, TREE_TYPE (arg), arg, inc);
4932 TREE_SIDE_EFFECTS (val) = 1;
4933 if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
4934 TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
4935 TYPE_UNQUALIFIED);
4936 ret = val;
4937 goto return_build_unary_op;
4938 }
4939
4940 case ADDR_EXPR:
4941 /* Note that this operation never does default_conversion. */
4942
4943 /* The operand of unary '&' must be an lvalue (which excludes
4944 expressions of type void), or, in C99, the result of a [] or
4945 unary '*' operator. */
4946 if (VOID_TYPE_P (TREE_TYPE (arg))
4947 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4948 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4949 pedwarn (location, 0, "taking address of expression of type %<void%>");
4950
4951 /* Let &* cancel out to simplify resulting code. */
4952 if (INDIRECT_REF_P (arg))
4953 {
4954 /* Don't let this be an lvalue. */
4955 if (lvalue_p (TREE_OPERAND (arg, 0)))
4956 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4957 ret = TREE_OPERAND (arg, 0);
4958 goto return_build_unary_op;
4959 }
4960
4961 /* Anything not already handled and not a true memory reference
4962 or a non-lvalue array is an error. */
4963 if (typecode != FUNCTION_TYPE && !noconvert
4964 && !lvalue_or_else (location, arg, lv_addressof))
4965 return error_mark_node;
4966
4967 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4968 folding later. */
4969 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4970 {
4971 tree inner = build_unary_op (location, code,
4972 C_MAYBE_CONST_EXPR_EXPR (arg),
4973 noconvert);
4974 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4975 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4976 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4977 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4978 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4979 goto return_build_unary_op;
4980 }
4981
4982 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4983 argtype = TREE_TYPE (arg);
4984
4985 /* If the lvalue is const or volatile, merge that into the type
4986 to which the address will point. This is only needed
4987 for function types. */
4988 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4989 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4990 && TREE_CODE (argtype) == FUNCTION_TYPE)
4991 {
4992 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4993 int quals = orig_quals;
4994
4995 if (TREE_READONLY (arg))
4996 quals |= TYPE_QUAL_CONST;
4997 if (TREE_THIS_VOLATILE (arg))
4998 quals |= TYPE_QUAL_VOLATILE;
4999
5000 argtype = c_build_qualified_type (argtype, quals);
5001 }
5002
5003 switch (TREE_CODE (arg))
5004 {
5005 case COMPONENT_REF:
5006 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5007 {
5008 error_at (location, "cannot take address of bit-field %qD",
5009 TREE_OPERAND (arg, 1));
5010 return error_mark_node;
5011 }
5012
5013 /* fall through */
5014
5015 case ARRAY_REF:
5016 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
5017 {
5018 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
5019 && !POINTER_TYPE_P (TREE_TYPE (arg))
5020 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
5021 {
5022 error_at (location, "cannot take address of scalar with "
5023 "reverse storage order");
5024 return error_mark_node;
5025 }
5026
5027 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
5028 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
5029 warning_at (location, OPT_Wscalar_storage_order,
5030 "address of array with reverse scalar storage "
5031 "order requested");
5032 }
5033
5034 default:
5035 break;
5036 }
5037
5038 if (!c_mark_addressable (arg))
5039 return error_mark_node;
5040
5041 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
5042 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
5043
5044 argtype = build_pointer_type (argtype);
5045
5046 /* ??? Cope with user tricks that amount to offsetof. Delete this
5047 when we have proper support for integer constant expressions. */
5048 val = get_base_address (arg);
5049 if (val && INDIRECT_REF_P (val)
5050 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5051 {
5052 ret = fold_offsetof (arg, argtype);
5053 goto return_build_unary_op;
5054 }
5055
5056 val = build1 (ADDR_EXPR, argtype, arg);
5057
5058 ret = val;
5059 goto return_build_unary_op;
5060
5061 case PAREN_EXPR:
5062 ret = build1 (code, TREE_TYPE (arg), arg);
5063 goto return_build_unary_op;
5064
5065 default:
5066 gcc_unreachable ();
5067 }
5068
5069 if (argtype == NULL_TREE)
5070 argtype = TREE_TYPE (arg);
5071 if (TREE_CODE (arg) == INTEGER_CST)
5072 ret = (require_constant_value
5073 ? fold_build1_initializer_loc (location, code, argtype, arg)
5074 : fold_build1_loc (location, code, argtype, arg));
5075 else
5076 ret = build1 (code, argtype, arg);
5077 return_build_unary_op:
5078 gcc_assert (ret != error_mark_node);
5079 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
5080 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
5081 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
5082 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
5083 ret = note_integer_operands (ret);
5084 if (eptype)
5085 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5086 protected_set_expr_location (ret, location);
5087 return ret;
5088 }
5089
5090 /* Return nonzero if REF is an lvalue valid for this language.
5091 Lvalues can be assigned, unless their type has TYPE_READONLY.
5092 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
5093
5094 bool
5095 lvalue_p (const_tree ref)
5096 {
5097 const enum tree_code code = TREE_CODE (ref);
5098
5099 switch (code)
5100 {
5101 case REALPART_EXPR:
5102 case IMAGPART_EXPR:
5103 case COMPONENT_REF:
5104 return lvalue_p (TREE_OPERAND (ref, 0));
5105
5106 case C_MAYBE_CONST_EXPR:
5107 return lvalue_p (TREE_OPERAND (ref, 1));
5108
5109 case COMPOUND_LITERAL_EXPR:
5110 case STRING_CST:
5111 return true;
5112
5113 case MEM_REF:
5114 case TARGET_MEM_REF:
5115 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
5116 here as well. */
5117 case INDIRECT_REF:
5118 case ARRAY_REF:
5119 case VAR_DECL:
5120 case PARM_DECL:
5121 case RESULT_DECL:
5122 case ERROR_MARK:
5123 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
5124 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5125
5126 case BIND_EXPR:
5127 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5128
5129 default:
5130 return false;
5131 }
5132 }
5133 \f
5134 /* Give a warning for storing in something that is read-only in GCC
5135 terms but not const in ISO C terms. */
5136
5137 static void
5138 readonly_warning (tree arg, enum lvalue_use use)
5139 {
5140 switch (use)
5141 {
5142 case lv_assign:
5143 warning (0, "assignment of read-only location %qE", arg);
5144 break;
5145 case lv_increment:
5146 warning (0, "increment of read-only location %qE", arg);
5147 break;
5148 case lv_decrement:
5149 warning (0, "decrement of read-only location %qE", arg);
5150 break;
5151 default:
5152 gcc_unreachable ();
5153 }
5154 return;
5155 }
5156
5157
5158 /* Return nonzero if REF is an lvalue valid for this language;
5159 otherwise, print an error message and return zero. USE says
5160 how the lvalue is being used and so selects the error message.
5161 LOCATION is the location at which any error should be reported. */
5162
5163 static int
5164 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5165 {
5166 int win = lvalue_p (ref);
5167
5168 if (!win)
5169 lvalue_error (loc, use);
5170
5171 return win;
5172 }
5173 \f
5174 /* Mark EXP saying that we need to be able to take the
5175 address of it; it should not be allocated in a register.
5176 Returns true if successful. ARRAY_REF_P is true if this
5177 is for ARRAY_REF construction - in that case we don't want
5178 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5179 it is fine to use ARRAY_REFs for vector subscripts on vector
5180 register variables. */
5181
5182 bool
5183 c_mark_addressable (tree exp, bool array_ref_p)
5184 {
5185 tree x = exp;
5186
5187 while (1)
5188 switch (TREE_CODE (x))
5189 {
5190 case VIEW_CONVERT_EXPR:
5191 if (array_ref_p
5192 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5193 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5194 return true;
5195 x = TREE_OPERAND (x, 0);
5196 break;
5197
5198 case COMPONENT_REF:
5199 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5200 {
5201 error ("cannot take address of bit-field %qD",
5202 TREE_OPERAND (x, 1));
5203 return false;
5204 }
5205 /* FALLTHRU */
5206 case ADDR_EXPR:
5207 case ARRAY_REF:
5208 case REALPART_EXPR:
5209 case IMAGPART_EXPR:
5210 x = TREE_OPERAND (x, 0);
5211 break;
5212
5213 case COMPOUND_LITERAL_EXPR:
5214 if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
5215 {
5216 error ("address of register compound literal requested");
5217 return false;
5218 }
5219 TREE_ADDRESSABLE (x) = 1;
5220 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5221 return true;
5222
5223 case CONSTRUCTOR:
5224 TREE_ADDRESSABLE (x) = 1;
5225 return true;
5226
5227 case VAR_DECL:
5228 case CONST_DECL:
5229 case PARM_DECL:
5230 case RESULT_DECL:
5231 if (C_DECL_REGISTER (x)
5232 && DECL_NONLOCAL (x))
5233 {
5234 if (TREE_PUBLIC (x) || is_global_var (x))
5235 {
5236 error
5237 ("global register variable %qD used in nested function", x);
5238 return false;
5239 }
5240 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5241 }
5242 else if (C_DECL_REGISTER (x))
5243 {
5244 if (TREE_PUBLIC (x) || is_global_var (x))
5245 error ("address of global register variable %qD requested", x);
5246 else
5247 error ("address of register variable %qD requested", x);
5248 return false;
5249 }
5250
5251 /* FALLTHRU */
5252 case FUNCTION_DECL:
5253 TREE_ADDRESSABLE (x) = 1;
5254 /* FALLTHRU */
5255 default:
5256 return true;
5257 }
5258 }
5259 \f
5260 /* Convert EXPR to TYPE, warning about conversion problems with
5261 constants. SEMANTIC_TYPE is the type this conversion would use
5262 without excess precision. If SEMANTIC_TYPE is NULL, this function
5263 is equivalent to convert_and_check. This function is a wrapper that
5264 handles conversions that may be different than
5265 the usual ones because of excess precision. */
5266
5267 static tree
5268 ep_convert_and_check (location_t loc, tree type, tree expr,
5269 tree semantic_type)
5270 {
5271 if (TREE_TYPE (expr) == type)
5272 return expr;
5273
5274 /* For C11, integer conversions may have results with excess
5275 precision. */
5276 if (flag_isoc11 || !semantic_type)
5277 return convert_and_check (loc, type, expr);
5278
5279 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5280 && TREE_TYPE (expr) != semantic_type)
5281 {
5282 /* For integers, we need to check the real conversion, not
5283 the conversion to the excess precision type. */
5284 expr = convert_and_check (loc, semantic_type, expr);
5285 }
5286 /* Result type is the excess precision type, which should be
5287 large enough, so do not check. */
5288 return convert (type, expr);
5289 }
5290
5291 /* If EXPR refers to a built-in declared without a prototype returns
5292 the actual type of the built-in and, if non-null, set *BLTIN to
5293 a pointer to the built-in. Otherwise return the type of EXPR
5294 and clear *BLTIN if non-null. */
5295
5296 static tree
5297 type_or_builtin_type (tree expr, tree *bltin = NULL)
5298 {
5299 tree dummy;
5300 if (!bltin)
5301 bltin = &dummy;
5302
5303 *bltin = NULL_TREE;
5304
5305 tree type = TREE_TYPE (expr);
5306 if (TREE_CODE (expr) != ADDR_EXPR)
5307 return type;
5308
5309 tree oper = TREE_OPERAND (expr, 0);
5310 if (!DECL_P (oper)
5311 || TREE_CODE (oper) != FUNCTION_DECL
5312 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5313 return type;
5314
5315 built_in_function code = DECL_FUNCTION_CODE (oper);
5316 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5317 return type;
5318
5319 if ((*bltin = builtin_decl_implicit (code)))
5320 type = build_pointer_type (TREE_TYPE (*bltin));
5321
5322 return type;
5323 }
5324
5325 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5326 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5327 if folded to an integer constant then the unselected half may
5328 contain arbitrary operations not normally permitted in constant
5329 expressions. Set the location of the expression to LOC. */
5330
5331 tree
5332 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5333 tree op1, tree op1_original_type, location_t op1_loc,
5334 tree op2, tree op2_original_type, location_t op2_loc)
5335 {
5336 tree type1;
5337 tree type2;
5338 enum tree_code code1;
5339 enum tree_code code2;
5340 tree result_type = NULL;
5341 tree semantic_result_type = NULL;
5342 tree orig_op1 = op1, orig_op2 = op2;
5343 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5344 bool ifexp_int_operands;
5345 tree ret;
5346
5347 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5348 if (op1_int_operands)
5349 op1 = remove_c_maybe_const_expr (op1);
5350 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5351 if (op2_int_operands)
5352 op2 = remove_c_maybe_const_expr (op2);
5353 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5354 if (ifexp_int_operands)
5355 ifexp = remove_c_maybe_const_expr (ifexp);
5356
5357 /* Promote both alternatives. */
5358
5359 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5360 op1 = default_conversion (op1);
5361 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5362 op2 = default_conversion (op2);
5363
5364 if (TREE_CODE (ifexp) == ERROR_MARK
5365 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5366 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5367 return error_mark_node;
5368
5369 tree bltin1 = NULL_TREE;
5370 tree bltin2 = NULL_TREE;
5371 type1 = type_or_builtin_type (op1, &bltin1);
5372 code1 = TREE_CODE (type1);
5373 type2 = type_or_builtin_type (op2, &bltin2);
5374 code2 = TREE_CODE (type2);
5375
5376 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5377 return error_mark_node;
5378
5379 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5380 return error_mark_node;
5381
5382 /* C90 does not permit non-lvalue arrays in conditional expressions.
5383 In C99 they will be pointers by now. */
5384 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5385 {
5386 error_at (colon_loc, "non-lvalue array in conditional expression");
5387 return error_mark_node;
5388 }
5389
5390 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5391 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5392 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5393 || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
5394 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5395 || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
5396 {
5397 semantic_result_type = c_common_type (type1, type2);
5398 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5399 {
5400 op1 = TREE_OPERAND (op1, 0);
5401 type1 = TREE_TYPE (op1);
5402 gcc_assert (TREE_CODE (type1) == code1);
5403 }
5404 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5405 {
5406 op2 = TREE_OPERAND (op2, 0);
5407 type2 = TREE_TYPE (op2);
5408 gcc_assert (TREE_CODE (type2) == code2);
5409 }
5410 }
5411
5412 if (warn_cxx_compat)
5413 {
5414 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5415 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5416
5417 if (TREE_CODE (t1) == ENUMERAL_TYPE
5418 && TREE_CODE (t2) == ENUMERAL_TYPE
5419 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5420 warning_at (colon_loc, OPT_Wc___compat,
5421 ("different enum types in conditional is "
5422 "invalid in C++: %qT vs %qT"),
5423 t1, t2);
5424 }
5425
5426 /* Quickly detect the usual case where op1 and op2 have the same type
5427 after promotion. */
5428 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5429 {
5430 if (type1 == type2)
5431 result_type = type1;
5432 else
5433 result_type = TYPE_MAIN_VARIANT (type1);
5434 }
5435 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5436 || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
5437 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5438 || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
5439 {
5440 /* In C11, a conditional expression between a floating-point
5441 type and an integer type should convert the integer type to
5442 the evaluation format of the floating-point type, with
5443 possible excess precision. */
5444 tree eptype1 = type1;
5445 tree eptype2 = type2;
5446 if (flag_isoc11)
5447 {
5448 tree eptype;
5449 if (ANY_INTEGRAL_TYPE_P (type1)
5450 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5451 {
5452 eptype2 = eptype;
5453 if (!semantic_result_type)
5454 semantic_result_type = c_common_type (type1, type2);
5455 }
5456 else if (ANY_INTEGRAL_TYPE_P (type2)
5457 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5458 {
5459 eptype1 = eptype;
5460 if (!semantic_result_type)
5461 semantic_result_type = c_common_type (type1, type2);
5462 }
5463 }
5464 result_type = c_common_type (eptype1, eptype2);
5465 if (result_type == error_mark_node)
5466 return error_mark_node;
5467 do_warn_double_promotion (result_type, type1, type2,
5468 "implicit conversion from %qT to %qT to "
5469 "match other result of conditional",
5470 colon_loc);
5471
5472 /* If -Wsign-compare, warn here if type1 and type2 have
5473 different signedness. We'll promote the signed to unsigned
5474 and later code won't know it used to be different.
5475 Do this check on the original types, so that explicit casts
5476 will be considered, but default promotions won't. */
5477 if (c_inhibit_evaluation_warnings == 0)
5478 {
5479 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5480 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5481
5482 if (unsigned_op1 ^ unsigned_op2)
5483 {
5484 bool ovf;
5485
5486 /* Do not warn if the result type is signed, since the
5487 signed type will only be chosen if it can represent
5488 all the values of the unsigned type. */
5489 if (!TYPE_UNSIGNED (result_type))
5490 /* OK */;
5491 else
5492 {
5493 bool op1_maybe_const = true;
5494 bool op2_maybe_const = true;
5495
5496 /* Do not warn if the signed quantity is an
5497 unsuffixed integer literal (or some static
5498 constant expression involving such literals) and
5499 it is non-negative. This warning requires the
5500 operands to be folded for best results, so do
5501 that folding in this case even without
5502 warn_sign_compare to avoid warning options
5503 possibly affecting code generation. */
5504 c_inhibit_evaluation_warnings
5505 += (ifexp == truthvalue_false_node);
5506 op1 = c_fully_fold (op1, require_constant_value,
5507 &op1_maybe_const);
5508 c_inhibit_evaluation_warnings
5509 -= (ifexp == truthvalue_false_node);
5510
5511 c_inhibit_evaluation_warnings
5512 += (ifexp == truthvalue_true_node);
5513 op2 = c_fully_fold (op2, require_constant_value,
5514 &op2_maybe_const);
5515 c_inhibit_evaluation_warnings
5516 -= (ifexp == truthvalue_true_node);
5517
5518 if (warn_sign_compare)
5519 {
5520 if ((unsigned_op2
5521 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5522 || (unsigned_op1
5523 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5524 /* OK */;
5525 else if (unsigned_op2)
5526 warning_at (op1_loc, OPT_Wsign_compare,
5527 "operand of %<?:%> changes signedness from "
5528 "%qT to %qT due to unsignedness of other "
5529 "operand", TREE_TYPE (orig_op1),
5530 TREE_TYPE (orig_op2));
5531 else
5532 warning_at (op2_loc, OPT_Wsign_compare,
5533 "operand of %<?:%> changes signedness from "
5534 "%qT to %qT due to unsignedness of other "
5535 "operand", TREE_TYPE (orig_op2),
5536 TREE_TYPE (orig_op1));
5537 }
5538 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5539 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5540 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5541 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5542 }
5543 }
5544 }
5545 }
5546 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5547 {
5548 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5549 pedwarn (colon_loc, OPT_Wpedantic,
5550 "ISO C forbids conditional expr with only one void side");
5551 result_type = void_type_node;
5552 }
5553 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5554 {
5555 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5556 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5557 addr_space_t as_common;
5558
5559 if (comp_target_types (colon_loc, type1, type2))
5560 result_type = common_pointer_type (type1, type2);
5561 else if (null_pointer_constant_p (orig_op1))
5562 result_type = type2;
5563 else if (null_pointer_constant_p (orig_op2))
5564 result_type = type1;
5565 else if (!addr_space_superset (as1, as2, &as_common))
5566 {
5567 error_at (colon_loc, "pointers to disjoint address spaces "
5568 "used in conditional expression");
5569 return error_mark_node;
5570 }
5571 else if ((VOID_TYPE_P (TREE_TYPE (type1))
5572 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5573 || (VOID_TYPE_P (TREE_TYPE (type2))
5574 && !TYPE_ATOMIC (TREE_TYPE (type2))))
5575 {
5576 tree t1 = TREE_TYPE (type1);
5577 tree t2 = TREE_TYPE (type2);
5578 if (!(VOID_TYPE_P (t1)
5579 && !TYPE_ATOMIC (t1)))
5580 {
5581 /* roles are swapped */
5582 t1 = t2;
5583 t2 = TREE_TYPE (type1);
5584 }
5585 tree t2_stripped = strip_array_types (t2);
5586 if ((TREE_CODE (t2) == ARRAY_TYPE)
5587 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5588 {
5589 if (!flag_isoc23)
5590 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5591 "pointer to array loses qualifier "
5592 "in conditional expression");
5593 else if (warn_c11_c23_compat > 0)
5594 warning_at (colon_loc, OPT_Wc11_c23_compat,
5595 "pointer to array loses qualifier "
5596 "in conditional expression in ISO C before C23");
5597 }
5598 if (TREE_CODE (t2) == FUNCTION_TYPE)
5599 pedwarn (colon_loc, OPT_Wpedantic,
5600 "ISO C forbids conditional expr between "
5601 "%<void *%> and function pointer");
5602 /* for array, use qualifiers of element type */
5603 if (flag_isoc23)
5604 t2 = t2_stripped;
5605 result_type = build_pointer_type (qualify_type (t1, t2));
5606 }
5607 /* Objective-C pointer comparisons are a bit more lenient. */
5608 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5609 result_type = objc_common_type (type1, type2);
5610 else
5611 {
5612 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5613 diagnostic_t kind = DK_PERMERROR;
5614 if (!flag_isoc99)
5615 /* This downgrade to a warning ensures that -std=gnu89
5616 -pedantic-errors does not flag these mismatches between
5617 builtins as errors (as DK_PERMERROR would). ISO C99
5618 and later do not have implicit function declarations,
5619 so the mismatch cannot occur naturally there. */
5620 kind = bltin1 && bltin2 ? DK_WARNING : DK_PEDWARN;
5621 if (emit_diagnostic (kind, colon_loc, OPT_Wincompatible_pointer_types,
5622 "pointer type mismatch "
5623 "in conditional expression"))
5624 {
5625 inform (op1_loc, "first expression has type %qT", type1);
5626 inform (op2_loc, "second expression has type %qT", type2);
5627 }
5628 result_type = build_pointer_type
5629 (build_qualified_type (void_type_node, qual));
5630 }
5631 }
5632 else if (code1 == POINTER_TYPE
5633 && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE))
5634 {
5635 if (!null_pointer_constant_p (orig_op2))
5636 permerror_opt (colon_loc, OPT_Wint_conversion,
5637 "pointer/integer type mismatch "
5638 "in conditional expression");
5639 else
5640 {
5641 op2 = null_pointer_node;
5642 }
5643 result_type = type1;
5644 }
5645 else if (code2 == POINTER_TYPE
5646 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
5647 {
5648 if (!null_pointer_constant_p (orig_op1))
5649 permerror_opt (colon_loc, OPT_Wint_conversion,
5650 "pointer/integer type mismatch "
5651 "in conditional expression");
5652 else
5653 {
5654 op1 = null_pointer_node;
5655 }
5656 result_type = type2;
5657 }
5658 /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
5659 type nullptr_t and the other is a pointer, the result type is the pointer
5660 type." */
5661 else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
5662 result_type = type2;
5663 else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
5664 result_type = type1;
5665 else if (RECORD_OR_UNION_TYPE_P (type1) && RECORD_OR_UNION_TYPE_P (type2)
5666 && comptypes (TYPE_MAIN_VARIANT (type1),
5667 TYPE_MAIN_VARIANT (type2)))
5668 result_type = composite_type (TYPE_MAIN_VARIANT (type1),
5669 TYPE_MAIN_VARIANT (type2));
5670
5671 if (!result_type)
5672 {
5673 if (flag_cond_mismatch)
5674 result_type = void_type_node;
5675 else
5676 {
5677 error_at (colon_loc, "type mismatch in conditional expression");
5678 return error_mark_node;
5679 }
5680 }
5681
5682 /* Merge const and volatile flags of the incoming types. */
5683 result_type
5684 = build_type_variant (result_type,
5685 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5686 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5687
5688 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5689 semantic_result_type);
5690 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5691 semantic_result_type);
5692
5693 if (ifexp_bcp && ifexp == truthvalue_true_node)
5694 {
5695 op2_int_operands = true;
5696 op1 = c_fully_fold (op1, require_constant_value, NULL);
5697 }
5698 if (ifexp_bcp && ifexp == truthvalue_false_node)
5699 {
5700 op1_int_operands = true;
5701 op2 = c_fully_fold (op2, require_constant_value, NULL);
5702 }
5703 int_const = int_operands = (ifexp_int_operands
5704 && op1_int_operands
5705 && op2_int_operands);
5706 if (int_operands)
5707 {
5708 int_const = ((ifexp == truthvalue_true_node
5709 && TREE_CODE (orig_op1) == INTEGER_CST
5710 && !TREE_OVERFLOW (orig_op1))
5711 || (ifexp == truthvalue_false_node
5712 && TREE_CODE (orig_op2) == INTEGER_CST
5713 && !TREE_OVERFLOW (orig_op2)));
5714 }
5715
5716 /* Need to convert condition operand into a vector mask. */
5717 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5718 {
5719 tree vectype = TREE_TYPE (ifexp);
5720 tree elem_type = TREE_TYPE (vectype);
5721 tree zero = build_int_cst (elem_type, 0);
5722 tree zero_vec = build_vector_from_val (vectype, zero);
5723 tree cmp_type = truth_type_for (vectype);
5724 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5725 }
5726
5727 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5728 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5729 else
5730 {
5731 if (int_operands)
5732 {
5733 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5734 nested inside of the expression. */
5735 op1 = c_fully_fold (op1, false, NULL);
5736 op2 = c_fully_fold (op2, false, NULL);
5737 }
5738 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5739 if (int_operands)
5740 ret = note_integer_operands (ret);
5741 }
5742 if (semantic_result_type)
5743 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5744
5745 protected_set_expr_location (ret, colon_loc);
5746
5747 /* If the OP1 and OP2 are the same and don't have side-effects,
5748 warn here, because the COND_EXPR will be turned into OP1. */
5749 if (warn_duplicated_branches
5750 && TREE_CODE (ret) == COND_EXPR
5751 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5752 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5753 "this condition has identical branches");
5754
5755 return ret;
5756 }
5757 \f
5758 /* EXPR is an expression, location LOC, whose result is discarded.
5759 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5760 whose right-hand operand is such a call, possibly recursively). */
5761
5762 static void
5763 maybe_warn_nodiscard (location_t loc, tree expr)
5764 {
5765 if (VOID_TYPE_P (TREE_TYPE (expr)))
5766 return;
5767 while (TREE_CODE (expr) == COMPOUND_EXPR)
5768 {
5769 expr = TREE_OPERAND (expr, 1);
5770 if (EXPR_HAS_LOCATION (expr))
5771 loc = EXPR_LOCATION (expr);
5772 }
5773 if (TREE_CODE (expr) != CALL_EXPR)
5774 return;
5775 tree fn = CALL_EXPR_FN (expr);
5776 if (!fn)
5777 return;
5778 tree attr;
5779 if (TREE_CODE (fn) == ADDR_EXPR
5780 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5781 && (attr = lookup_attribute ("nodiscard",
5782 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5783 {
5784 fn = TREE_OPERAND (fn, 0);
5785 tree args = TREE_VALUE (attr);
5786 if (args)
5787 args = TREE_VALUE (args);
5788 auto_diagnostic_group d;
5789 int warned;
5790 if (args)
5791 warned = warning_at (loc, OPT_Wunused_result,
5792 "ignoring return value of %qD, declared with "
5793 "attribute %<nodiscard%>: %E", fn, args);
5794 else
5795 warned = warning_at (loc, OPT_Wunused_result,
5796 "ignoring return value of %qD, declared with "
5797 "attribute %<nodiscard%>", fn);
5798 if (warned)
5799 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5800 }
5801 else
5802 {
5803 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5804 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5805 if (!attr)
5806 return;
5807 tree args = TREE_VALUE (attr);
5808 if (args)
5809 args = TREE_VALUE (args);
5810 auto_diagnostic_group d;
5811 int warned;
5812 if (args)
5813 warned = warning_at (loc, OPT_Wunused_result,
5814 "ignoring return value of type %qT, declared "
5815 "with attribute %<nodiscard%>: %E",
5816 rettype, args);
5817 else
5818 warned = warning_at (loc, OPT_Wunused_result,
5819 "ignoring return value of type %qT, declared "
5820 "with attribute %<nodiscard%>", rettype);
5821 if (warned)
5822 {
5823 if (TREE_CODE (fn) == ADDR_EXPR)
5824 {
5825 fn = TREE_OPERAND (fn, 0);
5826 if (TREE_CODE (fn) == FUNCTION_DECL)
5827 inform (DECL_SOURCE_LOCATION (fn),
5828 "in call to %qD, declared here", fn);
5829 }
5830 }
5831 }
5832 }
5833
5834 /* Return a compound expression that performs two expressions and
5835 returns the value of the second of them.
5836
5837 LOC is the location of the COMPOUND_EXPR. */
5838
5839 tree
5840 build_compound_expr (location_t loc, tree expr1, tree expr2)
5841 {
5842 bool expr1_int_operands, expr2_int_operands;
5843 tree eptype = NULL_TREE;
5844 tree ret;
5845
5846 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5847 if (expr1_int_operands)
5848 expr1 = remove_c_maybe_const_expr (expr1);
5849 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5850 if (expr2_int_operands)
5851 expr2 = remove_c_maybe_const_expr (expr2);
5852
5853 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5854 expr1 = TREE_OPERAND (expr1, 0);
5855 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5856 {
5857 eptype = TREE_TYPE (expr2);
5858 expr2 = TREE_OPERAND (expr2, 0);
5859 }
5860
5861 if (!TREE_SIDE_EFFECTS (expr1))
5862 {
5863 /* The left-hand operand of a comma expression is like an expression
5864 statement: with -Wunused, we should warn if it doesn't have
5865 any side-effects, unless it was explicitly cast to (void). */
5866 if (warn_unused_value)
5867 {
5868 if (VOID_TYPE_P (TREE_TYPE (expr1))
5869 && CONVERT_EXPR_P (expr1))
5870 ; /* (void) a, b */
5871 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5872 && TREE_CODE (expr1) == COMPOUND_EXPR
5873 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5874 ; /* (void) a, (void) b, c */
5875 else
5876 warning_at (loc, OPT_Wunused_value,
5877 "left-hand operand of comma expression has no effect");
5878 }
5879 }
5880 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5881 && warn_unused_value)
5882 {
5883 tree r = expr1;
5884 location_t cloc = loc;
5885 while (TREE_CODE (r) == COMPOUND_EXPR)
5886 {
5887 if (EXPR_HAS_LOCATION (r))
5888 cloc = EXPR_LOCATION (r);
5889 r = TREE_OPERAND (r, 1);
5890 }
5891 if (!TREE_SIDE_EFFECTS (r)
5892 && !VOID_TYPE_P (TREE_TYPE (r))
5893 && !CONVERT_EXPR_P (r))
5894 warning_at (cloc, OPT_Wunused_value,
5895 "right-hand operand of comma expression has no effect");
5896 }
5897
5898 /* With -Wunused, we should also warn if the left-hand operand does have
5899 side-effects, but computes a value which is not used. For example, in
5900 `foo() + bar(), baz()' the result of the `+' operator is not used,
5901 so we should issue a warning. */
5902 else if (warn_unused_value)
5903 warn_if_unused_value (expr1, loc);
5904
5905 maybe_warn_nodiscard (loc, expr1);
5906
5907 if (expr2 == error_mark_node)
5908 return error_mark_node;
5909
5910 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5911
5912 if (flag_isoc99
5913 && expr1_int_operands
5914 && expr2_int_operands)
5915 ret = note_integer_operands (ret);
5916
5917 if (eptype)
5918 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5919
5920 protected_set_expr_location (ret, loc);
5921 return ret;
5922 }
5923
5924 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5925 which we are casting. OTYPE is the type of the expression being
5926 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5927 of the cast. -Wcast-qual appeared on the command line. Named
5928 address space qualifiers are not handled here, because they result
5929 in different warnings. */
5930
5931 static void
5932 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5933 {
5934 tree in_type = type;
5935 tree in_otype = otype;
5936 int added = 0;
5937 int discarded = 0;
5938 bool is_const;
5939
5940 /* Check that the qualifiers on IN_TYPE are a superset of the
5941 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5942 nodes is uninteresting and we stop as soon as we hit a
5943 non-POINTER_TYPE node on either type. */
5944 do
5945 {
5946 in_otype = TREE_TYPE (in_otype);
5947 in_type = TREE_TYPE (in_type);
5948
5949 /* GNU C allows cv-qualified function types. 'const' means the
5950 function is very pure, 'volatile' means it can't return. We
5951 need to warn when such qualifiers are added, not when they're
5952 taken away. */
5953 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5954 && TREE_CODE (in_type) == FUNCTION_TYPE)
5955 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5956 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5957 else
5958 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5959 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5960 }
5961 while (TREE_CODE (in_type) == POINTER_TYPE
5962 && TREE_CODE (in_otype) == POINTER_TYPE);
5963
5964 if (added)
5965 warning_at (loc, OPT_Wcast_qual,
5966 "cast adds %q#v qualifier to function type", added);
5967
5968 if (discarded)
5969 /* There are qualifiers present in IN_OTYPE that are not present
5970 in IN_TYPE. */
5971 warning_at (loc, OPT_Wcast_qual,
5972 "cast discards %qv qualifier from pointer target type",
5973 discarded);
5974
5975 if (added || discarded)
5976 return;
5977
5978 /* A cast from **T to const **T is unsafe, because it can cause a
5979 const value to be changed with no additional warning. We only
5980 issue this warning if T is the same on both sides, and we only
5981 issue the warning if there are the same number of pointers on
5982 both sides, as otherwise the cast is clearly unsafe anyhow. A
5983 cast is unsafe when a qualifier is added at one level and const
5984 is not present at all outer levels.
5985
5986 To issue this warning, we check at each level whether the cast
5987 adds new qualifiers not already seen. We don't need to special
5988 case function types, as they won't have the same
5989 TYPE_MAIN_VARIANT. */
5990
5991 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5992 return;
5993 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5994 return;
5995
5996 in_type = type;
5997 in_otype = otype;
5998 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5999 do
6000 {
6001 in_type = TREE_TYPE (in_type);
6002 in_otype = TREE_TYPE (in_otype);
6003 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
6004 && !is_const)
6005 {
6006 warning_at (loc, OPT_Wcast_qual,
6007 "to be safe all intermediate pointers in cast from "
6008 "%qT to %qT must be %<const%> qualified",
6009 otype, type);
6010 break;
6011 }
6012 if (is_const)
6013 is_const = TYPE_READONLY (in_type);
6014 }
6015 while (TREE_CODE (in_type) == POINTER_TYPE);
6016 }
6017
6018 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
6019
6020 static bool
6021 c_safe_arg_type_equiv_p (tree t1, tree t2)
6022 {
6023 if (error_operand_p (t1) || error_operand_p (t2))
6024 return true;
6025
6026 t1 = TYPE_MAIN_VARIANT (t1);
6027 t2 = TYPE_MAIN_VARIANT (t2);
6028
6029 if (TREE_CODE (t1) == POINTER_TYPE
6030 && TREE_CODE (t2) == POINTER_TYPE)
6031 return true;
6032
6033 /* The signedness of the parameter matters only when an integral
6034 type smaller than int is promoted to int, otherwise only the
6035 precision of the parameter matters.
6036 This check should make sure that the callee does not see
6037 undefined values in argument registers. */
6038 if (INTEGRAL_TYPE_P (t1)
6039 && INTEGRAL_TYPE_P (t2)
6040 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
6041 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
6042 || !targetm.calls.promote_prototypes (NULL_TREE)
6043 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
6044 return true;
6045
6046 return comptypes (t1, t2);
6047 }
6048
6049 /* Check if a type cast between two function types can be considered safe. */
6050
6051 static bool
6052 c_safe_function_type_cast_p (tree t1, tree t2)
6053 {
6054 if (TREE_TYPE (t1) == void_type_node &&
6055 TYPE_ARG_TYPES (t1) == void_list_node)
6056 return true;
6057
6058 if (TREE_TYPE (t2) == void_type_node &&
6059 TYPE_ARG_TYPES (t2) == void_list_node)
6060 return true;
6061
6062 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
6063 return false;
6064
6065 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
6066 t1 && t2;
6067 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6068 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
6069 return false;
6070
6071 return true;
6072 }
6073
6074 /* Build an expression representing a cast to type TYPE of expression EXPR.
6075 LOC is the location of the cast-- typically the open paren of the cast. */
6076
6077 tree
6078 build_c_cast (location_t loc, tree type, tree expr)
6079 {
6080 tree value;
6081
6082 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
6083
6084 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
6085 expr = TREE_OPERAND (expr, 0);
6086
6087 value = expr;
6088 if (int_operands)
6089 value = remove_c_maybe_const_expr (value);
6090
6091 if (type == error_mark_node || expr == error_mark_node)
6092 return error_mark_node;
6093
6094 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
6095 only in <protocol> qualifications. But when constructing cast expressions,
6096 the protocols do matter and must be kept around. */
6097 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
6098 return build1 (NOP_EXPR, type, expr);
6099
6100 type = TYPE_MAIN_VARIANT (type);
6101
6102 if (TREE_CODE (type) == ARRAY_TYPE)
6103 {
6104 error_at (loc, "cast specifies array type");
6105 return error_mark_node;
6106 }
6107
6108 if (TREE_CODE (type) == FUNCTION_TYPE)
6109 {
6110 error_at (loc, "cast specifies function type");
6111 return error_mark_node;
6112 }
6113
6114 if (!VOID_TYPE_P (type))
6115 {
6116 value = require_complete_type (loc, value);
6117 if (value == error_mark_node)
6118 return error_mark_node;
6119 }
6120
6121 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
6122 {
6123 if (RECORD_OR_UNION_TYPE_P (type)
6124 && pedwarn (loc, OPT_Wpedantic,
6125 "ISO C forbids casting nonscalar to the same type"))
6126 ;
6127 else if (warn_useless_cast)
6128 warning_at (loc, OPT_Wuseless_cast,
6129 "useless cast to type %qT", type);
6130
6131 /* Convert to remove any qualifiers from VALUE's type. */
6132 value = convert (type, value);
6133 }
6134 else if (TREE_CODE (type) == UNION_TYPE)
6135 {
6136 tree field;
6137
6138 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6139 if (TREE_TYPE (field) != error_mark_node
6140 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
6141 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
6142 break;
6143
6144 if (field)
6145 {
6146 tree t;
6147 bool maybe_const = true;
6148
6149 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
6150 t = c_fully_fold (value, false, &maybe_const);
6151 t = build_constructor_single (type, field, t);
6152 if (!maybe_const)
6153 t = c_wrap_maybe_const (t, true);
6154 t = digest_init (loc, type, t,
6155 NULL_TREE, false, false, false, true, false, false);
6156 TREE_CONSTANT (t) = TREE_CONSTANT (value);
6157 return t;
6158 }
6159 error_at (loc, "cast to union type from type not present in union");
6160 return error_mark_node;
6161 }
6162 else
6163 {
6164 tree otype, ovalue;
6165
6166 if (type == void_type_node)
6167 {
6168 tree t = build1 (CONVERT_EXPR, type, value);
6169 SET_EXPR_LOCATION (t, loc);
6170 return t;
6171 }
6172
6173 otype = TREE_TYPE (value);
6174
6175 /* Optionally warn about potentially worrisome casts. */
6176 if (warn_cast_qual
6177 && TREE_CODE (type) == POINTER_TYPE
6178 && TREE_CODE (otype) == POINTER_TYPE)
6179 handle_warn_cast_qual (loc, type, otype);
6180
6181 /* Warn about conversions between pointers to disjoint
6182 address spaces. */
6183 if (TREE_CODE (type) == POINTER_TYPE
6184 && TREE_CODE (otype) == POINTER_TYPE
6185 && !null_pointer_constant_p (value))
6186 {
6187 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6188 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6189 addr_space_t as_common;
6190
6191 if (!addr_space_superset (as_to, as_from, &as_common))
6192 {
6193 if (ADDR_SPACE_GENERIC_P (as_from))
6194 warning_at (loc, 0, "cast to %qs address space pointer "
6195 "from disjoint generic address space pointer",
6196 c_addr_space_name (as_to));
6197
6198 else if (ADDR_SPACE_GENERIC_P (as_to))
6199 warning_at (loc, 0, "cast to generic address space pointer "
6200 "from disjoint %qs address space pointer",
6201 c_addr_space_name (as_from));
6202
6203 else
6204 warning_at (loc, 0, "cast to %qs address space pointer "
6205 "from disjoint %qs address space pointer",
6206 c_addr_space_name (as_to),
6207 c_addr_space_name (as_from));
6208 }
6209
6210 /* Warn of new allocations that are not big enough for the target
6211 type. */
6212 if (warn_alloc_size && TREE_CODE (value) == CALL_EXPR)
6213 if (tree fndecl = get_callee_fndecl (value))
6214 if (DECL_IS_MALLOC (fndecl))
6215 {
6216 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
6217 tree alloc_size = lookup_attribute ("alloc_size", attrs);
6218 if (alloc_size)
6219 warn_for_alloc_size (loc, TREE_TYPE (type), value,
6220 alloc_size);
6221 }
6222 }
6223
6224 /* Warn about possible alignment problems. */
6225 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6226 && TREE_CODE (type) == POINTER_TYPE
6227 && TREE_CODE (otype) == POINTER_TYPE
6228 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6229 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6230 /* Don't warn about opaque types, where the actual alignment
6231 restriction is unknown. */
6232 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6233 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6234 && min_align_of_type (TREE_TYPE (type))
6235 > min_align_of_type (TREE_TYPE (otype)))
6236 warning_at (loc, OPT_Wcast_align,
6237 "cast increases required alignment of target type");
6238
6239 if ((TREE_CODE (type) == INTEGER_TYPE
6240 || TREE_CODE (type) == BITINT_TYPE)
6241 && TREE_CODE (otype) == POINTER_TYPE
6242 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6243 /* Unlike conversion of integers to pointers, where the
6244 warning is disabled for converting constants because
6245 of cases such as SIG_*, warn about converting constant
6246 pointers to integers. In some cases it may cause unwanted
6247 sign extension, and a warning is appropriate. */
6248 warning_at (loc, OPT_Wpointer_to_int_cast,
6249 "cast from pointer to integer of different size");
6250
6251 if (TREE_CODE (value) == CALL_EXPR
6252 && TREE_CODE (type) != TREE_CODE (otype))
6253 warning_at (loc, OPT_Wbad_function_cast,
6254 "cast from function call of type %qT "
6255 "to non-matching type %qT", otype, type);
6256
6257 if (TREE_CODE (type) == POINTER_TYPE
6258 && (TREE_CODE (otype) == INTEGER_TYPE
6259 || TREE_CODE (otype) == BITINT_TYPE)
6260 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6261 /* Don't warn about converting any constant. */
6262 && !TREE_CONSTANT (value))
6263 warning_at (loc,
6264 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6265 "of different size");
6266
6267 if (warn_strict_aliasing <= 2)
6268 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6269
6270 /* If pedantic, warn for conversions between function and object
6271 pointer types, except for converting a null pointer constant
6272 to function pointer type. */
6273 if (pedantic
6274 && TREE_CODE (type) == POINTER_TYPE
6275 && TREE_CODE (otype) == POINTER_TYPE
6276 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6277 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6278 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6279 "conversion of function pointer to object pointer type");
6280
6281 if (pedantic
6282 && TREE_CODE (type) == POINTER_TYPE
6283 && TREE_CODE (otype) == POINTER_TYPE
6284 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6285 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6286 && !null_pointer_constant_p (value))
6287 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6288 "conversion of object pointer to function pointer type");
6289
6290 if (TREE_CODE (type) == POINTER_TYPE
6291 && TREE_CODE (otype) == POINTER_TYPE
6292 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6293 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6294 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6295 TREE_TYPE (otype)))
6296 warning_at (loc, OPT_Wcast_function_type,
6297 "cast between incompatible function types"
6298 " from %qT to %qT", otype, type);
6299
6300 ovalue = value;
6301 value = convert (type, value);
6302
6303 /* Ignore any integer overflow caused by the cast. */
6304 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6305 {
6306 if (TREE_OVERFLOW_P (ovalue))
6307 {
6308 if (!TREE_OVERFLOW (value))
6309 {
6310 /* Avoid clobbering a shared constant. */
6311 value = copy_node (value);
6312 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6313 }
6314 }
6315 else if (TREE_OVERFLOW (value))
6316 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6317 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6318 }
6319 }
6320
6321 /* Don't let a cast be an lvalue. */
6322 if (lvalue_p (value))
6323 value = non_lvalue_loc (loc, value);
6324
6325 /* Don't allow the results of casting to floating-point or complex
6326 types be confused with actual constants, or casts involving
6327 integer and pointer types other than direct integer-to-integer
6328 and integer-to-pointer be confused with integer constant
6329 expressions and null pointer constants. */
6330 if (TREE_CODE (value) == REAL_CST
6331 || TREE_CODE (value) == COMPLEX_CST
6332 || (TREE_CODE (value) == INTEGER_CST
6333 && !((TREE_CODE (expr) == INTEGER_CST
6334 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6335 || TREE_CODE (expr) == REAL_CST
6336 || TREE_CODE (expr) == COMPLEX_CST)))
6337 value = build1 (NOP_EXPR, type, value);
6338
6339 /* If the expression has integer operands and so can occur in an
6340 unevaluated part of an integer constant expression, ensure the
6341 return value reflects this. */
6342 if (int_operands
6343 && INTEGRAL_TYPE_P (type)
6344 && value != error_mark_node
6345 && !EXPR_INT_CONST_OPERANDS (value))
6346 value = note_integer_operands (value);
6347
6348 protected_set_expr_location (value, loc);
6349 return value;
6350 }
6351
6352 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6353 location of the open paren of the cast, or the position of the cast
6354 expr. */
6355 tree
6356 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6357 {
6358 tree type;
6359 tree type_expr = NULL_TREE;
6360 bool type_expr_const = true;
6361 tree ret;
6362 int saved_wsp = warn_strict_prototypes;
6363
6364 /* This avoids warnings about unprototyped casts on
6365 integers. E.g. "#define SIG_DFL (void(*)())0". */
6366 if (TREE_CODE (expr) == INTEGER_CST)
6367 warn_strict_prototypes = 0;
6368 type = groktypename (type_name, &type_expr, &type_expr_const);
6369 warn_strict_prototypes = saved_wsp;
6370
6371 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6372 && reject_gcc_builtin (expr))
6373 return error_mark_node;
6374
6375 ret = build_c_cast (loc, type, expr);
6376 if (type_expr)
6377 {
6378 bool inner_expr_const = true;
6379 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6380 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6381 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6382 && inner_expr_const);
6383 SET_EXPR_LOCATION (ret, loc);
6384 }
6385
6386 if (!EXPR_HAS_LOCATION (ret))
6387 protected_set_expr_location (ret, loc);
6388
6389 /* C++ does not permits types to be defined in a cast, but it
6390 allows references to incomplete types. */
6391 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6392 warning_at (loc, OPT_Wc___compat,
6393 "defining a type in a cast is invalid in C++");
6394
6395 return ret;
6396 }
6397 \f
6398 /* Build an assignment expression of lvalue LHS from value RHS.
6399 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6400 may differ from TREE_TYPE (LHS) for an enum bitfield.
6401 MODIFYCODE is the code for a binary operator that we use
6402 to combine the old value of LHS with RHS to get the new value.
6403 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6404 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6405 which may differ from TREE_TYPE (RHS) for an enum value.
6406
6407 LOCATION is the location of the MODIFYCODE operator.
6408 RHS_LOC is the location of the RHS. */
6409
6410 tree
6411 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6412 enum tree_code modifycode,
6413 location_t rhs_loc, tree rhs, tree rhs_origtype)
6414 {
6415 tree result;
6416 tree newrhs;
6417 tree rhseval = NULL_TREE;
6418 tree lhstype = TREE_TYPE (lhs);
6419 tree olhstype = lhstype;
6420 bool npc;
6421 bool is_atomic_op;
6422
6423 /* Types that aren't fully specified cannot be used in assignments. */
6424 lhs = require_complete_type (location, lhs);
6425
6426 /* Avoid duplicate error messages from operands that had errors. */
6427 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6428 return error_mark_node;
6429
6430 /* Ensure an error for assigning a non-lvalue array to an array in
6431 C90. */
6432 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6433 {
6434 error_at (location, "assignment to expression with array type");
6435 return error_mark_node;
6436 }
6437
6438 /* For ObjC properties, defer this check. */
6439 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6440 return error_mark_node;
6441
6442 is_atomic_op = really_atomic_lvalue (lhs);
6443
6444 newrhs = rhs;
6445
6446 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6447 {
6448 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6449 lhs_origtype, modifycode, rhs_loc, rhs,
6450 rhs_origtype);
6451 if (inner == error_mark_node)
6452 return error_mark_node;
6453 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6454 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6455 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6456 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6457 protected_set_expr_location (result, location);
6458 return result;
6459 }
6460
6461 /* If a binary op has been requested, combine the old LHS value with the RHS
6462 producing the value we should actually store into the LHS. */
6463
6464 if (modifycode != NOP_EXPR)
6465 {
6466 lhs = c_fully_fold (lhs, false, NULL, true);
6467 lhs = stabilize_reference (lhs);
6468
6469 /* Construct the RHS for any non-atomic compound assignemnt. */
6470 if (!is_atomic_op)
6471 {
6472 /* If in LHS op= RHS the RHS has side-effects, ensure they
6473 are preevaluated before the rest of the assignment expression's
6474 side-effects, because RHS could contain e.g. function calls
6475 that modify LHS. */
6476 if (TREE_SIDE_EFFECTS (rhs))
6477 {
6478 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6479 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6480 else
6481 newrhs = save_expr (rhs);
6482 rhseval = newrhs;
6483 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6484 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6485 newrhs);
6486 }
6487 newrhs = build_binary_op (location,
6488 modifycode, lhs, newrhs, true);
6489
6490 /* The original type of the right hand side is no longer
6491 meaningful. */
6492 rhs_origtype = NULL_TREE;
6493 }
6494 }
6495
6496 if (c_dialect_objc ())
6497 {
6498 /* Check if we are modifying an Objective-C property reference;
6499 if so, we need to generate setter calls. */
6500 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6501 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6502 else
6503 result = objc_maybe_build_modify_expr (lhs, newrhs);
6504 if (result)
6505 goto return_result;
6506
6507 /* Else, do the check that we postponed for Objective-C. */
6508 if (!lvalue_or_else (location, lhs, lv_assign))
6509 return error_mark_node;
6510 }
6511
6512 /* Give an error for storing in something that is 'const'. */
6513
6514 if (TYPE_READONLY (lhstype)
6515 || (RECORD_OR_UNION_TYPE_P (lhstype)
6516 && C_TYPE_FIELDS_READONLY (lhstype)))
6517 {
6518 readonly_error (location, lhs, lv_assign);
6519 return error_mark_node;
6520 }
6521 else if (TREE_READONLY (lhs))
6522 readonly_warning (lhs, lv_assign);
6523
6524 /* If storing into a structure or union member,
6525 it has probably been given type `int'.
6526 Compute the type that would go with
6527 the actual amount of storage the member occupies. */
6528
6529 if (TREE_CODE (lhs) == COMPONENT_REF
6530 && (TREE_CODE (lhstype) == INTEGER_TYPE
6531 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6532 || SCALAR_FLOAT_TYPE_P (lhstype)
6533 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6534 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6535
6536 /* If storing in a field that is in actuality a short or narrower than one,
6537 we must store in the field in its actual type. */
6538
6539 if (lhstype != TREE_TYPE (lhs))
6540 {
6541 lhs = copy_node (lhs);
6542 TREE_TYPE (lhs) = lhstype;
6543 }
6544
6545 /* Issue -Wc++-compat warnings about an assignment to an enum type
6546 when LHS does not have its original type. This happens for,
6547 e.g., an enum bitfield in a struct. */
6548 if (warn_cxx_compat
6549 && lhs_origtype != NULL_TREE
6550 && lhs_origtype != lhstype
6551 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6552 {
6553 tree checktype = (rhs_origtype != NULL_TREE
6554 ? rhs_origtype
6555 : TREE_TYPE (rhs));
6556 if (checktype != error_mark_node
6557 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6558 || (is_atomic_op && modifycode != NOP_EXPR)))
6559 warning_at (location, OPT_Wc___compat,
6560 "enum conversion in assignment is invalid in C++");
6561 }
6562
6563 /* Remove qualifiers. */
6564 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6565 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6566
6567 /* Convert new value to destination type. Fold it first, then
6568 restore any excess precision information, for the sake of
6569 conversion warnings. */
6570
6571 if (!(is_atomic_op && modifycode != NOP_EXPR))
6572 {
6573 tree rhs_semantic_type = NULL_TREE;
6574 if (!c_in_omp_for)
6575 {
6576 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6577 {
6578 rhs_semantic_type = TREE_TYPE (newrhs);
6579 newrhs = TREE_OPERAND (newrhs, 0);
6580 }
6581 npc = null_pointer_constant_p (newrhs);
6582 newrhs = c_fully_fold (newrhs, false, NULL);
6583 if (rhs_semantic_type)
6584 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6585 }
6586 else
6587 npc = null_pointer_constant_p (newrhs);
6588 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6589 rhs_origtype, ic_assign, npc,
6590 NULL_TREE, NULL_TREE, 0);
6591 if (TREE_CODE (newrhs) == ERROR_MARK)
6592 return error_mark_node;
6593 }
6594
6595 /* Emit ObjC write barrier, if necessary. */
6596 if (c_dialect_objc () && flag_objc_gc)
6597 {
6598 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6599 if (result)
6600 {
6601 protected_set_expr_location (result, location);
6602 goto return_result;
6603 }
6604 }
6605
6606 /* Scan operands. */
6607
6608 if (is_atomic_op)
6609 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6610 else
6611 {
6612 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6613 TREE_SIDE_EFFECTS (result) = 1;
6614 protected_set_expr_location (result, location);
6615 }
6616
6617 /* If we got the LHS in a different type for storing in,
6618 convert the result back to the nominal type of LHS
6619 so that the value we return always has the same type
6620 as the LHS argument. */
6621
6622 if (olhstype == TREE_TYPE (result))
6623 goto return_result;
6624
6625 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6626 rhs_origtype, ic_assign, false, NULL_TREE,
6627 NULL_TREE, 0);
6628 protected_set_expr_location (result, location);
6629
6630 return_result:
6631 if (rhseval)
6632 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6633 return result;
6634 }
6635 \f
6636 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6637 This is used to implement -fplan9-extensions. */
6638
6639 static bool
6640 find_anonymous_field_with_type (tree struct_type, tree type)
6641 {
6642 tree field;
6643 bool found;
6644
6645 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6646 found = false;
6647 for (field = TYPE_FIELDS (struct_type);
6648 field != NULL_TREE;
6649 field = TREE_CHAIN (field))
6650 {
6651 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6652 ? c_build_qualified_type (TREE_TYPE (field),
6653 TYPE_QUAL_ATOMIC)
6654 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6655 if (DECL_NAME (field) == NULL
6656 && comptypes (type, fieldtype))
6657 {
6658 if (found)
6659 return false;
6660 found = true;
6661 }
6662 else if (DECL_NAME (field) == NULL
6663 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6664 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6665 {
6666 if (found)
6667 return false;
6668 found = true;
6669 }
6670 }
6671 return found;
6672 }
6673
6674 /* RHS is an expression whose type is pointer to struct. If there is
6675 an anonymous field in RHS with type TYPE, then return a pointer to
6676 that field in RHS. This is used with -fplan9-extensions. This
6677 returns NULL if no conversion could be found. */
6678
6679 static tree
6680 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6681 {
6682 tree rhs_struct_type, lhs_main_type;
6683 tree field, found_field;
6684 bool found_sub_field;
6685 tree ret;
6686
6687 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6688 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6689 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6690
6691 gcc_assert (POINTER_TYPE_P (type));
6692 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6693 ? c_build_qualified_type (TREE_TYPE (type),
6694 TYPE_QUAL_ATOMIC)
6695 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6696
6697 found_field = NULL_TREE;
6698 found_sub_field = false;
6699 for (field = TYPE_FIELDS (rhs_struct_type);
6700 field != NULL_TREE;
6701 field = TREE_CHAIN (field))
6702 {
6703 if (DECL_NAME (field) != NULL_TREE
6704 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6705 continue;
6706 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6707 ? c_build_qualified_type (TREE_TYPE (field),
6708 TYPE_QUAL_ATOMIC)
6709 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6710 if (comptypes (lhs_main_type, fieldtype))
6711 {
6712 if (found_field != NULL_TREE)
6713 return NULL_TREE;
6714 found_field = field;
6715 }
6716 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6717 lhs_main_type))
6718 {
6719 if (found_field != NULL_TREE)
6720 return NULL_TREE;
6721 found_field = field;
6722 found_sub_field = true;
6723 }
6724 }
6725
6726 if (found_field == NULL_TREE)
6727 return NULL_TREE;
6728
6729 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6730 build_fold_indirect_ref (rhs), found_field,
6731 NULL_TREE);
6732 ret = build_fold_addr_expr_loc (location, ret);
6733
6734 if (found_sub_field)
6735 {
6736 ret = convert_to_anonymous_field (location, type, ret);
6737 gcc_assert (ret != NULL_TREE);
6738 }
6739
6740 return ret;
6741 }
6742
6743 /* Issue an error message for a bad initializer component.
6744 GMSGID identifies the message.
6745 The component name is taken from the spelling stack. */
6746
6747 static void ATTRIBUTE_GCC_DIAG (2,0)
6748 error_init (location_t loc, const char *gmsgid, ...)
6749 {
6750 char *ofwhat;
6751
6752 auto_diagnostic_group d;
6753
6754 /* The gmsgid may be a format string with %< and %>. */
6755 va_list ap;
6756 va_start (ap, gmsgid);
6757 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6758 va_end (ap);
6759
6760 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6761 if (*ofwhat && warned)
6762 inform (loc, "(near initialization for %qs)", ofwhat);
6763 }
6764
6765 /* Used to implement pedwarn_init and permerror_init. */
6766
6767 static void ATTRIBUTE_GCC_DIAG (3,0)
6768 pedwarn_permerror_init (location_t loc, int opt, const char *gmsgid,
6769 va_list *ap, diagnostic_t kind)
6770 {
6771 /* Use the location where a macro was expanded rather than where
6772 it was defined to make sure macros defined in system headers
6773 but used incorrectly elsewhere are diagnosed. */
6774 location_t exploc = expansion_point_location_if_in_system_header (loc);
6775 auto_diagnostic_group d;
6776 bool warned = emit_diagnostic_valist (kind, exploc, opt, gmsgid, ap);
6777 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6778 if (*ofwhat && warned)
6779 inform (exploc, "(near initialization for %qs)", ofwhat);
6780 }
6781
6782 /* Issue a pedantic warning for a bad initializer component. OPT is
6783 the option OPT_* (from options.h) controlling this warning or 0 if
6784 it is unconditionally given. GMSGID identifies the message. The
6785 component name is taken from the spelling stack. */
6786
6787 static void ATTRIBUTE_GCC_DIAG (3,0)
6788 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6789 {
6790 va_list ap;
6791 va_start (ap, gmsgid);
6792 pedwarn_permerror_init (loc, opt, gmsgid, &ap, DK_PEDWARN);
6793 va_end (ap);
6794 }
6795
6796 /* Like pedwarn_init, but issue a permerror. */
6797
6798 static void ATTRIBUTE_GCC_DIAG (3,0)
6799 permerror_init (location_t loc, int opt, const char *gmsgid, ...)
6800 {
6801 va_list ap;
6802 va_start (ap, gmsgid);
6803 pedwarn_permerror_init (loc, opt, gmsgid, &ap, DK_PERMERROR);
6804 va_end (ap);
6805 }
6806
6807 /* Issue a warning for a bad initializer component.
6808
6809 OPT is the OPT_W* value corresponding to the warning option that
6810 controls this warning. GMSGID identifies the message. The
6811 component name is taken from the spelling stack. */
6812
6813 static void
6814 warning_init (location_t loc, int opt, const char *gmsgid)
6815 {
6816 char *ofwhat;
6817 bool warned;
6818
6819 auto_diagnostic_group d;
6820
6821 /* Use the location where a macro was expanded rather than where
6822 it was defined to make sure macros defined in system headers
6823 but used incorrectly elsewhere are diagnosed. */
6824 location_t exploc = expansion_point_location_if_in_system_header (loc);
6825
6826 /* The gmsgid may be a format string with %< and %>. */
6827 warned = warning_at (exploc, opt, gmsgid);
6828 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6829 if (*ofwhat && warned)
6830 inform (exploc, "(near initialization for %qs)", ofwhat);
6831 }
6832 \f
6833 /* If TYPE is an array type and EXPR is a parenthesized string
6834 constant, warn if pedantic that EXPR is being used to initialize an
6835 object of type TYPE. */
6836
6837 void
6838 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6839 {
6840 if (pedantic
6841 && TREE_CODE (type) == ARRAY_TYPE
6842 && TREE_CODE (expr.value) == STRING_CST
6843 && expr.original_code != STRING_CST)
6844 pedwarn_init (loc, OPT_Wpedantic,
6845 "array initialized from parenthesized string constant");
6846 }
6847
6848 /* Attempt to locate the parameter with the given index within FNDECL,
6849 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6850
6851 static location_t
6852 get_fndecl_argument_location (tree fndecl, int argnum)
6853 {
6854 int i;
6855 tree param;
6856
6857 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6858 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6859 i < argnum && param;
6860 i++, param = TREE_CHAIN (param))
6861 ;
6862
6863 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6864 return DECL_SOURCE_LOCATION (FNDECL). */
6865 if (param == NULL)
6866 return DECL_SOURCE_LOCATION (fndecl);
6867
6868 return DECL_SOURCE_LOCATION (param);
6869 }
6870
6871 /* Issue a note about a mismatching argument for parameter PARMNUM
6872 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6873 Attempt to issue the note at the pertinent parameter of the decl;
6874 failing that issue it at the location of FUNDECL; failing that
6875 issue it at PLOC. */
6876
6877 static void
6878 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6879 tree expected_type, tree actual_type)
6880 {
6881 location_t loc;
6882 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6883 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6884 else
6885 loc = ploc;
6886
6887 inform (loc,
6888 "expected %qT but argument is of type %qT",
6889 expected_type, actual_type);
6890 }
6891
6892 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6893 function FUNDECL declared without prototype to parameter PARMNUM of
6894 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6895
6896 static void
6897 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6898 tree parmtype, tree argtype)
6899 {
6900 tree_code parmcode = TREE_CODE (parmtype);
6901 tree_code argcode = TREE_CODE (argtype);
6902 tree promoted = c_type_promotes_to (argtype);
6903
6904 /* Avoid warning for enum arguments that promote to an integer type
6905 of the same size/mode. */
6906 if (parmcode == INTEGER_TYPE
6907 && argcode == ENUMERAL_TYPE
6908 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6909 return;
6910
6911 if ((parmcode == argcode
6912 || (parmcode == INTEGER_TYPE
6913 && argcode == ENUMERAL_TYPE))
6914 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6915 return;
6916
6917 /* This diagnoses even signed/unsigned mismatches. Those might be
6918 safe in many cases but GCC may emit suboptimal code for them so
6919 warning on those cases drives efficiency improvements. */
6920 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6921 TYPE_MAIN_VARIANT (promoted) == argtype
6922 ? G_("%qD argument %d type is %qT where %qT is expected "
6923 "in a call to built-in function declared without "
6924 "prototype")
6925 : G_("%qD argument %d promotes to %qT where %qT is expected "
6926 "in a call to built-in function declared without "
6927 "prototype"),
6928 fundecl, parmnum, promoted, parmtype))
6929 inform (DECL_SOURCE_LOCATION (fundecl),
6930 "built-in %qD declared here",
6931 fundecl);
6932 }
6933
6934 /* Convert value RHS to type TYPE as preparation for an assignment to
6935 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6936 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6937 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6938 constant before any folding.
6939 The real work of conversion is done by `convert'.
6940 The purpose of this function is to generate error messages
6941 for assignments that are not allowed in C.
6942 ERRTYPE says whether it is argument passing, assignment,
6943 initialization or return.
6944
6945 In the following example, '~' denotes where EXPR_LOC and '^' where
6946 LOCATION point to:
6947
6948 f (var); [ic_argpass]
6949 ^ ~~~
6950 x = var; [ic_assign]
6951 ^ ~~~;
6952 int x = var; [ic_init]
6953 ^^^
6954 return x; [ic_return]
6955 ^
6956
6957 FUNCTION is a tree for the function being called.
6958 PARMNUM is the number of the argument, for printing in error messages.
6959 WARNOPT may be set to a warning option to issue the corresponding warning
6960 rather than an error for invalid conversions. Used for calls to built-in
6961 functions declared without a prototype. */
6962
6963 static tree
6964 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6965 tree rhs, tree origtype, enum impl_conv errtype,
6966 bool null_pointer_constant, tree fundecl,
6967 tree function, int parmnum, int warnopt /* = 0 */)
6968 {
6969 enum tree_code codel = TREE_CODE (type);
6970 tree orig_rhs = rhs;
6971 tree rhstype;
6972 enum tree_code coder;
6973 tree rname = NULL_TREE;
6974 bool objc_ok = false;
6975
6976 /* Use the expansion point location to handle cases such as user's
6977 function returning a wrong-type macro defined in a system header. */
6978 location = expansion_point_location_if_in_system_header (location);
6979
6980 if (errtype == ic_argpass)
6981 {
6982 tree selector;
6983 /* Change pointer to function to the function itself for
6984 diagnostics. */
6985 if (TREE_CODE (function) == ADDR_EXPR
6986 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6987 function = TREE_OPERAND (function, 0);
6988
6989 /* Handle an ObjC selector specially for diagnostics. */
6990 selector = objc_message_selector ();
6991 rname = function;
6992 if (selector && parmnum > 2)
6993 {
6994 rname = selector;
6995 parmnum -= 2;
6996 }
6997 }
6998
6999 /* This macro is used to emit diagnostics to ensure that all format
7000 strings are complete sentences, visible to gettext and checked at
7001 compile time. */
7002 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
7003 do { \
7004 switch (errtype) \
7005 { \
7006 case ic_argpass: \
7007 { \
7008 auto_diagnostic_group d; \
7009 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
7010 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
7011 } \
7012 break; \
7013 case ic_assign: \
7014 pedwarn (LOCATION, OPT, AS); \
7015 break; \
7016 case ic_init: \
7017 case ic_init_const: \
7018 pedwarn_init (LOCATION, OPT, IN); \
7019 break; \
7020 case ic_return: \
7021 pedwarn (LOCATION, OPT, RE); \
7022 break; \
7023 default: \
7024 gcc_unreachable (); \
7025 } \
7026 } while (0)
7027
7028 /* This macro is used to emit diagnostics to ensure that all format
7029 strings are complete sentences, visible to gettext and checked at
7030 compile time. It can be called with 'pedwarn' or 'warning_at'. */
7031 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
7032 do { \
7033 switch (errtype) \
7034 { \
7035 case ic_argpass: \
7036 { \
7037 auto_diagnostic_group d; \
7038 if (PEDWARN) { \
7039 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
7040 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
7041 } else { \
7042 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
7043 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
7044 } \
7045 } \
7046 break; \
7047 case ic_assign: \
7048 if (PEDWARN) \
7049 pedwarn (LOCATION, OPT, AS, QUALS); \
7050 else \
7051 warning_at (LOCATION, OPT, AS, QUALS); \
7052 break; \
7053 case ic_init: \
7054 case ic_init_const: \
7055 if (PEDWARN) \
7056 pedwarn (LOCATION, OPT, IN, QUALS); \
7057 else \
7058 warning_at (LOCATION, OPT, IN, QUALS); \
7059 break; \
7060 case ic_return: \
7061 if (PEDWARN) \
7062 pedwarn (LOCATION, OPT, RE, QUALS); \
7063 else \
7064 warning_at (LOCATION, OPT, RE, QUALS); \
7065 break; \
7066 default: \
7067 gcc_unreachable (); \
7068 } \
7069 } while (0)
7070
7071 /* This macro is used to emit diagnostics to ensure that all format
7072 strings are complete sentences, visible to gettext and checked at
7073 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
7074 extra parameter to enumerate qualifiers. */
7075 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
7076 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
7077
7078
7079 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7080 rhs = TREE_OPERAND (rhs, 0);
7081
7082 rhstype = TREE_TYPE (rhs);
7083 coder = TREE_CODE (rhstype);
7084
7085 if (coder == ERROR_MARK)
7086 return error_mark_node;
7087
7088 if (c_dialect_objc ())
7089 {
7090 int parmno;
7091
7092 switch (errtype)
7093 {
7094 case ic_return:
7095 parmno = 0;
7096 break;
7097
7098 case ic_assign:
7099 parmno = -1;
7100 break;
7101
7102 case ic_init:
7103 case ic_init_const:
7104 parmno = -2;
7105 break;
7106
7107 default:
7108 parmno = parmnum;
7109 break;
7110 }
7111
7112 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
7113 }
7114
7115 if (warn_cxx_compat)
7116 {
7117 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7118 if (checktype != error_mark_node
7119 && TREE_CODE (type) == ENUMERAL_TYPE
7120 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
7121 switch (errtype)
7122 {
7123 case ic_argpass:
7124 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
7125 "passing argument %d of %qE is invalid in C++",
7126 parmnum, rname))
7127 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
7128 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
7129 "expected %qT but argument is of type %qT",
7130 type, rhstype);
7131 break;
7132 case ic_assign:
7133 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7134 "%qT in assignment is invalid in C++", rhstype, type);
7135 break;
7136 case ic_init:
7137 case ic_init_const:
7138 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
7139 "%qT to %qT in initialization is invalid in C++",
7140 rhstype, type);
7141 break;
7142 case ic_return:
7143 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7144 "%qT in return is invalid in C++", rhstype, type);
7145 break;
7146 default:
7147 gcc_unreachable ();
7148 }
7149 }
7150
7151 if (warn_enum_conversion)
7152 {
7153 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7154 if (checktype != error_mark_node
7155 && TREE_CODE (checktype) == ENUMERAL_TYPE
7156 && TREE_CODE (type) == ENUMERAL_TYPE
7157 && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type)))
7158 {
7159 gcc_rich_location loc (location);
7160 warning_at (&loc, OPT_Wenum_conversion,
7161 "implicit conversion from %qT to %qT",
7162 checktype, type);
7163 }
7164 }
7165
7166 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
7167 {
7168 warn_for_address_of_packed_member (type, orig_rhs);
7169 return rhs;
7170 }
7171
7172 if (coder == VOID_TYPE)
7173 {
7174 /* Except for passing an argument to an unprototyped function,
7175 this is a constraint violation. When passing an argument to
7176 an unprototyped function, it is compile-time undefined;
7177 making it a constraint in that case was rejected in
7178 DR#252. */
7179 const char msg[] = "void value not ignored as it ought to be";
7180 if (warnopt)
7181 warning_at (location, warnopt, msg);
7182 else
7183 error_at (location, msg);
7184 return error_mark_node;
7185 }
7186 rhs = require_complete_type (location, rhs);
7187 if (rhs == error_mark_node)
7188 return error_mark_node;
7189
7190 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
7191 return error_mark_node;
7192
7193 /* A non-reference type can convert to a reference. This handles
7194 va_start, va_copy and possibly port built-ins. */
7195 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
7196 {
7197 if (!lvalue_p (rhs))
7198 {
7199 const char msg[] = "cannot pass rvalue to reference parameter";
7200 if (warnopt)
7201 warning_at (location, warnopt, msg);
7202 else
7203 error_at (location, msg);
7204 return error_mark_node;
7205 }
7206 if (!c_mark_addressable (rhs))
7207 return error_mark_node;
7208 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
7209 SET_EXPR_LOCATION (rhs, location);
7210
7211 rhs = convert_for_assignment (location, expr_loc,
7212 build_pointer_type (TREE_TYPE (type)),
7213 rhs, origtype, errtype,
7214 null_pointer_constant, fundecl, function,
7215 parmnum, warnopt);
7216 if (rhs == error_mark_node)
7217 return error_mark_node;
7218
7219 rhs = build1 (NOP_EXPR, type, rhs);
7220 SET_EXPR_LOCATION (rhs, location);
7221 return rhs;
7222 }
7223 /* Some types can interconvert without explicit casts. */
7224 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7225 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7226 return convert (type, rhs);
7227 /* Arithmetic types all interconvert, and enum is treated like int. */
7228 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7229 || codel == FIXED_POINT_TYPE
7230 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7231 || codel == BOOLEAN_TYPE || codel == BITINT_TYPE)
7232 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7233 || coder == FIXED_POINT_TYPE
7234 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7235 || coder == BOOLEAN_TYPE || coder == BITINT_TYPE))
7236 {
7237 if (warnopt && errtype == ic_argpass)
7238 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7239 rhstype);
7240
7241 bool save = in_late_binary_op;
7242 if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
7243 || (coder == REAL_TYPE
7244 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7245 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7246 in_late_binary_op = true;
7247 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7248 ? expr_loc : location, type, orig_rhs,
7249 errtype == ic_init_const);
7250 in_late_binary_op = save;
7251 return ret;
7252 }
7253
7254 /* Aggregates in different TUs might need conversion. */
7255 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7256 && codel == coder
7257 && comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (rhstype)))
7258 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7259 ? expr_loc : location, type, rhs);
7260
7261 /* Conversion to a transparent union or record from its member types.
7262 This applies only to function arguments. */
7263 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7264 && TYPE_TRANSPARENT_AGGR (type))
7265 && errtype == ic_argpass)
7266 {
7267 tree memb, marginal_memb = NULL_TREE;
7268
7269 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7270 {
7271 tree memb_type = TREE_TYPE (memb);
7272
7273 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7274 TYPE_MAIN_VARIANT (rhstype)))
7275 break;
7276
7277 if (TREE_CODE (memb_type) != POINTER_TYPE)
7278 continue;
7279
7280 if (coder == POINTER_TYPE)
7281 {
7282 tree ttl = TREE_TYPE (memb_type);
7283 tree ttr = TREE_TYPE (rhstype);
7284
7285 /* Any non-function converts to a [const][volatile] void *
7286 and vice versa; otherwise, targets must be the same.
7287 Meanwhile, the lhs target must have all the qualifiers of
7288 the rhs. */
7289 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7290 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7291 || comp_target_types (location, memb_type, rhstype))
7292 {
7293 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7294 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7295 /* If this type won't generate any warnings, use it. */
7296 if (lquals == rquals
7297 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7298 && TREE_CODE (ttl) == FUNCTION_TYPE)
7299 ? ((lquals | rquals) == rquals)
7300 : ((lquals | rquals) == lquals)))
7301 break;
7302
7303 /* Keep looking for a better type, but remember this one. */
7304 if (!marginal_memb)
7305 marginal_memb = memb;
7306 }
7307 }
7308
7309 /* Can convert integer zero to any pointer type. */
7310 if (null_pointer_constant)
7311 {
7312 rhs = null_pointer_node;
7313 break;
7314 }
7315 }
7316
7317 if (memb || marginal_memb)
7318 {
7319 if (!memb)
7320 {
7321 /* We have only a marginally acceptable member type;
7322 it needs a warning. */
7323 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7324 tree ttr = TREE_TYPE (rhstype);
7325
7326 /* Const and volatile mean something different for function
7327 types, so the usual warnings are not appropriate. */
7328 if (TREE_CODE (ttr) == FUNCTION_TYPE
7329 && TREE_CODE (ttl) == FUNCTION_TYPE)
7330 {
7331 /* Because const and volatile on functions are
7332 restrictions that say the function will not do
7333 certain things, it is okay to use a const or volatile
7334 function where an ordinary one is wanted, but not
7335 vice-versa. */
7336 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7337 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7338 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7339 OPT_Wdiscarded_qualifiers,
7340 G_("passing argument %d of %qE "
7341 "makes %q#v qualified function "
7342 "pointer from unqualified"),
7343 G_("assignment makes %q#v qualified "
7344 "function pointer from "
7345 "unqualified"),
7346 G_("initialization makes %q#v qualified "
7347 "function pointer from "
7348 "unqualified"),
7349 G_("return makes %q#v qualified function "
7350 "pointer from unqualified"),
7351 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7352 }
7353 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7354 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7355 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7356 OPT_Wdiscarded_qualifiers,
7357 G_("passing argument %d of %qE discards "
7358 "%qv qualifier from pointer target type"),
7359 G_("assignment discards %qv qualifier "
7360 "from pointer target type"),
7361 G_("initialization discards %qv qualifier "
7362 "from pointer target type"),
7363 G_("return discards %qv qualifier from "
7364 "pointer target type"),
7365 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7366
7367 memb = marginal_memb;
7368 }
7369
7370 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7371 pedwarn (location, OPT_Wpedantic,
7372 "ISO C prohibits argument conversion to union type");
7373
7374 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7375 return build_constructor_single (type, memb, rhs);
7376 }
7377 }
7378
7379 /* Conversions among pointers */
7380 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7381 && (coder == codel))
7382 {
7383 /* If RHS refers to a built-in declared without a prototype
7384 BLTIN is the declaration of the built-in with a prototype
7385 and RHSTYPE is set to the actual type of the built-in. */
7386 tree bltin;
7387 rhstype = type_or_builtin_type (rhs, &bltin);
7388
7389 tree ttl = TREE_TYPE (type);
7390 tree ttr = TREE_TYPE (rhstype);
7391 tree mvl = ttl;
7392 tree mvr = ttr;
7393 bool is_opaque_pointer;
7394 bool target_cmp = false; /* Cache comp_target_types () result. */
7395 addr_space_t asl;
7396 addr_space_t asr;
7397
7398 if (TREE_CODE (mvl) != ARRAY_TYPE)
7399 mvl = (TYPE_ATOMIC (mvl)
7400 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7401 TYPE_QUAL_ATOMIC)
7402 : TYPE_MAIN_VARIANT (mvl));
7403 if (TREE_CODE (mvr) != ARRAY_TYPE)
7404 mvr = (TYPE_ATOMIC (mvr)
7405 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7406 TYPE_QUAL_ATOMIC)
7407 : TYPE_MAIN_VARIANT (mvr));
7408 /* Opaque pointers are treated like void pointers. */
7409 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7410
7411 /* The Plan 9 compiler permits a pointer to a struct to be
7412 automatically converted into a pointer to an anonymous field
7413 within the struct. */
7414 if (flag_plan9_extensions
7415 && RECORD_OR_UNION_TYPE_P (mvl)
7416 && RECORD_OR_UNION_TYPE_P (mvr)
7417 && mvl != mvr)
7418 {
7419 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7420 if (new_rhs != NULL_TREE)
7421 {
7422 rhs = new_rhs;
7423 rhstype = TREE_TYPE (rhs);
7424 coder = TREE_CODE (rhstype);
7425 ttr = TREE_TYPE (rhstype);
7426 mvr = TYPE_MAIN_VARIANT (ttr);
7427 }
7428 }
7429
7430 /* C++ does not allow the implicit conversion void* -> T*. However,
7431 for the purpose of reducing the number of false positives, we
7432 tolerate the special case of
7433
7434 int *p = NULL;
7435
7436 where NULL is typically defined in C to be '(void *) 0'. */
7437 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7438 warning_at (errtype == ic_argpass ? expr_loc : location,
7439 OPT_Wc___compat,
7440 "request for implicit conversion "
7441 "from %qT to %qT not permitted in C++", rhstype, type);
7442
7443 /* Warn of new allocations that are not big enough for the target
7444 type. */
7445 if (warn_alloc_size && TREE_CODE (rhs) == CALL_EXPR)
7446 if (tree fndecl = get_callee_fndecl (rhs))
7447 if (DECL_IS_MALLOC (fndecl))
7448 {
7449 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
7450 tree alloc_size = lookup_attribute ("alloc_size", attrs);
7451 if (alloc_size)
7452 warn_for_alloc_size (location, ttl, rhs, alloc_size);
7453 }
7454
7455 /* See if the pointers point to incompatible address spaces. */
7456 asl = TYPE_ADDR_SPACE (ttl);
7457 asr = TYPE_ADDR_SPACE (ttr);
7458 if (!null_pointer_constant_p (rhs)
7459 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7460 {
7461 auto_diagnostic_group d;
7462 bool diagnosed = true;
7463 switch (errtype)
7464 {
7465 case ic_argpass:
7466 {
7467 const char msg[] = G_("passing argument %d of %qE from "
7468 "pointer to non-enclosed address space");
7469 if (warnopt)
7470 diagnosed
7471 = warning_at (expr_loc, warnopt, msg, parmnum, rname);
7472 else
7473 error_at (expr_loc, msg, parmnum, rname);
7474 break;
7475 }
7476 case ic_assign:
7477 {
7478 const char msg[] = G_("assignment from pointer to "
7479 "non-enclosed address space");
7480 if (warnopt)
7481 diagnosed = warning_at (location, warnopt, msg);
7482 else
7483 error_at (location, msg);
7484 break;
7485 }
7486 case ic_init:
7487 case ic_init_const:
7488 {
7489 const char msg[] = G_("initialization from pointer to "
7490 "non-enclosed address space");
7491 if (warnopt)
7492 diagnosed = warning_at (location, warnopt, msg);
7493 else
7494 error_at (location, msg);
7495 break;
7496 }
7497 case ic_return:
7498 {
7499 const char msg[] = G_("return from pointer to "
7500 "non-enclosed address space");
7501 if (warnopt)
7502 diagnosed = warning_at (location, warnopt, msg);
7503 else
7504 error_at (location, msg);
7505 break;
7506 }
7507 default:
7508 gcc_unreachable ();
7509 }
7510 if (diagnosed)
7511 {
7512 if (errtype == ic_argpass)
7513 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7514 else
7515 inform (location, "expected %qT but pointer is of type %qT",
7516 type, rhstype);
7517 }
7518 return error_mark_node;
7519 }
7520
7521 /* Check if the right-hand side has a format attribute but the
7522 left-hand side doesn't. */
7523 if (warn_suggest_attribute_format
7524 && check_missing_format_attribute (type, rhstype))
7525 {
7526 switch (errtype)
7527 {
7528 case ic_argpass:
7529 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7530 "argument %d of %qE might be "
7531 "a candidate for a format attribute",
7532 parmnum, rname);
7533 break;
7534 case ic_assign:
7535 warning_at (location, OPT_Wsuggest_attribute_format,
7536 "assignment left-hand side might be "
7537 "a candidate for a format attribute");
7538 break;
7539 case ic_init:
7540 case ic_init_const:
7541 warning_at (location, OPT_Wsuggest_attribute_format,
7542 "initialization left-hand side might be "
7543 "a candidate for a format attribute");
7544 break;
7545 case ic_return:
7546 warning_at (location, OPT_Wsuggest_attribute_format,
7547 "return type might be "
7548 "a candidate for a format attribute");
7549 break;
7550 default:
7551 gcc_unreachable ();
7552 }
7553 }
7554
7555 /* See if the pointers point to incompatible scalar storage orders. */
7556 if (warn_scalar_storage_order
7557 && !null_pointer_constant_p (rhs)
7558 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7559 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7560 {
7561 tree t;
7562
7563 switch (errtype)
7564 {
7565 case ic_argpass:
7566 /* Do not warn for built-in functions, for example memcpy, since we
7567 control how they behave and they can be useful in this area. */
7568 if (TREE_CODE (rname) != FUNCTION_DECL
7569 || !fndecl_built_in_p (rname))
7570 warning_at (location, OPT_Wscalar_storage_order,
7571 "passing argument %d of %qE from incompatible "
7572 "scalar storage order", parmnum, rname);
7573 break;
7574 case ic_assign:
7575 /* Do not warn if the RHS is a call to a function that returns a
7576 pointer that is not an alias. */
7577 if (TREE_CODE (rhs) != CALL_EXPR
7578 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7579 || !DECL_IS_MALLOC (t))
7580 warning_at (location, OPT_Wscalar_storage_order,
7581 "assignment to %qT from pointer type %qT with "
7582 "incompatible scalar storage order", type, rhstype);
7583 break;
7584 case ic_init:
7585 case ic_init_const:
7586 /* Likewise. */
7587 if (TREE_CODE (rhs) != CALL_EXPR
7588 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7589 || !DECL_IS_MALLOC (t))
7590 warning_at (location, OPT_Wscalar_storage_order,
7591 "initialization of %qT from pointer type %qT with "
7592 "incompatible scalar storage order", type, rhstype);
7593 break;
7594 case ic_return:
7595 warning_at (location, OPT_Wscalar_storage_order,
7596 "returning %qT from pointer type with incompatible "
7597 "scalar storage order %qT", rhstype, type);
7598 break;
7599 default:
7600 gcc_unreachable ();
7601 }
7602 }
7603
7604 /* Any non-function converts to a [const][volatile] void *
7605 and vice versa; otherwise, targets must be the same.
7606 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7607 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7608 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7609 || (target_cmp = comp_target_types (location, type, rhstype))
7610 || is_opaque_pointer
7611 || ((c_common_unsigned_type (mvl)
7612 == c_common_unsigned_type (mvr))
7613 && (c_common_signed_type (mvl)
7614 == c_common_signed_type (mvr))
7615 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7616 {
7617 /* Warn about loss of qualifers from pointers to arrays with
7618 qualifiers on the element type. */
7619 if (TREE_CODE (ttr) == ARRAY_TYPE)
7620 {
7621 ttr = strip_array_types (ttr);
7622 ttl = strip_array_types (ttl);
7623
7624 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7625 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7626 WARNING_FOR_QUALIFIERS (flag_isoc23,
7627 location, expr_loc,
7628 OPT_Wdiscarded_array_qualifiers,
7629 G_("passing argument %d of %qE discards "
7630 "%qv qualifier from pointer target type"),
7631 G_("assignment discards %qv qualifier "
7632 "from pointer target type"),
7633 G_("initialization discards %qv qualifier "
7634 "from pointer target type"),
7635 G_("return discards %qv qualifier from "
7636 "pointer target type"),
7637 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7638 }
7639 else if (pedantic
7640 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7641 ||
7642 (VOID_TYPE_P (ttr)
7643 && !null_pointer_constant
7644 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7645 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7646 G_("ISO C forbids passing argument %d of "
7647 "%qE between function pointer "
7648 "and %<void *%>"),
7649 G_("ISO C forbids assignment between "
7650 "function pointer and %<void *%>"),
7651 G_("ISO C forbids initialization between "
7652 "function pointer and %<void *%>"),
7653 G_("ISO C forbids return between function "
7654 "pointer and %<void *%>"));
7655 /* Const and volatile mean something different for function types,
7656 so the usual warnings are not appropriate. */
7657 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7658 && TREE_CODE (ttl) != FUNCTION_TYPE)
7659 {
7660 /* Assignments between atomic and non-atomic objects are OK. */
7661 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7662 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7663 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7664 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7665
7666 /* Don't warn about loss of qualifier for conversions from
7667 qualified void* to pointers to arrays with corresponding
7668 qualifier on the element type (except for pedantic before C23). */
7669 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc23))
7670 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7671 OPT_Wdiscarded_qualifiers,
7672 G_("passing argument %d of %qE discards "
7673 "%qv qualifier from pointer target type"),
7674 G_("assignment discards %qv qualifier "
7675 "from pointer target type"),
7676 G_("initialization discards %qv qualifier "
7677 "from pointer target type"),
7678 G_("return discards %qv qualifier from "
7679 "pointer target type"),
7680 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7681 else if (warn_quals_ped)
7682 pedwarn_c11 (location, OPT_Wc11_c23_compat,
7683 "array with qualifier on the element is not qualified before C23");
7684
7685 /* If this is not a case of ignoring a mismatch in signedness,
7686 no warning. */
7687 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7688 || target_cmp)
7689 ;
7690 /* If there is a mismatch, do warn. */
7691 else if (warn_pointer_sign)
7692 switch (errtype)
7693 {
7694 case ic_argpass:
7695 {
7696 auto_diagnostic_group d;
7697 range_label_for_type_mismatch rhs_label (rhstype, type);
7698 gcc_rich_location richloc (expr_loc, &rhs_label);
7699 if (pedwarn (&richloc, OPT_Wpointer_sign,
7700 "pointer targets in passing argument %d of "
7701 "%qE differ in signedness", parmnum, rname))
7702 inform_for_arg (fundecl, expr_loc, parmnum, type,
7703 rhstype);
7704 }
7705 break;
7706 case ic_assign:
7707 pedwarn (location, OPT_Wpointer_sign,
7708 "pointer targets in assignment from %qT to %qT "
7709 "differ in signedness", rhstype, type);
7710 break;
7711 case ic_init:
7712 case ic_init_const:
7713 pedwarn_init (location, OPT_Wpointer_sign,
7714 "pointer targets in initialization of %qT "
7715 "from %qT differ in signedness", type,
7716 rhstype);
7717 break;
7718 case ic_return:
7719 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7720 "returning %qT from a function with return type "
7721 "%qT differ in signedness", rhstype, type);
7722 break;
7723 default:
7724 gcc_unreachable ();
7725 }
7726 }
7727 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7728 && TREE_CODE (ttr) == FUNCTION_TYPE)
7729 {
7730 /* Because const and volatile on functions are restrictions
7731 that say the function will not do certain things,
7732 it is okay to use a const or volatile function
7733 where an ordinary one is wanted, but not vice-versa. */
7734 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7735 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7736 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7737 OPT_Wdiscarded_qualifiers,
7738 G_("passing argument %d of %qE makes "
7739 "%q#v qualified function pointer "
7740 "from unqualified"),
7741 G_("assignment makes %q#v qualified function "
7742 "pointer from unqualified"),
7743 G_("initialization makes %q#v qualified "
7744 "function pointer from unqualified"),
7745 G_("return makes %q#v qualified function "
7746 "pointer from unqualified"),
7747 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7748 }
7749 }
7750 /* Avoid warning about the volatile ObjC EH puts on decls. */
7751 else if (!objc_ok)
7752 {
7753 switch (errtype)
7754 {
7755 case ic_argpass:
7756 {
7757 auto_diagnostic_group d;
7758 range_label_for_type_mismatch rhs_label (rhstype, type);
7759 gcc_rich_location richloc (expr_loc, &rhs_label);
7760 if (permerror_opt (&richloc, OPT_Wincompatible_pointer_types,
7761 "passing argument %d of %qE from "
7762 "incompatible pointer type",
7763 parmnum, rname))
7764 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7765 }
7766 break;
7767 case ic_assign:
7768 if (bltin)
7769 permerror_opt (location, OPT_Wincompatible_pointer_types,
7770 "assignment to %qT from pointer to "
7771 "%qD with incompatible type %qT",
7772 type, bltin, rhstype);
7773 else
7774 permerror_opt (location, OPT_Wincompatible_pointer_types,
7775 "assignment to %qT from incompatible pointer "
7776 "type %qT", type, rhstype);
7777 break;
7778 case ic_init:
7779 case ic_init_const:
7780 if (bltin)
7781 permerror_init (location, OPT_Wincompatible_pointer_types,
7782 "initialization of %qT from pointer to "
7783 "%qD with incompatible type %qT",
7784 type, bltin, rhstype);
7785 else
7786 permerror_init (location, OPT_Wincompatible_pointer_types,
7787 "initialization of %qT from incompatible "
7788 "pointer type %qT",
7789 type, rhstype);
7790 break;
7791 case ic_return:
7792 if (bltin)
7793 permerror_opt (location, OPT_Wincompatible_pointer_types,
7794 "returning pointer to %qD of type %qT from "
7795 "a function with incompatible type %qT",
7796 bltin, rhstype, type);
7797 else
7798 permerror_opt (location, OPT_Wincompatible_pointer_types,
7799 "returning %qT from a function with "
7800 "incompatible return type %qT", rhstype, type);
7801 break;
7802 default:
7803 gcc_unreachable ();
7804 }
7805 }
7806
7807 /* If RHS isn't an address, check pointer or array of packed
7808 struct or union. */
7809 warn_for_address_of_packed_member (type, orig_rhs);
7810
7811 return convert (type, rhs);
7812 }
7813 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7814 {
7815 /* ??? This should not be an error when inlining calls to
7816 unprototyped functions. */
7817 const char msg[] = "invalid use of non-lvalue array";
7818 if (warnopt)
7819 warning_at (location, warnopt, msg);
7820 else
7821 error_at (location, msg);
7822 return error_mark_node;
7823 }
7824 else if (codel == POINTER_TYPE
7825 && (coder == INTEGER_TYPE
7826 || coder == NULLPTR_TYPE
7827 || coder == BITINT_TYPE))
7828 {
7829 /* An explicit constant 0 or type nullptr_t can convert to a pointer,
7830 or one that results from arithmetic, even including a cast to
7831 integer type. */
7832 if (!null_pointer_constant && coder != NULLPTR_TYPE)
7833 switch (errtype)
7834 {
7835 case ic_argpass:
7836 {
7837 auto_diagnostic_group d;
7838 range_label_for_type_mismatch rhs_label (rhstype, type);
7839 gcc_rich_location richloc (expr_loc, &rhs_label);
7840 if (permerror_opt (&richloc, OPT_Wint_conversion,
7841 "passing argument %d of %qE makes pointer "
7842 "from integer without a cast", parmnum, rname))
7843 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7844 }
7845 break;
7846 case ic_assign:
7847 permerror_opt (location, OPT_Wint_conversion,
7848 "assignment to %qT from %qT makes pointer from "
7849 "integer without a cast", type, rhstype);
7850 break;
7851 case ic_init:
7852 case ic_init_const:
7853 permerror_init (location, OPT_Wint_conversion,
7854 "initialization of %qT from %qT makes pointer "
7855 "from integer without a cast", type, rhstype);
7856 break;
7857 case ic_return:
7858 permerror_init (location, OPT_Wint_conversion,
7859 "returning %qT from a function with return type "
7860 "%qT makes pointer from integer without a cast",
7861 rhstype, type);
7862 break;
7863 default:
7864 gcc_unreachable ();
7865 }
7866
7867 return convert (type, rhs);
7868 }
7869 else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE)
7870 && coder == POINTER_TYPE)
7871 {
7872 switch (errtype)
7873 {
7874 case ic_argpass:
7875 {
7876 auto_diagnostic_group d;
7877 range_label_for_type_mismatch rhs_label (rhstype, type);
7878 gcc_rich_location richloc (expr_loc, &rhs_label);
7879 if (permerror_opt (&richloc, OPT_Wint_conversion,
7880 "passing argument %d of %qE makes integer from "
7881 "pointer without a cast", parmnum, rname))
7882 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7883 }
7884 break;
7885 case ic_assign:
7886 permerror_opt (location, OPT_Wint_conversion,
7887 "assignment to %qT from %qT makes integer from "
7888 "pointer without a cast", type, rhstype);
7889 break;
7890 case ic_init:
7891 case ic_init_const:
7892 permerror_init (location, OPT_Wint_conversion,
7893 "initialization of %qT from %qT makes integer "
7894 "from pointer without a cast", type, rhstype);
7895 break;
7896 case ic_return:
7897 permerror_opt (location, OPT_Wint_conversion, "returning %qT from a "
7898 "function with return type %qT makes integer from "
7899 "pointer without a cast", rhstype, type);
7900 break;
7901 default:
7902 gcc_unreachable ();
7903 }
7904
7905 return convert (type, rhs);
7906 }
7907 else if (C_BOOLEAN_TYPE_P (type)
7908 /* The type nullptr_t may be converted to bool. The
7909 result is false. */
7910 && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
7911 {
7912 tree ret;
7913 bool save = in_late_binary_op;
7914 in_late_binary_op = true;
7915 ret = convert (type, rhs);
7916 in_late_binary_op = save;
7917 return ret;
7918 }
7919 else if (codel == NULLPTR_TYPE && null_pointer_constant)
7920 return convert (type, rhs);
7921
7922 switch (errtype)
7923 {
7924 case ic_argpass:
7925 {
7926 auto_diagnostic_group d;
7927 range_label_for_type_mismatch rhs_label (rhstype, type);
7928 gcc_rich_location richloc (expr_loc, &rhs_label);
7929 const char msg[] = G_("incompatible type for argument %d of %qE");
7930 if (warnopt)
7931 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7932 else
7933 error_at (&richloc, msg, parmnum, rname);
7934 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7935 }
7936 break;
7937 case ic_assign:
7938 {
7939 const char msg[]
7940 = G_("incompatible types when assigning to type %qT from type %qT");
7941 if (warnopt)
7942 warning_at (expr_loc, 0, msg, type, rhstype);
7943 else
7944 error_at (expr_loc, msg, type, rhstype);
7945 break;
7946 }
7947 case ic_init:
7948 case ic_init_const:
7949 {
7950 const char msg[]
7951 = G_("incompatible types when initializing type %qT using type %qT");
7952 if (warnopt)
7953 warning_at (location, 0, msg, type, rhstype);
7954 else
7955 error_at (location, msg, type, rhstype);
7956 break;
7957 }
7958 case ic_return:
7959 {
7960 const char msg[]
7961 = G_("incompatible types when returning type %qT but %qT was expected");
7962 if (warnopt)
7963 warning_at (location, 0, msg, rhstype, type);
7964 else
7965 error_at (location, msg, rhstype, type);
7966 break;
7967 }
7968 default:
7969 gcc_unreachable ();
7970 }
7971
7972 return error_mark_node;
7973 }
7974 \f
7975 /* If VALUE is a compound expr all of whose expressions are constant, then
7976 return its value. Otherwise, return error_mark_node.
7977
7978 This is for handling COMPOUND_EXPRs as initializer elements
7979 which is allowed with a warning when -pedantic is specified. */
7980
7981 static tree
7982 valid_compound_expr_initializer (tree value, tree endtype)
7983 {
7984 if (TREE_CODE (value) == COMPOUND_EXPR)
7985 {
7986 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7987 == error_mark_node)
7988 return error_mark_node;
7989 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7990 endtype);
7991 }
7992 else if (!initializer_constant_valid_p (value, endtype))
7993 return error_mark_node;
7994 else
7995 return value;
7996 }
7997 \f
7998 /* Perform appropriate conversions on the initial value of a variable,
7999 store it in the declaration DECL,
8000 and print any error messages that are appropriate.
8001 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8002 If the init is invalid, store an ERROR_MARK.
8003
8004 INIT_LOC is the location of the initial value. */
8005
8006 void
8007 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
8008 {
8009 tree value, type;
8010 bool npc = false;
8011 bool int_const_expr = false;
8012 bool arith_const_expr = false;
8013
8014 /* If variable's type was invalidly declared, just ignore it. */
8015
8016 type = TREE_TYPE (decl);
8017 if (TREE_CODE (type) == ERROR_MARK)
8018 return;
8019
8020 /* Digest the specified initializer into an expression. */
8021
8022 if (init)
8023 {
8024 npc = null_pointer_constant_p (init);
8025 int_const_expr = (TREE_CODE (init) == INTEGER_CST
8026 && !TREE_OVERFLOW (init)
8027 && INTEGRAL_TYPE_P (TREE_TYPE (init)));
8028 /* Not fully determined before folding. */
8029 arith_const_expr = true;
8030 }
8031 bool constexpr_p = (VAR_P (decl)
8032 && C_DECL_DECLARED_CONSTEXPR (decl));
8033 value = digest_init (init_loc, type, init, origtype, npc, int_const_expr,
8034 arith_const_expr, true,
8035 TREE_STATIC (decl) || constexpr_p, constexpr_p);
8036
8037 /* Store the expression if valid; else report error. */
8038
8039 if (!in_system_header_at (input_location)
8040 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
8041 warning (OPT_Wtraditional, "traditional C rejects automatic "
8042 "aggregate initialization");
8043
8044 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
8045 DECL_INITIAL (decl) = value;
8046
8047 /* ANSI wants warnings about out-of-range constant initializers. */
8048 STRIP_TYPE_NOPS (value);
8049 if (TREE_STATIC (decl))
8050 constant_expression_warning (value);
8051
8052 /* Check if we need to set array size from compound literal size. */
8053 if (TREE_CODE (type) == ARRAY_TYPE
8054 && TYPE_DOMAIN (type) == NULL_TREE
8055 && value != error_mark_node)
8056 {
8057 tree inside_init = init;
8058
8059 STRIP_TYPE_NOPS (inside_init);
8060 inside_init = fold (inside_init);
8061
8062 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8063 {
8064 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8065
8066 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
8067 {
8068 /* For int foo[] = (int [3]){1}; we need to set array size
8069 now since later on array initializer will be just the
8070 brace enclosed list of the compound literal. */
8071 tree etype = strip_array_types (TREE_TYPE (decl));
8072 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8073 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
8074 layout_type (type);
8075 layout_decl (cldecl, 0);
8076 TREE_TYPE (decl)
8077 = c_build_qualified_type (type, TYPE_QUALS (etype));
8078 }
8079 }
8080 }
8081 }
8082 \f
8083 /* Methods for storing and printing names for error messages. */
8084
8085 /* Implement a spelling stack that allows components of a name to be pushed
8086 and popped. Each element on the stack is this structure. */
8087
8088 struct spelling
8089 {
8090 int kind;
8091 union
8092 {
8093 unsigned HOST_WIDE_INT i;
8094 const char *s;
8095 } u;
8096 };
8097
8098 #define SPELLING_STRING 1
8099 #define SPELLING_MEMBER 2
8100 #define SPELLING_BOUNDS 3
8101
8102 static struct spelling *spelling; /* Next stack element (unused). */
8103 static struct spelling *spelling_base; /* Spelling stack base. */
8104 static int spelling_size; /* Size of the spelling stack. */
8105
8106 /* Macros to save and restore the spelling stack around push_... functions.
8107 Alternative to SAVE_SPELLING_STACK. */
8108
8109 #define SPELLING_DEPTH() (spelling - spelling_base)
8110 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
8111
8112 /* Push an element on the spelling stack with type KIND and assign VALUE
8113 to MEMBER. */
8114
8115 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
8116 { \
8117 int depth = SPELLING_DEPTH (); \
8118 \
8119 if (depth >= spelling_size) \
8120 { \
8121 spelling_size += 10; \
8122 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
8123 spelling_size); \
8124 RESTORE_SPELLING_DEPTH (depth); \
8125 } \
8126 \
8127 spelling->kind = (KIND); \
8128 spelling->MEMBER = (VALUE); \
8129 spelling++; \
8130 }
8131
8132 /* Push STRING on the stack. Printed literally. */
8133
8134 static void
8135 push_string (const char *string)
8136 {
8137 PUSH_SPELLING (SPELLING_STRING, string, u.s);
8138 }
8139
8140 /* Push a member name on the stack. Printed as '.' STRING. */
8141
8142 static void
8143 push_member_name (tree decl)
8144 {
8145 const char *const string
8146 = (DECL_NAME (decl)
8147 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
8148 : _("<anonymous>"));
8149 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
8150 }
8151
8152 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
8153
8154 static void
8155 push_array_bounds (unsigned HOST_WIDE_INT bounds)
8156 {
8157 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
8158 }
8159
8160 /* Compute the maximum size in bytes of the printed spelling. */
8161
8162 static int
8163 spelling_length (void)
8164 {
8165 int size = 0;
8166 struct spelling *p;
8167
8168 for (p = spelling_base; p < spelling; p++)
8169 {
8170 if (p->kind == SPELLING_BOUNDS)
8171 size += 25;
8172 else
8173 size += strlen (p->u.s) + 1;
8174 }
8175
8176 return size;
8177 }
8178
8179 /* Print the spelling to BUFFER and return it. */
8180
8181 static char *
8182 print_spelling (char *buffer)
8183 {
8184 char *d = buffer;
8185 struct spelling *p;
8186
8187 for (p = spelling_base; p < spelling; p++)
8188 if (p->kind == SPELLING_BOUNDS)
8189 {
8190 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
8191 d += strlen (d);
8192 }
8193 else
8194 {
8195 const char *s;
8196 if (p->kind == SPELLING_MEMBER)
8197 *d++ = '.';
8198 for (s = p->u.s; (*d = *s++); d++)
8199 ;
8200 }
8201 *d++ = '\0';
8202 return buffer;
8203 }
8204
8205 /* Check whether INIT, a floating or integer constant, is
8206 representable in TYPE, a real floating type with the same radix or
8207 a decimal floating type initialized with a binary floating
8208 constant. Return true if OK, false if not. */
8209 static bool
8210 constexpr_init_fits_real_type (tree type, tree init)
8211 {
8212 gcc_assert (SCALAR_FLOAT_TYPE_P (type));
8213 gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
8214 if (TREE_CODE (init) == REAL_CST
8215 && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
8216 {
8217 /* Same mode, no conversion required except for the case of
8218 signaling NaNs if the types are incompatible (e.g. double and
8219 long double with the same mode). */
8220 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
8221 && !comptypes (TYPE_MAIN_VARIANT (type),
8222 TYPE_MAIN_VARIANT (TREE_TYPE (init))))
8223 return false;
8224 return true;
8225 }
8226 if (TREE_CODE (init) == INTEGER_CST)
8227 {
8228 tree converted = build_real_from_int_cst (type, init);
8229 bool fail = false;
8230 wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
8231 TYPE_PRECISION (TREE_TYPE (init)));
8232 return !fail && wi::eq_p (w, wi::to_wide (init));
8233 }
8234 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
8235 return false;
8236 if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
8237 && MODE_HAS_INFINITIES (TYPE_MODE (type)))
8238 || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
8239 && MODE_HAS_NANS (TYPE_MODE (type))))
8240 return true;
8241 if (DECIMAL_FLOAT_TYPE_P (type)
8242 && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
8243 {
8244 /* This is valid if the real number represented by the
8245 initializer can be exactly represented in the decimal
8246 type. Compare the values using MPFR. */
8247 REAL_VALUE_TYPE t;
8248 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
8249 mpfr_t bin_val, dec_val;
8250 mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
8251 mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
8252 mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
8253 char string[256];
8254 real_to_decimal (string, &t, sizeof string, 0, 1);
8255 bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
8256 && mpfr_equal_p (bin_val, dec_val));
8257 mpfr_clear (bin_val);
8258 mpfr_clear (dec_val);
8259 return res;
8260 }
8261 /* exact_real_truncate is not quite right here, since it doesn't
8262 allow even an exact conversion to subnormal values. */
8263 REAL_VALUE_TYPE t;
8264 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
8265 return real_identical (&t, &TREE_REAL_CST (init));
8266 }
8267
8268 /* Check whether INIT (location LOC) is valid as a 'constexpr'
8269 initializer for type TYPE, and give an error if not. INIT has
8270 already been folded and verified to be constant. INT_CONST_EXPR
8271 and ARITH_CONST_EXPR say whether it is an integer constant
8272 expression or arithmetic constant expression, respectively. If
8273 TYPE is not a scalar type, this function does nothing. */
8274
8275 static void
8276 check_constexpr_init (location_t loc, tree type, tree init,
8277 bool int_const_expr, bool arith_const_expr)
8278 {
8279 if (POINTER_TYPE_P (type))
8280 {
8281 /* The initializer must be null. */
8282 if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
8283 error_at (loc, "%<constexpr%> pointer initializer is not null");
8284 return;
8285 }
8286 if (INTEGRAL_TYPE_P (type))
8287 {
8288 /* The initializer must be an integer constant expression,
8289 representable in the target type. */
8290 if (!int_const_expr)
8291 error_at (loc, "%<constexpr%> integer initializer is not an "
8292 "integer constant expression");
8293 if (!int_fits_type_p (init, type))
8294 error_at (loc, "%<constexpr%> initializer not representable in "
8295 "type of object");
8296 return;
8297 }
8298 /* We don't apply any extra checks to extension types such as vector
8299 or fixed-point types. */
8300 if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
8301 return;
8302 if (!arith_const_expr)
8303 {
8304 error_at (loc, "%<constexpr%> initializer is not an arithmetic "
8305 "constant expression");
8306 return;
8307 }
8308 /* We don't apply any extra checks to complex integers. */
8309 if (TREE_CODE (type) == COMPLEX_TYPE
8310 && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8311 return;
8312 /* Following N3082, a real type cannot be initialized from a complex
8313 type and a binary type cannot be initialized from a decimal type
8314 (but initializing a decimal type from a binary type is OK).
8315 Signaling NaN initializers are OK only if the types are
8316 compatible (not just the same mode); all quiet NaN and infinity
8317 initializations are considered to preserve the value. */
8318 if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
8319 && SCALAR_FLOAT_TYPE_P (type))
8320 {
8321 error_at (loc, "%<constexpr%> initializer for a real type is of "
8322 "complex type");
8323 return;
8324 }
8325 if (SCALAR_FLOAT_TYPE_P (type)
8326 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
8327 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
8328 && !DECIMAL_FLOAT_TYPE_P (type))
8329 {
8330 error_at (loc, "%<constexpr%> initializer for a binary "
8331 "floating-point type is of decimal type");
8332 return;
8333 }
8334 bool fits;
8335 if (TREE_CODE (type) == COMPLEX_TYPE)
8336 {
8337 switch (TREE_CODE (init))
8338 {
8339 case INTEGER_CST:
8340 case REAL_CST:
8341 fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
8342 break;
8343 case COMPLEX_CST:
8344 fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
8345 TREE_REALPART (init))
8346 && constexpr_init_fits_real_type (TREE_TYPE (type),
8347 TREE_IMAGPART (init)));
8348 break;
8349 default:
8350 gcc_unreachable ();
8351 }
8352 }
8353 else
8354 fits = constexpr_init_fits_real_type (type, init);
8355 if (!fits)
8356 error_at (loc, "%<constexpr%> initializer not representable in "
8357 "type of object");
8358 }
8359
8360 /* Digest the parser output INIT as an initializer for type TYPE.
8361 Return a C expression of type TYPE to represent the initial value.
8362
8363 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8364
8365 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
8366 INT_CONST_EXPR is true if INIT is an integer constant expression,
8367 and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
8368 constant expression, false if it has already been determined in the
8369 caller that it is not (but folding may have made the value passed here
8370 indistinguishable from an arithmetic constant expression).
8371
8372 If INIT is a string constant, STRICT_STRING is true if it is
8373 unparenthesized or we should not warn here for it being parenthesized.
8374 For other types of INIT, STRICT_STRING is not used.
8375
8376 INIT_LOC is the location of the INIT.
8377
8378 REQUIRE_CONSTANT requests an error if non-constant initializers or
8379 elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
8380 on initializers for 'constexpr' objects apply. */
8381
8382 static tree
8383 digest_init (location_t init_loc, tree type, tree init, tree origtype,
8384 bool null_pointer_constant, bool int_const_expr,
8385 bool arith_const_expr, bool strict_string,
8386 bool require_constant, bool require_constexpr)
8387 {
8388 enum tree_code code = TREE_CODE (type);
8389 tree inside_init = init;
8390 tree semantic_type = NULL_TREE;
8391 bool maybe_const = true;
8392
8393 if (type == error_mark_node
8394 || !init
8395 || error_operand_p (init))
8396 return error_mark_node;
8397
8398 STRIP_TYPE_NOPS (inside_init);
8399
8400 if (!c_in_omp_for)
8401 {
8402 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
8403 {
8404 semantic_type = TREE_TYPE (inside_init);
8405 inside_init = TREE_OPERAND (inside_init, 0);
8406 }
8407 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
8408 }
8409 /* TODO: this may not detect all cases of expressions folding to
8410 constants that are not arithmetic constant expressions. */
8411 if (!maybe_const)
8412 arith_const_expr = false;
8413 else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
8414 && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
8415 && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
8416 arith_const_expr = false;
8417 else if (TREE_CODE (inside_init) != INTEGER_CST
8418 && TREE_CODE (inside_init) != REAL_CST
8419 && TREE_CODE (inside_init) != COMPLEX_CST)
8420 arith_const_expr = false;
8421 else if (TREE_OVERFLOW (inside_init))
8422 arith_const_expr = false;
8423
8424 /* Initialization of an array of chars from a string constant
8425 optionally enclosed in braces. */
8426
8427 if (code == ARRAY_TYPE && inside_init
8428 && TREE_CODE (inside_init) == STRING_CST)
8429 {
8430 tree typ1
8431 = (TYPE_ATOMIC (TREE_TYPE (type))
8432 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
8433 TYPE_QUAL_ATOMIC)
8434 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
8435 /* Note that an array could be both an array of character type
8436 and an array of wchar_t if wchar_t is signed char or unsigned
8437 char. */
8438 bool char_array = (typ1 == char_type_node
8439 || typ1 == signed_char_type_node
8440 || typ1 == unsigned_char_type_node);
8441 bool wchar_array = !!comptypes (typ1, wchar_type_node);
8442 bool char16_array = !!comptypes (typ1, char16_type_node);
8443 bool char32_array = !!comptypes (typ1, char32_type_node);
8444
8445 if (char_array || wchar_array || char16_array || char32_array)
8446 {
8447 struct c_expr expr;
8448 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8449 bool incompat_string_cst = false;
8450 expr.value = inside_init;
8451 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8452 expr.original_type = NULL;
8453 expr.m_decimal = 0;
8454 maybe_warn_string_init (init_loc, type, expr);
8455
8456 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8457 pedwarn_init (init_loc, OPT_Wpedantic,
8458 "initialization of a flexible array member");
8459
8460 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8461 TYPE_MAIN_VARIANT (type)))
8462 return inside_init;
8463
8464 if (char_array)
8465 {
8466 if (typ2 != char_type_node && typ2 != char8_type_node)
8467 incompat_string_cst = true;
8468 }
8469 else if (!comptypes (typ1, typ2))
8470 incompat_string_cst = true;
8471
8472 if (incompat_string_cst)
8473 {
8474 error_init (init_loc, "cannot initialize array of %qT from "
8475 "a string literal with type array of %qT",
8476 typ1, typ2);
8477 return error_mark_node;
8478 }
8479
8480 if (require_constexpr
8481 && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
8482 {
8483 /* Check if all characters of the string can be
8484 represented in the type of the constexpr object being
8485 initialized. */
8486 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8487 const unsigned char *p =
8488 (const unsigned char *) TREE_STRING_POINTER (inside_init);
8489 gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
8490 for (unsigned i = 0; i < len; i++)
8491 if (p[i] > 127)
8492 {
8493 error_init (init_loc, "%<constexpr%> initializer not "
8494 "representable in type of object");
8495 break;
8496 }
8497 }
8498
8499 if (TYPE_DOMAIN (type) != NULL_TREE
8500 && TYPE_SIZE (type) != NULL_TREE
8501 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8502 {
8503 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8504 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8505
8506 /* Subtract the size of a single (possibly wide) character
8507 because it's ok to ignore the terminating null char
8508 that is counted in the length of the constant. */
8509 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8510 pedwarn_init (init_loc, 0,
8511 ("initializer-string for array of %qT "
8512 "is too long"), typ1);
8513 else if (warn_cxx_compat
8514 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8515 warning_at (init_loc, OPT_Wc___compat,
8516 ("initializer-string for array of %qT "
8517 "is too long for C++"), typ1);
8518 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8519 {
8520 unsigned HOST_WIDE_INT size
8521 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8522 const char *p = TREE_STRING_POINTER (inside_init);
8523
8524 inside_init = build_string (size, p);
8525 }
8526 }
8527
8528 TREE_TYPE (inside_init) = type;
8529 return inside_init;
8530 }
8531 else if (INTEGRAL_TYPE_P (typ1))
8532 {
8533 error_init (init_loc, "array of inappropriate type initialized "
8534 "from string constant");
8535 return error_mark_node;
8536 }
8537 }
8538
8539 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8540 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8541 below and handle as a constructor. */
8542 if (code == VECTOR_TYPE
8543 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8544 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8545 && TREE_CONSTANT (inside_init))
8546 {
8547 if (TREE_CODE (inside_init) == VECTOR_CST
8548 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8549 TYPE_MAIN_VARIANT (type)))
8550 return inside_init;
8551
8552 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8553 {
8554 unsigned HOST_WIDE_INT ix;
8555 tree value;
8556 bool constant_p = true;
8557
8558 /* Iterate through elements and check if all constructor
8559 elements are *_CSTs. */
8560 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8561 if (!CONSTANT_CLASS_P (value))
8562 {
8563 constant_p = false;
8564 break;
8565 }
8566
8567 if (constant_p)
8568 return build_vector_from_ctor (type,
8569 CONSTRUCTOR_ELTS (inside_init));
8570 }
8571 }
8572
8573 if (warn_sequence_point)
8574 verify_sequence_points (inside_init);
8575
8576 /* Any type can be initialized
8577 from an expression of the same type, optionally with braces. */
8578
8579 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8580 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8581 TYPE_MAIN_VARIANT (type))
8582 || (code == ARRAY_TYPE
8583 && comptypes (TREE_TYPE (inside_init), type))
8584 || (gnu_vector_type_p (type)
8585 && comptypes (TREE_TYPE (inside_init), type))
8586 || (code == POINTER_TYPE
8587 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8588 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8589 TREE_TYPE (type)))))
8590 {
8591 if (code == POINTER_TYPE)
8592 {
8593 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8594 {
8595 if (TREE_CODE (inside_init) == STRING_CST
8596 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8597 inside_init = array_to_pointer_conversion
8598 (init_loc, inside_init);
8599 else
8600 {
8601 error_init (init_loc, "invalid use of non-lvalue array");
8602 return error_mark_node;
8603 }
8604 }
8605 }
8606
8607 if (code == VECTOR_TYPE || c_hardbool_type_attr (type))
8608 /* Although the types are compatible, we may require a
8609 conversion. */
8610 inside_init = convert (type, inside_init);
8611
8612 if ((code == RECORD_TYPE || code == UNION_TYPE)
8613 && !comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init))))
8614 {
8615 error_init (init_loc, "invalid initializer");
8616 return error_mark_node;
8617 }
8618
8619 if (require_constant
8620 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8621 {
8622 /* As an extension, allow initializing objects with static storage
8623 duration with compound literals (which are then treated just as
8624 the brace enclosed list they contain). Also allow this for
8625 vectors, as we can only assign them with compound literals. */
8626 if (flag_isoc99 && code != VECTOR_TYPE)
8627 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8628 "is not constant");
8629 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8630 inside_init = DECL_INITIAL (decl);
8631 }
8632
8633 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8634 && TREE_CODE (inside_init) != CONSTRUCTOR)
8635 {
8636 error_init (init_loc, "array initialized from non-constant array "
8637 "expression");
8638 return error_mark_node;
8639 }
8640
8641 /* Compound expressions can only occur here if -Wpedantic or
8642 -pedantic-errors is specified. In the later case, we always want
8643 an error. In the former case, we simply want a warning. */
8644 if (require_constant && pedantic
8645 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8646 {
8647 inside_init
8648 = valid_compound_expr_initializer (inside_init,
8649 TREE_TYPE (inside_init));
8650 if (inside_init == error_mark_node)
8651 error_init (init_loc, "initializer element is not constant");
8652 else
8653 pedwarn_init (init_loc, OPT_Wpedantic,
8654 "initializer element is not constant");
8655 if (flag_pedantic_errors)
8656 inside_init = error_mark_node;
8657 }
8658 else if (require_constant
8659 && !initializer_constant_valid_p (inside_init,
8660 TREE_TYPE (inside_init)))
8661 {
8662 error_init (init_loc, "initializer element is not constant");
8663 inside_init = error_mark_node;
8664 }
8665 else if (require_constant && !maybe_const)
8666 pedwarn_init (init_loc, OPT_Wpedantic,
8667 "initializer element is not a constant expression");
8668 else if (require_constexpr)
8669 check_constexpr_init (init_loc, type, inside_init,
8670 int_const_expr, arith_const_expr);
8671
8672 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8673 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8674 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8675 type, inside_init, origtype,
8676 (require_constant
8677 ? ic_init_const
8678 : ic_init), null_pointer_constant,
8679 NULL_TREE, NULL_TREE, 0);
8680 return inside_init;
8681 }
8682
8683 /* Handle scalar types, including conversions. */
8684
8685 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8686 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8687 || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE
8688 || code == BITINT_TYPE)
8689 {
8690 tree unconverted_init = inside_init;
8691 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8692 && (TREE_CODE (init) == STRING_CST
8693 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8694 inside_init = init = array_to_pointer_conversion (init_loc, init);
8695 if (semantic_type)
8696 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8697 inside_init);
8698 inside_init
8699 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8700 inside_init, origtype,
8701 require_constant ? ic_init_const : ic_init,
8702 null_pointer_constant, NULL_TREE, NULL_TREE,
8703 0);
8704
8705 /* Check to see if we have already given an error message. */
8706 if (inside_init == error_mark_node)
8707 ;
8708 else if (require_constant && !TREE_CONSTANT (inside_init))
8709 {
8710 error_init (init_loc, "initializer element is not constant");
8711 inside_init = error_mark_node;
8712 }
8713 else if (require_constant
8714 && !initializer_constant_valid_p (inside_init,
8715 TREE_TYPE (inside_init)))
8716 {
8717 error_init (init_loc, "initializer element is not computable at "
8718 "load time");
8719 inside_init = error_mark_node;
8720 }
8721 else if (require_constant && !maybe_const)
8722 pedwarn_init (init_loc, OPT_Wpedantic,
8723 "initializer element is not a constant expression");
8724 else if (require_constexpr)
8725 check_constexpr_init (init_loc, type, unconverted_init,
8726 int_const_expr, arith_const_expr);
8727
8728 return inside_init;
8729 }
8730
8731 /* Come here only for records and arrays. */
8732
8733 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8734 {
8735 error_init (init_loc,
8736 "variable-sized object may not be initialized except "
8737 "with an empty initializer");
8738 return error_mark_node;
8739 }
8740
8741 error_init (init_loc, "invalid initializer");
8742 return error_mark_node;
8743 }
8744 \f
8745 /* Handle initializers that use braces. */
8746
8747 /* Type of object we are accumulating a constructor for.
8748 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8749 static tree constructor_type;
8750
8751 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8752 left to fill. */
8753 static tree constructor_fields;
8754
8755 /* For an ARRAY_TYPE, this is the specified index
8756 at which to store the next element we get. */
8757 static tree constructor_index;
8758
8759 /* For an ARRAY_TYPE, this is the maximum index. */
8760 static tree constructor_max_index;
8761
8762 /* For a RECORD_TYPE, this is the first field not yet written out. */
8763 static tree constructor_unfilled_fields;
8764
8765 /* For an ARRAY_TYPE, this is the index of the first element
8766 not yet written out. */
8767 static tree constructor_unfilled_index;
8768
8769 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8770 This is so we can generate gaps between fields, when appropriate. */
8771 static tree constructor_bit_index;
8772
8773 /* If we are saving up the elements rather than allocating them,
8774 this is the list of elements so far (in reverse order,
8775 most recent first). */
8776 static vec<constructor_elt, va_gc> *constructor_elements;
8777
8778 /* 1 if constructor should be incrementally stored into a constructor chain,
8779 0 if all the elements should be kept in AVL tree. */
8780 static int constructor_incremental;
8781
8782 /* 1 if so far this constructor's elements are all compile-time constants. */
8783 static int constructor_constant;
8784
8785 /* 1 if so far this constructor's elements are all valid address constants. */
8786 static int constructor_simple;
8787
8788 /* 1 if this constructor has an element that cannot be part of a
8789 constant expression. */
8790 static int constructor_nonconst;
8791
8792 /* 1 if this constructor is erroneous so far. */
8793 static int constructor_erroneous;
8794
8795 /* 1 if this constructor is the universal zero initializer { 0 }. */
8796 static int constructor_zeroinit;
8797
8798 /* Structure for managing pending initializer elements, organized as an
8799 AVL tree. */
8800
8801 struct init_node
8802 {
8803 struct init_node *left, *right;
8804 struct init_node *parent;
8805 int balance;
8806 tree purpose;
8807 tree value;
8808 tree origtype;
8809 };
8810
8811 /* Tree of pending elements at this constructor level.
8812 These are elements encountered out of order
8813 which belong at places we haven't reached yet in actually
8814 writing the output.
8815 Will never hold tree nodes across GC runs. */
8816 static struct init_node *constructor_pending_elts;
8817
8818 /* The SPELLING_DEPTH of this constructor. */
8819 static int constructor_depth;
8820
8821 /* DECL node for which an initializer is being read.
8822 0 means we are reading a constructor expression
8823 such as (struct foo) {...}. */
8824 static tree constructor_decl;
8825
8826 /* Nonzero if there were any member designators in this initializer. */
8827 static int constructor_designated;
8828
8829 /* Nesting depth of designator list. */
8830 static int designator_depth;
8831
8832 /* Nonzero if there were diagnosed errors in this designator list. */
8833 static int designator_erroneous;
8834
8835 \f
8836 /* This stack has a level for each implicit or explicit level of
8837 structuring in the initializer, including the outermost one. It
8838 saves the values of most of the variables above. */
8839
8840 struct constructor_range_stack;
8841
8842 struct constructor_stack
8843 {
8844 struct constructor_stack *next;
8845 tree type;
8846 tree fields;
8847 tree index;
8848 tree max_index;
8849 tree unfilled_index;
8850 tree unfilled_fields;
8851 tree bit_index;
8852 vec<constructor_elt, va_gc> *elements;
8853 struct init_node *pending_elts;
8854 int offset;
8855 int depth;
8856 /* If value nonzero, this value should replace the entire
8857 constructor at this level. */
8858 struct c_expr replacement_value;
8859 struct constructor_range_stack *range_stack;
8860 char constant;
8861 char simple;
8862 char nonconst;
8863 char implicit;
8864 char erroneous;
8865 char outer;
8866 char incremental;
8867 char designated;
8868 int designator_depth;
8869 };
8870
8871 static struct constructor_stack *constructor_stack;
8872
8873 /* This stack represents designators from some range designator up to
8874 the last designator in the list. */
8875
8876 struct constructor_range_stack
8877 {
8878 struct constructor_range_stack *next, *prev;
8879 struct constructor_stack *stack;
8880 tree range_start;
8881 tree index;
8882 tree range_end;
8883 tree fields;
8884 };
8885
8886 static struct constructor_range_stack *constructor_range_stack;
8887
8888 /* This stack records separate initializers that are nested.
8889 Nested initializers can't happen in ANSI C, but GNU C allows them
8890 in cases like { ... (struct foo) { ... } ... }. */
8891
8892 struct initializer_stack
8893 {
8894 struct initializer_stack *next;
8895 tree decl;
8896 struct constructor_stack *constructor_stack;
8897 struct constructor_range_stack *constructor_range_stack;
8898 vec<constructor_elt, va_gc> *elements;
8899 struct spelling *spelling;
8900 struct spelling *spelling_base;
8901 int spelling_size;
8902 char require_constant_value;
8903 char require_constant_elements;
8904 char require_constexpr_value;
8905 char designated;
8906 rich_location *missing_brace_richloc;
8907 };
8908
8909 static struct initializer_stack *initializer_stack;
8910 \f
8911 /* Prepare to parse and output the initializer for variable DECL. */
8912
8913 void
8914 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
8915 bool init_require_constant, bool init_require_constexpr,
8916 rich_location *richloc)
8917 {
8918 const char *locus;
8919 struct initializer_stack *p = XNEW (struct initializer_stack);
8920
8921 p->decl = constructor_decl;
8922 p->require_constant_value = require_constant_value;
8923 p->require_constant_elements = require_constant_elements;
8924 p->require_constexpr_value = require_constexpr_value;
8925 p->constructor_stack = constructor_stack;
8926 p->constructor_range_stack = constructor_range_stack;
8927 p->elements = constructor_elements;
8928 p->spelling = spelling;
8929 p->spelling_base = spelling_base;
8930 p->spelling_size = spelling_size;
8931 p->next = initializer_stack;
8932 p->missing_brace_richloc = richloc;
8933 p->designated = constructor_designated;
8934 initializer_stack = p;
8935
8936 constructor_decl = decl;
8937 constructor_designated = 0;
8938
8939 require_constant_value = init_require_constant;
8940 require_constexpr_value = init_require_constexpr;
8941 if (decl != NULL_TREE && decl != error_mark_node)
8942 {
8943 require_constant_elements
8944 = ((init_require_constant || (pedantic && !flag_isoc99))
8945 /* For a scalar, you can always use any value to initialize,
8946 even within braces. */
8947 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8948 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8949 }
8950 else
8951 {
8952 require_constant_elements = false;
8953 locus = _("(anonymous)");
8954 }
8955
8956 constructor_stack = 0;
8957 constructor_range_stack = 0;
8958
8959 found_missing_braces = 0;
8960
8961 spelling_base = 0;
8962 spelling_size = 0;
8963 RESTORE_SPELLING_DEPTH (0);
8964
8965 if (locus)
8966 push_string (locus);
8967 }
8968
8969 void
8970 finish_init (void)
8971 {
8972 struct initializer_stack *p = initializer_stack;
8973
8974 /* Free the whole constructor stack of this initializer. */
8975 while (constructor_stack)
8976 {
8977 struct constructor_stack *q = constructor_stack;
8978 constructor_stack = q->next;
8979 XDELETE (q);
8980 }
8981
8982 gcc_assert (!constructor_range_stack);
8983
8984 /* Pop back to the data of the outer initializer (if any). */
8985 XDELETE (spelling_base);
8986
8987 constructor_decl = p->decl;
8988 require_constant_value = p->require_constant_value;
8989 require_constant_elements = p->require_constant_elements;
8990 require_constexpr_value = p->require_constexpr_value;
8991 constructor_stack = p->constructor_stack;
8992 constructor_designated = p->designated;
8993 constructor_range_stack = p->constructor_range_stack;
8994 constructor_elements = p->elements;
8995 spelling = p->spelling;
8996 spelling_base = p->spelling_base;
8997 spelling_size = p->spelling_size;
8998 initializer_stack = p->next;
8999 XDELETE (p);
9000 }
9001 \f
9002 /* Call here when we see the initializer is surrounded by braces.
9003 This is instead of a call to push_init_level;
9004 it is matched by a call to pop_init_level.
9005
9006 TYPE is the type to initialize, for a constructor expression.
9007 For an initializer for a decl, TYPE is zero. */
9008
9009 void
9010 really_start_incremental_init (tree type)
9011 {
9012 struct constructor_stack *p = XNEW (struct constructor_stack);
9013
9014 if (type == NULL_TREE)
9015 type = TREE_TYPE (constructor_decl);
9016
9017 if (VECTOR_TYPE_P (type)
9018 && TYPE_VECTOR_OPAQUE (type))
9019 error ("opaque vector types cannot be initialized");
9020
9021 p->type = constructor_type;
9022 p->fields = constructor_fields;
9023 p->index = constructor_index;
9024 p->max_index = constructor_max_index;
9025 p->unfilled_index = constructor_unfilled_index;
9026 p->unfilled_fields = constructor_unfilled_fields;
9027 p->bit_index = constructor_bit_index;
9028 p->elements = constructor_elements;
9029 p->constant = constructor_constant;
9030 p->simple = constructor_simple;
9031 p->nonconst = constructor_nonconst;
9032 p->erroneous = constructor_erroneous;
9033 p->pending_elts = constructor_pending_elts;
9034 p->depth = constructor_depth;
9035 p->replacement_value.value = 0;
9036 p->replacement_value.original_code = ERROR_MARK;
9037 p->replacement_value.original_type = NULL;
9038 p->implicit = 0;
9039 p->range_stack = 0;
9040 p->outer = 0;
9041 p->incremental = constructor_incremental;
9042 p->designated = constructor_designated;
9043 p->designator_depth = designator_depth;
9044 p->next = 0;
9045 constructor_stack = p;
9046
9047 constructor_constant = 1;
9048 constructor_simple = 1;
9049 constructor_nonconst = 0;
9050 constructor_depth = SPELLING_DEPTH ();
9051 constructor_elements = NULL;
9052 constructor_pending_elts = 0;
9053 constructor_type = type;
9054 constructor_incremental = 1;
9055 constructor_designated = 0;
9056 constructor_zeroinit = 1;
9057 designator_depth = 0;
9058 designator_erroneous = 0;
9059
9060 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9061 {
9062 constructor_fields = TYPE_FIELDS (constructor_type);
9063 /* Skip any nameless bit fields at the beginning. */
9064 while (constructor_fields != NULL_TREE
9065 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9066 constructor_fields = DECL_CHAIN (constructor_fields);
9067
9068 constructor_unfilled_fields = constructor_fields;
9069 constructor_bit_index = bitsize_zero_node;
9070 }
9071 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9072 {
9073 if (TYPE_DOMAIN (constructor_type))
9074 {
9075 constructor_max_index
9076 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
9077
9078 /* Detect non-empty initializations of zero-length arrays. */
9079 if (constructor_max_index == NULL_TREE
9080 && TYPE_SIZE (constructor_type))
9081 constructor_max_index = integer_minus_one_node;
9082
9083 /* constructor_max_index needs to be an INTEGER_CST. Attempts
9084 to initialize VLAs with a nonempty initializer will cause a
9085 proper error; avoid tree checking errors as well by setting a
9086 safe value. */
9087 if (constructor_max_index
9088 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9089 constructor_max_index = integer_minus_one_node;
9090
9091 constructor_index
9092 = convert (bitsizetype,
9093 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9094 }
9095 else
9096 {
9097 constructor_index = bitsize_zero_node;
9098 constructor_max_index = NULL_TREE;
9099 }
9100
9101 constructor_unfilled_index = constructor_index;
9102 }
9103 else if (gnu_vector_type_p (constructor_type))
9104 {
9105 /* Vectors are like simple fixed-size arrays. */
9106 constructor_max_index =
9107 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
9108 constructor_index = bitsize_zero_node;
9109 constructor_unfilled_index = constructor_index;
9110 }
9111 else
9112 {
9113 /* Handle the case of int x = {5}; */
9114 constructor_fields = constructor_type;
9115 constructor_unfilled_fields = constructor_type;
9116 }
9117 }
9118 \f
9119 extern location_t last_init_list_comma;
9120
9121 /* Called when we see an open brace for a nested initializer. Finish
9122 off any pending levels with implicit braces. */
9123 void
9124 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
9125 {
9126 while (constructor_stack->implicit)
9127 {
9128 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9129 && constructor_fields == NULL_TREE)
9130 process_init_element (input_location,
9131 pop_init_level (loc, 1, braced_init_obstack,
9132 last_init_list_comma),
9133 true, braced_init_obstack);
9134 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
9135 && constructor_max_index
9136 && tree_int_cst_lt (constructor_max_index,
9137 constructor_index))
9138 process_init_element (input_location,
9139 pop_init_level (loc, 1, braced_init_obstack,
9140 last_init_list_comma),
9141 true, braced_init_obstack);
9142 else
9143 break;
9144 }
9145 }
9146
9147 /* Push down into a subobject, for initialization.
9148 If this is for an explicit set of braces, IMPLICIT is 0.
9149 If it is because the next element belongs at a lower level,
9150 IMPLICIT is 1 (or 2 if the push is because of designator list). */
9151
9152 void
9153 push_init_level (location_t loc, int implicit,
9154 struct obstack *braced_init_obstack)
9155 {
9156 struct constructor_stack *p;
9157 tree value = NULL_TREE;
9158
9159 /* Unless this is an explicit brace, we need to preserve previous
9160 content if any. */
9161 if (implicit)
9162 {
9163 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
9164 value = find_init_member (constructor_fields, braced_init_obstack);
9165 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9166 value = find_init_member (constructor_index, braced_init_obstack);
9167 }
9168
9169 p = XNEW (struct constructor_stack);
9170 p->type = constructor_type;
9171 p->fields = constructor_fields;
9172 p->index = constructor_index;
9173 p->max_index = constructor_max_index;
9174 p->unfilled_index = constructor_unfilled_index;
9175 p->unfilled_fields = constructor_unfilled_fields;
9176 p->bit_index = constructor_bit_index;
9177 p->elements = constructor_elements;
9178 p->constant = constructor_constant;
9179 p->simple = constructor_simple;
9180 p->nonconst = constructor_nonconst;
9181 p->erroneous = constructor_erroneous;
9182 p->pending_elts = constructor_pending_elts;
9183 p->depth = constructor_depth;
9184 p->replacement_value.value = NULL_TREE;
9185 p->replacement_value.original_code = ERROR_MARK;
9186 p->replacement_value.original_type = NULL;
9187 p->implicit = implicit;
9188 p->outer = 0;
9189 p->incremental = constructor_incremental;
9190 p->designated = constructor_designated;
9191 p->designator_depth = designator_depth;
9192 p->next = constructor_stack;
9193 p->range_stack = 0;
9194 constructor_stack = p;
9195
9196 constructor_constant = 1;
9197 constructor_simple = 1;
9198 constructor_nonconst = 0;
9199 constructor_depth = SPELLING_DEPTH ();
9200 constructor_elements = NULL;
9201 constructor_incremental = 1;
9202 /* If the upper initializer is designated, then mark this as
9203 designated too to prevent bogus warnings. */
9204 constructor_designated = p->designated;
9205 constructor_pending_elts = 0;
9206 if (!implicit)
9207 {
9208 p->range_stack = constructor_range_stack;
9209 constructor_range_stack = 0;
9210 designator_depth = 0;
9211 designator_erroneous = 0;
9212 }
9213
9214 /* Don't die if an entire brace-pair level is superfluous
9215 in the containing level. */
9216 if (constructor_type == NULL_TREE)
9217 ;
9218 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9219 {
9220 /* Don't die if there are extra init elts at the end. */
9221 if (constructor_fields == NULL_TREE)
9222 constructor_type = NULL_TREE;
9223 else
9224 {
9225 constructor_type = TREE_TYPE (constructor_fields);
9226 push_member_name (constructor_fields);
9227 constructor_depth++;
9228 }
9229 }
9230 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9231 {
9232 constructor_type = TREE_TYPE (constructor_type);
9233 push_array_bounds (tree_to_uhwi (constructor_index));
9234 constructor_depth++;
9235 }
9236
9237 if (constructor_type == NULL_TREE)
9238 {
9239 error_init (loc, "extra brace group at end of initializer");
9240 constructor_fields = NULL_TREE;
9241 constructor_unfilled_fields = NULL_TREE;
9242 return;
9243 }
9244
9245 if (value && TREE_CODE (value) == CONSTRUCTOR)
9246 {
9247 constructor_constant = TREE_CONSTANT (value);
9248 constructor_simple = TREE_STATIC (value);
9249 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
9250 constructor_elements = CONSTRUCTOR_ELTS (value);
9251 if (!vec_safe_is_empty (constructor_elements)
9252 && (TREE_CODE (constructor_type) == RECORD_TYPE
9253 || TREE_CODE (constructor_type) == ARRAY_TYPE))
9254 set_nonincremental_init (braced_init_obstack);
9255 }
9256
9257 if (implicit == 1)
9258 {
9259 found_missing_braces = 1;
9260 if (initializer_stack->missing_brace_richloc)
9261 initializer_stack->missing_brace_richloc->add_fixit_insert_before
9262 (loc, "{");
9263 }
9264
9265 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9266 {
9267 constructor_fields = TYPE_FIELDS (constructor_type);
9268 /* Skip any nameless bit fields at the beginning. */
9269 while (constructor_fields != NULL_TREE
9270 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9271 constructor_fields = DECL_CHAIN (constructor_fields);
9272
9273 constructor_unfilled_fields = constructor_fields;
9274 constructor_bit_index = bitsize_zero_node;
9275 }
9276 else if (gnu_vector_type_p (constructor_type))
9277 {
9278 /* Vectors are like simple fixed-size arrays. */
9279 constructor_max_index =
9280 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
9281 constructor_index = bitsize_int (0);
9282 constructor_unfilled_index = constructor_index;
9283 }
9284 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9285 {
9286 if (TYPE_DOMAIN (constructor_type))
9287 {
9288 constructor_max_index
9289 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
9290
9291 /* Detect non-empty initializations of zero-length arrays. */
9292 if (constructor_max_index == NULL_TREE
9293 && TYPE_SIZE (constructor_type))
9294 constructor_max_index = integer_minus_one_node;
9295
9296 /* constructor_max_index needs to be an INTEGER_CST. Attempts
9297 to initialize VLAs will cause a proper error; avoid tree
9298 checking errors as well by setting a safe value. */
9299 if (constructor_max_index
9300 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9301 constructor_max_index = integer_minus_one_node;
9302
9303 constructor_index
9304 = convert (bitsizetype,
9305 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9306 }
9307 else
9308 constructor_index = bitsize_zero_node;
9309
9310 constructor_unfilled_index = constructor_index;
9311 if (value && TREE_CODE (value) == STRING_CST)
9312 {
9313 /* We need to split the char/wchar array into individual
9314 characters, so that we don't have to special case it
9315 everywhere. */
9316 set_nonincremental_init_from_string (value, braced_init_obstack);
9317 }
9318 }
9319 else
9320 {
9321 if (constructor_type != error_mark_node)
9322 warning_init (input_location, 0, "braces around scalar initializer");
9323 constructor_fields = constructor_type;
9324 constructor_unfilled_fields = constructor_type;
9325 }
9326 }
9327
9328 /* At the end of an implicit or explicit brace level,
9329 finish up that level of constructor. If a single expression
9330 with redundant braces initialized that level, return the
9331 c_expr structure for that expression. Otherwise, the original_code
9332 element is set to ERROR_MARK.
9333 If we were outputting the elements as they are read, return 0 as the value
9334 from inner levels (process_init_element ignores that),
9335 but return error_mark_node as the value from the outermost level
9336 (that's what we want to put in DECL_INITIAL).
9337 Otherwise, return a CONSTRUCTOR expression as the value. */
9338
9339 struct c_expr
9340 pop_init_level (location_t loc, int implicit,
9341 struct obstack *braced_init_obstack,
9342 location_t insert_before)
9343 {
9344 struct constructor_stack *p;
9345 struct c_expr ret;
9346 ret.value = NULL_TREE;
9347 ret.original_code = ERROR_MARK;
9348 ret.original_type = NULL;
9349 ret.m_decimal = 0;
9350
9351 if (implicit == 0)
9352 {
9353 /* When we come to an explicit close brace,
9354 pop any inner levels that didn't have explicit braces. */
9355 while (constructor_stack->implicit)
9356 process_init_element (input_location,
9357 pop_init_level (loc, 1, braced_init_obstack,
9358 insert_before),
9359 true, braced_init_obstack);
9360 gcc_assert (!constructor_range_stack);
9361 }
9362 else
9363 if (initializer_stack->missing_brace_richloc)
9364 initializer_stack->missing_brace_richloc->add_fixit_insert_before
9365 (insert_before, "}");
9366
9367 /* Now output all pending elements. */
9368 constructor_incremental = 1;
9369 output_pending_init_elements (1, braced_init_obstack);
9370
9371 p = constructor_stack;
9372
9373 /* Error for initializing a flexible array member, or a zero-length
9374 array member in an inappropriate context. */
9375 if (constructor_type && constructor_fields
9376 && TREE_CODE (constructor_type) == ARRAY_TYPE
9377 && TYPE_DOMAIN (constructor_type)
9378 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
9379 {
9380 /* Silently discard empty initializations. The parser will
9381 already have pedwarned for empty brackets. */
9382 if (integer_zerop (constructor_unfilled_index))
9383 constructor_type = NULL_TREE;
9384 else
9385 {
9386 gcc_assert (!TYPE_SIZE (constructor_type));
9387
9388 if (constructor_depth > 2)
9389 error_init (loc, "initialization of flexible array member in a nested context");
9390 else
9391 pedwarn_init (loc, OPT_Wpedantic,
9392 "initialization of a flexible array member");
9393
9394 /* We have already issued an error message for the existence
9395 of a flexible array member not at the end of the structure.
9396 Discard the initializer so that we do not die later. */
9397 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
9398 constructor_type = NULL_TREE;
9399 }
9400 }
9401
9402 switch (vec_safe_length (constructor_elements))
9403 {
9404 case 0:
9405 /* Initialization with { } counts as zeroinit. */
9406 constructor_zeroinit = 1;
9407 break;
9408 case 1:
9409 /* This might be zeroinit as well. */
9410 if (integer_zerop ((*constructor_elements)[0].value))
9411 constructor_zeroinit = 1;
9412 break;
9413 default:
9414 /* If the constructor has more than one element, it can't be { 0 }. */
9415 constructor_zeroinit = 0;
9416 break;
9417 }
9418
9419 /* Warn when some structs are initialized with direct aggregation. */
9420 if (!implicit && found_missing_braces && warn_missing_braces
9421 && !constructor_zeroinit)
9422 {
9423 gcc_assert (initializer_stack->missing_brace_richloc);
9424 warning_at (initializer_stack->missing_brace_richloc,
9425 OPT_Wmissing_braces,
9426 "missing braces around initializer");
9427 }
9428
9429 /* Warn when some struct elements are implicitly initialized to zero. */
9430 if (warn_missing_field_initializers
9431 && constructor_type
9432 && TREE_CODE (constructor_type) == RECORD_TYPE
9433 && constructor_unfilled_fields)
9434 {
9435 /* Do not warn for flexible array members or zero-length arrays. */
9436 while (constructor_unfilled_fields
9437 && (!DECL_SIZE (constructor_unfilled_fields)
9438 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
9439 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
9440
9441 if (constructor_unfilled_fields
9442 /* Do not warn if this level of the initializer uses member
9443 designators; it is likely to be deliberate. */
9444 && !constructor_designated
9445 /* Do not warn about initializing with { 0 } or with { }. */
9446 && !constructor_zeroinit)
9447 {
9448 if (warning_at (input_location, OPT_Wmissing_field_initializers,
9449 "missing initializer for field %qD of %qT",
9450 constructor_unfilled_fields,
9451 constructor_type))
9452 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9453 "%qD declared here", constructor_unfilled_fields);
9454 }
9455 }
9456
9457 /* Pad out the end of the structure. */
9458 if (p->replacement_value.value)
9459 /* If this closes a superfluous brace pair,
9460 just pass out the element between them. */
9461 ret = p->replacement_value;
9462 else if (constructor_type == NULL_TREE)
9463 ;
9464 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9465 && TREE_CODE (constructor_type) != ARRAY_TYPE
9466 && !gnu_vector_type_p (constructor_type))
9467 {
9468 /* A nonincremental scalar initializer--just return
9469 the element, after verifying there is just one.
9470 Empty scalar initializers are supported in C23. */
9471 if (vec_safe_is_empty (constructor_elements))
9472 {
9473 if (constructor_erroneous || constructor_type == error_mark_node)
9474 ret.value = error_mark_node;
9475 else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
9476 {
9477 error_init (loc, "invalid initializer");
9478 ret.value = error_mark_node;
9479 }
9480 else if (TREE_CODE (constructor_type) == POINTER_TYPE)
9481 /* Ensure this is a null pointer constant in the case of a
9482 'constexpr' object initialized with {}. */
9483 ret.value = build_zero_cst (ptr_type_node);
9484 else
9485 ret.value = build_zero_cst (constructor_type);
9486 }
9487 else if (vec_safe_length (constructor_elements) != 1)
9488 {
9489 error_init (loc, "extra elements in scalar initializer");
9490 ret.value = (*constructor_elements)[0].value;
9491 }
9492 else
9493 ret.value = (*constructor_elements)[0].value;
9494 }
9495 else
9496 {
9497 if (constructor_erroneous)
9498 ret.value = error_mark_node;
9499 else
9500 {
9501 ret.value = build_constructor (constructor_type,
9502 constructor_elements);
9503 if (constructor_constant)
9504 TREE_CONSTANT (ret.value) = 1;
9505 if (constructor_constant && constructor_simple)
9506 TREE_STATIC (ret.value) = 1;
9507 if (constructor_nonconst)
9508 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9509 }
9510 }
9511
9512 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9513 {
9514 if (constructor_nonconst)
9515 ret.original_code = C_MAYBE_CONST_EXPR;
9516 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9517 ret.original_code = ERROR_MARK;
9518 }
9519
9520 constructor_type = p->type;
9521 constructor_fields = p->fields;
9522 constructor_index = p->index;
9523 constructor_max_index = p->max_index;
9524 constructor_unfilled_index = p->unfilled_index;
9525 constructor_unfilled_fields = p->unfilled_fields;
9526 constructor_bit_index = p->bit_index;
9527 constructor_elements = p->elements;
9528 constructor_constant = p->constant;
9529 constructor_simple = p->simple;
9530 constructor_nonconst = p->nonconst;
9531 constructor_erroneous = p->erroneous;
9532 constructor_incremental = p->incremental;
9533 constructor_designated = p->designated;
9534 designator_depth = p->designator_depth;
9535 constructor_pending_elts = p->pending_elts;
9536 constructor_depth = p->depth;
9537 if (!p->implicit)
9538 constructor_range_stack = p->range_stack;
9539 RESTORE_SPELLING_DEPTH (constructor_depth);
9540
9541 constructor_stack = p->next;
9542 XDELETE (p);
9543
9544 if (ret.value == NULL_TREE && constructor_stack == 0)
9545 ret.value = error_mark_node;
9546 return ret;
9547 }
9548
9549 /* Common handling for both array range and field name designators.
9550 ARRAY argument is nonzero for array ranges. Returns false for success. */
9551
9552 static bool
9553 set_designator (location_t loc, bool array,
9554 struct obstack *braced_init_obstack)
9555 {
9556 tree subtype;
9557 enum tree_code subcode;
9558
9559 /* Don't die if an entire brace-pair level is superfluous
9560 in the containing level, or for an erroneous type. */
9561 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9562 return true;
9563
9564 /* If there were errors in this designator list already, bail out
9565 silently. */
9566 if (designator_erroneous)
9567 return true;
9568
9569 /* Likewise for an initializer for a variable-size type. Those are
9570 diagnosed in the parser, except for empty initializer braces. */
9571 if (COMPLETE_TYPE_P (constructor_type)
9572 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9573 return true;
9574
9575 if (!designator_depth)
9576 {
9577 gcc_assert (!constructor_range_stack);
9578
9579 /* Designator list starts at the level of closest explicit
9580 braces. */
9581 while (constructor_stack->implicit)
9582 process_init_element (input_location,
9583 pop_init_level (loc, 1, braced_init_obstack,
9584 last_init_list_comma),
9585 true, braced_init_obstack);
9586 constructor_designated = 1;
9587 return false;
9588 }
9589
9590 switch (TREE_CODE (constructor_type))
9591 {
9592 case RECORD_TYPE:
9593 case UNION_TYPE:
9594 subtype = TREE_TYPE (constructor_fields);
9595 if (subtype != error_mark_node)
9596 subtype = TYPE_MAIN_VARIANT (subtype);
9597 break;
9598 case ARRAY_TYPE:
9599 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9600 break;
9601 default:
9602 gcc_unreachable ();
9603 }
9604
9605 subcode = TREE_CODE (subtype);
9606 if (array && subcode != ARRAY_TYPE)
9607 {
9608 error_init (loc, "array index in non-array initializer");
9609 return true;
9610 }
9611 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9612 {
9613 error_init (loc, "field name not in record or union initializer");
9614 return true;
9615 }
9616
9617 constructor_designated = 1;
9618 finish_implicit_inits (loc, braced_init_obstack);
9619 push_init_level (loc, 2, braced_init_obstack);
9620 return false;
9621 }
9622
9623 /* If there are range designators in designator list, push a new designator
9624 to constructor_range_stack. RANGE_END is end of such stack range or
9625 NULL_TREE if there is no range designator at this level. */
9626
9627 static void
9628 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9629 {
9630 struct constructor_range_stack *p;
9631
9632 p = (struct constructor_range_stack *)
9633 obstack_alloc (braced_init_obstack,
9634 sizeof (struct constructor_range_stack));
9635 p->prev = constructor_range_stack;
9636 p->next = 0;
9637 p->fields = constructor_fields;
9638 p->range_start = constructor_index;
9639 p->index = constructor_index;
9640 p->stack = constructor_stack;
9641 p->range_end = range_end;
9642 if (constructor_range_stack)
9643 constructor_range_stack->next = p;
9644 constructor_range_stack = p;
9645 }
9646
9647 /* Within an array initializer, specify the next index to be initialized.
9648 FIRST is that index. If LAST is nonzero, then initialize a range
9649 of indices, running from FIRST through LAST. */
9650
9651 void
9652 set_init_index (location_t loc, tree first, tree last,
9653 struct obstack *braced_init_obstack)
9654 {
9655 if (set_designator (loc, true, braced_init_obstack))
9656 return;
9657
9658 designator_erroneous = 1;
9659
9660 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9661 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9662 {
9663 error_init (loc, "array index in initializer not of integer type");
9664 return;
9665 }
9666
9667 if (TREE_CODE (first) != INTEGER_CST)
9668 {
9669 first = c_fully_fold (first, false, NULL);
9670 if (TREE_CODE (first) == INTEGER_CST)
9671 pedwarn_init (loc, OPT_Wpedantic,
9672 "array index in initializer is not "
9673 "an integer constant expression");
9674 }
9675
9676 if (last && TREE_CODE (last) != INTEGER_CST)
9677 {
9678 last = c_fully_fold (last, false, NULL);
9679 if (TREE_CODE (last) == INTEGER_CST)
9680 pedwarn_init (loc, OPT_Wpedantic,
9681 "array index in initializer is not "
9682 "an integer constant expression");
9683 }
9684
9685 if (TREE_CODE (first) != INTEGER_CST)
9686 error_init (loc, "nonconstant array index in initializer");
9687 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9688 error_init (loc, "nonconstant array index in initializer");
9689 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9690 error_init (loc, "array index in non-array initializer");
9691 else if (tree_int_cst_sgn (first) == -1)
9692 error_init (loc, "array index in initializer exceeds array bounds");
9693 else if (constructor_max_index
9694 && tree_int_cst_lt (constructor_max_index, first))
9695 error_init (loc, "array index in initializer exceeds array bounds");
9696 else
9697 {
9698 constant_expression_warning (first);
9699 if (last)
9700 constant_expression_warning (last);
9701 constructor_index = convert (bitsizetype, first);
9702 if (tree_int_cst_lt (constructor_index, first))
9703 {
9704 constructor_index = copy_node (constructor_index);
9705 TREE_OVERFLOW (constructor_index) = 1;
9706 }
9707
9708 if (last)
9709 {
9710 if (tree_int_cst_equal (first, last))
9711 last = NULL_TREE;
9712 else if (tree_int_cst_lt (last, first))
9713 {
9714 error_init (loc, "empty index range in initializer");
9715 last = NULL_TREE;
9716 }
9717 else
9718 {
9719 last = convert (bitsizetype, last);
9720 if (constructor_max_index != NULL_TREE
9721 && tree_int_cst_lt (constructor_max_index, last))
9722 {
9723 error_init (loc, "array index range in initializer exceeds "
9724 "array bounds");
9725 last = NULL_TREE;
9726 }
9727 }
9728 }
9729
9730 designator_depth++;
9731 designator_erroneous = 0;
9732 if (constructor_range_stack || last)
9733 push_range_stack (last, braced_init_obstack);
9734 }
9735 }
9736
9737 /* Within a struct initializer, specify the next field to be initialized. */
9738
9739 void
9740 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9741 struct obstack *braced_init_obstack)
9742 {
9743 tree field;
9744
9745 if (set_designator (loc, false, braced_init_obstack))
9746 return;
9747
9748 designator_erroneous = 1;
9749
9750 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9751 {
9752 error_init (loc, "field name not in record or union initializer");
9753 return;
9754 }
9755
9756 field = lookup_field (constructor_type, fieldname);
9757
9758 if (field == NULL_TREE)
9759 {
9760 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9761 if (guessed_id)
9762 {
9763 gcc_rich_location rich_loc (fieldname_loc);
9764 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9765 error_at (&rich_loc,
9766 "%qT has no member named %qE; did you mean %qE?",
9767 constructor_type, fieldname, guessed_id);
9768 }
9769 else
9770 error_at (fieldname_loc, "%qT has no member named %qE",
9771 constructor_type, fieldname);
9772 }
9773 else
9774 do
9775 {
9776 constructor_fields = TREE_VALUE (field);
9777 designator_depth++;
9778 designator_erroneous = 0;
9779 if (constructor_range_stack)
9780 push_range_stack (NULL_TREE, braced_init_obstack);
9781 field = TREE_CHAIN (field);
9782 if (field)
9783 {
9784 if (set_designator (loc, false, braced_init_obstack))
9785 return;
9786 }
9787 }
9788 while (field != NULL_TREE);
9789 }
9790 \f
9791 /* Add a new initializer to the tree of pending initializers. PURPOSE
9792 identifies the initializer, either array index or field in a structure.
9793 VALUE is the value of that index or field. If ORIGTYPE is not
9794 NULL_TREE, it is the original type of VALUE.
9795
9796 IMPLICIT is true if value comes from pop_init_level (1),
9797 the new initializer has been merged with the existing one
9798 and thus no warnings should be emitted about overriding an
9799 existing initializer. */
9800
9801 static void
9802 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9803 bool implicit, struct obstack *braced_init_obstack)
9804 {
9805 struct init_node *p, **q, *r;
9806
9807 q = &constructor_pending_elts;
9808 p = 0;
9809
9810 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9811 {
9812 while (*q != 0)
9813 {
9814 p = *q;
9815 if (tree_int_cst_lt (purpose, p->purpose))
9816 q = &p->left;
9817 else if (tree_int_cst_lt (p->purpose, purpose))
9818 q = &p->right;
9819 else
9820 {
9821 if (!implicit)
9822 {
9823 if (TREE_SIDE_EFFECTS (p->value))
9824 warning_init (loc, OPT_Woverride_init_side_effects,
9825 "initialized field with side-effects "
9826 "overwritten");
9827 else if (warn_override_init)
9828 warning_init (loc, OPT_Woverride_init,
9829 "initialized field overwritten");
9830 }
9831 p->value = value;
9832 p->origtype = origtype;
9833 return;
9834 }
9835 }
9836 }
9837 else
9838 {
9839 tree bitpos;
9840
9841 bitpos = bit_position (purpose);
9842 while (*q != NULL)
9843 {
9844 p = *q;
9845 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9846 q = &p->left;
9847 else if (p->purpose != purpose)
9848 q = &p->right;
9849 else
9850 {
9851 if (!implicit)
9852 {
9853 if (TREE_SIDE_EFFECTS (p->value))
9854 warning_init (loc, OPT_Woverride_init_side_effects,
9855 "initialized field with side-effects "
9856 "overwritten");
9857 else if (warn_override_init)
9858 warning_init (loc, OPT_Woverride_init,
9859 "initialized field overwritten");
9860 }
9861 p->value = value;
9862 p->origtype = origtype;
9863 return;
9864 }
9865 }
9866 }
9867
9868 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9869 sizeof (struct init_node));
9870 r->purpose = purpose;
9871 r->value = value;
9872 r->origtype = origtype;
9873
9874 *q = r;
9875 r->parent = p;
9876 r->left = 0;
9877 r->right = 0;
9878 r->balance = 0;
9879
9880 while (p)
9881 {
9882 struct init_node *s;
9883
9884 if (r == p->left)
9885 {
9886 if (p->balance == 0)
9887 p->balance = -1;
9888 else if (p->balance < 0)
9889 {
9890 if (r->balance < 0)
9891 {
9892 /* L rotation. */
9893 p->left = r->right;
9894 if (p->left)
9895 p->left->parent = p;
9896 r->right = p;
9897
9898 p->balance = 0;
9899 r->balance = 0;
9900
9901 s = p->parent;
9902 p->parent = r;
9903 r->parent = s;
9904 if (s)
9905 {
9906 if (s->left == p)
9907 s->left = r;
9908 else
9909 s->right = r;
9910 }
9911 else
9912 constructor_pending_elts = r;
9913 }
9914 else
9915 {
9916 /* LR rotation. */
9917 struct init_node *t = r->right;
9918
9919 r->right = t->left;
9920 if (r->right)
9921 r->right->parent = r;
9922 t->left = r;
9923
9924 p->left = t->right;
9925 if (p->left)
9926 p->left->parent = p;
9927 t->right = p;
9928
9929 p->balance = t->balance < 0;
9930 r->balance = -(t->balance > 0);
9931 t->balance = 0;
9932
9933 s = p->parent;
9934 p->parent = t;
9935 r->parent = t;
9936 t->parent = s;
9937 if (s)
9938 {
9939 if (s->left == p)
9940 s->left = t;
9941 else
9942 s->right = t;
9943 }
9944 else
9945 constructor_pending_elts = t;
9946 }
9947 break;
9948 }
9949 else
9950 {
9951 /* p->balance == +1; growth of left side balances the node. */
9952 p->balance = 0;
9953 break;
9954 }
9955 }
9956 else /* r == p->right */
9957 {
9958 if (p->balance == 0)
9959 /* Growth propagation from right side. */
9960 p->balance++;
9961 else if (p->balance > 0)
9962 {
9963 if (r->balance > 0)
9964 {
9965 /* R rotation. */
9966 p->right = r->left;
9967 if (p->right)
9968 p->right->parent = p;
9969 r->left = p;
9970
9971 p->balance = 0;
9972 r->balance = 0;
9973
9974 s = p->parent;
9975 p->parent = r;
9976 r->parent = s;
9977 if (s)
9978 {
9979 if (s->left == p)
9980 s->left = r;
9981 else
9982 s->right = r;
9983 }
9984 else
9985 constructor_pending_elts = r;
9986 }
9987 else /* r->balance == -1 */
9988 {
9989 /* RL rotation */
9990 struct init_node *t = r->left;
9991
9992 r->left = t->right;
9993 if (r->left)
9994 r->left->parent = r;
9995 t->right = r;
9996
9997 p->right = t->left;
9998 if (p->right)
9999 p->right->parent = p;
10000 t->left = p;
10001
10002 r->balance = (t->balance < 0);
10003 p->balance = -(t->balance > 0);
10004 t->balance = 0;
10005
10006 s = p->parent;
10007 p->parent = t;
10008 r->parent = t;
10009 t->parent = s;
10010 if (s)
10011 {
10012 if (s->left == p)
10013 s->left = t;
10014 else
10015 s->right = t;
10016 }
10017 else
10018 constructor_pending_elts = t;
10019 }
10020 break;
10021 }
10022 else
10023 {
10024 /* p->balance == -1; growth of right side balances the node. */
10025 p->balance = 0;
10026 break;
10027 }
10028 }
10029
10030 r = p;
10031 p = p->parent;
10032 }
10033 }
10034
10035 /* Build AVL tree from a sorted chain. */
10036
10037 static void
10038 set_nonincremental_init (struct obstack * braced_init_obstack)
10039 {
10040 unsigned HOST_WIDE_INT ix;
10041 tree index, value;
10042
10043 if (TREE_CODE (constructor_type) != RECORD_TYPE
10044 && TREE_CODE (constructor_type) != ARRAY_TYPE)
10045 return;
10046
10047 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
10048 add_pending_init (input_location, index, value, NULL_TREE, true,
10049 braced_init_obstack);
10050 constructor_elements = NULL;
10051 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10052 {
10053 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
10054 /* Skip any nameless bit fields at the beginning. */
10055 while (constructor_unfilled_fields != NULL_TREE
10056 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
10057 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
10058
10059 }
10060 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10061 {
10062 if (TYPE_DOMAIN (constructor_type))
10063 constructor_unfilled_index
10064 = convert (bitsizetype,
10065 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10066 else
10067 constructor_unfilled_index = bitsize_zero_node;
10068 }
10069 constructor_incremental = 0;
10070 }
10071
10072 /* Build AVL tree from a string constant. */
10073
10074 static void
10075 set_nonincremental_init_from_string (tree str,
10076 struct obstack * braced_init_obstack)
10077 {
10078 tree value, purpose, type;
10079 HOST_WIDE_INT val[2];
10080 const char *p, *end;
10081 int byte, wchar_bytes, charwidth, bitpos;
10082
10083 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
10084
10085 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
10086 charwidth = TYPE_PRECISION (char_type_node);
10087 gcc_assert ((size_t) wchar_bytes * charwidth
10088 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
10089 type = TREE_TYPE (constructor_type);
10090 p = TREE_STRING_POINTER (str);
10091 end = p + TREE_STRING_LENGTH (str);
10092
10093 for (purpose = bitsize_zero_node;
10094 p < end
10095 && !(constructor_max_index
10096 && tree_int_cst_lt (constructor_max_index, purpose));
10097 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
10098 {
10099 if (wchar_bytes == 1)
10100 {
10101 val[0] = (unsigned char) *p++;
10102 val[1] = 0;
10103 }
10104 else
10105 {
10106 val[1] = 0;
10107 val[0] = 0;
10108 for (byte = 0; byte < wchar_bytes; byte++)
10109 {
10110 if (BYTES_BIG_ENDIAN)
10111 bitpos = (wchar_bytes - byte - 1) * charwidth;
10112 else
10113 bitpos = byte * charwidth;
10114 val[bitpos / HOST_BITS_PER_WIDE_INT]
10115 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
10116 << (bitpos % HOST_BITS_PER_WIDE_INT);
10117 }
10118 }
10119
10120 if (!TYPE_UNSIGNED (type))
10121 {
10122 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
10123 if (bitpos < HOST_BITS_PER_WIDE_INT)
10124 {
10125 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
10126 {
10127 val[0] |= HOST_WIDE_INT_M1U << bitpos;
10128 val[1] = -1;
10129 }
10130 }
10131 else if (bitpos == HOST_BITS_PER_WIDE_INT)
10132 {
10133 if (val[0] < 0)
10134 val[1] = -1;
10135 }
10136 else if (val[1] & (HOST_WIDE_INT_1
10137 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
10138 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
10139 }
10140
10141 value = wide_int_to_tree (type,
10142 wide_int::from_array (val, 2,
10143 HOST_BITS_PER_WIDE_INT * 2));
10144 add_pending_init (input_location, purpose, value, NULL_TREE, true,
10145 braced_init_obstack);
10146 }
10147
10148 constructor_incremental = 0;
10149 }
10150
10151 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
10152 not initialized yet. */
10153
10154 static tree
10155 find_init_member (tree field, struct obstack * braced_init_obstack)
10156 {
10157 struct init_node *p;
10158
10159 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10160 {
10161 if (constructor_incremental
10162 && tree_int_cst_lt (field, constructor_unfilled_index))
10163 set_nonincremental_init (braced_init_obstack);
10164
10165 p = constructor_pending_elts;
10166 while (p)
10167 {
10168 if (tree_int_cst_lt (field, p->purpose))
10169 p = p->left;
10170 else if (tree_int_cst_lt (p->purpose, field))
10171 p = p->right;
10172 else
10173 return p->value;
10174 }
10175 }
10176 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10177 {
10178 tree bitpos = bit_position (field);
10179
10180 if (constructor_incremental
10181 && (!constructor_unfilled_fields
10182 || tree_int_cst_lt (bitpos,
10183 bit_position (constructor_unfilled_fields))))
10184 set_nonincremental_init (braced_init_obstack);
10185
10186 p = constructor_pending_elts;
10187 while (p)
10188 {
10189 if (field == p->purpose)
10190 return p->value;
10191 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
10192 p = p->left;
10193 else
10194 p = p->right;
10195 }
10196 }
10197 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10198 {
10199 if (!vec_safe_is_empty (constructor_elements)
10200 && (constructor_elements->last ().index == field))
10201 return constructor_elements->last ().value;
10202 }
10203 return NULL_TREE;
10204 }
10205
10206 /* "Output" the next constructor element.
10207 At top level, really output it to assembler code now.
10208 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
10209 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
10210 TYPE is the data type that the containing data type wants here.
10211 FIELD is the field (a FIELD_DECL) or the index that this element fills.
10212 If VALUE is a string constant, STRICT_STRING is true if it is
10213 unparenthesized or we should not warn here for it being parenthesized.
10214 For other types of VALUE, STRICT_STRING is not used.
10215
10216 PENDING if true means output pending elements that belong
10217 right after this element. (PENDING is normally true;
10218 it is false while outputting pending elements, to avoid recursion.)
10219
10220 IMPLICIT is true if value comes from pop_init_level (1),
10221 the new initializer has been merged with the existing one
10222 and thus no warnings should be emitted about overriding an
10223 existing initializer. */
10224
10225 static void
10226 output_init_element (location_t loc, tree value, tree origtype,
10227 bool strict_string, tree type, tree field, bool pending,
10228 bool implicit, struct obstack * braced_init_obstack)
10229 {
10230 tree semantic_type = NULL_TREE;
10231 bool maybe_const = true;
10232 bool npc, int_const_expr, arith_const_expr;
10233
10234 if (type == error_mark_node || value == error_mark_node)
10235 {
10236 constructor_erroneous = 1;
10237 return;
10238 }
10239 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
10240 && (TREE_CODE (value) == STRING_CST
10241 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
10242 && !(TREE_CODE (value) == STRING_CST
10243 && TREE_CODE (type) == ARRAY_TYPE
10244 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
10245 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
10246 TYPE_MAIN_VARIANT (type)))
10247 value = array_to_pointer_conversion (input_location, value);
10248
10249 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
10250 && require_constant_value && pending)
10251 {
10252 /* As an extension, allow initializing objects with static storage
10253 duration with compound literals (which are then treated just as
10254 the brace enclosed list they contain). */
10255 if (flag_isoc99)
10256 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
10257 "constant");
10258 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
10259 value = DECL_INITIAL (decl);
10260 }
10261
10262 npc = null_pointer_constant_p (value);
10263 int_const_expr = (TREE_CODE (value) == INTEGER_CST
10264 && !TREE_OVERFLOW (value)
10265 && INTEGRAL_TYPE_P (TREE_TYPE (value)));
10266 /* Not fully determined before folding. */
10267 arith_const_expr = true;
10268 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
10269 {
10270 semantic_type = TREE_TYPE (value);
10271 value = TREE_OPERAND (value, 0);
10272 }
10273 value = c_fully_fold (value, require_constant_value, &maybe_const);
10274 /* TODO: this may not detect all cases of expressions folding to
10275 constants that are not arithmetic constant expressions. */
10276 if (!maybe_const)
10277 arith_const_expr = false;
10278 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
10279 && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
10280 && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
10281 arith_const_expr = false;
10282 else if (TREE_CODE (value) != INTEGER_CST
10283 && TREE_CODE (value) != REAL_CST
10284 && TREE_CODE (value) != COMPLEX_CST)
10285 arith_const_expr = false;
10286 else if (TREE_OVERFLOW (value))
10287 arith_const_expr = false;
10288
10289 if (value == error_mark_node)
10290 constructor_erroneous = 1;
10291 else if (!TREE_CONSTANT (value))
10292 constructor_constant = 0;
10293 else if (!initializer_constant_valid_p (value,
10294 TREE_TYPE (value),
10295 AGGREGATE_TYPE_P (constructor_type)
10296 && TYPE_REVERSE_STORAGE_ORDER
10297 (constructor_type))
10298 || (RECORD_OR_UNION_TYPE_P (constructor_type)
10299 && DECL_C_BIT_FIELD (field)
10300 && TREE_CODE (value) != INTEGER_CST))
10301 constructor_simple = 0;
10302 if (!maybe_const)
10303 constructor_nonconst = 1;
10304
10305 /* Digest the initializer and issue any errors about incompatible
10306 types before issuing errors about non-constant initializers. */
10307 tree new_value = value;
10308 if (semantic_type)
10309 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
10310 /* In the case of braces around a scalar initializer, the result of
10311 this initializer processing goes through digest_init again at the
10312 outer level. In the case of a constexpr initializer for a
10313 pointer, avoid converting a null pointer constant to something
10314 that is not a null pointer constant to avoid a spurious error
10315 from that second processing. */
10316 if (!require_constexpr_value
10317 || !npc
10318 || TREE_CODE (constructor_type) != POINTER_TYPE)
10319 new_value = digest_init (loc, type, new_value, origtype, npc,
10320 int_const_expr, arith_const_expr, strict_string,
10321 require_constant_value, require_constexpr_value);
10322 if (new_value == error_mark_node)
10323 {
10324 constructor_erroneous = 1;
10325 return;
10326 }
10327 if (require_constant_value || require_constant_elements)
10328 constant_expression_warning (new_value);
10329
10330 /* Proceed to check the constness of the original initializer. */
10331 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
10332 {
10333 if (require_constant_value)
10334 {
10335 error_init (loc, "initializer element is not constant");
10336 value = error_mark_node;
10337 }
10338 else if (require_constant_elements)
10339 pedwarn (loc, OPT_Wpedantic,
10340 "initializer element is not computable at load time");
10341 }
10342 else if (!maybe_const
10343 && (require_constant_value || require_constant_elements))
10344 pedwarn_init (loc, OPT_Wpedantic,
10345 "initializer element is not a constant expression");
10346 /* digest_init has already carried out the additional checks
10347 required for 'constexpr' initializers (using the information
10348 passed to it about whether the original initializer was certain
10349 kinds of constant expression), so that check does not need to be
10350 repeated here. */
10351
10352 /* Issue -Wc++-compat warnings about initializing a bitfield with
10353 enum type. */
10354 if (warn_cxx_compat
10355 && field != NULL_TREE
10356 && TREE_CODE (field) == FIELD_DECL
10357 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
10358 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
10359 != TYPE_MAIN_VARIANT (type))
10360 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
10361 {
10362 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
10363 if (checktype != error_mark_node
10364 && (TYPE_MAIN_VARIANT (checktype)
10365 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
10366 warning_init (loc, OPT_Wc___compat,
10367 "enum conversion in initialization is invalid in C++");
10368 }
10369
10370 /* If this field is empty and does not have side effects (and is not at
10371 the end of structure), don't do anything other than checking the
10372 initializer. */
10373 if (field
10374 && (TREE_TYPE (field) == error_mark_node
10375 || (COMPLETE_TYPE_P (TREE_TYPE (field))
10376 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
10377 && !TREE_SIDE_EFFECTS (new_value)
10378 && (TREE_CODE (constructor_type) == ARRAY_TYPE
10379 || DECL_CHAIN (field)))))
10380 return;
10381
10382 /* Finally, set VALUE to the initializer value digested above. */
10383 value = new_value;
10384
10385 /* If this element doesn't come next in sequence,
10386 put it on constructor_pending_elts. */
10387 if (TREE_CODE (constructor_type) == ARRAY_TYPE
10388 && (!constructor_incremental
10389 || !tree_int_cst_equal (field, constructor_unfilled_index)))
10390 {
10391 if (constructor_incremental
10392 && tree_int_cst_lt (field, constructor_unfilled_index))
10393 set_nonincremental_init (braced_init_obstack);
10394
10395 add_pending_init (loc, field, value, origtype, implicit,
10396 braced_init_obstack);
10397 return;
10398 }
10399 else if (TREE_CODE (constructor_type) == RECORD_TYPE
10400 && (!constructor_incremental
10401 || field != constructor_unfilled_fields))
10402 {
10403 /* We do this for records but not for unions. In a union,
10404 no matter which field is specified, it can be initialized
10405 right away since it starts at the beginning of the union. */
10406 if (constructor_incremental)
10407 {
10408 if (!constructor_unfilled_fields)
10409 set_nonincremental_init (braced_init_obstack);
10410 else
10411 {
10412 tree bitpos, unfillpos;
10413
10414 bitpos = bit_position (field);
10415 unfillpos = bit_position (constructor_unfilled_fields);
10416
10417 if (tree_int_cst_lt (bitpos, unfillpos))
10418 set_nonincremental_init (braced_init_obstack);
10419 }
10420 }
10421
10422 add_pending_init (loc, field, value, origtype, implicit,
10423 braced_init_obstack);
10424 return;
10425 }
10426 else if (TREE_CODE (constructor_type) == UNION_TYPE
10427 && !vec_safe_is_empty (constructor_elements))
10428 {
10429 if (!implicit)
10430 {
10431 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
10432 warning_init (loc, OPT_Woverride_init_side_effects,
10433 "initialized field with side-effects overwritten");
10434 else if (warn_override_init)
10435 warning_init (loc, OPT_Woverride_init,
10436 "initialized field overwritten");
10437 }
10438
10439 /* We can have just one union field set. */
10440 constructor_elements = NULL;
10441 }
10442
10443 /* Otherwise, output this element either to
10444 constructor_elements or to the assembler file. */
10445
10446 constructor_elt celt = {field, value};
10447 vec_safe_push (constructor_elements, celt);
10448
10449 /* Advance the variable that indicates sequential elements output. */
10450 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10451 constructor_unfilled_index
10452 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
10453 bitsize_one_node);
10454 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10455 {
10456 constructor_unfilled_fields
10457 = DECL_CHAIN (constructor_unfilled_fields);
10458
10459 /* Skip any nameless bit fields. */
10460 while (constructor_unfilled_fields != NULL_TREE
10461 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
10462 constructor_unfilled_fields =
10463 DECL_CHAIN (constructor_unfilled_fields);
10464 }
10465 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10466 constructor_unfilled_fields = NULL_TREE;
10467
10468 /* Now output any pending elements which have become next. */
10469 if (pending)
10470 output_pending_init_elements (0, braced_init_obstack);
10471 }
10472
10473 /* For two FIELD_DECLs in the same chain, return -1 if field1
10474 comes before field2, 1 if field1 comes after field2 and
10475 0 if field1 == field2. */
10476
10477 static int
10478 init_field_decl_cmp (tree field1, tree field2)
10479 {
10480 if (field1 == field2)
10481 return 0;
10482
10483 tree bitpos1 = bit_position (field1);
10484 tree bitpos2 = bit_position (field2);
10485 if (tree_int_cst_equal (bitpos1, bitpos2))
10486 {
10487 /* If one of the fields has non-zero bitsize, then that
10488 field must be the last one in a sequence of zero
10489 sized fields, fields after it will have bigger
10490 bit_position. */
10491 if (TREE_TYPE (field1) != error_mark_node
10492 && COMPLETE_TYPE_P (TREE_TYPE (field1))
10493 && integer_nonzerop (TREE_TYPE (field1)))
10494 return 1;
10495 if (TREE_TYPE (field2) != error_mark_node
10496 && COMPLETE_TYPE_P (TREE_TYPE (field2))
10497 && integer_nonzerop (TREE_TYPE (field2)))
10498 return -1;
10499 /* Otherwise, fallback to DECL_CHAIN walk to find out
10500 which field comes earlier. Walk chains of both
10501 fields, so that if field1 and field2 are close to each
10502 other in either order, it is found soon even for large
10503 sequences of zero sized fields. */
10504 tree f1 = field1, f2 = field2;
10505 while (1)
10506 {
10507 f1 = DECL_CHAIN (f1);
10508 f2 = DECL_CHAIN (f2);
10509 if (f1 == NULL_TREE)
10510 {
10511 gcc_assert (f2);
10512 return 1;
10513 }
10514 if (f2 == NULL_TREE)
10515 return -1;
10516 if (f1 == field2)
10517 return -1;
10518 if (f2 == field1)
10519 return 1;
10520 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10521 return 1;
10522 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10523 return -1;
10524 }
10525 }
10526 else if (tree_int_cst_lt (bitpos1, bitpos2))
10527 return -1;
10528 else
10529 return 1;
10530 }
10531
10532 /* Output any pending elements which have become next.
10533 As we output elements, constructor_unfilled_{fields,index}
10534 advances, which may cause other elements to become next;
10535 if so, they too are output.
10536
10537 If ALL is 0, we return when there are
10538 no more pending elements to output now.
10539
10540 If ALL is 1, we output space as necessary so that
10541 we can output all the pending elements. */
10542 static void
10543 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10544 {
10545 struct init_node *elt = constructor_pending_elts;
10546 tree next;
10547
10548 retry:
10549
10550 /* Look through the whole pending tree.
10551 If we find an element that should be output now,
10552 output it. Otherwise, set NEXT to the element
10553 that comes first among those still pending. */
10554
10555 next = NULL_TREE;
10556 while (elt)
10557 {
10558 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10559 {
10560 if (tree_int_cst_equal (elt->purpose,
10561 constructor_unfilled_index))
10562 output_init_element (input_location, elt->value, elt->origtype,
10563 true, TREE_TYPE (constructor_type),
10564 constructor_unfilled_index, false, false,
10565 braced_init_obstack);
10566 else if (tree_int_cst_lt (constructor_unfilled_index,
10567 elt->purpose))
10568 {
10569 /* Advance to the next smaller node. */
10570 if (elt->left)
10571 elt = elt->left;
10572 else
10573 {
10574 /* We have reached the smallest node bigger than the
10575 current unfilled index. Fill the space first. */
10576 next = elt->purpose;
10577 break;
10578 }
10579 }
10580 else
10581 {
10582 /* Advance to the next bigger node. */
10583 if (elt->right)
10584 elt = elt->right;
10585 else
10586 {
10587 /* We have reached the biggest node in a subtree. Find
10588 the parent of it, which is the next bigger node. */
10589 while (elt->parent && elt->parent->right == elt)
10590 elt = elt->parent;
10591 elt = elt->parent;
10592 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10593 elt->purpose))
10594 {
10595 next = elt->purpose;
10596 break;
10597 }
10598 }
10599 }
10600 }
10601 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10602 {
10603 /* If the current record is complete we are done. */
10604 if (constructor_unfilled_fields == NULL_TREE)
10605 break;
10606
10607 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10608 elt->purpose);
10609 if (cmp == 0)
10610 output_init_element (input_location, elt->value, elt->origtype,
10611 true, TREE_TYPE (elt->purpose),
10612 elt->purpose, false, false,
10613 braced_init_obstack);
10614 else if (cmp < 0)
10615 {
10616 /* Advance to the next smaller node. */
10617 if (elt->left)
10618 elt = elt->left;
10619 else
10620 {
10621 /* We have reached the smallest node bigger than the
10622 current unfilled field. Fill the space first. */
10623 next = elt->purpose;
10624 break;
10625 }
10626 }
10627 else
10628 {
10629 /* Advance to the next bigger node. */
10630 if (elt->right)
10631 elt = elt->right;
10632 else
10633 {
10634 /* We have reached the biggest node in a subtree. Find
10635 the parent of it, which is the next bigger node. */
10636 while (elt->parent && elt->parent->right == elt)
10637 elt = elt->parent;
10638 elt = elt->parent;
10639 if (elt
10640 && init_field_decl_cmp (constructor_unfilled_fields,
10641 elt->purpose) < 0)
10642 {
10643 next = elt->purpose;
10644 break;
10645 }
10646 }
10647 }
10648 }
10649 }
10650
10651 /* Ordinarily return, but not if we want to output all
10652 and there are elements left. */
10653 if (!(all && next != NULL_TREE))
10654 return;
10655
10656 /* If it's not incremental, just skip over the gap, so that after
10657 jumping to retry we will output the next successive element. */
10658 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10659 constructor_unfilled_fields = next;
10660 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10661 constructor_unfilled_index = next;
10662
10663 /* ELT now points to the node in the pending tree with the next
10664 initializer to output. */
10665 goto retry;
10666 }
10667 \f
10668 /* Expression VALUE coincides with the start of type TYPE in a braced
10669 initializer. Return true if we should treat VALUE as initializing
10670 the first element of TYPE, false if we should treat it as initializing
10671 TYPE as a whole.
10672
10673 If the initializer is clearly invalid, the question becomes:
10674 which choice gives the best error message? */
10675
10676 static bool
10677 initialize_elementwise_p (tree type, tree value)
10678 {
10679 if (type == error_mark_node || value == error_mark_node)
10680 return false;
10681
10682 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10683
10684 tree value_type = TREE_TYPE (value);
10685 if (value_type == error_mark_node)
10686 return false;
10687
10688 /* GNU vectors can be initialized elementwise. However, treat any
10689 kind of vector value as initializing the vector type as a whole,
10690 regardless of whether the value is a GNU vector. Such initializers
10691 are valid if and only if they would have been valid in a non-braced
10692 initializer like:
10693
10694 TYPE foo = VALUE;
10695
10696 so recursing into the vector type would be at best confusing or at
10697 worst wrong. For example, when -flax-vector-conversions is in effect,
10698 it's possible to initialize a V8HI from a V4SI, even though the vectors
10699 have different element types and different numbers of elements. */
10700 if (gnu_vector_type_p (type))
10701 return !VECTOR_TYPE_P (value_type);
10702
10703 if (AGGREGATE_TYPE_P (type))
10704 return !comptypes (type, TYPE_MAIN_VARIANT (value_type));
10705
10706 return false;
10707 }
10708
10709 /* Add one non-braced element to the current constructor level.
10710 This adjusts the current position within the constructor's type.
10711 This may also start or terminate implicit levels
10712 to handle a partly-braced initializer.
10713
10714 Once this has found the correct level for the new element,
10715 it calls output_init_element.
10716
10717 IMPLICIT is true if value comes from pop_init_level (1),
10718 the new initializer has been merged with the existing one
10719 and thus no warnings should be emitted about overriding an
10720 existing initializer. */
10721
10722 void
10723 process_init_element (location_t loc, struct c_expr value, bool implicit,
10724 struct obstack * braced_init_obstack)
10725 {
10726 tree orig_value = value.value;
10727 int string_flag
10728 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10729 bool strict_string = value.original_code == STRING_CST;
10730 bool was_designated = designator_depth != 0;
10731
10732 designator_depth = 0;
10733 designator_erroneous = 0;
10734
10735 if (!implicit && value.value && !integer_zerop (value.value))
10736 constructor_zeroinit = 0;
10737
10738 /* Handle superfluous braces around string cst as in
10739 char x[] = {"foo"}; */
10740 if (constructor_type
10741 && !was_designated
10742 && TREE_CODE (constructor_type) == ARRAY_TYPE
10743 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10744 && integer_zerop (constructor_unfilled_index))
10745 {
10746 if (constructor_stack->replacement_value.value)
10747 {
10748 error_init (loc, "excess elements in %qT initializer", constructor_type);
10749 return;
10750 }
10751 else if (string_flag)
10752 {
10753 constructor_stack->replacement_value = value;
10754 return;
10755 }
10756 }
10757
10758 if (constructor_stack->replacement_value.value != NULL_TREE)
10759 {
10760 error_init (loc, "excess elements in struct initializer");
10761 return;
10762 }
10763
10764 /* Ignore elements of a brace group if it is entirely superfluous
10765 and has already been diagnosed, or if the type is erroneous. */
10766 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10767 return;
10768
10769 /* Ignore elements of an initializer for a variable-size type.
10770 Those are diagnosed in the parser (empty initializer braces are OK). */
10771 if (COMPLETE_TYPE_P (constructor_type)
10772 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10773 return;
10774
10775 if (!implicit && warn_designated_init && !was_designated
10776 && TREE_CODE (constructor_type) == RECORD_TYPE
10777 && lookup_attribute ("designated_init",
10778 TYPE_ATTRIBUTES (constructor_type)))
10779 warning_init (loc,
10780 OPT_Wdesignated_init,
10781 "positional initialization of field "
10782 "in %<struct%> declared with %<designated_init%> attribute");
10783
10784 /* If we've exhausted any levels that didn't have braces,
10785 pop them now. */
10786 while (constructor_stack->implicit)
10787 {
10788 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10789 && constructor_fields == NULL_TREE)
10790 process_init_element (loc,
10791 pop_init_level (loc, 1, braced_init_obstack,
10792 last_init_list_comma),
10793 true, braced_init_obstack);
10794 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10795 || gnu_vector_type_p (constructor_type))
10796 && constructor_max_index
10797 && tree_int_cst_lt (constructor_max_index,
10798 constructor_index))
10799 process_init_element (loc,
10800 pop_init_level (loc, 1, braced_init_obstack,
10801 last_init_list_comma),
10802 true, braced_init_obstack);
10803 else
10804 break;
10805 }
10806
10807 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10808 if (constructor_range_stack)
10809 {
10810 /* If value is a compound literal and we'll be just using its
10811 content, don't put it into a SAVE_EXPR. */
10812 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10813 || !require_constant_value)
10814 {
10815 tree semantic_type = NULL_TREE;
10816 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10817 {
10818 semantic_type = TREE_TYPE (value.value);
10819 value.value = TREE_OPERAND (value.value, 0);
10820 }
10821 value.value = save_expr (value.value);
10822 if (semantic_type)
10823 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10824 value.value);
10825 }
10826 }
10827
10828 while (1)
10829 {
10830 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10831 {
10832 tree fieldtype;
10833 enum tree_code fieldcode;
10834
10835 if (constructor_fields == NULL_TREE)
10836 {
10837 pedwarn_init (loc, 0, "excess elements in struct initializer");
10838 break;
10839 }
10840
10841 fieldtype = TREE_TYPE (constructor_fields);
10842 if (fieldtype != error_mark_node)
10843 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10844 fieldcode = TREE_CODE (fieldtype);
10845
10846 /* Error for non-static initialization of a flexible array member. */
10847 if (fieldcode == ARRAY_TYPE
10848 && !require_constant_value
10849 && TYPE_SIZE (fieldtype) == NULL_TREE
10850 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10851 {
10852 error_init (loc, "non-static initialization of a flexible "
10853 "array member");
10854 break;
10855 }
10856
10857 /* Error for initialization of a flexible array member with
10858 a string constant if the structure is in an array. E.g.:
10859 struct S { int x; char y[]; };
10860 struct S s[] = { { 1, "foo" } };
10861 is invalid. */
10862 if (string_flag
10863 && fieldcode == ARRAY_TYPE
10864 && constructor_depth > 1
10865 && TYPE_SIZE (fieldtype) == NULL_TREE
10866 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10867 {
10868 bool in_array_p = false;
10869 for (struct constructor_stack *p = constructor_stack;
10870 p && p->type; p = p->next)
10871 if (TREE_CODE (p->type) == ARRAY_TYPE)
10872 {
10873 in_array_p = true;
10874 break;
10875 }
10876 if (in_array_p)
10877 {
10878 error_init (loc, "initialization of flexible array "
10879 "member in a nested context");
10880 break;
10881 }
10882 }
10883
10884 /* Accept a string constant to initialize a subarray. */
10885 if (value.value != NULL_TREE
10886 && fieldcode == ARRAY_TYPE
10887 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10888 && string_flag)
10889 value.value = orig_value;
10890 /* Otherwise, if we have come to a subaggregate,
10891 and we don't have an element of its type, push into it. */
10892 else if (value.value != NULL_TREE
10893 && initialize_elementwise_p (fieldtype, value.value))
10894 {
10895 push_init_level (loc, 1, braced_init_obstack);
10896 continue;
10897 }
10898
10899 if (value.value)
10900 {
10901 push_member_name (constructor_fields);
10902 output_init_element (loc, value.value, value.original_type,
10903 strict_string, fieldtype,
10904 constructor_fields, true, implicit,
10905 braced_init_obstack);
10906 RESTORE_SPELLING_DEPTH (constructor_depth);
10907 }
10908 else
10909 /* Do the bookkeeping for an element that was
10910 directly output as a constructor. */
10911 {
10912 /* For a record, keep track of end position of last field. */
10913 if (DECL_SIZE (constructor_fields))
10914 constructor_bit_index
10915 = size_binop_loc (input_location, PLUS_EXPR,
10916 bit_position (constructor_fields),
10917 DECL_SIZE (constructor_fields));
10918
10919 /* If the current field was the first one not yet written out,
10920 it isn't now, so update. */
10921 if (constructor_unfilled_fields == constructor_fields)
10922 {
10923 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10924 /* Skip any nameless bit fields. */
10925 while (constructor_unfilled_fields != 0
10926 && (DECL_UNNAMED_BIT_FIELD
10927 (constructor_unfilled_fields)))
10928 constructor_unfilled_fields =
10929 DECL_CHAIN (constructor_unfilled_fields);
10930 }
10931 }
10932
10933 constructor_fields = DECL_CHAIN (constructor_fields);
10934 /* Skip any nameless bit fields at the beginning. */
10935 while (constructor_fields != NULL_TREE
10936 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10937 constructor_fields = DECL_CHAIN (constructor_fields);
10938 }
10939 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10940 {
10941 tree fieldtype;
10942 enum tree_code fieldcode;
10943
10944 if (constructor_fields == NULL_TREE)
10945 {
10946 pedwarn_init (loc, 0,
10947 "excess elements in union initializer");
10948 break;
10949 }
10950
10951 fieldtype = TREE_TYPE (constructor_fields);
10952 if (fieldtype != error_mark_node)
10953 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10954 fieldcode = TREE_CODE (fieldtype);
10955
10956 /* Warn that traditional C rejects initialization of unions.
10957 We skip the warning if the value is zero. This is done
10958 under the assumption that the zero initializer in user
10959 code appears conditioned on e.g. __STDC__ to avoid
10960 "missing initializer" warnings and relies on default
10961 initialization to zero in the traditional C case.
10962 We also skip the warning if the initializer is designated,
10963 again on the assumption that this must be conditional on
10964 __STDC__ anyway (and we've already complained about the
10965 member-designator already). */
10966 if (!in_system_header_at (input_location) && !constructor_designated
10967 && !(value.value && (integer_zerop (value.value)
10968 || real_zerop (value.value))))
10969 warning (OPT_Wtraditional, "traditional C rejects initialization "
10970 "of unions");
10971
10972 /* Accept a string constant to initialize a subarray. */
10973 if (value.value != NULL_TREE
10974 && fieldcode == ARRAY_TYPE
10975 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10976 && string_flag)
10977 value.value = orig_value;
10978 /* Otherwise, if we have come to a subaggregate,
10979 and we don't have an element of its type, push into it. */
10980 else if (value.value != NULL_TREE
10981 && initialize_elementwise_p (fieldtype, value.value))
10982 {
10983 push_init_level (loc, 1, braced_init_obstack);
10984 continue;
10985 }
10986
10987 if (value.value)
10988 {
10989 push_member_name (constructor_fields);
10990 output_init_element (loc, value.value, value.original_type,
10991 strict_string, fieldtype,
10992 constructor_fields, true, implicit,
10993 braced_init_obstack);
10994 RESTORE_SPELLING_DEPTH (constructor_depth);
10995 }
10996 else
10997 /* Do the bookkeeping for an element that was
10998 directly output as a constructor. */
10999 {
11000 constructor_bit_index = DECL_SIZE (constructor_fields);
11001 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
11002 }
11003
11004 constructor_fields = NULL_TREE;
11005 }
11006 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11007 {
11008 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
11009 enum tree_code eltcode = TREE_CODE (elttype);
11010
11011 /* Accept a string constant to initialize a subarray. */
11012 if (value.value != NULL_TREE
11013 && eltcode == ARRAY_TYPE
11014 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
11015 && string_flag)
11016 value.value = orig_value;
11017 /* Otherwise, if we have come to a subaggregate,
11018 and we don't have an element of its type, push into it. */
11019 else if (value.value != NULL_TREE
11020 && initialize_elementwise_p (elttype, value.value))
11021 {
11022 push_init_level (loc, 1, braced_init_obstack);
11023 continue;
11024 }
11025
11026 if (constructor_max_index != NULL_TREE
11027 && (tree_int_cst_lt (constructor_max_index, constructor_index)
11028 || integer_all_onesp (constructor_max_index)))
11029 {
11030 pedwarn_init (loc, 0,
11031 "excess elements in array initializer");
11032 break;
11033 }
11034
11035 /* Now output the actual element. */
11036 if (value.value)
11037 {
11038 push_array_bounds (tree_to_uhwi (constructor_index));
11039 output_init_element (loc, value.value, value.original_type,
11040 strict_string, elttype,
11041 constructor_index, true, implicit,
11042 braced_init_obstack);
11043 RESTORE_SPELLING_DEPTH (constructor_depth);
11044 }
11045
11046 constructor_index
11047 = size_binop_loc (input_location, PLUS_EXPR,
11048 constructor_index, bitsize_one_node);
11049
11050 if (!value.value)
11051 /* If we are doing the bookkeeping for an element that was
11052 directly output as a constructor, we must update
11053 constructor_unfilled_index. */
11054 constructor_unfilled_index = constructor_index;
11055 }
11056 else if (gnu_vector_type_p (constructor_type))
11057 {
11058 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
11059
11060 /* Do a basic check of initializer size. Note that vectors
11061 always have a fixed size derived from their type. */
11062 if (tree_int_cst_lt (constructor_max_index, constructor_index))
11063 {
11064 pedwarn_init (loc, 0,
11065 "excess elements in vector initializer");
11066 break;
11067 }
11068
11069 /* Now output the actual element. */
11070 if (value.value)
11071 {
11072 if (TREE_CODE (value.value) == VECTOR_CST)
11073 elttype = TYPE_MAIN_VARIANT (constructor_type);
11074 output_init_element (loc, value.value, value.original_type,
11075 strict_string, elttype,
11076 constructor_index, true, implicit,
11077 braced_init_obstack);
11078 }
11079
11080 constructor_index
11081 = size_binop_loc (input_location,
11082 PLUS_EXPR, constructor_index, bitsize_one_node);
11083
11084 if (!value.value)
11085 /* If we are doing the bookkeeping for an element that was
11086 directly output as a constructor, we must update
11087 constructor_unfilled_index. */
11088 constructor_unfilled_index = constructor_index;
11089 }
11090
11091 /* Handle the sole element allowed in a braced initializer
11092 for a scalar variable. */
11093 else if (constructor_type != error_mark_node
11094 && constructor_fields == NULL_TREE)
11095 {
11096 pedwarn_init (loc, 0,
11097 "excess elements in scalar initializer");
11098 break;
11099 }
11100 else
11101 {
11102 if (value.value)
11103 output_init_element (loc, value.value, value.original_type,
11104 strict_string, constructor_type,
11105 NULL_TREE, true, implicit,
11106 braced_init_obstack);
11107 constructor_fields = NULL_TREE;
11108 }
11109
11110 /* Handle range initializers either at this level or anywhere higher
11111 in the designator stack. */
11112 if (constructor_range_stack)
11113 {
11114 struct constructor_range_stack *p, *range_stack;
11115 int finish = 0;
11116
11117 range_stack = constructor_range_stack;
11118 constructor_range_stack = 0;
11119 while (constructor_stack != range_stack->stack)
11120 {
11121 gcc_assert (constructor_stack->implicit);
11122 process_init_element (loc,
11123 pop_init_level (loc, 1,
11124 braced_init_obstack,
11125 last_init_list_comma),
11126 true, braced_init_obstack);
11127 }
11128 for (p = range_stack;
11129 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
11130 p = p->prev)
11131 {
11132 gcc_assert (constructor_stack->implicit);
11133 process_init_element (loc,
11134 pop_init_level (loc, 1,
11135 braced_init_obstack,
11136 last_init_list_comma),
11137 true, braced_init_obstack);
11138 }
11139
11140 p->index = size_binop_loc (input_location,
11141 PLUS_EXPR, p->index, bitsize_one_node);
11142 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
11143 finish = 1;
11144
11145 while (1)
11146 {
11147 constructor_index = p->index;
11148 constructor_fields = p->fields;
11149 if (finish && p->range_end && p->index == p->range_start)
11150 {
11151 finish = 0;
11152 p->prev = 0;
11153 }
11154 p = p->next;
11155 if (!p)
11156 break;
11157 finish_implicit_inits (loc, braced_init_obstack);
11158 push_init_level (loc, 2, braced_init_obstack);
11159 p->stack = constructor_stack;
11160 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
11161 p->index = p->range_start;
11162 }
11163
11164 if (!finish)
11165 constructor_range_stack = range_stack;
11166 continue;
11167 }
11168
11169 break;
11170 }
11171
11172 constructor_range_stack = 0;
11173 }
11174 \f
11175 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
11176 (guaranteed to be 'volatile' or null) and ARGS (represented using
11177 an ASM_EXPR node). */
11178 tree
11179 build_asm_stmt (bool is_volatile, tree args)
11180 {
11181 if (is_volatile)
11182 ASM_VOLATILE_P (args) = 1;
11183 return add_stmt (args);
11184 }
11185
11186 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
11187 some INPUTS, and some CLOBBERS. The latter three may be NULL.
11188 SIMPLE indicates whether there was anything at all after the
11189 string in the asm expression -- asm("blah") and asm("blah" : )
11190 are subtly different. We use a ASM_EXPR node to represent this.
11191 LOC is the location of the asm, and IS_INLINE says whether this
11192 is asm inline. */
11193 tree
11194 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
11195 tree clobbers, tree labels, bool simple, bool is_inline)
11196 {
11197 tree tail;
11198 tree args;
11199 int i;
11200 const char *constraint;
11201 const char **oconstraints;
11202 bool allows_mem, allows_reg, is_inout;
11203 int ninputs, noutputs;
11204
11205 ninputs = list_length (inputs);
11206 noutputs = list_length (outputs);
11207 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
11208
11209 string = resolve_asm_operand_names (string, outputs, inputs, labels);
11210
11211 /* Remove output conversions that change the type but not the mode. */
11212 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
11213 {
11214 tree output = TREE_VALUE (tail);
11215
11216 output = c_fully_fold (output, false, NULL, true);
11217
11218 /* ??? Really, this should not be here. Users should be using a
11219 proper lvalue, dammit. But there's a long history of using casts
11220 in the output operands. In cases like longlong.h, this becomes a
11221 primitive form of typechecking -- if the cast can be removed, then
11222 the output operand had a type of the proper width; otherwise we'll
11223 get an error. Gross, but ... */
11224 STRIP_NOPS (output);
11225
11226 if (!lvalue_or_else (loc, output, lv_asm))
11227 output = error_mark_node;
11228
11229 if (output != error_mark_node
11230 && (TREE_READONLY (output)
11231 || TYPE_READONLY (TREE_TYPE (output))
11232 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
11233 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
11234 readonly_error (loc, output, lv_asm);
11235
11236 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11237 oconstraints[i] = constraint;
11238
11239 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
11240 &allows_mem, &allows_reg, &is_inout))
11241 {
11242 /* If the operand is going to end up in memory,
11243 mark it addressable. */
11244 if (!allows_reg && !c_mark_addressable (output))
11245 output = error_mark_node;
11246 if (!(!allows_reg && allows_mem)
11247 && output != error_mark_node
11248 && VOID_TYPE_P (TREE_TYPE (output)))
11249 {
11250 error_at (loc, "invalid use of void expression");
11251 output = error_mark_node;
11252 }
11253 }
11254 else
11255 output = error_mark_node;
11256
11257 TREE_VALUE (tail) = output;
11258 }
11259
11260 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
11261 {
11262 tree input;
11263
11264 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11265 input = TREE_VALUE (tail);
11266
11267 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
11268 oconstraints, &allows_mem, &allows_reg))
11269 {
11270 /* If the operand is going to end up in memory,
11271 mark it addressable. */
11272 if (!allows_reg && allows_mem)
11273 {
11274 input = c_fully_fold (input, false, NULL, true);
11275
11276 /* Strip the nops as we allow this case. FIXME, this really
11277 should be rejected or made deprecated. */
11278 STRIP_NOPS (input);
11279 if (!c_mark_addressable (input))
11280 input = error_mark_node;
11281 }
11282 else
11283 {
11284 struct c_expr expr;
11285 memset (&expr, 0, sizeof (expr));
11286 expr.value = input;
11287 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
11288 input = c_fully_fold (expr.value, false, NULL);
11289
11290 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
11291 {
11292 error_at (loc, "invalid use of void expression");
11293 input = error_mark_node;
11294 }
11295 }
11296 }
11297 else
11298 input = error_mark_node;
11299
11300 TREE_VALUE (tail) = input;
11301 }
11302
11303 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
11304
11305 /* asm statements without outputs, including simple ones, are treated
11306 as volatile. */
11307 ASM_INPUT_P (args) = simple;
11308 ASM_VOLATILE_P (args) = (noutputs == 0);
11309 ASM_INLINE_P (args) = is_inline;
11310
11311 return args;
11312 }
11313 \f
11314 /* Generate a goto statement to LABEL. LOC is the location of the
11315 GOTO. */
11316
11317 tree
11318 c_finish_goto_label (location_t loc, tree label)
11319 {
11320 tree decl = lookup_label_for_goto (loc, label);
11321 if (!decl)
11322 return NULL_TREE;
11323 TREE_USED (decl) = 1;
11324 {
11325 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
11326 tree t = build1 (GOTO_EXPR, void_type_node, decl);
11327 SET_EXPR_LOCATION (t, loc);
11328 return add_stmt (t);
11329 }
11330 }
11331
11332 /* Generate a computed goto statement to EXPR. LOC is the location of
11333 the GOTO. */
11334
11335 tree
11336 c_finish_goto_ptr (location_t loc, c_expr val)
11337 {
11338 tree expr = val.value;
11339 tree t;
11340 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
11341 if (expr != error_mark_node
11342 && !POINTER_TYPE_P (TREE_TYPE (expr))
11343 && !null_pointer_constant_p (expr))
11344 {
11345 error_at (val.get_location (),
11346 "computed goto must be pointer type");
11347 expr = build_zero_cst (ptr_type_node);
11348 }
11349 expr = c_fully_fold (expr, false, NULL);
11350 expr = convert (ptr_type_node, expr);
11351 t = build1 (GOTO_EXPR, void_type_node, expr);
11352 SET_EXPR_LOCATION (t, loc);
11353 return add_stmt (t);
11354 }
11355
11356 /* Generate a C `return' statement. RETVAL is the expression for what
11357 to return, or a null pointer for `return;' with no value. LOC is
11358 the location of the return statement, or the location of the expression,
11359 if the statement has any. If ORIGTYPE is not NULL_TREE, it
11360 is the original type of RETVAL. */
11361
11362 tree
11363 c_finish_return (location_t loc, tree retval, tree origtype)
11364 {
11365 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
11366 bool no_warning = false;
11367 bool npc = false;
11368
11369 /* Use the expansion point to handle cases such as returning NULL
11370 in a function returning void. */
11371 location_t xloc = expansion_point_location_if_in_system_header (loc);
11372
11373 if (TREE_THIS_VOLATILE (current_function_decl))
11374 warning_at (xloc, 0,
11375 "function declared %<noreturn%> has a %<return%> statement");
11376
11377 if (retval)
11378 {
11379 tree semantic_type = NULL_TREE;
11380 npc = null_pointer_constant_p (retval);
11381 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
11382 {
11383 semantic_type = TREE_TYPE (retval);
11384 retval = TREE_OPERAND (retval, 0);
11385 }
11386 retval = c_fully_fold (retval, false, NULL);
11387 if (semantic_type
11388 && valtype != NULL_TREE
11389 && TREE_CODE (valtype) != VOID_TYPE)
11390 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
11391 }
11392
11393 if (!retval)
11394 {
11395 current_function_returns_null = 1;
11396 if ((warn_return_type >= 0 || flag_isoc99)
11397 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
11398 {
11399 no_warning = true;
11400 if (emit_diagnostic (flag_isoc99 ? DK_PERMERROR : DK_WARNING,
11401 loc, OPT_Wreturn_mismatch,
11402 "%<return%> with no value,"
11403 " in function returning non-void"))
11404 inform (DECL_SOURCE_LOCATION (current_function_decl),
11405 "declared here");
11406 }
11407 }
11408 else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
11409 {
11410 current_function_returns_null = 1;
11411 bool warned_here;
11412 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
11413 warned_here = permerror_opt
11414 (xloc, OPT_Wreturn_mismatch,
11415 "%<return%> with a value, in function returning void");
11416 else
11417 warned_here = pedwarn
11418 (xloc, OPT_Wpedantic, "ISO C forbids "
11419 "%<return%> with expression, in function returning void");
11420 if (warned_here)
11421 inform (DECL_SOURCE_LOCATION (current_function_decl),
11422 "declared here");
11423 }
11424 else
11425 {
11426 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
11427 retval, origtype, ic_return,
11428 npc, NULL_TREE, NULL_TREE, 0);
11429 tree res = DECL_RESULT (current_function_decl);
11430 tree inner;
11431 bool save;
11432
11433 current_function_returns_value = 1;
11434 if (t == error_mark_node)
11435 return NULL_TREE;
11436
11437 save = in_late_binary_op;
11438 if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
11439 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
11440 || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
11441 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
11442 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
11443 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
11444 in_late_binary_op = true;
11445 inner = t = convert (TREE_TYPE (res), t);
11446 in_late_binary_op = save;
11447
11448 /* Strip any conversions, additions, and subtractions, and see if
11449 we are returning the address of a local variable. Warn if so. */
11450 while (1)
11451 {
11452 switch (TREE_CODE (inner))
11453 {
11454 CASE_CONVERT:
11455 case NON_LVALUE_EXPR:
11456 case PLUS_EXPR:
11457 case POINTER_PLUS_EXPR:
11458 inner = TREE_OPERAND (inner, 0);
11459 continue;
11460
11461 case MINUS_EXPR:
11462 /* If the second operand of the MINUS_EXPR has a pointer
11463 type (or is converted from it), this may be valid, so
11464 don't give a warning. */
11465 {
11466 tree op1 = TREE_OPERAND (inner, 1);
11467
11468 while (!POINTER_TYPE_P (TREE_TYPE (op1))
11469 && (CONVERT_EXPR_P (op1)
11470 || TREE_CODE (op1) == NON_LVALUE_EXPR))
11471 op1 = TREE_OPERAND (op1, 0);
11472
11473 if (POINTER_TYPE_P (TREE_TYPE (op1)))
11474 break;
11475
11476 inner = TREE_OPERAND (inner, 0);
11477 continue;
11478 }
11479
11480 case ADDR_EXPR:
11481 inner = TREE_OPERAND (inner, 0);
11482
11483 while (REFERENCE_CLASS_P (inner)
11484 && !INDIRECT_REF_P (inner))
11485 inner = TREE_OPERAND (inner, 0);
11486
11487 if (DECL_P (inner)
11488 && !DECL_EXTERNAL (inner)
11489 && !TREE_STATIC (inner)
11490 && DECL_CONTEXT (inner) == current_function_decl
11491 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
11492 {
11493 if (TREE_CODE (inner) == LABEL_DECL)
11494 warning_at (loc, OPT_Wreturn_local_addr,
11495 "function returns address of label");
11496 else
11497 {
11498 warning_at (loc, OPT_Wreturn_local_addr,
11499 "function returns address of local variable");
11500 tree zero = build_zero_cst (TREE_TYPE (res));
11501 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11502 }
11503 }
11504 break;
11505
11506 default:
11507 break;
11508 }
11509
11510 break;
11511 }
11512
11513 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11514 SET_EXPR_LOCATION (retval, loc);
11515
11516 if (warn_sequence_point)
11517 verify_sequence_points (retval);
11518 }
11519
11520 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11521 if (no_warning)
11522 suppress_warning (ret_stmt, OPT_Wreturn_type);
11523 return add_stmt (ret_stmt);
11524 }
11525 \f
11526 struct c_switch {
11527 /* The SWITCH_STMT being built. */
11528 tree switch_stmt;
11529
11530 /* The original type of the testing expression, i.e. before the
11531 default conversion is applied. */
11532 tree orig_type;
11533
11534 /* A splay-tree mapping the low element of a case range to the high
11535 element, or NULL_TREE if there is no high element. Used to
11536 determine whether or not a new case label duplicates an old case
11537 label. We need a tree, rather than simply a hash table, because
11538 of the GNU case range extension. */
11539 splay_tree cases;
11540
11541 /* The bindings at the point of the switch. This is used for
11542 warnings crossing decls when branching to a case label. */
11543 struct c_spot_bindings *bindings;
11544
11545 /* Whether the switch includes any break statements. */
11546 bool break_stmt_seen_p;
11547
11548 /* The next node on the stack. */
11549 struct c_switch *next;
11550
11551 /* Remember whether the controlling expression had boolean type
11552 before integer promotions for the sake of -Wswitch-bool. */
11553 bool bool_cond_p;
11554 };
11555
11556 /* A stack of the currently active switch statements. The innermost
11557 switch statement is on the top of the stack. There is no need to
11558 mark the stack for garbage collection because it is only active
11559 during the processing of the body of a function, and we never
11560 collect at that point. */
11561
11562 struct c_switch *c_switch_stack;
11563
11564 /* Start a C switch statement, testing expression EXP. Return the new
11565 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11566 SWITCH_COND_LOC is the location of the switch's condition.
11567 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11568
11569 tree
11570 c_start_switch (location_t switch_loc,
11571 location_t switch_cond_loc,
11572 tree exp, bool explicit_cast_p)
11573 {
11574 tree orig_type = error_mark_node;
11575 bool bool_cond_p = false;
11576 struct c_switch *cs;
11577
11578 if (exp != error_mark_node)
11579 {
11580 orig_type = TREE_TYPE (exp);
11581
11582 if (!INTEGRAL_TYPE_P (orig_type))
11583 {
11584 if (orig_type != error_mark_node)
11585 {
11586 error_at (switch_cond_loc, "switch quantity not an integer");
11587 orig_type = error_mark_node;
11588 }
11589 exp = integer_zero_node;
11590 }
11591 else
11592 {
11593 tree type = TYPE_MAIN_VARIANT (orig_type);
11594 tree e = exp;
11595
11596 /* Warn if the condition has boolean value. */
11597 while (TREE_CODE (e) == COMPOUND_EXPR)
11598 e = TREE_OPERAND (e, 1);
11599
11600 if ((C_BOOLEAN_TYPE_P (type)
11601 || truth_value_p (TREE_CODE (e)))
11602 /* Explicit cast to int suppresses this warning. */
11603 && !(TREE_CODE (type) == INTEGER_TYPE
11604 && explicit_cast_p))
11605 bool_cond_p = true;
11606
11607 if (!in_system_header_at (input_location)
11608 && (type == long_integer_type_node
11609 || type == long_unsigned_type_node))
11610 warning_at (switch_cond_loc,
11611 OPT_Wtraditional, "%<long%> switch expression not "
11612 "converted to %<int%> in ISO C");
11613
11614 exp = c_fully_fold (exp, false, NULL);
11615 exp = default_conversion (exp);
11616
11617 if (warn_sequence_point)
11618 verify_sequence_points (exp);
11619 }
11620 }
11621
11622 /* Add this new SWITCH_STMT to the stack. */
11623 cs = XNEW (struct c_switch);
11624 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11625 NULL_TREE, orig_type, NULL_TREE);
11626 cs->orig_type = orig_type;
11627 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11628 cs->bindings = c_get_switch_bindings ();
11629 cs->break_stmt_seen_p = false;
11630 cs->bool_cond_p = bool_cond_p;
11631 cs->next = c_switch_stack;
11632 c_switch_stack = cs;
11633
11634 return add_stmt (cs->switch_stmt);
11635 }
11636
11637 /* Process a case label at location LOC, with attributes ATTRS. */
11638
11639 tree
11640 do_case (location_t loc, tree low_value, tree high_value, tree attrs)
11641 {
11642 tree label = NULL_TREE;
11643
11644 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11645 {
11646 low_value = c_fully_fold (low_value, false, NULL);
11647 if (TREE_CODE (low_value) == INTEGER_CST)
11648 pedwarn (loc, OPT_Wpedantic,
11649 "case label is not an integer constant expression");
11650 }
11651
11652 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11653 {
11654 high_value = c_fully_fold (high_value, false, NULL);
11655 if (TREE_CODE (high_value) == INTEGER_CST)
11656 pedwarn (input_location, OPT_Wpedantic,
11657 "case label is not an integer constant expression");
11658 }
11659
11660 if (c_switch_stack == NULL)
11661 {
11662 if (low_value)
11663 error_at (loc, "case label not within a switch statement");
11664 else
11665 error_at (loc, "%<default%> label not within a switch statement");
11666 return NULL_TREE;
11667 }
11668
11669 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11670 EXPR_LOCATION (c_switch_stack->switch_stmt),
11671 loc))
11672 return NULL_TREE;
11673
11674 label = c_add_case_label (loc, c_switch_stack->cases,
11675 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11676 low_value, high_value, attrs);
11677 if (label == error_mark_node)
11678 label = NULL_TREE;
11679 return label;
11680 }
11681
11682 /* Finish the switch statement. TYPE is the original type of the
11683 controlling expression of the switch, or NULL_TREE. */
11684
11685 void
11686 c_finish_switch (tree body, tree type)
11687 {
11688 struct c_switch *cs = c_switch_stack;
11689 location_t switch_location;
11690
11691 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11692
11693 /* Emit warnings as needed. */
11694 switch_location = EXPR_LOCATION (cs->switch_stmt);
11695 c_do_switch_warnings (cs->cases, switch_location,
11696 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11697 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11698 if (c_switch_covers_all_cases_p (cs->cases,
11699 SWITCH_STMT_TYPE (cs->switch_stmt)))
11700 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11701 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11702
11703 /* Pop the stack. */
11704 c_switch_stack = cs->next;
11705 splay_tree_delete (cs->cases);
11706 c_release_switch_bindings (cs->bindings);
11707 XDELETE (cs);
11708 }
11709 \f
11710 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11711 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11712 may be null. */
11713
11714 void
11715 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11716 tree else_block)
11717 {
11718 tree stmt;
11719
11720 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11721 SET_EXPR_LOCATION (stmt, if_locus);
11722 add_stmt (stmt);
11723 }
11724
11725 tree
11726 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11727 {
11728 /* In switch statements break is sometimes stylistically used after
11729 a return statement. This can lead to spurious warnings about
11730 control reaching the end of a non-void function when it is
11731 inlined. Note that we are calling block_may_fallthru with
11732 language specific tree nodes; this works because
11733 block_may_fallthru returns true when given something it does not
11734 understand. */
11735 bool skip = !block_may_fallthru (cur_stmt_list);
11736
11737 if (is_break)
11738 switch (in_statement)
11739 {
11740 case 0:
11741 error_at (loc, "break statement not within loop or switch");
11742 return NULL_TREE;
11743 case IN_OMP_BLOCK:
11744 error_at (loc, "invalid exit from OpenMP structured block");
11745 return NULL_TREE;
11746 case IN_OMP_FOR:
11747 error_at (loc, "break statement used with OpenMP for loop");
11748 return NULL_TREE;
11749 case IN_ITERATION_STMT:
11750 case IN_OBJC_FOREACH:
11751 break;
11752 default:
11753 gcc_assert (in_statement & IN_SWITCH_STMT);
11754 c_switch_stack->break_stmt_seen_p = true;
11755 break;
11756 }
11757 else
11758 switch (in_statement & ~IN_SWITCH_STMT)
11759 {
11760 case 0:
11761 error_at (loc, "continue statement not within a loop");
11762 return NULL_TREE;
11763 case IN_OMP_BLOCK:
11764 error_at (loc, "invalid exit from OpenMP structured block");
11765 return NULL_TREE;
11766 case IN_ITERATION_STMT:
11767 case IN_OMP_FOR:
11768 case IN_OBJC_FOREACH:
11769 break;
11770 default:
11771 gcc_unreachable ();
11772 }
11773
11774 if (skip)
11775 return NULL_TREE;
11776 else if ((in_statement & IN_OBJC_FOREACH)
11777 && !(is_break && (in_statement & IN_SWITCH_STMT)))
11778 {
11779 /* The foreach expander produces low-level code using gotos instead
11780 of a structured loop construct. */
11781 gcc_assert (label);
11782 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11783 }
11784 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11785 }
11786
11787 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11788
11789 static void
11790 emit_side_effect_warnings (location_t loc, tree expr)
11791 {
11792 maybe_warn_nodiscard (loc, expr);
11793 if (!warn_unused_value)
11794 return;
11795 if (expr == error_mark_node)
11796 ;
11797 else if (!TREE_SIDE_EFFECTS (expr))
11798 {
11799 if (!VOID_TYPE_P (TREE_TYPE (expr))
11800 && !warning_suppressed_p (expr, OPT_Wunused_value))
11801 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11802 }
11803 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11804 {
11805 tree r = expr;
11806 location_t cloc = loc;
11807 while (TREE_CODE (r) == COMPOUND_EXPR)
11808 {
11809 if (EXPR_HAS_LOCATION (r))
11810 cloc = EXPR_LOCATION (r);
11811 r = TREE_OPERAND (r, 1);
11812 }
11813 if (!TREE_SIDE_EFFECTS (r)
11814 && !VOID_TYPE_P (TREE_TYPE (r))
11815 && !CONVERT_EXPR_P (r)
11816 && !warning_suppressed_p (r, OPT_Wunused_value)
11817 && !warning_suppressed_p (expr, OPT_Wunused_value))
11818 warning_at (cloc, OPT_Wunused_value,
11819 "right-hand operand of comma expression has no effect");
11820 }
11821 else
11822 warn_if_unused_value (expr, loc);
11823 }
11824
11825 /* Process an expression as if it were a complete statement. Emit
11826 diagnostics, but do not call ADD_STMT. LOC is the location of the
11827 statement. */
11828
11829 tree
11830 c_process_expr_stmt (location_t loc, tree expr)
11831 {
11832 tree exprv;
11833
11834 if (!expr)
11835 return NULL_TREE;
11836
11837 expr = c_fully_fold (expr, false, NULL);
11838
11839 if (warn_sequence_point)
11840 verify_sequence_points (expr);
11841
11842 if (TREE_TYPE (expr) != error_mark_node
11843 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11844 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11845 error_at (loc, "expression statement has incomplete type");
11846
11847 /* If we're not processing a statement expression, warn about unused values.
11848 Warnings for statement expressions will be emitted later, once we figure
11849 out which is the result. */
11850 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11851 && (warn_unused_value || warn_unused_result))
11852 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11853
11854 exprv = expr;
11855 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11856 exprv = TREE_OPERAND (exprv, 1);
11857 while (CONVERT_EXPR_P (exprv))
11858 exprv = TREE_OPERAND (exprv, 0);
11859 if (DECL_P (exprv)
11860 || handled_component_p (exprv)
11861 || TREE_CODE (exprv) == ADDR_EXPR)
11862 mark_exp_read (exprv);
11863
11864 /* If the expression is not of a type to which we cannot assign a line
11865 number, wrap the thing in a no-op NOP_EXPR. */
11866 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11867 {
11868 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11869 SET_EXPR_LOCATION (expr, loc);
11870 }
11871
11872 return expr;
11873 }
11874
11875 /* Emit an expression as a statement. LOC is the location of the
11876 expression. */
11877
11878 tree
11879 c_finish_expr_stmt (location_t loc, tree expr)
11880 {
11881 if (expr)
11882 return add_stmt (c_process_expr_stmt (loc, expr));
11883 else
11884 return NULL;
11885 }
11886
11887 /* Do the opposite and emit a statement as an expression. To begin,
11888 create a new binding level and return it. */
11889
11890 tree
11891 c_begin_stmt_expr (void)
11892 {
11893 tree ret;
11894
11895 /* We must force a BLOCK for this level so that, if it is not expanded
11896 later, there is a way to turn off the entire subtree of blocks that
11897 are contained in it. */
11898 keep_next_level ();
11899 ret = c_begin_compound_stmt (true);
11900
11901 c_bindings_start_stmt_expr (c_switch_stack == NULL
11902 ? NULL
11903 : c_switch_stack->bindings);
11904
11905 /* Mark the current statement list as belonging to a statement list. */
11906 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11907
11908 return ret;
11909 }
11910
11911 /* LOC is the location of the compound statement to which this body
11912 belongs. */
11913
11914 tree
11915 c_finish_stmt_expr (location_t loc, tree body)
11916 {
11917 tree last, type, tmp, val;
11918 tree *last_p;
11919
11920 body = c_end_compound_stmt (loc, body, true);
11921
11922 c_bindings_end_stmt_expr (c_switch_stack == NULL
11923 ? NULL
11924 : c_switch_stack->bindings);
11925
11926 /* Locate the last statement in BODY. See c_end_compound_stmt
11927 about always returning a BIND_EXPR. */
11928 last_p = &BIND_EXPR_BODY (body);
11929 last = BIND_EXPR_BODY (body);
11930
11931 continue_searching:
11932 if (TREE_CODE (last) == STATEMENT_LIST)
11933 {
11934 tree_stmt_iterator l = tsi_last (last);
11935
11936 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11937 tsi_prev (&l);
11938
11939 /* This can happen with degenerate cases like ({ }). No value. */
11940 if (tsi_end_p (l))
11941 return body;
11942
11943 /* If we're supposed to generate side effects warnings, process
11944 all of the statements except the last. */
11945 if (warn_unused_value || warn_unused_result)
11946 {
11947 for (tree_stmt_iterator i = tsi_start (last);
11948 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11949 {
11950 location_t tloc;
11951 tree t = tsi_stmt (i);
11952
11953 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11954 emit_side_effect_warnings (tloc, t);
11955 }
11956 }
11957 last_p = tsi_stmt_ptr (l);
11958 last = *last_p;
11959 }
11960
11961 /* If the end of the list is exception related, then the list was split
11962 by a call to push_cleanup. Continue searching. */
11963 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11964 || TREE_CODE (last) == TRY_CATCH_EXPR)
11965 {
11966 last_p = &TREE_OPERAND (last, 0);
11967 last = *last_p;
11968 goto continue_searching;
11969 }
11970
11971 if (last == error_mark_node)
11972 return last;
11973
11974 /* In the case that the BIND_EXPR is not necessary, return the
11975 expression out from inside it. */
11976 if ((last == BIND_EXPR_BODY (body)
11977 /* Skip nested debug stmts. */
11978 || last == expr_first (BIND_EXPR_BODY (body)))
11979 && BIND_EXPR_VARS (body) == NULL)
11980 {
11981 /* Even if this looks constant, do not allow it in a constant
11982 expression. */
11983 last = c_wrap_maybe_const (last, true);
11984 /* Do not warn if the return value of a statement expression is
11985 unused. */
11986 suppress_warning (last, OPT_Wunused);
11987 return last;
11988 }
11989
11990 /* Extract the type of said expression. */
11991 type = TREE_TYPE (last);
11992
11993 /* If we're not returning a value at all, then the BIND_EXPR that
11994 we already have is a fine expression to return. */
11995 if (!type || VOID_TYPE_P (type))
11996 return body;
11997
11998 /* Now that we've located the expression containing the value, it seems
11999 silly to make voidify_wrapper_expr repeat the process. Create a
12000 temporary of the appropriate type and stick it in a TARGET_EXPR. */
12001 tmp = create_tmp_var_raw (type);
12002
12003 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
12004 tree_expr_nonnegative_p giving up immediately. */
12005 val = last;
12006 if (TREE_CODE (val) == NOP_EXPR
12007 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
12008 val = TREE_OPERAND (val, 0);
12009
12010 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
12011 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
12012
12013 {
12014 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
12015 SET_EXPR_LOCATION (t, loc);
12016 return t;
12017 }
12018 }
12019 \f
12020 /* Begin and end compound statements. This is as simple as pushing
12021 and popping new statement lists from the tree. */
12022
12023 tree
12024 c_begin_compound_stmt (bool do_scope)
12025 {
12026 tree stmt = push_stmt_list ();
12027 if (do_scope)
12028 push_scope ();
12029 return stmt;
12030 }
12031
12032 /* End a compound statement. STMT is the statement. LOC is the
12033 location of the compound statement-- this is usually the location
12034 of the opening brace. */
12035
12036 tree
12037 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
12038 {
12039 tree block = NULL;
12040
12041 if (do_scope)
12042 {
12043 if (c_dialect_objc ())
12044 objc_clear_super_receiver ();
12045 block = pop_scope ();
12046 }
12047
12048 stmt = pop_stmt_list (stmt);
12049 stmt = c_build_bind_expr (loc, block, stmt);
12050
12051 /* If this compound statement is nested immediately inside a statement
12052 expression, then force a BIND_EXPR to be created. Otherwise we'll
12053 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
12054 STATEMENT_LISTs merge, and thus we can lose track of what statement
12055 was really last. */
12056 if (building_stmt_list_p ()
12057 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
12058 && TREE_CODE (stmt) != BIND_EXPR)
12059 {
12060 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
12061 TREE_SIDE_EFFECTS (stmt) = 1;
12062 SET_EXPR_LOCATION (stmt, loc);
12063 }
12064
12065 return stmt;
12066 }
12067
12068 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
12069 when the current scope is exited. EH_ONLY is true when this is not
12070 meant to apply to normal control flow transfer. */
12071
12072 void
12073 push_cleanup (tree decl, tree cleanup, bool eh_only)
12074 {
12075 enum tree_code code;
12076 tree stmt, list;
12077 bool stmt_expr;
12078
12079 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
12080 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
12081 add_stmt (stmt);
12082 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
12083 list = push_stmt_list ();
12084 TREE_OPERAND (stmt, 0) = list;
12085 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
12086 }
12087 \f
12088 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
12089 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
12090
12091 static tree
12092 build_vec_cmp (tree_code code, tree type,
12093 tree arg0, tree arg1)
12094 {
12095 tree zero_vec = build_zero_cst (type);
12096 tree minus_one_vec = build_minus_one_cst (type);
12097 tree cmp_type = truth_type_for (TREE_TYPE (arg0));
12098 tree cmp = build2 (code, cmp_type, arg0, arg1);
12099 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
12100 }
12101
12102 /* Possibly warn about an address of OP never being NULL in a comparison
12103 operation CODE involving null. */
12104
12105 static void
12106 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
12107 {
12108 /* Prevent warnings issued for macro expansion. */
12109 if (!warn_address
12110 || warning_suppressed_p (op, OPT_Waddress)
12111 || from_macro_expansion_at (loc))
12112 return;
12113
12114 if (TREE_CODE (op) == NOP_EXPR)
12115 {
12116 /* Allow casts to intptr_t to suppress the warning. */
12117 tree type = TREE_TYPE (op);
12118 if (TREE_CODE (type) == INTEGER_TYPE)
12119 return;
12120 op = TREE_OPERAND (op, 0);
12121 }
12122
12123 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
12124 {
12125 /* Allow a cast to void* to suppress the warning. */
12126 tree type = TREE_TYPE (TREE_TYPE (op));
12127 if (VOID_TYPE_P (type))
12128 return;
12129
12130 /* Adding any value to a null pointer, including zero, is undefined
12131 in C. This includes the expression &p[0] where p is the null
12132 pointer, although &p[0] will have been folded to p by this point
12133 and so not diagnosed. */
12134 if (code == EQ_EXPR)
12135 warning_at (loc, OPT_Waddress,
12136 "the comparison will always evaluate as %<false%> "
12137 "for the pointer operand in %qE must not be NULL",
12138 op);
12139 else
12140 warning_at (loc, OPT_Waddress,
12141 "the comparison will always evaluate as %<true%> "
12142 "for the pointer operand in %qE must not be NULL",
12143 op);
12144
12145 return;
12146 }
12147
12148 if (TREE_CODE (op) != ADDR_EXPR)
12149 return;
12150
12151 op = TREE_OPERAND (op, 0);
12152
12153 if (TREE_CODE (op) == IMAGPART_EXPR
12154 || TREE_CODE (op) == REALPART_EXPR)
12155 {
12156 /* The address of either complex part may not be null. */
12157 if (code == EQ_EXPR)
12158 warning_at (loc, OPT_Waddress,
12159 "the comparison will always evaluate as %<false%> "
12160 "for the address of %qE will never be NULL",
12161 op);
12162 else
12163 warning_at (loc, OPT_Waddress,
12164 "the comparison will always evaluate as %<true%> "
12165 "for the address of %qE will never be NULL",
12166 op);
12167 return;
12168 }
12169
12170 /* Set to true in the loop below if OP dereferences is operand.
12171 In such a case the ultimate target need not be a decl for
12172 the null [in]equality test to be constant. */
12173 bool deref = false;
12174
12175 /* Get the outermost array or object, or member. */
12176 while (handled_component_p (op))
12177 {
12178 if (TREE_CODE (op) == COMPONENT_REF)
12179 {
12180 /* Get the member (its address is never null). */
12181 op = TREE_OPERAND (op, 1);
12182 break;
12183 }
12184
12185 /* Get the outer array/object to refer to in the warning. */
12186 op = TREE_OPERAND (op, 0);
12187 deref = true;
12188 }
12189
12190 if ((!deref && !decl_with_nonnull_addr_p (op))
12191 || from_macro_expansion_at (loc))
12192 return;
12193
12194 bool w;
12195 if (code == EQ_EXPR)
12196 w = warning_at (loc, OPT_Waddress,
12197 "the comparison will always evaluate as %<false%> "
12198 "for the address of %qE will never be NULL",
12199 op);
12200 else
12201 w = warning_at (loc, OPT_Waddress,
12202 "the comparison will always evaluate as %<true%> "
12203 "for the address of %qE will never be NULL",
12204 op);
12205
12206 if (w && DECL_P (op))
12207 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
12208 }
12209
12210 /* Build a binary-operation expression without default conversions.
12211 CODE is the kind of expression to build.
12212 LOCATION is the operator's location.
12213 This function differs from `build' in several ways:
12214 the data type of the result is computed and recorded in it,
12215 warnings are generated if arg data types are invalid,
12216 special handling for addition and subtraction of pointers is known,
12217 and some optimization is done (operations on narrow ints
12218 are done in the narrower type when that gives the same result).
12219 Constant folding is also done before the result is returned.
12220
12221 Note that the operands will never have enumeral types, or function
12222 or array types, because either they will have the default conversions
12223 performed or they have both just been converted to some other type in which
12224 the arithmetic is to be done. */
12225
12226 tree
12227 build_binary_op (location_t location, enum tree_code code,
12228 tree orig_op0, tree orig_op1, bool convert_p)
12229 {
12230 tree type0, type1, orig_type0, orig_type1;
12231 tree eptype;
12232 enum tree_code code0, code1;
12233 tree op0, op1;
12234 tree ret = error_mark_node;
12235 const char *invalid_op_diag;
12236 bool op0_int_operands, op1_int_operands;
12237 bool int_const, int_const_or_overflow, int_operands;
12238
12239 /* Expression code to give to the expression when it is built.
12240 Normally this is CODE, which is what the caller asked for,
12241 but in some special cases we change it. */
12242 enum tree_code resultcode = code;
12243
12244 /* Data type in which the computation is to be performed.
12245 In the simplest cases this is the common type of the arguments. */
12246 tree result_type = NULL;
12247
12248 /* When the computation is in excess precision, the type of the
12249 final EXCESS_PRECISION_EXPR. */
12250 tree semantic_result_type = NULL;
12251
12252 /* Nonzero means operands have already been type-converted
12253 in whatever way is necessary.
12254 Zero means they need to be converted to RESULT_TYPE. */
12255 int converted = 0;
12256
12257 /* Nonzero means create the expression with this type, rather than
12258 RESULT_TYPE. */
12259 tree build_type = NULL_TREE;
12260
12261 /* Nonzero means after finally constructing the expression
12262 convert it to this type. */
12263 tree final_type = NULL_TREE;
12264
12265 /* Nonzero if this is an operation like MIN or MAX which can
12266 safely be computed in short if both args are promoted shorts.
12267 Also implies COMMON.
12268 -1 indicates a bitwise operation; this makes a difference
12269 in the exact conditions for when it is safe to do the operation
12270 in a narrower mode. */
12271 int shorten = 0;
12272
12273 /* Nonzero if this is a comparison operation;
12274 if both args are promoted shorts, compare the original shorts.
12275 Also implies COMMON. */
12276 int short_compare = 0;
12277
12278 /* Nonzero if this is a right-shift operation, which can be computed on the
12279 original short and then promoted if the operand is a promoted short. */
12280 int short_shift = 0;
12281
12282 /* Nonzero means set RESULT_TYPE to the common type of the args. */
12283 int common = 0;
12284
12285 /* True means types are compatible as far as ObjC is concerned. */
12286 bool objc_ok;
12287
12288 /* True means this is an arithmetic operation that may need excess
12289 precision. */
12290 bool may_need_excess_precision;
12291
12292 /* True means this is a boolean operation that converts both its
12293 operands to truth-values. */
12294 bool boolean_op = false;
12295
12296 /* Remember whether we're doing / or %. */
12297 bool doing_div_or_mod = false;
12298
12299 /* Remember whether we're doing << or >>. */
12300 bool doing_shift = false;
12301
12302 /* Tree holding instrumentation expression. */
12303 tree instrument_expr = NULL;
12304
12305 if (location == UNKNOWN_LOCATION)
12306 location = input_location;
12307
12308 op0 = orig_op0;
12309 op1 = orig_op1;
12310
12311 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
12312 if (op0_int_operands)
12313 op0 = remove_c_maybe_const_expr (op0);
12314 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
12315 if (op1_int_operands)
12316 op1 = remove_c_maybe_const_expr (op1);
12317 int_operands = (op0_int_operands && op1_int_operands);
12318 if (int_operands)
12319 {
12320 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
12321 && TREE_CODE (orig_op1) == INTEGER_CST);
12322 int_const = (int_const_or_overflow
12323 && !TREE_OVERFLOW (orig_op0)
12324 && !TREE_OVERFLOW (orig_op1));
12325 }
12326 else
12327 int_const = int_const_or_overflow = false;
12328
12329 /* Do not apply default conversion in mixed vector/scalar expression. */
12330 if (convert_p
12331 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
12332 {
12333 op0 = default_conversion (op0);
12334 op1 = default_conversion (op1);
12335 }
12336
12337 orig_type0 = type0 = TREE_TYPE (op0);
12338
12339 orig_type1 = type1 = TREE_TYPE (op1);
12340
12341 /* The expression codes of the data types of the arguments tell us
12342 whether the arguments are integers, floating, pointers, etc. */
12343 code0 = TREE_CODE (type0);
12344 code1 = TREE_CODE (type1);
12345
12346 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
12347 STRIP_TYPE_NOPS (op0);
12348 STRIP_TYPE_NOPS (op1);
12349
12350 /* If an error was already reported for one of the arguments,
12351 avoid reporting another error. */
12352
12353 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12354 return error_mark_node;
12355
12356 if (code0 == POINTER_TYPE
12357 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
12358 return error_mark_node;
12359
12360 if (code1 == POINTER_TYPE
12361 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
12362 return error_mark_node;
12363
12364 if ((invalid_op_diag
12365 = targetm.invalid_binary_op (code, type0, type1)))
12366 {
12367 error_at (location, invalid_op_diag);
12368 return error_mark_node;
12369 }
12370
12371 switch (code)
12372 {
12373 case PLUS_EXPR:
12374 case MINUS_EXPR:
12375 case MULT_EXPR:
12376 case TRUNC_DIV_EXPR:
12377 case CEIL_DIV_EXPR:
12378 case FLOOR_DIV_EXPR:
12379 case ROUND_DIV_EXPR:
12380 case EXACT_DIV_EXPR:
12381 may_need_excess_precision = true;
12382 break;
12383
12384 case EQ_EXPR:
12385 case NE_EXPR:
12386 case LE_EXPR:
12387 case GE_EXPR:
12388 case LT_EXPR:
12389 case GT_EXPR:
12390 /* Excess precision for implicit conversions of integers to
12391 floating point in C11 and later. */
12392 may_need_excess_precision = (flag_isoc11
12393 && (ANY_INTEGRAL_TYPE_P (type0)
12394 || ANY_INTEGRAL_TYPE_P (type1)));
12395 break;
12396
12397 default:
12398 may_need_excess_precision = false;
12399 break;
12400 }
12401 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
12402 {
12403 op0 = TREE_OPERAND (op0, 0);
12404 type0 = TREE_TYPE (op0);
12405 }
12406 else if (may_need_excess_precision
12407 && (eptype = excess_precision_type (type0)) != NULL_TREE)
12408 {
12409 type0 = eptype;
12410 op0 = convert (eptype, op0);
12411 }
12412 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
12413 {
12414 op1 = TREE_OPERAND (op1, 0);
12415 type1 = TREE_TYPE (op1);
12416 }
12417 else if (may_need_excess_precision
12418 && (eptype = excess_precision_type (type1)) != NULL_TREE)
12419 {
12420 type1 = eptype;
12421 op1 = convert (eptype, op1);
12422 }
12423
12424 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
12425
12426 /* In case when one of the operands of the binary operation is
12427 a vector and another is a scalar -- convert scalar to vector. */
12428 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
12429 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
12430 {
12431 enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
12432 orig_op1, true);
12433
12434 switch (convert_flag)
12435 {
12436 case stv_error:
12437 return error_mark_node;
12438 case stv_firstarg:
12439 {
12440 bool maybe_const = true;
12441 tree sc;
12442 sc = c_fully_fold (op0, false, &maybe_const);
12443 sc = save_expr (sc);
12444 sc = convert (TREE_TYPE (type1), sc);
12445 op0 = build_vector_from_val (type1, sc);
12446 if (!maybe_const)
12447 op0 = c_wrap_maybe_const (op0, true);
12448 orig_type0 = type0 = TREE_TYPE (op0);
12449 code0 = TREE_CODE (type0);
12450 converted = 1;
12451 break;
12452 }
12453 case stv_secondarg:
12454 {
12455 bool maybe_const = true;
12456 tree sc;
12457 sc = c_fully_fold (op1, false, &maybe_const);
12458 sc = save_expr (sc);
12459 sc = convert (TREE_TYPE (type0), sc);
12460 op1 = build_vector_from_val (type0, sc);
12461 if (!maybe_const)
12462 op1 = c_wrap_maybe_const (op1, true);
12463 orig_type1 = type1 = TREE_TYPE (op1);
12464 code1 = TREE_CODE (type1);
12465 converted = 1;
12466 break;
12467 }
12468 default:
12469 break;
12470 }
12471 }
12472
12473 switch (code)
12474 {
12475 case PLUS_EXPR:
12476 /* Handle the pointer + int case. */
12477 if (code0 == POINTER_TYPE
12478 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12479 {
12480 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
12481 goto return_build_binary_op;
12482 }
12483 else if (code1 == POINTER_TYPE
12484 && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE))
12485 {
12486 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
12487 goto return_build_binary_op;
12488 }
12489 else
12490 common = 1;
12491 break;
12492
12493 case MINUS_EXPR:
12494 /* Subtraction of two similar pointers.
12495 We must subtract them as integers, then divide by object size. */
12496 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
12497 && comp_target_types (location, type0, type1))
12498 {
12499 ret = pointer_diff (location, op0, op1, &instrument_expr);
12500 goto return_build_binary_op;
12501 }
12502 /* Handle pointer minus int. Just like pointer plus int. */
12503 else if (code0 == POINTER_TYPE
12504 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12505 {
12506 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12507 goto return_build_binary_op;
12508 }
12509 else
12510 common = 1;
12511 break;
12512
12513 case MULT_EXPR:
12514 common = 1;
12515 break;
12516
12517 case TRUNC_DIV_EXPR:
12518 case CEIL_DIV_EXPR:
12519 case FLOOR_DIV_EXPR:
12520 case ROUND_DIV_EXPR:
12521 case EXACT_DIV_EXPR:
12522 doing_div_or_mod = true;
12523 warn_for_div_by_zero (location, op1);
12524
12525 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12526 || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
12527 || code0 == COMPLEX_TYPE
12528 || gnu_vector_type_p (type0))
12529 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12530 || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
12531 || code1 == COMPLEX_TYPE
12532 || gnu_vector_type_p (type1)))
12533 {
12534 enum tree_code tcode0 = code0, tcode1 = code1;
12535
12536 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12537 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12538 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12539 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12540
12541 if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE)
12542 && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE))
12543 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12544 resultcode = RDIV_EXPR;
12545 else
12546 /* Although it would be tempting to shorten always here, that
12547 loses on some targets, since the modulo instruction is
12548 undefined if the quotient can't be represented in the
12549 computation mode. We shorten only if unsigned or if
12550 dividing by something we know != -1. */
12551 shorten = may_shorten_divmod (op0, op1);
12552 common = 1;
12553 }
12554 break;
12555
12556 case BIT_AND_EXPR:
12557 case BIT_IOR_EXPR:
12558 case BIT_XOR_EXPR:
12559 if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
12560 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12561 shorten = -1;
12562 /* Allow vector types which are not floating point types. */
12563 else if (gnu_vector_type_p (type0)
12564 && gnu_vector_type_p (type1)
12565 && !VECTOR_FLOAT_TYPE_P (type0)
12566 && !VECTOR_FLOAT_TYPE_P (type1))
12567 common = 1;
12568 break;
12569
12570 case TRUNC_MOD_EXPR:
12571 case FLOOR_MOD_EXPR:
12572 doing_div_or_mod = true;
12573 warn_for_div_by_zero (location, op1);
12574
12575 if (gnu_vector_type_p (type0)
12576 && gnu_vector_type_p (type1)
12577 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12578 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12579 common = 1;
12580 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
12581 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12582 {
12583 /* Although it would be tempting to shorten always here, that loses
12584 on some targets, since the modulo instruction is undefined if the
12585 quotient can't be represented in the computation mode. We shorten
12586 only if unsigned or if dividing by something we know != -1. */
12587 shorten = may_shorten_divmod (op0, op1);
12588 common = 1;
12589 }
12590 break;
12591
12592 case TRUTH_ANDIF_EXPR:
12593 case TRUTH_ORIF_EXPR:
12594 case TRUTH_AND_EXPR:
12595 case TRUTH_OR_EXPR:
12596 case TRUTH_XOR_EXPR:
12597 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12598 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12599 || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE
12600 || code0 == BITINT_TYPE)
12601 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12602 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12603 || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE
12604 || code1 == BITINT_TYPE))
12605 {
12606 /* Result of these operations is always an int,
12607 but that does not mean the operands should be
12608 converted to ints! */
12609 result_type = integer_type_node;
12610 if (op0_int_operands)
12611 {
12612 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12613 op0 = remove_c_maybe_const_expr (op0);
12614 }
12615 else
12616 op0 = c_objc_common_truthvalue_conversion (location, op0);
12617 if (op1_int_operands)
12618 {
12619 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12620 op1 = remove_c_maybe_const_expr (op1);
12621 }
12622 else
12623 op1 = c_objc_common_truthvalue_conversion (location, op1);
12624 converted = 1;
12625 boolean_op = true;
12626 }
12627 if (code == TRUTH_ANDIF_EXPR)
12628 {
12629 int_const_or_overflow = (int_operands
12630 && TREE_CODE (orig_op0) == INTEGER_CST
12631 && (op0 == truthvalue_false_node
12632 || TREE_CODE (orig_op1) == INTEGER_CST));
12633 int_const = (int_const_or_overflow
12634 && !TREE_OVERFLOW (orig_op0)
12635 && (op0 == truthvalue_false_node
12636 || !TREE_OVERFLOW (orig_op1)));
12637 }
12638 else if (code == TRUTH_ORIF_EXPR)
12639 {
12640 int_const_or_overflow = (int_operands
12641 && TREE_CODE (orig_op0) == INTEGER_CST
12642 && (op0 == truthvalue_true_node
12643 || TREE_CODE (orig_op1) == INTEGER_CST));
12644 int_const = (int_const_or_overflow
12645 && !TREE_OVERFLOW (orig_op0)
12646 && (op0 == truthvalue_true_node
12647 || !TREE_OVERFLOW (orig_op1)));
12648 }
12649 break;
12650
12651 /* Shift operations: result has same type as first operand;
12652 always convert second operand to int.
12653 Also set SHORT_SHIFT if shifting rightward. */
12654
12655 case RSHIFT_EXPR:
12656 if (gnu_vector_type_p (type0)
12657 && gnu_vector_type_p (type1)
12658 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12659 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12660 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12661 TYPE_VECTOR_SUBPARTS (type1)))
12662 {
12663 result_type = type0;
12664 converted = 1;
12665 }
12666 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12667 || code0 == BITINT_TYPE
12668 || (gnu_vector_type_p (type0)
12669 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12670 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12671 {
12672 doing_shift = true;
12673 if (TREE_CODE (op1) == INTEGER_CST)
12674 {
12675 if (tree_int_cst_sgn (op1) < 0)
12676 {
12677 int_const = false;
12678 if (c_inhibit_evaluation_warnings == 0)
12679 warning_at (location, OPT_Wshift_count_negative,
12680 "right shift count is negative");
12681 }
12682 else if (code0 == VECTOR_TYPE)
12683 {
12684 if (compare_tree_int (op1,
12685 TYPE_PRECISION (TREE_TYPE (type0)))
12686 >= 0)
12687 {
12688 int_const = false;
12689 if (c_inhibit_evaluation_warnings == 0)
12690 warning_at (location, OPT_Wshift_count_overflow,
12691 "right shift count >= width of vector element");
12692 }
12693 }
12694 else
12695 {
12696 if (!integer_zerop (op1))
12697 short_shift = 1;
12698
12699 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12700 {
12701 int_const = false;
12702 if (c_inhibit_evaluation_warnings == 0)
12703 warning_at (location, OPT_Wshift_count_overflow,
12704 "right shift count >= width of type");
12705 }
12706 }
12707 }
12708
12709 /* Use the type of the value to be shifted. */
12710 result_type = type0;
12711 /* Avoid converting op1 to result_type later. */
12712 converted = 1;
12713 }
12714 break;
12715
12716 case LSHIFT_EXPR:
12717 if (gnu_vector_type_p (type0)
12718 && gnu_vector_type_p (type1)
12719 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12720 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12721 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12722 TYPE_VECTOR_SUBPARTS (type1)))
12723 {
12724 result_type = type0;
12725 converted = 1;
12726 }
12727 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12728 || code0 == BITINT_TYPE
12729 || (gnu_vector_type_p (type0)
12730 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12731 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12732 {
12733 doing_shift = true;
12734 if (TREE_CODE (op0) == INTEGER_CST
12735 && tree_int_cst_sgn (op0) < 0
12736 && !TYPE_OVERFLOW_WRAPS (type0))
12737 {
12738 /* Don't reject a left shift of a negative value in a context
12739 where a constant expression is needed in C90. */
12740 if (flag_isoc99)
12741 int_const = false;
12742 if (c_inhibit_evaluation_warnings == 0)
12743 warning_at (location, OPT_Wshift_negative_value,
12744 "left shift of negative value");
12745 }
12746 if (TREE_CODE (op1) == INTEGER_CST)
12747 {
12748 if (tree_int_cst_sgn (op1) < 0)
12749 {
12750 int_const = false;
12751 if (c_inhibit_evaluation_warnings == 0)
12752 warning_at (location, OPT_Wshift_count_negative,
12753 "left shift count is negative");
12754 }
12755 else if (code0 == VECTOR_TYPE)
12756 {
12757 if (compare_tree_int (op1,
12758 TYPE_PRECISION (TREE_TYPE (type0)))
12759 >= 0)
12760 {
12761 int_const = false;
12762 if (c_inhibit_evaluation_warnings == 0)
12763 warning_at (location, OPT_Wshift_count_overflow,
12764 "left shift count >= width of vector element");
12765 }
12766 }
12767 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12768 {
12769 int_const = false;
12770 if (c_inhibit_evaluation_warnings == 0)
12771 warning_at (location, OPT_Wshift_count_overflow,
12772 "left shift count >= width of type");
12773 }
12774 else if (TREE_CODE (op0) == INTEGER_CST
12775 && maybe_warn_shift_overflow (location, op0, op1)
12776 && flag_isoc99)
12777 int_const = false;
12778 }
12779
12780 /* Use the type of the value to be shifted. */
12781 result_type = type0;
12782 /* Avoid converting op1 to result_type later. */
12783 converted = 1;
12784 }
12785 break;
12786
12787 case EQ_EXPR:
12788 case NE_EXPR:
12789 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12790 {
12791 tree intt;
12792 if (!vector_types_compatible_elements_p (type0, type1))
12793 {
12794 error_at (location, "comparing vectors with different "
12795 "element types");
12796 return error_mark_node;
12797 }
12798
12799 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12800 TYPE_VECTOR_SUBPARTS (type1)))
12801 {
12802 error_at (location, "comparing vectors with different "
12803 "number of elements");
12804 return error_mark_node;
12805 }
12806
12807 /* It's not precisely specified how the usual arithmetic
12808 conversions apply to the vector types. Here, we use
12809 the unsigned type if one of the operands is signed and
12810 the other one is unsigned. */
12811 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12812 {
12813 if (!TYPE_UNSIGNED (type0))
12814 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12815 else
12816 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12817 warning_at (location, OPT_Wsign_compare, "comparison between "
12818 "types %qT and %qT", type0, type1);
12819 }
12820
12821 /* Always construct signed integer vector type. */
12822 intt = c_common_type_for_size (GET_MODE_BITSIZE
12823 (SCALAR_TYPE_MODE
12824 (TREE_TYPE (type0))), 0);
12825 if (!intt)
12826 {
12827 error_at (location, "could not find an integer type "
12828 "of the same size as %qT",
12829 TREE_TYPE (type0));
12830 return error_mark_node;
12831 }
12832 result_type = build_opaque_vector_type (intt,
12833 TYPE_VECTOR_SUBPARTS (type0));
12834 converted = 1;
12835 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12836 goto return_build_binary_op;
12837 }
12838 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12839 warning_at (location,
12840 OPT_Wfloat_equal,
12841 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12842 /* Result of comparison is always int,
12843 but don't convert the args to int! */
12844 build_type = integer_type_node;
12845 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE
12846 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12847 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12848 || code1 == BITINT_TYPE
12849 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12850 short_compare = 1;
12851 else if (code0 == POINTER_TYPE
12852 && (code1 == NULLPTR_TYPE
12853 || null_pointer_constant_p (orig_op1)))
12854 {
12855 maybe_warn_for_null_address (location, op0, code);
12856 result_type = type0;
12857 }
12858 else if (code1 == POINTER_TYPE
12859 && (code0 == NULLPTR_TYPE
12860 || null_pointer_constant_p (orig_op0)))
12861 {
12862 maybe_warn_for_null_address (location, op1, code);
12863 result_type = type1;
12864 }
12865 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12866 {
12867 tree tt0 = TREE_TYPE (type0);
12868 tree tt1 = TREE_TYPE (type1);
12869 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12870 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12871 addr_space_t as_common = ADDR_SPACE_GENERIC;
12872
12873 /* Anything compares with void *. void * compares with anything.
12874 Otherwise, the targets must be compatible
12875 and both must be object or both incomplete. */
12876 if (comp_target_types (location, type0, type1))
12877 result_type = common_pointer_type (type0, type1);
12878 else if (!addr_space_superset (as0, as1, &as_common))
12879 {
12880 error_at (location, "comparison of pointers to "
12881 "disjoint address spaces");
12882 return error_mark_node;
12883 }
12884 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12885 {
12886 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12887 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12888 "comparison of %<void *%> with function pointer");
12889 }
12890 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12891 {
12892 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12893 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12894 "comparison of %<void *%> with function pointer");
12895 }
12896 else
12897 /* Avoid warning about the volatile ObjC EH puts on decls. */
12898 if (!objc_ok)
12899 pedwarn (location, OPT_Wcompare_distinct_pointer_types,
12900 "comparison of distinct pointer types lacks a cast");
12901
12902 if (result_type == NULL_TREE)
12903 {
12904 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12905 result_type = build_pointer_type
12906 (build_qualified_type (void_type_node, qual));
12907 }
12908 }
12909 else if (code0 == POINTER_TYPE
12910 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
12911 {
12912 result_type = type0;
12913 pedwarn (location, 0, "comparison between pointer and integer");
12914 }
12915 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
12916 && code1 == POINTER_TYPE)
12917 {
12918 result_type = type1;
12919 pedwarn (location, 0, "comparison between pointer and integer");
12920 }
12921 /* 6.5.9: One of the following shall hold:
12922 -- both operands have type nullptr_t; */
12923 else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
12924 {
12925 result_type = nullptr_type_node;
12926 /* No need to convert the operands to result_type later. */
12927 converted = 1;
12928 }
12929 /* -- one operand has type nullptr_t and the other is a null pointer
12930 constant. We will have to convert the former to the type of the
12931 latter, because during gimplification we can't have mismatching
12932 comparison operand type. We convert from nullptr_t to the other
12933 type, since only nullptr_t can be converted to nullptr_t. Also,
12934 even a constant 0 is a null pointer constant, so we may have to
12935 create a pointer type from its type. */
12936 else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
12937 result_type = (INTEGRAL_TYPE_P (type1)
12938 ? build_pointer_type (type1) : type1);
12939 else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
12940 result_type = (INTEGRAL_TYPE_P (type0)
12941 ? build_pointer_type (type0) : type0);
12942 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
12943 || truth_value_p (TREE_CODE (orig_op0)))
12944 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
12945 || truth_value_p (TREE_CODE (orig_op1))))
12946 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12947 break;
12948
12949 case LE_EXPR:
12950 case GE_EXPR:
12951 case LT_EXPR:
12952 case GT_EXPR:
12953 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12954 {
12955 tree intt;
12956 if (!vector_types_compatible_elements_p (type0, type1))
12957 {
12958 error_at (location, "comparing vectors with different "
12959 "element types");
12960 return error_mark_node;
12961 }
12962
12963 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12964 TYPE_VECTOR_SUBPARTS (type1)))
12965 {
12966 error_at (location, "comparing vectors with different "
12967 "number of elements");
12968 return error_mark_node;
12969 }
12970
12971 /* It's not precisely specified how the usual arithmetic
12972 conversions apply to the vector types. Here, we use
12973 the unsigned type if one of the operands is signed and
12974 the other one is unsigned. */
12975 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12976 {
12977 if (!TYPE_UNSIGNED (type0))
12978 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12979 else
12980 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12981 warning_at (location, OPT_Wsign_compare, "comparison between "
12982 "types %qT and %qT", type0, type1);
12983 }
12984
12985 /* Always construct signed integer vector type. */
12986 intt = c_common_type_for_size (GET_MODE_BITSIZE
12987 (SCALAR_TYPE_MODE
12988 (TREE_TYPE (type0))), 0);
12989 if (!intt)
12990 {
12991 error_at (location, "could not find an integer type "
12992 "of the same size as %qT",
12993 TREE_TYPE (type0));
12994 return error_mark_node;
12995 }
12996 result_type = build_opaque_vector_type (intt,
12997 TYPE_VECTOR_SUBPARTS (type0));
12998 converted = 1;
12999 ret = build_vec_cmp (resultcode, result_type, op0, op1);
13000 goto return_build_binary_op;
13001 }
13002 build_type = integer_type_node;
13003 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
13004 || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE)
13005 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
13006 || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE))
13007 short_compare = 1;
13008 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
13009 {
13010 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
13011 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
13012 addr_space_t as_common;
13013
13014 if (comp_target_types (location, type0, type1))
13015 {
13016 result_type = common_pointer_type (type0, type1);
13017 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
13018 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
13019 pedwarn_c99 (location, OPT_Wpedantic,
13020 "comparison of complete and incomplete pointers");
13021 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
13022 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
13023 "ordered comparisons of pointers to functions");
13024 else if (null_pointer_constant_p (orig_op0)
13025 || null_pointer_constant_p (orig_op1))
13026 warning_at (location, OPT_Wextra,
13027 "ordered comparison of pointer with null pointer");
13028
13029 }
13030 else if (!addr_space_superset (as0, as1, &as_common))
13031 {
13032 error_at (location, "comparison of pointers to "
13033 "disjoint address spaces");
13034 return error_mark_node;
13035 }
13036 else
13037 {
13038 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
13039 result_type = build_pointer_type
13040 (build_qualified_type (void_type_node, qual));
13041 pedwarn (location, OPT_Wcompare_distinct_pointer_types,
13042 "comparison of distinct pointer types lacks a cast");
13043 }
13044 }
13045 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
13046 {
13047 result_type = type0;
13048 if (pedantic)
13049 pedwarn (location, OPT_Wpedantic,
13050 "ordered comparison of pointer with integer zero");
13051 else if (extra_warnings)
13052 warning_at (location, OPT_Wextra,
13053 "ordered comparison of pointer with integer zero");
13054 }
13055 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
13056 {
13057 result_type = type1;
13058 if (pedantic)
13059 pedwarn (location, OPT_Wpedantic,
13060 "ordered comparison of pointer with integer zero");
13061 else if (extra_warnings)
13062 warning_at (location, OPT_Wextra,
13063 "ordered comparison of pointer with integer zero");
13064 }
13065 else if (code0 == POINTER_TYPE
13066 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
13067 {
13068 result_type = type0;
13069 pedwarn (location, 0, "comparison between pointer and integer");
13070 }
13071 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
13072 && code1 == POINTER_TYPE)
13073 {
13074 result_type = type1;
13075 pedwarn (location, 0, "comparison between pointer and integer");
13076 }
13077
13078 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
13079 && current_function_decl != NULL_TREE
13080 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
13081 {
13082 op0 = save_expr (op0);
13083 op1 = save_expr (op1);
13084
13085 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
13086 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
13087 }
13088
13089 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
13090 || truth_value_p (TREE_CODE (orig_op0)))
13091 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
13092 || truth_value_p (TREE_CODE (orig_op1))))
13093 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
13094 break;
13095
13096 case MIN_EXPR:
13097 case MAX_EXPR:
13098 /* Used for OpenMP atomics. */
13099 gcc_assert (flag_openmp);
13100 common = 1;
13101 break;
13102
13103 default:
13104 gcc_unreachable ();
13105 }
13106
13107 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
13108 return error_mark_node;
13109
13110 if (gnu_vector_type_p (type0)
13111 && gnu_vector_type_p (type1)
13112 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
13113 || !vector_types_compatible_elements_p (type0, type1)))
13114 {
13115 gcc_rich_location richloc (location);
13116 maybe_range_label_for_tree_type_mismatch
13117 label_for_op0 (orig_op0, orig_op1),
13118 label_for_op1 (orig_op1, orig_op0);
13119 richloc.maybe_add_expr (orig_op0, &label_for_op0);
13120 richloc.maybe_add_expr (orig_op1, &label_for_op1);
13121 binary_op_error (&richloc, code, type0, type1);
13122 return error_mark_node;
13123 }
13124
13125 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
13126 || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
13127 || gnu_vector_type_p (type0))
13128 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
13129 || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
13130 || gnu_vector_type_p (type1)))
13131 {
13132 bool first_complex = (code0 == COMPLEX_TYPE);
13133 bool second_complex = (code1 == COMPLEX_TYPE);
13134 int none_complex = (!first_complex && !second_complex);
13135
13136 if (shorten || common || short_compare)
13137 {
13138 result_type = c_common_type (type0, type1);
13139 do_warn_double_promotion (result_type, type0, type1,
13140 "implicit conversion from %qT to %qT "
13141 "to match other operand of binary "
13142 "expression",
13143 location);
13144 if (result_type == error_mark_node)
13145 return error_mark_node;
13146 }
13147
13148 if (first_complex != second_complex
13149 && (code == PLUS_EXPR
13150 || code == MINUS_EXPR
13151 || code == MULT_EXPR
13152 || (code == TRUNC_DIV_EXPR && first_complex))
13153 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
13154 && flag_signed_zeros)
13155 {
13156 /* An operation on mixed real/complex operands must be
13157 handled specially, but the language-independent code can
13158 more easily optimize the plain complex arithmetic if
13159 -fno-signed-zeros. */
13160 tree real_type = TREE_TYPE (result_type);
13161 tree real, imag;
13162 if (type0 != orig_type0 || type1 != orig_type1)
13163 {
13164 gcc_assert (may_need_excess_precision && common);
13165 semantic_result_type = c_common_type (orig_type0, orig_type1);
13166 }
13167 if (first_complex)
13168 {
13169 if (TREE_TYPE (op0) != result_type)
13170 op0 = convert_and_check (location, result_type, op0);
13171 if (TREE_TYPE (op1) != real_type)
13172 op1 = convert_and_check (location, real_type, op1);
13173 }
13174 else
13175 {
13176 if (TREE_TYPE (op0) != real_type)
13177 op0 = convert_and_check (location, real_type, op0);
13178 if (TREE_TYPE (op1) != result_type)
13179 op1 = convert_and_check (location, result_type, op1);
13180 }
13181 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13182 return error_mark_node;
13183 if (first_complex)
13184 {
13185 op0 = save_expr (op0);
13186 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
13187 op0, true);
13188 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
13189 op0, true);
13190 switch (code)
13191 {
13192 case MULT_EXPR:
13193 case TRUNC_DIV_EXPR:
13194 op1 = save_expr (op1);
13195 imag = build2 (resultcode, real_type, imag, op1);
13196 /* Fall through. */
13197 case PLUS_EXPR:
13198 case MINUS_EXPR:
13199 real = build2 (resultcode, real_type, real, op1);
13200 break;
13201 default:
13202 gcc_unreachable();
13203 }
13204 }
13205 else
13206 {
13207 op1 = save_expr (op1);
13208 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
13209 op1, true);
13210 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
13211 op1, true);
13212 switch (code)
13213 {
13214 case MULT_EXPR:
13215 op0 = save_expr (op0);
13216 imag = build2 (resultcode, real_type, op0, imag);
13217 /* Fall through. */
13218 case PLUS_EXPR:
13219 real = build2 (resultcode, real_type, op0, real);
13220 break;
13221 case MINUS_EXPR:
13222 real = build2 (resultcode, real_type, op0, real);
13223 imag = build1 (NEGATE_EXPR, real_type, imag);
13224 break;
13225 default:
13226 gcc_unreachable();
13227 }
13228 }
13229 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
13230 goto return_build_binary_op;
13231 }
13232
13233 /* For certain operations (which identify themselves by shorten != 0)
13234 if both args were extended from the same smaller type,
13235 do the arithmetic in that type and then extend.
13236
13237 shorten !=0 and !=1 indicates a bitwise operation.
13238 For them, this optimization is safe only if
13239 both args are zero-extended or both are sign-extended.
13240 Otherwise, we might change the result.
13241 Eg, (short)-1 | (unsigned short)-1 is (int)-1
13242 but calculated in (unsigned short) it would be (unsigned short)-1. */
13243
13244 if (shorten && none_complex)
13245 {
13246 final_type = result_type;
13247 result_type = shorten_binary_op (result_type, op0, op1,
13248 shorten == -1);
13249 }
13250
13251 /* Shifts can be shortened if shifting right. */
13252
13253 if (short_shift)
13254 {
13255 int unsigned_arg;
13256 tree arg0 = get_narrower (op0, &unsigned_arg);
13257
13258 final_type = result_type;
13259
13260 if (arg0 == op0 && final_type == TREE_TYPE (op0))
13261 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
13262
13263 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
13264 && tree_int_cst_sgn (op1) > 0
13265 /* We can shorten only if the shift count is less than the
13266 number of bits in the smaller type size. */
13267 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
13268 /* We cannot drop an unsigned shift after sign-extension. */
13269 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
13270 {
13271 /* Do an unsigned shift if the operand was zero-extended. */
13272 result_type
13273 = c_common_signed_or_unsigned_type (unsigned_arg,
13274 TREE_TYPE (arg0));
13275 /* Convert value-to-be-shifted to that type. */
13276 if (TREE_TYPE (op0) != result_type)
13277 op0 = convert (result_type, op0);
13278 converted = 1;
13279 }
13280 }
13281
13282 /* Comparison operations are shortened too but differently.
13283 They identify themselves by setting short_compare = 1. */
13284
13285 if (short_compare)
13286 {
13287 /* Don't write &op0, etc., because that would prevent op0
13288 from being kept in a register.
13289 Instead, make copies of the our local variables and
13290 pass the copies by reference, then copy them back afterward. */
13291 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
13292 enum tree_code xresultcode = resultcode;
13293 tree val
13294 = shorten_compare (location, &xop0, &xop1, &xresult_type,
13295 &xresultcode);
13296
13297 if (val != NULL_TREE)
13298 {
13299 ret = val;
13300 goto return_build_binary_op;
13301 }
13302
13303 op0 = xop0, op1 = xop1;
13304 converted = 1;
13305 resultcode = xresultcode;
13306
13307 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
13308 {
13309 bool op0_maybe_const = true;
13310 bool op1_maybe_const = true;
13311 tree orig_op0_folded, orig_op1_folded;
13312
13313 if (in_late_binary_op)
13314 {
13315 orig_op0_folded = orig_op0;
13316 orig_op1_folded = orig_op1;
13317 }
13318 else
13319 {
13320 /* Fold for the sake of possible warnings, as in
13321 build_conditional_expr. This requires the
13322 "original" values to be folded, not just op0 and
13323 op1. */
13324 c_inhibit_evaluation_warnings++;
13325 op0 = c_fully_fold (op0, require_constant_value,
13326 &op0_maybe_const);
13327 op1 = c_fully_fold (op1, require_constant_value,
13328 &op1_maybe_const);
13329 c_inhibit_evaluation_warnings--;
13330 orig_op0_folded = c_fully_fold (orig_op0,
13331 require_constant_value,
13332 NULL);
13333 orig_op1_folded = c_fully_fold (orig_op1,
13334 require_constant_value,
13335 NULL);
13336 }
13337
13338 if (warn_sign_compare)
13339 warn_for_sign_compare (location, orig_op0_folded,
13340 orig_op1_folded, op0, op1,
13341 result_type, resultcode);
13342 if (!in_late_binary_op && !int_operands)
13343 {
13344 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
13345 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
13346 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
13347 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
13348 }
13349 }
13350 }
13351 }
13352
13353 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
13354 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
13355 Then the expression will be built.
13356 It will be given type FINAL_TYPE if that is nonzero;
13357 otherwise, it will be given type RESULT_TYPE. */
13358
13359 if (!result_type)
13360 {
13361 /* Favor showing any expression locations that are available. */
13362 op_location_t oploc (location, UNKNOWN_LOCATION);
13363 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
13364 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
13365 return error_mark_node;
13366 }
13367
13368 if (build_type == NULL_TREE)
13369 {
13370 build_type = result_type;
13371 if ((type0 != orig_type0 || type1 != orig_type1)
13372 && !boolean_op)
13373 {
13374 gcc_assert (may_need_excess_precision && common);
13375 semantic_result_type = c_common_type (orig_type0, orig_type1);
13376 }
13377 }
13378
13379 if (!converted)
13380 {
13381 op0 = ep_convert_and_check (location, result_type, op0,
13382 semantic_result_type);
13383 op1 = ep_convert_and_check (location, result_type, op1,
13384 semantic_result_type);
13385
13386 /* This can happen if one operand has a vector type, and the other
13387 has a different type. */
13388 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13389 return error_mark_node;
13390 }
13391
13392 if (sanitize_flags_p ((SANITIZE_SHIFT
13393 | SANITIZE_DIVIDE
13394 | SANITIZE_FLOAT_DIVIDE
13395 | SANITIZE_SI_OVERFLOW))
13396 && current_function_decl != NULL_TREE
13397 && (doing_div_or_mod || doing_shift)
13398 && !require_constant_value)
13399 {
13400 /* OP0 and/or OP1 might have side-effects. */
13401 op0 = save_expr (op0);
13402 op1 = save_expr (op1);
13403 op0 = c_fully_fold (op0, false, NULL);
13404 op1 = c_fully_fold (op1, false, NULL);
13405 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
13406 | SANITIZE_FLOAT_DIVIDE
13407 | SANITIZE_SI_OVERFLOW))))
13408 instrument_expr = ubsan_instrument_division (location, op0, op1);
13409 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
13410 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
13411 }
13412
13413 /* Treat expressions in initializers specially as they can't trap. */
13414 if (int_const_or_overflow)
13415 ret = (require_constant_value
13416 ? fold_build2_initializer_loc (location, resultcode, build_type,
13417 op0, op1)
13418 : fold_build2_loc (location, resultcode, build_type, op0, op1));
13419 else
13420 ret = build2 (resultcode, build_type, op0, op1);
13421 if (final_type != NULL_TREE)
13422 ret = convert (final_type, ret);
13423
13424 return_build_binary_op:
13425 gcc_assert (ret != error_mark_node);
13426 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
13427 ret = (int_operands
13428 ? note_integer_operands (ret)
13429 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
13430 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
13431 && !in_late_binary_op)
13432 ret = note_integer_operands (ret);
13433 protected_set_expr_location (ret, location);
13434
13435 if (instrument_expr != NULL)
13436 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
13437 instrument_expr, ret);
13438
13439 if (semantic_result_type)
13440 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
13441 semantic_result_type, ret);
13442
13443 return ret;
13444 }
13445
13446
13447 /* Convert EXPR to be a truth-value, validating its type for this
13448 purpose. LOCATION is the source location for the expression. */
13449
13450 tree
13451 c_objc_common_truthvalue_conversion (location_t location, tree expr)
13452 {
13453 bool int_const, int_operands;
13454
13455 switch (TREE_CODE (TREE_TYPE (expr)))
13456 {
13457 case ARRAY_TYPE:
13458 error_at (location, "used array that cannot be converted to pointer where scalar is required");
13459 return error_mark_node;
13460
13461 case RECORD_TYPE:
13462 error_at (location, "used struct type value where scalar is required");
13463 return error_mark_node;
13464
13465 case UNION_TYPE:
13466 error_at (location, "used union type value where scalar is required");
13467 return error_mark_node;
13468
13469 case VOID_TYPE:
13470 error_at (location, "void value not ignored as it ought to be");
13471 return error_mark_node;
13472
13473 case POINTER_TYPE:
13474 if (reject_gcc_builtin (expr))
13475 return error_mark_node;
13476 break;
13477
13478 case FUNCTION_TYPE:
13479 gcc_unreachable ();
13480
13481 case VECTOR_TYPE:
13482 error_at (location, "used vector type where scalar is required");
13483 return error_mark_node;
13484
13485 default:
13486 break;
13487 }
13488
13489 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
13490 int_operands = EXPR_INT_CONST_OPERANDS (expr);
13491 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
13492 {
13493 expr = remove_c_maybe_const_expr (expr);
13494 expr = build2 (NE_EXPR, integer_type_node, expr,
13495 convert (TREE_TYPE (expr), integer_zero_node));
13496 expr = note_integer_operands (expr);
13497 }
13498 else
13499 /* ??? Should we also give an error for vectors rather than leaving
13500 those to give errors later? */
13501 expr = c_common_truthvalue_conversion (location, expr);
13502
13503 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
13504 {
13505 if (TREE_OVERFLOW (expr))
13506 return expr;
13507 else
13508 return note_integer_operands (expr);
13509 }
13510 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
13511 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13512 return expr;
13513 }
13514 \f
13515
13516 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
13517 required. */
13518
13519 tree
13520 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
13521 {
13522 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
13523 {
13524 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
13525 /* Executing a compound literal inside a function reinitializes
13526 it. */
13527 if (!TREE_STATIC (decl))
13528 *se = true;
13529 return decl;
13530 }
13531 else
13532 return expr;
13533 }
13534 \f
13535 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13536 statement. LOC is the location of the construct. */
13537
13538 tree
13539 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13540 tree clauses)
13541 {
13542 body = c_end_compound_stmt (loc, body, true);
13543
13544 tree stmt = make_node (code);
13545 TREE_TYPE (stmt) = void_type_node;
13546 OMP_BODY (stmt) = body;
13547 OMP_CLAUSES (stmt) = clauses;
13548 SET_EXPR_LOCATION (stmt, loc);
13549
13550 return add_stmt (stmt);
13551 }
13552
13553 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13554 statement. LOC is the location of the OACC_DATA. */
13555
13556 tree
13557 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13558 {
13559 tree stmt;
13560
13561 block = c_end_compound_stmt (loc, block, true);
13562
13563 stmt = make_node (OACC_DATA);
13564 TREE_TYPE (stmt) = void_type_node;
13565 OACC_DATA_CLAUSES (stmt) = clauses;
13566 OACC_DATA_BODY (stmt) = block;
13567 SET_EXPR_LOCATION (stmt, loc);
13568
13569 return add_stmt (stmt);
13570 }
13571
13572 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13573 statement. LOC is the location of the OACC_HOST_DATA. */
13574
13575 tree
13576 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13577 {
13578 tree stmt;
13579
13580 block = c_end_compound_stmt (loc, block, true);
13581
13582 stmt = make_node (OACC_HOST_DATA);
13583 TREE_TYPE (stmt) = void_type_node;
13584 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13585 OACC_HOST_DATA_BODY (stmt) = block;
13586 SET_EXPR_LOCATION (stmt, loc);
13587
13588 return add_stmt (stmt);
13589 }
13590
13591 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13592
13593 tree
13594 c_begin_omp_parallel (void)
13595 {
13596 tree block;
13597
13598 keep_next_level ();
13599 block = c_begin_compound_stmt (true);
13600
13601 return block;
13602 }
13603
13604 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13605 statement. LOC is the location of the OMP_PARALLEL. */
13606
13607 tree
13608 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13609 {
13610 tree stmt;
13611
13612 block = c_end_compound_stmt (loc, block, true);
13613
13614 stmt = make_node (OMP_PARALLEL);
13615 TREE_TYPE (stmt) = void_type_node;
13616 OMP_PARALLEL_CLAUSES (stmt) = clauses;
13617 OMP_PARALLEL_BODY (stmt) = block;
13618 SET_EXPR_LOCATION (stmt, loc);
13619
13620 return add_stmt (stmt);
13621 }
13622
13623 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13624
13625 tree
13626 c_begin_omp_task (void)
13627 {
13628 tree block;
13629
13630 keep_next_level ();
13631 block = c_begin_compound_stmt (true);
13632
13633 return block;
13634 }
13635
13636 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13637 statement. LOC is the location of the #pragma. */
13638
13639 tree
13640 c_finish_omp_task (location_t loc, tree clauses, tree block)
13641 {
13642 tree stmt;
13643
13644 block = c_end_compound_stmt (loc, block, true);
13645
13646 stmt = make_node (OMP_TASK);
13647 TREE_TYPE (stmt) = void_type_node;
13648 OMP_TASK_CLAUSES (stmt) = clauses;
13649 OMP_TASK_BODY (stmt) = block;
13650 SET_EXPR_LOCATION (stmt, loc);
13651
13652 return add_stmt (stmt);
13653 }
13654
13655 /* Generate GOMP_cancel call for #pragma omp cancel. */
13656
13657 void
13658 c_finish_omp_cancel (location_t loc, tree clauses)
13659 {
13660 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13661 int mask = 0;
13662 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13663 mask = 1;
13664 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13665 mask = 2;
13666 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13667 mask = 4;
13668 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13669 mask = 8;
13670 else
13671 {
13672 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13673 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13674 "clauses");
13675 return;
13676 }
13677 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13678 if (ifc != NULL_TREE)
13679 {
13680 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13681 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13682 error_at (OMP_CLAUSE_LOCATION (ifc),
13683 "expected %<cancel%> %<if%> clause modifier");
13684 else
13685 {
13686 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13687 if (ifc2 != NULL_TREE)
13688 {
13689 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13690 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13691 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13692 error_at (OMP_CLAUSE_LOCATION (ifc2),
13693 "expected %<cancel%> %<if%> clause modifier");
13694 }
13695 }
13696
13697 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13698 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13699 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13700 build_zero_cst (type));
13701 }
13702 else
13703 ifc = boolean_true_node;
13704 tree stmt = build_call_expr_loc (loc, fn, 2,
13705 build_int_cst (integer_type_node, mask),
13706 ifc);
13707 add_stmt (stmt);
13708 }
13709
13710 /* Generate GOMP_cancellation_point call for
13711 #pragma omp cancellation point. */
13712
13713 void
13714 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13715 {
13716 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13717 int mask = 0;
13718 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13719 mask = 1;
13720 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13721 mask = 2;
13722 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13723 mask = 4;
13724 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13725 mask = 8;
13726 else
13727 {
13728 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13729 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13730 "clauses");
13731 return;
13732 }
13733 tree stmt = build_call_expr_loc (loc, fn, 1,
13734 build_int_cst (integer_type_node, mask));
13735 add_stmt (stmt);
13736 }
13737
13738 /* Helper function for handle_omp_array_sections. Called recursively
13739 to handle multiple array-section-subscripts. C is the clause,
13740 T current expression (initially OMP_CLAUSE_DECL), which is either
13741 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13742 expression if specified, TREE_VALUE length expression if specified,
13743 TREE_CHAIN is what it has been specified after, or some decl.
13744 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13745 set to true if any of the array-section-subscript could have length
13746 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13747 first array-section-subscript which is known not to have length
13748 of one. Given say:
13749 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13750 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13751 all are or may have length of 1, array-section-subscript [:2] is the
13752 first one known not to have length 1. For array-section-subscript
13753 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13754 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13755 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13756 case though, as some lengths could be zero. */
13757
13758 static tree
13759 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13760 bool &maybe_zero_len, unsigned int &first_non_one,
13761 enum c_omp_region_type ort)
13762 {
13763 tree ret, low_bound, length, type;
13764 bool openacc = (ort & C_ORT_ACC) != 0;
13765 if (TREE_CODE (t) != TREE_LIST)
13766 {
13767 if (error_operand_p (t))
13768 return error_mark_node;
13769 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
13770 ret = t;
13771 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13772 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13773 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13774 {
13775 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13776 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13777 return error_mark_node;
13778 }
13779 if (!ai.check_clause (c))
13780 return error_mark_node;
13781 else if (ai.component_access_p ()
13782 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13783 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13784 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13785 t = ai.get_root_term (true);
13786 else
13787 t = ai.unconverted_ref_origin ();
13788 if (t == error_mark_node)
13789 return error_mark_node;
13790 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13791 {
13792 if (DECL_P (t))
13793 error_at (OMP_CLAUSE_LOCATION (c),
13794 "%qD is not a variable in %qs clause", t,
13795 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13796 else
13797 error_at (OMP_CLAUSE_LOCATION (c),
13798 "%qE is not a variable in %qs clause", t,
13799 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13800 return error_mark_node;
13801 }
13802 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13803 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13804 && TYPE_ATOMIC (TREE_TYPE (t)))
13805 {
13806 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13807 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13808 return error_mark_node;
13809 }
13810 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13811 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13812 && VAR_P (t)
13813 && DECL_THREAD_LOCAL_P (t))
13814 {
13815 error_at (OMP_CLAUSE_LOCATION (c),
13816 "%qD is threadprivate variable in %qs clause", t,
13817 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13818 return error_mark_node;
13819 }
13820 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13821 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13822 && TYPE_ATOMIC (TREE_TYPE (t))
13823 && POINTER_TYPE_P (TREE_TYPE (t)))
13824 {
13825 /* If the array section is pointer based and the pointer
13826 itself is _Atomic qualified, we need to atomically load
13827 the pointer. */
13828 c_expr expr;
13829 memset (&expr, 0, sizeof (expr));
13830 expr.value = ret;
13831 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13832 expr, false, false);
13833 ret = expr.value;
13834 }
13835 return ret;
13836 }
13837
13838 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13839 maybe_zero_len, first_non_one, ort);
13840 if (ret == error_mark_node || ret == NULL_TREE)
13841 return ret;
13842
13843 type = TREE_TYPE (ret);
13844 low_bound = TREE_PURPOSE (t);
13845 length = TREE_VALUE (t);
13846
13847 if (low_bound == error_mark_node || length == error_mark_node)
13848 return error_mark_node;
13849
13850 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13851 {
13852 error_at (OMP_CLAUSE_LOCATION (c),
13853 "low bound %qE of array section does not have integral type",
13854 low_bound);
13855 return error_mark_node;
13856 }
13857 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13858 {
13859 error_at (OMP_CLAUSE_LOCATION (c),
13860 "length %qE of array section does not have integral type",
13861 length);
13862 return error_mark_node;
13863 }
13864 if (low_bound
13865 && TREE_CODE (low_bound) == INTEGER_CST
13866 && TYPE_PRECISION (TREE_TYPE (low_bound))
13867 > TYPE_PRECISION (sizetype))
13868 low_bound = fold_convert (sizetype, low_bound);
13869 if (length
13870 && TREE_CODE (length) == INTEGER_CST
13871 && TYPE_PRECISION (TREE_TYPE (length))
13872 > TYPE_PRECISION (sizetype))
13873 length = fold_convert (sizetype, length);
13874 if (low_bound == NULL_TREE)
13875 low_bound = integer_zero_node;
13876 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13877 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13878 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13879 {
13880 if (length != integer_one_node)
13881 {
13882 error_at (OMP_CLAUSE_LOCATION (c),
13883 "expected single pointer in %qs clause",
13884 user_omp_clause_code_name (c, openacc));
13885 return error_mark_node;
13886 }
13887 }
13888 if (length != NULL_TREE)
13889 {
13890 if (!integer_nonzerop (length))
13891 {
13892 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13893 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13894 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13895 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13896 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13897 {
13898 if (integer_zerop (length))
13899 {
13900 error_at (OMP_CLAUSE_LOCATION (c),
13901 "zero length array section in %qs clause",
13902 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13903 return error_mark_node;
13904 }
13905 }
13906 else
13907 maybe_zero_len = true;
13908 }
13909 if (first_non_one == types.length ()
13910 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13911 first_non_one++;
13912 }
13913 if (TREE_CODE (type) == ARRAY_TYPE)
13914 {
13915 if (length == NULL_TREE
13916 && (TYPE_DOMAIN (type) == NULL_TREE
13917 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13918 {
13919 error_at (OMP_CLAUSE_LOCATION (c),
13920 "for unknown bound array type length expression must "
13921 "be specified");
13922 return error_mark_node;
13923 }
13924 if (TREE_CODE (low_bound) == INTEGER_CST
13925 && tree_int_cst_sgn (low_bound) == -1)
13926 {
13927 error_at (OMP_CLAUSE_LOCATION (c),
13928 "negative low bound in array section in %qs clause",
13929 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13930 return error_mark_node;
13931 }
13932 if (length != NULL_TREE
13933 && TREE_CODE (length) == INTEGER_CST
13934 && tree_int_cst_sgn (length) == -1)
13935 {
13936 error_at (OMP_CLAUSE_LOCATION (c),
13937 "negative length in array section in %qs clause",
13938 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13939 return error_mark_node;
13940 }
13941 if (TYPE_DOMAIN (type)
13942 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13943 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13944 == INTEGER_CST)
13945 {
13946 tree size
13947 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13948 size = size_binop (PLUS_EXPR, size, size_one_node);
13949 if (TREE_CODE (low_bound) == INTEGER_CST)
13950 {
13951 if (tree_int_cst_lt (size, low_bound))
13952 {
13953 error_at (OMP_CLAUSE_LOCATION (c),
13954 "low bound %qE above array section size "
13955 "in %qs clause", low_bound,
13956 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13957 return error_mark_node;
13958 }
13959 if (tree_int_cst_equal (size, low_bound))
13960 {
13961 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13962 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13963 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13964 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13965 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13966 {
13967 error_at (OMP_CLAUSE_LOCATION (c),
13968 "zero length array section in %qs clause",
13969 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13970 return error_mark_node;
13971 }
13972 maybe_zero_len = true;
13973 }
13974 else if (length == NULL_TREE
13975 && first_non_one == types.length ()
13976 && tree_int_cst_equal
13977 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13978 low_bound))
13979 first_non_one++;
13980 }
13981 else if (length == NULL_TREE)
13982 {
13983 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13984 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13985 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13986 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13987 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13988 maybe_zero_len = true;
13989 if (first_non_one == types.length ())
13990 first_non_one++;
13991 }
13992 if (length && TREE_CODE (length) == INTEGER_CST)
13993 {
13994 if (tree_int_cst_lt (size, length))
13995 {
13996 error_at (OMP_CLAUSE_LOCATION (c),
13997 "length %qE above array section size "
13998 "in %qs clause", length,
13999 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14000 return error_mark_node;
14001 }
14002 if (TREE_CODE (low_bound) == INTEGER_CST)
14003 {
14004 tree lbpluslen
14005 = size_binop (PLUS_EXPR,
14006 fold_convert (sizetype, low_bound),
14007 fold_convert (sizetype, length));
14008 if (TREE_CODE (lbpluslen) == INTEGER_CST
14009 && tree_int_cst_lt (size, lbpluslen))
14010 {
14011 error_at (OMP_CLAUSE_LOCATION (c),
14012 "high bound %qE above array section size "
14013 "in %qs clause", lbpluslen,
14014 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14015 return error_mark_node;
14016 }
14017 }
14018 }
14019 }
14020 else if (length == NULL_TREE)
14021 {
14022 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
14023 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
14024 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14025 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
14026 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
14027 maybe_zero_len = true;
14028 if (first_non_one == types.length ())
14029 first_non_one++;
14030 }
14031
14032 /* For [lb:] we will need to evaluate lb more than once. */
14033 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
14034 {
14035 tree lb = save_expr (low_bound);
14036 if (lb != low_bound)
14037 {
14038 TREE_PURPOSE (t) = lb;
14039 low_bound = lb;
14040 }
14041 }
14042 }
14043 else if (TREE_CODE (type) == POINTER_TYPE)
14044 {
14045 if (length == NULL_TREE)
14046 {
14047 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
14048 error_at (OMP_CLAUSE_LOCATION (c),
14049 "for array function parameter length expression "
14050 "must be specified");
14051 else
14052 error_at (OMP_CLAUSE_LOCATION (c),
14053 "for pointer type length expression must be specified");
14054 return error_mark_node;
14055 }
14056 if (length != NULL_TREE
14057 && TREE_CODE (length) == INTEGER_CST
14058 && tree_int_cst_sgn (length) == -1)
14059 {
14060 error_at (OMP_CLAUSE_LOCATION (c),
14061 "negative length in array section in %qs clause",
14062 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14063 return error_mark_node;
14064 }
14065 /* If there is a pointer type anywhere but in the very first
14066 array-section-subscript, the array section could be non-contiguous. */
14067 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
14068 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
14069 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
14070 {
14071 /* If any prior dimension has a non-one length, then deem this
14072 array section as non-contiguous. */
14073 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
14074 d = TREE_CHAIN (d))
14075 {
14076 tree d_length = TREE_VALUE (d);
14077 if (d_length == NULL_TREE || !integer_onep (d_length))
14078 {
14079 error_at (OMP_CLAUSE_LOCATION (c),
14080 "array section is not contiguous in %qs clause",
14081 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14082 return error_mark_node;
14083 }
14084 }
14085 }
14086 }
14087 else
14088 {
14089 error_at (OMP_CLAUSE_LOCATION (c),
14090 "%qE does not have pointer or array type", ret);
14091 return error_mark_node;
14092 }
14093 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
14094 types.safe_push (TREE_TYPE (ret));
14095 /* We will need to evaluate lb more than once. */
14096 tree lb = save_expr (low_bound);
14097 if (lb != low_bound)
14098 {
14099 TREE_PURPOSE (t) = lb;
14100 low_bound = lb;
14101 }
14102 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
14103 return ret;
14104 }
14105
14106 /* Handle array sections for clause C. */
14107
14108 static bool
14109 handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
14110 {
14111 bool maybe_zero_len = false;
14112 unsigned int first_non_one = 0;
14113 auto_vec<tree, 10> types;
14114 tree *tp = &OMP_CLAUSE_DECL (c);
14115 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14116 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14117 && TREE_CODE (*tp) == TREE_LIST
14118 && TREE_PURPOSE (*tp)
14119 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
14120 tp = &TREE_VALUE (*tp);
14121 tree first = handle_omp_array_sections_1 (c, *tp, types,
14122 maybe_zero_len, first_non_one,
14123 ort);
14124 if (first == error_mark_node)
14125 return true;
14126 if (first == NULL_TREE)
14127 return false;
14128 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14129 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14130 {
14131 tree t = *tp;
14132 tree tem = NULL_TREE;
14133 /* Need to evaluate side effects in the length expressions
14134 if any. */
14135 while (TREE_CODE (t) == TREE_LIST)
14136 {
14137 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
14138 {
14139 if (tem == NULL_TREE)
14140 tem = TREE_VALUE (t);
14141 else
14142 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
14143 TREE_VALUE (t), tem);
14144 }
14145 t = TREE_CHAIN (t);
14146 }
14147 if (tem)
14148 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
14149 first = c_fully_fold (first, false, NULL, true);
14150 *tp = first;
14151 }
14152 else
14153 {
14154 unsigned int num = types.length (), i;
14155 tree t, side_effects = NULL_TREE, size = NULL_TREE;
14156 tree condition = NULL_TREE;
14157
14158 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
14159 maybe_zero_len = true;
14160
14161 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
14162 t = TREE_CHAIN (t))
14163 {
14164 tree low_bound = TREE_PURPOSE (t);
14165 tree length = TREE_VALUE (t);
14166
14167 i--;
14168 if (low_bound
14169 && TREE_CODE (low_bound) == INTEGER_CST
14170 && TYPE_PRECISION (TREE_TYPE (low_bound))
14171 > TYPE_PRECISION (sizetype))
14172 low_bound = fold_convert (sizetype, low_bound);
14173 if (length
14174 && TREE_CODE (length) == INTEGER_CST
14175 && TYPE_PRECISION (TREE_TYPE (length))
14176 > TYPE_PRECISION (sizetype))
14177 length = fold_convert (sizetype, length);
14178 if (low_bound == NULL_TREE)
14179 low_bound = integer_zero_node;
14180 if (!maybe_zero_len && i > first_non_one)
14181 {
14182 if (integer_nonzerop (low_bound))
14183 goto do_warn_noncontiguous;
14184 if (length != NULL_TREE
14185 && TREE_CODE (length) == INTEGER_CST
14186 && TYPE_DOMAIN (types[i])
14187 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
14188 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
14189 == INTEGER_CST)
14190 {
14191 tree size;
14192 size = size_binop (PLUS_EXPR,
14193 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14194 size_one_node);
14195 if (!tree_int_cst_equal (length, size))
14196 {
14197 do_warn_noncontiguous:
14198 error_at (OMP_CLAUSE_LOCATION (c),
14199 "array section is not contiguous in %qs "
14200 "clause",
14201 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14202 return true;
14203 }
14204 }
14205 if (length != NULL_TREE
14206 && TREE_SIDE_EFFECTS (length))
14207 {
14208 if (side_effects == NULL_TREE)
14209 side_effects = length;
14210 else
14211 side_effects = build2 (COMPOUND_EXPR,
14212 TREE_TYPE (side_effects),
14213 length, side_effects);
14214 }
14215 }
14216 else
14217 {
14218 tree l;
14219
14220 if (i > first_non_one
14221 && ((length && integer_nonzerop (length))
14222 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14223 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14224 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
14225 continue;
14226 if (length)
14227 l = fold_convert (sizetype, length);
14228 else
14229 {
14230 l = size_binop (PLUS_EXPR,
14231 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14232 size_one_node);
14233 l = size_binop (MINUS_EXPR, l,
14234 fold_convert (sizetype, low_bound));
14235 }
14236 if (i > first_non_one)
14237 {
14238 l = fold_build2 (NE_EXPR, boolean_type_node, l,
14239 size_zero_node);
14240 if (condition == NULL_TREE)
14241 condition = l;
14242 else
14243 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
14244 l, condition);
14245 }
14246 else if (size == NULL_TREE)
14247 {
14248 size = size_in_bytes (TREE_TYPE (types[i]));
14249 tree eltype = TREE_TYPE (types[num - 1]);
14250 while (TREE_CODE (eltype) == ARRAY_TYPE)
14251 eltype = TREE_TYPE (eltype);
14252 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14253 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14254 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14255 {
14256 if (integer_zerop (size)
14257 || integer_zerop (size_in_bytes (eltype)))
14258 {
14259 error_at (OMP_CLAUSE_LOCATION (c),
14260 "zero length array section in %qs clause",
14261 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14262 return error_mark_node;
14263 }
14264 size = size_binop (EXACT_DIV_EXPR, size,
14265 size_in_bytes (eltype));
14266 }
14267 size = size_binop (MULT_EXPR, size, l);
14268 if (condition)
14269 size = fold_build3 (COND_EXPR, sizetype, condition,
14270 size, size_zero_node);
14271 }
14272 else
14273 size = size_binop (MULT_EXPR, size, l);
14274 }
14275 }
14276 if (side_effects)
14277 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
14278 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14279 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14280 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14281 {
14282 size = size_binop (MINUS_EXPR, size, size_one_node);
14283 size = c_fully_fold (size, false, NULL);
14284 size = save_expr (size);
14285 tree index_type = build_index_type (size);
14286 tree eltype = TREE_TYPE (first);
14287 while (TREE_CODE (eltype) == ARRAY_TYPE)
14288 eltype = TREE_TYPE (eltype);
14289 tree type = build_array_type (eltype, index_type);
14290 tree ptype = build_pointer_type (eltype);
14291 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14292 t = build_fold_addr_expr (t);
14293 tree t2 = build_fold_addr_expr (first);
14294 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14295 ptrdiff_type_node, t2);
14296 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14297 ptrdiff_type_node, t2,
14298 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14299 ptrdiff_type_node, t));
14300 t2 = c_fully_fold (t2, false, NULL);
14301 if (tree_fits_shwi_p (t2))
14302 t = build2 (MEM_REF, type, t,
14303 build_int_cst (ptype, tree_to_shwi (t2)));
14304 else
14305 {
14306 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
14307 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
14308 TREE_TYPE (t), t, t2);
14309 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
14310 }
14311 OMP_CLAUSE_DECL (c) = t;
14312 return false;
14313 }
14314 first = c_fully_fold (first, false, NULL);
14315 OMP_CLAUSE_DECL (c) = first;
14316 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
14317 return false;
14318 /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */
14319 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14320 || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
14321 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
14322 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
14323 {
14324 if (size)
14325 size = c_fully_fold (size, false, NULL);
14326 OMP_CLAUSE_SIZE (c) = size;
14327 }
14328
14329 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14330 return false;
14331
14332 auto_vec<omp_addr_token *, 10> addr_tokens;
14333
14334 if (!omp_parse_expr (addr_tokens, first))
14335 return true;
14336
14337 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
14338
14339 tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
14340 if (nc != error_mark_node)
14341 {
14342 using namespace omp_addr_tokenizer;
14343
14344 if (ai.maybe_zero_length_array_section (c))
14345 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
14346
14347 /* !!! If we're accessing a base decl via chained access
14348 methods (e.g. multiple indirections), duplicate clause
14349 detection won't work properly. Skip it in that case. */
14350 if ((addr_tokens[0]->type == STRUCTURE_BASE
14351 || addr_tokens[0]->type == ARRAY_BASE)
14352 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
14353 && addr_tokens[1]->type == ACCESS_METHOD
14354 && omp_access_chain_p (addr_tokens, 1))
14355 c = nc;
14356
14357 return false;
14358 }
14359 }
14360 return false;
14361 }
14362
14363 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
14364 an inline call. But, remap
14365 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
14366 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
14367
14368 static tree
14369 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
14370 tree decl, tree placeholder)
14371 {
14372 copy_body_data id;
14373 hash_map<tree, tree> decl_map;
14374
14375 decl_map.put (omp_decl1, placeholder);
14376 decl_map.put (omp_decl2, decl);
14377 memset (&id, 0, sizeof (id));
14378 id.src_fn = DECL_CONTEXT (omp_decl1);
14379 id.dst_fn = current_function_decl;
14380 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
14381 id.decl_map = &decl_map;
14382
14383 id.copy_decl = copy_decl_no_change;
14384 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
14385 id.transform_new_cfg = true;
14386 id.transform_return_to_modify = false;
14387 id.eh_lp_nr = 0;
14388 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
14389 return stmt;
14390 }
14391
14392 /* Helper function of c_finish_omp_clauses, called via walk_tree.
14393 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
14394
14395 static tree
14396 c_find_omp_placeholder_r (tree *tp, int *, void *data)
14397 {
14398 if (*tp == (tree) data)
14399 return *tp;
14400 return NULL_TREE;
14401 }
14402
14403 /* Similarly, but also walk aggregate fields. */
14404
14405 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
14406
14407 static tree
14408 c_find_omp_var_r (tree *tp, int *, void *data)
14409 {
14410 if (*tp == ((struct c_find_omp_var_s *) data)->var)
14411 return *tp;
14412 if (RECORD_OR_UNION_TYPE_P (*tp))
14413 {
14414 tree field;
14415 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
14416
14417 for (field = TYPE_FIELDS (*tp); field;
14418 field = DECL_CHAIN (field))
14419 if (TREE_CODE (field) == FIELD_DECL)
14420 {
14421 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
14422 c_find_omp_var_r, data, pset);
14423 if (ret)
14424 return ret;
14425 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
14426 if (ret)
14427 return ret;
14428 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
14429 pset);
14430 if (ret)
14431 return ret;
14432 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
14433 if (ret)
14434 return ret;
14435 }
14436 }
14437 else if (INTEGRAL_TYPE_P (*tp))
14438 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
14439 ((struct c_find_omp_var_s *) data)->pset);
14440 return NULL_TREE;
14441 }
14442
14443 /* Finish OpenMP iterators ITER. Return true if they are errorneous
14444 and clauses containing them should be removed. */
14445
14446 static bool
14447 c_omp_finish_iterators (tree iter)
14448 {
14449 bool ret = false;
14450 for (tree it = iter; it; it = TREE_CHAIN (it))
14451 {
14452 tree var = TREE_VEC_ELT (it, 0);
14453 tree begin = TREE_VEC_ELT (it, 1);
14454 tree end = TREE_VEC_ELT (it, 2);
14455 tree step = TREE_VEC_ELT (it, 3);
14456 tree orig_step;
14457 tree type = TREE_TYPE (var);
14458 location_t loc = DECL_SOURCE_LOCATION (var);
14459 if (type == error_mark_node)
14460 {
14461 ret = true;
14462 continue;
14463 }
14464 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14465 {
14466 error_at (loc, "iterator %qD has neither integral nor pointer type",
14467 var);
14468 ret = true;
14469 continue;
14470 }
14471 else if (TYPE_ATOMIC (type))
14472 {
14473 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
14474 ret = true;
14475 continue;
14476 }
14477 else if (TYPE_READONLY (type))
14478 {
14479 error_at (loc, "iterator %qD has const qualified type", var);
14480 ret = true;
14481 continue;
14482 }
14483 else if (step == error_mark_node
14484 || TREE_TYPE (step) == error_mark_node)
14485 {
14486 ret = true;
14487 continue;
14488 }
14489 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14490 {
14491 error_at (EXPR_LOC_OR_LOC (step, loc),
14492 "iterator step with non-integral type");
14493 ret = true;
14494 continue;
14495 }
14496 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14497 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14498 orig_step = save_expr (c_fully_fold (step, false, NULL));
14499 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14500 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14501 if (POINTER_TYPE_P (type))
14502 {
14503 begin = save_expr (begin);
14504 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14505 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14506 fold_convert (sizetype, step),
14507 fold_convert (sizetype, begin));
14508 step = fold_convert (ssizetype, step);
14509 }
14510 if (integer_zerop (step))
14511 {
14512 error_at (loc, "iterator %qD has zero step", var);
14513 ret = true;
14514 continue;
14515 }
14516
14517 if (begin == error_mark_node
14518 || end == error_mark_node
14519 || step == error_mark_node
14520 || orig_step == error_mark_node)
14521 {
14522 ret = true;
14523 continue;
14524 }
14525 hash_set<tree> pset;
14526 tree it2;
14527 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14528 {
14529 tree var2 = TREE_VEC_ELT (it2, 0);
14530 tree begin2 = TREE_VEC_ELT (it2, 1);
14531 tree end2 = TREE_VEC_ELT (it2, 2);
14532 tree step2 = TREE_VEC_ELT (it2, 3);
14533 tree type2 = TREE_TYPE (var2);
14534 location_t loc2 = DECL_SOURCE_LOCATION (var2);
14535 struct c_find_omp_var_s data = { var, &pset };
14536 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14537 {
14538 error_at (loc2,
14539 "type of iterator %qD refers to outer iterator %qD",
14540 var2, var);
14541 break;
14542 }
14543 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14544 {
14545 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14546 "begin expression refers to outer iterator %qD", var);
14547 break;
14548 }
14549 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14550 {
14551 error_at (EXPR_LOC_OR_LOC (end2, loc2),
14552 "end expression refers to outer iterator %qD", var);
14553 break;
14554 }
14555 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14556 {
14557 error_at (EXPR_LOC_OR_LOC (step2, loc2),
14558 "step expression refers to outer iterator %qD", var);
14559 break;
14560 }
14561 }
14562 if (it2)
14563 {
14564 ret = true;
14565 continue;
14566 }
14567 TREE_VEC_ELT (it, 1) = begin;
14568 TREE_VEC_ELT (it, 2) = end;
14569 TREE_VEC_ELT (it, 3) = step;
14570 TREE_VEC_ELT (it, 4) = orig_step;
14571 }
14572 return ret;
14573 }
14574
14575 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14576 Return true if an error has been detected. */
14577
14578 static bool
14579 c_oacc_check_attachments (tree c)
14580 {
14581 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14582 return false;
14583
14584 /* OpenACC attach / detach clauses must be pointers. */
14585 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14586 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14587 {
14588 tree t = OMP_CLAUSE_DECL (c);
14589
14590 while (TREE_CODE (t) == TREE_LIST)
14591 t = TREE_CHAIN (t);
14592
14593 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14594 {
14595 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14596 user_omp_clause_code_name (c, true));
14597 return true;
14598 }
14599 }
14600
14601 return false;
14602 }
14603
14604 /* For all elements of CLAUSES, validate them against their constraints.
14605 Remove any elements from the list that are invalid. */
14606
14607 tree
14608 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14609 {
14610 bitmap_head generic_head, firstprivate_head, lastprivate_head;
14611 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14612 bitmap_head oacc_reduction_head, is_on_device_head;
14613 tree c, t, type, *pc;
14614 tree simdlen = NULL_TREE, safelen = NULL_TREE;
14615 bool branch_seen = false;
14616 bool copyprivate_seen = false;
14617 bool mergeable_seen = false;
14618 tree *detach_seen = NULL;
14619 bool linear_variable_step_check = false;
14620 tree *nowait_clause = NULL;
14621 tree ordered_clause = NULL_TREE;
14622 tree schedule_clause = NULL_TREE;
14623 bool oacc_async = false;
14624 tree last_iterators = NULL_TREE;
14625 bool last_iterators_remove = false;
14626 tree *nogroup_seen = NULL;
14627 tree *order_clause = NULL;
14628 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14629 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14630 int reduction_seen = 0;
14631 bool allocate_seen = false;
14632 bool implicit_moved = false;
14633 bool target_in_reduction_seen = false;
14634 bool openacc = (ort & C_ORT_ACC) != 0;
14635
14636 bitmap_obstack_initialize (NULL);
14637 bitmap_initialize (&generic_head, &bitmap_default_obstack);
14638 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14639 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14640 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14641 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14642 bitmap_initialize (&map_head, &bitmap_default_obstack);
14643 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14644 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14645 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14646 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14647 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14648 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14649
14650 if (openacc)
14651 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14652 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14653 {
14654 oacc_async = true;
14655 break;
14656 }
14657
14658 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
14659
14660 for (pc = &clauses, c = clauses; c ; c = *pc)
14661 {
14662 bool remove = false;
14663 bool need_complete = false;
14664 bool need_implicitly_determined = false;
14665
14666 /* We've reached the end of a list of expanded nodes. Reset the group
14667 start pointer. */
14668 if (c == grp_sentinel)
14669 grp_start_p = NULL;
14670
14671 switch (OMP_CLAUSE_CODE (c))
14672 {
14673 case OMP_CLAUSE_SHARED:
14674 need_implicitly_determined = true;
14675 goto check_dup_generic;
14676
14677 case OMP_CLAUSE_PRIVATE:
14678 need_complete = true;
14679 need_implicitly_determined = true;
14680 goto check_dup_generic;
14681
14682 case OMP_CLAUSE_REDUCTION:
14683 if (reduction_seen == 0)
14684 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14685 else if (reduction_seen != -2
14686 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14687 ? -1 : 1))
14688 {
14689 error_at (OMP_CLAUSE_LOCATION (c),
14690 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14691 "on the same construct");
14692 reduction_seen = -2;
14693 }
14694 /* FALLTHRU */
14695 case OMP_CLAUSE_IN_REDUCTION:
14696 case OMP_CLAUSE_TASK_REDUCTION:
14697 need_implicitly_determined = true;
14698 t = OMP_CLAUSE_DECL (c);
14699 if (TREE_CODE (t) == TREE_LIST)
14700 {
14701 if (handle_omp_array_sections (c, ort))
14702 {
14703 remove = true;
14704 break;
14705 }
14706
14707 t = OMP_CLAUSE_DECL (c);
14708 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14709 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14710 {
14711 error_at (OMP_CLAUSE_LOCATION (c),
14712 "%<inscan%> %<reduction%> clause with array "
14713 "section");
14714 remove = true;
14715 break;
14716 }
14717 }
14718 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14719 if (t == error_mark_node)
14720 {
14721 remove = true;
14722 break;
14723 }
14724 if (oacc_async)
14725 c_mark_addressable (t);
14726 type = TREE_TYPE (t);
14727 if (TREE_CODE (t) == MEM_REF)
14728 type = TREE_TYPE (type);
14729 if (TREE_CODE (type) == ARRAY_TYPE)
14730 {
14731 tree oatype = type;
14732 gcc_assert (TREE_CODE (t) != MEM_REF);
14733 while (TREE_CODE (type) == ARRAY_TYPE)
14734 type = TREE_TYPE (type);
14735 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14736 {
14737 error_at (OMP_CLAUSE_LOCATION (c),
14738 "%qD in %<reduction%> clause is a zero size array",
14739 t);
14740 remove = true;
14741 break;
14742 }
14743 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14744 TYPE_SIZE_UNIT (type));
14745 if (integer_zerop (size))
14746 {
14747 error_at (OMP_CLAUSE_LOCATION (c),
14748 "%qD in %<reduction%> clause is a zero size array",
14749 t);
14750 remove = true;
14751 break;
14752 }
14753 size = size_binop (MINUS_EXPR, size, size_one_node);
14754 size = save_expr (size);
14755 tree index_type = build_index_type (size);
14756 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14757 index_type);
14758 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14759 tree ptype = build_pointer_type (type);
14760 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14761 t = build_fold_addr_expr (t);
14762 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14763 OMP_CLAUSE_DECL (c) = t;
14764 }
14765 if (TYPE_ATOMIC (type))
14766 {
14767 error_at (OMP_CLAUSE_LOCATION (c),
14768 "%<_Atomic%> %qE in %<reduction%> clause", t);
14769 remove = true;
14770 break;
14771 }
14772 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14773 || OMP_CLAUSE_REDUCTION_TASK (c))
14774 {
14775 /* Disallow zero sized or potentially zero sized task
14776 reductions. */
14777 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14778 {
14779 error_at (OMP_CLAUSE_LOCATION (c),
14780 "zero sized type %qT in %qs clause", type,
14781 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14782 remove = true;
14783 break;
14784 }
14785 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14786 {
14787 error_at (OMP_CLAUSE_LOCATION (c),
14788 "variable sized type %qT in %qs clause", type,
14789 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14790 remove = true;
14791 break;
14792 }
14793 }
14794 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14795 && (FLOAT_TYPE_P (type)
14796 || TREE_CODE (type) == COMPLEX_TYPE))
14797 {
14798 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14799 const char *r_name = NULL;
14800
14801 switch (r_code)
14802 {
14803 case PLUS_EXPR:
14804 case MULT_EXPR:
14805 case MINUS_EXPR:
14806 case TRUTH_ANDIF_EXPR:
14807 case TRUTH_ORIF_EXPR:
14808 break;
14809 case MIN_EXPR:
14810 if (TREE_CODE (type) == COMPLEX_TYPE)
14811 r_name = "min";
14812 break;
14813 case MAX_EXPR:
14814 if (TREE_CODE (type) == COMPLEX_TYPE)
14815 r_name = "max";
14816 break;
14817 case BIT_AND_EXPR:
14818 r_name = "&";
14819 break;
14820 case BIT_XOR_EXPR:
14821 r_name = "^";
14822 break;
14823 case BIT_IOR_EXPR:
14824 r_name = "|";
14825 break;
14826 default:
14827 gcc_unreachable ();
14828 }
14829 if (r_name)
14830 {
14831 error_at (OMP_CLAUSE_LOCATION (c),
14832 "%qE has invalid type for %<reduction(%s)%>",
14833 t, r_name);
14834 remove = true;
14835 break;
14836 }
14837 }
14838 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14839 {
14840 error_at (OMP_CLAUSE_LOCATION (c),
14841 "user defined reduction not found for %qE", t);
14842 remove = true;
14843 break;
14844 }
14845 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14846 {
14847 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14848 type = TYPE_MAIN_VARIANT (type);
14849 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14850 VAR_DECL, NULL_TREE, type);
14851 tree decl_placeholder = NULL_TREE;
14852 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14853 DECL_ARTIFICIAL (placeholder) = 1;
14854 DECL_IGNORED_P (placeholder) = 1;
14855 if (TREE_CODE (t) == MEM_REF)
14856 {
14857 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14858 VAR_DECL, NULL_TREE, type);
14859 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14860 DECL_ARTIFICIAL (decl_placeholder) = 1;
14861 DECL_IGNORED_P (decl_placeholder) = 1;
14862 }
14863 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14864 c_mark_addressable (placeholder);
14865 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14866 c_mark_addressable (decl_placeholder ? decl_placeholder
14867 : OMP_CLAUSE_DECL (c));
14868 OMP_CLAUSE_REDUCTION_MERGE (c)
14869 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14870 TREE_VEC_ELT (list, 0),
14871 TREE_VEC_ELT (list, 1),
14872 decl_placeholder ? decl_placeholder
14873 : OMP_CLAUSE_DECL (c), placeholder);
14874 OMP_CLAUSE_REDUCTION_MERGE (c)
14875 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14876 void_type_node, NULL_TREE,
14877 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14878 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14879 if (TREE_VEC_LENGTH (list) == 6)
14880 {
14881 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14882 c_mark_addressable (decl_placeholder ? decl_placeholder
14883 : OMP_CLAUSE_DECL (c));
14884 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14885 c_mark_addressable (placeholder);
14886 tree init = TREE_VEC_ELT (list, 5);
14887 if (init == error_mark_node)
14888 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14889 OMP_CLAUSE_REDUCTION_INIT (c)
14890 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14891 TREE_VEC_ELT (list, 3),
14892 decl_placeholder ? decl_placeholder
14893 : OMP_CLAUSE_DECL (c), placeholder);
14894 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14895 {
14896 tree v = decl_placeholder ? decl_placeholder : t;
14897 OMP_CLAUSE_REDUCTION_INIT (c)
14898 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14899 OMP_CLAUSE_REDUCTION_INIT (c));
14900 }
14901 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14902 c_find_omp_placeholder_r,
14903 placeholder, NULL))
14904 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14905 }
14906 else
14907 {
14908 tree init;
14909 tree v = decl_placeholder ? decl_placeholder : t;
14910 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14911 init = build_constructor (TREE_TYPE (v), NULL);
14912 else
14913 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14914 OMP_CLAUSE_REDUCTION_INIT (c)
14915 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14916 }
14917 OMP_CLAUSE_REDUCTION_INIT (c)
14918 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14919 void_type_node, NULL_TREE,
14920 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14921 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14922 }
14923 if (TREE_CODE (t) == MEM_REF)
14924 {
14925 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14926 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14927 != INTEGER_CST)
14928 {
14929 sorry ("variable length element type in array "
14930 "%<reduction%> clause");
14931 remove = true;
14932 break;
14933 }
14934 t = TREE_OPERAND (t, 0);
14935 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14936 t = TREE_OPERAND (t, 0);
14937 if (TREE_CODE (t) == ADDR_EXPR)
14938 t = TREE_OPERAND (t, 0);
14939 }
14940 goto check_dup_generic_t;
14941
14942 case OMP_CLAUSE_COPYPRIVATE:
14943 copyprivate_seen = true;
14944 if (nowait_clause)
14945 {
14946 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14947 "%<nowait%> clause must not be used together "
14948 "with %<copyprivate%>");
14949 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14950 nowait_clause = NULL;
14951 }
14952 goto check_dup_generic;
14953
14954 case OMP_CLAUSE_COPYIN:
14955 t = OMP_CLAUSE_DECL (c);
14956 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14957 {
14958 error_at (OMP_CLAUSE_LOCATION (c),
14959 "%qE must be %<threadprivate%> for %<copyin%>", t);
14960 remove = true;
14961 break;
14962 }
14963 goto check_dup_generic;
14964
14965 case OMP_CLAUSE_LINEAR:
14966 if (ort != C_ORT_OMP_DECLARE_SIMD)
14967 need_implicitly_determined = true;
14968 t = OMP_CLAUSE_DECL (c);
14969 if (ort != C_ORT_OMP_DECLARE_SIMD
14970 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
14971 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
14972 {
14973 error_at (OMP_CLAUSE_LOCATION (c),
14974 "modifier should not be specified in %<linear%> "
14975 "clause on %<simd%> or %<for%> constructs when not "
14976 "using OpenMP 5.2 modifiers");
14977 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14978 }
14979 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14980 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14981 {
14982 error_at (OMP_CLAUSE_LOCATION (c),
14983 "linear clause applied to non-integral non-pointer "
14984 "variable with type %qT", TREE_TYPE (t));
14985 remove = true;
14986 break;
14987 }
14988 if (TYPE_ATOMIC (TREE_TYPE (t)))
14989 {
14990 error_at (OMP_CLAUSE_LOCATION (c),
14991 "%<_Atomic%> %qD in %<linear%> clause", t);
14992 remove = true;
14993 break;
14994 }
14995 if (ort == C_ORT_OMP_DECLARE_SIMD)
14996 {
14997 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14998 if (TREE_CODE (s) == PARM_DECL)
14999 {
15000 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
15001 /* map_head bitmap is used as uniform_head if
15002 declare_simd. */
15003 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
15004 linear_variable_step_check = true;
15005 goto check_dup_generic;
15006 }
15007 if (TREE_CODE (s) != INTEGER_CST)
15008 {
15009 error_at (OMP_CLAUSE_LOCATION (c),
15010 "%<linear%> clause step %qE is neither constant "
15011 "nor a parameter", s);
15012 remove = true;
15013 break;
15014 }
15015 }
15016 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
15017 {
15018 tree s = OMP_CLAUSE_LINEAR_STEP (c);
15019 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
15020 OMP_CLAUSE_DECL (c), s);
15021 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
15022 sizetype, fold_convert (sizetype, s),
15023 fold_convert
15024 (sizetype, OMP_CLAUSE_DECL (c)));
15025 if (s == error_mark_node)
15026 s = size_one_node;
15027 OMP_CLAUSE_LINEAR_STEP (c) = s;
15028 }
15029 else
15030 OMP_CLAUSE_LINEAR_STEP (c)
15031 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
15032 goto check_dup_generic;
15033
15034 check_dup_generic:
15035 t = OMP_CLAUSE_DECL (c);
15036 check_dup_generic_t:
15037 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15038 {
15039 error_at (OMP_CLAUSE_LOCATION (c),
15040 "%qE is not a variable in clause %qs", t,
15041 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15042 remove = true;
15043 }
15044 else if ((openacc && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
15045 || (ort == C_ORT_OMP
15046 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15047 || (OMP_CLAUSE_CODE (c)
15048 == OMP_CLAUSE_USE_DEVICE_ADDR)))
15049 || (ort == C_ORT_OMP_TARGET
15050 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
15051 {
15052 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15053 && (bitmap_bit_p (&generic_head, DECL_UID (t))
15054 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
15055 {
15056 error_at (OMP_CLAUSE_LOCATION (c),
15057 "%qD appears more than once in data-sharing "
15058 "clauses", t);
15059 remove = true;
15060 break;
15061 }
15062 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
15063 target_in_reduction_seen = true;
15064 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15065 {
15066 error_at (OMP_CLAUSE_LOCATION (c),
15067 openacc
15068 ? "%qD appears more than once in reduction clauses"
15069 : "%qD appears more than once in data clauses",
15070 t);
15071 remove = true;
15072 }
15073 else
15074 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
15075 }
15076 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15077 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15078 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
15079 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15080 {
15081 error_at (OMP_CLAUSE_LOCATION (c),
15082 "%qE appears more than once in data clauses", t);
15083 remove = true;
15084 }
15085 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15086 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
15087 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15088 && bitmap_bit_p (&map_head, DECL_UID (t)))
15089 {
15090 if (openacc)
15091 error_at (OMP_CLAUSE_LOCATION (c),
15092 "%qD appears more than once in data clauses", t);
15093 else
15094 error_at (OMP_CLAUSE_LOCATION (c),
15095 "%qD appears both in data and map clauses", t);
15096 remove = true;
15097 }
15098 else
15099 bitmap_set_bit (&generic_head, DECL_UID (t));
15100 break;
15101
15102 case OMP_CLAUSE_FIRSTPRIVATE:
15103 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
15104 {
15105 move_implicit:
15106 implicit_moved = true;
15107 /* Move firstprivate and map clauses with
15108 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
15109 clauses chain. */
15110 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
15111 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
15112 while (*pc1)
15113 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
15114 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
15115 {
15116 *pc3 = *pc1;
15117 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
15118 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15119 }
15120 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
15121 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
15122 {
15123 *pc2 = *pc1;
15124 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
15125 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15126 }
15127 else
15128 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
15129 *pc3 = NULL;
15130 *pc2 = cl2;
15131 *pc1 = cl1;
15132 continue;
15133 }
15134 t = OMP_CLAUSE_DECL (c);
15135 need_complete = true;
15136 need_implicitly_determined = true;
15137 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15138 {
15139 error_at (OMP_CLAUSE_LOCATION (c),
15140 "%qE is not a variable in clause %<firstprivate%>", t);
15141 remove = true;
15142 }
15143 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15144 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
15145 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15146 remove = true;
15147 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15148 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15149 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15150 {
15151 error_at (OMP_CLAUSE_LOCATION (c),
15152 "%qE appears more than once in data clauses", t);
15153 remove = true;
15154 }
15155 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15156 || bitmap_bit_p (&map_field_head, DECL_UID (t)))
15157 {
15158 if (openacc)
15159 error_at (OMP_CLAUSE_LOCATION (c),
15160 "%qD appears more than once in data clauses", t);
15161 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15162 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
15163 /* Silently drop the clause. */;
15164 else
15165 error_at (OMP_CLAUSE_LOCATION (c),
15166 "%qD appears both in data and map clauses", t);
15167 remove = true;
15168 }
15169 else
15170 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
15171 break;
15172
15173 case OMP_CLAUSE_LASTPRIVATE:
15174 t = OMP_CLAUSE_DECL (c);
15175 need_complete = true;
15176 need_implicitly_determined = true;
15177 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15178 {
15179 error_at (OMP_CLAUSE_LOCATION (c),
15180 "%qE is not a variable in clause %<lastprivate%>", t);
15181 remove = true;
15182 }
15183 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15184 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
15185 {
15186 error_at (OMP_CLAUSE_LOCATION (c),
15187 "%qE appears more than once in data clauses", t);
15188 remove = true;
15189 }
15190 else
15191 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
15192 break;
15193
15194 case OMP_CLAUSE_ALIGNED:
15195 t = OMP_CLAUSE_DECL (c);
15196 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15197 {
15198 error_at (OMP_CLAUSE_LOCATION (c),
15199 "%qE is not a variable in %<aligned%> clause", t);
15200 remove = true;
15201 }
15202 else if (!POINTER_TYPE_P (TREE_TYPE (t))
15203 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15204 {
15205 error_at (OMP_CLAUSE_LOCATION (c),
15206 "%qE in %<aligned%> clause is neither a pointer nor "
15207 "an array", t);
15208 remove = true;
15209 }
15210 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15211 {
15212 error_at (OMP_CLAUSE_LOCATION (c),
15213 "%<_Atomic%> %qD in %<aligned%> clause", t);
15214 remove = true;
15215 break;
15216 }
15217 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15218 {
15219 error_at (OMP_CLAUSE_LOCATION (c),
15220 "%qE appears more than once in %<aligned%> clauses",
15221 t);
15222 remove = true;
15223 }
15224 else
15225 bitmap_set_bit (&aligned_head, DECL_UID (t));
15226 break;
15227
15228 case OMP_CLAUSE_NONTEMPORAL:
15229 t = OMP_CLAUSE_DECL (c);
15230 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15231 {
15232 error_at (OMP_CLAUSE_LOCATION (c),
15233 "%qE is not a variable in %<nontemporal%> clause", t);
15234 remove = true;
15235 }
15236 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15237 {
15238 error_at (OMP_CLAUSE_LOCATION (c),
15239 "%qE appears more than once in %<nontemporal%> "
15240 "clauses", t);
15241 remove = true;
15242 }
15243 else
15244 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
15245 break;
15246
15247 case OMP_CLAUSE_ALLOCATE:
15248 t = OMP_CLAUSE_DECL (c);
15249 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15250 {
15251 error_at (OMP_CLAUSE_LOCATION (c),
15252 "%qE is not a variable in %<allocate%> clause", t);
15253 remove = true;
15254 }
15255 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15256 {
15257 warning_at (OMP_CLAUSE_LOCATION (c), 0,
15258 "%qE appears more than once in %<allocate%> clauses",
15259 t);
15260 remove = true;
15261 }
15262 else
15263 {
15264 bitmap_set_bit (&aligned_head, DECL_UID (t));
15265 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
15266 allocate_seen = true;
15267 }
15268 break;
15269
15270 case OMP_CLAUSE_DOACROSS:
15271 t = OMP_CLAUSE_DECL (c);
15272 if (t == NULL_TREE)
15273 break;
15274 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
15275 {
15276 gcc_assert (TREE_CODE (t) == TREE_LIST);
15277 for (; t; t = TREE_CHAIN (t))
15278 {
15279 tree decl = TREE_VALUE (t);
15280 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
15281 {
15282 tree offset = TREE_PURPOSE (t);
15283 bool neg = wi::neg_p (wi::to_wide (offset));
15284 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
15285 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
15286 neg ? MINUS_EXPR : PLUS_EXPR,
15287 decl, offset);
15288 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
15289 sizetype,
15290 fold_convert (sizetype, t2),
15291 fold_convert (sizetype, decl));
15292 if (t2 == error_mark_node)
15293 {
15294 remove = true;
15295 break;
15296 }
15297 TREE_PURPOSE (t) = t2;
15298 }
15299 }
15300 break;
15301 }
15302 gcc_unreachable ();
15303 case OMP_CLAUSE_DEPEND:
15304 case OMP_CLAUSE_AFFINITY:
15305 t = OMP_CLAUSE_DECL (c);
15306 if (TREE_CODE (t) == TREE_LIST
15307 && TREE_PURPOSE (t)
15308 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
15309 {
15310 if (TREE_PURPOSE (t) != last_iterators)
15311 last_iterators_remove
15312 = c_omp_finish_iterators (TREE_PURPOSE (t));
15313 last_iterators = TREE_PURPOSE (t);
15314 t = TREE_VALUE (t);
15315 if (last_iterators_remove)
15316 t = error_mark_node;
15317 }
15318 else
15319 last_iterators = NULL_TREE;
15320 if (TREE_CODE (t) == TREE_LIST)
15321 {
15322 if (handle_omp_array_sections (c, ort))
15323 remove = true;
15324 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15325 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15326 {
15327 error_at (OMP_CLAUSE_LOCATION (c),
15328 "%<depend%> clause with %<depobj%> dependence "
15329 "type on array section");
15330 remove = true;
15331 }
15332 break;
15333 }
15334 if (t == error_mark_node)
15335 remove = true;
15336 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15337 && t == ridpointers[RID_OMP_ALL_MEMORY])
15338 {
15339 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
15340 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
15341 {
15342 error_at (OMP_CLAUSE_LOCATION (c),
15343 "%<omp_all_memory%> used with %<depend%> kind "
15344 "other than %<out%> or %<inout%>");
15345 remove = true;
15346 }
15347 }
15348 else if (!lvalue_p (t))
15349 {
15350 error_at (OMP_CLAUSE_LOCATION (c),
15351 "%qE is not lvalue expression nor array section in "
15352 "%qs clause", t,
15353 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15354 remove = true;
15355 }
15356 else if (TREE_CODE (t) == COMPONENT_REF
15357 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
15358 {
15359 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15360 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
15361 error_at (OMP_CLAUSE_LOCATION (c),
15362 "bit-field %qE in %qs clause", t,
15363 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15364 remove = true;
15365 }
15366 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15367 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15368 {
15369 if (!c_omp_depend_t_p (TREE_TYPE (t)))
15370 {
15371 error_at (OMP_CLAUSE_LOCATION (c),
15372 "%qE does not have %<omp_depend_t%> type in "
15373 "%<depend%> clause with %<depobj%> dependence "
15374 "type", t);
15375 remove = true;
15376 }
15377 }
15378 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15379 && c_omp_depend_t_p (TREE_TYPE (t)))
15380 {
15381 error_at (OMP_CLAUSE_LOCATION (c),
15382 "%qE should not have %<omp_depend_t%> type in "
15383 "%<depend%> clause with dependence type other than "
15384 "%<depobj%>", t);
15385 remove = true;
15386 }
15387 if (!remove)
15388 {
15389 if (t == ridpointers[RID_OMP_ALL_MEMORY])
15390 t = null_pointer_node;
15391 else
15392 {
15393 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
15394 ADDR_EXPR, t, false);
15395 if (addr == error_mark_node)
15396 {
15397 remove = true;
15398 break;
15399 }
15400 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
15401 RO_UNARY_STAR);
15402 if (t == error_mark_node)
15403 {
15404 remove = true;
15405 break;
15406 }
15407 }
15408 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
15409 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
15410 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
15411 == TREE_VEC))
15412 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
15413 else
15414 OMP_CLAUSE_DECL (c) = t;
15415 }
15416 break;
15417
15418 case OMP_CLAUSE_MAP:
15419 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
15420 goto move_implicit;
15421 /* FALLTHRU */
15422 case OMP_CLAUSE_TO:
15423 case OMP_CLAUSE_FROM:
15424 case OMP_CLAUSE__CACHE_:
15425 {
15426 using namespace omp_addr_tokenizer;
15427 auto_vec<omp_addr_token *, 10> addr_tokens;
15428
15429 t = OMP_CLAUSE_DECL (c);
15430 if (TREE_CODE (t) == TREE_LIST)
15431 {
15432 grp_start_p = pc;
15433 grp_sentinel = OMP_CLAUSE_CHAIN (c);
15434
15435 if (handle_omp_array_sections (c, ort))
15436 remove = true;
15437 else
15438 {
15439 t = OMP_CLAUSE_DECL (c);
15440 if (!omp_mappable_type (TREE_TYPE (t)))
15441 {
15442 error_at (OMP_CLAUSE_LOCATION (c),
15443 "array section does not have mappable type "
15444 "in %qs clause",
15445 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15446 remove = true;
15447 }
15448 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15449 {
15450 error_at (OMP_CLAUSE_LOCATION (c),
15451 "%<_Atomic%> %qE in %qs clause", t,
15452 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15453 remove = true;
15454 }
15455 while (TREE_CODE (t) == ARRAY_REF)
15456 t = TREE_OPERAND (t, 0);
15457
15458 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15459
15460 if (!omp_parse_expr (addr_tokens, t))
15461 {
15462 sorry_at (OMP_CLAUSE_LOCATION (c),
15463 "unsupported map expression %qE",
15464 OMP_CLAUSE_DECL (c));
15465 remove = true;
15466 break;
15467 }
15468
15469 /* This check is to determine if this will be the only map
15470 node created for this clause. Otherwise, we'll check
15471 the following FIRSTPRIVATE_POINTER or ATTACH_DETACH
15472 node on the next iteration(s) of the loop. */
15473 if (addr_tokens.length () >= 4
15474 && addr_tokens[0]->type == STRUCTURE_BASE
15475 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
15476 && addr_tokens[1]->type == ACCESS_METHOD
15477 && addr_tokens[2]->type == COMPONENT_SELECTOR
15478 && addr_tokens[3]->type == ACCESS_METHOD
15479 && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
15480 || (addr_tokens[3]->u.access_kind
15481 == ACCESS_INDEXED_ARRAY)))
15482 {
15483 tree rt = addr_tokens[1]->expr;
15484
15485 gcc_assert (DECL_P (rt));
15486
15487 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15488 && OMP_CLAUSE_MAP_IMPLICIT (c)
15489 && (bitmap_bit_p (&map_head, DECL_UID (rt))
15490 || bitmap_bit_p (&map_field_head, DECL_UID (rt))
15491 || bitmap_bit_p (&map_firstprivate_head,
15492 DECL_UID (rt))))
15493 {
15494 remove = true;
15495 break;
15496 }
15497 if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
15498 break;
15499 if (bitmap_bit_p (&map_head, DECL_UID (rt)))
15500 {
15501 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15502 error_at (OMP_CLAUSE_LOCATION (c),
15503 "%qD appears more than once in motion "
15504 "clauses", rt);
15505 else if (openacc)
15506 error_at (OMP_CLAUSE_LOCATION (c),
15507 "%qD appears more than once in data "
15508 "clauses", rt);
15509 else
15510 error_at (OMP_CLAUSE_LOCATION (c),
15511 "%qD appears more than once in map "
15512 "clauses", rt);
15513 remove = true;
15514 }
15515 else
15516 {
15517 bitmap_set_bit (&map_head, DECL_UID (rt));
15518 bitmap_set_bit (&map_field_head, DECL_UID (rt));
15519 }
15520 }
15521 }
15522 if (c_oacc_check_attachments (c))
15523 remove = true;
15524 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15525 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15526 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
15527 && !OMP_CLAUSE_SIZE (c))
15528 /* In this case, we have a single array element which is a
15529 pointer, and we already set OMP_CLAUSE_SIZE in
15530 handle_omp_array_sections above. For attach/detach
15531 clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
15532 to zero here. */
15533 OMP_CLAUSE_SIZE (c) = size_zero_node;
15534 break;
15535 }
15536 else if (!omp_parse_expr (addr_tokens, t))
15537 {
15538 sorry_at (OMP_CLAUSE_LOCATION (c),
15539 "unsupported map expression %qE",
15540 OMP_CLAUSE_DECL (c));
15541 remove = true;
15542 break;
15543 }
15544 if (t == error_mark_node)
15545 {
15546 remove = true;
15547 break;
15548 }
15549 /* OpenACC attach / detach clauses must be pointers. */
15550 if (c_oacc_check_attachments (c))
15551 {
15552 remove = true;
15553 break;
15554 }
15555 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15556 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15557 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
15558 && !OMP_CLAUSE_SIZE (c))
15559 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15560 bias) to zero here, so it is not set erroneously to the pointer
15561 size later on in gimplify.cc. */
15562 OMP_CLAUSE_SIZE (c) = size_zero_node;
15563
15564 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15565
15566 if (!ai.check_clause (c))
15567 {
15568 remove = true;
15569 break;
15570 }
15571
15572 if (!ai.map_supported_p ())
15573 {
15574 sorry_at (OMP_CLAUSE_LOCATION (c),
15575 "unsupported map expression %qE",
15576 OMP_CLAUSE_DECL (c));
15577 remove = true;
15578 break;
15579 }
15580
15581 gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
15582 || addr_tokens[0]->type == STRUCTURE_BASE)
15583 && addr_tokens[1]->type == ACCESS_METHOD);
15584
15585 t = addr_tokens[1]->expr;
15586
15587 if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
15588 goto skip_decl_checks;
15589
15590 /* For OpenMP, we can access a struct "t" and "t.d" on the same
15591 mapping. OpenACC allows multiple fields of the same structure
15592 to be written. */
15593 if (addr_tokens[0]->type == STRUCTURE_BASE
15594 && (bitmap_bit_p (&map_field_head, DECL_UID (t))
15595 || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
15596 goto skip_decl_checks;
15597
15598 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15599 {
15600 error_at (OMP_CLAUSE_LOCATION (c),
15601 "%qE is not a variable in %qs clause", t,
15602 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15603 remove = true;
15604 }
15605 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15606 {
15607 error_at (OMP_CLAUSE_LOCATION (c),
15608 "%qD is threadprivate variable in %qs clause", t,
15609 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15610 remove = true;
15611 }
15612 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15613 || (OMP_CLAUSE_MAP_KIND (c)
15614 != GOMP_MAP_FIRSTPRIVATE_POINTER))
15615 && !c_mark_addressable (t))
15616 remove = true;
15617 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15618 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15619 || (OMP_CLAUSE_MAP_KIND (c)
15620 == GOMP_MAP_FIRSTPRIVATE_POINTER)
15621 || (OMP_CLAUSE_MAP_KIND (c)
15622 == GOMP_MAP_FORCE_DEVICEPTR)))
15623 && t == OMP_CLAUSE_DECL (c)
15624 && !omp_mappable_type (TREE_TYPE (t)))
15625 {
15626 error_at (OMP_CLAUSE_LOCATION (c),
15627 "%qD does not have a mappable type in %qs clause", t,
15628 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15629 remove = true;
15630 }
15631 else if (TREE_TYPE (t) == error_mark_node)
15632 remove = true;
15633 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15634 {
15635 error_at (OMP_CLAUSE_LOCATION (c),
15636 "%<_Atomic%> %qE in %qs clause", t,
15637 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15638 remove = true;
15639 }
15640 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15641 && OMP_CLAUSE_MAP_IMPLICIT (c)
15642 && (bitmap_bit_p (&map_head, DECL_UID (t))
15643 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15644 || bitmap_bit_p (&map_firstprivate_head,
15645 DECL_UID (t))))
15646 remove = true;
15647 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15648 && (OMP_CLAUSE_MAP_KIND (c)
15649 == GOMP_MAP_FIRSTPRIVATE_POINTER))
15650 {
15651 if (bitmap_bit_p (&generic_head, DECL_UID (t))
15652 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15653 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15654 {
15655 error_at (OMP_CLAUSE_LOCATION (c),
15656 "%qD appears more than once in data clauses", t);
15657 remove = true;
15658 }
15659 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15660 && !bitmap_bit_p (&map_field_head, DECL_UID (t))
15661 && openacc)
15662 {
15663 error_at (OMP_CLAUSE_LOCATION (c),
15664 "%qD appears more than once in data clauses", t);
15665 remove = true;
15666 }
15667 else
15668 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15669 }
15670 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15671 && !bitmap_bit_p (&map_field_head, DECL_UID (t))
15672 && ort != C_ORT_OMP
15673 && ort != C_ORT_OMP_EXIT_DATA)
15674 {
15675 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15676 error_at (OMP_CLAUSE_LOCATION (c),
15677 "%qD appears more than once in motion clauses", t);
15678 else if (openacc)
15679 error_at (OMP_CLAUSE_LOCATION (c),
15680 "%qD appears more than once in data clauses", t);
15681 else
15682 error_at (OMP_CLAUSE_LOCATION (c),
15683 "%qD appears more than once in map clauses", t);
15684 remove = true;
15685 }
15686 else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
15687 {
15688 error_at (OMP_CLAUSE_LOCATION (c),
15689 "%qD appears more than once in data clauses", t);
15690 remove = true;
15691 }
15692 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15693 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15694 {
15695 if (openacc)
15696 error_at (OMP_CLAUSE_LOCATION (c),
15697 "%qD appears more than once in data clauses", t);
15698 else
15699 error_at (OMP_CLAUSE_LOCATION (c),
15700 "%qD appears both in data and map clauses", t);
15701 remove = true;
15702 }
15703 else if (!omp_access_chain_p (addr_tokens, 1))
15704 {
15705 bitmap_set_bit (&map_head, DECL_UID (t));
15706 if (t != OMP_CLAUSE_DECL (c)
15707 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15708 bitmap_set_bit (&map_field_head, DECL_UID (t));
15709 }
15710
15711 skip_decl_checks:
15712 /* If we call omp_expand_map_clause in handle_omp_array_sections,
15713 the containing loop (here) iterates through the new nodes
15714 created by that expansion. Avoid expanding those again (just
15715 by checking the node type). */
15716 if (!remove
15717 && ort != C_ORT_DECLARE_SIMD
15718 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15719 || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
15720 && (OMP_CLAUSE_MAP_KIND (c)
15721 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
15722 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
15723 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
15724 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
15725 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
15726 {
15727 grp_start_p = pc;
15728 grp_sentinel = OMP_CLAUSE_CHAIN (c);
15729 tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
15730 addr_tokens, ort);
15731 if (nc != error_mark_node)
15732 c = nc;
15733 }
15734 }
15735 break;
15736
15737 case OMP_CLAUSE_ENTER:
15738 case OMP_CLAUSE_LINK:
15739 t = OMP_CLAUSE_DECL (c);
15740 const char *cname;
15741 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15742 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15743 && OMP_CLAUSE_ENTER_TO (c))
15744 cname = "to";
15745 if (TREE_CODE (t) == FUNCTION_DECL
15746 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15747 ;
15748 else if (!VAR_P (t))
15749 {
15750 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15751 error_at (OMP_CLAUSE_LOCATION (c),
15752 "%qE is neither a variable nor a function name in "
15753 "clause %qs", t, cname);
15754 else
15755 error_at (OMP_CLAUSE_LOCATION (c),
15756 "%qE is not a variable in clause %qs", t, cname);
15757 remove = true;
15758 }
15759 else if (DECL_THREAD_LOCAL_P (t))
15760 {
15761 error_at (OMP_CLAUSE_LOCATION (c),
15762 "%qD is threadprivate variable in %qs clause", t,
15763 cname);
15764 remove = true;
15765 }
15766 else if (!omp_mappable_type (TREE_TYPE (t)))
15767 {
15768 error_at (OMP_CLAUSE_LOCATION (c),
15769 "%qD does not have a mappable type in %qs clause", t,
15770 cname);
15771 remove = true;
15772 }
15773 if (remove)
15774 break;
15775 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15776 {
15777 error_at (OMP_CLAUSE_LOCATION (c),
15778 "%qE appears more than once on the same "
15779 "%<declare target%> directive", t);
15780 remove = true;
15781 }
15782 else
15783 bitmap_set_bit (&generic_head, DECL_UID (t));
15784 break;
15785
15786 case OMP_CLAUSE_UNIFORM:
15787 t = OMP_CLAUSE_DECL (c);
15788 if (TREE_CODE (t) != PARM_DECL)
15789 {
15790 if (DECL_P (t))
15791 error_at (OMP_CLAUSE_LOCATION (c),
15792 "%qD is not an argument in %<uniform%> clause", t);
15793 else
15794 error_at (OMP_CLAUSE_LOCATION (c),
15795 "%qE is not an argument in %<uniform%> clause", t);
15796 remove = true;
15797 break;
15798 }
15799 /* map_head bitmap is used as uniform_head if declare_simd. */
15800 bitmap_set_bit (&map_head, DECL_UID (t));
15801 goto check_dup_generic;
15802
15803 case OMP_CLAUSE_IS_DEVICE_PTR:
15804 case OMP_CLAUSE_USE_DEVICE_PTR:
15805 t = OMP_CLAUSE_DECL (c);
15806 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15807 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15808 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15809 {
15810 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15811 && !openacc)
15812 {
15813 error_at (OMP_CLAUSE_LOCATION (c),
15814 "%qs variable is not a pointer",
15815 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15816 remove = true;
15817 }
15818 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15819 {
15820 error_at (OMP_CLAUSE_LOCATION (c),
15821 "%qs variable is neither a pointer nor an array",
15822 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15823 remove = true;
15824 }
15825 }
15826 goto check_dup_generic;
15827
15828 case OMP_CLAUSE_HAS_DEVICE_ADDR:
15829 t = OMP_CLAUSE_DECL (c);
15830 if (TREE_CODE (t) == TREE_LIST)
15831 {
15832 if (handle_omp_array_sections (c, ort))
15833 remove = true;
15834 else
15835 {
15836 t = OMP_CLAUSE_DECL (c);
15837 while (TREE_CODE (t) == ARRAY_REF)
15838 t = TREE_OPERAND (t, 0);
15839 }
15840 }
15841 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15842 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15843 c_mark_addressable (t);
15844 goto check_dup_generic_t;
15845
15846 case OMP_CLAUSE_USE_DEVICE_ADDR:
15847 t = OMP_CLAUSE_DECL (c);
15848 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15849 c_mark_addressable (t);
15850 goto check_dup_generic;
15851
15852 case OMP_CLAUSE_NOWAIT:
15853 if (copyprivate_seen)
15854 {
15855 error_at (OMP_CLAUSE_LOCATION (c),
15856 "%<nowait%> clause must not be used together "
15857 "with %<copyprivate%>");
15858 remove = true;
15859 break;
15860 }
15861 nowait_clause = pc;
15862 pc = &OMP_CLAUSE_CHAIN (c);
15863 continue;
15864
15865 case OMP_CLAUSE_ORDER:
15866 if (ordered_clause)
15867 {
15868 error_at (OMP_CLAUSE_LOCATION (c),
15869 "%<order%> clause must not be used together "
15870 "with %<ordered%>");
15871 remove = true;
15872 break;
15873 }
15874 else if (order_clause)
15875 {
15876 /* Silently remove duplicates. */
15877 remove = true;
15878 break;
15879 }
15880 order_clause = pc;
15881 pc = &OMP_CLAUSE_CHAIN (c);
15882 continue;
15883
15884 case OMP_CLAUSE_DETACH:
15885 t = OMP_CLAUSE_DECL (c);
15886 if (detach_seen)
15887 {
15888 error_at (OMP_CLAUSE_LOCATION (c),
15889 "too many %qs clauses on a task construct",
15890 "detach");
15891 remove = true;
15892 break;
15893 }
15894 detach_seen = pc;
15895 pc = &OMP_CLAUSE_CHAIN (c);
15896 c_mark_addressable (t);
15897 continue;
15898
15899 case OMP_CLAUSE_IF:
15900 case OMP_CLAUSE_SELF:
15901 case OMP_CLAUSE_NUM_THREADS:
15902 case OMP_CLAUSE_NUM_TEAMS:
15903 case OMP_CLAUSE_THREAD_LIMIT:
15904 case OMP_CLAUSE_DEFAULT:
15905 case OMP_CLAUSE_UNTIED:
15906 case OMP_CLAUSE_COLLAPSE:
15907 case OMP_CLAUSE_FINAL:
15908 case OMP_CLAUSE_DEVICE:
15909 case OMP_CLAUSE_DIST_SCHEDULE:
15910 case OMP_CLAUSE_PARALLEL:
15911 case OMP_CLAUSE_FOR:
15912 case OMP_CLAUSE_SECTIONS:
15913 case OMP_CLAUSE_TASKGROUP:
15914 case OMP_CLAUSE_PROC_BIND:
15915 case OMP_CLAUSE_DEVICE_TYPE:
15916 case OMP_CLAUSE_PRIORITY:
15917 case OMP_CLAUSE_GRAINSIZE:
15918 case OMP_CLAUSE_NUM_TASKS:
15919 case OMP_CLAUSE_THREADS:
15920 case OMP_CLAUSE_SIMD:
15921 case OMP_CLAUSE_HINT:
15922 case OMP_CLAUSE_FILTER:
15923 case OMP_CLAUSE_DEFAULTMAP:
15924 case OMP_CLAUSE_BIND:
15925 case OMP_CLAUSE_NUM_GANGS:
15926 case OMP_CLAUSE_NUM_WORKERS:
15927 case OMP_CLAUSE_VECTOR_LENGTH:
15928 case OMP_CLAUSE_ASYNC:
15929 case OMP_CLAUSE_WAIT:
15930 case OMP_CLAUSE_AUTO:
15931 case OMP_CLAUSE_INDEPENDENT:
15932 case OMP_CLAUSE_SEQ:
15933 case OMP_CLAUSE_GANG:
15934 case OMP_CLAUSE_WORKER:
15935 case OMP_CLAUSE_VECTOR:
15936 case OMP_CLAUSE_TILE:
15937 case OMP_CLAUSE_IF_PRESENT:
15938 case OMP_CLAUSE_FINALIZE:
15939 case OMP_CLAUSE_NOHOST:
15940 case OMP_CLAUSE_INDIRECT:
15941 pc = &OMP_CLAUSE_CHAIN (c);
15942 continue;
15943
15944 case OMP_CLAUSE_MERGEABLE:
15945 mergeable_seen = true;
15946 pc = &OMP_CLAUSE_CHAIN (c);
15947 continue;
15948
15949 case OMP_CLAUSE_NOGROUP:
15950 nogroup_seen = pc;
15951 pc = &OMP_CLAUSE_CHAIN (c);
15952 continue;
15953
15954 case OMP_CLAUSE_SCHEDULE:
15955 schedule_clause = c;
15956 pc = &OMP_CLAUSE_CHAIN (c);
15957 continue;
15958
15959 case OMP_CLAUSE_ORDERED:
15960 ordered_clause = c;
15961 if (order_clause)
15962 {
15963 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15964 "%<order%> clause must not be used together "
15965 "with %<ordered%>");
15966 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15967 order_clause = NULL;
15968 }
15969 pc = &OMP_CLAUSE_CHAIN (c);
15970 continue;
15971
15972 case OMP_CLAUSE_SAFELEN:
15973 safelen = c;
15974 pc = &OMP_CLAUSE_CHAIN (c);
15975 continue;
15976 case OMP_CLAUSE_SIMDLEN:
15977 simdlen = c;
15978 pc = &OMP_CLAUSE_CHAIN (c);
15979 continue;
15980
15981 case OMP_CLAUSE_INBRANCH:
15982 case OMP_CLAUSE_NOTINBRANCH:
15983 if (branch_seen)
15984 {
15985 error_at (OMP_CLAUSE_LOCATION (c),
15986 "%<inbranch%> clause is incompatible with "
15987 "%<notinbranch%>");
15988 remove = true;
15989 break;
15990 }
15991 branch_seen = true;
15992 pc = &OMP_CLAUSE_CHAIN (c);
15993 continue;
15994
15995 case OMP_CLAUSE_INCLUSIVE:
15996 case OMP_CLAUSE_EXCLUSIVE:
15997 need_complete = true;
15998 need_implicitly_determined = true;
15999 t = OMP_CLAUSE_DECL (c);
16000 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16001 {
16002 error_at (OMP_CLAUSE_LOCATION (c),
16003 "%qE is not a variable in clause %qs", t,
16004 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16005 remove = true;
16006 }
16007 break;
16008
16009 default:
16010 gcc_unreachable ();
16011 }
16012
16013 if (!remove)
16014 {
16015 t = OMP_CLAUSE_DECL (c);
16016
16017 if (need_complete)
16018 {
16019 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
16020 if (t == error_mark_node)
16021 remove = true;
16022 }
16023
16024 if (need_implicitly_determined)
16025 {
16026 const char *share_name = NULL;
16027
16028 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
16029 share_name = "threadprivate";
16030 else switch (c_omp_predetermined_sharing (t))
16031 {
16032 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
16033 break;
16034 case OMP_CLAUSE_DEFAULT_SHARED:
16035 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
16036 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
16037 && c_omp_predefined_variable (t))
16038 /* The __func__ variable and similar function-local
16039 predefined variables may be listed in a shared or
16040 firstprivate clause. */
16041 break;
16042 share_name = "shared";
16043 break;
16044 case OMP_CLAUSE_DEFAULT_PRIVATE:
16045 share_name = "private";
16046 break;
16047 default:
16048 gcc_unreachable ();
16049 }
16050 if (share_name)
16051 {
16052 error_at (OMP_CLAUSE_LOCATION (c),
16053 "%qE is predetermined %qs for %qs",
16054 t, share_name,
16055 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16056 remove = true;
16057 }
16058 else if (TREE_READONLY (t)
16059 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
16060 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
16061 {
16062 error_at (OMP_CLAUSE_LOCATION (c),
16063 "%<const%> qualified %qE may appear only in "
16064 "%<shared%> or %<firstprivate%> clauses", t);
16065 remove = true;
16066 }
16067 }
16068 }
16069
16070 if (remove)
16071 {
16072 if (grp_start_p)
16073 {
16074 /* If we found a clause to remove, we want to remove the whole
16075 expanded group, otherwise gimplify
16076 (omp_resolve_clause_dependencies) can get confused. */
16077 *grp_start_p = grp_sentinel;
16078 pc = grp_start_p;
16079 grp_start_p = NULL;
16080 }
16081 else
16082 *pc = OMP_CLAUSE_CHAIN (c);
16083 }
16084 else
16085 pc = &OMP_CLAUSE_CHAIN (c);
16086 }
16087
16088 if (simdlen
16089 && safelen
16090 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
16091 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
16092 {
16093 error_at (OMP_CLAUSE_LOCATION (simdlen),
16094 "%<simdlen%> clause value is bigger than "
16095 "%<safelen%> clause value");
16096 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
16097 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
16098 }
16099
16100 if (ordered_clause
16101 && schedule_clause
16102 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16103 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
16104 {
16105 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16106 "%<nonmonotonic%> schedule modifier specified together "
16107 "with %<ordered%> clause");
16108 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16109 = (enum omp_clause_schedule_kind)
16110 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16111 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
16112 }
16113
16114 if (reduction_seen < 0 && ordered_clause)
16115 {
16116 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
16117 "%qs clause specified together with %<inscan%> "
16118 "%<reduction%> clause", "ordered");
16119 reduction_seen = -2;
16120 }
16121
16122 if (reduction_seen < 0 && schedule_clause)
16123 {
16124 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16125 "%qs clause specified together with %<inscan%> "
16126 "%<reduction%> clause", "schedule");
16127 reduction_seen = -2;
16128 }
16129
16130 if (linear_variable_step_check
16131 || reduction_seen == -2
16132 || allocate_seen
16133 || target_in_reduction_seen)
16134 for (pc = &clauses, c = clauses; c ; c = *pc)
16135 {
16136 bool remove = false;
16137 if (allocate_seen)
16138 switch (OMP_CLAUSE_CODE (c))
16139 {
16140 case OMP_CLAUSE_REDUCTION:
16141 case OMP_CLAUSE_IN_REDUCTION:
16142 case OMP_CLAUSE_TASK_REDUCTION:
16143 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
16144 {
16145 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
16146 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16147 t = TREE_OPERAND (t, 0);
16148 if (TREE_CODE (t) == ADDR_EXPR
16149 || INDIRECT_REF_P (t))
16150 t = TREE_OPERAND (t, 0);
16151 if (DECL_P (t))
16152 bitmap_clear_bit (&aligned_head, DECL_UID (t));
16153 break;
16154 }
16155 /* FALLTHRU */
16156 case OMP_CLAUSE_PRIVATE:
16157 case OMP_CLAUSE_FIRSTPRIVATE:
16158 case OMP_CLAUSE_LASTPRIVATE:
16159 case OMP_CLAUSE_LINEAR:
16160 if (DECL_P (OMP_CLAUSE_DECL (c)))
16161 bitmap_clear_bit (&aligned_head,
16162 DECL_UID (OMP_CLAUSE_DECL (c)));
16163 break;
16164 default:
16165 break;
16166 }
16167 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16168 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
16169 && !bitmap_bit_p (&map_head,
16170 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
16171 {
16172 error_at (OMP_CLAUSE_LOCATION (c),
16173 "%<linear%> clause step is a parameter %qD not "
16174 "specified in %<uniform%> clause",
16175 OMP_CLAUSE_LINEAR_STEP (c));
16176 remove = true;
16177 }
16178 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16179 && reduction_seen == -2)
16180 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
16181 if (target_in_reduction_seen
16182 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
16183 {
16184 tree t = OMP_CLAUSE_DECL (c);
16185 while (handled_component_p (t)
16186 || INDIRECT_REF_P (t)
16187 || TREE_CODE (t) == ADDR_EXPR
16188 || TREE_CODE (t) == MEM_REF
16189 || TREE_CODE (t) == NON_LVALUE_EXPR)
16190 t = TREE_OPERAND (t, 0);
16191 if (DECL_P (t)
16192 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
16193 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
16194 }
16195
16196 if (remove)
16197 *pc = OMP_CLAUSE_CHAIN (c);
16198 else
16199 pc = &OMP_CLAUSE_CHAIN (c);
16200 }
16201
16202 if (allocate_seen)
16203 for (pc = &clauses, c = clauses; c ; c = *pc)
16204 {
16205 bool remove = false;
16206 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
16207 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
16208 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
16209 {
16210 error_at (OMP_CLAUSE_LOCATION (c),
16211 "%qD specified in %<allocate%> clause but not in "
16212 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
16213 remove = true;
16214 }
16215 if (remove)
16216 *pc = OMP_CLAUSE_CHAIN (c);
16217 else
16218 pc = &OMP_CLAUSE_CHAIN (c);
16219 }
16220
16221 if (nogroup_seen && reduction_seen)
16222 {
16223 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
16224 "%<nogroup%> clause must not be used together with "
16225 "%<reduction%> clause");
16226 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
16227 }
16228
16229 if (detach_seen)
16230 {
16231 if (mergeable_seen)
16232 {
16233 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
16234 "%<detach%> clause must not be used together with "
16235 "%<mergeable%> clause");
16236 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
16237 }
16238 else
16239 {
16240 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
16241
16242 for (pc = &clauses, c = clauses; c ; c = *pc)
16243 {
16244 bool remove = false;
16245 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
16246 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
16247 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
16248 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
16249 && OMP_CLAUSE_DECL (c) == detach_decl)
16250 {
16251 error_at (OMP_CLAUSE_LOCATION (c),
16252 "the event handle of a %<detach%> clause "
16253 "should not be in a data-sharing clause");
16254 remove = true;
16255 }
16256 if (remove)
16257 *pc = OMP_CLAUSE_CHAIN (c);
16258 else
16259 pc = &OMP_CLAUSE_CHAIN (c);
16260 }
16261 }
16262 }
16263
16264 bitmap_obstack_release (NULL);
16265 return clauses;
16266 }
16267
16268 /* Return code to initialize DST with a copy constructor from SRC.
16269 C doesn't have copy constructors nor assignment operators, only for
16270 _Atomic vars we need to perform __atomic_load from src into a temporary
16271 followed by __atomic_store of the temporary to dst. */
16272
16273 tree
16274 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
16275 {
16276 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
16277 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
16278
16279 location_t loc = OMP_CLAUSE_LOCATION (clause);
16280 tree type = TREE_TYPE (dst);
16281 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
16282 tree tmp = create_tmp_var (nonatomic_type);
16283 tree tmp_addr = build_fold_addr_expr (tmp);
16284 TREE_ADDRESSABLE (tmp) = 1;
16285 suppress_warning (tmp);
16286 tree src_addr = build_fold_addr_expr (src);
16287 tree dst_addr = build_fold_addr_expr (dst);
16288 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
16289 vec<tree, va_gc> *params;
16290 /* Expansion of a generic atomic load may require an addition
16291 element, so allocate enough to prevent a resize. */
16292 vec_alloc (params, 4);
16293
16294 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
16295 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
16296 params->quick_push (src_addr);
16297 params->quick_push (tmp_addr);
16298 params->quick_push (seq_cst);
16299 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16300
16301 vec_alloc (params, 4);
16302
16303 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
16304 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
16305 params->quick_push (dst_addr);
16306 params->quick_push (tmp_addr);
16307 params->quick_push (seq_cst);
16308 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16309 return build2 (COMPOUND_EXPR, void_type_node, load, store);
16310 }
16311
16312 /* Create a transaction node. */
16313
16314 tree
16315 c_finish_transaction (location_t loc, tree block, int flags)
16316 {
16317 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
16318 if (flags & TM_STMT_ATTR_OUTER)
16319 TRANSACTION_EXPR_OUTER (stmt) = 1;
16320 if (flags & TM_STMT_ATTR_RELAXED)
16321 TRANSACTION_EXPR_RELAXED (stmt) = 1;
16322 return add_stmt (stmt);
16323 }
16324
16325 /* Make a variant type in the proper way for C/C++, propagating qualifiers
16326 down to the element type of an array. If ORIG_QUAL_TYPE is not
16327 NULL, then it should be used as the qualified type
16328 ORIG_QUAL_INDIRECT levels down in array type derivation (to
16329 preserve information about the typedef name from which an array
16330 type was derived). */
16331
16332 tree
16333 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
16334 size_t orig_qual_indirect)
16335 {
16336 if (type == error_mark_node)
16337 return type;
16338
16339 if (TREE_CODE (type) == ARRAY_TYPE)
16340 {
16341 tree t;
16342 tree element_type = c_build_qualified_type (TREE_TYPE (type),
16343 type_quals, orig_qual_type,
16344 orig_qual_indirect - 1);
16345
16346 /* See if we already have an identically qualified type. */
16347 if (orig_qual_type && orig_qual_indirect == 0)
16348 t = orig_qual_type;
16349 else
16350 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
16351 {
16352 if (TYPE_QUALS (strip_array_types (t)) == type_quals
16353 && TYPE_NAME (t) == TYPE_NAME (type)
16354 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
16355 && attribute_list_equal (TYPE_ATTRIBUTES (t),
16356 TYPE_ATTRIBUTES (type)))
16357 break;
16358 }
16359 if (!t)
16360 {
16361 tree domain = TYPE_DOMAIN (type);
16362
16363 t = build_variant_type_copy (type);
16364 TREE_TYPE (t) = element_type;
16365 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (element_type);
16366
16367 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
16368 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
16369 SET_TYPE_STRUCTURAL_EQUALITY (t);
16370 else if (TYPE_CANONICAL (element_type) != element_type
16371 || (domain && TYPE_CANONICAL (domain) != domain))
16372 {
16373 tree unqualified_canon
16374 = build_array_type (TYPE_CANONICAL (element_type),
16375 domain? TYPE_CANONICAL (domain)
16376 : NULL_TREE);
16377 if (TYPE_REVERSE_STORAGE_ORDER (type))
16378 {
16379 unqualified_canon
16380 = build_distinct_type_copy (unqualified_canon);
16381 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
16382 }
16383 TYPE_CANONICAL (t)
16384 = c_build_qualified_type (unqualified_canon, type_quals);
16385 }
16386 else
16387 TYPE_CANONICAL (t) = t;
16388 }
16389 return t;
16390 }
16391
16392 /* A restrict-qualified pointer type must be a pointer to object or
16393 incomplete type. Note that the use of POINTER_TYPE_P also allows
16394 REFERENCE_TYPEs, which is appropriate for C++. */
16395 if ((type_quals & TYPE_QUAL_RESTRICT)
16396 && (!POINTER_TYPE_P (type)
16397 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
16398 {
16399 error ("invalid use of %<restrict%>");
16400 type_quals &= ~TYPE_QUAL_RESTRICT;
16401 }
16402
16403 tree var_type = (orig_qual_type && orig_qual_indirect == 0
16404 ? orig_qual_type
16405 : build_qualified_type (type, type_quals));
16406 /* A variant type does not inherit the list of incomplete vars from the
16407 type main variant. */
16408 if ((RECORD_OR_UNION_TYPE_P (var_type)
16409 || TREE_CODE (var_type) == ENUMERAL_TYPE)
16410 && TYPE_MAIN_VARIANT (var_type) != var_type)
16411 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
16412 return var_type;
16413 }
16414
16415 /* Build a VA_ARG_EXPR for the C parser. */
16416
16417 tree
16418 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
16419 {
16420 if (error_operand_p (type))
16421 return error_mark_node;
16422 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
16423 order because it takes the address of the expression. */
16424 else if (handled_component_p (expr)
16425 && reverse_storage_order_for_component_p (expr))
16426 {
16427 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
16428 return error_mark_node;
16429 }
16430 else if (!COMPLETE_TYPE_P (type))
16431 {
16432 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
16433 "type %qT", type);
16434 return error_mark_node;
16435 }
16436 else if (TREE_CODE (type) == FUNCTION_TYPE)
16437 {
16438 error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
16439 type);
16440 return error_mark_node;
16441 }
16442 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
16443 warning_at (loc2, OPT_Wc___compat,
16444 "C++ requires promoted type, not enum type, in %<va_arg%>");
16445 return build_va_arg (loc2, expr, type);
16446 }
16447
16448 /* Return truthvalue of whether T1 is the same tree structure as T2.
16449 Return 1 if they are the same. Return false if they are different. */
16450
16451 bool
16452 c_tree_equal (tree t1, tree t2)
16453 {
16454 enum tree_code code1, code2;
16455
16456 if (t1 == t2)
16457 return true;
16458 if (!t1 || !t2)
16459 return false;
16460
16461 for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
16462 code1 = TREE_CODE (t1))
16463 t1 = TREE_OPERAND (t1, 0);
16464 for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
16465 code2 = TREE_CODE (t2))
16466 t2 = TREE_OPERAND (t2, 0);
16467
16468 /* They might have become equal now. */
16469 if (t1 == t2)
16470 return true;
16471
16472 if (code1 != code2)
16473 return false;
16474
16475 if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16476 return false;
16477
16478 switch (code1)
16479 {
16480 case INTEGER_CST:
16481 return wi::to_wide (t1) == wi::to_wide (t2);
16482
16483 case REAL_CST:
16484 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
16485
16486 case STRING_CST:
16487 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
16488 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
16489 TREE_STRING_LENGTH (t1));
16490
16491 case FIXED_CST:
16492 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
16493 TREE_FIXED_CST (t2));
16494
16495 case COMPLEX_CST:
16496 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
16497 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16498
16499 case VECTOR_CST:
16500 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16501
16502 case CONSTRUCTOR:
16503 /* We need to do this when determining whether or not two
16504 non-type pointer to member function template arguments
16505 are the same. */
16506 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16507 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16508 return false;
16509 {
16510 tree field, value;
16511 unsigned int i;
16512 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16513 {
16514 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16515 if (!c_tree_equal (field, elt2->index)
16516 || !c_tree_equal (value, elt2->value))
16517 return false;
16518 }
16519 }
16520 return true;
16521
16522 case TREE_LIST:
16523 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16524 return false;
16525 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16526 return false;
16527 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16528
16529 case SAVE_EXPR:
16530 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16531
16532 case CALL_EXPR:
16533 {
16534 tree arg1, arg2;
16535 call_expr_arg_iterator iter1, iter2;
16536 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16537 return false;
16538 for (arg1 = first_call_expr_arg (t1, &iter1),
16539 arg2 = first_call_expr_arg (t2, &iter2);
16540 arg1 && arg2;
16541 arg1 = next_call_expr_arg (&iter1),
16542 arg2 = next_call_expr_arg (&iter2))
16543 if (!c_tree_equal (arg1, arg2))
16544 return false;
16545 if (arg1 || arg2)
16546 return false;
16547 return true;
16548 }
16549
16550 case TARGET_EXPR:
16551 {
16552 tree o1 = TREE_OPERAND (t1, 0);
16553 tree o2 = TREE_OPERAND (t2, 0);
16554
16555 /* Special case: if either target is an unallocated VAR_DECL,
16556 it means that it's going to be unified with whatever the
16557 TARGET_EXPR is really supposed to initialize, so treat it
16558 as being equivalent to anything. */
16559 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16560 && !DECL_RTL_SET_P (o1))
16561 /*Nop*/;
16562 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16563 && !DECL_RTL_SET_P (o2))
16564 /*Nop*/;
16565 else if (!c_tree_equal (o1, o2))
16566 return false;
16567
16568 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16569 }
16570
16571 case COMPONENT_REF:
16572 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16573 return false;
16574 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16575
16576 case PARM_DECL:
16577 case VAR_DECL:
16578 case CONST_DECL:
16579 case FIELD_DECL:
16580 case FUNCTION_DECL:
16581 case IDENTIFIER_NODE:
16582 case SSA_NAME:
16583 return false;
16584
16585 case TREE_VEC:
16586 {
16587 unsigned ix;
16588 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16589 return false;
16590 for (ix = TREE_VEC_LENGTH (t1); ix--;)
16591 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16592 TREE_VEC_ELT (t2, ix)))
16593 return false;
16594 return true;
16595 }
16596
16597 CASE_CONVERT:
16598 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16599 return false;
16600 break;
16601
16602 default:
16603 break;
16604 }
16605
16606 switch (TREE_CODE_CLASS (code1))
16607 {
16608 case tcc_unary:
16609 case tcc_binary:
16610 case tcc_comparison:
16611 case tcc_expression:
16612 case tcc_vl_exp:
16613 case tcc_reference:
16614 case tcc_statement:
16615 {
16616 int i, n = TREE_OPERAND_LENGTH (t1);
16617
16618 switch (code1)
16619 {
16620 case PREINCREMENT_EXPR:
16621 case PREDECREMENT_EXPR:
16622 case POSTINCREMENT_EXPR:
16623 case POSTDECREMENT_EXPR:
16624 n = 1;
16625 break;
16626 case ARRAY_REF:
16627 n = 2;
16628 break;
16629 default:
16630 break;
16631 }
16632
16633 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16634 && n != TREE_OPERAND_LENGTH (t2))
16635 return false;
16636
16637 for (i = 0; i < n; ++i)
16638 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16639 return false;
16640
16641 return true;
16642 }
16643
16644 case tcc_type:
16645 return comptypes (t1, t2);
16646 default:
16647 gcc_unreachable ();
16648 }
16649 }
16650
16651 /* Returns true when the function declaration FNDECL is implicit,
16652 introduced as a result of a call to an otherwise undeclared
16653 function, and false otherwise. */
16654
16655 bool
16656 c_decl_implicit (const_tree fndecl)
16657 {
16658 return C_DECL_IMPLICIT (fndecl);
16659 }