Debian Codegraph @

gcc/c/c-typeck.cc

raw
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2025 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 "c-family/c-type-mismatch.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "asan.h"
55 #include "realmpfr.h"
56 #include "tree-pretty-print-markup.h"
57 #include "gcc-urlifier.h"
58
59 /* Possible cases of implicit conversions. Used to select diagnostic messages
60 and control folding initializers in convert_for_assignment. */
61 enum impl_conv {
62 ic_argpass,
63 ic_assign,
64 ic_init,
65 ic_init_const,
66 ic_return
67 };
68
69 /* The level of nesting inside "__alignof__". */
70 int in_alignof;
71
72 /* The level of nesting inside "sizeof". */
73 int in_sizeof;
74
75 /* The level of nesting inside "typeof". */
76 int in_typeof;
77
78 /* True when parsing OpenMP loop expressions. */
79 bool c_in_omp_for;
80
81 /* True when parsing OpenMP map clause. */
82 bool c_omp_array_section_p;
83
84 /* The argument of last parsed sizeof expression, only to be tested
85 if expr.original_code == SIZEOF_EXPR. */
86 tree c_last_sizeof_arg;
87 location_t c_last_sizeof_loc;
88
89 /* Nonzero if we might need to print a "missing braces around
90 initializer" message within this initializer. */
91 static int found_missing_braces;
92
93 static bool require_constant_value;
94 static bool require_constant_elements;
95 static bool require_constexpr_value;
96
97 static tree qualify_type (tree, tree);
98 struct comptypes_data;
99 static bool tagged_types_tu_compatible_p (const_tree, const_tree,
100 struct comptypes_data *);
101 static bool comp_target_types (location_t, tree, tree);
102 static bool function_types_compatible_p (const_tree, const_tree,
103 struct comptypes_data *);
104 static bool type_lists_compatible_p (const_tree, const_tree,
105 struct comptypes_data *);
106 static int convert_arguments (location_t, vec<location_t>, tree,
107 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
108 tree);
109 static tree pointer_diff (location_t, tree, tree, tree *);
110 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
111 enum impl_conv, bool, tree, tree, int,
112 int = 0);
113 static tree valid_compound_expr_initializer (tree, tree);
114 static void push_string (const char *);
115 static void push_member_name (tree);
116 static int spelling_length (void);
117 static char *print_spelling (char *);
118 static void warning_init (location_t, int, const char *);
119 static tree digest_init (location_t, tree, tree, tree, tree, bool, bool, bool,
120 bool, bool, bool);
121 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
122 bool, struct obstack *);
123 static void output_pending_init_elements (int, struct obstack *);
124 static bool set_designator (location_t, bool, struct obstack *);
125 static void push_range_stack (tree, struct obstack *);
126 static void add_pending_init (location_t, tree, tree, tree, bool,
127 struct obstack *);
128 static void set_nonincremental_init (struct obstack *);
129 static void set_nonincremental_init_from_string (tree, struct obstack *);
130 static tree find_init_member (tree, struct obstack *);
131 static void readonly_warning (tree, enum lvalue_use);
132 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
133 static void record_maybe_used_decl (tree);
134 static bool comptypes_internal (const_tree, const_tree,
135 struct comptypes_data *data);
136
137 /* Return true if EXP is a null pointer constant, false otherwise. */
138
139 bool
140 null_pointer_constant_p (const_tree expr)
141 {
142 /* This should really operate on c_expr structures, but they aren't
143 yet available everywhere required. */
144 tree type = TREE_TYPE (expr);
145
146 /* An integer constant expression with the value 0, such an expression
147 cast to type void*, or the predefined constant nullptr, are a null
148 pointer constant. */
149 if (expr == nullptr_node)
150 return true;
151
152 return (TREE_CODE (expr) == INTEGER_CST
153 && !TREE_OVERFLOW (expr)
154 && integer_zerop (expr)
155 && (INTEGRAL_TYPE_P (type)
156 || (TREE_CODE (type) == POINTER_TYPE
157 && VOID_TYPE_P (TREE_TYPE (type))
158 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
159 }
160
161 /* EXPR may appear in an unevaluated part of an integer constant
162 expression, but not in an evaluated part. Wrap it in a
163 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
164 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
165
166 static tree
167 note_integer_operands (tree expr)
168 {
169 tree ret;
170 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
171 {
172 ret = copy_node (expr);
173 TREE_OVERFLOW (ret) = 1;
174 }
175 else
176 {
177 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
178 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
179 }
180 return ret;
181 }
182
183 /* Having checked whether EXPR may appear in an unevaluated part of an
184 integer constant expression and found that it may, remove any
185 C_MAYBE_CONST_EXPR noting this fact and return the resulting
186 expression. */
187
188 static inline tree
189 remove_c_maybe_const_expr (tree expr)
190 {
191 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
192 return C_MAYBE_CONST_EXPR_EXPR (expr);
193 else
194 return expr;
195 }
196
197 /* This is a cache to hold if two types are seen. */
198
199 struct tagged_tu_seen_cache {
200 const struct tagged_tu_seen_cache * next;
201 const_tree t1;
202 const_tree t2;
203 };
204
205 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
206 does not have an incomplete type. (That includes void types.)
207 LOC is the location of the use. */
208
209 tree
210 require_complete_type (location_t loc, tree value)
211 {
212 tree type = TREE_TYPE (value);
213
214 if (error_operand_p (value))
215 return error_mark_node;
216
217 /* First, detect a valid value with a complete type. */
218 if (COMPLETE_TYPE_P (type))
219 return value;
220
221 c_incomplete_type_error (loc, value, type);
222 return error_mark_node;
223 }
224
225 /* Print an error message for invalid use of an incomplete type.
226 VALUE is the expression that was used (or 0 if that isn't known)
227 and TYPE is the type that was invalid. LOC is the location for
228 the error. */
229
230 void
231 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
232 {
233 /* Avoid duplicate error message. */
234 if (TREE_CODE (type) == ERROR_MARK)
235 return;
236
237 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
238 error_at (loc, "%qD has an incomplete type %qT", value, type);
239 else
240 {
241 retry:
242 /* We must print an error message. Be clever about what it says. */
243
244 switch (TREE_CODE (type))
245 {
246 case RECORD_TYPE:
247 case UNION_TYPE:
248 case ENUMERAL_TYPE:
249 break;
250
251 case VOID_TYPE:
252 error_at (loc, "invalid use of void expression");
253 return;
254
255 case ARRAY_TYPE:
256 if (TYPE_DOMAIN (type))
257 {
258 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
259 {
260 error_at (loc, "invalid use of flexible array member");
261 return;
262 }
263 type = TREE_TYPE (type);
264 goto retry;
265 }
266 error_at (loc, "invalid use of array with unspecified bounds");
267 return;
268
269 default:
270 gcc_unreachable ();
271 }
272
273 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
274 error_at (loc, "invalid use of undefined type %qT", type);
275 else
276 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
277 error_at (loc, "invalid use of incomplete typedef %qT", type);
278 }
279 }
280
281 /* Given a type, apply default promotions wrt unnamed function
282 arguments and return the new type. */
283
284 tree
285 c_type_promotes_to (tree type)
286 {
287 tree ret = NULL_TREE;
288
289 if (TYPE_MAIN_VARIANT (type) == float_type_node)
290 ret = double_type_node;
291 else if (c_promoting_integer_type_p (type))
292 {
293 /* Preserve unsignedness if not really getting any wider. */
294 if (TYPE_UNSIGNED (type)
295 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
296 ret = unsigned_type_node;
297 else
298 ret = integer_type_node;
299 }
300
301 if (ret != NULL_TREE)
302 return (TYPE_ATOMIC (type)
303 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
304 : ret);
305
306 return type;
307 }
308
309 /* Return true if between two named address spaces, whether there is a superset
310 named address space that encompasses both address spaces. If there is a
311 superset, return which address space is the superset. */
312
313 static bool
314 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
315 {
316 if (as1 == as2)
317 {
318 *common = as1;
319 return true;
320 }
321 else if (targetm.addr_space.subset_p (as1, as2))
322 {
323 *common = as2;
324 return true;
325 }
326 else if (targetm.addr_space.subset_p (as2, as1))
327 {
328 *common = as1;
329 return true;
330 }
331 else
332 return false;
333 }
334
335 /* Return a variant of TYPE which has all the type qualifiers of LIKE
336 as well as those of TYPE. */
337
338 static tree
339 qualify_type (tree type, tree like)
340 {
341 addr_space_t as_type = TYPE_ADDR_SPACE (type);
342 addr_space_t as_like = TYPE_ADDR_SPACE (like);
343 addr_space_t as_common;
344
345 /* If the two named address spaces are different, determine the common
346 superset address space. If there isn't one, raise an error. */
347 if (!addr_space_superset (as_type, as_like, &as_common))
348 {
349 as_common = as_type;
350 error ("%qT and %qT are in disjoint named address spaces",
351 type, like);
352 }
353
354 return c_build_qualified_type (type,
355 TYPE_QUALS_NO_ADDR_SPACE (type)
356 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
357 | ENCODE_QUAL_ADDR_SPACE (as_common));
358 }
359
360
361 /* Check consistency of type TYPE. For derived types, we test that
362 C_TYPE_VARIABLE_SIZE and C_TYPE_VARIABLY_MODIFIED are consistent with
363 the requirements of the base type. We also check that arrays with a
364 non-constant length are marked with C_TYPE_VARIABLE_SIZE. If any
365 inconsistency is detected false is returned and true otherwise. */
366
367 static bool
368 c_verify_type (tree type)
369 {
370 switch (TREE_CODE (type))
371 {
372 case POINTER_TYPE:
373 case FUNCTION_TYPE:
374 /* Pointer and funcions can not have variable size. */
375 if (C_TYPE_VARIABLE_SIZE (type))
376 return false;
377 /* Pointer and funcions are variably modified if and only if the
378 return / target type is variably modified. */
379 if (C_TYPE_VARIABLY_MODIFIED (type)
380 != C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type)))
381 return false;
382 break;
383 case ARRAY_TYPE:
384 /* An array has variable size if and only if it has a non-constant
385 dimensions or its element type has variable size. */
386 if ((C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
387 || (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
388 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
389 != INTEGER_CST))
390 != C_TYPE_VARIABLE_SIZE (type))
391 return false;
392 /* If the element type or the array has variable size, then the
393 array has variable size and is variably modified. */
394 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
395 || C_TYPE_VARIABLE_SIZE (type))
396 {
397 if (!C_TYPE_VARIABLE_SIZE (type))
398 return false;
399 if (!C_TYPE_VARIABLY_MODIFIED (type))
400 return false;
401 }
402 /* If the element type is variably modified, then also the array. */
403 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type))
404 && !C_TYPE_VARIABLY_MODIFIED (type))
405 return false;
406 break;
407 default:
408 break;
409 }
410 return true;
411 }
412
413 /* Propagate C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
414 from a base type to a newly built derived or qualified type. */
415
416 static tree
417 c_set_type_bits (tree new_type, tree old_type)
418 {
419 gcc_checking_assert (c_verify_type (old_type));
420
421 if (C_TYPE_VARIABLY_MODIFIED (old_type))
422 C_TYPE_VARIABLY_MODIFIED (new_type) = true;
423
424 if (TREE_CODE (new_type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (old_type))
425 {
426 C_TYPE_VARIABLY_MODIFIED (new_type) = true;
427 C_TYPE_VARIABLE_SIZE (new_type) = true;
428 }
429 return new_type;
430 }
431
432 /* Build a pointer type using the default pointer mode. */
433
434 tree
435 c_build_pointer_type (tree to_type)
436 {
437 addr_space_t as = to_type == error_mark_node ? ADDR_SPACE_GENERIC
438 : TYPE_ADDR_SPACE (to_type);
439 machine_mode pointer_mode;
440
441 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
442 pointer_mode = targetm.addr_space.pointer_mode (as);
443 else
444 pointer_mode = c_default_pointer_mode;
445
446 return c_build_pointer_type_for_mode (to_type, pointer_mode, false);
447 }
448
449 /* Build a pointer type using the given mode. */
450
451 tree
452 c_build_pointer_type_for_mode (tree type, machine_mode mode, bool m)
453 {
454 tree ret = build_pointer_type_for_mode (type, mode, m);
455 return c_set_type_bits (ret, type);
456 }
457
458 /* Build a function type. */
459
460 tree
461 c_build_function_type (tree type, tree args, bool no)
462 {
463 tree ret = build_function_type (type, args, no);
464 return c_set_type_bits (ret, type);
465 }
466
467 /* Build an array type. This sets typeless storage as required
468 by C2Y and C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
469 based on the element type and domain. */
470
471 tree
472 c_build_array_type (tree type, tree domain)
473 {
474 int type_quals = TYPE_QUALS (type);
475
476 /* Identify typeless storage as introduced in C2Y
477 and supported also in earlier language modes. */
478 bool typeless = (char_type_p (type) && !(type_quals & TYPE_QUAL_ATOMIC))
479 || (AGGREGATE_TYPE_P (type) && TYPE_TYPELESS_STORAGE (type));
480
481 tree ret = build_array_type (type, domain, typeless);
482
483 if (domain && TYPE_MAX_VALUE (domain)
484 && TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
485 {
486 C_TYPE_VARIABLE_SIZE (ret) = 1;
487 C_TYPE_VARIABLY_MODIFIED (ret) = 1;
488 }
489
490 return c_set_type_bits (ret, type);
491 }
492
493
494 /* Build an array type of unspecified size. */
495 tree
496 c_build_array_type_unspecified (tree type)
497 {
498 tree upper = build2 (COMPOUND_EXPR, TREE_TYPE (size_zero_node),
499 integer_zero_node, size_zero_node);
500 return c_build_array_type (type, build_index_type (upper));
501 }
502
503
504 tree
505 c_build_type_attribute_qual_variant (tree type, tree attrs, int quals)
506 {
507 tree ret = build_type_attribute_qual_variant (type, attrs, quals);
508 return c_set_type_bits (ret, type);
509 }
510
511 tree
512 c_build_type_attribute_variant (tree type, tree attrs)
513 {
514 return c_build_type_attribute_qual_variant (type, attrs, TYPE_QUALS (type));
515 }
516
517 /* Reconstruct a complex derived type. This is used to re-construct types
518 with the vector attribute. It is called via a langhook. */
519
520 tree
521 c_reconstruct_complex_type (tree type, tree bottom)
522 {
523 tree inner, outer;
524
525 if (TREE_CODE (type) == POINTER_TYPE)
526 {
527 inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
528 outer = c_build_pointer_type_for_mode (inner, TYPE_MODE (type),
529 TYPE_REF_CAN_ALIAS_ALL (type));
530 }
531 else if (TREE_CODE (type) == ARRAY_TYPE)
532 {
533 bool rso = TYPE_REVERSE_STORAGE_ORDER (type);
534 inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
535 outer = c_build_array_type (inner, TYPE_DOMAIN (type));
536 if (rso)
537 {
538 outer = build_distinct_type_copy (outer);
539 TYPE_REVERSE_STORAGE_ORDER (outer) = 1;
540 }
541
542 gcc_checking_assert (C_TYPE_VARIABLE_SIZE (type)
543 == C_TYPE_VARIABLE_SIZE (outer));
544 }
545 else if (TREE_CODE (type) == FUNCTION_TYPE)
546 {
547 inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
548 outer = c_build_function_type (inner, TYPE_ARG_TYPES (type),
549 TYPE_NO_NAMED_ARGS_STDARG_P (type));
550 }
551 else if (TREE_CODE (type) == REFERENCE_TYPE
552 || TREE_CODE (type) == OFFSET_TYPE)
553 gcc_unreachable ();
554 else
555 return bottom;
556
557 return c_build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
558 TYPE_QUALS (type));
559 }
560
561
562 /* Helper function for c_canonical_type. Check whether FIELD
563 contains a pointer to a structure or union with tag,
564 possibly nested in other type derivations, and return the
565 type of this nested structure or union. */
566 static tree
567 ptr_to_tagged_member (tree field)
568 {
569 gcc_assert (FIELD_DECL == TREE_CODE (field));
570 tree type = TREE_TYPE (field);
571 bool ptr_seen = false;
572 while (TREE_CODE (type) == ARRAY_TYPE
573 || TREE_CODE (type) == FUNCTION_TYPE
574 || TREE_CODE (type) == POINTER_TYPE)
575 {
576 if (TREE_CODE (type) == POINTER_TYPE)
577 ptr_seen = true;
578 type = TREE_TYPE (type);
579 }
580
581 if (ptr_seen
582 && RECORD_OR_UNION_TYPE_P (type)
583 && NULL_TREE != c_type_tag (type))
584 return type;
585
586 return NULL_TREE;
587 }
588
589 /* For a record or union type, make necessary adaptations so that the
590 type can be used as TYPE_CANONICAL.
591
592 If the TYPE contains a pointer (possibly nested in other type
593 derivations) to a structure or union as a member, create a copy and
594 change the nested type to an incomplete type. Otherwise, the middle-end
595 gets confused when recording component aliases in the case where we
596 have formed equivalency classes that include types for which these
597 member pointers end up pointing to other structure or unions types
598 which have the same tag but are not compatible. */
599 tree
600 c_type_canonical (tree type)
601 {
602 gcc_assert (RECORD_OR_UNION_TYPE_P (type));
603
604 bool needs_mod = false;
605 for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
606 {
607 if (!ptr_to_tagged_member (x))
608 continue;
609 needs_mod = true;
610 break;
611 }
612 if (!needs_mod)
613 return type;
614
615 /* Construct new version with such members replaced. */
616 tree n = build_distinct_type_copy (type);
617 tree *fields = &TYPE_FIELDS (n);
618 for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
619 {
620 tree f = copy_node (x);
621 if (tree m = ptr_to_tagged_member (x))
622 {
623 tree new_node = make_node (TREE_CODE (m));
624 TYPE_NAME (new_node) = TYPE_NAME (m);
625 SET_TYPE_STRUCTURAL_EQUALITY (new_node);
626 new_node = qualify_type (new_node, m);
627 TREE_TYPE (f) = c_reconstruct_complex_type (TREE_TYPE (x),
628 new_node);
629 }
630 *fields = f;
631 fields = &DECL_CHAIN (f);
632 }
633 TYPE_CANONICAL (n) = n;
634 return n;
635 }
636
637
638 /* If NTYPE is a type of a non-variadic function with a prototype
639 and OTYPE is a type of a function without a prototype and ATTRS
640 contains attribute format, diagnosess and removes it from ATTRS.
641 Returns the result of build_type_attribute_variant of NTYPE and
642 the (possibly) modified ATTRS. */
643
644 static tree
645 c_build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
646 {
647 if (!prototype_p (otype)
648 && prototype_p (ntype)
649 && lookup_attribute ("format", attrs))
650 {
651 warning_at (input_location, OPT_Wattributes,
652 "%qs attribute can only be applied to variadic functions",
653 "format");
654 attrs = remove_attribute ("format", attrs);
655 }
656 return c_build_type_attribute_variant (ntype, attrs);
657
658 }
659
660 /* Given a type which could be a typedef name, make sure to return the
661 original type. See set_underlying_type. */
662 static const_tree
663 c_type_original (const_tree t)
664 {
665 /* It may even be a typedef of a typedef...
666 In the case of compiler-created builtin structs the TYPE_DECL
667 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
668 while (TYPE_NAME (t)
669 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
670 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
671 t = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
672 return t;
673 }
674
675 /* Return the tag for a tagged type. */
676 tree
677 c_type_tag (const_tree t)
678 {
679 gcc_assert (RECORD_OR_UNION_TYPE_P (t) || TREE_CODE (t) == ENUMERAL_TYPE);
680 const_tree orig = c_type_original (t);
681 tree name = TYPE_NAME (orig);
682 if (!name)
683 return NULL_TREE;
684 if (TREE_CODE (name) == TYPE_DECL)
685 {
686 if (!DECL_NAME (name))
687 return NULL_TREE;
688 name = DECL_NAME (name);
689 }
690 gcc_checking_assert (TREE_CODE (name) == IDENTIFIER_NODE);
691 return name;
692 }
693
694
695
696 /* Return the composite type of two compatible types.
697
698 We assume that comptypes has already been done and returned
699 nonzero; if that isn't so, this may crash. In particular, we
700 assume that qualifiers match. */
701
702 struct composite_cache {
703 tree t1;
704 tree t2;
705 tree composite;
706 struct composite_cache* next;
707 };
708
709 tree
710 composite_type_internal (tree t1, tree t2, struct composite_cache* cache)
711 {
712 enum tree_code code1;
713 enum tree_code code2;
714 tree attributes;
715
716 /* Save time if the two types are the same. */
717
718 if (t1 == t2) return t1;
719
720 /* If one type is nonsense, use the other. */
721 if (t1 == error_mark_node)
722 return t2;
723 if (t2 == error_mark_node)
724 return t1;
725
726 code1 = TREE_CODE (t1);
727 code2 = TREE_CODE (t2);
728
729 /* Merge the attributes. */
730 attributes = targetm.merge_type_attributes (t1, t2);
731
732 /* If one is an enumerated type and the other is the compatible
733 integer type, the composite type might be either of the two
734 (DR#013 question 3). For consistency, use the enumerated type as
735 the composite type. */
736
737 if (code1 == ENUMERAL_TYPE
738 && (code2 == INTEGER_TYPE
739 || code2 == BOOLEAN_TYPE))
740 return t1;
741 if (code2 == ENUMERAL_TYPE
742 && (code1 == INTEGER_TYPE
743 || code1 == BOOLEAN_TYPE))
744 return t2;
745
746 gcc_assert (code1 == code2);
747
748 switch (code1)
749 {
750 case POINTER_TYPE:
751 /* For two pointers, do this recursively on the target type. */
752 {
753 tree pointed_to_1 = TREE_TYPE (t1);
754 tree pointed_to_2 = TREE_TYPE (t2);
755 tree target = composite_type_internal (pointed_to_1,
756 pointed_to_2, cache);
757 t1 = c_build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
758 t1 = c_build_type_attribute_variant (t1, attributes);
759 return qualify_type (t1, t2);
760 }
761
762 case ARRAY_TYPE:
763 {
764 tree elt = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
765 cache);
766 int quals;
767 tree unqual_elt;
768 tree d1 = TYPE_DOMAIN (t1);
769 tree d2 = TYPE_DOMAIN (t2);
770 bool d1_variable, d2_variable;
771 bool d1_zero, d2_zero;
772 bool t1_complete, t2_complete;
773
774 /* We should not have any type quals on arrays at all. */
775 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
776 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
777
778 t1_complete = COMPLETE_TYPE_P (t1);
779 t2_complete = COMPLETE_TYPE_P (t2);
780
781 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
782 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
783
784 d1_variable = (!d1_zero
785 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
786 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
787 d2_variable = (!d2_zero
788 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
789 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
790
791 bool use1 = TYPE_DOMAIN (t1)
792 && (d2_variable || d2_zero || !d1_variable);
793 bool use2 = TYPE_DOMAIN (t2)
794 && (d1_variable || d1_zero || !d2_variable);
795
796 /* If the first is an unspecified size pick the other one. */
797 if (d2_variable && c_type_unspecified_p (t1))
798 {
799 gcc_assert (use1 && use2);
800 use1 = false;
801 }
802
803 /* Save space: see if the result is identical to one of the args. */
804 if (elt == TREE_TYPE (t1) && use1)
805 return c_build_type_attribute_variant (t1, attributes);
806 if (elt == TREE_TYPE (t2) && use2)
807 return c_build_type_attribute_variant (t2, attributes);
808
809 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
810 return c_build_type_attribute_variant (t1, attributes);
811 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
812 return c_build_type_attribute_variant (t2, attributes);
813
814 /* Merge the element types, and have a size if either arg has
815 one. We may have qualifiers on the element types. To set
816 up TYPE_MAIN_VARIANT correctly, we need to form the
817 composite of the unqualified types and add the qualifiers
818 back at the end. */
819 quals = TYPE_QUALS (strip_array_types (elt));
820 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
821 t1 = c_build_array_type (unqual_elt, TYPE_DOMAIN (use1 ? t1 : t2));
822
823 /* Check that a type which has a varying outermost dimension
824 got marked has having a variable size. */
825 bool varsize = (d1_variable && d2_variable)
826 || (d1_variable && !t2_complete)
827 || (d2_variable && !t1_complete);
828 gcc_checking_assert (!varsize || C_TYPE_VARIABLE_SIZE (t1));
829
830 /* Ensure a composite type involving a zero-length array type
831 is a zero-length type not an incomplete type. */
832 if (d1_zero && d2_zero
833 && (t1_complete || t2_complete)
834 && !COMPLETE_TYPE_P (t1))
835 {
836 TYPE_SIZE (t1) = bitsize_zero_node;
837 TYPE_SIZE_UNIT (t1) = size_zero_node;
838 }
839 t1 = c_build_qualified_type (t1, quals);
840 return c_build_type_attribute_variant (t1, attributes);
841 }
842
843 case RECORD_TYPE:
844 case UNION_TYPE:
845 if (flag_isoc23 && !comptypes_same_p (t1, t2))
846 {
847 /* Go to the original type to get the right tag. */
848 tree tag = c_type_tag (t1);
849
850 gcc_checking_assert (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2));
851 gcc_checking_assert (!tag || comptypes (t1, t2));
852
853 /* If a composite type for these two types is already under
854 construction, return it. */
855
856 for (struct composite_cache *c = cache; c != NULL; c = c->next)
857 if (c->t1 == t1 && c->t2 == t2)
858 return c->composite;
859
860 /* Otherwise, create a new type node and link it into the cache. */
861
862 tree n = make_node (code1);
863 SET_TYPE_STRUCTURAL_EQUALITY (n);
864 TYPE_NAME (n) = tag;
865
866 struct composite_cache cache2 = { t1, t2, n, cache };
867 cache = &cache2;
868
869 tree f1 = TYPE_FIELDS (t1);
870 tree f2 = TYPE_FIELDS (t2);
871 tree fields = NULL_TREE;
872
873 for (tree a = f1, b = f2; a && b;
874 a = DECL_CHAIN (a), b = DECL_CHAIN (b))
875 {
876 tree ta = TREE_TYPE (a);
877 tree tb = TREE_TYPE (b);
878
879 if (DECL_C_BIT_FIELD (a))
880 {
881 ta = DECL_BIT_FIELD_TYPE (a);
882 tb = DECL_BIT_FIELD_TYPE (b);
883 }
884
885 gcc_assert (DECL_NAME (a) == DECL_NAME (b));
886 gcc_checking_assert (!DECL_NAME (a) || comptypes (ta, tb));
887
888 tree t = composite_type_internal (ta, tb, cache);
889 tree f = build_decl (input_location, FIELD_DECL, DECL_NAME (a), t);
890
891 DECL_PACKED (f) = DECL_PACKED (a);
892 SET_DECL_ALIGN (f, DECL_ALIGN (a));
893 DECL_ATTRIBUTES (f) = DECL_ATTRIBUTES (a);
894 C_DECL_VARIABLE_SIZE (f) = C_TYPE_VARIABLE_SIZE (t);
895
896 decl_attributes (&f, DECL_ATTRIBUTES (f), 0);
897
898 finish_decl (f, input_location, NULL, NULL, NULL);
899
900 if (DECL_C_BIT_FIELD (a))
901 {
902 /* This will be processed by finish_struct. */
903 SET_DECL_C_BIT_FIELD (f);
904 DECL_INITIAL (f) = build_int_cst (integer_type_node,
905 tree_to_uhwi (DECL_SIZE (a)));
906 DECL_NONADDRESSABLE_P (f) = true;
907 DECL_PADDING_P (f) = !DECL_NAME (a);
908 }
909
910 DECL_CHAIN (f) = fields;
911 fields = f;
912 }
913
914 fields = nreverse (fields);
915
916 /* Setup the struct/union type. Because we inherit all variably
917 modified components, we can ignore the size expression. */
918 tree expr = NULL_TREE;
919
920 /* Set TYPE_STUB_DECL for debugging symbols. */
921 TYPE_STUB_DECL (n) = pushdecl (build_decl (input_location, TYPE_DECL,
922 NULL_TREE, n));
923
924 n = finish_struct (input_location, n, fields, attributes, NULL,
925 &expr);
926
927 n = qualify_type (n, t1);
928
929 gcc_checking_assert (!TYPE_NAME (n) || comptypes (n, t1));
930 gcc_checking_assert (!TYPE_NAME (n) || comptypes (n, t2));
931
932 return n;
933 }
934 /* FALLTHRU */
935 case ENUMERAL_TYPE:
936 if (attributes != NULL)
937 {
938 /* Try harder not to create a new aggregate type. */
939 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
940 return t1;
941 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
942 return t2;
943 }
944 return c_build_type_attribute_variant (t1, attributes);
945
946 case FUNCTION_TYPE:
947 /* Function types: prefer the one that specified arg types.
948 If both do, merge the arg types. Also merge the return types. */
949 {
950 tree valtype = composite_type_internal (TREE_TYPE (t1),
951 TREE_TYPE (t2), cache);
952 tree p1 = TYPE_ARG_TYPES (t1);
953 tree p2 = TYPE_ARG_TYPES (t2);
954 int len;
955 tree newargs, n;
956 int i;
957
958 /* Save space: see if the result is identical to one of the args. */
959 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
960 return c_build_functype_attribute_variant (t1, t2, attributes);
961 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
962 return c_build_functype_attribute_variant (t2, t1, attributes);
963
964 /* Simple way if one arg fails to specify argument types. */
965 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
966 {
967 t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t2),
968 TYPE_NO_NAMED_ARGS_STDARG_P (t2));
969 t1 = c_build_type_attribute_variant (t1, attributes);
970 return qualify_type (t1, t2);
971 }
972 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
973 {
974 t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t1),
975 TYPE_NO_NAMED_ARGS_STDARG_P (t1));
976 t1 = c_build_type_attribute_variant (t1, attributes);
977 return qualify_type (t1, t2);
978 }
979
980 /* If both args specify argument types, we must merge the two
981 lists, argument by argument. */
982
983 for (len = 0, newargs = p1;
984 newargs && newargs != void_list_node;
985 len++, newargs = TREE_CHAIN (newargs))
986 ;
987
988 for (i = 0; i < len; i++)
989 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
990
991 n = newargs;
992
993 for (; p1 && p1 != void_list_node;
994 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
995 {
996 tree mv1 = TREE_VALUE (p1);
997 if (mv1 && mv1 != error_mark_node
998 && TREE_CODE (mv1) != ARRAY_TYPE)
999 mv1 = TYPE_MAIN_VARIANT (mv1);
1000
1001 tree mv2 = TREE_VALUE (p2);
1002 if (mv2 && mv2 != error_mark_node
1003 && TREE_CODE (mv2) != ARRAY_TYPE)
1004 mv2 = TYPE_MAIN_VARIANT (mv2);
1005
1006 /* A null type means arg type is not specified.
1007 Take whatever the other function type has. */
1008 if (TREE_VALUE (p1) == NULL_TREE)
1009 {
1010 TREE_VALUE (n) = TREE_VALUE (p2);
1011 goto parm_done;
1012 }
1013 if (TREE_VALUE (p2) == NULL_TREE)
1014 {
1015 TREE_VALUE (n) = TREE_VALUE (p1);
1016 goto parm_done;
1017 }
1018
1019 /* Given wait (union {union wait *u; int *i} *)
1020 and wait (union wait *),
1021 prefer union wait * as type of parm. */
1022 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
1023 && TREE_VALUE (p1) != TREE_VALUE (p2))
1024 {
1025 tree memb;
1026 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
1027 memb; memb = DECL_CHAIN (memb))
1028 {
1029 tree mv3 = TREE_TYPE (memb);
1030 if (mv3 && mv3 != error_mark_node
1031 && TREE_CODE (mv3) != ARRAY_TYPE)
1032 mv3 = TYPE_MAIN_VARIANT (mv3);
1033 if (comptypes (mv3, mv2))
1034 {
1035 TREE_VALUE (n) = composite_type_internal (TREE_TYPE (memb),
1036 TREE_VALUE (p2),
1037 cache);
1038 pedwarn (input_location, OPT_Wpedantic,
1039 "function types not truly compatible in ISO C");
1040 goto parm_done;
1041 }
1042 }
1043 }
1044 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
1045 && TREE_VALUE (p2) != TREE_VALUE (p1))
1046 {
1047 tree memb;
1048 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
1049 memb; memb = DECL_CHAIN (memb))
1050 {
1051 tree mv3 = TREE_TYPE (memb);
1052 if (mv3 && mv3 != error_mark_node
1053 && TREE_CODE (mv3) != ARRAY_TYPE)
1054 mv3 = TYPE_MAIN_VARIANT (mv3);
1055 if (comptypes (mv3, mv1))
1056 {
1057 TREE_VALUE (n)
1058 = composite_type_internal (TREE_TYPE (memb),
1059 TREE_VALUE (p1),
1060 cache);
1061 pedwarn (input_location, OPT_Wpedantic,
1062 "function types not truly compatible in ISO C");
1063 goto parm_done;
1064 }
1065 }
1066 }
1067 TREE_VALUE (n) = composite_type_internal (mv1, mv2, cache);
1068 parm_done: ;
1069 }
1070
1071 t1 = c_build_function_type (valtype, newargs);
1072 t1 = qualify_type (t1, t2);
1073 }
1074 /* FALLTHRU */
1075
1076 default:
1077 return c_build_type_attribute_variant (t1, attributes);
1078 }
1079 }
1080
1081 tree
1082 composite_type (tree t1, tree t2)
1083 {
1084 struct composite_cache cache = { };
1085 return composite_type_internal (t1, t2, &cache);
1086 }
1087
1088 /* Return the type of a conditional expression between pointers to
1089 possibly differently qualified versions of compatible types.
1090
1091 We assume that comp_target_types has already been done and returned
1092 true; if that isn't so, this may crash. */
1093
1094 static tree
1095 common_pointer_type (tree t1, tree t2)
1096 {
1097 tree attributes;
1098 tree pointed_to_1, mv1;
1099 tree pointed_to_2, mv2;
1100 tree target;
1101 unsigned target_quals;
1102 addr_space_t as1, as2, as_common;
1103 int quals1, quals2;
1104
1105 /* Save time if the two types are the same. */
1106
1107 if (t1 == t2) return t1;
1108
1109 /* If one type is nonsense, use the other. */
1110 if (t1 == error_mark_node)
1111 return t2;
1112 if (t2 == error_mark_node)
1113 return t1;
1114
1115 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
1116 && TREE_CODE (t2) == POINTER_TYPE);
1117
1118 /* Merge the attributes. */
1119 attributes = targetm.merge_type_attributes (t1, t2);
1120
1121 /* Find the composite type of the target types, and combine the
1122 qualifiers of the two types' targets. Do not lose qualifiers on
1123 array element types by taking the TYPE_MAIN_VARIANT. */
1124 mv1 = pointed_to_1 = TREE_TYPE (t1);
1125 mv2 = pointed_to_2 = TREE_TYPE (t2);
1126 if (TREE_CODE (mv1) != ARRAY_TYPE)
1127 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
1128 if (TREE_CODE (mv2) != ARRAY_TYPE)
1129 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
1130 target = composite_type (mv1, mv2);
1131
1132 /* Strip array types to get correct qualifier for pointers to arrays */
1133 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
1134 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
1135
1136 /* For function types do not merge const qualifiers, but drop them
1137 if used inconsistently. The middle-end uses these to mark const
1138 and noreturn functions. */
1139 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
1140 target_quals = (quals1 & quals2);
1141 else
1142 target_quals = (quals1 | quals2);
1143
1144 /* If the two named address spaces are different, determine the common
1145 superset address space. This is guaranteed to exist due to the
1146 assumption that comp_target_type returned true. */
1147 as1 = TYPE_ADDR_SPACE (pointed_to_1);
1148 as2 = TYPE_ADDR_SPACE (pointed_to_2);
1149 if (!addr_space_superset (as1, as2, &as_common))
1150 gcc_unreachable ();
1151
1152 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
1153
1154 t1 = c_build_pointer_type (c_build_qualified_type (target, target_quals));
1155 return c_build_type_attribute_variant (t1, attributes);
1156 }
1157
1158 /* Return the common type for two arithmetic types under the usual
1159 arithmetic conversions. The default conversions have already been
1160 applied, and enumerated types converted to their compatible integer
1161 types. The resulting type is unqualified and has no attributes.
1162
1163 This is the type for the result of most arithmetic operations
1164 if the operands have the given two types. */
1165
1166 static tree
1167 c_common_type (tree t1, tree t2)
1168 {
1169 enum tree_code code1;
1170 enum tree_code code2;
1171
1172 /* If one type is nonsense, use the other. */
1173 if (t1 == error_mark_node)
1174 return t2;
1175 if (t2 == error_mark_node)
1176 return t1;
1177
1178 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
1179 t1 = TYPE_MAIN_VARIANT (t1);
1180
1181 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
1182 t2 = TYPE_MAIN_VARIANT (t2);
1183
1184 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
1185 {
1186 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
1187 t1 = c_build_type_attribute_variant (t1, attrs);
1188 }
1189
1190 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
1191 {
1192 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
1193 t2 = c_build_type_attribute_variant (t2, attrs);
1194 }
1195
1196 /* Save time if the two types are the same. */
1197
1198 if (t1 == t2) return t1;
1199
1200 code1 = TREE_CODE (t1);
1201 code2 = TREE_CODE (t2);
1202
1203 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
1204 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
1205 || code1 == INTEGER_TYPE || code1 == BITINT_TYPE);
1206 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
1207 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
1208 || code2 == INTEGER_TYPE || code2 == BITINT_TYPE);
1209
1210 /* When one operand is a decimal float type, the other operand cannot be
1211 a generic float type or a complex type. We also disallow vector types
1212 here. */
1213 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
1214 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
1215 {
1216 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
1217 {
1218 error ("cannot mix operands of decimal floating and vector types");
1219 return error_mark_node;
1220 }
1221 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1222 {
1223 error ("cannot mix operands of decimal floating and complex types");
1224 return error_mark_node;
1225 }
1226 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1227 {
1228 error ("cannot mix operands of decimal floating "
1229 "and other floating types");
1230 return error_mark_node;
1231 }
1232 }
1233
1234 /* If one type is a vector type, return that type. (How the usual
1235 arithmetic conversions apply to the vector types extension is not
1236 precisely specified.) */
1237 if (code1 == VECTOR_TYPE)
1238 return t1;
1239
1240 if (code2 == VECTOR_TYPE)
1241 return t2;
1242
1243 /* If one type is complex, form the common type of the non-complex
1244 components, then make that complex. Use T1 or T2 if it is the
1245 required type. */
1246 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1247 {
1248 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
1249 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
1250 tree subtype = c_common_type (subtype1, subtype2);
1251
1252 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
1253 return t1;
1254 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
1255 return t2;
1256 else if (TREE_CODE (subtype) == BITINT_TYPE)
1257 {
1258 sorry ("%<_Complex _BitInt(%d)%> unsupported",
1259 TYPE_PRECISION (subtype));
1260 return code1 == COMPLEX_TYPE ? t1 : t2;
1261 }
1262 else
1263 return build_complex_type (subtype);
1264 }
1265
1266 /* If only one is real, use it as the result. */
1267
1268 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
1269 return t1;
1270
1271 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
1272 return t2;
1273
1274 /* If both are real and either are decimal floating point types, use
1275 the decimal floating point type with the greater precision. */
1276
1277 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1278 {
1279 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
1280 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
1281 return dfloat128_type_node;
1282 /* And prefer _Decimal128 over _Decimal64x which has in GCC's
1283 implementation the same mode. */
1284 else if (TYPE_MAIN_VARIANT (t1) == dfloat64x_type_node
1285 || TYPE_MAIN_VARIANT (t2) == dfloat64x_type_node)
1286 return dfloat64x_type_node;
1287 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
1288 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
1289 return dfloat64_type_node;
1290 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
1291 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
1292 return dfloat32_type_node;
1293 }
1294
1295 /* Deal with fixed-point types. */
1296 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
1297 {
1298 unsigned int unsignedp = 0, satp = 0;
1299 scalar_mode m1, m2;
1300 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
1301
1302 m1 = SCALAR_TYPE_MODE (t1);
1303 m2 = SCALAR_TYPE_MODE (t2);
1304
1305 /* If one input type is saturating, the result type is saturating. */
1306 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
1307 satp = 1;
1308
1309 /* If both fixed-point types are unsigned, the result type is unsigned.
1310 When mixing fixed-point and integer types, follow the sign of the
1311 fixed-point type.
1312 Otherwise, the result type is signed. */
1313 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
1314 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
1315 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
1316 && TYPE_UNSIGNED (t1))
1317 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
1318 && TYPE_UNSIGNED (t2)))
1319 unsignedp = 1;
1320
1321 /* The result type is signed. */
1322 if (unsignedp == 0)
1323 {
1324 /* If the input type is unsigned, we need to convert to the
1325 signed type. */
1326 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
1327 {
1328 enum mode_class mclass = (enum mode_class) 0;
1329 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
1330 mclass = MODE_FRACT;
1331 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
1332 mclass = MODE_ACCUM;
1333 else
1334 gcc_unreachable ();
1335 m1 = as_a <scalar_mode>
1336 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
1337 }
1338 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
1339 {
1340 enum mode_class mclass = (enum mode_class) 0;
1341 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
1342 mclass = MODE_FRACT;
1343 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
1344 mclass = MODE_ACCUM;
1345 else
1346 gcc_unreachable ();
1347 m2 = as_a <scalar_mode>
1348 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
1349 }
1350 }
1351
1352 if (code1 == FIXED_POINT_TYPE)
1353 {
1354 fbit1 = GET_MODE_FBIT (m1);
1355 ibit1 = GET_MODE_IBIT (m1);
1356 }
1357 else
1358 {
1359 fbit1 = 0;
1360 /* Signed integers need to subtract one sign bit. */
1361 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
1362 }
1363
1364 if (code2 == FIXED_POINT_TYPE)
1365 {
1366 fbit2 = GET_MODE_FBIT (m2);
1367 ibit2 = GET_MODE_IBIT (m2);
1368 }
1369 else
1370 {
1371 fbit2 = 0;
1372 /* Signed integers need to subtract one sign bit. */
1373 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
1374 }
1375
1376 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
1377 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
1378 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
1379 satp);
1380 }
1381
1382 /* Both real or both integers; use the one with greater precision. */
1383
1384 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
1385 return t1;
1386 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
1387 return t2;
1388
1389 /* Same precision. Prefer long longs to longs to ints when the
1390 same precision, following the C99 rules on integer type rank
1391 (which are equivalent to the C90 rules for C90 types). */
1392
1393 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
1394 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
1395 return long_long_unsigned_type_node;
1396
1397 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
1398 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
1399 {
1400 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1401 return long_long_unsigned_type_node;
1402 else
1403 return long_long_integer_type_node;
1404 }
1405
1406 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
1407 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
1408 return long_unsigned_type_node;
1409
1410 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
1411 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
1412 {
1413 /* But preserve unsignedness from the other type,
1414 since long cannot hold all the values of an unsigned int. */
1415 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1416 return long_unsigned_type_node;
1417 else
1418 return long_integer_type_node;
1419 }
1420
1421 /* For floating types of the same TYPE_PRECISION (which we here
1422 assume means either the same set of values, or sets of values
1423 neither a subset of the other, with behavior being undefined in
1424 the latter case), follow the rules from TS 18661-3: prefer
1425 interchange types _FloatN, then standard types long double,
1426 double, float, then extended types _FloatNx. For extended types,
1427 check them starting with _Float128x as that seems most consistent
1428 in spirit with preferring long double to double; for interchange
1429 types, also check in that order for consistency although it's not
1430 possible for more than one of them to have the same
1431 precision. */
1432 tree mv1 = TYPE_MAIN_VARIANT (t1);
1433 tree mv2 = TYPE_MAIN_VARIANT (t2);
1434
1435 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
1436 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
1437 return FLOATN_TYPE_NODE (i);
1438
1439 /* Likewise, prefer long double to double even if same size. */
1440 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1441 return long_double_type_node;
1442
1443 /* Likewise, prefer double to float even if same size.
1444 We got a couple of embedded targets with 32 bit doubles, and the
1445 pdp11 might have 64 bit floats. */
1446 if (mv1 == double_type_node || mv2 == double_type_node)
1447 return double_type_node;
1448
1449 if (mv1 == float_type_node || mv2 == float_type_node)
1450 return float_type_node;
1451
1452 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1453 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1454 return FLOATNX_TYPE_NODE (i);
1455
1456 if ((code1 == BITINT_TYPE || code2 == BITINT_TYPE) && code1 != code2)
1457 {
1458 /* Prefer any other integral types over bit-precise integer types. */
1459 if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2))
1460 return code1 == BITINT_TYPE ? t2 : t1;
1461 /* If BITINT_TYPE is unsigned and the other type is signed
1462 non-BITINT_TYPE with the same precision, the latter has higher rank.
1463 In that case:
1464 Otherwise, both operands are converted to the unsigned integer type
1465 corresponding to the type of the operand with signed integer type. */
1466 if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2))
1467 return c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
1468 }
1469
1470 /* Otherwise prefer the unsigned one. */
1471
1472 if (TYPE_UNSIGNED (t1))
1473 return t1;
1474 else
1475 return t2;
1476 }
1477
1478 /* Wrapper around c_common_type that is used by c-common.cc and other
1479 front end optimizations that remove promotions. ENUMERAL_TYPEs
1480 are allowed here and are converted to their compatible integer types.
1481 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1482 preferably a non-Boolean type as the common type. */
1483 tree
1484 common_type (tree t1, tree t2)
1485 {
1486 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1487 t1 = ENUM_UNDERLYING_TYPE (t1);
1488 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1489 t2 = ENUM_UNDERLYING_TYPE (t2);
1490
1491 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1492 if (TREE_CODE (t1) == BOOLEAN_TYPE
1493 && TREE_CODE (t2) == BOOLEAN_TYPE)
1494 return boolean_type_node;
1495
1496 /* If either type is BOOLEAN_TYPE, then return the other. */
1497 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1498 return t2;
1499 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1500 return t1;
1501
1502 return c_common_type (t1, t2);
1503 }
1504
1505
1506
1507 /* Helper function for comptypes. For two compatible types, return 1
1508 if they pass consistency checks. In particular we test that
1509 TYPE_CANONICAL is set correctly, i.e. the two types can alias. */
1510
1511 static bool
1512 comptypes_verify (tree type1, tree type2)
1513 {
1514 if (type1 == type2 || !type1 || !type2
1515 || TREE_CODE (type1) == ERROR_MARK || TREE_CODE (type2) == ERROR_MARK)
1516 return true;
1517
1518 gcc_checking_assert (c_verify_type (type1));
1519 gcc_checking_assert (c_verify_type (type2));
1520
1521 if (TYPE_CANONICAL (type1) != TYPE_CANONICAL (type2)
1522 && !TYPE_STRUCTURAL_EQUALITY_P (type1)
1523 && !TYPE_STRUCTURAL_EQUALITY_P (type2))
1524 {
1525 /* FIXME: check other types. */
1526 if (RECORD_OR_UNION_TYPE_P (type1)
1527 || TREE_CODE (type1) == ENUMERAL_TYPE
1528 || TREE_CODE (type2) == ENUMERAL_TYPE)
1529 return false;
1530 }
1531 return true;
1532 }
1533
1534 struct comptypes_data {
1535 bool enum_and_int_p;
1536 bool different_types_p;
1537 bool warning_needed;
1538 bool anon_field;
1539 bool pointedto;
1540 bool equiv;
1541
1542 const struct tagged_tu_seen_cache* cache;
1543 };
1544
1545 /* C implementation of compatible_types_for_indirection_note_p. */
1546
1547 bool
1548 compatible_types_for_indirection_note_p (tree type1, tree type2)
1549 {
1550 return comptypes (type1, type2) == 1;
1551 }
1552
1553 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1554 or various other operations. Return 2 if they are compatible
1555 but a warning may be needed if you use them together. */
1556
1557 int
1558 comptypes (tree type1, tree type2)
1559 {
1560 struct comptypes_data data = { };
1561 bool ret = comptypes_internal (type1, type2, &data);
1562
1563 gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1564
1565 return ret ? (data.warning_needed ? 2 : 1) : 0;
1566 }
1567
1568
1569 /* Like comptypes, but it returns non-zero only for identical
1570 types. */
1571
1572 bool
1573 comptypes_same_p (tree type1, tree type2)
1574 {
1575 struct comptypes_data data = { };
1576 bool ret = comptypes_internal (type1, type2, &data);
1577
1578 gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1579
1580 if (data.different_types_p)
1581 return false;
1582
1583 return ret;
1584 }
1585
1586
1587 /* Like comptypes, but if it returns non-zero because enum and int are
1588 compatible, it sets *ENUM_AND_INT_P to true. */
1589
1590 int
1591 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1592 {
1593 struct comptypes_data data = { };
1594 bool ret = comptypes_internal (type1, type2, &data);
1595 *enum_and_int_p = data.enum_and_int_p;
1596
1597 gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1598
1599 return ret ? (data.warning_needed ? 2 : 1) : 0;
1600 }
1601
1602 /* Like comptypes, but if it returns nonzero for different types, it
1603 sets *DIFFERENT_TYPES_P to true. */
1604
1605 int
1606 comptypes_check_different_types (tree type1, tree type2,
1607 bool *different_types_p)
1608 {
1609 struct comptypes_data data = { };
1610 bool ret = comptypes_internal (type1, type2, &data);
1611 *different_types_p = data.different_types_p;
1612
1613 gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1614
1615 return ret ? (data.warning_needed ? 2 : 1) : 0;
1616 }
1617
1618
1619 /* Like comptypes, but if it returns true for struct and union types
1620 considered equivalent for aliasing purposes, i.e. for setting
1621 TYPE_CANONICAL after completing a struct or union.
1622
1623 This function must return false only for types which are not
1624 compatible according to C language semantics (cf. comptypes),
1625 otherwise the middle-end would make incorrect aliasing decisions.
1626 It may return true for some similar types that are not compatible
1627 according to those stricter rules.
1628
1629 In particular, we ignore size expression in arrays so that the
1630 following structs are in the same equivalence class:
1631
1632 struct foo { char (*buf)[]; };
1633 struct foo { char (*buf)[3]; };
1634 struct foo { char (*buf)[4]; };
1635
1636 We also treat unions / structs with members which are pointers to
1637 structures or unions with the same tag as equivalent (if they are not
1638 incompatible for other reasons). Although incomplete structure
1639 or union types are not compatible to any other type, they may become
1640 compatible to different types when completed. To avoid having to update
1641 TYPE_CANONICAL at this point, we only consider the tag when forming
1642 the equivalence classes. For example, the following types with tag
1643 'foo' are all considered equivalent:
1644
1645 struct bar;
1646 struct foo { struct bar *x };
1647 struct foo { struct bar { int a; } *x };
1648 struct foo { struct bar { char b; } *x }; */
1649
1650 bool
1651 comptypes_equiv_p (tree type1, tree type2)
1652 {
1653 struct comptypes_data data = { };
1654 data.equiv = true;
1655 bool ret = comptypes_internal (type1, type2, &data);
1656
1657 /* check that different equivance classes are assigned only
1658 to types that are not compatible. */
1659 gcc_checking_assert (ret || !comptypes (type1, type2));
1660
1661 return ret;
1662 }
1663
1664
1665 /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1666 or various other operations. If they are compatible but a warning may
1667 be needed if you use them together, 'warning_needed' in DATA is set.
1668 If one type is an enum and the other a compatible integer type, then
1669 this sets 'enum_and_int_p' in DATA to true (it is never set to
1670 false). If the types are compatible but different enough not to be
1671 permitted in C11 typedef redeclarations, then this sets
1672 'different_types_p' in DATA to true; it is never set to
1673 false, but may or may not be set if the types are incompatible.
1674 This differs from comptypes, in that we don't free the seen
1675 types. */
1676
1677 static bool
1678 comptypes_internal (const_tree type1, const_tree type2,
1679 struct comptypes_data *data)
1680 {
1681 const_tree t1 = type1;
1682 const_tree t2 = type2;
1683
1684 /* Suppress errors caused by previously reported errors. */
1685
1686 if (t1 == t2 || !t1 || !t2
1687 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1688 return true;
1689
1690 /* Enumerated types are compatible with integer types, but this is
1691 not transitive: two enumerated types in the same translation unit
1692 are compatible with each other only if they are the same type. */
1693
1694 if (TREE_CODE (t1) == ENUMERAL_TYPE
1695 && COMPLETE_TYPE_P (t1)
1696 && TREE_CODE (t2) != ENUMERAL_TYPE)
1697 {
1698 t1 = ENUM_UNDERLYING_TYPE (t1);
1699 if (TREE_CODE (t2) != VOID_TYPE)
1700 {
1701 data->enum_and_int_p = true;
1702 data->different_types_p = true;
1703 }
1704 }
1705 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1706 && COMPLETE_TYPE_P (t2)
1707 && TREE_CODE (t1) != ENUMERAL_TYPE)
1708 {
1709 t2 = ENUM_UNDERLYING_TYPE (t2);
1710 if (TREE_CODE (t1) != VOID_TYPE)
1711 {
1712 data->enum_and_int_p = true;
1713 data->different_types_p = true;
1714 }
1715 }
1716
1717 if (t1 == t2)
1718 return true;
1719
1720 /* Different classes of types can't be compatible. */
1721
1722 if (TREE_CODE (t1) != TREE_CODE (t2))
1723 return false;
1724
1725 /* Qualifiers must match. C99 6.7.3p9 */
1726
1727 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1728 return false;
1729
1730 /* Allow for two different type nodes which have essentially the same
1731 definition. Note that we already checked for equality of the type
1732 qualifiers (just above). */
1733
1734 if (TREE_CODE (t1) != ARRAY_TYPE
1735 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1736 return true;
1737
1738 int attrval;
1739
1740 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1741 if (!(attrval = comp_type_attributes (t1, t2)))
1742 return false;
1743
1744 if (2 == attrval)
1745 data->warning_needed = true;
1746
1747 switch (TREE_CODE (t1))
1748 {
1749 case INTEGER_TYPE:
1750 case FIXED_POINT_TYPE:
1751 case REAL_TYPE:
1752 case BITINT_TYPE:
1753 /* With these nodes, we can't determine type equivalence by
1754 looking at what is stored in the nodes themselves, because
1755 two nodes might have different TYPE_MAIN_VARIANTs but still
1756 represent the same type. For example, wchar_t and int could
1757 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1758 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1759 and are distinct types. On the other hand, int and the
1760 following typedef
1761
1762 typedef int INT __attribute((may_alias));
1763
1764 have identical properties, different TYPE_MAIN_VARIANTs, but
1765 represent the same type. The canonical type system keeps
1766 track of equivalence in this case, so we fall back on it. */
1767 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1768
1769 case POINTER_TYPE:
1770 /* Do not remove mode information. */
1771 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1772 return false;
1773 data->pointedto = true;
1774 return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1775
1776 case FUNCTION_TYPE:
1777 return function_types_compatible_p (t1, t2, data);
1778
1779 case ARRAY_TYPE:
1780 {
1781 tree d1 = TYPE_DOMAIN (t1);
1782 tree d2 = TYPE_DOMAIN (t2);
1783 bool d1_variable, d2_variable;
1784 bool d1_zero, d2_zero;
1785
1786 /* Target types must match incl. qualifiers. */
1787 if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data))
1788 return false;
1789
1790 if ((d1 == NULL_TREE) != (d2 == NULL_TREE))
1791 data->different_types_p = true;
1792 /* Ignore size mismatches when forming equivalence classes. */
1793 if (data->equiv)
1794 return true;
1795 /* Sizes must match unless one is missing or variable. */
1796 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1797 return true;
1798
1799 d1_zero = !TYPE_MAX_VALUE (d1);
1800 d2_zero = !TYPE_MAX_VALUE (d2);
1801
1802 d1_variable = (!d1_zero
1803 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1804 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1805 d2_variable = (!d2_zero
1806 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1807 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1808
1809 if (d1_variable != d2_variable)
1810 data->different_types_p = true;
1811 if (d1_variable || d2_variable)
1812 return true;
1813 if (d1_zero && d2_zero)
1814 return true;
1815 if (d1_zero || d2_zero
1816 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1817 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1818 return false;
1819
1820 return true;
1821 }
1822
1823 case ENUMERAL_TYPE:
1824 case RECORD_TYPE:
1825 case UNION_TYPE:
1826
1827 if (!flag_isoc23)
1828 return false;
1829
1830 return tagged_types_tu_compatible_p (t1, t2, data);
1831
1832 case VECTOR_TYPE:
1833 return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1834 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1835
1836 default:
1837 return false;
1838 }
1839 gcc_unreachable ();
1840 }
1841
1842 /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring
1843 their qualifiers, except for named address spaces. If the pointers point to
1844 different named addresses, then we must determine if one address space is a
1845 subset of the other. */
1846
1847 static bool
1848 comp_target_types (location_t location, tree ttl, tree ttr)
1849 {
1850 int val;
1851 int val_ped;
1852 tree mvl = TREE_TYPE (ttl);
1853 tree mvr = TREE_TYPE (ttr);
1854 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1855 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1856 addr_space_t as_common;
1857 bool enum_and_int_p;
1858
1859 /* Fail if pointers point to incompatible address spaces. */
1860 if (!addr_space_superset (asl, asr, &as_common))
1861 return 0;
1862
1863 /* For pedantic record result of comptypes on arrays before losing
1864 qualifiers on the element type below. */
1865 val_ped = 1;
1866
1867 if (TREE_CODE (mvl) == ARRAY_TYPE
1868 && TREE_CODE (mvr) == ARRAY_TYPE)
1869 val_ped = comptypes (mvl, mvr);
1870
1871 /* Qualifiers on element types of array types that are
1872 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1873
1874 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1875 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1876 : TYPE_MAIN_VARIANT (mvl));
1877
1878 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1879 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1880 : TYPE_MAIN_VARIANT (mvr));
1881
1882 enum_and_int_p = false;
1883 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1884
1885 if (val == 1 && val_ped != 1)
1886 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1887 "in ISO C before C23");
1888
1889 if (val == 2)
1890 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1891
1892 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1893 warning_at (location, OPT_Wc___compat,
1894 "pointer target types incompatible in C++");
1895
1896 return val;
1897 }
1898
1899 /* Subroutines of `comptypes'. */
1900
1901 /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are
1902 compatible. The two types are not the same (which has been
1903 checked earlier in comptypes_internal). */
1904
1905 static bool
1906 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1907 struct comptypes_data *data)
1908 {
1909 tree s1, s2;
1910
1911 if (c_type_tag (t1) != c_type_tag (t2))
1912 return false;
1913
1914 /* When forming equivalence classes for TYPE_CANONICAL in C23, we treat
1915 structs with the same tag as equivalent, but only when they are targets
1916 of pointers inside other structs. */
1917 if (data->equiv && data->pointedto && NULL_TREE != c_type_tag (t1))
1918 return true;
1919
1920 /* Different types without tag are incompatible except as an anonymous
1921 field or when forming equivalence classes for TYPE_CANONICAL. */
1922 if (!data->anon_field && !data->equiv && NULL_TREE == c_type_tag (t1))
1923 return false;
1924
1925 if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2))
1926 data->different_types_p = true;
1927
1928 /* Incomplete types are incompatible inside a TU. */
1929 if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL)
1930 return false;
1931
1932 if (ENUMERAL_TYPE != TREE_CODE (t1)
1933 && (TYPE_REVERSE_STORAGE_ORDER (t1)
1934 != TYPE_REVERSE_STORAGE_ORDER (t2)))
1935 return false;
1936
1937 if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
1938 data->different_types_p = true;
1939
1940 /* For types already being looked at in some active
1941 invocation of this function, assume compatibility.
1942 The cache is built as a linked list on the stack
1943 with the head of the list passed downwards. */
1944 for (const struct tagged_tu_seen_cache *t = data->cache;
1945 t != NULL; t = t->next)
1946 if (t->t1 == t1 && t->t2 == t2)
1947 return true;
1948
1949 const struct tagged_tu_seen_cache entry = { data->cache, t1, t2 };
1950
1951 switch (TREE_CODE (t1))
1952 {
1953 case ENUMERAL_TYPE:
1954 {
1955 if (!comptypes (ENUM_UNDERLYING_TYPE (t1), ENUM_UNDERLYING_TYPE (t2)))
1956 return false;
1957
1958 /* Speed up the case where the type values are in the same order. */
1959 tree tv1 = TYPE_VALUES (t1);
1960 tree tv2 = TYPE_VALUES (t2);
1961
1962 if (tv1 == tv2)
1963 return true;
1964
1965 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1966 {
1967 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1968 break;
1969
1970 if (simple_cst_equal (DECL_INITIAL (TREE_VALUE (tv1)),
1971 DECL_INITIAL (TREE_VALUE (tv2))) != 1)
1972 break;
1973 }
1974
1975 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1976 return true;
1977
1978 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1979 return false;
1980
1981 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1982 return false;
1983
1984 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1985 {
1986 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1987
1988 if (s2 == NULL
1989 || simple_cst_equal (DECL_INITIAL (TREE_VALUE (s1)),
1990 DECL_INITIAL (TREE_VALUE (s2))) != 1)
1991 return false;
1992 }
1993
1994 return true;
1995 }
1996
1997 case UNION_TYPE:
1998 case RECORD_TYPE:
1999
2000 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
2001 return false;
2002
2003 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
2004 s1 && s2;
2005 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
2006 {
2007 gcc_assert (TREE_CODE (s1) == FIELD_DECL);
2008 gcc_assert (TREE_CODE (s2) == FIELD_DECL);
2009
2010 tree ft1 = TREE_TYPE (s1);
2011 tree ft2 = TREE_TYPE (s2);
2012
2013 if (DECL_NAME (s1) != DECL_NAME (s2))
2014 return false;
2015
2016 if (DECL_ALIGN (s1) != DECL_ALIGN (s2))
2017 return false;
2018
2019 if (DECL_C_BIT_FIELD (s1) != DECL_C_BIT_FIELD (s2))
2020 return false;
2021
2022 if (DECL_C_BIT_FIELD (s1))
2023 {
2024 if (!tree_int_cst_equal (DECL_SIZE (s1), DECL_SIZE (s2)))
2025 return false;
2026
2027 ft1 = DECL_BIT_FIELD_TYPE (s1);
2028 ft2 = DECL_BIT_FIELD_TYPE (s2);
2029 }
2030
2031 if (TREE_CODE (ft1) == ERROR_MARK || TREE_CODE (ft2) == ERROR_MARK)
2032 return false;
2033
2034 data->anon_field = !DECL_NAME (s1);
2035 data->pointedto = false;
2036
2037 const struct tagged_tu_seen_cache *cache = data->cache;
2038 data->cache = &entry;
2039 bool ret = comptypes_internal (ft1, ft2, data);
2040 data->cache = cache;
2041 if (!ret)
2042 return false;
2043
2044 tree st1 = TYPE_SIZE (TREE_TYPE (s1));
2045 tree st2 = TYPE_SIZE (TREE_TYPE (s2));
2046
2047 if (data->equiv
2048 && st1 && TREE_CODE (st1) == INTEGER_CST
2049 && st2 && TREE_CODE (st2) == INTEGER_CST
2050 && !tree_int_cst_equal (st1, st2))
2051 return false;
2052
2053 tree counted_by1 = lookup_attribute ("counted_by",
2054 DECL_ATTRIBUTES (s1));
2055 tree counted_by2 = lookup_attribute ("counted_by",
2056 DECL_ATTRIBUTES (s2));
2057 /* If there is no counted_by attribute for both fields. */
2058 if (!counted_by1 && !counted_by2)
2059 continue;
2060
2061 /* If only one field has counted_by attribute. */
2062 if ((counted_by1 && !counted_by2)
2063 || (!counted_by1 && counted_by2))
2064 return false;
2065
2066 /* Now both s1 and s2 have counted_by attributes, check
2067 whether they are the same. */
2068
2069 tree counted_by_field1
2070 = lookup_field (t1, TREE_VALUE (TREE_VALUE (counted_by1)));
2071 tree counted_by_field2
2072 = lookup_field (t2, TREE_VALUE (TREE_VALUE (counted_by2)));
2073
2074 gcc_assert (counted_by_field1 && counted_by_field2);
2075
2076 while (TREE_CHAIN (counted_by_field1))
2077 counted_by_field1 = TREE_CHAIN (counted_by_field1);
2078 while (TREE_CHAIN (counted_by_field2))
2079 counted_by_field2 = TREE_CHAIN (counted_by_field2);
2080
2081 if (DECL_NAME (TREE_VALUE (counted_by_field1))
2082 != DECL_NAME (TREE_VALUE (counted_by_field2)))
2083 return false;
2084 }
2085 return true;
2086
2087 default:
2088 gcc_unreachable ();
2089 }
2090 }
2091
2092 /* Return true if two function types F1 and F2 are compatible.
2093 If either type specifies no argument types,
2094 the other must specify a fixed number of self-promoting arg types.
2095 Otherwise, if one type specifies only the number of arguments,
2096 the other must specify that number of self-promoting arg types.
2097 Otherwise, the argument types must match. */
2098
2099 static bool
2100 function_types_compatible_p (const_tree f1, const_tree f2,
2101 struct comptypes_data *data)
2102 {
2103 tree args1, args2;
2104 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
2105 int val = 1;
2106 int val1;
2107 tree ret1, ret2;
2108
2109 ret1 = TREE_TYPE (f1);
2110 ret2 = TREE_TYPE (f2);
2111
2112 /* 'volatile' qualifiers on a function's return type used to mean
2113 the function is noreturn. */
2114 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
2115 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
2116 if (TYPE_VOLATILE (ret1))
2117 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
2118 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
2119 if (TYPE_VOLATILE (ret2))
2120 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
2121 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
2122 val = comptypes_internal (ret1, ret2, data);
2123 if (val == 0)
2124 return 0;
2125
2126 args1 = TYPE_ARG_TYPES (f1);
2127 args2 = TYPE_ARG_TYPES (f2);
2128
2129 if ((args1 == NULL_TREE) != (args2 == NULL_TREE))
2130 data->different_types_p = true;
2131
2132 /* An unspecified parmlist matches any specified parmlist
2133 whose argument types don't need default promotions. */
2134
2135 if (args1 == NULL_TREE)
2136 {
2137 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2138 return 0;
2139 if (!self_promoting_args_p (args2))
2140 return 0;
2141 /* If one of these types comes from a non-prototype fn definition,
2142 compare that with the other type's arglist.
2143 If they don't match, ask for a warning (but no error). */
2144 if (TYPE_ACTUAL_ARG_TYPES (f1)
2145 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
2146 data) != 1)
2147 {
2148 val = 1;
2149 data->warning_needed = true;
2150 }
2151 return val;
2152 }
2153 if (args2 == NULL_TREE)
2154 {
2155 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2156 return 0;
2157 if (!self_promoting_args_p (args1))
2158 return 0;
2159 if (TYPE_ACTUAL_ARG_TYPES (f2)
2160 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
2161 data) != 1)
2162 {
2163 val = 1;
2164 data->warning_needed = true;
2165 }
2166 return val;
2167 }
2168
2169 /* Both types have argument lists: compare them and propagate results. */
2170 val1 = type_lists_compatible_p (args1, args2, data);
2171 return val1;
2172 }
2173
2174 /* Check two lists of types for compatibility, returning false for
2175 incompatible, true for compatible. */
2176
2177 static bool
2178 type_lists_compatible_p (const_tree args1, const_tree args2,
2179 struct comptypes_data *data)
2180 {
2181 while (1)
2182 {
2183 tree a1, mv1, a2, mv2;
2184 if (args1 == NULL_TREE && args2 == NULL_TREE)
2185 return true;
2186 /* If one list is shorter than the other,
2187 they fail to match. */
2188 if (args1 == NULL_TREE || args2 == NULL_TREE)
2189 return 0;
2190 mv1 = a1 = TREE_VALUE (args1);
2191 mv2 = a2 = TREE_VALUE (args2);
2192 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
2193 mv1 = (TYPE_ATOMIC (mv1)
2194 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
2195 TYPE_QUAL_ATOMIC)
2196 : TYPE_MAIN_VARIANT (mv1));
2197 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
2198 mv2 = (TYPE_ATOMIC (mv2)
2199 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
2200 TYPE_QUAL_ATOMIC)
2201 : TYPE_MAIN_VARIANT (mv2));
2202 /* A null pointer instead of a type
2203 means there is supposed to be an argument
2204 but nothing is specified about what type it has.
2205 So match anything that self-promotes. */
2206 if ((a1 == NULL_TREE) != (a2 == NULL_TREE))
2207 data->different_types_p = true;
2208 if (a1 == NULL_TREE)
2209 {
2210 if (c_type_promotes_to (a2) != a2)
2211 return 0;
2212 }
2213 else if (a2 == NULL_TREE)
2214 {
2215 if (c_type_promotes_to (a1) != a1)
2216 return 0;
2217 }
2218 /* If one of the lists has an error marker, ignore this arg. */
2219 else if (TREE_CODE (a1) == ERROR_MARK
2220 || TREE_CODE (a2) == ERROR_MARK)
2221 ;
2222 else if (!comptypes_internal (mv1, mv2, data))
2223 {
2224 data->different_types_p = true;
2225 /* Allow wait (union {union wait *u; int *i} *)
2226 and wait (union wait *) to be compatible. */
2227 if (TREE_CODE (a1) == UNION_TYPE
2228 && (TYPE_NAME (a1) == NULL_TREE
2229 || TYPE_TRANSPARENT_AGGR (a1))
2230 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
2231 && tree_int_cst_equal (TYPE_SIZE (a1),
2232 TYPE_SIZE (a2)))
2233 {
2234 tree memb;
2235 for (memb = TYPE_FIELDS (a1);
2236 memb; memb = DECL_CHAIN (memb))
2237 {
2238 tree mv3 = TREE_TYPE (memb);
2239 if (mv3 && mv3 != error_mark_node
2240 && TREE_CODE (mv3) != ARRAY_TYPE)
2241 mv3 = (TYPE_ATOMIC (mv3)
2242 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
2243 TYPE_QUAL_ATOMIC)
2244 : TYPE_MAIN_VARIANT (mv3));
2245 if (comptypes_internal (mv3, mv2, data))
2246 break;
2247 }
2248 if (memb == NULL_TREE)
2249 return 0;
2250 }
2251 else if (TREE_CODE (a2) == UNION_TYPE
2252 && (TYPE_NAME (a2) == NULL_TREE
2253 || TYPE_TRANSPARENT_AGGR (a2))
2254 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
2255 && tree_int_cst_equal (TYPE_SIZE (a2),
2256 TYPE_SIZE (a1)))
2257 {
2258 tree memb;
2259 for (memb = TYPE_FIELDS (a2);
2260 memb; memb = DECL_CHAIN (memb))
2261 {
2262 tree mv3 = TREE_TYPE (memb);
2263 if (mv3 && mv3 != error_mark_node
2264 && TREE_CODE (mv3) != ARRAY_TYPE)
2265 mv3 = (TYPE_ATOMIC (mv3)
2266 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
2267 TYPE_QUAL_ATOMIC)
2268 : TYPE_MAIN_VARIANT (mv3));
2269 if (comptypes_internal (mv3, mv1, data))
2270 break;
2271 }
2272 if (memb == NULL_TREE)
2273 return 0;
2274 }
2275 else
2276 return 0;
2277 }
2278
2279 args1 = TREE_CHAIN (args1);
2280 args2 = TREE_CHAIN (args2);
2281 }
2282 }
2283
2284 /* Compute the size to increment a pointer by. When a function type or void
2285 type or incomplete type is passed, size_one_node is returned.
2286 This function does not emit any diagnostics; the caller is responsible
2287 for that. */
2288
2289 static tree
2290 c_size_in_bytes (const_tree type)
2291 {
2292 enum tree_code code = TREE_CODE (type);
2293
2294 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
2295 || !COMPLETE_TYPE_P (type))
2296 return size_one_node;
2297
2298 /* Convert in case a char is more than one unit. */
2299 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
2300 size_int (TYPE_PRECISION (char_type_node)
2301 / BITS_PER_UNIT));
2302 }
2303
2304 /* Return either DECL or its known constant value (if it has one). */
2305
2306 tree
2307 decl_constant_value_1 (tree decl, bool in_init)
2308 {
2309 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
2310 TREE_CODE (decl) != PARM_DECL
2311 && !TREE_THIS_VOLATILE (decl)
2312 && TREE_READONLY (decl)
2313 && DECL_INITIAL (decl) != NULL_TREE
2314 && !error_operand_p (DECL_INITIAL (decl))
2315 /* This is invalid if initial value is not constant.
2316 If it has either a function call, a memory reference,
2317 or a variable, then re-evaluating it could give different results. */
2318 && TREE_CONSTANT (DECL_INITIAL (decl))
2319 /* Check for cases where this is sub-optimal, even though valid. */
2320 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
2321 return DECL_INITIAL (decl);
2322 return decl;
2323 }
2324
2325 /* Return either DECL or its known constant value (if it has one).
2326 Like the above, but always return decl outside of functions. */
2327
2328 tree
2329 decl_constant_value (tree decl)
2330 {
2331 /* Don't change a variable array bound or initial value to a constant
2332 in a place where a variable is invalid. */
2333 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
2334 }
2335
2336 /* Convert the array expression EXP to a pointer. */
2337 static tree
2338 array_to_pointer_conversion (location_t loc, tree exp)
2339 {
2340 tree orig_exp = exp;
2341 tree type = TREE_TYPE (exp);
2342 tree adr;
2343 tree restype = TREE_TYPE (type);
2344 tree ptrtype;
2345
2346 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
2347
2348 STRIP_TYPE_NOPS (exp);
2349
2350 copy_warning (exp, orig_exp);
2351
2352 ptrtype = c_build_pointer_type (restype);
2353
2354 if (INDIRECT_REF_P (exp))
2355 return convert (ptrtype, TREE_OPERAND (exp, 0));
2356
2357 /* In C++ array compound literals are temporary objects unless they are
2358 const or appear in namespace scope, so they are destroyed too soon
2359 to use them for much of anything (c++/53220). */
2360 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
2361 {
2362 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2363 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
2364 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
2365 "converting an array compound literal to a pointer "
2366 "leads to a dangling pointer in C++");
2367 }
2368
2369 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
2370 return convert (ptrtype, adr);
2371 }
2372
2373 /* Convert the function expression EXP to a pointer. */
2374 static tree
2375 function_to_pointer_conversion (location_t loc, tree exp)
2376 {
2377 tree orig_exp = exp;
2378
2379 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
2380
2381 STRIP_TYPE_NOPS (exp);
2382
2383 copy_warning (exp, orig_exp);
2384
2385 return build_unary_op (loc, ADDR_EXPR, exp, false);
2386 }
2387
2388 /* Mark EXP as read, not just set, for set but not used -Wunused
2389 warning purposes. */
2390
2391 void
2392 mark_exp_read (tree exp)
2393 {
2394 switch (TREE_CODE (exp))
2395 {
2396 case VAR_DECL:
2397 case PARM_DECL:
2398 DECL_READ_P (exp) = 1;
2399 break;
2400 case ARRAY_REF:
2401 case COMPONENT_REF:
2402 case MODIFY_EXPR:
2403 case REALPART_EXPR:
2404 case IMAGPART_EXPR:
2405 CASE_CONVERT:
2406 case ADDR_EXPR:
2407 case VIEW_CONVERT_EXPR:
2408 mark_exp_read (TREE_OPERAND (exp, 0));
2409 break;
2410 case COMPOUND_EXPR:
2411 /* Pattern match what build_atomic_assign produces with modifycode
2412 NOP_EXPR. */
2413 if (VAR_P (TREE_OPERAND (exp, 1))
2414 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
2415 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
2416 {
2417 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2418 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
2419 if (TREE_CODE (t1) == TARGET_EXPR
2420 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
2421 && TREE_CODE (t2) == CALL_EXPR)
2422 {
2423 tree fndecl = get_callee_fndecl (t2);
2424 tree arg = NULL_TREE;
2425 if (fndecl
2426 && TREE_CODE (fndecl) == FUNCTION_DECL
2427 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2428 && call_expr_nargs (t2) >= 2)
2429 switch (DECL_FUNCTION_CODE (fndecl))
2430 {
2431 case BUILT_IN_ATOMIC_STORE:
2432 arg = CALL_EXPR_ARG (t2, 1);
2433 break;
2434 case BUILT_IN_ATOMIC_STORE_1:
2435 case BUILT_IN_ATOMIC_STORE_2:
2436 case BUILT_IN_ATOMIC_STORE_4:
2437 case BUILT_IN_ATOMIC_STORE_8:
2438 case BUILT_IN_ATOMIC_STORE_16:
2439 arg = CALL_EXPR_ARG (t2, 0);
2440 break;
2441 default:
2442 break;
2443 }
2444 if (arg)
2445 {
2446 STRIP_NOPS (arg);
2447 if (TREE_CODE (arg) == ADDR_EXPR
2448 && DECL_P (TREE_OPERAND (arg, 0))
2449 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2450 mark_exp_read (TREE_OPERAND (arg, 0));
2451 }
2452 }
2453 }
2454 /* FALLTHRU */
2455 case C_MAYBE_CONST_EXPR:
2456 mark_exp_read (TREE_OPERAND (exp, 1));
2457 break;
2458 case OMP_ARRAY_SECTION:
2459 mark_exp_read (TREE_OPERAND (exp, 0));
2460 if (TREE_OPERAND (exp, 1))
2461 mark_exp_read (TREE_OPERAND (exp, 1));
2462 if (TREE_OPERAND (exp, 2))
2463 mark_exp_read (TREE_OPERAND (exp, 2));
2464 break;
2465 default:
2466 break;
2467 }
2468 }
2469
2470 /* Perform the default conversion of arrays and functions to pointers.
2471 Return the result of converting EXP. For any other expression, just
2472 return EXP.
2473
2474 LOC is the location of the expression. */
2475
2476 struct c_expr
2477 default_function_array_conversion (location_t loc, struct c_expr exp)
2478 {
2479 tree orig_exp = exp.value;
2480 tree type = TREE_TYPE (exp.value);
2481 enum tree_code code = TREE_CODE (type);
2482
2483 switch (code)
2484 {
2485 case ARRAY_TYPE:
2486 {
2487 bool not_lvalue = false;
2488 bool lvalue_array_p;
2489
2490 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2491 || CONVERT_EXPR_P (exp.value))
2492 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2493 {
2494 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2495 not_lvalue = true;
2496 exp.value = TREE_OPERAND (exp.value, 0);
2497 }
2498
2499 copy_warning (exp.value, orig_exp);
2500
2501 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2502 if (!flag_isoc99 && !lvalue_array_p)
2503 {
2504 /* Before C99, non-lvalue arrays do not decay to pointers.
2505 Normally, using such an array would be invalid; but it can
2506 be used correctly inside sizeof or as a statement expression.
2507 Thus, do not give an error here; an error will result later. */
2508 return exp;
2509 }
2510
2511 exp.value = array_to_pointer_conversion (loc, exp.value);
2512 }
2513 break;
2514 case FUNCTION_TYPE:
2515 exp.value = function_to_pointer_conversion (loc, exp.value);
2516 break;
2517 default:
2518 break;
2519 }
2520
2521 return exp;
2522 }
2523
2524 struct c_expr
2525 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2526 {
2527 mark_exp_read (exp.value);
2528 return default_function_array_conversion (loc, exp);
2529 }
2530
2531 /* Return whether EXPR should be treated as an atomic lvalue for the
2532 purposes of load and store handling. */
2533
2534 static bool
2535 really_atomic_lvalue (tree expr)
2536 {
2537 if (error_operand_p (expr))
2538 return false;
2539 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2540 return false;
2541 if (!lvalue_p (expr))
2542 return false;
2543
2544 /* Ignore _Atomic on register variables, since their addresses can't
2545 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2546 sequences wouldn't work. Ignore _Atomic on structures containing
2547 bit-fields, since accessing elements of atomic structures or
2548 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2549 it's undefined at translation time or execution time, and the
2550 normal atomic sequences again wouldn't work. */
2551 while (handled_component_p (expr))
2552 {
2553 if (TREE_CODE (expr) == COMPONENT_REF
2554 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2555 return false;
2556 expr = TREE_OPERAND (expr, 0);
2557 }
2558 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2559 return false;
2560 return true;
2561 }
2562
2563 /* If EXPR is a named constant (C23) derived from a constexpr variable
2564 - that is, a reference to such a variable, or a member extracted by
2565 a sequence of structure and union (but not array) member accesses
2566 (where union member accesses must access the same member as
2567 initialized) - then return the corresponding initializer;
2568 otherwise, return NULL_TREE. */
2569
2570 static tree
2571 maybe_get_constexpr_init (tree expr)
2572 {
2573 tree decl = NULL_TREE;
2574 if (TREE_CODE (expr) == VAR_DECL)
2575 decl = expr;
2576 else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2577 decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2578 if (decl
2579 && C_DECL_DECLARED_CONSTEXPR (decl)
2580 && DECL_INITIAL (decl) != NULL_TREE
2581 && !error_operand_p (DECL_INITIAL (decl)))
2582 return DECL_INITIAL (decl);
2583 if (TREE_CODE (expr) != COMPONENT_REF)
2584 return NULL_TREE;
2585 tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2586 if (inner == NULL_TREE)
2587 return NULL_TREE;
2588 while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2589 && !error_operand_p (inner)
2590 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2591 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2592 inner = TREE_OPERAND (inner, 0);
2593 if (TREE_CODE (inner) != CONSTRUCTOR)
2594 return NULL_TREE;
2595 tree field = TREE_OPERAND (expr, 1);
2596 unsigned HOST_WIDE_INT cidx;
2597 tree cfield, cvalue;
2598 bool have_other_init = false;
2599 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2600 {
2601 if (cfield == field)
2602 return cvalue;
2603 have_other_init = true;
2604 }
2605 if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2606 && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2607 return NULL_TREE;
2608 /* Return a default initializer. */
2609 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2610 return build_constructor (TREE_TYPE (expr), NULL);
2611 return build_zero_cst (TREE_TYPE (expr));
2612 }
2613
2614 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2615 including converting functions and arrays to pointers if CONVERT_P.
2616 If READ_P, also mark the expression as having been read. If
2617 FOR_INIT, constexpr expressions of structure and union type should
2618 be replaced by the corresponding CONSTRUCTOR; otherwise, only
2619 constexpr scalars (including elements of structures and unions) are
2620 replaced by their initializers. */
2621
2622 struct c_expr
2623 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2624 bool convert_p, bool read_p, bool for_init)
2625 {
2626 bool force_non_npc = false;
2627 if (read_p)
2628 mark_exp_read (exp.value);
2629 if (convert_p)
2630 exp = default_function_array_conversion (loc, exp);
2631 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2632 exp.value = require_complete_type (loc, exp.value);
2633 if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2634 {
2635 tree init = maybe_get_constexpr_init (exp.value);
2636 if (init != NULL_TREE)
2637 {
2638 /* A named constant of pointer type or type nullptr_t is not
2639 a null pointer constant even if the initializer is
2640 one. */
2641 if (TREE_CODE (init) == INTEGER_CST
2642 && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2643 && integer_zerop (init))
2644 force_non_npc = true;
2645 exp.value = init;
2646 }
2647 }
2648 if (really_atomic_lvalue (exp.value))
2649 {
2650 vec<tree, va_gc> *params;
2651 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2652 tree expr_type = TREE_TYPE (exp.value);
2653 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2654 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2655
2656 gcc_assert (TYPE_ATOMIC (expr_type));
2657
2658 /* Expansion of a generic atomic load may require an addition
2659 element, so allocate enough to prevent a resize. */
2660 vec_alloc (params, 4);
2661
2662 /* Remove the qualifiers for the rest of the expressions and
2663 create the VAL temp variable to hold the RHS. */
2664 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2665 tmp = create_tmp_var_raw (nonatomic_type);
2666 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2667 TREE_ADDRESSABLE (tmp) = 1;
2668 /* Do not disable warnings for TMP even though it's artificial.
2669 -Winvalid-memory-model depends on it. */
2670
2671 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2672 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2673 params->quick_push (expr_addr);
2674 params->quick_push (tmp_addr);
2675 params->quick_push (seq_cst);
2676 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2677
2678 /* EXPR is always read. */
2679 mark_exp_read (exp.value);
2680
2681 /* Return tmp which contains the value loaded. */
2682 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2683 NULL_TREE, NULL_TREE);
2684 }
2685 if (convert_p && !error_operand_p (exp.value)
2686 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2687 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2688 if (force_non_npc)
2689 exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2690
2691 {
2692 tree false_value, true_value;
2693 if (convert_p && !error_operand_p (exp.value)
2694 && c_hardbool_type_attr (TREE_TYPE (exp.value),
2695 &false_value, &true_value))
2696 {
2697 tree t = save_expr (exp.value);
2698
2699 mark_exp_read (exp.value);
2700
2701 tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
2702 tree expr = build_call_expr_loc (loc, trapfn, 0);
2703 expr = build_compound_expr (loc, expr, boolean_true_node);
2704 expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2705 fold_build2_loc (loc, NE_EXPR,
2706 boolean_type_node,
2707 t, true_value),
2708 expr, boolean_true_node);
2709 expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2710 fold_build2_loc (loc, NE_EXPR,
2711 boolean_type_node,
2712 t, false_value),
2713 expr, boolean_false_node);
2714
2715 exp.value = expr;
2716 }
2717 }
2718
2719 return exp;
2720 }
2721
2722 /* EXP is an expression of integer type. Apply the integer promotions
2723 to it and return the promoted value. */
2724
2725 tree
2726 perform_integral_promotions (tree exp)
2727 {
2728 tree type = TREE_TYPE (exp);
2729 enum tree_code code = TREE_CODE (type);
2730
2731 gcc_assert (INTEGRAL_TYPE_P (type));
2732
2733 /* Convert enums to the result of applying the integer promotions to
2734 their underlying type. */
2735 if (code == ENUMERAL_TYPE)
2736 {
2737 type = ENUM_UNDERLYING_TYPE (type);
2738 if (c_promoting_integer_type_p (type))
2739 {
2740 if (TYPE_UNSIGNED (type)
2741 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2742 type = unsigned_type_node;
2743 else
2744 type = integer_type_node;
2745 }
2746
2747 return convert (type, exp);
2748 }
2749
2750 /* ??? This should no longer be needed now bit-fields have their
2751 proper types. */
2752 if (TREE_CODE (exp) == COMPONENT_REF
2753 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
2754 {
2755 if (TREE_CODE (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)))
2756 == BITINT_TYPE)
2757 return convert (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)), exp);
2758 /* If it's thinner than an int, promote it like a
2759 c_promoting_integer_type_p, otherwise leave it alone. */
2760 if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2761 TYPE_PRECISION (integer_type_node)) < 0)
2762 return convert (integer_type_node, exp);
2763 }
2764
2765 if (c_promoting_integer_type_p (type))
2766 {
2767 /* Preserve unsignedness if not really getting any wider. */
2768 if (TYPE_UNSIGNED (type)
2769 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2770 return convert (unsigned_type_node, exp);
2771
2772 return convert (integer_type_node, exp);
2773 }
2774
2775 return exp;
2776 }
2777
2778
2779 /* Perform default promotions for C data used in expressions.
2780 Enumeral types or short or char are converted to int.
2781 In addition, manifest constants symbols are replaced by their values. */
2782
2783 tree
2784 default_conversion (tree exp)
2785 {
2786 tree orig_exp;
2787 tree type = TREE_TYPE (exp);
2788 enum tree_code code = TREE_CODE (type);
2789 tree promoted_type;
2790
2791 mark_exp_read (exp);
2792
2793 /* Functions and arrays have been converted during parsing. */
2794 gcc_assert (code != FUNCTION_TYPE);
2795 if (code == ARRAY_TYPE)
2796 return exp;
2797
2798 /* Constants can be used directly unless they're not loadable. */
2799 if (TREE_CODE (exp) == CONST_DECL)
2800 exp = DECL_INITIAL (exp);
2801
2802 /* Strip no-op conversions. */
2803 orig_exp = exp;
2804 STRIP_TYPE_NOPS (exp);
2805
2806 copy_warning (exp, orig_exp);
2807
2808 if (code == VOID_TYPE)
2809 {
2810 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2811 "void value not ignored as it ought to be");
2812 return error_mark_node;
2813 }
2814
2815 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2816 if (exp == error_mark_node)
2817 return error_mark_node;
2818
2819 promoted_type = targetm.promoted_type (type);
2820 if (promoted_type)
2821 return convert (promoted_type, exp);
2822
2823 if (INTEGRAL_TYPE_P (type))
2824 return perform_integral_promotions (exp);
2825
2826 return exp;
2827 }
2828
2829 /* Look up COMPONENT in a structure or union TYPE.
2830
2831 If the component name is not found, returns NULL_TREE. Otherwise,
2832 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2833 stepping down the chain to the component, which is in the last
2834 TREE_VALUE of the list. Normally the list is of length one, but if
2835 the component is embedded within (nested) anonymous structures or
2836 unions, the list steps down the chain to the component. */
2837
2838 tree
2839 lookup_field (const_tree type, tree component)
2840 {
2841 tree field;
2842
2843 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2844 to the field elements. Use a binary search on this array to quickly
2845 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2846 will always be set for structures which have many elements.
2847
2848 Duplicate field checking replaces duplicates with NULL_TREE so
2849 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2850 case just iterate using DECL_CHAIN. */
2851
2852 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2853 && !seen_error ())
2854 {
2855 int bot, top, half;
2856 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2857
2858 field = TYPE_FIELDS (type);
2859 bot = 0;
2860 top = TYPE_LANG_SPECIFIC (type)->s->len;
2861 while (top - bot > 1)
2862 {
2863 half = (top - bot + 1) >> 1;
2864 field = field_array[bot+half];
2865
2866 if (DECL_NAME (field) == NULL_TREE)
2867 {
2868 /* Step through all anon unions in linear fashion. */
2869 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2870 {
2871 field = field_array[bot++];
2872 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2873 {
2874 tree anon = lookup_field (TREE_TYPE (field), component);
2875
2876 if (anon)
2877 return tree_cons (NULL_TREE, field, anon);
2878
2879 /* The Plan 9 compiler permits referring
2880 directly to an anonymous struct/union field
2881 using a typedef name. */
2882 if (flag_plan9_extensions
2883 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2884 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2885 == TYPE_DECL)
2886 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2887 == component))
2888 break;
2889 }
2890 }
2891
2892 /* Entire record is only anon unions. */
2893 if (bot > top)
2894 return NULL_TREE;
2895
2896 /* Restart the binary search, with new lower bound. */
2897 continue;
2898 }
2899
2900 if (DECL_NAME (field) == component)
2901 break;
2902 if (DECL_NAME (field) < component)
2903 bot += half;
2904 else
2905 top = bot + half;
2906 }
2907
2908 if (DECL_NAME (field_array[bot]) == component)
2909 field = field_array[bot];
2910 else if (DECL_NAME (field) != component)
2911 return NULL_TREE;
2912 }
2913 else
2914 {
2915 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2916 {
2917 if (DECL_NAME (field) == NULL_TREE
2918 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2919 {
2920 tree anon = lookup_field (TREE_TYPE (field), component);
2921
2922 if (anon)
2923 return tree_cons (NULL_TREE, field, anon);
2924
2925 /* The Plan 9 compiler permits referring directly to an
2926 anonymous struct/union field using a typedef
2927 name. */
2928 if (flag_plan9_extensions
2929 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2930 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2931 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2932 == component))
2933 break;
2934 }
2935
2936 if (DECL_NAME (field) == component)
2937 break;
2938 }
2939
2940 if (field == NULL_TREE)
2941 return NULL_TREE;
2942 }
2943
2944 return tree_cons (NULL_TREE, field, NULL_TREE);
2945 }
2946
2947 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2948
2949 static void
2950 lookup_field_fuzzy_find_candidates (tree type, tree component,
2951 vec<tree> *candidates)
2952 {
2953 tree field;
2954 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2955 {
2956 if (DECL_NAME (field) == NULL_TREE
2957 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2958 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2959 candidates);
2960
2961 if (DECL_NAME (field))
2962 candidates->safe_push (DECL_NAME (field));
2963 }
2964 }
2965
2966 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2967 rather than returning a TREE_LIST for an exact match. */
2968
2969 static tree
2970 lookup_field_fuzzy (tree type, tree component)
2971 {
2972 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2973
2974 /* First, gather a list of candidates. */
2975 auto_vec <tree> candidates;
2976
2977 lookup_field_fuzzy_find_candidates (type, component,
2978 &candidates);
2979
2980 return find_closest_identifier (component, &candidates);
2981 }
2982
2983 /* Support function for build_component_ref's error-handling.
2984
2985 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2986 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2987
2988 static bool
2989 should_suggest_deref_p (tree datum_type)
2990 {
2991 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2992 allows "." for ptrs; we could be handling a failed attempt
2993 to access a property. */
2994 if (c_dialect_objc ())
2995 return false;
2996
2997 /* Only suggest it for pointers... */
2998 if (TREE_CODE (datum_type) != POINTER_TYPE)
2999 return false;
3000
3001 /* ...to structs/unions. */
3002 tree underlying_type = TREE_TYPE (datum_type);
3003 enum tree_code code = TREE_CODE (underlying_type);
3004 if (code == RECORD_TYPE || code == UNION_TYPE)
3005 return true;
3006 else
3007 return false;
3008 }
3009
3010 /* For a SUBDATUM field of a structure or union DATUM, generate a REF to
3011 the object that represents its counted_by per the attribute counted_by
3012 attached to this field if it's a flexible array member field, otherwise
3013 return NULL_TREE.
3014 Set COUNTED_BY_TYPE to the TYPE of the counted_by field.
3015 For example, if:
3016
3017 struct P {
3018 int k;
3019 int x[] __attribute__ ((counted_by (k)));
3020 } *p;
3021
3022 for:
3023 p->x
3024
3025 the ref to the object that represents its element count will be:
3026
3027 &(p->k)
3028
3029 */
3030 static tree
3031 build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type)
3032 {
3033 tree type = TREE_TYPE (datum);
3034 if (!c_flexible_array_member_type_p (TREE_TYPE (subdatum)))
3035 return NULL_TREE;
3036
3037 tree attr_counted_by = lookup_attribute ("counted_by",
3038 DECL_ATTRIBUTES (subdatum));
3039 tree counted_by_ref = NULL_TREE;
3040 *counted_by_type = NULL_TREE;
3041 if (attr_counted_by)
3042 {
3043 tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by));
3044 counted_by_ref
3045 = build_component_ref (UNKNOWN_LOCATION,
3046 datum, field_id,
3047 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
3048 counted_by_ref = build_fold_addr_expr (counted_by_ref);
3049
3050 /* Get the TYPE of the counted_by field. */
3051 tree counted_by_field = lookup_field (type, field_id);
3052 gcc_assert (counted_by_field);
3053
3054 do
3055 {
3056 *counted_by_type = TREE_TYPE (TREE_VALUE (counted_by_field));
3057 counted_by_field = TREE_CHAIN (counted_by_field);
3058 }
3059 while (counted_by_field);
3060 }
3061 return counted_by_ref;
3062 }
3063
3064 /* Given a COMPONENT_REF REF with the location LOC, the corresponding
3065 COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate an INDIRECT_REF
3066 to a call to the internal function .ACCESS_WITH_SIZE.
3067
3068 REF
3069
3070 to:
3071
3072 (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1,
3073 (TYPE_OF_ARRAY *)0))
3074
3075 NOTE: The return type of this function is the POINTER type pointing
3076 to the original flexible array type.
3077 Then the type of the INDIRECT_REF is the original flexible array type.
3078
3079 The type of the first argument of this function is a POINTER type
3080 to the original flexible array type.
3081
3082 The 4th argument of the call is a constant 0 with the TYPE of the
3083 object pointed by COUNTED_BY_REF.
3084
3085 The 6th argument of the call is a constant 0 with the pointer TYPE
3086 to the original flexible array type.
3087
3088 */
3089 static tree
3090 build_access_with_size_for_counted_by (location_t loc, tree ref,
3091 tree counted_by_ref,
3092 tree counted_by_type)
3093 {
3094 gcc_assert (c_flexible_array_member_type_p (TREE_TYPE (ref)));
3095 /* The result type of the call is a pointer to the flexible array type. */
3096 tree result_type = c_build_pointer_type (TREE_TYPE (ref));
3097 tree first_param
3098 = c_fully_fold (array_to_pointer_conversion (loc, ref), false, NULL);
3099 tree second_param
3100 = c_fully_fold (counted_by_ref, false, NULL);
3101
3102 tree call
3103 = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE,
3104 result_type, 6,
3105 first_param,
3106 second_param,
3107 build_int_cst (integer_type_node, 1),
3108 build_int_cst (counted_by_type, 0),
3109 build_int_cst (integer_type_node, -1),
3110 build_int_cst (result_type, 0));
3111 /* Wrap the call with an INDIRECT_REF with the flexible array type. */
3112 call = build1 (INDIRECT_REF, TREE_TYPE (ref), call);
3113 SET_EXPR_LOCATION (call, loc);
3114 return call;
3115 }
3116
3117 /* For the COMPONENT_REF ref, check whether it has a counted_by attribute,
3118 if so, wrap this COMPONENT_REF with the corresponding CALL to the
3119 function .ACCESS_WITH_SIZE.
3120 Otherwise, return the ref itself. */
3121
3122 tree
3123 handle_counted_by_for_component_ref (location_t loc, tree ref)
3124 {
3125 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3126 tree datum = TREE_OPERAND (ref, 0);
3127 tree subdatum = TREE_OPERAND (ref, 1);
3128 tree counted_by_type = NULL_TREE;
3129 tree counted_by_ref = build_counted_by_ref (datum, subdatum,
3130 &counted_by_type);
3131 if (counted_by_ref)
3132 ref = build_access_with_size_for_counted_by (loc, ref,
3133 counted_by_ref,
3134 counted_by_type);
3135 return ref;
3136 }
3137
3138 /* Make an expression to refer to the COMPONENT field of structure or
3139 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
3140 location of the COMPONENT_REF. COMPONENT_LOC is the location
3141 of COMPONENT. ARROW_LOC is the location of the first -> operand if
3142 it is from -> operator.
3143 If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate
3144 a call to .ACCESS_WITH_SIZE. Otherwise, ignore the attribute. */
3145
3146 tree
3147 build_component_ref (location_t loc, tree datum, tree component,
3148 location_t component_loc, location_t arrow_loc,
3149 bool handle_counted_by)
3150 {
3151 tree type = TREE_TYPE (datum);
3152 enum tree_code code = TREE_CODE (type);
3153 tree field = NULL;
3154 tree ref;
3155 bool datum_lvalue = lvalue_p (datum);
3156
3157 if (!objc_is_public (datum, component))
3158 return error_mark_node;
3159
3160 /* Detect Objective-C property syntax object.property. */
3161 if (c_dialect_objc ()
3162 && (ref = objc_maybe_build_component_ref (datum, component)))
3163 return ref;
3164
3165 /* See if there is a field or component with name COMPONENT. */
3166
3167 if (code == RECORD_TYPE || code == UNION_TYPE)
3168 {
3169 if (!COMPLETE_TYPE_P (type))
3170 {
3171 c_incomplete_type_error (loc, NULL_TREE, type);
3172 return error_mark_node;
3173 }
3174
3175 field = lookup_field (type, component);
3176
3177 if (!field)
3178 {
3179 tree guessed_id = lookup_field_fuzzy (type, component);
3180 if (guessed_id)
3181 {
3182 /* Attempt to provide a fixit replacement hint, if
3183 we have a valid range for the component. */
3184 location_t reported_loc
3185 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
3186 gcc_rich_location rich_loc (reported_loc);
3187 if (component_loc != UNKNOWN_LOCATION)
3188 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
3189 error_at (&rich_loc,
3190 "%qT has no member named %qE; did you mean %qE?",
3191 type, component, guessed_id);
3192 }
3193 else
3194 error_at (loc, "%qT has no member named %qE", type, component);
3195 return error_mark_node;
3196 }
3197
3198 /* Accessing elements of atomic structures or unions is undefined
3199 behavior (C11 6.5.2.3#5). */
3200 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
3201 {
3202 if (code == RECORD_TYPE)
3203 warning_at (loc, 0, "accessing a member %qE of an atomic "
3204 "structure %qE", component, datum);
3205 else
3206 warning_at (loc, 0, "accessing a member %qE of an atomic "
3207 "union %qE", component, datum);
3208 }
3209
3210 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
3211 This might be better solved in future the way the C++ front
3212 end does it - by giving the anonymous entities each a
3213 separate name and type, and then have build_component_ref
3214 recursively call itself. We can't do that here. */
3215 do
3216 {
3217 tree subdatum = TREE_VALUE (field);
3218 int quals;
3219 tree subtype;
3220 bool use_datum_quals;
3221 /* Do not handle counted_by when in typeof and alignof operator. */
3222 handle_counted_by = handle_counted_by && !in_typeof && !in_alignof;
3223
3224 if (TREE_TYPE (subdatum) == error_mark_node)
3225 return error_mark_node;
3226
3227 /* If this is an rvalue, it does not have qualifiers in C
3228 standard terms and we must avoid propagating such
3229 qualifiers down to a non-lvalue array that is then
3230 converted to a pointer. */
3231 use_datum_quals = (datum_lvalue
3232 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
3233
3234 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
3235 if (use_datum_quals)
3236 quals |= TYPE_QUALS (TREE_TYPE (datum));
3237 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
3238
3239 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
3240 NULL_TREE);
3241 SET_EXPR_LOCATION (ref, loc);
3242
3243 if (handle_counted_by)
3244 ref = handle_counted_by_for_component_ref (loc, ref);
3245
3246 if (TREE_READONLY (subdatum)
3247 || (use_datum_quals && TREE_READONLY (datum)))
3248 TREE_READONLY (ref) = 1;
3249 if (TREE_THIS_VOLATILE (subdatum)
3250 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
3251 TREE_THIS_VOLATILE (ref) = 1;
3252
3253 if (TREE_UNAVAILABLE (subdatum))
3254 error_unavailable_use (subdatum, NULL_TREE);
3255 else if (TREE_DEPRECATED (subdatum))
3256 warn_deprecated_use (subdatum, NULL_TREE);
3257
3258 datum = ref;
3259
3260 field = TREE_CHAIN (field);
3261 }
3262 while (field);
3263
3264 return ref;
3265 }
3266 else if (should_suggest_deref_p (type))
3267 {
3268 /* Special-case the error message for "ptr.field" for the case
3269 where the user has confused "." vs "->". */
3270 rich_location richloc (line_table, loc);
3271 if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
3272 {
3273 richloc.add_fixit_insert_before (arrow_loc, "(*");
3274 richloc.add_fixit_insert_after (arrow_loc, ")");
3275 error_at (&richloc,
3276 "%qE is a pointer to pointer; did you mean to dereference "
3277 "it before applying %<->%> to it?",
3278 TREE_OPERAND (datum, 0));
3279 }
3280 else
3281 {
3282 /* "loc" should be the "." token. */
3283 richloc.add_fixit_replace ("->");
3284 error_at (&richloc,
3285 "%qE is a pointer; did you mean to use %<->%>?",
3286 datum);
3287 }
3288 return error_mark_node;
3289 }
3290 else if (code != ERROR_MARK)
3291 error_at (loc,
3292 "request for member %qE in something not a structure or union",
3293 component);
3294
3295 return error_mark_node;
3296 }
3297
3298 /* Given an expression PTR for a pointer, return an expression
3299 for the value pointed to.
3300 ERRORSTRING is the name of the operator to appear in error messages.
3301
3302 LOC is the location to use for the generated tree. */
3303
3304 tree
3305 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
3306 {
3307 tree pointer = default_conversion (ptr);
3308 tree type = TREE_TYPE (pointer);
3309 tree ref;
3310
3311 if (TREE_CODE (type) == POINTER_TYPE)
3312 {
3313 if (CONVERT_EXPR_P (pointer)
3314 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
3315 {
3316 /* If a warning is issued, mark it to avoid duplicates from
3317 the backend. This only needs to be done at
3318 warn_strict_aliasing > 2. */
3319 if (warn_strict_aliasing > 2)
3320 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
3321 type, TREE_OPERAND (pointer, 0)))
3322 suppress_warning (pointer, OPT_Wstrict_aliasing_);
3323 }
3324
3325 if (TREE_CODE (pointer) == ADDR_EXPR
3326 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
3327 == TREE_TYPE (type)))
3328 {
3329 ref = TREE_OPERAND (pointer, 0);
3330 protected_set_expr_location (ref, loc);
3331 return ref;
3332 }
3333 else
3334 {
3335 tree t = TREE_TYPE (type);
3336
3337 ref = build1 (INDIRECT_REF, t, pointer);
3338
3339 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
3340 warning_at (loc, 0, "dereferencing %<void *%> pointer");
3341
3342 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3343 so that we get the proper error message if the result is used
3344 to assign to. Also, &* is supposed to be a no-op.
3345 And ANSI C seems to specify that the type of the result
3346 should be the const type. */
3347 /* A de-reference of a pointer to const is not a const. It is valid
3348 to change it via some other pointer. */
3349 TREE_READONLY (ref) = TYPE_READONLY (t);
3350 TREE_SIDE_EFFECTS (ref)
3351 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
3352 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
3353 protected_set_expr_location (ref, loc);
3354 return ref;
3355 }
3356 }
3357 else if (TREE_CODE (pointer) != ERROR_MARK)
3358 invalid_indirection_error (loc, type, errstring);
3359
3360 return error_mark_node;
3361 }
3362
3363 /* This handles expressions of the form "a[i]", which denotes
3364 an array reference.
3365
3366 This is logically equivalent in C to *(a+i), but we may do it differently.
3367 If A is a variable or a member, we generate a primitive ARRAY_REF.
3368 This avoids forcing the array out of registers, and can work on
3369 arrays that are not lvalues (for example, members of structures returned
3370 by functions).
3371
3372 For vector types, allow vector[i] but not i[vector], and create
3373 *(((type*)&vectortype) + i) for the expression.
3374
3375 LOC is the location to use for the returned expression. */
3376
3377 tree
3378 build_array_ref (location_t loc, tree array, tree index)
3379 {
3380 tree ret;
3381 bool swapped = false;
3382 if (TREE_TYPE (array) == error_mark_node
3383 || TREE_TYPE (index) == error_mark_node)
3384 return error_mark_node;
3385
3386 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
3387 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
3388 /* Allow vector[index] but not index[vector]. */
3389 && !gnu_vector_type_p (TREE_TYPE (array)))
3390 {
3391 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
3392 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
3393 {
3394 error_at (loc,
3395 "subscripted value is neither array nor pointer nor vector");
3396
3397 return error_mark_node;
3398 }
3399 std::swap (array, index);
3400 swapped = true;
3401 }
3402
3403 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
3404 {
3405 error_at (loc, "array subscript is not an integer");
3406 return error_mark_node;
3407 }
3408
3409 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
3410 {
3411 error_at (loc, "subscripted value is pointer to function");
3412 return error_mark_node;
3413 }
3414
3415 /* ??? Existing practice has been to warn only when the char
3416 index is syntactically the index, not for char[array]. */
3417 if (!swapped)
3418 warn_array_subscript_with_type_char (loc, index);
3419
3420 /* Apply default promotions *after* noticing character types. */
3421 index = default_conversion (index);
3422 if (index == error_mark_node)
3423 return error_mark_node;
3424
3425 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
3426 || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE);
3427
3428 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
3429 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
3430
3431 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3432 {
3433 tree rval, type;
3434
3435 /* An array that is indexed by a non-constant
3436 cannot be stored in a register; we must be able to do
3437 address arithmetic on its address.
3438 Likewise an array of elements of variable size. */
3439 if (TREE_CODE (index) != INTEGER_CST
3440 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3441 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
3442 {
3443 if (!c_mark_addressable (array, true))
3444 return error_mark_node;
3445 }
3446 /* An array that is indexed by a constant value which is not within
3447 the array bounds cannot be stored in a register either; because we
3448 would get a crash in store_bit_field/extract_bit_field when trying
3449 to access a non-existent part of the register. */
3450 if (TREE_CODE (index) == INTEGER_CST
3451 && TYPE_DOMAIN (TREE_TYPE (array))
3452 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
3453 {
3454 if (!c_mark_addressable (array))
3455 return error_mark_node;
3456 }
3457
3458 if ((pedantic || warn_c90_c99_compat)
3459 && ! was_vector)
3460 {
3461 tree foo = array;
3462 while (TREE_CODE (foo) == COMPONENT_REF)
3463 foo = TREE_OPERAND (foo, 0);
3464 if (VAR_P (foo) && C_DECL_REGISTER (foo))
3465 pedwarn (loc, OPT_Wpedantic,
3466 "ISO C forbids subscripting %<register%> array");
3467 else if (!lvalue_p (foo))
3468 pedwarn_c90 (loc, OPT_Wpedantic,
3469 "ISO C90 forbids subscripting non-lvalue "
3470 "array");
3471 }
3472
3473 if (TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE
3474 && TYPE_PRECISION (TREE_TYPE (index)) > TYPE_PRECISION (sizetype))
3475 index = fold_convert (sizetype, index);
3476
3477 type = TREE_TYPE (TREE_TYPE (array));
3478 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
3479 /* Array ref is const/volatile if the array elements are
3480 or if the array is. */
3481 TREE_READONLY (rval)
3482 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
3483 | TREE_READONLY (array));
3484 TREE_SIDE_EFFECTS (rval)
3485 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3486 | TREE_SIDE_EFFECTS (array));
3487 TREE_THIS_VOLATILE (rval)
3488 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3489 /* This was added by rms on 16 Nov 91.
3490 It fixes vol struct foo *a; a->elts[1]
3491 in an inline function.
3492 Hope it doesn't break something else. */
3493 | TREE_THIS_VOLATILE (array));
3494 ret = require_complete_type (loc, rval);
3495 protected_set_expr_location (ret, loc);
3496 if (non_lvalue)
3497 ret = non_lvalue_loc (loc, ret);
3498 return ret;
3499 }
3500 else
3501 {
3502 tree ar = default_conversion (array);
3503
3504 if (ar == error_mark_node)
3505 return ar;
3506
3507 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
3508 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
3509
3510 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
3511 index, false),
3512 RO_ARRAY_INDEXING);
3513 if (non_lvalue)
3514 ret = non_lvalue_loc (loc, ret);
3515 return ret;
3516 }
3517 }
3518
3519 /* Build an OpenMP array section reference, creating an exact type for the
3520 resulting expression based on the element type and bounds if possible. If
3521 we have variable bounds, create an incomplete array type for the result
3522 instead. */
3523
3524 tree
3525 build_omp_array_section (location_t loc, tree array, tree index, tree length)
3526 {
3527 tree type = TREE_TYPE (array);
3528 gcc_assert (type);
3529
3530 tree sectype, eltype = TREE_TYPE (type);
3531
3532 /* It's not an array or pointer type. Just reuse the type of the original
3533 expression as the type of the array section (an error will be raised
3534 anyway, later). */
3535 if (eltype == NULL_TREE || error_operand_p (eltype))
3536 sectype = TREE_TYPE (array);
3537 else
3538 {
3539 tree idxtype = NULL_TREE;
3540
3541 if (index != NULL_TREE
3542 && length != NULL_TREE
3543 && INTEGRAL_TYPE_P (TREE_TYPE (index))
3544 && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3545 {
3546 tree low = fold_convert (sizetype, index);
3547 tree high = fold_convert (sizetype, length);
3548 high = size_binop (PLUS_EXPR, low, high);
3549 high = size_binop (MINUS_EXPR, high, size_one_node);
3550 idxtype = build_range_type (sizetype, low, high);
3551 }
3552 else if ((index == NULL_TREE || integer_zerop (index))
3553 && length != NULL_TREE
3554 && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3555 idxtype = build_index_type (length);
3556
3557 gcc_assert (!error_operand_p (idxtype));
3558
3559 sectype = c_build_array_type (eltype, idxtype);
3560 }
3561
3562 return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array, index, length);
3563 }
3564
3565
3566 /* Build an external reference to identifier ID. FUN indicates
3567 whether this will be used for a function call. LOC is the source
3568 location of the identifier. This sets *TYPE to the type of the
3569 identifier, which is not the same as the type of the returned value
3570 for CONST_DECLs defined as enum constants. If the type of the
3571 identifier is not available, *TYPE is set to NULL. */
3572 tree
3573 build_external_ref (location_t loc, tree id, bool fun, tree *type)
3574 {
3575 tree ref;
3576 tree decl = lookup_name (id);
3577
3578 /* In Objective-C, an instance variable (ivar) may be preferred to
3579 whatever lookup_name() found. */
3580 decl = objc_lookup_ivar (decl, id);
3581
3582 *type = NULL;
3583 if (decl && decl != error_mark_node)
3584 {
3585 ref = decl;
3586 *type = TREE_TYPE (ref);
3587 if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
3588 error_at (loc, "underspecified %qD referenced in its initializer",
3589 decl);
3590 }
3591 else if (fun)
3592 /* Implicit function declaration. */
3593 ref = implicitly_declare (loc, id);
3594 else if (decl == error_mark_node)
3595 /* Don't complain about something that's already been
3596 complained about. */
3597 return error_mark_node;
3598 else
3599 {
3600 undeclared_variable (loc, id);
3601 return error_mark_node;
3602 }
3603
3604 /* For an OpenMP map clause, we can get better diagnostics for decls with
3605 unmappable types if we return the decl with an error_mark_node type,
3606 rather than returning error_mark_node for the decl itself. */
3607 if (TREE_TYPE (ref) == error_mark_node
3608 && !c_omp_array_section_p)
3609 return error_mark_node;
3610
3611 if (TREE_UNAVAILABLE (ref))
3612 error_unavailable_use (ref, NULL_TREE);
3613 else if (TREE_DEPRECATED (ref))
3614 warn_deprecated_use (ref, NULL_TREE);
3615
3616 /* Recursive call does not count as usage. */
3617 if (ref != current_function_decl)
3618 {
3619 TREE_USED (ref) = 1;
3620 }
3621
3622 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
3623 {
3624 if (!in_sizeof && !in_typeof)
3625 C_DECL_USED (ref) = 1;
3626 else if (DECL_INITIAL (ref) == NULL_TREE
3627 && DECL_EXTERNAL (ref)
3628 && !TREE_PUBLIC (ref))
3629 record_maybe_used_decl (ref);
3630 }
3631
3632 if (TREE_CODE (ref) == CONST_DECL)
3633 {
3634 used_types_insert (TREE_TYPE (ref));
3635
3636 if (warn_cxx_compat
3637 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
3638 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
3639 {
3640 warning_at (loc, OPT_Wc___compat,
3641 ("enum constant defined in struct or union "
3642 "is not visible in C++"));
3643 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
3644 }
3645
3646 ref = DECL_INITIAL (ref);
3647 TREE_CONSTANT (ref) = 1;
3648 }
3649 else if (current_function_decl != NULL_TREE
3650 && !DECL_FILE_SCOPE_P (current_function_decl)
3651 && (VAR_OR_FUNCTION_DECL_P (ref)
3652 || TREE_CODE (ref) == PARM_DECL))
3653 {
3654 tree context = decl_function_context (ref);
3655
3656 if (context != NULL_TREE && context != current_function_decl)
3657 DECL_NONLOCAL (ref) = 1;
3658 }
3659 /* C99 6.7.4p3: An inline definition of a function with external
3660 linkage ... shall not contain a reference to an identifier with
3661 internal linkage. */
3662 else if (current_function_decl != NULL_TREE
3663 && DECL_DECLARED_INLINE_P (current_function_decl)
3664 && DECL_EXTERNAL (current_function_decl)
3665 && VAR_OR_FUNCTION_DECL_P (ref)
3666 && (!VAR_P (ref) || TREE_STATIC (ref))
3667 && ! TREE_PUBLIC (ref)
3668 && DECL_CONTEXT (ref) != current_function_decl)
3669 record_inline_static (loc, current_function_decl, ref,
3670 csi_internal);
3671
3672 return ref;
3673 }
3674
3675 /* Record details of decls possibly used inside sizeof or typeof. */
3676 struct maybe_used_decl
3677 {
3678 /* The decl. */
3679 tree decl;
3680 /* The level seen at (in_sizeof + in_typeof). */
3681 int level;
3682 /* The next one at this level or above, or NULL. */
3683 struct maybe_used_decl *next;
3684 };
3685
3686 static struct maybe_used_decl *maybe_used_decls;
3687
3688 /* Record that DECL, an undefined static function reference seen
3689 inside sizeof or typeof, might be used if the operand of sizeof is
3690 a VLA type or the operand of typeof is a variably modified
3691 type. */
3692
3693 static void
3694 record_maybe_used_decl (tree decl)
3695 {
3696 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3697 t->decl = decl;
3698 t->level = in_sizeof + in_typeof;
3699 t->next = maybe_used_decls;
3700 maybe_used_decls = t;
3701 }
3702
3703 /* Pop the stack of decls possibly used inside sizeof or typeof. If
3704 USED is false, just discard them. If it is true, mark them used
3705 (if no longer inside sizeof or typeof) or move them to the next
3706 level up (if still inside sizeof or typeof). */
3707
3708 void
3709 pop_maybe_used (bool used)
3710 {
3711 struct maybe_used_decl *p = maybe_used_decls;
3712 int cur_level = in_sizeof + in_typeof;
3713 while (p && p->level > cur_level)
3714 {
3715 if (used)
3716 {
3717 if (cur_level == 0)
3718 C_DECL_USED (p->decl) = 1;
3719 else
3720 p->level = cur_level;
3721 }
3722 p = p->next;
3723 }
3724 if (!used || cur_level == 0)
3725 maybe_used_decls = p;
3726 }
3727
3728 /* Return the result of sizeof applied to EXPR. */
3729
3730 struct c_expr
3731 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
3732 {
3733 struct c_expr ret;
3734 if (expr.value == error_mark_node)
3735 {
3736 ret.value = error_mark_node;
3737 ret.original_code = ERROR_MARK;
3738 ret.original_type = NULL;
3739 ret.m_decimal = 0;
3740 pop_maybe_used (false);
3741 }
3742 else
3743 {
3744 bool expr_const_operands = true;
3745
3746 if (TREE_CODE (expr.value) == PARM_DECL
3747 && C_ARRAY_PARAMETER (expr.value))
3748 {
3749 auto_diagnostic_group d;
3750 if (warning_at (loc, OPT_Wsizeof_array_argument,
3751 "%<sizeof%> on array function parameter %qE will "
3752 "return size of %qT", expr.value,
3753 TREE_TYPE (expr.value)))
3754 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3755 }
3756 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3757 &expr_const_operands);
3758 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3759 c_last_sizeof_arg = expr.value;
3760 c_last_sizeof_loc = loc;
3761 ret.original_code = SIZEOF_EXPR;
3762 ret.original_type = NULL;
3763 ret.m_decimal = 0;
3764 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3765 {
3766 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3767 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3768 folded_expr, ret.value);
3769 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3770 SET_EXPR_LOCATION (ret.value, loc);
3771 }
3772 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3773 }
3774 return ret;
3775 }
3776
3777 /* Return the result of sizeof applied to T, a structure for the type
3778 name passed to sizeof (rather than the type itself). LOC is the
3779 location of the original expression. */
3780
3781 struct c_expr
3782 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3783 {
3784 tree type;
3785 struct c_expr ret;
3786 tree type_expr = NULL_TREE;
3787 bool type_expr_const = true;
3788 type = groktypename (t, &type_expr, &type_expr_const);
3789 ret.value = c_sizeof (loc, type);
3790 c_last_sizeof_arg = type;
3791 c_last_sizeof_loc = loc;
3792 ret.original_code = SIZEOF_EXPR;
3793 ret.original_type = NULL;
3794 ret.m_decimal = 0;
3795 if (type == error_mark_node)
3796 {
3797 ret.value = error_mark_node;
3798 ret.original_code = ERROR_MARK;
3799 }
3800 else
3801 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3802 && C_TYPE_VARIABLE_SIZE (type))
3803 {
3804 /* If the type is a [*] array, it is a VLA but is represented as
3805 having a size of zero. In such a case we must ensure that
3806 the result of sizeof does not get folded to a constant by
3807 c_fully_fold, because if the size is evaluated the result is
3808 not constant and so constraints on zero or negative size
3809 arrays must not be applied when this sizeof call is inside
3810 another array declarator. */
3811 if (!type_expr)
3812 type_expr = integer_zero_node;
3813 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3814 type_expr, ret.value);
3815 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3816 }
3817 pop_maybe_used (type != error_mark_node
3818 ? C_TYPE_VARIABLE_SIZE (type) : false);
3819 return ret;
3820 }
3821
3822 /* Build a function call to function FUNCTION with parameters PARAMS.
3823 The function call is at LOC.
3824 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3825 TREE_VALUE of each node is a parameter-expression.
3826 FUNCTION's data type may be a function type or a pointer-to-function. */
3827
3828 tree
3829 build_function_call (location_t loc, tree function, tree params)
3830 {
3831 vec<tree, va_gc> *v;
3832 tree ret;
3833
3834 vec_alloc (v, list_length (params));
3835 for (; params; params = TREE_CHAIN (params))
3836 v->quick_push (TREE_VALUE (params));
3837 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3838 vec_free (v);
3839 return ret;
3840 }
3841
3842 /* Give a note about the location of the declaration of DECL,
3843 or, failing that, a pertinent declaration for FUNCTION_EXPR. */
3844
3845 static void
3846 inform_declaration (tree decl, tree function_expr)
3847 {
3848 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3849 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3850 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3851 else if (function_expr)
3852 switch (TREE_CODE (function_expr))
3853 {
3854 default:
3855 break;
3856 case COMPONENT_REF:
3857 /* Show the decl of the pertinent field (e.g. for callback
3858 fields in a struct. */
3859 {
3860 tree field_decl = TREE_OPERAND (function_expr, 1);
3861 if (location_t loc = DECL_SOURCE_LOCATION (field_decl))
3862 inform (loc, "declared here");
3863 }
3864 break;
3865 }
3866 }
3867
3868 /* C implementation of callback for use when checking param types. */
3869
3870 static bool
3871 comp_parm_types (tree wanted_type, tree actual_type)
3872 {
3873 return comptypes (wanted_type, actual_type);
3874 }
3875
3876 /* Build a function call to function FUNCTION with parameters PARAMS.
3877 If FUNCTION is the result of resolving an overloaded target built-in,
3878 ORIG_FUNDECL is the original function decl, otherwise it is null.
3879 ORIGTYPES, if not NULL, is a vector of types; each element is
3880 either NULL or the original type of the corresponding element in
3881 PARAMS. The original type may differ from TREE_TYPE of the
3882 parameter for enums. FUNCTION's data type may be a function type
3883 or pointer-to-function. This function changes the elements of
3884 PARAMS. */
3885
3886 tree
3887 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3888 tree function, vec<tree, va_gc> *params,
3889 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3890 {
3891 tree fntype, fundecl = NULL_TREE;
3892 tree name = NULL_TREE, result;
3893 tree tem;
3894 int nargs;
3895 tree *argarray;
3896
3897
3898 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3899 STRIP_TYPE_NOPS (function);
3900
3901 /* Convert anything with function type to a pointer-to-function. */
3902 if (TREE_CODE (function) == FUNCTION_DECL)
3903 {
3904 name = DECL_NAME (function);
3905
3906 if (flag_tm)
3907 tm_malloc_replacement (function);
3908 fundecl = function;
3909 if (!orig_fundecl)
3910 orig_fundecl = fundecl;
3911 /* Atomic functions have type checking/casting already done. They are
3912 often rewritten and don't match the original parameter list. */
3913 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3914 origtypes = NULL;
3915 }
3916 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3917 function = function_to_pointer_conversion (loc, function);
3918
3919 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3920 expressions, like those used for ObjC messenger dispatches. */
3921 if (params && !params->is_empty ())
3922 function = objc_rewrite_function_call (function, (*params)[0]);
3923
3924 function = c_fully_fold (function, false, NULL);
3925
3926 fntype = TREE_TYPE (function);
3927
3928 if (TREE_CODE (fntype) == ERROR_MARK)
3929 return error_mark_node;
3930
3931 if (!(TREE_CODE (fntype) == POINTER_TYPE
3932 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3933 {
3934 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3935 error_at (loc,
3936 "called object %qE is not a function or function pointer",
3937 function);
3938 else if (DECL_P (function))
3939 {
3940 auto_diagnostic_group d;
3941 error_at (loc,
3942 "called object %qD is not a function or function pointer",
3943 function);
3944 inform_declaration (function, NULL_TREE);
3945 }
3946 else
3947 error_at (loc,
3948 "called object is not a function or function pointer");
3949 return error_mark_node;
3950 }
3951
3952 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3953 current_function_returns_abnormally = 1;
3954
3955 /* fntype now gets the type of function pointed to. */
3956 fntype = TREE_TYPE (fntype);
3957 tree return_type = TREE_TYPE (fntype);
3958
3959 /* Convert the parameters to the types declared in the
3960 function prototype, or apply default promotions. */
3961
3962 nargs = convert_arguments (loc, arg_loc, fntype, params,
3963 origtypes, function, fundecl);
3964 if (nargs < 0)
3965 return error_mark_node;
3966
3967 /* Check that the function is called through a compatible prototype.
3968 If it is not, warn. */
3969 if (CONVERT_EXPR_P (function)
3970 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3971 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3972 && !comptypes (fntype, TREE_TYPE (tem)))
3973 {
3974 /* This situation leads to run-time undefined behavior. We can't,
3975 therefore, simply error unless we can prove that all possible
3976 executions of the program must execute the code. */
3977 warning_at (loc, 0, "function called through a non-compatible type");
3978
3979 if (VOID_TYPE_P (return_type)
3980 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3981 pedwarn (loc, 0,
3982 "function with qualified void return type called");
3983 }
3984
3985 argarray = vec_safe_address (params);
3986
3987 /* Check that arguments to builtin functions match the expectations. */
3988 if (fundecl
3989 && fndecl_built_in_p (fundecl)
3990 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3991 orig_fundecl, nargs, argarray))
3992 return error_mark_node;
3993
3994 /* Check that the arguments to the function are valid. */
3995 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3996 nargs, argarray, &arg_loc,
3997 comp_parm_types);
3998
3999 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
4000 && !VOID_TYPE_P (return_type))
4001 return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
4002 if (name != NULL_TREE
4003 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
4004 {
4005 if (require_constant_value)
4006 result
4007 = fold_build_call_array_initializer_loc (loc, return_type,
4008 function, nargs, argarray);
4009 else
4010 result = fold_build_call_array_loc (loc, return_type,
4011 function, nargs, argarray);
4012 if (TREE_CODE (result) == NOP_EXPR
4013 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
4014 STRIP_TYPE_NOPS (result);
4015 }
4016 else
4017 result = build_call_array_loc (loc, return_type,
4018 function, nargs, argarray);
4019 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
4020 later. */
4021 if (warned_p && TREE_CODE (result) == CALL_EXPR)
4022 suppress_warning (result, OPT_Wnonnull);
4023
4024 /* In this improbable scenario, a nested function returns a VM type.
4025 Create a TARGET_EXPR so that the call always has a LHS, much as
4026 what the C++ FE does for functions returning non-PODs. */
4027 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (fntype)))
4028 {
4029 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
4030 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
4031 NULL_TREE, NULL_TREE);
4032 }
4033
4034 if (VOID_TYPE_P (TREE_TYPE (result)))
4035 {
4036 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
4037 pedwarn (loc, 0,
4038 "function with qualified void return type called");
4039 return result;
4040 }
4041 return require_complete_type (loc, result);
4042 }
4043
4044 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
4045
4046 tree
4047 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
4048 tree function, vec<tree, va_gc> *params,
4049 vec<tree, va_gc> *origtypes)
4050 {
4051 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4052 STRIP_TYPE_NOPS (function);
4053
4054 /* Convert anything with function type to a pointer-to-function. */
4055 if (TREE_CODE (function) == FUNCTION_DECL)
4056 {
4057 /* Implement type-directed function overloading for builtins.
4058 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
4059 handle all the type checking. The result is a complete expression
4060 that implements this function call. */
4061 tree tem = resolve_overloaded_builtin (loc, function, params);
4062 if (tem)
4063 return tem;
4064 }
4065 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
4066 }
4067
4068 /* Helper for convert_arguments called to convert the VALue of argument
4069 number ARGNUM from ORIGTYPE to the corresponding parameter number
4070 PARMNUM and TYPE.
4071 PLOC is the location where the conversion is being performed.
4072 FUNCTION and FUNDECL are the same as in convert_arguments.
4073 VALTYPE is the original type of VAL before the conversion and,
4074 for EXCESS_PRECISION_EXPR, the operand of the expression.
4075 NPC is true if VAL represents the null pointer constant (VAL itself
4076 will have been folded to an integer constant).
4077 RNAME is the same as FUNCTION except in Objective C when it's
4078 the function selector.
4079 EXCESS_PRECISION is true when VAL was originally represented
4080 as EXCESS_PRECISION_EXPR.
4081 WARNOPT is the same as in convert_for_assignment. */
4082
4083 static tree
4084 convert_argument (location_t ploc, tree function, tree fundecl,
4085 tree type, tree origtype, tree val, tree valtype,
4086 bool npc, tree rname, int parmnum, int argnum,
4087 bool excess_precision, int warnopt)
4088 {
4089 /* Formal parm type is specified by a function prototype. */
4090
4091 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4092 {
4093 error_at (ploc, "type of formal parameter %d is incomplete",
4094 parmnum + 1);
4095 return error_mark_node;
4096 }
4097
4098 /* Optionally warn about conversions that differ from the default
4099 conversions. */
4100 if (warn_traditional_conversion || warn_traditional)
4101 {
4102 if (INTEGRAL_TYPE_P (type)
4103 && SCALAR_FLOAT_TYPE_P (valtype))
4104 warning_at (ploc, OPT_Wtraditional_conversion,
4105 "passing argument %d of %qE as integer rather "
4106 "than floating due to prototype",
4107 argnum, rname);
4108 if (INTEGRAL_TYPE_P (type)
4109 && TREE_CODE (valtype) == COMPLEX_TYPE)
4110 warning_at (ploc, OPT_Wtraditional_conversion,
4111 "passing argument %d of %qE as integer rather "
4112 "than complex due to prototype",
4113 argnum, rname);
4114 else if (TREE_CODE (type) == COMPLEX_TYPE
4115 && SCALAR_FLOAT_TYPE_P (valtype))
4116 warning_at (ploc, OPT_Wtraditional_conversion,
4117 "passing argument %d of %qE as complex rather "
4118 "than floating due to prototype",
4119 argnum, rname);
4120 else if (SCALAR_FLOAT_TYPE_P (type)
4121 && INTEGRAL_TYPE_P (valtype))
4122 warning_at (ploc, OPT_Wtraditional_conversion,
4123 "passing argument %d of %qE as floating rather "
4124 "than integer due to prototype",
4125 argnum, rname);
4126 else if (TREE_CODE (type) == COMPLEX_TYPE
4127 && INTEGRAL_TYPE_P (valtype))
4128 warning_at (ploc, OPT_Wtraditional_conversion,
4129 "passing argument %d of %qE as complex rather "
4130 "than integer due to prototype",
4131 argnum, rname);
4132 else if (SCALAR_FLOAT_TYPE_P (type)
4133 && TREE_CODE (valtype) == COMPLEX_TYPE)
4134 warning_at (ploc, OPT_Wtraditional_conversion,
4135 "passing argument %d of %qE as floating rather "
4136 "than complex due to prototype",
4137 argnum, rname);
4138 /* ??? At some point, messages should be written about
4139 conversions between complex types, but that's too messy
4140 to do now. */
4141 else if (SCALAR_FLOAT_TYPE_P (type)
4142 && SCALAR_FLOAT_TYPE_P (valtype))
4143 {
4144 unsigned int formal_prec = TYPE_PRECISION (type);
4145
4146 /* Warn if any argument is passed as `float',
4147 since without a prototype it would be `double'. */
4148 if (formal_prec == TYPE_PRECISION (float_type_node)
4149 && type != dfloat32_type_node)
4150 warning_at (ploc, 0,
4151 "passing argument %d of %qE as %<float%> "
4152 "rather than %<double%> due to prototype",
4153 argnum, rname);
4154
4155 /* Warn if mismatch between argument and prototype
4156 for decimal float types. Warn of conversions with
4157 binary float types and of precision narrowing due to
4158 prototype. */
4159 else if (type != valtype
4160 && (type == dfloat32_type_node
4161 || type == dfloat64_type_node
4162 || type == dfloat128_type_node
4163 || valtype == dfloat32_type_node
4164 || valtype == dfloat64_type_node
4165 || valtype == dfloat128_type_node)
4166 && (formal_prec
4167 <= TYPE_PRECISION (valtype)
4168 || (type == dfloat128_type_node
4169 && (valtype
4170 != dfloat64_type_node
4171 && (valtype
4172 != dfloat32_type_node)))
4173 || (type == dfloat64_type_node
4174 && (valtype
4175 != dfloat32_type_node))))
4176 warning_at (ploc, 0,
4177 "passing argument %d of %qE as %qT "
4178 "rather than %qT due to prototype",
4179 argnum, rname, type, valtype);
4180
4181 }
4182 /* Detect integer changing in width or signedness.
4183 These warnings are only activated with
4184 -Wtraditional-conversion, not with -Wtraditional. */
4185 else if (warn_traditional_conversion
4186 && INTEGRAL_TYPE_P (type)
4187 && INTEGRAL_TYPE_P (valtype))
4188 {
4189 unsigned int formal_prec = TYPE_PRECISION (type);
4190 tree would_have_been = default_conversion (val);
4191 tree type1 = TREE_TYPE (would_have_been);
4192
4193 if (val == error_mark_node)
4194 /* VAL could have been of incomplete type. */;
4195 else if (TREE_CODE (type) == ENUMERAL_TYPE
4196 && (TYPE_MAIN_VARIANT (type)
4197 == TYPE_MAIN_VARIANT (valtype)))
4198 /* No warning if function asks for enum
4199 and the actual arg is that enum type. */
4200 ;
4201 else if (formal_prec != TYPE_PRECISION (type1))
4202 warning_at (ploc, OPT_Wtraditional_conversion,
4203 "passing argument %d of %qE "
4204 "with different width due to prototype",
4205 argnum, rname);
4206 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
4207 ;
4208 /* Don't complain if the formal parameter type
4209 is an enum, because we can't tell now whether
4210 the value was an enum--even the same enum. */
4211 else if (TREE_CODE (type) == ENUMERAL_TYPE)
4212 ;
4213 else if (TREE_CODE (val) == INTEGER_CST
4214 && int_fits_type_p (val, type))
4215 /* Change in signedness doesn't matter
4216 if a constant value is unaffected. */
4217 ;
4218 /* If the value is extended from a narrower
4219 unsigned type, it doesn't matter whether we
4220 pass it as signed or unsigned; the value
4221 certainly is the same either way. */
4222 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
4223 && TYPE_UNSIGNED (valtype))
4224 ;
4225 else if (TYPE_UNSIGNED (type))
4226 warning_at (ploc, OPT_Wtraditional_conversion,
4227 "passing argument %d of %qE "
4228 "as unsigned due to prototype",
4229 argnum, rname);
4230 else
4231 warning_at (ploc, OPT_Wtraditional_conversion,
4232 "passing argument %d of %qE "
4233 "as signed due to prototype",
4234 argnum, rname);
4235 }
4236 }
4237
4238 /* Possibly restore an EXCESS_PRECISION_EXPR for the
4239 sake of better warnings from convert_and_check. */
4240 if (excess_precision)
4241 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
4242
4243 tree parmval = convert_for_assignment (ploc, ploc, type,
4244 val, origtype, ic_argpass,
4245 npc, fundecl, function,
4246 parmnum + 1, warnopt);
4247
4248 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
4249 && INTEGRAL_TYPE_P (type)
4250 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4251 parmval = default_conversion (parmval);
4252
4253 return parmval;
4254 }
4255
4256 /* Convert the argument expressions in the vector VALUES
4257 to the types in the list TYPE_ARG_TYPES (FNTYPE).
4258
4259 If the list is exhausted, or when an element has NULL as its type,
4260 perform the default conversions.
4261
4262 ORIGTYPES is the original types of the expressions in VALUES. This
4263 holds the type of enum values which have been converted to integral
4264 types. It may be NULL.
4265
4266 FUNCTION is a tree for the called function. It is used only for
4267 error messages, where it is formatted with %qE.
4268
4269 This is also where warnings about wrong number of args are generated.
4270
4271 ARG_LOC are locations of function arguments (if any).
4272
4273 Returns the actual number of arguments processed (which may be less
4274 than the length of VALUES in some error situations), or -1 on
4275 failure. */
4276
4277 static int
4278 convert_arguments (location_t loc, vec<location_t> arg_loc, tree fntype,
4279 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
4280 tree function, tree fundecl)
4281 {
4282 tree typelist = TYPE_ARG_TYPES (fntype);
4283 unsigned int parmnum;
4284 bool error_args = false;
4285 const bool type_generic = fundecl
4286 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
4287 bool type_generic_remove_excess_precision = false;
4288 bool type_generic_overflow_p = false;
4289 bool type_generic_bit_query = false;
4290 tree selector;
4291
4292 /* Change pointer to function to the function itself for
4293 diagnostics. */
4294 if (TREE_CODE (function) == ADDR_EXPR
4295 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4296 function = TREE_OPERAND (function, 0);
4297
4298 /* Handle an ObjC selector specially for diagnostics. */
4299 selector = objc_message_selector ();
4300
4301 /* For a call to a built-in function declared without a prototype,
4302 set to the built-in function's argument list. */
4303 tree builtin_typelist = NULL_TREE;
4304
4305 /* For type-generic built-in functions, determine whether excess
4306 precision should be removed (classification) or not
4307 (comparison). */
4308 if (fundecl
4309 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
4310 {
4311 built_in_function code = DECL_FUNCTION_CODE (fundecl);
4312 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
4313 {
4314 /* For a call to a built-in function declared without a prototype
4315 use the types of the parameters of the internal built-in to
4316 match those of the arguments to. */
4317 if (tree bdecl = builtin_decl_explicit (code))
4318 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
4319 }
4320
4321 /* For type-generic built-in functions, determine whether excess
4322 precision should be removed (classification) or not
4323 (comparison). */
4324 if (type_generic)
4325 switch (code)
4326 {
4327 case BUILT_IN_ISFINITE:
4328 case BUILT_IN_ISINF:
4329 case BUILT_IN_ISINF_SIGN:
4330 case BUILT_IN_ISNAN:
4331 case BUILT_IN_ISNORMAL:
4332 case BUILT_IN_ISSIGNALING:
4333 case BUILT_IN_FPCLASSIFY:
4334 type_generic_remove_excess_precision = true;
4335 break;
4336
4337 case BUILT_IN_ADD_OVERFLOW_P:
4338 case BUILT_IN_SUB_OVERFLOW_P:
4339 case BUILT_IN_MUL_OVERFLOW_P:
4340 /* The last argument of these type-generic builtins
4341 should not be promoted. */
4342 type_generic_overflow_p = true;
4343 break;
4344
4345 case BUILT_IN_CLZG:
4346 case BUILT_IN_CTZG:
4347 case BUILT_IN_CLRSBG:
4348 case BUILT_IN_FFSG:
4349 case BUILT_IN_PARITYG:
4350 case BUILT_IN_POPCOUNTG:
4351 /* The first argument of these type-generic builtins
4352 should not be promoted. */
4353 type_generic_bit_query = true;
4354 break;
4355
4356 default:
4357 break;
4358 }
4359 }
4360
4361 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
4362 individual converted arguments. */
4363
4364 tree typetail, builtin_typetail, val;
4365 for (typetail = typelist,
4366 builtin_typetail = builtin_typelist,
4367 parmnum = 0;
4368 values && values->iterate (parmnum, &val);
4369 ++parmnum)
4370 {
4371 /* The type of the function parameter (if it was declared with one). */
4372 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
4373 /* The type of the built-in function parameter (if the function
4374 is a built-in). Used to detect type incompatibilities in
4375 calls to built-ins declared without a prototype. */
4376 tree builtin_type = (builtin_typetail
4377 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
4378 /* The original type of the argument being passed to the function. */
4379 tree valtype = TREE_TYPE (val);
4380 /* The called function (or function selector in Objective C). */
4381 tree rname = function;
4382 int argnum = parmnum + 1;
4383 const char *invalid_func_diag;
4384 /* Set for EXCESS_PRECISION_EXPR arguments. */
4385 bool excess_precision = false;
4386 /* The value of the argument after conversion to the type
4387 of the function parameter it is passed to. */
4388 tree parmval;
4389 /* Some __atomic_* builtins have additional hidden argument at
4390 position 0. */
4391 location_t ploc
4392 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
4393 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
4394 : input_location;
4395
4396 if (type == void_type_node)
4397 {
4398 auto_diagnostic_group d;
4399 int num_expected = parmnum;
4400 int num_actual = values->length ();
4401 gcc_rich_location rich_loc (loc);
4402 if (ploc != input_location)
4403 rich_loc.add_range (ploc);
4404 if (selector)
4405 error_at (&rich_loc,
4406 "too many arguments to method %qE; expected %i, have %i",
4407 selector, num_expected, num_actual);
4408 else
4409 error_at (&rich_loc,
4410 "too many arguments to function %qE; expected %i, have %i",
4411 function, num_expected, num_actual);
4412 inform_declaration (fundecl, function);
4413 return error_args ? -1 : (int) parmnum;
4414 }
4415
4416 if (builtin_type == void_type_node)
4417 {
4418 auto_diagnostic_group d;
4419 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
4420 "too many arguments to built-in function %qE "
4421 "expecting %d", function, parmnum))
4422 inform_declaration (fundecl, function);
4423 builtin_typetail = NULL_TREE;
4424 }
4425
4426 if (!typetail && parmnum == 0 && !TYPE_NO_NAMED_ARGS_STDARG_P (fntype)
4427 && !(fundecl && fndecl_built_in_p (fundecl)))
4428 {
4429 auto_diagnostic_group d;
4430 bool warned;
4431 if (selector)
4432 warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4433 "ISO C23 does not allow arguments"
4434 " for method %qE declared without parameters",
4435 function);
4436 else
4437 warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4438 "ISO C23 does not allow arguments"
4439 " for function %qE declared without parameters",
4440 function);
4441 if (warned)
4442 inform_declaration (fundecl, function);
4443 }
4444
4445 if (selector && argnum > 2)
4446 {
4447 rname = selector;
4448 argnum -= 2;
4449 }
4450
4451 /* Determine if VAL is a null pointer constant before folding it. */
4452 bool npc = null_pointer_constant_p (val);
4453
4454 /* If there is excess precision and a prototype, convert once to
4455 the required type rather than converting via the semantic
4456 type. Likewise without a prototype a float value represented
4457 as long double should be converted once to double. But for
4458 type-generic classification functions excess precision must
4459 be removed here. */
4460 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
4461 && (type || !type_generic || !type_generic_remove_excess_precision))
4462 {
4463 val = TREE_OPERAND (val, 0);
4464 excess_precision = true;
4465 }
4466 val = c_fully_fold (val, false, NULL);
4467 STRIP_TYPE_NOPS (val);
4468
4469 val = require_complete_type (ploc, val);
4470
4471 /* Some floating-point arguments must be promoted to double when
4472 no type is specified by a prototype. This applies to
4473 arguments of type float, and to architecture-specific types
4474 (ARM __fp16), but not to _FloatN or _FloatNx types. */
4475 bool promote_float_arg = false;
4476 if (type == NULL_TREE
4477 && TREE_CODE (valtype) == REAL_TYPE
4478 && (TYPE_PRECISION (valtype)
4479 <= TYPE_PRECISION (double_type_node))
4480 && TYPE_MAIN_VARIANT (valtype) != double_type_node
4481 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
4482 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
4483 {
4484 /* Promote this argument, unless it has a _FloatN or
4485 _FloatNx type. */
4486 promote_float_arg = true;
4487 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4488 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
4489 {
4490 promote_float_arg = false;
4491 break;
4492 }
4493 /* Don't promote __bf16 either. */
4494 if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
4495 promote_float_arg = false;
4496 }
4497
4498 if (type != NULL_TREE)
4499 {
4500 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4501 parmval = convert_argument (ploc, function, fundecl, type, origtype,
4502 val, valtype, npc, rname, parmnum, argnum,
4503 excess_precision, 0);
4504 }
4505 else if (promote_float_arg)
4506 {
4507 if (type_generic)
4508 parmval = val;
4509 else
4510 {
4511 /* Convert `float' to `double'. */
4512 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
4513 warning_at (ploc, OPT_Wdouble_promotion,
4514 "implicit conversion from %qT to %qT when passing "
4515 "argument to function",
4516 valtype, double_type_node);
4517 parmval = convert (double_type_node, val);
4518 }
4519 }
4520 else if ((excess_precision && !type_generic)
4521 || (type_generic_overflow_p && parmnum == 2)
4522 || (type_generic_bit_query && parmnum == 0))
4523 /* A "double" argument with excess precision being passed
4524 without a prototype or in variable arguments.
4525 The last argument of __builtin_*_overflow_p should not be
4526 promoted, similarly the first argument of
4527 __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
4528 parmval = convert (valtype, val);
4529 else if ((invalid_func_diag =
4530 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4531 {
4532 error (invalid_func_diag);
4533 return -1;
4534 }
4535 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
4536 {
4537 return -1;
4538 }
4539 else
4540 /* Convert `short' and `char' to full-size `int'. */
4541 parmval = default_conversion (val);
4542
4543 (*values)[parmnum] = parmval;
4544 if (parmval == error_mark_node)
4545 error_args = true;
4546
4547 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
4548 {
4549 /* For a call to a built-in function declared without a prototype,
4550 perform the conversions from the argument to the expected type
4551 but issue warnings rather than errors for any mismatches.
4552 Ignore the converted argument and use the PARMVAL obtained
4553 above by applying default conversions instead. */
4554 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4555 convert_argument (ploc, function, fundecl, builtin_type, origtype,
4556 val, valtype, npc, rname, parmnum, argnum,
4557 excess_precision,
4558 OPT_Wbuiltin_declaration_mismatch);
4559 }
4560
4561 if (typetail)
4562 typetail = TREE_CHAIN (typetail);
4563
4564 if (builtin_typetail)
4565 builtin_typetail = TREE_CHAIN (builtin_typetail);
4566 }
4567
4568 gcc_assert (parmnum == vec_safe_length (values));
4569
4570 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
4571 {
4572 /* Not enough args.
4573 Determine minimum number of arguments required. */
4574 int min_expected_num = 0;
4575 bool at_least_p = false;
4576 tree iter = typelist;
4577 while (true)
4578 {
4579 if (!iter)
4580 {
4581 /* Variadic arguments; stop iterating. */
4582 at_least_p = true;
4583 break;
4584 }
4585 if (iter == void_list_node)
4586 /* End of arguments; stop iterating. */
4587 break;
4588 ++min_expected_num;
4589 iter = TREE_CHAIN (iter);
4590 }
4591 auto_diagnostic_group d;
4592 int actual_num = vec_safe_length (values);
4593 error_at (loc,
4594 at_least_p
4595 ? G_("too few arguments to function %qE; expected at least %i, have %i")
4596 : G_("too few arguments to function %qE; expected %i, have %i"),
4597 function, min_expected_num, actual_num);
4598 inform_declaration (fundecl, function);
4599 return -1;
4600 }
4601
4602 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
4603 {
4604 unsigned nargs = parmnum;
4605 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
4606 ++nargs;
4607
4608 auto_diagnostic_group d;
4609 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
4610 "too few arguments to built-in function %qE "
4611 "expecting %u", function, nargs - 1))
4612 inform_declaration (fundecl, function);
4613 }
4614
4615 return error_args ? -1 : (int) parmnum;
4616 }
4617
4618 /* This is the entry point used by the parser to build unary operators
4619 in the input. CODE, a tree_code, specifies the unary operator, and
4620 ARG is the operand. For unary plus, the C parser currently uses
4621 CONVERT_EXPR for code.
4622
4623 LOC is the location to use for the tree generated.
4624 */
4625
4626 struct c_expr
4627 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
4628 {
4629 struct c_expr result;
4630
4631 result.original_code = code;
4632 result.original_type = NULL;
4633 result.m_decimal = 0;
4634
4635 if (reject_gcc_builtin (arg.value))
4636 {
4637 result.value = error_mark_node;
4638 }
4639 else
4640 {
4641 result.value = build_unary_op (loc, code, arg.value, false);
4642
4643 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
4644 overflow_warning (loc, result.value, arg.value);
4645 }
4646
4647 /* We are typically called when parsing a prefix token at LOC acting on
4648 ARG. Reflect this by updating the source range of the result to
4649 start at LOC and end at the end of ARG. */
4650 set_c_expr_source_range (&result,
4651 loc, arg.get_finish ());
4652
4653 return result;
4654 }
4655
4656 /* Returns true if TYPE is a character type, *not* including wchar_t. */
4657
4658 bool
4659 char_type_p (tree type)
4660 {
4661 return (type == char_type_node
4662 || type == unsigned_char_type_node
4663 || type == signed_char_type_node
4664 || type == char16_type_node
4665 || type == char32_type_node);
4666 }
4667
4668 /* This is the entry point used by the parser to build binary operators
4669 in the input. CODE, a tree_code, specifies the binary operator, and
4670 ARG1 and ARG2 are the operands. In addition to constructing the
4671 expression, we check for operands that were written with other binary
4672 operators in a way that is likely to confuse the user.
4673
4674 LOCATION is the location of the binary operator. */
4675
4676 struct c_expr
4677 parser_build_binary_op (location_t location, enum tree_code code,
4678 struct c_expr arg1, struct c_expr arg2)
4679 {
4680 struct c_expr result;
4681 result.m_decimal = 0;
4682
4683 enum tree_code code1 = arg1.original_code;
4684 enum tree_code code2 = arg2.original_code;
4685 tree type1 = (arg1.original_type
4686 ? arg1.original_type
4687 : TREE_TYPE (arg1.value));
4688 tree type2 = (arg2.original_type
4689 ? arg2.original_type
4690 : TREE_TYPE (arg2.value));
4691
4692 result.value = build_binary_op (location, code,
4693 arg1.value, arg2.value, true);
4694 result.original_code = code;
4695 result.original_type = NULL;
4696 result.m_decimal = 0;
4697
4698 if (TREE_CODE (result.value) == ERROR_MARK)
4699 {
4700 set_c_expr_source_range (&result,
4701 arg1.get_start (),
4702 arg2.get_finish ());
4703 return result;
4704 }
4705
4706 if (location != UNKNOWN_LOCATION)
4707 protected_set_expr_location (result.value, location);
4708
4709 set_c_expr_source_range (&result,
4710 arg1.get_start (),
4711 arg2.get_finish ());
4712
4713 /* Check for cases such as x+y<<z which users are likely
4714 to misinterpret. */
4715 if (warn_parentheses)
4716 warn_about_parentheses (location, code, code1, arg1.value, code2,
4717 arg2.value);
4718
4719 if (warn_logical_op)
4720 warn_logical_operator (location, code, TREE_TYPE (result.value),
4721 code1, arg1.value, code2, arg2.value);
4722
4723 if (warn_tautological_compare)
4724 {
4725 tree lhs = arg1.value;
4726 tree rhs = arg2.value;
4727 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4728 {
4729 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
4730 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
4731 lhs = NULL_TREE;
4732 else
4733 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
4734 }
4735 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
4736 {
4737 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
4738 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
4739 rhs = NULL_TREE;
4740 else
4741 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
4742 }
4743 if (lhs != NULL_TREE && rhs != NULL_TREE)
4744 warn_tautological_cmp (location, code, lhs, rhs);
4745 }
4746
4747 if (warn_logical_not_paren
4748 && TREE_CODE_CLASS (code) == tcc_comparison
4749 && code1 == TRUTH_NOT_EXPR
4750 && code2 != TRUTH_NOT_EXPR
4751 /* Avoid warning for !!x == y. */
4752 && (TREE_CODE (arg1.value) != NE_EXPR
4753 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
4754 {
4755 /* Avoid warning for !b == y where b has _Bool type. */
4756 tree t = integer_zero_node;
4757 if (TREE_CODE (arg1.value) == EQ_EXPR
4758 && integer_zerop (TREE_OPERAND (arg1.value, 1))
4759 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
4760 {
4761 t = TREE_OPERAND (arg1.value, 0);
4762 do
4763 {
4764 if (TREE_TYPE (t) != integer_type_node)
4765 break;
4766 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
4767 t = C_MAYBE_CONST_EXPR_EXPR (t);
4768 else if (CONVERT_EXPR_P (t))
4769 t = TREE_OPERAND (t, 0);
4770 else
4771 break;
4772 }
4773 while (1);
4774 }
4775 if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
4776 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
4777 }
4778
4779 /* Warn about comparisons against string literals, with the exception
4780 of testing for equality or inequality of a string literal with NULL. */
4781 if (code == EQ_EXPR || code == NE_EXPR)
4782 {
4783 if ((code1 == STRING_CST
4784 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
4785 || (code2 == STRING_CST
4786 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
4787 warning_at (location, OPT_Waddress,
4788 "comparison with string literal results in unspecified behavior");
4789 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
4790 if (POINTER_TYPE_P (type1)
4791 && null_pointer_constant_p (arg2.value)
4792 && char_type_p (type2))
4793 {
4794 auto_diagnostic_group d;
4795 if (warning_at (location, OPT_Wpointer_compare,
4796 "comparison between pointer and zero character "
4797 "constant"))
4798 inform (arg1.get_start (),
4799 "did you mean to dereference the pointer?");
4800 }
4801 else if (POINTER_TYPE_P (type2)
4802 && null_pointer_constant_p (arg1.value)
4803 && char_type_p (type1))
4804 {
4805 auto_diagnostic_group d;
4806 if (warning_at (location, OPT_Wpointer_compare,
4807 "comparison between pointer and zero character "
4808 "constant"))
4809 inform (arg2.get_start (),
4810 "did you mean to dereference the pointer?");
4811 }
4812 }
4813 else if (TREE_CODE_CLASS (code) == tcc_comparison
4814 && (code1 == STRING_CST || code2 == STRING_CST))
4815 warning_at (location, OPT_Waddress,
4816 "comparison with string literal results in unspecified "
4817 "behavior");
4818
4819 if (warn_zero_as_null_pointer_constant
4820 && c_inhibit_evaluation_warnings == 0
4821 && TREE_CODE_CLASS (code) == tcc_comparison)
4822 {
4823 if ((TREE_CODE (type1) == POINTER_TYPE
4824 || TREE_CODE (type1) == NULLPTR_TYPE)
4825 && TREE_CODE (type2) == INTEGER_TYPE
4826 && null_pointer_constant_p (arg2.value))
4827 warning_at (arg2.get_location(), OPT_Wzero_as_null_pointer_constant,
4828 "zero as null pointer constant");
4829
4830 if ((TREE_CODE (type2) == POINTER_TYPE
4831 || TREE_CODE (type2) == NULLPTR_TYPE)
4832 && TREE_CODE (type1) == INTEGER_TYPE
4833 && null_pointer_constant_p (arg1.value))
4834 warning_at (arg1.get_location(), OPT_Wzero_as_null_pointer_constant,
4835 "zero as null pointer constant");
4836 }
4837
4838 if (warn_array_compare
4839 && TREE_CODE_CLASS (code) == tcc_comparison
4840 && TREE_CODE (type1) == ARRAY_TYPE
4841 && TREE_CODE (type2) == ARRAY_TYPE)
4842 do_warn_array_compare (location, code, arg1.value, arg2.value);
4843
4844 if (TREE_OVERFLOW_P (result.value)
4845 && !TREE_OVERFLOW_P (arg1.value)
4846 && !TREE_OVERFLOW_P (arg2.value))
4847 overflow_warning (location, result.value);
4848
4849 /* Warn about comparisons of different enum types. */
4850 if (warn_enum_compare
4851 && TREE_CODE_CLASS (code) == tcc_comparison
4852 && TREE_CODE (type1) == ENUMERAL_TYPE
4853 && TREE_CODE (type2) == ENUMERAL_TYPE
4854 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
4855 warning_at (location, OPT_Wenum_compare,
4856 "comparison between %qT and %qT",
4857 type1, type2);
4858
4859 if (warn_xor_used_as_pow
4860 && code == BIT_XOR_EXPR
4861 && arg1.m_decimal
4862 && arg2.m_decimal)
4863 check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
4864 location,
4865 arg2.get_location (), arg2.value);
4866
4867 return result;
4868 }
4869
4870 /* Return a tree for the difference of pointers OP0 and OP1.
4871 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
4872 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
4873
4874 static tree
4875 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
4876 {
4877 tree restype = ptrdiff_type_node;
4878 tree result, inttype;
4879
4880 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
4881 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
4882 tree target_type = TREE_TYPE (TREE_TYPE (op0));
4883 tree orig_op0 = op0;
4884 tree orig_op1 = op1;
4885
4886 /* If the operands point into different address spaces, we need to
4887 explicitly convert them to pointers into the common address space
4888 before we can subtract the numerical address values. */
4889 if (as0 != as1)
4890 {
4891 addr_space_t as_common;
4892 tree common_type;
4893
4894 /* Determine the common superset address space. This is guaranteed
4895 to exist because the caller verified that comp_target_types
4896 returned non-zero. */
4897 if (!addr_space_superset (as0, as1, &as_common))
4898 gcc_unreachable ();
4899
4900 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4901 op0 = convert (common_type, op0);
4902 op1 = convert (common_type, op1);
4903 }
4904
4905 /* Determine integer type result of the subtraction. This will usually
4906 be the same as the result type (ptrdiff_t), but may need to be a wider
4907 type if pointers for the address space are wider than ptrdiff_t. */
4908 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4909 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4910 else
4911 inttype = restype;
4912
4913 if (VOID_TYPE_P (target_type))
4914 pedwarn (loc, OPT_Wpointer_arith,
4915 "pointer of type %<void *%> used in subtraction");
4916 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4917 pedwarn (loc, OPT_Wpointer_arith,
4918 "pointer to a function used in subtraction");
4919
4920 if (current_function_decl != NULL_TREE
4921 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4922 {
4923 op0 = save_expr (c_fully_fold (op0, false, NULL));
4924 op1 = save_expr (c_fully_fold (op1, false, NULL));
4925
4926 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4927 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4928 }
4929
4930 /* First do the subtraction, then build the divide operator
4931 and only convert at the very end.
4932 Do not do default conversions in case restype is a short type. */
4933
4934 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4935 pointers. If some platform cannot provide that, or has a larger
4936 ptrdiff_type to support differences larger than half the address
4937 space, cast the pointers to some larger integer type and do the
4938 computations in that type. */
4939 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4940 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4941 convert (inttype, op1), false);
4942 else
4943 {
4944 /* Cast away qualifiers. */
4945 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4946 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4947 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4948 }
4949
4950 /* This generates an error if op1 is pointer to incomplete type. */
4951 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4952 error_at (loc, "arithmetic on pointer to an incomplete type");
4953 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4954 TREE_TYPE (TREE_TYPE (orig_op0))))
4955 verify_type_context (loc, TCTX_POINTER_ARITH,
4956 TREE_TYPE (TREE_TYPE (orig_op1)));
4957
4958 op1 = c_size_in_bytes (target_type);
4959
4960 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4961 error_at (loc, "arithmetic on pointer to an empty aggregate");
4962
4963 /* Divide by the size, in easiest possible way. */
4964 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4965 op0, convert (inttype, op1));
4966
4967 /* Convert to final result type if necessary. */
4968 return convert (restype, result);
4969 }
4970
4971 /* Expand atomic compound assignments into an appropriate sequence as
4972 specified by the C11 standard section 6.5.16.2.
4973
4974 _Atomic T1 E1
4975 T2 E2
4976 E1 op= E2
4977
4978 This sequence is used for all types for which these operations are
4979 supported.
4980
4981 In addition, built-in versions of the 'fe' prefixed routines may
4982 need to be invoked for floating point (real, complex or vector) when
4983 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4984
4985 T1 newval;
4986 T1 old;
4987 T1 *addr
4988 T2 val
4989 fenv_t fenv
4990
4991 addr = &E1;
4992 val = (E2);
4993 __atomic_load (addr, &old, SEQ_CST);
4994 feholdexcept (&fenv);
4995 loop:
4996 newval = old op val;
4997 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4998 SEQ_CST))
4999 goto done;
5000 feclearexcept (FE_ALL_EXCEPT);
5001 goto loop:
5002 done:
5003 feupdateenv (&fenv);
5004
5005 The compiler will issue the __atomic_fetch_* built-in when possible,
5006 otherwise it will generate the generic form of the atomic operations.
5007 This requires temp(s) and has their address taken. The atomic processing
5008 is smart enough to figure out when the size of an object can utilize
5009 a lock-free version, and convert the built-in call to the appropriate
5010 lock-free routine. The optimizers will then dispose of any temps that
5011 are no longer required, and lock-free implementations are utilized as
5012 long as there is target support for the required size.
5013
5014 If the operator is NOP_EXPR, then this is a simple assignment, and
5015 an __atomic_store is issued to perform the assignment rather than
5016 the above loop. */
5017
5018 /* Build an atomic assignment at LOC, expanding into the proper
5019 sequence to store LHS MODIFYCODE= RHS. Return a value representing
5020 the result of the operation, unless RETURN_OLD_P, in which case
5021 return the old value of LHS (this is only for postincrement and
5022 postdecrement). */
5023
5024 static tree
5025 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
5026 tree rhs, bool return_old_p)
5027 {
5028 tree fndecl, func_call;
5029 vec<tree, va_gc> *params;
5030 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
5031 tree old, old_addr;
5032 tree compound_stmt = NULL_TREE;
5033 tree stmt, goto_stmt;
5034 tree loop_label, loop_decl, done_label, done_decl;
5035
5036 tree lhs_type = TREE_TYPE (lhs);
5037 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
5038 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
5039 tree rhs_semantic_type = TREE_TYPE (rhs);
5040 tree nonatomic_rhs_semantic_type;
5041 tree rhs_type;
5042
5043 gcc_assert (TYPE_ATOMIC (lhs_type));
5044
5045 if (return_old_p)
5046 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
5047
5048 /* Allocate enough vector items for a compare_exchange. */
5049 vec_alloc (params, 6);
5050
5051 /* Create a compound statement to hold the sequence of statements
5052 with a loop. */
5053 if (modifycode != NOP_EXPR)
5054 {
5055 compound_stmt = c_begin_compound_stmt (false);
5056
5057 /* For consistency with build_modify_expr on non-_Atomic,
5058 mark the lhs as read. Also, it would be very hard to match
5059 such expressions in mark_exp_read. */
5060 mark_exp_read (lhs);
5061 }
5062
5063 /* Remove any excess precision (which is only present here in the
5064 case of compound assignments). */
5065 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5066 {
5067 gcc_assert (modifycode != NOP_EXPR);
5068 rhs = TREE_OPERAND (rhs, 0);
5069 }
5070 rhs_type = TREE_TYPE (rhs);
5071
5072 /* Fold the RHS if it hasn't already been folded. */
5073 if (modifycode != NOP_EXPR)
5074 rhs = c_fully_fold (rhs, false, NULL);
5075
5076 /* Remove the qualifiers for the rest of the expressions and create
5077 the VAL temp variable to hold the RHS. */
5078 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
5079 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5080 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
5081 TYPE_UNQUALIFIED);
5082 val = create_tmp_var_raw (nonatomic_rhs_type);
5083 TREE_ADDRESSABLE (val) = 1;
5084 suppress_warning (val);
5085 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
5086 NULL_TREE);
5087 TREE_SIDE_EFFECTS (rhs) = 1;
5088 SET_EXPR_LOCATION (rhs, loc);
5089 if (modifycode != NOP_EXPR)
5090 add_stmt (rhs);
5091
5092 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
5093 an atomic_store. */
5094 if (modifycode == NOP_EXPR)
5095 {
5096 compound_stmt = rhs;
5097 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
5098 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
5099 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
5100 params->quick_push (lhs_addr);
5101 params->quick_push (rhs);
5102 params->quick_push (seq_cst);
5103 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5104
5105 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
5106 compound_stmt, func_call);
5107
5108 /* VAL is the value which was stored, return a COMPOUND_STMT of
5109 the statement and that value. */
5110 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
5111 }
5112
5113 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
5114 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
5115 isn't applicable for such builtins. ??? Do we want to handle enums? */
5116 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
5117 && TREE_CODE (rhs_type) == INTEGER_TYPE)
5118 {
5119 built_in_function fncode;
5120 switch (modifycode)
5121 {
5122 case PLUS_EXPR:
5123 case POINTER_PLUS_EXPR:
5124 fncode = (return_old_p
5125 ? BUILT_IN_ATOMIC_FETCH_ADD_N
5126 : BUILT_IN_ATOMIC_ADD_FETCH_N);
5127 break;
5128 case MINUS_EXPR:
5129 fncode = (return_old_p
5130 ? BUILT_IN_ATOMIC_FETCH_SUB_N
5131 : BUILT_IN_ATOMIC_SUB_FETCH_N);
5132 break;
5133 case BIT_AND_EXPR:
5134 fncode = (return_old_p
5135 ? BUILT_IN_ATOMIC_FETCH_AND_N
5136 : BUILT_IN_ATOMIC_AND_FETCH_N);
5137 break;
5138 case BIT_IOR_EXPR:
5139 fncode = (return_old_p
5140 ? BUILT_IN_ATOMIC_FETCH_OR_N
5141 : BUILT_IN_ATOMIC_OR_FETCH_N);
5142 break;
5143 case BIT_XOR_EXPR:
5144 fncode = (return_old_p
5145 ? BUILT_IN_ATOMIC_FETCH_XOR_N
5146 : BUILT_IN_ATOMIC_XOR_FETCH_N);
5147 break;
5148 default:
5149 goto cas_loop;
5150 }
5151
5152 /* We can only use "_1" through "_16" variants of the atomic fetch
5153 built-ins. */
5154 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
5155 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
5156 goto cas_loop;
5157
5158 /* If this is a pointer type, we need to multiply by the size of
5159 the pointer target type. */
5160 if (POINTER_TYPE_P (lhs_type))
5161 {
5162 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
5163 /* ??? This would introduce -Wdiscarded-qualifiers
5164 warning: __atomic_fetch_* expect volatile void *
5165 type as the first argument. (Assignments between
5166 atomic and non-atomic objects are OK.) */
5167 || TYPE_RESTRICT (lhs_type))
5168 goto cas_loop;
5169 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
5170 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
5171 convert (ptrdiff_type_node, rhs),
5172 convert (ptrdiff_type_node, sz));
5173 }
5174
5175 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
5176 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
5177 fndecl = builtin_decl_explicit (fncode);
5178 params->quick_push (lhs_addr);
5179 params->quick_push (rhs);
5180 params->quick_push (seq_cst);
5181 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5182
5183 newval = create_tmp_var_raw (nonatomic_lhs_type);
5184 TREE_ADDRESSABLE (newval) = 1;
5185 suppress_warning (newval);
5186 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
5187 NULL_TREE, NULL_TREE);
5188 SET_EXPR_LOCATION (rhs, loc);
5189 add_stmt (rhs);
5190
5191 /* Finish the compound statement. */
5192 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5193
5194 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
5195 the statement and that value. */
5196 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
5197 }
5198
5199 cas_loop:
5200 /* Create the variables and labels required for the op= form. */
5201 old = create_tmp_var_raw (nonatomic_lhs_type);
5202 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
5203 TREE_ADDRESSABLE (old) = 1;
5204 suppress_warning (old);
5205
5206 newval = create_tmp_var_raw (nonatomic_lhs_type);
5207 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
5208 TREE_ADDRESSABLE (newval) = 1;
5209 suppress_warning (newval);
5210
5211 loop_decl = create_artificial_label (loc);
5212 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
5213
5214 done_decl = create_artificial_label (loc);
5215 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
5216
5217 /* __atomic_load (addr, &old, SEQ_CST). */
5218 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
5219 params->quick_push (lhs_addr);
5220 params->quick_push (old_addr);
5221 params->quick_push (seq_cst);
5222 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5223 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
5224 NULL_TREE);
5225 add_stmt (old);
5226 params->truncate (0);
5227
5228 /* Create the expressions for floating-point environment
5229 manipulation, if required. */
5230 bool need_fenv = (flag_trapping_math
5231 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
5232 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
5233 if (need_fenv)
5234 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
5235
5236 if (hold_call)
5237 add_stmt (hold_call);
5238
5239 /* loop: */
5240 add_stmt (loop_label);
5241
5242 /* newval = old + val; */
5243 if (rhs_type != rhs_semantic_type)
5244 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
5245 rhs = build_binary_op (loc, modifycode, old, val, true);
5246 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5247 {
5248 tree eptype = TREE_TYPE (rhs);
5249 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
5250 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
5251 }
5252 else
5253 rhs = c_fully_fold (rhs, false, NULL);
5254 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
5255 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
5256 NULL_TREE, 0);
5257 /* The temporary variable for NEWVAL is only gimplified correctly (in
5258 particular, given a DECL_CONTEXT) if used in a TARGET_EXPR. To avoid
5259 subsequent ICEs in nested function processing after an error, ensure such
5260 a TARGET_EXPR is built even after an error. */
5261 if (rhs == error_mark_node)
5262 rhs = old;
5263 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
5264 NULL_TREE);
5265 SET_EXPR_LOCATION (rhs, loc);
5266 add_stmt (rhs);
5267
5268 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
5269 goto done; */
5270 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
5271 params->quick_push (lhs_addr);
5272 params->quick_push (old_addr);
5273 params->quick_push (newval_addr);
5274 params->quick_push (integer_zero_node);
5275 params->quick_push (seq_cst);
5276 params->quick_push (seq_cst);
5277 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5278
5279 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
5280 SET_EXPR_LOCATION (goto_stmt, loc);
5281
5282 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
5283 SET_EXPR_LOCATION (stmt, loc);
5284 add_stmt (stmt);
5285
5286 if (clear_call)
5287 add_stmt (clear_call);
5288
5289 /* goto loop; */
5290 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
5291 SET_EXPR_LOCATION (goto_stmt, loc);
5292 add_stmt (goto_stmt);
5293
5294 /* done: */
5295 add_stmt (done_label);
5296
5297 if (update_call)
5298 add_stmt (update_call);
5299
5300 /* Finish the compound statement. */
5301 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5302
5303 /* NEWVAL is the value that was successfully stored, return a
5304 COMPOUND_EXPR of the statement and the appropriate value. */
5305 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
5306 return_old_p ? old : newval);
5307 }
5308
5309 /* Construct and perhaps optimize a tree representation
5310 for a unary operation. CODE, a tree_code, specifies the operation
5311 and XARG is the operand.
5312 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
5313 promotions (such as from short to int).
5314 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
5315 non-lvalues; this is only used to handle conversion of non-lvalue arrays
5316 to pointers in C99.
5317
5318 LOCATION is the location of the operator. */
5319
5320 tree
5321 build_unary_op (location_t location, enum tree_code code, tree xarg,
5322 bool noconvert)
5323 {
5324 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5325 tree arg = xarg;
5326 tree argtype = NULL_TREE;
5327 enum tree_code typecode;
5328 tree val;
5329 tree ret = error_mark_node;
5330 tree eptype = NULL_TREE;
5331 const char *invalid_op_diag;
5332 bool int_operands;
5333
5334 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
5335 if (int_operands)
5336 arg = remove_c_maybe_const_expr (arg);
5337
5338 if (code != ADDR_EXPR)
5339 arg = require_complete_type (location, arg);
5340
5341 typecode = TREE_CODE (TREE_TYPE (arg));
5342 if (typecode == ERROR_MARK)
5343 return error_mark_node;
5344 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
5345 typecode = INTEGER_TYPE;
5346
5347 if ((invalid_op_diag
5348 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
5349 {
5350 error_at (location, invalid_op_diag);
5351 return error_mark_node;
5352 }
5353
5354 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
5355 {
5356 eptype = TREE_TYPE (arg);
5357 arg = TREE_OPERAND (arg, 0);
5358 }
5359
5360 switch (code)
5361 {
5362 case CONVERT_EXPR:
5363 /* This is used for unary plus, because a CONVERT_EXPR
5364 is enough to prevent anybody from looking inside for
5365 associativity, but won't generate any code. */
5366 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5367 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5368 || typecode == BITINT_TYPE
5369 || gnu_vector_type_p (TREE_TYPE (arg))))
5370 {
5371 error_at (location, "wrong type argument to unary plus");
5372 return error_mark_node;
5373 }
5374 else if (!noconvert)
5375 arg = default_conversion (arg);
5376 arg = non_lvalue_loc (location, arg);
5377 break;
5378
5379 case NEGATE_EXPR:
5380 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5381 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5382 || typecode == BITINT_TYPE
5383 || gnu_vector_type_p (TREE_TYPE (arg))))
5384 {
5385 error_at (location, "wrong type argument to unary minus");
5386 return error_mark_node;
5387 }
5388 else if (!noconvert)
5389 arg = default_conversion (arg);
5390 break;
5391
5392 case BIT_NOT_EXPR:
5393 /* ~ works on integer types and non float vectors. */
5394 if (typecode == INTEGER_TYPE
5395 || typecode == BITINT_TYPE
5396 || (gnu_vector_type_p (TREE_TYPE (arg))
5397 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
5398 {
5399 tree e = arg;
5400
5401 /* Warn if the expression has boolean value. */
5402 while (TREE_CODE (e) == COMPOUND_EXPR)
5403 e = TREE_OPERAND (e, 1);
5404
5405 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
5406 || truth_value_p (TREE_CODE (e))))
5407 {
5408 auto_diagnostic_group d;
5409 if (warning_at (location, OPT_Wbool_operation,
5410 "%<~%> on a boolean expression"))
5411 {
5412 gcc_rich_location richloc (location);
5413 richloc.add_fixit_insert_before (location, "!");
5414 inform (&richloc, "did you mean to use logical not?");
5415 }
5416 }
5417 if (!noconvert)
5418 arg = default_conversion (arg);
5419 }
5420 else if (typecode == COMPLEX_TYPE)
5421 {
5422 code = CONJ_EXPR;
5423 pedwarn (location, OPT_Wpedantic,
5424 "ISO C does not support %<~%> for complex conjugation");
5425 if (!noconvert)
5426 arg = default_conversion (arg);
5427 }
5428 else
5429 {
5430 error_at (location, "wrong type argument to bit-complement");
5431 return error_mark_node;
5432 }
5433 break;
5434
5435 case ABS_EXPR:
5436 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
5437 {
5438 error_at (location, "wrong type argument to abs");
5439 return error_mark_node;
5440 }
5441 else if (!noconvert)
5442 arg = default_conversion (arg);
5443 break;
5444
5445 case ABSU_EXPR:
5446 if (!(typecode == INTEGER_TYPE))
5447 {
5448 error_at (location, "wrong type argument to absu");
5449 return error_mark_node;
5450 }
5451 else if (!noconvert)
5452 arg = default_conversion (arg);
5453 break;
5454
5455 case CONJ_EXPR:
5456 /* Conjugating a real value is a no-op, but allow it anyway. */
5457 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5458 || typecode == COMPLEX_TYPE))
5459 {
5460 error_at (location, "wrong type argument to conjugation");
5461 return error_mark_node;
5462 }
5463 else if (!noconvert)
5464 arg = default_conversion (arg);
5465 break;
5466
5467 case TRUTH_NOT_EXPR:
5468 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
5469 && typecode != REAL_TYPE && typecode != POINTER_TYPE
5470 && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE
5471 && typecode != BITINT_TYPE)
5472 {
5473 error_at (location,
5474 "wrong type argument to unary exclamation mark");
5475 return error_mark_node;
5476 }
5477 if (int_operands)
5478 {
5479 arg = c_objc_common_truthvalue_conversion (location, xarg);
5480 arg = remove_c_maybe_const_expr (arg);
5481 }
5482 else
5483 arg = c_objc_common_truthvalue_conversion (location, arg);
5484 ret = invert_truthvalue_loc (location, arg);
5485 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
5486 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
5487 location = EXPR_LOCATION (ret);
5488 goto return_build_unary_op;
5489
5490 case REALPART_EXPR:
5491 case IMAGPART_EXPR:
5492 ret = build_real_imag_expr (location, code, arg);
5493 if (ret == error_mark_node)
5494 return error_mark_node;
5495 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
5496 eptype = TREE_TYPE (eptype);
5497 goto return_build_unary_op;
5498
5499 case PREINCREMENT_EXPR:
5500 case POSTINCREMENT_EXPR:
5501 case PREDECREMENT_EXPR:
5502 case POSTDECREMENT_EXPR:
5503
5504 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
5505 {
5506 tree inner = build_unary_op (location, code,
5507 C_MAYBE_CONST_EXPR_EXPR (arg),
5508 noconvert);
5509 if (inner == error_mark_node)
5510 return error_mark_node;
5511 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5512 C_MAYBE_CONST_EXPR_PRE (arg), inner);
5513 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
5514 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
5515 goto return_build_unary_op;
5516 }
5517
5518 /* Complain about anything that is not a true lvalue. In
5519 Objective-C, skip this check for property_refs. */
5520 if (!objc_is_property_ref (arg)
5521 && !lvalue_or_else (location,
5522 arg, ((code == PREINCREMENT_EXPR
5523 || code == POSTINCREMENT_EXPR)
5524 ? lv_increment
5525 : lv_decrement)))
5526 return error_mark_node;
5527
5528 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
5529 {
5530 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5531 warning_at (location, OPT_Wc___compat,
5532 "increment of enumeration value is invalid in C++");
5533 else
5534 warning_at (location, OPT_Wc___compat,
5535 "decrement of enumeration value is invalid in C++");
5536 }
5537
5538 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
5539 {
5540 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5541 warning_at (location, OPT_Wbool_operation,
5542 "increment of a boolean expression");
5543 else
5544 warning_at (location, OPT_Wbool_operation,
5545 "decrement of a boolean expression");
5546 }
5547
5548 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
5549 arg = c_fully_fold (arg, false, NULL, true);
5550
5551 bool atomic_op;
5552 atomic_op = really_atomic_lvalue (arg);
5553
5554 /* Increment or decrement the real part of the value,
5555 and don't change the imaginary part. */
5556 if (typecode == COMPLEX_TYPE)
5557 {
5558 tree real, imag;
5559
5560 pedwarn_c23 (location, OPT_Wpedantic,
5561 "ISO C does not support %<++%> and %<--%> on complex "
5562 "types before C2Y");
5563
5564 if (!atomic_op)
5565 {
5566 arg = stabilize_reference (arg);
5567 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
5568 true);
5569 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
5570 true);
5571 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
5572 if (real == error_mark_node || imag == error_mark_node)
5573 return error_mark_node;
5574 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5575 real, imag);
5576 goto return_build_unary_op;
5577 }
5578 }
5579
5580 /* Report invalid types. */
5581
5582 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
5583 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
5584 && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE
5585 && !gnu_vector_type_p (TREE_TYPE (arg)))
5586 {
5587 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5588 error_at (location, "wrong type argument to increment");
5589 else
5590 error_at (location, "wrong type argument to decrement");
5591
5592 return error_mark_node;
5593 }
5594
5595 {
5596 tree inc;
5597
5598 argtype = TREE_TYPE (arg);
5599
5600 /* Compute the increment. */
5601
5602 if (typecode == POINTER_TYPE)
5603 {
5604 /* If pointer target is an incomplete type,
5605 we just cannot know how to do the arithmetic. */
5606 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
5607 {
5608 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5609 error_at (location,
5610 "increment of pointer to an incomplete type %qT",
5611 TREE_TYPE (argtype));
5612 else
5613 error_at (location,
5614 "decrement of pointer to an incomplete type %qT",
5615 TREE_TYPE (argtype));
5616 }
5617 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
5618 || VOID_TYPE_P (TREE_TYPE (argtype)))
5619 {
5620 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5621 pedwarn (location, OPT_Wpointer_arith,
5622 "wrong type argument to increment");
5623 else
5624 pedwarn (location, OPT_Wpointer_arith,
5625 "wrong type argument to decrement");
5626 }
5627 else
5628 verify_type_context (location, TCTX_POINTER_ARITH,
5629 TREE_TYPE (argtype));
5630
5631 inc = c_size_in_bytes (TREE_TYPE (argtype));
5632 inc = convert_to_ptrofftype_loc (location, inc);
5633 }
5634 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
5635 {
5636 /* For signed fract types, we invert ++ to -- or
5637 -- to ++, and change inc from 1 to -1, because
5638 it is not possible to represent 1 in signed fract constants.
5639 For unsigned fract types, the result always overflows and
5640 we get an undefined (original) or the maximum value. */
5641 if (code == PREINCREMENT_EXPR)
5642 code = PREDECREMENT_EXPR;
5643 else if (code == PREDECREMENT_EXPR)
5644 code = PREINCREMENT_EXPR;
5645 else if (code == POSTINCREMENT_EXPR)
5646 code = POSTDECREMENT_EXPR;
5647 else /* code == POSTDECREMENT_EXPR */
5648 code = POSTINCREMENT_EXPR;
5649
5650 inc = integer_minus_one_node;
5651 inc = convert (argtype, inc);
5652 }
5653 else
5654 {
5655 inc = VECTOR_TYPE_P (argtype)
5656 ? build_one_cst (argtype)
5657 : integer_one_node;
5658 inc = convert (argtype, inc);
5659 }
5660
5661 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5662 need to ask Objective-C to build the increment or decrement
5663 expression for it. */
5664 if (objc_is_property_ref (arg))
5665 return objc_build_incr_expr_for_property_ref (location, code,
5666 arg, inc);
5667
5668 /* Report a read-only lvalue. */
5669 if (TYPE_READONLY (argtype))
5670 {
5671 readonly_error (location, arg,
5672 ((code == PREINCREMENT_EXPR
5673 || code == POSTINCREMENT_EXPR)
5674 ? lv_increment : lv_decrement));
5675 return error_mark_node;
5676 }
5677 else if (TREE_READONLY (arg))
5678 readonly_warning (arg,
5679 ((code == PREINCREMENT_EXPR
5680 || code == POSTINCREMENT_EXPR)
5681 ? lv_increment : lv_decrement));
5682
5683 /* If the argument is atomic, use the special code sequences for
5684 atomic compound assignment. */
5685 if (atomic_op)
5686 {
5687 arg = stabilize_reference (arg);
5688 ret = build_atomic_assign (location, arg,
5689 ((code == PREINCREMENT_EXPR
5690 || code == POSTINCREMENT_EXPR)
5691 ? PLUS_EXPR
5692 : MINUS_EXPR),
5693 (FRACT_MODE_P (TYPE_MODE (argtype))
5694 ? inc
5695 : integer_one_node),
5696 (code == POSTINCREMENT_EXPR
5697 || code == POSTDECREMENT_EXPR));
5698 goto return_build_unary_op;
5699 }
5700
5701 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
5702 val = boolean_increment (code, arg);
5703 else
5704 val = build2 (code, TREE_TYPE (arg), arg, inc);
5705 TREE_SIDE_EFFECTS (val) = 1;
5706 if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
5707 TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
5708 TYPE_UNQUALIFIED);
5709 ret = val;
5710 goto return_build_unary_op;
5711 }
5712
5713 case ADDR_EXPR:
5714 /* Note that this operation never does default_conversion. */
5715
5716 /* The operand of unary '&' must be an lvalue (which excludes
5717 expressions of type void), or, in C99, the result of a [] or
5718 unary '*' operator. */
5719 if (VOID_TYPE_P (TREE_TYPE (arg))
5720 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
5721 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
5722 pedwarn (location, 0, "taking address of expression of type %<void%>");
5723
5724 /* Let &* cancel out to simplify resulting code. */
5725 if (INDIRECT_REF_P (arg))
5726 {
5727 /* Don't let this be an lvalue. */
5728 if (lvalue_p (TREE_OPERAND (arg, 0)))
5729 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
5730 ret = TREE_OPERAND (arg, 0);
5731 goto return_build_unary_op;
5732 }
5733
5734 /* Anything not already handled and not a true memory reference
5735 or a non-lvalue array is an error. */
5736 if (typecode != FUNCTION_TYPE && !noconvert
5737 && !lvalue_or_else (location, arg, lv_addressof))
5738 return error_mark_node;
5739
5740 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
5741 folding later. */
5742 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
5743 {
5744 tree inner = build_unary_op (location, code,
5745 C_MAYBE_CONST_EXPR_EXPR (arg),
5746 noconvert);
5747 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5748 C_MAYBE_CONST_EXPR_PRE (arg), inner);
5749 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
5750 C_MAYBE_CONST_EXPR_NON_CONST (ret)
5751 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
5752 goto return_build_unary_op;
5753 }
5754
5755 /* Ordinary case; arg is a COMPONENT_REF or a decl, or a call to
5756 .ACCESS_WITH_SIZE. */
5757 if (is_access_with_size_p (arg))
5758 arg = TREE_OPERAND (TREE_OPERAND (CALL_EXPR_ARG (arg, 0), 0), 0);
5759
5760 argtype = TREE_TYPE (arg);
5761
5762 /* If the lvalue is const or volatile, merge that into the type
5763 to which the address will point. This is only needed
5764 for function types. */
5765 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
5766 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
5767 && TREE_CODE (argtype) == FUNCTION_TYPE)
5768 {
5769 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
5770 int quals = orig_quals;
5771
5772 if (TREE_READONLY (arg))
5773 quals |= TYPE_QUAL_CONST;
5774 if (TREE_THIS_VOLATILE (arg))
5775 quals |= TYPE_QUAL_VOLATILE;
5776
5777 argtype = c_build_qualified_type (argtype, quals);
5778 }
5779
5780 switch (TREE_CODE (arg))
5781 {
5782 case COMPONENT_REF:
5783 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5784 {
5785 error_at (location, "cannot take address of bit-field %qD",
5786 TREE_OPERAND (arg, 1));
5787 return error_mark_node;
5788 }
5789
5790 /* fall through */
5791
5792 case ARRAY_REF:
5793 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
5794 {
5795 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
5796 && !POINTER_TYPE_P (TREE_TYPE (arg))
5797 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
5798 {
5799 error_at (location, "cannot take address of scalar with "
5800 "reverse storage order");
5801 return error_mark_node;
5802 }
5803
5804 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
5805 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
5806 warning_at (location, OPT_Wscalar_storage_order,
5807 "address of array with reverse scalar storage "
5808 "order requested");
5809 }
5810
5811 default:
5812 break;
5813 }
5814
5815 if (!c_mark_addressable (arg))
5816 return error_mark_node;
5817
5818 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
5819 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
5820
5821 argtype = c_build_pointer_type (argtype);
5822
5823 /* ??? Cope with user tricks that amount to offsetof. Delete this
5824 when we have proper support for integer constant expressions. */
5825 val = get_base_address (arg);
5826 if (val && INDIRECT_REF_P (val)
5827 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5828 {
5829 ret = fold_offsetof (arg, argtype);
5830 goto return_build_unary_op;
5831 }
5832
5833 val = build1 (ADDR_EXPR, argtype, arg);
5834
5835 ret = val;
5836 goto return_build_unary_op;
5837
5838 case PAREN_EXPR:
5839 ret = build1 (code, TREE_TYPE (arg), arg);
5840 goto return_build_unary_op;
5841
5842 default:
5843 gcc_unreachable ();
5844 }
5845
5846 if (argtype == NULL_TREE)
5847 argtype = TREE_TYPE (arg);
5848 if (TREE_CODE (arg) == INTEGER_CST)
5849 ret = (require_constant_value
5850 ? fold_build1_initializer_loc (location, code, argtype, arg)
5851 : fold_build1_loc (location, code, argtype, arg));
5852 else
5853 ret = build1 (code, argtype, arg);
5854 return_build_unary_op:
5855 gcc_assert (ret != error_mark_node);
5856 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
5857 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
5858 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
5859 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
5860 ret = note_integer_operands (ret);
5861 if (eptype)
5862 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5863 protected_set_expr_location (ret, location);
5864 return ret;
5865 }
5866
5867 /* Return nonzero if REF is an lvalue valid for this language.
5868 Lvalues can be assigned, unless their type has TYPE_READONLY.
5869 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
5870
5871 bool
5872 lvalue_p (const_tree ref)
5873 {
5874 const enum tree_code code = TREE_CODE (ref);
5875
5876 switch (code)
5877 {
5878 case REALPART_EXPR:
5879 case IMAGPART_EXPR:
5880 case COMPONENT_REF:
5881 return lvalue_p (TREE_OPERAND (ref, 0));
5882
5883 case C_MAYBE_CONST_EXPR:
5884 return lvalue_p (TREE_OPERAND (ref, 1));
5885
5886 case COMPOUND_LITERAL_EXPR:
5887 case STRING_CST:
5888 return true;
5889
5890 case MEM_REF:
5891 case TARGET_MEM_REF:
5892 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
5893 here as well. */
5894 case INDIRECT_REF:
5895 case ARRAY_REF:
5896 case VAR_DECL:
5897 case PARM_DECL:
5898 case RESULT_DECL:
5899 case ERROR_MARK:
5900 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
5901 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5902
5903 case BIND_EXPR:
5904 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5905
5906 case CALL_EXPR:
5907 return is_access_with_size_p (ref);
5908
5909 default:
5910 return false;
5911 }
5912 }
5913
5914 /* Give a warning for storing in something that is read-only in GCC
5915 terms but not const in ISO C terms. */
5916
5917 static void
5918 readonly_warning (tree arg, enum lvalue_use use)
5919 {
5920 switch (use)
5921 {
5922 case lv_assign:
5923 warning (0, "assignment of read-only location %qE", arg);
5924 break;
5925 case lv_increment:
5926 warning (0, "increment of read-only location %qE", arg);
5927 break;
5928 case lv_decrement:
5929 warning (0, "decrement of read-only location %qE", arg);
5930 break;
5931 default:
5932 gcc_unreachable ();
5933 }
5934 return;
5935 }
5936
5937
5938 /* Return nonzero if REF is an lvalue valid for this language;
5939 otherwise, print an error message and return zero. USE says
5940 how the lvalue is being used and so selects the error message.
5941 LOCATION is the location at which any error should be reported. */
5942
5943 static int
5944 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5945 {
5946 int win = lvalue_p (ref);
5947
5948 if (!win)
5949 lvalue_error (loc, use);
5950
5951 return win;
5952 }
5953
5954 /* Mark EXP saying that we need to be able to take the
5955 address of it; it should not be allocated in a register.
5956 Returns true if successful. ARRAY_REF_P is true if this
5957 is for ARRAY_REF construction - in that case we don't want
5958 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5959 it is fine to use ARRAY_REFs for vector subscripts on vector
5960 register variables. */
5961
5962 bool
5963 c_mark_addressable (tree exp, bool array_ref_p)
5964 {
5965 tree x = exp;
5966
5967 while (1)
5968 switch (TREE_CODE (x))
5969 {
5970 case VIEW_CONVERT_EXPR:
5971 if (array_ref_p
5972 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5973 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5974 return true;
5975 x = TREE_OPERAND (x, 0);
5976 break;
5977
5978 case COMPONENT_REF:
5979 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5980 {
5981 error ("cannot take address of bit-field %qD",
5982 TREE_OPERAND (x, 1));
5983 return false;
5984 }
5985 /* FALLTHRU */
5986 case ADDR_EXPR:
5987 case ARRAY_REF:
5988 case REALPART_EXPR:
5989 case IMAGPART_EXPR:
5990 x = TREE_OPERAND (x, 0);
5991 break;
5992
5993 case COMPOUND_LITERAL_EXPR:
5994 if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
5995 {
5996 error ("address of register compound literal requested");
5997 return false;
5998 }
5999 TREE_ADDRESSABLE (x) = 1;
6000 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
6001 return true;
6002
6003 case CONSTRUCTOR:
6004 TREE_ADDRESSABLE (x) = 1;
6005 return true;
6006
6007 case VAR_DECL:
6008 case CONST_DECL:
6009 case PARM_DECL:
6010 case RESULT_DECL:
6011 if (C_DECL_REGISTER (x)
6012 && DECL_NONLOCAL (x))
6013 {
6014 if (TREE_PUBLIC (x) || is_global_var (x))
6015 {
6016 error
6017 ("global register variable %qD used in nested function", x);
6018 return false;
6019 }
6020 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
6021 }
6022 else if (C_DECL_REGISTER (x))
6023 {
6024 if (TREE_PUBLIC (x) || is_global_var (x))
6025 error ("address of global register variable %qD requested", x);
6026 else
6027 error ("address of register variable %qD requested", x);
6028 return false;
6029 }
6030
6031 /* FALLTHRU */
6032 case FUNCTION_DECL:
6033 TREE_ADDRESSABLE (x) = 1;
6034 /* FALLTHRU */
6035 default:
6036 return true;
6037 }
6038 }
6039
6040 /* Convert EXPR to TYPE, warning about conversion problems with
6041 constants. SEMANTIC_TYPE is the type this conversion would use
6042 without excess precision. If SEMANTIC_TYPE is NULL, this function
6043 is equivalent to convert_and_check. This function is a wrapper that
6044 handles conversions that may be different than
6045 the usual ones because of excess precision. */
6046
6047 static tree
6048 ep_convert_and_check (location_t loc, tree type, tree expr,
6049 tree semantic_type)
6050 {
6051 if (TREE_TYPE (expr) == type)
6052 return expr;
6053
6054 /* For C11, integer conversions may have results with excess
6055 precision. */
6056 if (flag_isoc11 || !semantic_type)
6057 return convert_and_check (loc, type, expr);
6058
6059 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
6060 && TREE_TYPE (expr) != semantic_type)
6061 {
6062 /* For integers, we need to check the real conversion, not
6063 the conversion to the excess precision type. */
6064 expr = convert_and_check (loc, semantic_type, expr);
6065 }
6066 /* Result type is the excess precision type, which should be
6067 large enough, so do not check. */
6068 return convert (type, expr);
6069 }
6070
6071 /* If EXPR refers to a built-in declared without a prototype returns
6072 the actual type of the built-in and, if non-null, set *BLTIN to
6073 a pointer to the built-in. Otherwise return the type of EXPR
6074 and clear *BLTIN if non-null. */
6075
6076 static tree
6077 type_or_builtin_type (tree expr, tree *bltin = NULL)
6078 {
6079 tree dummy;
6080 if (!bltin)
6081 bltin = &dummy;
6082
6083 *bltin = NULL_TREE;
6084
6085 tree type = TREE_TYPE (expr);
6086 if (TREE_CODE (expr) != ADDR_EXPR)
6087 return type;
6088
6089 tree oper = TREE_OPERAND (expr, 0);
6090 if (!DECL_P (oper)
6091 || TREE_CODE (oper) != FUNCTION_DECL
6092 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
6093 return type;
6094
6095 built_in_function code = DECL_FUNCTION_CODE (oper);
6096 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
6097 return type;
6098
6099 if ((*bltin = builtin_decl_implicit (code)))
6100 type = c_build_pointer_type (TREE_TYPE (*bltin));
6101
6102 return type;
6103 }
6104
6105 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
6106 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
6107 if folded to an integer constant then the unselected half may
6108 contain arbitrary operations not normally permitted in constant
6109 expressions. Set the location of the expression to LOC. */
6110
6111 tree
6112 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
6113 tree op1, tree op1_original_type, location_t op1_loc,
6114 tree op2, tree op2_original_type, location_t op2_loc)
6115 {
6116 tree type1;
6117 tree type2;
6118 enum tree_code code1;
6119 enum tree_code code2;
6120 tree result_type = NULL;
6121 tree semantic_result_type = NULL;
6122 tree orig_op1 = op1, orig_op2 = op2;
6123 bool int_const, op1_int_operands, op2_int_operands, int_operands;
6124 bool ifexp_int_operands;
6125 tree ret;
6126
6127 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
6128 if (op1_int_operands)
6129 op1 = remove_c_maybe_const_expr (op1);
6130 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
6131 if (op2_int_operands)
6132 op2 = remove_c_maybe_const_expr (op2);
6133 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
6134 if (ifexp_int_operands)
6135 ifexp = remove_c_maybe_const_expr (ifexp);
6136
6137 /* Promote both alternatives. */
6138
6139 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
6140 op1 = default_conversion (op1);
6141 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
6142 op2 = default_conversion (op2);
6143
6144 if (TREE_CODE (ifexp) == ERROR_MARK
6145 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
6146 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
6147 return error_mark_node;
6148
6149 tree bltin1 = NULL_TREE;
6150 tree bltin2 = NULL_TREE;
6151 type1 = type_or_builtin_type (op1, &bltin1);
6152 code1 = TREE_CODE (type1);
6153 type2 = type_or_builtin_type (op2, &bltin2);
6154 code2 = TREE_CODE (type2);
6155
6156 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
6157 return error_mark_node;
6158
6159 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
6160 return error_mark_node;
6161
6162 /* C90 does not permit non-lvalue arrays in conditional expressions.
6163 In C99 they will be pointers by now. */
6164 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
6165 {
6166 error_at (colon_loc, "non-lvalue array in conditional expression");
6167 return error_mark_node;
6168 }
6169
6170 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
6171 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6172 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6173 || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6174 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6175 || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6176 {
6177 semantic_result_type = c_common_type (type1, type2);
6178 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
6179 {
6180 op1 = TREE_OPERAND (op1, 0);
6181 type1 = TREE_TYPE (op1);
6182 gcc_assert (TREE_CODE (type1) == code1);
6183 }
6184 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6185 {
6186 op2 = TREE_OPERAND (op2, 0);
6187 type2 = TREE_TYPE (op2);
6188 gcc_assert (TREE_CODE (type2) == code2);
6189 }
6190 }
6191
6192 if (warn_cxx_compat)
6193 {
6194 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
6195 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
6196
6197 if (TREE_CODE (t1) == ENUMERAL_TYPE
6198 && TREE_CODE (t2) == ENUMERAL_TYPE
6199 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
6200 warning_at (colon_loc, OPT_Wc___compat,
6201 ("different enum types in conditional is "
6202 "invalid in C++: %qT vs %qT"),
6203 t1, t2);
6204 }
6205
6206 if (warn_zero_as_null_pointer_constant
6207 && c_inhibit_evaluation_warnings == 0)
6208 {
6209 if ((code1 == POINTER_TYPE || code1 == NULLPTR_TYPE)
6210 && code2 == INTEGER_TYPE && null_pointer_constant_p (orig_op2))
6211 warning_at (op2_loc, OPT_Wzero_as_null_pointer_constant,
6212 "zero as null pointer constant");
6213
6214 if ((code2 == POINTER_TYPE || code2 == NULLPTR_TYPE)
6215 && code1 == INTEGER_TYPE && null_pointer_constant_p (orig_op1))
6216 warning_at (op1_loc, OPT_Wzero_as_null_pointer_constant,
6217 "zero as null pointer constant");
6218 }
6219
6220 /* Quickly detect the usual case where op1 and op2 have the same type
6221 after promotion. */
6222 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
6223 {
6224 if (type1 == type2)
6225 result_type = type1;
6226 else
6227 result_type = TYPE_MAIN_VARIANT (type1);
6228 }
6229 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
6230 || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6231 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6232 || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6233 {
6234 /* In C11, a conditional expression between a floating-point
6235 type and an integer type should convert the integer type to
6236 the evaluation format of the floating-point type, with
6237 possible excess precision. */
6238 tree eptype1 = type1;
6239 tree eptype2 = type2;
6240 if (flag_isoc11)
6241 {
6242 tree eptype;
6243 if (ANY_INTEGRAL_TYPE_P (type1)
6244 && (eptype = excess_precision_type (type2)) != NULL_TREE)
6245 {
6246 eptype2 = eptype;
6247 if (!semantic_result_type)
6248 semantic_result_type = c_common_type (type1, type2);
6249 }
6250 else if (ANY_INTEGRAL_TYPE_P (type2)
6251 && (eptype = excess_precision_type (type1)) != NULL_TREE)
6252 {
6253 eptype1 = eptype;
6254 if (!semantic_result_type)
6255 semantic_result_type = c_common_type (type1, type2);
6256 }
6257 }
6258 result_type = c_common_type (eptype1, eptype2);
6259 if (result_type == error_mark_node)
6260 return error_mark_node;
6261 do_warn_double_promotion (result_type, type1, type2,
6262 "implicit conversion from %qT to %qT to "
6263 "match other result of conditional",
6264 colon_loc);
6265
6266 /* If -Wsign-compare, warn here if type1 and type2 have
6267 different signedness. We'll promote the signed to unsigned
6268 and later code won't know it used to be different.
6269 Do this check on the original types, so that explicit casts
6270 will be considered, but default promotions won't. */
6271 if (c_inhibit_evaluation_warnings == 0)
6272 {
6273 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
6274 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
6275
6276 if (unsigned_op1 ^ unsigned_op2)
6277 {
6278 bool ovf;
6279
6280 /* Do not warn if the result type is signed, since the
6281 signed type will only be chosen if it can represent
6282 all the values of the unsigned type. */
6283 if (!TYPE_UNSIGNED (result_type))
6284 /* OK */;
6285 else
6286 {
6287 bool op1_maybe_const = true;
6288 bool op2_maybe_const = true;
6289
6290 /* Do not warn if the signed quantity is an
6291 unsuffixed integer literal (or some static
6292 constant expression involving such literals) and
6293 it is non-negative. This warning requires the
6294 operands to be folded for best results, so do
6295 that folding in this case even without
6296 warn_sign_compare to avoid warning options
6297 possibly affecting code generation. */
6298 c_inhibit_evaluation_warnings
6299 += (ifexp == truthvalue_false_node);
6300 op1 = c_fully_fold (op1, require_constant_value,
6301 &op1_maybe_const);
6302 c_inhibit_evaluation_warnings
6303 -= (ifexp == truthvalue_false_node);
6304
6305 c_inhibit_evaluation_warnings
6306 += (ifexp == truthvalue_true_node);
6307 op2 = c_fully_fold (op2, require_constant_value,
6308 &op2_maybe_const);
6309 c_inhibit_evaluation_warnings
6310 -= (ifexp == truthvalue_true_node);
6311
6312 if (warn_sign_compare)
6313 {
6314 if ((unsigned_op2
6315 && tree_expr_nonnegative_warnv_p (op1, &ovf))
6316 || (unsigned_op1
6317 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
6318 /* OK */;
6319 else if (unsigned_op2)
6320 warning_at (op1_loc, OPT_Wsign_compare,
6321 "operand of %<?:%> changes signedness from "
6322 "%qT to %qT due to unsignedness of other "
6323 "operand", TREE_TYPE (orig_op1),
6324 TREE_TYPE (orig_op2));
6325 else
6326 warning_at (op2_loc, OPT_Wsign_compare,
6327 "operand of %<?:%> changes signedness from "
6328 "%qT to %qT due to unsignedness of other "
6329 "operand", TREE_TYPE (orig_op2),
6330 TREE_TYPE (orig_op1));
6331 }
6332 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
6333 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
6334 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
6335 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
6336 }
6337 }
6338 }
6339 }
6340 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
6341 {
6342 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
6343 pedwarn (colon_loc, OPT_Wpedantic,
6344 "ISO C forbids conditional expr with only one void side");
6345 result_type = void_type_node;
6346 }
6347 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
6348 {
6349 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
6350 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
6351 addr_space_t as_common;
6352
6353 if (comp_target_types (colon_loc, type1, type2))
6354 result_type = common_pointer_type (type1, type2);
6355 else if (null_pointer_constant_p (orig_op1))
6356 result_type = type2;
6357 else if (null_pointer_constant_p (orig_op2))
6358 result_type = type1;
6359 else if (!addr_space_superset (as1, as2, &as_common))
6360 {
6361 error_at (colon_loc, "pointers to disjoint address spaces "
6362 "used in conditional expression");
6363 return error_mark_node;
6364 }
6365 else if ((VOID_TYPE_P (TREE_TYPE (type1))
6366 && !TYPE_ATOMIC (TREE_TYPE (type1)))
6367 || (VOID_TYPE_P (TREE_TYPE (type2))
6368 && !TYPE_ATOMIC (TREE_TYPE (type2))))
6369 {
6370 tree t1 = TREE_TYPE (type1);
6371 tree t2 = TREE_TYPE (type2);
6372 if (!(VOID_TYPE_P (t1)
6373 && !TYPE_ATOMIC (t1)))
6374 {
6375 /* roles are swapped */
6376 t1 = t2;
6377 t2 = TREE_TYPE (type1);
6378 }
6379 tree t2_stripped = strip_array_types (t2);
6380 if ((TREE_CODE (t2) == ARRAY_TYPE)
6381 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
6382 {
6383 if (!flag_isoc23)
6384 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
6385 "pointer to array loses qualifier "
6386 "in conditional expression");
6387 else if (warn_c11_c23_compat > 0)
6388 warning_at (colon_loc, OPT_Wc11_c23_compat,
6389 "pointer to array loses qualifier "
6390 "in conditional expression in ISO C before C23");
6391 }
6392 if (TREE_CODE (t2) == FUNCTION_TYPE)
6393 pedwarn (colon_loc, OPT_Wpedantic,
6394 "ISO C forbids conditional expr between "
6395 "%<void *%> and function pointer");
6396 /* for array, use qualifiers of element type */
6397 if (flag_isoc23)
6398 t2 = t2_stripped;
6399 result_type = c_build_pointer_type (qualify_type (t1, t2));
6400 }
6401 /* Objective-C pointer comparisons are a bit more lenient. */
6402 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
6403 result_type = objc_common_type (type1, type2);
6404 else
6405 {
6406 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
6407 diagnostic_t kind = DK_PERMERROR;
6408 if (!flag_isoc99)
6409 /* This downgrade to a warning ensures that -std=gnu89
6410 -pedantic-errors does not flag these mismatches between
6411 builtins as errors (as DK_PERMERROR would). ISO C99
6412 and later do not have implicit function declarations,
6413 so the mismatch cannot occur naturally there. */
6414 kind = bltin1 && bltin2 ? DK_WARNING : DK_PEDWARN;
6415 if (emit_diagnostic (kind, colon_loc, OPT_Wincompatible_pointer_types,
6416 "pointer type mismatch "
6417 "in conditional expression"))
6418 {
6419 inform (op1_loc, "first expression has type %qT", type1);
6420 inform (op2_loc, "second expression has type %qT", type2);
6421 }
6422 result_type = c_build_pointer_type
6423 (c_build_qualified_type (void_type_node, qual));
6424 }
6425 }
6426 else if (code1 == POINTER_TYPE
6427 && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE))
6428 {
6429 if (!null_pointer_constant_p (orig_op2))
6430 permerror_opt (colon_loc, OPT_Wint_conversion,
6431 "pointer/integer type mismatch "
6432 "in conditional expression");
6433 else
6434 {
6435 op2 = null_pointer_node;
6436 }
6437 result_type = type1;
6438 }
6439 else if (code2 == POINTER_TYPE
6440 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
6441 {
6442 if (!null_pointer_constant_p (orig_op1))
6443 permerror_opt (colon_loc, OPT_Wint_conversion,
6444 "pointer/integer type mismatch "
6445 "in conditional expression");
6446 else
6447 {
6448 op1 = null_pointer_node;
6449 }
6450 result_type = type2;
6451 }
6452 /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
6453 type nullptr_t and the other is a pointer, the result type is the pointer
6454 type." */
6455 else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
6456 result_type = type2;
6457 else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
6458 result_type = type1;
6459 else if (RECORD_OR_UNION_TYPE_P (type1) && RECORD_OR_UNION_TYPE_P (type2)
6460 && comptypes (TYPE_MAIN_VARIANT (type1),
6461 TYPE_MAIN_VARIANT (type2)))
6462 result_type = composite_type (TYPE_MAIN_VARIANT (type1),
6463 TYPE_MAIN_VARIANT (type2));
6464
6465 if (!result_type)
6466 {
6467 if (flag_cond_mismatch)
6468 result_type = void_type_node;
6469 else
6470 {
6471 error_at (colon_loc, "type mismatch in conditional expression");
6472 return error_mark_node;
6473 }
6474 }
6475
6476 /* Merge const and volatile flags of the incoming types. */
6477 result_type
6478 = build_type_variant (result_type,
6479 TYPE_READONLY (type1) || TYPE_READONLY (type2),
6480 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
6481
6482 op1 = ep_convert_and_check (colon_loc, result_type, op1,
6483 semantic_result_type);
6484 op2 = ep_convert_and_check (colon_loc, result_type, op2,
6485 semantic_result_type);
6486
6487 if (ifexp_bcp && ifexp == truthvalue_true_node)
6488 {
6489 op2_int_operands = true;
6490 op1 = c_fully_fold (op1, require_constant_value, NULL);
6491 }
6492 if (ifexp_bcp && ifexp == truthvalue_false_node)
6493 {
6494 op1_int_operands = true;
6495 op2 = c_fully_fold (op2, require_constant_value, NULL);
6496 }
6497 int_const = int_operands = (ifexp_int_operands
6498 && op1_int_operands
6499 && op2_int_operands);
6500 if (int_operands)
6501 {
6502 int_const = ((ifexp == truthvalue_true_node
6503 && TREE_CODE (orig_op1) == INTEGER_CST
6504 && !TREE_OVERFLOW (orig_op1))
6505 || (ifexp == truthvalue_false_node
6506 && TREE_CODE (orig_op2) == INTEGER_CST
6507 && !TREE_OVERFLOW (orig_op2)));
6508 }
6509
6510 /* Need to convert condition operand into a vector mask. */
6511 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
6512 {
6513 tree vectype = TREE_TYPE (ifexp);
6514 tree elem_type = TREE_TYPE (vectype);
6515 tree zero = build_int_cst (elem_type, 0);
6516 tree zero_vec = build_vector_from_val (vectype, zero);
6517 tree cmp_type = truth_type_for (vectype);
6518 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
6519 }
6520
6521 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
6522 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
6523 else
6524 {
6525 if (int_operands)
6526 {
6527 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
6528 nested inside of the expression. */
6529 op1 = c_fully_fold (op1, false, NULL);
6530 op2 = c_fully_fold (op2, false, NULL);
6531 }
6532 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
6533 if (int_operands)
6534 ret = note_integer_operands (ret);
6535 }
6536 if (semantic_result_type)
6537 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
6538
6539 protected_set_expr_location (ret, colon_loc);
6540
6541 /* If the OP1 and OP2 are the same and don't have side-effects,
6542 warn here, because the COND_EXPR will be turned into OP1. */
6543 if (warn_duplicated_branches
6544 && TREE_CODE (ret) == COND_EXPR
6545 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
6546 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
6547 "this condition has identical branches");
6548
6549 return ret;
6550 }
6551
6552 /* EXPR is an expression, location LOC, whose result is discarded.
6553 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
6554 whose right-hand operand is such a call, possibly recursively). */
6555
6556 static void
6557 maybe_warn_nodiscard (location_t loc, tree expr)
6558 {
6559 if (VOID_TYPE_P (TREE_TYPE (expr)))
6560 return;
6561 while (TREE_CODE (expr) == COMPOUND_EXPR)
6562 {
6563 expr = TREE_OPERAND (expr, 1);
6564 if (EXPR_HAS_LOCATION (expr))
6565 loc = EXPR_LOCATION (expr);
6566 }
6567 if (TREE_CODE (expr) != CALL_EXPR)
6568 return;
6569 tree fn = CALL_EXPR_FN (expr);
6570 if (!fn)
6571 return;
6572 tree attr;
6573 if (TREE_CODE (fn) == ADDR_EXPR
6574 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
6575 && (attr = lookup_attribute ("nodiscard",
6576 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
6577 {
6578 fn = TREE_OPERAND (fn, 0);
6579 tree args = TREE_VALUE (attr);
6580 if (args)
6581 args = TREE_VALUE (args);
6582 auto_diagnostic_group d;
6583 auto_urlify_attributes sentinel;
6584 int warned;
6585 if (args)
6586 warned = warning_at (loc, OPT_Wunused_result,
6587 "ignoring return value of %qD, declared with "
6588 "attribute %<nodiscard%>: %E", fn, args);
6589 else
6590 warned = warning_at (loc, OPT_Wunused_result,
6591 "ignoring return value of %qD, declared with "
6592 "attribute %<nodiscard%>", fn);
6593 if (warned)
6594 inform (DECL_SOURCE_LOCATION (fn), "declared here");
6595 }
6596 else
6597 {
6598 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
6599 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
6600 if (!attr)
6601 return;
6602 tree args = TREE_VALUE (attr);
6603 if (args)
6604 args = TREE_VALUE (args);
6605 auto_diagnostic_group d;
6606 auto_urlify_attributes sentinel;
6607 int warned;
6608 if (args)
6609 warned = warning_at (loc, OPT_Wunused_result,
6610 "ignoring return value of type %qT, declared "
6611 "with attribute %<nodiscard%>: %E",
6612 rettype, args);
6613 else
6614 warned = warning_at (loc, OPT_Wunused_result,
6615 "ignoring return value of type %qT, declared "
6616 "with attribute %<nodiscard%>", rettype);
6617 if (warned)
6618 {
6619 if (TREE_CODE (fn) == ADDR_EXPR)
6620 {
6621 fn = TREE_OPERAND (fn, 0);
6622 if (TREE_CODE (fn) == FUNCTION_DECL)
6623 inform (DECL_SOURCE_LOCATION (fn),
6624 "in call to %qD, declared here", fn);
6625 }
6626 }
6627 }
6628 }
6629
6630 /* Return a compound expression that performs two expressions and
6631 returns the value of the second of them.
6632
6633 LOC is the location of the COMPOUND_EXPR. */
6634
6635 tree
6636 build_compound_expr (location_t loc, tree expr1, tree expr2)
6637 {
6638 bool expr1_int_operands, expr2_int_operands;
6639 tree eptype = NULL_TREE;
6640 tree ret;
6641
6642 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
6643 if (expr1_int_operands)
6644 expr1 = remove_c_maybe_const_expr (expr1);
6645 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
6646 if (expr2_int_operands)
6647 expr2 = remove_c_maybe_const_expr (expr2);
6648
6649 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
6650 expr1 = TREE_OPERAND (expr1, 0);
6651 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
6652 {
6653 eptype = TREE_TYPE (expr2);
6654 expr2 = TREE_OPERAND (expr2, 0);
6655 }
6656
6657 if (!TREE_SIDE_EFFECTS (expr1))
6658 {
6659 /* The left-hand operand of a comma expression is like an expression
6660 statement: with -Wunused, we should warn if it doesn't have
6661 any side-effects, unless it was explicitly cast to (void). */
6662 if (warn_unused_value)
6663 {
6664 if (VOID_TYPE_P (TREE_TYPE (expr1))
6665 && CONVERT_EXPR_P (expr1))
6666 ; /* (void) a, b */
6667 else if (VOID_TYPE_P (TREE_TYPE (expr1))
6668 && TREE_CODE (expr1) == COMPOUND_EXPR
6669 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
6670 ; /* (void) a, (void) b, c */
6671 else
6672 warning_at (loc, OPT_Wunused_value,
6673 "left-hand operand of comma expression has no effect");
6674 }
6675 }
6676 else if (TREE_CODE (expr1) == COMPOUND_EXPR
6677 && warn_unused_value)
6678 {
6679 tree r = expr1;
6680 location_t cloc = loc;
6681 while (TREE_CODE (r) == COMPOUND_EXPR)
6682 {
6683 if (EXPR_HAS_LOCATION (r))
6684 cloc = EXPR_LOCATION (r);
6685 r = TREE_OPERAND (r, 1);
6686 }
6687 if (!TREE_SIDE_EFFECTS (r)
6688 && !VOID_TYPE_P (TREE_TYPE (r))
6689 && !CONVERT_EXPR_P (r))
6690 warning_at (cloc, OPT_Wunused_value,
6691 "right-hand operand of comma expression has no effect");
6692 }
6693
6694 /* With -Wunused, we should also warn if the left-hand operand does have
6695 side-effects, but computes a value which is not used. For example, in
6696 `foo() + bar(), baz()' the result of the `+' operator is not used,
6697 so we should issue a warning. */
6698 else if (warn_unused_value)
6699 warn_if_unused_value (expr1, loc);
6700
6701 maybe_warn_nodiscard (loc, expr1);
6702
6703 if (expr2 == error_mark_node)
6704 return error_mark_node;
6705
6706 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
6707
6708 if (flag_isoc99
6709 && expr1_int_operands
6710 && expr2_int_operands)
6711 ret = note_integer_operands (ret);
6712
6713 if (eptype)
6714 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
6715
6716 protected_set_expr_location (ret, loc);
6717 return ret;
6718 }
6719
6720 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
6721 which we are casting. OTYPE is the type of the expression being
6722 cast. Both TYPE and OTYPE are pointer types. LOC is the location
6723 of the cast. -Wcast-qual appeared on the command line. Named
6724 address space qualifiers are not handled here, because they result
6725 in different warnings. */
6726
6727 static void
6728 handle_warn_cast_qual (location_t loc, tree type, tree otype)
6729 {
6730 tree in_type = type;
6731 tree in_otype = otype;
6732 int added = 0;
6733 int discarded = 0;
6734 bool is_const;
6735
6736 /* Check that the qualifiers on IN_TYPE are a superset of the
6737 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
6738 nodes is uninteresting and we stop as soon as we hit a
6739 non-POINTER_TYPE node on either type. */
6740 do
6741 {
6742 in_otype = TREE_TYPE (in_otype);
6743 in_type = TREE_TYPE (in_type);
6744
6745 /* GNU C allows cv-qualified function types. 'const' means the
6746 function is very pure, 'volatile' means it can't return. We
6747 need to warn when such qualifiers are added, not when they're
6748 taken away. */
6749 if (TREE_CODE (in_otype) == FUNCTION_TYPE
6750 && TREE_CODE (in_type) == FUNCTION_TYPE)
6751 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
6752 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
6753 else
6754 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
6755 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
6756 }
6757 while (TREE_CODE (in_type) == POINTER_TYPE
6758 && TREE_CODE (in_otype) == POINTER_TYPE);
6759
6760 if (added)
6761 warning_at (loc, OPT_Wcast_qual,
6762 "cast adds %q#v qualifier to function type", added);
6763
6764 if (discarded)
6765 /* There are qualifiers present in IN_OTYPE that are not present
6766 in IN_TYPE. */
6767 warning_at (loc, OPT_Wcast_qual,
6768 "cast discards %qv qualifier from pointer target type",
6769 discarded);
6770
6771 if (added || discarded)
6772 return;
6773
6774 /* A cast from **T to const **T is unsafe, because it can cause a
6775 const value to be changed with no additional warning. We only
6776 issue this warning if T is the same on both sides, and we only
6777 issue the warning if there are the same number of pointers on
6778 both sides, as otherwise the cast is clearly unsafe anyhow. A
6779 cast is unsafe when a qualifier is added at one level and const
6780 is not present at all outer levels.
6781
6782 To issue this warning, we check at each level whether the cast
6783 adds new qualifiers not already seen. We don't need to special
6784 case function types, as they won't have the same
6785 TYPE_MAIN_VARIANT. */
6786
6787 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
6788 return;
6789 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
6790 return;
6791
6792 in_type = type;
6793 in_otype = otype;
6794 is_const = TYPE_READONLY (TREE_TYPE (in_type));
6795 do
6796 {
6797 in_type = TREE_TYPE (in_type);
6798 in_otype = TREE_TYPE (in_otype);
6799 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
6800 && !is_const)
6801 {
6802 warning_at (loc, OPT_Wcast_qual,
6803 "to be safe all intermediate pointers in cast from "
6804 "%qT to %qT must be %<const%> qualified",
6805 otype, type);
6806 break;
6807 }
6808 if (is_const)
6809 is_const = TYPE_READONLY (in_type);
6810 }
6811 while (TREE_CODE (in_type) == POINTER_TYPE);
6812 }
6813
6814 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
6815
6816 static bool
6817 c_safe_arg_type_equiv_p (tree t1, tree t2)
6818 {
6819 if (error_operand_p (t1) || error_operand_p (t2))
6820 return true;
6821
6822 t1 = TYPE_MAIN_VARIANT (t1);
6823 t2 = TYPE_MAIN_VARIANT (t2);
6824
6825 if (TREE_CODE (t1) == POINTER_TYPE
6826 && TREE_CODE (t2) == POINTER_TYPE)
6827 return true;
6828
6829 /* The signedness of the parameter matters only when an integral
6830 type smaller than int is promoted to int, otherwise only the
6831 precision of the parameter matters.
6832 This check should make sure that the callee does not see
6833 undefined values in argument registers. */
6834 if (INTEGRAL_TYPE_P (t1)
6835 && INTEGRAL_TYPE_P (t2)
6836 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
6837 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
6838 || !targetm.calls.promote_prototypes (NULL_TREE)
6839 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
6840 return true;
6841
6842 return comptypes (t1, t2);
6843 }
6844
6845 /* Check if a type cast between two function types can be considered safe. */
6846
6847 static bool
6848 c_safe_function_type_cast_p (tree t1, tree t2)
6849 {
6850 if (TREE_TYPE (t1) == void_type_node &&
6851 TYPE_ARG_TYPES (t1) == void_list_node)
6852 return true;
6853
6854 if (TREE_TYPE (t2) == void_type_node &&
6855 TYPE_ARG_TYPES (t2) == void_list_node)
6856 return true;
6857
6858 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
6859 return false;
6860
6861 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
6862 t1 && t2;
6863 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6864 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
6865 return false;
6866
6867 return true;
6868 }
6869
6870 /* Build an expression representing a cast to type TYPE of expression EXPR.
6871 LOC is the location of the cast-- typically the open paren of the cast. */
6872
6873 tree
6874 build_c_cast (location_t loc, tree type, tree expr)
6875 {
6876 tree value;
6877
6878 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
6879
6880 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
6881 expr = TREE_OPERAND (expr, 0);
6882
6883 value = expr;
6884 if (int_operands)
6885 value = remove_c_maybe_const_expr (value);
6886
6887 if (type == error_mark_node || expr == error_mark_node)
6888 return error_mark_node;
6889
6890 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
6891 only in <protocol> qualifications. But when constructing cast expressions,
6892 the protocols do matter and must be kept around. */
6893 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
6894 return build1 (NOP_EXPR, type, expr);
6895
6896 type = TYPE_MAIN_VARIANT (type);
6897
6898 if (TREE_CODE (type) == ARRAY_TYPE)
6899 {
6900 error_at (loc, "cast specifies array type");
6901 return error_mark_node;
6902 }
6903
6904 if (TREE_CODE (type) == FUNCTION_TYPE)
6905 {
6906 error_at (loc, "cast specifies function type");
6907 return error_mark_node;
6908 }
6909
6910 if (!VOID_TYPE_P (type))
6911 {
6912 value = require_complete_type (loc, value);
6913 if (value == error_mark_node)
6914 return error_mark_node;
6915 }
6916
6917 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
6918 {
6919 if (RECORD_OR_UNION_TYPE_P (type)
6920 && pedwarn (loc, OPT_Wpedantic,
6921 "ISO C forbids casting nonscalar to the same type"))
6922 ;
6923 else if (warn_useless_cast)
6924 warning_at (loc, OPT_Wuseless_cast,
6925 "useless cast to type %qT", type);
6926
6927 /* Convert to remove any qualifiers from VALUE's type. */
6928 value = convert (type, value);
6929 }
6930 else if (TREE_CODE (type) == UNION_TYPE)
6931 {
6932 tree field;
6933
6934 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6935 if (TREE_TYPE (field) != error_mark_node
6936 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
6937 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
6938 break;
6939
6940 if (field)
6941 {
6942 tree t;
6943 bool maybe_const = true;
6944
6945 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
6946 t = c_fully_fold (value, false, &maybe_const);
6947 t = build_constructor_single (type, field, t);
6948 if (!maybe_const)
6949 t = c_wrap_maybe_const (t, true);
6950 t = digest_init (loc, field, type, t,
6951 NULL_TREE, false, false, false, true, false, false);
6952 TREE_CONSTANT (t) = TREE_CONSTANT (value);
6953 return t;
6954 }
6955 error_at (loc, "cast to union type from type not present in union");
6956 return error_mark_node;
6957 }
6958 else
6959 {
6960 tree otype, ovalue;
6961
6962 if (type == void_type_node)
6963 {
6964 tree t = build1 (CONVERT_EXPR, type, value);
6965 SET_EXPR_LOCATION (t, loc);
6966 return t;
6967 }
6968
6969 otype = TREE_TYPE (value);
6970
6971 /* Optionally warn about potentially worrisome casts. */
6972 if (warn_cast_qual
6973 && TREE_CODE (type) == POINTER_TYPE
6974 && TREE_CODE (otype) == POINTER_TYPE)
6975 handle_warn_cast_qual (loc, type, otype);
6976
6977 /* Warn about conversions between pointers to disjoint
6978 address spaces. */
6979 if (TREE_CODE (type) == POINTER_TYPE
6980 && TREE_CODE (otype) == POINTER_TYPE
6981 && !null_pointer_constant_p (value))
6982 {
6983 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6984 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6985 addr_space_t as_common;
6986
6987 if (!addr_space_superset (as_to, as_from, &as_common))
6988 {
6989 if (ADDR_SPACE_GENERIC_P (as_from))
6990 warning_at (loc, 0, "cast to %qs address space pointer "
6991 "from disjoint generic address space pointer",
6992 c_addr_space_name (as_to));
6993
6994 else if (ADDR_SPACE_GENERIC_P (as_to))
6995 warning_at (loc, 0, "cast to generic address space pointer "
6996 "from disjoint %qs address space pointer",
6997 c_addr_space_name (as_from));
6998
6999 else
7000 warning_at (loc, 0, "cast to %qs address space pointer "
7001 "from disjoint %qs address space pointer",
7002 c_addr_space_name (as_to),
7003 c_addr_space_name (as_from));
7004 }
7005
7006 /* Warn of new allocations that are not big enough for the target
7007 type. */
7008 if (warn_alloc_size && TREE_CODE (value) == CALL_EXPR)
7009 if (tree fndecl = get_callee_fndecl (value))
7010 if (DECL_IS_MALLOC (fndecl))
7011 {
7012 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
7013 tree alloc_size = lookup_attribute ("alloc_size", attrs);
7014 if (alloc_size)
7015 warn_for_alloc_size (loc, TREE_TYPE (type), value,
7016 alloc_size);
7017 }
7018 }
7019
7020 /* Warn about possible alignment problems. */
7021 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7022 && TREE_CODE (type) == POINTER_TYPE
7023 && TREE_CODE (otype) == POINTER_TYPE
7024 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
7025 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7026 /* Don't warn about opaque types, where the actual alignment
7027 restriction is unknown. */
7028 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
7029 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
7030 && min_align_of_type (TREE_TYPE (type))
7031 > min_align_of_type (TREE_TYPE (otype)))
7032 warning_at (loc, OPT_Wcast_align,
7033 "cast increases required alignment of target type");
7034
7035 if ((TREE_CODE (type) == INTEGER_TYPE
7036 || TREE_CODE (type) == BITINT_TYPE)
7037 && TREE_CODE (otype) == POINTER_TYPE
7038 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
7039 /* Unlike conversion of integers to pointers, where the
7040 warning is disabled for converting constants because
7041 of cases such as SIG_*, warn about converting constant
7042 pointers to integers. In some cases it may cause unwanted
7043 sign extension, and a warning is appropriate. */
7044 warning_at (loc, OPT_Wpointer_to_int_cast,
7045 "cast from pointer to integer of different size");
7046
7047 if (TREE_CODE (value) == CALL_EXPR
7048 && TREE_CODE (type) != TREE_CODE (otype))
7049 warning_at (loc, OPT_Wbad_function_cast,
7050 "cast from function call of type %qT "
7051 "to non-matching type %qT", otype, type);
7052
7053 if (TREE_CODE (type) == POINTER_TYPE
7054 && (TREE_CODE (otype) == INTEGER_TYPE
7055 || TREE_CODE (otype) == BITINT_TYPE)
7056 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
7057 /* Don't warn about converting any constant. */
7058 && !TREE_CONSTANT (value))
7059 warning_at (loc,
7060 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
7061 "of different size");
7062
7063 if (warn_strict_aliasing <= 2)
7064 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
7065
7066 /* If pedantic, warn for conversions between function and object
7067 pointer types, except for converting a null pointer constant
7068 to function pointer type. */
7069 if (pedantic
7070 && TREE_CODE (type) == POINTER_TYPE
7071 && TREE_CODE (otype) == POINTER_TYPE
7072 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7073 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7074 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7075 "conversion of function pointer to object pointer type");
7076
7077 if (pedantic
7078 && TREE_CODE (type) == POINTER_TYPE
7079 && TREE_CODE (otype) == POINTER_TYPE
7080 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7081 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7082 && !null_pointer_constant_p (value))
7083 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7084 "conversion of object pointer to function pointer type");
7085
7086 if (TREE_CODE (type) == POINTER_TYPE
7087 && TREE_CODE (otype) == POINTER_TYPE
7088 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7089 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7090 && !c_safe_function_type_cast_p (TREE_TYPE (type),
7091 TREE_TYPE (otype)))
7092 warning_at (loc, OPT_Wcast_function_type,
7093 "cast between incompatible function types"
7094 " from %qT to %qT", otype, type);
7095
7096 ovalue = value;
7097 /* If converting to boolean a value with integer operands that
7098 is not itself represented as an INTEGER_CST, the call below
7099 to note_integer_operands may insert a C_MAYBE_CONST_EXPR, but
7100 build_binary_op as called by c_common_truthvalue_conversion
7101 may also insert a C_MAYBE_CONST_EXPR to indicate that a
7102 subexpression has been fully folded. To avoid nested
7103 C_MAYBE_CONST_EXPR, ensure that
7104 c_objc_common_truthvalue_conversion receives an argument
7105 properly marked as having integer operands in that case. */
7106 if (int_operands
7107 && TREE_CODE (value) != INTEGER_CST
7108 && (TREE_CODE (type) == BOOLEAN_TYPE
7109 || (TREE_CODE (type) == ENUMERAL_TYPE
7110 && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
7111 && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) == BOOLEAN_TYPE)))
7112 value = note_integer_operands (value);
7113 value = convert (type, value);
7114
7115 /* Ignore any integer overflow caused by the cast. */
7116 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
7117 {
7118 if (TREE_OVERFLOW_P (ovalue))
7119 {
7120 if (!TREE_OVERFLOW (value))
7121 {
7122 /* Avoid clobbering a shared constant. */
7123 value = copy_node (value);
7124 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
7125 }
7126 }
7127 else if (TREE_OVERFLOW (value))
7128 /* Reset VALUE's overflow flags, ensuring constant sharing. */
7129 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
7130 }
7131 }
7132
7133 /* Don't let a cast be an lvalue. */
7134 if (lvalue_p (value))
7135 value = non_lvalue_loc (loc, value);
7136
7137 /* Don't allow the results of casting to floating-point or complex
7138 types be confused with actual constants, or casts involving
7139 integer and pointer types other than direct integer-to-integer
7140 and integer-to-pointer be confused with integer constant
7141 expressions and null pointer constants. */
7142 if (TREE_CODE (value) == REAL_CST
7143 || TREE_CODE (value) == COMPLEX_CST
7144 || (TREE_CODE (value) == INTEGER_CST
7145 && !((TREE_CODE (expr) == INTEGER_CST
7146 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
7147 || TREE_CODE (expr) == REAL_CST
7148 || TREE_CODE (expr) == COMPLEX_CST)))
7149 value = build1 (NOP_EXPR, type, value);
7150
7151 /* If the expression has integer operands and so can occur in an
7152 unevaluated part of an integer constant expression, ensure the
7153 return value reflects this. */
7154 if (int_operands
7155 && INTEGRAL_TYPE_P (type)
7156 && value != error_mark_node
7157 && !EXPR_INT_CONST_OPERANDS (value))
7158 value = note_integer_operands (value);
7159
7160 protected_set_expr_location (value, loc);
7161 return value;
7162 }
7163
7164 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
7165 location of the open paren of the cast, or the position of the cast
7166 expr. */
7167 tree
7168 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
7169 {
7170 tree type;
7171 tree type_expr = NULL_TREE;
7172 bool type_expr_const = true;
7173 tree ret;
7174 int saved_wsp = warn_strict_prototypes;
7175
7176 /* This avoids warnings about unprototyped casts on
7177 integers. E.g. "#define SIG_DFL (void(*)())0". */
7178 if (TREE_CODE (expr) == INTEGER_CST)
7179 warn_strict_prototypes = 0;
7180 type = groktypename (type_name, &type_expr, &type_expr_const);
7181 warn_strict_prototypes = saved_wsp;
7182
7183 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
7184 && reject_gcc_builtin (expr))
7185 return error_mark_node;
7186
7187 ret = build_c_cast (loc, type, expr);
7188 if (ret == error_mark_node)
7189 return error_mark_node;
7190
7191 if (type_expr)
7192 {
7193 bool inner_expr_const = true;
7194 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
7195 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
7196 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
7197 && inner_expr_const);
7198 SET_EXPR_LOCATION (ret, loc);
7199 }
7200
7201 if (!EXPR_HAS_LOCATION (ret))
7202 protected_set_expr_location (ret, loc);
7203
7204 /* C++ does not permits types to be defined in a cast, but it
7205 allows references to incomplete types. */
7206 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
7207 warning_at (loc, OPT_Wc___compat,
7208 "defining a type in a cast is invalid in C++");
7209
7210 return ret;
7211 }
7212
7213 /* Build an assignment expression of lvalue LHS from value RHS.
7214 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
7215 may differ from TREE_TYPE (LHS) for an enum bitfield.
7216 MODIFYCODE is the code for a binary operator that we use
7217 to combine the old value of LHS with RHS to get the new value.
7218 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7219 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
7220 which may differ from TREE_TYPE (RHS) for an enum value.
7221
7222 LOCATION is the location of the MODIFYCODE operator.
7223 RHS_LOC is the location of the RHS. */
7224
7225 tree
7226 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
7227 enum tree_code modifycode,
7228 location_t rhs_loc, tree rhs, tree rhs_origtype)
7229 {
7230 tree result;
7231 tree newrhs;
7232 tree rhseval = NULL_TREE;
7233 tree lhstype = TREE_TYPE (lhs);
7234 tree olhstype = lhstype;
7235 bool npc;
7236 bool is_atomic_op;
7237
7238 /* Types that aren't fully specified cannot be used in assignments. */
7239 lhs = require_complete_type (location, lhs);
7240 rhs = require_complete_type (location, rhs);
7241
7242 /* Avoid duplicate error messages from operands that had errors. */
7243 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
7244 return error_mark_node;
7245
7246 /* Ensure an error for assigning a non-lvalue array to an array in
7247 C90. */
7248 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7249 {
7250 error_at (location, "assignment to expression with array type");
7251 return error_mark_node;
7252 }
7253
7254 /* For ObjC properties, defer this check. */
7255 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
7256 return error_mark_node;
7257
7258 is_atomic_op = really_atomic_lvalue (lhs);
7259
7260 newrhs = rhs;
7261
7262 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
7263 {
7264 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
7265 lhs_origtype, modifycode, rhs_loc, rhs,
7266 rhs_origtype);
7267 if (inner == error_mark_node)
7268 return error_mark_node;
7269 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
7270 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
7271 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
7272 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
7273 protected_set_expr_location (result, location);
7274 return result;
7275 }
7276
7277 /* If a binary op has been requested, combine the old LHS value with the RHS
7278 producing the value we should actually store into the LHS. */
7279
7280 if (modifycode != NOP_EXPR)
7281 {
7282 lhs = c_fully_fold (lhs, false, NULL, true);
7283 lhs = stabilize_reference (lhs);
7284
7285 /* Construct the RHS for any non-atomic compound assignemnt. */
7286 if (!is_atomic_op)
7287 {
7288 /* If in LHS op= RHS the RHS has side-effects, ensure they
7289 are preevaluated before the rest of the assignment expression's
7290 side-effects, because RHS could contain e.g. function calls
7291 that modify LHS. */
7292 if (TREE_SIDE_EFFECTS (rhs))
7293 {
7294 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7295 newrhs = save_expr (TREE_OPERAND (rhs, 0));
7296 else
7297 newrhs = save_expr (rhs);
7298 rhseval = newrhs;
7299 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7300 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
7301 newrhs);
7302 }
7303 newrhs = build_binary_op (location,
7304 modifycode, lhs, newrhs, true);
7305
7306 /* The original type of the right hand side is no longer
7307 meaningful. */
7308 rhs_origtype = NULL_TREE;
7309 }
7310 }
7311
7312 if (c_dialect_objc ())
7313 {
7314 /* Check if we are modifying an Objective-C property reference;
7315 if so, we need to generate setter calls. */
7316 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7317 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
7318 else
7319 result = objc_maybe_build_modify_expr (lhs, newrhs);
7320 if (result)
7321 goto return_result;
7322
7323 /* Else, do the check that we postponed for Objective-C. */
7324 if (!lvalue_or_else (location, lhs, lv_assign))
7325 return error_mark_node;
7326 }
7327
7328 /* Give an error for storing in something that is 'const'. */
7329
7330 if (TYPE_READONLY (lhstype)
7331 || (RECORD_OR_UNION_TYPE_P (lhstype)
7332 && C_TYPE_FIELDS_READONLY (lhstype)))
7333 {
7334 readonly_error (location, lhs, lv_assign);
7335 return error_mark_node;
7336 }
7337 else if (TREE_READONLY (lhs))
7338 readonly_warning (lhs, lv_assign);
7339
7340 /* If storing into a structure or union member,
7341 it has probably been given type `int'.
7342 Compute the type that would go with
7343 the actual amount of storage the member occupies. */
7344
7345 if (TREE_CODE (lhs) == COMPONENT_REF
7346 && (TREE_CODE (lhstype) == INTEGER_TYPE
7347 || TREE_CODE (lhstype) == BOOLEAN_TYPE
7348 || SCALAR_FLOAT_TYPE_P (lhstype)
7349 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
7350 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
7351
7352 /* If storing in a field that is in actuality a short or narrower than one,
7353 we must store in the field in its actual type. */
7354
7355 if (lhstype != TREE_TYPE (lhs))
7356 {
7357 lhs = copy_node (lhs);
7358 TREE_TYPE (lhs) = lhstype;
7359 }
7360
7361 /* Issue -Wc++-compat warnings about an assignment to an enum type
7362 when LHS does not have its original type. This happens for,
7363 e.g., an enum bitfield in a struct. */
7364 if (warn_cxx_compat
7365 && lhs_origtype != NULL_TREE
7366 && lhs_origtype != lhstype
7367 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
7368 {
7369 tree checktype = (rhs_origtype != NULL_TREE
7370 ? rhs_origtype
7371 : TREE_TYPE (rhs));
7372 if (checktype != error_mark_node
7373 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
7374 || (is_atomic_op && modifycode != NOP_EXPR)))
7375 warning_at (location, OPT_Wc___compat,
7376 "enum conversion in assignment is invalid in C++");
7377 }
7378
7379 /* Remove qualifiers. */
7380 lhstype = c_build_qualified_type (lhstype, TYPE_UNQUALIFIED);
7381 olhstype = c_build_qualified_type (olhstype, TYPE_UNQUALIFIED);
7382
7383 /* Convert new value to destination type. Fold it first, then
7384 restore any excess precision information, for the sake of
7385 conversion warnings. */
7386
7387 if (!(is_atomic_op && modifycode != NOP_EXPR))
7388 {
7389 tree rhs_semantic_type = NULL_TREE;
7390 if (!c_in_omp_for)
7391 {
7392 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7393 {
7394 rhs_semantic_type = TREE_TYPE (newrhs);
7395 newrhs = TREE_OPERAND (newrhs, 0);
7396 }
7397 npc = null_pointer_constant_p (newrhs);
7398 newrhs = c_fully_fold (newrhs, false, NULL);
7399 if (rhs_semantic_type)
7400 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
7401 }
7402 else
7403 npc = null_pointer_constant_p (newrhs);
7404 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
7405 rhs_origtype, ic_assign, npc,
7406 NULL_TREE, NULL_TREE, 0);
7407 if (TREE_CODE (newrhs) == ERROR_MARK)
7408 return error_mark_node;
7409 }
7410
7411 /* Emit ObjC write barrier, if necessary. */
7412 if (c_dialect_objc () && flag_objc_gc)
7413 {
7414 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7415 if (result)
7416 {
7417 protected_set_expr_location (result, location);
7418 goto return_result;
7419 }
7420 }
7421
7422 /* Scan operands. */
7423
7424 if (is_atomic_op)
7425 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
7426 else
7427 {
7428 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
7429 TREE_SIDE_EFFECTS (result) = 1;
7430 protected_set_expr_location (result, location);
7431 }
7432
7433 /* If we got the LHS in a different type for storing in,
7434 convert the result back to the nominal type of LHS
7435 so that the value we return always has the same type
7436 as the LHS argument. */
7437
7438 if (olhstype == TREE_TYPE (result))
7439 goto return_result;
7440
7441 result = convert_for_assignment (location, rhs_loc, olhstype, result,
7442 rhs_origtype, ic_assign, false, NULL_TREE,
7443 NULL_TREE, 0);
7444 protected_set_expr_location (result, location);
7445
7446 return_result:
7447 if (rhseval)
7448 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
7449 return result;
7450 }
7451
7452 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
7453 This is used to implement -fplan9-extensions. */
7454
7455 static bool
7456 find_anonymous_field_with_type (tree struct_type, tree type)
7457 {
7458 tree field;
7459 bool found;
7460
7461 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
7462 found = false;
7463 for (field = TYPE_FIELDS (struct_type);
7464 field != NULL_TREE;
7465 field = TREE_CHAIN (field))
7466 {
7467 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
7468 ? c_build_qualified_type (TREE_TYPE (field),
7469 TYPE_QUAL_ATOMIC)
7470 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
7471 if (DECL_NAME (field) == NULL
7472 && comptypes (type, fieldtype))
7473 {
7474 if (found)
7475 return false;
7476 found = true;
7477 }
7478 else if (DECL_NAME (field) == NULL
7479 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
7480 && find_anonymous_field_with_type (TREE_TYPE (field), type))
7481 {
7482 if (found)
7483 return false;
7484 found = true;
7485 }
7486 }
7487 return found;
7488 }
7489
7490 /* RHS is an expression whose type is pointer to struct. If there is
7491 an anonymous field in RHS with type TYPE, then return a pointer to
7492 that field in RHS. This is used with -fplan9-extensions. This
7493 returns NULL if no conversion could be found. */
7494
7495 static tree
7496 convert_to_anonymous_field (location_t location, tree type, tree rhs)
7497 {
7498 tree rhs_struct_type, lhs_main_type;
7499 tree field, found_field;
7500 bool found_sub_field;
7501 tree ret;
7502
7503 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
7504 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
7505 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
7506
7507 gcc_assert (POINTER_TYPE_P (type));
7508 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
7509 ? c_build_qualified_type (TREE_TYPE (type),
7510 TYPE_QUAL_ATOMIC)
7511 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7512
7513 found_field = NULL_TREE;
7514 found_sub_field = false;
7515 for (field = TYPE_FIELDS (rhs_struct_type);
7516 field != NULL_TREE;
7517 field = TREE_CHAIN (field))
7518 {
7519 if (DECL_NAME (field) != NULL_TREE
7520 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
7521 continue;
7522 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
7523 ? c_build_qualified_type (TREE_TYPE (field),
7524 TYPE_QUAL_ATOMIC)
7525 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
7526 if (comptypes (lhs_main_type, fieldtype))
7527 {
7528 if (found_field != NULL_TREE)
7529 return NULL_TREE;
7530 found_field = field;
7531 }
7532 else if (find_anonymous_field_with_type (TREE_TYPE (field),
7533 lhs_main_type))
7534 {
7535 if (found_field != NULL_TREE)
7536 return NULL_TREE;
7537 found_field = field;
7538 found_sub_field = true;
7539 }
7540 }
7541
7542 if (found_field == NULL_TREE)
7543 return NULL_TREE;
7544
7545 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
7546 build_fold_indirect_ref (rhs), found_field,
7547 NULL_TREE);
7548 ret = build_fold_addr_expr_loc (location, ret);
7549
7550 if (found_sub_field)
7551 {
7552 ret = convert_to_anonymous_field (location, type, ret);
7553 gcc_assert (ret != NULL_TREE);
7554 }
7555
7556 return ret;
7557 }
7558
7559 /* Issue an error message for a bad initializer component.
7560 GMSGID identifies the message.
7561 The component name is taken from the spelling stack. */
7562
7563 static void ATTRIBUTE_GCC_DIAG (2,0)
7564 error_init (location_t loc, const char *gmsgid, ...)
7565 {
7566 char *ofwhat;
7567
7568 auto_diagnostic_group d;
7569
7570 /* The gmsgid may be a format string with %< and %>. */
7571 va_list ap;
7572 va_start (ap, gmsgid);
7573 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
7574 va_end (ap);
7575
7576 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
7577 if (*ofwhat && warned)
7578 inform (loc, "(near initialization for %qs)", ofwhat);
7579 }
7580
7581 /* Used to implement pedwarn_init and permerror_init. */
7582
7583 static bool ATTRIBUTE_GCC_DIAG (3,0)
7584 pedwarn_permerror_init (location_t loc, int opt, const char *gmsgid,
7585 va_list *ap, diagnostic_t kind)
7586 {
7587 /* Use the location where a macro was expanded rather than where
7588 it was defined to make sure macros defined in system headers
7589 but used incorrectly elsewhere are diagnosed. */
7590 location_t exploc = expansion_point_location_if_in_system_header (loc);
7591 auto_diagnostic_group d;
7592 bool warned = emit_diagnostic_valist (kind, exploc, opt, gmsgid, ap);
7593 if (warned)
7594 {
7595 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
7596 if (*ofwhat)
7597 inform (exploc, "(near initialization for %qs)", ofwhat);
7598 }
7599 return warned;
7600 }
7601
7602 /* Issue a pedantic warning for a bad initializer component. OPT is
7603 the option OPT_* (from options.h) controlling this warning or 0 if
7604 it is unconditionally given. GMSGID identifies the message. The
7605 component name is taken from the spelling stack. */
7606
7607 static bool ATTRIBUTE_GCC_DIAG (3,0)
7608 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
7609 {
7610 va_list ap;
7611 va_start (ap, gmsgid);
7612 bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap, DK_PEDWARN);
7613 va_end (ap);
7614 return warned;
7615 }
7616
7617 /* Like pedwarn_init, but issue a permerror. */
7618
7619 static bool ATTRIBUTE_GCC_DIAG (3,0)
7620 permerror_init (location_t loc, int opt, const char *gmsgid, ...)
7621 {
7622 va_list ap;
7623 va_start (ap, gmsgid);
7624 bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap, DK_PERMERROR);
7625 va_end (ap);
7626 return warned;
7627 }
7628
7629 /* Issue a warning for a bad initializer component.
7630
7631 OPT is the OPT_W* value corresponding to the warning option that
7632 controls this warning. GMSGID identifies the message. The
7633 component name is taken from the spelling stack. */
7634
7635 static void
7636 warning_init (location_t loc, int opt, const char *gmsgid)
7637 {
7638 char *ofwhat;
7639 bool warned;
7640
7641 auto_diagnostic_group d;
7642
7643 /* Use the location where a macro was expanded rather than where
7644 it was defined to make sure macros defined in system headers
7645 but used incorrectly elsewhere are diagnosed. */
7646 location_t exploc = expansion_point_location_if_in_system_header (loc);
7647
7648 /* The gmsgid may be a format string with %< and %>. */
7649 warned = warning_at (exploc, opt, gmsgid);
7650 if (warned)
7651 {
7652 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
7653 if (*ofwhat)
7654 inform (exploc, "(near initialization for %qs)", ofwhat);
7655 }
7656 }
7657
7658 /* If TYPE is an array type and EXPR is a parenthesized string
7659 constant, warn if pedantic that EXPR is being used to initialize an
7660 object of type TYPE. */
7661
7662 void
7663 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
7664 {
7665 if (pedantic
7666 && TREE_CODE (type) == ARRAY_TYPE
7667 && TREE_CODE (expr.value) == STRING_CST
7668 && expr.original_code != STRING_CST)
7669 pedwarn_init (loc, OPT_Wpedantic,
7670 "array initialized from parenthesized string constant");
7671 }
7672
7673 /* Attempt to locate the parameter with the given index within FNDECL,
7674 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
7675
7676 static location_t
7677 get_fndecl_argument_location (tree fndecl, int argnum)
7678 {
7679 int i;
7680 tree param;
7681
7682 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
7683 for (i = 0, param = DECL_ARGUMENTS (fndecl);
7684 i < argnum && param;
7685 i++, param = TREE_CHAIN (param))
7686 ;
7687
7688 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
7689 return DECL_SOURCE_LOCATION (FNDECL). */
7690 if (param == NULL)
7691 return DECL_SOURCE_LOCATION (fndecl);
7692
7693 return DECL_SOURCE_LOCATION (param);
7694 }
7695
7696 /* Issue a note about a mismatching argument for parameter PARMNUM
7697 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
7698 Attempt to issue the note at the pertinent parameter of the decl;
7699 failing that issue it at the location of FUNDECL; failing that
7700 issue it at PLOC.
7701 Use highlight_colors::actual for the ACTUAL_TYPE
7702 and highlight_colors::expected for EXPECTED_TYPE and the
7703 parameter of FUNDECL*/
7704
7705 static void
7706 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
7707 tree expected_type, tree actual_type)
7708 {
7709 location_t loc;
7710 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
7711 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
7712 else
7713 loc = ploc;
7714
7715 gcc_rich_location richloc (loc, nullptr, highlight_colors::expected);
7716
7717 pp_markup::element_expected_type elem_expected_type (expected_type);
7718 pp_markup::element_actual_type elem_actual_type (actual_type);
7719 inform (&richloc,
7720 "expected %e but argument is of type %e",
7721 &elem_expected_type, &elem_actual_type);
7722 }
7723
7724 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
7725 function FUNDECL declared without prototype to parameter PARMNUM of
7726 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
7727
7728 static void
7729 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
7730 tree parmtype, tree argtype)
7731 {
7732 tree_code parmcode = TREE_CODE (parmtype);
7733 tree_code argcode = TREE_CODE (argtype);
7734 tree promoted = c_type_promotes_to (argtype);
7735
7736 /* Avoid warning for enum arguments that promote to an integer type
7737 of the same size/mode. */
7738 if (parmcode == INTEGER_TYPE
7739 && argcode == ENUMERAL_TYPE
7740 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
7741 return;
7742
7743 if ((parmcode == argcode
7744 || (parmcode == INTEGER_TYPE
7745 && argcode == ENUMERAL_TYPE))
7746 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
7747 return;
7748
7749 /* This diagnoses even signed/unsigned mismatches. Those might be
7750 safe in many cases but GCC may emit suboptimal code for them so
7751 warning on those cases drives efficiency improvements. */
7752 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
7753 TYPE_MAIN_VARIANT (promoted) == argtype
7754 ? G_("%qD argument %d type is %qT where %qT is expected "
7755 "in a call to built-in function declared without "
7756 "prototype")
7757 : G_("%qD argument %d promotes to %qT where %qT is expected "
7758 "in a call to built-in function declared without "
7759 "prototype"),
7760 fundecl, parmnum, promoted, parmtype))
7761 inform (DECL_SOURCE_LOCATION (fundecl),
7762 "built-in %qD declared here",
7763 fundecl);
7764 }
7765
7766 /* Print a declaration in quotes, with the given highlight_color.
7767 Analogous to handler for %qD, but with a specific highlight color. */
7768
7769 class pp_element_quoted_decl : public pp_element
7770 {
7771 public:
7772 pp_element_quoted_decl (tree decl, const char *highlight_color)
7773 : m_decl (decl),
7774 m_highlight_color (highlight_color)
7775 {
7776 }
7777
7778 void add_to_phase_2 (pp_markup::context &ctxt) override
7779 {
7780 ctxt.begin_quote ();
7781 ctxt.begin_highlight_color (m_highlight_color);
7782
7783 print_decl (ctxt);
7784
7785 ctxt.end_highlight_color ();
7786 ctxt.end_quote ();
7787 }
7788
7789 void print_decl (pp_markup::context &ctxt)
7790 {
7791 pretty_printer *const pp = &ctxt.m_pp;
7792 pp->set_padding (pp_none);
7793 if (DECL_NAME (m_decl))
7794 pp_identifier (pp, lang_hooks.decl_printable_name (m_decl, 2));
7795 else
7796 pp_string (pp, _("({anonymous})"));
7797 }
7798
7799 private:
7800 tree m_decl;
7801 const char *m_highlight_color;
7802 };
7803
7804 /* If TYPE is from a typedef, issue a note showing the location
7805 to the user.
7806 Use HIGHLIGHT_COLOR as the highlight color. */
7807
7808 static void
7809 maybe_inform_typedef_location (tree type, const char *highlight_color)
7810 {
7811 if (!typedef_variant_p (type))
7812 return;
7813
7814 tree typedef_decl = TYPE_NAME (type);
7815 gcc_assert (TREE_CODE (typedef_decl) == TYPE_DECL);
7816 gcc_rich_location richloc (DECL_SOURCE_LOCATION (typedef_decl),
7817 nullptr, highlight_color);
7818 pp_element_quoted_decl e_typedef_decl (typedef_decl, highlight_color);
7819 inform (&richloc, "%e declared here", &e_typedef_decl);
7820 }
7821
7822 /* Convert value RHS to type TYPE as preparation for an assignment to
7823 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
7824 original type of RHS; this differs from TREE_TYPE (RHS) for enum
7825 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
7826 constant before any folding.
7827 The real work of conversion is done by `convert'.
7828 The purpose of this function is to generate error messages
7829 for assignments that are not allowed in C.
7830 ERRTYPE says whether it is argument passing, assignment,
7831 initialization or return.
7832
7833 In the following example, '~' denotes where EXPR_LOC and '^' where
7834 LOCATION point to:
7835
7836 f (var); [ic_argpass]
7837 ^ ~~~
7838 x = var; [ic_assign]
7839 ^ ~~~;
7840 int x = var; [ic_init]
7841 ^^^
7842 return x; [ic_return]
7843 ^
7844
7845 FUNCTION is a tree for the function being called.
7846 PARMNUM is the number of the argument, for printing in error messages.
7847 WARNOPT may be set to a warning option to issue the corresponding warning
7848 rather than an error for invalid conversions. Used for calls to built-in
7849 functions declared without a prototype. */
7850
7851 static tree
7852 convert_for_assignment (location_t location, location_t expr_loc, tree type,
7853 tree rhs, tree origtype, enum impl_conv errtype,
7854 bool null_pointer_constant, tree fundecl,
7855 tree function, int parmnum, int warnopt /* = 0 */)
7856 {
7857 enum tree_code codel = TREE_CODE (type);
7858 tree orig_rhs = rhs;
7859 tree rhstype;
7860 enum tree_code coder;
7861 tree rname = NULL_TREE;
7862 bool objc_ok = false;
7863
7864 /* Use the expansion point location to handle cases such as user's
7865 function returning a wrong-type macro defined in a system header. */
7866 location = expansion_point_location_if_in_system_header (location);
7867
7868 if (errtype == ic_argpass)
7869 {
7870 tree selector;
7871 /* Change pointer to function to the function itself for
7872 diagnostics. */
7873 if (TREE_CODE (function) == ADDR_EXPR
7874 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
7875 function = TREE_OPERAND (function, 0);
7876
7877 /* Handle an ObjC selector specially for diagnostics. */
7878 selector = objc_message_selector ();
7879 rname = function;
7880 if (selector && parmnum > 2)
7881 {
7882 rname = selector;
7883 parmnum -= 2;
7884 }
7885 }
7886
7887 /* This macro is used to emit diagnostics to ensure that all format
7888 strings are complete sentences, visible to gettext and checked at
7889 compile time. */
7890 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
7891 do { \
7892 switch (errtype) \
7893 { \
7894 case ic_argpass: \
7895 { \
7896 auto_diagnostic_group d; \
7897 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
7898 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
7899 } \
7900 break; \
7901 case ic_assign: \
7902 pedwarn (LOCATION, OPT, AS); \
7903 break; \
7904 case ic_init: \
7905 case ic_init_const: \
7906 pedwarn_init (LOCATION, OPT, IN); \
7907 break; \
7908 case ic_return: \
7909 pedwarn (LOCATION, OPT, RE); \
7910 break; \
7911 default: \
7912 gcc_unreachable (); \
7913 } \
7914 } while (0)
7915
7916 /* This macro is used to emit diagnostics to ensure that all format
7917 strings are complete sentences, visible to gettext and checked at
7918 compile time. It can be called with 'pedwarn' or 'warning_at'. */
7919 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
7920 do { \
7921 switch (errtype) \
7922 { \
7923 case ic_argpass: \
7924 { \
7925 auto_diagnostic_group d; \
7926 if (PEDWARN) { \
7927 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
7928 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
7929 } else { \
7930 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
7931 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
7932 } \
7933 } \
7934 break; \
7935 case ic_assign: \
7936 if (PEDWARN) \
7937 pedwarn (LOCATION, OPT, AS, QUALS); \
7938 else \
7939 warning_at (LOCATION, OPT, AS, QUALS); \
7940 break; \
7941 case ic_init: \
7942 case ic_init_const: \
7943 if (PEDWARN) \
7944 pedwarn (LOCATION, OPT, IN, QUALS); \
7945 else \
7946 warning_at (LOCATION, OPT, IN, QUALS); \
7947 break; \
7948 case ic_return: \
7949 if (PEDWARN) \
7950 pedwarn (LOCATION, OPT, RE, QUALS); \
7951 else \
7952 warning_at (LOCATION, OPT, RE, QUALS); \
7953 break; \
7954 default: \
7955 gcc_unreachable (); \
7956 } \
7957 } while (0)
7958
7959 /* This macro is used to emit diagnostics to ensure that all format
7960 strings are complete sentences, visible to gettext and checked at
7961 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
7962 extra parameter to enumerate qualifiers. */
7963 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
7964 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
7965
7966
7967 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7968 rhs = TREE_OPERAND (rhs, 0);
7969
7970 rhstype = TREE_TYPE (rhs);
7971 coder = TREE_CODE (rhstype);
7972
7973 if (coder == ERROR_MARK)
7974 return error_mark_node;
7975
7976 if (c_dialect_objc ())
7977 {
7978 int parmno;
7979
7980 switch (errtype)
7981 {
7982 case ic_return:
7983 parmno = 0;
7984 break;
7985
7986 case ic_assign:
7987 parmno = -1;
7988 break;
7989
7990 case ic_init:
7991 case ic_init_const:
7992 parmno = -2;
7993 break;
7994
7995 default:
7996 parmno = parmnum;
7997 break;
7998 }
7999
8000 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
8001 }
8002
8003 if (warn_cxx_compat)
8004 {
8005 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8006 if (checktype != error_mark_node
8007 && TREE_CODE (type) == ENUMERAL_TYPE
8008 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
8009 switch (errtype)
8010 {
8011 case ic_argpass:
8012 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
8013 "passing argument %d of %qE is invalid in C++",
8014 parmnum, rname))
8015 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8016 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
8017 "expected %qT but argument is of type %qT",
8018 type, rhstype);
8019 break;
8020 case ic_assign:
8021 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8022 "%qT in assignment is invalid in C++", rhstype, type);
8023 break;
8024 case ic_init:
8025 case ic_init_const:
8026 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
8027 "%qT to %qT in initialization is invalid in C++",
8028 rhstype, type);
8029 break;
8030 case ic_return:
8031 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8032 "%qT in return is invalid in C++", rhstype, type);
8033 break;
8034 default:
8035 gcc_unreachable ();
8036 }
8037 }
8038
8039 if (warn_enum_conversion)
8040 {
8041 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8042 if (checktype != error_mark_node
8043 && TREE_CODE (checktype) == ENUMERAL_TYPE
8044 && TREE_CODE (type) == ENUMERAL_TYPE
8045 && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type)))
8046 {
8047 gcc_rich_location loc (location);
8048 warning_at (&loc, OPT_Wenum_conversion,
8049 "implicit conversion from %qT to %qT",
8050 checktype, type);
8051 }
8052 }
8053
8054 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
8055 {
8056 warn_for_address_of_packed_member (type, orig_rhs);
8057 if (type != rhstype)
8058 /* Convert RHS to TYPE in order to not lose TYPE in diagnostics. */
8059 rhs = convert (type, rhs);
8060 return rhs;
8061 }
8062
8063 if (coder == VOID_TYPE)
8064 {
8065 /* Except for passing an argument to an unprototyped function,
8066 this is a constraint violation. When passing an argument to
8067 an unprototyped function, it is compile-time undefined;
8068 making it a constraint in that case was rejected in
8069 DR#252. */
8070 const char msg[] = "void value not ignored as it ought to be";
8071 if (warnopt)
8072 warning_at (location, warnopt, msg);
8073 else
8074 error_at (location, msg);
8075 return error_mark_node;
8076 }
8077 rhs = require_complete_type (location, rhs);
8078 if (rhs == error_mark_node)
8079 return error_mark_node;
8080
8081 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
8082 return error_mark_node;
8083
8084 /* A non-reference type can convert to a reference. This handles
8085 va_start, va_copy and possibly port built-ins. */
8086 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
8087 {
8088 if (!lvalue_p (rhs))
8089 {
8090 const char msg[] = "cannot pass rvalue to reference parameter";
8091 if (warnopt)
8092 warning_at (location, warnopt, msg);
8093 else
8094 error_at (location, msg);
8095 return error_mark_node;
8096 }
8097 if (!c_mark_addressable (rhs))
8098 return error_mark_node;
8099 rhs = build1 (ADDR_EXPR, c_build_pointer_type (TREE_TYPE (rhs)), rhs);
8100 SET_EXPR_LOCATION (rhs, location);
8101
8102 rhs = convert_for_assignment (location, expr_loc,
8103 c_build_pointer_type (TREE_TYPE (type)),
8104 rhs, origtype, errtype,
8105 null_pointer_constant, fundecl, function,
8106 parmnum, warnopt);
8107 if (rhs == error_mark_node)
8108 return error_mark_node;
8109
8110 rhs = build1 (NOP_EXPR, type, rhs);
8111 SET_EXPR_LOCATION (rhs, location);
8112 return rhs;
8113 }
8114 /* Some types can interconvert without explicit casts. */
8115 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
8116 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
8117 return convert (type, rhs);
8118 /* Arithmetic types all interconvert, and enum is treated like int. */
8119 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
8120 || codel == FIXED_POINT_TYPE
8121 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
8122 || codel == BOOLEAN_TYPE || codel == BITINT_TYPE)
8123 && (coder == INTEGER_TYPE || coder == REAL_TYPE
8124 || coder == FIXED_POINT_TYPE
8125 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
8126 || coder == BOOLEAN_TYPE || coder == BITINT_TYPE))
8127 {
8128 if (warnopt && errtype == ic_argpass)
8129 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
8130 rhstype);
8131
8132 bool save = in_late_binary_op;
8133 if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
8134 || (coder == REAL_TYPE
8135 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
8136 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
8137 in_late_binary_op = true;
8138 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
8139 ? expr_loc : location, type, orig_rhs,
8140 errtype == ic_init_const);
8141 in_late_binary_op = save;
8142 return ret;
8143 }
8144
8145 /* Aggregates in different TUs might need conversion. */
8146 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
8147 && codel == coder
8148 && comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (rhstype)))
8149 return convert_and_check (expr_loc != UNKNOWN_LOCATION
8150 ? expr_loc : location, type, rhs);
8151
8152 /* Conversion to a transparent union or record from its member types.
8153 This applies only to function arguments. */
8154 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
8155 && TYPE_TRANSPARENT_AGGR (type))
8156 && errtype == ic_argpass)
8157 {
8158 tree memb, marginal_memb = NULL_TREE;
8159
8160 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
8161 {
8162 tree memb_type = TREE_TYPE (memb);
8163
8164 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
8165 TYPE_MAIN_VARIANT (rhstype)))
8166 break;
8167
8168 if (TREE_CODE (memb_type) != POINTER_TYPE)
8169 continue;
8170
8171 if (coder == POINTER_TYPE)
8172 {
8173 tree ttl = TREE_TYPE (memb_type);
8174 tree ttr = TREE_TYPE (rhstype);
8175
8176 /* Any non-function converts to a [const][volatile] void *
8177 and vice versa; otherwise, targets must be the same.
8178 Meanwhile, the lhs target must have all the qualifiers of
8179 the rhs. */
8180 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8181 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8182 || comp_target_types (location, memb_type, rhstype))
8183 {
8184 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
8185 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
8186 /* If this type won't generate any warnings, use it. */
8187 if (lquals == rquals
8188 || ((TREE_CODE (ttr) == FUNCTION_TYPE
8189 && TREE_CODE (ttl) == FUNCTION_TYPE)
8190 ? ((lquals | rquals) == rquals)
8191 : ((lquals | rquals) == lquals)))
8192 break;
8193
8194 /* Keep looking for a better type, but remember this one. */
8195 if (!marginal_memb)
8196 marginal_memb = memb;
8197 }
8198 }
8199
8200 /* Can convert integer zero to any pointer type. */
8201 if (null_pointer_constant)
8202 {
8203 rhs = null_pointer_node;
8204 break;
8205 }
8206 }
8207
8208 if (memb || marginal_memb)
8209 {
8210 if (!memb)
8211 {
8212 /* We have only a marginally acceptable member type;
8213 it needs a warning. */
8214 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
8215 tree ttr = TREE_TYPE (rhstype);
8216
8217 /* Const and volatile mean something different for function
8218 types, so the usual warnings are not appropriate. */
8219 if (TREE_CODE (ttr) == FUNCTION_TYPE
8220 && TREE_CODE (ttl) == FUNCTION_TYPE)
8221 {
8222 /* Because const and volatile on functions are
8223 restrictions that say the function will not do
8224 certain things, it is okay to use a const or volatile
8225 function where an ordinary one is wanted, but not
8226 vice-versa. */
8227 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
8228 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8229 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8230 OPT_Wdiscarded_qualifiers,
8231 G_("passing argument %d of %qE "
8232 "makes %q#v qualified function "
8233 "pointer from unqualified"),
8234 G_("assignment makes %q#v qualified "
8235 "function pointer from "
8236 "unqualified"),
8237 G_("initialization makes %q#v qualified "
8238 "function pointer from "
8239 "unqualified"),
8240 G_("return makes %q#v qualified function "
8241 "pointer from unqualified"),
8242 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
8243 }
8244 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
8245 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
8246 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8247 OPT_Wdiscarded_qualifiers,
8248 G_("passing argument %d of %qE discards "
8249 "%qv qualifier from pointer target type"),
8250 G_("assignment discards %qv qualifier "
8251 "from pointer target type"),
8252 G_("initialization discards %qv qualifier "
8253 "from pointer target type"),
8254 G_("return discards %qv qualifier from "
8255 "pointer target type"),
8256 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8257
8258 memb = marginal_memb;
8259 }
8260
8261 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
8262 pedwarn (location, OPT_Wpedantic,
8263 "ISO C prohibits argument conversion to union type");
8264
8265 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
8266 return build_constructor_single (type, memb, rhs);
8267 }
8268 }
8269
8270 /* Conversions among pointers */
8271 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8272 && (coder == codel))
8273 {
8274 /* If RHS refers to a built-in declared without a prototype
8275 BLTIN is the declaration of the built-in with a prototype
8276 and RHSTYPE is set to the actual type of the built-in. */
8277 tree bltin;
8278 rhstype = type_or_builtin_type (rhs, &bltin);
8279
8280 tree ttl = TREE_TYPE (type);
8281 tree ttr = TREE_TYPE (rhstype);
8282 tree mvl = ttl;
8283 tree mvr = ttr;
8284 bool is_opaque_pointer;
8285 bool target_cmp = false; /* Cache comp_target_types () result. */
8286 addr_space_t asl;
8287 addr_space_t asr;
8288
8289 if (TREE_CODE (mvl) != ARRAY_TYPE)
8290 mvl = (TYPE_ATOMIC (mvl)
8291 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
8292 TYPE_QUAL_ATOMIC)
8293 : TYPE_MAIN_VARIANT (mvl));
8294 if (TREE_CODE (mvr) != ARRAY_TYPE)
8295 mvr = (TYPE_ATOMIC (mvr)
8296 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
8297 TYPE_QUAL_ATOMIC)
8298 : TYPE_MAIN_VARIANT (mvr));
8299 /* Opaque pointers are treated like void pointers. */
8300 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
8301
8302 /* The Plan 9 compiler permits a pointer to a struct to be
8303 automatically converted into a pointer to an anonymous field
8304 within the struct. */
8305 if (flag_plan9_extensions
8306 && RECORD_OR_UNION_TYPE_P (mvl)
8307 && RECORD_OR_UNION_TYPE_P (mvr)
8308 && mvl != mvr)
8309 {
8310 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
8311 if (new_rhs != NULL_TREE)
8312 {
8313 rhs = new_rhs;
8314 rhstype = TREE_TYPE (rhs);
8315 coder = TREE_CODE (rhstype);
8316 ttr = TREE_TYPE (rhstype);
8317 mvr = TYPE_MAIN_VARIANT (ttr);
8318 }
8319 }
8320
8321 /* C++ does not allow the implicit conversion void* -> T*. However,
8322 for the purpose of reducing the number of false positives, we
8323 tolerate the special case of
8324
8325 int *p = NULL;
8326
8327 where NULL is typically defined in C to be '(void *) 0'. */
8328 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8329 warning_at (errtype == ic_argpass ? expr_loc : location,
8330 OPT_Wc___compat,
8331 "request for implicit conversion "
8332 "from %qT to %qT not permitted in C++", rhstype, type);
8333
8334 /* Warn of new allocations that are not big enough for the target
8335 type. */
8336 if (warn_alloc_size && TREE_CODE (rhs) == CALL_EXPR)
8337 if (tree fndecl = get_callee_fndecl (rhs))
8338 if (DECL_IS_MALLOC (fndecl))
8339 {
8340 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
8341 tree alloc_size = lookup_attribute ("alloc_size", attrs);
8342 if (alloc_size)
8343 warn_for_alloc_size (location, ttl, rhs, alloc_size);
8344 }
8345
8346 /* See if the pointers point to incompatible address spaces. */
8347 asl = TYPE_ADDR_SPACE (ttl);
8348 asr = TYPE_ADDR_SPACE (ttr);
8349 if (!null_pointer_constant_p (rhs)
8350 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
8351 {
8352 auto_diagnostic_group d;
8353 bool diagnosed = true;
8354 switch (errtype)
8355 {
8356 case ic_argpass:
8357 {
8358 const char msg[] = G_("passing argument %d of %qE from "
8359 "pointer to non-enclosed address space");
8360 if (warnopt)
8361 diagnosed
8362 = warning_at (expr_loc, warnopt, msg, parmnum, rname);
8363 else
8364 error_at (expr_loc, msg, parmnum, rname);
8365 break;
8366 }
8367 case ic_assign:
8368 {
8369 const char msg[] = G_("assignment from pointer to "
8370 "non-enclosed address space");
8371 if (warnopt)
8372 diagnosed = warning_at (location, warnopt, msg);
8373 else
8374 error_at (location, msg);
8375 break;
8376 }
8377 case ic_init:
8378 case ic_init_const:
8379 {
8380 const char msg[] = G_("initialization from pointer to "
8381 "non-enclosed address space");
8382 if (warnopt)
8383 diagnosed = warning_at (location, warnopt, msg);
8384 else
8385 error_at (location, msg);
8386 break;
8387 }
8388 case ic_return:
8389 {
8390 const char msg[] = G_("return from pointer to "
8391 "non-enclosed address space");
8392 if (warnopt)
8393 diagnosed = warning_at (location, warnopt, msg);
8394 else
8395 error_at (location, msg);
8396 break;
8397 }
8398 default:
8399 gcc_unreachable ();
8400 }
8401 if (diagnosed)
8402 {
8403 if (errtype == ic_argpass)
8404 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8405 else
8406 inform (location, "expected %qT but pointer is of type %qT",
8407 type, rhstype);
8408 }
8409 return error_mark_node;
8410 }
8411
8412 /* Check if the right-hand side has a format attribute but the
8413 left-hand side doesn't. */
8414 if (warn_suggest_attribute_format
8415 && check_missing_format_attribute (type, rhstype))
8416 {
8417 switch (errtype)
8418 {
8419 case ic_argpass:
8420 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
8421 "argument %d of %qE might be "
8422 "a candidate for a format attribute",
8423 parmnum, rname);
8424 break;
8425 case ic_assign:
8426 warning_at (location, OPT_Wsuggest_attribute_format,
8427 "assignment left-hand side might be "
8428 "a candidate for a format attribute");
8429 break;
8430 case ic_init:
8431 case ic_init_const:
8432 warning_at (location, OPT_Wsuggest_attribute_format,
8433 "initialization left-hand side might be "
8434 "a candidate for a format attribute");
8435 break;
8436 case ic_return:
8437 warning_at (location, OPT_Wsuggest_attribute_format,
8438 "return type might be "
8439 "a candidate for a format attribute");
8440 break;
8441 default:
8442 gcc_unreachable ();
8443 }
8444 }
8445
8446 /* See if the pointers point to incompatible scalar storage orders. */
8447 if (warn_scalar_storage_order
8448 && !null_pointer_constant_p (rhs)
8449 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
8450 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
8451 {
8452 tree t;
8453
8454 switch (errtype)
8455 {
8456 case ic_argpass:
8457 /* Do not warn for built-in functions, for example memcpy, since we
8458 control how they behave and they can be useful in this area. */
8459 if (TREE_CODE (rname) != FUNCTION_DECL
8460 || !fndecl_built_in_p (rname))
8461 warning_at (location, OPT_Wscalar_storage_order,
8462 "passing argument %d of %qE from incompatible "
8463 "scalar storage order", parmnum, rname);
8464 break;
8465 case ic_assign:
8466 /* Do not warn if the RHS is a call to a function that returns a
8467 pointer that is not an alias. */
8468 if (TREE_CODE (rhs) != CALL_EXPR
8469 || (t = get_callee_fndecl (rhs)) == NULL_TREE
8470 || !DECL_IS_MALLOC (t))
8471 warning_at (location, OPT_Wscalar_storage_order,
8472 "assignment to %qT from pointer type %qT with "
8473 "incompatible scalar storage order", type, rhstype);
8474 break;
8475 case ic_init:
8476 case ic_init_const:
8477 /* Likewise. */
8478 if (TREE_CODE (rhs) != CALL_EXPR
8479 || (t = get_callee_fndecl (rhs)) == NULL_TREE
8480 || !DECL_IS_MALLOC (t))
8481 warning_at (location, OPT_Wscalar_storage_order,
8482 "initialization of %qT from pointer type %qT with "
8483 "incompatible scalar storage order", type, rhstype);
8484 break;
8485 case ic_return:
8486 warning_at (location, OPT_Wscalar_storage_order,
8487 "returning %qT from pointer type with incompatible "
8488 "scalar storage order %qT", rhstype, type);
8489 break;
8490 default:
8491 gcc_unreachable ();
8492 }
8493 }
8494
8495 /* Any non-function converts to a [const][volatile] void *
8496 and vice versa; otherwise, targets must be the same.
8497 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
8498 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8499 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8500 || (target_cmp = comp_target_types (location, type, rhstype))
8501 || is_opaque_pointer
8502 || ((c_common_unsigned_type (mvl)
8503 == c_common_unsigned_type (mvr))
8504 && (c_common_signed_type (mvl)
8505 == c_common_signed_type (mvr))
8506 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
8507 {
8508 /* Warn about loss of qualifers from pointers to arrays with
8509 qualifiers on the element type. */
8510 if (TREE_CODE (ttr) == ARRAY_TYPE)
8511 {
8512 ttr = strip_array_types (ttr);
8513 ttl = strip_array_types (ttl);
8514
8515 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
8516 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
8517 WARNING_FOR_QUALIFIERS (flag_isoc23,
8518 location, expr_loc,
8519 OPT_Wdiscarded_array_qualifiers,
8520 G_("passing argument %d of %qE discards "
8521 "%qv qualifier from pointer target type"),
8522 G_("assignment discards %qv qualifier "
8523 "from pointer target type"),
8524 G_("initialization discards %qv qualifier "
8525 "from pointer target type"),
8526 G_("return discards %qv qualifier from "
8527 "pointer target type"),
8528 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8529 }
8530 else if (pedantic
8531 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
8532 ||
8533 (VOID_TYPE_P (ttr)
8534 && !null_pointer_constant
8535 && TREE_CODE (ttl) == FUNCTION_TYPE)))
8536 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
8537 G_("ISO C forbids passing argument %d of "
8538 "%qE between function pointer "
8539 "and %<void *%>"),
8540 G_("ISO C forbids assignment between "
8541 "function pointer and %<void *%>"),
8542 G_("ISO C forbids initialization between "
8543 "function pointer and %<void *%>"),
8544 G_("ISO C forbids return between function "
8545 "pointer and %<void *%>"));
8546 /* Const and volatile mean something different for function types,
8547 so the usual warnings are not appropriate. */
8548 else if (TREE_CODE (ttr) != FUNCTION_TYPE
8549 && TREE_CODE (ttl) != FUNCTION_TYPE)
8550 {
8551 /* Assignments between atomic and non-atomic objects are OK. */
8552 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
8553 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
8554 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
8555 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
8556
8557 /* Don't warn about loss of qualifier for conversions from
8558 qualified void* to pointers to arrays with corresponding
8559 qualifier on the element type (except for pedantic before C23). */
8560 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc23))
8561 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8562 OPT_Wdiscarded_qualifiers,
8563 G_("passing argument %d of %qE discards "
8564 "%qv qualifier from pointer target type"),
8565 G_("assignment discards %qv qualifier "
8566 "from pointer target type"),
8567 G_("initialization discards %qv qualifier "
8568 "from pointer target type"),
8569 G_("return discards %qv qualifier from "
8570 "pointer target type"),
8571 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8572 else if (warn_quals_ped)
8573 pedwarn_c11 (location, OPT_Wc11_c23_compat,
8574 "array with qualifier on the element is not qualified before C23");
8575
8576 /* If this is not a case of ignoring a mismatch in signedness,
8577 no warning. */
8578 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
8579 || target_cmp)
8580 ;
8581 /* If there is a mismatch, do warn. */
8582 else if (warn_pointer_sign)
8583 switch (errtype)
8584 {
8585 case ic_argpass:
8586 {
8587 auto_diagnostic_group d;
8588 range_label_for_type_mismatch rhs_label (rhstype, type);
8589 gcc_rich_location richloc (expr_loc, &rhs_label,
8590 highlight_colors::actual);
8591 if (pedwarn (&richloc, OPT_Wpointer_sign,
8592 "pointer targets in passing argument %d of "
8593 "%qE differ in signedness", parmnum, rname))
8594 inform_for_arg (fundecl, expr_loc, parmnum, type,
8595 rhstype);
8596 }
8597 break;
8598 case ic_assign:
8599 pedwarn (location, OPT_Wpointer_sign,
8600 "pointer targets in assignment from %qT to %qT "
8601 "differ in signedness", rhstype, type);
8602 break;
8603 case ic_init:
8604 case ic_init_const:
8605 pedwarn_init (location, OPT_Wpointer_sign,
8606 "pointer targets in initialization of %qT "
8607 "from %qT differ in signedness", type,
8608 rhstype);
8609 break;
8610 case ic_return:
8611 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
8612 "returning %qT from a function with return type "
8613 "%qT differ in signedness", rhstype, type);
8614 break;
8615 default:
8616 gcc_unreachable ();
8617 }
8618 }
8619 else if (TREE_CODE (ttl) == FUNCTION_TYPE
8620 && TREE_CODE (ttr) == FUNCTION_TYPE)
8621 {
8622 /* Because const and volatile on functions are restrictions
8623 that say the function will not do certain things,
8624 it is okay to use a const or volatile function
8625 where an ordinary one is wanted, but not vice-versa. */
8626 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
8627 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8628 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8629 OPT_Wdiscarded_qualifiers,
8630 G_("passing argument %d of %qE makes "
8631 "%q#v qualified function pointer "
8632 "from unqualified"),
8633 G_("assignment makes %q#v qualified function "
8634 "pointer from unqualified"),
8635 G_("initialization makes %q#v qualified "
8636 "function pointer from unqualified"),
8637 G_("return makes %q#v qualified function "
8638 "pointer from unqualified"),
8639 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
8640 }
8641 }
8642 /* Avoid warning about the volatile ObjC EH puts on decls. */
8643 else if (!objc_ok)
8644 {
8645 auto_diagnostic_group d;
8646 bool warned = false;
8647 pp_markup::element_expected_type e_type (type);
8648 pp_markup::element_actual_type e_rhstype (rhstype);
8649 switch (errtype)
8650 {
8651 case ic_argpass:
8652 {
8653 range_label_for_type_mismatch rhs_label (rhstype, type);
8654 gcc_rich_location richloc (expr_loc, &rhs_label,
8655 highlight_colors::actual);
8656 warned
8657 = permerror_opt (&richloc, OPT_Wincompatible_pointer_types,
8658 "passing argument %d of %qE from "
8659 "incompatible pointer type",
8660 parmnum, rname);
8661 if (warned)
8662 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8663 }
8664 break;
8665 case ic_assign:
8666 if (bltin)
8667 warned
8668 = permerror_opt (location, OPT_Wincompatible_pointer_types,
8669 "assignment to %e from pointer to "
8670 "%qD with incompatible type %e",
8671 &e_type, bltin, &e_rhstype);
8672 else
8673 warned
8674 = permerror_opt (location, OPT_Wincompatible_pointer_types,
8675 "assignment to %e from incompatible "
8676 "pointer type %e",
8677 &e_type, &e_rhstype);
8678 break;
8679 case ic_init:
8680 case ic_init_const:
8681 if (bltin)
8682 warned
8683 = permerror_init (location, OPT_Wincompatible_pointer_types,
8684 "initialization of %e from pointer to "
8685 "%qD with incompatible type %e",
8686 &e_type, bltin, &e_rhstype);
8687 else
8688 warned
8689 = permerror_init (location, OPT_Wincompatible_pointer_types,
8690 "initialization of %e from incompatible "
8691 "pointer type %e",
8692 &e_type, &e_rhstype);
8693 break;
8694 case ic_return:
8695 if (bltin)
8696 warned
8697 = permerror_opt (location, OPT_Wincompatible_pointer_types,
8698 "returning pointer to %qD of type %e from "
8699 "a function with incompatible type %e",
8700 bltin, &e_rhstype, &e_type);
8701 else
8702 warned
8703 = permerror_opt (location, OPT_Wincompatible_pointer_types,
8704 "returning %e from a function with "
8705 "incompatible return type %e",
8706 &e_rhstype, &e_type);
8707 break;
8708 default:
8709 gcc_unreachable ();
8710 }
8711 if (warned)
8712 {
8713 /* If the mismatching function type is a pointer to a function,
8714 try to show the decl of the function. */
8715 if (TREE_CODE (rhs) == ADDR_EXPR
8716 && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL)
8717 {
8718 tree rhs_fndecl = TREE_OPERAND (rhs, 0);
8719 if (!DECL_IS_UNDECLARED_BUILTIN (rhs_fndecl))
8720 {
8721 gcc_rich_location richloc
8722 (DECL_SOURCE_LOCATION (rhs_fndecl), nullptr,
8723 highlight_colors::actual);
8724 pp_element_quoted_decl e_rhs_fndecl
8725 (rhs_fndecl, highlight_colors::actual);
8726 inform (&richloc,
8727 "%e declared here", &e_rhs_fndecl);
8728 }
8729 }
8730 /* If either/both of the types are typedefs, show the decl. */
8731 maybe_inform_typedef_location (type,
8732 highlight_colors::expected);
8733 maybe_inform_typedef_location (rhstype,
8734 highlight_colors::actual);
8735 }
8736 }
8737
8738 /* If RHS isn't an address, check pointer or array of packed
8739 struct or union. */
8740 warn_for_address_of_packed_member (type, orig_rhs);
8741
8742 return convert (type, rhs);
8743 }
8744 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
8745 {
8746 /* ??? This should not be an error when inlining calls to
8747 unprototyped functions. */
8748 const char msg[] = "invalid use of non-lvalue array";
8749 if (warnopt)
8750 warning_at (location, warnopt, msg);
8751 else
8752 error_at (location, msg);
8753 return error_mark_node;
8754 }
8755 else if (codel == POINTER_TYPE
8756 && (coder == INTEGER_TYPE
8757 || coder == ENUMERAL_TYPE
8758 || coder == BOOLEAN_TYPE
8759 || coder == NULLPTR_TYPE
8760 || coder == BITINT_TYPE))
8761 {
8762 if (null_pointer_constant && c_inhibit_evaluation_warnings == 0
8763 && coder == INTEGER_TYPE)
8764 warning_at (location, OPT_Wzero_as_null_pointer_constant,
8765 "zero as null pointer constant");
8766
8767 /* An explicit constant 0 or type nullptr_t can convert to a pointer,
8768 or one that results from arithmetic, even including a cast to
8769 integer type. */
8770 if (!null_pointer_constant && coder != NULLPTR_TYPE)
8771 switch (errtype)
8772 {
8773 case ic_argpass:
8774 {
8775 auto_diagnostic_group d;
8776 range_label_for_type_mismatch rhs_label (rhstype, type);
8777 gcc_rich_location richloc (expr_loc, &rhs_label,
8778 highlight_colors::actual);
8779 if (permerror_opt (&richloc, OPT_Wint_conversion,
8780 "passing argument %d of %qE makes pointer "
8781 "from integer without a cast", parmnum, rname))
8782 {
8783 maybe_emit_indirection_note (expr_loc, rhs, type);
8784 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8785 }
8786 }
8787 break;
8788 case ic_assign:
8789 permerror_opt (location, OPT_Wint_conversion,
8790 "assignment to %qT from %qT makes pointer from "
8791 "integer without a cast", type, rhstype);
8792 break;
8793 case ic_init:
8794 case ic_init_const:
8795 permerror_init (location, OPT_Wint_conversion,
8796 "initialization of %qT from %qT makes pointer "
8797 "from integer without a cast", type, rhstype);
8798 break;
8799 case ic_return:
8800 permerror_init (location, OPT_Wint_conversion,
8801 "returning %qT from a function with return type "
8802 "%qT makes pointer from integer without a cast",
8803 rhstype, type);
8804 break;
8805 default:
8806 gcc_unreachable ();
8807 }
8808
8809 return convert (type, rhs);
8810 }
8811 else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE)
8812 && coder == POINTER_TYPE)
8813 {
8814 switch (errtype)
8815 {
8816 case ic_argpass:
8817 {
8818 auto_diagnostic_group d;
8819 range_label_for_type_mismatch rhs_label (rhstype, type);
8820 gcc_rich_location richloc (expr_loc, &rhs_label,
8821 highlight_colors::actual);
8822 if (permerror_opt (&richloc, OPT_Wint_conversion,
8823 "passing argument %d of %qE makes integer from "
8824 "pointer without a cast", parmnum, rname))
8825 {
8826 maybe_emit_indirection_note (expr_loc, rhs, type);
8827 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8828 }
8829 }
8830 break;
8831 case ic_assign:
8832 permerror_opt (location, OPT_Wint_conversion,
8833 "assignment to %qT from %qT makes integer from "
8834 "pointer without a cast", type, rhstype);
8835 break;
8836 case ic_init:
8837 case ic_init_const:
8838 permerror_init (location, OPT_Wint_conversion,
8839 "initialization of %qT from %qT makes integer "
8840 "from pointer without a cast", type, rhstype);
8841 break;
8842 case ic_return:
8843 permerror_opt (location, OPT_Wint_conversion, "returning %qT from a "
8844 "function with return type %qT makes integer from "
8845 "pointer without a cast", rhstype, type);
8846 break;
8847 default:
8848 gcc_unreachable ();
8849 }
8850
8851 return convert (type, rhs);
8852 }
8853 else if (C_BOOLEAN_TYPE_P (type)
8854 /* The type nullptr_t may be converted to bool. The
8855 result is false. */
8856 && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
8857 {
8858 tree ret;
8859 bool save = in_late_binary_op;
8860 in_late_binary_op = true;
8861 ret = convert (type, rhs);
8862 in_late_binary_op = save;
8863 return ret;
8864 }
8865 else if (codel == NULLPTR_TYPE && null_pointer_constant)
8866 return convert (type, rhs);
8867
8868 switch (errtype)
8869 {
8870 case ic_argpass:
8871 {
8872 auto_diagnostic_group d;
8873 range_label_for_type_mismatch rhs_label (rhstype, type);
8874 gcc_rich_location richloc (expr_loc, &rhs_label,
8875 highlight_colors::actual);
8876 const char msg[] = G_("incompatible type for argument %d of %qE");
8877 if (warnopt)
8878 warning_at (expr_loc, warnopt, msg, parmnum, rname);
8879 else
8880 error_at (&richloc, msg, parmnum, rname);
8881 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8882 }
8883 break;
8884 case ic_assign:
8885 {
8886 const char msg[]
8887 = G_("incompatible types when assigning to type %qT from type %qT");
8888 if (warnopt)
8889 warning_at (expr_loc, 0, msg, type, rhstype);
8890 else
8891 error_at (expr_loc, msg, type, rhstype);
8892 break;
8893 }
8894 case ic_init:
8895 case ic_init_const:
8896 {
8897 const char msg[]
8898 = G_("incompatible types when initializing type %qT using type %qT");
8899 if (warnopt)
8900 warning_at (location, 0, msg, type, rhstype);
8901 else
8902 error_at (location, msg, type, rhstype);
8903 break;
8904 }
8905 case ic_return:
8906 {
8907 const char msg[]
8908 = G_("incompatible types when returning type %qT but %qT was expected");
8909 if (warnopt)
8910 warning_at (location, 0, msg, rhstype, type);
8911 else
8912 error_at (location, msg, rhstype, type);
8913 break;
8914 }
8915 default:
8916 gcc_unreachable ();
8917 }
8918
8919 return error_mark_node;
8920 }
8921
8922 /* If VALUE is a compound expr all of whose expressions are constant, then
8923 return its value. Otherwise, return error_mark_node.
8924
8925 This is for handling COMPOUND_EXPRs as initializer elements
8926 which is allowed with a warning when -pedantic is specified. */
8927
8928 static tree
8929 valid_compound_expr_initializer (tree value, tree endtype)
8930 {
8931 if (TREE_CODE (value) == COMPOUND_EXPR)
8932 {
8933 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
8934 == error_mark_node)
8935 return error_mark_node;
8936 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
8937 endtype);
8938 }
8939 else if (!initializer_constant_valid_p (value, endtype))
8940 return error_mark_node;
8941 else
8942 return value;
8943 }
8944
8945 /* Perform appropriate conversions on the initial value of a variable,
8946 store it in the declaration DECL,
8947 and print any error messages that are appropriate.
8948 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8949 If the init is invalid, store an ERROR_MARK.
8950
8951 INIT_LOC is the location of the initial value. */
8952
8953 void
8954 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
8955 {
8956 tree value, type;
8957 bool npc = false;
8958 bool int_const_expr = false;
8959 bool arith_const_expr = false;
8960
8961 /* If variable's type was invalidly declared, just ignore it. */
8962
8963 type = TREE_TYPE (decl);
8964 if (TREE_CODE (type) == ERROR_MARK)
8965 return;
8966
8967 /* Digest the specified initializer into an expression. */
8968
8969 if (init)
8970 {
8971 npc = null_pointer_constant_p (init);
8972 int_const_expr = (TREE_CODE (init) == INTEGER_CST
8973 && !TREE_OVERFLOW (init)
8974 && INTEGRAL_TYPE_P (TREE_TYPE (init)));
8975 /* Not fully determined before folding. */
8976 arith_const_expr = true;
8977 }
8978 bool constexpr_p = (VAR_P (decl)
8979 && C_DECL_DECLARED_CONSTEXPR (decl));
8980 value = digest_init (init_loc, decl, type, init, origtype, npc,
8981 int_const_expr, arith_const_expr, true,
8982 TREE_STATIC (decl) || constexpr_p, constexpr_p);
8983
8984 /* Store the expression if valid; else report error. */
8985
8986 if (!in_system_header_at (input_location)
8987 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
8988 warning (OPT_Wtraditional, "traditional C rejects automatic "
8989 "aggregate initialization");
8990
8991 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
8992 DECL_INITIAL (decl) = value;
8993
8994 /* ANSI wants warnings about out-of-range constant initializers. */
8995 STRIP_TYPE_NOPS (value);
8996 if (TREE_STATIC (decl))
8997 constant_expression_warning (value);
8998
8999 /* Check if we need to set array size from compound literal size. */
9000 if (TREE_CODE (type) == ARRAY_TYPE
9001 && TYPE_DOMAIN (type) == NULL_TREE
9002 && value != error_mark_node)
9003 {
9004 tree inside_init = init;
9005
9006 STRIP_TYPE_NOPS (inside_init);
9007 inside_init = fold (inside_init);
9008
9009 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9010 {
9011 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
9012
9013 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
9014 {
9015 /* For int foo[] = (int [3]){1}; we need to set array size
9016 now since later on array initializer will be just the
9017 brace enclosed list of the compound literal. */
9018 tree etype = strip_array_types (TREE_TYPE (decl));
9019 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9020 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
9021 layout_type (type);
9022 layout_decl (cldecl, 0);
9023 TREE_TYPE (decl)
9024 = c_build_qualified_type (type, TYPE_QUALS (etype));
9025 }
9026 }
9027 }
9028 }
9029
9030 /* Methods for storing and printing names for error messages. */
9031
9032 /* Implement a spelling stack that allows components of a name to be pushed
9033 and popped. Each element on the stack is this structure. */
9034
9035 struct spelling
9036 {
9037 int kind;
9038 union
9039 {
9040 unsigned HOST_WIDE_INT i;
9041 const char *s;
9042 } u;
9043 };
9044
9045 #define SPELLING_STRING 1
9046 #define SPELLING_MEMBER 2
9047 #define SPELLING_BOUNDS 3
9048
9049 static struct spelling *spelling; /* Next stack element (unused). */
9050 static struct spelling *spelling_base; /* Spelling stack base. */
9051 static int spelling_size; /* Size of the spelling stack. */
9052
9053 /* Macros to save and restore the spelling stack around push_... functions.
9054 Alternative to SAVE_SPELLING_STACK. */
9055
9056 #define SPELLING_DEPTH() (spelling - spelling_base)
9057 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
9058
9059 /* Push an element on the spelling stack with type KIND and assign VALUE
9060 to MEMBER. */
9061
9062 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
9063 { \
9064 int depth = SPELLING_DEPTH (); \
9065 \
9066 if (depth >= spelling_size) \
9067 { \
9068 spelling_size += 10; \
9069 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
9070 spelling_size); \
9071 RESTORE_SPELLING_DEPTH (depth); \
9072 } \
9073 \
9074 spelling->kind = (KIND); \
9075 spelling->MEMBER = (VALUE); \
9076 spelling++; \
9077 }
9078
9079 /* Push STRING on the stack. Printed literally. */
9080
9081 static void
9082 push_string (const char *string)
9083 {
9084 PUSH_SPELLING (SPELLING_STRING, string, u.s);
9085 }
9086
9087 /* Push a member name on the stack. Printed as '.' STRING. */
9088
9089 static void
9090 push_member_name (tree decl)
9091 {
9092 const char *const string
9093 = (DECL_NAME (decl)
9094 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
9095 : _("<anonymous>"));
9096 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
9097 }
9098
9099 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
9100
9101 static void
9102 push_array_bounds (unsigned HOST_WIDE_INT bounds)
9103 {
9104 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
9105 }
9106
9107 /* Compute the maximum size in bytes of the printed spelling. */
9108
9109 static int
9110 spelling_length (void)
9111 {
9112 int size = 0;
9113 struct spelling *p;
9114
9115 for (p = spelling_base; p < spelling; p++)
9116 {
9117 if (p->kind == SPELLING_BOUNDS)
9118 size += 25;
9119 else
9120 size += strlen (p->u.s) + 1;
9121 }
9122
9123 return size;
9124 }
9125
9126 /* Print the spelling to BUFFER and return it. */
9127
9128 static char *
9129 print_spelling (char *buffer)
9130 {
9131 char *d = buffer;
9132 struct spelling *p;
9133
9134 for (p = spelling_base; p < spelling; p++)
9135 if (p->kind == SPELLING_BOUNDS)
9136 {
9137 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
9138 d += strlen (d);
9139 }
9140 else
9141 {
9142 const char *s;
9143 if (p->kind == SPELLING_MEMBER)
9144 *d++ = '.';
9145 for (s = p->u.s; (*d = *s++); d++)
9146 ;
9147 }
9148 *d++ = '\0';
9149 return buffer;
9150 }
9151
9152 /* Check whether INIT, a floating or integer constant, is
9153 representable in TYPE, a real floating type with the same radix or
9154 a decimal floating type initialized with a binary floating
9155 constant. Return true if OK, false if not. */
9156 static bool
9157 constexpr_init_fits_real_type (tree type, tree init)
9158 {
9159 gcc_assert (SCALAR_FLOAT_TYPE_P (type));
9160 gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
9161 if (TREE_CODE (init) == REAL_CST
9162 && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
9163 {
9164 /* Same mode, no conversion required except for the case of
9165 signaling NaNs if the types are incompatible (e.g. double and
9166 long double with the same mode). */
9167 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
9168 && !comptypes (TYPE_MAIN_VARIANT (type),
9169 TYPE_MAIN_VARIANT (TREE_TYPE (init))))
9170 return false;
9171 return true;
9172 }
9173 if (TREE_CODE (init) == INTEGER_CST)
9174 {
9175 tree converted = build_real_from_int_cst (type, init);
9176 bool fail = false;
9177 wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
9178 TYPE_PRECISION (TREE_TYPE (init)));
9179 return !fail && wi::eq_p (w, wi::to_wide (init));
9180 }
9181 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
9182 return false;
9183 if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
9184 && MODE_HAS_INFINITIES (TYPE_MODE (type)))
9185 || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
9186 && MODE_HAS_NANS (TYPE_MODE (type))))
9187 return true;
9188 if (DECIMAL_FLOAT_TYPE_P (type)
9189 && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
9190 {
9191 /* This is valid if the real number represented by the
9192 initializer can be exactly represented in the decimal
9193 type. Compare the values using MPFR. */
9194 REAL_VALUE_TYPE t;
9195 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9196 mpfr_t bin_val, dec_val;
9197 mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9198 mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9199 mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
9200 char string[256];
9201 real_to_decimal (string, &t, sizeof string, 0, 1);
9202 bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
9203 && mpfr_equal_p (bin_val, dec_val));
9204 mpfr_clear (bin_val);
9205 mpfr_clear (dec_val);
9206 return res;
9207 }
9208 /* exact_real_truncate is not quite right here, since it doesn't
9209 allow even an exact conversion to subnormal values. */
9210 REAL_VALUE_TYPE t;
9211 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9212 return real_identical (&t, &TREE_REAL_CST (init));
9213 }
9214
9215 /* Check whether INIT (location LOC) is valid as a 'constexpr'
9216 initializer for type TYPE, and give an error if not. INIT has
9217 already been folded and verified to be constant. INT_CONST_EXPR
9218 and ARITH_CONST_EXPR say whether it is an integer constant
9219 expression or arithmetic constant expression, respectively. If
9220 TYPE is not a scalar type, this function does nothing. */
9221
9222 static void
9223 check_constexpr_init (location_t loc, tree type, tree init,
9224 bool int_const_expr, bool arith_const_expr)
9225 {
9226 if (POINTER_TYPE_P (type))
9227 {
9228 /* The initializer must be null. */
9229 if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
9230 error_at (loc, "%<constexpr%> pointer initializer is not null");
9231 return;
9232 }
9233 if (INTEGRAL_TYPE_P (type))
9234 {
9235 /* The initializer must be an integer constant expression,
9236 representable in the target type. */
9237 if (!int_const_expr)
9238 {
9239 if (TREE_CODE (init) == RAW_DATA_CST
9240 && TYPE_PRECISION (type) == CHAR_BIT)
9241 {
9242 if (!TYPE_UNSIGNED (type))
9243 for (unsigned int i = 0;
9244 i < (unsigned) RAW_DATA_LENGTH (init); ++i)
9245 if (RAW_DATA_SCHAR_ELT (init, i) < 0)
9246 {
9247 error_at (loc, "%<constexpr%> initializer not "
9248 "representable in type of object");
9249 break;
9250 }
9251 }
9252 else
9253 error_at (loc, "%<constexpr%> integer initializer is not an "
9254 "integer constant expression");
9255 }
9256 else if (!int_fits_type_p (init, type))
9257 error_at (loc, "%<constexpr%> initializer not representable in "
9258 "type of object");
9259 return;
9260 }
9261 /* We don't apply any extra checks to extension types such as vector
9262 or fixed-point types. */
9263 if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
9264 return;
9265 if (!arith_const_expr)
9266 {
9267 error_at (loc, "%<constexpr%> initializer is not an arithmetic "
9268 "constant expression");
9269 return;
9270 }
9271 /* We don't apply any extra checks to complex integers. */
9272 if (TREE_CODE (type) == COMPLEX_TYPE
9273 && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
9274 return;
9275 /* Following N3082, a real type cannot be initialized from a complex
9276 type and a binary type cannot be initialized from a decimal type
9277 (but initializing a decimal type from a binary type is OK).
9278 Signaling NaN initializers are OK only if the types are
9279 compatible (not just the same mode); all quiet NaN and infinity
9280 initializations are considered to preserve the value. */
9281 if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
9282 && SCALAR_FLOAT_TYPE_P (type))
9283 {
9284 error_at (loc, "%<constexpr%> initializer for a real type is of "
9285 "complex type");
9286 return;
9287 }
9288 if (SCALAR_FLOAT_TYPE_P (type)
9289 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
9290 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
9291 && !DECIMAL_FLOAT_TYPE_P (type))
9292 {
9293 error_at (loc, "%<constexpr%> initializer for a binary "
9294 "floating-point type is of decimal type");
9295 return;
9296 }
9297 bool fits;
9298 if (TREE_CODE (type) == COMPLEX_TYPE)
9299 {
9300 switch (TREE_CODE (init))
9301 {
9302 case INTEGER_CST:
9303 case REAL_CST:
9304 fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
9305 break;
9306 case COMPLEX_CST:
9307 fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
9308 TREE_REALPART (init))
9309 && constexpr_init_fits_real_type (TREE_TYPE (type),
9310 TREE_IMAGPART (init)));
9311 break;
9312 default:
9313 gcc_unreachable ();
9314 }
9315 }
9316 else
9317 fits = constexpr_init_fits_real_type (type, init);
9318 if (!fits)
9319 error_at (loc, "%<constexpr%> initializer not representable in "
9320 "type of object");
9321 }
9322
9323 /* Digest the parser output INIT as an initializer for type TYPE
9324 initializing DECL.
9325 Return a C expression of type TYPE to represent the initial value.
9326
9327 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9328
9329 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
9330 INT_CONST_EXPR is true if INIT is an integer constant expression,
9331 and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
9332 constant expression, false if it has already been determined in the
9333 caller that it is not (but folding may have made the value passed here
9334 indistinguishable from an arithmetic constant expression).
9335
9336 If INIT is a string constant, STRICT_STRING is true if it is
9337 unparenthesized or we should not warn here for it being parenthesized.
9338 For other types of INIT, STRICT_STRING is not used.
9339
9340 INIT_LOC is the location of the INIT.
9341
9342 REQUIRE_CONSTANT requests an error if non-constant initializers or
9343 elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
9344 on initializers for 'constexpr' objects apply. */
9345
9346 static tree
9347 digest_init (location_t init_loc, tree decl, tree type, tree init,
9348 tree origtype, bool null_pointer_constant, bool int_const_expr,
9349 bool arith_const_expr, bool strict_string,
9350 bool require_constant, bool require_constexpr)
9351 {
9352 enum tree_code code = TREE_CODE (type);
9353 tree inside_init = init;
9354 tree semantic_type = NULL_TREE;
9355 bool maybe_const = true;
9356
9357 if (type == error_mark_node
9358 || !init
9359 || error_operand_p (init))
9360 return error_mark_node;
9361
9362 STRIP_TYPE_NOPS (inside_init);
9363
9364 /* If require_constant is TRUE, when the initializer is a call to
9365 .ACCESS_WITH_SIZE, use the first argument as the initializer.
9366 For example:
9367 y = (char *) .ACCESS_WITH_SIZE ((char *) &static_annotated.c,...)
9368 will be converted to
9369 y = &static_annotated.c. */
9370
9371 if (require_constant
9372 && TREE_CODE (inside_init) == NOP_EXPR
9373 && TREE_CODE (TREE_OPERAND (inside_init, 0)) == CALL_EXPR
9374 && is_access_with_size_p (TREE_OPERAND (inside_init, 0)))
9375 inside_init
9376 = get_ref_from_access_with_size (TREE_OPERAND (inside_init, 0));
9377
9378 if (!c_in_omp_for)
9379 {
9380 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
9381 {
9382 semantic_type = TREE_TYPE (inside_init);
9383 inside_init = TREE_OPERAND (inside_init, 0);
9384 }
9385 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
9386 }
9387 /* TODO: this may not detect all cases of expressions folding to
9388 constants that are not arithmetic constant expressions. */
9389 if (!maybe_const)
9390 arith_const_expr = false;
9391 else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
9392 && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
9393 && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
9394 arith_const_expr = false;
9395 else if (TREE_CODE (inside_init) != INTEGER_CST
9396 && TREE_CODE (inside_init) != REAL_CST
9397 && TREE_CODE (inside_init) != COMPLEX_CST
9398 && TREE_CODE (inside_init) != RAW_DATA_CST)
9399 arith_const_expr = false;
9400 else if (TREE_OVERFLOW (inside_init))
9401 arith_const_expr = false;
9402
9403 /* Initialization of an array of chars from a string constant
9404 optionally enclosed in braces. */
9405
9406 if (code == ARRAY_TYPE && inside_init
9407 && TREE_CODE (inside_init) == STRING_CST)
9408 {
9409 tree typ1
9410 = (TYPE_ATOMIC (TREE_TYPE (type))
9411 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
9412 TYPE_QUAL_ATOMIC)
9413 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
9414 /* Note that an array could be both an array of character type
9415 and an array of wchar_t if wchar_t is signed char or unsigned
9416 char. */
9417 bool char_array = (typ1 == char_type_node
9418 || typ1 == signed_char_type_node
9419 || typ1 == unsigned_char_type_node);
9420 bool wchar_array = !!comptypes (typ1, wchar_type_node);
9421 bool char16_array = !!comptypes (typ1, char16_type_node);
9422 bool char32_array = !!comptypes (typ1, char32_type_node);
9423
9424 if (char_array || wchar_array || char16_array || char32_array)
9425 {
9426 struct c_expr expr;
9427 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
9428 bool incompat_string_cst = false;
9429 expr.value = inside_init;
9430 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
9431 expr.original_type = NULL;
9432 expr.m_decimal = 0;
9433 maybe_warn_string_init (init_loc, type, expr);
9434
9435 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
9436 pedwarn_init (init_loc, OPT_Wpedantic,
9437 "initialization of a flexible array member");
9438
9439 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9440 TYPE_MAIN_VARIANT (type)))
9441 return inside_init;
9442
9443 if (char_array)
9444 {
9445 if (typ2 != char_type_node && typ2 != char8_type_node)
9446 incompat_string_cst = true;
9447 }
9448 else if (!comptypes (typ1, typ2))
9449 incompat_string_cst = true;
9450
9451 if (incompat_string_cst)
9452 {
9453 error_init (init_loc, "cannot initialize array of %qT from "
9454 "a string literal with type array of %qT",
9455 typ1, typ2);
9456 return error_mark_node;
9457 }
9458
9459 if (require_constexpr
9460 && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
9461 {
9462 /* Check if all characters of the string can be
9463 represented in the type of the constexpr object being
9464 initialized. */
9465 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
9466 const unsigned char *p =
9467 (const unsigned char *) TREE_STRING_POINTER (inside_init);
9468 gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
9469 for (unsigned i = 0; i < len; i++)
9470 if (p[i] > 127)
9471 {
9472 error_init (init_loc, "%<constexpr%> initializer not "
9473 "representable in type of object");
9474 break;
9475 }
9476 }
9477
9478 if (TYPE_DOMAIN (type) != NULL_TREE
9479 && TYPE_SIZE (type) != NULL_TREE
9480 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9481 {
9482 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
9483
9484 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
9485 {
9486 unsigned HOST_WIDE_INT avail
9487 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
9488 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
9489 const char *p = TREE_STRING_POINTER (inside_init);
9490
9491 /* Construct truncated string. */
9492 inside_init = build_string (avail, p);
9493
9494 /* Subtract the size of a single (possibly wide) character
9495 because it may be ok to ignore the terminating NUL char
9496 that is counted in the length of the constant. */
9497 if (len - unit > avail)
9498 pedwarn_init (init_loc, 0,
9499 "initializer-string for array of %qT "
9500 "is too long (%wu chars into %wu "
9501 "available)", typ1, len, avail);
9502 else if (warn_cxx_compat)
9503 warning_at (init_loc, OPT_Wc___compat,
9504 "initializer-string for array of %qT "
9505 "is too long for C++ (%wu chars into %wu "
9506 "available)", typ1, len, avail);
9507 else if (warn_unterminated_string_initialization
9508 && get_attr_nonstring_decl (decl) == NULL_TREE)
9509 warning_at (init_loc,
9510 OPT_Wunterminated_string_initialization,
9511 "initializer-string for array of %qT "
9512 "truncates NUL terminator but destination "
9513 "lacks %qs attribute (%wu chars into %wu "
9514 "available)", typ1, "nonstring", len, avail);
9515 }
9516 }
9517
9518 TREE_TYPE (inside_init) = type;
9519 return inside_init;
9520 }
9521 else if (INTEGRAL_TYPE_P (typ1))
9522 {
9523 error_init (init_loc, "array of inappropriate type initialized "
9524 "from string constant");
9525 return error_mark_node;
9526 }
9527 }
9528
9529 /* Build a VECTOR_CST from a *constant* vector constructor. If the
9530 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
9531 below and handle as a constructor. */
9532 if (code == VECTOR_TYPE
9533 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
9534 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
9535 && TREE_CONSTANT (inside_init))
9536 {
9537 if (TREE_CODE (inside_init) == VECTOR_CST
9538 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9539 TYPE_MAIN_VARIANT (type)))
9540 return inside_init;
9541
9542 if (TREE_CODE (inside_init) == CONSTRUCTOR)
9543 {
9544 unsigned HOST_WIDE_INT ix;
9545 tree value;
9546 bool constant_p = true;
9547
9548 /* Iterate through elements and check if all constructor
9549 elements are *_CSTs. */
9550 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
9551 if (!CONSTANT_CLASS_P (value))
9552 {
9553 constant_p = false;
9554 break;
9555 }
9556
9557 if (constant_p)
9558 return build_vector_from_ctor (type,
9559 CONSTRUCTOR_ELTS (inside_init));
9560 }
9561 }
9562
9563 if (warn_sequence_point)
9564 verify_sequence_points (inside_init);
9565
9566 /* Any type can be initialized
9567 from an expression of the same type, optionally with braces. */
9568
9569 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
9570 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9571 TYPE_MAIN_VARIANT (type))
9572 || (code == ARRAY_TYPE
9573 && comptypes (TREE_TYPE (inside_init), type))
9574 || (gnu_vector_type_p (type)
9575 && comptypes (TREE_TYPE (inside_init), type))
9576 || (code == POINTER_TYPE
9577 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
9578 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
9579 TREE_TYPE (type)))))
9580 {
9581 if (code == POINTER_TYPE)
9582 {
9583 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
9584 {
9585 if (TREE_CODE (inside_init) == STRING_CST
9586 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9587 inside_init = array_to_pointer_conversion
9588 (init_loc, inside_init);
9589 else
9590 {
9591 error_init (init_loc, "invalid use of non-lvalue array");
9592 return error_mark_node;
9593 }
9594 }
9595 }
9596
9597 if (code == VECTOR_TYPE || c_hardbool_type_attr (type))
9598 /* Although the types are compatible, we may require a
9599 conversion. */
9600 inside_init = convert (type, inside_init);
9601
9602 if ((code == RECORD_TYPE || code == UNION_TYPE)
9603 && !comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init))))
9604 {
9605 error_init (init_loc, "invalid initializer");
9606 return error_mark_node;
9607 }
9608
9609 if (require_constant
9610 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9611 {
9612 /* As an extension, allow initializing objects with static storage
9613 duration with compound literals (which are then treated just as
9614 the brace enclosed list they contain). Also allow this for
9615 vectors, as we can only assign them with compound literals. */
9616 if (flag_isoc99 && code != VECTOR_TYPE)
9617 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
9618 "is not constant");
9619 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
9620 inside_init = DECL_INITIAL (decl);
9621 }
9622
9623 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
9624 && TREE_CODE (inside_init) != CONSTRUCTOR)
9625 {
9626 error_init (init_loc, "array initialized from non-constant array "
9627 "expression");
9628 return error_mark_node;
9629 }
9630
9631 /* Compound expressions can only occur here if -Wpedantic or
9632 -pedantic-errors is specified. In the later case, we always want
9633 an error. In the former case, we simply want a warning. */
9634 if (require_constant && pedantic
9635 && TREE_CODE (inside_init) == COMPOUND_EXPR)
9636 {
9637 inside_init
9638 = valid_compound_expr_initializer (inside_init,
9639 TREE_TYPE (inside_init));
9640 if (inside_init == error_mark_node)
9641 error_init (init_loc, "initializer element is not constant");
9642 else
9643 pedwarn_init (init_loc, OPT_Wpedantic,
9644 "initializer element is not constant");
9645 if (flag_pedantic_errors)
9646 inside_init = error_mark_node;
9647 }
9648 else if (require_constant
9649 && !initializer_constant_valid_p (inside_init,
9650 TREE_TYPE (inside_init)))
9651 {
9652 error_init (init_loc, "initializer element is not constant");
9653 inside_init = error_mark_node;
9654 }
9655 else if (require_constant && !maybe_const)
9656 pedwarn_init (init_loc, OPT_Wpedantic,
9657 "initializer element is not a constant expression");
9658 else if (require_constexpr)
9659 check_constexpr_init (init_loc, type, inside_init,
9660 int_const_expr, arith_const_expr);
9661
9662 /* Added to enable additional -Wsuggest-attribute=format warnings. */
9663 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
9664 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
9665 type, inside_init, origtype,
9666 (require_constant
9667 ? ic_init_const
9668 : ic_init), null_pointer_constant,
9669 NULL_TREE, NULL_TREE, 0);
9670 if (TREE_CODE (inside_init) == RAW_DATA_CST
9671 && c_inhibit_evaluation_warnings == 0
9672 && warn_conversion
9673 && !TYPE_UNSIGNED (type)
9674 && TYPE_PRECISION (type) == CHAR_BIT)
9675 for (unsigned int i = 0;
9676 i < (unsigned) RAW_DATA_LENGTH (inside_init); ++i)
9677 if (RAW_DATA_SCHAR_ELT (inside_init, i) < 0)
9678 warning_at (init_loc, OPT_Wconversion,
9679 "conversion from %qT to %qT changes value from "
9680 "%qd to %qd",
9681 integer_type_node, type,
9682 RAW_DATA_UCHAR_ELT (inside_init, i),
9683 RAW_DATA_SCHAR_ELT (inside_init, i));
9684 return inside_init;
9685 }
9686
9687 /* Handle scalar types, including conversions. */
9688
9689 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
9690 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
9691 || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE
9692 || code == BITINT_TYPE)
9693 {
9694 tree unconverted_init = inside_init;
9695 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
9696 && (TREE_CODE (init) == STRING_CST
9697 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
9698 inside_init = init = array_to_pointer_conversion (init_loc, init);
9699 if (semantic_type)
9700 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9701 inside_init);
9702 inside_init
9703 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
9704 inside_init, origtype,
9705 require_constant ? ic_init_const : ic_init,
9706 null_pointer_constant, NULL_TREE, NULL_TREE,
9707 0);
9708
9709 /* Check to see if we have already given an error message. */
9710 if (inside_init == error_mark_node)
9711 ;
9712 else if (require_constant && !TREE_CONSTANT (inside_init))
9713 {
9714 error_init (init_loc, "initializer element is not constant");
9715 inside_init = error_mark_node;
9716 }
9717 else if (require_constant
9718 && !initializer_constant_valid_p (inside_init,
9719 TREE_TYPE (inside_init)))
9720 {
9721 error_init (init_loc, "initializer element is not computable at "
9722 "load time");
9723 inside_init = error_mark_node;
9724 }
9725 else if (require_constant && !maybe_const)
9726 pedwarn_init (init_loc, OPT_Wpedantic,
9727 "initializer element is not a constant expression");
9728 else if (require_constexpr)
9729 check_constexpr_init (init_loc, type, unconverted_init,
9730 int_const_expr, arith_const_expr);
9731
9732 return inside_init;
9733 }
9734
9735 /* Come here only for records and arrays. */
9736
9737 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
9738 {
9739 error_init (init_loc,
9740 "variable-sized object may not be initialized except "
9741 "with an empty initializer");
9742 return error_mark_node;
9743 }
9744
9745 error_init (init_loc, "invalid initializer");
9746 return error_mark_node;
9747 }
9748
9749 /* Handle initializers that use braces. */
9750
9751 /* Type of object we are accumulating a constructor for.
9752 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
9753 static tree constructor_type;
9754
9755 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
9756 left to fill. */
9757 static tree constructor_fields;
9758
9759 /* For an ARRAY_TYPE, this is the specified index
9760 at which to store the next element we get. */
9761 static tree constructor_index;
9762
9763 /* For an ARRAY_TYPE, this is the maximum index. */
9764 static tree constructor_max_index;
9765
9766 /* For a RECORD_TYPE, this is the first field not yet written out. */
9767 static tree constructor_unfilled_fields;
9768
9769 /* For an ARRAY_TYPE, this is the index of the first element
9770 not yet written out. */
9771 static tree constructor_unfilled_index;
9772
9773 /* In a RECORD_TYPE, the byte index of the next consecutive field.
9774 This is so we can generate gaps between fields, when appropriate. */
9775 static tree constructor_bit_index;
9776
9777 /* If we are saving up the elements rather than allocating them,
9778 this is the list of elements so far (in reverse order,
9779 most recent first). */
9780 static vec<constructor_elt, va_gc> *constructor_elements;
9781
9782 /* 1 if constructor should be incrementally stored into a constructor chain,
9783 0 if all the elements should be kept in AVL tree. */
9784 static int constructor_incremental;
9785
9786 /* 1 if so far this constructor's elements are all compile-time constants. */
9787 static int constructor_constant;
9788
9789 /* 1 if so far this constructor's elements are all valid address constants. */
9790 static int constructor_simple;
9791
9792 /* 1 if this constructor has an element that cannot be part of a
9793 constant expression. */
9794 static int constructor_nonconst;
9795
9796 /* 1 if this constructor is erroneous so far. */
9797 static int constructor_erroneous;
9798
9799 /* 1 if this constructor is the universal zero initializer { 0 }. */
9800 static int constructor_zeroinit;
9801
9802 /* 1 if this constructor should have padding bits zeroed (C23 {}. */
9803 static bool constructor_zero_padding_bits;
9804
9805 /* Structure for managing pending initializer elements, organized as an
9806 AVL tree. */
9807
9808 struct init_node
9809 {
9810 struct init_node *left, *right;
9811 struct init_node *parent;
9812 int balance;
9813 tree purpose;
9814 tree value;
9815 tree origtype;
9816 };
9817
9818 /* Tree of pending elements at this constructor level.
9819 These are elements encountered out of order
9820 which belong at places we haven't reached yet in actually
9821 writing the output.
9822 Will never hold tree nodes across GC runs. */
9823 static struct init_node *constructor_pending_elts;
9824
9825 /* The SPELLING_DEPTH of this constructor. */
9826 static int constructor_depth;
9827
9828 /* DECL node for which an initializer is being read.
9829 0 means we are reading a constructor expression
9830 such as (struct foo) {...}. */
9831 static tree constructor_decl;
9832
9833 /* Nonzero if there were any member designators in this initializer. */
9834 static int constructor_designated;
9835
9836 /* Nesting depth of designator list. */
9837 static int designator_depth;
9838
9839 /* Nonzero if there were diagnosed errors in this designator list. */
9840 static int designator_erroneous;
9841
9842
9843 /* This stack has a level for each implicit or explicit level of
9844 structuring in the initializer, including the outermost one. It
9845 saves the values of most of the variables above. */
9846
9847 struct constructor_range_stack;
9848
9849 struct constructor_stack
9850 {
9851 struct constructor_stack *next;
9852 tree type;
9853 tree fields;
9854 tree index;
9855 tree max_index;
9856 tree unfilled_index;
9857 tree unfilled_fields;
9858 tree bit_index;
9859 vec<constructor_elt, va_gc> *elements;
9860 struct init_node *pending_elts;
9861 int offset;
9862 int depth;
9863 /* If value nonzero, this value should replace the entire
9864 constructor at this level. */
9865 struct c_expr replacement_value;
9866 struct constructor_range_stack *range_stack;
9867 char constant;
9868 char simple;
9869 char nonconst;
9870 char implicit;
9871 char erroneous;
9872 char outer;
9873 char incremental;
9874 char designated;
9875 bool zero_padding_bits;
9876 int designator_depth;
9877 };
9878
9879 static struct constructor_stack *constructor_stack;
9880
9881 /* This stack represents designators from some range designator up to
9882 the last designator in the list. */
9883
9884 struct constructor_range_stack
9885 {
9886 struct constructor_range_stack *next, *prev;
9887 struct constructor_stack *stack;
9888 tree range_start;
9889 tree index;
9890 tree range_end;
9891 tree fields;
9892 };
9893
9894 static struct constructor_range_stack *constructor_range_stack;
9895
9896 /* This stack records separate initializers that are nested.
9897 Nested initializers can't happen in ANSI C, but GNU C allows them
9898 in cases like { ... (struct foo) { ... } ... }. */
9899
9900 struct initializer_stack
9901 {
9902 struct initializer_stack *next;
9903 tree decl;
9904 struct constructor_stack *constructor_stack;
9905 struct constructor_range_stack *constructor_range_stack;
9906 vec<constructor_elt, va_gc> *elements;
9907 struct spelling *spelling;
9908 struct spelling *spelling_base;
9909 int spelling_size;
9910 char require_constant_value;
9911 char require_constant_elements;
9912 char require_constexpr_value;
9913 char designated;
9914 rich_location *missing_brace_richloc;
9915 };
9916
9917 static struct initializer_stack *initializer_stack;
9918
9919 /* Prepare to parse and output the initializer for variable DECL. */
9920
9921 void
9922 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
9923 bool init_require_constant, bool init_require_constexpr,
9924 rich_location *richloc)
9925 {
9926 const char *locus;
9927 struct initializer_stack *p = XNEW (struct initializer_stack);
9928
9929 p->decl = constructor_decl;
9930 p->require_constant_value = require_constant_value;
9931 p->require_constant_elements = require_constant_elements;
9932 p->require_constexpr_value = require_constexpr_value;
9933 p->constructor_stack = constructor_stack;
9934 p->constructor_range_stack = constructor_range_stack;
9935 p->elements = constructor_elements;
9936 p->spelling = spelling;
9937 p->spelling_base = spelling_base;
9938 p->spelling_size = spelling_size;
9939 p->next = initializer_stack;
9940 p->missing_brace_richloc = richloc;
9941 p->designated = constructor_designated;
9942 initializer_stack = p;
9943
9944 constructor_decl = decl;
9945 constructor_designated = 0;
9946
9947 require_constant_value = init_require_constant;
9948 require_constexpr_value = init_require_constexpr;
9949 if (decl != NULL_TREE && decl != error_mark_node)
9950 {
9951 require_constant_elements
9952 = ((init_require_constant || (pedantic && !flag_isoc99))
9953 /* For a scalar, you can always use any value to initialize,
9954 even within braces. */
9955 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
9956 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
9957 }
9958 else
9959 {
9960 require_constant_elements = false;
9961 locus = _("(anonymous)");
9962 }
9963
9964 constructor_stack = 0;
9965 constructor_range_stack = 0;
9966
9967 found_missing_braces = 0;
9968
9969 spelling_base = 0;
9970 spelling_size = 0;
9971 RESTORE_SPELLING_DEPTH (0);
9972
9973 if (locus)
9974 push_string (locus);
9975 }
9976
9977 void
9978 finish_init (void)
9979 {
9980 struct initializer_stack *p = initializer_stack;
9981
9982 /* Free the whole constructor stack of this initializer. */
9983 while (constructor_stack)
9984 {
9985 struct constructor_stack *q = constructor_stack;
9986 constructor_stack = q->next;
9987 XDELETE (q);
9988 }
9989
9990 gcc_assert (!constructor_range_stack);
9991
9992 /* Pop back to the data of the outer initializer (if any). */
9993 XDELETE (spelling_base);
9994
9995 constructor_decl = p->decl;
9996 require_constant_value = p->require_constant_value;
9997 require_constant_elements = p->require_constant_elements;
9998 require_constexpr_value = p->require_constexpr_value;
9999 constructor_stack = p->constructor_stack;
10000 constructor_designated = p->designated;
10001 constructor_range_stack = p->constructor_range_stack;
10002 constructor_elements = p->elements;
10003 spelling = p->spelling;
10004 spelling_base = p->spelling_base;
10005 spelling_size = p->spelling_size;
10006 initializer_stack = p->next;
10007 XDELETE (p);
10008 }
10009
10010 /* Call here when we see the initializer is surrounded by braces.
10011 This is instead of a call to push_init_level;
10012 it is matched by a call to pop_init_level.
10013
10014 TYPE is the type to initialize, for a constructor expression.
10015 For an initializer for a decl, TYPE is zero. */
10016
10017 void
10018 really_start_incremental_init (tree type)
10019 {
10020 struct constructor_stack *p = XNEW (struct constructor_stack);
10021
10022 if (type == NULL_TREE)
10023 type = TREE_TYPE (constructor_decl);
10024
10025 if (VECTOR_TYPE_P (type)
10026 && TYPE_VECTOR_OPAQUE (type))
10027 error ("opaque vector types cannot be initialized");
10028
10029 p->type = constructor_type;
10030 p->fields = constructor_fields;
10031 p->index = constructor_index;
10032 p->max_index = constructor_max_index;
10033 p->unfilled_index = constructor_unfilled_index;
10034 p->unfilled_fields = constructor_unfilled_fields;
10035 p->bit_index = constructor_bit_index;
10036 p->elements = constructor_elements;
10037 p->constant = constructor_constant;
10038 p->simple = constructor_simple;
10039 p->nonconst = constructor_nonconst;
10040 p->erroneous = constructor_erroneous;
10041 p->pending_elts = constructor_pending_elts;
10042 p->depth = constructor_depth;
10043 p->replacement_value.value = 0;
10044 p->replacement_value.original_code = ERROR_MARK;
10045 p->replacement_value.original_type = NULL;
10046 p->implicit = 0;
10047 p->range_stack = 0;
10048 p->outer = 0;
10049 p->incremental = constructor_incremental;
10050 p->designated = constructor_designated;
10051 p->zero_padding_bits = constructor_zero_padding_bits;
10052 p->designator_depth = designator_depth;
10053 p->next = 0;
10054 constructor_stack = p;
10055
10056 constructor_constant = 1;
10057 constructor_simple = 1;
10058 constructor_nonconst = 0;
10059 constructor_depth = SPELLING_DEPTH ();
10060 constructor_elements = NULL;
10061 constructor_pending_elts = 0;
10062 constructor_type = type;
10063 constructor_incremental = 1;
10064 constructor_designated = 0;
10065 constructor_zero_padding_bits = false;
10066 constructor_zeroinit = 1;
10067 designator_depth = 0;
10068 designator_erroneous = 0;
10069
10070 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10071 {
10072 constructor_fields = TYPE_FIELDS (constructor_type);
10073 /* Skip any nameless bit fields at the beginning. */
10074 while (constructor_fields != NULL_TREE
10075 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10076 constructor_fields = DECL_CHAIN (constructor_fields);
10077
10078 constructor_unfilled_fields = constructor_fields;
10079 constructor_bit_index = bitsize_zero_node;
10080 }
10081 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10082 {
10083 if (TYPE_DOMAIN (constructor_type))
10084 {
10085 constructor_max_index
10086 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10087
10088 /* Detect non-empty initializations of zero-length arrays. */
10089 if (constructor_max_index == NULL_TREE
10090 && TYPE_SIZE (constructor_type))
10091 constructor_max_index = integer_minus_one_node;
10092
10093 /* constructor_max_index needs to be an INTEGER_CST. Attempts
10094 to initialize VLAs with a nonempty initializer will cause a
10095 proper error; avoid tree checking errors as well by setting a
10096 safe value. */
10097 if (constructor_max_index
10098 && TREE_CODE (constructor_max_index) != INTEGER_CST)
10099 constructor_max_index = integer_minus_one_node;
10100
10101 constructor_index
10102 = convert (bitsizetype,
10103 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10104 }
10105 else
10106 {
10107 constructor_index = bitsize_zero_node;
10108 constructor_max_index = NULL_TREE;
10109 }
10110
10111 constructor_unfilled_index = constructor_index;
10112 }
10113 else if (gnu_vector_type_p (constructor_type))
10114 {
10115 /* Vectors are like simple fixed-size arrays. */
10116 constructor_max_index =
10117 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10118 constructor_index = bitsize_zero_node;
10119 constructor_unfilled_index = constructor_index;
10120 }
10121 else
10122 {
10123 /* Handle the case of int x = {5}; */
10124 constructor_fields = constructor_type;
10125 constructor_unfilled_fields = constructor_type;
10126 }
10127 }
10128
10129 extern location_t last_init_list_comma;
10130
10131 /* Called when we see an open brace for a nested initializer. Finish
10132 off any pending levels with implicit braces. */
10133 void
10134 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
10135 {
10136 while (constructor_stack->implicit)
10137 {
10138 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10139 && constructor_fields == NULL_TREE)
10140 process_init_element (input_location,
10141 pop_init_level (loc, 1, braced_init_obstack,
10142 last_init_list_comma),
10143 true, braced_init_obstack);
10144 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
10145 && constructor_max_index
10146 && tree_int_cst_lt (constructor_max_index,
10147 constructor_index))
10148 process_init_element (input_location,
10149 pop_init_level (loc, 1, braced_init_obstack,
10150 last_init_list_comma),
10151 true, braced_init_obstack);
10152 else
10153 break;
10154 }
10155 }
10156
10157 /* Push down into a subobject, for initialization.
10158 If this is for an explicit set of braces, IMPLICIT is 0.
10159 If it is because the next element belongs at a lower level,
10160 IMPLICIT is 1 (or 2 if the push is because of designator list). */
10161
10162 void
10163 push_init_level (location_t loc, int implicit,
10164 struct obstack *braced_init_obstack)
10165 {
10166 struct constructor_stack *p;
10167 tree value = NULL_TREE;
10168
10169 /* Unless this is an explicit brace, we need to preserve previous
10170 content if any. */
10171 if (implicit)
10172 {
10173 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
10174 value = find_init_member (constructor_fields, braced_init_obstack);
10175 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10176 value = find_init_member (constructor_index, braced_init_obstack);
10177 }
10178
10179 p = XNEW (struct constructor_stack);
10180 p->type = constructor_type;
10181 p->fields = constructor_fields;
10182 p->index = constructor_index;
10183 p->max_index = constructor_max_index;
10184 p->unfilled_index = constructor_unfilled_index;
10185 p->unfilled_fields = constructor_unfilled_fields;
10186 p->bit_index = constructor_bit_index;
10187 p->elements = constructor_elements;
10188 p->constant = constructor_constant;
10189 p->simple = constructor_simple;
10190 p->nonconst = constructor_nonconst;
10191 p->erroneous = constructor_erroneous;
10192 p->pending_elts = constructor_pending_elts;
10193 p->depth = constructor_depth;
10194 p->replacement_value.value = NULL_TREE;
10195 p->replacement_value.original_code = ERROR_MARK;
10196 p->replacement_value.original_type = NULL;
10197 p->implicit = implicit;
10198 p->outer = 0;
10199 p->incremental = constructor_incremental;
10200 p->designated = constructor_designated;
10201 p->zero_padding_bits = constructor_zero_padding_bits;
10202 p->designator_depth = designator_depth;
10203 p->next = constructor_stack;
10204 p->range_stack = 0;
10205 constructor_stack = p;
10206
10207 constructor_constant = 1;
10208 constructor_simple = 1;
10209 constructor_nonconst = 0;
10210 constructor_depth = SPELLING_DEPTH ();
10211 constructor_elements = NULL;
10212 constructor_incremental = 1;
10213 /* If the upper initializer is designated, then mark this as
10214 designated too to prevent bogus warnings. */
10215 constructor_designated = p->designated;
10216 /* If the upper initializer has padding bits zeroed, that includes
10217 all nested initializers as well. */
10218 constructor_zero_padding_bits = p->zero_padding_bits;
10219 constructor_pending_elts = 0;
10220 if (!implicit)
10221 {
10222 p->range_stack = constructor_range_stack;
10223 constructor_range_stack = 0;
10224 designator_depth = 0;
10225 designator_erroneous = 0;
10226 }
10227
10228 /* Don't die if an entire brace-pair level is superfluous
10229 in the containing level. */
10230 if (constructor_type == NULL_TREE)
10231 ;
10232 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10233 {
10234 /* Don't die if there are extra init elts at the end. */
10235 if (constructor_fields == NULL_TREE)
10236 constructor_type = NULL_TREE;
10237 else
10238 {
10239 constructor_type = TREE_TYPE (constructor_fields);
10240 push_member_name (constructor_fields);
10241 constructor_depth++;
10242 }
10243 }
10244 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10245 {
10246 constructor_type = TREE_TYPE (constructor_type);
10247 push_array_bounds (tree_to_uhwi (constructor_index));
10248 constructor_depth++;
10249 }
10250
10251 if (constructor_type == NULL_TREE)
10252 {
10253 error_init (loc, "extra brace group at end of initializer");
10254 constructor_fields = NULL_TREE;
10255 constructor_unfilled_fields = NULL_TREE;
10256 return;
10257 }
10258
10259 if (value && TREE_CODE (value) == CONSTRUCTOR)
10260 {
10261 constructor_constant = TREE_CONSTANT (value);
10262 constructor_simple = TREE_STATIC (value);
10263 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
10264 constructor_elements = CONSTRUCTOR_ELTS (value);
10265 constructor_zero_padding_bits |= CONSTRUCTOR_ZERO_PADDING_BITS (value);
10266 if (!vec_safe_is_empty (constructor_elements)
10267 && (TREE_CODE (constructor_type) == RECORD_TYPE
10268 || TREE_CODE (constructor_type) == ARRAY_TYPE))
10269 set_nonincremental_init (braced_init_obstack);
10270 }
10271
10272 if (implicit == 1)
10273 {
10274 found_missing_braces = 1;
10275 if (initializer_stack->missing_brace_richloc)
10276 initializer_stack->missing_brace_richloc->add_fixit_insert_before
10277 (loc, "{");
10278 }
10279
10280 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10281 {
10282 constructor_fields = TYPE_FIELDS (constructor_type);
10283 /* Skip any nameless bit fields at the beginning. */
10284 while (constructor_fields != NULL_TREE
10285 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10286 constructor_fields = DECL_CHAIN (constructor_fields);
10287
10288 constructor_unfilled_fields = constructor_fields;
10289 constructor_bit_index = bitsize_zero_node;
10290 }
10291 else if (gnu_vector_type_p (constructor_type))
10292 {
10293 /* Vectors are like simple fixed-size arrays. */
10294 constructor_max_index =
10295 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10296 constructor_index = bitsize_int (0);
10297 constructor_unfilled_index = constructor_index;
10298 }
10299 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10300 {
10301 if (TYPE_DOMAIN (constructor_type))
10302 {
10303 constructor_max_index
10304 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10305
10306 /* Detect non-empty initializations of zero-length arrays. */
10307 if (constructor_max_index == NULL_TREE
10308 && TYPE_SIZE (constructor_type))
10309 constructor_max_index = integer_minus_one_node;
10310
10311 /* constructor_max_index needs to be an INTEGER_CST. Attempts
10312 to initialize VLAs will cause a proper error; avoid tree
10313 checking errors as well by setting a safe value. */
10314 if (constructor_max_index
10315 && TREE_CODE (constructor_max_index) != INTEGER_CST)
10316 constructor_max_index = integer_minus_one_node;
10317
10318 constructor_index
10319 = convert (bitsizetype,
10320 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10321 }
10322 else
10323 constructor_index = bitsize_zero_node;
10324
10325 constructor_unfilled_index = constructor_index;
10326 if (value && TREE_CODE (value) == STRING_CST)
10327 {
10328 /* We need to split the char/wchar array into individual
10329 characters, so that we don't have to special case it
10330 everywhere. */
10331 set_nonincremental_init_from_string (value, braced_init_obstack);
10332 }
10333 }
10334 else
10335 {
10336 if (constructor_type != error_mark_node)
10337 warning_init (input_location, 0, "braces around scalar initializer");
10338 constructor_fields = constructor_type;
10339 constructor_unfilled_fields = constructor_type;
10340 }
10341 }
10342
10343 /* At the end of an implicit or explicit brace level,
10344 finish up that level of constructor. If a single expression
10345 with redundant braces initialized that level, return the
10346 c_expr structure for that expression. Otherwise, the original_code
10347 element is set to ERROR_MARK.
10348 If we were outputting the elements as they are read, return 0 as the value
10349 from inner levels (process_init_element ignores that),
10350 but return error_mark_node as the value from the outermost level
10351 (that's what we want to put in DECL_INITIAL).
10352 Otherwise, return a CONSTRUCTOR expression as the value. */
10353
10354 struct c_expr
10355 pop_init_level (location_t loc, int implicit,
10356 struct obstack *braced_init_obstack,
10357 location_t insert_before)
10358 {
10359 struct constructor_stack *p;
10360 struct c_expr ret;
10361 ret.value = NULL_TREE;
10362 ret.original_code = ERROR_MARK;
10363 ret.original_type = NULL;
10364 ret.m_decimal = 0;
10365
10366 if (implicit == 0)
10367 {
10368 /* When we come to an explicit close brace,
10369 pop any inner levels that didn't have explicit braces. */
10370 while (constructor_stack->implicit)
10371 process_init_element (input_location,
10372 pop_init_level (loc, 1, braced_init_obstack,
10373 insert_before),
10374 true, braced_init_obstack);
10375 gcc_assert (!constructor_range_stack);
10376 }
10377 else
10378 if (initializer_stack->missing_brace_richloc)
10379 initializer_stack->missing_brace_richloc->add_fixit_insert_before
10380 (insert_before, "}");
10381
10382 /* Now output all pending elements. */
10383 constructor_incremental = 1;
10384 output_pending_init_elements (1, braced_init_obstack);
10385
10386 p = constructor_stack;
10387
10388 /* Error for initializing a flexible array member, or a zero-length
10389 array member in an inappropriate context. */
10390 if (constructor_type && constructor_fields
10391 && TREE_CODE (constructor_type) == ARRAY_TYPE
10392 && TYPE_DOMAIN (constructor_type)
10393 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
10394 {
10395 /* Silently discard empty initializations. The parser will
10396 already have pedwarned for empty brackets for C17 and earlier. */
10397 if (integer_zerop (constructor_unfilled_index))
10398 constructor_type = NULL_TREE;
10399 if (constructor_type || flag_isoc23)
10400 {
10401 gcc_assert (!constructor_type || !TYPE_SIZE (constructor_type));
10402
10403 if (constructor_depth <= 2)
10404 pedwarn_init (loc, OPT_Wpedantic,
10405 "initialization of a flexible array member");
10406 else if (constructor_type)
10407 error_init (loc, "initialization of flexible array member "
10408 "in a nested context");
10409 else
10410 pedwarn_init (loc, OPT_Wpedantic,
10411 "initialization of flexible array member "
10412 "in a nested context");
10413
10414 /* We have already issued an error message for the existence
10415 of a flexible array member not at the end of the structure.
10416 Discard the initializer so that we do not die later. */
10417 if (constructor_type
10418 && DECL_CHAIN (constructor_fields) != NULL_TREE
10419 && (!p->type || TREE_CODE (p->type) != UNION_TYPE))
10420 constructor_type = NULL_TREE;
10421 }
10422 }
10423
10424 switch (vec_safe_length (constructor_elements))
10425 {
10426 case 0:
10427 /* Initialization with { } counts as zeroinit. */
10428 constructor_zeroinit = 1;
10429 break;
10430 case 1:
10431 /* This might be zeroinit as well. */
10432 if (integer_zerop ((*constructor_elements)[0].value))
10433 constructor_zeroinit = 1;
10434 break;
10435 default:
10436 /* If the constructor has more than one element, it can't be { 0 }. */
10437 constructor_zeroinit = 0;
10438 break;
10439 }
10440
10441 /* Warn when some structs are initialized with direct aggregation. */
10442 if (!implicit && found_missing_braces && warn_missing_braces
10443 && !constructor_zeroinit)
10444 {
10445 gcc_assert (initializer_stack->missing_brace_richloc);
10446 warning_at (initializer_stack->missing_brace_richloc,
10447 OPT_Wmissing_braces,
10448 "missing braces around initializer");
10449 }
10450
10451 /* Warn when some struct elements are implicitly initialized to zero. */
10452 if (warn_missing_field_initializers
10453 && constructor_type
10454 && TREE_CODE (constructor_type) == RECORD_TYPE
10455 && constructor_unfilled_fields)
10456 {
10457 /* Do not warn for flexible array members or zero-length arrays. */
10458 while (constructor_unfilled_fields
10459 && (!DECL_SIZE (constructor_unfilled_fields)
10460 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
10461 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
10462
10463 if (constructor_unfilled_fields
10464 /* Do not warn if this level of the initializer uses member
10465 designators; it is likely to be deliberate. */
10466 && !constructor_designated
10467 /* Do not warn about initializing with { 0 } or with { }. */
10468 && !constructor_zeroinit)
10469 {
10470 if (warning_at (input_location, OPT_Wmissing_field_initializers,
10471 "missing initializer for field %qD of %qT",
10472 constructor_unfilled_fields,
10473 constructor_type))
10474 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
10475 "%qD declared here", constructor_unfilled_fields);
10476 }
10477 }
10478
10479 /* Pad out the end of the structure. */
10480 if (p->replacement_value.value)
10481 /* If this closes a superfluous brace pair,
10482 just pass out the element between them. */
10483 ret = p->replacement_value;
10484 else if (constructor_type == NULL_TREE)
10485 ;
10486 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
10487 && TREE_CODE (constructor_type) != ARRAY_TYPE
10488 && !gnu_vector_type_p (constructor_type))
10489 {
10490 /* A nonincremental scalar initializer--just return
10491 the element, after verifying there is just one.
10492 Empty scalar initializers are supported in C23. */
10493 if (vec_safe_is_empty (constructor_elements))
10494 {
10495 if (constructor_erroneous || constructor_type == error_mark_node)
10496 ret.value = error_mark_node;
10497 else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
10498 {
10499 error_init (loc, "invalid initializer");
10500 ret.value = error_mark_node;
10501 }
10502 else if (TREE_CODE (constructor_type) == POINTER_TYPE)
10503 /* Ensure this is a null pointer constant in the case of a
10504 'constexpr' object initialized with {}. */
10505 ret.value = build_zero_cst (ptr_type_node);
10506 else
10507 ret.value = build_zero_cst (constructor_type);
10508 }
10509 else if (vec_safe_length (constructor_elements) != 1)
10510 {
10511 error_init (loc, "extra elements in scalar initializer");
10512 ret.value = (*constructor_elements)[0].value;
10513 }
10514 else
10515 ret.value = (*constructor_elements)[0].value;
10516 }
10517 else
10518 {
10519 if (constructor_erroneous)
10520 ret.value = error_mark_node;
10521 else
10522 {
10523 ret.value = build_constructor (constructor_type,
10524 constructor_elements);
10525 if (constructor_constant)
10526 TREE_CONSTANT (ret.value) = 1;
10527 if (constructor_constant && constructor_simple)
10528 TREE_STATIC (ret.value) = 1;
10529 if (constructor_nonconst)
10530 CONSTRUCTOR_NON_CONST (ret.value) = 1;
10531 if (constructor_zero_padding_bits)
10532 CONSTRUCTOR_ZERO_PADDING_BITS (ret.value) = 1;
10533 }
10534 }
10535
10536 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
10537 {
10538 if (constructor_nonconst)
10539 ret.original_code = C_MAYBE_CONST_EXPR;
10540 else if (ret.original_code == C_MAYBE_CONST_EXPR)
10541 ret.original_code = ERROR_MARK;
10542 }
10543
10544 constructor_type = p->type;
10545 constructor_fields = p->fields;
10546 constructor_index = p->index;
10547 constructor_max_index = p->max_index;
10548 constructor_unfilled_index = p->unfilled_index;
10549 constructor_unfilled_fields = p->unfilled_fields;
10550 constructor_bit_index = p->bit_index;
10551 constructor_elements = p->elements;
10552 constructor_constant = p->constant;
10553 constructor_simple = p->simple;
10554 constructor_nonconst = p->nonconst;
10555 constructor_erroneous = p->erroneous;
10556 constructor_incremental = p->incremental;
10557 constructor_designated = p->designated;
10558 constructor_zero_padding_bits = p->zero_padding_bits;
10559 designator_depth = p->designator_depth;
10560 constructor_pending_elts = p->pending_elts;
10561 constructor_depth = p->depth;
10562 if (!p->implicit)
10563 constructor_range_stack = p->range_stack;
10564 RESTORE_SPELLING_DEPTH (constructor_depth);
10565
10566 constructor_stack = p->next;
10567 XDELETE (p);
10568
10569 if (ret.value == NULL_TREE && constructor_stack == 0)
10570 ret.value = error_mark_node;
10571 return ret;
10572 }
10573
10574 /* Common handling for both array range and field name designators.
10575 ARRAY argument is nonzero for array ranges. Returns false for success. */
10576
10577 static bool
10578 set_designator (location_t loc, bool array,
10579 struct obstack *braced_init_obstack)
10580 {
10581 tree subtype;
10582 enum tree_code subcode;
10583
10584 /* Don't die if an entire brace-pair level is superfluous
10585 in the containing level, or for an erroneous type. */
10586 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10587 return true;
10588
10589 /* If there were errors in this designator list already, bail out
10590 silently. */
10591 if (designator_erroneous)
10592 return true;
10593
10594 /* Likewise for an initializer for a variable-size type. Those are
10595 diagnosed in the parser, except for empty initializer braces. */
10596 if (COMPLETE_TYPE_P (constructor_type)
10597 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
10598 return true;
10599
10600 if (!designator_depth)
10601 {
10602 gcc_assert (!constructor_range_stack);
10603
10604 /* Designator list starts at the level of closest explicit
10605 braces. */
10606 while (constructor_stack->implicit)
10607 process_init_element (input_location,
10608 pop_init_level (loc, 1, braced_init_obstack,
10609 last_init_list_comma),
10610 true, braced_init_obstack);
10611 constructor_designated = 1;
10612 return false;
10613 }
10614
10615 switch (TREE_CODE (constructor_type))
10616 {
10617 case RECORD_TYPE:
10618 case UNION_TYPE:
10619 subtype = TREE_TYPE (constructor_fields);
10620 if (subtype != error_mark_node)
10621 subtype = TYPE_MAIN_VARIANT (subtype);
10622 break;
10623 case ARRAY_TYPE:
10624 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10625 break;
10626 default:
10627 gcc_unreachable ();
10628 }
10629
10630 subcode = TREE_CODE (subtype);
10631 if (array && subcode != ARRAY_TYPE)
10632 {
10633 error_init (loc, "array index in non-array initializer");
10634 return true;
10635 }
10636 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
10637 {
10638 error_init (loc, "field name not in record or union initializer");
10639 return true;
10640 }
10641
10642 constructor_designated = 1;
10643 finish_implicit_inits (loc, braced_init_obstack);
10644 push_init_level (loc, 2, braced_init_obstack);
10645 return false;
10646 }
10647
10648 /* If there are range designators in designator list, push a new designator
10649 to constructor_range_stack. RANGE_END is end of such stack range or
10650 NULL_TREE if there is no range designator at this level. */
10651
10652 static void
10653 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
10654 {
10655 struct constructor_range_stack *p;
10656
10657 p = (struct constructor_range_stack *)
10658 obstack_alloc (braced_init_obstack,
10659 sizeof (struct constructor_range_stack));
10660 p->prev = constructor_range_stack;
10661 p->next = 0;
10662 p->fields = constructor_fields;
10663 p->range_start = constructor_index;
10664 p->index = constructor_index;
10665 p->stack = constructor_stack;
10666 p->range_end = range_end;
10667 if (constructor_range_stack)
10668 constructor_range_stack->next = p;
10669 constructor_range_stack = p;
10670 }
10671
10672 /* Within an array initializer, specify the next index to be initialized.
10673 FIRST is that index. If LAST is nonzero, then initialize a range
10674 of indices, running from FIRST through LAST. */
10675
10676 void
10677 set_init_index (location_t loc, tree first, tree last,
10678 struct obstack *braced_init_obstack)
10679 {
10680 if (set_designator (loc, true, braced_init_obstack))
10681 return;
10682
10683 designator_erroneous = 1;
10684
10685 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
10686 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
10687 {
10688 error_init (loc, "array index in initializer not of integer type");
10689 return;
10690 }
10691
10692 if (TREE_CODE (first) != INTEGER_CST)
10693 {
10694 first = c_fully_fold (first, false, NULL);
10695 if (TREE_CODE (first) == INTEGER_CST)
10696 pedwarn_init (loc, OPT_Wpedantic,
10697 "array index in initializer is not "
10698 "an integer constant expression");
10699 }
10700
10701 if (last && TREE_CODE (last) != INTEGER_CST)
10702 {
10703 last = c_fully_fold (last, false, NULL);
10704 if (TREE_CODE (last) == INTEGER_CST)
10705 pedwarn_init (loc, OPT_Wpedantic,
10706 "array index in initializer is not "
10707 "an integer constant expression");
10708 }
10709
10710 if (TREE_CODE (first) != INTEGER_CST)
10711 error_init (loc, "nonconstant array index in initializer");
10712 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
10713 error_init (loc, "nonconstant array index in initializer");
10714 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
10715 error_init (loc, "array index in non-array initializer");
10716 else if (tree_int_cst_sgn (first) == -1)
10717 error_init (loc, "array index in initializer exceeds array bounds");
10718 else if (constructor_max_index
10719 && tree_int_cst_lt (constructor_max_index, first))
10720 error_init (loc, "array index in initializer exceeds array bounds");
10721 else
10722 {
10723 constant_expression_warning (first);
10724 if (last)
10725 constant_expression_warning (last);
10726 constructor_index = convert (bitsizetype, first);
10727 if (tree_int_cst_lt (constructor_index, first))
10728 {
10729 constructor_index = copy_node (constructor_index);
10730 TREE_OVERFLOW (constructor_index) = 1;
10731 }
10732
10733 if (last)
10734 {
10735 if (tree_int_cst_equal (first, last))
10736 last = NULL_TREE;
10737 else if (tree_int_cst_lt (last, first))
10738 {
10739 error_init (loc, "empty index range in initializer");
10740 last = NULL_TREE;
10741 }
10742 else
10743 {
10744 last = convert (bitsizetype, last);
10745 if (constructor_max_index != NULL_TREE
10746 && tree_int_cst_lt (constructor_max_index, last))
10747 {
10748 error_init (loc, "array index range in initializer exceeds "
10749 "array bounds");
10750 last = NULL_TREE;
10751 }
10752 }
10753 }
10754
10755 designator_depth++;
10756 designator_erroneous = 0;
10757 if (constructor_range_stack || last)
10758 push_range_stack (last, braced_init_obstack);
10759 }
10760 }
10761
10762 /* Within a struct initializer, specify the next field to be initialized. */
10763
10764 void
10765 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
10766 struct obstack *braced_init_obstack)
10767 {
10768 tree field;
10769
10770 if (set_designator (loc, false, braced_init_obstack))
10771 return;
10772
10773 designator_erroneous = 1;
10774
10775 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
10776 {
10777 error_init (loc, "field name not in record or union initializer");
10778 return;
10779 }
10780
10781 field = lookup_field (constructor_type, fieldname);
10782
10783 if (field == NULL_TREE)
10784 {
10785 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
10786 if (guessed_id)
10787 {
10788 gcc_rich_location rich_loc (fieldname_loc);
10789 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
10790 error_at (&rich_loc,
10791 "%qT has no member named %qE; did you mean %qE?",
10792 constructor_type, fieldname, guessed_id);
10793 }
10794 else
10795 error_at (fieldname_loc, "%qT has no member named %qE",
10796 constructor_type, fieldname);
10797 }
10798 else
10799 do
10800 {
10801 constructor_fields = TREE_VALUE (field);
10802 designator_depth++;
10803 designator_erroneous = 0;
10804 if (constructor_range_stack)
10805 push_range_stack (NULL_TREE, braced_init_obstack);
10806 field = TREE_CHAIN (field);
10807 if (field)
10808 {
10809 if (set_designator (loc, false, braced_init_obstack))
10810 return;
10811 }
10812 }
10813 while (field != NULL_TREE);
10814 }
10815
10816 /* Helper function for add_pending_init. Find inorder successor of P
10817 in AVL tree. */
10818 static struct init_node *
10819 init_node_successor (struct init_node *p)
10820 {
10821 struct init_node *r;
10822 if (p->right)
10823 {
10824 r = p->right;
10825 while (r->left)
10826 r = r->left;
10827 return r;
10828 }
10829 r = p->parent;
10830 while (r && p == r->right)
10831 {
10832 p = r;
10833 r = r->parent;
10834 }
10835 return r;
10836 }
10837
10838 /* Add a new initializer to the tree of pending initializers. PURPOSE
10839 identifies the initializer, either array index or field in a structure.
10840 VALUE is the value of that index or field. If ORIGTYPE is not
10841 NULL_TREE, it is the original type of VALUE.
10842
10843 IMPLICIT is true if value comes from pop_init_level (1),
10844 the new initializer has been merged with the existing one
10845 and thus no warnings should be emitted about overriding an
10846 existing initializer. */
10847
10848 static void
10849 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
10850 bool implicit, struct obstack *braced_init_obstack)
10851 {
10852 struct init_node *p, **q, *r;
10853
10854 q = &constructor_pending_elts;
10855 p = 0;
10856
10857 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10858 {
10859 while (*q != 0)
10860 {
10861 p = *q;
10862 if (tree_int_cst_lt (purpose, p->purpose))
10863 q = &p->left;
10864 else if (tree_int_cst_lt (p->purpose, purpose))
10865 {
10866 if (TREE_CODE (p->value) != RAW_DATA_CST
10867 || (p->right
10868 && tree_int_cst_le (p->right->purpose, purpose)))
10869 q = &p->right;
10870 else
10871 {
10872 widest_int pp = wi::to_widest (p->purpose);
10873 widest_int pw = wi::to_widest (purpose);
10874 if (pp + RAW_DATA_LENGTH (p->value) <= pw)
10875 q = &p->right;
10876 else
10877 {
10878 /* Override which should split the old RAW_DATA_CST
10879 into 2 or 3 pieces. */
10880 if (!implicit && warn_override_init)
10881 warning_init (loc, OPT_Woverride_init,
10882 "initialized field overwritten");
10883 unsigned HOST_WIDE_INT start = (pw - pp).to_uhwi ();
10884 unsigned HOST_WIDE_INT len = 1;
10885 if (TREE_CODE (value) == RAW_DATA_CST)
10886 len = RAW_DATA_LENGTH (value);
10887 unsigned HOST_WIDE_INT end = 0;
10888 unsigned plen = RAW_DATA_LENGTH (p->value);
10889 gcc_checking_assert (start < plen && start);
10890 if (plen - start > len)
10891 end = plen - start - len;
10892 tree v = p->value;
10893 tree origtype = p->origtype;
10894 if (start == 1)
10895 p->value = build_int_cst (TREE_TYPE (v),
10896 RAW_DATA_UCHAR_ELT (v, 0));
10897 else
10898 {
10899 p->value = v;
10900 if (end > 1)
10901 v = copy_node (v);
10902 RAW_DATA_LENGTH (p->value) = start;
10903 }
10904 if (end)
10905 {
10906 tree epurpose
10907 = size_binop (PLUS_EXPR, purpose,
10908 bitsize_int (len));
10909 if (end > 1)
10910 {
10911 RAW_DATA_LENGTH (v) -= plen - end;
10912 RAW_DATA_POINTER (v) += plen - end;
10913 }
10914 else
10915 v = build_int_cst (TREE_TYPE (v),
10916 RAW_DATA_UCHAR_ELT (v, plen
10917 - end));
10918 add_pending_init (loc, epurpose, v, origtype,
10919 implicit, braced_init_obstack);
10920 }
10921 q = &constructor_pending_elts;
10922 continue;
10923 }
10924 }
10925 }
10926 else
10927 {
10928 if (TREE_CODE (p->value) == RAW_DATA_CST
10929 && (RAW_DATA_LENGTH (p->value)
10930 > (TREE_CODE (value) == RAW_DATA_CST
10931 ? RAW_DATA_LENGTH (value) : 1)))
10932 {
10933 /* Override which should split the old RAW_DATA_CST
10934 into 2 pieces. */
10935 if (!implicit && warn_override_init)
10936 warning_init (loc, OPT_Woverride_init,
10937 "initialized field overwritten");
10938 unsigned HOST_WIDE_INT len = 1;
10939 if (TREE_CODE (value) == RAW_DATA_CST)
10940 len = RAW_DATA_LENGTH (value);
10941 if ((unsigned) RAW_DATA_LENGTH (p->value) > len + 1)
10942 {
10943 RAW_DATA_LENGTH (p->value) -= len;
10944 RAW_DATA_POINTER (p->value) += len;
10945 }
10946 else
10947 {
10948 unsigned int l = RAW_DATA_LENGTH (p->value) - 1;
10949 p->value
10950 = build_int_cst (TREE_TYPE (p->value),
10951 RAW_DATA_UCHAR_ELT (p->value, l));
10952 }
10953 p->purpose = size_binop (PLUS_EXPR, p->purpose,
10954 bitsize_int (len));
10955 continue;
10956 }
10957 if (TREE_CODE (value) == RAW_DATA_CST)
10958 {
10959 handle_raw_data:
10960 /* RAW_DATA_CST value might overlap various further
10961 prior initval entries. Find out how many. */
10962 unsigned cnt = 0;
10963 widest_int w
10964 = wi::to_widest (purpose) + RAW_DATA_LENGTH (value);
10965 struct init_node *r = p, *last = NULL;
10966 bool override_init = warn_override_init;
10967 while ((r = init_node_successor (r))
10968 && wi::to_widest (r->purpose) < w)
10969 {
10970 ++cnt;
10971 if (TREE_SIDE_EFFECTS (r->value))
10972 warning_init (loc, OPT_Woverride_init_side_effects,
10973 "initialized field with side-effects "
10974 "overwritten");
10975 else if (override_init)
10976 {
10977 warning_init (loc, OPT_Woverride_init,
10978 "initialized field overwritten");
10979 override_init = false;
10980 }
10981 last = r;
10982 }
10983 if (cnt)
10984 {
10985 if (TREE_CODE (last->value) == RAW_DATA_CST
10986 && (wi::to_widest (last->purpose)
10987 + RAW_DATA_LENGTH (last->value) > w))
10988 {
10989 /* The last overlapping prior initval overlaps
10990 only partially. Shrink it and decrease cnt. */
10991 unsigned int l = (wi::to_widest (last->purpose)
10992 + RAW_DATA_LENGTH (last->value)
10993 - w).to_uhwi ();
10994 --cnt;
10995 RAW_DATA_LENGTH (last->value) -= l;
10996 RAW_DATA_POINTER (last->value) += l;
10997 if (RAW_DATA_LENGTH (last->value) == 1)
10998 last->value
10999 = build_int_cst (TREE_TYPE (last->value),
11000 RAW_DATA_UCHAR_ELT (last->value,
11001 0));
11002 last->purpose
11003 = size_binop (PLUS_EXPR, last->purpose,
11004 bitsize_int (l));
11005 }
11006 /* Instead of deleting cnt nodes from the AVL tree
11007 and rebalancing, peel of last cnt bytes from the
11008 RAW_DATA_CST. Overriding thousands of previously
11009 initialized array elements with #embed needs to work,
11010 but doesn't need to be super efficient. */
11011 gcc_checking_assert ((unsigned) RAW_DATA_LENGTH (value)
11012 > cnt);
11013 RAW_DATA_LENGTH (value) -= cnt;
11014 const unsigned char *s
11015 = &RAW_DATA_UCHAR_ELT (value, RAW_DATA_LENGTH (value));
11016 unsigned int o = RAW_DATA_LENGTH (value);
11017 for (r = p; cnt--; ++o, ++s)
11018 {
11019 r = init_node_successor (r);
11020 r->purpose = size_binop (PLUS_EXPR, purpose,
11021 bitsize_int (o));
11022 r->value = build_int_cst (TREE_TYPE (value), *s);
11023 r->origtype = origtype;
11024 }
11025 if (RAW_DATA_LENGTH (value) == 1)
11026 value = build_int_cst (TREE_TYPE (value),
11027 RAW_DATA_UCHAR_ELT (value, 0));
11028 }
11029 }
11030 if (!implicit)
11031 {
11032 if (TREE_SIDE_EFFECTS (p->value))
11033 warning_init (loc, OPT_Woverride_init_side_effects,
11034 "initialized field with side-effects "
11035 "overwritten");
11036 else if (warn_override_init)
11037 warning_init (loc, OPT_Woverride_init,
11038 "initialized field overwritten");
11039 }
11040 p->value = value;
11041 p->origtype = origtype;
11042 return;
11043 }
11044 }
11045 if (TREE_CODE (value) == RAW_DATA_CST && p)
11046 {
11047 struct init_node *r;
11048 if (q == &p->left)
11049 r = p;
11050 else
11051 r = init_node_successor (p);
11052 if (r && wi::to_widest (r->purpose) < (wi::to_widest (purpose)
11053 + RAW_DATA_LENGTH (value)))
11054 {
11055 /* Overlap with at least one prior initval in the range but
11056 not at the start. */
11057 p = r;
11058 p->purpose = purpose;
11059 goto handle_raw_data;
11060 }
11061 }
11062 }
11063 else
11064 {
11065 tree bitpos;
11066
11067 bitpos = bit_position (purpose);
11068 while (*q != NULL)
11069 {
11070 p = *q;
11071 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11072 q = &p->left;
11073 else if (p->purpose != purpose)
11074 q = &p->right;
11075 else
11076 {
11077 if (!implicit)
11078 {
11079 if (TREE_SIDE_EFFECTS (p->value))
11080 warning_init (loc, OPT_Woverride_init_side_effects,
11081 "initialized field with side-effects "
11082 "overwritten");
11083 else if (warn_override_init)
11084 warning_init (loc, OPT_Woverride_init,
11085 "initialized field overwritten");
11086 }
11087 p->value = value;
11088 p->origtype = origtype;
11089 return;
11090 }
11091 }
11092 }
11093
11094 r = (struct init_node *) obstack_alloc (braced_init_obstack,
11095 sizeof (struct init_node));
11096 r->purpose = purpose;
11097 r->value = value;
11098 r->origtype = origtype;
11099
11100 *q = r;
11101 r->parent = p;
11102 r->left = 0;
11103 r->right = 0;
11104 r->balance = 0;
11105
11106 while (p)
11107 {
11108 struct init_node *s;
11109
11110 if (r == p->left)
11111 {
11112 if (p->balance == 0)
11113 p->balance = -1;
11114 else if (p->balance < 0)
11115 {
11116 if (r->balance < 0)
11117 {
11118 /* L rotation. */
11119 p->left = r->right;
11120 if (p->left)
11121 p->left->parent = p;
11122 r->right = p;
11123
11124 p->balance = 0;
11125 r->balance = 0;
11126
11127 s = p->parent;
11128 p->parent = r;
11129 r->parent = s;
11130 if (s)
11131 {
11132 if (s->left == p)
11133 s->left = r;
11134 else
11135 s->right = r;
11136 }
11137 else
11138 constructor_pending_elts = r;
11139 }
11140 else
11141 {
11142 /* LR rotation. */
11143 struct init_node *t = r->right;
11144
11145 r->right = t->left;
11146 if (r->right)
11147 r->right->parent = r;
11148 t->left = r;
11149
11150 p->left = t->right;
11151 if (p->left)
11152 p->left->parent = p;
11153 t->right = p;
11154
11155 p->balance = t->balance < 0;
11156 r->balance = -(t->balance > 0);
11157 t->balance = 0;
11158
11159 s = p->parent;
11160 p->parent = t;
11161 r->parent = t;
11162 t->parent = s;
11163 if (s)
11164 {
11165 if (s->left == p)
11166 s->left = t;
11167 else
11168 s->right = t;
11169 }
11170 else
11171 constructor_pending_elts = t;
11172 }
11173 break;
11174 }
11175 else
11176 {
11177 /* p->balance == +1; growth of left side balances the node. */
11178 p->balance = 0;
11179 break;
11180 }
11181 }
11182 else /* r == p->right */
11183 {
11184 if (p->balance == 0)
11185 /* Growth propagation from right side. */
11186 p->balance++;
11187 else if (p->balance > 0)
11188 {
11189 if (r->balance > 0)
11190 {
11191 /* R rotation. */
11192 p->right = r->left;
11193 if (p->right)
11194 p->right->parent = p;
11195 r->left = p;
11196
11197 p->balance = 0;
11198 r->balance = 0;
11199
11200 s = p->parent;
11201 p->parent = r;
11202 r->parent = s;
11203 if (s)
11204 {
11205 if (s->left == p)
11206 s->left = r;
11207 else
11208 s->right = r;
11209 }
11210 else
11211 constructor_pending_elts = r;
11212 }
11213 else /* r->balance == -1 */
11214 {
11215 /* RL rotation */
11216 struct init_node *t = r->left;
11217
11218 r->left = t->right;
11219 if (r->left)
11220 r->left->parent = r;
11221 t->right = r;
11222
11223 p->right = t->left;
11224 if (p->right)
11225 p->right->parent = p;
11226 t->left = p;
11227
11228 r->balance = (t->balance < 0);
11229 p->balance = -(t->balance > 0);
11230 t->balance = 0;
11231
11232 s = p->parent;
11233 p->parent = t;
11234 r->parent = t;
11235 t->parent = s;
11236 if (s)
11237 {
11238 if (s->left == p)
11239 s->left = t;
11240 else
11241 s->right = t;
11242 }
11243 else
11244 constructor_pending_elts = t;
11245 }
11246 break;
11247 }
11248 else
11249 {
11250 /* p->balance == -1; growth of right side balances the node. */
11251 p->balance = 0;
11252 break;
11253 }
11254 }
11255
11256 r = p;
11257 p = p->parent;
11258 }
11259 }
11260
11261 /* Build AVL tree from a sorted chain. */
11262
11263 static void
11264 set_nonincremental_init (struct obstack * braced_init_obstack)
11265 {
11266 unsigned HOST_WIDE_INT ix;
11267 tree index, value;
11268
11269 if (TREE_CODE (constructor_type) != RECORD_TYPE
11270 && TREE_CODE (constructor_type) != ARRAY_TYPE)
11271 return;
11272
11273 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
11274 add_pending_init (input_location, index, value, NULL_TREE, true,
11275 braced_init_obstack);
11276 constructor_elements = NULL;
11277 if (TREE_CODE (constructor_type) == RECORD_TYPE)
11278 {
11279 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
11280 /* Skip any nameless bit fields at the beginning. */
11281 while (constructor_unfilled_fields != NULL_TREE
11282 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
11283 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
11284
11285 }
11286 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11287 {
11288 if (TYPE_DOMAIN (constructor_type))
11289 constructor_unfilled_index
11290 = convert (bitsizetype,
11291 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
11292 else
11293 constructor_unfilled_index = bitsize_zero_node;
11294 }
11295 constructor_incremental = 0;
11296 }
11297
11298 /* Build AVL tree from a string constant. */
11299
11300 static void
11301 set_nonincremental_init_from_string (tree str,
11302 struct obstack * braced_init_obstack)
11303 {
11304 tree value, purpose, type;
11305 HOST_WIDE_INT val[2];
11306 const char *p, *end;
11307 int byte, wchar_bytes, charwidth, bitpos;
11308
11309 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
11310
11311 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
11312 charwidth = TYPE_PRECISION (char_type_node);
11313 gcc_assert ((size_t) wchar_bytes * charwidth
11314 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
11315 type = TREE_TYPE (constructor_type);
11316 p = TREE_STRING_POINTER (str);
11317 end = p + TREE_STRING_LENGTH (str);
11318
11319 for (purpose = bitsize_zero_node;
11320 p < end
11321 && !(constructor_max_index
11322 && tree_int_cst_lt (constructor_max_index, purpose));
11323 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
11324 {
11325 if (wchar_bytes == 1)
11326 {
11327 val[0] = (unsigned char) *p++;
11328 val[1] = 0;
11329 }
11330 else
11331 {
11332 val[1] = 0;
11333 val[0] = 0;
11334 for (byte = 0; byte < wchar_bytes; byte++)
11335 {
11336 if (BYTES_BIG_ENDIAN)
11337 bitpos = (wchar_bytes - byte - 1) * charwidth;
11338 else
11339 bitpos = byte * charwidth;
11340 val[bitpos / HOST_BITS_PER_WIDE_INT]
11341 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
11342 << (bitpos % HOST_BITS_PER_WIDE_INT);
11343 }
11344 }
11345
11346 if (!TYPE_UNSIGNED (type))
11347 {
11348 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
11349 if (bitpos < HOST_BITS_PER_WIDE_INT)
11350 {
11351 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
11352 {
11353 val[0] |= HOST_WIDE_INT_M1U << bitpos;
11354 val[1] = -1;
11355 }
11356 }
11357 else if (bitpos == HOST_BITS_PER_WIDE_INT)
11358 {
11359 if (val[0] < 0)
11360 val[1] = -1;
11361 }
11362 else if (val[1] & (HOST_WIDE_INT_1
11363 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
11364 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
11365 }
11366
11367 value = wide_int_to_tree (type,
11368 wide_int::from_array (val, 2,
11369 HOST_BITS_PER_WIDE_INT * 2));
11370 add_pending_init (input_location, purpose, value, NULL_TREE, true,
11371 braced_init_obstack);
11372 }
11373
11374 constructor_incremental = 0;
11375 }
11376
11377 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
11378 not initialized yet. */
11379
11380 static tree
11381 find_init_member (tree field, struct obstack * braced_init_obstack)
11382 {
11383 struct init_node *p;
11384
11385 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11386 {
11387 if (constructor_incremental
11388 && tree_int_cst_lt (field, constructor_unfilled_index))
11389 set_nonincremental_init (braced_init_obstack);
11390
11391 p = constructor_pending_elts;
11392 while (p)
11393 {
11394 if (tree_int_cst_lt (field, p->purpose))
11395 p = p->left;
11396 else if (tree_int_cst_lt (p->purpose, field))
11397 p = p->right;
11398 else
11399 return p->value;
11400 }
11401 }
11402 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
11403 {
11404 tree bitpos = bit_position (field);
11405
11406 if (constructor_incremental
11407 && (!constructor_unfilled_fields
11408 || tree_int_cst_lt (bitpos,
11409 bit_position (constructor_unfilled_fields))))
11410 set_nonincremental_init (braced_init_obstack);
11411
11412 p = constructor_pending_elts;
11413 while (p)
11414 {
11415 if (field == p->purpose)
11416 return p->value;
11417 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11418 p = p->left;
11419 else
11420 p = p->right;
11421 }
11422 }
11423 else if (TREE_CODE (constructor_type) == UNION_TYPE)
11424 {
11425 if (!vec_safe_is_empty (constructor_elements)
11426 && (constructor_elements->last ().index == field))
11427 return constructor_elements->last ().value;
11428 }
11429 return NULL_TREE;
11430 }
11431
11432 /* "Output" the next constructor element.
11433 At top level, really output it to assembler code now.
11434 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
11435 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
11436 TYPE is the data type that the containing data type wants here.
11437 FIELD is the field (a FIELD_DECL) or the index that this element fills.
11438 If VALUE is a string constant, STRICT_STRING is true if it is
11439 unparenthesized or we should not warn here for it being parenthesized.
11440 For other types of VALUE, STRICT_STRING is not used.
11441
11442 PENDING if true means output pending elements that belong
11443 right after this element. (PENDING is normally true;
11444 it is false while outputting pending elements, to avoid recursion.)
11445
11446 IMPLICIT is true if value comes from pop_init_level (1),
11447 the new initializer has been merged with the existing one
11448 and thus no warnings should be emitted about overriding an
11449 existing initializer. */
11450
11451 static void
11452 output_init_element (location_t loc, tree value, tree origtype,
11453 bool strict_string, tree type, tree field, bool pending,
11454 bool implicit, struct obstack * braced_init_obstack)
11455 {
11456 tree semantic_type = NULL_TREE;
11457 bool maybe_const = true;
11458 bool npc, int_const_expr, arith_const_expr;
11459
11460 if (type == error_mark_node || value == error_mark_node)
11461 {
11462 constructor_erroneous = 1;
11463 return;
11464 }
11465 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
11466 && (TREE_CODE (value) == STRING_CST
11467 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
11468 && !(TREE_CODE (value) == STRING_CST
11469 && TREE_CODE (type) == ARRAY_TYPE
11470 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
11471 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
11472 TYPE_MAIN_VARIANT (type)))
11473 value = array_to_pointer_conversion (input_location, value);
11474
11475 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
11476 && require_constant_value && pending)
11477 {
11478 /* As an extension, allow initializing objects with static storage
11479 duration with compound literals (which are then treated just as
11480 the brace enclosed list they contain). */
11481 if (flag_isoc99)
11482 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
11483 "constant");
11484 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
11485 value = DECL_INITIAL (decl);
11486 }
11487
11488 npc = null_pointer_constant_p (value);
11489 int_const_expr = (TREE_CODE (value) == INTEGER_CST
11490 && !TREE_OVERFLOW (value)
11491 && INTEGRAL_TYPE_P (TREE_TYPE (value)));
11492 /* Not fully determined before folding. */
11493 arith_const_expr = true;
11494 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
11495 {
11496 semantic_type = TREE_TYPE (value);
11497 value = TREE_OPERAND (value, 0);
11498 }
11499 value = c_fully_fold (value, require_constant_value, &maybe_const);
11500 /* TODO: this may not detect all cases of expressions folding to
11501 constants that are not arithmetic constant expressions. */
11502 if (!maybe_const)
11503 arith_const_expr = false;
11504 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
11505 && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
11506 && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
11507 arith_const_expr = false;
11508 else if (TREE_CODE (value) != INTEGER_CST
11509 && TREE_CODE (value) != REAL_CST
11510 && TREE_CODE (value) != COMPLEX_CST
11511 && TREE_CODE (value) != RAW_DATA_CST)
11512 arith_const_expr = false;
11513 else if (TREE_OVERFLOW (value))
11514 arith_const_expr = false;
11515
11516 if (value == error_mark_node)
11517 constructor_erroneous = 1;
11518 else if (!TREE_CONSTANT (value))
11519 constructor_constant = 0;
11520 else if (!initializer_constant_valid_p (value,
11521 TREE_TYPE (value),
11522 AGGREGATE_TYPE_P (constructor_type)
11523 && TYPE_REVERSE_STORAGE_ORDER
11524 (constructor_type))
11525 || (RECORD_OR_UNION_TYPE_P (constructor_type)
11526 && DECL_C_BIT_FIELD (field)
11527 && TREE_CODE (value) != INTEGER_CST))
11528 constructor_simple = 0;
11529 if (!maybe_const)
11530 constructor_nonconst = 1;
11531
11532 /* Digest the initializer and issue any errors about incompatible
11533 types before issuing errors about non-constant initializers. */
11534 tree new_value = value;
11535 if (semantic_type)
11536 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
11537 /* In the case of braces around a scalar initializer, the result of
11538 this initializer processing goes through digest_init again at the
11539 outer level. In the case of a constexpr initializer for a
11540 pointer, avoid converting a null pointer constant to something
11541 that is not a null pointer constant to avoid a spurious error
11542 from that second processing. */
11543 if (!require_constexpr_value
11544 || !npc
11545 || TREE_CODE (constructor_type) != POINTER_TYPE)
11546 new_value = digest_init (loc,
11547 RECORD_OR_UNION_TYPE_P (constructor_type)
11548 ? field : constructor_fields
11549 ? constructor_fields : constructor_decl,
11550 type, new_value, origtype, npc,
11551 int_const_expr, arith_const_expr, strict_string,
11552 require_constant_value, require_constexpr_value);
11553 if (new_value == error_mark_node)
11554 {
11555 constructor_erroneous = 1;
11556 return;
11557 }
11558 if (require_constant_value || require_constant_elements)
11559 constant_expression_warning (new_value);
11560
11561 /* Proceed to check the constness of the original initializer. */
11562 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
11563 {
11564 if (require_constant_value)
11565 {
11566 error_init (loc, "initializer element is not constant");
11567 value = error_mark_node;
11568 }
11569 else if (require_constant_elements)
11570 pedwarn (loc, OPT_Wpedantic,
11571 "initializer element is not computable at load time");
11572 }
11573 else if (!maybe_const
11574 && (require_constant_value || require_constant_elements))
11575 pedwarn_init (loc, OPT_Wpedantic,
11576 "initializer element is not a constant expression");
11577 /* digest_init has already carried out the additional checks
11578 required for 'constexpr' initializers (using the information
11579 passed to it about whether the original initializer was certain
11580 kinds of constant expression), so that check does not need to be
11581 repeated here. */
11582
11583 /* Issue -Wc++-compat warnings about initializing a bitfield with
11584 enum type. */
11585 if (warn_cxx_compat
11586 && field != NULL_TREE
11587 && TREE_CODE (field) == FIELD_DECL
11588 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
11589 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
11590 != TYPE_MAIN_VARIANT (type))
11591 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
11592 {
11593 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
11594 if (checktype != error_mark_node
11595 && (TYPE_MAIN_VARIANT (checktype)
11596 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
11597 warning_init (loc, OPT_Wc___compat,
11598 "enum conversion in initialization is invalid in C++");
11599 }
11600
11601 /* If this field is empty and does not have side effects (and is not at
11602 the end of structure), don't do anything other than checking the
11603 initializer. */
11604 if (field
11605 && (TREE_TYPE (field) == error_mark_node
11606 || (COMPLETE_TYPE_P (TREE_TYPE (field))
11607 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
11608 && !TREE_SIDE_EFFECTS (new_value)
11609 && (TREE_CODE (constructor_type) == ARRAY_TYPE
11610 || DECL_CHAIN (field)))))
11611 return;
11612
11613 /* Finally, set VALUE to the initializer value digested above. */
11614 value = new_value;
11615
11616 /* If this element doesn't come next in sequence,
11617 put it on constructor_pending_elts. */
11618 if (TREE_CODE (constructor_type) == ARRAY_TYPE
11619 && (!constructor_incremental
11620 || !tree_int_cst_equal (field, constructor_unfilled_index)
11621 || (TREE_CODE (value) == RAW_DATA_CST
11622 && constructor_pending_elts
11623 && pending)))
11624 {
11625 if (constructor_incremental
11626 && (tree_int_cst_lt (field, constructor_unfilled_index)
11627 || (TREE_CODE (value) == RAW_DATA_CST
11628 && constructor_pending_elts
11629 && pending)))
11630 set_nonincremental_init (braced_init_obstack);
11631
11632 add_pending_init (loc, field, value, origtype, implicit,
11633 braced_init_obstack);
11634 return;
11635 }
11636 else if (TREE_CODE (constructor_type) == RECORD_TYPE
11637 && (!constructor_incremental
11638 || field != constructor_unfilled_fields))
11639 {
11640 /* We do this for records but not for unions. In a union,
11641 no matter which field is specified, it can be initialized
11642 right away since it starts at the beginning of the union. */
11643 if (constructor_incremental)
11644 {
11645 if (!constructor_unfilled_fields)
11646 set_nonincremental_init (braced_init_obstack);
11647 else
11648 {
11649 tree bitpos, unfillpos;
11650
11651 bitpos = bit_position (field);
11652 unfillpos = bit_position (constructor_unfilled_fields);
11653
11654 if (tree_int_cst_lt (bitpos, unfillpos))
11655 set_nonincremental_init (braced_init_obstack);
11656 }
11657 }
11658
11659 add_pending_init (loc, field, value, origtype, implicit,
11660 braced_init_obstack);
11661 return;
11662 }
11663 else if (TREE_CODE (constructor_type) == UNION_TYPE
11664 && !vec_safe_is_empty (constructor_elements))
11665 {
11666 if (!implicit)
11667 {
11668 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
11669 warning_init (loc, OPT_Woverride_init_side_effects,
11670 "initialized field with side-effects overwritten");
11671 else if (warn_override_init)
11672 warning_init (loc, OPT_Woverride_init,
11673 "initialized field overwritten");
11674 }
11675
11676 /* We can have just one union field set. */
11677 constructor_elements = NULL;
11678 }
11679
11680 /* Otherwise, output this element either to
11681 constructor_elements or to the assembler file. */
11682
11683 constructor_elt celt = {field, value};
11684 vec_safe_push (constructor_elements, celt);
11685
11686 /* Advance the variable that indicates sequential elements output. */
11687 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11688 {
11689 tree inc = bitsize_one_node;
11690 if (value && TREE_CODE (value) == RAW_DATA_CST)
11691 inc = bitsize_int (RAW_DATA_LENGTH (value));
11692 constructor_unfilled_index
11693 = size_binop_loc (input_location, PLUS_EXPR,
11694 constructor_unfilled_index, inc);
11695 }
11696 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
11697 {
11698 constructor_unfilled_fields
11699 = DECL_CHAIN (constructor_unfilled_fields);
11700
11701 /* Skip any nameless bit fields. */
11702 while (constructor_unfilled_fields != NULL_TREE
11703 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
11704 constructor_unfilled_fields
11705 = DECL_CHAIN (constructor_unfilled_fields);
11706 }
11707 else if (TREE_CODE (constructor_type) == UNION_TYPE)
11708 constructor_unfilled_fields = NULL_TREE;
11709
11710 /* Now output any pending elements which have become next. */
11711 if (pending)
11712 output_pending_init_elements (0, braced_init_obstack);
11713 }
11714
11715 /* For two FIELD_DECLs in the same chain, return -1 if field1
11716 comes before field2, 1 if field1 comes after field2 and
11717 0 if field1 == field2. */
11718
11719 static int
11720 init_field_decl_cmp (tree field1, tree field2)
11721 {
11722 if (field1 == field2)
11723 return 0;
11724
11725 tree bitpos1 = bit_position (field1);
11726 tree bitpos2 = bit_position (field2);
11727 if (tree_int_cst_equal (bitpos1, bitpos2))
11728 {
11729 /* If one of the fields has non-zero bitsize, then that
11730 field must be the last one in a sequence of zero
11731 sized fields, fields after it will have bigger
11732 bit_position. */
11733 if (TREE_TYPE (field1) != error_mark_node
11734 && COMPLETE_TYPE_P (TREE_TYPE (field1))
11735 && integer_nonzerop (TREE_TYPE (field1)))
11736 return 1;
11737 if (TREE_TYPE (field2) != error_mark_node
11738 && COMPLETE_TYPE_P (TREE_TYPE (field2))
11739 && integer_nonzerop (TREE_TYPE (field2)))
11740 return -1;
11741 /* Otherwise, fallback to DECL_CHAIN walk to find out
11742 which field comes earlier. Walk chains of both
11743 fields, so that if field1 and field2 are close to each
11744 other in either order, it is found soon even for large
11745 sequences of zero sized fields. */
11746 tree f1 = field1, f2 = field2;
11747 while (1)
11748 {
11749 f1 = DECL_CHAIN (f1);
11750 f2 = DECL_CHAIN (f2);
11751 if (f1 == NULL_TREE)
11752 {
11753 gcc_assert (f2);
11754 return 1;
11755 }
11756 if (f2 == NULL_TREE)
11757 return -1;
11758 if (f1 == field2)
11759 return -1;
11760 if (f2 == field1)
11761 return 1;
11762 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
11763 return 1;
11764 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
11765 return -1;
11766 }
11767 }
11768 else if (tree_int_cst_lt (bitpos1, bitpos2))
11769 return -1;
11770 else
11771 return 1;
11772 }
11773
11774 /* Output any pending elements which have become next.
11775 As we output elements, constructor_unfilled_{fields,index}
11776 advances, which may cause other elements to become next;
11777 if so, they too are output.
11778
11779 If ALL is 0, we return when there are
11780 no more pending elements to output now.
11781
11782 If ALL is 1, we output space as necessary so that
11783 we can output all the pending elements. */
11784 static void
11785 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
11786 {
11787 struct init_node *elt = constructor_pending_elts;
11788 tree next;
11789
11790 retry:
11791
11792 /* Look through the whole pending tree.
11793 If we find an element that should be output now,
11794 output it. Otherwise, set NEXT to the element
11795 that comes first among those still pending. */
11796
11797 next = NULL_TREE;
11798 while (elt)
11799 {
11800 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11801 {
11802 if (tree_int_cst_equal (elt->purpose,
11803 constructor_unfilled_index))
11804 output_init_element (input_location, elt->value, elt->origtype,
11805 true, TREE_TYPE (constructor_type),
11806 constructor_unfilled_index, false, false,
11807 braced_init_obstack);
11808 else if (tree_int_cst_lt (constructor_unfilled_index,
11809 elt->purpose))
11810 {
11811 /* Advance to the next smaller node. */
11812 if (elt->left)
11813 elt = elt->left;
11814 else
11815 {
11816 /* We have reached the smallest node bigger than the
11817 current unfilled index. Fill the space first. */
11818 next = elt->purpose;
11819 break;
11820 }
11821 }
11822 else
11823 {
11824 /* Advance to the next bigger node. */
11825 if (elt->right)
11826 elt = elt->right;
11827 else
11828 {
11829 /* We have reached the biggest node in a subtree. Find
11830 the parent of it, which is the next bigger node. */
11831 while (elt->parent && elt->parent->right == elt)
11832 elt = elt->parent;
11833 elt = elt->parent;
11834 if (elt && tree_int_cst_lt (constructor_unfilled_index,
11835 elt->purpose))
11836 {
11837 next = elt->purpose;
11838 break;
11839 }
11840 }
11841 }
11842 }
11843 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
11844 {
11845 /* If the current record is complete we are done. */
11846 if (constructor_unfilled_fields == NULL_TREE)
11847 break;
11848
11849 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
11850 elt->purpose);
11851 if (cmp == 0)
11852 output_init_element (input_location, elt->value, elt->origtype,
11853 true, TREE_TYPE (elt->purpose),
11854 elt->purpose, false, false,
11855 braced_init_obstack);
11856 else if (cmp < 0)
11857 {
11858 /* Advance to the next smaller node. */
11859 if (elt->left)
11860 elt = elt->left;
11861 else
11862 {
11863 /* We have reached the smallest node bigger than the
11864 current unfilled field. Fill the space first. */
11865 next = elt->purpose;
11866 break;
11867 }
11868 }
11869 else
11870 {
11871 /* Advance to the next bigger node. */
11872 if (elt->right)
11873 elt = elt->right;
11874 else
11875 {
11876 /* We have reached the biggest node in a subtree. Find
11877 the parent of it, which is the next bigger node. */
11878 while (elt->parent && elt->parent->right == elt)
11879 elt = elt->parent;
11880 elt = elt->parent;
11881 if (elt
11882 && init_field_decl_cmp (constructor_unfilled_fields,
11883 elt->purpose) < 0)
11884 {
11885 next = elt->purpose;
11886 break;
11887 }
11888 }
11889 }
11890 }
11891 }
11892
11893 /* Ordinarily return, but not if we want to output all
11894 and there are elements left. */
11895 if (!(all && next != NULL_TREE))
11896 return;
11897
11898 /* If it's not incremental, just skip over the gap, so that after
11899 jumping to retry we will output the next successive element. */
11900 if (RECORD_OR_UNION_TYPE_P (constructor_type))
11901 constructor_unfilled_fields = next;
11902 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11903 constructor_unfilled_index = next;
11904
11905 /* ELT now points to the node in the pending tree with the next
11906 initializer to output. */
11907 goto retry;
11908 }
11909
11910 /* Expression VALUE coincides with the start of type TYPE in a braced
11911 initializer. Return true if we should treat VALUE as initializing
11912 the first element of TYPE, false if we should treat it as initializing
11913 TYPE as a whole.
11914
11915 If the initializer is clearly invalid, the question becomes:
11916 which choice gives the best error message? */
11917
11918 static bool
11919 initialize_elementwise_p (tree type, tree value)
11920 {
11921 if (type == error_mark_node || value == error_mark_node)
11922 return false;
11923
11924 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
11925
11926 tree value_type = TREE_TYPE (value);
11927 if (value_type == error_mark_node)
11928 return false;
11929
11930 /* GNU vectors can be initialized elementwise. However, treat any
11931 kind of vector value as initializing the vector type as a whole,
11932 regardless of whether the value is a GNU vector. Such initializers
11933 are valid if and only if they would have been valid in a non-braced
11934 initializer like:
11935
11936 TYPE foo = VALUE;
11937
11938 so recursing into the vector type would be at best confusing or at
11939 worst wrong. For example, when -flax-vector-conversions is in effect,
11940 it's possible to initialize a V8HI from a V4SI, even though the vectors
11941 have different element types and different numbers of elements. */
11942 if (gnu_vector_type_p (type))
11943 return !VECTOR_TYPE_P (value_type);
11944
11945 if (AGGREGATE_TYPE_P (type))
11946 return !comptypes (type, TYPE_MAIN_VARIANT (value_type));
11947
11948 return false;
11949 }
11950
11951 /* Helper function for process_init_element. Split first element of
11952 RAW_DATA_CST and save the rest to *RAW_DATA. */
11953
11954 static inline tree
11955 maybe_split_raw_data (tree value, tree *raw_data)
11956 {
11957 if (value == NULL_TREE || TREE_CODE (value) != RAW_DATA_CST)
11958 return value;
11959 *raw_data = value;
11960 value = build_int_cst (integer_type_node,
11961 RAW_DATA_UCHAR_ELT (*raw_data, 0));
11962 ++RAW_DATA_POINTER (*raw_data);
11963 --RAW_DATA_LENGTH (*raw_data);
11964 return value;
11965 }
11966
11967 /* Return non-zero if c_parser_initval should attempt to optimize
11968 large initializers into RAW_DATA_CST. In that case return how
11969 many elements to optimize at most. */
11970
11971 unsigned
11972 c_maybe_optimize_large_byte_initializer (void)
11973 {
11974 if (!constructor_type
11975 || TREE_CODE (constructor_type) != ARRAY_TYPE
11976 || constructor_stack->implicit)
11977 return 0;
11978 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
11979 if (TREE_CODE (elttype) != INTEGER_TYPE
11980 && TREE_CODE (elttype) != BITINT_TYPE)
11981 return 0;
11982 if (TYPE_PRECISION (elttype) != CHAR_BIT
11983 || constructor_stack->replacement_value.value
11984 || (COMPLETE_TYPE_P (constructor_type)
11985 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
11986 || constructor_range_stack)
11987 return 0;
11988 if (constructor_max_index == NULL_TREE)
11989 return INT_MAX;
11990 if (tree_int_cst_le (constructor_max_index, constructor_index)
11991 || integer_all_onesp (constructor_max_index))
11992 return 0;
11993 widest_int w = wi::to_widest (constructor_max_index);
11994 w -= wi::to_widest (constructor_index);
11995 w += 1;
11996 if (w < 64)
11997 return 0;
11998 if (w > INT_MAX)
11999 return INT_MAX;
12000 return w.to_uhwi ();
12001 }
12002
12003 /* Add one non-braced element to the current constructor level.
12004 This adjusts the current position within the constructor's type.
12005 This may also start or terminate implicit levels
12006 to handle a partly-braced initializer.
12007
12008 Once this has found the correct level for the new element,
12009 it calls output_init_element.
12010
12011 IMPLICIT is true if value comes from pop_init_level (1),
12012 the new initializer has been merged with the existing one
12013 and thus no warnings should be emitted about overriding an
12014 existing initializer. */
12015
12016 void
12017 process_init_element (location_t loc, struct c_expr value, bool implicit,
12018 struct obstack * braced_init_obstack)
12019 {
12020 tree orig_value = value.value;
12021 int string_flag
12022 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
12023 bool strict_string = value.original_code == STRING_CST;
12024 bool was_designated = designator_depth != 0;
12025 tree raw_data = NULL_TREE;
12026
12027 retry:
12028 designator_depth = 0;
12029 designator_erroneous = 0;
12030
12031 if (!implicit && value.value && !integer_zerop (value.value))
12032 constructor_zeroinit = 0;
12033
12034 /* Handle superfluous braces around string cst as in
12035 char x[] = {"foo"}; */
12036 if (constructor_type
12037 && !was_designated
12038 && TREE_CODE (constructor_type) == ARRAY_TYPE
12039 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
12040 && integer_zerop (constructor_unfilled_index))
12041 {
12042 if (constructor_stack->replacement_value.value)
12043 {
12044 error_init (loc, "excess elements in %qT initializer", constructor_type);
12045 return;
12046 }
12047 else if (string_flag)
12048 {
12049 constructor_stack->replacement_value = value;
12050 return;
12051 }
12052 }
12053
12054 if (constructor_stack->replacement_value.value != NULL_TREE)
12055 {
12056 error_init (loc, "excess elements in struct initializer");
12057 return;
12058 }
12059
12060 /* Ignore elements of a brace group if it is entirely superfluous
12061 and has already been diagnosed, or if the type is erroneous. */
12062 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
12063 return;
12064
12065 /* Ignore elements of an initializer for a variable-size type.
12066 Those are diagnosed in the parser (empty initializer braces are OK). */
12067 if (COMPLETE_TYPE_P (constructor_type)
12068 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12069 return;
12070
12071 if (!implicit && warn_designated_init && !was_designated
12072 && TREE_CODE (constructor_type) == RECORD_TYPE
12073 && lookup_attribute ("designated_init",
12074 TYPE_ATTRIBUTES (constructor_type)))
12075 warning_init (loc,
12076 OPT_Wdesignated_init,
12077 "positional initialization of field "
12078 "in %<struct%> declared with %<designated_init%> attribute");
12079
12080 /* If we've exhausted any levels that didn't have braces,
12081 pop them now. */
12082 while (constructor_stack->implicit)
12083 {
12084 if (RECORD_OR_UNION_TYPE_P (constructor_type)
12085 && constructor_fields == NULL_TREE)
12086 process_init_element (loc,
12087 pop_init_level (loc, 1, braced_init_obstack,
12088 last_init_list_comma),
12089 true, braced_init_obstack);
12090 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
12091 || gnu_vector_type_p (constructor_type))
12092 && constructor_max_index
12093 && tree_int_cst_lt (constructor_max_index,
12094 constructor_index))
12095 process_init_element (loc,
12096 pop_init_level (loc, 1, braced_init_obstack,
12097 last_init_list_comma),
12098 true, braced_init_obstack);
12099 else
12100 break;
12101 }
12102
12103 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
12104 if (constructor_range_stack)
12105 {
12106 /* If value is a compound literal and we'll be just using its
12107 content, don't put it into a SAVE_EXPR. */
12108 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
12109 || !require_constant_value)
12110 {
12111 tree semantic_type = NULL_TREE;
12112 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
12113 {
12114 semantic_type = TREE_TYPE (value.value);
12115 value.value = TREE_OPERAND (value.value, 0);
12116 }
12117 value.value = save_expr (value.value);
12118 if (semantic_type)
12119 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
12120 value.value);
12121 }
12122 }
12123
12124 while (1)
12125 {
12126 if (TREE_CODE (constructor_type) == RECORD_TYPE)
12127 {
12128 tree fieldtype;
12129 enum tree_code fieldcode;
12130
12131 if (constructor_fields == NULL_TREE)
12132 {
12133 pedwarn_init (loc, 0, "excess elements in struct initializer");
12134 break;
12135 }
12136
12137 fieldtype = TREE_TYPE (constructor_fields);
12138 if (fieldtype != error_mark_node)
12139 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12140 fieldcode = TREE_CODE (fieldtype);
12141
12142 /* Error for non-static initialization of a flexible array member. */
12143 if (fieldcode == ARRAY_TYPE
12144 && !require_constant_value
12145 && TYPE_SIZE (fieldtype) == NULL_TREE
12146 && DECL_CHAIN (constructor_fields) == NULL_TREE)
12147 {
12148 error_init (loc, "non-static initialization of a flexible "
12149 "array member");
12150 break;
12151 }
12152
12153 /* Error for initialization of a flexible array member with
12154 a string constant if the structure is in an array. E.g.:
12155 struct S { int x; char y[]; };
12156 struct S s[] = { { 1, "foo" } };
12157 is invalid. */
12158 if (string_flag
12159 && fieldcode == ARRAY_TYPE
12160 && constructor_depth > 1
12161 && TYPE_SIZE (fieldtype) == NULL_TREE
12162 && DECL_CHAIN (constructor_fields) == NULL_TREE)
12163 {
12164 bool in_array_p = false;
12165 for (struct constructor_stack *p = constructor_stack;
12166 p && p->type; p = p->next)
12167 if (TREE_CODE (p->type) == ARRAY_TYPE)
12168 {
12169 in_array_p = true;
12170 break;
12171 }
12172 if (in_array_p)
12173 {
12174 error_init (loc, "initialization of flexible array "
12175 "member in a nested context");
12176 break;
12177 }
12178 }
12179
12180 /* Accept a string constant to initialize a subarray. */
12181 if (value.value != NULL_TREE
12182 && fieldcode == ARRAY_TYPE
12183 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12184 && string_flag)
12185 value.value = orig_value;
12186 /* Otherwise, if we have come to a subaggregate,
12187 and we don't have an element of its type, push into it. */
12188 else if (value.value != NULL_TREE
12189 && initialize_elementwise_p (fieldtype, value.value))
12190 {
12191 push_init_level (loc, 1, braced_init_obstack);
12192 continue;
12193 }
12194
12195 value.value = maybe_split_raw_data (value.value, &raw_data);
12196 if (value.value)
12197 {
12198 push_member_name (constructor_fields);
12199 output_init_element (loc, value.value, value.original_type,
12200 strict_string, fieldtype,
12201 constructor_fields, true, implicit,
12202 braced_init_obstack);
12203 RESTORE_SPELLING_DEPTH (constructor_depth);
12204 }
12205 else
12206 /* Do the bookkeeping for an element that was
12207 directly output as a constructor. */
12208 {
12209 /* For a record, keep track of end position of last field. */
12210 if (DECL_SIZE (constructor_fields))
12211 constructor_bit_index
12212 = size_binop_loc (input_location, PLUS_EXPR,
12213 bit_position (constructor_fields),
12214 DECL_SIZE (constructor_fields));
12215
12216 /* If the current field was the first one not yet written out,
12217 it isn't now, so update. */
12218 if (constructor_unfilled_fields == constructor_fields)
12219 {
12220 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12221 /* Skip any nameless bit fields. */
12222 while (constructor_unfilled_fields != 0
12223 && (DECL_UNNAMED_BIT_FIELD
12224 (constructor_unfilled_fields)))
12225 constructor_unfilled_fields =
12226 DECL_CHAIN (constructor_unfilled_fields);
12227 }
12228 }
12229
12230 constructor_fields = DECL_CHAIN (constructor_fields);
12231 /* Skip any nameless bit fields at the beginning. */
12232 while (constructor_fields != NULL_TREE
12233 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
12234 constructor_fields = DECL_CHAIN (constructor_fields);
12235 }
12236 else if (TREE_CODE (constructor_type) == UNION_TYPE)
12237 {
12238 tree fieldtype;
12239 enum tree_code fieldcode;
12240
12241 if (constructor_fields == NULL_TREE)
12242 {
12243 pedwarn_init (loc, 0,
12244 "excess elements in union initializer");
12245 break;
12246 }
12247
12248 fieldtype = TREE_TYPE (constructor_fields);
12249 if (fieldtype != error_mark_node)
12250 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12251 fieldcode = TREE_CODE (fieldtype);
12252
12253 /* Warn that traditional C rejects initialization of unions.
12254 We skip the warning if the value is zero. This is done
12255 under the assumption that the zero initializer in user
12256 code appears conditioned on e.g. __STDC__ to avoid
12257 "missing initializer" warnings and relies on default
12258 initialization to zero in the traditional C case.
12259 We also skip the warning if the initializer is designated,
12260 again on the assumption that this must be conditional on
12261 __STDC__ anyway (and we've already complained about the
12262 member-designator already). */
12263 if (!in_system_header_at (input_location) && !constructor_designated
12264 && !(value.value && (integer_zerop (value.value)
12265 || real_zerop (value.value))))
12266 warning (OPT_Wtraditional, "traditional C rejects initialization "
12267 "of unions");
12268
12269 /* Error for non-static initialization of a flexible array member. */
12270 if (fieldcode == ARRAY_TYPE
12271 && !require_constant_value
12272 && TYPE_SIZE (fieldtype) == NULL_TREE)
12273 {
12274 error_init (loc, "non-static initialization of a flexible "
12275 "array member");
12276 break;
12277 }
12278
12279 /* Error for initialization of a flexible array member with
12280 a string constant if the structure is in an array. E.g.:
12281 union U { int x; char y[]; };
12282 union U s[] = { { 1, "foo" } };
12283 is invalid. */
12284 if (string_flag
12285 && fieldcode == ARRAY_TYPE
12286 && constructor_depth > 1
12287 && TYPE_SIZE (fieldtype) == NULL_TREE)
12288 {
12289 bool in_array_p = false;
12290 for (struct constructor_stack *p = constructor_stack;
12291 p && p->type; p = p->next)
12292 if (TREE_CODE (p->type) == ARRAY_TYPE)
12293 {
12294 in_array_p = true;
12295 break;
12296 }
12297 if (in_array_p)
12298 {
12299 error_init (loc, "initialization of flexible array "
12300 "member in a nested context");
12301 break;
12302 }
12303 }
12304
12305 /* Accept a string constant to initialize a subarray. */
12306 if (value.value != NULL_TREE
12307 && fieldcode == ARRAY_TYPE
12308 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12309 && string_flag)
12310 value.value = orig_value;
12311 /* Otherwise, if we have come to a subaggregate,
12312 and we don't have an element of its type, push into it. */
12313 else if (value.value != NULL_TREE
12314 && initialize_elementwise_p (fieldtype, value.value))
12315 {
12316 push_init_level (loc, 1, braced_init_obstack);
12317 continue;
12318 }
12319
12320 value.value = maybe_split_raw_data (value.value, &raw_data);
12321 if (value.value)
12322 {
12323 push_member_name (constructor_fields);
12324 output_init_element (loc, value.value, value.original_type,
12325 strict_string, fieldtype,
12326 constructor_fields, true, implicit,
12327 braced_init_obstack);
12328 RESTORE_SPELLING_DEPTH (constructor_depth);
12329 }
12330 else
12331 /* Do the bookkeeping for an element that was
12332 directly output as a constructor. */
12333 {
12334 constructor_bit_index = DECL_SIZE (constructor_fields);
12335 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12336 }
12337
12338 constructor_fields = NULL_TREE;
12339 }
12340 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12341 {
12342 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12343 enum tree_code eltcode = TREE_CODE (elttype);
12344
12345 /* Accept a string constant to initialize a subarray. */
12346 if (value.value != NULL_TREE
12347 && eltcode == ARRAY_TYPE
12348 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
12349 && string_flag)
12350 value.value = orig_value;
12351 /* Otherwise, if we have come to a subaggregate,
12352 and we don't have an element of its type, push into it. */
12353 else if (value.value != NULL_TREE
12354 && initialize_elementwise_p (elttype, value.value))
12355 {
12356 push_init_level (loc, 1, braced_init_obstack);
12357 continue;
12358 }
12359
12360 if (constructor_max_index != NULL_TREE
12361 && (tree_int_cst_lt (constructor_max_index, constructor_index)
12362 || integer_all_onesp (constructor_max_index)))
12363 {
12364 pedwarn_init (loc, 0,
12365 "excess elements in array initializer");
12366 break;
12367 }
12368
12369 if (value.value
12370 && TREE_CODE (value.value) == RAW_DATA_CST
12371 && RAW_DATA_LENGTH (value.value) > 1
12372 && (TREE_CODE (elttype) == INTEGER_TYPE
12373 || TREE_CODE (elttype) == BITINT_TYPE)
12374 && TYPE_PRECISION (elttype) == CHAR_BIT
12375 && (constructor_max_index == NULL_TREE
12376 || tree_int_cst_lt (constructor_index,
12377 constructor_max_index)))
12378 {
12379 unsigned int len = RAW_DATA_LENGTH (value.value);
12380 if (constructor_max_index)
12381 {
12382 widest_int w = wi::to_widest (constructor_max_index);
12383 w -= wi::to_widest (constructor_index);
12384 w += 1;
12385 if (w < len)
12386 len = w.to_uhwi ();
12387 }
12388 if (len < (unsigned) RAW_DATA_LENGTH (value.value))
12389 {
12390 raw_data = copy_node (value.value);
12391 RAW_DATA_LENGTH (raw_data) -= len;
12392 RAW_DATA_POINTER (raw_data) += len;
12393 RAW_DATA_LENGTH (value.value) = len;
12394 }
12395 TREE_TYPE (value.value) = elttype;
12396 push_array_bounds (tree_to_uhwi (constructor_index));
12397 output_init_element (loc, value.value, value.original_type,
12398 false, elttype, constructor_index, true,
12399 implicit, braced_init_obstack);
12400 RESTORE_SPELLING_DEPTH (constructor_depth);
12401 constructor_index
12402 = size_binop_loc (input_location, PLUS_EXPR,
12403 constructor_index, bitsize_int (len));
12404 }
12405 else
12406 {
12407 value.value = maybe_split_raw_data (value.value, &raw_data);
12408 /* Now output the actual element. */
12409 if (value.value)
12410 {
12411 push_array_bounds (tree_to_uhwi (constructor_index));
12412 output_init_element (loc, value.value, value.original_type,
12413 strict_string, elttype,
12414 constructor_index, true, implicit,
12415 braced_init_obstack);
12416 RESTORE_SPELLING_DEPTH (constructor_depth);
12417 }
12418
12419 constructor_index
12420 = size_binop_loc (input_location, PLUS_EXPR,
12421 constructor_index, bitsize_one_node);
12422
12423 if (!value.value)
12424 /* If we are doing the bookkeeping for an element that was
12425 directly output as a constructor, we must update
12426 constructor_unfilled_index. */
12427 constructor_unfilled_index = constructor_index;
12428 }
12429 }
12430 else if (gnu_vector_type_p (constructor_type))
12431 {
12432 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12433
12434 /* Do a basic check of initializer size. Note that vectors
12435 may not always have a fixed size derived from their type. */
12436 if (maybe_lt (tree_to_poly_uint64 (constructor_max_index),
12437 tree_to_poly_uint64 (constructor_index)))
12438 {
12439 /* Diagose VLA out-of-bounds as errors. */
12440 if (tree_to_poly_uint64 (constructor_max_index).is_constant())
12441 pedwarn_init (loc, 0,
12442 "excess elements in vector initializer");
12443 else
12444 error_init (loc, "excess elements in vector initializer");
12445
12446 break;
12447 }
12448
12449 value.value = maybe_split_raw_data (value.value, &raw_data);
12450 /* Now output the actual element. */
12451 if (value.value)
12452 {
12453 if (TREE_CODE (value.value) == VECTOR_CST)
12454 elttype = TYPE_MAIN_VARIANT (constructor_type);
12455 output_init_element (loc, value.value, value.original_type,
12456 strict_string, elttype,
12457 constructor_index, true, implicit,
12458 braced_init_obstack);
12459 }
12460
12461 constructor_index
12462 = size_binop_loc (input_location,
12463 PLUS_EXPR, constructor_index, bitsize_one_node);
12464
12465 if (!value.value)
12466 /* If we are doing the bookkeeping for an element that was
12467 directly output as a constructor, we must update
12468 constructor_unfilled_index. */
12469 constructor_unfilled_index = constructor_index;
12470 }
12471
12472 /* Handle the sole element allowed in a braced initializer
12473 for a scalar variable. */
12474 else if (constructor_type != error_mark_node
12475 && constructor_fields == NULL_TREE)
12476 {
12477 pedwarn_init (loc, 0,
12478 "excess elements in scalar initializer");
12479 break;
12480 }
12481 else
12482 {
12483 value.value = maybe_split_raw_data (value.value, &raw_data);
12484 if (value.value)
12485 output_init_element (loc, value.value, value.original_type,
12486 strict_string, constructor_type,
12487 NULL_TREE, true, implicit,
12488 braced_init_obstack);
12489 constructor_fields = NULL_TREE;
12490 }
12491
12492 /* Handle range initializers either at this level or anywhere higher
12493 in the designator stack. */
12494 if (constructor_range_stack)
12495 {
12496 struct constructor_range_stack *p, *range_stack;
12497 int finish = 0;
12498
12499 range_stack = constructor_range_stack;
12500 constructor_range_stack = 0;
12501 while (constructor_stack != range_stack->stack)
12502 {
12503 gcc_assert (constructor_stack->implicit);
12504 process_init_element (loc,
12505 pop_init_level (loc, 1,
12506 braced_init_obstack,
12507 last_init_list_comma),
12508 true, braced_init_obstack);
12509 }
12510 for (p = range_stack;
12511 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
12512 p = p->prev)
12513 {
12514 gcc_assert (constructor_stack->implicit);
12515 process_init_element (loc,
12516 pop_init_level (loc, 1,
12517 braced_init_obstack,
12518 last_init_list_comma),
12519 true, braced_init_obstack);
12520 }
12521
12522 p->index = size_binop_loc (input_location,
12523 PLUS_EXPR, p->index, bitsize_one_node);
12524 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
12525 finish = 1;
12526
12527 while (1)
12528 {
12529 constructor_index = p->index;
12530 constructor_fields = p->fields;
12531 if (finish && p->range_end && p->index == p->range_start)
12532 {
12533 finish = 0;
12534 p->prev = 0;
12535 }
12536 p = p->next;
12537 if (!p)
12538 break;
12539 finish_implicit_inits (loc, braced_init_obstack);
12540 push_init_level (loc, 2, braced_init_obstack);
12541 p->stack = constructor_stack;
12542 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
12543 p->index = p->range_start;
12544 }
12545
12546 if (!finish)
12547 constructor_range_stack = range_stack;
12548 continue;
12549 }
12550
12551 break;
12552 }
12553
12554 constructor_range_stack = 0;
12555
12556 if (raw_data && RAW_DATA_LENGTH (raw_data))
12557 {
12558 gcc_assert (!string_flag && !was_designated);
12559 value.value = raw_data;
12560 raw_data = NULL_TREE;
12561 goto retry;
12562 }
12563 }
12564
12565 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
12566 (guaranteed to be 'volatile' or null) and ARGS (represented using
12567 an ASM_EXPR node). */
12568 tree
12569 build_asm_stmt (bool is_volatile, tree args)
12570 {
12571 if (is_volatile)
12572 ASM_VOLATILE_P (args) = 1;
12573 return add_stmt (args);
12574 }
12575
12576 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
12577 some INPUTS, and some CLOBBERS. The latter three may be NULL.
12578 SIMPLE indicates whether there was anything at all after the
12579 string in the asm expression -- asm("blah") and asm("blah" : )
12580 are subtly different. We use a ASM_EXPR node to represent this.
12581 LOC is the location of the asm, and IS_INLINE says whether this
12582 is asm inline. */
12583 tree
12584 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
12585 tree clobbers, tree labels, bool simple, bool is_inline)
12586 {
12587 tree tail;
12588 tree args;
12589 int i;
12590 const char *constraint;
12591 const char **oconstraints;
12592 bool allows_mem, allows_reg, is_inout;
12593 int ninputs, noutputs;
12594
12595 ninputs = list_length (inputs);
12596 noutputs = list_length (outputs);
12597 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
12598
12599 string = resolve_asm_operand_names (string, outputs, inputs, labels);
12600
12601 /* Remove output conversions that change the type but not the mode. */
12602 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
12603 {
12604 tree output = TREE_VALUE (tail);
12605
12606 output = c_fully_fold (output, false, NULL, true);
12607
12608 /* ??? Really, this should not be here. Users should be using a
12609 proper lvalue, dammit. But there's a long history of using casts
12610 in the output operands. In cases like longlong.h, this becomes a
12611 primitive form of typechecking -- if the cast can be removed, then
12612 the output operand had a type of the proper width; otherwise we'll
12613 get an error. Gross, but ... */
12614 STRIP_NOPS (output);
12615
12616 if (!lvalue_or_else (loc, output, lv_asm))
12617 output = error_mark_node;
12618
12619 if (output != error_mark_node
12620 && (TREE_READONLY (output)
12621 || TYPE_READONLY (TREE_TYPE (output))
12622 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
12623 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
12624 readonly_error (loc, output, lv_asm);
12625
12626 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
12627 oconstraints[i] = constraint;
12628
12629 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
12630 &allows_mem, &allows_reg, &is_inout))
12631 {
12632 /* If the operand is going to end up in memory,
12633 mark it addressable. */
12634 if (!allows_reg && !c_mark_addressable (output))
12635 output = error_mark_node;
12636 if (!(!allows_reg && allows_mem)
12637 && output != error_mark_node
12638 && VOID_TYPE_P (TREE_TYPE (output)))
12639 {
12640 error_at (loc, "invalid use of void expression");
12641 output = error_mark_node;
12642 }
12643 if (allows_reg && current_function_decl == NULL_TREE)
12644 {
12645 error_at (loc, "constraint allows registers outside of "
12646 "a function");
12647 output = error_mark_node;
12648 }
12649 }
12650 else
12651 output = error_mark_node;
12652
12653 if (current_function_decl == NULL_TREE && output != error_mark_node)
12654 {
12655 if (TREE_SIDE_EFFECTS (output))
12656 {
12657 error_at (loc, "side-effects in output operand outside "
12658 "of a function");
12659 output = error_mark_node;
12660 }
12661 else
12662 {
12663 tree addr = build_unary_op (loc, ADDR_EXPR, output, false);
12664 if (addr == error_mark_node)
12665 output = error_mark_node;
12666 else if (!initializer_constant_valid_p (addr, TREE_TYPE (addr)))
12667 {
12668 error_at (loc, "output operand outside of a function is not "
12669 "constant");
12670 output = error_mark_node;
12671 }
12672 }
12673 }
12674 else if (output != error_mark_node && strstr (constraint, "-"))
12675 {
12676 error_at (loc, "%<-%> modifier used inside of a function");
12677 output = error_mark_node;
12678 }
12679
12680 TREE_VALUE (tail) = output;
12681 }
12682
12683 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
12684 {
12685 tree input;
12686
12687 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
12688 input = TREE_VALUE (tail);
12689
12690 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
12691 oconstraints, &allows_mem, &allows_reg))
12692 {
12693 /* If the operand is going to end up in memory,
12694 mark it addressable. */
12695 if (!allows_reg && allows_mem)
12696 {
12697 input = c_fully_fold (input, false, NULL, true);
12698
12699 /* Strip the nops as we allow this case. FIXME, this really
12700 should be rejected or made deprecated. */
12701 STRIP_NOPS (input);
12702 if (!c_mark_addressable (input))
12703 input = error_mark_node;
12704 }
12705 else
12706 {
12707 struct c_expr expr;
12708 memset (&expr, 0, sizeof (expr));
12709 expr.value = input;
12710 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
12711 input = c_fully_fold (expr.value, false, NULL);
12712
12713 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
12714 {
12715 error_at (loc, "invalid use of void expression");
12716 input = error_mark_node;
12717 }
12718 }
12719 if (allows_reg && current_function_decl == NULL_TREE)
12720 {
12721 error_at (loc, "constraint allows registers outside of "
12722 "a function");
12723 input = error_mark_node;
12724 }
12725 if (constraint[0] == ':' && input != error_mark_node)
12726 {
12727 tree t = input;
12728 STRIP_NOPS (t);
12729 if (TREE_CODE (t) != ADDR_EXPR
12730 || !(TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
12731 || (VAR_P (TREE_OPERAND (t, 0))
12732 && is_global_var (TREE_OPERAND (t, 0)))))
12733 {
12734 error_at (loc, "%<:%> constraint operand is not address "
12735 "of a function or non-automatic variable");
12736 input = error_mark_node;
12737 }
12738 }
12739 }
12740 else
12741 input = error_mark_node;
12742
12743 if (current_function_decl == NULL_TREE && input != error_mark_node)
12744 {
12745 if (TREE_SIDE_EFFECTS (input))
12746 {
12747 error_at (loc, "side-effects in input operand outside "
12748 "of a function");
12749 input = error_mark_node;
12750 }
12751 else
12752 {
12753 tree tem = input;
12754 if (allows_mem && lvalue_p (input))
12755 tem = build_unary_op (loc, ADDR_EXPR, input, false);
12756 if (!initializer_constant_valid_p (tem, TREE_TYPE (tem)))
12757 {
12758 error_at (loc, "input operand outside of a function is not "
12759 "constant");
12760 input = error_mark_node;
12761 }
12762 }
12763 }
12764 else if (input != error_mark_node && strstr (constraint, "-"))
12765 {
12766 error_at (loc, "%<-%> modifier used inside of a function");
12767 input = error_mark_node;
12768 }
12769
12770 TREE_VALUE (tail) = input;
12771 }
12772
12773 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
12774
12775 /* asm statements without outputs, including simple ones, are treated
12776 as volatile. */
12777 ASM_BASIC_P (args) = simple;
12778 ASM_VOLATILE_P (args) = (noutputs == 0);
12779 ASM_INLINE_P (args) = is_inline;
12780
12781 return args;
12782 }
12783
12784 /* Generate a goto statement to LABEL. LOC is the location of the
12785 GOTO. */
12786
12787 tree
12788 c_finish_goto_label (location_t loc, tree label)
12789 {
12790 tree decl = lookup_label_for_goto (loc, label);
12791 if (!decl)
12792 return NULL_TREE;
12793 TREE_USED (decl) = 1;
12794 {
12795 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
12796 tree t = build1 (GOTO_EXPR, void_type_node, decl);
12797 SET_EXPR_LOCATION (t, loc);
12798 return add_stmt (t);
12799 }
12800 }
12801
12802 /* Generate a computed goto statement to EXPR. LOC is the location of
12803 the GOTO. */
12804
12805 tree
12806 c_finish_goto_ptr (location_t loc, c_expr val)
12807 {
12808 tree expr = val.value;
12809 tree t;
12810 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
12811 if (expr != error_mark_node
12812 && !POINTER_TYPE_P (TREE_TYPE (expr))
12813 && !null_pointer_constant_p (expr))
12814 {
12815 error_at (val.get_location (),
12816 "computed goto must be pointer type");
12817 expr = build_zero_cst (ptr_type_node);
12818 }
12819 expr = c_fully_fold (expr, false, NULL);
12820 expr = convert (ptr_type_node, expr);
12821 t = build1 (GOTO_EXPR, void_type_node, expr);
12822 SET_EXPR_LOCATION (t, loc);
12823 return add_stmt (t);
12824 }
12825
12826 /* Generate a C `return' statement. RETVAL is the expression for what
12827 to return, or a null pointer for `return;' with no value. LOC is
12828 the location of the return statement, or the location of the expression,
12829 if the statement has any. If ORIGTYPE is not NULL_TREE, it
12830 is the original type of RETVAL. MUSTTAIL_P indicates a musttail
12831 attribute. */
12832
12833 tree
12834 c_finish_return (location_t loc, tree retval, tree origtype, bool musttail_p)
12835 {
12836 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
12837 bool no_warning = false;
12838 bool npc = false;
12839
12840 /* Use the expansion point to handle cases such as returning NULL
12841 in a function returning void. */
12842 location_t xloc = expansion_point_location_if_in_system_header (loc);
12843
12844 if (TREE_THIS_VOLATILE (current_function_decl))
12845 warning_at (xloc, 0,
12846 "function declared %<noreturn%> has a %<return%> statement");
12847
12848 set_musttail_on_return (retval, xloc, musttail_p);
12849
12850 if (retval)
12851 {
12852 tree semantic_type = NULL_TREE;
12853 npc = null_pointer_constant_p (retval);
12854 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
12855 {
12856 semantic_type = TREE_TYPE (retval);
12857 retval = TREE_OPERAND (retval, 0);
12858 }
12859 retval = c_fully_fold (retval, false, NULL);
12860 if (semantic_type
12861 && valtype != NULL_TREE
12862 && TREE_CODE (valtype) != VOID_TYPE)
12863 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
12864 }
12865
12866 if (!retval)
12867 {
12868 current_function_returns_null = 1;
12869 if ((warn_return_type >= 0 || flag_isoc99)
12870 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
12871 {
12872 no_warning = true;
12873 if (emit_diagnostic (flag_isoc99 ? DK_PERMERROR : DK_WARNING,
12874 loc, OPT_Wreturn_mismatch,
12875 "%<return%> with no value,"
12876 " in function returning non-void"))
12877 inform (DECL_SOURCE_LOCATION (current_function_decl),
12878 "declared here");
12879 }
12880 }
12881 else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
12882 {
12883 current_function_returns_null = 1;
12884 bool warned_here;
12885 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
12886 warned_here = permerror_opt
12887 (xloc, OPT_Wreturn_mismatch,
12888 "%<return%> with a value, in function returning void");
12889 else
12890 warned_here = pedwarn
12891 (xloc, OPT_Wpedantic, "ISO C forbids "
12892 "%<return%> with expression, in function returning void");
12893 if (warned_here)
12894 inform (DECL_SOURCE_LOCATION (current_function_decl),
12895 "declared here");
12896 }
12897 else
12898 {
12899 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
12900 retval, origtype, ic_return,
12901 npc, NULL_TREE, NULL_TREE, 0);
12902 tree res = DECL_RESULT (current_function_decl);
12903 tree inner;
12904 bool save;
12905
12906 current_function_returns_value = 1;
12907 if (t == error_mark_node)
12908 {
12909 /* Suppress -Wreturn-type for this function. */
12910 if (warn_return_type)
12911 suppress_warning (current_function_decl, OPT_Wreturn_type);
12912 return NULL_TREE;
12913 }
12914
12915 save = in_late_binary_op;
12916 if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
12917 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
12918 || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
12919 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
12920 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
12921 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
12922 in_late_binary_op = true;
12923 inner = t = convert (TREE_TYPE (res), t);
12924 in_late_binary_op = save;
12925
12926 /* Strip any conversions, additions, and subtractions, and see if
12927 we are returning the address of a local variable. Warn if so. */
12928 while (1)
12929 {
12930 switch (TREE_CODE (inner))
12931 {
12932 CASE_CONVERT:
12933 case NON_LVALUE_EXPR:
12934 case PLUS_EXPR:
12935 case POINTER_PLUS_EXPR:
12936 inner = TREE_OPERAND (inner, 0);
12937 continue;
12938
12939 case MINUS_EXPR:
12940 /* If the second operand of the MINUS_EXPR has a pointer
12941 type (or is converted from it), this may be valid, so
12942 don't give a warning. */
12943 {
12944 tree op1 = TREE_OPERAND (inner, 1);
12945
12946 while (!POINTER_TYPE_P (TREE_TYPE (op1))
12947 && (CONVERT_EXPR_P (op1)
12948 || TREE_CODE (op1) == NON_LVALUE_EXPR))
12949 op1 = TREE_OPERAND (op1, 0);
12950
12951 if (POINTER_TYPE_P (TREE_TYPE (op1)))
12952 break;
12953
12954 inner = TREE_OPERAND (inner, 0);
12955 continue;
12956 }
12957
12958 case ADDR_EXPR:
12959 inner = TREE_OPERAND (inner, 0);
12960
12961 while (REFERENCE_CLASS_P (inner)
12962 && !INDIRECT_REF_P (inner))
12963 inner = TREE_OPERAND (inner, 0);
12964
12965 if (DECL_P (inner)
12966 && !DECL_EXTERNAL (inner)
12967 && !TREE_STATIC (inner)
12968 && DECL_CONTEXT (inner) == current_function_decl
12969 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
12970 {
12971 if (TREE_CODE (inner) == LABEL_DECL)
12972 warning_at (loc, OPT_Wreturn_local_addr,
12973 "function returns address of label");
12974 else
12975 {
12976 warning_at (loc, OPT_Wreturn_local_addr,
12977 "function returns address of local variable");
12978 tree zero = build_zero_cst (TREE_TYPE (res));
12979 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
12980 }
12981 }
12982 break;
12983
12984 default:
12985 break;
12986 }
12987
12988 break;
12989 }
12990
12991 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
12992 SET_EXPR_LOCATION (retval, loc);
12993
12994 if (warn_sequence_point)
12995 verify_sequence_points (retval);
12996 }
12997
12998 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
12999 if (no_warning)
13000 suppress_warning (ret_stmt, OPT_Wreturn_type);
13001 return add_stmt (ret_stmt);
13002 }
13003
13004 struct c_switch {
13005 /* The SWITCH_STMT being built. */
13006 tree switch_stmt;
13007
13008 /* The original type of the testing expression, i.e. before the
13009 default conversion is applied. */
13010 tree orig_type;
13011
13012 /* A splay-tree mapping the low element of a case range to the high
13013 element, or NULL_TREE if there is no high element. Used to
13014 determine whether or not a new case label duplicates an old case
13015 label. We need a tree, rather than simply a hash table, because
13016 of the GNU case range extension. */
13017 splay_tree cases;
13018
13019 /* The bindings at the point of the switch. This is used for
13020 warnings crossing decls when branching to a case label. */
13021 struct c_spot_bindings *bindings;
13022
13023 /* Whether the switch includes any break statements. */
13024 bool break_stmt_seen_p;
13025
13026 /* The next node on the stack. */
13027 struct c_switch *next;
13028
13029 /* Remember whether the controlling expression had boolean type
13030 before integer promotions for the sake of -Wswitch-bool. */
13031 bool bool_cond_p;
13032 };
13033
13034 /* A stack of the currently active switch statements. The innermost
13035 switch statement is on the top of the stack. There is no need to
13036 mark the stack for garbage collection because it is only active
13037 during the processing of the body of a function, and we never
13038 collect at that point. */
13039
13040 struct c_switch *c_switch_stack;
13041
13042 /* Start a C switch statement, testing expression EXP. Return the new
13043 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
13044 SWITCH_COND_LOC is the location of the switch's condition.
13045 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
13046
13047 tree
13048 c_start_switch (location_t switch_loc,
13049 location_t switch_cond_loc,
13050 tree exp, bool explicit_cast_p, tree switch_name)
13051 {
13052 tree orig_type = error_mark_node;
13053 bool bool_cond_p = false;
13054 struct c_switch *cs;
13055
13056 if (exp != error_mark_node)
13057 {
13058 orig_type = TREE_TYPE (exp);
13059
13060 if (!INTEGRAL_TYPE_P (orig_type))
13061 {
13062 if (orig_type != error_mark_node)
13063 {
13064 error_at (switch_cond_loc, "switch quantity not an integer");
13065 orig_type = error_mark_node;
13066 }
13067 exp = integer_zero_node;
13068 }
13069 else
13070 {
13071 tree type = TYPE_MAIN_VARIANT (orig_type);
13072 tree e = exp;
13073
13074 /* Warn if the condition has boolean value. */
13075 while (TREE_CODE (e) == COMPOUND_EXPR)
13076 e = TREE_OPERAND (e, 1);
13077
13078 if ((C_BOOLEAN_TYPE_P (type)
13079 || truth_value_p (TREE_CODE (e)))
13080 /* Explicit cast to int suppresses this warning. */
13081 && !(TREE_CODE (type) == INTEGER_TYPE
13082 && explicit_cast_p))
13083 bool_cond_p = true;
13084
13085 if (!in_system_header_at (input_location)
13086 && (type == long_integer_type_node
13087 || type == long_unsigned_type_node))
13088 warning_at (switch_cond_loc,
13089 OPT_Wtraditional, "%<long%> switch expression not "
13090 "converted to %<int%> in ISO C");
13091
13092 exp = c_fully_fold (exp, false, NULL);
13093 exp = default_conversion (exp);
13094
13095 if (warn_sequence_point)
13096 verify_sequence_points (exp);
13097 }
13098 }
13099
13100 /* Add this new SWITCH_STMT to the stack. */
13101 cs = XNEW (struct c_switch);
13102 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
13103 NULL_TREE, orig_type, NULL_TREE, switch_name);
13104 cs->orig_type = orig_type;
13105 cs->cases = splay_tree_new (case_compare, NULL, NULL);
13106 cs->bindings = c_get_switch_bindings ();
13107 cs->break_stmt_seen_p = false;
13108 cs->bool_cond_p = bool_cond_p;
13109 cs->next = c_switch_stack;
13110 c_switch_stack = cs;
13111
13112 return add_stmt (cs->switch_stmt);
13113 }
13114
13115 /* Process a case label at location LOC, with attributes ATTRS. */
13116
13117 tree
13118 do_case (location_t loc, tree low_value, tree high_value, tree attrs)
13119 {
13120 tree label = NULL_TREE;
13121
13122 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
13123 {
13124 low_value = c_fully_fold (low_value, false, NULL);
13125 if (TREE_CODE (low_value) == INTEGER_CST)
13126 pedwarn (loc, OPT_Wpedantic,
13127 "case label is not an integer constant expression");
13128 }
13129
13130 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
13131 {
13132 high_value = c_fully_fold (high_value, false, NULL);
13133 if (TREE_CODE (high_value) == INTEGER_CST)
13134 pedwarn (input_location, OPT_Wpedantic,
13135 "case label is not an integer constant expression");
13136 }
13137
13138 if (c_switch_stack == NULL)
13139 {
13140 if (low_value)
13141 error_at (loc, "case label not within a switch statement");
13142 else
13143 error_at (loc, "%<default%> label not within a switch statement");
13144 return NULL_TREE;
13145 }
13146
13147 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
13148 EXPR_LOCATION (c_switch_stack->switch_stmt),
13149 loc))
13150 return NULL_TREE;
13151
13152 label = c_add_case_label (loc, c_switch_stack->cases,
13153 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
13154 low_value, high_value, attrs);
13155 if (label == error_mark_node)
13156 label = NULL_TREE;
13157 return label;
13158 }
13159
13160 /* Finish the switch statement. TYPE is the original type of the
13161 controlling expression of the switch, or NULL_TREE. */
13162
13163 void
13164 c_finish_switch (tree body, tree type)
13165 {
13166 struct c_switch *cs = c_switch_stack;
13167 location_t switch_location;
13168
13169 SWITCH_STMT_BODY (cs->switch_stmt) = body;
13170
13171 /* Emit warnings as needed. */
13172 switch_location = EXPR_LOCATION (cs->switch_stmt);
13173 c_do_switch_warnings (cs->cases, switch_location,
13174 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
13175 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
13176 if (c_switch_covers_all_cases_p (cs->cases,
13177 SWITCH_STMT_TYPE (cs->switch_stmt)))
13178 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
13179 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
13180
13181 /* Pop the stack. */
13182 c_switch_stack = cs->next;
13183 splay_tree_delete (cs->cases);
13184 c_release_switch_bindings (cs->bindings);
13185 XDELETE (cs);
13186 }
13187
13188 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
13189 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
13190 may be null. */
13191
13192 void
13193 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
13194 tree else_block)
13195 {
13196 tree stmt;
13197
13198 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
13199 SET_EXPR_LOCATION (stmt, if_locus);
13200 add_stmt (stmt);
13201 }
13202
13203 tree
13204 c_finish_bc_stmt (location_t loc, tree label, bool is_break, tree name)
13205 {
13206 /* In switch statements break is sometimes stylistically used after
13207 a return statement. This can lead to spurious warnings about
13208 control reaching the end of a non-void function when it is
13209 inlined. Note that we are calling block_may_fallthru with
13210 language specific tree nodes; this works because
13211 block_may_fallthru returns true when given something it does not
13212 understand. */
13213 bool skip = !block_may_fallthru (cur_stmt_list);
13214
13215 if (is_break)
13216 switch (in_statement & ~IN_NAMED_STMT)
13217 {
13218 case 0:
13219 error_at (loc, "break statement not within loop or switch");
13220 return NULL_TREE;
13221 case IN_OMP_BLOCK:
13222 error_at (loc, "invalid exit from OpenMP structured block");
13223 return NULL_TREE;
13224 case IN_OMP_FOR:
13225 error_at (loc, "break statement used with OpenMP for loop");
13226 return NULL_TREE;
13227 case IN_ITERATION_STMT:
13228 case IN_OBJC_FOREACH:
13229 break;
13230 default:
13231 gcc_assert (in_statement & IN_SWITCH_STMT);
13232 c_switch_stack->break_stmt_seen_p = true;
13233 break;
13234 }
13235 else
13236 switch (in_statement & ~(IN_SWITCH_STMT | IN_NAMED_STMT))
13237 {
13238 case 0:
13239 error_at (loc, "continue statement not within a loop");
13240 return NULL_TREE;
13241 case IN_OMP_BLOCK:
13242 error_at (loc, "invalid exit from OpenMP structured block");
13243 return NULL_TREE;
13244 case IN_ITERATION_STMT:
13245 case IN_OMP_FOR:
13246 case IN_OBJC_FOREACH:
13247 break;
13248 default:
13249 gcc_unreachable ();
13250 }
13251
13252 if (skip)
13253 return NULL_TREE;
13254 else if ((in_statement & IN_OBJC_FOREACH)
13255 && !(is_break && (in_statement & IN_SWITCH_STMT))
13256 && name == NULL_TREE)
13257 {
13258 /* The foreach expander produces low-level code using gotos instead
13259 of a structured loop construct. */
13260 gcc_assert (label);
13261 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13262 }
13263 else if (name && C_DECL_LOOP_NAME (name) && C_DECL_SWITCH_NAME (name))
13264 {
13265 label = DECL_CHAIN (name);
13266 if (!is_break)
13267 label = DECL_CHAIN (label);
13268 /* Foreach expander from some outer level. */
13269 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13270 }
13271 return add_stmt (build_stmt (loc, is_break ? BREAK_STMT : CONTINUE_STMT,
13272 name));
13273 }
13274
13275 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
13276
13277 static void
13278 emit_side_effect_warnings (location_t loc, tree expr)
13279 {
13280 maybe_warn_nodiscard (loc, expr);
13281 if (!warn_unused_value)
13282 return;
13283 if (expr == error_mark_node)
13284 ;
13285 else if (!TREE_SIDE_EFFECTS (expr))
13286 {
13287 if (!VOID_TYPE_P (TREE_TYPE (expr))
13288 && !warning_suppressed_p (expr, OPT_Wunused_value))
13289 warning_at (loc, OPT_Wunused_value, "statement with no effect");
13290 }
13291 else if (TREE_CODE (expr) == COMPOUND_EXPR)
13292 {
13293 tree r = expr;
13294 location_t cloc = loc;
13295 while (TREE_CODE (r) == COMPOUND_EXPR)
13296 {
13297 if (EXPR_HAS_LOCATION (r))
13298 cloc = EXPR_LOCATION (r);
13299 r = TREE_OPERAND (r, 1);
13300 }
13301 if (!TREE_SIDE_EFFECTS (r)
13302 && !VOID_TYPE_P (TREE_TYPE (r))
13303 && !CONVERT_EXPR_P (r)
13304 && !warning_suppressed_p (r, OPT_Wunused_value)
13305 && !warning_suppressed_p (expr, OPT_Wunused_value))
13306 warning_at (cloc, OPT_Wunused_value,
13307 "right-hand operand of comma expression has no effect");
13308 }
13309 else
13310 warn_if_unused_value (expr, loc);
13311 }
13312
13313 /* Process an expression as if it were a complete statement. Emit
13314 diagnostics, but do not call ADD_STMT. LOC is the location of the
13315 statement. */
13316
13317 tree
13318 c_process_expr_stmt (location_t loc, tree expr)
13319 {
13320 tree exprv;
13321
13322 if (!expr)
13323 return NULL_TREE;
13324
13325 expr = c_fully_fold (expr, false, NULL);
13326
13327 if (warn_sequence_point)
13328 verify_sequence_points (expr);
13329
13330 if (TREE_TYPE (expr) != error_mark_node
13331 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
13332 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
13333 error_at (loc, "expression statement has incomplete type");
13334
13335 /* If we're not processing a statement expression, warn about unused values.
13336 Warnings for statement expressions will be emitted later, once we figure
13337 out which is the result. */
13338 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
13339 && (warn_unused_value || warn_unused_result))
13340 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
13341
13342 exprv = expr;
13343 while (TREE_CODE (exprv) == COMPOUND_EXPR)
13344 exprv = TREE_OPERAND (exprv, 1);
13345 while (CONVERT_EXPR_P (exprv))
13346 exprv = TREE_OPERAND (exprv, 0);
13347 if (DECL_P (exprv)
13348 || handled_component_p (exprv)
13349 || TREE_CODE (exprv) == ADDR_EXPR)
13350 mark_exp_read (exprv);
13351
13352 /* If the expression is not of a type to which we cannot assign a line
13353 number, wrap the thing in a no-op NOP_EXPR. */
13354 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
13355 {
13356 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13357 SET_EXPR_LOCATION (expr, loc);
13358 }
13359
13360 return expr;
13361 }
13362
13363 /* Emit an expression as a statement. LOC is the location of the
13364 expression. */
13365
13366 tree
13367 c_finish_expr_stmt (location_t loc, tree expr)
13368 {
13369 if (expr)
13370 return add_stmt (c_process_expr_stmt (loc, expr));
13371 else
13372 return NULL;
13373 }
13374
13375 /* Do the opposite and emit a statement as an expression. To begin,
13376 create a new binding level and return it. */
13377
13378 tree
13379 c_begin_stmt_expr (void)
13380 {
13381 tree ret;
13382
13383 /* We must force a BLOCK for this level so that, if it is not expanded
13384 later, there is a way to turn off the entire subtree of blocks that
13385 are contained in it. */
13386 keep_next_level ();
13387 ret = c_begin_compound_stmt (true);
13388
13389 c_bindings_start_stmt_expr (c_switch_stack == NULL
13390 ? NULL
13391 : c_switch_stack->bindings);
13392
13393 /* Mark the current statement list as belonging to a statement list. */
13394 STATEMENT_LIST_STMT_EXPR (ret) = 1;
13395
13396 return ret;
13397 }
13398
13399 /* LOC is the location of the compound statement to which this body
13400 belongs. */
13401
13402 tree
13403 c_finish_stmt_expr (location_t loc, tree body)
13404 {
13405 tree last, type, tmp, val;
13406 tree *last_p;
13407
13408 body = c_end_compound_stmt (loc, body, true);
13409
13410 c_bindings_end_stmt_expr (c_switch_stack == NULL
13411 ? NULL
13412 : c_switch_stack->bindings);
13413
13414 /* Locate the last statement in BODY. See c_end_compound_stmt
13415 about always returning a BIND_EXPR. */
13416 last_p = &BIND_EXPR_BODY (body);
13417 last = BIND_EXPR_BODY (body);
13418
13419 continue_searching:
13420 if (TREE_CODE (last) == STATEMENT_LIST)
13421 {
13422 tree_stmt_iterator l = tsi_last (last);
13423
13424 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
13425 tsi_prev (&l);
13426
13427 /* This can happen with degenerate cases like ({ }). No value. */
13428 if (tsi_end_p (l))
13429 return body;
13430
13431 /* If we're supposed to generate side effects warnings, process
13432 all of the statements except the last. */
13433 if (warn_unused_value || warn_unused_result)
13434 {
13435 for (tree_stmt_iterator i = tsi_start (last);
13436 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
13437 {
13438 location_t tloc;
13439 tree t = tsi_stmt (i);
13440
13441 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
13442 emit_side_effect_warnings (tloc, t);
13443 }
13444 }
13445 last_p = tsi_stmt_ptr (l);
13446 last = *last_p;
13447 }
13448
13449 /* If the end of the list is exception related, then the list was split
13450 by a call to push_cleanup. Continue searching. */
13451 if (TREE_CODE (last) == TRY_FINALLY_EXPR
13452 || TREE_CODE (last) == TRY_CATCH_EXPR)
13453 {
13454 last_p = &TREE_OPERAND (last, 0);
13455 last = *last_p;
13456 goto continue_searching;
13457 }
13458
13459 if (last == error_mark_node)
13460 return last;
13461
13462 /* In the case that the BIND_EXPR is not necessary, return the
13463 expression out from inside it. */
13464 if ((last == BIND_EXPR_BODY (body)
13465 /* Skip nested debug stmts. */
13466 || last == expr_first (BIND_EXPR_BODY (body)))
13467 && BIND_EXPR_VARS (body) == NULL)
13468 {
13469 /* Even if this looks constant, do not allow it in a constant
13470 expression. */
13471 last = c_wrap_maybe_const (last, true);
13472 /* Do not warn if the return value of a statement expression is
13473 unused. */
13474 suppress_warning (last, OPT_Wunused);
13475 return last;
13476 }
13477
13478 /* Extract the type of said expression. */
13479 type = TREE_TYPE (last);
13480
13481 /* If we're not returning a value at all, then the BIND_EXPR that
13482 we already have is a fine expression to return. */
13483 if (!type || VOID_TYPE_P (type))
13484 return body;
13485
13486 /* Now that we've located the expression containing the value, it seems
13487 silly to make voidify_wrapper_expr repeat the process. Create a
13488 temporary of the appropriate type and stick it in a TARGET_EXPR. */
13489 tmp = create_tmp_var_raw (type);
13490
13491 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
13492 tree_expr_nonnegative_p giving up immediately. */
13493 val = last;
13494 if (TREE_CODE (val) == NOP_EXPR
13495 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
13496 val = TREE_OPERAND (val, 0);
13497
13498 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
13499 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
13500
13501 {
13502 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
13503 SET_EXPR_LOCATION (t, loc);
13504 return t;
13505 }
13506 }
13507
13508 /* Begin and end compound statements. This is as simple as pushing
13509 and popping new statement lists from the tree. */
13510
13511 tree
13512 c_begin_compound_stmt (bool do_scope)
13513 {
13514 tree stmt = push_stmt_list ();
13515 if (do_scope)
13516 push_scope ();
13517 return stmt;
13518 }
13519
13520 /* End a compound statement. STMT is the statement. LOC is the
13521 location of the compound statement-- this is usually the location
13522 of the opening brace. */
13523
13524 tree
13525 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
13526 {
13527 tree block = NULL;
13528
13529 if (do_scope)
13530 {
13531 if (c_dialect_objc ())
13532 objc_clear_super_receiver ();
13533 block = pop_scope ();
13534 }
13535
13536 stmt = pop_stmt_list (stmt);
13537 stmt = c_build_bind_expr (loc, block, stmt);
13538
13539 /* If this compound statement is nested immediately inside a statement
13540 expression, then force a BIND_EXPR to be created. Otherwise we'll
13541 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
13542 STATEMENT_LISTs merge, and thus we can lose track of what statement
13543 was really last. */
13544 if (building_stmt_list_p ()
13545 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
13546 && TREE_CODE (stmt) != BIND_EXPR)
13547 {
13548 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
13549 TREE_SIDE_EFFECTS (stmt) = 1;
13550 SET_EXPR_LOCATION (stmt, loc);
13551 }
13552
13553 return stmt;
13554 }
13555
13556 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
13557 when the current scope is exited. EH_ONLY is true when this is not
13558 meant to apply to normal control flow transfer. */
13559
13560 void
13561 push_cleanup (tree decl, tree cleanup, bool eh_only)
13562 {
13563 enum tree_code code;
13564 tree stmt, list;
13565 bool stmt_expr;
13566
13567 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
13568 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
13569 add_stmt (stmt);
13570 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
13571 list = push_stmt_list ();
13572 TREE_OPERAND (stmt, 0) = list;
13573 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
13574 }
13575
13576 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
13577 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
13578
13579 static tree
13580 build_vec_cmp (tree_code code, tree type,
13581 tree arg0, tree arg1)
13582 {
13583 tree zero_vec = build_zero_cst (type);
13584 tree minus_one_vec = build_minus_one_cst (type);
13585 tree cmp_type = truth_type_for (TREE_TYPE (arg0));
13586 tree cmp = build2 (code, cmp_type, arg0, arg1);
13587 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
13588 }
13589
13590 /* Possibly warn about an address of OP never being NULL in a comparison
13591 operation CODE involving null. */
13592
13593 static void
13594 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
13595 {
13596 /* Prevent warnings issued for macro expansion. */
13597 if (!warn_address
13598 || warning_suppressed_p (op, OPT_Waddress)
13599 || from_macro_expansion_at (loc))
13600 return;
13601
13602 if (TREE_CODE (op) == NOP_EXPR)
13603 {
13604 /* Allow casts to intptr_t to suppress the warning. */
13605 tree type = TREE_TYPE (op);
13606 if (TREE_CODE (type) == INTEGER_TYPE)
13607 return;
13608 op = TREE_OPERAND (op, 0);
13609 }
13610
13611 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
13612 {
13613 /* Allow a cast to void* to suppress the warning. */
13614 tree type = TREE_TYPE (TREE_TYPE (op));
13615 if (VOID_TYPE_P (type))
13616 return;
13617
13618 /* Adding any value to a null pointer, including zero, is undefined
13619 in C. This includes the expression &p[0] where p is the null
13620 pointer, although &p[0] will have been folded to p by this point
13621 and so not diagnosed. */
13622 if (code == EQ_EXPR)
13623 warning_at (loc, OPT_Waddress,
13624 "the comparison will always evaluate as %<false%> "
13625 "for the pointer operand in %qE must not be NULL",
13626 op);
13627 else
13628 warning_at (loc, OPT_Waddress,
13629 "the comparison will always evaluate as %<true%> "
13630 "for the pointer operand in %qE must not be NULL",
13631 op);
13632
13633 return;
13634 }
13635
13636 if (TREE_CODE (op) != ADDR_EXPR)
13637 return;
13638
13639 op = TREE_OPERAND (op, 0);
13640
13641 if (TREE_CODE (op) == IMAGPART_EXPR
13642 || TREE_CODE (op) == REALPART_EXPR)
13643 {
13644 /* The address of either complex part may not be null. */
13645 if (code == EQ_EXPR)
13646 warning_at (loc, OPT_Waddress,
13647 "the comparison will always evaluate as %<false%> "
13648 "for the address of %qE will never be NULL",
13649 op);
13650 else
13651 warning_at (loc, OPT_Waddress,
13652 "the comparison will always evaluate as %<true%> "
13653 "for the address of %qE will never be NULL",
13654 op);
13655 return;
13656 }
13657
13658 /* Set to true in the loop below if OP dereferences is operand.
13659 In such a case the ultimate target need not be a decl for
13660 the null [in]equality test to be constant. */
13661 bool deref = false;
13662
13663 /* Get the outermost array or object, or member. */
13664 while (handled_component_p (op))
13665 {
13666 if (TREE_CODE (op) == COMPONENT_REF)
13667 {
13668 /* Get the member (its address is never null). */
13669 op = TREE_OPERAND (op, 1);
13670 break;
13671 }
13672
13673 /* Get the outer array/object to refer to in the warning. */
13674 op = TREE_OPERAND (op, 0);
13675 deref = true;
13676 }
13677
13678 if ((!deref && !decl_with_nonnull_addr_p (op))
13679 || from_macro_expansion_at (loc))
13680 return;
13681
13682 bool w;
13683 if (code == EQ_EXPR)
13684 w = warning_at (loc, OPT_Waddress,
13685 "the comparison will always evaluate as %<false%> "
13686 "for the address of %qE will never be NULL",
13687 op);
13688 else
13689 w = warning_at (loc, OPT_Waddress,
13690 "the comparison will always evaluate as %<true%> "
13691 "for the address of %qE will never be NULL",
13692 op);
13693
13694 if (w && DECL_P (op))
13695 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
13696 }
13697
13698 /* Build a binary-operation expression without default conversions.
13699 CODE is the kind of expression to build.
13700 LOCATION is the operator's location.
13701 This function differs from `build' in several ways:
13702 the data type of the result is computed and recorded in it,
13703 warnings are generated if arg data types are invalid,
13704 special handling for addition and subtraction of pointers is known,
13705 and some optimization is done (operations on narrow ints
13706 are done in the narrower type when that gives the same result).
13707 Constant folding is also done before the result is returned.
13708
13709 Note that the operands will never have enumeral types, or function
13710 or array types, because either they will have the default conversions
13711 performed or they have both just been converted to some other type in which
13712 the arithmetic is to be done. */
13713
13714 tree
13715 build_binary_op (location_t location, enum tree_code code,
13716 tree orig_op0, tree orig_op1, bool convert_p)
13717 {
13718 tree type0, type1, orig_type0, orig_type1;
13719 tree eptype;
13720 enum tree_code code0, code1;
13721 tree op0, op1;
13722 tree ret = error_mark_node;
13723 const char *invalid_op_diag;
13724 bool op0_int_operands, op1_int_operands;
13725 bool int_const, int_const_or_overflow, int_operands;
13726
13727 /* Expression code to give to the expression when it is built.
13728 Normally this is CODE, which is what the caller asked for,
13729 but in some special cases we change it. */
13730 enum tree_code resultcode = code;
13731
13732 /* Data type in which the computation is to be performed.
13733 In the simplest cases this is the common type of the arguments. */
13734 tree result_type = NULL;
13735
13736 /* When the computation is in excess precision, the type of the
13737 final EXCESS_PRECISION_EXPR. */
13738 tree semantic_result_type = NULL;
13739
13740 /* Nonzero means operands have already been type-converted
13741 in whatever way is necessary.
13742 Zero means they need to be converted to RESULT_TYPE. */
13743 int converted = 0;
13744
13745 /* Nonzero means create the expression with this type, rather than
13746 RESULT_TYPE. */
13747 tree build_type = NULL_TREE;
13748
13749 /* Nonzero means after finally constructing the expression
13750 convert it to this type. */
13751 tree final_type = NULL_TREE;
13752
13753 /* Nonzero if this is an operation like MIN or MAX which can
13754 safely be computed in short if both args are promoted shorts.
13755 Also implies COMMON.
13756 -1 indicates a bitwise operation; this makes a difference
13757 in the exact conditions for when it is safe to do the operation
13758 in a narrower mode. */
13759 int shorten = 0;
13760
13761 /* Nonzero if this is a comparison operation;
13762 if both args are promoted shorts, compare the original shorts.
13763 Also implies COMMON. */
13764 int short_compare = 0;
13765
13766 /* Nonzero if this is a right-shift operation, which can be computed on the
13767 original short and then promoted if the operand is a promoted short. */
13768 int short_shift = 0;
13769
13770 /* Nonzero means set RESULT_TYPE to the common type of the args. */
13771 int common = 0;
13772
13773 /* True means types are compatible as far as ObjC is concerned. */
13774 bool objc_ok;
13775
13776 /* True means this is an arithmetic operation that may need excess
13777 precision. */
13778 bool may_need_excess_precision;
13779
13780 /* True means this is a boolean operation that converts both its
13781 operands to truth-values. */
13782 bool boolean_op = false;
13783
13784 /* Remember whether we're doing / or %. */
13785 bool doing_div_or_mod = false;
13786
13787 /* Remember whether we're doing << or >>. */
13788 bool doing_shift = false;
13789
13790 /* Tree holding instrumentation expression. */
13791 tree instrument_expr = NULL;
13792
13793 if (location == UNKNOWN_LOCATION)
13794 location = input_location;
13795
13796 op0 = orig_op0;
13797 op1 = orig_op1;
13798
13799 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
13800 if (op0_int_operands)
13801 op0 = remove_c_maybe_const_expr (op0);
13802 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
13803 if (op1_int_operands)
13804 op1 = remove_c_maybe_const_expr (op1);
13805 int_operands = (op0_int_operands && op1_int_operands);
13806 if (int_operands)
13807 {
13808 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
13809 && TREE_CODE (orig_op1) == INTEGER_CST);
13810 int_const = (int_const_or_overflow
13811 && !TREE_OVERFLOW (orig_op0)
13812 && !TREE_OVERFLOW (orig_op1));
13813 }
13814 else
13815 int_const = int_const_or_overflow = false;
13816
13817 /* Do not apply default conversion in mixed vector/scalar expression. */
13818 if (convert_p
13819 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
13820 {
13821 op0 = default_conversion (op0);
13822 op1 = default_conversion (op1);
13823 }
13824
13825 orig_type0 = type0 = TREE_TYPE (op0);
13826
13827 orig_type1 = type1 = TREE_TYPE (op1);
13828
13829 /* The expression codes of the data types of the arguments tell us
13830 whether the arguments are integers, floating, pointers, etc. */
13831 code0 = TREE_CODE (type0);
13832 code1 = TREE_CODE (type1);
13833
13834 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
13835 STRIP_TYPE_NOPS (op0);
13836 STRIP_TYPE_NOPS (op1);
13837
13838 /* If an error was already reported for one of the arguments,
13839 avoid reporting another error. */
13840
13841 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
13842 return error_mark_node;
13843
13844 if (code0 == POINTER_TYPE
13845 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
13846 return error_mark_node;
13847
13848 if (code1 == POINTER_TYPE
13849 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
13850 return error_mark_node;
13851
13852 if ((invalid_op_diag
13853 = targetm.invalid_binary_op (code, type0, type1)))
13854 {
13855 error_at (location, invalid_op_diag);
13856 return error_mark_node;
13857 }
13858
13859 switch (code)
13860 {
13861 case PLUS_EXPR:
13862 case MINUS_EXPR:
13863 case MULT_EXPR:
13864 case TRUNC_DIV_EXPR:
13865 case CEIL_DIV_EXPR:
13866 case FLOOR_DIV_EXPR:
13867 case ROUND_DIV_EXPR:
13868 case EXACT_DIV_EXPR:
13869 may_need_excess_precision = true;
13870 break;
13871
13872 case EQ_EXPR:
13873 case NE_EXPR:
13874 case LE_EXPR:
13875 case GE_EXPR:
13876 case LT_EXPR:
13877 case GT_EXPR:
13878 /* Excess precision for implicit conversions of integers to
13879 floating point in C11 and later. */
13880 may_need_excess_precision = (flag_isoc11
13881 && (ANY_INTEGRAL_TYPE_P (type0)
13882 || ANY_INTEGRAL_TYPE_P (type1)));
13883 break;
13884
13885 default:
13886 may_need_excess_precision = false;
13887 break;
13888 }
13889 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
13890 {
13891 op0 = TREE_OPERAND (op0, 0);
13892 type0 = TREE_TYPE (op0);
13893 }
13894 else if (may_need_excess_precision
13895 && (eptype = excess_precision_type (type0)) != NULL_TREE)
13896 {
13897 type0 = eptype;
13898 op0 = convert (eptype, op0);
13899 }
13900 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
13901 {
13902 op1 = TREE_OPERAND (op1, 0);
13903 type1 = TREE_TYPE (op1);
13904 }
13905 else if (may_need_excess_precision
13906 && (eptype = excess_precision_type (type1)) != NULL_TREE)
13907 {
13908 type1 = eptype;
13909 op1 = convert (eptype, op1);
13910 }
13911
13912 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
13913
13914 /* In case when one of the operands of the binary operation is
13915 a vector and another is a scalar -- convert scalar to vector. */
13916 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
13917 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
13918 {
13919 enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
13920 orig_op1, true);
13921
13922 switch (convert_flag)
13923 {
13924 case stv_error:
13925 return error_mark_node;
13926 case stv_firstarg:
13927 {
13928 bool maybe_const = true;
13929 tree sc;
13930 sc = c_fully_fold (op0, false, &maybe_const);
13931 sc = save_expr (sc);
13932 sc = convert (TREE_TYPE (type1), sc);
13933 op0 = build_vector_from_val (type1, sc);
13934 if (!maybe_const)
13935 op0 = c_wrap_maybe_const (op0, true);
13936 orig_type0 = type0 = TREE_TYPE (op0);
13937 code0 = TREE_CODE (type0);
13938 converted = 1;
13939 break;
13940 }
13941 case stv_secondarg:
13942 {
13943 bool maybe_const = true;
13944 tree sc;
13945 sc = c_fully_fold (op1, false, &maybe_const);
13946 sc = save_expr (sc);
13947 sc = convert (TREE_TYPE (type0), sc);
13948 op1 = build_vector_from_val (type0, sc);
13949 if (!maybe_const)
13950 op1 = c_wrap_maybe_const (op1, true);
13951 orig_type1 = type1 = TREE_TYPE (op1);
13952 code1 = TREE_CODE (type1);
13953 converted = 1;
13954 break;
13955 }
13956 default:
13957 break;
13958 }
13959 }
13960
13961 switch (code)
13962 {
13963 case PLUS_EXPR:
13964 /* Handle the pointer + int case. */
13965 if (code0 == POINTER_TYPE
13966 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
13967 {
13968 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
13969 goto return_build_binary_op;
13970 }
13971 else if (code1 == POINTER_TYPE
13972 && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE))
13973 {
13974 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
13975 goto return_build_binary_op;
13976 }
13977 else
13978 common = 1;
13979 break;
13980
13981 case MINUS_EXPR:
13982 /* Subtraction of two similar pointers.
13983 We must subtract them as integers, then divide by object size. */
13984 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
13985 && comp_target_types (location, type0, type1))
13986 {
13987 ret = pointer_diff (location, op0, op1, &instrument_expr);
13988 goto return_build_binary_op;
13989 }
13990 /* Handle pointer minus int. Just like pointer plus int. */
13991 else if (code0 == POINTER_TYPE
13992 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
13993 {
13994 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
13995 goto return_build_binary_op;
13996 }
13997 else
13998 common = 1;
13999 break;
14000
14001 case MULT_EXPR:
14002 common = 1;
14003 break;
14004
14005 case TRUNC_DIV_EXPR:
14006 case CEIL_DIV_EXPR:
14007 case FLOOR_DIV_EXPR:
14008 case ROUND_DIV_EXPR:
14009 case EXACT_DIV_EXPR:
14010 doing_div_or_mod = true;
14011 warn_for_div_by_zero (location, op1);
14012
14013 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
14014 || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
14015 || code0 == COMPLEX_TYPE
14016 || gnu_vector_type_p (type0))
14017 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14018 || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
14019 || code1 == COMPLEX_TYPE
14020 || gnu_vector_type_p (type1)))
14021 {
14022 enum tree_code tcode0 = code0, tcode1 = code1;
14023
14024 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
14025 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
14026 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
14027 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
14028
14029 if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE)
14030 && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE))
14031 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
14032 resultcode = RDIV_EXPR;
14033 else
14034 /* Although it would be tempting to shorten always here, that
14035 loses on some targets, since the modulo instruction is
14036 undefined if the quotient can't be represented in the
14037 computation mode. We shorten only if unsigned or if
14038 dividing by something we know != -1. */
14039 shorten = may_shorten_divmod (op0, op1);
14040 common = 1;
14041 }
14042 break;
14043
14044 case BIT_AND_EXPR:
14045 case BIT_IOR_EXPR:
14046 case BIT_XOR_EXPR:
14047 if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14048 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14049 shorten = -1;
14050 /* Allow vector types which are not floating point types. */
14051 else if (gnu_vector_type_p (type0)
14052 && gnu_vector_type_p (type1)
14053 && !VECTOR_FLOAT_TYPE_P (type0)
14054 && !VECTOR_FLOAT_TYPE_P (type1))
14055 common = 1;
14056 break;
14057
14058 case TRUNC_MOD_EXPR:
14059 case FLOOR_MOD_EXPR:
14060 doing_div_or_mod = true;
14061 warn_for_div_by_zero (location, op1);
14062
14063 if (gnu_vector_type_p (type0)
14064 && gnu_vector_type_p (type1)
14065 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14066 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
14067 common = 1;
14068 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14069 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14070 {
14071 /* Although it would be tempting to shorten always here, that loses
14072 on some targets, since the modulo instruction is undefined if the
14073 quotient can't be represented in the computation mode. We shorten
14074 only if unsigned or if dividing by something we know != -1. */
14075 shorten = may_shorten_divmod (op0, op1);
14076 common = 1;
14077 }
14078 break;
14079
14080 case TRUTH_ANDIF_EXPR:
14081 case TRUTH_ORIF_EXPR:
14082 case TRUTH_AND_EXPR:
14083 case TRUTH_OR_EXPR:
14084 case TRUTH_XOR_EXPR:
14085 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
14086 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
14087 || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE
14088 || code0 == BITINT_TYPE)
14089 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
14090 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
14091 || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE
14092 || code1 == BITINT_TYPE))
14093 {
14094 /* Result of these operations is always an int,
14095 but that does not mean the operands should be
14096 converted to ints! */
14097 result_type = integer_type_node;
14098 if (op0_int_operands)
14099 {
14100 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
14101 op0 = remove_c_maybe_const_expr (op0);
14102 }
14103 else
14104 op0 = c_objc_common_truthvalue_conversion (location, op0);
14105 if (op1_int_operands)
14106 {
14107 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
14108 op1 = remove_c_maybe_const_expr (op1);
14109 }
14110 else
14111 op1 = c_objc_common_truthvalue_conversion (location, op1);
14112 converted = 1;
14113 boolean_op = true;
14114 }
14115 if (code == TRUTH_ANDIF_EXPR)
14116 {
14117 int_const_or_overflow = (int_operands
14118 && TREE_CODE (orig_op0) == INTEGER_CST
14119 && (op0 == truthvalue_false_node
14120 || TREE_CODE (orig_op1) == INTEGER_CST));
14121 int_const = (int_const_or_overflow
14122 && !TREE_OVERFLOW (orig_op0)
14123 && (op0 == truthvalue_false_node
14124 || !TREE_OVERFLOW (orig_op1)));
14125 }
14126 else if (code == TRUTH_ORIF_EXPR)
14127 {
14128 int_const_or_overflow = (int_operands
14129 && TREE_CODE (orig_op0) == INTEGER_CST
14130 && (op0 == truthvalue_true_node
14131 || TREE_CODE (orig_op1) == INTEGER_CST));
14132 int_const = (int_const_or_overflow
14133 && !TREE_OVERFLOW (orig_op0)
14134 && (op0 == truthvalue_true_node
14135 || !TREE_OVERFLOW (orig_op1)));
14136 }
14137 break;
14138
14139 /* Shift operations: result has same type as first operand;
14140 always convert second operand to int.
14141 Also set SHORT_SHIFT if shifting rightward. */
14142
14143 case RSHIFT_EXPR:
14144 if (gnu_vector_type_p (type0)
14145 && gnu_vector_type_p (type1)
14146 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14147 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14148 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14149 TYPE_VECTOR_SUBPARTS (type1)))
14150 {
14151 result_type = type0;
14152 converted = 1;
14153 }
14154 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14155 || code0 == BITINT_TYPE
14156 || (gnu_vector_type_p (type0)
14157 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14158 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14159 {
14160 doing_shift = true;
14161 if (TREE_CODE (op1) == INTEGER_CST)
14162 {
14163 if (tree_int_cst_sgn (op1) < 0)
14164 {
14165 int_const = false;
14166 if (c_inhibit_evaluation_warnings == 0)
14167 warning_at (location, OPT_Wshift_count_negative,
14168 "right shift count is negative");
14169 }
14170 else if (code0 == VECTOR_TYPE)
14171 {
14172 if (compare_tree_int (op1,
14173 TYPE_PRECISION (TREE_TYPE (type0)))
14174 >= 0)
14175 {
14176 int_const = false;
14177 if (c_inhibit_evaluation_warnings == 0)
14178 warning_at (location, OPT_Wshift_count_overflow,
14179 "right shift count >= width of vector element");
14180 }
14181 }
14182 else
14183 {
14184 if (!integer_zerop (op1))
14185 short_shift = 1;
14186
14187 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14188 {
14189 int_const = false;
14190 if (c_inhibit_evaluation_warnings == 0)
14191 warning_at (location, OPT_Wshift_count_overflow,
14192 "right shift count >= width of type");
14193 }
14194 }
14195 }
14196
14197 /* Use the type of the value to be shifted. */
14198 result_type = type0;
14199 /* Avoid converting op1 to result_type later. */
14200 converted = 1;
14201 }
14202 break;
14203
14204 case LSHIFT_EXPR:
14205 if (gnu_vector_type_p (type0)
14206 && gnu_vector_type_p (type1)
14207 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14208 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14209 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14210 TYPE_VECTOR_SUBPARTS (type1)))
14211 {
14212 result_type = type0;
14213 converted = 1;
14214 }
14215 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14216 || code0 == BITINT_TYPE
14217 || (gnu_vector_type_p (type0)
14218 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14219 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14220 {
14221 doing_shift = true;
14222 if (TREE_CODE (op0) == INTEGER_CST
14223 && tree_int_cst_sgn (op0) < 0
14224 && !TYPE_OVERFLOW_WRAPS (type0))
14225 {
14226 /* Don't reject a left shift of a negative value in a context
14227 where a constant expression is needed in C90. */
14228 if (flag_isoc99)
14229 int_const = false;
14230 if (c_inhibit_evaluation_warnings == 0)
14231 warning_at (location, OPT_Wshift_negative_value,
14232 "left shift of negative value");
14233 }
14234 if (TREE_CODE (op1) == INTEGER_CST)
14235 {
14236 if (tree_int_cst_sgn (op1) < 0)
14237 {
14238 int_const = false;
14239 if (c_inhibit_evaluation_warnings == 0)
14240 warning_at (location, OPT_Wshift_count_negative,
14241 "left shift count is negative");
14242 }
14243 else if (code0 == VECTOR_TYPE)
14244 {
14245 if (compare_tree_int (op1,
14246 TYPE_PRECISION (TREE_TYPE (type0)))
14247 >= 0)
14248 {
14249 int_const = false;
14250 if (c_inhibit_evaluation_warnings == 0)
14251 warning_at (location, OPT_Wshift_count_overflow,
14252 "left shift count >= width of vector element");
14253 }
14254 }
14255 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14256 {
14257 int_const = false;
14258 if (c_inhibit_evaluation_warnings == 0)
14259 warning_at (location, OPT_Wshift_count_overflow,
14260 "left shift count >= width of type");
14261 }
14262 else if (TREE_CODE (op0) == INTEGER_CST
14263 && maybe_warn_shift_overflow (location, op0, op1)
14264 && flag_isoc99)
14265 int_const = false;
14266 }
14267
14268 /* Use the type of the value to be shifted. */
14269 result_type = type0;
14270 /* Avoid converting op1 to result_type later. */
14271 converted = 1;
14272 }
14273 break;
14274
14275 case EQ_EXPR:
14276 case NE_EXPR:
14277 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14278 {
14279 tree intt;
14280 if (!vector_types_compatible_elements_p (type0, type1))
14281 {
14282 error_at (location, "comparing vectors with different "
14283 "element types");
14284 return error_mark_node;
14285 }
14286
14287 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14288 TYPE_VECTOR_SUBPARTS (type1)))
14289 {
14290 error_at (location, "comparing vectors with different "
14291 "number of elements");
14292 return error_mark_node;
14293 }
14294
14295 /* It's not precisely specified how the usual arithmetic
14296 conversions apply to the vector types. Here, we use
14297 the unsigned type if one of the operands is signed and
14298 the other one is unsigned. */
14299 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14300 {
14301 if (!TYPE_UNSIGNED (type0))
14302 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14303 else
14304 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
14305 warning_at (location, OPT_Wsign_compare, "comparison between "
14306 "types %qT and %qT", type0, type1);
14307 }
14308
14309 /* Always construct signed integer vector type. */
14310 intt = c_common_type_for_size (GET_MODE_BITSIZE
14311 (SCALAR_TYPE_MODE
14312 (TREE_TYPE (type0))), 0);
14313 if (!intt)
14314 {
14315 error_at (location, "could not find an integer type "
14316 "of the same size as %qT",
14317 TREE_TYPE (type0));
14318 return error_mark_node;
14319 }
14320 result_type = build_opaque_vector_type (intt,
14321 TYPE_VECTOR_SUBPARTS (type0));
14322 converted = 1;
14323 ret = build_vec_cmp (resultcode, result_type, op0, op1);
14324 goto return_build_binary_op;
14325 }
14326 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
14327 warning_at (location,
14328 OPT_Wfloat_equal,
14329 "comparing floating-point with %<==%> or %<!=%> is unsafe");
14330 /* Result of comparison is always int,
14331 but don't convert the args to int! */
14332 build_type = integer_type_node;
14333 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE
14334 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
14335 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14336 || code1 == BITINT_TYPE
14337 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
14338 short_compare = 1;
14339 else if (code0 == POINTER_TYPE
14340 && (code1 == NULLPTR_TYPE
14341 || null_pointer_constant_p (orig_op1)))
14342 {
14343 maybe_warn_for_null_address (location, op0, code);
14344 result_type = type0;
14345 }
14346 else if (code1 == POINTER_TYPE
14347 && (code0 == NULLPTR_TYPE
14348 || null_pointer_constant_p (orig_op0)))
14349 {
14350 maybe_warn_for_null_address (location, op1, code);
14351 result_type = type1;
14352 }
14353 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
14354 {
14355 tree tt0 = TREE_TYPE (type0);
14356 tree tt1 = TREE_TYPE (type1);
14357 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
14358 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
14359 addr_space_t as_common = ADDR_SPACE_GENERIC;
14360
14361 /* Anything compares with void *. void * compares with anything.
14362 Otherwise, the targets must be compatible
14363 and both must be object or both incomplete. */
14364 if (comp_target_types (location, type0, type1))
14365 result_type = common_pointer_type (type0, type1);
14366 else if (!addr_space_superset (as0, as1, &as_common))
14367 {
14368 error_at (location, "comparison of pointers to "
14369 "disjoint address spaces");
14370 return error_mark_node;
14371 }
14372 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
14373 {
14374 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
14375 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14376 "comparison of %<void *%> with function pointer");
14377 }
14378 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
14379 {
14380 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
14381 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14382 "comparison of %<void *%> with function pointer");
14383 }
14384 else
14385 /* Avoid warning about the volatile ObjC EH puts on decls. */
14386 if (!objc_ok)
14387 pedwarn (location, OPT_Wcompare_distinct_pointer_types,
14388 "comparison of distinct pointer types lacks a cast");
14389
14390 if (result_type == NULL_TREE)
14391 {
14392 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
14393 result_type = c_build_pointer_type
14394 (c_build_qualified_type (void_type_node, qual));
14395 }
14396 }
14397 else if (code0 == POINTER_TYPE
14398 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14399 {
14400 result_type = type0;
14401 pedwarn (location, 0, "comparison between pointer and integer");
14402 }
14403 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14404 && code1 == POINTER_TYPE)
14405 {
14406 result_type = type1;
14407 pedwarn (location, 0, "comparison between pointer and integer");
14408 }
14409 /* 6.5.9: One of the following shall hold:
14410 -- both operands have type nullptr_t; */
14411 else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
14412 {
14413 result_type = nullptr_type_node;
14414 /* No need to convert the operands to result_type later. */
14415 converted = 1;
14416 }
14417 /* -- one operand has type nullptr_t and the other is a null pointer
14418 constant. We will have to convert the former to the type of the
14419 latter, because during gimplification we can't have mismatching
14420 comparison operand type. We convert from nullptr_t to the other
14421 type, since only nullptr_t can be converted to nullptr_t. Also,
14422 even a constant 0 is a null pointer constant, so we may have to
14423 create a pointer type from its type. */
14424 else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
14425 result_type = (INTEGRAL_TYPE_P (type1)
14426 ? c_build_pointer_type (type1) : type1);
14427 else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
14428 result_type = (INTEGRAL_TYPE_P (type0)
14429 ? c_build_pointer_type (type0) : type0);
14430 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
14431 || truth_value_p (TREE_CODE (orig_op0)))
14432 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
14433 || truth_value_p (TREE_CODE (orig_op1))))
14434 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
14435 break;
14436
14437 case LE_EXPR:
14438 case GE_EXPR:
14439 case LT_EXPR:
14440 case GT_EXPR:
14441 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14442 {
14443 tree intt;
14444 if (!vector_types_compatible_elements_p (type0, type1))
14445 {
14446 error_at (location, "comparing vectors with different "
14447 "element types");
14448 return error_mark_node;
14449 }
14450
14451 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14452 TYPE_VECTOR_SUBPARTS (type1)))
14453 {
14454 error_at (location, "comparing vectors with different "
14455 "number of elements");
14456 return error_mark_node;
14457 }
14458
14459 /* It's not precisely specified how the usual arithmetic
14460 conversions apply to the vector types. Here, we use
14461 the unsigned type if one of the operands is signed and
14462 the other one is unsigned. */
14463 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14464 {
14465 if (!TYPE_UNSIGNED (type0))
14466 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14467 else
14468 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
14469 warning_at (location, OPT_Wsign_compare, "comparison between "
14470 "types %qT and %qT", type0, type1);
14471 }
14472
14473 /* Always construct signed integer vector type. */
14474 intt = c_common_type_for_size (GET_MODE_BITSIZE
14475 (SCALAR_TYPE_MODE
14476 (TREE_TYPE (type0))), 0);
14477 if (!intt)
14478 {
14479 error_at (location, "could not find an integer type "
14480 "of the same size as %qT",
14481 TREE_TYPE (type0));
14482 return error_mark_node;
14483 }
14484 result_type = build_opaque_vector_type (intt,
14485 TYPE_VECTOR_SUBPARTS (type0));
14486 converted = 1;
14487 ret = build_vec_cmp (resultcode, result_type, op0, op1);
14488 goto return_build_binary_op;
14489 }
14490 build_type = integer_type_node;
14491 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
14492 || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE)
14493 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14494 || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE))
14495 short_compare = 1;
14496 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
14497 {
14498 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
14499 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
14500 addr_space_t as_common;
14501
14502 if (comp_target_types (location, type0, type1))
14503 {
14504 result_type = common_pointer_type (type0, type1);
14505 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
14506 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
14507 pedwarn_c99 (location, OPT_Wpedantic,
14508 "comparison of complete and incomplete pointers");
14509 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
14510 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14511 "ordered comparisons of pointers to functions");
14512 else if (null_pointer_constant_p (orig_op0)
14513 || null_pointer_constant_p (orig_op1))
14514 warning_at (location, OPT_Wextra,
14515 "ordered comparison of pointer with null pointer");
14516
14517 }
14518 else if (!addr_space_superset (as0, as1, &as_common))
14519 {
14520 error_at (location, "comparison of pointers to "
14521 "disjoint address spaces");
14522 return error_mark_node;
14523 }
14524 else
14525 {
14526 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
14527 result_type = c_build_pointer_type
14528 (c_build_qualified_type (void_type_node, qual));
14529 pedwarn (location, OPT_Wcompare_distinct_pointer_types,
14530 "comparison of distinct pointer types lacks a cast");
14531 }
14532 }
14533 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
14534 {
14535 result_type = type0;
14536 if (pedantic)
14537 pedwarn (location, OPT_Wpedantic,
14538 "ordered comparison of pointer with integer zero");
14539 else if (extra_warnings)
14540 warning_at (location, OPT_Wextra,
14541 "ordered comparison of pointer with integer zero");
14542 }
14543 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
14544 {
14545 result_type = type1;
14546 if (pedantic)
14547 pedwarn (location, OPT_Wpedantic,
14548 "ordered comparison of pointer with integer zero");
14549 else if (extra_warnings)
14550 warning_at (location, OPT_Wextra,
14551 "ordered comparison of pointer with integer zero");
14552 }
14553 else if (code0 == POINTER_TYPE
14554 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14555 {
14556 result_type = type0;
14557 pedwarn (location, 0, "comparison between pointer and integer");
14558 }
14559 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14560 && code1 == POINTER_TYPE)
14561 {
14562 result_type = type1;
14563 pedwarn (location, 0, "comparison between pointer and integer");
14564 }
14565
14566 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
14567 && current_function_decl != NULL_TREE
14568 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
14569 {
14570 op0 = save_expr (c_fully_fold (op0, false, NULL));
14571 op1 = save_expr (c_fully_fold (op1, false, NULL));
14572
14573 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
14574 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
14575 }
14576
14577 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
14578 || truth_value_p (TREE_CODE (orig_op0)))
14579 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
14580 || truth_value_p (TREE_CODE (orig_op1))))
14581 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
14582 break;
14583
14584 case MIN_EXPR:
14585 case MAX_EXPR:
14586 /* Used for OpenMP atomics. */
14587 gcc_assert (flag_openmp);
14588 common = 1;
14589 break;
14590
14591 default:
14592 gcc_unreachable ();
14593 }
14594
14595 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
14596 return error_mark_node;
14597
14598 if (gnu_vector_type_p (type0)
14599 && gnu_vector_type_p (type1)
14600 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
14601 || !vector_types_compatible_elements_p (type0, type1)))
14602 {
14603 gcc_rich_location richloc (location);
14604 maybe_range_label_for_tree_type_mismatch
14605 label_for_op0 (orig_op0, orig_op1),
14606 label_for_op1 (orig_op1, orig_op0);
14607 richloc.maybe_add_expr (orig_op0, &label_for_op0, highlight_colors::lhs);
14608 richloc.maybe_add_expr (orig_op1, &label_for_op1, highlight_colors::rhs);
14609 binary_op_error (&richloc, code, type0, type1);
14610 return error_mark_node;
14611 }
14612
14613 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
14614 || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
14615 || gnu_vector_type_p (type0))
14616 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
14617 || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
14618 || gnu_vector_type_p (type1)))
14619 {
14620 bool first_complex = (code0 == COMPLEX_TYPE);
14621 bool second_complex = (code1 == COMPLEX_TYPE);
14622 int none_complex = (!first_complex && !second_complex);
14623
14624 if (shorten || common || short_compare)
14625 {
14626 result_type = c_common_type (type0, type1);
14627 do_warn_double_promotion (result_type, type0, type1,
14628 "implicit conversion from %qT to %qT "
14629 "to match other operand of binary "
14630 "expression",
14631 location);
14632 if (result_type == error_mark_node)
14633 return error_mark_node;
14634 }
14635
14636 if (first_complex != second_complex
14637 && (code == PLUS_EXPR
14638 || code == MINUS_EXPR
14639 || code == MULT_EXPR
14640 || (code == TRUNC_DIV_EXPR && first_complex))
14641 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
14642 && flag_signed_zeros)
14643 {
14644 /* An operation on mixed real/complex operands must be
14645 handled specially, but the language-independent code can
14646 more easily optimize the plain complex arithmetic if
14647 -fno-signed-zeros. */
14648 tree real_type = TREE_TYPE (result_type);
14649 tree real, imag;
14650 if (type0 != orig_type0 || type1 != orig_type1)
14651 {
14652 gcc_assert (may_need_excess_precision && common);
14653 semantic_result_type = c_common_type (orig_type0, orig_type1);
14654 }
14655 if (first_complex)
14656 {
14657 if (TREE_TYPE (op0) != result_type)
14658 op0 = convert_and_check (location, result_type, op0);
14659 if (TREE_TYPE (op1) != real_type)
14660 op1 = convert_and_check (location, real_type, op1);
14661 }
14662 else
14663 {
14664 if (TREE_TYPE (op0) != real_type)
14665 op0 = convert_and_check (location, real_type, op0);
14666 if (TREE_TYPE (op1) != result_type)
14667 op1 = convert_and_check (location, result_type, op1);
14668 }
14669 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
14670 return error_mark_node;
14671 if (first_complex)
14672 {
14673 op0 = save_expr (op0);
14674 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
14675 op0, true);
14676 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
14677 op0, true);
14678 switch (code)
14679 {
14680 case MULT_EXPR:
14681 case TRUNC_DIV_EXPR:
14682 op1 = save_expr (op1);
14683 imag = build2 (resultcode, real_type, imag, op1);
14684 /* Fall through. */
14685 case PLUS_EXPR:
14686 case MINUS_EXPR:
14687 real = build2 (resultcode, real_type, real, op1);
14688 break;
14689 default:
14690 gcc_unreachable();
14691 }
14692 }
14693 else
14694 {
14695 op1 = save_expr (op1);
14696 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
14697 op1, true);
14698 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
14699 op1, true);
14700 switch (code)
14701 {
14702 case MULT_EXPR:
14703 op0 = save_expr (op0);
14704 imag = build2 (resultcode, real_type, op0, imag);
14705 /* Fall through. */
14706 case PLUS_EXPR:
14707 real = build2 (resultcode, real_type, op0, real);
14708 break;
14709 case MINUS_EXPR:
14710 real = build2 (resultcode, real_type, op0, real);
14711 imag = build1 (NEGATE_EXPR, real_type, imag);
14712 break;
14713 default:
14714 gcc_unreachable();
14715 }
14716 }
14717 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
14718 goto return_build_binary_op;
14719 }
14720
14721 /* For certain operations (which identify themselves by shorten != 0)
14722 if both args were extended from the same smaller type,
14723 do the arithmetic in that type and then extend.
14724
14725 shorten !=0 and !=1 indicates a bitwise operation.
14726 For them, this optimization is safe only if
14727 both args are zero-extended or both are sign-extended.
14728 Otherwise, we might change the result.
14729 Eg, (short)-1 | (unsigned short)-1 is (int)-1
14730 but calculated in (unsigned short) it would be (unsigned short)-1. */
14731
14732 if (shorten && none_complex)
14733 {
14734 final_type = result_type;
14735 result_type = shorten_binary_op (result_type, op0, op1,
14736 shorten == -1);
14737 }
14738
14739 /* Shifts can be shortened if shifting right. */
14740
14741 if (short_shift)
14742 {
14743 int unsigned_arg;
14744 tree arg0 = get_narrower (op0, &unsigned_arg);
14745
14746 final_type = result_type;
14747
14748 if (arg0 == op0 && final_type == TREE_TYPE (op0))
14749 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
14750
14751 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
14752 && tree_int_cst_sgn (op1) > 0
14753 /* We can shorten only if the shift count is less than the
14754 number of bits in the smaller type size. */
14755 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
14756 /* We cannot drop an unsigned shift after sign-extension. */
14757 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
14758 {
14759 /* Do an unsigned shift if the operand was zero-extended. */
14760 result_type
14761 = c_common_signed_or_unsigned_type (unsigned_arg,
14762 TREE_TYPE (arg0));
14763 /* Convert value-to-be-shifted to that type. */
14764 if (TREE_TYPE (op0) != result_type)
14765 op0 = convert (result_type, op0);
14766 converted = 1;
14767 }
14768 }
14769
14770 /* Comparison operations are shortened too but differently.
14771 They identify themselves by setting short_compare = 1. */
14772
14773 if (short_compare)
14774 {
14775 /* Don't write &op0, etc., because that would prevent op0
14776 from being kept in a register.
14777 Instead, make copies of the our local variables and
14778 pass the copies by reference, then copy them back afterward. */
14779 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
14780 enum tree_code xresultcode = resultcode;
14781 tree val
14782 = shorten_compare (location, &xop0, &xop1, &xresult_type,
14783 &xresultcode);
14784
14785 if (val != NULL_TREE)
14786 {
14787 ret = val;
14788 goto return_build_binary_op;
14789 }
14790
14791 op0 = xop0, op1 = xop1;
14792 converted = 1;
14793 resultcode = xresultcode;
14794
14795 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
14796 {
14797 bool op0_maybe_const = true;
14798 bool op1_maybe_const = true;
14799 tree orig_op0_folded, orig_op1_folded;
14800
14801 if (in_late_binary_op)
14802 {
14803 orig_op0_folded = orig_op0;
14804 orig_op1_folded = orig_op1;
14805 }
14806 else
14807 {
14808 /* Fold for the sake of possible warnings, as in
14809 build_conditional_expr. This requires the
14810 "original" values to be folded, not just op0 and
14811 op1. */
14812 c_inhibit_evaluation_warnings++;
14813 op0 = c_fully_fold (op0, require_constant_value,
14814 &op0_maybe_const);
14815 op1 = c_fully_fold (op1, require_constant_value,
14816 &op1_maybe_const);
14817 c_inhibit_evaluation_warnings--;
14818 orig_op0_folded = c_fully_fold (orig_op0,
14819 require_constant_value,
14820 NULL);
14821 orig_op1_folded = c_fully_fold (orig_op1,
14822 require_constant_value,
14823 NULL);
14824 }
14825
14826 if (warn_sign_compare)
14827 warn_for_sign_compare (location, orig_op0_folded,
14828 orig_op1_folded, op0, op1,
14829 result_type, resultcode);
14830 if (!in_late_binary_op && !int_operands)
14831 {
14832 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
14833 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
14834 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
14835 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
14836 }
14837 }
14838 }
14839 }
14840
14841 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
14842 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
14843 Then the expression will be built.
14844 It will be given type FINAL_TYPE if that is nonzero;
14845 otherwise, it will be given type RESULT_TYPE. */
14846
14847 if (!result_type)
14848 {
14849 /* Favor showing any expression locations that are available. */
14850 op_location_t oploc (location, UNKNOWN_LOCATION);
14851 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
14852 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
14853 return error_mark_node;
14854 }
14855
14856 if (build_type == NULL_TREE)
14857 {
14858 build_type = result_type;
14859 if ((type0 != orig_type0 || type1 != orig_type1)
14860 && !boolean_op)
14861 {
14862 gcc_assert (may_need_excess_precision && common);
14863 semantic_result_type = c_common_type (orig_type0, orig_type1);
14864 }
14865 }
14866
14867 if (!converted)
14868 {
14869 op0 = ep_convert_and_check (location, result_type, op0,
14870 semantic_result_type);
14871 op1 = ep_convert_and_check (location, result_type, op1,
14872 semantic_result_type);
14873
14874 /* This can happen if one operand has a vector type, and the other
14875 has a different type. */
14876 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
14877 return error_mark_node;
14878 }
14879
14880 if (sanitize_flags_p ((SANITIZE_SHIFT
14881 | SANITIZE_DIVIDE
14882 | SANITIZE_FLOAT_DIVIDE
14883 | SANITIZE_SI_OVERFLOW))
14884 && current_function_decl != NULL_TREE
14885 && (doing_div_or_mod || doing_shift)
14886 && !require_constant_value)
14887 {
14888 /* OP0 and/or OP1 might have side-effects. */
14889 op0 = save_expr (op0);
14890 op1 = save_expr (op1);
14891 op0 = c_fully_fold (op0, false, NULL);
14892 op1 = c_fully_fold (op1, false, NULL);
14893 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
14894 | SANITIZE_FLOAT_DIVIDE
14895 | SANITIZE_SI_OVERFLOW))))
14896 instrument_expr = ubsan_instrument_division (location, op0, op1);
14897 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
14898 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
14899 }
14900
14901 /* Treat expressions in initializers specially as they can't trap. */
14902 if (int_const_or_overflow)
14903 ret = (require_constant_value
14904 ? fold_build2_initializer_loc (location, resultcode, build_type,
14905 op0, op1)
14906 : fold_build2_loc (location, resultcode, build_type, op0, op1));
14907 else
14908 ret = build2 (resultcode, build_type, op0, op1);
14909 if (final_type != NULL_TREE)
14910 ret = convert (final_type, ret);
14911
14912 return_build_binary_op:
14913 gcc_assert (ret != error_mark_node);
14914 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
14915 ret = (int_operands
14916 ? note_integer_operands (ret)
14917 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
14918 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
14919 && !in_late_binary_op)
14920 ret = note_integer_operands (ret);
14921 protected_set_expr_location (ret, location);
14922
14923 if (instrument_expr != NULL)
14924 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
14925 instrument_expr, ret);
14926
14927 if (semantic_result_type)
14928 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
14929 semantic_result_type, ret);
14930
14931 return ret;
14932 }
14933
14934
14935 /* Convert EXPR to be a truth-value (type TYPE), validating its type for this
14936 purpose. LOCATION is the source location for the expression. */
14937
14938 tree
14939 c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type)
14940 {
14941 bool int_const, int_operands;
14942
14943 switch (TREE_CODE (TREE_TYPE (expr)))
14944 {
14945 case ARRAY_TYPE:
14946 error_at (location, "used array that cannot be converted to pointer where scalar is required");
14947 return error_mark_node;
14948
14949 case RECORD_TYPE:
14950 error_at (location, "used struct type value where scalar is required");
14951 return error_mark_node;
14952
14953 case UNION_TYPE:
14954 error_at (location, "used union type value where scalar is required");
14955 return error_mark_node;
14956
14957 case VOID_TYPE:
14958 error_at (location, "void value not ignored as it ought to be");
14959 return error_mark_node;
14960
14961 case POINTER_TYPE:
14962 if (reject_gcc_builtin (expr))
14963 return error_mark_node;
14964 break;
14965
14966 case FUNCTION_TYPE:
14967 gcc_unreachable ();
14968
14969 case VECTOR_TYPE:
14970 error_at (location, "used vector type where scalar is required");
14971 return error_mark_node;
14972
14973 default:
14974 break;
14975 }
14976
14977 /* Conversion of a floating constant to boolean goes through here
14978 and yields an integer constant expression. Otherwise, the result
14979 is only an integer constant expression if the argument is. */
14980 int_const = ((TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr))
14981 || ((TREE_CODE (expr) == REAL_CST
14982 || TREE_CODE (expr) == COMPLEX_CST)
14983 && (TREE_CODE (type) == BOOLEAN_TYPE
14984 || (TREE_CODE (type) == ENUMERAL_TYPE
14985 && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
14986 && (TREE_CODE (ENUM_UNDERLYING_TYPE (type))
14987 == BOOLEAN_TYPE)))));
14988 int_operands = EXPR_INT_CONST_OPERANDS (expr);
14989 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
14990 {
14991 expr = remove_c_maybe_const_expr (expr);
14992 expr = build2 (NE_EXPR, type, expr,
14993 convert (TREE_TYPE (expr), integer_zero_node));
14994 expr = note_integer_operands (expr);
14995 }
14996 else
14997 {
14998 /* ??? Should we also give an error for vectors rather than leaving
14999 those to give errors later? */
15000 expr = c_common_truthvalue_conversion (location, expr);
15001 expr = fold_convert_loc (location, type, expr);
15002 }
15003
15004 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
15005 {
15006 if (TREE_OVERFLOW (expr))
15007 return expr;
15008 else
15009 return note_integer_operands (expr);
15010 }
15011 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
15012 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
15013 return expr;
15014 }
15015
15016
15017 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
15018 required. */
15019
15020 tree
15021 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
15022 {
15023 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
15024 {
15025 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
15026 /* Executing a compound literal inside a function reinitializes
15027 it. */
15028 if (!TREE_STATIC (decl))
15029 *se = true;
15030 return decl;
15031 }
15032 else
15033 return expr;
15034 }
15035
15036 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
15037 statement. LOC is the location of the construct. */
15038
15039 tree
15040 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
15041 tree clauses)
15042 {
15043 body = c_end_compound_stmt (loc, body, true);
15044
15045 tree stmt = make_node (code);
15046 TREE_TYPE (stmt) = void_type_node;
15047 OMP_BODY (stmt) = body;
15048 OMP_CLAUSES (stmt) = clauses;
15049 SET_EXPR_LOCATION (stmt, loc);
15050
15051 return add_stmt (stmt);
15052 }
15053
15054 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
15055 statement. LOC is the location of the OACC_DATA. */
15056
15057 tree
15058 c_finish_oacc_data (location_t loc, tree clauses, tree block)
15059 {
15060 tree stmt;
15061
15062 block = c_end_compound_stmt (loc, block, true);
15063
15064 stmt = make_node (OACC_DATA);
15065 TREE_TYPE (stmt) = void_type_node;
15066 OACC_DATA_CLAUSES (stmt) = clauses;
15067 OACC_DATA_BODY (stmt) = block;
15068 SET_EXPR_LOCATION (stmt, loc);
15069
15070 return add_stmt (stmt);
15071 }
15072
15073 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
15074 statement. LOC is the location of the OACC_HOST_DATA. */
15075
15076 tree
15077 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
15078 {
15079 tree stmt;
15080
15081 block = c_end_compound_stmt (loc, block, true);
15082
15083 stmt = make_node (OACC_HOST_DATA);
15084 TREE_TYPE (stmt) = void_type_node;
15085 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
15086 OACC_HOST_DATA_BODY (stmt) = block;
15087 SET_EXPR_LOCATION (stmt, loc);
15088
15089 return add_stmt (stmt);
15090 }
15091
15092 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15093
15094 tree
15095 c_begin_omp_parallel (void)
15096 {
15097 tree block;
15098
15099 keep_next_level ();
15100 block = c_begin_compound_stmt (true);
15101
15102 return block;
15103 }
15104
15105 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
15106 statement. LOC is the location of the OMP_PARALLEL. */
15107
15108 tree
15109 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
15110 {
15111 tree stmt;
15112
15113 block = c_end_compound_stmt (loc, block, true);
15114
15115 stmt = make_node (OMP_PARALLEL);
15116 TREE_TYPE (stmt) = void_type_node;
15117 OMP_PARALLEL_CLAUSES (stmt) = clauses;
15118 OMP_PARALLEL_BODY (stmt) = block;
15119 SET_EXPR_LOCATION (stmt, loc);
15120
15121 return add_stmt (stmt);
15122 }
15123
15124 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15125
15126 tree
15127 c_begin_omp_task (void)
15128 {
15129 tree block;
15130
15131 keep_next_level ();
15132 block = c_begin_compound_stmt (true);
15133
15134 return block;
15135 }
15136
15137 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
15138 statement. LOC is the location of the #pragma. */
15139
15140 tree
15141 c_finish_omp_task (location_t loc, tree clauses, tree block)
15142 {
15143 tree stmt;
15144
15145 block = c_end_compound_stmt (loc, block, true);
15146
15147 stmt = make_node (OMP_TASK);
15148 TREE_TYPE (stmt) = void_type_node;
15149 OMP_TASK_CLAUSES (stmt) = clauses;
15150 OMP_TASK_BODY (stmt) = block;
15151 SET_EXPR_LOCATION (stmt, loc);
15152
15153 return add_stmt (stmt);
15154 }
15155
15156 /* Generate GOMP_cancel call for #pragma omp cancel. */
15157
15158 void
15159 c_finish_omp_cancel (location_t loc, tree clauses)
15160 {
15161 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
15162 int mask = 0;
15163 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15164 mask = 1;
15165 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15166 mask = 2;
15167 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15168 mask = 4;
15169 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15170 mask = 8;
15171 else
15172 {
15173 error_at (loc, "%<#pragma omp cancel%> must specify one of "
15174 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15175 "clauses");
15176 return;
15177 }
15178 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
15179 if (ifc != NULL_TREE)
15180 {
15181 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
15182 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
15183 error_at (OMP_CLAUSE_LOCATION (ifc),
15184 "expected %<cancel%> %<if%> clause modifier");
15185 else
15186 {
15187 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
15188 if (ifc2 != NULL_TREE)
15189 {
15190 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
15191 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
15192 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
15193 error_at (OMP_CLAUSE_LOCATION (ifc2),
15194 "expected %<cancel%> %<if%> clause modifier");
15195 }
15196 }
15197
15198 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
15199 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
15200 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
15201 build_zero_cst (type));
15202 }
15203 else
15204 ifc = boolean_true_node;
15205 tree stmt = build_call_expr_loc (loc, fn, 2,
15206 build_int_cst (integer_type_node, mask),
15207 ifc);
15208 add_stmt (stmt);
15209 }
15210
15211 /* Generate GOMP_cancellation_point call for
15212 #pragma omp cancellation point. */
15213
15214 void
15215 c_finish_omp_cancellation_point (location_t loc, tree clauses)
15216 {
15217 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
15218 int mask = 0;
15219 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15220 mask = 1;
15221 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15222 mask = 2;
15223 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15224 mask = 4;
15225 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15226 mask = 8;
15227 else
15228 {
15229 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
15230 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15231 "clauses");
15232 return;
15233 }
15234 tree stmt = build_call_expr_loc (loc, fn, 1,
15235 build_int_cst (integer_type_node, mask));
15236 add_stmt (stmt);
15237 }
15238
15239 /* Helper function for handle_omp_array_sections. Called recursively
15240 to handle multiple array-section-subscripts. C is the clause,
15241 T current expression (initially OMP_CLAUSE_DECL), which is either
15242 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
15243 expression if specified, TREE_VALUE length expression if specified,
15244 TREE_CHAIN is what it has been specified after, or some decl.
15245 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
15246 set to true if any of the array-section-subscript could have length
15247 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
15248 first array-section-subscript which is known not to have length
15249 of one. Given say:
15250 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
15251 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
15252 all are or may have length of 1, array-section-subscript [:2] is the
15253 first one known not to have length 1. For array-section-subscript
15254 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
15255 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
15256 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
15257 case though, as some lengths could be zero. */
15258
15259 static tree
15260 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
15261 bool &maybe_zero_len, unsigned int &first_non_one,
15262 enum c_omp_region_type ort)
15263 {
15264 tree ret, low_bound, length, type;
15265 bool openacc = (ort & C_ORT_ACC) != 0;
15266 if (TREE_CODE (t) != OMP_ARRAY_SECTION)
15267 {
15268 if (error_operand_p (t))
15269 return error_mark_node;
15270 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15271 ret = t;
15272 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15273 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15274 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15275 {
15276 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
15277 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15278 return error_mark_node;
15279 }
15280 if (!ai.check_clause (c))
15281 return error_mark_node;
15282 else if (ai.component_access_p ()
15283 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15284 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
15285 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
15286 t = ai.get_root_term (true);
15287 else
15288 t = ai.unconverted_ref_origin ();
15289 if (t == error_mark_node)
15290 return error_mark_node;
15291 if (!VAR_P (t)
15292 && (ort == C_ORT_ACC || !EXPR_P (t))
15293 && TREE_CODE (t) != PARM_DECL)
15294 {
15295 if (DECL_P (t))
15296 error_at (OMP_CLAUSE_LOCATION (c),
15297 "%qD is not a variable in %qs clause", t,
15298 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15299 else
15300 error_at (OMP_CLAUSE_LOCATION (c),
15301 "%qE is not a variable in %qs clause", t,
15302 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15303 return error_mark_node;
15304 }
15305 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15306 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15307 && TYPE_ATOMIC (TREE_TYPE (t)))
15308 {
15309 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
15310 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15311 return error_mark_node;
15312 }
15313 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15314 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15315 && VAR_P (t)
15316 && DECL_THREAD_LOCAL_P (t))
15317 {
15318 error_at (OMP_CLAUSE_LOCATION (c),
15319 "%qD is threadprivate variable in %qs clause", t,
15320 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15321 return error_mark_node;
15322 }
15323 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15324 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
15325 && TYPE_ATOMIC (TREE_TYPE (t))
15326 && POINTER_TYPE_P (TREE_TYPE (t)))
15327 {
15328 /* If the array section is pointer based and the pointer
15329 itself is _Atomic qualified, we need to atomically load
15330 the pointer. */
15331 c_expr expr;
15332 memset (&expr, 0, sizeof (expr));
15333 expr.value = ret;
15334 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
15335 expr, false, false);
15336 ret = expr.value;
15337 }
15338 return ret;
15339 }
15340
15341 ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
15342 maybe_zero_len, first_non_one, ort);
15343 if (ret == error_mark_node || ret == NULL_TREE)
15344 return ret;
15345
15346 type = TREE_TYPE (ret);
15347 low_bound = TREE_OPERAND (t, 1);
15348 length = TREE_OPERAND (t, 2);
15349
15350 if (low_bound == error_mark_node || length == error_mark_node)
15351 return error_mark_node;
15352
15353 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
15354 {
15355 error_at (OMP_CLAUSE_LOCATION (c),
15356 "low bound %qE of array section does not have integral type",
15357 low_bound);
15358 return error_mark_node;
15359 }
15360 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
15361 {
15362 error_at (OMP_CLAUSE_LOCATION (c),
15363 "length %qE of array section does not have integral type",
15364 length);
15365 return error_mark_node;
15366 }
15367 if (low_bound
15368 && TREE_CODE (low_bound) == INTEGER_CST
15369 && TYPE_PRECISION (TREE_TYPE (low_bound))
15370 > TYPE_PRECISION (sizetype))
15371 low_bound = fold_convert (sizetype, low_bound);
15372 if (length
15373 && TREE_CODE (length) == INTEGER_CST
15374 && TYPE_PRECISION (TREE_TYPE (length))
15375 > TYPE_PRECISION (sizetype))
15376 length = fold_convert (sizetype, length);
15377 if (low_bound == NULL_TREE)
15378 low_bound = integer_zero_node;
15379 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15380 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15381 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15382 {
15383 if (length != integer_one_node)
15384 {
15385 error_at (OMP_CLAUSE_LOCATION (c),
15386 "expected single pointer in %qs clause",
15387 user_omp_clause_code_name (c, openacc));
15388 return error_mark_node;
15389 }
15390 }
15391 if (length != NULL_TREE)
15392 {
15393 if (!integer_nonzerop (length))
15394 {
15395 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15396 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15397 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15398 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15399 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15400 {
15401 if (integer_zerop (length))
15402 {
15403 error_at (OMP_CLAUSE_LOCATION (c),
15404 "zero length array section in %qs clause",
15405 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15406 return error_mark_node;
15407 }
15408 }
15409 else
15410 maybe_zero_len = true;
15411 }
15412 if (first_non_one == types.length ()
15413 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
15414 first_non_one++;
15415 }
15416 if (TREE_CODE (type) == ARRAY_TYPE)
15417 {
15418 if (length == NULL_TREE
15419 && (TYPE_DOMAIN (type) == NULL_TREE
15420 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
15421 {
15422 error_at (OMP_CLAUSE_LOCATION (c),
15423 "for unknown bound array type length expression must "
15424 "be specified");
15425 return error_mark_node;
15426 }
15427 if (TREE_CODE (low_bound) == INTEGER_CST
15428 && tree_int_cst_sgn (low_bound) == -1)
15429 {
15430 error_at (OMP_CLAUSE_LOCATION (c),
15431 "negative low bound in array section in %qs clause",
15432 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15433 return error_mark_node;
15434 }
15435 if (length != NULL_TREE
15436 && TREE_CODE (length) == INTEGER_CST
15437 && tree_int_cst_sgn (length) == -1)
15438 {
15439 error_at (OMP_CLAUSE_LOCATION (c),
15440 "negative length in array section in %qs clause",
15441 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15442 return error_mark_node;
15443 }
15444 if (TYPE_DOMAIN (type)
15445 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
15446 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
15447 == INTEGER_CST)
15448 {
15449 tree size
15450 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
15451 size = size_binop (PLUS_EXPR, size, size_one_node);
15452 if (TREE_CODE (low_bound) == INTEGER_CST)
15453 {
15454 if (tree_int_cst_lt (size, low_bound))
15455 {
15456 error_at (OMP_CLAUSE_LOCATION (c),
15457 "low bound %qE above array section size "
15458 "in %qs clause", low_bound,
15459 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15460 return error_mark_node;
15461 }
15462 if (tree_int_cst_equal (size, low_bound))
15463 {
15464 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15465 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15466 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15467 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15468 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15469 {
15470 error_at (OMP_CLAUSE_LOCATION (c),
15471 "zero length array section in %qs clause",
15472 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15473 return error_mark_node;
15474 }
15475 maybe_zero_len = true;
15476 }
15477 else if (length == NULL_TREE
15478 && first_non_one == types.length ()
15479 && tree_int_cst_equal
15480 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
15481 low_bound))
15482 first_non_one++;
15483 }
15484 else if (length == NULL_TREE)
15485 {
15486 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15487 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15488 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
15489 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
15490 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
15491 maybe_zero_len = true;
15492 if (first_non_one == types.length ())
15493 first_non_one++;
15494 }
15495 if (length && TREE_CODE (length) == INTEGER_CST)
15496 {
15497 if (tree_int_cst_lt (size, length))
15498 {
15499 error_at (OMP_CLAUSE_LOCATION (c),
15500 "length %qE above array section size "
15501 "in %qs clause", length,
15502 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15503 return error_mark_node;
15504 }
15505 if (TREE_CODE (low_bound) == INTEGER_CST)
15506 {
15507 tree lbpluslen
15508 = size_binop (PLUS_EXPR,
15509 fold_convert (sizetype, low_bound),
15510 fold_convert (sizetype, length));
15511 if (TREE_CODE (lbpluslen) == INTEGER_CST
15512 && tree_int_cst_lt (size, lbpluslen))
15513 {
15514 error_at (OMP_CLAUSE_LOCATION (c),
15515 "high bound %qE above array section size "
15516 "in %qs clause", lbpluslen,
15517 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15518 return error_mark_node;
15519 }
15520 }
15521 }
15522 }
15523 else if (length == NULL_TREE)
15524 {
15525 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15526 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15527 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
15528 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
15529 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
15530 maybe_zero_len = true;
15531 if (first_non_one == types.length ())
15532 first_non_one++;
15533 }
15534
15535 /* For [lb:] we will need to evaluate lb more than once. */
15536 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
15537 {
15538 tree lb = save_expr (low_bound);
15539 if (lb != low_bound)
15540 {
15541 TREE_OPERAND (t, 1) = lb;
15542 low_bound = lb;
15543 }
15544 }
15545 }
15546 else if (TREE_CODE (type) == POINTER_TYPE)
15547 {
15548 if (length == NULL_TREE)
15549 {
15550 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
15551 error_at (OMP_CLAUSE_LOCATION (c),
15552 "for array function parameter length expression "
15553 "must be specified");
15554 else
15555 error_at (OMP_CLAUSE_LOCATION (c),
15556 "for pointer type length expression must be specified");
15557 return error_mark_node;
15558 }
15559 if (length != NULL_TREE
15560 && TREE_CODE (length) == INTEGER_CST
15561 && tree_int_cst_sgn (length) == -1)
15562 {
15563 error_at (OMP_CLAUSE_LOCATION (c),
15564 "negative length in array section in %qs clause",
15565 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15566 return error_mark_node;
15567 }
15568 /* If there is a pointer type anywhere but in the very first
15569 array-section-subscript, the array section could be non-contiguous. */
15570 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15571 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15572 && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
15573 {
15574 /* If any prior dimension has a non-one length, then deem this
15575 array section as non-contiguous. */
15576 for (tree d = TREE_OPERAND (t, 0);
15577 TREE_CODE (d) == OMP_ARRAY_SECTION;
15578 d = TREE_OPERAND (d, 0))
15579 {
15580 tree d_length = TREE_OPERAND (d, 2);
15581 if (d_length == NULL_TREE || !integer_onep (d_length))
15582 {
15583 error_at (OMP_CLAUSE_LOCATION (c),
15584 "array section is not contiguous in %qs clause",
15585 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15586 return error_mark_node;
15587 }
15588 }
15589 }
15590 }
15591 else
15592 {
15593 error_at (OMP_CLAUSE_LOCATION (c),
15594 "%qE does not have pointer or array type", ret);
15595 return error_mark_node;
15596 }
15597 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
15598 types.safe_push (TREE_TYPE (ret));
15599 /* We will need to evaluate lb more than once. */
15600 tree lb = save_expr (low_bound);
15601 if (lb != low_bound)
15602 {
15603 TREE_OPERAND (t, 1) = lb;
15604 low_bound = lb;
15605 }
15606 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
15607 return ret;
15608 }
15609
15610 /* Handle array sections for clause C. */
15611
15612 static bool
15613 handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
15614 {
15615 bool maybe_zero_len = false;
15616 unsigned int first_non_one = 0;
15617 auto_vec<tree, 10> types;
15618 tree *tp = &OMP_CLAUSE_DECL (c);
15619 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15620 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
15621 && TREE_CODE (*tp) == TREE_LIST
15622 && TREE_PURPOSE (*tp)
15623 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
15624 tp = &TREE_VALUE (*tp);
15625 tree first = handle_omp_array_sections_1 (c, *tp, types,
15626 maybe_zero_len, first_non_one,
15627 ort);
15628 if (first == error_mark_node)
15629 return true;
15630 if (first == NULL_TREE)
15631 return false;
15632 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15633 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
15634 {
15635 tree t = *tp;
15636 tree tem = NULL_TREE;
15637 /* Need to evaluate side effects in the length expressions
15638 if any. */
15639 while (TREE_CODE (t) == TREE_LIST)
15640 {
15641 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
15642 {
15643 if (tem == NULL_TREE)
15644 tem = TREE_VALUE (t);
15645 else
15646 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
15647 TREE_VALUE (t), tem);
15648 }
15649 t = TREE_CHAIN (t);
15650 }
15651 if (tem)
15652 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
15653 first = c_fully_fold (first, false, NULL, true);
15654 *tp = first;
15655 }
15656 else
15657 {
15658 unsigned int num = types.length (), i;
15659 tree t, side_effects = NULL_TREE, size = NULL_TREE;
15660 tree condition = NULL_TREE;
15661
15662 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
15663 maybe_zero_len = true;
15664
15665 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
15666 t = TREE_OPERAND (t, 0))
15667 {
15668 tree low_bound = TREE_OPERAND (t, 1);
15669 tree length = TREE_OPERAND (t, 2);
15670
15671 i--;
15672 if (low_bound
15673 && TREE_CODE (low_bound) == INTEGER_CST
15674 && TYPE_PRECISION (TREE_TYPE (low_bound))
15675 > TYPE_PRECISION (sizetype))
15676 low_bound = fold_convert (sizetype, low_bound);
15677 if (length
15678 && TREE_CODE (length) == INTEGER_CST
15679 && TYPE_PRECISION (TREE_TYPE (length))
15680 > TYPE_PRECISION (sizetype))
15681 length = fold_convert (sizetype, length);
15682 if (low_bound == NULL_TREE)
15683 low_bound = integer_zero_node;
15684 if (!maybe_zero_len && i > first_non_one)
15685 {
15686 if (integer_nonzerop (low_bound))
15687 goto do_warn_noncontiguous;
15688 if (length != NULL_TREE
15689 && TREE_CODE (length) == INTEGER_CST
15690 && TYPE_DOMAIN (types[i])
15691 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
15692 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
15693 == INTEGER_CST)
15694 {
15695 tree size;
15696 size = size_binop (PLUS_EXPR,
15697 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
15698 size_one_node);
15699 if (!tree_int_cst_equal (length, size))
15700 {
15701 do_warn_noncontiguous:
15702 error_at (OMP_CLAUSE_LOCATION (c),
15703 "array section is not contiguous in %qs "
15704 "clause",
15705 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15706 return true;
15707 }
15708 }
15709 if (length != NULL_TREE
15710 && TREE_SIDE_EFFECTS (length))
15711 {
15712 if (side_effects == NULL_TREE)
15713 side_effects = length;
15714 else
15715 side_effects = build2 (COMPOUND_EXPR,
15716 TREE_TYPE (side_effects),
15717 length, side_effects);
15718 }
15719 }
15720 else
15721 {
15722 tree l;
15723
15724 if (i > first_non_one
15725 && ((length && integer_nonzerop (length))
15726 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15727 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15728 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
15729 continue;
15730 if (length)
15731 l = fold_convert (sizetype, length);
15732 else
15733 {
15734 l = size_binop (PLUS_EXPR,
15735 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
15736 size_one_node);
15737 l = size_binop (MINUS_EXPR, l,
15738 fold_convert (sizetype, low_bound));
15739 }
15740 if (i > first_non_one)
15741 {
15742 l = fold_build2 (NE_EXPR, boolean_type_node, l,
15743 size_zero_node);
15744 if (condition == NULL_TREE)
15745 condition = l;
15746 else
15747 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
15748 l, condition);
15749 }
15750 else if (size == NULL_TREE)
15751 {
15752 size = size_in_bytes (TREE_TYPE (types[i]));
15753 tree eltype = TREE_TYPE (types[num - 1]);
15754 while (TREE_CODE (eltype) == ARRAY_TYPE)
15755 eltype = TREE_TYPE (eltype);
15756 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15757 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15758 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15759 {
15760 if (integer_zerop (size)
15761 || integer_zerop (size_in_bytes (eltype)))
15762 {
15763 error_at (OMP_CLAUSE_LOCATION (c),
15764 "zero length array section in %qs clause",
15765 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15766 return error_mark_node;
15767 }
15768 size = size_binop (EXACT_DIV_EXPR, size,
15769 size_in_bytes (eltype));
15770 }
15771 size = size_binop (MULT_EXPR, size, l);
15772 if (condition)
15773 size = fold_build3 (COND_EXPR, sizetype, condition,
15774 size, size_zero_node);
15775 }
15776 else
15777 size = size_binop (MULT_EXPR, size, l);
15778 }
15779 }
15780 if (side_effects)
15781 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
15782 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15783 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15784 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15785 {
15786 size = size_binop (MINUS_EXPR, size, size_one_node);
15787 size = c_fully_fold (size, false, NULL);
15788 size = save_expr (size);
15789 tree index_type = build_index_type (size);
15790 tree eltype = TREE_TYPE (first);
15791 while (TREE_CODE (eltype) == ARRAY_TYPE)
15792 eltype = TREE_TYPE (eltype);
15793 tree type = c_build_array_type (eltype, index_type);
15794 tree ptype = c_build_pointer_type (eltype);
15795 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
15796 t = build_fold_addr_expr (t);
15797 tree t2 = build_fold_addr_expr (first);
15798 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
15799 ptrdiff_type_node, t2);
15800 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
15801 ptrdiff_type_node, t2,
15802 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
15803 ptrdiff_type_node, t));
15804 t2 = c_fully_fold (t2, false, NULL);
15805 if (tree_fits_shwi_p (t2))
15806 t = build2 (MEM_REF, type, t,
15807 build_int_cst (ptype, tree_to_shwi (t2)));
15808 else
15809 {
15810 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
15811 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
15812 TREE_TYPE (t), t, t2);
15813 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
15814 }
15815 OMP_CLAUSE_DECL (c) = t;
15816 return false;
15817 }
15818 first = c_fully_fold (first, false, NULL);
15819 OMP_CLAUSE_DECL (c) = first;
15820 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
15821 return false;
15822 /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */
15823 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15824 || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
15825 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
15826 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
15827 {
15828 if (size)
15829 size = c_fully_fold (size, false, NULL);
15830 OMP_CLAUSE_SIZE (c) = size;
15831 }
15832
15833 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15834 return false;
15835
15836 auto_vec<omp_addr_token *, 10> addr_tokens;
15837
15838 if (!omp_parse_expr (addr_tokens, first))
15839 return true;
15840
15841 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15842
15843 tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
15844 if (nc != error_mark_node)
15845 {
15846 using namespace omp_addr_tokenizer;
15847
15848 if (ai.maybe_zero_length_array_section (c))
15849 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
15850
15851 /* !!! If we're accessing a base decl via chained access
15852 methods (e.g. multiple indirections), duplicate clause
15853 detection won't work properly. Skip it in that case. */
15854 if ((addr_tokens[0]->type == STRUCTURE_BASE
15855 || addr_tokens[0]->type == ARRAY_BASE)
15856 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
15857 && addr_tokens[1]->type == ACCESS_METHOD
15858 && omp_access_chain_p (addr_tokens, 1))
15859 c = nc;
15860
15861 return false;
15862 }
15863 }
15864 return false;
15865 }
15866
15867 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
15868 an inline call. But, remap
15869 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
15870 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
15871
15872 static tree
15873 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
15874 tree decl, tree placeholder)
15875 {
15876 copy_body_data id;
15877 hash_map<tree, tree> decl_map;
15878
15879 decl_map.put (omp_decl1, placeholder);
15880 decl_map.put (omp_decl2, decl);
15881 memset (&id, 0, sizeof (id));
15882 id.src_fn = DECL_CONTEXT (omp_decl1);
15883 id.dst_fn = current_function_decl;
15884 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
15885 id.decl_map = &decl_map;
15886
15887 id.copy_decl = copy_decl_no_change;
15888 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
15889 id.transform_new_cfg = true;
15890 id.transform_return_to_modify = false;
15891 id.eh_lp_nr = 0;
15892 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
15893 return stmt;
15894 }
15895
15896 /* Helper function of c_finish_omp_clauses, called via walk_tree.
15897 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
15898
15899 static tree
15900 c_find_omp_placeholder_r (tree *tp, int *, void *data)
15901 {
15902 if (*tp == (tree) data)
15903 return *tp;
15904 return NULL_TREE;
15905 }
15906
15907 /* Similarly, but also walk aggregate fields. */
15908
15909 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
15910
15911 static tree
15912 c_find_omp_var_r (tree *tp, int *, void *data)
15913 {
15914 if (*tp == ((struct c_find_omp_var_s *) data)->var)
15915 return *tp;
15916 if (RECORD_OR_UNION_TYPE_P (*tp))
15917 {
15918 tree field;
15919 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
15920
15921 for (field = TYPE_FIELDS (*tp); field;
15922 field = DECL_CHAIN (field))
15923 if (TREE_CODE (field) == FIELD_DECL)
15924 {
15925 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
15926 c_find_omp_var_r, data, pset);
15927 if (ret)
15928 return ret;
15929 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
15930 if (ret)
15931 return ret;
15932 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
15933 pset);
15934 if (ret)
15935 return ret;
15936 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
15937 if (ret)
15938 return ret;
15939 }
15940 }
15941 else if (INTEGRAL_TYPE_P (*tp))
15942 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
15943 ((struct c_find_omp_var_s *) data)->pset);
15944 return NULL_TREE;
15945 }
15946
15947 /* Finish OpenMP iterators ITER. Return true if they are errorneous
15948 and clauses containing them should be removed. */
15949
15950 static bool
15951 c_omp_finish_iterators (tree iter)
15952 {
15953 bool ret = false;
15954 for (tree it = iter; it; it = TREE_CHAIN (it))
15955 {
15956 tree var = TREE_VEC_ELT (it, 0);
15957 tree begin = TREE_VEC_ELT (it, 1);
15958 tree end = TREE_VEC_ELT (it, 2);
15959 tree step = TREE_VEC_ELT (it, 3);
15960 tree orig_step;
15961 tree type = TREE_TYPE (var);
15962 location_t loc = DECL_SOURCE_LOCATION (var);
15963 if (type == error_mark_node)
15964 {
15965 ret = true;
15966 continue;
15967 }
15968 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15969 {
15970 error_at (loc, "iterator %qD has neither integral nor pointer type",
15971 var);
15972 ret = true;
15973 continue;
15974 }
15975 else if (TYPE_ATOMIC (type))
15976 {
15977 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
15978 ret = true;
15979 continue;
15980 }
15981 else if (TYPE_READONLY (type))
15982 {
15983 error_at (loc, "iterator %qD has const qualified type", var);
15984 ret = true;
15985 continue;
15986 }
15987 else if (step == error_mark_node
15988 || TREE_TYPE (step) == error_mark_node)
15989 {
15990 ret = true;
15991 continue;
15992 }
15993 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
15994 {
15995 error_at (EXPR_LOC_OR_LOC (step, loc),
15996 "iterator step with non-integral type");
15997 ret = true;
15998 continue;
15999 }
16000 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
16001 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
16002 orig_step = save_expr (c_fully_fold (step, false, NULL));
16003 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
16004 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
16005 if (POINTER_TYPE_P (type))
16006 {
16007 begin = save_expr (begin);
16008 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
16009 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
16010 fold_convert (sizetype, step),
16011 fold_convert (sizetype, begin));
16012 step = fold_convert (ssizetype, step);
16013 }
16014 if (integer_zerop (step))
16015 {
16016 error_at (loc, "iterator %qD has zero step", var);
16017 ret = true;
16018 continue;
16019 }
16020
16021 if (begin == error_mark_node
16022 || end == error_mark_node
16023 || step == error_mark_node
16024 || orig_step == error_mark_node)
16025 {
16026 ret = true;
16027 continue;
16028 }
16029 hash_set<tree> pset;
16030 tree it2;
16031 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
16032 {
16033 tree var2 = TREE_VEC_ELT (it2, 0);
16034 tree begin2 = TREE_VEC_ELT (it2, 1);
16035 tree end2 = TREE_VEC_ELT (it2, 2);
16036 tree step2 = TREE_VEC_ELT (it2, 3);
16037 tree type2 = TREE_TYPE (var2);
16038 location_t loc2 = DECL_SOURCE_LOCATION (var2);
16039 struct c_find_omp_var_s data = { var, &pset };
16040 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
16041 {
16042 error_at (loc2,
16043 "type of iterator %qD refers to outer iterator %qD",
16044 var2, var);
16045 break;
16046 }
16047 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
16048 {
16049 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
16050 "begin expression refers to outer iterator %qD", var);
16051 break;
16052 }
16053 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
16054 {
16055 error_at (EXPR_LOC_OR_LOC (end2, loc2),
16056 "end expression refers to outer iterator %qD", var);
16057 break;
16058 }
16059 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
16060 {
16061 error_at (EXPR_LOC_OR_LOC (step2, loc2),
16062 "step expression refers to outer iterator %qD", var);
16063 break;
16064 }
16065 }
16066 if (it2)
16067 {
16068 ret = true;
16069 continue;
16070 }
16071 TREE_VEC_ELT (it, 1) = begin;
16072 TREE_VEC_ELT (it, 2) = end;
16073 TREE_VEC_ELT (it, 3) = step;
16074 TREE_VEC_ELT (it, 4) = orig_step;
16075 }
16076 return ret;
16077 }
16078
16079 /* Ensure that pointers are used in OpenACC attach and detach clauses.
16080 Return true if an error has been detected. */
16081
16082 static bool
16083 c_oacc_check_attachments (tree c)
16084 {
16085 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16086 return false;
16087
16088 /* OpenACC attach / detach clauses must be pointers. */
16089 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16090 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
16091 {
16092 tree t = OMP_CLAUSE_DECL (c);
16093
16094 while (TREE_CODE (t) == OMP_ARRAY_SECTION)
16095 t = TREE_OPERAND (t, 0);
16096
16097 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
16098 {
16099 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
16100 user_omp_clause_code_name (c, true));
16101 return true;
16102 }
16103 }
16104
16105 return false;
16106 }
16107
16108 /* For all elements of CLAUSES, validate them against their constraints.
16109 Remove any elements from the list that are invalid. */
16110
16111 tree
16112 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
16113 {
16114 bitmap_head generic_head, firstprivate_head, lastprivate_head;
16115 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
16116 bitmap_head oacc_reduction_head, is_on_device_head;
16117 tree c, t, type, *pc;
16118 tree simdlen = NULL_TREE, safelen = NULL_TREE;
16119 bool branch_seen = false;
16120 bool copyprivate_seen = false;
16121 bool mergeable_seen = false;
16122 tree *detach_seen = NULL;
16123 bool linear_variable_step_check = false;
16124 tree *nowait_clause = NULL;
16125 tree *grainsize_seen = NULL;
16126 bool num_tasks_seen = false;
16127 tree ordered_clause = NULL_TREE;
16128 tree schedule_clause = NULL_TREE;
16129 bool oacc_async = false;
16130 tree last_iterators = NULL_TREE;
16131 bool last_iterators_remove = false;
16132 tree *nogroup_seen = NULL;
16133 tree *order_clause = NULL;
16134 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
16135 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
16136 int reduction_seen = 0;
16137 bool allocate_seen = false;
16138 bool implicit_moved = false;
16139 bool target_in_reduction_seen = false;
16140 tree *full_seen = NULL;
16141 bool partial_seen = false;
16142 bool openacc = (ort & C_ORT_ACC) != 0;
16143 bool init_seen = false;
16144 bool init_use_destroy_seen = false;
16145 tree init_no_targetsync_clause = NULL_TREE;
16146 tree depend_clause = NULL_TREE;
16147
16148 bitmap_obstack_initialize (NULL);
16149 bitmap_initialize (&generic_head, &bitmap_default_obstack);
16150 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
16151 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
16152 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
16153 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
16154 bitmap_initialize (&map_head, &bitmap_default_obstack);
16155 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
16156 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
16157 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
16158 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
16159 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
16160 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
16161
16162 if (openacc)
16163 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16164 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
16165 {
16166 oacc_async = true;
16167 break;
16168 }
16169
16170 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
16171
16172 for (pc = &clauses, c = clauses; c ; c = *pc)
16173 {
16174 bool remove = false;
16175 bool need_complete = false;
16176 bool need_implicitly_determined = false;
16177
16178 /* We've reached the end of a list of expanded nodes. Reset the group
16179 start pointer. */
16180 if (c == grp_sentinel)
16181 grp_start_p = NULL;
16182
16183 switch (OMP_CLAUSE_CODE (c))
16184 {
16185 case OMP_CLAUSE_SHARED:
16186 need_implicitly_determined = true;
16187 goto check_dup_generic;
16188
16189 case OMP_CLAUSE_PRIVATE:
16190 need_complete = true;
16191 need_implicitly_determined = true;
16192 goto check_dup_generic;
16193
16194 case OMP_CLAUSE_REDUCTION:
16195 if (reduction_seen == 0)
16196 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
16197 else if (reduction_seen != -2
16198 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
16199 ? -1 : 1))
16200 {
16201 error_at (OMP_CLAUSE_LOCATION (c),
16202 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
16203 "on the same construct");
16204 reduction_seen = -2;
16205 }
16206 /* FALLTHRU */
16207 case OMP_CLAUSE_IN_REDUCTION:
16208 case OMP_CLAUSE_TASK_REDUCTION:
16209 need_implicitly_determined = true;
16210 t = OMP_CLAUSE_DECL (c);
16211 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
16212 {
16213 if (handle_omp_array_sections (c, ort))
16214 {
16215 remove = true;
16216 break;
16217 }
16218
16219 t = OMP_CLAUSE_DECL (c);
16220 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16221 && OMP_CLAUSE_REDUCTION_INSCAN (c))
16222 {
16223 error_at (OMP_CLAUSE_LOCATION (c),
16224 "%<inscan%> %<reduction%> clause with array "
16225 "section");
16226 remove = true;
16227 break;
16228 }
16229 }
16230 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
16231 if (t == error_mark_node)
16232 {
16233 remove = true;
16234 break;
16235 }
16236 if (oacc_async)
16237 c_mark_addressable (t);
16238 type = TREE_TYPE (t);
16239 if (TREE_CODE (t) == MEM_REF)
16240 type = TREE_TYPE (type);
16241 if (TREE_CODE (type) == ARRAY_TYPE)
16242 {
16243 tree oatype = type;
16244 gcc_assert (TREE_CODE (t) != MEM_REF);
16245 while (TREE_CODE (type) == ARRAY_TYPE)
16246 type = TREE_TYPE (type);
16247 if (integer_zerop (TYPE_SIZE_UNIT (type)))
16248 {
16249 error_at (OMP_CLAUSE_LOCATION (c),
16250 "%qD in %<reduction%> clause is a zero size array",
16251 t);
16252 remove = true;
16253 break;
16254 }
16255 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
16256 TYPE_SIZE_UNIT (type));
16257 if (integer_zerop (size))
16258 {
16259 error_at (OMP_CLAUSE_LOCATION (c),
16260 "%qD in %<reduction%> clause is a zero size array",
16261 t);
16262 remove = true;
16263 break;
16264 }
16265 size = size_binop (MINUS_EXPR, size, size_one_node);
16266 size = save_expr (size);
16267 tree index_type = build_index_type (size);
16268 tree atype = c_build_array_type (TYPE_MAIN_VARIANT (type),
16269 index_type);
16270 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
16271 tree ptype = c_build_pointer_type (type);
16272 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16273 t = build_fold_addr_expr (t);
16274 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
16275 OMP_CLAUSE_DECL (c) = t;
16276 }
16277 if (TYPE_ATOMIC (type))
16278 {
16279 error_at (OMP_CLAUSE_LOCATION (c),
16280 "%<_Atomic%> %qE in %<reduction%> clause", t);
16281 remove = true;
16282 break;
16283 }
16284 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16285 || OMP_CLAUSE_REDUCTION_TASK (c))
16286 {
16287 /* Disallow zero sized or potentially zero sized task
16288 reductions. */
16289 if (integer_zerop (TYPE_SIZE_UNIT (type)))
16290 {
16291 error_at (OMP_CLAUSE_LOCATION (c),
16292 "zero sized type %qT in %qs clause", type,
16293 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16294 remove = true;
16295 break;
16296 }
16297 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
16298 {
16299 error_at (OMP_CLAUSE_LOCATION (c),
16300 "variable sized type %qT in %qs clause", type,
16301 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16302 remove = true;
16303 break;
16304 }
16305 }
16306 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
16307 && (FLOAT_TYPE_P (type)
16308 || TREE_CODE (type) == COMPLEX_TYPE))
16309 {
16310 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
16311 const char *r_name = NULL;
16312
16313 switch (r_code)
16314 {
16315 case PLUS_EXPR:
16316 case MULT_EXPR:
16317 case MINUS_EXPR:
16318 case TRUTH_ANDIF_EXPR:
16319 case TRUTH_ORIF_EXPR:
16320 break;
16321 case MIN_EXPR:
16322 if (TREE_CODE (type) == COMPLEX_TYPE)
16323 r_name = "min";
16324 break;
16325 case MAX_EXPR:
16326 if (TREE_CODE (type) == COMPLEX_TYPE)
16327 r_name = "max";
16328 break;
16329 case BIT_AND_EXPR:
16330 r_name = "&";
16331 break;
16332 case BIT_XOR_EXPR:
16333 r_name = "^";
16334 break;
16335 case BIT_IOR_EXPR:
16336 r_name = "|";
16337 break;
16338 default:
16339 gcc_unreachable ();
16340 }
16341 if (r_name)
16342 {
16343 error_at (OMP_CLAUSE_LOCATION (c),
16344 "%qE has invalid type for %<reduction(%s)%>",
16345 t, r_name);
16346 remove = true;
16347 break;
16348 }
16349 }
16350 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
16351 {
16352 error_at (OMP_CLAUSE_LOCATION (c),
16353 "user defined reduction not found for %qE", t);
16354 remove = true;
16355 break;
16356 }
16357 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
16358 {
16359 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
16360 type = TYPE_MAIN_VARIANT (type);
16361 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16362 VAR_DECL, NULL_TREE, type);
16363 tree decl_placeholder = NULL_TREE;
16364 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
16365 DECL_ARTIFICIAL (placeholder) = 1;
16366 DECL_IGNORED_P (placeholder) = 1;
16367 if (TREE_CODE (t) == MEM_REF)
16368 {
16369 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16370 VAR_DECL, NULL_TREE, type);
16371 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
16372 DECL_ARTIFICIAL (decl_placeholder) = 1;
16373 DECL_IGNORED_P (decl_placeholder) = 1;
16374 }
16375 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
16376 c_mark_addressable (placeholder);
16377 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
16378 c_mark_addressable (decl_placeholder ? decl_placeholder
16379 : OMP_CLAUSE_DECL (c));
16380 OMP_CLAUSE_REDUCTION_MERGE (c)
16381 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
16382 TREE_VEC_ELT (list, 0),
16383 TREE_VEC_ELT (list, 1),
16384 decl_placeholder ? decl_placeholder
16385 : OMP_CLAUSE_DECL (c), placeholder);
16386 OMP_CLAUSE_REDUCTION_MERGE (c)
16387 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16388 void_type_node, NULL_TREE,
16389 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
16390 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
16391 if (TREE_VEC_LENGTH (list) == 6)
16392 {
16393 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
16394 c_mark_addressable (decl_placeholder ? decl_placeholder
16395 : OMP_CLAUSE_DECL (c));
16396 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
16397 c_mark_addressable (placeholder);
16398 tree init = TREE_VEC_ELT (list, 5);
16399 if (init == error_mark_node)
16400 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
16401 OMP_CLAUSE_REDUCTION_INIT (c)
16402 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
16403 TREE_VEC_ELT (list, 3),
16404 decl_placeholder ? decl_placeholder
16405 : OMP_CLAUSE_DECL (c), placeholder);
16406 if (TREE_VEC_ELT (list, 5) == error_mark_node)
16407 {
16408 tree v = decl_placeholder ? decl_placeholder : t;
16409 OMP_CLAUSE_REDUCTION_INIT (c)
16410 = build2 (INIT_EXPR, TREE_TYPE (v), v,
16411 OMP_CLAUSE_REDUCTION_INIT (c));
16412 }
16413 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
16414 c_find_omp_placeholder_r,
16415 placeholder, NULL))
16416 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
16417 }
16418 else
16419 {
16420 tree init;
16421 tree v = decl_placeholder ? decl_placeholder : t;
16422 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
16423 init = build_constructor (TREE_TYPE (v), NULL);
16424 else
16425 init = fold_convert (TREE_TYPE (v), integer_zero_node);
16426 OMP_CLAUSE_REDUCTION_INIT (c)
16427 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
16428 }
16429 OMP_CLAUSE_REDUCTION_INIT (c)
16430 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16431 void_type_node, NULL_TREE,
16432 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
16433 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
16434 }
16435 if (TREE_CODE (t) == MEM_REF)
16436 {
16437 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
16438 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
16439 != INTEGER_CST)
16440 {
16441 sorry ("variable length element type in array "
16442 "%<reduction%> clause");
16443 remove = true;
16444 break;
16445 }
16446 t = TREE_OPERAND (t, 0);
16447 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16448 t = TREE_OPERAND (t, 0);
16449 if (TREE_CODE (t) == ADDR_EXPR)
16450 t = TREE_OPERAND (t, 0);
16451 }
16452 goto check_dup_generic_t;
16453
16454 case OMP_CLAUSE_COPYPRIVATE:
16455 copyprivate_seen = true;
16456 if (nowait_clause)
16457 {
16458 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
16459 "%<nowait%> clause must not be used together "
16460 "with %<copyprivate%> clause");
16461 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
16462 nowait_clause = NULL;
16463 }
16464 goto check_dup_generic;
16465
16466 case OMP_CLAUSE_COPYIN:
16467 t = OMP_CLAUSE_DECL (c);
16468 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
16469 {
16470 error_at (OMP_CLAUSE_LOCATION (c),
16471 "%qE must be %<threadprivate%> for %<copyin%>", t);
16472 remove = true;
16473 break;
16474 }
16475 goto check_dup_generic;
16476
16477 case OMP_CLAUSE_LINEAR:
16478 if (ort != C_ORT_OMP_DECLARE_SIMD)
16479 need_implicitly_determined = true;
16480 t = OMP_CLAUSE_DECL (c);
16481 if (ort != C_ORT_OMP_DECLARE_SIMD
16482 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
16483 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
16484 {
16485 error_at (OMP_CLAUSE_LOCATION (c),
16486 "modifier should not be specified in %<linear%> "
16487 "clause on %<simd%> or %<for%> constructs when not "
16488 "using OpenMP 5.2 modifiers");
16489 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
16490 }
16491 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
16492 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
16493 {
16494 error_at (OMP_CLAUSE_LOCATION (c),
16495 "linear clause applied to non-integral non-pointer "
16496 "variable with type %qT", TREE_TYPE (t));
16497 remove = true;
16498 break;
16499 }
16500 if (TYPE_ATOMIC (TREE_TYPE (t)))
16501 {
16502 error_at (OMP_CLAUSE_LOCATION (c),
16503 "%<_Atomic%> %qD in %<linear%> clause", t);
16504 remove = true;
16505 break;
16506 }
16507 if (ort == C_ORT_OMP_DECLARE_SIMD)
16508 {
16509 tree s = OMP_CLAUSE_LINEAR_STEP (c);
16510 if (TREE_CODE (s) == PARM_DECL)
16511 {
16512 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
16513 /* map_head bitmap is used as uniform_head if
16514 declare_simd. */
16515 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
16516 linear_variable_step_check = true;
16517 goto check_dup_generic;
16518 }
16519 if (TREE_CODE (s) != INTEGER_CST)
16520 {
16521 error_at (OMP_CLAUSE_LOCATION (c),
16522 "%<linear%> clause step %qE is neither constant "
16523 "nor a parameter", s);
16524 remove = true;
16525 break;
16526 }
16527 }
16528 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
16529 {
16530 tree s = OMP_CLAUSE_LINEAR_STEP (c);
16531 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
16532 OMP_CLAUSE_DECL (c), s);
16533 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
16534 sizetype, fold_convert (sizetype, s),
16535 fold_convert
16536 (sizetype, OMP_CLAUSE_DECL (c)));
16537 if (s == error_mark_node)
16538 s = size_one_node;
16539 OMP_CLAUSE_LINEAR_STEP (c) = s;
16540 }
16541 else
16542 OMP_CLAUSE_LINEAR_STEP (c)
16543 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
16544 goto check_dup_generic;
16545
16546 check_dup_generic:
16547 t = OMP_CLAUSE_DECL (c);
16548 check_dup_generic_t:
16549 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16550 {
16551 error_at (OMP_CLAUSE_LOCATION (c),
16552 "%qE is not a variable in clause %qs", t,
16553 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16554 remove = true;
16555 }
16556 else if ((openacc && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
16557 || (ort == C_ORT_OMP
16558 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
16559 || (OMP_CLAUSE_CODE (c)
16560 == OMP_CLAUSE_USE_DEVICE_ADDR)))
16561 || (ort == C_ORT_OMP_TARGET
16562 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
16563 {
16564 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16565 && (bitmap_bit_p (&generic_head, DECL_UID (t))
16566 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
16567 {
16568 error_at (OMP_CLAUSE_LOCATION (c),
16569 "%qD appears more than once in data-sharing "
16570 "clauses", t);
16571 remove = true;
16572 break;
16573 }
16574 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
16575 target_in_reduction_seen = true;
16576 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
16577 {
16578 error_at (OMP_CLAUSE_LOCATION (c),
16579 openacc
16580 ? "%qD appears more than once in reduction clauses"
16581 : "%qD appears more than once in data clauses",
16582 t);
16583 remove = true;
16584 }
16585 else
16586 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
16587 }
16588 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
16589 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
16590 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
16591 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
16592 {
16593 error_at (OMP_CLAUSE_LOCATION (c),
16594 "%qE appears more than once in data clauses", t);
16595 remove = true;
16596 }
16597 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
16598 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
16599 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
16600 && bitmap_bit_p (&map_head, DECL_UID (t)))
16601 {
16602 if (openacc)
16603 error_at (OMP_CLAUSE_LOCATION (c),
16604 "%qD appears more than once in data clauses", t);
16605 else
16606 error_at (OMP_CLAUSE_LOCATION (c),
16607 "%qD appears both in data and map clauses", t);
16608 remove = true;
16609 }
16610 else
16611 bitmap_set_bit (&generic_head, DECL_UID (t));
16612 break;
16613
16614 case OMP_CLAUSE_FIRSTPRIVATE:
16615 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
16616 {
16617 move_implicit:
16618 implicit_moved = true;
16619 /* Move firstprivate and map clauses with
16620 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
16621 clauses chain. */
16622 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
16623 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
16624 while (*pc1)
16625 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
16626 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
16627 {
16628 *pc3 = *pc1;
16629 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
16630 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
16631 }
16632 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
16633 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
16634 {
16635 *pc2 = *pc1;
16636 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
16637 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
16638 }
16639 else
16640 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
16641 *pc3 = NULL;
16642 *pc2 = cl2;
16643 *pc1 = cl1;
16644 continue;
16645 }
16646 t = OMP_CLAUSE_DECL (c);
16647 need_complete = true;
16648 need_implicitly_determined = true;
16649 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16650 {
16651 error_at (OMP_CLAUSE_LOCATION (c),
16652 "%qE is not a variable in clause %<firstprivate%>", t);
16653 remove = true;
16654 }
16655 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
16656 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
16657 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
16658 remove = true;
16659 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
16660 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
16661 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
16662 {
16663 error_at (OMP_CLAUSE_LOCATION (c),
16664 "%qE appears more than once in data clauses", t);
16665 remove = true;
16666 }
16667 else if (bitmap_bit_p (&map_head, DECL_UID (t))
16668 || bitmap_bit_p (&map_field_head, DECL_UID (t)))
16669 {
16670 if (openacc)
16671 error_at (OMP_CLAUSE_LOCATION (c),
16672 "%qD appears more than once in data clauses", t);
16673 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
16674 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
16675 /* Silently drop the clause. */;
16676 else
16677 error_at (OMP_CLAUSE_LOCATION (c),
16678 "%qD appears both in data and map clauses", t);
16679 remove = true;
16680 }
16681 else
16682 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
16683 break;
16684
16685 case OMP_CLAUSE_LASTPRIVATE:
16686 t = OMP_CLAUSE_DECL (c);
16687 need_complete = true;
16688 need_implicitly_determined = true;
16689 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16690 {
16691 error_at (OMP_CLAUSE_LOCATION (c),
16692 "%qE is not a variable in clause %<lastprivate%>", t);
16693 remove = true;
16694 }
16695 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
16696 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
16697 {
16698 error_at (OMP_CLAUSE_LOCATION (c),
16699 "%qE appears more than once in data clauses", t);
16700 remove = true;
16701 }
16702 else
16703 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
16704 break;
16705
16706 case OMP_CLAUSE_ALIGNED:
16707 t = OMP_CLAUSE_DECL (c);
16708 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16709 {
16710 error_at (OMP_CLAUSE_LOCATION (c),
16711 "%qE is not a variable in %<aligned%> clause", t);
16712 remove = true;
16713 }
16714 else if (!POINTER_TYPE_P (TREE_TYPE (t))
16715 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
16716 {
16717 error_at (OMP_CLAUSE_LOCATION (c),
16718 "%qE in %<aligned%> clause is neither a pointer nor "
16719 "an array", t);
16720 remove = true;
16721 }
16722 else if (TYPE_ATOMIC (TREE_TYPE (t)))
16723 {
16724 error_at (OMP_CLAUSE_LOCATION (c),
16725 "%<_Atomic%> %qD in %<aligned%> clause", t);
16726 remove = true;
16727 break;
16728 }
16729 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
16730 {
16731 error_at (OMP_CLAUSE_LOCATION (c),
16732 "%qE appears more than once in %<aligned%> clauses",
16733 t);
16734 remove = true;
16735 }
16736 else
16737 bitmap_set_bit (&aligned_head, DECL_UID (t));
16738 break;
16739
16740 case OMP_CLAUSE_NONTEMPORAL:
16741 t = OMP_CLAUSE_DECL (c);
16742 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16743 {
16744 error_at (OMP_CLAUSE_LOCATION (c),
16745 "%qE is not a variable in %<nontemporal%> clause", t);
16746 remove = true;
16747 }
16748 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
16749 {
16750 error_at (OMP_CLAUSE_LOCATION (c),
16751 "%qE appears more than once in %<nontemporal%> "
16752 "clauses", t);
16753 remove = true;
16754 }
16755 else
16756 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
16757 break;
16758
16759 case OMP_CLAUSE_ALLOCATE:
16760 t = OMP_CLAUSE_DECL (c);
16761 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16762 {
16763 error_at (OMP_CLAUSE_LOCATION (c),
16764 "%qE is not a variable in %<allocate%> clause", t);
16765 remove = true;
16766 }
16767 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
16768 {
16769 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
16770 "%qE appears more than once in %<allocate%> clauses",
16771 t);
16772 remove = true;
16773 }
16774 else
16775 {
16776 bitmap_set_bit (&aligned_head, DECL_UID (t));
16777 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
16778 allocate_seen = true;
16779 }
16780 break;
16781
16782 case OMP_CLAUSE_DOACROSS:
16783 t = OMP_CLAUSE_DECL (c);
16784 if (t == NULL_TREE)
16785 break;
16786 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
16787 {
16788 gcc_assert (TREE_CODE (t) == TREE_LIST);
16789 for (; t; t = TREE_CHAIN (t))
16790 {
16791 tree decl = TREE_VALUE (t);
16792 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
16793 {
16794 tree offset = TREE_PURPOSE (t);
16795 bool neg = wi::neg_p (wi::to_wide (offset));
16796 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
16797 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
16798 neg ? MINUS_EXPR : PLUS_EXPR,
16799 decl, offset);
16800 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
16801 sizetype,
16802 fold_convert (sizetype, t2),
16803 fold_convert (sizetype, decl));
16804 if (t2 == error_mark_node)
16805 {
16806 remove = true;
16807 break;
16808 }
16809 TREE_PURPOSE (t) = t2;
16810 }
16811 }
16812 break;
16813 }
16814 gcc_unreachable ();
16815 case OMP_CLAUSE_DEPEND:
16816 depend_clause = c;
16817 /* FALLTHRU */
16818 case OMP_CLAUSE_AFFINITY:
16819 t = OMP_CLAUSE_DECL (c);
16820 if (TREE_CODE (t) == TREE_LIST
16821 && TREE_PURPOSE (t)
16822 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
16823 {
16824 if (TREE_PURPOSE (t) != last_iterators)
16825 last_iterators_remove
16826 = c_omp_finish_iterators (TREE_PURPOSE (t));
16827 last_iterators = TREE_PURPOSE (t);
16828 t = TREE_VALUE (t);
16829 if (last_iterators_remove)
16830 t = error_mark_node;
16831 }
16832 else
16833 last_iterators = NULL_TREE;
16834 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
16835 {
16836 if (handle_omp_array_sections (c, ort))
16837 remove = true;
16838 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16839 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
16840 {
16841 error_at (OMP_CLAUSE_LOCATION (c),
16842 "%<depend%> clause with %<depobj%> dependence "
16843 "type on array section");
16844 remove = true;
16845 }
16846 break;
16847 }
16848 if (t == error_mark_node)
16849 remove = true;
16850 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16851 && t == ridpointers[RID_OMP_ALL_MEMORY])
16852 {
16853 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
16854 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
16855 {
16856 error_at (OMP_CLAUSE_LOCATION (c),
16857 "%<omp_all_memory%> used with %<depend%> kind "
16858 "other than %<out%> or %<inout%>");
16859 remove = true;
16860 }
16861 }
16862 else if (!lvalue_p (t))
16863 {
16864 error_at (OMP_CLAUSE_LOCATION (c),
16865 "%qE is not lvalue expression nor array section in "
16866 "%qs clause", t,
16867 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16868 remove = true;
16869 }
16870 else if (TREE_CODE (t) == COMPONENT_REF
16871 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
16872 {
16873 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16874 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
16875 error_at (OMP_CLAUSE_LOCATION (c),
16876 "bit-field %qE in %qs clause", t,
16877 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16878 remove = true;
16879 }
16880 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16881 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
16882 {
16883 if (!c_omp_depend_t_p (TREE_TYPE (t)))
16884 {
16885 error_at (OMP_CLAUSE_LOCATION (c),
16886 "%qE does not have %<omp_depend_t%> type in "
16887 "%<depend%> clause with %<depobj%> dependence "
16888 "type", t);
16889 remove = true;
16890 }
16891 }
16892 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16893 && c_omp_depend_t_p (TREE_TYPE (t)))
16894 {
16895 error_at (OMP_CLAUSE_LOCATION (c),
16896 "%qE should not have %<omp_depend_t%> type in "
16897 "%<depend%> clause with dependence type other than "
16898 "%<depobj%>", t);
16899 remove = true;
16900 }
16901 if (!remove)
16902 {
16903 if (t == ridpointers[RID_OMP_ALL_MEMORY])
16904 t = null_pointer_node;
16905 else
16906 {
16907 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
16908 ADDR_EXPR, t, false);
16909 if (addr == error_mark_node)
16910 {
16911 remove = true;
16912 break;
16913 }
16914 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
16915 RO_UNARY_STAR);
16916 if (t == error_mark_node)
16917 {
16918 remove = true;
16919 break;
16920 }
16921 }
16922 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
16923 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
16924 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
16925 == TREE_VEC))
16926 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
16927 else
16928 OMP_CLAUSE_DECL (c) = t;
16929 }
16930 break;
16931
16932 case OMP_CLAUSE_MAP:
16933 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
16934 goto move_implicit;
16935 /* FALLTHRU */
16936 case OMP_CLAUSE_TO:
16937 case OMP_CLAUSE_FROM:
16938 case OMP_CLAUSE__CACHE_:
16939 {
16940 using namespace omp_addr_tokenizer;
16941 auto_vec<omp_addr_token *, 10> addr_tokens;
16942
16943 t = OMP_CLAUSE_DECL (c);
16944 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
16945 {
16946 grp_start_p = pc;
16947 grp_sentinel = OMP_CLAUSE_CHAIN (c);
16948
16949 if (handle_omp_array_sections (c, ort))
16950 remove = true;
16951 else
16952 {
16953 t = OMP_CLAUSE_DECL (c);
16954 if (!omp_mappable_type (TREE_TYPE (t)))
16955 {
16956 error_at (OMP_CLAUSE_LOCATION (c),
16957 "array section does not have mappable type "
16958 "in %qs clause",
16959 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16960 remove = true;
16961 }
16962 else if (TYPE_ATOMIC (TREE_TYPE (t)))
16963 {
16964 error_at (OMP_CLAUSE_LOCATION (c),
16965 "%<_Atomic%> %qE in %qs clause", t,
16966 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16967 remove = true;
16968 }
16969 while (TREE_CODE (t) == ARRAY_REF)
16970 t = TREE_OPERAND (t, 0);
16971
16972 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
16973
16974 if (!omp_parse_expr (addr_tokens, t))
16975 {
16976 sorry_at (OMP_CLAUSE_LOCATION (c),
16977 "unsupported map expression %qE",
16978 OMP_CLAUSE_DECL (c));
16979 remove = true;
16980 break;
16981 }
16982
16983 /* This check is to determine if this will be the only map
16984 node created for this clause. Otherwise, we'll check
16985 the following FIRSTPRIVATE_POINTER or ATTACH_DETACH
16986 node on the next iteration(s) of the loop. */
16987 if (addr_tokens.length () >= 4
16988 && addr_tokens[0]->type == STRUCTURE_BASE
16989 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
16990 && addr_tokens[1]->type == ACCESS_METHOD
16991 && addr_tokens[2]->type == COMPONENT_SELECTOR
16992 && addr_tokens[3]->type == ACCESS_METHOD
16993 && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
16994 || (addr_tokens[3]->u.access_kind
16995 == ACCESS_INDEXED_ARRAY)))
16996 {
16997 tree rt = addr_tokens[1]->expr;
16998
16999 gcc_assert (DECL_P (rt));
17000
17001 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17002 && OMP_CLAUSE_MAP_IMPLICIT (c)
17003 && (bitmap_bit_p (&map_head, DECL_UID (rt))
17004 || bitmap_bit_p (&map_field_head, DECL_UID (rt))
17005 || bitmap_bit_p (&map_firstprivate_head,
17006 DECL_UID (rt))))
17007 {
17008 remove = true;
17009 break;
17010 }
17011 if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
17012 break;
17013 if (bitmap_bit_p (&map_head, DECL_UID (rt)))
17014 {
17015 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17016 error_at (OMP_CLAUSE_LOCATION (c),
17017 "%qD appears more than once in motion "
17018 "clauses", rt);
17019 else if (openacc)
17020 error_at (OMP_CLAUSE_LOCATION (c),
17021 "%qD appears more than once in data "
17022 "clauses", rt);
17023 else
17024 error_at (OMP_CLAUSE_LOCATION (c),
17025 "%qD appears more than once in map "
17026 "clauses", rt);
17027 remove = true;
17028 }
17029 else
17030 {
17031 bitmap_set_bit (&map_head, DECL_UID (rt));
17032 bitmap_set_bit (&map_field_head, DECL_UID (rt));
17033 }
17034 }
17035 }
17036 if (c_oacc_check_attachments (c))
17037 remove = true;
17038 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17039 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17040 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17041 && !OMP_CLAUSE_SIZE (c))
17042 /* In this case, we have a single array element which is a
17043 pointer, and we already set OMP_CLAUSE_SIZE in
17044 handle_omp_array_sections above. For attach/detach
17045 clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
17046 to zero here. */
17047 OMP_CLAUSE_SIZE (c) = size_zero_node;
17048 break;
17049 }
17050 else if (!omp_parse_expr (addr_tokens, t))
17051 {
17052 sorry_at (OMP_CLAUSE_LOCATION (c),
17053 "unsupported map expression %qE",
17054 OMP_CLAUSE_DECL (c));
17055 remove = true;
17056 break;
17057 }
17058 if (t == error_mark_node)
17059 {
17060 remove = true;
17061 break;
17062 }
17063 /* OpenACC attach / detach clauses must be pointers. */
17064 if (c_oacc_check_attachments (c))
17065 {
17066 remove = true;
17067 break;
17068 }
17069 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17070 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17071 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17072 && !OMP_CLAUSE_SIZE (c))
17073 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
17074 bias) to zero here, so it is not set erroneously to the pointer
17075 size later on in gimplify.cc. */
17076 OMP_CLAUSE_SIZE (c) = size_zero_node;
17077
17078 c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17079
17080 if (!ai.check_clause (c))
17081 {
17082 remove = true;
17083 break;
17084 }
17085
17086 if (!ai.map_supported_p ())
17087 {
17088 sorry_at (OMP_CLAUSE_LOCATION (c),
17089 "unsupported map expression %qE",
17090 OMP_CLAUSE_DECL (c));
17091 remove = true;
17092 break;
17093 }
17094
17095 gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
17096 || addr_tokens[0]->type == STRUCTURE_BASE)
17097 && addr_tokens[1]->type == ACCESS_METHOD);
17098
17099 t = addr_tokens[1]->expr;
17100
17101 if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
17102 goto skip_decl_checks;
17103
17104 /* For OpenMP, we can access a struct "t" and "t.d" on the same
17105 mapping. OpenACC allows multiple fields of the same structure
17106 to be written. */
17107 if (addr_tokens[0]->type == STRUCTURE_BASE
17108 && (bitmap_bit_p (&map_field_head, DECL_UID (t))
17109 || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
17110 goto skip_decl_checks;
17111
17112 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17113 {
17114 if (ort != C_ORT_ACC && EXPR_P (t))
17115 break;
17116
17117 error_at (OMP_CLAUSE_LOCATION (c),
17118 "%qE is not a variable in %qs clause", t,
17119 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17120 remove = true;
17121 }
17122 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
17123 {
17124 error_at (OMP_CLAUSE_LOCATION (c),
17125 "%qD is threadprivate variable in %qs clause", t,
17126 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17127 remove = true;
17128 }
17129 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17130 || (OMP_CLAUSE_MAP_KIND (c)
17131 != GOMP_MAP_FIRSTPRIVATE_POINTER))
17132 && !c_mark_addressable (t))
17133 remove = true;
17134 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17135 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
17136 || (OMP_CLAUSE_MAP_KIND (c)
17137 == GOMP_MAP_FIRSTPRIVATE_POINTER)
17138 || (OMP_CLAUSE_MAP_KIND (c)
17139 == GOMP_MAP_FORCE_DEVICEPTR)))
17140 && t == OMP_CLAUSE_DECL (c)
17141 && !omp_mappable_type (TREE_TYPE (t)))
17142 {
17143 error_at (OMP_CLAUSE_LOCATION (c),
17144 "%qD does not have a mappable type in %qs clause", t,
17145 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17146 remove = true;
17147 }
17148 else if (TREE_TYPE (t) == error_mark_node)
17149 remove = true;
17150 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
17151 {
17152 error_at (OMP_CLAUSE_LOCATION (c),
17153 "%<_Atomic%> %qE in %qs clause", t,
17154 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17155 remove = true;
17156 }
17157 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17158 && OMP_CLAUSE_MAP_IMPLICIT (c)
17159 && (bitmap_bit_p (&map_head, DECL_UID (t))
17160 || bitmap_bit_p (&map_field_head, DECL_UID (t))
17161 || bitmap_bit_p (&map_firstprivate_head,
17162 DECL_UID (t))))
17163 remove = true;
17164 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17165 && (OMP_CLAUSE_MAP_KIND (c)
17166 == GOMP_MAP_FIRSTPRIVATE_POINTER))
17167 {
17168 if (bitmap_bit_p (&generic_head, DECL_UID (t))
17169 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17170 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17171 {
17172 error_at (OMP_CLAUSE_LOCATION (c),
17173 "%qD appears more than once in data clauses", t);
17174 remove = true;
17175 }
17176 else if (bitmap_bit_p (&map_head, DECL_UID (t))
17177 && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17178 && openacc)
17179 {
17180 error_at (OMP_CLAUSE_LOCATION (c),
17181 "%qD appears more than once in data clauses", t);
17182 remove = true;
17183 }
17184 else
17185 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
17186 }
17187 else if (bitmap_bit_p (&map_head, DECL_UID (t))
17188 && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17189 && ort != C_ORT_OMP
17190 && ort != C_ORT_OMP_EXIT_DATA)
17191 {
17192 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17193 error_at (OMP_CLAUSE_LOCATION (c),
17194 "%qD appears more than once in motion clauses", t);
17195 else if (openacc)
17196 error_at (OMP_CLAUSE_LOCATION (c),
17197 "%qD appears more than once in data clauses", t);
17198 else
17199 error_at (OMP_CLAUSE_LOCATION (c),
17200 "%qD appears more than once in map clauses", t);
17201 remove = true;
17202 }
17203 else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
17204 {
17205 error_at (OMP_CLAUSE_LOCATION (c),
17206 "%qD appears more than once in data clauses", t);
17207 remove = true;
17208 }
17209 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17210 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
17211 {
17212 if (openacc)
17213 error_at (OMP_CLAUSE_LOCATION (c),
17214 "%qD appears more than once in data clauses", t);
17215 else
17216 error_at (OMP_CLAUSE_LOCATION (c),
17217 "%qD appears both in data and map clauses", t);
17218 remove = true;
17219 }
17220 else if (!omp_access_chain_p (addr_tokens, 1))
17221 {
17222 bitmap_set_bit (&map_head, DECL_UID (t));
17223 if (t != OMP_CLAUSE_DECL (c)
17224 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
17225 bitmap_set_bit (&map_field_head, DECL_UID (t));
17226 }
17227
17228 skip_decl_checks:
17229 /* If we call omp_expand_map_clause in handle_omp_array_sections,
17230 the containing loop (here) iterates through the new nodes
17231 created by that expansion. Avoid expanding those again (just
17232 by checking the node type). */
17233 if (!remove
17234 && ort != C_ORT_DECLARE_SIMD
17235 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17236 || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
17237 && (OMP_CLAUSE_MAP_KIND (c)
17238 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
17239 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
17240 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
17241 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
17242 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
17243 {
17244 grp_start_p = pc;
17245 grp_sentinel = OMP_CLAUSE_CHAIN (c);
17246 tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
17247 addr_tokens, ort);
17248 if (nc != error_mark_node)
17249 c = nc;
17250 }
17251 }
17252 break;
17253
17254 case OMP_CLAUSE_ENTER:
17255 case OMP_CLAUSE_LINK:
17256 t = OMP_CLAUSE_DECL (c);
17257 const char *cname;
17258 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
17259 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
17260 && OMP_CLAUSE_ENTER_TO (c))
17261 cname = "to";
17262 if (TREE_CODE (t) == FUNCTION_DECL
17263 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17264 ;
17265 else if (!VAR_P (t))
17266 {
17267 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17268 error_at (OMP_CLAUSE_LOCATION (c),
17269 "%qE is neither a variable nor a function name in "
17270 "clause %qs", t, cname);
17271 else
17272 error_at (OMP_CLAUSE_LOCATION (c),
17273 "%qE is not a variable in clause %qs", t, cname);
17274 remove = true;
17275 }
17276 else if (DECL_THREAD_LOCAL_P (t))
17277 {
17278 error_at (OMP_CLAUSE_LOCATION (c),
17279 "%qD is threadprivate variable in %qs clause", t,
17280 cname);
17281 remove = true;
17282 }
17283 else if (!omp_mappable_type (TREE_TYPE (t)))
17284 {
17285 error_at (OMP_CLAUSE_LOCATION (c),
17286 "%qD does not have a mappable type in %qs clause", t,
17287 cname);
17288 remove = true;
17289 }
17290 if (remove)
17291 break;
17292 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
17293 {
17294 error_at (OMP_CLAUSE_LOCATION (c),
17295 "%qE appears more than once on the same "
17296 "%<declare target%> directive", t);
17297 remove = true;
17298 }
17299 else
17300 bitmap_set_bit (&generic_head, DECL_UID (t));
17301 break;
17302
17303 case OMP_CLAUSE_UNIFORM:
17304 t = OMP_CLAUSE_DECL (c);
17305 if (TREE_CODE (t) != PARM_DECL)
17306 {
17307 if (DECL_P (t))
17308 error_at (OMP_CLAUSE_LOCATION (c),
17309 "%qD is not an argument in %<uniform%> clause", t);
17310 else
17311 error_at (OMP_CLAUSE_LOCATION (c),
17312 "%qE is not an argument in %<uniform%> clause", t);
17313 remove = true;
17314 break;
17315 }
17316 /* map_head bitmap is used as uniform_head if declare_simd. */
17317 bitmap_set_bit (&map_head, DECL_UID (t));
17318 goto check_dup_generic;
17319
17320 case OMP_CLAUSE_IS_DEVICE_PTR:
17321 case OMP_CLAUSE_USE_DEVICE_PTR:
17322 t = OMP_CLAUSE_DECL (c);
17323 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
17324 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
17325 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
17326 {
17327 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
17328 && !openacc)
17329 {
17330 error_at (OMP_CLAUSE_LOCATION (c),
17331 "%qs variable is not a pointer",
17332 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17333 remove = true;
17334 }
17335 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
17336 {
17337 error_at (OMP_CLAUSE_LOCATION (c),
17338 "%qs variable is neither a pointer nor an array",
17339 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17340 remove = true;
17341 }
17342 }
17343 goto check_dup_generic;
17344
17345 case OMP_CLAUSE_HAS_DEVICE_ADDR:
17346 t = OMP_CLAUSE_DECL (c);
17347 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17348 {
17349 if (handle_omp_array_sections (c, ort))
17350 remove = true;
17351 else
17352 {
17353 t = OMP_CLAUSE_DECL (c);
17354 while (TREE_CODE (t) == ARRAY_REF)
17355 t = TREE_OPERAND (t, 0);
17356 }
17357 }
17358 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
17359 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
17360 c_mark_addressable (t);
17361 goto check_dup_generic_t;
17362
17363 case OMP_CLAUSE_USE_DEVICE_ADDR:
17364 t = OMP_CLAUSE_DECL (c);
17365 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
17366 c_mark_addressable (t);
17367 goto check_dup_generic;
17368
17369 case OMP_CLAUSE_NOWAIT:
17370 if (copyprivate_seen)
17371 {
17372 error_at (OMP_CLAUSE_LOCATION (c),
17373 "%<nowait%> clause must not be used together "
17374 "with %<copyprivate%> clause");
17375 remove = true;
17376 break;
17377 }
17378 nowait_clause = pc;
17379 pc = &OMP_CLAUSE_CHAIN (c);
17380 continue;
17381
17382 case OMP_CLAUSE_ORDER:
17383 if (ordered_clause)
17384 {
17385 error_at (OMP_CLAUSE_LOCATION (c),
17386 "%<order%> clause must not be used together "
17387 "with %<ordered%> clause");
17388 remove = true;
17389 break;
17390 }
17391 else if (order_clause)
17392 {
17393 /* Silently remove duplicates. */
17394 remove = true;
17395 break;
17396 }
17397 order_clause = pc;
17398 pc = &OMP_CLAUSE_CHAIN (c);
17399 continue;
17400
17401 case OMP_CLAUSE_DETACH:
17402 t = OMP_CLAUSE_DECL (c);
17403 if (detach_seen)
17404 {
17405 error_at (OMP_CLAUSE_LOCATION (c),
17406 "too many %qs clauses on a task construct",
17407 "detach");
17408 remove = true;
17409 break;
17410 }
17411 detach_seen = pc;
17412 pc = &OMP_CLAUSE_CHAIN (c);
17413 c_mark_addressable (t);
17414 continue;
17415
17416 case OMP_CLAUSE_IF:
17417 case OMP_CLAUSE_SELF:
17418 case OMP_CLAUSE_NUM_THREADS:
17419 case OMP_CLAUSE_NUM_TEAMS:
17420 case OMP_CLAUSE_THREAD_LIMIT:
17421 case OMP_CLAUSE_DEFAULT:
17422 case OMP_CLAUSE_UNTIED:
17423 case OMP_CLAUSE_COLLAPSE:
17424 case OMP_CLAUSE_FINAL:
17425 case OMP_CLAUSE_DEVICE:
17426 case OMP_CLAUSE_DIST_SCHEDULE:
17427 case OMP_CLAUSE_PARALLEL:
17428 case OMP_CLAUSE_FOR:
17429 case OMP_CLAUSE_SECTIONS:
17430 case OMP_CLAUSE_TASKGROUP:
17431 case OMP_CLAUSE_PROC_BIND:
17432 case OMP_CLAUSE_DEVICE_TYPE:
17433 case OMP_CLAUSE_PRIORITY:
17434 case OMP_CLAUSE_THREADS:
17435 case OMP_CLAUSE_SIMD:
17436 case OMP_CLAUSE_HINT:
17437 case OMP_CLAUSE_FILTER:
17438 case OMP_CLAUSE_DEFAULTMAP:
17439 case OMP_CLAUSE_BIND:
17440 case OMP_CLAUSE_NUM_GANGS:
17441 case OMP_CLAUSE_NUM_WORKERS:
17442 case OMP_CLAUSE_VECTOR_LENGTH:
17443 case OMP_CLAUSE_ASYNC:
17444 case OMP_CLAUSE_WAIT:
17445 case OMP_CLAUSE_AUTO:
17446 case OMP_CLAUSE_INDEPENDENT:
17447 case OMP_CLAUSE_SEQ:
17448 case OMP_CLAUSE_GANG:
17449 case OMP_CLAUSE_WORKER:
17450 case OMP_CLAUSE_VECTOR:
17451 case OMP_CLAUSE_TILE:
17452 case OMP_CLAUSE_IF_PRESENT:
17453 case OMP_CLAUSE_FINALIZE:
17454 case OMP_CLAUSE_NOHOST:
17455 case OMP_CLAUSE_INDIRECT:
17456 case OMP_CLAUSE_NOVARIANTS:
17457 case OMP_CLAUSE_NOCONTEXT:
17458 pc = &OMP_CLAUSE_CHAIN (c);
17459 continue;
17460
17461 case OMP_CLAUSE_GRAINSIZE:
17462 grainsize_seen = pc;
17463 pc = &OMP_CLAUSE_CHAIN (c);
17464 continue;
17465
17466 case OMP_CLAUSE_NUM_TASKS:
17467 num_tasks_seen = true;
17468 pc = &OMP_CLAUSE_CHAIN (c);
17469 continue;
17470
17471 case OMP_CLAUSE_MERGEABLE:
17472 mergeable_seen = true;
17473 pc = &OMP_CLAUSE_CHAIN (c);
17474 continue;
17475
17476 case OMP_CLAUSE_NOGROUP:
17477 nogroup_seen = pc;
17478 pc = &OMP_CLAUSE_CHAIN (c);
17479 continue;
17480
17481 case OMP_CLAUSE_SCHEDULE:
17482 schedule_clause = c;
17483 pc = &OMP_CLAUSE_CHAIN (c);
17484 continue;
17485
17486 case OMP_CLAUSE_ORDERED:
17487 ordered_clause = c;
17488 if (order_clause)
17489 {
17490 error_at (OMP_CLAUSE_LOCATION (*order_clause),
17491 "%<order%> clause must not be used together "
17492 "with %<ordered%> clause");
17493 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
17494 order_clause = NULL;
17495 }
17496 pc = &OMP_CLAUSE_CHAIN (c);
17497 continue;
17498
17499 case OMP_CLAUSE_SAFELEN:
17500 safelen = c;
17501 pc = &OMP_CLAUSE_CHAIN (c);
17502 continue;
17503 case OMP_CLAUSE_SIMDLEN:
17504 simdlen = c;
17505 pc = &OMP_CLAUSE_CHAIN (c);
17506 continue;
17507
17508 case OMP_CLAUSE_FULL:
17509 full_seen = pc;
17510 pc = &OMP_CLAUSE_CHAIN (c);
17511 continue;
17512
17513 case OMP_CLAUSE_PARTIAL:
17514 partial_seen = true;
17515 pc = &OMP_CLAUSE_CHAIN (c);
17516 continue;
17517
17518 case OMP_CLAUSE_SIZES:
17519 pc = &OMP_CLAUSE_CHAIN (c);
17520 continue;
17521
17522 case OMP_CLAUSE_INBRANCH:
17523 case OMP_CLAUSE_NOTINBRANCH:
17524 if (branch_seen)
17525 {
17526 error_at (OMP_CLAUSE_LOCATION (c),
17527 "%<inbranch%> clause is incompatible with "
17528 "%<notinbranch%>");
17529 remove = true;
17530 break;
17531 }
17532 branch_seen = true;
17533 pc = &OMP_CLAUSE_CHAIN (c);
17534 continue;
17535
17536 case OMP_CLAUSE_INCLUSIVE:
17537 case OMP_CLAUSE_EXCLUSIVE:
17538 need_complete = true;
17539 need_implicitly_determined = true;
17540 t = OMP_CLAUSE_DECL (c);
17541 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17542 {
17543 error_at (OMP_CLAUSE_LOCATION (c),
17544 "%qE is not a variable in clause %qs", t,
17545 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17546 remove = true;
17547 }
17548 break;
17549
17550 case OMP_CLAUSE_INIT:
17551 init_seen = true;
17552 if (!OMP_CLAUSE_INIT_TARGETSYNC (c))
17553 init_no_targetsync_clause = c;
17554 /* FALLTHRU */
17555 case OMP_CLAUSE_DESTROY:
17556 case OMP_CLAUSE_USE:
17557 init_use_destroy_seen = true;
17558 t = OMP_CLAUSE_DECL (c);
17559 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
17560 {
17561 error_at (OMP_CLAUSE_LOCATION (c),
17562 "%qD appears more than once in action clauses", t);
17563 remove = true;
17564 break;
17565 }
17566 bitmap_set_bit (&generic_head, DECL_UID (t));
17567 /* FALLTHRU */
17568 case OMP_CLAUSE_INTEROP:
17569 if (/* ort == C_ORT_OMP_INTEROP [uncomment for depobj init] */
17570 !c_omp_interop_t_p (TREE_TYPE (OMP_CLAUSE_DECL (c))))
17571 {
17572 error_at (OMP_CLAUSE_LOCATION (c),
17573 "%qD must be of %<omp_interop_t%>",
17574 OMP_CLAUSE_DECL (c));
17575 remove = true;
17576 }
17577 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INIT
17578 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DESTROY)
17579 && TREE_READONLY (OMP_CLAUSE_DECL (c)))
17580 {
17581 error_at (OMP_CLAUSE_LOCATION (c),
17582 "%qD shall not be const", OMP_CLAUSE_DECL (c));
17583 remove = true;
17584 }
17585 pc = &OMP_CLAUSE_CHAIN (c);
17586 break;
17587 default:
17588 gcc_unreachable ();
17589 }
17590
17591 if (!remove)
17592 {
17593 t = OMP_CLAUSE_DECL (c);
17594
17595 if (need_complete)
17596 {
17597 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
17598 if (t == error_mark_node)
17599 remove = true;
17600 }
17601
17602 if (need_implicitly_determined)
17603 {
17604 const char *share_name = NULL;
17605
17606 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
17607 share_name = "threadprivate";
17608 else switch (c_omp_predetermined_sharing (t))
17609 {
17610 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
17611 break;
17612 case OMP_CLAUSE_DEFAULT_SHARED:
17613 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
17614 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
17615 && c_omp_predefined_variable (t))
17616 /* The __func__ variable and similar function-local
17617 predefined variables may be listed in a shared or
17618 firstprivate clause. */
17619 break;
17620 share_name = "shared";
17621 break;
17622 case OMP_CLAUSE_DEFAULT_PRIVATE:
17623 share_name = "private";
17624 break;
17625 default:
17626 gcc_unreachable ();
17627 }
17628 if (share_name)
17629 {
17630 error_at (OMP_CLAUSE_LOCATION (c),
17631 "%qE is predetermined %qs for %qs",
17632 t, share_name,
17633 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17634 remove = true;
17635 }
17636 else if (TREE_READONLY (t)
17637 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
17638 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
17639 {
17640 error_at (OMP_CLAUSE_LOCATION (c),
17641 "%<const%> qualified %qE may appear only in "
17642 "%<shared%> or %<firstprivate%> clauses", t);
17643 remove = true;
17644 }
17645 }
17646 }
17647
17648 if (remove)
17649 {
17650 if (grp_start_p)
17651 {
17652 /* If we found a clause to remove, we want to remove the whole
17653 expanded group, otherwise gimplify
17654 (omp_resolve_clause_dependencies) can get confused. */
17655 *grp_start_p = grp_sentinel;
17656 pc = grp_start_p;
17657 grp_start_p = NULL;
17658 }
17659 else
17660 *pc = OMP_CLAUSE_CHAIN (c);
17661 }
17662 else
17663 pc = &OMP_CLAUSE_CHAIN (c);
17664 }
17665
17666 if (simdlen
17667 && safelen
17668 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
17669 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
17670 {
17671 error_at (OMP_CLAUSE_LOCATION (simdlen),
17672 "%<simdlen%> clause value is bigger than "
17673 "%<safelen%> clause value");
17674 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
17675 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
17676 }
17677
17678 if (ordered_clause
17679 && schedule_clause
17680 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
17681 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
17682 {
17683 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
17684 "%<nonmonotonic%> schedule modifier specified together "
17685 "with %<ordered%> clause");
17686 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
17687 = (enum omp_clause_schedule_kind)
17688 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
17689 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
17690 }
17691
17692 if (reduction_seen < 0 && ordered_clause)
17693 {
17694 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
17695 "%qs clause specified together with %<inscan%> "
17696 "%<reduction%> clause", "ordered");
17697 reduction_seen = -2;
17698 }
17699
17700 if (reduction_seen < 0 && schedule_clause)
17701 {
17702 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
17703 "%qs clause specified together with %<inscan%> "
17704 "%<reduction%> clause", "schedule");
17705 reduction_seen = -2;
17706 }
17707
17708 if (linear_variable_step_check
17709 || reduction_seen == -2
17710 || allocate_seen
17711 || target_in_reduction_seen)
17712 for (pc = &clauses, c = clauses; c ; c = *pc)
17713 {
17714 bool remove = false;
17715 if (allocate_seen)
17716 switch (OMP_CLAUSE_CODE (c))
17717 {
17718 case OMP_CLAUSE_REDUCTION:
17719 case OMP_CLAUSE_IN_REDUCTION:
17720 case OMP_CLAUSE_TASK_REDUCTION:
17721 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
17722 {
17723 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
17724 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
17725 t = TREE_OPERAND (t, 0);
17726 if (TREE_CODE (t) == ADDR_EXPR
17727 || INDIRECT_REF_P (t))
17728 t = TREE_OPERAND (t, 0);
17729 if (DECL_P (t))
17730 bitmap_clear_bit (&aligned_head, DECL_UID (t));
17731 break;
17732 }
17733 /* FALLTHRU */
17734 case OMP_CLAUSE_PRIVATE:
17735 case OMP_CLAUSE_FIRSTPRIVATE:
17736 case OMP_CLAUSE_LASTPRIVATE:
17737 case OMP_CLAUSE_LINEAR:
17738 if (DECL_P (OMP_CLAUSE_DECL (c)))
17739 bitmap_clear_bit (&aligned_head,
17740 DECL_UID (OMP_CLAUSE_DECL (c)));
17741 break;
17742 default:
17743 break;
17744 }
17745 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
17746 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
17747 && !bitmap_bit_p (&map_head,
17748 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
17749 {
17750 error_at (OMP_CLAUSE_LOCATION (c),
17751 "%<linear%> clause step is a parameter %qD not "
17752 "specified in %<uniform%> clause",
17753 OMP_CLAUSE_LINEAR_STEP (c));
17754 remove = true;
17755 }
17756 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
17757 && reduction_seen == -2)
17758 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
17759 if (target_in_reduction_seen
17760 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
17761 {
17762 tree t = OMP_CLAUSE_DECL (c);
17763 while (handled_component_p (t)
17764 || INDIRECT_REF_P (t)
17765 || TREE_CODE (t) == ADDR_EXPR
17766 || TREE_CODE (t) == MEM_REF
17767 || TREE_CODE (t) == NON_LVALUE_EXPR)
17768 t = TREE_OPERAND (t, 0);
17769 if (DECL_P (t)
17770 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17771 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
17772 }
17773
17774 if (remove)
17775 *pc = OMP_CLAUSE_CHAIN (c);
17776 else
17777 pc = &OMP_CLAUSE_CHAIN (c);
17778 }
17779
17780 if (allocate_seen)
17781 for (pc = &clauses, c = clauses; c ; c = *pc)
17782 {
17783 bool remove = false;
17784 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
17785 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
17786 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
17787 {
17788 error_at (OMP_CLAUSE_LOCATION (c),
17789 "%qD specified in %<allocate%> clause but not in "
17790 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
17791 remove = true;
17792 }
17793 if (remove)
17794 *pc = OMP_CLAUSE_CHAIN (c);
17795 else
17796 pc = &OMP_CLAUSE_CHAIN (c);
17797 }
17798
17799 if (nogroup_seen && reduction_seen)
17800 {
17801 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
17802 "%<nogroup%> clause must not be used together with "
17803 "%<reduction%> clause");
17804 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
17805 }
17806
17807 if (grainsize_seen && num_tasks_seen)
17808 {
17809 error_at (OMP_CLAUSE_LOCATION (*grainsize_seen),
17810 "%<grainsize%> clause must not be used together with "
17811 "%<num_tasks%> clause");
17812 *grainsize_seen = OMP_CLAUSE_CHAIN (*grainsize_seen);
17813 }
17814
17815 if (full_seen && partial_seen)
17816 {
17817 error_at (OMP_CLAUSE_LOCATION (*full_seen),
17818 "%<full%> clause must not be used together with "
17819 "%<partial%> clause");
17820 *full_seen = OMP_CLAUSE_CHAIN (*full_seen);
17821 }
17822
17823 if (detach_seen)
17824 {
17825 if (mergeable_seen)
17826 {
17827 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
17828 "%<detach%> clause must not be used together with "
17829 "%<mergeable%> clause");
17830 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
17831 }
17832 else
17833 {
17834 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
17835
17836 for (pc = &clauses, c = clauses; c ; c = *pc)
17837 {
17838 bool remove = false;
17839 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
17840 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
17841 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
17842 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
17843 && OMP_CLAUSE_DECL (c) == detach_decl)
17844 {
17845 error_at (OMP_CLAUSE_LOCATION (c),
17846 "the event handle of a %<detach%> clause "
17847 "should not be in a data-sharing clause");
17848 remove = true;
17849 }
17850 if (remove)
17851 *pc = OMP_CLAUSE_CHAIN (c);
17852 else
17853 pc = &OMP_CLAUSE_CHAIN (c);
17854 }
17855 }
17856 }
17857
17858 if (ort == C_ORT_OMP_INTEROP
17859 && depend_clause
17860 && (!init_use_destroy_seen
17861 || (init_seen && init_no_targetsync_clause)))
17862 {
17863 error_at (OMP_CLAUSE_LOCATION (depend_clause),
17864 "%<depend%> clause requires action clauses with "
17865 "%<targetsync%> interop-type");
17866 if (init_no_targetsync_clause)
17867 inform (OMP_CLAUSE_LOCATION (init_no_targetsync_clause),
17868 "%<init%> clause lacks the %<targetsync%> modifier");
17869 }
17870
17871 bitmap_obstack_release (NULL);
17872 return clauses;
17873 }
17874
17875 /* Return code to initialize DST with a copy constructor from SRC.
17876 C doesn't have copy constructors nor assignment operators, only for
17877 _Atomic vars we need to perform __atomic_load from src into a temporary
17878 followed by __atomic_store of the temporary to dst. */
17879
17880 tree
17881 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
17882 {
17883 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
17884 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
17885
17886 location_t loc = OMP_CLAUSE_LOCATION (clause);
17887 tree type = TREE_TYPE (dst);
17888 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
17889 tree tmp = create_tmp_var (nonatomic_type);
17890 tree tmp_addr = build_fold_addr_expr (tmp);
17891 TREE_ADDRESSABLE (tmp) = 1;
17892 suppress_warning (tmp);
17893 tree src_addr = build_fold_addr_expr (src);
17894 tree dst_addr = build_fold_addr_expr (dst);
17895 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
17896 vec<tree, va_gc> *params;
17897 /* Expansion of a generic atomic load may require an addition
17898 element, so allocate enough to prevent a resize. */
17899 vec_alloc (params, 4);
17900
17901 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
17902 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
17903 params->quick_push (src_addr);
17904 params->quick_push (tmp_addr);
17905 params->quick_push (seq_cst);
17906 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
17907
17908 vec_alloc (params, 4);
17909
17910 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
17911 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
17912 params->quick_push (dst_addr);
17913 params->quick_push (tmp_addr);
17914 params->quick_push (seq_cst);
17915 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
17916 return build2 (COMPOUND_EXPR, void_type_node, load, store);
17917 }
17918
17919 /* Create a transaction node. */
17920
17921 tree
17922 c_finish_transaction (location_t loc, tree block, int flags)
17923 {
17924 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
17925 if (flags & TM_STMT_ATTR_OUTER)
17926 TRANSACTION_EXPR_OUTER (stmt) = 1;
17927 if (flags & TM_STMT_ATTR_RELAXED)
17928 TRANSACTION_EXPR_RELAXED (stmt) = 1;
17929 return add_stmt (stmt);
17930 }
17931
17932 /* Make a variant type in the proper way for C/C++, propagating qualifiers
17933 down to the element type of an array. If ORIG_QUAL_TYPE is not
17934 NULL, then it should be used as the qualified type
17935 ORIG_QUAL_INDIRECT levels down in array type derivation (to
17936 preserve information about the typedef name from which an array
17937 type was derived). */
17938
17939 tree
17940 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
17941 size_t orig_qual_indirect)
17942 {
17943 if (type == error_mark_node)
17944 return type;
17945
17946 if (TREE_CODE (type) == ARRAY_TYPE)
17947 {
17948 tree t;
17949 tree element_type = c_build_qualified_type (TREE_TYPE (type),
17950 type_quals, orig_qual_type,
17951 orig_qual_indirect - 1);
17952
17953 /* See if we already have an identically qualified type. */
17954 if (orig_qual_type && orig_qual_indirect == 0)
17955 t = orig_qual_type;
17956 else
17957 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
17958 {
17959 if (TYPE_QUALS (strip_array_types (t)) == type_quals
17960 && TYPE_NAME (t) == TYPE_NAME (type)
17961 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
17962 && attribute_list_equal (TYPE_ATTRIBUTES (t),
17963 TYPE_ATTRIBUTES (type)))
17964 break;
17965 }
17966 if (!t)
17967 {
17968 tree domain = TYPE_DOMAIN (type);
17969
17970 t = build_variant_type_copy (type);
17971 TREE_TYPE (t) = element_type;
17972 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (element_type);
17973
17974 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
17975 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
17976 SET_TYPE_STRUCTURAL_EQUALITY (t);
17977 else if (TYPE_CANONICAL (element_type) != element_type
17978 || (domain && TYPE_CANONICAL (domain) != domain))
17979 {
17980 tree unqualified_canon
17981 = c_build_array_type (TYPE_CANONICAL (element_type),
17982 domain ? TYPE_CANONICAL (domain)
17983 : NULL_TREE);
17984 if (TYPE_REVERSE_STORAGE_ORDER (type))
17985 {
17986 unqualified_canon
17987 = build_distinct_type_copy (unqualified_canon);
17988 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
17989 }
17990 TYPE_CANONICAL (t)
17991 = c_build_qualified_type (unqualified_canon, type_quals);
17992 }
17993 else
17994 TYPE_CANONICAL (t) = t;
17995 }
17996 return t;
17997 }
17998
17999 /* A restrict-qualified pointer type must be a pointer to object or
18000 incomplete type. Note that the use of POINTER_TYPE_P also allows
18001 REFERENCE_TYPEs, which is appropriate for C++. */
18002 if ((type_quals & TYPE_QUAL_RESTRICT)
18003 && (!POINTER_TYPE_P (type)
18004 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
18005 {
18006 error ("invalid use of %<restrict%>");
18007 type_quals &= ~TYPE_QUAL_RESTRICT;
18008 }
18009
18010 tree var_type = (orig_qual_type && orig_qual_indirect == 0
18011 ? orig_qual_type
18012 : build_qualified_type (type, type_quals));
18013
18014 gcc_checking_assert (C_TYPE_VARIABLE_SIZE (var_type)
18015 == C_TYPE_VARIABLE_SIZE (type));
18016 gcc_checking_assert (C_TYPE_VARIABLY_MODIFIED (var_type)
18017 == C_TYPE_VARIABLY_MODIFIED (type));
18018
18019 /* A variant type does not inherit the list of incomplete vars from the
18020 type main variant. */
18021 if ((RECORD_OR_UNION_TYPE_P (var_type)
18022 || TREE_CODE (var_type) == ENUMERAL_TYPE)
18023 && TYPE_MAIN_VARIANT (var_type) != var_type)
18024 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
18025 return var_type;
18026 }
18027
18028 /* Build a VA_ARG_EXPR for the C parser. */
18029
18030 tree
18031 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
18032 {
18033 if (error_operand_p (type))
18034 return error_mark_node;
18035 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
18036 order because it takes the address of the expression. */
18037 else if (handled_component_p (expr)
18038 && reverse_storage_order_for_component_p (expr))
18039 {
18040 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
18041 return error_mark_node;
18042 }
18043 else if (!COMPLETE_TYPE_P (type))
18044 {
18045 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
18046 "type %qT", type);
18047 return error_mark_node;
18048 }
18049 else if (TREE_CODE (type) == FUNCTION_TYPE)
18050 {
18051 error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
18052 type);
18053 return error_mark_node;
18054 }
18055 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
18056 warning_at (loc2, OPT_Wc___compat,
18057 "C++ requires promoted type, not enum type, in %<va_arg%>");
18058 return build_va_arg (loc2, expr, type);
18059 }
18060
18061 /* Return truthvalue of whether T1 is the same tree structure as T2.
18062 Return 1 if they are the same. Return false if they are different. */
18063
18064 bool
18065 c_tree_equal (tree t1, tree t2)
18066 {
18067 enum tree_code code1, code2;
18068
18069 if (t1 == t2)
18070 return true;
18071 if (!t1 || !t2)
18072 return false;
18073
18074 for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
18075 code1 = TREE_CODE (t1))
18076 t1 = TREE_OPERAND (t1, 0);
18077 for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
18078 code2 = TREE_CODE (t2))
18079 t2 = TREE_OPERAND (t2, 0);
18080
18081 /* They might have become equal now. */
18082 if (t1 == t2)
18083 return true;
18084
18085 if (code1 != code2)
18086 return false;
18087
18088 if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18089 return false;
18090
18091 switch (code1)
18092 {
18093 case INTEGER_CST:
18094 return wi::to_wide (t1) == wi::to_wide (t2);
18095
18096 case REAL_CST:
18097 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
18098
18099 case STRING_CST:
18100 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
18101 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
18102 TREE_STRING_LENGTH (t1));
18103
18104 case FIXED_CST:
18105 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
18106 TREE_FIXED_CST (t2));
18107
18108 case COMPLEX_CST:
18109 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
18110 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
18111
18112 case VECTOR_CST:
18113 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
18114
18115 case CONSTRUCTOR:
18116 /* We need to do this when determining whether or not two
18117 non-type pointer to member function template arguments
18118 are the same. */
18119 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
18120 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
18121 return false;
18122 {
18123 tree field, value;
18124 unsigned int i;
18125 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
18126 {
18127 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
18128 if (!c_tree_equal (field, elt2->index)
18129 || !c_tree_equal (value, elt2->value))
18130 return false;
18131 }
18132 }
18133 return true;
18134
18135 case TREE_LIST:
18136 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
18137 return false;
18138 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
18139 return false;
18140 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
18141
18142 case SAVE_EXPR:
18143 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18144
18145 case CALL_EXPR:
18146 {
18147 tree arg1, arg2;
18148 call_expr_arg_iterator iter1, iter2;
18149 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
18150 return false;
18151 for (arg1 = first_call_expr_arg (t1, &iter1),
18152 arg2 = first_call_expr_arg (t2, &iter2);
18153 arg1 && arg2;
18154 arg1 = next_call_expr_arg (&iter1),
18155 arg2 = next_call_expr_arg (&iter2))
18156 if (!c_tree_equal (arg1, arg2))
18157 return false;
18158 if (arg1 || arg2)
18159 return false;
18160 return true;
18161 }
18162
18163 case TARGET_EXPR:
18164 {
18165 tree o1 = TREE_OPERAND (t1, 0);
18166 tree o2 = TREE_OPERAND (t2, 0);
18167
18168 /* Special case: if either target is an unallocated VAR_DECL,
18169 it means that it's going to be unified with whatever the
18170 TARGET_EXPR is really supposed to initialize, so treat it
18171 as being equivalent to anything. */
18172 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
18173 && !DECL_RTL_SET_P (o1))
18174 /*Nop*/;
18175 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
18176 && !DECL_RTL_SET_P (o2))
18177 /*Nop*/;
18178 else if (!c_tree_equal (o1, o2))
18179 return false;
18180
18181 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
18182 }
18183
18184 case COMPONENT_REF:
18185 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
18186 return false;
18187 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18188
18189 case PARM_DECL:
18190 case VAR_DECL:
18191 case CONST_DECL:
18192 case FIELD_DECL:
18193 case FUNCTION_DECL:
18194 case IDENTIFIER_NODE:
18195 case SSA_NAME:
18196 return false;
18197
18198 case TREE_VEC:
18199 {
18200 unsigned ix;
18201 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
18202 return false;
18203 for (ix = TREE_VEC_LENGTH (t1); ix--;)
18204 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
18205 TREE_VEC_ELT (t2, ix)))
18206 return false;
18207 return true;
18208 }
18209
18210 CASE_CONVERT:
18211 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18212 return false;
18213 break;
18214
18215 default:
18216 break;
18217 }
18218
18219 switch (TREE_CODE_CLASS (code1))
18220 {
18221 case tcc_unary:
18222 case tcc_binary:
18223 case tcc_comparison:
18224 case tcc_expression:
18225 case tcc_vl_exp:
18226 case tcc_reference:
18227 case tcc_statement:
18228 {
18229 int i, n = TREE_OPERAND_LENGTH (t1);
18230
18231 switch (code1)
18232 {
18233 case PREINCREMENT_EXPR:
18234 case PREDECREMENT_EXPR:
18235 case POSTINCREMENT_EXPR:
18236 case POSTDECREMENT_EXPR:
18237 n = 1;
18238 break;
18239 case ARRAY_REF:
18240 n = 2;
18241 break;
18242 default:
18243 break;
18244 }
18245
18246 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
18247 && n != TREE_OPERAND_LENGTH (t2))
18248 return false;
18249
18250 for (i = 0; i < n; ++i)
18251 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
18252 return false;
18253
18254 return true;
18255 }
18256
18257 case tcc_type:
18258 return comptypes (t1, t2);
18259 default:
18260 gcc_unreachable ();
18261 }
18262 }
18263
18264 /* Returns true when the function declaration FNDECL is implicit,
18265 introduced as a result of a call to an otherwise undeclared
18266 function, and false otherwise. */
18267
18268 bool
18269 c_decl_implicit (const_tree fndecl)
18270 {
18271 return C_DECL_IMPLICIT (fndecl);
18272 }