1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2022 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "memmodel.h"
30 #include "target.h"
31 #include "function.h"
32 #include "bitmap.h"
33 #include "c-tree.h"
34 #include "gimple-expr.h"
35 #include "predict.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "langhooks.h"
41 #include "c-lang.h"
42 #include "intl.h"
43 #include "tree-iterator.h"
44 #include "gimplify.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "asan.h"
55
56 /* Possible cases of implicit conversions. Used to select diagnostic messages
57 and control folding initializers in convert_for_assignment. */
58 enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_init_const,
63 ic_return
64 };
65
66 /* The level of nesting inside "__alignof__". */
67 int in_alignof;
68
69 /* The level of nesting inside "sizeof". */
70 int in_sizeof;
71
72 /* The level of nesting inside "typeof". */
73 int in_typeof;
74
75 /* True when parsing OpenMP loop expressions. */
76 bool c_in_omp_for;
77
78 /* The argument of last parsed sizeof expression, only to be tested
79 if expr.original_code == SIZEOF_EXPR. */
80 tree c_last_sizeof_arg;
81 location_t c_last_sizeof_loc;
82
83 /* Nonzero if we might need to print a "missing braces around
84 initializer" message within this initializer. */
85 static int found_missing_braces;
86
87 static int require_constant_value;
88 static int require_constant_elements;
89
90 static bool null_pointer_constant_p (const_tree);
91 static tree qualify_type (tree, tree);
92 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
93 bool *);
94 static int comp_target_types (location_t, tree, tree);
95 static int function_types_compatible_p (const_tree, const_tree, bool *,
96 bool *);
97 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
98 static tree lookup_field (tree, tree);
99 static int convert_arguments (location_t, vec<location_t>, tree,
100 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
101 tree);
102 static tree pointer_diff (location_t, tree, tree, tree *);
103 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
104 enum impl_conv, bool, tree, tree, int,
105 int = 0);
106 static tree valid_compound_expr_initializer (tree, tree);
107 static void push_string (const char *);
108 static void push_member_name (tree);
109 static int spelling_length (void);
110 static char *print_spelling (char *);
111 static void warning_init (location_t, int, const char *);
112 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
113 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
114 bool, struct obstack *);
115 static void output_pending_init_elements (int, struct obstack *);
116 static bool set_designator (location_t, bool, struct obstack *);
117 static void push_range_stack (tree, struct obstack *);
118 static void add_pending_init (location_t, tree, tree, tree, bool,
119 struct obstack *);
120 static void set_nonincremental_init (struct obstack *);
121 static void set_nonincremental_init_from_string (tree, struct obstack *);
122 static tree find_init_member (tree, struct obstack *);
123 static void readonly_warning (tree, enum lvalue_use);
124 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
125 static void record_maybe_used_decl (tree);
126 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
127
128 /* Return true if EXP is a null pointer constant, false otherwise. */
129
130 static bool
null_pointer_constant_p(const_tree expr)131 null_pointer_constant_p (const_tree expr)
132 {
133 /* This should really operate on c_expr structures, but they aren't
134 yet available everywhere required. */
135 tree type = TREE_TYPE (expr);
136 return (TREE_CODE (expr) == INTEGER_CST
137 && !TREE_OVERFLOW (expr)
138 && integer_zerop (expr)
139 && (INTEGRAL_TYPE_P (type)
140 || (TREE_CODE (type) == POINTER_TYPE
141 && VOID_TYPE_P (TREE_TYPE (type))
142 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
143 }
144
145 /* EXPR may appear in an unevaluated part of an integer constant
146 expression, but not in an evaluated part. Wrap it in a
147 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
148 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
149
150 static tree
note_integer_operands(tree expr)151 note_integer_operands (tree expr)
152 {
153 tree ret;
154 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
155 {
156 ret = copy_node (expr);
157 TREE_OVERFLOW (ret) = 1;
158 }
159 else
160 {
161 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
162 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
163 }
164 return ret;
165 }
166
167 /* Having checked whether EXPR may appear in an unevaluated part of an
168 integer constant expression and found that it may, remove any
169 C_MAYBE_CONST_EXPR noting this fact and return the resulting
170 expression. */
171
172 static inline tree
remove_c_maybe_const_expr(tree expr)173 remove_c_maybe_const_expr (tree expr)
174 {
175 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
176 return C_MAYBE_CONST_EXPR_EXPR (expr);
177 else
178 return expr;
179 }
180
181 /* This is a cache to hold if two types are compatible or not. */
182
183 struct tagged_tu_seen_cache {
184 const struct tagged_tu_seen_cache * next;
185 const_tree t1;
186 const_tree t2;
187 /* The return value of tagged_types_tu_compatible_p if we had seen
188 these two types already. */
189 int val;
190 };
191
192 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
193 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
194
195 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
196 does not have an incomplete type. (That includes void types.)
197 LOC is the location of the use. */
198
199 tree
require_complete_type(location_t loc,tree value)200 require_complete_type (location_t loc, tree value)
201 {
202 tree type = TREE_TYPE (value);
203
204 if (error_operand_p (value))
205 return error_mark_node;
206
207 /* First, detect a valid value with a complete type. */
208 if (COMPLETE_TYPE_P (type))
209 return value;
210
211 c_incomplete_type_error (loc, value, type);
212 return error_mark_node;
213 }
214
215 /* Print an error message for invalid use of an incomplete type.
216 VALUE is the expression that was used (or 0 if that isn't known)
217 and TYPE is the type that was invalid. LOC is the location for
218 the error. */
219
220 void
c_incomplete_type_error(location_t loc,const_tree value,const_tree type)221 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
222 {
223 /* Avoid duplicate error message. */
224 if (TREE_CODE (type) == ERROR_MARK)
225 return;
226
227 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
228 error_at (loc, "%qD has an incomplete type %qT", value, type);
229 else
230 {
231 retry:
232 /* We must print an error message. Be clever about what it says. */
233
234 switch (TREE_CODE (type))
235 {
236 case RECORD_TYPE:
237 case UNION_TYPE:
238 case ENUMERAL_TYPE:
239 break;
240
241 case VOID_TYPE:
242 error_at (loc, "invalid use of void expression");
243 return;
244
245 case ARRAY_TYPE:
246 if (TYPE_DOMAIN (type))
247 {
248 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
249 {
250 error_at (loc, "invalid use of flexible array member");
251 return;
252 }
253 type = TREE_TYPE (type);
254 goto retry;
255 }
256 error_at (loc, "invalid use of array with unspecified bounds");
257 return;
258
259 default:
260 gcc_unreachable ();
261 }
262
263 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
264 error_at (loc, "invalid use of undefined type %qT", type);
265 else
266 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
267 error_at (loc, "invalid use of incomplete typedef %qT", type);
268 }
269 }
270
271 /* Given a type, apply default promotions wrt unnamed function
272 arguments and return the new type. */
273
274 tree
c_type_promotes_to(tree type)275 c_type_promotes_to (tree type)
276 {
277 tree ret = NULL_TREE;
278
279 if (TYPE_MAIN_VARIANT (type) == float_type_node)
280 ret = double_type_node;
281 else if (c_promoting_integer_type_p (type))
282 {
283 /* Preserve unsignedness if not really getting any wider. */
284 if (TYPE_UNSIGNED (type)
285 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
286 ret = unsigned_type_node;
287 else
288 ret = integer_type_node;
289 }
290
291 if (ret != NULL_TREE)
292 return (TYPE_ATOMIC (type)
293 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
294 : ret);
295
296 return type;
297 }
298
299 /* Return true if between two named address spaces, whether there is a superset
300 named address space that encompasses both address spaces. If there is a
301 superset, return which address space is the superset. */
302
303 static bool
addr_space_superset(addr_space_t as1,addr_space_t as2,addr_space_t * common)304 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
305 {
306 if (as1 == as2)
307 {
308 *common = as1;
309 return true;
310 }
311 else if (targetm.addr_space.subset_p (as1, as2))
312 {
313 *common = as2;
314 return true;
315 }
316 else if (targetm.addr_space.subset_p (as2, as1))
317 {
318 *common = as1;
319 return true;
320 }
321 else
322 return false;
323 }
324
325 /* Return a variant of TYPE which has all the type qualifiers of LIKE
326 as well as those of TYPE. */
327
328 static tree
qualify_type(tree type,tree like)329 qualify_type (tree type, tree like)
330 {
331 addr_space_t as_type = TYPE_ADDR_SPACE (type);
332 addr_space_t as_like = TYPE_ADDR_SPACE (like);
333 addr_space_t as_common;
334
335 /* If the two named address spaces are different, determine the common
336 superset address space. If there isn't one, raise an error. */
337 if (!addr_space_superset (as_type, as_like, &as_common))
338 {
339 as_common = as_type;
340 error ("%qT and %qT are in disjoint named address spaces",
341 type, like);
342 }
343
344 return c_build_qualified_type (type,
345 TYPE_QUALS_NO_ADDR_SPACE (type)
346 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
347 | ENCODE_QUAL_ADDR_SPACE (as_common));
348 }
349
350 /* Return true iff the given tree T is a variable length array. */
351
352 bool
c_vla_type_p(const_tree t)353 c_vla_type_p (const_tree t)
354 {
355 if (TREE_CODE (t) == ARRAY_TYPE
356 && C_TYPE_VARIABLE_SIZE (t))
357 return true;
358 return false;
359 }
360
361 /* If NTYPE is a type of a non-variadic function with a prototype
362 and OTYPE is a type of a function without a prototype and ATTRS
363 contains attribute format, diagnosess and removes it from ATTRS.
364 Returns the result of build_type_attribute_variant of NTYPE and
365 the (possibly) modified ATTRS. */
366
367 static tree
build_functype_attribute_variant(tree ntype,tree otype,tree attrs)368 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
369 {
370 if (!prototype_p (otype)
371 && prototype_p (ntype)
372 && lookup_attribute ("format", attrs))
373 {
374 warning_at (input_location, OPT_Wattributes,
375 "%qs attribute cannot be applied to a function that "
376 "does not take variable arguments", "format");
377 attrs = remove_attribute ("format", attrs);
378 }
379 return build_type_attribute_variant (ntype, attrs);
380
381 }
382 /* Return the composite type of two compatible types.
383
384 We assume that comptypes has already been done and returned
385 nonzero; if that isn't so, this may crash. In particular, we
386 assume that qualifiers match. */
387
388 tree
composite_type(tree t1,tree t2)389 composite_type (tree t1, tree t2)
390 {
391 enum tree_code code1;
392 enum tree_code code2;
393 tree attributes;
394
395 /* Save time if the two types are the same. */
396
397 if (t1 == t2) return t1;
398
399 /* If one type is nonsense, use the other. */
400 if (t1 == error_mark_node)
401 return t2;
402 if (t2 == error_mark_node)
403 return t1;
404
405 code1 = TREE_CODE (t1);
406 code2 = TREE_CODE (t2);
407
408 /* Merge the attributes. */
409 attributes = targetm.merge_type_attributes (t1, t2);
410
411 /* If one is an enumerated type and the other is the compatible
412 integer type, the composite type might be either of the two
413 (DR#013 question 3). For consistency, use the enumerated type as
414 the composite type. */
415
416 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
417 return t1;
418 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
419 return t2;
420
421 gcc_assert (code1 == code2);
422
423 switch (code1)
424 {
425 case POINTER_TYPE:
426 /* For two pointers, do this recursively on the target type. */
427 {
428 tree pointed_to_1 = TREE_TYPE (t1);
429 tree pointed_to_2 = TREE_TYPE (t2);
430 tree target = composite_type (pointed_to_1, pointed_to_2);
431 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
432 t1 = build_type_attribute_variant (t1, attributes);
433 return qualify_type (t1, t2);
434 }
435
436 case ARRAY_TYPE:
437 {
438 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
439 int quals;
440 tree unqual_elt;
441 tree d1 = TYPE_DOMAIN (t1);
442 tree d2 = TYPE_DOMAIN (t2);
443 bool d1_variable, d2_variable;
444 bool d1_zero, d2_zero;
445 bool t1_complete, t2_complete;
446
447 /* We should not have any type quals on arrays at all. */
448 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
449 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
450
451 t1_complete = COMPLETE_TYPE_P (t1);
452 t2_complete = COMPLETE_TYPE_P (t2);
453
454 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
455 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
456
457 d1_variable = (!d1_zero
458 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
459 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
460 d2_variable = (!d2_zero
461 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
462 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
463 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
464 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
465
466 /* Save space: see if the result is identical to one of the args. */
467 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
468 && (d2_variable || d2_zero || !d1_variable))
469 return build_type_attribute_variant (t1, attributes);
470 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
471 && (d1_variable || d1_zero || !d2_variable))
472 return build_type_attribute_variant (t2, attributes);
473
474 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
475 return build_type_attribute_variant (t1, attributes);
476 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
477 return build_type_attribute_variant (t2, attributes);
478
479 /* Merge the element types, and have a size if either arg has
480 one. We may have qualifiers on the element types. To set
481 up TYPE_MAIN_VARIANT correctly, we need to form the
482 composite of the unqualified types and add the qualifiers
483 back at the end. */
484 quals = TYPE_QUALS (strip_array_types (elt));
485 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
486 t1 = build_array_type (unqual_elt,
487 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
488 && (d2_variable
489 || d2_zero
490 || !d1_variable))
491 ? t1
492 : t2));
493 /* Ensure a composite type involving a zero-length array type
494 is a zero-length type not an incomplete type. */
495 if (d1_zero && d2_zero
496 && (t1_complete || t2_complete)
497 && !COMPLETE_TYPE_P (t1))
498 {
499 TYPE_SIZE (t1) = bitsize_zero_node;
500 TYPE_SIZE_UNIT (t1) = size_zero_node;
501 }
502 t1 = c_build_qualified_type (t1, quals);
503 return build_type_attribute_variant (t1, attributes);
504 }
505
506 case ENUMERAL_TYPE:
507 case RECORD_TYPE:
508 case UNION_TYPE:
509 if (attributes != NULL)
510 {
511 /* Try harder not to create a new aggregate type. */
512 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
513 return t1;
514 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
515 return t2;
516 }
517 return build_type_attribute_variant (t1, attributes);
518
519 case FUNCTION_TYPE:
520 /* Function types: prefer the one that specified arg types.
521 If both do, merge the arg types. Also merge the return types. */
522 {
523 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
524 tree p1 = TYPE_ARG_TYPES (t1);
525 tree p2 = TYPE_ARG_TYPES (t2);
526 int len;
527 tree newargs, n;
528 int i;
529
530 /* Save space: see if the result is identical to one of the args. */
531 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
532 return build_functype_attribute_variant (t1, t2, attributes);
533 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
534 return build_functype_attribute_variant (t2, t1, attributes);
535
536 /* Simple way if one arg fails to specify argument types. */
537 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
538 {
539 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
540 t1 = build_type_attribute_variant (t1, attributes);
541 return qualify_type (t1, t2);
542 }
543 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
544 {
545 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
546 t1 = build_type_attribute_variant (t1, attributes);
547 return qualify_type (t1, t2);
548 }
549
550 /* If both args specify argument types, we must merge the two
551 lists, argument by argument. */
552
553 for (len = 0, newargs = p1;
554 newargs && newargs != void_list_node;
555 len++, newargs = TREE_CHAIN (newargs))
556 ;
557
558 for (i = 0; i < len; i++)
559 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
560
561 n = newargs;
562
563 for (; p1 && p1 != void_list_node;
564 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
565 {
566 /* A null type means arg type is not specified.
567 Take whatever the other function type has. */
568 if (TREE_VALUE (p1) == NULL_TREE)
569 {
570 TREE_VALUE (n) = TREE_VALUE (p2);
571 goto parm_done;
572 }
573 if (TREE_VALUE (p2) == NULL_TREE)
574 {
575 TREE_VALUE (n) = TREE_VALUE (p1);
576 goto parm_done;
577 }
578
579 /* Given wait (union {union wait *u; int *i} *)
580 and wait (union wait *),
581 prefer union wait * as type of parm. */
582 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
583 && TREE_VALUE (p1) != TREE_VALUE (p2))
584 {
585 tree memb;
586 tree mv2 = TREE_VALUE (p2);
587 if (mv2 && mv2 != error_mark_node
588 && TREE_CODE (mv2) != ARRAY_TYPE)
589 mv2 = TYPE_MAIN_VARIANT (mv2);
590 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
591 memb; memb = DECL_CHAIN (memb))
592 {
593 tree mv3 = TREE_TYPE (memb);
594 if (mv3 && mv3 != error_mark_node
595 && TREE_CODE (mv3) != ARRAY_TYPE)
596 mv3 = TYPE_MAIN_VARIANT (mv3);
597 if (comptypes (mv3, mv2))
598 {
599 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
600 TREE_VALUE (p2));
601 pedwarn (input_location, OPT_Wpedantic,
602 "function types not truly compatible in ISO C");
603 goto parm_done;
604 }
605 }
606 }
607 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
608 && TREE_VALUE (p2) != TREE_VALUE (p1))
609 {
610 tree memb;
611 tree mv1 = TREE_VALUE (p1);
612 if (mv1 && mv1 != error_mark_node
613 && TREE_CODE (mv1) != ARRAY_TYPE)
614 mv1 = TYPE_MAIN_VARIANT (mv1);
615 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
616 memb; memb = DECL_CHAIN (memb))
617 {
618 tree mv3 = TREE_TYPE (memb);
619 if (mv3 && mv3 != error_mark_node
620 && TREE_CODE (mv3) != ARRAY_TYPE)
621 mv3 = TYPE_MAIN_VARIANT (mv3);
622 if (comptypes (mv3, mv1))
623 {
624 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
625 TREE_VALUE (p1));
626 pedwarn (input_location, OPT_Wpedantic,
627 "function types not truly compatible in ISO C");
628 goto parm_done;
629 }
630 }
631 }
632 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
633 parm_done: ;
634 }
635
636 t1 = build_function_type (valtype, newargs);
637 t1 = qualify_type (t1, t2);
638 }
639 /* FALLTHRU */
640
641 default:
642 return build_type_attribute_variant (t1, attributes);
643 }
644
645 }
646
647 /* Return the type of a conditional expression between pointers to
648 possibly differently qualified versions of compatible types.
649
650 We assume that comp_target_types has already been done and returned
651 nonzero; if that isn't so, this may crash. */
652
653 static tree
common_pointer_type(tree t1,tree t2)654 common_pointer_type (tree t1, tree t2)
655 {
656 tree attributes;
657 tree pointed_to_1, mv1;
658 tree pointed_to_2, mv2;
659 tree target;
660 unsigned target_quals;
661 addr_space_t as1, as2, as_common;
662 int quals1, quals2;
663
664 /* Save time if the two types are the same. */
665
666 if (t1 == t2) return t1;
667
668 /* If one type is nonsense, use the other. */
669 if (t1 == error_mark_node)
670 return t2;
671 if (t2 == error_mark_node)
672 return t1;
673
674 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
675 && TREE_CODE (t2) == POINTER_TYPE);
676
677 /* Merge the attributes. */
678 attributes = targetm.merge_type_attributes (t1, t2);
679
680 /* Find the composite type of the target types, and combine the
681 qualifiers of the two types' targets. Do not lose qualifiers on
682 array element types by taking the TYPE_MAIN_VARIANT. */
683 mv1 = pointed_to_1 = TREE_TYPE (t1);
684 mv2 = pointed_to_2 = TREE_TYPE (t2);
685 if (TREE_CODE (mv1) != ARRAY_TYPE)
686 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
687 if (TREE_CODE (mv2) != ARRAY_TYPE)
688 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
689 target = composite_type (mv1, mv2);
690
691 /* Strip array types to get correct qualifier for pointers to arrays */
692 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
693 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
694
695 /* For function types do not merge const qualifiers, but drop them
696 if used inconsistently. The middle-end uses these to mark const
697 and noreturn functions. */
698 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
699 target_quals = (quals1 & quals2);
700 else
701 target_quals = (quals1 | quals2);
702
703 /* If the two named address spaces are different, determine the common
704 superset address space. This is guaranteed to exist due to the
705 assumption that comp_target_type returned non-zero. */
706 as1 = TYPE_ADDR_SPACE (pointed_to_1);
707 as2 = TYPE_ADDR_SPACE (pointed_to_2);
708 if (!addr_space_superset (as1, as2, &as_common))
709 gcc_unreachable ();
710
711 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
712
713 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
714 return build_type_attribute_variant (t1, attributes);
715 }
716
717 /* Return the common type for two arithmetic types under the usual
718 arithmetic conversions. The default conversions have already been
719 applied, and enumerated types converted to their compatible integer
720 types. The resulting type is unqualified and has no attributes.
721
722 This is the type for the result of most arithmetic operations
723 if the operands have the given two types. */
724
725 static tree
c_common_type(tree t1,tree t2)726 c_common_type (tree t1, tree t2)
727 {
728 enum tree_code code1;
729 enum tree_code code2;
730
731 /* If one type is nonsense, use the other. */
732 if (t1 == error_mark_node)
733 return t2;
734 if (t2 == error_mark_node)
735 return t1;
736
737 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
738 t1 = TYPE_MAIN_VARIANT (t1);
739
740 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
741 t2 = TYPE_MAIN_VARIANT (t2);
742
743 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
744 {
745 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
746 t1 = build_type_attribute_variant (t1, attrs);
747 }
748
749 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
750 {
751 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
752 t2 = build_type_attribute_variant (t2, attrs);
753 }
754
755 /* Save time if the two types are the same. */
756
757 if (t1 == t2) return t1;
758
759 code1 = TREE_CODE (t1);
760 code2 = TREE_CODE (t2);
761
762 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
763 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
764 || code1 == INTEGER_TYPE);
765 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
766 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
767 || code2 == INTEGER_TYPE);
768
769 /* When one operand is a decimal float type, the other operand cannot be
770 a generic float type or a complex type. We also disallow vector types
771 here. */
772 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
773 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
774 {
775 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
776 {
777 error ("cannot mix operands of decimal floating and vector types");
778 return error_mark_node;
779 }
780 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
781 {
782 error ("cannot mix operands of decimal floating and complex types");
783 return error_mark_node;
784 }
785 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
786 {
787 error ("cannot mix operands of decimal floating "
788 "and other floating types");
789 return error_mark_node;
790 }
791 }
792
793 /* If one type is a vector type, return that type. (How the usual
794 arithmetic conversions apply to the vector types extension is not
795 precisely specified.) */
796 if (code1 == VECTOR_TYPE)
797 return t1;
798
799 if (code2 == VECTOR_TYPE)
800 return t2;
801
802 /* If one type is complex, form the common type of the non-complex
803 components, then make that complex. Use T1 or T2 if it is the
804 required type. */
805 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
806 {
807 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
808 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
809 tree subtype = c_common_type (subtype1, subtype2);
810
811 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
812 return t1;
813 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
814 return t2;
815 else
816 return build_complex_type (subtype);
817 }
818
819 /* If only one is real, use it as the result. */
820
821 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
822 return t1;
823
824 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
825 return t2;
826
827 /* If both are real and either are decimal floating point types, use
828 the decimal floating point type with the greater precision. */
829
830 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
831 {
832 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
833 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
834 return dfloat128_type_node;
835 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
836 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
837 return dfloat64_type_node;
838 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
839 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
840 return dfloat32_type_node;
841 }
842
843 /* Deal with fixed-point types. */
844 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
845 {
846 unsigned int unsignedp = 0, satp = 0;
847 scalar_mode m1, m2;
848 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
849
850 m1 = SCALAR_TYPE_MODE (t1);
851 m2 = SCALAR_TYPE_MODE (t2);
852
853 /* If one input type is saturating, the result type is saturating. */
854 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
855 satp = 1;
856
857 /* If both fixed-point types are unsigned, the result type is unsigned.
858 When mixing fixed-point and integer types, follow the sign of the
859 fixed-point type.
860 Otherwise, the result type is signed. */
861 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
862 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
863 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
864 && TYPE_UNSIGNED (t1))
865 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
866 && TYPE_UNSIGNED (t2)))
867 unsignedp = 1;
868
869 /* The result type is signed. */
870 if (unsignedp == 0)
871 {
872 /* If the input type is unsigned, we need to convert to the
873 signed type. */
874 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
875 {
876 enum mode_class mclass = (enum mode_class) 0;
877 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
878 mclass = MODE_FRACT;
879 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
880 mclass = MODE_ACCUM;
881 else
882 gcc_unreachable ();
883 m1 = as_a <scalar_mode>
884 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
885 }
886 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
887 {
888 enum mode_class mclass = (enum mode_class) 0;
889 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
890 mclass = MODE_FRACT;
891 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
892 mclass = MODE_ACCUM;
893 else
894 gcc_unreachable ();
895 m2 = as_a <scalar_mode>
896 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
897 }
898 }
899
900 if (code1 == FIXED_POINT_TYPE)
901 {
902 fbit1 = GET_MODE_FBIT (m1);
903 ibit1 = GET_MODE_IBIT (m1);
904 }
905 else
906 {
907 fbit1 = 0;
908 /* Signed integers need to subtract one sign bit. */
909 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
910 }
911
912 if (code2 == FIXED_POINT_TYPE)
913 {
914 fbit2 = GET_MODE_FBIT (m2);
915 ibit2 = GET_MODE_IBIT (m2);
916 }
917 else
918 {
919 fbit2 = 0;
920 /* Signed integers need to subtract one sign bit. */
921 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
922 }
923
924 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
925 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
926 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
927 satp);
928 }
929
930 /* Both real or both integers; use the one with greater precision. */
931
932 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
933 return t1;
934 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
935 return t2;
936
937 /* Same precision. Prefer long longs to longs to ints when the
938 same precision, following the C99 rules on integer type rank
939 (which are equivalent to the C90 rules for C90 types). */
940
941 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
942 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
943 return long_long_unsigned_type_node;
944
945 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
946 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
947 {
948 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
949 return long_long_unsigned_type_node;
950 else
951 return long_long_integer_type_node;
952 }
953
954 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
955 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
956 return long_unsigned_type_node;
957
958 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
959 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
960 {
961 /* But preserve unsignedness from the other type,
962 since long cannot hold all the values of an unsigned int. */
963 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
964 return long_unsigned_type_node;
965 else
966 return long_integer_type_node;
967 }
968
969 /* For floating types of the same TYPE_PRECISION (which we here
970 assume means either the same set of values, or sets of values
971 neither a subset of the other, with behavior being undefined in
972 the latter case), follow the rules from TS 18661-3: prefer
973 interchange types _FloatN, then standard types long double,
974 double, float, then extended types _FloatNx. For extended types,
975 check them starting with _Float128x as that seems most consistent
976 in spirit with preferring long double to double; for interchange
977 types, also check in that order for consistency although it's not
978 possible for more than one of them to have the same
979 precision. */
980 tree mv1 = TYPE_MAIN_VARIANT (t1);
981 tree mv2 = TYPE_MAIN_VARIANT (t2);
982
983 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
984 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
985 return FLOATN_TYPE_NODE (i);
986
987 /* Likewise, prefer long double to double even if same size. */
988 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
989 return long_double_type_node;
990
991 /* Likewise, prefer double to float even if same size.
992 We got a couple of embedded targets with 32 bit doubles, and the
993 pdp11 might have 64 bit floats. */
994 if (mv1 == double_type_node || mv2 == double_type_node)
995 return double_type_node;
996
997 if (mv1 == float_type_node || mv2 == float_type_node)
998 return float_type_node;
999
1000 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1001 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1002 return FLOATNX_TYPE_NODE (i);
1003
1004 /* Otherwise prefer the unsigned one. */
1005
1006 if (TYPE_UNSIGNED (t1))
1007 return t1;
1008 else
1009 return t2;
1010 }
1011
1012 /* Wrapper around c_common_type that is used by c-common.cc and other
1013 front end optimizations that remove promotions. ENUMERAL_TYPEs
1014 are allowed here and are converted to their compatible integer types.
1015 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1016 preferably a non-Boolean type as the common type. */
1017 tree
common_type(tree t1,tree t2)1018 common_type (tree t1, tree t2)
1019 {
1020 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1021 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1022 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1023 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1024
1025 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1026 if (TREE_CODE (t1) == BOOLEAN_TYPE
1027 && TREE_CODE (t2) == BOOLEAN_TYPE)
1028 return boolean_type_node;
1029
1030 /* If either type is BOOLEAN_TYPE, then return the other. */
1031 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1032 return t2;
1033 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1034 return t1;
1035
1036 return c_common_type (t1, t2);
1037 }
1038
1039 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1040 or various other operations. Return 2 if they are compatible
1041 but a warning may be needed if you use them together. */
1042
1043 int
comptypes(tree type1,tree type2)1044 comptypes (tree type1, tree type2)
1045 {
1046 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1047 int val;
1048
1049 val = comptypes_internal (type1, type2, NULL, NULL);
1050 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1051
1052 return val;
1053 }
1054
1055 /* Like comptypes, but if it returns non-zero because enum and int are
1056 compatible, it sets *ENUM_AND_INT_P to true. */
1057
1058 static int
comptypes_check_enum_int(tree type1,tree type2,bool * enum_and_int_p)1059 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1060 {
1061 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1062 int val;
1063
1064 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1065 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1066
1067 return val;
1068 }
1069
1070 /* Like comptypes, but if it returns nonzero for different types, it
1071 sets *DIFFERENT_TYPES_P to true. */
1072
1073 int
comptypes_check_different_types(tree type1,tree type2,bool * different_types_p)1074 comptypes_check_different_types (tree type1, tree type2,
1075 bool *different_types_p)
1076 {
1077 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1078 int val;
1079
1080 val = comptypes_internal (type1, type2, NULL, different_types_p);
1081 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1082
1083 return val;
1084 }
1085
1086 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1087 or various other operations. Return 2 if they are compatible
1088 but a warning may be needed if you use them together. If
1089 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1090 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1091 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1092 NULL, and the types are compatible but different enough not to be
1093 permitted in C11 typedef redeclarations, then this sets
1094 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1095 false, but may or may not be set if the types are incompatible.
1096 This differs from comptypes, in that we don't free the seen
1097 types. */
1098
1099 static int
comptypes_internal(const_tree type1,const_tree type2,bool * enum_and_int_p,bool * different_types_p)1100 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1101 bool *different_types_p)
1102 {
1103 const_tree t1 = type1;
1104 const_tree t2 = type2;
1105 int attrval, val;
1106
1107 /* Suppress errors caused by previously reported errors. */
1108
1109 if (t1 == t2 || !t1 || !t2
1110 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1111 return 1;
1112
1113 /* Enumerated types are compatible with integer types, but this is
1114 not transitive: two enumerated types in the same translation unit
1115 are compatible with each other only if they are the same type. */
1116
1117 if (TREE_CODE (t1) == ENUMERAL_TYPE
1118 && COMPLETE_TYPE_P (t1)
1119 && TREE_CODE (t2) != ENUMERAL_TYPE)
1120 {
1121 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1122 if (TREE_CODE (t2) != VOID_TYPE)
1123 {
1124 if (enum_and_int_p != NULL)
1125 *enum_and_int_p = true;
1126 if (different_types_p != NULL)
1127 *different_types_p = true;
1128 }
1129 }
1130 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1131 && COMPLETE_TYPE_P (t2)
1132 && TREE_CODE (t1) != ENUMERAL_TYPE)
1133 {
1134 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1135 if (TREE_CODE (t1) != VOID_TYPE)
1136 {
1137 if (enum_and_int_p != NULL)
1138 *enum_and_int_p = true;
1139 if (different_types_p != NULL)
1140 *different_types_p = true;
1141 }
1142 }
1143
1144 if (t1 == t2)
1145 return 1;
1146
1147 /* Different classes of types can't be compatible. */
1148
1149 if (TREE_CODE (t1) != TREE_CODE (t2))
1150 return 0;
1151
1152 /* Qualifiers must match. C99 6.7.3p9 */
1153
1154 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1155 return 0;
1156
1157 /* Allow for two different type nodes which have essentially the same
1158 definition. Note that we already checked for equality of the type
1159 qualifiers (just above). */
1160
1161 if (TREE_CODE (t1) != ARRAY_TYPE
1162 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1163 return 1;
1164
1165 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1166 if (!(attrval = comp_type_attributes (t1, t2)))
1167 return 0;
1168
1169 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1170 val = 0;
1171
1172 switch (TREE_CODE (t1))
1173 {
1174 case INTEGER_TYPE:
1175 case FIXED_POINT_TYPE:
1176 case REAL_TYPE:
1177 /* With these nodes, we can't determine type equivalence by
1178 looking at what is stored in the nodes themselves, because
1179 two nodes might have different TYPE_MAIN_VARIANTs but still
1180 represent the same type. For example, wchar_t and int could
1181 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1182 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1183 and are distinct types. On the other hand, int and the
1184 following typedef
1185
1186 typedef int INT __attribute((may_alias));
1187
1188 have identical properties, different TYPE_MAIN_VARIANTs, but
1189 represent the same type. The canonical type system keeps
1190 track of equivalence in this case, so we fall back on it. */
1191 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1192
1193 case POINTER_TYPE:
1194 /* Do not remove mode information. */
1195 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1196 break;
1197 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1198 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1199 enum_and_int_p, different_types_p));
1200 break;
1201
1202 case FUNCTION_TYPE:
1203 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1204 different_types_p);
1205 break;
1206
1207 case ARRAY_TYPE:
1208 {
1209 tree d1 = TYPE_DOMAIN (t1);
1210 tree d2 = TYPE_DOMAIN (t2);
1211 bool d1_variable, d2_variable;
1212 bool d1_zero, d2_zero;
1213 val = 1;
1214
1215 /* Target types must match incl. qualifiers. */
1216 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1217 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1218 enum_and_int_p,
1219 different_types_p)) == 0)
1220 return 0;
1221
1222 if (different_types_p != NULL
1223 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1224 *different_types_p = true;
1225 /* Sizes must match unless one is missing or variable. */
1226 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1227 break;
1228
1229 d1_zero = !TYPE_MAX_VALUE (d1);
1230 d2_zero = !TYPE_MAX_VALUE (d2);
1231
1232 d1_variable = (!d1_zero
1233 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1234 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1235 d2_variable = (!d2_zero
1236 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1237 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1238 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1239 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1240
1241 if (different_types_p != NULL
1242 && d1_variable != d2_variable)
1243 *different_types_p = true;
1244 if (d1_variable || d2_variable)
1245 break;
1246 if (d1_zero && d2_zero)
1247 break;
1248 if (d1_zero || d2_zero
1249 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1250 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1251 val = 0;
1252
1253 break;
1254 }
1255
1256 case ENUMERAL_TYPE:
1257 case RECORD_TYPE:
1258 case UNION_TYPE:
1259 if (val != 1 && !same_translation_unit_p (t1, t2))
1260 {
1261 tree a1 = TYPE_ATTRIBUTES (t1);
1262 tree a2 = TYPE_ATTRIBUTES (t2);
1263
1264 if (! attribute_list_contained (a1, a2)
1265 && ! attribute_list_contained (a2, a1))
1266 break;
1267
1268 if (attrval != 2)
1269 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1270 different_types_p);
1271 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1272 different_types_p);
1273 }
1274 break;
1275
1276 case VECTOR_TYPE:
1277 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1278 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1279 enum_and_int_p, different_types_p));
1280 break;
1281
1282 default:
1283 break;
1284 }
1285 return attrval == 2 && val == 1 ? 2 : val;
1286 }
1287
1288 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1289 their qualifiers, except for named address spaces. If the pointers point to
1290 different named addresses, then we must determine if one address space is a
1291 subset of the other. */
1292
1293 static int
comp_target_types(location_t location,tree ttl,tree ttr)1294 comp_target_types (location_t location, tree ttl, tree ttr)
1295 {
1296 int val;
1297 int val_ped;
1298 tree mvl = TREE_TYPE (ttl);
1299 tree mvr = TREE_TYPE (ttr);
1300 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1301 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1302 addr_space_t as_common;
1303 bool enum_and_int_p;
1304
1305 /* Fail if pointers point to incompatible address spaces. */
1306 if (!addr_space_superset (asl, asr, &as_common))
1307 return 0;
1308
1309 /* For pedantic record result of comptypes on arrays before losing
1310 qualifiers on the element type below. */
1311 val_ped = 1;
1312
1313 if (TREE_CODE (mvl) == ARRAY_TYPE
1314 && TREE_CODE (mvr) == ARRAY_TYPE)
1315 val_ped = comptypes (mvl, mvr);
1316
1317 /* Qualifiers on element types of array types that are
1318 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1319
1320 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1321 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1322 : TYPE_MAIN_VARIANT (mvl));
1323
1324 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1325 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1326 : TYPE_MAIN_VARIANT (mvr));
1327
1328 enum_and_int_p = false;
1329 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1330
1331 if (val == 1 && val_ped != 1)
1332 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1333 "in ISO C before C2X");
1334
1335 if (val == 2)
1336 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1337
1338 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1339 warning_at (location, OPT_Wc___compat,
1340 "pointer target types incompatible in C++");
1341
1342 return val;
1343 }
1344
1345 /* Subroutines of `comptypes'. */
1346
1347 /* Determine whether two trees derive from the same translation unit.
1348 If the CONTEXT chain ends in a null, that tree's context is still
1349 being parsed, so if two trees have context chains ending in null,
1350 they're in the same translation unit. */
1351
1352 bool
same_translation_unit_p(const_tree t1,const_tree t2)1353 same_translation_unit_p (const_tree t1, const_tree t2)
1354 {
1355 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1356 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1357 {
1358 case tcc_declaration:
1359 t1 = DECL_CONTEXT (t1); break;
1360 case tcc_type:
1361 t1 = TYPE_CONTEXT (t1); break;
1362 case tcc_exceptional:
1363 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1364 default: gcc_unreachable ();
1365 }
1366
1367 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1368 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1369 {
1370 case tcc_declaration:
1371 t2 = DECL_CONTEXT (t2); break;
1372 case tcc_type:
1373 t2 = TYPE_CONTEXT (t2); break;
1374 case tcc_exceptional:
1375 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1376 default: gcc_unreachable ();
1377 }
1378
1379 return t1 == t2;
1380 }
1381
1382 /* Allocate the seen two types, assuming that they are compatible. */
1383
1384 static struct tagged_tu_seen_cache *
alloc_tagged_tu_seen_cache(const_tree t1,const_tree t2)1385 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1386 {
1387 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1388 tu->next = tagged_tu_seen_base;
1389 tu->t1 = t1;
1390 tu->t2 = t2;
1391
1392 tagged_tu_seen_base = tu;
1393
1394 /* The C standard says that two structures in different translation
1395 units are compatible with each other only if the types of their
1396 fields are compatible (among other things). We assume that they
1397 are compatible until proven otherwise when building the cache.
1398 An example where this can occur is:
1399 struct a
1400 {
1401 struct a *next;
1402 };
1403 If we are comparing this against a similar struct in another TU,
1404 and did not assume they were compatible, we end up with an infinite
1405 loop. */
1406 tu->val = 1;
1407 return tu;
1408 }
1409
1410 /* Free the seen types until we get to TU_TIL. */
1411
1412 static void
free_all_tagged_tu_seen_up_to(const struct tagged_tu_seen_cache * tu_til)1413 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1414 {
1415 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1416 while (tu != tu_til)
1417 {
1418 const struct tagged_tu_seen_cache *const tu1
1419 = (const struct tagged_tu_seen_cache *) tu;
1420 tu = tu1->next;
1421 XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1422 }
1423 tagged_tu_seen_base = tu_til;
1424 }
1425
1426 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1427 compatible. If the two types are not the same (which has been
1428 checked earlier), this can only happen when multiple translation
1429 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1430 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1431 comptypes_internal. */
1432
1433 static int
tagged_types_tu_compatible_p(const_tree t1,const_tree t2,bool * enum_and_int_p,bool * different_types_p)1434 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1435 bool *enum_and_int_p, bool *different_types_p)
1436 {
1437 tree s1, s2;
1438 bool needs_warning = false;
1439
1440 /* We have to verify that the tags of the types are the same. This
1441 is harder than it looks because this may be a typedef, so we have
1442 to go look at the original type. It may even be a typedef of a
1443 typedef...
1444 In the case of compiler-created builtin structs the TYPE_DECL
1445 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1446 while (TYPE_NAME (t1)
1447 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1448 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1449 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1450
1451 while (TYPE_NAME (t2)
1452 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1453 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1454 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1455
1456 /* C90 didn't have the requirement that the two tags be the same. */
1457 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1458 return 0;
1459
1460 /* C90 didn't say what happened if one or both of the types were
1461 incomplete; we choose to follow C99 rules here, which is that they
1462 are compatible. */
1463 if (TYPE_SIZE (t1) == NULL
1464 || TYPE_SIZE (t2) == NULL)
1465 return 1;
1466
1467 {
1468 const struct tagged_tu_seen_cache * tts_i;
1469 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1470 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1471 return tts_i->val;
1472 }
1473
1474 switch (TREE_CODE (t1))
1475 {
1476 case ENUMERAL_TYPE:
1477 {
1478 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1479 /* Speed up the case where the type values are in the same order. */
1480 tree tv1 = TYPE_VALUES (t1);
1481 tree tv2 = TYPE_VALUES (t2);
1482
1483 if (tv1 == tv2)
1484 {
1485 return 1;
1486 }
1487
1488 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1489 {
1490 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1491 break;
1492 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1493 {
1494 tu->val = 0;
1495 return 0;
1496 }
1497 }
1498
1499 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1500 {
1501 return 1;
1502 }
1503 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1504 {
1505 tu->val = 0;
1506 return 0;
1507 }
1508
1509 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1510 {
1511 tu->val = 0;
1512 return 0;
1513 }
1514
1515 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1516 {
1517 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1518 if (s2 == NULL
1519 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1520 {
1521 tu->val = 0;
1522 return 0;
1523 }
1524 }
1525 return 1;
1526 }
1527
1528 case UNION_TYPE:
1529 {
1530 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1531 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1532 {
1533 tu->val = 0;
1534 return 0;
1535 }
1536
1537 /* Speed up the common case where the fields are in the same order. */
1538 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1539 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1540 {
1541 int result;
1542
1543 if (DECL_NAME (s1) != DECL_NAME (s2))
1544 break;
1545 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1546 enum_and_int_p, different_types_p);
1547
1548 if (result != 1 && !DECL_NAME (s1))
1549 break;
1550 if (result == 0)
1551 {
1552 tu->val = 0;
1553 return 0;
1554 }
1555 if (result == 2)
1556 needs_warning = true;
1557
1558 if (TREE_CODE (s1) == FIELD_DECL
1559 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1560 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1561 {
1562 tu->val = 0;
1563 return 0;
1564 }
1565 }
1566 if (!s1 && !s2)
1567 {
1568 tu->val = needs_warning ? 2 : 1;
1569 return tu->val;
1570 }
1571
1572 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1573 {
1574 bool ok = false;
1575
1576 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1577 if (DECL_NAME (s1) == DECL_NAME (s2))
1578 {
1579 int result;
1580
1581 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1582 enum_and_int_p,
1583 different_types_p);
1584
1585 if (result != 1 && !DECL_NAME (s1))
1586 continue;
1587 if (result == 0)
1588 {
1589 tu->val = 0;
1590 return 0;
1591 }
1592 if (result == 2)
1593 needs_warning = true;
1594
1595 if (TREE_CODE (s1) == FIELD_DECL
1596 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1597 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1598 break;
1599
1600 ok = true;
1601 break;
1602 }
1603 if (!ok)
1604 {
1605 tu->val = 0;
1606 return 0;
1607 }
1608 }
1609 tu->val = needs_warning ? 2 : 10;
1610 return tu->val;
1611 }
1612
1613 case RECORD_TYPE:
1614 {
1615 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1616
1617 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1618 s1 && s2;
1619 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1620 {
1621 int result;
1622 if (TREE_CODE (s1) != TREE_CODE (s2)
1623 || DECL_NAME (s1) != DECL_NAME (s2))
1624 break;
1625 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1626 enum_and_int_p, different_types_p);
1627 if (result == 0)
1628 break;
1629 if (result == 2)
1630 needs_warning = true;
1631
1632 if (TREE_CODE (s1) == FIELD_DECL
1633 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1634 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1635 break;
1636 }
1637 if (s1 && s2)
1638 tu->val = 0;
1639 else
1640 tu->val = needs_warning ? 2 : 1;
1641 return tu->val;
1642 }
1643
1644 default:
1645 gcc_unreachable ();
1646 }
1647 }
1648
1649 /* Return 1 if two function types F1 and F2 are compatible.
1650 If either type specifies no argument types,
1651 the other must specify a fixed number of self-promoting arg types.
1652 Otherwise, if one type specifies only the number of arguments,
1653 the other must specify that number of self-promoting arg types.
1654 Otherwise, the argument types must match.
1655 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1656
1657 static int
function_types_compatible_p(const_tree f1,const_tree f2,bool * enum_and_int_p,bool * different_types_p)1658 function_types_compatible_p (const_tree f1, const_tree f2,
1659 bool *enum_and_int_p, bool *different_types_p)
1660 {
1661 tree args1, args2;
1662 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1663 int val = 1;
1664 int val1;
1665 tree ret1, ret2;
1666
1667 ret1 = TREE_TYPE (f1);
1668 ret2 = TREE_TYPE (f2);
1669
1670 /* 'volatile' qualifiers on a function's return type used to mean
1671 the function is noreturn. */
1672 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1673 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1674 if (TYPE_VOLATILE (ret1))
1675 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1676 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1677 if (TYPE_VOLATILE (ret2))
1678 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1679 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1680 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1681 if (val == 0)
1682 return 0;
1683
1684 args1 = TYPE_ARG_TYPES (f1);
1685 args2 = TYPE_ARG_TYPES (f2);
1686
1687 if (different_types_p != NULL
1688 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1689 *different_types_p = true;
1690
1691 /* An unspecified parmlist matches any specified parmlist
1692 whose argument types don't need default promotions. */
1693
1694 if (args1 == NULL_TREE)
1695 {
1696 if (!self_promoting_args_p (args2))
1697 return 0;
1698 /* If one of these types comes from a non-prototype fn definition,
1699 compare that with the other type's arglist.
1700 If they don't match, ask for a warning (but no error). */
1701 if (TYPE_ACTUAL_ARG_TYPES (f1)
1702 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1703 enum_and_int_p, different_types_p) != 1)
1704 val = 2;
1705 return val;
1706 }
1707 if (args2 == NULL_TREE)
1708 {
1709 if (!self_promoting_args_p (args1))
1710 return 0;
1711 if (TYPE_ACTUAL_ARG_TYPES (f2)
1712 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1713 enum_and_int_p, different_types_p) != 1)
1714 val = 2;
1715 return val;
1716 }
1717
1718 /* Both types have argument lists: compare them and propagate results. */
1719 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1720 different_types_p);
1721 return val1 != 1 ? val1 : val;
1722 }
1723
1724 /* Check two lists of types for compatibility, returning 0 for
1725 incompatible, 1 for compatible, or 2 for compatible with
1726 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1727 comptypes_internal. */
1728
1729 static int
type_lists_compatible_p(const_tree args1,const_tree args2,bool * enum_and_int_p,bool * different_types_p)1730 type_lists_compatible_p (const_tree args1, const_tree args2,
1731 bool *enum_and_int_p, bool *different_types_p)
1732 {
1733 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1734 int val = 1;
1735 int newval = 0;
1736
1737 while (1)
1738 {
1739 tree a1, mv1, a2, mv2;
1740 if (args1 == NULL_TREE && args2 == NULL_TREE)
1741 return val;
1742 /* If one list is shorter than the other,
1743 they fail to match. */
1744 if (args1 == NULL_TREE || args2 == NULL_TREE)
1745 return 0;
1746 mv1 = a1 = TREE_VALUE (args1);
1747 mv2 = a2 = TREE_VALUE (args2);
1748 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1749 mv1 = (TYPE_ATOMIC (mv1)
1750 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1751 TYPE_QUAL_ATOMIC)
1752 : TYPE_MAIN_VARIANT (mv1));
1753 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1754 mv2 = (TYPE_ATOMIC (mv2)
1755 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1756 TYPE_QUAL_ATOMIC)
1757 : TYPE_MAIN_VARIANT (mv2));
1758 /* A null pointer instead of a type
1759 means there is supposed to be an argument
1760 but nothing is specified about what type it has.
1761 So match anything that self-promotes. */
1762 if (different_types_p != NULL
1763 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1764 *different_types_p = true;
1765 if (a1 == NULL_TREE)
1766 {
1767 if (c_type_promotes_to (a2) != a2)
1768 return 0;
1769 }
1770 else if (a2 == NULL_TREE)
1771 {
1772 if (c_type_promotes_to (a1) != a1)
1773 return 0;
1774 }
1775 /* If one of the lists has an error marker, ignore this arg. */
1776 else if (TREE_CODE (a1) == ERROR_MARK
1777 || TREE_CODE (a2) == ERROR_MARK)
1778 ;
1779 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1780 different_types_p)))
1781 {
1782 if (different_types_p != NULL)
1783 *different_types_p = true;
1784 /* Allow wait (union {union wait *u; int *i} *)
1785 and wait (union wait *) to be compatible. */
1786 if (TREE_CODE (a1) == UNION_TYPE
1787 && (TYPE_NAME (a1) == NULL_TREE
1788 || TYPE_TRANSPARENT_AGGR (a1))
1789 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1790 && tree_int_cst_equal (TYPE_SIZE (a1),
1791 TYPE_SIZE (a2)))
1792 {
1793 tree memb;
1794 for (memb = TYPE_FIELDS (a1);
1795 memb; memb = DECL_CHAIN (memb))
1796 {
1797 tree mv3 = TREE_TYPE (memb);
1798 if (mv3 && mv3 != error_mark_node
1799 && TREE_CODE (mv3) != ARRAY_TYPE)
1800 mv3 = (TYPE_ATOMIC (mv3)
1801 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1802 TYPE_QUAL_ATOMIC)
1803 : TYPE_MAIN_VARIANT (mv3));
1804 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1805 different_types_p))
1806 break;
1807 }
1808 if (memb == NULL_TREE)
1809 return 0;
1810 }
1811 else if (TREE_CODE (a2) == UNION_TYPE
1812 && (TYPE_NAME (a2) == NULL_TREE
1813 || TYPE_TRANSPARENT_AGGR (a2))
1814 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1815 && tree_int_cst_equal (TYPE_SIZE (a2),
1816 TYPE_SIZE (a1)))
1817 {
1818 tree memb;
1819 for (memb = TYPE_FIELDS (a2);
1820 memb; memb = DECL_CHAIN (memb))
1821 {
1822 tree mv3 = TREE_TYPE (memb);
1823 if (mv3 && mv3 != error_mark_node
1824 && TREE_CODE (mv3) != ARRAY_TYPE)
1825 mv3 = (TYPE_ATOMIC (mv3)
1826 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1827 TYPE_QUAL_ATOMIC)
1828 : TYPE_MAIN_VARIANT (mv3));
1829 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1830 different_types_p))
1831 break;
1832 }
1833 if (memb == NULL_TREE)
1834 return 0;
1835 }
1836 else
1837 return 0;
1838 }
1839
1840 /* comptypes said ok, but record if it said to warn. */
1841 if (newval > val)
1842 val = newval;
1843
1844 args1 = TREE_CHAIN (args1);
1845 args2 = TREE_CHAIN (args2);
1846 }
1847 }
1848
1849 /* Compute the size to increment a pointer by. When a function type or void
1850 type or incomplete type is passed, size_one_node is returned.
1851 This function does not emit any diagnostics; the caller is responsible
1852 for that. */
1853
1854 static tree
c_size_in_bytes(const_tree type)1855 c_size_in_bytes (const_tree type)
1856 {
1857 enum tree_code code = TREE_CODE (type);
1858
1859 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1860 || !COMPLETE_TYPE_P (type))
1861 return size_one_node;
1862
1863 /* Convert in case a char is more than one unit. */
1864 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1865 size_int (TYPE_PRECISION (char_type_node)
1866 / BITS_PER_UNIT));
1867 }
1868
1869 /* Return either DECL or its known constant value (if it has one). */
1870
1871 tree
decl_constant_value_1(tree decl,bool in_init)1872 decl_constant_value_1 (tree decl, bool in_init)
1873 {
1874 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1875 TREE_CODE (decl) != PARM_DECL
1876 && !TREE_THIS_VOLATILE (decl)
1877 && TREE_READONLY (decl)
1878 && DECL_INITIAL (decl) != NULL_TREE
1879 && !error_operand_p (DECL_INITIAL (decl))
1880 /* This is invalid if initial value is not constant.
1881 If it has either a function call, a memory reference,
1882 or a variable, then re-evaluating it could give different results. */
1883 && TREE_CONSTANT (DECL_INITIAL (decl))
1884 /* Check for cases where this is sub-optimal, even though valid. */
1885 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1886 return DECL_INITIAL (decl);
1887 return decl;
1888 }
1889
1890 /* Return either DECL or its known constant value (if it has one).
1891 Like the above, but always return decl outside of functions. */
1892
1893 tree
decl_constant_value(tree decl)1894 decl_constant_value (tree decl)
1895 {
1896 /* Don't change a variable array bound or initial value to a constant
1897 in a place where a variable is invalid. */
1898 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1899 }
1900
1901 /* Convert the array expression EXP to a pointer. */
1902 static tree
array_to_pointer_conversion(location_t loc,tree exp)1903 array_to_pointer_conversion (location_t loc, tree exp)
1904 {
1905 tree orig_exp = exp;
1906 tree type = TREE_TYPE (exp);
1907 tree adr;
1908 tree restype = TREE_TYPE (type);
1909 tree ptrtype;
1910
1911 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1912
1913 STRIP_TYPE_NOPS (exp);
1914
1915 copy_warning (exp, orig_exp);
1916
1917 ptrtype = build_pointer_type (restype);
1918
1919 if (INDIRECT_REF_P (exp))
1920 return convert (ptrtype, TREE_OPERAND (exp, 0));
1921
1922 /* In C++ array compound literals are temporary objects unless they are
1923 const or appear in namespace scope, so they are destroyed too soon
1924 to use them for much of anything (c++/53220). */
1925 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1926 {
1927 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1928 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1929 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1930 "converting an array compound literal to a pointer "
1931 "is ill-formed in C++");
1932 }
1933
1934 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1935 return convert (ptrtype, adr);
1936 }
1937
1938 /* Convert the function expression EXP to a pointer. */
1939 static tree
function_to_pointer_conversion(location_t loc,tree exp)1940 function_to_pointer_conversion (location_t loc, tree exp)
1941 {
1942 tree orig_exp = exp;
1943
1944 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1945
1946 STRIP_TYPE_NOPS (exp);
1947
1948 copy_warning (exp, orig_exp);
1949
1950 return build_unary_op (loc, ADDR_EXPR, exp, false);
1951 }
1952
1953 /* Mark EXP as read, not just set, for set but not used -Wunused
1954 warning purposes. */
1955
1956 void
mark_exp_read(tree exp)1957 mark_exp_read (tree exp)
1958 {
1959 switch (TREE_CODE (exp))
1960 {
1961 case VAR_DECL:
1962 case PARM_DECL:
1963 DECL_READ_P (exp) = 1;
1964 break;
1965 case ARRAY_REF:
1966 case COMPONENT_REF:
1967 case MODIFY_EXPR:
1968 case REALPART_EXPR:
1969 case IMAGPART_EXPR:
1970 CASE_CONVERT:
1971 case ADDR_EXPR:
1972 case VIEW_CONVERT_EXPR:
1973 mark_exp_read (TREE_OPERAND (exp, 0));
1974 break;
1975 case COMPOUND_EXPR:
1976 /* Pattern match what build_atomic_assign produces with modifycode
1977 NOP_EXPR. */
1978 if (VAR_P (TREE_OPERAND (exp, 1))
1979 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1980 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1981 {
1982 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1983 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1984 if (TREE_CODE (t1) == TARGET_EXPR
1985 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1986 && TREE_CODE (t2) == CALL_EXPR)
1987 {
1988 tree fndecl = get_callee_fndecl (t2);
1989 tree arg = NULL_TREE;
1990 if (fndecl
1991 && TREE_CODE (fndecl) == FUNCTION_DECL
1992 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1993 && call_expr_nargs (t2) >= 2)
1994 switch (DECL_FUNCTION_CODE (fndecl))
1995 {
1996 case BUILT_IN_ATOMIC_STORE:
1997 arg = CALL_EXPR_ARG (t2, 1);
1998 break;
1999 case BUILT_IN_ATOMIC_STORE_1:
2000 case BUILT_IN_ATOMIC_STORE_2:
2001 case BUILT_IN_ATOMIC_STORE_4:
2002 case BUILT_IN_ATOMIC_STORE_8:
2003 case BUILT_IN_ATOMIC_STORE_16:
2004 arg = CALL_EXPR_ARG (t2, 0);
2005 break;
2006 default:
2007 break;
2008 }
2009 if (arg)
2010 {
2011 STRIP_NOPS (arg);
2012 if (TREE_CODE (arg) == ADDR_EXPR
2013 && DECL_P (TREE_OPERAND (arg, 0))
2014 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2015 mark_exp_read (TREE_OPERAND (arg, 0));
2016 }
2017 }
2018 }
2019 /* FALLTHRU */
2020 case C_MAYBE_CONST_EXPR:
2021 mark_exp_read (TREE_OPERAND (exp, 1));
2022 break;
2023 default:
2024 break;
2025 }
2026 }
2027
2028 /* Perform the default conversion of arrays and functions to pointers.
2029 Return the result of converting EXP. For any other expression, just
2030 return EXP.
2031
2032 LOC is the location of the expression. */
2033
2034 struct c_expr
default_function_array_conversion(location_t loc,struct c_expr exp)2035 default_function_array_conversion (location_t loc, struct c_expr exp)
2036 {
2037 tree orig_exp = exp.value;
2038 tree type = TREE_TYPE (exp.value);
2039 enum tree_code code = TREE_CODE (type);
2040
2041 switch (code)
2042 {
2043 case ARRAY_TYPE:
2044 {
2045 bool not_lvalue = false;
2046 bool lvalue_array_p;
2047
2048 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2049 || CONVERT_EXPR_P (exp.value))
2050 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2051 {
2052 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2053 not_lvalue = true;
2054 exp.value = TREE_OPERAND (exp.value, 0);
2055 }
2056
2057 copy_warning (exp.value, orig_exp);
2058
2059 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2060 if (!flag_isoc99 && !lvalue_array_p)
2061 {
2062 /* Before C99, non-lvalue arrays do not decay to pointers.
2063 Normally, using such an array would be invalid; but it can
2064 be used correctly inside sizeof or as a statement expression.
2065 Thus, do not give an error here; an error will result later. */
2066 return exp;
2067 }
2068
2069 exp.value = array_to_pointer_conversion (loc, exp.value);
2070 }
2071 break;
2072 case FUNCTION_TYPE:
2073 exp.value = function_to_pointer_conversion (loc, exp.value);
2074 break;
2075 default:
2076 break;
2077 }
2078
2079 return exp;
2080 }
2081
2082 struct c_expr
default_function_array_read_conversion(location_t loc,struct c_expr exp)2083 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2084 {
2085 mark_exp_read (exp.value);
2086 return default_function_array_conversion (loc, exp);
2087 }
2088
2089 /* Return whether EXPR should be treated as an atomic lvalue for the
2090 purposes of load and store handling. */
2091
2092 static bool
really_atomic_lvalue(tree expr)2093 really_atomic_lvalue (tree expr)
2094 {
2095 if (error_operand_p (expr))
2096 return false;
2097 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2098 return false;
2099 if (!lvalue_p (expr))
2100 return false;
2101
2102 /* Ignore _Atomic on register variables, since their addresses can't
2103 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2104 sequences wouldn't work. Ignore _Atomic on structures containing
2105 bit-fields, since accessing elements of atomic structures or
2106 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2107 it's undefined at translation time or execution time, and the
2108 normal atomic sequences again wouldn't work. */
2109 while (handled_component_p (expr))
2110 {
2111 if (TREE_CODE (expr) == COMPONENT_REF
2112 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2113 return false;
2114 expr = TREE_OPERAND (expr, 0);
2115 }
2116 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2117 return false;
2118 return true;
2119 }
2120
2121 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2122 including converting functions and arrays to pointers if CONVERT_P.
2123 If READ_P, also mark the expression as having been read. */
2124
2125 struct c_expr
convert_lvalue_to_rvalue(location_t loc,struct c_expr exp,bool convert_p,bool read_p)2126 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2127 bool convert_p, bool read_p)
2128 {
2129 if (read_p)
2130 mark_exp_read (exp.value);
2131 if (convert_p)
2132 exp = default_function_array_conversion (loc, exp);
2133 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2134 exp.value = require_complete_type (loc, exp.value);
2135 if (really_atomic_lvalue (exp.value))
2136 {
2137 vec<tree, va_gc> *params;
2138 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2139 tree expr_type = TREE_TYPE (exp.value);
2140 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2141 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2142
2143 gcc_assert (TYPE_ATOMIC (expr_type));
2144
2145 /* Expansion of a generic atomic load may require an addition
2146 element, so allocate enough to prevent a resize. */
2147 vec_alloc (params, 4);
2148
2149 /* Remove the qualifiers for the rest of the expressions and
2150 create the VAL temp variable to hold the RHS. */
2151 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2152 tmp = create_tmp_var_raw (nonatomic_type);
2153 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2154 TREE_ADDRESSABLE (tmp) = 1;
2155 /* Do not disable warnings for TMP even though it's artificial.
2156 -Winvalid-memory-model depends on it. */
2157
2158 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2159 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2160 params->quick_push (expr_addr);
2161 params->quick_push (tmp_addr);
2162 params->quick_push (seq_cst);
2163 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2164
2165 /* EXPR is always read. */
2166 mark_exp_read (exp.value);
2167
2168 /* Return tmp which contains the value loaded. */
2169 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2170 NULL_TREE, NULL_TREE);
2171 }
2172 if (convert_p && !error_operand_p (exp.value)
2173 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2174 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2175 return exp;
2176 }
2177
2178 /* EXP is an expression of integer type. Apply the integer promotions
2179 to it and return the promoted value. */
2180
2181 tree
perform_integral_promotions(tree exp)2182 perform_integral_promotions (tree exp)
2183 {
2184 tree type = TREE_TYPE (exp);
2185 enum tree_code code = TREE_CODE (type);
2186
2187 gcc_assert (INTEGRAL_TYPE_P (type));
2188
2189 /* Normally convert enums to int,
2190 but convert wide enums to something wider. */
2191 if (code == ENUMERAL_TYPE)
2192 {
2193 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2194 TYPE_PRECISION (integer_type_node)),
2195 ((TYPE_PRECISION (type)
2196 >= TYPE_PRECISION (integer_type_node))
2197 && TYPE_UNSIGNED (type)));
2198
2199 return convert (type, exp);
2200 }
2201
2202 /* ??? This should no longer be needed now bit-fields have their
2203 proper types. */
2204 if (TREE_CODE (exp) == COMPONENT_REF
2205 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2206 /* If it's thinner than an int, promote it like a
2207 c_promoting_integer_type_p, otherwise leave it alone. */
2208 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2209 TYPE_PRECISION (integer_type_node)) < 0)
2210 return convert (integer_type_node, exp);
2211
2212 if (c_promoting_integer_type_p (type))
2213 {
2214 /* Preserve unsignedness if not really getting any wider. */
2215 if (TYPE_UNSIGNED (type)
2216 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2217 return convert (unsigned_type_node, exp);
2218
2219 return convert (integer_type_node, exp);
2220 }
2221
2222 return exp;
2223 }
2224
2225
2226 /* Perform default promotions for C data used in expressions.
2227 Enumeral types or short or char are converted to int.
2228 In addition, manifest constants symbols are replaced by their values. */
2229
2230 tree
default_conversion(tree exp)2231 default_conversion (tree exp)
2232 {
2233 tree orig_exp;
2234 tree type = TREE_TYPE (exp);
2235 enum tree_code code = TREE_CODE (type);
2236 tree promoted_type;
2237
2238 mark_exp_read (exp);
2239
2240 /* Functions and arrays have been converted during parsing. */
2241 gcc_assert (code != FUNCTION_TYPE);
2242 if (code == ARRAY_TYPE)
2243 return exp;
2244
2245 /* Constants can be used directly unless they're not loadable. */
2246 if (TREE_CODE (exp) == CONST_DECL)
2247 exp = DECL_INITIAL (exp);
2248
2249 /* Strip no-op conversions. */
2250 orig_exp = exp;
2251 STRIP_TYPE_NOPS (exp);
2252
2253 copy_warning (exp, orig_exp);
2254
2255 if (code == VOID_TYPE)
2256 {
2257 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2258 "void value not ignored as it ought to be");
2259 return error_mark_node;
2260 }
2261
2262 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2263 if (exp == error_mark_node)
2264 return error_mark_node;
2265
2266 promoted_type = targetm.promoted_type (type);
2267 if (promoted_type)
2268 return convert (promoted_type, exp);
2269
2270 if (INTEGRAL_TYPE_P (type))
2271 return perform_integral_promotions (exp);
2272
2273 return exp;
2274 }
2275
2276 /* Look up COMPONENT in a structure or union TYPE.
2277
2278 If the component name is not found, returns NULL_TREE. Otherwise,
2279 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2280 stepping down the chain to the component, which is in the last
2281 TREE_VALUE of the list. Normally the list is of length one, but if
2282 the component is embedded within (nested) anonymous structures or
2283 unions, the list steps down the chain to the component. */
2284
2285 static tree
lookup_field(tree type,tree component)2286 lookup_field (tree type, tree component)
2287 {
2288 tree field;
2289
2290 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2291 to the field elements. Use a binary search on this array to quickly
2292 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2293 will always be set for structures which have many elements.
2294
2295 Duplicate field checking replaces duplicates with NULL_TREE so
2296 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2297 case just iterate using DECL_CHAIN. */
2298
2299 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2300 && !seen_error ())
2301 {
2302 int bot, top, half;
2303 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2304
2305 field = TYPE_FIELDS (type);
2306 bot = 0;
2307 top = TYPE_LANG_SPECIFIC (type)->s->len;
2308 while (top - bot > 1)
2309 {
2310 half = (top - bot + 1) >> 1;
2311 field = field_array[bot+half];
2312
2313 if (DECL_NAME (field) == NULL_TREE)
2314 {
2315 /* Step through all anon unions in linear fashion. */
2316 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2317 {
2318 field = field_array[bot++];
2319 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2320 {
2321 tree anon = lookup_field (TREE_TYPE (field), component);
2322
2323 if (anon)
2324 return tree_cons (NULL_TREE, field, anon);
2325
2326 /* The Plan 9 compiler permits referring
2327 directly to an anonymous struct/union field
2328 using a typedef name. */
2329 if (flag_plan9_extensions
2330 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2331 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2332 == TYPE_DECL)
2333 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2334 == component))
2335 break;
2336 }
2337 }
2338
2339 /* Entire record is only anon unions. */
2340 if (bot > top)
2341 return NULL_TREE;
2342
2343 /* Restart the binary search, with new lower bound. */
2344 continue;
2345 }
2346
2347 if (DECL_NAME (field) == component)
2348 break;
2349 if (DECL_NAME (field) < component)
2350 bot += half;
2351 else
2352 top = bot + half;
2353 }
2354
2355 if (DECL_NAME (field_array[bot]) == component)
2356 field = field_array[bot];
2357 else if (DECL_NAME (field) != component)
2358 return NULL_TREE;
2359 }
2360 else
2361 {
2362 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2363 {
2364 if (DECL_NAME (field) == NULL_TREE
2365 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2366 {
2367 tree anon = lookup_field (TREE_TYPE (field), component);
2368
2369 if (anon)
2370 return tree_cons (NULL_TREE, field, anon);
2371
2372 /* The Plan 9 compiler permits referring directly to an
2373 anonymous struct/union field using a typedef
2374 name. */
2375 if (flag_plan9_extensions
2376 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2377 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2378 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2379 == component))
2380 break;
2381 }
2382
2383 if (DECL_NAME (field) == component)
2384 break;
2385 }
2386
2387 if (field == NULL_TREE)
2388 return NULL_TREE;
2389 }
2390
2391 return tree_cons (NULL_TREE, field, NULL_TREE);
2392 }
2393
2394 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2395
2396 static void
lookup_field_fuzzy_find_candidates(tree type,tree component,vec<tree> * candidates)2397 lookup_field_fuzzy_find_candidates (tree type, tree component,
2398 vec<tree> *candidates)
2399 {
2400 tree field;
2401 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2402 {
2403 if (DECL_NAME (field) == NULL_TREE
2404 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2405 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2406 candidates);
2407
2408 if (DECL_NAME (field))
2409 candidates->safe_push (DECL_NAME (field));
2410 }
2411 }
2412
2413 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2414 rather than returning a TREE_LIST for an exact match. */
2415
2416 static tree
lookup_field_fuzzy(tree type,tree component)2417 lookup_field_fuzzy (tree type, tree component)
2418 {
2419 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2420
2421 /* First, gather a list of candidates. */
2422 auto_vec <tree> candidates;
2423
2424 lookup_field_fuzzy_find_candidates (type, component,
2425 &candidates);
2426
2427 return find_closest_identifier (component, &candidates);
2428 }
2429
2430 /* Support function for build_component_ref's error-handling.
2431
2432 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2433 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2434
2435 static bool
should_suggest_deref_p(tree datum_type)2436 should_suggest_deref_p (tree datum_type)
2437 {
2438 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2439 allows "." for ptrs; we could be handling a failed attempt
2440 to access a property. */
2441 if (c_dialect_objc ())
2442 return false;
2443
2444 /* Only suggest it for pointers... */
2445 if (TREE_CODE (datum_type) != POINTER_TYPE)
2446 return false;
2447
2448 /* ...to structs/unions. */
2449 tree underlying_type = TREE_TYPE (datum_type);
2450 enum tree_code code = TREE_CODE (underlying_type);
2451 if (code == RECORD_TYPE || code == UNION_TYPE)
2452 return true;
2453 else
2454 return false;
2455 }
2456
2457 /* Make an expression to refer to the COMPONENT field of structure or
2458 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2459 location of the COMPONENT_REF. COMPONENT_LOC is the location
2460 of COMPONENT. */
2461
2462 tree
build_component_ref(location_t loc,tree datum,tree component,location_t component_loc)2463 build_component_ref (location_t loc, tree datum, tree component,
2464 location_t component_loc)
2465 {
2466 tree type = TREE_TYPE (datum);
2467 enum tree_code code = TREE_CODE (type);
2468 tree field = NULL;
2469 tree ref;
2470 bool datum_lvalue = lvalue_p (datum);
2471
2472 if (!objc_is_public (datum, component))
2473 return error_mark_node;
2474
2475 /* Detect Objective-C property syntax object.property. */
2476 if (c_dialect_objc ()
2477 && (ref = objc_maybe_build_component_ref (datum, component)))
2478 return ref;
2479
2480 /* See if there is a field or component with name COMPONENT. */
2481
2482 if (code == RECORD_TYPE || code == UNION_TYPE)
2483 {
2484 if (!COMPLETE_TYPE_P (type))
2485 {
2486 c_incomplete_type_error (loc, NULL_TREE, type);
2487 return error_mark_node;
2488 }
2489
2490 field = lookup_field (type, component);
2491
2492 if (!field)
2493 {
2494 tree guessed_id = lookup_field_fuzzy (type, component);
2495 if (guessed_id)
2496 {
2497 /* Attempt to provide a fixit replacement hint, if
2498 we have a valid range for the component. */
2499 location_t reported_loc
2500 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2501 gcc_rich_location rich_loc (reported_loc);
2502 if (component_loc != UNKNOWN_LOCATION)
2503 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2504 error_at (&rich_loc,
2505 "%qT has no member named %qE; did you mean %qE?",
2506 type, component, guessed_id);
2507 }
2508 else
2509 error_at (loc, "%qT has no member named %qE", type, component);
2510 return error_mark_node;
2511 }
2512
2513 /* Accessing elements of atomic structures or unions is undefined
2514 behavior (C11 6.5.2.3#5). */
2515 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2516 {
2517 if (code == RECORD_TYPE)
2518 warning_at (loc, 0, "accessing a member %qE of an atomic "
2519 "structure %qE", component, datum);
2520 else
2521 warning_at (loc, 0, "accessing a member %qE of an atomic "
2522 "union %qE", component, datum);
2523 }
2524
2525 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2526 This might be better solved in future the way the C++ front
2527 end does it - by giving the anonymous entities each a
2528 separate name and type, and then have build_component_ref
2529 recursively call itself. We can't do that here. */
2530 do
2531 {
2532 tree subdatum = TREE_VALUE (field);
2533 int quals;
2534 tree subtype;
2535 bool use_datum_quals;
2536
2537 if (TREE_TYPE (subdatum) == error_mark_node)
2538 return error_mark_node;
2539
2540 /* If this is an rvalue, it does not have qualifiers in C
2541 standard terms and we must avoid propagating such
2542 qualifiers down to a non-lvalue array that is then
2543 converted to a pointer. */
2544 use_datum_quals = (datum_lvalue
2545 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2546
2547 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2548 if (use_datum_quals)
2549 quals |= TYPE_QUALS (TREE_TYPE (datum));
2550 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2551
2552 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2553 NULL_TREE);
2554 SET_EXPR_LOCATION (ref, loc);
2555 if (TREE_READONLY (subdatum)
2556 || (use_datum_quals && TREE_READONLY (datum)))
2557 TREE_READONLY (ref) = 1;
2558 if (TREE_THIS_VOLATILE (subdatum)
2559 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2560 TREE_THIS_VOLATILE (ref) = 1;
2561
2562 if (TREE_UNAVAILABLE (subdatum))
2563 error_unavailable_use (subdatum, NULL_TREE);
2564 else if (TREE_DEPRECATED (subdatum))
2565 warn_deprecated_use (subdatum, NULL_TREE);
2566
2567 datum = ref;
2568
2569 field = TREE_CHAIN (field);
2570 }
2571 while (field);
2572
2573 return ref;
2574 }
2575 else if (should_suggest_deref_p (type))
2576 {
2577 /* Special-case the error message for "ptr.field" for the case
2578 where the user has confused "." vs "->". */
2579 rich_location richloc (line_table, loc);
2580 /* "loc" should be the "." token. */
2581 richloc.add_fixit_replace ("->");
2582 error_at (&richloc,
2583 "%qE is a pointer; did you mean to use %<->%>?",
2584 datum);
2585 return error_mark_node;
2586 }
2587 else if (code != ERROR_MARK)
2588 error_at (loc,
2589 "request for member %qE in something not a structure or union",
2590 component);
2591
2592 return error_mark_node;
2593 }
2594
2595 /* Given an expression PTR for a pointer, return an expression
2596 for the value pointed to.
2597 ERRORSTRING is the name of the operator to appear in error messages.
2598
2599 LOC is the location to use for the generated tree. */
2600
2601 tree
build_indirect_ref(location_t loc,tree ptr,ref_operator errstring)2602 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2603 {
2604 tree pointer = default_conversion (ptr);
2605 tree type = TREE_TYPE (pointer);
2606 tree ref;
2607
2608 if (TREE_CODE (type) == POINTER_TYPE)
2609 {
2610 if (CONVERT_EXPR_P (pointer)
2611 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2612 {
2613 /* If a warning is issued, mark it to avoid duplicates from
2614 the backend. This only needs to be done at
2615 warn_strict_aliasing > 2. */
2616 if (warn_strict_aliasing > 2)
2617 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2618 type, TREE_OPERAND (pointer, 0)))
2619 suppress_warning (pointer, OPT_Wstrict_aliasing_);
2620 }
2621
2622 if (TREE_CODE (pointer) == ADDR_EXPR
2623 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2624 == TREE_TYPE (type)))
2625 {
2626 ref = TREE_OPERAND (pointer, 0);
2627 protected_set_expr_location (ref, loc);
2628 return ref;
2629 }
2630 else
2631 {
2632 tree t = TREE_TYPE (type);
2633
2634 ref = build1 (INDIRECT_REF, t, pointer);
2635
2636 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2637 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2638
2639 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2640 so that we get the proper error message if the result is used
2641 to assign to. Also, &* is supposed to be a no-op.
2642 And ANSI C seems to specify that the type of the result
2643 should be the const type. */
2644 /* A de-reference of a pointer to const is not a const. It is valid
2645 to change it via some other pointer. */
2646 TREE_READONLY (ref) = TYPE_READONLY (t);
2647 TREE_SIDE_EFFECTS (ref)
2648 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2649 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2650 protected_set_expr_location (ref, loc);
2651 return ref;
2652 }
2653 }
2654 else if (TREE_CODE (pointer) != ERROR_MARK)
2655 invalid_indirection_error (loc, type, errstring);
2656
2657 return error_mark_node;
2658 }
2659
2660 /* This handles expressions of the form "a[i]", which denotes
2661 an array reference.
2662
2663 This is logically equivalent in C to *(a+i), but we may do it differently.
2664 If A is a variable or a member, we generate a primitive ARRAY_REF.
2665 This avoids forcing the array out of registers, and can work on
2666 arrays that are not lvalues (for example, members of structures returned
2667 by functions).
2668
2669 For vector types, allow vector[i] but not i[vector], and create
2670 *(((type*)&vectortype) + i) for the expression.
2671
2672 LOC is the location to use for the returned expression. */
2673
2674 tree
build_array_ref(location_t loc,tree array,tree index)2675 build_array_ref (location_t loc, tree array, tree index)
2676 {
2677 tree ret;
2678 bool swapped = false;
2679 if (TREE_TYPE (array) == error_mark_node
2680 || TREE_TYPE (index) == error_mark_node)
2681 return error_mark_node;
2682
2683 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2684 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2685 /* Allow vector[index] but not index[vector]. */
2686 && !gnu_vector_type_p (TREE_TYPE (array)))
2687 {
2688 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2689 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2690 {
2691 error_at (loc,
2692 "subscripted value is neither array nor pointer nor vector");
2693
2694 return error_mark_node;
2695 }
2696 std::swap (array, index);
2697 swapped = true;
2698 }
2699
2700 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2701 {
2702 error_at (loc, "array subscript is not an integer");
2703 return error_mark_node;
2704 }
2705
2706 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2707 {
2708 error_at (loc, "subscripted value is pointer to function");
2709 return error_mark_node;
2710 }
2711
2712 /* ??? Existing practice has been to warn only when the char
2713 index is syntactically the index, not for char[array]. */
2714 if (!swapped)
2715 warn_array_subscript_with_type_char (loc, index);
2716
2717 /* Apply default promotions *after* noticing character types. */
2718 index = default_conversion (index);
2719 if (index == error_mark_node)
2720 return error_mark_node;
2721
2722 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2723
2724 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2725 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2726
2727 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2728 {
2729 tree rval, type;
2730
2731 /* An array that is indexed by a non-constant
2732 cannot be stored in a register; we must be able to do
2733 address arithmetic on its address.
2734 Likewise an array of elements of variable size. */
2735 if (TREE_CODE (index) != INTEGER_CST
2736 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2737 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2738 {
2739 if (!c_mark_addressable (array, true))
2740 return error_mark_node;
2741 }
2742 /* An array that is indexed by a constant value which is not within
2743 the array bounds cannot be stored in a register either; because we
2744 would get a crash in store_bit_field/extract_bit_field when trying
2745 to access a non-existent part of the register. */
2746 if (TREE_CODE (index) == INTEGER_CST
2747 && TYPE_DOMAIN (TREE_TYPE (array))
2748 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2749 {
2750 if (!c_mark_addressable (array))
2751 return error_mark_node;
2752 }
2753
2754 if ((pedantic || warn_c90_c99_compat)
2755 && ! was_vector)
2756 {
2757 tree foo = array;
2758 while (TREE_CODE (foo) == COMPONENT_REF)
2759 foo = TREE_OPERAND (foo, 0);
2760 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2761 pedwarn (loc, OPT_Wpedantic,
2762 "ISO C forbids subscripting %<register%> array");
2763 else if (!lvalue_p (foo))
2764 pedwarn_c90 (loc, OPT_Wpedantic,
2765 "ISO C90 forbids subscripting non-lvalue "
2766 "array");
2767 }
2768
2769 type = TREE_TYPE (TREE_TYPE (array));
2770 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2771 /* Array ref is const/volatile if the array elements are
2772 or if the array is. */
2773 TREE_READONLY (rval)
2774 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2775 | TREE_READONLY (array));
2776 TREE_SIDE_EFFECTS (rval)
2777 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2778 | TREE_SIDE_EFFECTS (array));
2779 TREE_THIS_VOLATILE (rval)
2780 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2781 /* This was added by rms on 16 Nov 91.
2782 It fixes vol struct foo *a; a->elts[1]
2783 in an inline function.
2784 Hope it doesn't break something else. */
2785 | TREE_THIS_VOLATILE (array));
2786 ret = require_complete_type (loc, rval);
2787 protected_set_expr_location (ret, loc);
2788 if (non_lvalue)
2789 ret = non_lvalue_loc (loc, ret);
2790 return ret;
2791 }
2792 else
2793 {
2794 tree ar = default_conversion (array);
2795
2796 if (ar == error_mark_node)
2797 return ar;
2798
2799 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2800 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2801
2802 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2803 index, false),
2804 RO_ARRAY_INDEXING);
2805 if (non_lvalue)
2806 ret = non_lvalue_loc (loc, ret);
2807 return ret;
2808 }
2809 }
2810
2811 /* Build an external reference to identifier ID. FUN indicates
2812 whether this will be used for a function call. LOC is the source
2813 location of the identifier. This sets *TYPE to the type of the
2814 identifier, which is not the same as the type of the returned value
2815 for CONST_DECLs defined as enum constants. If the type of the
2816 identifier is not available, *TYPE is set to NULL. */
2817 tree
build_external_ref(location_t loc,tree id,bool fun,tree * type)2818 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2819 {
2820 tree ref;
2821 tree decl = lookup_name (id);
2822
2823 /* In Objective-C, an instance variable (ivar) may be preferred to
2824 whatever lookup_name() found. */
2825 decl = objc_lookup_ivar (decl, id);
2826
2827 *type = NULL;
2828 if (decl && decl != error_mark_node)
2829 {
2830 ref = decl;
2831 *type = TREE_TYPE (ref);
2832 }
2833 else if (fun)
2834 /* Implicit function declaration. */
2835 ref = implicitly_declare (loc, id);
2836 else if (decl == error_mark_node)
2837 /* Don't complain about something that's already been
2838 complained about. */
2839 return error_mark_node;
2840 else
2841 {
2842 undeclared_variable (loc, id);
2843 return error_mark_node;
2844 }
2845
2846 if (TREE_TYPE (ref) == error_mark_node)
2847 return error_mark_node;
2848
2849 if (TREE_UNAVAILABLE (ref))
2850 error_unavailable_use (ref, NULL_TREE);
2851 else if (TREE_DEPRECATED (ref))
2852 warn_deprecated_use (ref, NULL_TREE);
2853
2854 /* Recursive call does not count as usage. */
2855 if (ref != current_function_decl)
2856 {
2857 TREE_USED (ref) = 1;
2858 }
2859
2860 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2861 {
2862 if (!in_sizeof && !in_typeof)
2863 C_DECL_USED (ref) = 1;
2864 else if (DECL_INITIAL (ref) == NULL_TREE
2865 && DECL_EXTERNAL (ref)
2866 && !TREE_PUBLIC (ref))
2867 record_maybe_used_decl (ref);
2868 }
2869
2870 if (TREE_CODE (ref) == CONST_DECL)
2871 {
2872 used_types_insert (TREE_TYPE (ref));
2873
2874 if (warn_cxx_compat
2875 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2876 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2877 {
2878 warning_at (loc, OPT_Wc___compat,
2879 ("enum constant defined in struct or union "
2880 "is not visible in C++"));
2881 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2882 }
2883
2884 ref = DECL_INITIAL (ref);
2885 TREE_CONSTANT (ref) = 1;
2886 }
2887 else if (current_function_decl != NULL_TREE
2888 && !DECL_FILE_SCOPE_P (current_function_decl)
2889 && (VAR_OR_FUNCTION_DECL_P (ref)
2890 || TREE_CODE (ref) == PARM_DECL))
2891 {
2892 tree context = decl_function_context (ref);
2893
2894 if (context != NULL_TREE && context != current_function_decl)
2895 DECL_NONLOCAL (ref) = 1;
2896 }
2897 /* C99 6.7.4p3: An inline definition of a function with external
2898 linkage ... shall not contain a reference to an identifier with
2899 internal linkage. */
2900 else if (current_function_decl != NULL_TREE
2901 && DECL_DECLARED_INLINE_P (current_function_decl)
2902 && DECL_EXTERNAL (current_function_decl)
2903 && VAR_OR_FUNCTION_DECL_P (ref)
2904 && (!VAR_P (ref) || TREE_STATIC (ref))
2905 && ! TREE_PUBLIC (ref)
2906 && DECL_CONTEXT (ref) != current_function_decl)
2907 record_inline_static (loc, current_function_decl, ref,
2908 csi_internal);
2909
2910 return ref;
2911 }
2912
2913 /* Record details of decls possibly used inside sizeof or typeof. */
2914 struct maybe_used_decl
2915 {
2916 /* The decl. */
2917 tree decl;
2918 /* The level seen at (in_sizeof + in_typeof). */
2919 int level;
2920 /* The next one at this level or above, or NULL. */
2921 struct maybe_used_decl *next;
2922 };
2923
2924 static struct maybe_used_decl *maybe_used_decls;
2925
2926 /* Record that DECL, an undefined static function reference seen
2927 inside sizeof or typeof, might be used if the operand of sizeof is
2928 a VLA type or the operand of typeof is a variably modified
2929 type. */
2930
2931 static void
record_maybe_used_decl(tree decl)2932 record_maybe_used_decl (tree decl)
2933 {
2934 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2935 t->decl = decl;
2936 t->level = in_sizeof + in_typeof;
2937 t->next = maybe_used_decls;
2938 maybe_used_decls = t;
2939 }
2940
2941 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2942 USED is false, just discard them. If it is true, mark them used
2943 (if no longer inside sizeof or typeof) or move them to the next
2944 level up (if still inside sizeof or typeof). */
2945
2946 void
pop_maybe_used(bool used)2947 pop_maybe_used (bool used)
2948 {
2949 struct maybe_used_decl *p = maybe_used_decls;
2950 int cur_level = in_sizeof + in_typeof;
2951 while (p && p->level > cur_level)
2952 {
2953 if (used)
2954 {
2955 if (cur_level == 0)
2956 C_DECL_USED (p->decl) = 1;
2957 else
2958 p->level = cur_level;
2959 }
2960 p = p->next;
2961 }
2962 if (!used || cur_level == 0)
2963 maybe_used_decls = p;
2964 }
2965
2966 /* Return the result of sizeof applied to EXPR. */
2967
2968 struct c_expr
c_expr_sizeof_expr(location_t loc,struct c_expr expr)2969 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2970 {
2971 struct c_expr ret;
2972 if (expr.value == error_mark_node)
2973 {
2974 ret.value = error_mark_node;
2975 ret.original_code = ERROR_MARK;
2976 ret.original_type = NULL;
2977 pop_maybe_used (false);
2978 }
2979 else
2980 {
2981 bool expr_const_operands = true;
2982
2983 if (TREE_CODE (expr.value) == PARM_DECL
2984 && C_ARRAY_PARAMETER (expr.value))
2985 {
2986 auto_diagnostic_group d;
2987 if (warning_at (loc, OPT_Wsizeof_array_argument,
2988 "%<sizeof%> on array function parameter %qE will "
2989 "return size of %qT", expr.value,
2990 TREE_TYPE (expr.value)))
2991 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2992 }
2993 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2994 &expr_const_operands);
2995 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2996 c_last_sizeof_arg = expr.value;
2997 c_last_sizeof_loc = loc;
2998 ret.original_code = SIZEOF_EXPR;
2999 ret.original_type = NULL;
3000 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3001 {
3002 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3003 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3004 folded_expr, ret.value);
3005 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3006 SET_EXPR_LOCATION (ret.value, loc);
3007 }
3008 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3009 }
3010 return ret;
3011 }
3012
3013 /* Return the result of sizeof applied to T, a structure for the type
3014 name passed to sizeof (rather than the type itself). LOC is the
3015 location of the original expression. */
3016
3017 struct c_expr
c_expr_sizeof_type(location_t loc,struct c_type_name * t)3018 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3019 {
3020 tree type;
3021 struct c_expr ret;
3022 tree type_expr = NULL_TREE;
3023 bool type_expr_const = true;
3024 type = groktypename (t, &type_expr, &type_expr_const);
3025 ret.value = c_sizeof (loc, type);
3026 c_last_sizeof_arg = type;
3027 c_last_sizeof_loc = loc;
3028 ret.original_code = SIZEOF_EXPR;
3029 ret.original_type = NULL;
3030 if (type == error_mark_node)
3031 {
3032 ret.value = error_mark_node;
3033 ret.original_code = ERROR_MARK;
3034 }
3035 else
3036 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3037 && C_TYPE_VARIABLE_SIZE (type))
3038 {
3039 /* If the type is a [*] array, it is a VLA but is represented as
3040 having a size of zero. In such a case we must ensure that
3041 the result of sizeof does not get folded to a constant by
3042 c_fully_fold, because if the size is evaluated the result is
3043 not constant and so constraints on zero or negative size
3044 arrays must not be applied when this sizeof call is inside
3045 another array declarator. */
3046 if (!type_expr)
3047 type_expr = integer_zero_node;
3048 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3049 type_expr, ret.value);
3050 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3051 }
3052 pop_maybe_used (type != error_mark_node
3053 ? C_TYPE_VARIABLE_SIZE (type) : false);
3054 return ret;
3055 }
3056
3057 /* Build a function call to function FUNCTION with parameters PARAMS.
3058 The function call is at LOC.
3059 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3060 TREE_VALUE of each node is a parameter-expression.
3061 FUNCTION's data type may be a function type or a pointer-to-function. */
3062
3063 tree
build_function_call(location_t loc,tree function,tree params)3064 build_function_call (location_t loc, tree function, tree params)
3065 {
3066 vec<tree, va_gc> *v;
3067 tree ret;
3068
3069 vec_alloc (v, list_length (params));
3070 for (; params; params = TREE_CHAIN (params))
3071 v->quick_push (TREE_VALUE (params));
3072 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3073 vec_free (v);
3074 return ret;
3075 }
3076
3077 /* Give a note about the location of the declaration of DECL. */
3078
3079 static void
inform_declaration(tree decl)3080 inform_declaration (tree decl)
3081 {
3082 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3083 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3084 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3085 }
3086
3087 /* Build a function call to function FUNCTION with parameters PARAMS.
3088 If FUNCTION is the result of resolving an overloaded target built-in,
3089 ORIG_FUNDECL is the original function decl, otherwise it is null.
3090 ORIGTYPES, if not NULL, is a vector of types; each element is
3091 either NULL or the original type of the corresponding element in
3092 PARAMS. The original type may differ from TREE_TYPE of the
3093 parameter for enums. FUNCTION's data type may be a function type
3094 or pointer-to-function. This function changes the elements of
3095 PARAMS. */
3096
3097 tree
build_function_call_vec(location_t loc,vec<location_t> arg_loc,tree function,vec<tree,va_gc> * params,vec<tree,va_gc> * origtypes,tree orig_fundecl)3098 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3099 tree function, vec<tree, va_gc> *params,
3100 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3101 {
3102 tree fntype, fundecl = NULL_TREE;
3103 tree name = NULL_TREE, result;
3104 tree tem;
3105 int nargs;
3106 tree *argarray;
3107
3108
3109 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3110 STRIP_TYPE_NOPS (function);
3111
3112 /* Convert anything with function type to a pointer-to-function. */
3113 if (TREE_CODE (function) == FUNCTION_DECL)
3114 {
3115 name = DECL_NAME (function);
3116
3117 if (flag_tm)
3118 tm_malloc_replacement (function);
3119 fundecl = function;
3120 if (!orig_fundecl)
3121 orig_fundecl = fundecl;
3122 /* Atomic functions have type checking/casting already done. They are
3123 often rewritten and don't match the original parameter list. */
3124 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3125 origtypes = NULL;
3126 }
3127 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3128 function = function_to_pointer_conversion (loc, function);
3129
3130 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3131 expressions, like those used for ObjC messenger dispatches. */
3132 if (params && !params->is_empty ())
3133 function = objc_rewrite_function_call (function, (*params)[0]);
3134
3135 function = c_fully_fold (function, false, NULL);
3136
3137 fntype = TREE_TYPE (function);
3138
3139 if (TREE_CODE (fntype) == ERROR_MARK)
3140 return error_mark_node;
3141
3142 if (!(TREE_CODE (fntype) == POINTER_TYPE
3143 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3144 {
3145 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3146 error_at (loc,
3147 "called object %qE is not a function or function pointer",
3148 function);
3149 else if (DECL_P (function))
3150 {
3151 error_at (loc,
3152 "called object %qD is not a function or function pointer",
3153 function);
3154 inform_declaration (function);
3155 }
3156 else
3157 error_at (loc,
3158 "called object is not a function or function pointer");
3159 return error_mark_node;
3160 }
3161
3162 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3163 current_function_returns_abnormally = 1;
3164
3165 /* fntype now gets the type of function pointed to. */
3166 fntype = TREE_TYPE (fntype);
3167
3168 /* Convert the parameters to the types declared in the
3169 function prototype, or apply default promotions. */
3170
3171 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3172 origtypes, function, fundecl);
3173 if (nargs < 0)
3174 return error_mark_node;
3175
3176 /* Check that the function is called through a compatible prototype.
3177 If it is not, warn. */
3178 if (CONVERT_EXPR_P (function)
3179 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3180 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3181 && !comptypes (fntype, TREE_TYPE (tem)))
3182 {
3183 tree return_type = TREE_TYPE (fntype);
3184
3185 /* This situation leads to run-time undefined behavior. We can't,
3186 therefore, simply error unless we can prove that all possible
3187 executions of the program must execute the code. */
3188 warning_at (loc, 0, "function called through a non-compatible type");
3189
3190 if (VOID_TYPE_P (return_type)
3191 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3192 pedwarn (loc, 0,
3193 "function with qualified void return type called");
3194 }
3195
3196 argarray = vec_safe_address (params);
3197
3198 /* Check that arguments to builtin functions match the expectations. */
3199 if (fundecl
3200 && fndecl_built_in_p (fundecl)
3201 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3202 orig_fundecl, nargs, argarray))
3203 return error_mark_node;
3204
3205 /* Check that the arguments to the function are valid. */
3206 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3207 nargs, argarray, &arg_loc);
3208
3209 if (name != NULL_TREE
3210 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3211 {
3212 if (require_constant_value)
3213 result
3214 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3215 function, nargs, argarray);
3216 else
3217 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3218 function, nargs, argarray);
3219 if (TREE_CODE (result) == NOP_EXPR
3220 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3221 STRIP_TYPE_NOPS (result);
3222 }
3223 else
3224 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3225 function, nargs, argarray);
3226 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3227 later. */
3228 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3229 suppress_warning (result, OPT_Wnonnull);
3230
3231 /* In this improbable scenario, a nested function returns a VM type.
3232 Create a TARGET_EXPR so that the call always has a LHS, much as
3233 what the C++ FE does for functions returning non-PODs. */
3234 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3235 {
3236 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3237 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3238 NULL_TREE, NULL_TREE);
3239 }
3240
3241 if (VOID_TYPE_P (TREE_TYPE (result)))
3242 {
3243 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3244 pedwarn (loc, 0,
3245 "function with qualified void return type called");
3246 return result;
3247 }
3248 return require_complete_type (loc, result);
3249 }
3250
3251 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3252
3253 tree
c_build_function_call_vec(location_t loc,const vec<location_t> & arg_loc,tree function,vec<tree,va_gc> * params,vec<tree,va_gc> * origtypes)3254 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3255 tree function, vec<tree, va_gc> *params,
3256 vec<tree, va_gc> *origtypes)
3257 {
3258 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3259 STRIP_TYPE_NOPS (function);
3260
3261 /* Convert anything with function type to a pointer-to-function. */
3262 if (TREE_CODE (function) == FUNCTION_DECL)
3263 {
3264 /* Implement type-directed function overloading for builtins.
3265 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3266 handle all the type checking. The result is a complete expression
3267 that implements this function call. */
3268 tree tem = resolve_overloaded_builtin (loc, function, params);
3269 if (tem)
3270 return tem;
3271 }
3272 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3273 }
3274
3275 /* Helper for convert_arguments called to convert the VALue of argument
3276 number ARGNUM from ORIGTYPE to the corresponding parameter number
3277 PARMNUM and TYPE.
3278 PLOC is the location where the conversion is being performed.
3279 FUNCTION and FUNDECL are the same as in convert_arguments.
3280 VALTYPE is the original type of VAL before the conversion and,
3281 for EXCESS_PRECISION_EXPR, the operand of the expression.
3282 NPC is true if VAL represents the null pointer constant (VAL itself
3283 will have been folded to an integer constant).
3284 RNAME is the same as FUNCTION except in Objective C when it's
3285 the function selector.
3286 EXCESS_PRECISION is true when VAL was originally represented
3287 as EXCESS_PRECISION_EXPR.
3288 WARNOPT is the same as in convert_for_assignment. */
3289
3290 static tree
convert_argument(location_t ploc,tree function,tree fundecl,tree type,tree origtype,tree val,tree valtype,bool npc,tree rname,int parmnum,int argnum,bool excess_precision,int warnopt)3291 convert_argument (location_t ploc, tree function, tree fundecl,
3292 tree type, tree origtype, tree val, tree valtype,
3293 bool npc, tree rname, int parmnum, int argnum,
3294 bool excess_precision, int warnopt)
3295 {
3296 /* Formal parm type is specified by a function prototype. */
3297
3298 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3299 {
3300 error_at (ploc, "type of formal parameter %d is incomplete",
3301 parmnum + 1);
3302 return val;
3303 }
3304
3305 /* Optionally warn about conversions that differ from the default
3306 conversions. */
3307 if (warn_traditional_conversion || warn_traditional)
3308 {
3309 unsigned int formal_prec = TYPE_PRECISION (type);
3310
3311 if (INTEGRAL_TYPE_P (type)
3312 && TREE_CODE (valtype) == REAL_TYPE)
3313 warning_at (ploc, OPT_Wtraditional_conversion,
3314 "passing argument %d of %qE as integer rather "
3315 "than floating due to prototype",
3316 argnum, rname);
3317 if (INTEGRAL_TYPE_P (type)
3318 && TREE_CODE (valtype) == COMPLEX_TYPE)
3319 warning_at (ploc, OPT_Wtraditional_conversion,
3320 "passing argument %d of %qE as integer rather "
3321 "than complex due to prototype",
3322 argnum, rname);
3323 else if (TREE_CODE (type) == COMPLEX_TYPE
3324 && TREE_CODE (valtype) == REAL_TYPE)
3325 warning_at (ploc, OPT_Wtraditional_conversion,
3326 "passing argument %d of %qE as complex rather "
3327 "than floating due to prototype",
3328 argnum, rname);
3329 else if (TREE_CODE (type) == REAL_TYPE
3330 && INTEGRAL_TYPE_P (valtype))
3331 warning_at (ploc, OPT_Wtraditional_conversion,
3332 "passing argument %d of %qE as floating rather "
3333 "than integer due to prototype",
3334 argnum, rname);
3335 else if (TREE_CODE (type) == COMPLEX_TYPE
3336 && INTEGRAL_TYPE_P (valtype))
3337 warning_at (ploc, OPT_Wtraditional_conversion,
3338 "passing argument %d of %qE as complex rather "
3339 "than integer due to prototype",
3340 argnum, rname);
3341 else if (TREE_CODE (type) == REAL_TYPE
3342 && TREE_CODE (valtype) == COMPLEX_TYPE)
3343 warning_at (ploc, OPT_Wtraditional_conversion,
3344 "passing argument %d of %qE as floating rather "
3345 "than complex due to prototype",
3346 argnum, rname);
3347 /* ??? At some point, messages should be written about
3348 conversions between complex types, but that's too messy
3349 to do now. */
3350 else if (TREE_CODE (type) == REAL_TYPE
3351 && TREE_CODE (valtype) == REAL_TYPE)
3352 {
3353 /* Warn if any argument is passed as `float',
3354 since without a prototype it would be `double'. */
3355 if (formal_prec == TYPE_PRECISION (float_type_node)
3356 && type != dfloat32_type_node)
3357 warning_at (ploc, 0,
3358 "passing argument %d of %qE as %<float%> "
3359 "rather than %<double%> due to prototype",
3360 argnum, rname);
3361
3362 /* Warn if mismatch between argument and prototype
3363 for decimal float types. Warn of conversions with
3364 binary float types and of precision narrowing due to
3365 prototype. */
3366 else if (type != valtype
3367 && (type == dfloat32_type_node
3368 || type == dfloat64_type_node
3369 || type == dfloat128_type_node
3370 || valtype == dfloat32_type_node
3371 || valtype == dfloat64_type_node
3372 || valtype == dfloat128_type_node)
3373 && (formal_prec
3374 <= TYPE_PRECISION (valtype)
3375 || (type == dfloat128_type_node
3376 && (valtype
3377 != dfloat64_type_node
3378 && (valtype
3379 != dfloat32_type_node)))
3380 || (type == dfloat64_type_node
3381 && (valtype
3382 != dfloat32_type_node))))
3383 warning_at (ploc, 0,
3384 "passing argument %d of %qE as %qT "
3385 "rather than %qT due to prototype",
3386 argnum, rname, type, valtype);
3387
3388 }
3389 /* Detect integer changing in width or signedness.
3390 These warnings are only activated with
3391 -Wtraditional-conversion, not with -Wtraditional. */
3392 else if (warn_traditional_conversion
3393 && INTEGRAL_TYPE_P (type)
3394 && INTEGRAL_TYPE_P (valtype))
3395 {
3396 tree would_have_been = default_conversion (val);
3397 tree type1 = TREE_TYPE (would_have_been);
3398
3399 if (val == error_mark_node)
3400 /* VAL could have been of incomplete type. */;
3401 else if (TREE_CODE (type) == ENUMERAL_TYPE
3402 && (TYPE_MAIN_VARIANT (type)
3403 == TYPE_MAIN_VARIANT (valtype)))
3404 /* No warning if function asks for enum
3405 and the actual arg is that enum type. */
3406 ;
3407 else if (formal_prec != TYPE_PRECISION (type1))
3408 warning_at (ploc, OPT_Wtraditional_conversion,
3409 "passing argument %d of %qE "
3410 "with different width due to prototype",
3411 argnum, rname);
3412 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3413 ;
3414 /* Don't complain if the formal parameter type
3415 is an enum, because we can't tell now whether
3416 the value was an enum--even the same enum. */
3417 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3418 ;
3419 else if (TREE_CODE (val) == INTEGER_CST
3420 && int_fits_type_p (val, type))
3421 /* Change in signedness doesn't matter
3422 if a constant value is unaffected. */
3423 ;
3424 /* If the value is extended from a narrower
3425 unsigned type, it doesn't matter whether we
3426 pass it as signed or unsigned; the value
3427 certainly is the same either way. */
3428 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3429 && TYPE_UNSIGNED (valtype))
3430 ;
3431 else if (TYPE_UNSIGNED (type))
3432 warning_at (ploc, OPT_Wtraditional_conversion,
3433 "passing argument %d of %qE "
3434 "as unsigned due to prototype",
3435 argnum, rname);
3436 else
3437 warning_at (ploc, OPT_Wtraditional_conversion,
3438 "passing argument %d of %qE "
3439 "as signed due to prototype",
3440 argnum, rname);
3441 }
3442 }
3443
3444 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3445 sake of better warnings from convert_and_check. */
3446 if (excess_precision)
3447 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3448
3449 tree parmval = convert_for_assignment (ploc, ploc, type,
3450 val, origtype, ic_argpass,
3451 npc, fundecl, function,
3452 parmnum + 1, warnopt);
3453
3454 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3455 && INTEGRAL_TYPE_P (type)
3456 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3457 parmval = default_conversion (parmval);
3458
3459 return parmval;
3460 }
3461
3462 /* Convert the argument expressions in the vector VALUES
3463 to the types in the list TYPELIST.
3464
3465 If TYPELIST is exhausted, or when an element has NULL as its type,
3466 perform the default conversions.
3467
3468 ORIGTYPES is the original types of the expressions in VALUES. This
3469 holds the type of enum values which have been converted to integral
3470 types. It may be NULL.
3471
3472 FUNCTION is a tree for the called function. It is used only for
3473 error messages, where it is formatted with %qE.
3474
3475 This is also where warnings about wrong number of args are generated.
3476
3477 ARG_LOC are locations of function arguments (if any).
3478
3479 Returns the actual number of arguments processed (which may be less
3480 than the length of VALUES in some error situations), or -1 on
3481 failure. */
3482
3483 static int
convert_arguments(location_t loc,vec<location_t> arg_loc,tree typelist,vec<tree,va_gc> * values,vec<tree,va_gc> * origtypes,tree function,tree fundecl)3484 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3485 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3486 tree function, tree fundecl)
3487 {
3488 unsigned int parmnum;
3489 bool error_args = false;
3490 const bool type_generic = fundecl
3491 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3492 bool type_generic_remove_excess_precision = false;
3493 bool type_generic_overflow_p = false;
3494 tree selector;
3495
3496 /* Change pointer to function to the function itself for
3497 diagnostics. */
3498 if (TREE_CODE (function) == ADDR_EXPR
3499 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3500 function = TREE_OPERAND (function, 0);
3501
3502 /* Handle an ObjC selector specially for diagnostics. */
3503 selector = objc_message_selector ();
3504
3505 /* For a call to a built-in function declared without a prototype,
3506 set to the built-in function's argument list. */
3507 tree builtin_typelist = NULL_TREE;
3508
3509 /* For type-generic built-in functions, determine whether excess
3510 precision should be removed (classification) or not
3511 (comparison). */
3512 if (fundecl
3513 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3514 {
3515 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3516 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3517 {
3518 /* For a call to a built-in function declared without a prototype
3519 use the types of the parameters of the internal built-in to
3520 match those of the arguments to. */
3521 if (tree bdecl = builtin_decl_explicit (code))
3522 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3523 }
3524
3525 /* For type-generic built-in functions, determine whether excess
3526 precision should be removed (classification) or not
3527 (comparison). */
3528 if (type_generic)
3529 switch (code)
3530 {
3531 case BUILT_IN_ISFINITE:
3532 case BUILT_IN_ISINF:
3533 case BUILT_IN_ISINF_SIGN:
3534 case BUILT_IN_ISNAN:
3535 case BUILT_IN_ISNORMAL:
3536 case BUILT_IN_FPCLASSIFY:
3537 type_generic_remove_excess_precision = true;
3538 break;
3539
3540 case BUILT_IN_ADD_OVERFLOW_P:
3541 case BUILT_IN_SUB_OVERFLOW_P:
3542 case BUILT_IN_MUL_OVERFLOW_P:
3543 /* The last argument of these type-generic builtins
3544 should not be promoted. */
3545 type_generic_overflow_p = true;
3546 break;
3547
3548 default:
3549 break;
3550 }
3551 }
3552
3553 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3554 individual converted arguments. */
3555
3556 tree typetail, builtin_typetail, val;
3557 for (typetail = typelist,
3558 builtin_typetail = builtin_typelist,
3559 parmnum = 0;
3560 values && values->iterate (parmnum, &val);
3561 ++parmnum)
3562 {
3563 /* The type of the function parameter (if it was declared with one). */
3564 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3565 /* The type of the built-in function parameter (if the function
3566 is a built-in). Used to detect type incompatibilities in
3567 calls to built-ins declared without a prototype. */
3568 tree builtin_type = (builtin_typetail
3569 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3570 /* The original type of the argument being passed to the function. */
3571 tree valtype = TREE_TYPE (val);
3572 /* The called function (or function selector in Objective C). */
3573 tree rname = function;
3574 int argnum = parmnum + 1;
3575 const char *invalid_func_diag;
3576 /* Set for EXCESS_PRECISION_EXPR arguments. */
3577 bool excess_precision = false;
3578 /* The value of the argument after conversion to the type
3579 of the function parameter it is passed to. */
3580 tree parmval;
3581 /* Some __atomic_* builtins have additional hidden argument at
3582 position 0. */
3583 location_t ploc
3584 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3585 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3586 : input_location;
3587
3588 if (type == void_type_node)
3589 {
3590 if (selector)
3591 error_at (loc, "too many arguments to method %qE", selector);
3592 else
3593 error_at (loc, "too many arguments to function %qE", function);
3594 inform_declaration (fundecl);
3595 return error_args ? -1 : (int) parmnum;
3596 }
3597
3598 if (builtin_type == void_type_node)
3599 {
3600 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3601 "too many arguments to built-in function %qE "
3602 "expecting %d", function, parmnum))
3603 inform_declaration (fundecl);
3604 builtin_typetail = NULL_TREE;
3605 }
3606
3607 if (selector && argnum > 2)
3608 {
3609 rname = selector;
3610 argnum -= 2;
3611 }
3612
3613 /* Determine if VAL is a null pointer constant before folding it. */
3614 bool npc = null_pointer_constant_p (val);
3615
3616 /* If there is excess precision and a prototype, convert once to
3617 the required type rather than converting via the semantic
3618 type. Likewise without a prototype a float value represented
3619 as long double should be converted once to double. But for
3620 type-generic classification functions excess precision must
3621 be removed here. */
3622 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3623 && (type || !type_generic || !type_generic_remove_excess_precision))
3624 {
3625 val = TREE_OPERAND (val, 0);
3626 excess_precision = true;
3627 }
3628 val = c_fully_fold (val, false, NULL);
3629 STRIP_TYPE_NOPS (val);
3630
3631 val = require_complete_type (ploc, val);
3632
3633 /* Some floating-point arguments must be promoted to double when
3634 no type is specified by a prototype. This applies to
3635 arguments of type float, and to architecture-specific types
3636 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3637 bool promote_float_arg = false;
3638 if (type == NULL_TREE
3639 && TREE_CODE (valtype) == REAL_TYPE
3640 && (TYPE_PRECISION (valtype)
3641 <= TYPE_PRECISION (double_type_node))
3642 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3643 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3644 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3645 {
3646 /* Promote this argument, unless it has a _FloatN or
3647 _FloatNx type. */
3648 promote_float_arg = true;
3649 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3650 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3651 {
3652 promote_float_arg = false;
3653 break;
3654 }
3655 }
3656
3657 if (type != NULL_TREE)
3658 {
3659 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3660 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3661 val, valtype, npc, rname, parmnum, argnum,
3662 excess_precision, 0);
3663 }
3664 else if (promote_float_arg)
3665 {
3666 if (type_generic)
3667 parmval = val;
3668 else
3669 {
3670 /* Convert `float' to `double'. */
3671 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3672 warning_at (ploc, OPT_Wdouble_promotion,
3673 "implicit conversion from %qT to %qT when passing "
3674 "argument to function",
3675 valtype, double_type_node);
3676 parmval = convert (double_type_node, val);
3677 }
3678 }
3679 else if ((excess_precision && !type_generic)
3680 || (type_generic_overflow_p && parmnum == 2))
3681 /* A "double" argument with excess precision being passed
3682 without a prototype or in variable arguments.
3683 The last argument of __builtin_*_overflow_p should not be
3684 promoted. */
3685 parmval = convert (valtype, val);
3686 else if ((invalid_func_diag =
3687 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3688 {
3689 error (invalid_func_diag);
3690 return -1;
3691 }
3692 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3693 {
3694 return -1;
3695 }
3696 else
3697 /* Convert `short' and `char' to full-size `int'. */
3698 parmval = default_conversion (val);
3699
3700 (*values)[parmnum] = parmval;
3701 if (parmval == error_mark_node)
3702 error_args = true;
3703
3704 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3705 {
3706 /* For a call to a built-in function declared without a prototype,
3707 perform the conversions from the argument to the expected type
3708 but issue warnings rather than errors for any mismatches.
3709 Ignore the converted argument and use the PARMVAL obtained
3710 above by applying default conversions instead. */
3711 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3712 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3713 val, valtype, npc, rname, parmnum, argnum,
3714 excess_precision,
3715 OPT_Wbuiltin_declaration_mismatch);
3716 }
3717
3718 if (typetail)
3719 typetail = TREE_CHAIN (typetail);
3720
3721 if (builtin_typetail)
3722 builtin_typetail = TREE_CHAIN (builtin_typetail);
3723 }
3724
3725 gcc_assert (parmnum == vec_safe_length (values));
3726
3727 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3728 {
3729 error_at (loc, "too few arguments to function %qE", function);
3730 inform_declaration (fundecl);
3731 return -1;
3732 }
3733
3734 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3735 {
3736 unsigned nargs = parmnum;
3737 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3738 ++nargs;
3739
3740 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3741 "too few arguments to built-in function %qE "
3742 "expecting %u", function, nargs - 1))
3743 inform_declaration (fundecl);
3744 }
3745
3746 return error_args ? -1 : (int) parmnum;
3747 }
3748
3749 /* This is the entry point used by the parser to build unary operators
3750 in the input. CODE, a tree_code, specifies the unary operator, and
3751 ARG is the operand. For unary plus, the C parser currently uses
3752 CONVERT_EXPR for code.
3753
3754 LOC is the location to use for the tree generated.
3755 */
3756
3757 struct c_expr
parser_build_unary_op(location_t loc,enum tree_code code,struct c_expr arg)3758 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3759 {
3760 struct c_expr result;
3761
3762 result.original_code = code;
3763 result.original_type = NULL;
3764
3765 if (reject_gcc_builtin (arg.value))
3766 {
3767 result.value = error_mark_node;
3768 }
3769 else
3770 {
3771 result.value = build_unary_op (loc, code, arg.value, false);
3772
3773 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3774 overflow_warning (loc, result.value, arg.value);
3775 }
3776
3777 /* We are typically called when parsing a prefix token at LOC acting on
3778 ARG. Reflect this by updating the source range of the result to
3779 start at LOC and end at the end of ARG. */
3780 set_c_expr_source_range (&result,
3781 loc, arg.get_finish ());
3782
3783 return result;
3784 }
3785
3786 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3787
3788 bool
char_type_p(tree type)3789 char_type_p (tree type)
3790 {
3791 return (type == char_type_node
3792 || type == unsigned_char_type_node
3793 || type == signed_char_type_node
3794 || type == char16_type_node
3795 || type == char32_type_node);
3796 }
3797
3798 /* This is the entry point used by the parser to build binary operators
3799 in the input. CODE, a tree_code, specifies the binary operator, and
3800 ARG1 and ARG2 are the operands. In addition to constructing the
3801 expression, we check for operands that were written with other binary
3802 operators in a way that is likely to confuse the user.
3803
3804 LOCATION is the location of the binary operator. */
3805
3806 struct c_expr
parser_build_binary_op(location_t location,enum tree_code code,struct c_expr arg1,struct c_expr arg2)3807 parser_build_binary_op (location_t location, enum tree_code code,
3808 struct c_expr arg1, struct c_expr arg2)
3809 {
3810 struct c_expr result;
3811
3812 enum tree_code code1 = arg1.original_code;
3813 enum tree_code code2 = arg2.original_code;
3814 tree type1 = (arg1.original_type
3815 ? arg1.original_type
3816 : TREE_TYPE (arg1.value));
3817 tree type2 = (arg2.original_type
3818 ? arg2.original_type
3819 : TREE_TYPE (arg2.value));
3820
3821 result.value = build_binary_op (location, code,
3822 arg1.value, arg2.value, true);
3823 result.original_code = code;
3824 result.original_type = NULL;
3825
3826 if (TREE_CODE (result.value) == ERROR_MARK)
3827 {
3828 set_c_expr_source_range (&result,
3829 arg1.get_start (),
3830 arg2.get_finish ());
3831 return result;
3832 }
3833
3834 if (location != UNKNOWN_LOCATION)
3835 protected_set_expr_location (result.value, location);
3836
3837 set_c_expr_source_range (&result,
3838 arg1.get_start (),
3839 arg2.get_finish ());
3840
3841 /* Check for cases such as x+y<<z which users are likely
3842 to misinterpret. */
3843 if (warn_parentheses)
3844 warn_about_parentheses (location, code, code1, arg1.value, code2,
3845 arg2.value);
3846
3847 if (warn_logical_op)
3848 warn_logical_operator (location, code, TREE_TYPE (result.value),
3849 code1, arg1.value, code2, arg2.value);
3850
3851 if (warn_tautological_compare)
3852 {
3853 tree lhs = arg1.value;
3854 tree rhs = arg2.value;
3855 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3856 {
3857 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3858 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3859 lhs = NULL_TREE;
3860 else
3861 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3862 }
3863 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3864 {
3865 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3866 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3867 rhs = NULL_TREE;
3868 else
3869 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3870 }
3871 if (lhs != NULL_TREE && rhs != NULL_TREE)
3872 warn_tautological_cmp (location, code, lhs, rhs);
3873 }
3874
3875 if (warn_logical_not_paren
3876 && TREE_CODE_CLASS (code) == tcc_comparison
3877 && code1 == TRUTH_NOT_EXPR
3878 && code2 != TRUTH_NOT_EXPR
3879 /* Avoid warning for !!x == y. */
3880 && (TREE_CODE (arg1.value) != NE_EXPR
3881 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3882 {
3883 /* Avoid warning for !b == y where b has _Bool type. */
3884 tree t = integer_zero_node;
3885 if (TREE_CODE (arg1.value) == EQ_EXPR
3886 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3887 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3888 {
3889 t = TREE_OPERAND (arg1.value, 0);
3890 do
3891 {
3892 if (TREE_TYPE (t) != integer_type_node)
3893 break;
3894 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3895 t = C_MAYBE_CONST_EXPR_EXPR (t);
3896 else if (CONVERT_EXPR_P (t))
3897 t = TREE_OPERAND (t, 0);
3898 else
3899 break;
3900 }
3901 while (1);
3902 }
3903 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3904 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3905 }
3906
3907 /* Warn about comparisons against string literals, with the exception
3908 of testing for equality or inequality of a string literal with NULL. */
3909 if (code == EQ_EXPR || code == NE_EXPR)
3910 {
3911 if ((code1 == STRING_CST
3912 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3913 || (code2 == STRING_CST
3914 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3915 warning_at (location, OPT_Waddress,
3916 "comparison with string literal results in unspecified behavior");
3917 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3918 if (POINTER_TYPE_P (type1)
3919 && null_pointer_constant_p (arg2.value)
3920 && char_type_p (type2))
3921 {
3922 auto_diagnostic_group d;
3923 if (warning_at (location, OPT_Wpointer_compare,
3924 "comparison between pointer and zero character "
3925 "constant"))
3926 inform (arg1.get_start (),
3927 "did you mean to dereference the pointer?");
3928 }
3929 else if (POINTER_TYPE_P (type2)
3930 && null_pointer_constant_p (arg1.value)
3931 && char_type_p (type1))
3932 {
3933 auto_diagnostic_group d;
3934 if (warning_at (location, OPT_Wpointer_compare,
3935 "comparison between pointer and zero character "
3936 "constant"))
3937 inform (arg2.get_start (),
3938 "did you mean to dereference the pointer?");
3939 }
3940 }
3941 else if (TREE_CODE_CLASS (code) == tcc_comparison
3942 && (code1 == STRING_CST || code2 == STRING_CST))
3943 warning_at (location, OPT_Waddress,
3944 "comparison with string literal results in unspecified "
3945 "behavior");
3946
3947 if (warn_array_compare
3948 && TREE_CODE_CLASS (code) == tcc_comparison
3949 && TREE_CODE (type1) == ARRAY_TYPE
3950 && TREE_CODE (type2) == ARRAY_TYPE)
3951 do_warn_array_compare (location, code, arg1.value, arg2.value);
3952
3953 if (TREE_OVERFLOW_P (result.value)
3954 && !TREE_OVERFLOW_P (arg1.value)
3955 && !TREE_OVERFLOW_P (arg2.value))
3956 overflow_warning (location, result.value);
3957
3958 /* Warn about comparisons of different enum types. */
3959 if (warn_enum_compare
3960 && TREE_CODE_CLASS (code) == tcc_comparison
3961 && TREE_CODE (type1) == ENUMERAL_TYPE
3962 && TREE_CODE (type2) == ENUMERAL_TYPE
3963 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3964 warning_at (location, OPT_Wenum_compare,
3965 "comparison between %qT and %qT",
3966 type1, type2);
3967
3968 return result;
3969 }
3970
3971 /* Return a tree for the difference of pointers OP0 and OP1.
3972 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3973 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3974
3975 static tree
pointer_diff(location_t loc,tree op0,tree op1,tree * instrument_expr)3976 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3977 {
3978 tree restype = ptrdiff_type_node;
3979 tree result, inttype;
3980
3981 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3982 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3983 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3984 tree orig_op0 = op0;
3985 tree orig_op1 = op1;
3986
3987 /* If the operands point into different address spaces, we need to
3988 explicitly convert them to pointers into the common address space
3989 before we can subtract the numerical address values. */
3990 if (as0 != as1)
3991 {
3992 addr_space_t as_common;
3993 tree common_type;
3994
3995 /* Determine the common superset address space. This is guaranteed
3996 to exist because the caller verified that comp_target_types
3997 returned non-zero. */
3998 if (!addr_space_superset (as0, as1, &as_common))
3999 gcc_unreachable ();
4000
4001 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4002 op0 = convert (common_type, op0);
4003 op1 = convert (common_type, op1);
4004 }
4005
4006 /* Determine integer type result of the subtraction. This will usually
4007 be the same as the result type (ptrdiff_t), but may need to be a wider
4008 type if pointers for the address space are wider than ptrdiff_t. */
4009 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4010 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4011 else
4012 inttype = restype;
4013
4014 if (TREE_CODE (target_type) == VOID_TYPE)
4015 pedwarn (loc, OPT_Wpointer_arith,
4016 "pointer of type %<void *%> used in subtraction");
4017 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4018 pedwarn (loc, OPT_Wpointer_arith,
4019 "pointer to a function used in subtraction");
4020
4021 if (current_function_decl != NULL_TREE
4022 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4023 {
4024 op0 = save_expr (op0);
4025 op1 = save_expr (op1);
4026
4027 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4028 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4029 }
4030
4031 /* First do the subtraction, then build the divide operator
4032 and only convert at the very end.
4033 Do not do default conversions in case restype is a short type. */
4034
4035 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4036 pointers. If some platform cannot provide that, or has a larger
4037 ptrdiff_type to support differences larger than half the address
4038 space, cast the pointers to some larger integer type and do the
4039 computations in that type. */
4040 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4041 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4042 convert (inttype, op1), false);
4043 else
4044 {
4045 /* Cast away qualifiers. */
4046 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4047 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4048 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4049 }
4050
4051 /* This generates an error if op1 is pointer to incomplete type. */
4052 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4053 error_at (loc, "arithmetic on pointer to an incomplete type");
4054 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4055 TREE_TYPE (TREE_TYPE (orig_op0))))
4056 verify_type_context (loc, TCTX_POINTER_ARITH,
4057 TREE_TYPE (TREE_TYPE (orig_op1)));
4058
4059 op1 = c_size_in_bytes (target_type);
4060
4061 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4062 error_at (loc, "arithmetic on pointer to an empty aggregate");
4063
4064 /* Divide by the size, in easiest possible way. */
4065 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4066 op0, convert (inttype, op1));
4067
4068 /* Convert to final result type if necessary. */
4069 return convert (restype, result);
4070 }
4071
4072 /* Expand atomic compound assignments into an appropriate sequence as
4073 specified by the C11 standard section 6.5.16.2.
4074
4075 _Atomic T1 E1
4076 T2 E2
4077 E1 op= E2
4078
4079 This sequence is used for all types for which these operations are
4080 supported.
4081
4082 In addition, built-in versions of the 'fe' prefixed routines may
4083 need to be invoked for floating point (real, complex or vector) when
4084 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4085
4086 T1 newval;
4087 T1 old;
4088 T1 *addr
4089 T2 val
4090 fenv_t fenv
4091
4092 addr = &E1;
4093 val = (E2);
4094 __atomic_load (addr, &old, SEQ_CST);
4095 feholdexcept (&fenv);
4096 loop:
4097 newval = old op val;
4098 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4099 SEQ_CST))
4100 goto done;
4101 feclearexcept (FE_ALL_EXCEPT);
4102 goto loop:
4103 done:
4104 feupdateenv (&fenv);
4105
4106 The compiler will issue the __atomic_fetch_* built-in when possible,
4107 otherwise it will generate the generic form of the atomic operations.
4108 This requires temp(s) and has their address taken. The atomic processing
4109 is smart enough to figure out when the size of an object can utilize
4110 a lock-free version, and convert the built-in call to the appropriate
4111 lock-free routine. The optimizers will then dispose of any temps that
4112 are no longer required, and lock-free implementations are utilized as
4113 long as there is target support for the required size.
4114
4115 If the operator is NOP_EXPR, then this is a simple assignment, and
4116 an __atomic_store is issued to perform the assignment rather than
4117 the above loop. */
4118
4119 /* Build an atomic assignment at LOC, expanding into the proper
4120 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4121 the result of the operation, unless RETURN_OLD_P, in which case
4122 return the old value of LHS (this is only for postincrement and
4123 postdecrement). */
4124
4125 static tree
build_atomic_assign(location_t loc,tree lhs,enum tree_code modifycode,tree rhs,bool return_old_p)4126 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4127 tree rhs, bool return_old_p)
4128 {
4129 tree fndecl, func_call;
4130 vec<tree, va_gc> *params;
4131 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4132 tree old, old_addr;
4133 tree compound_stmt = NULL_TREE;
4134 tree stmt, goto_stmt;
4135 tree loop_label, loop_decl, done_label, done_decl;
4136
4137 tree lhs_type = TREE_TYPE (lhs);
4138 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4139 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4140 tree rhs_semantic_type = TREE_TYPE (rhs);
4141 tree nonatomic_rhs_semantic_type;
4142 tree rhs_type;
4143
4144 gcc_assert (TYPE_ATOMIC (lhs_type));
4145
4146 if (return_old_p)
4147 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4148
4149 /* Allocate enough vector items for a compare_exchange. */
4150 vec_alloc (params, 6);
4151
4152 /* Create a compound statement to hold the sequence of statements
4153 with a loop. */
4154 if (modifycode != NOP_EXPR)
4155 {
4156 compound_stmt = c_begin_compound_stmt (false);
4157
4158 /* For consistency with build_modify_expr on non-_Atomic,
4159 mark the lhs as read. Also, it would be very hard to match
4160 such expressions in mark_exp_read. */
4161 mark_exp_read (lhs);
4162 }
4163
4164 /* Remove any excess precision (which is only present here in the
4165 case of compound assignments). */
4166 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4167 {
4168 gcc_assert (modifycode != NOP_EXPR);
4169 rhs = TREE_OPERAND (rhs, 0);
4170 }
4171 rhs_type = TREE_TYPE (rhs);
4172
4173 /* Fold the RHS if it hasn't already been folded. */
4174 if (modifycode != NOP_EXPR)
4175 rhs = c_fully_fold (rhs, false, NULL);
4176
4177 /* Remove the qualifiers for the rest of the expressions and create
4178 the VAL temp variable to hold the RHS. */
4179 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4180 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4181 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4182 TYPE_UNQUALIFIED);
4183 val = create_tmp_var_raw (nonatomic_rhs_type);
4184 TREE_ADDRESSABLE (val) = 1;
4185 suppress_warning (val);
4186 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4187 NULL_TREE);
4188 TREE_SIDE_EFFECTS (rhs) = 1;
4189 SET_EXPR_LOCATION (rhs, loc);
4190 if (modifycode != NOP_EXPR)
4191 add_stmt (rhs);
4192
4193 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4194 an atomic_store. */
4195 if (modifycode == NOP_EXPR)
4196 {
4197 compound_stmt = rhs;
4198 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4199 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4200 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4201 params->quick_push (lhs_addr);
4202 params->quick_push (rhs);
4203 params->quick_push (seq_cst);
4204 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4205
4206 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4207 compound_stmt, func_call);
4208
4209 /* VAL is the value which was stored, return a COMPOUND_STMT of
4210 the statement and that value. */
4211 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4212 }
4213
4214 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4215 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4216 isn't applicable for such builtins. ??? Do we want to handle enums? */
4217 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4218 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4219 {
4220 built_in_function fncode;
4221 switch (modifycode)
4222 {
4223 case PLUS_EXPR:
4224 case POINTER_PLUS_EXPR:
4225 fncode = (return_old_p
4226 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4227 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4228 break;
4229 case MINUS_EXPR:
4230 fncode = (return_old_p
4231 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4232 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4233 break;
4234 case BIT_AND_EXPR:
4235 fncode = (return_old_p
4236 ? BUILT_IN_ATOMIC_FETCH_AND_N
4237 : BUILT_IN_ATOMIC_AND_FETCH_N);
4238 break;
4239 case BIT_IOR_EXPR:
4240 fncode = (return_old_p
4241 ? BUILT_IN_ATOMIC_FETCH_OR_N
4242 : BUILT_IN_ATOMIC_OR_FETCH_N);
4243 break;
4244 case BIT_XOR_EXPR:
4245 fncode = (return_old_p
4246 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4247 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4248 break;
4249 default:
4250 goto cas_loop;
4251 }
4252
4253 /* We can only use "_1" through "_16" variants of the atomic fetch
4254 built-ins. */
4255 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4256 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4257 goto cas_loop;
4258
4259 /* If this is a pointer type, we need to multiply by the size of
4260 the pointer target type. */
4261 if (POINTER_TYPE_P (lhs_type))
4262 {
4263 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4264 /* ??? This would introduce -Wdiscarded-qualifiers
4265 warning: __atomic_fetch_* expect volatile void *
4266 type as the first argument. (Assignments between
4267 atomic and non-atomic objects are OK.) */
4268 || TYPE_RESTRICT (lhs_type))
4269 goto cas_loop;
4270 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4271 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4272 convert (ptrdiff_type_node, rhs),
4273 convert (ptrdiff_type_node, sz));
4274 }
4275
4276 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4277 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4278 fndecl = builtin_decl_explicit (fncode);
4279 params->quick_push (lhs_addr);
4280 params->quick_push (rhs);
4281 params->quick_push (seq_cst);
4282 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4283
4284 newval = create_tmp_var_raw (nonatomic_lhs_type);
4285 TREE_ADDRESSABLE (newval) = 1;
4286 suppress_warning (newval);
4287 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4288 NULL_TREE, NULL_TREE);
4289 SET_EXPR_LOCATION (rhs, loc);
4290 add_stmt (rhs);
4291
4292 /* Finish the compound statement. */
4293 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4294
4295 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4296 the statement and that value. */
4297 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4298 }
4299
4300 cas_loop:
4301 /* Create the variables and labels required for the op= form. */
4302 old = create_tmp_var_raw (nonatomic_lhs_type);
4303 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4304 TREE_ADDRESSABLE (old) = 1;
4305 suppress_warning (old);
4306
4307 newval = create_tmp_var_raw (nonatomic_lhs_type);
4308 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4309 TREE_ADDRESSABLE (newval) = 1;
4310 suppress_warning (newval);
4311
4312 loop_decl = create_artificial_label (loc);
4313 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4314
4315 done_decl = create_artificial_label (loc);
4316 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4317
4318 /* __atomic_load (addr, &old, SEQ_CST). */
4319 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4320 params->quick_push (lhs_addr);
4321 params->quick_push (old_addr);
4322 params->quick_push (seq_cst);
4323 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4324 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4325 NULL_TREE);
4326 add_stmt (old);
4327 params->truncate (0);
4328
4329 /* Create the expressions for floating-point environment
4330 manipulation, if required. */
4331 bool need_fenv = (flag_trapping_math
4332 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4333 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4334 if (need_fenv)
4335 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4336
4337 if (hold_call)
4338 add_stmt (hold_call);
4339
4340 /* loop: */
4341 add_stmt (loop_label);
4342
4343 /* newval = old + val; */
4344 if (rhs_type != rhs_semantic_type)
4345 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4346 rhs = build_binary_op (loc, modifycode, old, val, true);
4347 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4348 {
4349 tree eptype = TREE_TYPE (rhs);
4350 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4351 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4352 }
4353 else
4354 rhs = c_fully_fold (rhs, false, NULL);
4355 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4356 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4357 NULL_TREE, 0);
4358 if (rhs != error_mark_node)
4359 {
4360 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4361 NULL_TREE);
4362 SET_EXPR_LOCATION (rhs, loc);
4363 add_stmt (rhs);
4364 }
4365
4366 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4367 goto done; */
4368 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4369 params->quick_push (lhs_addr);
4370 params->quick_push (old_addr);
4371 params->quick_push (newval_addr);
4372 params->quick_push (integer_zero_node);
4373 params->quick_push (seq_cst);
4374 params->quick_push (seq_cst);
4375 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4376
4377 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4378 SET_EXPR_LOCATION (goto_stmt, loc);
4379
4380 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4381 SET_EXPR_LOCATION (stmt, loc);
4382 add_stmt (stmt);
4383
4384 if (clear_call)
4385 add_stmt (clear_call);
4386
4387 /* goto loop; */
4388 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4389 SET_EXPR_LOCATION (goto_stmt, loc);
4390 add_stmt (goto_stmt);
4391
4392 /* done: */
4393 add_stmt (done_label);
4394
4395 if (update_call)
4396 add_stmt (update_call);
4397
4398 /* Finish the compound statement. */
4399 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4400
4401 /* NEWVAL is the value that was successfully stored, return a
4402 COMPOUND_EXPR of the statement and the appropriate value. */
4403 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4404 return_old_p ? old : newval);
4405 }
4406
4407 /* Construct and perhaps optimize a tree representation
4408 for a unary operation. CODE, a tree_code, specifies the operation
4409 and XARG is the operand.
4410 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4411 promotions (such as from short to int).
4412 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4413 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4414 to pointers in C99.
4415
4416 LOCATION is the location of the operator. */
4417
4418 tree
build_unary_op(location_t location,enum tree_code code,tree xarg,bool noconvert)4419 build_unary_op (location_t location, enum tree_code code, tree xarg,
4420 bool noconvert)
4421 {
4422 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4423 tree arg = xarg;
4424 tree argtype = NULL_TREE;
4425 enum tree_code typecode;
4426 tree val;
4427 tree ret = error_mark_node;
4428 tree eptype = NULL_TREE;
4429 const char *invalid_op_diag;
4430 bool int_operands;
4431
4432 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4433 if (int_operands)
4434 arg = remove_c_maybe_const_expr (arg);
4435
4436 if (code != ADDR_EXPR)
4437 arg = require_complete_type (location, arg);
4438
4439 typecode = TREE_CODE (TREE_TYPE (arg));
4440 if (typecode == ERROR_MARK)
4441 return error_mark_node;
4442 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4443 typecode = INTEGER_TYPE;
4444
4445 if ((invalid_op_diag
4446 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4447 {
4448 error_at (location, invalid_op_diag);
4449 return error_mark_node;
4450 }
4451
4452 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4453 {
4454 eptype = TREE_TYPE (arg);
4455 arg = TREE_OPERAND (arg, 0);
4456 }
4457
4458 switch (code)
4459 {
4460 case CONVERT_EXPR:
4461 /* This is used for unary plus, because a CONVERT_EXPR
4462 is enough to prevent anybody from looking inside for
4463 associativity, but won't generate any code. */
4464 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4465 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4466 || gnu_vector_type_p (TREE_TYPE (arg))))
4467 {
4468 error_at (location, "wrong type argument to unary plus");
4469 return error_mark_node;
4470 }
4471 else if (!noconvert)
4472 arg = default_conversion (arg);
4473 arg = non_lvalue_loc (location, arg);
4474 break;
4475
4476 case NEGATE_EXPR:
4477 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4478 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4479 || gnu_vector_type_p (TREE_TYPE (arg))))
4480 {
4481 error_at (location, "wrong type argument to unary minus");
4482 return error_mark_node;
4483 }
4484 else if (!noconvert)
4485 arg = default_conversion (arg);
4486 break;
4487
4488 case BIT_NOT_EXPR:
4489 /* ~ works on integer types and non float vectors. */
4490 if (typecode == INTEGER_TYPE
4491 || (gnu_vector_type_p (TREE_TYPE (arg))
4492 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4493 {
4494 tree e = arg;
4495
4496 /* Warn if the expression has boolean value. */
4497 while (TREE_CODE (e) == COMPOUND_EXPR)
4498 e = TREE_OPERAND (e, 1);
4499
4500 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4501 || truth_value_p (TREE_CODE (e))))
4502 {
4503 auto_diagnostic_group d;
4504 if (warning_at (location, OPT_Wbool_operation,
4505 "%<~%> on a boolean expression"))
4506 {
4507 gcc_rich_location richloc (location);
4508 richloc.add_fixit_insert_before (location, "!");
4509 inform (&richloc, "did you mean to use logical not?");
4510 }
4511 }
4512 if (!noconvert)
4513 arg = default_conversion (arg);
4514 }
4515 else if (typecode == COMPLEX_TYPE)
4516 {
4517 code = CONJ_EXPR;
4518 pedwarn (location, OPT_Wpedantic,
4519 "ISO C does not support %<~%> for complex conjugation");
4520 if (!noconvert)
4521 arg = default_conversion (arg);
4522 }
4523 else
4524 {
4525 error_at (location, "wrong type argument to bit-complement");
4526 return error_mark_node;
4527 }
4528 break;
4529
4530 case ABS_EXPR:
4531 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4532 {
4533 error_at (location, "wrong type argument to abs");
4534 return error_mark_node;
4535 }
4536 else if (!noconvert)
4537 arg = default_conversion (arg);
4538 break;
4539
4540 case ABSU_EXPR:
4541 if (!(typecode == INTEGER_TYPE))
4542 {
4543 error_at (location, "wrong type argument to absu");
4544 return error_mark_node;
4545 }
4546 else if (!noconvert)
4547 arg = default_conversion (arg);
4548 break;
4549
4550 case CONJ_EXPR:
4551 /* Conjugating a real value is a no-op, but allow it anyway. */
4552 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4553 || typecode == COMPLEX_TYPE))
4554 {
4555 error_at (location, "wrong type argument to conjugation");
4556 return error_mark_node;
4557 }
4558 else if (!noconvert)
4559 arg = default_conversion (arg);
4560 break;
4561
4562 case TRUTH_NOT_EXPR:
4563 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4564 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4565 && typecode != COMPLEX_TYPE)
4566 {
4567 error_at (location,
4568 "wrong type argument to unary exclamation mark");
4569 return error_mark_node;
4570 }
4571 if (int_operands)
4572 {
4573 arg = c_objc_common_truthvalue_conversion (location, xarg);
4574 arg = remove_c_maybe_const_expr (arg);
4575 }
4576 else
4577 arg = c_objc_common_truthvalue_conversion (location, arg);
4578 ret = invert_truthvalue_loc (location, arg);
4579 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4580 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4581 location = EXPR_LOCATION (ret);
4582 goto return_build_unary_op;
4583
4584 case REALPART_EXPR:
4585 case IMAGPART_EXPR:
4586 ret = build_real_imag_expr (location, code, arg);
4587 if (ret == error_mark_node)
4588 return error_mark_node;
4589 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4590 eptype = TREE_TYPE (eptype);
4591 goto return_build_unary_op;
4592
4593 case PREINCREMENT_EXPR:
4594 case POSTINCREMENT_EXPR:
4595 case PREDECREMENT_EXPR:
4596 case POSTDECREMENT_EXPR:
4597
4598 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4599 {
4600 tree inner = build_unary_op (location, code,
4601 C_MAYBE_CONST_EXPR_EXPR (arg),
4602 noconvert);
4603 if (inner == error_mark_node)
4604 return error_mark_node;
4605 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4606 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4607 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4608 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4609 goto return_build_unary_op;
4610 }
4611
4612 /* Complain about anything that is not a true lvalue. In
4613 Objective-C, skip this check for property_refs. */
4614 if (!objc_is_property_ref (arg)
4615 && !lvalue_or_else (location,
4616 arg, ((code == PREINCREMENT_EXPR
4617 || code == POSTINCREMENT_EXPR)
4618 ? lv_increment
4619 : lv_decrement)))
4620 return error_mark_node;
4621
4622 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4623 {
4624 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4625 warning_at (location, OPT_Wc___compat,
4626 "increment of enumeration value is invalid in C++");
4627 else
4628 warning_at (location, OPT_Wc___compat,
4629 "decrement of enumeration value is invalid in C++");
4630 }
4631
4632 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4633 {
4634 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4635 warning_at (location, OPT_Wbool_operation,
4636 "increment of a boolean expression");
4637 else
4638 warning_at (location, OPT_Wbool_operation,
4639 "decrement of a boolean expression");
4640 }
4641
4642 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4643 arg = c_fully_fold (arg, false, NULL, true);
4644
4645 bool atomic_op;
4646 atomic_op = really_atomic_lvalue (arg);
4647
4648 /* Increment or decrement the real part of the value,
4649 and don't change the imaginary part. */
4650 if (typecode == COMPLEX_TYPE)
4651 {
4652 tree real, imag;
4653
4654 pedwarn (location, OPT_Wpedantic,
4655 "ISO C does not support %<++%> and %<--%> on complex types");
4656
4657 if (!atomic_op)
4658 {
4659 arg = stabilize_reference (arg);
4660 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4661 true);
4662 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4663 true);
4664 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4665 if (real == error_mark_node || imag == error_mark_node)
4666 return error_mark_node;
4667 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4668 real, imag);
4669 goto return_build_unary_op;
4670 }
4671 }
4672
4673 /* Report invalid types. */
4674
4675 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4676 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4677 && typecode != COMPLEX_TYPE
4678 && !gnu_vector_type_p (TREE_TYPE (arg)))
4679 {
4680 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4681 error_at (location, "wrong type argument to increment");
4682 else
4683 error_at (location, "wrong type argument to decrement");
4684
4685 return error_mark_node;
4686 }
4687
4688 {
4689 tree inc;
4690
4691 argtype = TREE_TYPE (arg);
4692
4693 /* Compute the increment. */
4694
4695 if (typecode == POINTER_TYPE)
4696 {
4697 /* If pointer target is an incomplete type,
4698 we just cannot know how to do the arithmetic. */
4699 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4700 {
4701 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4702 error_at (location,
4703 "increment of pointer to an incomplete type %qT",
4704 TREE_TYPE (argtype));
4705 else
4706 error_at (location,
4707 "decrement of pointer to an incomplete type %qT",
4708 TREE_TYPE (argtype));
4709 }
4710 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4711 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4712 {
4713 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4714 pedwarn (location, OPT_Wpointer_arith,
4715 "wrong type argument to increment");
4716 else
4717 pedwarn (location, OPT_Wpointer_arith,
4718 "wrong type argument to decrement");
4719 }
4720 else
4721 verify_type_context (location, TCTX_POINTER_ARITH,
4722 TREE_TYPE (argtype));
4723
4724 inc = c_size_in_bytes (TREE_TYPE (argtype));
4725 inc = convert_to_ptrofftype_loc (location, inc);
4726 }
4727 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4728 {
4729 /* For signed fract types, we invert ++ to -- or
4730 -- to ++, and change inc from 1 to -1, because
4731 it is not possible to represent 1 in signed fract constants.
4732 For unsigned fract types, the result always overflows and
4733 we get an undefined (original) or the maximum value. */
4734 if (code == PREINCREMENT_EXPR)
4735 code = PREDECREMENT_EXPR;
4736 else if (code == PREDECREMENT_EXPR)
4737 code = PREINCREMENT_EXPR;
4738 else if (code == POSTINCREMENT_EXPR)
4739 code = POSTDECREMENT_EXPR;
4740 else /* code == POSTDECREMENT_EXPR */
4741 code = POSTINCREMENT_EXPR;
4742
4743 inc = integer_minus_one_node;
4744 inc = convert (argtype, inc);
4745 }
4746 else
4747 {
4748 inc = VECTOR_TYPE_P (argtype)
4749 ? build_one_cst (argtype)
4750 : integer_one_node;
4751 inc = convert (argtype, inc);
4752 }
4753
4754 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4755 need to ask Objective-C to build the increment or decrement
4756 expression for it. */
4757 if (objc_is_property_ref (arg))
4758 return objc_build_incr_expr_for_property_ref (location, code,
4759 arg, inc);
4760
4761 /* Report a read-only lvalue. */
4762 if (TYPE_READONLY (argtype))
4763 {
4764 readonly_error (location, arg,
4765 ((code == PREINCREMENT_EXPR
4766 || code == POSTINCREMENT_EXPR)
4767 ? lv_increment : lv_decrement));
4768 return error_mark_node;
4769 }
4770 else if (TREE_READONLY (arg))
4771 readonly_warning (arg,
4772 ((code == PREINCREMENT_EXPR
4773 || code == POSTINCREMENT_EXPR)
4774 ? lv_increment : lv_decrement));
4775
4776 /* If the argument is atomic, use the special code sequences for
4777 atomic compound assignment. */
4778 if (atomic_op)
4779 {
4780 arg = stabilize_reference (arg);
4781 ret = build_atomic_assign (location, arg,
4782 ((code == PREINCREMENT_EXPR
4783 || code == POSTINCREMENT_EXPR)
4784 ? PLUS_EXPR
4785 : MINUS_EXPR),
4786 (FRACT_MODE_P (TYPE_MODE (argtype))
4787 ? inc
4788 : integer_one_node),
4789 (code == POSTINCREMENT_EXPR
4790 || code == POSTDECREMENT_EXPR));
4791 goto return_build_unary_op;
4792 }
4793
4794 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4795 val = boolean_increment (code, arg);
4796 else
4797 val = build2 (code, TREE_TYPE (arg), arg, inc);
4798 TREE_SIDE_EFFECTS (val) = 1;
4799 ret = val;
4800 goto return_build_unary_op;
4801 }
4802
4803 case ADDR_EXPR:
4804 /* Note that this operation never does default_conversion. */
4805
4806 /* The operand of unary '&' must be an lvalue (which excludes
4807 expressions of type void), or, in C99, the result of a [] or
4808 unary '*' operator. */
4809 if (VOID_TYPE_P (TREE_TYPE (arg))
4810 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4811 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4812 pedwarn (location, 0, "taking address of expression of type %<void%>");
4813
4814 /* Let &* cancel out to simplify resulting code. */
4815 if (INDIRECT_REF_P (arg))
4816 {
4817 /* Don't let this be an lvalue. */
4818 if (lvalue_p (TREE_OPERAND (arg, 0)))
4819 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4820 ret = TREE_OPERAND (arg, 0);
4821 goto return_build_unary_op;
4822 }
4823
4824 /* Anything not already handled and not a true memory reference
4825 or a non-lvalue array is an error. */
4826 if (typecode != FUNCTION_TYPE && !noconvert
4827 && !lvalue_or_else (location, arg, lv_addressof))
4828 return error_mark_node;
4829
4830 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4831 folding later. */
4832 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4833 {
4834 tree inner = build_unary_op (location, code,
4835 C_MAYBE_CONST_EXPR_EXPR (arg),
4836 noconvert);
4837 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4838 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4839 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4840 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4841 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4842 goto return_build_unary_op;
4843 }
4844
4845 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4846 argtype = TREE_TYPE (arg);
4847
4848 /* If the lvalue is const or volatile, merge that into the type
4849 to which the address will point. This is only needed
4850 for function types. */
4851 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4852 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4853 && TREE_CODE (argtype) == FUNCTION_TYPE)
4854 {
4855 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4856 int quals = orig_quals;
4857
4858 if (TREE_READONLY (arg))
4859 quals |= TYPE_QUAL_CONST;
4860 if (TREE_THIS_VOLATILE (arg))
4861 quals |= TYPE_QUAL_VOLATILE;
4862
4863 argtype = c_build_qualified_type (argtype, quals);
4864 }
4865
4866 switch (TREE_CODE (arg))
4867 {
4868 case COMPONENT_REF:
4869 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4870 {
4871 error_at (location, "cannot take address of bit-field %qD",
4872 TREE_OPERAND (arg, 1));
4873 return error_mark_node;
4874 }
4875
4876 /* fall through */
4877
4878 case ARRAY_REF:
4879 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4880 {
4881 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4882 && !POINTER_TYPE_P (TREE_TYPE (arg))
4883 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4884 {
4885 error_at (location, "cannot take address of scalar with "
4886 "reverse storage order");
4887 return error_mark_node;
4888 }
4889
4890 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4891 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4892 warning_at (location, OPT_Wscalar_storage_order,
4893 "address of array with reverse scalar storage "
4894 "order requested");
4895 }
4896
4897 default:
4898 break;
4899 }
4900
4901 if (!c_mark_addressable (arg))
4902 return error_mark_node;
4903
4904 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4905 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4906
4907 argtype = build_pointer_type (argtype);
4908
4909 /* ??? Cope with user tricks that amount to offsetof. Delete this
4910 when we have proper support for integer constant expressions. */
4911 val = get_base_address (arg);
4912 if (val && INDIRECT_REF_P (val)
4913 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4914 {
4915 ret = fold_offsetof (arg, argtype);
4916 goto return_build_unary_op;
4917 }
4918
4919 val = build1 (ADDR_EXPR, argtype, arg);
4920
4921 ret = val;
4922 goto return_build_unary_op;
4923
4924 case PAREN_EXPR:
4925 ret = build1 (code, TREE_TYPE (arg), arg);
4926 goto return_build_unary_op;
4927
4928 default:
4929 gcc_unreachable ();
4930 }
4931
4932 if (argtype == NULL_TREE)
4933 argtype = TREE_TYPE (arg);
4934 if (TREE_CODE (arg) == INTEGER_CST)
4935 ret = (require_constant_value
4936 ? fold_build1_initializer_loc (location, code, argtype, arg)
4937 : fold_build1_loc (location, code, argtype, arg));
4938 else
4939 ret = build1 (code, argtype, arg);
4940 return_build_unary_op:
4941 gcc_assert (ret != error_mark_node);
4942 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4943 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4944 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4945 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4946 ret = note_integer_operands (ret);
4947 if (eptype)
4948 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4949 protected_set_expr_location (ret, location);
4950 return ret;
4951 }
4952
4953 /* Return nonzero if REF is an lvalue valid for this language.
4954 Lvalues can be assigned, unless their type has TYPE_READONLY.
4955 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4956
4957 bool
lvalue_p(const_tree ref)4958 lvalue_p (const_tree ref)
4959 {
4960 const enum tree_code code = TREE_CODE (ref);
4961
4962 switch (code)
4963 {
4964 case REALPART_EXPR:
4965 case IMAGPART_EXPR:
4966 case COMPONENT_REF:
4967 return lvalue_p (TREE_OPERAND (ref, 0));
4968
4969 case C_MAYBE_CONST_EXPR:
4970 return lvalue_p (TREE_OPERAND (ref, 1));
4971
4972 case COMPOUND_LITERAL_EXPR:
4973 case STRING_CST:
4974 return true;
4975
4976 case MEM_REF:
4977 case TARGET_MEM_REF:
4978 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
4979 here as well. */
4980 case INDIRECT_REF:
4981 case ARRAY_REF:
4982 case VAR_DECL:
4983 case PARM_DECL:
4984 case RESULT_DECL:
4985 case ERROR_MARK:
4986 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4987 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4988
4989 case BIND_EXPR:
4990 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4991
4992 default:
4993 return false;
4994 }
4995 }
4996
4997 /* Give a warning for storing in something that is read-only in GCC
4998 terms but not const in ISO C terms. */
4999
5000 static void
readonly_warning(tree arg,enum lvalue_use use)5001 readonly_warning (tree arg, enum lvalue_use use)
5002 {
5003 switch (use)
5004 {
5005 case lv_assign:
5006 warning (0, "assignment of read-only location %qE", arg);
5007 break;
5008 case lv_increment:
5009 warning (0, "increment of read-only location %qE", arg);
5010 break;
5011 case lv_decrement:
5012 warning (0, "decrement of read-only location %qE", arg);
5013 break;
5014 default:
5015 gcc_unreachable ();
5016 }
5017 return;
5018 }
5019
5020
5021 /* Return nonzero if REF is an lvalue valid for this language;
5022 otherwise, print an error message and return zero. USE says
5023 how the lvalue is being used and so selects the error message.
5024 LOCATION is the location at which any error should be reported. */
5025
5026 static int
lvalue_or_else(location_t loc,const_tree ref,enum lvalue_use use)5027 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5028 {
5029 int win = lvalue_p (ref);
5030
5031 if (!win)
5032 lvalue_error (loc, use);
5033
5034 return win;
5035 }
5036
5037 /* Mark EXP saying that we need to be able to take the
5038 address of it; it should not be allocated in a register.
5039 Returns true if successful. ARRAY_REF_P is true if this
5040 is for ARRAY_REF construction - in that case we don't want
5041 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5042 it is fine to use ARRAY_REFs for vector subscripts on vector
5043 register variables. */
5044
5045 bool
c_mark_addressable(tree exp,bool array_ref_p)5046 c_mark_addressable (tree exp, bool array_ref_p)
5047 {
5048 tree x = exp;
5049
5050 while (1)
5051 switch (TREE_CODE (x))
5052 {
5053 case VIEW_CONVERT_EXPR:
5054 if (array_ref_p
5055 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5056 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5057 return true;
5058 x = TREE_OPERAND (x, 0);
5059 break;
5060
5061 case COMPONENT_REF:
5062 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5063 {
5064 error ("cannot take address of bit-field %qD",
5065 TREE_OPERAND (x, 1));
5066 return false;
5067 }
5068 /* FALLTHRU */
5069 case ADDR_EXPR:
5070 case ARRAY_REF:
5071 case REALPART_EXPR:
5072 case IMAGPART_EXPR:
5073 x = TREE_OPERAND (x, 0);
5074 break;
5075
5076 case COMPOUND_LITERAL_EXPR:
5077 TREE_ADDRESSABLE (x) = 1;
5078 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5079 return true;
5080
5081 case CONSTRUCTOR:
5082 TREE_ADDRESSABLE (x) = 1;
5083 return true;
5084
5085 case VAR_DECL:
5086 case CONST_DECL:
5087 case PARM_DECL:
5088 case RESULT_DECL:
5089 if (C_DECL_REGISTER (x)
5090 && DECL_NONLOCAL (x))
5091 {
5092 if (TREE_PUBLIC (x) || is_global_var (x))
5093 {
5094 error
5095 ("global register variable %qD used in nested function", x);
5096 return false;
5097 }
5098 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5099 }
5100 else if (C_DECL_REGISTER (x))
5101 {
5102 if (TREE_PUBLIC (x) || is_global_var (x))
5103 error ("address of global register variable %qD requested", x);
5104 else
5105 error ("address of register variable %qD requested", x);
5106 return false;
5107 }
5108
5109 /* FALLTHRU */
5110 case FUNCTION_DECL:
5111 TREE_ADDRESSABLE (x) = 1;
5112 /* FALLTHRU */
5113 default:
5114 return true;
5115 }
5116 }
5117
5118 /* Convert EXPR to TYPE, warning about conversion problems with
5119 constants. SEMANTIC_TYPE is the type this conversion would use
5120 without excess precision. If SEMANTIC_TYPE is NULL, this function
5121 is equivalent to convert_and_check. This function is a wrapper that
5122 handles conversions that may be different than
5123 the usual ones because of excess precision. */
5124
5125 static tree
ep_convert_and_check(location_t loc,tree type,tree expr,tree semantic_type)5126 ep_convert_and_check (location_t loc, tree type, tree expr,
5127 tree semantic_type)
5128 {
5129 if (TREE_TYPE (expr) == type)
5130 return expr;
5131
5132 /* For C11, integer conversions may have results with excess
5133 precision. */
5134 if (flag_isoc11 || !semantic_type)
5135 return convert_and_check (loc, type, expr);
5136
5137 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5138 && TREE_TYPE (expr) != semantic_type)
5139 {
5140 /* For integers, we need to check the real conversion, not
5141 the conversion to the excess precision type. */
5142 expr = convert_and_check (loc, semantic_type, expr);
5143 }
5144 /* Result type is the excess precision type, which should be
5145 large enough, so do not check. */
5146 return convert (type, expr);
5147 }
5148
5149 /* If EXPR refers to a built-in declared without a prototype returns
5150 the actual type of the built-in and, if non-null, set *BLTIN to
5151 a pointer to the built-in. Otherwise return the type of EXPR
5152 and clear *BLTIN if non-null. */
5153
5154 static tree
type_or_builtin_type(tree expr,tree * bltin=NULL)5155 type_or_builtin_type (tree expr, tree *bltin = NULL)
5156 {
5157 tree dummy;
5158 if (!bltin)
5159 bltin = &dummy;
5160
5161 *bltin = NULL_TREE;
5162
5163 tree type = TREE_TYPE (expr);
5164 if (TREE_CODE (expr) != ADDR_EXPR)
5165 return type;
5166
5167 tree oper = TREE_OPERAND (expr, 0);
5168 if (!DECL_P (oper)
5169 || TREE_CODE (oper) != FUNCTION_DECL
5170 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5171 return type;
5172
5173 built_in_function code = DECL_FUNCTION_CODE (oper);
5174 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5175 return type;
5176
5177 if ((*bltin = builtin_decl_implicit (code)))
5178 type = build_pointer_type (TREE_TYPE (*bltin));
5179
5180 return type;
5181 }
5182
5183 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5184 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5185 if folded to an integer constant then the unselected half may
5186 contain arbitrary operations not normally permitted in constant
5187 expressions. Set the location of the expression to LOC. */
5188
5189 tree
build_conditional_expr(location_t colon_loc,tree ifexp,bool ifexp_bcp,tree op1,tree op1_original_type,location_t op1_loc,tree op2,tree op2_original_type,location_t op2_loc)5190 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5191 tree op1, tree op1_original_type, location_t op1_loc,
5192 tree op2, tree op2_original_type, location_t op2_loc)
5193 {
5194 tree type1;
5195 tree type2;
5196 enum tree_code code1;
5197 enum tree_code code2;
5198 tree result_type = NULL;
5199 tree semantic_result_type = NULL;
5200 tree orig_op1 = op1, orig_op2 = op2;
5201 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5202 bool ifexp_int_operands;
5203 tree ret;
5204
5205 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5206 if (op1_int_operands)
5207 op1 = remove_c_maybe_const_expr (op1);
5208 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5209 if (op2_int_operands)
5210 op2 = remove_c_maybe_const_expr (op2);
5211 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5212 if (ifexp_int_operands)
5213 ifexp = remove_c_maybe_const_expr (ifexp);
5214
5215 /* Promote both alternatives. */
5216
5217 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5218 op1 = default_conversion (op1);
5219 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5220 op2 = default_conversion (op2);
5221
5222 if (TREE_CODE (ifexp) == ERROR_MARK
5223 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5224 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5225 return error_mark_node;
5226
5227 tree bltin1 = NULL_TREE;
5228 tree bltin2 = NULL_TREE;
5229 type1 = type_or_builtin_type (op1, &bltin1);
5230 code1 = TREE_CODE (type1);
5231 type2 = type_or_builtin_type (op2, &bltin2);
5232 code2 = TREE_CODE (type2);
5233
5234 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5235 return error_mark_node;
5236
5237 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5238 return error_mark_node;
5239
5240 /* C90 does not permit non-lvalue arrays in conditional expressions.
5241 In C99 they will be pointers by now. */
5242 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5243 {
5244 error_at (colon_loc, "non-lvalue array in conditional expression");
5245 return error_mark_node;
5246 }
5247
5248 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5249 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5250 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5251 || code1 == COMPLEX_TYPE)
5252 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5253 || code2 == COMPLEX_TYPE))
5254 {
5255 semantic_result_type = c_common_type (type1, type2);
5256 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5257 {
5258 op1 = TREE_OPERAND (op1, 0);
5259 type1 = TREE_TYPE (op1);
5260 gcc_assert (TREE_CODE (type1) == code1);
5261 }
5262 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5263 {
5264 op2 = TREE_OPERAND (op2, 0);
5265 type2 = TREE_TYPE (op2);
5266 gcc_assert (TREE_CODE (type2) == code2);
5267 }
5268 }
5269
5270 if (warn_cxx_compat)
5271 {
5272 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5273 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5274
5275 if (TREE_CODE (t1) == ENUMERAL_TYPE
5276 && TREE_CODE (t2) == ENUMERAL_TYPE
5277 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5278 warning_at (colon_loc, OPT_Wc___compat,
5279 ("different enum types in conditional is "
5280 "invalid in C++: %qT vs %qT"),
5281 t1, t2);
5282 }
5283
5284 /* Quickly detect the usual case where op1 and op2 have the same type
5285 after promotion. */
5286 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5287 {
5288 if (type1 == type2)
5289 result_type = type1;
5290 else
5291 result_type = TYPE_MAIN_VARIANT (type1);
5292 }
5293 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5294 || code1 == COMPLEX_TYPE)
5295 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5296 || code2 == COMPLEX_TYPE))
5297 {
5298 /* In C11, a conditional expression between a floating-point
5299 type and an integer type should convert the integer type to
5300 the evaluation format of the floating-point type, with
5301 possible excess precision. */
5302 tree eptype1 = type1;
5303 tree eptype2 = type2;
5304 if (flag_isoc11)
5305 {
5306 tree eptype;
5307 if (ANY_INTEGRAL_TYPE_P (type1)
5308 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5309 {
5310 eptype2 = eptype;
5311 if (!semantic_result_type)
5312 semantic_result_type = c_common_type (type1, type2);
5313 }
5314 else if (ANY_INTEGRAL_TYPE_P (type2)
5315 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5316 {
5317 eptype1 = eptype;
5318 if (!semantic_result_type)
5319 semantic_result_type = c_common_type (type1, type2);
5320 }
5321 }
5322 result_type = c_common_type (eptype1, eptype2);
5323 if (result_type == error_mark_node)
5324 return error_mark_node;
5325 do_warn_double_promotion (result_type, type1, type2,
5326 "implicit conversion from %qT to %qT to "
5327 "match other result of conditional",
5328 colon_loc);
5329
5330 /* If -Wsign-compare, warn here if type1 and type2 have
5331 different signedness. We'll promote the signed to unsigned
5332 and later code won't know it used to be different.
5333 Do this check on the original types, so that explicit casts
5334 will be considered, but default promotions won't. */
5335 if (c_inhibit_evaluation_warnings == 0)
5336 {
5337 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5338 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5339
5340 if (unsigned_op1 ^ unsigned_op2)
5341 {
5342 bool ovf;
5343
5344 /* Do not warn if the result type is signed, since the
5345 signed type will only be chosen if it can represent
5346 all the values of the unsigned type. */
5347 if (!TYPE_UNSIGNED (result_type))
5348 /* OK */;
5349 else
5350 {
5351 bool op1_maybe_const = true;
5352 bool op2_maybe_const = true;
5353
5354 /* Do not warn if the signed quantity is an
5355 unsuffixed integer literal (or some static
5356 constant expression involving such literals) and
5357 it is non-negative. This warning requires the
5358 operands to be folded for best results, so do
5359 that folding in this case even without
5360 warn_sign_compare to avoid warning options
5361 possibly affecting code generation. */
5362 c_inhibit_evaluation_warnings
5363 += (ifexp == truthvalue_false_node);
5364 op1 = c_fully_fold (op1, require_constant_value,
5365 &op1_maybe_const);
5366 c_inhibit_evaluation_warnings
5367 -= (ifexp == truthvalue_false_node);
5368
5369 c_inhibit_evaluation_warnings
5370 += (ifexp == truthvalue_true_node);
5371 op2 = c_fully_fold (op2, require_constant_value,
5372 &op2_maybe_const);
5373 c_inhibit_evaluation_warnings
5374 -= (ifexp == truthvalue_true_node);
5375
5376 if (warn_sign_compare)
5377 {
5378 if ((unsigned_op2
5379 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5380 || (unsigned_op1
5381 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5382 /* OK */;
5383 else if (unsigned_op2)
5384 warning_at (op1_loc, OPT_Wsign_compare,
5385 "operand of %<?:%> changes signedness from "
5386 "%qT to %qT due to unsignedness of other "
5387 "operand", TREE_TYPE (orig_op1),
5388 TREE_TYPE (orig_op2));
5389 else
5390 warning_at (op2_loc, OPT_Wsign_compare,
5391 "operand of %<?:%> changes signedness from "
5392 "%qT to %qT due to unsignedness of other "
5393 "operand", TREE_TYPE (orig_op2),
5394 TREE_TYPE (orig_op1));
5395 }
5396 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5397 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5398 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5399 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5400 }
5401 }
5402 }
5403 }
5404 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5405 {
5406 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5407 pedwarn (colon_loc, OPT_Wpedantic,
5408 "ISO C forbids conditional expr with only one void side");
5409 result_type = void_type_node;
5410 }
5411 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5412 {
5413 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5414 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5415 addr_space_t as_common;
5416
5417 if (comp_target_types (colon_loc, type1, type2))
5418 result_type = common_pointer_type (type1, type2);
5419 else if (null_pointer_constant_p (orig_op1))
5420 result_type = type2;
5421 else if (null_pointer_constant_p (orig_op2))
5422 result_type = type1;
5423 else if (!addr_space_superset (as1, as2, &as_common))
5424 {
5425 error_at (colon_loc, "pointers to disjoint address spaces "
5426 "used in conditional expression");
5427 return error_mark_node;
5428 }
5429 else if ((VOID_TYPE_P (TREE_TYPE (type1))
5430 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5431 || (VOID_TYPE_P (TREE_TYPE (type2))
5432 && !TYPE_ATOMIC (TREE_TYPE (type2))))
5433 {
5434 tree t1 = TREE_TYPE (type1);
5435 tree t2 = TREE_TYPE (type2);
5436 if (!(VOID_TYPE_P (t1)
5437 && !TYPE_ATOMIC (t1)))
5438 {
5439 /* roles are swapped */
5440 t1 = t2;
5441 t2 = TREE_TYPE (type1);
5442 }
5443 tree t2_stripped = strip_array_types (t2);
5444 if ((TREE_CODE (t2) == ARRAY_TYPE)
5445 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5446 {
5447 if (!flag_isoc2x)
5448 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5449 "pointer to array loses qualifier "
5450 "in conditional expression");
5451 else if (warn_c11_c2x_compat > 0)
5452 warning_at (colon_loc, OPT_Wc11_c2x_compat,
5453 "pointer to array loses qualifier "
5454 "in conditional expression in ISO C before C2X");
5455 }
5456 if (TREE_CODE (t2) == FUNCTION_TYPE)
5457 pedwarn (colon_loc, OPT_Wpedantic,
5458 "ISO C forbids conditional expr between "
5459 "%<void *%> and function pointer");
5460 /* for array, use qualifiers of element type */
5461 if (flag_isoc2x)
5462 t2 = t2_stripped;
5463 result_type = build_pointer_type (qualify_type (t1, t2));
5464 }
5465 /* Objective-C pointer comparisons are a bit more lenient. */
5466 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5467 result_type = objc_common_type (type1, type2);
5468 else
5469 {
5470 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5471 if (bltin1 && bltin2)
5472 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5473 "pointer type mismatch between %qT and %qT "
5474 "of %qD and %qD in conditional expression",
5475 type1, type2, bltin1, bltin2);
5476 else
5477 pedwarn (colon_loc, 0,
5478 "pointer type mismatch in conditional expression");
5479 result_type = build_pointer_type
5480 (build_qualified_type (void_type_node, qual));
5481 }
5482 }
5483 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5484 {
5485 if (!null_pointer_constant_p (orig_op2))
5486 pedwarn (colon_loc, 0,
5487 "pointer/integer type mismatch in conditional expression");
5488 else
5489 {
5490 op2 = null_pointer_node;
5491 }
5492 result_type = type1;
5493 }
5494 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5495 {
5496 if (!null_pointer_constant_p (orig_op1))
5497 pedwarn (colon_loc, 0,
5498 "pointer/integer type mismatch in conditional expression");
5499 else
5500 {
5501 op1 = null_pointer_node;
5502 }
5503 result_type = type2;
5504 }
5505
5506 if (!result_type)
5507 {
5508 if (flag_cond_mismatch)
5509 result_type = void_type_node;
5510 else
5511 {
5512 error_at (colon_loc, "type mismatch in conditional expression");
5513 return error_mark_node;
5514 }
5515 }
5516
5517 /* Merge const and volatile flags of the incoming types. */
5518 result_type
5519 = build_type_variant (result_type,
5520 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5521 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5522
5523 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5524 semantic_result_type);
5525 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5526 semantic_result_type);
5527
5528 if (ifexp_bcp && ifexp == truthvalue_true_node)
5529 {
5530 op2_int_operands = true;
5531 op1 = c_fully_fold (op1, require_constant_value, NULL);
5532 }
5533 if (ifexp_bcp && ifexp == truthvalue_false_node)
5534 {
5535 op1_int_operands = true;
5536 op2 = c_fully_fold (op2, require_constant_value, NULL);
5537 }
5538 int_const = int_operands = (ifexp_int_operands
5539 && op1_int_operands
5540 && op2_int_operands);
5541 if (int_operands)
5542 {
5543 int_const = ((ifexp == truthvalue_true_node
5544 && TREE_CODE (orig_op1) == INTEGER_CST
5545 && !TREE_OVERFLOW (orig_op1))
5546 || (ifexp == truthvalue_false_node
5547 && TREE_CODE (orig_op2) == INTEGER_CST
5548 && !TREE_OVERFLOW (orig_op2)));
5549 }
5550
5551 /* Need to convert condition operand into a vector mask. */
5552 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5553 {
5554 tree vectype = TREE_TYPE (ifexp);
5555 tree elem_type = TREE_TYPE (vectype);
5556 tree zero = build_int_cst (elem_type, 0);
5557 tree zero_vec = build_vector_from_val (vectype, zero);
5558 tree cmp_type = truth_type_for (vectype);
5559 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5560 }
5561
5562 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5563 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5564 else
5565 {
5566 if (int_operands)
5567 {
5568 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5569 nested inside of the expression. */
5570 op1 = c_fully_fold (op1, false, NULL);
5571 op2 = c_fully_fold (op2, false, NULL);
5572 }
5573 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5574 if (int_operands)
5575 ret = note_integer_operands (ret);
5576 }
5577 if (semantic_result_type)
5578 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5579
5580 protected_set_expr_location (ret, colon_loc);
5581
5582 /* If the OP1 and OP2 are the same and don't have side-effects,
5583 warn here, because the COND_EXPR will be turned into OP1. */
5584 if (warn_duplicated_branches
5585 && TREE_CODE (ret) == COND_EXPR
5586 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5587 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5588 "this condition has identical branches");
5589
5590 return ret;
5591 }
5592
5593 /* EXPR is an expression, location LOC, whose result is discarded.
5594 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5595 whose right-hand operand is such a call, possibly recursively). */
5596
5597 static void
maybe_warn_nodiscard(location_t loc,tree expr)5598 maybe_warn_nodiscard (location_t loc, tree expr)
5599 {
5600 if (VOID_TYPE_P (TREE_TYPE (expr)))
5601 return;
5602 while (TREE_CODE (expr) == COMPOUND_EXPR)
5603 {
5604 expr = TREE_OPERAND (expr, 1);
5605 if (EXPR_HAS_LOCATION (expr))
5606 loc = EXPR_LOCATION (expr);
5607 }
5608 if (TREE_CODE (expr) != CALL_EXPR)
5609 return;
5610 tree fn = CALL_EXPR_FN (expr);
5611 if (!fn)
5612 return;
5613 tree attr;
5614 if (TREE_CODE (fn) == ADDR_EXPR
5615 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5616 && (attr = lookup_attribute ("nodiscard",
5617 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5618 {
5619 fn = TREE_OPERAND (fn, 0);
5620 tree args = TREE_VALUE (attr);
5621 if (args)
5622 args = TREE_VALUE (args);
5623 auto_diagnostic_group d;
5624 int warned;
5625 if (args)
5626 warned = warning_at (loc, OPT_Wunused_result,
5627 "ignoring return value of %qD, declared with "
5628 "attribute %<nodiscard%>: %E", fn, args);
5629 else
5630 warned = warning_at (loc, OPT_Wunused_result,
5631 "ignoring return value of %qD, declared with "
5632 "attribute %<nodiscard%>", fn);
5633 if (warned)
5634 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5635 }
5636 else
5637 {
5638 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5639 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5640 if (!attr)
5641 return;
5642 tree args = TREE_VALUE (attr);
5643 if (args)
5644 args = TREE_VALUE (args);
5645 auto_diagnostic_group d;
5646 int warned;
5647 if (args)
5648 warned = warning_at (loc, OPT_Wunused_result,
5649 "ignoring return value of type %qT, declared "
5650 "with attribute %<nodiscard%>: %E",
5651 rettype, args);
5652 else
5653 warned = warning_at (loc, OPT_Wunused_result,
5654 "ignoring return value of type %qT, declared "
5655 "with attribute %<nodiscard%>", rettype);
5656 if (warned)
5657 {
5658 if (TREE_CODE (fn) == ADDR_EXPR)
5659 {
5660 fn = TREE_OPERAND (fn, 0);
5661 if (TREE_CODE (fn) == FUNCTION_DECL)
5662 inform (DECL_SOURCE_LOCATION (fn),
5663 "in call to %qD, declared here", fn);
5664 }
5665 }
5666 }
5667 }
5668
5669 /* Return a compound expression that performs two expressions and
5670 returns the value of the second of them.
5671
5672 LOC is the location of the COMPOUND_EXPR. */
5673
5674 tree
build_compound_expr(location_t loc,tree expr1,tree expr2)5675 build_compound_expr (location_t loc, tree expr1, tree expr2)
5676 {
5677 bool expr1_int_operands, expr2_int_operands;
5678 tree eptype = NULL_TREE;
5679 tree ret;
5680
5681 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5682 if (expr1_int_operands)
5683 expr1 = remove_c_maybe_const_expr (expr1);
5684 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5685 if (expr2_int_operands)
5686 expr2 = remove_c_maybe_const_expr (expr2);
5687
5688 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5689 expr1 = TREE_OPERAND (expr1, 0);
5690 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5691 {
5692 eptype = TREE_TYPE (expr2);
5693 expr2 = TREE_OPERAND (expr2, 0);
5694 }
5695
5696 if (!TREE_SIDE_EFFECTS (expr1))
5697 {
5698 /* The left-hand operand of a comma expression is like an expression
5699 statement: with -Wunused, we should warn if it doesn't have
5700 any side-effects, unless it was explicitly cast to (void). */
5701 if (warn_unused_value)
5702 {
5703 if (VOID_TYPE_P (TREE_TYPE (expr1))
5704 && CONVERT_EXPR_P (expr1))
5705 ; /* (void) a, b */
5706 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5707 && TREE_CODE (expr1) == COMPOUND_EXPR
5708 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5709 ; /* (void) a, (void) b, c */
5710 else
5711 warning_at (loc, OPT_Wunused_value,
5712 "left-hand operand of comma expression has no effect");
5713 }
5714 }
5715 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5716 && warn_unused_value)
5717 {
5718 tree r = expr1;
5719 location_t cloc = loc;
5720 while (TREE_CODE (r) == COMPOUND_EXPR)
5721 {
5722 if (EXPR_HAS_LOCATION (r))
5723 cloc = EXPR_LOCATION (r);
5724 r = TREE_OPERAND (r, 1);
5725 }
5726 if (!TREE_SIDE_EFFECTS (r)
5727 && !VOID_TYPE_P (TREE_TYPE (r))
5728 && !CONVERT_EXPR_P (r))
5729 warning_at (cloc, OPT_Wunused_value,
5730 "right-hand operand of comma expression has no effect");
5731 }
5732
5733 /* With -Wunused, we should also warn if the left-hand operand does have
5734 side-effects, but computes a value which is not used. For example, in
5735 `foo() + bar(), baz()' the result of the `+' operator is not used,
5736 so we should issue a warning. */
5737 else if (warn_unused_value)
5738 warn_if_unused_value (expr1, loc);
5739
5740 maybe_warn_nodiscard (loc, expr1);
5741
5742 if (expr2 == error_mark_node)
5743 return error_mark_node;
5744
5745 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5746
5747 if (flag_isoc99
5748 && expr1_int_operands
5749 && expr2_int_operands)
5750 ret = note_integer_operands (ret);
5751
5752 if (eptype)
5753 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5754
5755 protected_set_expr_location (ret, loc);
5756 return ret;
5757 }
5758
5759 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5760 which we are casting. OTYPE is the type of the expression being
5761 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5762 of the cast. -Wcast-qual appeared on the command line. Named
5763 address space qualifiers are not handled here, because they result
5764 in different warnings. */
5765
5766 static void
handle_warn_cast_qual(location_t loc,tree type,tree otype)5767 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5768 {
5769 tree in_type = type;
5770 tree in_otype = otype;
5771 int added = 0;
5772 int discarded = 0;
5773 bool is_const;
5774
5775 /* Check that the qualifiers on IN_TYPE are a superset of the
5776 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5777 nodes is uninteresting and we stop as soon as we hit a
5778 non-POINTER_TYPE node on either type. */
5779 do
5780 {
5781 in_otype = TREE_TYPE (in_otype);
5782 in_type = TREE_TYPE (in_type);
5783
5784 /* GNU C allows cv-qualified function types. 'const' means the
5785 function is very pure, 'volatile' means it can't return. We
5786 need to warn when such qualifiers are added, not when they're
5787 taken away. */
5788 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5789 && TREE_CODE (in_type) == FUNCTION_TYPE)
5790 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5791 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5792 else
5793 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5794 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5795 }
5796 while (TREE_CODE (in_type) == POINTER_TYPE
5797 && TREE_CODE (in_otype) == POINTER_TYPE);
5798
5799 if (added)
5800 warning_at (loc, OPT_Wcast_qual,
5801 "cast adds %q#v qualifier to function type", added);
5802
5803 if (discarded)
5804 /* There are qualifiers present in IN_OTYPE that are not present
5805 in IN_TYPE. */
5806 warning_at (loc, OPT_Wcast_qual,
5807 "cast discards %qv qualifier from pointer target type",
5808 discarded);
5809
5810 if (added || discarded)
5811 return;
5812
5813 /* A cast from **T to const **T is unsafe, because it can cause a
5814 const value to be changed with no additional warning. We only
5815 issue this warning if T is the same on both sides, and we only
5816 issue the warning if there are the same number of pointers on
5817 both sides, as otherwise the cast is clearly unsafe anyhow. A
5818 cast is unsafe when a qualifier is added at one level and const
5819 is not present at all outer levels.
5820
5821 To issue this warning, we check at each level whether the cast
5822 adds new qualifiers not already seen. We don't need to special
5823 case function types, as they won't have the same
5824 TYPE_MAIN_VARIANT. */
5825
5826 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5827 return;
5828 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5829 return;
5830
5831 in_type = type;
5832 in_otype = otype;
5833 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5834 do
5835 {
5836 in_type = TREE_TYPE (in_type);
5837 in_otype = TREE_TYPE (in_otype);
5838 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5839 && !is_const)
5840 {
5841 warning_at (loc, OPT_Wcast_qual,
5842 "to be safe all intermediate pointers in cast from "
5843 "%qT to %qT must be %<const%> qualified",
5844 otype, type);
5845 break;
5846 }
5847 if (is_const)
5848 is_const = TYPE_READONLY (in_type);
5849 }
5850 while (TREE_CODE (in_type) == POINTER_TYPE);
5851 }
5852
5853 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5854
5855 static bool
c_safe_arg_type_equiv_p(tree t1,tree t2)5856 c_safe_arg_type_equiv_p (tree t1, tree t2)
5857 {
5858 t1 = TYPE_MAIN_VARIANT (t1);
5859 t2 = TYPE_MAIN_VARIANT (t2);
5860
5861 if (TREE_CODE (t1) == POINTER_TYPE
5862 && TREE_CODE (t2) == POINTER_TYPE)
5863 return true;
5864
5865 /* The signedness of the parameter matters only when an integral
5866 type smaller than int is promoted to int, otherwise only the
5867 precision of the parameter matters.
5868 This check should make sure that the callee does not see
5869 undefined values in argument registers. */
5870 if (INTEGRAL_TYPE_P (t1)
5871 && INTEGRAL_TYPE_P (t2)
5872 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5873 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5874 || !targetm.calls.promote_prototypes (NULL_TREE)
5875 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5876 return true;
5877
5878 return comptypes (t1, t2);
5879 }
5880
5881 /* Check if a type cast between two function types can be considered safe. */
5882
5883 static bool
c_safe_function_type_cast_p(tree t1,tree t2)5884 c_safe_function_type_cast_p (tree t1, tree t2)
5885 {
5886 if (TREE_TYPE (t1) == void_type_node &&
5887 TYPE_ARG_TYPES (t1) == void_list_node)
5888 return true;
5889
5890 if (TREE_TYPE (t2) == void_type_node &&
5891 TYPE_ARG_TYPES (t2) == void_list_node)
5892 return true;
5893
5894 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5895 return false;
5896
5897 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5898 t1 && t2;
5899 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5900 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5901 return false;
5902
5903 return true;
5904 }
5905
5906 /* Build an expression representing a cast to type TYPE of expression EXPR.
5907 LOC is the location of the cast-- typically the open paren of the cast. */
5908
5909 tree
build_c_cast(location_t loc,tree type,tree expr)5910 build_c_cast (location_t loc, tree type, tree expr)
5911 {
5912 tree value;
5913
5914 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5915
5916 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5917 expr = TREE_OPERAND (expr, 0);
5918
5919 value = expr;
5920 if (int_operands)
5921 value = remove_c_maybe_const_expr (value);
5922
5923 if (type == error_mark_node || expr == error_mark_node)
5924 return error_mark_node;
5925
5926 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5927 only in <protocol> qualifications. But when constructing cast expressions,
5928 the protocols do matter and must be kept around. */
5929 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5930 return build1 (NOP_EXPR, type, expr);
5931
5932 type = TYPE_MAIN_VARIANT (type);
5933
5934 if (TREE_CODE (type) == ARRAY_TYPE)
5935 {
5936 error_at (loc, "cast specifies array type");
5937 return error_mark_node;
5938 }
5939
5940 if (TREE_CODE (type) == FUNCTION_TYPE)
5941 {
5942 error_at (loc, "cast specifies function type");
5943 return error_mark_node;
5944 }
5945
5946 if (!VOID_TYPE_P (type))
5947 {
5948 value = require_complete_type (loc, value);
5949 if (value == error_mark_node)
5950 return error_mark_node;
5951 }
5952
5953 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5954 {
5955 if (RECORD_OR_UNION_TYPE_P (type))
5956 pedwarn (loc, OPT_Wpedantic,
5957 "ISO C forbids casting nonscalar to the same type");
5958
5959 /* Convert to remove any qualifiers from VALUE's type. */
5960 value = convert (type, value);
5961 }
5962 else if (TREE_CODE (type) == UNION_TYPE)
5963 {
5964 tree field;
5965
5966 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5967 if (TREE_TYPE (field) != error_mark_node
5968 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5969 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5970 break;
5971
5972 if (field)
5973 {
5974 tree t;
5975 bool maybe_const = true;
5976
5977 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5978 t = c_fully_fold (value, false, &maybe_const);
5979 t = build_constructor_single (type, field, t);
5980 if (!maybe_const)
5981 t = c_wrap_maybe_const (t, true);
5982 t = digest_init (loc, type, t,
5983 NULL_TREE, false, true, 0);
5984 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5985 return t;
5986 }
5987 error_at (loc, "cast to union type from type not present in union");
5988 return error_mark_node;
5989 }
5990 else
5991 {
5992 tree otype, ovalue;
5993
5994 if (type == void_type_node)
5995 {
5996 tree t = build1 (CONVERT_EXPR, type, value);
5997 SET_EXPR_LOCATION (t, loc);
5998 return t;
5999 }
6000
6001 otype = TREE_TYPE (value);
6002
6003 /* Optionally warn about potentially worrisome casts. */
6004 if (warn_cast_qual
6005 && TREE_CODE (type) == POINTER_TYPE
6006 && TREE_CODE (otype) == POINTER_TYPE)
6007 handle_warn_cast_qual (loc, type, otype);
6008
6009 /* Warn about conversions between pointers to disjoint
6010 address spaces. */
6011 if (TREE_CODE (type) == POINTER_TYPE
6012 && TREE_CODE (otype) == POINTER_TYPE
6013 && !null_pointer_constant_p (value))
6014 {
6015 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6016 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6017 addr_space_t as_common;
6018
6019 if (!addr_space_superset (as_to, as_from, &as_common))
6020 {
6021 if (ADDR_SPACE_GENERIC_P (as_from))
6022 warning_at (loc, 0, "cast to %s address space pointer "
6023 "from disjoint generic address space pointer",
6024 c_addr_space_name (as_to));
6025
6026 else if (ADDR_SPACE_GENERIC_P (as_to))
6027 warning_at (loc, 0, "cast to generic address space pointer "
6028 "from disjoint %s address space pointer",
6029 c_addr_space_name (as_from));
6030
6031 else
6032 warning_at (loc, 0, "cast to %s address space pointer "
6033 "from disjoint %s address space pointer",
6034 c_addr_space_name (as_to),
6035 c_addr_space_name (as_from));
6036 }
6037 }
6038
6039 /* Warn about possible alignment problems. */
6040 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6041 && TREE_CODE (type) == POINTER_TYPE
6042 && TREE_CODE (otype) == POINTER_TYPE
6043 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6044 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6045 /* Don't warn about opaque types, where the actual alignment
6046 restriction is unknown. */
6047 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6048 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6049 && min_align_of_type (TREE_TYPE (type))
6050 > min_align_of_type (TREE_TYPE (otype)))
6051 warning_at (loc, OPT_Wcast_align,
6052 "cast increases required alignment of target type");
6053
6054 if (TREE_CODE (type) == INTEGER_TYPE
6055 && TREE_CODE (otype) == POINTER_TYPE
6056 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6057 /* Unlike conversion of integers to pointers, where the
6058 warning is disabled for converting constants because
6059 of cases such as SIG_*, warn about converting constant
6060 pointers to integers. In some cases it may cause unwanted
6061 sign extension, and a warning is appropriate. */
6062 warning_at (loc, OPT_Wpointer_to_int_cast,
6063 "cast from pointer to integer of different size");
6064
6065 if (TREE_CODE (value) == CALL_EXPR
6066 && TREE_CODE (type) != TREE_CODE (otype))
6067 warning_at (loc, OPT_Wbad_function_cast,
6068 "cast from function call of type %qT "
6069 "to non-matching type %qT", otype, type);
6070
6071 if (TREE_CODE (type) == POINTER_TYPE
6072 && TREE_CODE (otype) == INTEGER_TYPE
6073 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6074 /* Don't warn about converting any constant. */
6075 && !TREE_CONSTANT (value))
6076 warning_at (loc,
6077 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6078 "of different size");
6079
6080 if (warn_strict_aliasing <= 2)
6081 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6082
6083 /* If pedantic, warn for conversions between function and object
6084 pointer types, except for converting a null pointer constant
6085 to function pointer type. */
6086 if (pedantic
6087 && TREE_CODE (type) == POINTER_TYPE
6088 && TREE_CODE (otype) == POINTER_TYPE
6089 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6090 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6091 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6092 "conversion of function pointer to object pointer type");
6093
6094 if (pedantic
6095 && TREE_CODE (type) == POINTER_TYPE
6096 && TREE_CODE (otype) == POINTER_TYPE
6097 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6098 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6099 && !null_pointer_constant_p (value))
6100 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6101 "conversion of object pointer to function pointer type");
6102
6103 if (TREE_CODE (type) == POINTER_TYPE
6104 && TREE_CODE (otype) == POINTER_TYPE
6105 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6106 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6107 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6108 TREE_TYPE (otype)))
6109 warning_at (loc, OPT_Wcast_function_type,
6110 "cast between incompatible function types"
6111 " from %qT to %qT", otype, type);
6112
6113 ovalue = value;
6114 value = convert (type, value);
6115
6116 /* Ignore any integer overflow caused by the cast. */
6117 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6118 {
6119 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6120 {
6121 if (!TREE_OVERFLOW (value))
6122 {
6123 /* Avoid clobbering a shared constant. */
6124 value = copy_node (value);
6125 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6126 }
6127 }
6128 else if (TREE_OVERFLOW (value))
6129 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6130 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6131 }
6132 }
6133
6134 /* Don't let a cast be an lvalue. */
6135 if (lvalue_p (value))
6136 value = non_lvalue_loc (loc, value);
6137
6138 /* Don't allow the results of casting to floating-point or complex
6139 types be confused with actual constants, or casts involving
6140 integer and pointer types other than direct integer-to-integer
6141 and integer-to-pointer be confused with integer constant
6142 expressions and null pointer constants. */
6143 if (TREE_CODE (value) == REAL_CST
6144 || TREE_CODE (value) == COMPLEX_CST
6145 || (TREE_CODE (value) == INTEGER_CST
6146 && !((TREE_CODE (expr) == INTEGER_CST
6147 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6148 || TREE_CODE (expr) == REAL_CST
6149 || TREE_CODE (expr) == COMPLEX_CST)))
6150 value = build1 (NOP_EXPR, type, value);
6151
6152 /* If the expression has integer operands and so can occur in an
6153 unevaluated part of an integer constant expression, ensure the
6154 return value reflects this. */
6155 if (int_operands
6156 && INTEGRAL_TYPE_P (type)
6157 && value != error_mark_node
6158 && !EXPR_INT_CONST_OPERANDS (value))
6159 value = note_integer_operands (value);
6160
6161 protected_set_expr_location (value, loc);
6162 return value;
6163 }
6164
6165 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6166 location of the open paren of the cast, or the position of the cast
6167 expr. */
6168 tree
c_cast_expr(location_t loc,struct c_type_name * type_name,tree expr)6169 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6170 {
6171 tree type;
6172 tree type_expr = NULL_TREE;
6173 bool type_expr_const = true;
6174 tree ret;
6175 int saved_wsp = warn_strict_prototypes;
6176
6177 /* This avoids warnings about unprototyped casts on
6178 integers. E.g. "#define SIG_DFL (void(*)())0". */
6179 if (TREE_CODE (expr) == INTEGER_CST)
6180 warn_strict_prototypes = 0;
6181 type = groktypename (type_name, &type_expr, &type_expr_const);
6182 warn_strict_prototypes = saved_wsp;
6183
6184 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6185 && reject_gcc_builtin (expr))
6186 return error_mark_node;
6187
6188 ret = build_c_cast (loc, type, expr);
6189 if (type_expr)
6190 {
6191 bool inner_expr_const = true;
6192 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6193 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6194 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6195 && inner_expr_const);
6196 SET_EXPR_LOCATION (ret, loc);
6197 }
6198
6199 if (!EXPR_HAS_LOCATION (ret))
6200 protected_set_expr_location (ret, loc);
6201
6202 /* C++ does not permits types to be defined in a cast, but it
6203 allows references to incomplete types. */
6204 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6205 warning_at (loc, OPT_Wc___compat,
6206 "defining a type in a cast is invalid in C++");
6207
6208 return ret;
6209 }
6210
6211 /* Build an assignment expression of lvalue LHS from value RHS.
6212 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6213 may differ from TREE_TYPE (LHS) for an enum bitfield.
6214 MODIFYCODE is the code for a binary operator that we use
6215 to combine the old value of LHS with RHS to get the new value.
6216 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6217 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6218 which may differ from TREE_TYPE (RHS) for an enum value.
6219
6220 LOCATION is the location of the MODIFYCODE operator.
6221 RHS_LOC is the location of the RHS. */
6222
6223 tree
build_modify_expr(location_t location,tree lhs,tree lhs_origtype,enum tree_code modifycode,location_t rhs_loc,tree rhs,tree rhs_origtype)6224 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6225 enum tree_code modifycode,
6226 location_t rhs_loc, tree rhs, tree rhs_origtype)
6227 {
6228 tree result;
6229 tree newrhs;
6230 tree rhseval = NULL_TREE;
6231 tree lhstype = TREE_TYPE (lhs);
6232 tree olhstype = lhstype;
6233 bool npc;
6234 bool is_atomic_op;
6235
6236 /* Types that aren't fully specified cannot be used in assignments. */
6237 lhs = require_complete_type (location, lhs);
6238
6239 /* Avoid duplicate error messages from operands that had errors. */
6240 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6241 return error_mark_node;
6242
6243 /* Ensure an error for assigning a non-lvalue array to an array in
6244 C90. */
6245 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6246 {
6247 error_at (location, "assignment to expression with array type");
6248 return error_mark_node;
6249 }
6250
6251 /* For ObjC properties, defer this check. */
6252 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6253 return error_mark_node;
6254
6255 is_atomic_op = really_atomic_lvalue (lhs);
6256
6257 newrhs = rhs;
6258
6259 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6260 {
6261 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6262 lhs_origtype, modifycode, rhs_loc, rhs,
6263 rhs_origtype);
6264 if (inner == error_mark_node)
6265 return error_mark_node;
6266 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6267 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6268 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6269 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6270 protected_set_expr_location (result, location);
6271 return result;
6272 }
6273
6274 /* If a binary op has been requested, combine the old LHS value with the RHS
6275 producing the value we should actually store into the LHS. */
6276
6277 if (modifycode != NOP_EXPR)
6278 {
6279 lhs = c_fully_fold (lhs, false, NULL, true);
6280 lhs = stabilize_reference (lhs);
6281
6282 /* Construct the RHS for any non-atomic compound assignemnt. */
6283 if (!is_atomic_op)
6284 {
6285 /* If in LHS op= RHS the RHS has side-effects, ensure they
6286 are preevaluated before the rest of the assignment expression's
6287 side-effects, because RHS could contain e.g. function calls
6288 that modify LHS. */
6289 if (TREE_SIDE_EFFECTS (rhs))
6290 {
6291 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6292 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6293 else
6294 newrhs = save_expr (rhs);
6295 rhseval = newrhs;
6296 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6297 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6298 newrhs);
6299 }
6300 newrhs = build_binary_op (location,
6301 modifycode, lhs, newrhs, true);
6302
6303 /* The original type of the right hand side is no longer
6304 meaningful. */
6305 rhs_origtype = NULL_TREE;
6306 }
6307 }
6308
6309 if (c_dialect_objc ())
6310 {
6311 /* Check if we are modifying an Objective-C property reference;
6312 if so, we need to generate setter calls. */
6313 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6314 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6315 else
6316 result = objc_maybe_build_modify_expr (lhs, newrhs);
6317 if (result)
6318 goto return_result;
6319
6320 /* Else, do the check that we postponed for Objective-C. */
6321 if (!lvalue_or_else (location, lhs, lv_assign))
6322 return error_mark_node;
6323 }
6324
6325 /* Give an error for storing in something that is 'const'. */
6326
6327 if (TYPE_READONLY (lhstype)
6328 || (RECORD_OR_UNION_TYPE_P (lhstype)
6329 && C_TYPE_FIELDS_READONLY (lhstype)))
6330 {
6331 readonly_error (location, lhs, lv_assign);
6332 return error_mark_node;
6333 }
6334 else if (TREE_READONLY (lhs))
6335 readonly_warning (lhs, lv_assign);
6336
6337 /* If storing into a structure or union member,
6338 it has probably been given type `int'.
6339 Compute the type that would go with
6340 the actual amount of storage the member occupies. */
6341
6342 if (TREE_CODE (lhs) == COMPONENT_REF
6343 && (TREE_CODE (lhstype) == INTEGER_TYPE
6344 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6345 || TREE_CODE (lhstype) == REAL_TYPE
6346 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6347 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6348
6349 /* If storing in a field that is in actuality a short or narrower than one,
6350 we must store in the field in its actual type. */
6351
6352 if (lhstype != TREE_TYPE (lhs))
6353 {
6354 lhs = copy_node (lhs);
6355 TREE_TYPE (lhs) = lhstype;
6356 }
6357
6358 /* Issue -Wc++-compat warnings about an assignment to an enum type
6359 when LHS does not have its original type. This happens for,
6360 e.g., an enum bitfield in a struct. */
6361 if (warn_cxx_compat
6362 && lhs_origtype != NULL_TREE
6363 && lhs_origtype != lhstype
6364 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6365 {
6366 tree checktype = (rhs_origtype != NULL_TREE
6367 ? rhs_origtype
6368 : TREE_TYPE (rhs));
6369 if (checktype != error_mark_node
6370 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6371 || (is_atomic_op && modifycode != NOP_EXPR)))
6372 warning_at (location, OPT_Wc___compat,
6373 "enum conversion in assignment is invalid in C++");
6374 }
6375
6376 /* Remove qualifiers. */
6377 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6378 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6379
6380 /* Convert new value to destination type. Fold it first, then
6381 restore any excess precision information, for the sake of
6382 conversion warnings. */
6383
6384 if (!(is_atomic_op && modifycode != NOP_EXPR))
6385 {
6386 tree rhs_semantic_type = NULL_TREE;
6387 if (!c_in_omp_for)
6388 {
6389 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6390 {
6391 rhs_semantic_type = TREE_TYPE (newrhs);
6392 newrhs = TREE_OPERAND (newrhs, 0);
6393 }
6394 npc = null_pointer_constant_p (newrhs);
6395 newrhs = c_fully_fold (newrhs, false, NULL);
6396 if (rhs_semantic_type)
6397 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6398 }
6399 else
6400 npc = null_pointer_constant_p (newrhs);
6401 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6402 rhs_origtype, ic_assign, npc,
6403 NULL_TREE, NULL_TREE, 0);
6404 if (TREE_CODE (newrhs) == ERROR_MARK)
6405 return error_mark_node;
6406 }
6407
6408 /* Emit ObjC write barrier, if necessary. */
6409 if (c_dialect_objc () && flag_objc_gc)
6410 {
6411 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6412 if (result)
6413 {
6414 protected_set_expr_location (result, location);
6415 goto return_result;
6416 }
6417 }
6418
6419 /* Scan operands. */
6420
6421 if (is_atomic_op)
6422 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6423 else
6424 {
6425 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6426 TREE_SIDE_EFFECTS (result) = 1;
6427 protected_set_expr_location (result, location);
6428 }
6429
6430 /* If we got the LHS in a different type for storing in,
6431 convert the result back to the nominal type of LHS
6432 so that the value we return always has the same type
6433 as the LHS argument. */
6434
6435 if (olhstype == TREE_TYPE (result))
6436 goto return_result;
6437
6438 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6439 rhs_origtype, ic_assign, false, NULL_TREE,
6440 NULL_TREE, 0);
6441 protected_set_expr_location (result, location);
6442
6443 return_result:
6444 if (rhseval)
6445 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6446 return result;
6447 }
6448
6449 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6450 This is used to implement -fplan9-extensions. */
6451
6452 static bool
find_anonymous_field_with_type(tree struct_type,tree type)6453 find_anonymous_field_with_type (tree struct_type, tree type)
6454 {
6455 tree field;
6456 bool found;
6457
6458 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6459 found = false;
6460 for (field = TYPE_FIELDS (struct_type);
6461 field != NULL_TREE;
6462 field = TREE_CHAIN (field))
6463 {
6464 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6465 ? c_build_qualified_type (TREE_TYPE (field),
6466 TYPE_QUAL_ATOMIC)
6467 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6468 if (DECL_NAME (field) == NULL
6469 && comptypes (type, fieldtype))
6470 {
6471 if (found)
6472 return false;
6473 found = true;
6474 }
6475 else if (DECL_NAME (field) == NULL
6476 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6477 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6478 {
6479 if (found)
6480 return false;
6481 found = true;
6482 }
6483 }
6484 return found;
6485 }
6486
6487 /* RHS is an expression whose type is pointer to struct. If there is
6488 an anonymous field in RHS with type TYPE, then return a pointer to
6489 that field in RHS. This is used with -fplan9-extensions. This
6490 returns NULL if no conversion could be found. */
6491
6492 static tree
convert_to_anonymous_field(location_t location,tree type,tree rhs)6493 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6494 {
6495 tree rhs_struct_type, lhs_main_type;
6496 tree field, found_field;
6497 bool found_sub_field;
6498 tree ret;
6499
6500 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6501 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6502 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6503
6504 gcc_assert (POINTER_TYPE_P (type));
6505 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6506 ? c_build_qualified_type (TREE_TYPE (type),
6507 TYPE_QUAL_ATOMIC)
6508 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6509
6510 found_field = NULL_TREE;
6511 found_sub_field = false;
6512 for (field = TYPE_FIELDS (rhs_struct_type);
6513 field != NULL_TREE;
6514 field = TREE_CHAIN (field))
6515 {
6516 if (DECL_NAME (field) != NULL_TREE
6517 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6518 continue;
6519 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6520 ? c_build_qualified_type (TREE_TYPE (field),
6521 TYPE_QUAL_ATOMIC)
6522 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6523 if (comptypes (lhs_main_type, fieldtype))
6524 {
6525 if (found_field != NULL_TREE)
6526 return NULL_TREE;
6527 found_field = field;
6528 }
6529 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6530 lhs_main_type))
6531 {
6532 if (found_field != NULL_TREE)
6533 return NULL_TREE;
6534 found_field = field;
6535 found_sub_field = true;
6536 }
6537 }
6538
6539 if (found_field == NULL_TREE)
6540 return NULL_TREE;
6541
6542 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6543 build_fold_indirect_ref (rhs), found_field,
6544 NULL_TREE);
6545 ret = build_fold_addr_expr_loc (location, ret);
6546
6547 if (found_sub_field)
6548 {
6549 ret = convert_to_anonymous_field (location, type, ret);
6550 gcc_assert (ret != NULL_TREE);
6551 }
6552
6553 return ret;
6554 }
6555
6556 /* Issue an error message for a bad initializer component.
6557 GMSGID identifies the message.
6558 The component name is taken from the spelling stack. */
6559
6560 static void ATTRIBUTE_GCC_DIAG (2,0)
error_init(location_t loc,const char * gmsgid,...)6561 error_init (location_t loc, const char *gmsgid, ...)
6562 {
6563 char *ofwhat;
6564
6565 auto_diagnostic_group d;
6566
6567 /* The gmsgid may be a format string with %< and %>. */
6568 va_list ap;
6569 va_start (ap, gmsgid);
6570 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6571 va_end (ap);
6572
6573 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6574 if (*ofwhat && warned)
6575 inform (loc, "(near initialization for %qs)", ofwhat);
6576 }
6577
6578 /* Issue a pedantic warning for a bad initializer component. OPT is
6579 the option OPT_* (from options.h) controlling this warning or 0 if
6580 it is unconditionally given. GMSGID identifies the message. The
6581 component name is taken from the spelling stack. */
6582
6583 static void ATTRIBUTE_GCC_DIAG (3,0)
pedwarn_init(location_t loc,int opt,const char * gmsgid,...)6584 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6585 {
6586 /* Use the location where a macro was expanded rather than where
6587 it was defined to make sure macros defined in system headers
6588 but used incorrectly elsewhere are diagnosed. */
6589 location_t exploc = expansion_point_location_if_in_system_header (loc);
6590 auto_diagnostic_group d;
6591 va_list ap;
6592 va_start (ap, gmsgid);
6593 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6594 va_end (ap);
6595 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6596 if (*ofwhat && warned)
6597 inform (exploc, "(near initialization for %qs)", ofwhat);
6598 }
6599
6600 /* Issue a warning for a bad initializer component.
6601
6602 OPT is the OPT_W* value corresponding to the warning option that
6603 controls this warning. GMSGID identifies the message. The
6604 component name is taken from the spelling stack. */
6605
6606 static void
warning_init(location_t loc,int opt,const char * gmsgid)6607 warning_init (location_t loc, int opt, const char *gmsgid)
6608 {
6609 char *ofwhat;
6610 bool warned;
6611
6612 auto_diagnostic_group d;
6613
6614 /* Use the location where a macro was expanded rather than where
6615 it was defined to make sure macros defined in system headers
6616 but used incorrectly elsewhere are diagnosed. */
6617 location_t exploc = expansion_point_location_if_in_system_header (loc);
6618
6619 /* The gmsgid may be a format string with %< and %>. */
6620 warned = warning_at (exploc, opt, gmsgid);
6621 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6622 if (*ofwhat && warned)
6623 inform (exploc, "(near initialization for %qs)", ofwhat);
6624 }
6625
6626 /* If TYPE is an array type and EXPR is a parenthesized string
6627 constant, warn if pedantic that EXPR is being used to initialize an
6628 object of type TYPE. */
6629
6630 void
maybe_warn_string_init(location_t loc,tree type,struct c_expr expr)6631 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6632 {
6633 if (pedantic
6634 && TREE_CODE (type) == ARRAY_TYPE
6635 && TREE_CODE (expr.value) == STRING_CST
6636 && expr.original_code != STRING_CST)
6637 pedwarn_init (loc, OPT_Wpedantic,
6638 "array initialized from parenthesized string constant");
6639 }
6640
6641 /* Attempt to locate the parameter with the given index within FNDECL,
6642 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6643
6644 static location_t
get_fndecl_argument_location(tree fndecl,int argnum)6645 get_fndecl_argument_location (tree fndecl, int argnum)
6646 {
6647 int i;
6648 tree param;
6649
6650 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6651 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6652 i < argnum && param;
6653 i++, param = TREE_CHAIN (param))
6654 ;
6655
6656 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6657 return DECL_SOURCE_LOCATION (FNDECL). */
6658 if (param == NULL)
6659 return DECL_SOURCE_LOCATION (fndecl);
6660
6661 return DECL_SOURCE_LOCATION (param);
6662 }
6663
6664 /* Issue a note about a mismatching argument for parameter PARMNUM
6665 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6666 Attempt to issue the note at the pertinent parameter of the decl;
6667 failing that issue it at the location of FUNDECL; failing that
6668 issue it at PLOC. */
6669
6670 static void
inform_for_arg(tree fundecl,location_t ploc,int parmnum,tree expected_type,tree actual_type)6671 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6672 tree expected_type, tree actual_type)
6673 {
6674 location_t loc;
6675 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6676 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6677 else
6678 loc = ploc;
6679
6680 inform (loc,
6681 "expected %qT but argument is of type %qT",
6682 expected_type, actual_type);
6683 }
6684
6685 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6686 function FUNDECL declared without prototype to parameter PARMNUM of
6687 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6688
6689 static void
maybe_warn_builtin_no_proto_arg(location_t loc,tree fundecl,int parmnum,tree parmtype,tree argtype)6690 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6691 tree parmtype, tree argtype)
6692 {
6693 tree_code parmcode = TREE_CODE (parmtype);
6694 tree_code argcode = TREE_CODE (argtype);
6695 tree promoted = c_type_promotes_to (argtype);
6696
6697 /* Avoid warning for enum arguments that promote to an integer type
6698 of the same size/mode. */
6699 if (parmcode == INTEGER_TYPE
6700 && argcode == ENUMERAL_TYPE
6701 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6702 return;
6703
6704 if ((parmcode == argcode
6705 || (parmcode == INTEGER_TYPE
6706 && argcode == ENUMERAL_TYPE))
6707 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6708 return;
6709
6710 /* This diagnoses even signed/unsigned mismatches. Those might be
6711 safe in many cases but GCC may emit suboptimal code for them so
6712 warning on those cases drives efficiency improvements. */
6713 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6714 TYPE_MAIN_VARIANT (promoted) == argtype
6715 ? G_("%qD argument %d type is %qT where %qT is expected "
6716 "in a call to built-in function declared without "
6717 "prototype")
6718 : G_("%qD argument %d promotes to %qT where %qT is expected "
6719 "in a call to built-in function declared without "
6720 "prototype"),
6721 fundecl, parmnum, promoted, parmtype))
6722 inform (DECL_SOURCE_LOCATION (fundecl),
6723 "built-in %qD declared here",
6724 fundecl);
6725 }
6726
6727 /* Convert value RHS to type TYPE as preparation for an assignment to
6728 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6729 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6730 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6731 constant before any folding.
6732 The real work of conversion is done by `convert'.
6733 The purpose of this function is to generate error messages
6734 for assignments that are not allowed in C.
6735 ERRTYPE says whether it is argument passing, assignment,
6736 initialization or return.
6737
6738 In the following example, '~' denotes where EXPR_LOC and '^' where
6739 LOCATION point to:
6740
6741 f (var); [ic_argpass]
6742 ^ ~~~
6743 x = var; [ic_assign]
6744 ^ ~~~;
6745 int x = var; [ic_init]
6746 ^^^
6747 return x; [ic_return]
6748 ^
6749
6750 FUNCTION is a tree for the function being called.
6751 PARMNUM is the number of the argument, for printing in error messages.
6752 WARNOPT may be set to a warning option to issue the corresponding warning
6753 rather than an error for invalid conversions. Used for calls to built-in
6754 functions declared without a prototype. */
6755
6756 static tree
convert_for_assignment(location_t location,location_t expr_loc,tree type,tree rhs,tree origtype,enum impl_conv errtype,bool null_pointer_constant,tree fundecl,tree function,int parmnum,int warnopt)6757 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6758 tree rhs, tree origtype, enum impl_conv errtype,
6759 bool null_pointer_constant, tree fundecl,
6760 tree function, int parmnum, int warnopt /* = 0 */)
6761 {
6762 enum tree_code codel = TREE_CODE (type);
6763 tree orig_rhs = rhs;
6764 tree rhstype;
6765 enum tree_code coder;
6766 tree rname = NULL_TREE;
6767 bool objc_ok = false;
6768
6769 /* Use the expansion point location to handle cases such as user's
6770 function returning a wrong-type macro defined in a system header. */
6771 location = expansion_point_location_if_in_system_header (location);
6772
6773 if (errtype == ic_argpass)
6774 {
6775 tree selector;
6776 /* Change pointer to function to the function itself for
6777 diagnostics. */
6778 if (TREE_CODE (function) == ADDR_EXPR
6779 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6780 function = TREE_OPERAND (function, 0);
6781
6782 /* Handle an ObjC selector specially for diagnostics. */
6783 selector = objc_message_selector ();
6784 rname = function;
6785 if (selector && parmnum > 2)
6786 {
6787 rname = selector;
6788 parmnum -= 2;
6789 }
6790 }
6791
6792 /* This macro is used to emit diagnostics to ensure that all format
6793 strings are complete sentences, visible to gettext and checked at
6794 compile time. */
6795 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6796 do { \
6797 switch (errtype) \
6798 { \
6799 case ic_argpass: \
6800 { \
6801 auto_diagnostic_group d; \
6802 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6803 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6804 } \
6805 break; \
6806 case ic_assign: \
6807 pedwarn (LOCATION, OPT, AS); \
6808 break; \
6809 case ic_init: \
6810 case ic_init_const: \
6811 pedwarn_init (LOCATION, OPT, IN); \
6812 break; \
6813 case ic_return: \
6814 pedwarn (LOCATION, OPT, RE); \
6815 break; \
6816 default: \
6817 gcc_unreachable (); \
6818 } \
6819 } while (0)
6820
6821 /* This macro is used to emit diagnostics to ensure that all format
6822 strings are complete sentences, visible to gettext and checked at
6823 compile time. It can be called with 'pedwarn' or 'warning_at'. */
6824 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6825 do { \
6826 switch (errtype) \
6827 { \
6828 case ic_argpass: \
6829 { \
6830 auto_diagnostic_group d; \
6831 if (PEDWARN) { \
6832 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6833 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6834 } else { \
6835 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6836 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6837 } \
6838 } \
6839 break; \
6840 case ic_assign: \
6841 if (PEDWARN) \
6842 pedwarn (LOCATION, OPT, AS, QUALS); \
6843 else \
6844 warning_at (LOCATION, OPT, AS, QUALS); \
6845 break; \
6846 case ic_init: \
6847 case ic_init_const: \
6848 if (PEDWARN) \
6849 pedwarn (LOCATION, OPT, IN, QUALS); \
6850 else \
6851 warning_at (LOCATION, OPT, IN, QUALS); \
6852 break; \
6853 case ic_return: \
6854 if (PEDWARN) \
6855 pedwarn (LOCATION, OPT, RE, QUALS); \
6856 else \
6857 warning_at (LOCATION, OPT, RE, QUALS); \
6858 break; \
6859 default: \
6860 gcc_unreachable (); \
6861 } \
6862 } while (0)
6863
6864 /* This macro is used to emit diagnostics to ensure that all format
6865 strings are complete sentences, visible to gettext and checked at
6866 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6867 extra parameter to enumerate qualifiers. */
6868 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6869 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
6870
6871
6872 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6873 rhs = TREE_OPERAND (rhs, 0);
6874
6875 rhstype = TREE_TYPE (rhs);
6876 coder = TREE_CODE (rhstype);
6877
6878 if (coder == ERROR_MARK)
6879 return error_mark_node;
6880
6881 if (c_dialect_objc ())
6882 {
6883 int parmno;
6884
6885 switch (errtype)
6886 {
6887 case ic_return:
6888 parmno = 0;
6889 break;
6890
6891 case ic_assign:
6892 parmno = -1;
6893 break;
6894
6895 case ic_init:
6896 case ic_init_const:
6897 parmno = -2;
6898 break;
6899
6900 default:
6901 parmno = parmnum;
6902 break;
6903 }
6904
6905 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6906 }
6907
6908 if (warn_cxx_compat)
6909 {
6910 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6911 if (checktype != error_mark_node
6912 && TREE_CODE (type) == ENUMERAL_TYPE
6913 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6914 switch (errtype)
6915 {
6916 case ic_argpass:
6917 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6918 "passing argument %d of %qE is invalid in C++",
6919 parmnum, rname))
6920 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6921 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6922 "expected %qT but argument is of type %qT",
6923 type, rhstype);
6924 break;
6925 case ic_assign:
6926 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6927 "%qT in assignment is invalid in C++", rhstype, type);
6928 break;
6929 case ic_init:
6930 case ic_init_const:
6931 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6932 "%qT to %qT in initialization is invalid in C++",
6933 rhstype, type);
6934 break;
6935 case ic_return:
6936 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6937 "%qT in return is invalid in C++", rhstype, type);
6938 break;
6939 default:
6940 gcc_unreachable ();
6941 }
6942 }
6943
6944 if (warn_enum_conversion)
6945 {
6946 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6947 if (checktype != error_mark_node
6948 && TREE_CODE (checktype) == ENUMERAL_TYPE
6949 && TREE_CODE (type) == ENUMERAL_TYPE
6950 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6951 {
6952 gcc_rich_location loc (location);
6953 warning_at (&loc, OPT_Wenum_conversion,
6954 "implicit conversion from %qT to %qT",
6955 checktype, type);
6956 }
6957 }
6958
6959 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6960 {
6961 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6962 return rhs;
6963 }
6964
6965 if (coder == VOID_TYPE)
6966 {
6967 /* Except for passing an argument to an unprototyped function,
6968 this is a constraint violation. When passing an argument to
6969 an unprototyped function, it is compile-time undefined;
6970 making it a constraint in that case was rejected in
6971 DR#252. */
6972 const char msg[] = "void value not ignored as it ought to be";
6973 if (warnopt)
6974 warning_at (location, warnopt, msg);
6975 else
6976 error_at (location, msg);
6977 return error_mark_node;
6978 }
6979 rhs = require_complete_type (location, rhs);
6980 if (rhs == error_mark_node)
6981 return error_mark_node;
6982
6983 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6984 return error_mark_node;
6985
6986 /* A non-reference type can convert to a reference. This handles
6987 va_start, va_copy and possibly port built-ins. */
6988 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6989 {
6990 if (!lvalue_p (rhs))
6991 {
6992 const char msg[] = "cannot pass rvalue to reference parameter";
6993 if (warnopt)
6994 warning_at (location, warnopt, msg);
6995 else
6996 error_at (location, msg);
6997 return error_mark_node;
6998 }
6999 if (!c_mark_addressable (rhs))
7000 return error_mark_node;
7001 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
7002 SET_EXPR_LOCATION (rhs, location);
7003
7004 rhs = convert_for_assignment (location, expr_loc,
7005 build_pointer_type (TREE_TYPE (type)),
7006 rhs, origtype, errtype,
7007 null_pointer_constant, fundecl, function,
7008 parmnum, warnopt);
7009 if (rhs == error_mark_node)
7010 return error_mark_node;
7011
7012 rhs = build1 (NOP_EXPR, type, rhs);
7013 SET_EXPR_LOCATION (rhs, location);
7014 return rhs;
7015 }
7016 /* Some types can interconvert without explicit casts. */
7017 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7018 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7019 return convert (type, rhs);
7020 /* Arithmetic types all interconvert, and enum is treated like int. */
7021 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7022 || codel == FIXED_POINT_TYPE
7023 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7024 || codel == BOOLEAN_TYPE)
7025 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7026 || coder == FIXED_POINT_TYPE
7027 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7028 || coder == BOOLEAN_TYPE))
7029 {
7030 if (warnopt && errtype == ic_argpass)
7031 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7032 rhstype);
7033
7034 bool save = in_late_binary_op;
7035 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
7036 || (coder == REAL_TYPE
7037 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7038 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7039 in_late_binary_op = true;
7040 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7041 ? expr_loc : location, type, orig_rhs,
7042 errtype == ic_init_const);
7043 in_late_binary_op = save;
7044 return ret;
7045 }
7046
7047 /* Aggregates in different TUs might need conversion. */
7048 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7049 && codel == coder
7050 && comptypes (type, rhstype))
7051 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7052 ? expr_loc : location, type, rhs);
7053
7054 /* Conversion to a transparent union or record from its member types.
7055 This applies only to function arguments. */
7056 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7057 && TYPE_TRANSPARENT_AGGR (type))
7058 && errtype == ic_argpass)
7059 {
7060 tree memb, marginal_memb = NULL_TREE;
7061
7062 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7063 {
7064 tree memb_type = TREE_TYPE (memb);
7065
7066 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7067 TYPE_MAIN_VARIANT (rhstype)))
7068 break;
7069
7070 if (TREE_CODE (memb_type) != POINTER_TYPE)
7071 continue;
7072
7073 if (coder == POINTER_TYPE)
7074 {
7075 tree ttl = TREE_TYPE (memb_type);
7076 tree ttr = TREE_TYPE (rhstype);
7077
7078 /* Any non-function converts to a [const][volatile] void *
7079 and vice versa; otherwise, targets must be the same.
7080 Meanwhile, the lhs target must have all the qualifiers of
7081 the rhs. */
7082 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7083 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7084 || comp_target_types (location, memb_type, rhstype))
7085 {
7086 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7087 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7088 /* If this type won't generate any warnings, use it. */
7089 if (lquals == rquals
7090 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7091 && TREE_CODE (ttl) == FUNCTION_TYPE)
7092 ? ((lquals | rquals) == rquals)
7093 : ((lquals | rquals) == lquals)))
7094 break;
7095
7096 /* Keep looking for a better type, but remember this one. */
7097 if (!marginal_memb)
7098 marginal_memb = memb;
7099 }
7100 }
7101
7102 /* Can convert integer zero to any pointer type. */
7103 if (null_pointer_constant)
7104 {
7105 rhs = null_pointer_node;
7106 break;
7107 }
7108 }
7109
7110 if (memb || marginal_memb)
7111 {
7112 if (!memb)
7113 {
7114 /* We have only a marginally acceptable member type;
7115 it needs a warning. */
7116 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7117 tree ttr = TREE_TYPE (rhstype);
7118
7119 /* Const and volatile mean something different for function
7120 types, so the usual warnings are not appropriate. */
7121 if (TREE_CODE (ttr) == FUNCTION_TYPE
7122 && TREE_CODE (ttl) == FUNCTION_TYPE)
7123 {
7124 /* Because const and volatile on functions are
7125 restrictions that say the function will not do
7126 certain things, it is okay to use a const or volatile
7127 function where an ordinary one is wanted, but not
7128 vice-versa. */
7129 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7130 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7131 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7132 OPT_Wdiscarded_qualifiers,
7133 G_("passing argument %d of %qE "
7134 "makes %q#v qualified function "
7135 "pointer from unqualified"),
7136 G_("assignment makes %q#v qualified "
7137 "function pointer from "
7138 "unqualified"),
7139 G_("initialization makes %q#v qualified "
7140 "function pointer from "
7141 "unqualified"),
7142 G_("return makes %q#v qualified function "
7143 "pointer from unqualified"),
7144 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7145 }
7146 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7147 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7148 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7149 OPT_Wdiscarded_qualifiers,
7150 G_("passing argument %d of %qE discards "
7151 "%qv qualifier from pointer target type"),
7152 G_("assignment discards %qv qualifier "
7153 "from pointer target type"),
7154 G_("initialization discards %qv qualifier "
7155 "from pointer target type"),
7156 G_("return discards %qv qualifier from "
7157 "pointer target type"),
7158 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7159
7160 memb = marginal_memb;
7161 }
7162
7163 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7164 pedwarn (location, OPT_Wpedantic,
7165 "ISO C prohibits argument conversion to union type");
7166
7167 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7168 return build_constructor_single (type, memb, rhs);
7169 }
7170 }
7171
7172 /* Conversions among pointers */
7173 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7174 && (coder == codel))
7175 {
7176 /* If RHS refers to a built-in declared without a prototype
7177 BLTIN is the declaration of the built-in with a prototype
7178 and RHSTYPE is set to the actual type of the built-in. */
7179 tree bltin;
7180 rhstype = type_or_builtin_type (rhs, &bltin);
7181
7182 tree ttl = TREE_TYPE (type);
7183 tree ttr = TREE_TYPE (rhstype);
7184 tree mvl = ttl;
7185 tree mvr = ttr;
7186 bool is_opaque_pointer;
7187 int target_cmp = 0; /* Cache comp_target_types () result. */
7188 addr_space_t asl;
7189 addr_space_t asr;
7190
7191 if (TREE_CODE (mvl) != ARRAY_TYPE)
7192 mvl = (TYPE_ATOMIC (mvl)
7193 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7194 TYPE_QUAL_ATOMIC)
7195 : TYPE_MAIN_VARIANT (mvl));
7196 if (TREE_CODE (mvr) != ARRAY_TYPE)
7197 mvr = (TYPE_ATOMIC (mvr)
7198 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7199 TYPE_QUAL_ATOMIC)
7200 : TYPE_MAIN_VARIANT (mvr));
7201 /* Opaque pointers are treated like void pointers. */
7202 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7203
7204 /* The Plan 9 compiler permits a pointer to a struct to be
7205 automatically converted into a pointer to an anonymous field
7206 within the struct. */
7207 if (flag_plan9_extensions
7208 && RECORD_OR_UNION_TYPE_P (mvl)
7209 && RECORD_OR_UNION_TYPE_P (mvr)
7210 && mvl != mvr)
7211 {
7212 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7213 if (new_rhs != NULL_TREE)
7214 {
7215 rhs = new_rhs;
7216 rhstype = TREE_TYPE (rhs);
7217 coder = TREE_CODE (rhstype);
7218 ttr = TREE_TYPE (rhstype);
7219 mvr = TYPE_MAIN_VARIANT (ttr);
7220 }
7221 }
7222
7223 /* C++ does not allow the implicit conversion void* -> T*. However,
7224 for the purpose of reducing the number of false positives, we
7225 tolerate the special case of
7226
7227 int *p = NULL;
7228
7229 where NULL is typically defined in C to be '(void *) 0'. */
7230 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7231 warning_at (errtype == ic_argpass ? expr_loc : location,
7232 OPT_Wc___compat,
7233 "request for implicit conversion "
7234 "from %qT to %qT not permitted in C++", rhstype, type);
7235
7236 /* See if the pointers point to incompatible address spaces. */
7237 asl = TYPE_ADDR_SPACE (ttl);
7238 asr = TYPE_ADDR_SPACE (ttr);
7239 if (!null_pointer_constant_p (rhs)
7240 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7241 {
7242 switch (errtype)
7243 {
7244 case ic_argpass:
7245 {
7246 const char msg[] = G_("passing argument %d of %qE from "
7247 "pointer to non-enclosed address space");
7248 if (warnopt)
7249 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7250 else
7251 error_at (expr_loc, msg, parmnum, rname);
7252 break;
7253 }
7254 case ic_assign:
7255 {
7256 const char msg[] = G_("assignment from pointer to "
7257 "non-enclosed address space");
7258 if (warnopt)
7259 warning_at (location, warnopt, msg);
7260 else
7261 error_at (location, msg);
7262 break;
7263 }
7264 case ic_init:
7265 case ic_init_const:
7266 {
7267 const char msg[] = G_("initialization from pointer to "
7268 "non-enclosed address space");
7269 if (warnopt)
7270 warning_at (location, warnopt, msg);
7271 else
7272 error_at (location, msg);
7273 break;
7274 }
7275 case ic_return:
7276 {
7277 const char msg[] = G_("return from pointer to "
7278 "non-enclosed address space");
7279 if (warnopt)
7280 warning_at (location, warnopt, msg);
7281 else
7282 error_at (location, msg);
7283 break;
7284 }
7285 default:
7286 gcc_unreachable ();
7287 }
7288 return error_mark_node;
7289 }
7290
7291 /* Check if the right-hand side has a format attribute but the
7292 left-hand side doesn't. */
7293 if (warn_suggest_attribute_format
7294 && check_missing_format_attribute (type, rhstype))
7295 {
7296 switch (errtype)
7297 {
7298 case ic_argpass:
7299 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7300 "argument %d of %qE might be "
7301 "a candidate for a format attribute",
7302 parmnum, rname);
7303 break;
7304 case ic_assign:
7305 warning_at (location, OPT_Wsuggest_attribute_format,
7306 "assignment left-hand side might be "
7307 "a candidate for a format attribute");
7308 break;
7309 case ic_init:
7310 case ic_init_const:
7311 warning_at (location, OPT_Wsuggest_attribute_format,
7312 "initialization left-hand side might be "
7313 "a candidate for a format attribute");
7314 break;
7315 case ic_return:
7316 warning_at (location, OPT_Wsuggest_attribute_format,
7317 "return type might be "
7318 "a candidate for a format attribute");
7319 break;
7320 default:
7321 gcc_unreachable ();
7322 }
7323 }
7324
7325 /* See if the pointers point to incompatible scalar storage orders. */
7326 if (warn_scalar_storage_order
7327 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7328 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7329 {
7330 tree t;
7331
7332 switch (errtype)
7333 {
7334 case ic_argpass:
7335 /* Do not warn for built-in functions, for example memcpy, since we
7336 control how they behave and they can be useful in this area. */
7337 if (TREE_CODE (rname) != FUNCTION_DECL
7338 || !fndecl_built_in_p (rname))
7339 warning_at (location, OPT_Wscalar_storage_order,
7340 "passing argument %d of %qE from incompatible "
7341 "scalar storage order", parmnum, rname);
7342 break;
7343 case ic_assign:
7344 /* Do not warn if the RHS is a call to a function that returns a
7345 pointer that is not an alias. */
7346 if (TREE_CODE (rhs) != CALL_EXPR
7347 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7348 || !DECL_IS_MALLOC (t))
7349 warning_at (location, OPT_Wscalar_storage_order,
7350 "assignment to %qT from pointer type %qT with "
7351 "incompatible scalar storage order", type, rhstype);
7352 break;
7353 case ic_init:
7354 case ic_init_const:
7355 /* Likewise. */
7356 if (TREE_CODE (rhs) != CALL_EXPR
7357 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7358 || !DECL_IS_MALLOC (t))
7359 warning_at (location, OPT_Wscalar_storage_order,
7360 "initialization of %qT from pointer type %qT with "
7361 "incompatible scalar storage order", type, rhstype);
7362 break;
7363 case ic_return:
7364 warning_at (location, OPT_Wscalar_storage_order,
7365 "returning %qT from pointer type with incompatible "
7366 "scalar storage order %qT", rhstype, type);
7367 break;
7368 default:
7369 gcc_unreachable ();
7370 }
7371 }
7372
7373 /* Any non-function converts to a [const][volatile] void *
7374 and vice versa; otherwise, targets must be the same.
7375 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7376 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7377 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7378 || (target_cmp = comp_target_types (location, type, rhstype))
7379 || is_opaque_pointer
7380 || ((c_common_unsigned_type (mvl)
7381 == c_common_unsigned_type (mvr))
7382 && (c_common_signed_type (mvl)
7383 == c_common_signed_type (mvr))
7384 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7385 {
7386 /* Warn about loss of qualifers from pointers to arrays with
7387 qualifiers on the element type. */
7388 if (TREE_CODE (ttr) == ARRAY_TYPE)
7389 {
7390 ttr = strip_array_types (ttr);
7391 ttl = strip_array_types (ttl);
7392
7393 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7394 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7395 WARNING_FOR_QUALIFIERS (flag_isoc2x,
7396 location, expr_loc,
7397 OPT_Wdiscarded_array_qualifiers,
7398 G_("passing argument %d of %qE discards "
7399 "%qv qualifier from pointer target type"),
7400 G_("assignment discards %qv qualifier "
7401 "from pointer target type"),
7402 G_("initialization discards %qv qualifier "
7403 "from pointer target type"),
7404 G_("return discards %qv qualifier from "
7405 "pointer target type"),
7406 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7407 }
7408 else if (pedantic
7409 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7410 ||
7411 (VOID_TYPE_P (ttr)
7412 && !null_pointer_constant
7413 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7414 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7415 G_("ISO C forbids passing argument %d of "
7416 "%qE between function pointer "
7417 "and %<void *%>"),
7418 G_("ISO C forbids assignment between "
7419 "function pointer and %<void *%>"),
7420 G_("ISO C forbids initialization between "
7421 "function pointer and %<void *%>"),
7422 G_("ISO C forbids return between function "
7423 "pointer and %<void *%>"));
7424 /* Const and volatile mean something different for function types,
7425 so the usual warnings are not appropriate. */
7426 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7427 && TREE_CODE (ttl) != FUNCTION_TYPE)
7428 {
7429 /* Assignments between atomic and non-atomic objects are OK. */
7430 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7431 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7432 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7433 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7434
7435 /* Don't warn about loss of qualifier for conversions from
7436 qualified void* to pointers to arrays with corresponding
7437 qualifier on the element type (except for pedantic before C23). */
7438 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc2x))
7439 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7440 OPT_Wdiscarded_qualifiers,
7441 G_("passing argument %d of %qE discards "
7442 "%qv qualifier from pointer target type"),
7443 G_("assignment discards %qv qualifier "
7444 "from pointer target type"),
7445 G_("initialization discards %qv qualifier "
7446 "from pointer target type"),
7447 G_("return discards %qv qualifier from "
7448 "pointer target type"),
7449 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7450 else if (warn_quals_ped)
7451 pedwarn_c11 (location, OPT_Wc11_c2x_compat,
7452 "array with qualifier on the element is not qualified before C2X");
7453
7454 /* If this is not a case of ignoring a mismatch in signedness,
7455 no warning. */
7456 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7457 || target_cmp)
7458 ;
7459 /* If there is a mismatch, do warn. */
7460 else if (warn_pointer_sign)
7461 switch (errtype)
7462 {
7463 case ic_argpass:
7464 {
7465 auto_diagnostic_group d;
7466 range_label_for_type_mismatch rhs_label (rhstype, type);
7467 gcc_rich_location richloc (expr_loc, &rhs_label);
7468 if (pedwarn (&richloc, OPT_Wpointer_sign,
7469 "pointer targets in passing argument %d of "
7470 "%qE differ in signedness", parmnum, rname))
7471 inform_for_arg (fundecl, expr_loc, parmnum, type,
7472 rhstype);
7473 }
7474 break;
7475 case ic_assign:
7476 pedwarn (location, OPT_Wpointer_sign,
7477 "pointer targets in assignment from %qT to %qT "
7478 "differ in signedness", rhstype, type);
7479 break;
7480 case ic_init:
7481 case ic_init_const:
7482 pedwarn_init (location, OPT_Wpointer_sign,
7483 "pointer targets in initialization of %qT "
7484 "from %qT differ in signedness", type,
7485 rhstype);
7486 break;
7487 case ic_return:
7488 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7489 "returning %qT from a function with return type "
7490 "%qT differ in signedness", rhstype, type);
7491 break;
7492 default:
7493 gcc_unreachable ();
7494 }
7495 }
7496 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7497 && TREE_CODE (ttr) == FUNCTION_TYPE)
7498 {
7499 /* Because const and volatile on functions are restrictions
7500 that say the function will not do certain things,
7501 it is okay to use a const or volatile function
7502 where an ordinary one is wanted, but not vice-versa. */
7503 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7504 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7505 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7506 OPT_Wdiscarded_qualifiers,
7507 G_("passing argument %d of %qE makes "
7508 "%q#v qualified function pointer "
7509 "from unqualified"),
7510 G_("assignment makes %q#v qualified function "
7511 "pointer from unqualified"),
7512 G_("initialization makes %q#v qualified "
7513 "function pointer from unqualified"),
7514 G_("return makes %q#v qualified function "
7515 "pointer from unqualified"),
7516 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7517 }
7518 }
7519 /* Avoid warning about the volatile ObjC EH puts on decls. */
7520 else if (!objc_ok)
7521 {
7522 switch (errtype)
7523 {
7524 case ic_argpass:
7525 {
7526 auto_diagnostic_group d;
7527 range_label_for_type_mismatch rhs_label (rhstype, type);
7528 gcc_rich_location richloc (expr_loc, &rhs_label);
7529 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7530 "passing argument %d of %qE from incompatible "
7531 "pointer type", parmnum, rname))
7532 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7533 }
7534 break;
7535 case ic_assign:
7536 if (bltin)
7537 pedwarn (location, OPT_Wincompatible_pointer_types,
7538 "assignment to %qT from pointer to "
7539 "%qD with incompatible type %qT",
7540 type, bltin, rhstype);
7541 else
7542 pedwarn (location, OPT_Wincompatible_pointer_types,
7543 "assignment to %qT from incompatible pointer type %qT",
7544 type, rhstype);
7545 break;
7546 case ic_init:
7547 case ic_init_const:
7548 if (bltin)
7549 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7550 "initialization of %qT from pointer to "
7551 "%qD with incompatible type %qT",
7552 type, bltin, rhstype);
7553 else
7554 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7555 "initialization of %qT from incompatible "
7556 "pointer type %qT",
7557 type, rhstype);
7558 break;
7559 case ic_return:
7560 if (bltin)
7561 pedwarn (location, OPT_Wincompatible_pointer_types,
7562 "returning pointer to %qD of type %qT from "
7563 "a function with incompatible type %qT",
7564 bltin, rhstype, type);
7565 else
7566 pedwarn (location, OPT_Wincompatible_pointer_types,
7567 "returning %qT from a function with incompatible "
7568 "return type %qT", rhstype, type);
7569 break;
7570 default:
7571 gcc_unreachable ();
7572 }
7573 }
7574
7575 /* If RHS isn't an address, check pointer or array of packed
7576 struct or union. */
7577 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7578
7579 return convert (type, rhs);
7580 }
7581 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7582 {
7583 /* ??? This should not be an error when inlining calls to
7584 unprototyped functions. */
7585 const char msg[] = "invalid use of non-lvalue array";
7586 if (warnopt)
7587 warning_at (location, warnopt, msg);
7588 else
7589 error_at (location, msg);
7590 return error_mark_node;
7591 }
7592 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7593 {
7594 /* An explicit constant 0 can convert to a pointer,
7595 or one that results from arithmetic, even including
7596 a cast to integer type. */
7597 if (!null_pointer_constant)
7598 switch (errtype)
7599 {
7600 case ic_argpass:
7601 {
7602 auto_diagnostic_group d;
7603 range_label_for_type_mismatch rhs_label (rhstype, type);
7604 gcc_rich_location richloc (expr_loc, &rhs_label);
7605 if (pedwarn (&richloc, OPT_Wint_conversion,
7606 "passing argument %d of %qE makes pointer from "
7607 "integer without a cast", parmnum, rname))
7608 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7609 }
7610 break;
7611 case ic_assign:
7612 pedwarn (location, OPT_Wint_conversion,
7613 "assignment to %qT from %qT makes pointer from integer "
7614 "without a cast", type, rhstype);
7615 break;
7616 case ic_init:
7617 case ic_init_const:
7618 pedwarn_init (location, OPT_Wint_conversion,
7619 "initialization of %qT from %qT makes pointer from "
7620 "integer without a cast", type, rhstype);
7621 break;
7622 case ic_return:
7623 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7624 "function with return type %qT makes pointer from "
7625 "integer without a cast", rhstype, type);
7626 break;
7627 default:
7628 gcc_unreachable ();
7629 }
7630
7631 return convert (type, rhs);
7632 }
7633 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7634 {
7635 switch (errtype)
7636 {
7637 case ic_argpass:
7638 {
7639 auto_diagnostic_group d;
7640 range_label_for_type_mismatch rhs_label (rhstype, type);
7641 gcc_rich_location richloc (expr_loc, &rhs_label);
7642 if (pedwarn (&richloc, OPT_Wint_conversion,
7643 "passing argument %d of %qE makes integer from "
7644 "pointer without a cast", parmnum, rname))
7645 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7646 }
7647 break;
7648 case ic_assign:
7649 pedwarn (location, OPT_Wint_conversion,
7650 "assignment to %qT from %qT makes integer from pointer "
7651 "without a cast", type, rhstype);
7652 break;
7653 case ic_init:
7654 case ic_init_const:
7655 pedwarn_init (location, OPT_Wint_conversion,
7656 "initialization of %qT from %qT makes integer from "
7657 "pointer without a cast", type, rhstype);
7658 break;
7659 case ic_return:
7660 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7661 "function with return type %qT makes integer from "
7662 "pointer without a cast", rhstype, type);
7663 break;
7664 default:
7665 gcc_unreachable ();
7666 }
7667
7668 return convert (type, rhs);
7669 }
7670 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7671 {
7672 tree ret;
7673 bool save = in_late_binary_op;
7674 in_late_binary_op = true;
7675 ret = convert (type, rhs);
7676 in_late_binary_op = save;
7677 return ret;
7678 }
7679
7680 switch (errtype)
7681 {
7682 case ic_argpass:
7683 {
7684 auto_diagnostic_group d;
7685 range_label_for_type_mismatch rhs_label (rhstype, type);
7686 gcc_rich_location richloc (expr_loc, &rhs_label);
7687 const char msg[] = G_("incompatible type for argument %d of %qE");
7688 if (warnopt)
7689 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7690 else
7691 error_at (&richloc, msg, parmnum, rname);
7692 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7693 }
7694 break;
7695 case ic_assign:
7696 {
7697 const char msg[]
7698 = G_("incompatible types when assigning to type %qT from type %qT");
7699 if (warnopt)
7700 warning_at (expr_loc, 0, msg, type, rhstype);
7701 else
7702 error_at (expr_loc, msg, type, rhstype);
7703 break;
7704 }
7705 case ic_init:
7706 case ic_init_const:
7707 {
7708 const char msg[]
7709 = G_("incompatible types when initializing type %qT using type %qT");
7710 if (warnopt)
7711 warning_at (location, 0, msg, type, rhstype);
7712 else
7713 error_at (location, msg, type, rhstype);
7714 break;
7715 }
7716 case ic_return:
7717 {
7718 const char msg[]
7719 = G_("incompatible types when returning type %qT but %qT was expected");
7720 if (warnopt)
7721 warning_at (location, 0, msg, rhstype, type);
7722 else
7723 error_at (location, msg, rhstype, type);
7724 break;
7725 }
7726 default:
7727 gcc_unreachable ();
7728 }
7729
7730 return error_mark_node;
7731 }
7732
7733 /* If VALUE is a compound expr all of whose expressions are constant, then
7734 return its value. Otherwise, return error_mark_node.
7735
7736 This is for handling COMPOUND_EXPRs as initializer elements
7737 which is allowed with a warning when -pedantic is specified. */
7738
7739 static tree
valid_compound_expr_initializer(tree value,tree endtype)7740 valid_compound_expr_initializer (tree value, tree endtype)
7741 {
7742 if (TREE_CODE (value) == COMPOUND_EXPR)
7743 {
7744 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7745 == error_mark_node)
7746 return error_mark_node;
7747 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7748 endtype);
7749 }
7750 else if (!initializer_constant_valid_p (value, endtype))
7751 return error_mark_node;
7752 else
7753 return value;
7754 }
7755
7756 /* Perform appropriate conversions on the initial value of a variable,
7757 store it in the declaration DECL,
7758 and print any error messages that are appropriate.
7759 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7760 If the init is invalid, store an ERROR_MARK.
7761
7762 INIT_LOC is the location of the initial value. */
7763
7764 void
store_init_value(location_t init_loc,tree decl,tree init,tree origtype)7765 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7766 {
7767 tree value, type;
7768 bool npc = false;
7769
7770 /* If variable's type was invalidly declared, just ignore it. */
7771
7772 type = TREE_TYPE (decl);
7773 if (TREE_CODE (type) == ERROR_MARK)
7774 return;
7775
7776 /* Digest the specified initializer into an expression. */
7777
7778 if (init)
7779 npc = null_pointer_constant_p (init);
7780 value = digest_init (init_loc, type, init, origtype, npc,
7781 true, TREE_STATIC (decl));
7782
7783 /* Store the expression if valid; else report error. */
7784
7785 if (!in_system_header_at (input_location)
7786 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7787 warning (OPT_Wtraditional, "traditional C rejects automatic "
7788 "aggregate initialization");
7789
7790 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7791 DECL_INITIAL (decl) = value;
7792
7793 /* ANSI wants warnings about out-of-range constant initializers. */
7794 STRIP_TYPE_NOPS (value);
7795 if (TREE_STATIC (decl))
7796 constant_expression_warning (value);
7797
7798 /* Check if we need to set array size from compound literal size. */
7799 if (TREE_CODE (type) == ARRAY_TYPE
7800 && TYPE_DOMAIN (type) == NULL_TREE
7801 && value != error_mark_node)
7802 {
7803 tree inside_init = init;
7804
7805 STRIP_TYPE_NOPS (inside_init);
7806 inside_init = fold (inside_init);
7807
7808 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7809 {
7810 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7811
7812 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7813 {
7814 /* For int foo[] = (int [3]){1}; we need to set array size
7815 now since later on array initializer will be just the
7816 brace enclosed list of the compound literal. */
7817 tree etype = strip_array_types (TREE_TYPE (decl));
7818 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7819 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7820 layout_type (type);
7821 layout_decl (cldecl, 0);
7822 TREE_TYPE (decl)
7823 = c_build_qualified_type (type, TYPE_QUALS (etype));
7824 }
7825 }
7826 }
7827 }
7828
7829 /* Methods for storing and printing names for error messages. */
7830
7831 /* Implement a spelling stack that allows components of a name to be pushed
7832 and popped. Each element on the stack is this structure. */
7833
7834 struct spelling
7835 {
7836 int kind;
7837 union
7838 {
7839 unsigned HOST_WIDE_INT i;
7840 const char *s;
7841 } u;
7842 };
7843
7844 #define SPELLING_STRING 1
7845 #define SPELLING_MEMBER 2
7846 #define SPELLING_BOUNDS 3
7847
7848 static struct spelling *spelling; /* Next stack element (unused). */
7849 static struct spelling *spelling_base; /* Spelling stack base. */
7850 static int spelling_size; /* Size of the spelling stack. */
7851
7852 /* Macros to save and restore the spelling stack around push_... functions.
7853 Alternative to SAVE_SPELLING_STACK. */
7854
7855 #define SPELLING_DEPTH() (spelling - spelling_base)
7856 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7857
7858 /* Push an element on the spelling stack with type KIND and assign VALUE
7859 to MEMBER. */
7860
7861 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7862 { \
7863 int depth = SPELLING_DEPTH (); \
7864 \
7865 if (depth >= spelling_size) \
7866 { \
7867 spelling_size += 10; \
7868 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7869 spelling_size); \
7870 RESTORE_SPELLING_DEPTH (depth); \
7871 } \
7872 \
7873 spelling->kind = (KIND); \
7874 spelling->MEMBER = (VALUE); \
7875 spelling++; \
7876 }
7877
7878 /* Push STRING on the stack. Printed literally. */
7879
7880 static void
push_string(const char * string)7881 push_string (const char *string)
7882 {
7883 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7884 }
7885
7886 /* Push a member name on the stack. Printed as '.' STRING. */
7887
7888 static void
push_member_name(tree decl)7889 push_member_name (tree decl)
7890 {
7891 const char *const string
7892 = (DECL_NAME (decl)
7893 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7894 : _("<anonymous>"));
7895 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7896 }
7897
7898 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7899
7900 static void
push_array_bounds(unsigned HOST_WIDE_INT bounds)7901 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7902 {
7903 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7904 }
7905
7906 /* Compute the maximum size in bytes of the printed spelling. */
7907
7908 static int
spelling_length(void)7909 spelling_length (void)
7910 {
7911 int size = 0;
7912 struct spelling *p;
7913
7914 for (p = spelling_base; p < spelling; p++)
7915 {
7916 if (p->kind == SPELLING_BOUNDS)
7917 size += 25;
7918 else
7919 size += strlen (p->u.s) + 1;
7920 }
7921
7922 return size;
7923 }
7924
7925 /* Print the spelling to BUFFER and return it. */
7926
7927 static char *
print_spelling(char * buffer)7928 print_spelling (char *buffer)
7929 {
7930 char *d = buffer;
7931 struct spelling *p;
7932
7933 for (p = spelling_base; p < spelling; p++)
7934 if (p->kind == SPELLING_BOUNDS)
7935 {
7936 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7937 d += strlen (d);
7938 }
7939 else
7940 {
7941 const char *s;
7942 if (p->kind == SPELLING_MEMBER)
7943 *d++ = '.';
7944 for (s = p->u.s; (*d = *s++); d++)
7945 ;
7946 }
7947 *d++ = '\0';
7948 return buffer;
7949 }
7950
7951 /* Digest the parser output INIT as an initializer for type TYPE.
7952 Return a C expression of type TYPE to represent the initial value.
7953
7954 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7955
7956 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7957
7958 If INIT is a string constant, STRICT_STRING is true if it is
7959 unparenthesized or we should not warn here for it being parenthesized.
7960 For other types of INIT, STRICT_STRING is not used.
7961
7962 INIT_LOC is the location of the INIT.
7963
7964 REQUIRE_CONSTANT requests an error if non-constant initializers or
7965 elements are seen. */
7966
7967 static tree
digest_init(location_t init_loc,tree type,tree init,tree origtype,bool null_pointer_constant,bool strict_string,int require_constant)7968 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7969 bool null_pointer_constant, bool strict_string,
7970 int require_constant)
7971 {
7972 enum tree_code code = TREE_CODE (type);
7973 tree inside_init = init;
7974 tree semantic_type = NULL_TREE;
7975 bool maybe_const = true;
7976
7977 if (type == error_mark_node
7978 || !init
7979 || error_operand_p (init))
7980 return error_mark_node;
7981
7982 STRIP_TYPE_NOPS (inside_init);
7983
7984 if (!c_in_omp_for)
7985 {
7986 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7987 {
7988 semantic_type = TREE_TYPE (inside_init);
7989 inside_init = TREE_OPERAND (inside_init, 0);
7990 }
7991 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7992 }
7993
7994 /* Initialization of an array of chars from a string constant
7995 optionally enclosed in braces. */
7996
7997 if (code == ARRAY_TYPE && inside_init
7998 && TREE_CODE (inside_init) == STRING_CST)
7999 {
8000 tree typ1
8001 = (TYPE_ATOMIC (TREE_TYPE (type))
8002 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
8003 TYPE_QUAL_ATOMIC)
8004 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
8005 /* Note that an array could be both an array of character type
8006 and an array of wchar_t if wchar_t is signed char or unsigned
8007 char. */
8008 bool char_array = (typ1 == char_type_node
8009 || typ1 == signed_char_type_node
8010 || typ1 == unsigned_char_type_node);
8011 bool wchar_array = !!comptypes (typ1, wchar_type_node);
8012 bool char16_array = !!comptypes (typ1, char16_type_node);
8013 bool char32_array = !!comptypes (typ1, char32_type_node);
8014
8015 if (char_array || wchar_array || char16_array || char32_array)
8016 {
8017 struct c_expr expr;
8018 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8019 bool incompat_string_cst = false;
8020 expr.value = inside_init;
8021 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8022 expr.original_type = NULL;
8023 maybe_warn_string_init (init_loc, type, expr);
8024
8025 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8026 pedwarn_init (init_loc, OPT_Wpedantic,
8027 "initialization of a flexible array member");
8028
8029 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8030 TYPE_MAIN_VARIANT (type)))
8031 return inside_init;
8032
8033 if (char_array)
8034 {
8035 if (typ2 != char_type_node)
8036 incompat_string_cst = true;
8037 }
8038 else if (!comptypes (typ1, typ2))
8039 incompat_string_cst = true;
8040
8041 if (incompat_string_cst)
8042 {
8043 error_init (init_loc, "cannot initialize array of %qT from "
8044 "a string literal with type array of %qT",
8045 typ1, typ2);
8046 return error_mark_node;
8047 }
8048
8049 if (TYPE_DOMAIN (type) != NULL_TREE
8050 && TYPE_SIZE (type) != NULL_TREE
8051 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8052 {
8053 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8054 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8055
8056 /* Subtract the size of a single (possibly wide) character
8057 because it's ok to ignore the terminating null char
8058 that is counted in the length of the constant. */
8059 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8060 pedwarn_init (init_loc, 0,
8061 ("initializer-string for array of %qT "
8062 "is too long"), typ1);
8063 else if (warn_cxx_compat
8064 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8065 warning_at (init_loc, OPT_Wc___compat,
8066 ("initializer-string for array of %qT "
8067 "is too long for C++"), typ1);
8068 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8069 {
8070 unsigned HOST_WIDE_INT size
8071 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8072 const char *p = TREE_STRING_POINTER (inside_init);
8073
8074 inside_init = build_string (size, p);
8075 }
8076 }
8077
8078 TREE_TYPE (inside_init) = type;
8079 return inside_init;
8080 }
8081 else if (INTEGRAL_TYPE_P (typ1))
8082 {
8083 error_init (init_loc, "array of inappropriate type initialized "
8084 "from string constant");
8085 return error_mark_node;
8086 }
8087 }
8088
8089 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8090 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8091 below and handle as a constructor. */
8092 if (code == VECTOR_TYPE
8093 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8094 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8095 && TREE_CONSTANT (inside_init))
8096 {
8097 if (TREE_CODE (inside_init) == VECTOR_CST
8098 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8099 TYPE_MAIN_VARIANT (type)))
8100 return inside_init;
8101
8102 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8103 {
8104 unsigned HOST_WIDE_INT ix;
8105 tree value;
8106 bool constant_p = true;
8107
8108 /* Iterate through elements and check if all constructor
8109 elements are *_CSTs. */
8110 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8111 if (!CONSTANT_CLASS_P (value))
8112 {
8113 constant_p = false;
8114 break;
8115 }
8116
8117 if (constant_p)
8118 return build_vector_from_ctor (type,
8119 CONSTRUCTOR_ELTS (inside_init));
8120 }
8121 }
8122
8123 if (warn_sequence_point)
8124 verify_sequence_points (inside_init);
8125
8126 /* Any type can be initialized
8127 from an expression of the same type, optionally with braces. */
8128
8129 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8130 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8131 TYPE_MAIN_VARIANT (type))
8132 || (code == ARRAY_TYPE
8133 && comptypes (TREE_TYPE (inside_init), type))
8134 || (gnu_vector_type_p (type)
8135 && comptypes (TREE_TYPE (inside_init), type))
8136 || (code == POINTER_TYPE
8137 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8138 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8139 TREE_TYPE (type)))))
8140 {
8141 if (code == POINTER_TYPE)
8142 {
8143 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8144 {
8145 if (TREE_CODE (inside_init) == STRING_CST
8146 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8147 inside_init = array_to_pointer_conversion
8148 (init_loc, inside_init);
8149 else
8150 {
8151 error_init (init_loc, "invalid use of non-lvalue array");
8152 return error_mark_node;
8153 }
8154 }
8155 }
8156
8157 if (code == VECTOR_TYPE)
8158 /* Although the types are compatible, we may require a
8159 conversion. */
8160 inside_init = convert (type, inside_init);
8161
8162 if (require_constant
8163 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8164 {
8165 /* As an extension, allow initializing objects with static storage
8166 duration with compound literals (which are then treated just as
8167 the brace enclosed list they contain). Also allow this for
8168 vectors, as we can only assign them with compound literals. */
8169 if (flag_isoc99 && code != VECTOR_TYPE)
8170 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8171 "is not constant");
8172 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8173 inside_init = DECL_INITIAL (decl);
8174 }
8175
8176 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8177 && TREE_CODE (inside_init) != CONSTRUCTOR)
8178 {
8179 error_init (init_loc, "array initialized from non-constant array "
8180 "expression");
8181 return error_mark_node;
8182 }
8183
8184 /* Compound expressions can only occur here if -Wpedantic or
8185 -pedantic-errors is specified. In the later case, we always want
8186 an error. In the former case, we simply want a warning. */
8187 if (require_constant && pedantic
8188 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8189 {
8190 inside_init
8191 = valid_compound_expr_initializer (inside_init,
8192 TREE_TYPE (inside_init));
8193 if (inside_init == error_mark_node)
8194 error_init (init_loc, "initializer element is not constant");
8195 else
8196 pedwarn_init (init_loc, OPT_Wpedantic,
8197 "initializer element is not constant");
8198 if (flag_pedantic_errors)
8199 inside_init = error_mark_node;
8200 }
8201 else if (require_constant
8202 && !initializer_constant_valid_p (inside_init,
8203 TREE_TYPE (inside_init)))
8204 {
8205 error_init (init_loc, "initializer element is not constant");
8206 inside_init = error_mark_node;
8207 }
8208 else if (require_constant && !maybe_const)
8209 pedwarn_init (init_loc, OPT_Wpedantic,
8210 "initializer element is not a constant expression");
8211
8212 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8213 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8214 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8215 type, inside_init, origtype,
8216 (require_constant
8217 ? ic_init_const
8218 : ic_init), null_pointer_constant,
8219 NULL_TREE, NULL_TREE, 0);
8220 return inside_init;
8221 }
8222
8223 /* Handle scalar types, including conversions. */
8224
8225 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8226 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8227 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8228 {
8229 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8230 && (TREE_CODE (init) == STRING_CST
8231 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8232 inside_init = init = array_to_pointer_conversion (init_loc, init);
8233 if (semantic_type)
8234 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8235 inside_init);
8236 inside_init
8237 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8238 inside_init, origtype,
8239 require_constant ? ic_init_const : ic_init,
8240 null_pointer_constant, NULL_TREE, NULL_TREE,
8241 0);
8242
8243 /* Check to see if we have already given an error message. */
8244 if (inside_init == error_mark_node)
8245 ;
8246 else if (require_constant && !TREE_CONSTANT (inside_init))
8247 {
8248 error_init (init_loc, "initializer element is not constant");
8249 inside_init = error_mark_node;
8250 }
8251 else if (require_constant
8252 && !initializer_constant_valid_p (inside_init,
8253 TREE_TYPE (inside_init)))
8254 {
8255 error_init (init_loc, "initializer element is not computable at "
8256 "load time");
8257 inside_init = error_mark_node;
8258 }
8259 else if (require_constant && !maybe_const)
8260 pedwarn_init (init_loc, OPT_Wpedantic,
8261 "initializer element is not a constant expression");
8262
8263 return inside_init;
8264 }
8265
8266 /* Come here only for records and arrays. */
8267
8268 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8269 {
8270 error_init (init_loc, "variable-sized object may not be initialized");
8271 return error_mark_node;
8272 }
8273
8274 error_init (init_loc, "invalid initializer");
8275 return error_mark_node;
8276 }
8277
8278 /* Handle initializers that use braces. */
8279
8280 /* Type of object we are accumulating a constructor for.
8281 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8282 static tree constructor_type;
8283
8284 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8285 left to fill. */
8286 static tree constructor_fields;
8287
8288 /* For an ARRAY_TYPE, this is the specified index
8289 at which to store the next element we get. */
8290 static tree constructor_index;
8291
8292 /* For an ARRAY_TYPE, this is the maximum index. */
8293 static tree constructor_max_index;
8294
8295 /* For a RECORD_TYPE, this is the first field not yet written out. */
8296 static tree constructor_unfilled_fields;
8297
8298 /* For an ARRAY_TYPE, this is the index of the first element
8299 not yet written out. */
8300 static tree constructor_unfilled_index;
8301
8302 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8303 This is so we can generate gaps between fields, when appropriate. */
8304 static tree constructor_bit_index;
8305
8306 /* If we are saving up the elements rather than allocating them,
8307 this is the list of elements so far (in reverse order,
8308 most recent first). */
8309 static vec<constructor_elt, va_gc> *constructor_elements;
8310
8311 /* 1 if constructor should be incrementally stored into a constructor chain,
8312 0 if all the elements should be kept in AVL tree. */
8313 static int constructor_incremental;
8314
8315 /* 1 if so far this constructor's elements are all compile-time constants. */
8316 static int constructor_constant;
8317
8318 /* 1 if so far this constructor's elements are all valid address constants. */
8319 static int constructor_simple;
8320
8321 /* 1 if this constructor has an element that cannot be part of a
8322 constant expression. */
8323 static int constructor_nonconst;
8324
8325 /* 1 if this constructor is erroneous so far. */
8326 static int constructor_erroneous;
8327
8328 /* 1 if this constructor is the universal zero initializer { 0 }. */
8329 static int constructor_zeroinit;
8330
8331 /* Structure for managing pending initializer elements, organized as an
8332 AVL tree. */
8333
8334 struct init_node
8335 {
8336 struct init_node *left, *right;
8337 struct init_node *parent;
8338 int balance;
8339 tree purpose;
8340 tree value;
8341 tree origtype;
8342 };
8343
8344 /* Tree of pending elements at this constructor level.
8345 These are elements encountered out of order
8346 which belong at places we haven't reached yet in actually
8347 writing the output.
8348 Will never hold tree nodes across GC runs. */
8349 static struct init_node *constructor_pending_elts;
8350
8351 /* The SPELLING_DEPTH of this constructor. */
8352 static int constructor_depth;
8353
8354 /* DECL node for which an initializer is being read.
8355 0 means we are reading a constructor expression
8356 such as (struct foo) {...}. */
8357 static tree constructor_decl;
8358
8359 /* Nonzero if this is an initializer for a top-level decl. */
8360 static int constructor_top_level;
8361
8362 /* Nonzero if there were any member designators in this initializer. */
8363 static int constructor_designated;
8364
8365 /* Nesting depth of designator list. */
8366 static int designator_depth;
8367
8368 /* Nonzero if there were diagnosed errors in this designator list. */
8369 static int designator_erroneous;
8370
8371
8372 /* This stack has a level for each implicit or explicit level of
8373 structuring in the initializer, including the outermost one. It
8374 saves the values of most of the variables above. */
8375
8376 struct constructor_range_stack;
8377
8378 struct constructor_stack
8379 {
8380 struct constructor_stack *next;
8381 tree type;
8382 tree fields;
8383 tree index;
8384 tree max_index;
8385 tree unfilled_index;
8386 tree unfilled_fields;
8387 tree bit_index;
8388 vec<constructor_elt, va_gc> *elements;
8389 struct init_node *pending_elts;
8390 int offset;
8391 int depth;
8392 /* If value nonzero, this value should replace the entire
8393 constructor at this level. */
8394 struct c_expr replacement_value;
8395 struct constructor_range_stack *range_stack;
8396 char constant;
8397 char simple;
8398 char nonconst;
8399 char implicit;
8400 char erroneous;
8401 char outer;
8402 char incremental;
8403 char designated;
8404 int designator_depth;
8405 };
8406
8407 static struct constructor_stack *constructor_stack;
8408
8409 /* This stack represents designators from some range designator up to
8410 the last designator in the list. */
8411
8412 struct constructor_range_stack
8413 {
8414 struct constructor_range_stack *next, *prev;
8415 struct constructor_stack *stack;
8416 tree range_start;
8417 tree index;
8418 tree range_end;
8419 tree fields;
8420 };
8421
8422 static struct constructor_range_stack *constructor_range_stack;
8423
8424 /* This stack records separate initializers that are nested.
8425 Nested initializers can't happen in ANSI C, but GNU C allows them
8426 in cases like { ... (struct foo) { ... } ... }. */
8427
8428 struct initializer_stack
8429 {
8430 struct initializer_stack *next;
8431 tree decl;
8432 struct constructor_stack *constructor_stack;
8433 struct constructor_range_stack *constructor_range_stack;
8434 vec<constructor_elt, va_gc> *elements;
8435 struct spelling *spelling;
8436 struct spelling *spelling_base;
8437 int spelling_size;
8438 char top_level;
8439 char require_constant_value;
8440 char require_constant_elements;
8441 char designated;
8442 rich_location *missing_brace_richloc;
8443 };
8444
8445 static struct initializer_stack *initializer_stack;
8446
8447 /* Prepare to parse and output the initializer for variable DECL. */
8448
8449 void
start_init(tree decl,tree asmspec_tree ATTRIBUTE_UNUSED,int top_level,rich_location * richloc)8450 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8451 rich_location *richloc)
8452 {
8453 const char *locus;
8454 struct initializer_stack *p = XNEW (struct initializer_stack);
8455
8456 p->decl = constructor_decl;
8457 p->require_constant_value = require_constant_value;
8458 p->require_constant_elements = require_constant_elements;
8459 p->constructor_stack = constructor_stack;
8460 p->constructor_range_stack = constructor_range_stack;
8461 p->elements = constructor_elements;
8462 p->spelling = spelling;
8463 p->spelling_base = spelling_base;
8464 p->spelling_size = spelling_size;
8465 p->top_level = constructor_top_level;
8466 p->next = initializer_stack;
8467 p->missing_brace_richloc = richloc;
8468 p->designated = constructor_designated;
8469 initializer_stack = p;
8470
8471 constructor_decl = decl;
8472 constructor_designated = 0;
8473 constructor_top_level = top_level;
8474
8475 if (decl != NULL_TREE && decl != error_mark_node)
8476 {
8477 require_constant_value = TREE_STATIC (decl);
8478 require_constant_elements
8479 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8480 /* For a scalar, you can always use any value to initialize,
8481 even within braces. */
8482 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8483 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8484 }
8485 else
8486 {
8487 require_constant_value = 0;
8488 require_constant_elements = 0;
8489 locus = _("(anonymous)");
8490 }
8491
8492 constructor_stack = 0;
8493 constructor_range_stack = 0;
8494
8495 found_missing_braces = 0;
8496
8497 spelling_base = 0;
8498 spelling_size = 0;
8499 RESTORE_SPELLING_DEPTH (0);
8500
8501 if (locus)
8502 push_string (locus);
8503 }
8504
8505 void
finish_init(void)8506 finish_init (void)
8507 {
8508 struct initializer_stack *p = initializer_stack;
8509
8510 /* Free the whole constructor stack of this initializer. */
8511 while (constructor_stack)
8512 {
8513 struct constructor_stack *q = constructor_stack;
8514 constructor_stack = q->next;
8515 XDELETE (q);
8516 }
8517
8518 gcc_assert (!constructor_range_stack);
8519
8520 /* Pop back to the data of the outer initializer (if any). */
8521 XDELETE (spelling_base);
8522
8523 constructor_decl = p->decl;
8524 require_constant_value = p->require_constant_value;
8525 require_constant_elements = p->require_constant_elements;
8526 constructor_stack = p->constructor_stack;
8527 constructor_designated = p->designated;
8528 constructor_range_stack = p->constructor_range_stack;
8529 constructor_elements = p->elements;
8530 spelling = p->spelling;
8531 spelling_base = p->spelling_base;
8532 spelling_size = p->spelling_size;
8533 constructor_top_level = p->top_level;
8534 initializer_stack = p->next;
8535 XDELETE (p);
8536 }
8537
8538 /* Call here when we see the initializer is surrounded by braces.
8539 This is instead of a call to push_init_level;
8540 it is matched by a call to pop_init_level.
8541
8542 TYPE is the type to initialize, for a constructor expression.
8543 For an initializer for a decl, TYPE is zero. */
8544
8545 void
really_start_incremental_init(tree type)8546 really_start_incremental_init (tree type)
8547 {
8548 struct constructor_stack *p = XNEW (struct constructor_stack);
8549
8550 if (type == NULL_TREE)
8551 type = TREE_TYPE (constructor_decl);
8552
8553 if (VECTOR_TYPE_P (type)
8554 && TYPE_VECTOR_OPAQUE (type))
8555 error ("opaque vector types cannot be initialized");
8556
8557 p->type = constructor_type;
8558 p->fields = constructor_fields;
8559 p->index = constructor_index;
8560 p->max_index = constructor_max_index;
8561 p->unfilled_index = constructor_unfilled_index;
8562 p->unfilled_fields = constructor_unfilled_fields;
8563 p->bit_index = constructor_bit_index;
8564 p->elements = constructor_elements;
8565 p->constant = constructor_constant;
8566 p->simple = constructor_simple;
8567 p->nonconst = constructor_nonconst;
8568 p->erroneous = constructor_erroneous;
8569 p->pending_elts = constructor_pending_elts;
8570 p->depth = constructor_depth;
8571 p->replacement_value.value = 0;
8572 p->replacement_value.original_code = ERROR_MARK;
8573 p->replacement_value.original_type = NULL;
8574 p->implicit = 0;
8575 p->range_stack = 0;
8576 p->outer = 0;
8577 p->incremental = constructor_incremental;
8578 p->designated = constructor_designated;
8579 p->designator_depth = designator_depth;
8580 p->next = 0;
8581 constructor_stack = p;
8582
8583 constructor_constant = 1;
8584 constructor_simple = 1;
8585 constructor_nonconst = 0;
8586 constructor_depth = SPELLING_DEPTH ();
8587 constructor_elements = NULL;
8588 constructor_pending_elts = 0;
8589 constructor_type = type;
8590 constructor_incremental = 1;
8591 constructor_designated = 0;
8592 constructor_zeroinit = 1;
8593 designator_depth = 0;
8594 designator_erroneous = 0;
8595
8596 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8597 {
8598 constructor_fields = TYPE_FIELDS (constructor_type);
8599 /* Skip any nameless bit fields at the beginning. */
8600 while (constructor_fields != NULL_TREE
8601 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8602 constructor_fields = DECL_CHAIN (constructor_fields);
8603
8604 constructor_unfilled_fields = constructor_fields;
8605 constructor_bit_index = bitsize_zero_node;
8606 }
8607 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8608 {
8609 if (TYPE_DOMAIN (constructor_type))
8610 {
8611 constructor_max_index
8612 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8613
8614 /* Detect non-empty initializations of zero-length arrays. */
8615 if (constructor_max_index == NULL_TREE
8616 && TYPE_SIZE (constructor_type))
8617 constructor_max_index = integer_minus_one_node;
8618
8619 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8620 to initialize VLAs will cause a proper error; avoid tree
8621 checking errors as well by setting a safe value. */
8622 if (constructor_max_index
8623 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8624 constructor_max_index = integer_minus_one_node;
8625
8626 constructor_index
8627 = convert (bitsizetype,
8628 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8629 }
8630 else
8631 {
8632 constructor_index = bitsize_zero_node;
8633 constructor_max_index = NULL_TREE;
8634 }
8635
8636 constructor_unfilled_index = constructor_index;
8637 }
8638 else if (gnu_vector_type_p (constructor_type))
8639 {
8640 /* Vectors are like simple fixed-size arrays. */
8641 constructor_max_index =
8642 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8643 constructor_index = bitsize_zero_node;
8644 constructor_unfilled_index = constructor_index;
8645 }
8646 else
8647 {
8648 /* Handle the case of int x = {5}; */
8649 constructor_fields = constructor_type;
8650 constructor_unfilled_fields = constructor_type;
8651 }
8652 }
8653
8654 extern location_t last_init_list_comma;
8655
8656 /* Called when we see an open brace for a nested initializer. Finish
8657 off any pending levels with implicit braces. */
8658 void
finish_implicit_inits(location_t loc,struct obstack * braced_init_obstack)8659 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8660 {
8661 while (constructor_stack->implicit)
8662 {
8663 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8664 && constructor_fields == NULL_TREE)
8665 process_init_element (input_location,
8666 pop_init_level (loc, 1, braced_init_obstack,
8667 last_init_list_comma),
8668 true, braced_init_obstack);
8669 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8670 && constructor_max_index
8671 && tree_int_cst_lt (constructor_max_index,
8672 constructor_index))
8673 process_init_element (input_location,
8674 pop_init_level (loc, 1, braced_init_obstack,
8675 last_init_list_comma),
8676 true, braced_init_obstack);
8677 else
8678 break;
8679 }
8680 }
8681
8682 /* Push down into a subobject, for initialization.
8683 If this is for an explicit set of braces, IMPLICIT is 0.
8684 If it is because the next element belongs at a lower level,
8685 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8686
8687 void
push_init_level(location_t loc,int implicit,struct obstack * braced_init_obstack)8688 push_init_level (location_t loc, int implicit,
8689 struct obstack *braced_init_obstack)
8690 {
8691 struct constructor_stack *p;
8692 tree value = NULL_TREE;
8693
8694 /* Unless this is an explicit brace, we need to preserve previous
8695 content if any. */
8696 if (implicit)
8697 {
8698 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8699 value = find_init_member (constructor_fields, braced_init_obstack);
8700 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8701 value = find_init_member (constructor_index, braced_init_obstack);
8702 }
8703
8704 p = XNEW (struct constructor_stack);
8705 p->type = constructor_type;
8706 p->fields = constructor_fields;
8707 p->index = constructor_index;
8708 p->max_index = constructor_max_index;
8709 p->unfilled_index = constructor_unfilled_index;
8710 p->unfilled_fields = constructor_unfilled_fields;
8711 p->bit_index = constructor_bit_index;
8712 p->elements = constructor_elements;
8713 p->constant = constructor_constant;
8714 p->simple = constructor_simple;
8715 p->nonconst = constructor_nonconst;
8716 p->erroneous = constructor_erroneous;
8717 p->pending_elts = constructor_pending_elts;
8718 p->depth = constructor_depth;
8719 p->replacement_value.value = NULL_TREE;
8720 p->replacement_value.original_code = ERROR_MARK;
8721 p->replacement_value.original_type = NULL;
8722 p->implicit = implicit;
8723 p->outer = 0;
8724 p->incremental = constructor_incremental;
8725 p->designated = constructor_designated;
8726 p->designator_depth = designator_depth;
8727 p->next = constructor_stack;
8728 p->range_stack = 0;
8729 constructor_stack = p;
8730
8731 constructor_constant = 1;
8732 constructor_simple = 1;
8733 constructor_nonconst = 0;
8734 constructor_depth = SPELLING_DEPTH ();
8735 constructor_elements = NULL;
8736 constructor_incremental = 1;
8737 /* If the upper initializer is designated, then mark this as
8738 designated too to prevent bogus warnings. */
8739 constructor_designated = p->designated;
8740 constructor_pending_elts = 0;
8741 if (!implicit)
8742 {
8743 p->range_stack = constructor_range_stack;
8744 constructor_range_stack = 0;
8745 designator_depth = 0;
8746 designator_erroneous = 0;
8747 }
8748
8749 /* Don't die if an entire brace-pair level is superfluous
8750 in the containing level. */
8751 if (constructor_type == NULL_TREE)
8752 ;
8753 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8754 {
8755 /* Don't die if there are extra init elts at the end. */
8756 if (constructor_fields == NULL_TREE)
8757 constructor_type = NULL_TREE;
8758 else
8759 {
8760 constructor_type = TREE_TYPE (constructor_fields);
8761 push_member_name (constructor_fields);
8762 constructor_depth++;
8763 }
8764 }
8765 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8766 {
8767 constructor_type = TREE_TYPE (constructor_type);
8768 push_array_bounds (tree_to_uhwi (constructor_index));
8769 constructor_depth++;
8770 }
8771
8772 if (constructor_type == NULL_TREE)
8773 {
8774 error_init (loc, "extra brace group at end of initializer");
8775 constructor_fields = NULL_TREE;
8776 constructor_unfilled_fields = NULL_TREE;
8777 return;
8778 }
8779
8780 if (value && TREE_CODE (value) == CONSTRUCTOR)
8781 {
8782 constructor_constant = TREE_CONSTANT (value);
8783 constructor_simple = TREE_STATIC (value);
8784 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8785 constructor_elements = CONSTRUCTOR_ELTS (value);
8786 if (!vec_safe_is_empty (constructor_elements)
8787 && (TREE_CODE (constructor_type) == RECORD_TYPE
8788 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8789 set_nonincremental_init (braced_init_obstack);
8790 }
8791
8792 if (implicit == 1)
8793 {
8794 found_missing_braces = 1;
8795 if (initializer_stack->missing_brace_richloc)
8796 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8797 (loc, "{");
8798 }
8799
8800 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8801 {
8802 constructor_fields = TYPE_FIELDS (constructor_type);
8803 /* Skip any nameless bit fields at the beginning. */
8804 while (constructor_fields != NULL_TREE
8805 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8806 constructor_fields = DECL_CHAIN (constructor_fields);
8807
8808 constructor_unfilled_fields = constructor_fields;
8809 constructor_bit_index = bitsize_zero_node;
8810 }
8811 else if (gnu_vector_type_p (constructor_type))
8812 {
8813 /* Vectors are like simple fixed-size arrays. */
8814 constructor_max_index =
8815 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8816 constructor_index = bitsize_int (0);
8817 constructor_unfilled_index = constructor_index;
8818 }
8819 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8820 {
8821 if (TYPE_DOMAIN (constructor_type))
8822 {
8823 constructor_max_index
8824 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8825
8826 /* Detect non-empty initializations of zero-length arrays. */
8827 if (constructor_max_index == NULL_TREE
8828 && TYPE_SIZE (constructor_type))
8829 constructor_max_index = integer_minus_one_node;
8830
8831 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8832 to initialize VLAs will cause a proper error; avoid tree
8833 checking errors as well by setting a safe value. */
8834 if (constructor_max_index
8835 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8836 constructor_max_index = integer_minus_one_node;
8837
8838 constructor_index
8839 = convert (bitsizetype,
8840 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8841 }
8842 else
8843 constructor_index = bitsize_zero_node;
8844
8845 constructor_unfilled_index = constructor_index;
8846 if (value && TREE_CODE (value) == STRING_CST)
8847 {
8848 /* We need to split the char/wchar array into individual
8849 characters, so that we don't have to special case it
8850 everywhere. */
8851 set_nonincremental_init_from_string (value, braced_init_obstack);
8852 }
8853 }
8854 else
8855 {
8856 if (constructor_type != error_mark_node)
8857 warning_init (input_location, 0, "braces around scalar initializer");
8858 constructor_fields = constructor_type;
8859 constructor_unfilled_fields = constructor_type;
8860 }
8861 }
8862
8863 /* At the end of an implicit or explicit brace level,
8864 finish up that level of constructor. If a single expression
8865 with redundant braces initialized that level, return the
8866 c_expr structure for that expression. Otherwise, the original_code
8867 element is set to ERROR_MARK.
8868 If we were outputting the elements as they are read, return 0 as the value
8869 from inner levels (process_init_element ignores that),
8870 but return error_mark_node as the value from the outermost level
8871 (that's what we want to put in DECL_INITIAL).
8872 Otherwise, return a CONSTRUCTOR expression as the value. */
8873
8874 struct c_expr
pop_init_level(location_t loc,int implicit,struct obstack * braced_init_obstack,location_t insert_before)8875 pop_init_level (location_t loc, int implicit,
8876 struct obstack *braced_init_obstack,
8877 location_t insert_before)
8878 {
8879 struct constructor_stack *p;
8880 struct c_expr ret;
8881 ret.value = NULL_TREE;
8882 ret.original_code = ERROR_MARK;
8883 ret.original_type = NULL;
8884
8885 if (implicit == 0)
8886 {
8887 /* When we come to an explicit close brace,
8888 pop any inner levels that didn't have explicit braces. */
8889 while (constructor_stack->implicit)
8890 process_init_element (input_location,
8891 pop_init_level (loc, 1, braced_init_obstack,
8892 insert_before),
8893 true, braced_init_obstack);
8894 gcc_assert (!constructor_range_stack);
8895 }
8896 else
8897 if (initializer_stack->missing_brace_richloc)
8898 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8899 (insert_before, "}");
8900
8901 /* Now output all pending elements. */
8902 constructor_incremental = 1;
8903 output_pending_init_elements (1, braced_init_obstack);
8904
8905 p = constructor_stack;
8906
8907 /* Error for initializing a flexible array member, or a zero-length
8908 array member in an inappropriate context. */
8909 if (constructor_type && constructor_fields
8910 && TREE_CODE (constructor_type) == ARRAY_TYPE
8911 && TYPE_DOMAIN (constructor_type)
8912 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8913 {
8914 /* Silently discard empty initializations. The parser will
8915 already have pedwarned for empty brackets. */
8916 if (integer_zerop (constructor_unfilled_index))
8917 constructor_type = NULL_TREE;
8918 else
8919 {
8920 gcc_assert (!TYPE_SIZE (constructor_type));
8921
8922 if (constructor_depth > 2)
8923 error_init (loc, "initialization of flexible array member in a nested context");
8924 else
8925 pedwarn_init (loc, OPT_Wpedantic,
8926 "initialization of a flexible array member");
8927
8928 /* We have already issued an error message for the existence
8929 of a flexible array member not at the end of the structure.
8930 Discard the initializer so that we do not die later. */
8931 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8932 constructor_type = NULL_TREE;
8933 }
8934 }
8935
8936 switch (vec_safe_length (constructor_elements))
8937 {
8938 case 0:
8939 /* Initialization with { } counts as zeroinit. */
8940 constructor_zeroinit = 1;
8941 break;
8942 case 1:
8943 /* This might be zeroinit as well. */
8944 if (integer_zerop ((*constructor_elements)[0].value))
8945 constructor_zeroinit = 1;
8946 break;
8947 default:
8948 /* If the constructor has more than one element, it can't be { 0 }. */
8949 constructor_zeroinit = 0;
8950 break;
8951 }
8952
8953 /* Warn when some structs are initialized with direct aggregation. */
8954 if (!implicit && found_missing_braces && warn_missing_braces
8955 && !constructor_zeroinit)
8956 {
8957 gcc_assert (initializer_stack->missing_brace_richloc);
8958 warning_at (initializer_stack->missing_brace_richloc,
8959 OPT_Wmissing_braces,
8960 "missing braces around initializer");
8961 }
8962
8963 /* Warn when some struct elements are implicitly initialized to zero. */
8964 if (warn_missing_field_initializers
8965 && constructor_type
8966 && TREE_CODE (constructor_type) == RECORD_TYPE
8967 && constructor_unfilled_fields)
8968 {
8969 /* Do not warn for flexible array members or zero-length arrays. */
8970 while (constructor_unfilled_fields
8971 && (!DECL_SIZE (constructor_unfilled_fields)
8972 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8973 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8974
8975 if (constructor_unfilled_fields
8976 /* Do not warn if this level of the initializer uses member
8977 designators; it is likely to be deliberate. */
8978 && !constructor_designated
8979 /* Do not warn about initializing with { 0 } or with { }. */
8980 && !constructor_zeroinit)
8981 {
8982 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8983 "missing initializer for field %qD of %qT",
8984 constructor_unfilled_fields,
8985 constructor_type))
8986 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8987 "%qD declared here", constructor_unfilled_fields);
8988 }
8989 }
8990
8991 /* Pad out the end of the structure. */
8992 if (p->replacement_value.value)
8993 /* If this closes a superfluous brace pair,
8994 just pass out the element between them. */
8995 ret = p->replacement_value;
8996 else if (constructor_type == NULL_TREE)
8997 ;
8998 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8999 && TREE_CODE (constructor_type) != ARRAY_TYPE
9000 && !gnu_vector_type_p (constructor_type))
9001 {
9002 /* A nonincremental scalar initializer--just return
9003 the element, after verifying there is just one. */
9004 if (vec_safe_is_empty (constructor_elements))
9005 {
9006 if (!constructor_erroneous && constructor_type != error_mark_node)
9007 error_init (loc, "empty scalar initializer");
9008 ret.value = error_mark_node;
9009 }
9010 else if (vec_safe_length (constructor_elements) != 1)
9011 {
9012 error_init (loc, "extra elements in scalar initializer");
9013 ret.value = (*constructor_elements)[0].value;
9014 }
9015 else
9016 ret.value = (*constructor_elements)[0].value;
9017 }
9018 else
9019 {
9020 if (constructor_erroneous)
9021 ret.value = error_mark_node;
9022 else
9023 {
9024 ret.value = build_constructor (constructor_type,
9025 constructor_elements);
9026 if (constructor_constant)
9027 TREE_CONSTANT (ret.value) = 1;
9028 if (constructor_constant && constructor_simple)
9029 TREE_STATIC (ret.value) = 1;
9030 if (constructor_nonconst)
9031 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9032 }
9033 }
9034
9035 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9036 {
9037 if (constructor_nonconst)
9038 ret.original_code = C_MAYBE_CONST_EXPR;
9039 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9040 ret.original_code = ERROR_MARK;
9041 }
9042
9043 constructor_type = p->type;
9044 constructor_fields = p->fields;
9045 constructor_index = p->index;
9046 constructor_max_index = p->max_index;
9047 constructor_unfilled_index = p->unfilled_index;
9048 constructor_unfilled_fields = p->unfilled_fields;
9049 constructor_bit_index = p->bit_index;
9050 constructor_elements = p->elements;
9051 constructor_constant = p->constant;
9052 constructor_simple = p->simple;
9053 constructor_nonconst = p->nonconst;
9054 constructor_erroneous = p->erroneous;
9055 constructor_incremental = p->incremental;
9056 constructor_designated = p->designated;
9057 designator_depth = p->designator_depth;
9058 constructor_pending_elts = p->pending_elts;
9059 constructor_depth = p->depth;
9060 if (!p->implicit)
9061 constructor_range_stack = p->range_stack;
9062 RESTORE_SPELLING_DEPTH (constructor_depth);
9063
9064 constructor_stack = p->next;
9065 XDELETE (p);
9066
9067 if (ret.value == NULL_TREE && constructor_stack == 0)
9068 ret.value = error_mark_node;
9069 return ret;
9070 }
9071
9072 /* Common handling for both array range and field name designators.
9073 ARRAY argument is nonzero for array ranges. Returns false for success. */
9074
9075 static bool
set_designator(location_t loc,bool array,struct obstack * braced_init_obstack)9076 set_designator (location_t loc, bool array,
9077 struct obstack *braced_init_obstack)
9078 {
9079 tree subtype;
9080 enum tree_code subcode;
9081
9082 /* Don't die if an entire brace-pair level is superfluous
9083 in the containing level, or for an erroneous type. */
9084 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9085 return true;
9086
9087 /* If there were errors in this designator list already, bail out
9088 silently. */
9089 if (designator_erroneous)
9090 return true;
9091
9092 /* Likewise for an initializer for a variable-size type. Those are
9093 diagnosed in digest_init. */
9094 if (COMPLETE_TYPE_P (constructor_type)
9095 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9096 return true;
9097
9098 if (!designator_depth)
9099 {
9100 gcc_assert (!constructor_range_stack);
9101
9102 /* Designator list starts at the level of closest explicit
9103 braces. */
9104 while (constructor_stack->implicit)
9105 process_init_element (input_location,
9106 pop_init_level (loc, 1, braced_init_obstack,
9107 last_init_list_comma),
9108 true, braced_init_obstack);
9109 constructor_designated = 1;
9110 return false;
9111 }
9112
9113 switch (TREE_CODE (constructor_type))
9114 {
9115 case RECORD_TYPE:
9116 case UNION_TYPE:
9117 subtype = TREE_TYPE (constructor_fields);
9118 if (subtype != error_mark_node)
9119 subtype = TYPE_MAIN_VARIANT (subtype);
9120 break;
9121 case ARRAY_TYPE:
9122 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9123 break;
9124 default:
9125 gcc_unreachable ();
9126 }
9127
9128 subcode = TREE_CODE (subtype);
9129 if (array && subcode != ARRAY_TYPE)
9130 {
9131 error_init (loc, "array index in non-array initializer");
9132 return true;
9133 }
9134 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9135 {
9136 error_init (loc, "field name not in record or union initializer");
9137 return true;
9138 }
9139
9140 constructor_designated = 1;
9141 finish_implicit_inits (loc, braced_init_obstack);
9142 push_init_level (loc, 2, braced_init_obstack);
9143 return false;
9144 }
9145
9146 /* If there are range designators in designator list, push a new designator
9147 to constructor_range_stack. RANGE_END is end of such stack range or
9148 NULL_TREE if there is no range designator at this level. */
9149
9150 static void
push_range_stack(tree range_end,struct obstack * braced_init_obstack)9151 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9152 {
9153 struct constructor_range_stack *p;
9154
9155 p = (struct constructor_range_stack *)
9156 obstack_alloc (braced_init_obstack,
9157 sizeof (struct constructor_range_stack));
9158 p->prev = constructor_range_stack;
9159 p->next = 0;
9160 p->fields = constructor_fields;
9161 p->range_start = constructor_index;
9162 p->index = constructor_index;
9163 p->stack = constructor_stack;
9164 p->range_end = range_end;
9165 if (constructor_range_stack)
9166 constructor_range_stack->next = p;
9167 constructor_range_stack = p;
9168 }
9169
9170 /* Within an array initializer, specify the next index to be initialized.
9171 FIRST is that index. If LAST is nonzero, then initialize a range
9172 of indices, running from FIRST through LAST. */
9173
9174 void
set_init_index(location_t loc,tree first,tree last,struct obstack * braced_init_obstack)9175 set_init_index (location_t loc, tree first, tree last,
9176 struct obstack *braced_init_obstack)
9177 {
9178 if (set_designator (loc, true, braced_init_obstack))
9179 return;
9180
9181 designator_erroneous = 1;
9182
9183 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9184 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9185 {
9186 error_init (loc, "array index in initializer not of integer type");
9187 return;
9188 }
9189
9190 if (TREE_CODE (first) != INTEGER_CST)
9191 {
9192 first = c_fully_fold (first, false, NULL);
9193 if (TREE_CODE (first) == INTEGER_CST)
9194 pedwarn_init (loc, OPT_Wpedantic,
9195 "array index in initializer is not "
9196 "an integer constant expression");
9197 }
9198
9199 if (last && TREE_CODE (last) != INTEGER_CST)
9200 {
9201 last = c_fully_fold (last, false, NULL);
9202 if (TREE_CODE (last) == INTEGER_CST)
9203 pedwarn_init (loc, OPT_Wpedantic,
9204 "array index in initializer is not "
9205 "an integer constant expression");
9206 }
9207
9208 if (TREE_CODE (first) != INTEGER_CST)
9209 error_init (loc, "nonconstant array index in initializer");
9210 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9211 error_init (loc, "nonconstant array index in initializer");
9212 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9213 error_init (loc, "array index in non-array initializer");
9214 else if (tree_int_cst_sgn (first) == -1)
9215 error_init (loc, "array index in initializer exceeds array bounds");
9216 else if (constructor_max_index
9217 && tree_int_cst_lt (constructor_max_index, first))
9218 error_init (loc, "array index in initializer exceeds array bounds");
9219 else
9220 {
9221 constant_expression_warning (first);
9222 if (last)
9223 constant_expression_warning (last);
9224 constructor_index = convert (bitsizetype, first);
9225 if (tree_int_cst_lt (constructor_index, first))
9226 {
9227 constructor_index = copy_node (constructor_index);
9228 TREE_OVERFLOW (constructor_index) = 1;
9229 }
9230
9231 if (last)
9232 {
9233 if (tree_int_cst_equal (first, last))
9234 last = NULL_TREE;
9235 else if (tree_int_cst_lt (last, first))
9236 {
9237 error_init (loc, "empty index range in initializer");
9238 last = NULL_TREE;
9239 }
9240 else
9241 {
9242 last = convert (bitsizetype, last);
9243 if (constructor_max_index != NULL_TREE
9244 && tree_int_cst_lt (constructor_max_index, last))
9245 {
9246 error_init (loc, "array index range in initializer exceeds "
9247 "array bounds");
9248 last = NULL_TREE;
9249 }
9250 }
9251 }
9252
9253 designator_depth++;
9254 designator_erroneous = 0;
9255 if (constructor_range_stack || last)
9256 push_range_stack (last, braced_init_obstack);
9257 }
9258 }
9259
9260 /* Within a struct initializer, specify the next field to be initialized. */
9261
9262 void
set_init_label(location_t loc,tree fieldname,location_t fieldname_loc,struct obstack * braced_init_obstack)9263 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9264 struct obstack *braced_init_obstack)
9265 {
9266 tree field;
9267
9268 if (set_designator (loc, false, braced_init_obstack))
9269 return;
9270
9271 designator_erroneous = 1;
9272
9273 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9274 {
9275 error_init (loc, "field name not in record or union initializer");
9276 return;
9277 }
9278
9279 field = lookup_field (constructor_type, fieldname);
9280
9281 if (field == NULL_TREE)
9282 {
9283 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9284 if (guessed_id)
9285 {
9286 gcc_rich_location rich_loc (fieldname_loc);
9287 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9288 error_at (&rich_loc,
9289 "%qT has no member named %qE; did you mean %qE?",
9290 constructor_type, fieldname, guessed_id);
9291 }
9292 else
9293 error_at (fieldname_loc, "%qT has no member named %qE",
9294 constructor_type, fieldname);
9295 }
9296 else
9297 do
9298 {
9299 constructor_fields = TREE_VALUE (field);
9300 designator_depth++;
9301 designator_erroneous = 0;
9302 if (constructor_range_stack)
9303 push_range_stack (NULL_TREE, braced_init_obstack);
9304 field = TREE_CHAIN (field);
9305 if (field)
9306 {
9307 if (set_designator (loc, false, braced_init_obstack))
9308 return;
9309 }
9310 }
9311 while (field != NULL_TREE);
9312 }
9313
9314 /* Add a new initializer to the tree of pending initializers. PURPOSE
9315 identifies the initializer, either array index or field in a structure.
9316 VALUE is the value of that index or field. If ORIGTYPE is not
9317 NULL_TREE, it is the original type of VALUE.
9318
9319 IMPLICIT is true if value comes from pop_init_level (1),
9320 the new initializer has been merged with the existing one
9321 and thus no warnings should be emitted about overriding an
9322 existing initializer. */
9323
9324 static void
add_pending_init(location_t loc,tree purpose,tree value,tree origtype,bool implicit,struct obstack * braced_init_obstack)9325 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9326 bool implicit, struct obstack *braced_init_obstack)
9327 {
9328 struct init_node *p, **q, *r;
9329
9330 q = &constructor_pending_elts;
9331 p = 0;
9332
9333 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9334 {
9335 while (*q != 0)
9336 {
9337 p = *q;
9338 if (tree_int_cst_lt (purpose, p->purpose))
9339 q = &p->left;
9340 else if (tree_int_cst_lt (p->purpose, purpose))
9341 q = &p->right;
9342 else
9343 {
9344 if (!implicit)
9345 {
9346 if (TREE_SIDE_EFFECTS (p->value))
9347 warning_init (loc, OPT_Woverride_init_side_effects,
9348 "initialized field with side-effects "
9349 "overwritten");
9350 else if (warn_override_init)
9351 warning_init (loc, OPT_Woverride_init,
9352 "initialized field overwritten");
9353 }
9354 p->value = value;
9355 p->origtype = origtype;
9356 return;
9357 }
9358 }
9359 }
9360 else
9361 {
9362 tree bitpos;
9363
9364 bitpos = bit_position (purpose);
9365 while (*q != NULL)
9366 {
9367 p = *q;
9368 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9369 q = &p->left;
9370 else if (p->purpose != purpose)
9371 q = &p->right;
9372 else
9373 {
9374 if (!implicit)
9375 {
9376 if (TREE_SIDE_EFFECTS (p->value))
9377 warning_init (loc, OPT_Woverride_init_side_effects,
9378 "initialized field with side-effects "
9379 "overwritten");
9380 else if (warn_override_init)
9381 warning_init (loc, OPT_Woverride_init,
9382 "initialized field overwritten");
9383 }
9384 p->value = value;
9385 p->origtype = origtype;
9386 return;
9387 }
9388 }
9389 }
9390
9391 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9392 sizeof (struct init_node));
9393 r->purpose = purpose;
9394 r->value = value;
9395 r->origtype = origtype;
9396
9397 *q = r;
9398 r->parent = p;
9399 r->left = 0;
9400 r->right = 0;
9401 r->balance = 0;
9402
9403 while (p)
9404 {
9405 struct init_node *s;
9406
9407 if (r == p->left)
9408 {
9409 if (p->balance == 0)
9410 p->balance = -1;
9411 else if (p->balance < 0)
9412 {
9413 if (r->balance < 0)
9414 {
9415 /* L rotation. */
9416 p->left = r->right;
9417 if (p->left)
9418 p->left->parent = p;
9419 r->right = p;
9420
9421 p->balance = 0;
9422 r->balance = 0;
9423
9424 s = p->parent;
9425 p->parent = r;
9426 r->parent = s;
9427 if (s)
9428 {
9429 if (s->left == p)
9430 s->left = r;
9431 else
9432 s->right = r;
9433 }
9434 else
9435 constructor_pending_elts = r;
9436 }
9437 else
9438 {
9439 /* LR rotation. */
9440 struct init_node *t = r->right;
9441
9442 r->right = t->left;
9443 if (r->right)
9444 r->right->parent = r;
9445 t->left = r;
9446
9447 p->left = t->right;
9448 if (p->left)
9449 p->left->parent = p;
9450 t->right = p;
9451
9452 p->balance = t->balance < 0;
9453 r->balance = -(t->balance > 0);
9454 t->balance = 0;
9455
9456 s = p->parent;
9457 p->parent = t;
9458 r->parent = t;
9459 t->parent = s;
9460 if (s)
9461 {
9462 if (s->left == p)
9463 s->left = t;
9464 else
9465 s->right = t;
9466 }
9467 else
9468 constructor_pending_elts = t;
9469 }
9470 break;
9471 }
9472 else
9473 {
9474 /* p->balance == +1; growth of left side balances the node. */
9475 p->balance = 0;
9476 break;
9477 }
9478 }
9479 else /* r == p->right */
9480 {
9481 if (p->balance == 0)
9482 /* Growth propagation from right side. */
9483 p->balance++;
9484 else if (p->balance > 0)
9485 {
9486 if (r->balance > 0)
9487 {
9488 /* R rotation. */
9489 p->right = r->left;
9490 if (p->right)
9491 p->right->parent = p;
9492 r->left = p;
9493
9494 p->balance = 0;
9495 r->balance = 0;
9496
9497 s = p->parent;
9498 p->parent = r;
9499 r->parent = s;
9500 if (s)
9501 {
9502 if (s->left == p)
9503 s->left = r;
9504 else
9505 s->right = r;
9506 }
9507 else
9508 constructor_pending_elts = r;
9509 }
9510 else /* r->balance == -1 */
9511 {
9512 /* RL rotation */
9513 struct init_node *t = r->left;
9514
9515 r->left = t->right;
9516 if (r->left)
9517 r->left->parent = r;
9518 t->right = r;
9519
9520 p->right = t->left;
9521 if (p->right)
9522 p->right->parent = p;
9523 t->left = p;
9524
9525 r->balance = (t->balance < 0);
9526 p->balance = -(t->balance > 0);
9527 t->balance = 0;
9528
9529 s = p->parent;
9530 p->parent = t;
9531 r->parent = t;
9532 t->parent = s;
9533 if (s)
9534 {
9535 if (s->left == p)
9536 s->left = t;
9537 else
9538 s->right = t;
9539 }
9540 else
9541 constructor_pending_elts = t;
9542 }
9543 break;
9544 }
9545 else
9546 {
9547 /* p->balance == -1; growth of right side balances the node. */
9548 p->balance = 0;
9549 break;
9550 }
9551 }
9552
9553 r = p;
9554 p = p->parent;
9555 }
9556 }
9557
9558 /* Build AVL tree from a sorted chain. */
9559
9560 static void
set_nonincremental_init(struct obstack * braced_init_obstack)9561 set_nonincremental_init (struct obstack * braced_init_obstack)
9562 {
9563 unsigned HOST_WIDE_INT ix;
9564 tree index, value;
9565
9566 if (TREE_CODE (constructor_type) != RECORD_TYPE
9567 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9568 return;
9569
9570 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9571 add_pending_init (input_location, index, value, NULL_TREE, true,
9572 braced_init_obstack);
9573 constructor_elements = NULL;
9574 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9575 {
9576 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9577 /* Skip any nameless bit fields at the beginning. */
9578 while (constructor_unfilled_fields != NULL_TREE
9579 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9580 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9581
9582 }
9583 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9584 {
9585 if (TYPE_DOMAIN (constructor_type))
9586 constructor_unfilled_index
9587 = convert (bitsizetype,
9588 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9589 else
9590 constructor_unfilled_index = bitsize_zero_node;
9591 }
9592 constructor_incremental = 0;
9593 }
9594
9595 /* Build AVL tree from a string constant. */
9596
9597 static void
set_nonincremental_init_from_string(tree str,struct obstack * braced_init_obstack)9598 set_nonincremental_init_from_string (tree str,
9599 struct obstack * braced_init_obstack)
9600 {
9601 tree value, purpose, type;
9602 HOST_WIDE_INT val[2];
9603 const char *p, *end;
9604 int byte, wchar_bytes, charwidth, bitpos;
9605
9606 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9607
9608 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9609 charwidth = TYPE_PRECISION (char_type_node);
9610 gcc_assert ((size_t) wchar_bytes * charwidth
9611 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9612 type = TREE_TYPE (constructor_type);
9613 p = TREE_STRING_POINTER (str);
9614 end = p + TREE_STRING_LENGTH (str);
9615
9616 for (purpose = bitsize_zero_node;
9617 p < end
9618 && !(constructor_max_index
9619 && tree_int_cst_lt (constructor_max_index, purpose));
9620 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9621 {
9622 if (wchar_bytes == 1)
9623 {
9624 val[0] = (unsigned char) *p++;
9625 val[1] = 0;
9626 }
9627 else
9628 {
9629 val[1] = 0;
9630 val[0] = 0;
9631 for (byte = 0; byte < wchar_bytes; byte++)
9632 {
9633 if (BYTES_BIG_ENDIAN)
9634 bitpos = (wchar_bytes - byte - 1) * charwidth;
9635 else
9636 bitpos = byte * charwidth;
9637 val[bitpos / HOST_BITS_PER_WIDE_INT]
9638 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9639 << (bitpos % HOST_BITS_PER_WIDE_INT);
9640 }
9641 }
9642
9643 if (!TYPE_UNSIGNED (type))
9644 {
9645 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9646 if (bitpos < HOST_BITS_PER_WIDE_INT)
9647 {
9648 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9649 {
9650 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9651 val[1] = -1;
9652 }
9653 }
9654 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9655 {
9656 if (val[0] < 0)
9657 val[1] = -1;
9658 }
9659 else if (val[1] & (HOST_WIDE_INT_1
9660 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9661 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9662 }
9663
9664 value = wide_int_to_tree (type,
9665 wide_int::from_array (val, 2,
9666 HOST_BITS_PER_WIDE_INT * 2));
9667 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9668 braced_init_obstack);
9669 }
9670
9671 constructor_incremental = 0;
9672 }
9673
9674 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9675 not initialized yet. */
9676
9677 static tree
find_init_member(tree field,struct obstack * braced_init_obstack)9678 find_init_member (tree field, struct obstack * braced_init_obstack)
9679 {
9680 struct init_node *p;
9681
9682 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9683 {
9684 if (constructor_incremental
9685 && tree_int_cst_lt (field, constructor_unfilled_index))
9686 set_nonincremental_init (braced_init_obstack);
9687
9688 p = constructor_pending_elts;
9689 while (p)
9690 {
9691 if (tree_int_cst_lt (field, p->purpose))
9692 p = p->left;
9693 else if (tree_int_cst_lt (p->purpose, field))
9694 p = p->right;
9695 else
9696 return p->value;
9697 }
9698 }
9699 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9700 {
9701 tree bitpos = bit_position (field);
9702
9703 if (constructor_incremental
9704 && (!constructor_unfilled_fields
9705 || tree_int_cst_lt (bitpos,
9706 bit_position (constructor_unfilled_fields))))
9707 set_nonincremental_init (braced_init_obstack);
9708
9709 p = constructor_pending_elts;
9710 while (p)
9711 {
9712 if (field == p->purpose)
9713 return p->value;
9714 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9715 p = p->left;
9716 else
9717 p = p->right;
9718 }
9719 }
9720 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9721 {
9722 if (!vec_safe_is_empty (constructor_elements)
9723 && (constructor_elements->last ().index == field))
9724 return constructor_elements->last ().value;
9725 }
9726 return NULL_TREE;
9727 }
9728
9729 /* "Output" the next constructor element.
9730 At top level, really output it to assembler code now.
9731 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9732 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9733 TYPE is the data type that the containing data type wants here.
9734 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9735 If VALUE is a string constant, STRICT_STRING is true if it is
9736 unparenthesized or we should not warn here for it being parenthesized.
9737 For other types of VALUE, STRICT_STRING is not used.
9738
9739 PENDING if true means output pending elements that belong
9740 right after this element. (PENDING is normally true;
9741 it is false while outputting pending elements, to avoid recursion.)
9742
9743 IMPLICIT is true if value comes from pop_init_level (1),
9744 the new initializer has been merged with the existing one
9745 and thus no warnings should be emitted about overriding an
9746 existing initializer. */
9747
9748 static void
output_init_element(location_t loc,tree value,tree origtype,bool strict_string,tree type,tree field,bool pending,bool implicit,struct obstack * braced_init_obstack)9749 output_init_element (location_t loc, tree value, tree origtype,
9750 bool strict_string, tree type, tree field, bool pending,
9751 bool implicit, struct obstack * braced_init_obstack)
9752 {
9753 tree semantic_type = NULL_TREE;
9754 bool maybe_const = true;
9755 bool npc;
9756
9757 if (type == error_mark_node || value == error_mark_node)
9758 {
9759 constructor_erroneous = 1;
9760 return;
9761 }
9762 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9763 && (TREE_CODE (value) == STRING_CST
9764 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9765 && !(TREE_CODE (value) == STRING_CST
9766 && TREE_CODE (type) == ARRAY_TYPE
9767 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9768 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9769 TYPE_MAIN_VARIANT (type)))
9770 value = array_to_pointer_conversion (input_location, value);
9771
9772 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9773 && require_constant_value && pending)
9774 {
9775 /* As an extension, allow initializing objects with static storage
9776 duration with compound literals (which are then treated just as
9777 the brace enclosed list they contain). */
9778 if (flag_isoc99)
9779 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9780 "constant");
9781 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9782 value = DECL_INITIAL (decl);
9783 }
9784
9785 npc = null_pointer_constant_p (value);
9786 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9787 {
9788 semantic_type = TREE_TYPE (value);
9789 value = TREE_OPERAND (value, 0);
9790 }
9791 value = c_fully_fold (value, require_constant_value, &maybe_const);
9792
9793 if (value == error_mark_node)
9794 constructor_erroneous = 1;
9795 else if (!TREE_CONSTANT (value))
9796 constructor_constant = 0;
9797 else if (!initializer_constant_valid_p (value,
9798 TREE_TYPE (value),
9799 AGGREGATE_TYPE_P (constructor_type)
9800 && TYPE_REVERSE_STORAGE_ORDER
9801 (constructor_type))
9802 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9803 && DECL_C_BIT_FIELD (field)
9804 && TREE_CODE (value) != INTEGER_CST))
9805 constructor_simple = 0;
9806 if (!maybe_const)
9807 constructor_nonconst = 1;
9808
9809 /* Digest the initializer and issue any errors about incompatible
9810 types before issuing errors about non-constant initializers. */
9811 tree new_value = value;
9812 if (semantic_type)
9813 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9814 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9815 require_constant_value);
9816 if (new_value == error_mark_node)
9817 {
9818 constructor_erroneous = 1;
9819 return;
9820 }
9821 if (require_constant_value || require_constant_elements)
9822 constant_expression_warning (new_value);
9823
9824 /* Proceed to check the constness of the original initializer. */
9825 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9826 {
9827 if (require_constant_value)
9828 {
9829 error_init (loc, "initializer element is not constant");
9830 value = error_mark_node;
9831 }
9832 else if (require_constant_elements)
9833 pedwarn (loc, OPT_Wpedantic,
9834 "initializer element is not computable at load time");
9835 }
9836 else if (!maybe_const
9837 && (require_constant_value || require_constant_elements))
9838 pedwarn_init (loc, OPT_Wpedantic,
9839 "initializer element is not a constant expression");
9840
9841 /* Issue -Wc++-compat warnings about initializing a bitfield with
9842 enum type. */
9843 if (warn_cxx_compat
9844 && field != NULL_TREE
9845 && TREE_CODE (field) == FIELD_DECL
9846 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9847 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9848 != TYPE_MAIN_VARIANT (type))
9849 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9850 {
9851 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9852 if (checktype != error_mark_node
9853 && (TYPE_MAIN_VARIANT (checktype)
9854 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9855 warning_init (loc, OPT_Wc___compat,
9856 "enum conversion in initialization is invalid in C++");
9857 }
9858
9859 /* If this field is empty and does not have side effects (and is not at
9860 the end of structure), don't do anything other than checking the
9861 initializer. */
9862 if (field
9863 && (TREE_TYPE (field) == error_mark_node
9864 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9865 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9866 && !TREE_SIDE_EFFECTS (new_value)
9867 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9868 || DECL_CHAIN (field)))))
9869 return;
9870
9871 /* Finally, set VALUE to the initializer value digested above. */
9872 value = new_value;
9873
9874 /* If this element doesn't come next in sequence,
9875 put it on constructor_pending_elts. */
9876 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9877 && (!constructor_incremental
9878 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9879 {
9880 if (constructor_incremental
9881 && tree_int_cst_lt (field, constructor_unfilled_index))
9882 set_nonincremental_init (braced_init_obstack);
9883
9884 add_pending_init (loc, field, value, origtype, implicit,
9885 braced_init_obstack);
9886 return;
9887 }
9888 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9889 && (!constructor_incremental
9890 || field != constructor_unfilled_fields))
9891 {
9892 /* We do this for records but not for unions. In a union,
9893 no matter which field is specified, it can be initialized
9894 right away since it starts at the beginning of the union. */
9895 if (constructor_incremental)
9896 {
9897 if (!constructor_unfilled_fields)
9898 set_nonincremental_init (braced_init_obstack);
9899 else
9900 {
9901 tree bitpos, unfillpos;
9902
9903 bitpos = bit_position (field);
9904 unfillpos = bit_position (constructor_unfilled_fields);
9905
9906 if (tree_int_cst_lt (bitpos, unfillpos))
9907 set_nonincremental_init (braced_init_obstack);
9908 }
9909 }
9910
9911 add_pending_init (loc, field, value, origtype, implicit,
9912 braced_init_obstack);
9913 return;
9914 }
9915 else if (TREE_CODE (constructor_type) == UNION_TYPE
9916 && !vec_safe_is_empty (constructor_elements))
9917 {
9918 if (!implicit)
9919 {
9920 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9921 warning_init (loc, OPT_Woverride_init_side_effects,
9922 "initialized field with side-effects overwritten");
9923 else if (warn_override_init)
9924 warning_init (loc, OPT_Woverride_init,
9925 "initialized field overwritten");
9926 }
9927
9928 /* We can have just one union field set. */
9929 constructor_elements = NULL;
9930 }
9931
9932 /* Otherwise, output this element either to
9933 constructor_elements or to the assembler file. */
9934
9935 constructor_elt celt = {field, value};
9936 vec_safe_push (constructor_elements, celt);
9937
9938 /* Advance the variable that indicates sequential elements output. */
9939 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9940 constructor_unfilled_index
9941 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9942 bitsize_one_node);
9943 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9944 {
9945 constructor_unfilled_fields
9946 = DECL_CHAIN (constructor_unfilled_fields);
9947
9948 /* Skip any nameless bit fields. */
9949 while (constructor_unfilled_fields != NULL_TREE
9950 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9951 constructor_unfilled_fields =
9952 DECL_CHAIN (constructor_unfilled_fields);
9953 }
9954 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9955 constructor_unfilled_fields = NULL_TREE;
9956
9957 /* Now output any pending elements which have become next. */
9958 if (pending)
9959 output_pending_init_elements (0, braced_init_obstack);
9960 }
9961
9962 /* For two FIELD_DECLs in the same chain, return -1 if field1
9963 comes before field2, 1 if field1 comes after field2 and
9964 0 if field1 == field2. */
9965
9966 static int
init_field_decl_cmp(tree field1,tree field2)9967 init_field_decl_cmp (tree field1, tree field2)
9968 {
9969 if (field1 == field2)
9970 return 0;
9971
9972 tree bitpos1 = bit_position (field1);
9973 tree bitpos2 = bit_position (field2);
9974 if (tree_int_cst_equal (bitpos1, bitpos2))
9975 {
9976 /* If one of the fields has non-zero bitsize, then that
9977 field must be the last one in a sequence of zero
9978 sized fields, fields after it will have bigger
9979 bit_position. */
9980 if (TREE_TYPE (field1) != error_mark_node
9981 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9982 && integer_nonzerop (TREE_TYPE (field1)))
9983 return 1;
9984 if (TREE_TYPE (field2) != error_mark_node
9985 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9986 && integer_nonzerop (TREE_TYPE (field2)))
9987 return -1;
9988 /* Otherwise, fallback to DECL_CHAIN walk to find out
9989 which field comes earlier. Walk chains of both
9990 fields, so that if field1 and field2 are close to each
9991 other in either order, it is found soon even for large
9992 sequences of zero sized fields. */
9993 tree f1 = field1, f2 = field2;
9994 while (1)
9995 {
9996 f1 = DECL_CHAIN (f1);
9997 f2 = DECL_CHAIN (f2);
9998 if (f1 == NULL_TREE)
9999 {
10000 gcc_assert (f2);
10001 return 1;
10002 }
10003 if (f2 == NULL_TREE)
10004 return -1;
10005 if (f1 == field2)
10006 return -1;
10007 if (f2 == field1)
10008 return 1;
10009 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10010 return 1;
10011 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10012 return -1;
10013 }
10014 }
10015 else if (tree_int_cst_lt (bitpos1, bitpos2))
10016 return -1;
10017 else
10018 return 1;
10019 }
10020
10021 /* Output any pending elements which have become next.
10022 As we output elements, constructor_unfilled_{fields,index}
10023 advances, which may cause other elements to become next;
10024 if so, they too are output.
10025
10026 If ALL is 0, we return when there are
10027 no more pending elements to output now.
10028
10029 If ALL is 1, we output space as necessary so that
10030 we can output all the pending elements. */
10031 static void
output_pending_init_elements(int all,struct obstack * braced_init_obstack)10032 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10033 {
10034 struct init_node *elt = constructor_pending_elts;
10035 tree next;
10036
10037 retry:
10038
10039 /* Look through the whole pending tree.
10040 If we find an element that should be output now,
10041 output it. Otherwise, set NEXT to the element
10042 that comes first among those still pending. */
10043
10044 next = NULL_TREE;
10045 while (elt)
10046 {
10047 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10048 {
10049 if (tree_int_cst_equal (elt->purpose,
10050 constructor_unfilled_index))
10051 output_init_element (input_location, elt->value, elt->origtype,
10052 true, TREE_TYPE (constructor_type),
10053 constructor_unfilled_index, false, false,
10054 braced_init_obstack);
10055 else if (tree_int_cst_lt (constructor_unfilled_index,
10056 elt->purpose))
10057 {
10058 /* Advance to the next smaller node. */
10059 if (elt->left)
10060 elt = elt->left;
10061 else
10062 {
10063 /* We have reached the smallest node bigger than the
10064 current unfilled index. Fill the space first. */
10065 next = elt->purpose;
10066 break;
10067 }
10068 }
10069 else
10070 {
10071 /* Advance to the next bigger node. */
10072 if (elt->right)
10073 elt = elt->right;
10074 else
10075 {
10076 /* We have reached the biggest node in a subtree. Find
10077 the parent of it, which is the next bigger node. */
10078 while (elt->parent && elt->parent->right == elt)
10079 elt = elt->parent;
10080 elt = elt->parent;
10081 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10082 elt->purpose))
10083 {
10084 next = elt->purpose;
10085 break;
10086 }
10087 }
10088 }
10089 }
10090 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10091 {
10092 /* If the current record is complete we are done. */
10093 if (constructor_unfilled_fields == NULL_TREE)
10094 break;
10095
10096 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10097 elt->purpose);
10098 if (cmp == 0)
10099 output_init_element (input_location, elt->value, elt->origtype,
10100 true, TREE_TYPE (elt->purpose),
10101 elt->purpose, false, false,
10102 braced_init_obstack);
10103 else if (cmp < 0)
10104 {
10105 /* Advance to the next smaller node. */
10106 if (elt->left)
10107 elt = elt->left;
10108 else
10109 {
10110 /* We have reached the smallest node bigger than the
10111 current unfilled field. Fill the space first. */
10112 next = elt->purpose;
10113 break;
10114 }
10115 }
10116 else
10117 {
10118 /* Advance to the next bigger node. */
10119 if (elt->right)
10120 elt = elt->right;
10121 else
10122 {
10123 /* We have reached the biggest node in a subtree. Find
10124 the parent of it, which is the next bigger node. */
10125 while (elt->parent && elt->parent->right == elt)
10126 elt = elt->parent;
10127 elt = elt->parent;
10128 if (elt
10129 && init_field_decl_cmp (constructor_unfilled_fields,
10130 elt->purpose) < 0)
10131 {
10132 next = elt->purpose;
10133 break;
10134 }
10135 }
10136 }
10137 }
10138 }
10139
10140 /* Ordinarily return, but not if we want to output all
10141 and there are elements left. */
10142 if (!(all && next != NULL_TREE))
10143 return;
10144
10145 /* If it's not incremental, just skip over the gap, so that after
10146 jumping to retry we will output the next successive element. */
10147 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10148 constructor_unfilled_fields = next;
10149 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10150 constructor_unfilled_index = next;
10151
10152 /* ELT now points to the node in the pending tree with the next
10153 initializer to output. */
10154 goto retry;
10155 }
10156
10157 /* Expression VALUE coincides with the start of type TYPE in a braced
10158 initializer. Return true if we should treat VALUE as initializing
10159 the first element of TYPE, false if we should treat it as initializing
10160 TYPE as a whole.
10161
10162 If the initializer is clearly invalid, the question becomes:
10163 which choice gives the best error message? */
10164
10165 static bool
initialize_elementwise_p(tree type,tree value)10166 initialize_elementwise_p (tree type, tree value)
10167 {
10168 if (type == error_mark_node || value == error_mark_node)
10169 return false;
10170
10171 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10172
10173 tree value_type = TREE_TYPE (value);
10174 if (value_type == error_mark_node)
10175 return false;
10176
10177 /* GNU vectors can be initialized elementwise. However, treat any
10178 kind of vector value as initializing the vector type as a whole,
10179 regardless of whether the value is a GNU vector. Such initializers
10180 are valid if and only if they would have been valid in a non-braced
10181 initializer like:
10182
10183 TYPE foo = VALUE;
10184
10185 so recursing into the vector type would be at best confusing or at
10186 worst wrong. For example, when -flax-vector-conversions is in effect,
10187 it's possible to initialize a V8HI from a V4SI, even though the vectors
10188 have different element types and different numbers of elements. */
10189 if (gnu_vector_type_p (type))
10190 return !VECTOR_TYPE_P (value_type);
10191
10192 if (AGGREGATE_TYPE_P (type))
10193 return type != TYPE_MAIN_VARIANT (value_type);
10194
10195 return false;
10196 }
10197
10198 /* Add one non-braced element to the current constructor level.
10199 This adjusts the current position within the constructor's type.
10200 This may also start or terminate implicit levels
10201 to handle a partly-braced initializer.
10202
10203 Once this has found the correct level for the new element,
10204 it calls output_init_element.
10205
10206 IMPLICIT is true if value comes from pop_init_level (1),
10207 the new initializer has been merged with the existing one
10208 and thus no warnings should be emitted about overriding an
10209 existing initializer. */
10210
10211 void
process_init_element(location_t loc,struct c_expr value,bool implicit,struct obstack * braced_init_obstack)10212 process_init_element (location_t loc, struct c_expr value, bool implicit,
10213 struct obstack * braced_init_obstack)
10214 {
10215 tree orig_value = value.value;
10216 int string_flag
10217 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10218 bool strict_string = value.original_code == STRING_CST;
10219 bool was_designated = designator_depth != 0;
10220
10221 designator_depth = 0;
10222 designator_erroneous = 0;
10223
10224 if (!implicit && value.value && !integer_zerop (value.value))
10225 constructor_zeroinit = 0;
10226
10227 /* Handle superfluous braces around string cst as in
10228 char x[] = {"foo"}; */
10229 if (string_flag
10230 && constructor_type
10231 && !was_designated
10232 && TREE_CODE (constructor_type) == ARRAY_TYPE
10233 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10234 && integer_zerop (constructor_unfilled_index))
10235 {
10236 if (constructor_stack->replacement_value.value)
10237 error_init (loc, "excess elements in %<char%> array initializer");
10238 constructor_stack->replacement_value = value;
10239 return;
10240 }
10241
10242 if (constructor_stack->replacement_value.value != NULL_TREE)
10243 {
10244 error_init (loc, "excess elements in struct initializer");
10245 return;
10246 }
10247
10248 /* Ignore elements of a brace group if it is entirely superfluous
10249 and has already been diagnosed, or if the type is erroneous. */
10250 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10251 return;
10252
10253 /* Ignore elements of an initializer for a variable-size type.
10254 Those are diagnosed in digest_init. */
10255 if (COMPLETE_TYPE_P (constructor_type)
10256 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10257 return;
10258
10259 if (!implicit && warn_designated_init && !was_designated
10260 && TREE_CODE (constructor_type) == RECORD_TYPE
10261 && lookup_attribute ("designated_init",
10262 TYPE_ATTRIBUTES (constructor_type)))
10263 warning_init (loc,
10264 OPT_Wdesignated_init,
10265 "positional initialization of field "
10266 "in %<struct%> declared with %<designated_init%> attribute");
10267
10268 /* If we've exhausted any levels that didn't have braces,
10269 pop them now. */
10270 while (constructor_stack->implicit)
10271 {
10272 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10273 && constructor_fields == NULL_TREE)
10274 process_init_element (loc,
10275 pop_init_level (loc, 1, braced_init_obstack,
10276 last_init_list_comma),
10277 true, braced_init_obstack);
10278 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10279 || gnu_vector_type_p (constructor_type))
10280 && constructor_max_index
10281 && tree_int_cst_lt (constructor_max_index,
10282 constructor_index))
10283 process_init_element (loc,
10284 pop_init_level (loc, 1, braced_init_obstack,
10285 last_init_list_comma),
10286 true, braced_init_obstack);
10287 else
10288 break;
10289 }
10290
10291 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10292 if (constructor_range_stack)
10293 {
10294 /* If value is a compound literal and we'll be just using its
10295 content, don't put it into a SAVE_EXPR. */
10296 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10297 || !require_constant_value)
10298 {
10299 tree semantic_type = NULL_TREE;
10300 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10301 {
10302 semantic_type = TREE_TYPE (value.value);
10303 value.value = TREE_OPERAND (value.value, 0);
10304 }
10305 value.value = save_expr (value.value);
10306 if (semantic_type)
10307 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10308 value.value);
10309 }
10310 }
10311
10312 while (1)
10313 {
10314 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10315 {
10316 tree fieldtype;
10317 enum tree_code fieldcode;
10318
10319 if (constructor_fields == NULL_TREE)
10320 {
10321 pedwarn_init (loc, 0, "excess elements in struct initializer");
10322 break;
10323 }
10324
10325 fieldtype = TREE_TYPE (constructor_fields);
10326 if (fieldtype != error_mark_node)
10327 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10328 fieldcode = TREE_CODE (fieldtype);
10329
10330 /* Error for non-static initialization of a flexible array member. */
10331 if (fieldcode == ARRAY_TYPE
10332 && !require_constant_value
10333 && TYPE_SIZE (fieldtype) == NULL_TREE
10334 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10335 {
10336 error_init (loc, "non-static initialization of a flexible "
10337 "array member");
10338 break;
10339 }
10340
10341 /* Error for initialization of a flexible array member with
10342 a string constant if the structure is in an array. E.g.:
10343 struct S { int x; char y[]; };
10344 struct S s[] = { { 1, "foo" } };
10345 is invalid. */
10346 if (string_flag
10347 && fieldcode == ARRAY_TYPE
10348 && constructor_depth > 1
10349 && TYPE_SIZE (fieldtype) == NULL_TREE
10350 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10351 {
10352 bool in_array_p = false;
10353 for (struct constructor_stack *p = constructor_stack;
10354 p && p->type; p = p->next)
10355 if (TREE_CODE (p->type) == ARRAY_TYPE)
10356 {
10357 in_array_p = true;
10358 break;
10359 }
10360 if (in_array_p)
10361 {
10362 error_init (loc, "initialization of flexible array "
10363 "member in a nested context");
10364 break;
10365 }
10366 }
10367
10368 /* Accept a string constant to initialize a subarray. */
10369 if (value.value != NULL_TREE
10370 && fieldcode == ARRAY_TYPE
10371 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10372 && string_flag)
10373 value.value = orig_value;
10374 /* Otherwise, if we have come to a subaggregate,
10375 and we don't have an element of its type, push into it. */
10376 else if (value.value != NULL_TREE
10377 && initialize_elementwise_p (fieldtype, value.value))
10378 {
10379 push_init_level (loc, 1, braced_init_obstack);
10380 continue;
10381 }
10382
10383 if (value.value)
10384 {
10385 push_member_name (constructor_fields);
10386 output_init_element (loc, value.value, value.original_type,
10387 strict_string, fieldtype,
10388 constructor_fields, true, implicit,
10389 braced_init_obstack);
10390 RESTORE_SPELLING_DEPTH (constructor_depth);
10391 }
10392 else
10393 /* Do the bookkeeping for an element that was
10394 directly output as a constructor. */
10395 {
10396 /* For a record, keep track of end position of last field. */
10397 if (DECL_SIZE (constructor_fields))
10398 constructor_bit_index
10399 = size_binop_loc (input_location, PLUS_EXPR,
10400 bit_position (constructor_fields),
10401 DECL_SIZE (constructor_fields));
10402
10403 /* If the current field was the first one not yet written out,
10404 it isn't now, so update. */
10405 if (constructor_unfilled_fields == constructor_fields)
10406 {
10407 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10408 /* Skip any nameless bit fields. */
10409 while (constructor_unfilled_fields != 0
10410 && (DECL_UNNAMED_BIT_FIELD
10411 (constructor_unfilled_fields)))
10412 constructor_unfilled_fields =
10413 DECL_CHAIN (constructor_unfilled_fields);
10414 }
10415 }
10416
10417 constructor_fields = DECL_CHAIN (constructor_fields);
10418 /* Skip any nameless bit fields at the beginning. */
10419 while (constructor_fields != NULL_TREE
10420 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10421 constructor_fields = DECL_CHAIN (constructor_fields);
10422 }
10423 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10424 {
10425 tree fieldtype;
10426 enum tree_code fieldcode;
10427
10428 if (constructor_fields == NULL_TREE)
10429 {
10430 pedwarn_init (loc, 0,
10431 "excess elements in union initializer");
10432 break;
10433 }
10434
10435 fieldtype = TREE_TYPE (constructor_fields);
10436 if (fieldtype != error_mark_node)
10437 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10438 fieldcode = TREE_CODE (fieldtype);
10439
10440 /* Warn that traditional C rejects initialization of unions.
10441 We skip the warning if the value is zero. This is done
10442 under the assumption that the zero initializer in user
10443 code appears conditioned on e.g. __STDC__ to avoid
10444 "missing initializer" warnings and relies on default
10445 initialization to zero in the traditional C case.
10446 We also skip the warning if the initializer is designated,
10447 again on the assumption that this must be conditional on
10448 __STDC__ anyway (and we've already complained about the
10449 member-designator already). */
10450 if (!in_system_header_at (input_location) && !constructor_designated
10451 && !(value.value && (integer_zerop (value.value)
10452 || real_zerop (value.value))))
10453 warning (OPT_Wtraditional, "traditional C rejects initialization "
10454 "of unions");
10455
10456 /* Accept a string constant to initialize a subarray. */
10457 if (value.value != NULL_TREE
10458 && fieldcode == ARRAY_TYPE
10459 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10460 && string_flag)
10461 value.value = orig_value;
10462 /* Otherwise, if we have come to a subaggregate,
10463 and we don't have an element of its type, push into it. */
10464 else if (value.value != NULL_TREE
10465 && initialize_elementwise_p (fieldtype, value.value))
10466 {
10467 push_init_level (loc, 1, braced_init_obstack);
10468 continue;
10469 }
10470
10471 if (value.value)
10472 {
10473 push_member_name (constructor_fields);
10474 output_init_element (loc, value.value, value.original_type,
10475 strict_string, fieldtype,
10476 constructor_fields, true, implicit,
10477 braced_init_obstack);
10478 RESTORE_SPELLING_DEPTH (constructor_depth);
10479 }
10480 else
10481 /* Do the bookkeeping for an element that was
10482 directly output as a constructor. */
10483 {
10484 constructor_bit_index = DECL_SIZE (constructor_fields);
10485 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10486 }
10487
10488 constructor_fields = NULL_TREE;
10489 }
10490 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10491 {
10492 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10493 enum tree_code eltcode = TREE_CODE (elttype);
10494
10495 /* Accept a string constant to initialize a subarray. */
10496 if (value.value != NULL_TREE
10497 && eltcode == ARRAY_TYPE
10498 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10499 && string_flag)
10500 value.value = orig_value;
10501 /* Otherwise, if we have come to a subaggregate,
10502 and we don't have an element of its type, push into it. */
10503 else if (value.value != NULL_TREE
10504 && initialize_elementwise_p (elttype, value.value))
10505 {
10506 push_init_level (loc, 1, braced_init_obstack);
10507 continue;
10508 }
10509
10510 if (constructor_max_index != NULL_TREE
10511 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10512 || integer_all_onesp (constructor_max_index)))
10513 {
10514 pedwarn_init (loc, 0,
10515 "excess elements in array initializer");
10516 break;
10517 }
10518
10519 /* Now output the actual element. */
10520 if (value.value)
10521 {
10522 push_array_bounds (tree_to_uhwi (constructor_index));
10523 output_init_element (loc, value.value, value.original_type,
10524 strict_string, elttype,
10525 constructor_index, true, implicit,
10526 braced_init_obstack);
10527 RESTORE_SPELLING_DEPTH (constructor_depth);
10528 }
10529
10530 constructor_index
10531 = size_binop_loc (input_location, PLUS_EXPR,
10532 constructor_index, bitsize_one_node);
10533
10534 if (!value.value)
10535 /* If we are doing the bookkeeping for an element that was
10536 directly output as a constructor, we must update
10537 constructor_unfilled_index. */
10538 constructor_unfilled_index = constructor_index;
10539 }
10540 else if (gnu_vector_type_p (constructor_type))
10541 {
10542 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10543
10544 /* Do a basic check of initializer size. Note that vectors
10545 always have a fixed size derived from their type. */
10546 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10547 {
10548 pedwarn_init (loc, 0,
10549 "excess elements in vector initializer");
10550 break;
10551 }
10552
10553 /* Now output the actual element. */
10554 if (value.value)
10555 {
10556 if (TREE_CODE (value.value) == VECTOR_CST)
10557 elttype = TYPE_MAIN_VARIANT (constructor_type);
10558 output_init_element (loc, value.value, value.original_type,
10559 strict_string, elttype,
10560 constructor_index, true, implicit,
10561 braced_init_obstack);
10562 }
10563
10564 constructor_index
10565 = size_binop_loc (input_location,
10566 PLUS_EXPR, constructor_index, bitsize_one_node);
10567
10568 if (!value.value)
10569 /* If we are doing the bookkeeping for an element that was
10570 directly output as a constructor, we must update
10571 constructor_unfilled_index. */
10572 constructor_unfilled_index = constructor_index;
10573 }
10574
10575 /* Handle the sole element allowed in a braced initializer
10576 for a scalar variable. */
10577 else if (constructor_type != error_mark_node
10578 && constructor_fields == NULL_TREE)
10579 {
10580 pedwarn_init (loc, 0,
10581 "excess elements in scalar initializer");
10582 break;
10583 }
10584 else
10585 {
10586 if (value.value)
10587 output_init_element (loc, value.value, value.original_type,
10588 strict_string, constructor_type,
10589 NULL_TREE, true, implicit,
10590 braced_init_obstack);
10591 constructor_fields = NULL_TREE;
10592 }
10593
10594 /* Handle range initializers either at this level or anywhere higher
10595 in the designator stack. */
10596 if (constructor_range_stack)
10597 {
10598 struct constructor_range_stack *p, *range_stack;
10599 int finish = 0;
10600
10601 range_stack = constructor_range_stack;
10602 constructor_range_stack = 0;
10603 while (constructor_stack != range_stack->stack)
10604 {
10605 gcc_assert (constructor_stack->implicit);
10606 process_init_element (loc,
10607 pop_init_level (loc, 1,
10608 braced_init_obstack,
10609 last_init_list_comma),
10610 true, braced_init_obstack);
10611 }
10612 for (p = range_stack;
10613 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10614 p = p->prev)
10615 {
10616 gcc_assert (constructor_stack->implicit);
10617 process_init_element (loc,
10618 pop_init_level (loc, 1,
10619 braced_init_obstack,
10620 last_init_list_comma),
10621 true, braced_init_obstack);
10622 }
10623
10624 p->index = size_binop_loc (input_location,
10625 PLUS_EXPR, p->index, bitsize_one_node);
10626 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10627 finish = 1;
10628
10629 while (1)
10630 {
10631 constructor_index = p->index;
10632 constructor_fields = p->fields;
10633 if (finish && p->range_end && p->index == p->range_start)
10634 {
10635 finish = 0;
10636 p->prev = 0;
10637 }
10638 p = p->next;
10639 if (!p)
10640 break;
10641 finish_implicit_inits (loc, braced_init_obstack);
10642 push_init_level (loc, 2, braced_init_obstack);
10643 p->stack = constructor_stack;
10644 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10645 p->index = p->range_start;
10646 }
10647
10648 if (!finish)
10649 constructor_range_stack = range_stack;
10650 continue;
10651 }
10652
10653 break;
10654 }
10655
10656 constructor_range_stack = 0;
10657 }
10658
10659 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10660 (guaranteed to be 'volatile' or null) and ARGS (represented using
10661 an ASM_EXPR node). */
10662 tree
build_asm_stmt(bool is_volatile,tree args)10663 build_asm_stmt (bool is_volatile, tree args)
10664 {
10665 if (is_volatile)
10666 ASM_VOLATILE_P (args) = 1;
10667 return add_stmt (args);
10668 }
10669
10670 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10671 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10672 SIMPLE indicates whether there was anything at all after the
10673 string in the asm expression -- asm("blah") and asm("blah" : )
10674 are subtly different. We use a ASM_EXPR node to represent this.
10675 LOC is the location of the asm, and IS_INLINE says whether this
10676 is asm inline. */
10677 tree
build_asm_expr(location_t loc,tree string,tree outputs,tree inputs,tree clobbers,tree labels,bool simple,bool is_inline)10678 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10679 tree clobbers, tree labels, bool simple, bool is_inline)
10680 {
10681 tree tail;
10682 tree args;
10683 int i;
10684 const char *constraint;
10685 const char **oconstraints;
10686 bool allows_mem, allows_reg, is_inout;
10687 int ninputs, noutputs;
10688
10689 ninputs = list_length (inputs);
10690 noutputs = list_length (outputs);
10691 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10692
10693 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10694
10695 /* Remove output conversions that change the type but not the mode. */
10696 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10697 {
10698 tree output = TREE_VALUE (tail);
10699
10700 output = c_fully_fold (output, false, NULL, true);
10701
10702 /* ??? Really, this should not be here. Users should be using a
10703 proper lvalue, dammit. But there's a long history of using casts
10704 in the output operands. In cases like longlong.h, this becomes a
10705 primitive form of typechecking -- if the cast can be removed, then
10706 the output operand had a type of the proper width; otherwise we'll
10707 get an error. Gross, but ... */
10708 STRIP_NOPS (output);
10709
10710 if (!lvalue_or_else (loc, output, lv_asm))
10711 output = error_mark_node;
10712
10713 if (output != error_mark_node
10714 && (TREE_READONLY (output)
10715 || TYPE_READONLY (TREE_TYPE (output))
10716 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10717 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10718 readonly_error (loc, output, lv_asm);
10719
10720 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10721 oconstraints[i] = constraint;
10722
10723 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10724 &allows_mem, &allows_reg, &is_inout))
10725 {
10726 /* If the operand is going to end up in memory,
10727 mark it addressable. */
10728 if (!allows_reg && !c_mark_addressable (output))
10729 output = error_mark_node;
10730 if (!(!allows_reg && allows_mem)
10731 && output != error_mark_node
10732 && VOID_TYPE_P (TREE_TYPE (output)))
10733 {
10734 error_at (loc, "invalid use of void expression");
10735 output = error_mark_node;
10736 }
10737 }
10738 else
10739 output = error_mark_node;
10740
10741 TREE_VALUE (tail) = output;
10742 }
10743
10744 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10745 {
10746 tree input;
10747
10748 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10749 input = TREE_VALUE (tail);
10750
10751 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10752 oconstraints, &allows_mem, &allows_reg))
10753 {
10754 /* If the operand is going to end up in memory,
10755 mark it addressable. */
10756 if (!allows_reg && allows_mem)
10757 {
10758 input = c_fully_fold (input, false, NULL, true);
10759
10760 /* Strip the nops as we allow this case. FIXME, this really
10761 should be rejected or made deprecated. */
10762 STRIP_NOPS (input);
10763 if (!c_mark_addressable (input))
10764 input = error_mark_node;
10765 }
10766 else
10767 {
10768 struct c_expr expr;
10769 memset (&expr, 0, sizeof (expr));
10770 expr.value = input;
10771 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10772 input = c_fully_fold (expr.value, false, NULL);
10773
10774 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10775 {
10776 error_at (loc, "invalid use of void expression");
10777 input = error_mark_node;
10778 }
10779 }
10780 }
10781 else
10782 input = error_mark_node;
10783
10784 TREE_VALUE (tail) = input;
10785 }
10786
10787 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10788
10789 /* asm statements without outputs, including simple ones, are treated
10790 as volatile. */
10791 ASM_INPUT_P (args) = simple;
10792 ASM_VOLATILE_P (args) = (noutputs == 0);
10793 ASM_INLINE_P (args) = is_inline;
10794
10795 return args;
10796 }
10797
10798 /* Generate a goto statement to LABEL. LOC is the location of the
10799 GOTO. */
10800
10801 tree
c_finish_goto_label(location_t loc,tree label)10802 c_finish_goto_label (location_t loc, tree label)
10803 {
10804 tree decl = lookup_label_for_goto (loc, label);
10805 if (!decl)
10806 return NULL_TREE;
10807 TREE_USED (decl) = 1;
10808 {
10809 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10810 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10811 SET_EXPR_LOCATION (t, loc);
10812 return add_stmt (t);
10813 }
10814 }
10815
10816 /* Generate a computed goto statement to EXPR. LOC is the location of
10817 the GOTO. */
10818
10819 tree
c_finish_goto_ptr(location_t loc,c_expr val)10820 c_finish_goto_ptr (location_t loc, c_expr val)
10821 {
10822 tree expr = val.value;
10823 tree t;
10824 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10825 if (expr != error_mark_node
10826 && !POINTER_TYPE_P (TREE_TYPE (expr))
10827 && !null_pointer_constant_p (expr))
10828 {
10829 error_at (val.get_location (),
10830 "computed goto must be pointer type");
10831 expr = build_zero_cst (ptr_type_node);
10832 }
10833 expr = c_fully_fold (expr, false, NULL);
10834 expr = convert (ptr_type_node, expr);
10835 t = build1 (GOTO_EXPR, void_type_node, expr);
10836 SET_EXPR_LOCATION (t, loc);
10837 return add_stmt (t);
10838 }
10839
10840 /* Generate a C `return' statement. RETVAL is the expression for what
10841 to return, or a null pointer for `return;' with no value. LOC is
10842 the location of the return statement, or the location of the expression,
10843 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10844 is the original type of RETVAL. */
10845
10846 tree
c_finish_return(location_t loc,tree retval,tree origtype)10847 c_finish_return (location_t loc, tree retval, tree origtype)
10848 {
10849 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10850 bool no_warning = false;
10851 bool npc = false;
10852
10853 /* Use the expansion point to handle cases such as returning NULL
10854 in a function returning void. */
10855 location_t xloc = expansion_point_location_if_in_system_header (loc);
10856
10857 if (TREE_THIS_VOLATILE (current_function_decl))
10858 warning_at (xloc, 0,
10859 "function declared %<noreturn%> has a %<return%> statement");
10860
10861 if (retval)
10862 {
10863 tree semantic_type = NULL_TREE;
10864 npc = null_pointer_constant_p (retval);
10865 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10866 {
10867 semantic_type = TREE_TYPE (retval);
10868 retval = TREE_OPERAND (retval, 0);
10869 }
10870 retval = c_fully_fold (retval, false, NULL);
10871 if (semantic_type
10872 && valtype != NULL_TREE
10873 && TREE_CODE (valtype) != VOID_TYPE)
10874 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10875 }
10876
10877 if (!retval)
10878 {
10879 current_function_returns_null = 1;
10880 if ((warn_return_type >= 0 || flag_isoc99)
10881 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10882 {
10883 bool warned_here;
10884 if (flag_isoc99)
10885 warned_here = pedwarn
10886 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10887 "%<return%> with no value, in function returning non-void");
10888 else
10889 warned_here = warning_at
10890 (loc, OPT_Wreturn_type,
10891 "%<return%> with no value, in function returning non-void");
10892 no_warning = true;
10893 if (warned_here)
10894 inform (DECL_SOURCE_LOCATION (current_function_decl),
10895 "declared here");
10896 }
10897 }
10898 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10899 {
10900 current_function_returns_null = 1;
10901 bool warned_here;
10902 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10903 warned_here = pedwarn
10904 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10905 "%<return%> with a value, in function returning void");
10906 else
10907 warned_here = pedwarn
10908 (xloc, OPT_Wpedantic, "ISO C forbids "
10909 "%<return%> with expression, in function returning void");
10910 if (warned_here)
10911 inform (DECL_SOURCE_LOCATION (current_function_decl),
10912 "declared here");
10913 }
10914 else
10915 {
10916 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10917 retval, origtype, ic_return,
10918 npc, NULL_TREE, NULL_TREE, 0);
10919 tree res = DECL_RESULT (current_function_decl);
10920 tree inner;
10921 bool save;
10922
10923 current_function_returns_value = 1;
10924 if (t == error_mark_node)
10925 return NULL_TREE;
10926
10927 save = in_late_binary_op;
10928 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10929 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10930 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10931 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10932 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10933 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10934 in_late_binary_op = true;
10935 inner = t = convert (TREE_TYPE (res), t);
10936 in_late_binary_op = save;
10937
10938 /* Strip any conversions, additions, and subtractions, and see if
10939 we are returning the address of a local variable. Warn if so. */
10940 while (1)
10941 {
10942 switch (TREE_CODE (inner))
10943 {
10944 CASE_CONVERT:
10945 case NON_LVALUE_EXPR:
10946 case PLUS_EXPR:
10947 case POINTER_PLUS_EXPR:
10948 inner = TREE_OPERAND (inner, 0);
10949 continue;
10950
10951 case MINUS_EXPR:
10952 /* If the second operand of the MINUS_EXPR has a pointer
10953 type (or is converted from it), this may be valid, so
10954 don't give a warning. */
10955 {
10956 tree op1 = TREE_OPERAND (inner, 1);
10957
10958 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10959 && (CONVERT_EXPR_P (op1)
10960 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10961 op1 = TREE_OPERAND (op1, 0);
10962
10963 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10964 break;
10965
10966 inner = TREE_OPERAND (inner, 0);
10967 continue;
10968 }
10969
10970 case ADDR_EXPR:
10971 inner = TREE_OPERAND (inner, 0);
10972
10973 while (REFERENCE_CLASS_P (inner)
10974 && !INDIRECT_REF_P (inner))
10975 inner = TREE_OPERAND (inner, 0);
10976
10977 if (DECL_P (inner)
10978 && !DECL_EXTERNAL (inner)
10979 && !TREE_STATIC (inner)
10980 && DECL_CONTEXT (inner) == current_function_decl
10981 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
10982 {
10983 if (TREE_CODE (inner) == LABEL_DECL)
10984 warning_at (loc, OPT_Wreturn_local_addr,
10985 "function returns address of label");
10986 else
10987 {
10988 warning_at (loc, OPT_Wreturn_local_addr,
10989 "function returns address of local variable");
10990 tree zero = build_zero_cst (TREE_TYPE (res));
10991 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10992 }
10993 }
10994 break;
10995
10996 default:
10997 break;
10998 }
10999
11000 break;
11001 }
11002
11003 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11004 SET_EXPR_LOCATION (retval, loc);
11005
11006 if (warn_sequence_point)
11007 verify_sequence_points (retval);
11008 }
11009
11010 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11011 if (no_warning)
11012 suppress_warning (ret_stmt, OPT_Wreturn_type);
11013 return add_stmt (ret_stmt);
11014 }
11015
11016 struct c_switch {
11017 /* The SWITCH_STMT being built. */
11018 tree switch_stmt;
11019
11020 /* The original type of the testing expression, i.e. before the
11021 default conversion is applied. */
11022 tree orig_type;
11023
11024 /* A splay-tree mapping the low element of a case range to the high
11025 element, or NULL_TREE if there is no high element. Used to
11026 determine whether or not a new case label duplicates an old case
11027 label. We need a tree, rather than simply a hash table, because
11028 of the GNU case range extension. */
11029 splay_tree cases;
11030
11031 /* The bindings at the point of the switch. This is used for
11032 warnings crossing decls when branching to a case label. */
11033 struct c_spot_bindings *bindings;
11034
11035 /* Whether the switch includes any break statements. */
11036 bool break_stmt_seen_p;
11037
11038 /* The next node on the stack. */
11039 struct c_switch *next;
11040
11041 /* Remember whether the controlling expression had boolean type
11042 before integer promotions for the sake of -Wswitch-bool. */
11043 bool bool_cond_p;
11044 };
11045
11046 /* A stack of the currently active switch statements. The innermost
11047 switch statement is on the top of the stack. There is no need to
11048 mark the stack for garbage collection because it is only active
11049 during the processing of the body of a function, and we never
11050 collect at that point. */
11051
11052 struct c_switch *c_switch_stack;
11053
11054 /* Start a C switch statement, testing expression EXP. Return the new
11055 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11056 SWITCH_COND_LOC is the location of the switch's condition.
11057 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11058
11059 tree
c_start_switch(location_t switch_loc,location_t switch_cond_loc,tree exp,bool explicit_cast_p)11060 c_start_switch (location_t switch_loc,
11061 location_t switch_cond_loc,
11062 tree exp, bool explicit_cast_p)
11063 {
11064 tree orig_type = error_mark_node;
11065 bool bool_cond_p = false;
11066 struct c_switch *cs;
11067
11068 if (exp != error_mark_node)
11069 {
11070 orig_type = TREE_TYPE (exp);
11071
11072 if (!INTEGRAL_TYPE_P (orig_type))
11073 {
11074 if (orig_type != error_mark_node)
11075 {
11076 error_at (switch_cond_loc, "switch quantity not an integer");
11077 orig_type = error_mark_node;
11078 }
11079 exp = integer_zero_node;
11080 }
11081 else
11082 {
11083 tree type = TYPE_MAIN_VARIANT (orig_type);
11084 tree e = exp;
11085
11086 /* Warn if the condition has boolean value. */
11087 while (TREE_CODE (e) == COMPOUND_EXPR)
11088 e = TREE_OPERAND (e, 1);
11089
11090 if ((TREE_CODE (type) == BOOLEAN_TYPE
11091 || truth_value_p (TREE_CODE (e)))
11092 /* Explicit cast to int suppresses this warning. */
11093 && !(TREE_CODE (type) == INTEGER_TYPE
11094 && explicit_cast_p))
11095 bool_cond_p = true;
11096
11097 if (!in_system_header_at (input_location)
11098 && (type == long_integer_type_node
11099 || type == long_unsigned_type_node))
11100 warning_at (switch_cond_loc,
11101 OPT_Wtraditional, "%<long%> switch expression not "
11102 "converted to %<int%> in ISO C");
11103
11104 exp = c_fully_fold (exp, false, NULL);
11105 exp = default_conversion (exp);
11106
11107 if (warn_sequence_point)
11108 verify_sequence_points (exp);
11109 }
11110 }
11111
11112 /* Add this new SWITCH_STMT to the stack. */
11113 cs = XNEW (struct c_switch);
11114 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11115 NULL_TREE, orig_type, NULL_TREE);
11116 cs->orig_type = orig_type;
11117 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11118 cs->bindings = c_get_switch_bindings ();
11119 cs->break_stmt_seen_p = false;
11120 cs->bool_cond_p = bool_cond_p;
11121 cs->next = c_switch_stack;
11122 c_switch_stack = cs;
11123
11124 return add_stmt (cs->switch_stmt);
11125 }
11126
11127 /* Process a case label at location LOC. */
11128
11129 tree
do_case(location_t loc,tree low_value,tree high_value)11130 do_case (location_t loc, tree low_value, tree high_value)
11131 {
11132 tree label = NULL_TREE;
11133
11134 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11135 {
11136 low_value = c_fully_fold (low_value, false, NULL);
11137 if (TREE_CODE (low_value) == INTEGER_CST)
11138 pedwarn (loc, OPT_Wpedantic,
11139 "case label is not an integer constant expression");
11140 }
11141
11142 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11143 {
11144 high_value = c_fully_fold (high_value, false, NULL);
11145 if (TREE_CODE (high_value) == INTEGER_CST)
11146 pedwarn (input_location, OPT_Wpedantic,
11147 "case label is not an integer constant expression");
11148 }
11149
11150 if (c_switch_stack == NULL)
11151 {
11152 if (low_value)
11153 error_at (loc, "case label not within a switch statement");
11154 else
11155 error_at (loc, "%<default%> label not within a switch statement");
11156 return NULL_TREE;
11157 }
11158
11159 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11160 EXPR_LOCATION (c_switch_stack->switch_stmt),
11161 loc))
11162 return NULL_TREE;
11163
11164 label = c_add_case_label (loc, c_switch_stack->cases,
11165 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11166 low_value, high_value);
11167 if (label == error_mark_node)
11168 label = NULL_TREE;
11169 return label;
11170 }
11171
11172 /* Finish the switch statement. TYPE is the original type of the
11173 controlling expression of the switch, or NULL_TREE. */
11174
11175 void
c_finish_switch(tree body,tree type)11176 c_finish_switch (tree body, tree type)
11177 {
11178 struct c_switch *cs = c_switch_stack;
11179 location_t switch_location;
11180
11181 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11182
11183 /* Emit warnings as needed. */
11184 switch_location = EXPR_LOCATION (cs->switch_stmt);
11185 c_do_switch_warnings (cs->cases, switch_location,
11186 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11187 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11188 if (c_switch_covers_all_cases_p (cs->cases,
11189 SWITCH_STMT_TYPE (cs->switch_stmt)))
11190 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11191 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11192
11193 /* Pop the stack. */
11194 c_switch_stack = cs->next;
11195 splay_tree_delete (cs->cases);
11196 c_release_switch_bindings (cs->bindings);
11197 XDELETE (cs);
11198 }
11199
11200 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11201 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11202 may be null. */
11203
11204 void
c_finish_if_stmt(location_t if_locus,tree cond,tree then_block,tree else_block)11205 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11206 tree else_block)
11207 {
11208 tree stmt;
11209
11210 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11211 SET_EXPR_LOCATION (stmt, if_locus);
11212 add_stmt (stmt);
11213 }
11214
11215 tree
c_finish_bc_stmt(location_t loc,tree label,bool is_break)11216 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11217 {
11218 /* In switch statements break is sometimes stylistically used after
11219 a return statement. This can lead to spurious warnings about
11220 control reaching the end of a non-void function when it is
11221 inlined. Note that we are calling block_may_fallthru with
11222 language specific tree nodes; this works because
11223 block_may_fallthru returns true when given something it does not
11224 understand. */
11225 bool skip = !block_may_fallthru (cur_stmt_list);
11226
11227 if (is_break)
11228 switch (in_statement)
11229 {
11230 case 0:
11231 error_at (loc, "break statement not within loop or switch");
11232 return NULL_TREE;
11233 case IN_OMP_BLOCK:
11234 error_at (loc, "invalid exit from OpenMP structured block");
11235 return NULL_TREE;
11236 case IN_OMP_FOR:
11237 error_at (loc, "break statement used with OpenMP for loop");
11238 return NULL_TREE;
11239 case IN_ITERATION_STMT:
11240 case IN_OBJC_FOREACH:
11241 break;
11242 default:
11243 gcc_assert (in_statement & IN_SWITCH_STMT);
11244 c_switch_stack->break_stmt_seen_p = true;
11245 break;
11246 }
11247 else
11248 switch (in_statement & ~IN_SWITCH_STMT)
11249 {
11250 case 0:
11251 error_at (loc, "continue statement not within a loop");
11252 return NULL_TREE;
11253 case IN_OMP_BLOCK:
11254 error_at (loc, "invalid exit from OpenMP structured block");
11255 return NULL_TREE;
11256 case IN_ITERATION_STMT:
11257 case IN_OMP_FOR:
11258 case IN_OBJC_FOREACH:
11259 break;
11260 default:
11261 gcc_unreachable ();
11262 }
11263
11264 if (skip)
11265 return NULL_TREE;
11266 else if ((in_statement & IN_OBJC_FOREACH)
11267 && !(is_break && (in_statement & IN_SWITCH_STMT)))
11268 {
11269 /* The foreach expander produces low-level code using gotos instead
11270 of a structured loop construct. */
11271 gcc_assert (label);
11272 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11273 }
11274 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11275 }
11276
11277 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11278
11279 static void
emit_side_effect_warnings(location_t loc,tree expr)11280 emit_side_effect_warnings (location_t loc, tree expr)
11281 {
11282 maybe_warn_nodiscard (loc, expr);
11283 if (!warn_unused_value)
11284 return;
11285 if (expr == error_mark_node)
11286 ;
11287 else if (!TREE_SIDE_EFFECTS (expr))
11288 {
11289 if (!VOID_TYPE_P (TREE_TYPE (expr))
11290 && !warning_suppressed_p (expr, OPT_Wunused_value))
11291 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11292 }
11293 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11294 {
11295 tree r = expr;
11296 location_t cloc = loc;
11297 while (TREE_CODE (r) == COMPOUND_EXPR)
11298 {
11299 if (EXPR_HAS_LOCATION (r))
11300 cloc = EXPR_LOCATION (r);
11301 r = TREE_OPERAND (r, 1);
11302 }
11303 if (!TREE_SIDE_EFFECTS (r)
11304 && !VOID_TYPE_P (TREE_TYPE (r))
11305 && !CONVERT_EXPR_P (r)
11306 && !warning_suppressed_p (r, OPT_Wunused_value)
11307 && !warning_suppressed_p (expr, OPT_Wunused_value))
11308 warning_at (cloc, OPT_Wunused_value,
11309 "right-hand operand of comma expression has no effect");
11310 }
11311 else
11312 warn_if_unused_value (expr, loc);
11313 }
11314
11315 /* Process an expression as if it were a complete statement. Emit
11316 diagnostics, but do not call ADD_STMT. LOC is the location of the
11317 statement. */
11318
11319 tree
c_process_expr_stmt(location_t loc,tree expr)11320 c_process_expr_stmt (location_t loc, tree expr)
11321 {
11322 tree exprv;
11323
11324 if (!expr)
11325 return NULL_TREE;
11326
11327 expr = c_fully_fold (expr, false, NULL);
11328
11329 if (warn_sequence_point)
11330 verify_sequence_points (expr);
11331
11332 if (TREE_TYPE (expr) != error_mark_node
11333 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11334 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11335 error_at (loc, "expression statement has incomplete type");
11336
11337 /* If we're not processing a statement expression, warn about unused values.
11338 Warnings for statement expressions will be emitted later, once we figure
11339 out which is the result. */
11340 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11341 && (warn_unused_value || warn_unused_result))
11342 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11343
11344 exprv = expr;
11345 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11346 exprv = TREE_OPERAND (exprv, 1);
11347 while (CONVERT_EXPR_P (exprv))
11348 exprv = TREE_OPERAND (exprv, 0);
11349 if (DECL_P (exprv)
11350 || handled_component_p (exprv)
11351 || TREE_CODE (exprv) == ADDR_EXPR)
11352 mark_exp_read (exprv);
11353
11354 /* If the expression is not of a type to which we cannot assign a line
11355 number, wrap the thing in a no-op NOP_EXPR. */
11356 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11357 {
11358 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11359 SET_EXPR_LOCATION (expr, loc);
11360 }
11361
11362 return expr;
11363 }
11364
11365 /* Emit an expression as a statement. LOC is the location of the
11366 expression. */
11367
11368 tree
c_finish_expr_stmt(location_t loc,tree expr)11369 c_finish_expr_stmt (location_t loc, tree expr)
11370 {
11371 if (expr)
11372 return add_stmt (c_process_expr_stmt (loc, expr));
11373 else
11374 return NULL;
11375 }
11376
11377 /* Do the opposite and emit a statement as an expression. To begin,
11378 create a new binding level and return it. */
11379
11380 tree
c_begin_stmt_expr(void)11381 c_begin_stmt_expr (void)
11382 {
11383 tree ret;
11384
11385 /* We must force a BLOCK for this level so that, if it is not expanded
11386 later, there is a way to turn off the entire subtree of blocks that
11387 are contained in it. */
11388 keep_next_level ();
11389 ret = c_begin_compound_stmt (true);
11390
11391 c_bindings_start_stmt_expr (c_switch_stack == NULL
11392 ? NULL
11393 : c_switch_stack->bindings);
11394
11395 /* Mark the current statement list as belonging to a statement list. */
11396 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11397
11398 return ret;
11399 }
11400
11401 /* LOC is the location of the compound statement to which this body
11402 belongs. */
11403
11404 tree
c_finish_stmt_expr(location_t loc,tree body)11405 c_finish_stmt_expr (location_t loc, tree body)
11406 {
11407 tree last, type, tmp, val;
11408 tree *last_p;
11409
11410 body = c_end_compound_stmt (loc, body, true);
11411
11412 c_bindings_end_stmt_expr (c_switch_stack == NULL
11413 ? NULL
11414 : c_switch_stack->bindings);
11415
11416 /* Locate the last statement in BODY. See c_end_compound_stmt
11417 about always returning a BIND_EXPR. */
11418 last_p = &BIND_EXPR_BODY (body);
11419 last = BIND_EXPR_BODY (body);
11420
11421 continue_searching:
11422 if (TREE_CODE (last) == STATEMENT_LIST)
11423 {
11424 tree_stmt_iterator l = tsi_last (last);
11425
11426 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11427 tsi_prev (&l);
11428
11429 /* This can happen with degenerate cases like ({ }). No value. */
11430 if (tsi_end_p (l))
11431 return body;
11432
11433 /* If we're supposed to generate side effects warnings, process
11434 all of the statements except the last. */
11435 if (warn_unused_value || warn_unused_result)
11436 {
11437 for (tree_stmt_iterator i = tsi_start (last);
11438 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11439 {
11440 location_t tloc;
11441 tree t = tsi_stmt (i);
11442
11443 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11444 emit_side_effect_warnings (tloc, t);
11445 }
11446 }
11447 last_p = tsi_stmt_ptr (l);
11448 last = *last_p;
11449 }
11450
11451 /* If the end of the list is exception related, then the list was split
11452 by a call to push_cleanup. Continue searching. */
11453 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11454 || TREE_CODE (last) == TRY_CATCH_EXPR)
11455 {
11456 last_p = &TREE_OPERAND (last, 0);
11457 last = *last_p;
11458 goto continue_searching;
11459 }
11460
11461 if (last == error_mark_node)
11462 return last;
11463
11464 /* In the case that the BIND_EXPR is not necessary, return the
11465 expression out from inside it. */
11466 if ((last == BIND_EXPR_BODY (body)
11467 /* Skip nested debug stmts. */
11468 || last == expr_first (BIND_EXPR_BODY (body)))
11469 && BIND_EXPR_VARS (body) == NULL)
11470 {
11471 /* Even if this looks constant, do not allow it in a constant
11472 expression. */
11473 last = c_wrap_maybe_const (last, true);
11474 /* Do not warn if the return value of a statement expression is
11475 unused. */
11476 suppress_warning (last, OPT_Wunused);
11477 return last;
11478 }
11479
11480 /* Extract the type of said expression. */
11481 type = TREE_TYPE (last);
11482
11483 /* If we're not returning a value at all, then the BIND_EXPR that
11484 we already have is a fine expression to return. */
11485 if (!type || VOID_TYPE_P (type))
11486 return body;
11487
11488 /* Now that we've located the expression containing the value, it seems
11489 silly to make voidify_wrapper_expr repeat the process. Create a
11490 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11491 tmp = create_tmp_var_raw (type);
11492
11493 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11494 tree_expr_nonnegative_p giving up immediately. */
11495 val = last;
11496 if (TREE_CODE (val) == NOP_EXPR
11497 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11498 val = TREE_OPERAND (val, 0);
11499
11500 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11501 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11502
11503 {
11504 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11505 SET_EXPR_LOCATION (t, loc);
11506 return t;
11507 }
11508 }
11509
11510 /* Begin and end compound statements. This is as simple as pushing
11511 and popping new statement lists from the tree. */
11512
11513 tree
c_begin_compound_stmt(bool do_scope)11514 c_begin_compound_stmt (bool do_scope)
11515 {
11516 tree stmt = push_stmt_list ();
11517 if (do_scope)
11518 push_scope ();
11519 return stmt;
11520 }
11521
11522 /* End a compound statement. STMT is the statement. LOC is the
11523 location of the compound statement-- this is usually the location
11524 of the opening brace. */
11525
11526 tree
c_end_compound_stmt(location_t loc,tree stmt,bool do_scope)11527 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11528 {
11529 tree block = NULL;
11530
11531 if (do_scope)
11532 {
11533 if (c_dialect_objc ())
11534 objc_clear_super_receiver ();
11535 block = pop_scope ();
11536 }
11537
11538 stmt = pop_stmt_list (stmt);
11539 stmt = c_build_bind_expr (loc, block, stmt);
11540
11541 /* If this compound statement is nested immediately inside a statement
11542 expression, then force a BIND_EXPR to be created. Otherwise we'll
11543 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11544 STATEMENT_LISTs merge, and thus we can lose track of what statement
11545 was really last. */
11546 if (building_stmt_list_p ()
11547 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11548 && TREE_CODE (stmt) != BIND_EXPR)
11549 {
11550 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11551 TREE_SIDE_EFFECTS (stmt) = 1;
11552 SET_EXPR_LOCATION (stmt, loc);
11553 }
11554
11555 return stmt;
11556 }
11557
11558 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11559 when the current scope is exited. EH_ONLY is true when this is not
11560 meant to apply to normal control flow transfer. */
11561
11562 void
push_cleanup(tree decl,tree cleanup,bool eh_only)11563 push_cleanup (tree decl, tree cleanup, bool eh_only)
11564 {
11565 enum tree_code code;
11566 tree stmt, list;
11567 bool stmt_expr;
11568
11569 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11570 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11571 add_stmt (stmt);
11572 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11573 list = push_stmt_list ();
11574 TREE_OPERAND (stmt, 0) = list;
11575 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11576 }
11577
11578 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11579 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11580
11581 static tree
build_vec_cmp(tree_code code,tree type,tree arg0,tree arg1)11582 build_vec_cmp (tree_code code, tree type,
11583 tree arg0, tree arg1)
11584 {
11585 tree zero_vec = build_zero_cst (type);
11586 tree minus_one_vec = build_minus_one_cst (type);
11587 tree cmp_type = truth_type_for (type);
11588 tree cmp = build2 (code, cmp_type, arg0, arg1);
11589 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11590 }
11591
11592 /* Possibly warn about an address of OP never being NULL in a comparison
11593 operation CODE involving null. */
11594
11595 static void
maybe_warn_for_null_address(location_t loc,tree op,tree_code code)11596 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
11597 {
11598 /* Prevent warnings issued for macro expansion. */
11599 if (!warn_address
11600 || warning_suppressed_p (op, OPT_Waddress)
11601 || from_macro_expansion_at (loc))
11602 return;
11603
11604 if (TREE_CODE (op) == NOP_EXPR)
11605 {
11606 /* Allow casts to intptr_t to suppress the warning. */
11607 tree type = TREE_TYPE (op);
11608 if (TREE_CODE (type) == INTEGER_TYPE)
11609 return;
11610 op = TREE_OPERAND (op, 0);
11611 }
11612
11613 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
11614 {
11615 /* Allow a cast to void* to suppress the warning. */
11616 tree type = TREE_TYPE (TREE_TYPE (op));
11617 if (VOID_TYPE_P (type))
11618 return;
11619
11620 /* Adding any value to a null pointer, including zero, is undefined
11621 in C. This includes the expression &p[0] where p is the null
11622 pointer, although &p[0] will have been folded to p by this point
11623 and so not diagnosed. */
11624 if (code == EQ_EXPR)
11625 warning_at (loc, OPT_Waddress,
11626 "the comparison will always evaluate as %<false%> "
11627 "for the pointer operand in %qE must not be NULL",
11628 op);
11629 else
11630 warning_at (loc, OPT_Waddress,
11631 "the comparison will always evaluate as %<true%> "
11632 "for the pointer operand in %qE must not be NULL",
11633 op);
11634
11635 return;
11636 }
11637
11638 if (TREE_CODE (op) != ADDR_EXPR)
11639 return;
11640
11641 op = TREE_OPERAND (op, 0);
11642
11643 if (TREE_CODE (op) == IMAGPART_EXPR
11644 || TREE_CODE (op) == REALPART_EXPR)
11645 {
11646 /* The address of either complex part may not be null. */
11647 if (code == EQ_EXPR)
11648 warning_at (loc, OPT_Waddress,
11649 "the comparison will always evaluate as %<false%> "
11650 "for the address of %qE will never be NULL",
11651 op);
11652 else
11653 warning_at (loc, OPT_Waddress,
11654 "the comparison will always evaluate as %<true%> "
11655 "for the address of %qE will never be NULL",
11656 op);
11657 return;
11658 }
11659
11660 /* Set to true in the loop below if OP dereferences is operand.
11661 In such a case the ultimate target need not be a decl for
11662 the null [in]equality test to be constant. */
11663 bool deref = false;
11664
11665 /* Get the outermost array or object, or member. */
11666 while (handled_component_p (op))
11667 {
11668 if (TREE_CODE (op) == COMPONENT_REF)
11669 {
11670 /* Get the member (its address is never null). */
11671 op = TREE_OPERAND (op, 1);
11672 break;
11673 }
11674
11675 /* Get the outer array/object to refer to in the warning. */
11676 op = TREE_OPERAND (op, 0);
11677 deref = true;
11678 }
11679
11680 if ((!deref && !decl_with_nonnull_addr_p (op))
11681 || from_macro_expansion_at (loc))
11682 return;
11683
11684 bool w;
11685 if (code == EQ_EXPR)
11686 w = warning_at (loc, OPT_Waddress,
11687 "the comparison will always evaluate as %<false%> "
11688 "for the address of %qE will never be NULL",
11689 op);
11690 else
11691 w = warning_at (loc, OPT_Waddress,
11692 "the comparison will always evaluate as %<true%> "
11693 "for the address of %qE will never be NULL",
11694 op);
11695
11696 if (w && DECL_P (op))
11697 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
11698 }
11699
11700 /* Build a binary-operation expression without default conversions.
11701 CODE is the kind of expression to build.
11702 LOCATION is the operator's location.
11703 This function differs from `build' in several ways:
11704 the data type of the result is computed and recorded in it,
11705 warnings are generated if arg data types are invalid,
11706 special handling for addition and subtraction of pointers is known,
11707 and some optimization is done (operations on narrow ints
11708 are done in the narrower type when that gives the same result).
11709 Constant folding is also done before the result is returned.
11710
11711 Note that the operands will never have enumeral types, or function
11712 or array types, because either they will have the default conversions
11713 performed or they have both just been converted to some other type in which
11714 the arithmetic is to be done. */
11715
11716 tree
build_binary_op(location_t location,enum tree_code code,tree orig_op0,tree orig_op1,bool convert_p)11717 build_binary_op (location_t location, enum tree_code code,
11718 tree orig_op0, tree orig_op1, bool convert_p)
11719 {
11720 tree type0, type1, orig_type0, orig_type1;
11721 tree eptype;
11722 enum tree_code code0, code1;
11723 tree op0, op1;
11724 tree ret = error_mark_node;
11725 const char *invalid_op_diag;
11726 bool op0_int_operands, op1_int_operands;
11727 bool int_const, int_const_or_overflow, int_operands;
11728
11729 /* Expression code to give to the expression when it is built.
11730 Normally this is CODE, which is what the caller asked for,
11731 but in some special cases we change it. */
11732 enum tree_code resultcode = code;
11733
11734 /* Data type in which the computation is to be performed.
11735 In the simplest cases this is the common type of the arguments. */
11736 tree result_type = NULL;
11737
11738 /* When the computation is in excess precision, the type of the
11739 final EXCESS_PRECISION_EXPR. */
11740 tree semantic_result_type = NULL;
11741
11742 /* Nonzero means operands have already been type-converted
11743 in whatever way is necessary.
11744 Zero means they need to be converted to RESULT_TYPE. */
11745 int converted = 0;
11746
11747 /* Nonzero means create the expression with this type, rather than
11748 RESULT_TYPE. */
11749 tree build_type = NULL_TREE;
11750
11751 /* Nonzero means after finally constructing the expression
11752 convert it to this type. */
11753 tree final_type = NULL_TREE;
11754
11755 /* Nonzero if this is an operation like MIN or MAX which can
11756 safely be computed in short if both args are promoted shorts.
11757 Also implies COMMON.
11758 -1 indicates a bitwise operation; this makes a difference
11759 in the exact conditions for when it is safe to do the operation
11760 in a narrower mode. */
11761 int shorten = 0;
11762
11763 /* Nonzero if this is a comparison operation;
11764 if both args are promoted shorts, compare the original shorts.
11765 Also implies COMMON. */
11766 int short_compare = 0;
11767
11768 /* Nonzero if this is a right-shift operation, which can be computed on the
11769 original short and then promoted if the operand is a promoted short. */
11770 int short_shift = 0;
11771
11772 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11773 int common = 0;
11774
11775 /* True means types are compatible as far as ObjC is concerned. */
11776 bool objc_ok;
11777
11778 /* True means this is an arithmetic operation that may need excess
11779 precision. */
11780 bool may_need_excess_precision;
11781
11782 /* True means this is a boolean operation that converts both its
11783 operands to truth-values. */
11784 bool boolean_op = false;
11785
11786 /* Remember whether we're doing / or %. */
11787 bool doing_div_or_mod = false;
11788
11789 /* Remember whether we're doing << or >>. */
11790 bool doing_shift = false;
11791
11792 /* Tree holding instrumentation expression. */
11793 tree instrument_expr = NULL;
11794
11795 if (location == UNKNOWN_LOCATION)
11796 location = input_location;
11797
11798 op0 = orig_op0;
11799 op1 = orig_op1;
11800
11801 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11802 if (op0_int_operands)
11803 op0 = remove_c_maybe_const_expr (op0);
11804 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11805 if (op1_int_operands)
11806 op1 = remove_c_maybe_const_expr (op1);
11807 int_operands = (op0_int_operands && op1_int_operands);
11808 if (int_operands)
11809 {
11810 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11811 && TREE_CODE (orig_op1) == INTEGER_CST);
11812 int_const = (int_const_or_overflow
11813 && !TREE_OVERFLOW (orig_op0)
11814 && !TREE_OVERFLOW (orig_op1));
11815 }
11816 else
11817 int_const = int_const_or_overflow = false;
11818
11819 /* Do not apply default conversion in mixed vector/scalar expression. */
11820 if (convert_p
11821 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11822 {
11823 op0 = default_conversion (op0);
11824 op1 = default_conversion (op1);
11825 }
11826
11827 orig_type0 = type0 = TREE_TYPE (op0);
11828
11829 orig_type1 = type1 = TREE_TYPE (op1);
11830
11831 /* The expression codes of the data types of the arguments tell us
11832 whether the arguments are integers, floating, pointers, etc. */
11833 code0 = TREE_CODE (type0);
11834 code1 = TREE_CODE (type1);
11835
11836 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11837 STRIP_TYPE_NOPS (op0);
11838 STRIP_TYPE_NOPS (op1);
11839
11840 /* If an error was already reported for one of the arguments,
11841 avoid reporting another error. */
11842
11843 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11844 return error_mark_node;
11845
11846 if (code0 == POINTER_TYPE
11847 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11848 return error_mark_node;
11849
11850 if (code1 == POINTER_TYPE
11851 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11852 return error_mark_node;
11853
11854 if ((invalid_op_diag
11855 = targetm.invalid_binary_op (code, type0, type1)))
11856 {
11857 error_at (location, invalid_op_diag);
11858 return error_mark_node;
11859 }
11860
11861 switch (code)
11862 {
11863 case PLUS_EXPR:
11864 case MINUS_EXPR:
11865 case MULT_EXPR:
11866 case TRUNC_DIV_EXPR:
11867 case CEIL_DIV_EXPR:
11868 case FLOOR_DIV_EXPR:
11869 case ROUND_DIV_EXPR:
11870 case EXACT_DIV_EXPR:
11871 may_need_excess_precision = true;
11872 break;
11873
11874 case EQ_EXPR:
11875 case NE_EXPR:
11876 case LE_EXPR:
11877 case GE_EXPR:
11878 case LT_EXPR:
11879 case GT_EXPR:
11880 /* Excess precision for implicit conversions of integers to
11881 floating point in C11 and later. */
11882 may_need_excess_precision = (flag_isoc11
11883 && (ANY_INTEGRAL_TYPE_P (type0)
11884 || ANY_INTEGRAL_TYPE_P (type1)));
11885 break;
11886
11887 default:
11888 may_need_excess_precision = false;
11889 break;
11890 }
11891 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11892 {
11893 op0 = TREE_OPERAND (op0, 0);
11894 type0 = TREE_TYPE (op0);
11895 }
11896 else if (may_need_excess_precision
11897 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11898 {
11899 type0 = eptype;
11900 op0 = convert (eptype, op0);
11901 }
11902 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11903 {
11904 op1 = TREE_OPERAND (op1, 0);
11905 type1 = TREE_TYPE (op1);
11906 }
11907 else if (may_need_excess_precision
11908 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11909 {
11910 type1 = eptype;
11911 op1 = convert (eptype, op1);
11912 }
11913
11914 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11915
11916 /* In case when one of the operands of the binary operation is
11917 a vector and another is a scalar -- convert scalar to vector. */
11918 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11919 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11920 {
11921 enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
11922 orig_op1, true);
11923
11924 switch (convert_flag)
11925 {
11926 case stv_error:
11927 return error_mark_node;
11928 case stv_firstarg:
11929 {
11930 bool maybe_const = true;
11931 tree sc;
11932 sc = c_fully_fold (op0, false, &maybe_const);
11933 sc = save_expr (sc);
11934 sc = convert (TREE_TYPE (type1), sc);
11935 op0 = build_vector_from_val (type1, sc);
11936 if (!maybe_const)
11937 op0 = c_wrap_maybe_const (op0, true);
11938 orig_type0 = type0 = TREE_TYPE (op0);
11939 code0 = TREE_CODE (type0);
11940 converted = 1;
11941 break;
11942 }
11943 case stv_secondarg:
11944 {
11945 bool maybe_const = true;
11946 tree sc;
11947 sc = c_fully_fold (op1, false, &maybe_const);
11948 sc = save_expr (sc);
11949 sc = convert (TREE_TYPE (type0), sc);
11950 op1 = build_vector_from_val (type0, sc);
11951 if (!maybe_const)
11952 op1 = c_wrap_maybe_const (op1, true);
11953 orig_type1 = type1 = TREE_TYPE (op1);
11954 code1 = TREE_CODE (type1);
11955 converted = 1;
11956 break;
11957 }
11958 default:
11959 break;
11960 }
11961 }
11962
11963 switch (code)
11964 {
11965 case PLUS_EXPR:
11966 /* Handle the pointer + int case. */
11967 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11968 {
11969 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11970 goto return_build_binary_op;
11971 }
11972 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11973 {
11974 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11975 goto return_build_binary_op;
11976 }
11977 else
11978 common = 1;
11979 break;
11980
11981 case MINUS_EXPR:
11982 /* Subtraction of two similar pointers.
11983 We must subtract them as integers, then divide by object size. */
11984 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11985 && comp_target_types (location, type0, type1))
11986 {
11987 ret = pointer_diff (location, op0, op1, &instrument_expr);
11988 goto return_build_binary_op;
11989 }
11990 /* Handle pointer minus int. Just like pointer plus int. */
11991 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11992 {
11993 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11994 goto return_build_binary_op;
11995 }
11996 else
11997 common = 1;
11998 break;
11999
12000 case MULT_EXPR:
12001 common = 1;
12002 break;
12003
12004 case TRUNC_DIV_EXPR:
12005 case CEIL_DIV_EXPR:
12006 case FLOOR_DIV_EXPR:
12007 case ROUND_DIV_EXPR:
12008 case EXACT_DIV_EXPR:
12009 doing_div_or_mod = true;
12010 warn_for_div_by_zero (location, op1);
12011
12012 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12013 || code0 == FIXED_POINT_TYPE
12014 || code0 == COMPLEX_TYPE
12015 || gnu_vector_type_p (type0))
12016 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12017 || code1 == FIXED_POINT_TYPE
12018 || code1 == COMPLEX_TYPE
12019 || gnu_vector_type_p (type1)))
12020 {
12021 enum tree_code tcode0 = code0, tcode1 = code1;
12022
12023 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12024 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12025 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12026 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12027
12028 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
12029 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12030 resultcode = RDIV_EXPR;
12031 else
12032 /* Although it would be tempting to shorten always here, that
12033 loses on some targets, since the modulo instruction is
12034 undefined if the quotient can't be represented in the
12035 computation mode. We shorten only if unsigned or if
12036 dividing by something we know != -1. */
12037 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12038 || (TREE_CODE (op1) == INTEGER_CST
12039 && !integer_all_onesp (op1)));
12040 common = 1;
12041 }
12042 break;
12043
12044 case BIT_AND_EXPR:
12045 case BIT_IOR_EXPR:
12046 case BIT_XOR_EXPR:
12047 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12048 shorten = -1;
12049 /* Allow vector types which are not floating point types. */
12050 else if (gnu_vector_type_p (type0)
12051 && gnu_vector_type_p (type1)
12052 && !VECTOR_FLOAT_TYPE_P (type0)
12053 && !VECTOR_FLOAT_TYPE_P (type1))
12054 common = 1;
12055 break;
12056
12057 case TRUNC_MOD_EXPR:
12058 case FLOOR_MOD_EXPR:
12059 doing_div_or_mod = true;
12060 warn_for_div_by_zero (location, op1);
12061
12062 if (gnu_vector_type_p (type0)
12063 && gnu_vector_type_p (type1)
12064 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12065 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12066 common = 1;
12067 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12068 {
12069 /* Although it would be tempting to shorten always here, that loses
12070 on some targets, since the modulo instruction is undefined if the
12071 quotient can't be represented in the computation mode. We shorten
12072 only if unsigned or if dividing by something we know != -1. */
12073 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12074 || (TREE_CODE (op1) == INTEGER_CST
12075 && !integer_all_onesp (op1)));
12076 common = 1;
12077 }
12078 break;
12079
12080 case TRUTH_ANDIF_EXPR:
12081 case TRUTH_ORIF_EXPR:
12082 case TRUTH_AND_EXPR:
12083 case TRUTH_OR_EXPR:
12084 case TRUTH_XOR_EXPR:
12085 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12086 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12087 || code0 == FIXED_POINT_TYPE)
12088 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12089 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12090 || code1 == FIXED_POINT_TYPE))
12091 {
12092 /* Result of these operations is always an int,
12093 but that does not mean the operands should be
12094 converted to ints! */
12095 result_type = integer_type_node;
12096 if (op0_int_operands)
12097 {
12098 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12099 op0 = remove_c_maybe_const_expr (op0);
12100 }
12101 else
12102 op0 = c_objc_common_truthvalue_conversion (location, op0);
12103 if (op1_int_operands)
12104 {
12105 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12106 op1 = remove_c_maybe_const_expr (op1);
12107 }
12108 else
12109 op1 = c_objc_common_truthvalue_conversion (location, op1);
12110 converted = 1;
12111 boolean_op = true;
12112 }
12113 if (code == TRUTH_ANDIF_EXPR)
12114 {
12115 int_const_or_overflow = (int_operands
12116 && TREE_CODE (orig_op0) == INTEGER_CST
12117 && (op0 == truthvalue_false_node
12118 || TREE_CODE (orig_op1) == INTEGER_CST));
12119 int_const = (int_const_or_overflow
12120 && !TREE_OVERFLOW (orig_op0)
12121 && (op0 == truthvalue_false_node
12122 || !TREE_OVERFLOW (orig_op1)));
12123 }
12124 else if (code == TRUTH_ORIF_EXPR)
12125 {
12126 int_const_or_overflow = (int_operands
12127 && TREE_CODE (orig_op0) == INTEGER_CST
12128 && (op0 == truthvalue_true_node
12129 || TREE_CODE (orig_op1) == INTEGER_CST));
12130 int_const = (int_const_or_overflow
12131 && !TREE_OVERFLOW (orig_op0)
12132 && (op0 == truthvalue_true_node
12133 || !TREE_OVERFLOW (orig_op1)));
12134 }
12135 break;
12136
12137 /* Shift operations: result has same type as first operand;
12138 always convert second operand to int.
12139 Also set SHORT_SHIFT if shifting rightward. */
12140
12141 case RSHIFT_EXPR:
12142 if (gnu_vector_type_p (type0)
12143 && gnu_vector_type_p (type1)
12144 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12145 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12146 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12147 TYPE_VECTOR_SUBPARTS (type1)))
12148 {
12149 result_type = type0;
12150 converted = 1;
12151 }
12152 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12153 || (gnu_vector_type_p (type0)
12154 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12155 && code1 == INTEGER_TYPE)
12156 {
12157 doing_shift = true;
12158 if (TREE_CODE (op1) == INTEGER_CST)
12159 {
12160 if (tree_int_cst_sgn (op1) < 0)
12161 {
12162 int_const = false;
12163 if (c_inhibit_evaluation_warnings == 0)
12164 warning_at (location, OPT_Wshift_count_negative,
12165 "right shift count is negative");
12166 }
12167 else if (code0 == VECTOR_TYPE)
12168 {
12169 if (compare_tree_int (op1,
12170 TYPE_PRECISION (TREE_TYPE (type0)))
12171 >= 0)
12172 {
12173 int_const = false;
12174 if (c_inhibit_evaluation_warnings == 0)
12175 warning_at (location, OPT_Wshift_count_overflow,
12176 "right shift count >= width of vector element");
12177 }
12178 }
12179 else
12180 {
12181 if (!integer_zerop (op1))
12182 short_shift = 1;
12183
12184 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12185 {
12186 int_const = false;
12187 if (c_inhibit_evaluation_warnings == 0)
12188 warning_at (location, OPT_Wshift_count_overflow,
12189 "right shift count >= width of type");
12190 }
12191 }
12192 }
12193
12194 /* Use the type of the value to be shifted. */
12195 result_type = type0;
12196 /* Avoid converting op1 to result_type later. */
12197 converted = 1;
12198 }
12199 break;
12200
12201 case LSHIFT_EXPR:
12202 if (gnu_vector_type_p (type0)
12203 && gnu_vector_type_p (type1)
12204 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12205 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12206 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12207 TYPE_VECTOR_SUBPARTS (type1)))
12208 {
12209 result_type = type0;
12210 converted = 1;
12211 }
12212 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12213 || (gnu_vector_type_p (type0)
12214 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12215 && code1 == INTEGER_TYPE)
12216 {
12217 doing_shift = true;
12218 if (TREE_CODE (op0) == INTEGER_CST
12219 && tree_int_cst_sgn (op0) < 0
12220 && !TYPE_OVERFLOW_WRAPS (type0))
12221 {
12222 /* Don't reject a left shift of a negative value in a context
12223 where a constant expression is needed in C90. */
12224 if (flag_isoc99)
12225 int_const = false;
12226 if (c_inhibit_evaluation_warnings == 0)
12227 warning_at (location, OPT_Wshift_negative_value,
12228 "left shift of negative value");
12229 }
12230 if (TREE_CODE (op1) == INTEGER_CST)
12231 {
12232 if (tree_int_cst_sgn (op1) < 0)
12233 {
12234 int_const = false;
12235 if (c_inhibit_evaluation_warnings == 0)
12236 warning_at (location, OPT_Wshift_count_negative,
12237 "left shift count is negative");
12238 }
12239 else if (code0 == VECTOR_TYPE)
12240 {
12241 if (compare_tree_int (op1,
12242 TYPE_PRECISION (TREE_TYPE (type0)))
12243 >= 0)
12244 {
12245 int_const = false;
12246 if (c_inhibit_evaluation_warnings == 0)
12247 warning_at (location, OPT_Wshift_count_overflow,
12248 "left shift count >= width of vector element");
12249 }
12250 }
12251 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12252 {
12253 int_const = false;
12254 if (c_inhibit_evaluation_warnings == 0)
12255 warning_at (location, OPT_Wshift_count_overflow,
12256 "left shift count >= width of type");
12257 }
12258 else if (TREE_CODE (op0) == INTEGER_CST
12259 && maybe_warn_shift_overflow (location, op0, op1)
12260 && flag_isoc99)
12261 int_const = false;
12262 }
12263
12264 /* Use the type of the value to be shifted. */
12265 result_type = type0;
12266 /* Avoid converting op1 to result_type later. */
12267 converted = 1;
12268 }
12269 break;
12270
12271 case EQ_EXPR:
12272 case NE_EXPR:
12273 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12274 {
12275 tree intt;
12276 if (!vector_types_compatible_elements_p (type0, type1))
12277 {
12278 error_at (location, "comparing vectors with different "
12279 "element types");
12280 return error_mark_node;
12281 }
12282
12283 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12284 TYPE_VECTOR_SUBPARTS (type1)))
12285 {
12286 error_at (location, "comparing vectors with different "
12287 "number of elements");
12288 return error_mark_node;
12289 }
12290
12291 /* It's not precisely specified how the usual arithmetic
12292 conversions apply to the vector types. Here, we use
12293 the unsigned type if one of the operands is signed and
12294 the other one is unsigned. */
12295 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12296 {
12297 if (!TYPE_UNSIGNED (type0))
12298 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12299 else
12300 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12301 warning_at (location, OPT_Wsign_compare, "comparison between "
12302 "types %qT and %qT", type0, type1);
12303 }
12304
12305 /* Always construct signed integer vector type. */
12306 intt = c_common_type_for_size (GET_MODE_BITSIZE
12307 (SCALAR_TYPE_MODE
12308 (TREE_TYPE (type0))), 0);
12309 if (!intt)
12310 {
12311 error_at (location, "could not find an integer type "
12312 "of the same size as %qT",
12313 TREE_TYPE (type0));
12314 return error_mark_node;
12315 }
12316 result_type = build_opaque_vector_type (intt,
12317 TYPE_VECTOR_SUBPARTS (type0));
12318 converted = 1;
12319 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12320 goto return_build_binary_op;
12321 }
12322 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12323 warning_at (location,
12324 OPT_Wfloat_equal,
12325 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12326 /* Result of comparison is always int,
12327 but don't convert the args to int! */
12328 build_type = integer_type_node;
12329 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12330 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12331 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12332 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12333 short_compare = 1;
12334 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12335 {
12336 maybe_warn_for_null_address (location, op0, code);
12337 result_type = type0;
12338 }
12339 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12340 {
12341 maybe_warn_for_null_address (location, op1, code);
12342 result_type = type1;
12343 }
12344 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12345 {
12346 tree tt0 = TREE_TYPE (type0);
12347 tree tt1 = TREE_TYPE (type1);
12348 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12349 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12350 addr_space_t as_common = ADDR_SPACE_GENERIC;
12351
12352 /* Anything compares with void *. void * compares with anything.
12353 Otherwise, the targets must be compatible
12354 and both must be object or both incomplete. */
12355 if (comp_target_types (location, type0, type1))
12356 result_type = common_pointer_type (type0, type1);
12357 else if (!addr_space_superset (as0, as1, &as_common))
12358 {
12359 error_at (location, "comparison of pointers to "
12360 "disjoint address spaces");
12361 return error_mark_node;
12362 }
12363 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12364 {
12365 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12366 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12367 "comparison of %<void *%> with function pointer");
12368 }
12369 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12370 {
12371 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12372 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12373 "comparison of %<void *%> with function pointer");
12374 }
12375 else
12376 /* Avoid warning about the volatile ObjC EH puts on decls. */
12377 if (!objc_ok)
12378 pedwarn (location, 0,
12379 "comparison of distinct pointer types lacks a cast");
12380
12381 if (result_type == NULL_TREE)
12382 {
12383 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12384 result_type = build_pointer_type
12385 (build_qualified_type (void_type_node, qual));
12386 }
12387 }
12388 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12389 {
12390 result_type = type0;
12391 pedwarn (location, 0, "comparison between pointer and integer");
12392 }
12393 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12394 {
12395 result_type = type1;
12396 pedwarn (location, 0, "comparison between pointer and integer");
12397 }
12398 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12399 || truth_value_p (TREE_CODE (orig_op0)))
12400 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12401 || truth_value_p (TREE_CODE (orig_op1))))
12402 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12403 break;
12404
12405 case LE_EXPR:
12406 case GE_EXPR:
12407 case LT_EXPR:
12408 case GT_EXPR:
12409 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12410 {
12411 tree intt;
12412 if (!vector_types_compatible_elements_p (type0, type1))
12413 {
12414 error_at (location, "comparing vectors with different "
12415 "element types");
12416 return error_mark_node;
12417 }
12418
12419 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12420 TYPE_VECTOR_SUBPARTS (type1)))
12421 {
12422 error_at (location, "comparing vectors with different "
12423 "number of elements");
12424 return error_mark_node;
12425 }
12426
12427 /* It's not precisely specified how the usual arithmetic
12428 conversions apply to the vector types. Here, we use
12429 the unsigned type if one of the operands is signed and
12430 the other one is unsigned. */
12431 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12432 {
12433 if (!TYPE_UNSIGNED (type0))
12434 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12435 else
12436 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12437 warning_at (location, OPT_Wsign_compare, "comparison between "
12438 "types %qT and %qT", type0, type1);
12439 }
12440
12441 /* Always construct signed integer vector type. */
12442 intt = c_common_type_for_size (GET_MODE_BITSIZE
12443 (SCALAR_TYPE_MODE
12444 (TREE_TYPE (type0))), 0);
12445 if (!intt)
12446 {
12447 error_at (location, "could not find an integer type "
12448 "of the same size as %qT",
12449 TREE_TYPE (type0));
12450 return error_mark_node;
12451 }
12452 result_type = build_opaque_vector_type (intt,
12453 TYPE_VECTOR_SUBPARTS (type0));
12454 converted = 1;
12455 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12456 goto return_build_binary_op;
12457 }
12458 build_type = integer_type_node;
12459 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12460 || code0 == FIXED_POINT_TYPE)
12461 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12462 || code1 == FIXED_POINT_TYPE))
12463 short_compare = 1;
12464 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12465 {
12466 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12467 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12468 addr_space_t as_common;
12469
12470 if (comp_target_types (location, type0, type1))
12471 {
12472 result_type = common_pointer_type (type0, type1);
12473 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12474 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12475 pedwarn_c99 (location, OPT_Wpedantic,
12476 "comparison of complete and incomplete pointers");
12477 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12478 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12479 "ordered comparisons of pointers to functions");
12480 else if (null_pointer_constant_p (orig_op0)
12481 || null_pointer_constant_p (orig_op1))
12482 warning_at (location, OPT_Wextra,
12483 "ordered comparison of pointer with null pointer");
12484
12485 }
12486 else if (!addr_space_superset (as0, as1, &as_common))
12487 {
12488 error_at (location, "comparison of pointers to "
12489 "disjoint address spaces");
12490 return error_mark_node;
12491 }
12492 else
12493 {
12494 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12495 result_type = build_pointer_type
12496 (build_qualified_type (void_type_node, qual));
12497 pedwarn (location, 0,
12498 "comparison of distinct pointer types lacks a cast");
12499 }
12500 }
12501 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12502 {
12503 result_type = type0;
12504 if (pedantic)
12505 pedwarn (location, OPT_Wpedantic,
12506 "ordered comparison of pointer with integer zero");
12507 else if (extra_warnings)
12508 warning_at (location, OPT_Wextra,
12509 "ordered comparison of pointer with integer zero");
12510 }
12511 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12512 {
12513 result_type = type1;
12514 if (pedantic)
12515 pedwarn (location, OPT_Wpedantic,
12516 "ordered comparison of pointer with integer zero");
12517 else if (extra_warnings)
12518 warning_at (location, OPT_Wextra,
12519 "ordered comparison of pointer with integer zero");
12520 }
12521 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12522 {
12523 result_type = type0;
12524 pedwarn (location, 0, "comparison between pointer and integer");
12525 }
12526 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12527 {
12528 result_type = type1;
12529 pedwarn (location, 0, "comparison between pointer and integer");
12530 }
12531
12532 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12533 && current_function_decl != NULL_TREE
12534 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12535 {
12536 op0 = save_expr (op0);
12537 op1 = save_expr (op1);
12538
12539 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12540 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12541 }
12542
12543 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12544 || truth_value_p (TREE_CODE (orig_op0)))
12545 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12546 || truth_value_p (TREE_CODE (orig_op1))))
12547 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12548 break;
12549
12550 case MIN_EXPR:
12551 case MAX_EXPR:
12552 /* Used for OpenMP atomics. */
12553 gcc_assert (flag_openmp);
12554 common = 1;
12555 break;
12556
12557 default:
12558 gcc_unreachable ();
12559 }
12560
12561 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12562 return error_mark_node;
12563
12564 if (gnu_vector_type_p (type0)
12565 && gnu_vector_type_p (type1)
12566 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12567 || !vector_types_compatible_elements_p (type0, type1)))
12568 {
12569 gcc_rich_location richloc (location);
12570 maybe_range_label_for_tree_type_mismatch
12571 label_for_op0 (orig_op0, orig_op1),
12572 label_for_op1 (orig_op1, orig_op0);
12573 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12574 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12575 binary_op_error (&richloc, code, type0, type1);
12576 return error_mark_node;
12577 }
12578
12579 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12580 || code0 == FIXED_POINT_TYPE
12581 || gnu_vector_type_p (type0))
12582 &&
12583 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12584 || code1 == FIXED_POINT_TYPE
12585 || gnu_vector_type_p (type1)))
12586 {
12587 bool first_complex = (code0 == COMPLEX_TYPE);
12588 bool second_complex = (code1 == COMPLEX_TYPE);
12589 int none_complex = (!first_complex && !second_complex);
12590
12591 if (shorten || common || short_compare)
12592 {
12593 result_type = c_common_type (type0, type1);
12594 do_warn_double_promotion (result_type, type0, type1,
12595 "implicit conversion from %qT to %qT "
12596 "to match other operand of binary "
12597 "expression",
12598 location);
12599 if (result_type == error_mark_node)
12600 return error_mark_node;
12601 }
12602
12603 if (first_complex != second_complex
12604 && (code == PLUS_EXPR
12605 || code == MINUS_EXPR
12606 || code == MULT_EXPR
12607 || (code == TRUNC_DIV_EXPR && first_complex))
12608 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12609 && flag_signed_zeros)
12610 {
12611 /* An operation on mixed real/complex operands must be
12612 handled specially, but the language-independent code can
12613 more easily optimize the plain complex arithmetic if
12614 -fno-signed-zeros. */
12615 tree real_type = TREE_TYPE (result_type);
12616 tree real, imag;
12617 if (type0 != orig_type0 || type1 != orig_type1)
12618 {
12619 gcc_assert (may_need_excess_precision && common);
12620 semantic_result_type = c_common_type (orig_type0, orig_type1);
12621 }
12622 if (first_complex)
12623 {
12624 if (TREE_TYPE (op0) != result_type)
12625 op0 = convert_and_check (location, result_type, op0);
12626 if (TREE_TYPE (op1) != real_type)
12627 op1 = convert_and_check (location, real_type, op1);
12628 }
12629 else
12630 {
12631 if (TREE_TYPE (op0) != real_type)
12632 op0 = convert_and_check (location, real_type, op0);
12633 if (TREE_TYPE (op1) != result_type)
12634 op1 = convert_and_check (location, result_type, op1);
12635 }
12636 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12637 return error_mark_node;
12638 if (first_complex)
12639 {
12640 op0 = save_expr (op0);
12641 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12642 op0, true);
12643 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12644 op0, true);
12645 switch (code)
12646 {
12647 case MULT_EXPR:
12648 case TRUNC_DIV_EXPR:
12649 op1 = save_expr (op1);
12650 imag = build2 (resultcode, real_type, imag, op1);
12651 /* Fall through. */
12652 case PLUS_EXPR:
12653 case MINUS_EXPR:
12654 real = build2 (resultcode, real_type, real, op1);
12655 break;
12656 default:
12657 gcc_unreachable();
12658 }
12659 }
12660 else
12661 {
12662 op1 = save_expr (op1);
12663 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12664 op1, true);
12665 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12666 op1, true);
12667 switch (code)
12668 {
12669 case MULT_EXPR:
12670 op0 = save_expr (op0);
12671 imag = build2 (resultcode, real_type, op0, imag);
12672 /* Fall through. */
12673 case PLUS_EXPR:
12674 real = build2 (resultcode, real_type, op0, real);
12675 break;
12676 case MINUS_EXPR:
12677 real = build2 (resultcode, real_type, op0, real);
12678 imag = build1 (NEGATE_EXPR, real_type, imag);
12679 break;
12680 default:
12681 gcc_unreachable();
12682 }
12683 }
12684 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12685 goto return_build_binary_op;
12686 }
12687
12688 /* For certain operations (which identify themselves by shorten != 0)
12689 if both args were extended from the same smaller type,
12690 do the arithmetic in that type and then extend.
12691
12692 shorten !=0 and !=1 indicates a bitwise operation.
12693 For them, this optimization is safe only if
12694 both args are zero-extended or both are sign-extended.
12695 Otherwise, we might change the result.
12696 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12697 but calculated in (unsigned short) it would be (unsigned short)-1. */
12698
12699 if (shorten && none_complex)
12700 {
12701 final_type = result_type;
12702 result_type = shorten_binary_op (result_type, op0, op1,
12703 shorten == -1);
12704 }
12705
12706 /* Shifts can be shortened if shifting right. */
12707
12708 if (short_shift)
12709 {
12710 int unsigned_arg;
12711 tree arg0 = get_narrower (op0, &unsigned_arg);
12712
12713 final_type = result_type;
12714
12715 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12716 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12717
12718 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12719 && tree_int_cst_sgn (op1) > 0
12720 /* We can shorten only if the shift count is less than the
12721 number of bits in the smaller type size. */
12722 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12723 /* We cannot drop an unsigned shift after sign-extension. */
12724 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12725 {
12726 /* Do an unsigned shift if the operand was zero-extended. */
12727 result_type
12728 = c_common_signed_or_unsigned_type (unsigned_arg,
12729 TREE_TYPE (arg0));
12730 /* Convert value-to-be-shifted to that type. */
12731 if (TREE_TYPE (op0) != result_type)
12732 op0 = convert (result_type, op0);
12733 converted = 1;
12734 }
12735 }
12736
12737 /* Comparison operations are shortened too but differently.
12738 They identify themselves by setting short_compare = 1. */
12739
12740 if (short_compare)
12741 {
12742 /* Don't write &op0, etc., because that would prevent op0
12743 from being kept in a register.
12744 Instead, make copies of the our local variables and
12745 pass the copies by reference, then copy them back afterward. */
12746 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12747 enum tree_code xresultcode = resultcode;
12748 tree val
12749 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12750 &xresultcode);
12751
12752 if (val != NULL_TREE)
12753 {
12754 ret = val;
12755 goto return_build_binary_op;
12756 }
12757
12758 op0 = xop0, op1 = xop1;
12759 converted = 1;
12760 resultcode = xresultcode;
12761
12762 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12763 {
12764 bool op0_maybe_const = true;
12765 bool op1_maybe_const = true;
12766 tree orig_op0_folded, orig_op1_folded;
12767
12768 if (in_late_binary_op)
12769 {
12770 orig_op0_folded = orig_op0;
12771 orig_op1_folded = orig_op1;
12772 }
12773 else
12774 {
12775 /* Fold for the sake of possible warnings, as in
12776 build_conditional_expr. This requires the
12777 "original" values to be folded, not just op0 and
12778 op1. */
12779 c_inhibit_evaluation_warnings++;
12780 op0 = c_fully_fold (op0, require_constant_value,
12781 &op0_maybe_const);
12782 op1 = c_fully_fold (op1, require_constant_value,
12783 &op1_maybe_const);
12784 c_inhibit_evaluation_warnings--;
12785 orig_op0_folded = c_fully_fold (orig_op0,
12786 require_constant_value,
12787 NULL);
12788 orig_op1_folded = c_fully_fold (orig_op1,
12789 require_constant_value,
12790 NULL);
12791 }
12792
12793 if (warn_sign_compare)
12794 warn_for_sign_compare (location, orig_op0_folded,
12795 orig_op1_folded, op0, op1,
12796 result_type, resultcode);
12797 if (!in_late_binary_op && !int_operands)
12798 {
12799 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12800 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12801 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12802 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12803 }
12804 }
12805 }
12806 }
12807
12808 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12809 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12810 Then the expression will be built.
12811 It will be given type FINAL_TYPE if that is nonzero;
12812 otherwise, it will be given type RESULT_TYPE. */
12813
12814 if (!result_type)
12815 {
12816 /* Favor showing any expression locations that are available. */
12817 op_location_t oploc (location, UNKNOWN_LOCATION);
12818 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12819 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12820 return error_mark_node;
12821 }
12822
12823 if (build_type == NULL_TREE)
12824 {
12825 build_type = result_type;
12826 if ((type0 != orig_type0 || type1 != orig_type1)
12827 && !boolean_op)
12828 {
12829 gcc_assert (may_need_excess_precision && common);
12830 semantic_result_type = c_common_type (orig_type0, orig_type1);
12831 }
12832 }
12833
12834 if (!converted)
12835 {
12836 op0 = ep_convert_and_check (location, result_type, op0,
12837 semantic_result_type);
12838 op1 = ep_convert_and_check (location, result_type, op1,
12839 semantic_result_type);
12840
12841 /* This can happen if one operand has a vector type, and the other
12842 has a different type. */
12843 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12844 return error_mark_node;
12845 }
12846
12847 if (sanitize_flags_p ((SANITIZE_SHIFT
12848 | SANITIZE_DIVIDE
12849 | SANITIZE_FLOAT_DIVIDE
12850 | SANITIZE_SI_OVERFLOW))
12851 && current_function_decl != NULL_TREE
12852 && (doing_div_or_mod || doing_shift)
12853 && !require_constant_value)
12854 {
12855 /* OP0 and/or OP1 might have side-effects. */
12856 op0 = save_expr (op0);
12857 op1 = save_expr (op1);
12858 op0 = c_fully_fold (op0, false, NULL);
12859 op1 = c_fully_fold (op1, false, NULL);
12860 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12861 | SANITIZE_FLOAT_DIVIDE
12862 | SANITIZE_SI_OVERFLOW))))
12863 instrument_expr = ubsan_instrument_division (location, op0, op1);
12864 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12865 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12866 }
12867
12868 /* Treat expressions in initializers specially as they can't trap. */
12869 if (int_const_or_overflow)
12870 ret = (require_constant_value
12871 ? fold_build2_initializer_loc (location, resultcode, build_type,
12872 op0, op1)
12873 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12874 else
12875 ret = build2 (resultcode, build_type, op0, op1);
12876 if (final_type != NULL_TREE)
12877 ret = convert (final_type, ret);
12878
12879 return_build_binary_op:
12880 gcc_assert (ret != error_mark_node);
12881 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12882 ret = (int_operands
12883 ? note_integer_operands (ret)
12884 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12885 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12886 && !in_late_binary_op)
12887 ret = note_integer_operands (ret);
12888 protected_set_expr_location (ret, location);
12889
12890 if (instrument_expr != NULL)
12891 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12892 instrument_expr, ret);
12893
12894 if (semantic_result_type)
12895 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12896 semantic_result_type, ret);
12897
12898 return ret;
12899 }
12900
12901
12902 /* Convert EXPR to be a truth-value, validating its type for this
12903 purpose. LOCATION is the source location for the expression. */
12904
12905 tree
c_objc_common_truthvalue_conversion(location_t location,tree expr)12906 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12907 {
12908 bool int_const, int_operands;
12909
12910 switch (TREE_CODE (TREE_TYPE (expr)))
12911 {
12912 case ARRAY_TYPE:
12913 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12914 return error_mark_node;
12915
12916 case RECORD_TYPE:
12917 error_at (location, "used struct type value where scalar is required");
12918 return error_mark_node;
12919
12920 case UNION_TYPE:
12921 error_at (location, "used union type value where scalar is required");
12922 return error_mark_node;
12923
12924 case VOID_TYPE:
12925 error_at (location, "void value not ignored as it ought to be");
12926 return error_mark_node;
12927
12928 case POINTER_TYPE:
12929 if (reject_gcc_builtin (expr))
12930 return error_mark_node;
12931 break;
12932
12933 case FUNCTION_TYPE:
12934 gcc_unreachable ();
12935
12936 case VECTOR_TYPE:
12937 error_at (location, "used vector type where scalar is required");
12938 return error_mark_node;
12939
12940 default:
12941 break;
12942 }
12943
12944 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12945 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12946 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12947 {
12948 expr = remove_c_maybe_const_expr (expr);
12949 expr = build2 (NE_EXPR, integer_type_node, expr,
12950 convert (TREE_TYPE (expr), integer_zero_node));
12951 expr = note_integer_operands (expr);
12952 }
12953 else
12954 /* ??? Should we also give an error for vectors rather than leaving
12955 those to give errors later? */
12956 expr = c_common_truthvalue_conversion (location, expr);
12957
12958 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12959 {
12960 if (TREE_OVERFLOW (expr))
12961 return expr;
12962 else
12963 return note_integer_operands (expr);
12964 }
12965 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12966 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12967 return expr;
12968 }
12969
12970
12971 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12972 required. */
12973
12974 tree
c_expr_to_decl(tree expr,bool * tc ATTRIBUTE_UNUSED,bool * se)12975 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12976 {
12977 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12978 {
12979 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12980 /* Executing a compound literal inside a function reinitializes
12981 it. */
12982 if (!TREE_STATIC (decl))
12983 *se = true;
12984 return decl;
12985 }
12986 else
12987 return expr;
12988 }
12989
12990 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12991 statement. LOC is the location of the construct. */
12992
12993 tree
c_finish_omp_construct(location_t loc,enum tree_code code,tree body,tree clauses)12994 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12995 tree clauses)
12996 {
12997 body = c_end_compound_stmt (loc, body, true);
12998
12999 tree stmt = make_node (code);
13000 TREE_TYPE (stmt) = void_type_node;
13001 OMP_BODY (stmt) = body;
13002 OMP_CLAUSES (stmt) = clauses;
13003 SET_EXPR_LOCATION (stmt, loc);
13004
13005 return add_stmt (stmt);
13006 }
13007
13008 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13009 statement. LOC is the location of the OACC_DATA. */
13010
13011 tree
c_finish_oacc_data(location_t loc,tree clauses,tree block)13012 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13013 {
13014 tree stmt;
13015
13016 block = c_end_compound_stmt (loc, block, true);
13017
13018 stmt = make_node (OACC_DATA);
13019 TREE_TYPE (stmt) = void_type_node;
13020 OACC_DATA_CLAUSES (stmt) = clauses;
13021 OACC_DATA_BODY (stmt) = block;
13022 SET_EXPR_LOCATION (stmt, loc);
13023
13024 return add_stmt (stmt);
13025 }
13026
13027 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13028 statement. LOC is the location of the OACC_HOST_DATA. */
13029
13030 tree
c_finish_oacc_host_data(location_t loc,tree clauses,tree block)13031 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13032 {
13033 tree stmt;
13034
13035 block = c_end_compound_stmt (loc, block, true);
13036
13037 stmt = make_node (OACC_HOST_DATA);
13038 TREE_TYPE (stmt) = void_type_node;
13039 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13040 OACC_HOST_DATA_BODY (stmt) = block;
13041 SET_EXPR_LOCATION (stmt, loc);
13042
13043 return add_stmt (stmt);
13044 }
13045
13046 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13047
13048 tree
c_begin_omp_parallel(void)13049 c_begin_omp_parallel (void)
13050 {
13051 tree block;
13052
13053 keep_next_level ();
13054 block = c_begin_compound_stmt (true);
13055
13056 return block;
13057 }
13058
13059 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13060 statement. LOC is the location of the OMP_PARALLEL. */
13061
13062 tree
c_finish_omp_parallel(location_t loc,tree clauses,tree block)13063 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13064 {
13065 tree stmt;
13066
13067 block = c_end_compound_stmt (loc, block, true);
13068
13069 stmt = make_node (OMP_PARALLEL);
13070 TREE_TYPE (stmt) = void_type_node;
13071 OMP_PARALLEL_CLAUSES (stmt) = clauses;
13072 OMP_PARALLEL_BODY (stmt) = block;
13073 SET_EXPR_LOCATION (stmt, loc);
13074
13075 return add_stmt (stmt);
13076 }
13077
13078 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13079
13080 tree
c_begin_omp_task(void)13081 c_begin_omp_task (void)
13082 {
13083 tree block;
13084
13085 keep_next_level ();
13086 block = c_begin_compound_stmt (true);
13087
13088 return block;
13089 }
13090
13091 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13092 statement. LOC is the location of the #pragma. */
13093
13094 tree
c_finish_omp_task(location_t loc,tree clauses,tree block)13095 c_finish_omp_task (location_t loc, tree clauses, tree block)
13096 {
13097 tree stmt;
13098
13099 block = c_end_compound_stmt (loc, block, true);
13100
13101 stmt = make_node (OMP_TASK);
13102 TREE_TYPE (stmt) = void_type_node;
13103 OMP_TASK_CLAUSES (stmt) = clauses;
13104 OMP_TASK_BODY (stmt) = block;
13105 SET_EXPR_LOCATION (stmt, loc);
13106
13107 return add_stmt (stmt);
13108 }
13109
13110 /* Generate GOMP_cancel call for #pragma omp cancel. */
13111
13112 void
c_finish_omp_cancel(location_t loc,tree clauses)13113 c_finish_omp_cancel (location_t loc, tree clauses)
13114 {
13115 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13116 int mask = 0;
13117 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13118 mask = 1;
13119 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13120 mask = 2;
13121 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13122 mask = 4;
13123 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13124 mask = 8;
13125 else
13126 {
13127 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13128 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13129 "clauses");
13130 return;
13131 }
13132 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13133 if (ifc != NULL_TREE)
13134 {
13135 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13136 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13137 error_at (OMP_CLAUSE_LOCATION (ifc),
13138 "expected %<cancel%> %<if%> clause modifier");
13139 else
13140 {
13141 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13142 if (ifc2 != NULL_TREE)
13143 {
13144 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13145 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13146 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13147 error_at (OMP_CLAUSE_LOCATION (ifc2),
13148 "expected %<cancel%> %<if%> clause modifier");
13149 }
13150 }
13151
13152 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13153 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13154 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13155 build_zero_cst (type));
13156 }
13157 else
13158 ifc = boolean_true_node;
13159 tree stmt = build_call_expr_loc (loc, fn, 2,
13160 build_int_cst (integer_type_node, mask),
13161 ifc);
13162 add_stmt (stmt);
13163 }
13164
13165 /* Generate GOMP_cancellation_point call for
13166 #pragma omp cancellation point. */
13167
13168 void
c_finish_omp_cancellation_point(location_t loc,tree clauses)13169 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13170 {
13171 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13172 int mask = 0;
13173 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13174 mask = 1;
13175 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13176 mask = 2;
13177 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13178 mask = 4;
13179 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13180 mask = 8;
13181 else
13182 {
13183 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13184 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13185 "clauses");
13186 return;
13187 }
13188 tree stmt = build_call_expr_loc (loc, fn, 1,
13189 build_int_cst (integer_type_node, mask));
13190 add_stmt (stmt);
13191 }
13192
13193 /* Helper function for handle_omp_array_sections. Called recursively
13194 to handle multiple array-section-subscripts. C is the clause,
13195 T current expression (initially OMP_CLAUSE_DECL), which is either
13196 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13197 expression if specified, TREE_VALUE length expression if specified,
13198 TREE_CHAIN is what it has been specified after, or some decl.
13199 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13200 set to true if any of the array-section-subscript could have length
13201 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13202 first array-section-subscript which is known not to have length
13203 of one. Given say:
13204 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13205 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13206 all are or may have length of 1, array-section-subscript [:2] is the
13207 first one known not to have length 1. For array-section-subscript
13208 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13209 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13210 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13211 case though, as some lengths could be zero. */
13212
13213 static tree
handle_omp_array_sections_1(tree c,tree t,vec<tree> & types,bool & maybe_zero_len,unsigned int & first_non_one,enum c_omp_region_type ort)13214 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13215 bool &maybe_zero_len, unsigned int &first_non_one,
13216 enum c_omp_region_type ort)
13217 {
13218 tree ret, low_bound, length, type;
13219 if (TREE_CODE (t) != TREE_LIST)
13220 {
13221 if (error_operand_p (t))
13222 return error_mark_node;
13223 ret = t;
13224 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13225 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13226 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13227 {
13228 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13229 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13230 return error_mark_node;
13231 }
13232 while (TREE_CODE (t) == INDIRECT_REF)
13233 {
13234 t = TREE_OPERAND (t, 0);
13235 STRIP_NOPS (t);
13236 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13237 t = TREE_OPERAND (t, 0);
13238 }
13239 while (TREE_CODE (t) == COMPOUND_EXPR)
13240 {
13241 t = TREE_OPERAND (t, 1);
13242 STRIP_NOPS (t);
13243 }
13244 if (TREE_CODE (t) == COMPONENT_REF
13245 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13246 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13247 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13248 {
13249 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13250 {
13251 error_at (OMP_CLAUSE_LOCATION (c),
13252 "bit-field %qE in %qs clause",
13253 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13254 return error_mark_node;
13255 }
13256 while (TREE_CODE (t) == COMPONENT_REF)
13257 {
13258 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13259 {
13260 error_at (OMP_CLAUSE_LOCATION (c),
13261 "%qE is a member of a union", t);
13262 return error_mark_node;
13263 }
13264 t = TREE_OPERAND (t, 0);
13265 while (TREE_CODE (t) == MEM_REF
13266 || TREE_CODE (t) == INDIRECT_REF
13267 || TREE_CODE (t) == ARRAY_REF)
13268 {
13269 t = TREE_OPERAND (t, 0);
13270 STRIP_NOPS (t);
13271 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13272 t = TREE_OPERAND (t, 0);
13273 }
13274 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13275 {
13276 if (maybe_ne (mem_ref_offset (t), 0))
13277 error_at (OMP_CLAUSE_LOCATION (c),
13278 "cannot dereference %qE in %qs clause", t,
13279 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13280 else
13281 t = TREE_OPERAND (t, 0);
13282 }
13283 }
13284 }
13285 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13286 {
13287 if (DECL_P (t))
13288 error_at (OMP_CLAUSE_LOCATION (c),
13289 "%qD is not a variable in %qs clause", t,
13290 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13291 else
13292 error_at (OMP_CLAUSE_LOCATION (c),
13293 "%qE is not a variable in %qs clause", t,
13294 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13295 return error_mark_node;
13296 }
13297 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13298 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13299 && TYPE_ATOMIC (TREE_TYPE (t)))
13300 {
13301 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13302 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13303 return error_mark_node;
13304 }
13305 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13306 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13307 && VAR_P (t)
13308 && DECL_THREAD_LOCAL_P (t))
13309 {
13310 error_at (OMP_CLAUSE_LOCATION (c),
13311 "%qD is threadprivate variable in %qs clause", t,
13312 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13313 return error_mark_node;
13314 }
13315 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13316 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13317 && TYPE_ATOMIC (TREE_TYPE (t))
13318 && POINTER_TYPE_P (TREE_TYPE (t)))
13319 {
13320 /* If the array section is pointer based and the pointer
13321 itself is _Atomic qualified, we need to atomically load
13322 the pointer. */
13323 c_expr expr;
13324 memset (&expr, 0, sizeof (expr));
13325 expr.value = ret;
13326 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13327 expr, false, false);
13328 ret = expr.value;
13329 }
13330 return ret;
13331 }
13332
13333 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13334 maybe_zero_len, first_non_one, ort);
13335 if (ret == error_mark_node || ret == NULL_TREE)
13336 return ret;
13337
13338 type = TREE_TYPE (ret);
13339 low_bound = TREE_PURPOSE (t);
13340 length = TREE_VALUE (t);
13341
13342 if (low_bound == error_mark_node || length == error_mark_node)
13343 return error_mark_node;
13344
13345 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13346 {
13347 error_at (OMP_CLAUSE_LOCATION (c),
13348 "low bound %qE of array section does not have integral type",
13349 low_bound);
13350 return error_mark_node;
13351 }
13352 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13353 {
13354 error_at (OMP_CLAUSE_LOCATION (c),
13355 "length %qE of array section does not have integral type",
13356 length);
13357 return error_mark_node;
13358 }
13359 if (low_bound
13360 && TREE_CODE (low_bound) == INTEGER_CST
13361 && TYPE_PRECISION (TREE_TYPE (low_bound))
13362 > TYPE_PRECISION (sizetype))
13363 low_bound = fold_convert (sizetype, low_bound);
13364 if (length
13365 && TREE_CODE (length) == INTEGER_CST
13366 && TYPE_PRECISION (TREE_TYPE (length))
13367 > TYPE_PRECISION (sizetype))
13368 length = fold_convert (sizetype, length);
13369 if (low_bound == NULL_TREE)
13370 low_bound = integer_zero_node;
13371 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13372 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13373 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13374 {
13375 if (length != integer_one_node)
13376 {
13377 error_at (OMP_CLAUSE_LOCATION (c),
13378 "expected single pointer in %qs clause",
13379 user_omp_clause_code_name (c, ort == C_ORT_ACC));
13380 return error_mark_node;
13381 }
13382 }
13383 if (length != NULL_TREE)
13384 {
13385 if (!integer_nonzerop (length))
13386 {
13387 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13388 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13389 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13390 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13391 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13392 {
13393 if (integer_zerop (length))
13394 {
13395 error_at (OMP_CLAUSE_LOCATION (c),
13396 "zero length array section in %qs clause",
13397 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13398 return error_mark_node;
13399 }
13400 }
13401 else
13402 maybe_zero_len = true;
13403 }
13404 if (first_non_one == types.length ()
13405 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13406 first_non_one++;
13407 }
13408 if (TREE_CODE (type) == ARRAY_TYPE)
13409 {
13410 if (length == NULL_TREE
13411 && (TYPE_DOMAIN (type) == NULL_TREE
13412 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13413 {
13414 error_at (OMP_CLAUSE_LOCATION (c),
13415 "for unknown bound array type length expression must "
13416 "be specified");
13417 return error_mark_node;
13418 }
13419 if (TREE_CODE (low_bound) == INTEGER_CST
13420 && tree_int_cst_sgn (low_bound) == -1)
13421 {
13422 error_at (OMP_CLAUSE_LOCATION (c),
13423 "negative low bound in array section in %qs clause",
13424 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13425 return error_mark_node;
13426 }
13427 if (length != NULL_TREE
13428 && TREE_CODE (length) == INTEGER_CST
13429 && tree_int_cst_sgn (length) == -1)
13430 {
13431 error_at (OMP_CLAUSE_LOCATION (c),
13432 "negative length in array section in %qs clause",
13433 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13434 return error_mark_node;
13435 }
13436 if (TYPE_DOMAIN (type)
13437 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13438 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13439 == INTEGER_CST)
13440 {
13441 tree size
13442 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13443 size = size_binop (PLUS_EXPR, size, size_one_node);
13444 if (TREE_CODE (low_bound) == INTEGER_CST)
13445 {
13446 if (tree_int_cst_lt (size, low_bound))
13447 {
13448 error_at (OMP_CLAUSE_LOCATION (c),
13449 "low bound %qE above array section size "
13450 "in %qs clause", low_bound,
13451 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13452 return error_mark_node;
13453 }
13454 if (tree_int_cst_equal (size, low_bound))
13455 {
13456 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13457 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13458 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13459 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13460 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13461 {
13462 error_at (OMP_CLAUSE_LOCATION (c),
13463 "zero length array section in %qs clause",
13464 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13465 return error_mark_node;
13466 }
13467 maybe_zero_len = true;
13468 }
13469 else if (length == NULL_TREE
13470 && first_non_one == types.length ()
13471 && tree_int_cst_equal
13472 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13473 low_bound))
13474 first_non_one++;
13475 }
13476 else if (length == NULL_TREE)
13477 {
13478 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13479 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13480 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13481 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13482 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13483 maybe_zero_len = true;
13484 if (first_non_one == types.length ())
13485 first_non_one++;
13486 }
13487 if (length && TREE_CODE (length) == INTEGER_CST)
13488 {
13489 if (tree_int_cst_lt (size, length))
13490 {
13491 error_at (OMP_CLAUSE_LOCATION (c),
13492 "length %qE above array section size "
13493 "in %qs clause", length,
13494 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13495 return error_mark_node;
13496 }
13497 if (TREE_CODE (low_bound) == INTEGER_CST)
13498 {
13499 tree lbpluslen
13500 = size_binop (PLUS_EXPR,
13501 fold_convert (sizetype, low_bound),
13502 fold_convert (sizetype, length));
13503 if (TREE_CODE (lbpluslen) == INTEGER_CST
13504 && tree_int_cst_lt (size, lbpluslen))
13505 {
13506 error_at (OMP_CLAUSE_LOCATION (c),
13507 "high bound %qE above array section size "
13508 "in %qs clause", lbpluslen,
13509 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13510 return error_mark_node;
13511 }
13512 }
13513 }
13514 }
13515 else if (length == NULL_TREE)
13516 {
13517 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13518 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13519 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13520 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13521 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13522 maybe_zero_len = true;
13523 if (first_non_one == types.length ())
13524 first_non_one++;
13525 }
13526
13527 /* For [lb:] we will need to evaluate lb more than once. */
13528 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13529 {
13530 tree lb = save_expr (low_bound);
13531 if (lb != low_bound)
13532 {
13533 TREE_PURPOSE (t) = lb;
13534 low_bound = lb;
13535 }
13536 }
13537 }
13538 else if (TREE_CODE (type) == POINTER_TYPE)
13539 {
13540 if (length == NULL_TREE)
13541 {
13542 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13543 error_at (OMP_CLAUSE_LOCATION (c),
13544 "for array function parameter length expression "
13545 "must be specified");
13546 else
13547 error_at (OMP_CLAUSE_LOCATION (c),
13548 "for pointer type length expression must be specified");
13549 return error_mark_node;
13550 }
13551 if (length != NULL_TREE
13552 && TREE_CODE (length) == INTEGER_CST
13553 && tree_int_cst_sgn (length) == -1)
13554 {
13555 error_at (OMP_CLAUSE_LOCATION (c),
13556 "negative length in array section in %qs clause",
13557 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13558 return error_mark_node;
13559 }
13560 /* If there is a pointer type anywhere but in the very first
13561 array-section-subscript, the array section could be non-contiguous. */
13562 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13563 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13564 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13565 {
13566 /* If any prior dimension has a non-one length, then deem this
13567 array section as non-contiguous. */
13568 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
13569 d = TREE_CHAIN (d))
13570 {
13571 tree d_length = TREE_VALUE (d);
13572 if (d_length == NULL_TREE || !integer_onep (d_length))
13573 {
13574 error_at (OMP_CLAUSE_LOCATION (c),
13575 "array section is not contiguous in %qs clause",
13576 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13577 return error_mark_node;
13578 }
13579 }
13580 }
13581 }
13582 else
13583 {
13584 error_at (OMP_CLAUSE_LOCATION (c),
13585 "%qE does not have pointer or array type", ret);
13586 return error_mark_node;
13587 }
13588 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13589 types.safe_push (TREE_TYPE (ret));
13590 /* We will need to evaluate lb more than once. */
13591 tree lb = save_expr (low_bound);
13592 if (lb != low_bound)
13593 {
13594 TREE_PURPOSE (t) = lb;
13595 low_bound = lb;
13596 }
13597 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13598 return ret;
13599 }
13600
13601 /* Handle array sections for clause C. */
13602
13603 static bool
handle_omp_array_sections(tree c,enum c_omp_region_type ort)13604 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13605 {
13606 bool maybe_zero_len = false;
13607 unsigned int first_non_one = 0;
13608 auto_vec<tree, 10> types;
13609 tree *tp = &OMP_CLAUSE_DECL (c);
13610 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13611 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13612 && TREE_CODE (*tp) == TREE_LIST
13613 && TREE_PURPOSE (*tp)
13614 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13615 tp = &TREE_VALUE (*tp);
13616 tree first = handle_omp_array_sections_1 (c, *tp, types,
13617 maybe_zero_len, first_non_one,
13618 ort);
13619 if (first == error_mark_node)
13620 return true;
13621 if (first == NULL_TREE)
13622 return false;
13623 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13624 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13625 {
13626 tree t = *tp;
13627 tree tem = NULL_TREE;
13628 /* Need to evaluate side effects in the length expressions
13629 if any. */
13630 while (TREE_CODE (t) == TREE_LIST)
13631 {
13632 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13633 {
13634 if (tem == NULL_TREE)
13635 tem = TREE_VALUE (t);
13636 else
13637 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13638 TREE_VALUE (t), tem);
13639 }
13640 t = TREE_CHAIN (t);
13641 }
13642 if (tem)
13643 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13644 first = c_fully_fold (first, false, NULL, true);
13645 *tp = first;
13646 }
13647 else
13648 {
13649 unsigned int num = types.length (), i;
13650 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13651 tree condition = NULL_TREE;
13652
13653 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13654 maybe_zero_len = true;
13655
13656 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13657 t = TREE_CHAIN (t))
13658 {
13659 tree low_bound = TREE_PURPOSE (t);
13660 tree length = TREE_VALUE (t);
13661
13662 i--;
13663 if (low_bound
13664 && TREE_CODE (low_bound) == INTEGER_CST
13665 && TYPE_PRECISION (TREE_TYPE (low_bound))
13666 > TYPE_PRECISION (sizetype))
13667 low_bound = fold_convert (sizetype, low_bound);
13668 if (length
13669 && TREE_CODE (length) == INTEGER_CST
13670 && TYPE_PRECISION (TREE_TYPE (length))
13671 > TYPE_PRECISION (sizetype))
13672 length = fold_convert (sizetype, length);
13673 if (low_bound == NULL_TREE)
13674 low_bound = integer_zero_node;
13675 if (!maybe_zero_len && i > first_non_one)
13676 {
13677 if (integer_nonzerop (low_bound))
13678 goto do_warn_noncontiguous;
13679 if (length != NULL_TREE
13680 && TREE_CODE (length) == INTEGER_CST
13681 && TYPE_DOMAIN (types[i])
13682 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13683 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13684 == INTEGER_CST)
13685 {
13686 tree size;
13687 size = size_binop (PLUS_EXPR,
13688 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13689 size_one_node);
13690 if (!tree_int_cst_equal (length, size))
13691 {
13692 do_warn_noncontiguous:
13693 error_at (OMP_CLAUSE_LOCATION (c),
13694 "array section is not contiguous in %qs "
13695 "clause",
13696 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13697 return true;
13698 }
13699 }
13700 if (length != NULL_TREE
13701 && TREE_SIDE_EFFECTS (length))
13702 {
13703 if (side_effects == NULL_TREE)
13704 side_effects = length;
13705 else
13706 side_effects = build2 (COMPOUND_EXPR,
13707 TREE_TYPE (side_effects),
13708 length, side_effects);
13709 }
13710 }
13711 else
13712 {
13713 tree l;
13714
13715 if (i > first_non_one
13716 && ((length && integer_nonzerop (length))
13717 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13718 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13719 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13720 continue;
13721 if (length)
13722 l = fold_convert (sizetype, length);
13723 else
13724 {
13725 l = size_binop (PLUS_EXPR,
13726 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13727 size_one_node);
13728 l = size_binop (MINUS_EXPR, l,
13729 fold_convert (sizetype, low_bound));
13730 }
13731 if (i > first_non_one)
13732 {
13733 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13734 size_zero_node);
13735 if (condition == NULL_TREE)
13736 condition = l;
13737 else
13738 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13739 l, condition);
13740 }
13741 else if (size == NULL_TREE)
13742 {
13743 size = size_in_bytes (TREE_TYPE (types[i]));
13744 tree eltype = TREE_TYPE (types[num - 1]);
13745 while (TREE_CODE (eltype) == ARRAY_TYPE)
13746 eltype = TREE_TYPE (eltype);
13747 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13748 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13749 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13750 {
13751 if (integer_zerop (size)
13752 || integer_zerop (size_in_bytes (eltype)))
13753 {
13754 error_at (OMP_CLAUSE_LOCATION (c),
13755 "zero length array section in %qs clause",
13756 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13757 return error_mark_node;
13758 }
13759 size = size_binop (EXACT_DIV_EXPR, size,
13760 size_in_bytes (eltype));
13761 }
13762 size = size_binop (MULT_EXPR, size, l);
13763 if (condition)
13764 size = fold_build3 (COND_EXPR, sizetype, condition,
13765 size, size_zero_node);
13766 }
13767 else
13768 size = size_binop (MULT_EXPR, size, l);
13769 }
13770 }
13771 if (side_effects)
13772 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13773 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13774 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13775 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13776 {
13777 size = size_binop (MINUS_EXPR, size, size_one_node);
13778 size = c_fully_fold (size, false, NULL);
13779 size = save_expr (size);
13780 tree index_type = build_index_type (size);
13781 tree eltype = TREE_TYPE (first);
13782 while (TREE_CODE (eltype) == ARRAY_TYPE)
13783 eltype = TREE_TYPE (eltype);
13784 tree type = build_array_type (eltype, index_type);
13785 tree ptype = build_pointer_type (eltype);
13786 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13787 t = build_fold_addr_expr (t);
13788 tree t2 = build_fold_addr_expr (first);
13789 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13790 ptrdiff_type_node, t2);
13791 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13792 ptrdiff_type_node, t2,
13793 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13794 ptrdiff_type_node, t));
13795 t2 = c_fully_fold (t2, false, NULL);
13796 if (tree_fits_shwi_p (t2))
13797 t = build2 (MEM_REF, type, t,
13798 build_int_cst (ptype, tree_to_shwi (t2)));
13799 else
13800 {
13801 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13802 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13803 TREE_TYPE (t), t, t2);
13804 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13805 }
13806 OMP_CLAUSE_DECL (c) = t;
13807 return false;
13808 }
13809 first = c_fully_fold (first, false, NULL);
13810 OMP_CLAUSE_DECL (c) = first;
13811 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13812 return false;
13813 if (size)
13814 size = c_fully_fold (size, false, NULL);
13815 OMP_CLAUSE_SIZE (c) = size;
13816 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13817 || (TREE_CODE (t) == COMPONENT_REF
13818 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13819 return false;
13820 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13821 switch (OMP_CLAUSE_MAP_KIND (c))
13822 {
13823 case GOMP_MAP_ALLOC:
13824 case GOMP_MAP_IF_PRESENT:
13825 case GOMP_MAP_TO:
13826 case GOMP_MAP_FROM:
13827 case GOMP_MAP_TOFROM:
13828 case GOMP_MAP_ALWAYS_TO:
13829 case GOMP_MAP_ALWAYS_FROM:
13830 case GOMP_MAP_ALWAYS_TOFROM:
13831 case GOMP_MAP_RELEASE:
13832 case GOMP_MAP_DELETE:
13833 case GOMP_MAP_FORCE_TO:
13834 case GOMP_MAP_FORCE_FROM:
13835 case GOMP_MAP_FORCE_TOFROM:
13836 case GOMP_MAP_FORCE_PRESENT:
13837 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13838 break;
13839 default:
13840 break;
13841 }
13842 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13843 if (TREE_CODE (t) == COMPONENT_REF)
13844 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
13845 else
13846 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13847 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
13848 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13849 && !c_mark_addressable (t))
13850 return false;
13851 OMP_CLAUSE_DECL (c2) = t;
13852 t = build_fold_addr_expr (first);
13853 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13854 tree ptr = OMP_CLAUSE_DECL (c2);
13855 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13856 ptr = build_fold_addr_expr (ptr);
13857 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13858 ptrdiff_type_node, t,
13859 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13860 ptrdiff_type_node, ptr));
13861 t = c_fully_fold (t, false, NULL);
13862 OMP_CLAUSE_SIZE (c2) = t;
13863 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13864 OMP_CLAUSE_CHAIN (c) = c2;
13865 }
13866 return false;
13867 }
13868
13869 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13870 an inline call. But, remap
13871 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13872 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13873
13874 static tree
c_clone_omp_udr(tree stmt,tree omp_decl1,tree omp_decl2,tree decl,tree placeholder)13875 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13876 tree decl, tree placeholder)
13877 {
13878 copy_body_data id;
13879 hash_map<tree, tree> decl_map;
13880
13881 decl_map.put (omp_decl1, placeholder);
13882 decl_map.put (omp_decl2, decl);
13883 memset (&id, 0, sizeof (id));
13884 id.src_fn = DECL_CONTEXT (omp_decl1);
13885 id.dst_fn = current_function_decl;
13886 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13887 id.decl_map = &decl_map;
13888
13889 id.copy_decl = copy_decl_no_change;
13890 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13891 id.transform_new_cfg = true;
13892 id.transform_return_to_modify = false;
13893 id.eh_lp_nr = 0;
13894 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13895 return stmt;
13896 }
13897
13898 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13899 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13900
13901 static tree
c_find_omp_placeholder_r(tree * tp,int *,void * data)13902 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13903 {
13904 if (*tp == (tree) data)
13905 return *tp;
13906 return NULL_TREE;
13907 }
13908
13909 /* Similarly, but also walk aggregate fields. */
13910
13911 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13912
13913 static tree
c_find_omp_var_r(tree * tp,int *,void * data)13914 c_find_omp_var_r (tree *tp, int *, void *data)
13915 {
13916 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13917 return *tp;
13918 if (RECORD_OR_UNION_TYPE_P (*tp))
13919 {
13920 tree field;
13921 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13922
13923 for (field = TYPE_FIELDS (*tp); field;
13924 field = DECL_CHAIN (field))
13925 if (TREE_CODE (field) == FIELD_DECL)
13926 {
13927 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13928 c_find_omp_var_r, data, pset);
13929 if (ret)
13930 return ret;
13931 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13932 if (ret)
13933 return ret;
13934 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13935 pset);
13936 if (ret)
13937 return ret;
13938 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13939 if (ret)
13940 return ret;
13941 }
13942 }
13943 else if (INTEGRAL_TYPE_P (*tp))
13944 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13945 ((struct c_find_omp_var_s *) data)->pset);
13946 return NULL_TREE;
13947 }
13948
13949 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13950 and clauses containing them should be removed. */
13951
13952 static bool
c_omp_finish_iterators(tree iter)13953 c_omp_finish_iterators (tree iter)
13954 {
13955 bool ret = false;
13956 for (tree it = iter; it; it = TREE_CHAIN (it))
13957 {
13958 tree var = TREE_VEC_ELT (it, 0);
13959 tree begin = TREE_VEC_ELT (it, 1);
13960 tree end = TREE_VEC_ELT (it, 2);
13961 tree step = TREE_VEC_ELT (it, 3);
13962 tree orig_step;
13963 tree type = TREE_TYPE (var);
13964 location_t loc = DECL_SOURCE_LOCATION (var);
13965 if (type == error_mark_node)
13966 {
13967 ret = true;
13968 continue;
13969 }
13970 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13971 {
13972 error_at (loc, "iterator %qD has neither integral nor pointer type",
13973 var);
13974 ret = true;
13975 continue;
13976 }
13977 else if (TYPE_ATOMIC (type))
13978 {
13979 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13980 ret = true;
13981 continue;
13982 }
13983 else if (TYPE_READONLY (type))
13984 {
13985 error_at (loc, "iterator %qD has const qualified type", var);
13986 ret = true;
13987 continue;
13988 }
13989 else if (step == error_mark_node
13990 || TREE_TYPE (step) == error_mark_node)
13991 {
13992 ret = true;
13993 continue;
13994 }
13995 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13996 {
13997 error_at (EXPR_LOC_OR_LOC (step, loc),
13998 "iterator step with non-integral type");
13999 ret = true;
14000 continue;
14001 }
14002 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14003 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14004 orig_step = save_expr (c_fully_fold (step, false, NULL));
14005 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14006 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14007 if (POINTER_TYPE_P (type))
14008 {
14009 begin = save_expr (begin);
14010 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14011 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14012 fold_convert (sizetype, step),
14013 fold_convert (sizetype, begin));
14014 step = fold_convert (ssizetype, step);
14015 }
14016 if (integer_zerop (step))
14017 {
14018 error_at (loc, "iterator %qD has zero step", var);
14019 ret = true;
14020 continue;
14021 }
14022
14023 if (begin == error_mark_node
14024 || end == error_mark_node
14025 || step == error_mark_node
14026 || orig_step == error_mark_node)
14027 {
14028 ret = true;
14029 continue;
14030 }
14031 hash_set<tree> pset;
14032 tree it2;
14033 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14034 {
14035 tree var2 = TREE_VEC_ELT (it2, 0);
14036 tree begin2 = TREE_VEC_ELT (it2, 1);
14037 tree end2 = TREE_VEC_ELT (it2, 2);
14038 tree step2 = TREE_VEC_ELT (it2, 3);
14039 tree type2 = TREE_TYPE (var2);
14040 location_t loc2 = DECL_SOURCE_LOCATION (var2);
14041 struct c_find_omp_var_s data = { var, &pset };
14042 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14043 {
14044 error_at (loc2,
14045 "type of iterator %qD refers to outer iterator %qD",
14046 var2, var);
14047 break;
14048 }
14049 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14050 {
14051 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14052 "begin expression refers to outer iterator %qD", var);
14053 break;
14054 }
14055 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14056 {
14057 error_at (EXPR_LOC_OR_LOC (end2, loc2),
14058 "end expression refers to outer iterator %qD", var);
14059 break;
14060 }
14061 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14062 {
14063 error_at (EXPR_LOC_OR_LOC (step2, loc2),
14064 "step expression refers to outer iterator %qD", var);
14065 break;
14066 }
14067 }
14068 if (it2)
14069 {
14070 ret = true;
14071 continue;
14072 }
14073 TREE_VEC_ELT (it, 1) = begin;
14074 TREE_VEC_ELT (it, 2) = end;
14075 TREE_VEC_ELT (it, 3) = step;
14076 TREE_VEC_ELT (it, 4) = orig_step;
14077 }
14078 return ret;
14079 }
14080
14081 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14082 Return true if an error has been detected. */
14083
14084 static bool
c_oacc_check_attachments(tree c)14085 c_oacc_check_attachments (tree c)
14086 {
14087 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14088 return false;
14089
14090 /* OpenACC attach / detach clauses must be pointers. */
14091 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14092 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14093 {
14094 tree t = OMP_CLAUSE_DECL (c);
14095
14096 while (TREE_CODE (t) == TREE_LIST)
14097 t = TREE_CHAIN (t);
14098
14099 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14100 {
14101 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14102 user_omp_clause_code_name (c, true));
14103 return true;
14104 }
14105 }
14106
14107 return false;
14108 }
14109
14110 /* For all elements of CLAUSES, validate them against their constraints.
14111 Remove any elements from the list that are invalid. */
14112
14113 tree
c_finish_omp_clauses(tree clauses,enum c_omp_region_type ort)14114 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14115 {
14116 bitmap_head generic_head, firstprivate_head, lastprivate_head;
14117 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14118 bitmap_head oacc_reduction_head, is_on_device_head;
14119 tree c, t, type, *pc;
14120 tree simdlen = NULL_TREE, safelen = NULL_TREE;
14121 bool branch_seen = false;
14122 bool copyprivate_seen = false;
14123 bool mergeable_seen = false;
14124 tree *detach_seen = NULL;
14125 bool linear_variable_step_check = false;
14126 tree *nowait_clause = NULL;
14127 tree ordered_clause = NULL_TREE;
14128 tree schedule_clause = NULL_TREE;
14129 bool oacc_async = false;
14130 bool indir_component_ref_p = false;
14131 tree last_iterators = NULL_TREE;
14132 bool last_iterators_remove = false;
14133 tree *nogroup_seen = NULL;
14134 tree *order_clause = NULL;
14135 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14136 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14137 int reduction_seen = 0;
14138 bool allocate_seen = false;
14139 bool implicit_moved = false;
14140 bool target_in_reduction_seen = false;
14141
14142 bitmap_obstack_initialize (NULL);
14143 bitmap_initialize (&generic_head, &bitmap_default_obstack);
14144 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14145 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14146 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14147 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14148 bitmap_initialize (&map_head, &bitmap_default_obstack);
14149 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14150 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14151 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14152 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14153 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14154 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14155
14156 if (ort & C_ORT_ACC)
14157 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14158 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14159 {
14160 oacc_async = true;
14161 break;
14162 }
14163
14164 for (pc = &clauses, c = clauses; c ; c = *pc)
14165 {
14166 bool remove = false;
14167 bool need_complete = false;
14168 bool need_implicitly_determined = false;
14169
14170 switch (OMP_CLAUSE_CODE (c))
14171 {
14172 case OMP_CLAUSE_SHARED:
14173 need_implicitly_determined = true;
14174 goto check_dup_generic;
14175
14176 case OMP_CLAUSE_PRIVATE:
14177 need_complete = true;
14178 need_implicitly_determined = true;
14179 goto check_dup_generic;
14180
14181 case OMP_CLAUSE_REDUCTION:
14182 if (reduction_seen == 0)
14183 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14184 else if (reduction_seen != -2
14185 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14186 ? -1 : 1))
14187 {
14188 error_at (OMP_CLAUSE_LOCATION (c),
14189 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14190 "on the same construct");
14191 reduction_seen = -2;
14192 }
14193 /* FALLTHRU */
14194 case OMP_CLAUSE_IN_REDUCTION:
14195 case OMP_CLAUSE_TASK_REDUCTION:
14196 need_implicitly_determined = true;
14197 t = OMP_CLAUSE_DECL (c);
14198 if (TREE_CODE (t) == TREE_LIST)
14199 {
14200 if (handle_omp_array_sections (c, ort))
14201 {
14202 remove = true;
14203 break;
14204 }
14205
14206 t = OMP_CLAUSE_DECL (c);
14207 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14208 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14209 {
14210 error_at (OMP_CLAUSE_LOCATION (c),
14211 "%<inscan%> %<reduction%> clause with array "
14212 "section");
14213 remove = true;
14214 break;
14215 }
14216 }
14217 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14218 if (t == error_mark_node)
14219 {
14220 remove = true;
14221 break;
14222 }
14223 if (oacc_async)
14224 c_mark_addressable (t);
14225 type = TREE_TYPE (t);
14226 if (TREE_CODE (t) == MEM_REF)
14227 type = TREE_TYPE (type);
14228 if (TREE_CODE (type) == ARRAY_TYPE)
14229 {
14230 tree oatype = type;
14231 gcc_assert (TREE_CODE (t) != MEM_REF);
14232 while (TREE_CODE (type) == ARRAY_TYPE)
14233 type = TREE_TYPE (type);
14234 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14235 {
14236 error_at (OMP_CLAUSE_LOCATION (c),
14237 "%qD in %<reduction%> clause is a zero size array",
14238 t);
14239 remove = true;
14240 break;
14241 }
14242 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14243 TYPE_SIZE_UNIT (type));
14244 if (integer_zerop (size))
14245 {
14246 error_at (OMP_CLAUSE_LOCATION (c),
14247 "%qD in %<reduction%> clause is a zero size array",
14248 t);
14249 remove = true;
14250 break;
14251 }
14252 size = size_binop (MINUS_EXPR, size, size_one_node);
14253 size = save_expr (size);
14254 tree index_type = build_index_type (size);
14255 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14256 index_type);
14257 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14258 tree ptype = build_pointer_type (type);
14259 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14260 t = build_fold_addr_expr (t);
14261 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14262 OMP_CLAUSE_DECL (c) = t;
14263 }
14264 if (TYPE_ATOMIC (type))
14265 {
14266 error_at (OMP_CLAUSE_LOCATION (c),
14267 "%<_Atomic%> %qE in %<reduction%> clause", t);
14268 remove = true;
14269 break;
14270 }
14271 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14272 || OMP_CLAUSE_REDUCTION_TASK (c))
14273 {
14274 /* Disallow zero sized or potentially zero sized task
14275 reductions. */
14276 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14277 {
14278 error_at (OMP_CLAUSE_LOCATION (c),
14279 "zero sized type %qT in %qs clause", type,
14280 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14281 remove = true;
14282 break;
14283 }
14284 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14285 {
14286 error_at (OMP_CLAUSE_LOCATION (c),
14287 "variable sized type %qT in %qs clause", type,
14288 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14289 remove = true;
14290 break;
14291 }
14292 }
14293 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14294 && (FLOAT_TYPE_P (type)
14295 || TREE_CODE (type) == COMPLEX_TYPE))
14296 {
14297 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14298 const char *r_name = NULL;
14299
14300 switch (r_code)
14301 {
14302 case PLUS_EXPR:
14303 case MULT_EXPR:
14304 case MINUS_EXPR:
14305 case TRUTH_ANDIF_EXPR:
14306 case TRUTH_ORIF_EXPR:
14307 break;
14308 case MIN_EXPR:
14309 if (TREE_CODE (type) == COMPLEX_TYPE)
14310 r_name = "min";
14311 break;
14312 case MAX_EXPR:
14313 if (TREE_CODE (type) == COMPLEX_TYPE)
14314 r_name = "max";
14315 break;
14316 case BIT_AND_EXPR:
14317 r_name = "&";
14318 break;
14319 case BIT_XOR_EXPR:
14320 r_name = "^";
14321 break;
14322 case BIT_IOR_EXPR:
14323 r_name = "|";
14324 break;
14325 default:
14326 gcc_unreachable ();
14327 }
14328 if (r_name)
14329 {
14330 error_at (OMP_CLAUSE_LOCATION (c),
14331 "%qE has invalid type for %<reduction(%s)%>",
14332 t, r_name);
14333 remove = true;
14334 break;
14335 }
14336 }
14337 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14338 {
14339 error_at (OMP_CLAUSE_LOCATION (c),
14340 "user defined reduction not found for %qE", t);
14341 remove = true;
14342 break;
14343 }
14344 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14345 {
14346 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14347 type = TYPE_MAIN_VARIANT (type);
14348 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14349 VAR_DECL, NULL_TREE, type);
14350 tree decl_placeholder = NULL_TREE;
14351 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14352 DECL_ARTIFICIAL (placeholder) = 1;
14353 DECL_IGNORED_P (placeholder) = 1;
14354 if (TREE_CODE (t) == MEM_REF)
14355 {
14356 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14357 VAR_DECL, NULL_TREE, type);
14358 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14359 DECL_ARTIFICIAL (decl_placeholder) = 1;
14360 DECL_IGNORED_P (decl_placeholder) = 1;
14361 }
14362 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14363 c_mark_addressable (placeholder);
14364 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14365 c_mark_addressable (decl_placeholder ? decl_placeholder
14366 : OMP_CLAUSE_DECL (c));
14367 OMP_CLAUSE_REDUCTION_MERGE (c)
14368 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14369 TREE_VEC_ELT (list, 0),
14370 TREE_VEC_ELT (list, 1),
14371 decl_placeholder ? decl_placeholder
14372 : OMP_CLAUSE_DECL (c), placeholder);
14373 OMP_CLAUSE_REDUCTION_MERGE (c)
14374 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14375 void_type_node, NULL_TREE,
14376 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14377 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14378 if (TREE_VEC_LENGTH (list) == 6)
14379 {
14380 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14381 c_mark_addressable (decl_placeholder ? decl_placeholder
14382 : OMP_CLAUSE_DECL (c));
14383 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14384 c_mark_addressable (placeholder);
14385 tree init = TREE_VEC_ELT (list, 5);
14386 if (init == error_mark_node)
14387 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14388 OMP_CLAUSE_REDUCTION_INIT (c)
14389 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14390 TREE_VEC_ELT (list, 3),
14391 decl_placeholder ? decl_placeholder
14392 : OMP_CLAUSE_DECL (c), placeholder);
14393 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14394 {
14395 tree v = decl_placeholder ? decl_placeholder : t;
14396 OMP_CLAUSE_REDUCTION_INIT (c)
14397 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14398 OMP_CLAUSE_REDUCTION_INIT (c));
14399 }
14400 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14401 c_find_omp_placeholder_r,
14402 placeholder, NULL))
14403 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14404 }
14405 else
14406 {
14407 tree init;
14408 tree v = decl_placeholder ? decl_placeholder : t;
14409 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14410 init = build_constructor (TREE_TYPE (v), NULL);
14411 else
14412 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14413 OMP_CLAUSE_REDUCTION_INIT (c)
14414 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14415 }
14416 OMP_CLAUSE_REDUCTION_INIT (c)
14417 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14418 void_type_node, NULL_TREE,
14419 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14420 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14421 }
14422 if (TREE_CODE (t) == MEM_REF)
14423 {
14424 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14425 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14426 != INTEGER_CST)
14427 {
14428 sorry ("variable length element type in array "
14429 "%<reduction%> clause");
14430 remove = true;
14431 break;
14432 }
14433 t = TREE_OPERAND (t, 0);
14434 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14435 t = TREE_OPERAND (t, 0);
14436 if (TREE_CODE (t) == ADDR_EXPR)
14437 t = TREE_OPERAND (t, 0);
14438 }
14439 goto check_dup_generic_t;
14440
14441 case OMP_CLAUSE_COPYPRIVATE:
14442 copyprivate_seen = true;
14443 if (nowait_clause)
14444 {
14445 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14446 "%<nowait%> clause must not be used together "
14447 "with %<copyprivate%>");
14448 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14449 nowait_clause = NULL;
14450 }
14451 goto check_dup_generic;
14452
14453 case OMP_CLAUSE_COPYIN:
14454 t = OMP_CLAUSE_DECL (c);
14455 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14456 {
14457 error_at (OMP_CLAUSE_LOCATION (c),
14458 "%qE must be %<threadprivate%> for %<copyin%>", t);
14459 remove = true;
14460 break;
14461 }
14462 goto check_dup_generic;
14463
14464 case OMP_CLAUSE_LINEAR:
14465 if (ort != C_ORT_OMP_DECLARE_SIMD)
14466 need_implicitly_determined = true;
14467 t = OMP_CLAUSE_DECL (c);
14468 if (ort != C_ORT_OMP_DECLARE_SIMD
14469 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
14470 {
14471 error_at (OMP_CLAUSE_LOCATION (c),
14472 "modifier should not be specified in %<linear%> "
14473 "clause on %<simd%> or %<for%> constructs");
14474 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14475 }
14476 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14477 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14478 {
14479 error_at (OMP_CLAUSE_LOCATION (c),
14480 "linear clause applied to non-integral non-pointer "
14481 "variable with type %qT", TREE_TYPE (t));
14482 remove = true;
14483 break;
14484 }
14485 if (TYPE_ATOMIC (TREE_TYPE (t)))
14486 {
14487 error_at (OMP_CLAUSE_LOCATION (c),
14488 "%<_Atomic%> %qD in %<linear%> clause", t);
14489 remove = true;
14490 break;
14491 }
14492 if (ort == C_ORT_OMP_DECLARE_SIMD)
14493 {
14494 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14495 if (TREE_CODE (s) == PARM_DECL)
14496 {
14497 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14498 /* map_head bitmap is used as uniform_head if
14499 declare_simd. */
14500 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14501 linear_variable_step_check = true;
14502 goto check_dup_generic;
14503 }
14504 if (TREE_CODE (s) != INTEGER_CST)
14505 {
14506 error_at (OMP_CLAUSE_LOCATION (c),
14507 "%<linear%> clause step %qE is neither constant "
14508 "nor a parameter", s);
14509 remove = true;
14510 break;
14511 }
14512 }
14513 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14514 {
14515 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14516 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14517 OMP_CLAUSE_DECL (c), s);
14518 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14519 sizetype, fold_convert (sizetype, s),
14520 fold_convert
14521 (sizetype, OMP_CLAUSE_DECL (c)));
14522 if (s == error_mark_node)
14523 s = size_one_node;
14524 OMP_CLAUSE_LINEAR_STEP (c) = s;
14525 }
14526 else
14527 OMP_CLAUSE_LINEAR_STEP (c)
14528 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14529 goto check_dup_generic;
14530
14531 check_dup_generic:
14532 t = OMP_CLAUSE_DECL (c);
14533 check_dup_generic_t:
14534 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14535 {
14536 error_at (OMP_CLAUSE_LOCATION (c),
14537 "%qE is not a variable in clause %qs", t,
14538 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14539 remove = true;
14540 }
14541 else if ((ort == C_ORT_ACC
14542 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14543 || (ort == C_ORT_OMP
14544 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14545 || (OMP_CLAUSE_CODE (c)
14546 == OMP_CLAUSE_USE_DEVICE_ADDR)))
14547 || (ort == C_ORT_OMP_TARGET
14548 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14549 {
14550 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14551 && (bitmap_bit_p (&generic_head, DECL_UID (t))
14552 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14553 {
14554 error_at (OMP_CLAUSE_LOCATION (c),
14555 "%qD appears more than once in data-sharing "
14556 "clauses", t);
14557 remove = true;
14558 break;
14559 }
14560 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14561 target_in_reduction_seen = true;
14562 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14563 {
14564 error_at (OMP_CLAUSE_LOCATION (c),
14565 ort == C_ORT_ACC
14566 ? "%qD appears more than once in reduction clauses"
14567 : "%qD appears more than once in data clauses",
14568 t);
14569 remove = true;
14570 }
14571 else
14572 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14573 }
14574 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14575 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14576 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
14577 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14578 {
14579 error_at (OMP_CLAUSE_LOCATION (c),
14580 "%qE appears more than once in data clauses", t);
14581 remove = true;
14582 }
14583 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14584 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
14585 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
14586 && bitmap_bit_p (&map_head, DECL_UID (t)))
14587 {
14588 if (ort == C_ORT_ACC)
14589 error_at (OMP_CLAUSE_LOCATION (c),
14590 "%qD appears more than once in data clauses", t);
14591 else
14592 error_at (OMP_CLAUSE_LOCATION (c),
14593 "%qD appears both in data and map clauses", t);
14594 remove = true;
14595 }
14596 else
14597 bitmap_set_bit (&generic_head, DECL_UID (t));
14598 break;
14599
14600 case OMP_CLAUSE_FIRSTPRIVATE:
14601 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
14602 {
14603 move_implicit:
14604 implicit_moved = true;
14605 /* Move firstprivate and map clauses with
14606 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
14607 clauses chain. */
14608 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
14609 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
14610 while (*pc1)
14611 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
14612 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
14613 {
14614 *pc3 = *pc1;
14615 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
14616 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14617 }
14618 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
14619 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
14620 {
14621 *pc2 = *pc1;
14622 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
14623 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14624 }
14625 else
14626 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
14627 *pc3 = NULL;
14628 *pc2 = cl2;
14629 *pc1 = cl1;
14630 continue;
14631 }
14632 t = OMP_CLAUSE_DECL (c);
14633 need_complete = true;
14634 need_implicitly_determined = true;
14635 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14636 {
14637 error_at (OMP_CLAUSE_LOCATION (c),
14638 "%qE is not a variable in clause %<firstprivate%>", t);
14639 remove = true;
14640 }
14641 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14642 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
14643 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14644 remove = true;
14645 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14646 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14647 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14648 {
14649 error_at (OMP_CLAUSE_LOCATION (c),
14650 "%qE appears more than once in data clauses", t);
14651 remove = true;
14652 }
14653 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14654 {
14655 if (ort == C_ORT_ACC)
14656 error_at (OMP_CLAUSE_LOCATION (c),
14657 "%qD appears more than once in data clauses", t);
14658 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14659 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
14660 /* Silently drop the clause. */;
14661 else
14662 error_at (OMP_CLAUSE_LOCATION (c),
14663 "%qD appears both in data and map clauses", t);
14664 remove = true;
14665 }
14666 else
14667 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14668 break;
14669
14670 case OMP_CLAUSE_LASTPRIVATE:
14671 t = OMP_CLAUSE_DECL (c);
14672 need_complete = true;
14673 need_implicitly_determined = true;
14674 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14675 {
14676 error_at (OMP_CLAUSE_LOCATION (c),
14677 "%qE is not a variable in clause %<lastprivate%>", t);
14678 remove = true;
14679 }
14680 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14681 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14682 {
14683 error_at (OMP_CLAUSE_LOCATION (c),
14684 "%qE appears more than once in data clauses", t);
14685 remove = true;
14686 }
14687 else
14688 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14689 break;
14690
14691 case OMP_CLAUSE_ALIGNED:
14692 t = OMP_CLAUSE_DECL (c);
14693 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14694 {
14695 error_at (OMP_CLAUSE_LOCATION (c),
14696 "%qE is not a variable in %<aligned%> clause", t);
14697 remove = true;
14698 }
14699 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14700 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14701 {
14702 error_at (OMP_CLAUSE_LOCATION (c),
14703 "%qE in %<aligned%> clause is neither a pointer nor "
14704 "an array", t);
14705 remove = true;
14706 }
14707 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14708 {
14709 error_at (OMP_CLAUSE_LOCATION (c),
14710 "%<_Atomic%> %qD in %<aligned%> clause", t);
14711 remove = true;
14712 break;
14713 }
14714 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14715 {
14716 error_at (OMP_CLAUSE_LOCATION (c),
14717 "%qE appears more than once in %<aligned%> clauses",
14718 t);
14719 remove = true;
14720 }
14721 else
14722 bitmap_set_bit (&aligned_head, DECL_UID (t));
14723 break;
14724
14725 case OMP_CLAUSE_NONTEMPORAL:
14726 t = OMP_CLAUSE_DECL (c);
14727 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14728 {
14729 error_at (OMP_CLAUSE_LOCATION (c),
14730 "%qE is not a variable in %<nontemporal%> clause", t);
14731 remove = true;
14732 }
14733 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14734 {
14735 error_at (OMP_CLAUSE_LOCATION (c),
14736 "%qE appears more than once in %<nontemporal%> "
14737 "clauses", t);
14738 remove = true;
14739 }
14740 else
14741 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14742 break;
14743
14744 case OMP_CLAUSE_ALLOCATE:
14745 t = OMP_CLAUSE_DECL (c);
14746 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14747 {
14748 error_at (OMP_CLAUSE_LOCATION (c),
14749 "%qE is not a variable in %<allocate%> clause", t);
14750 remove = true;
14751 }
14752 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14753 {
14754 warning_at (OMP_CLAUSE_LOCATION (c), 0,
14755 "%qE appears more than once in %<allocate%> clauses",
14756 t);
14757 remove = true;
14758 }
14759 else
14760 {
14761 bitmap_set_bit (&aligned_head, DECL_UID (t));
14762 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
14763 allocate_seen = true;
14764 }
14765 break;
14766
14767 case OMP_CLAUSE_DEPEND:
14768 t = OMP_CLAUSE_DECL (c);
14769 if (t == NULL_TREE)
14770 {
14771 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14772 == OMP_CLAUSE_DEPEND_SOURCE);
14773 break;
14774 }
14775 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14776 {
14777 gcc_assert (TREE_CODE (t) == TREE_LIST);
14778 for (; t; t = TREE_CHAIN (t))
14779 {
14780 tree decl = TREE_VALUE (t);
14781 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14782 {
14783 tree offset = TREE_PURPOSE (t);
14784 bool neg = wi::neg_p (wi::to_wide (offset));
14785 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14786 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14787 neg ? MINUS_EXPR : PLUS_EXPR,
14788 decl, offset);
14789 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14790 sizetype,
14791 fold_convert (sizetype, t2),
14792 fold_convert (sizetype, decl));
14793 if (t2 == error_mark_node)
14794 {
14795 remove = true;
14796 break;
14797 }
14798 TREE_PURPOSE (t) = t2;
14799 }
14800 }
14801 break;
14802 }
14803 /* FALLTHRU */
14804 case OMP_CLAUSE_AFFINITY:
14805 t = OMP_CLAUSE_DECL (c);
14806 if (TREE_CODE (t) == TREE_LIST
14807 && TREE_PURPOSE (t)
14808 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14809 {
14810 if (TREE_PURPOSE (t) != last_iterators)
14811 last_iterators_remove
14812 = c_omp_finish_iterators (TREE_PURPOSE (t));
14813 last_iterators = TREE_PURPOSE (t);
14814 t = TREE_VALUE (t);
14815 if (last_iterators_remove)
14816 t = error_mark_node;
14817 }
14818 else
14819 last_iterators = NULL_TREE;
14820 if (TREE_CODE (t) == TREE_LIST)
14821 {
14822 if (handle_omp_array_sections (c, ort))
14823 remove = true;
14824 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14825 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14826 {
14827 error_at (OMP_CLAUSE_LOCATION (c),
14828 "%<depend%> clause with %<depobj%> dependence "
14829 "type on array section");
14830 remove = true;
14831 }
14832 break;
14833 }
14834 if (t == error_mark_node)
14835 remove = true;
14836 else if (!lvalue_p (t))
14837 {
14838 error_at (OMP_CLAUSE_LOCATION (c),
14839 "%qE is not lvalue expression nor array section in "
14840 "%qs clause", t,
14841 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14842 remove = true;
14843 }
14844 else if (TREE_CODE (t) == COMPONENT_REF
14845 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14846 {
14847 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14848 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
14849 error_at (OMP_CLAUSE_LOCATION (c),
14850 "bit-field %qE in %qs clause", t,
14851 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14852 remove = true;
14853 }
14854 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14855 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14856 {
14857 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14858 {
14859 error_at (OMP_CLAUSE_LOCATION (c),
14860 "%qE does not have %<omp_depend_t%> type in "
14861 "%<depend%> clause with %<depobj%> dependence "
14862 "type", t);
14863 remove = true;
14864 }
14865 }
14866 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14867 && c_omp_depend_t_p (TREE_TYPE (t)))
14868 {
14869 error_at (OMP_CLAUSE_LOCATION (c),
14870 "%qE should not have %<omp_depend_t%> type in "
14871 "%<depend%> clause with dependence type other than "
14872 "%<depobj%>", t);
14873 remove = true;
14874 }
14875 if (!remove)
14876 {
14877 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14878 t, false);
14879 if (addr == error_mark_node)
14880 remove = true;
14881 else
14882 {
14883 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14884 RO_UNARY_STAR);
14885 if (t == error_mark_node)
14886 remove = true;
14887 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14888 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14889 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14890 == TREE_VEC))
14891 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14892 else
14893 OMP_CLAUSE_DECL (c) = t;
14894 }
14895 }
14896 break;
14897
14898 case OMP_CLAUSE_MAP:
14899 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
14900 goto move_implicit;
14901 /* FALLTHRU */
14902 case OMP_CLAUSE_TO:
14903 case OMP_CLAUSE_FROM:
14904 case OMP_CLAUSE__CACHE_:
14905 t = OMP_CLAUSE_DECL (c);
14906 if (TREE_CODE (t) == TREE_LIST)
14907 {
14908 if (handle_omp_array_sections (c, ort))
14909 remove = true;
14910 else
14911 {
14912 t = OMP_CLAUSE_DECL (c);
14913 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14914 {
14915 error_at (OMP_CLAUSE_LOCATION (c),
14916 "array section does not have mappable type "
14917 "in %qs clause",
14918 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14919 remove = true;
14920 }
14921 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14922 {
14923 error_at (OMP_CLAUSE_LOCATION (c),
14924 "%<_Atomic%> %qE in %qs clause", t,
14925 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14926 remove = true;
14927 }
14928 while (TREE_CODE (t) == ARRAY_REF)
14929 t = TREE_OPERAND (t, 0);
14930 if (TREE_CODE (t) == COMPONENT_REF
14931 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14932 {
14933 do
14934 {
14935 t = TREE_OPERAND (t, 0);
14936 if (TREE_CODE (t) == MEM_REF
14937 || TREE_CODE (t) == INDIRECT_REF)
14938 {
14939 t = TREE_OPERAND (t, 0);
14940 STRIP_NOPS (t);
14941 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14942 t = TREE_OPERAND (t, 0);
14943 }
14944 }
14945 while (TREE_CODE (t) == COMPONENT_REF
14946 || TREE_CODE (t) == ARRAY_REF);
14947
14948 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14949 && OMP_CLAUSE_MAP_IMPLICIT (c)
14950 && (bitmap_bit_p (&map_head, DECL_UID (t))
14951 || bitmap_bit_p (&map_field_head, DECL_UID (t))
14952 || bitmap_bit_p (&map_firstprivate_head,
14953 DECL_UID (t))))
14954 {
14955 remove = true;
14956 break;
14957 }
14958 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14959 break;
14960 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14961 {
14962 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14963 error_at (OMP_CLAUSE_LOCATION (c),
14964 "%qD appears more than once in motion "
14965 "clauses", t);
14966 else if (ort == C_ORT_ACC)
14967 error_at (OMP_CLAUSE_LOCATION (c),
14968 "%qD appears more than once in data "
14969 "clauses", t);
14970 else
14971 error_at (OMP_CLAUSE_LOCATION (c),
14972 "%qD appears more than once in map "
14973 "clauses", t);
14974 remove = true;
14975 }
14976 else
14977 {
14978 bitmap_set_bit (&map_head, DECL_UID (t));
14979 bitmap_set_bit (&map_field_head, DECL_UID (t));
14980 }
14981 }
14982 }
14983 if (c_oacc_check_attachments (c))
14984 remove = true;
14985 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14986 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14987 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14988 /* In this case, we have a single array element which is a
14989 pointer, and we already set OMP_CLAUSE_SIZE in
14990 handle_omp_array_sections above. For attach/detach clauses,
14991 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
14992 here. */
14993 OMP_CLAUSE_SIZE (c) = size_zero_node;
14994 break;
14995 }
14996 if (t == error_mark_node)
14997 {
14998 remove = true;
14999 break;
15000 }
15001 /* OpenACC attach / detach clauses must be pointers. */
15002 if (c_oacc_check_attachments (c))
15003 {
15004 remove = true;
15005 break;
15006 }
15007 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15008 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15009 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15010 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15011 bias) to zero here, so it is not set erroneously to the pointer
15012 size later on in gimplify.cc. */
15013 OMP_CLAUSE_SIZE (c) = size_zero_node;
15014 while (TREE_CODE (t) == INDIRECT_REF
15015 || TREE_CODE (t) == ARRAY_REF)
15016 {
15017 t = TREE_OPERAND (t, 0);
15018 STRIP_NOPS (t);
15019 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15020 t = TREE_OPERAND (t, 0);
15021 }
15022 while (TREE_CODE (t) == COMPOUND_EXPR)
15023 {
15024 t = TREE_OPERAND (t, 1);
15025 STRIP_NOPS (t);
15026 }
15027 indir_component_ref_p = false;
15028 if (TREE_CODE (t) == COMPONENT_REF
15029 && (TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
15030 || TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF
15031 || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
15032 {
15033 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
15034 indir_component_ref_p = true;
15035 STRIP_NOPS (t);
15036 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15037 t = TREE_OPERAND (t, 0);
15038 }
15039
15040 if (TREE_CODE (t) == COMPONENT_REF
15041 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
15042 {
15043 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
15044 {
15045 error_at (OMP_CLAUSE_LOCATION (c),
15046 "bit-field %qE in %qs clause",
15047 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15048 remove = true;
15049 }
15050 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
15051 {
15052 error_at (OMP_CLAUSE_LOCATION (c),
15053 "%qE does not have a mappable type in %qs clause",
15054 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15055 remove = true;
15056 }
15057 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15058 {
15059 error_at (OMP_CLAUSE_LOCATION (c),
15060 "%<_Atomic%> %qE in %qs clause", t,
15061 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15062 remove = true;
15063 }
15064 while (TREE_CODE (t) == COMPONENT_REF)
15065 {
15066 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
15067 == UNION_TYPE)
15068 {
15069 error_at (OMP_CLAUSE_LOCATION (c),
15070 "%qE is a member of a union", t);
15071 remove = true;
15072 break;
15073 }
15074 t = TREE_OPERAND (t, 0);
15075 if (TREE_CODE (t) == MEM_REF)
15076 {
15077 if (maybe_ne (mem_ref_offset (t), 0))
15078 error_at (OMP_CLAUSE_LOCATION (c),
15079 "cannot dereference %qE in %qs clause", t,
15080 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15081 else
15082 t = TREE_OPERAND (t, 0);
15083 }
15084 while (TREE_CODE (t) == MEM_REF
15085 || TREE_CODE (t) == INDIRECT_REF
15086 || TREE_CODE (t) == ARRAY_REF)
15087 {
15088 t = TREE_OPERAND (t, 0);
15089 STRIP_NOPS (t);
15090 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15091 t = TREE_OPERAND (t, 0);
15092 }
15093 }
15094 if (remove)
15095 break;
15096 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15097 {
15098 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
15099 || (ort != C_ORT_ACC
15100 && bitmap_bit_p (&map_head, DECL_UID (t))))
15101 break;
15102 }
15103 }
15104 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15105 {
15106 error_at (OMP_CLAUSE_LOCATION (c),
15107 "%qE is not a variable in %qs clause", t,
15108 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15109 remove = true;
15110 }
15111 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15112 {
15113 error_at (OMP_CLAUSE_LOCATION (c),
15114 "%qD is threadprivate variable in %qs clause", t,
15115 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15116 remove = true;
15117 }
15118 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15119 || (OMP_CLAUSE_MAP_KIND (c)
15120 != GOMP_MAP_FIRSTPRIVATE_POINTER))
15121 && !indir_component_ref_p
15122 && !c_mark_addressable (t))
15123 remove = true;
15124 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15125 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15126 || (OMP_CLAUSE_MAP_KIND (c)
15127 == GOMP_MAP_FIRSTPRIVATE_POINTER)
15128 || (OMP_CLAUSE_MAP_KIND (c)
15129 == GOMP_MAP_FORCE_DEVICEPTR)))
15130 && t == OMP_CLAUSE_DECL (c)
15131 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
15132 {
15133 error_at (OMP_CLAUSE_LOCATION (c),
15134 "%qD does not have a mappable type in %qs clause", t,
15135 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15136 remove = true;
15137 }
15138 else if (TREE_TYPE (t) == error_mark_node)
15139 remove = true;
15140 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15141 {
15142 error_at (OMP_CLAUSE_LOCATION (c),
15143 "%<_Atomic%> %qE in %qs clause", t,
15144 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15145 remove = true;
15146 }
15147 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15148 && OMP_CLAUSE_MAP_IMPLICIT (c)
15149 && (bitmap_bit_p (&map_head, DECL_UID (t))
15150 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15151 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15152 remove = true;
15153 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15154 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15155 {
15156 if (bitmap_bit_p (&generic_head, DECL_UID (t))
15157 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15158 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15159 {
15160 error_at (OMP_CLAUSE_LOCATION (c),
15161 "%qD appears more than once in data clauses", t);
15162 remove = true;
15163 }
15164 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15165 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15166 {
15167 if (ort == C_ORT_ACC)
15168 error_at (OMP_CLAUSE_LOCATION (c),
15169 "%qD appears more than once in data clauses", t);
15170 else
15171 error_at (OMP_CLAUSE_LOCATION (c),
15172 "%qD appears both in data and map clauses", t);
15173 remove = true;
15174 }
15175 else
15176 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15177 }
15178 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15179 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15180 {
15181 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15182 error_at (OMP_CLAUSE_LOCATION (c),
15183 "%qD appears more than once in motion clauses", t);
15184 else if (ort == C_ORT_ACC)
15185 error_at (OMP_CLAUSE_LOCATION (c),
15186 "%qD appears more than once in data clauses", t);
15187 else
15188 error_at (OMP_CLAUSE_LOCATION (c),
15189 "%qD appears more than once in map clauses", t);
15190 remove = true;
15191 }
15192 else if (ort == C_ORT_ACC
15193 && bitmap_bit_p (&generic_head, DECL_UID (t)))
15194 {
15195 error_at (OMP_CLAUSE_LOCATION (c),
15196 "%qD appears more than once in data clauses", t);
15197 remove = true;
15198 }
15199 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15200 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15201 {
15202 if (ort == C_ORT_ACC)
15203 error_at (OMP_CLAUSE_LOCATION (c),
15204 "%qD appears more than once in data clauses", t);
15205 else
15206 error_at (OMP_CLAUSE_LOCATION (c),
15207 "%qD appears both in data and map clauses", t);
15208 remove = true;
15209 }
15210 else
15211 {
15212 bitmap_set_bit (&map_head, DECL_UID (t));
15213 if (t != OMP_CLAUSE_DECL (c)
15214 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15215 bitmap_set_bit (&map_field_head, DECL_UID (t));
15216 }
15217 break;
15218
15219 case OMP_CLAUSE_TO_DECLARE:
15220 case OMP_CLAUSE_LINK:
15221 t = OMP_CLAUSE_DECL (c);
15222 if (TREE_CODE (t) == FUNCTION_DECL
15223 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
15224 ;
15225 else if (!VAR_P (t))
15226 {
15227 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
15228 error_at (OMP_CLAUSE_LOCATION (c),
15229 "%qE is neither a variable nor a function name in "
15230 "clause %qs", t,
15231 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15232 else
15233 error_at (OMP_CLAUSE_LOCATION (c),
15234 "%qE is not a variable in clause %qs", t,
15235 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15236 remove = true;
15237 }
15238 else if (DECL_THREAD_LOCAL_P (t))
15239 {
15240 error_at (OMP_CLAUSE_LOCATION (c),
15241 "%qD is threadprivate variable in %qs clause", t,
15242 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15243 remove = true;
15244 }
15245 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
15246 {
15247 error_at (OMP_CLAUSE_LOCATION (c),
15248 "%qD does not have a mappable type in %qs clause", t,
15249 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15250 remove = true;
15251 }
15252 if (remove)
15253 break;
15254 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15255 {
15256 error_at (OMP_CLAUSE_LOCATION (c),
15257 "%qE appears more than once on the same "
15258 "%<declare target%> directive", t);
15259 remove = true;
15260 }
15261 else
15262 bitmap_set_bit (&generic_head, DECL_UID (t));
15263 break;
15264
15265 case OMP_CLAUSE_UNIFORM:
15266 t = OMP_CLAUSE_DECL (c);
15267 if (TREE_CODE (t) != PARM_DECL)
15268 {
15269 if (DECL_P (t))
15270 error_at (OMP_CLAUSE_LOCATION (c),
15271 "%qD is not an argument in %<uniform%> clause", t);
15272 else
15273 error_at (OMP_CLAUSE_LOCATION (c),
15274 "%qE is not an argument in %<uniform%> clause", t);
15275 remove = true;
15276 break;
15277 }
15278 /* map_head bitmap is used as uniform_head if declare_simd. */
15279 bitmap_set_bit (&map_head, DECL_UID (t));
15280 goto check_dup_generic;
15281
15282 case OMP_CLAUSE_IS_DEVICE_PTR:
15283 case OMP_CLAUSE_USE_DEVICE_PTR:
15284 t = OMP_CLAUSE_DECL (c);
15285 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15286 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15287 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15288 {
15289 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15290 && ort != C_ORT_ACC)
15291 {
15292 error_at (OMP_CLAUSE_LOCATION (c),
15293 "%qs variable is not a pointer",
15294 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15295 remove = true;
15296 }
15297 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15298 {
15299 error_at (OMP_CLAUSE_LOCATION (c),
15300 "%qs variable is neither a pointer nor an array",
15301 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15302 remove = true;
15303 }
15304 }
15305 goto check_dup_generic;
15306
15307 case OMP_CLAUSE_HAS_DEVICE_ADDR:
15308 t = OMP_CLAUSE_DECL (c);
15309 if (TREE_CODE (t) == TREE_LIST)
15310 {
15311 if (handle_omp_array_sections (c, ort))
15312 remove = true;
15313 else
15314 {
15315 t = OMP_CLAUSE_DECL (c);
15316 while (TREE_CODE (t) == ARRAY_REF)
15317 t = TREE_OPERAND (t, 0);
15318 }
15319 }
15320 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15321 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15322 c_mark_addressable (t);
15323 goto check_dup_generic_t;
15324
15325 case OMP_CLAUSE_USE_DEVICE_ADDR:
15326 t = OMP_CLAUSE_DECL (c);
15327 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15328 c_mark_addressable (t);
15329 goto check_dup_generic;
15330
15331 case OMP_CLAUSE_NOWAIT:
15332 if (copyprivate_seen)
15333 {
15334 error_at (OMP_CLAUSE_LOCATION (c),
15335 "%<nowait%> clause must not be used together "
15336 "with %<copyprivate%>");
15337 remove = true;
15338 break;
15339 }
15340 nowait_clause = pc;
15341 pc = &OMP_CLAUSE_CHAIN (c);
15342 continue;
15343
15344 case OMP_CLAUSE_ORDER:
15345 if (ordered_clause)
15346 {
15347 error_at (OMP_CLAUSE_LOCATION (c),
15348 "%<order%> clause must not be used together "
15349 "with %<ordered%>");
15350 remove = true;
15351 break;
15352 }
15353 else if (order_clause)
15354 {
15355 /* Silently remove duplicates. */
15356 remove = true;
15357 break;
15358 }
15359 order_clause = pc;
15360 pc = &OMP_CLAUSE_CHAIN (c);
15361 continue;
15362
15363 case OMP_CLAUSE_DETACH:
15364 t = OMP_CLAUSE_DECL (c);
15365 if (detach_seen)
15366 {
15367 error_at (OMP_CLAUSE_LOCATION (c),
15368 "too many %qs clauses on a task construct",
15369 "detach");
15370 remove = true;
15371 break;
15372 }
15373 detach_seen = pc;
15374 pc = &OMP_CLAUSE_CHAIN (c);
15375 c_mark_addressable (t);
15376 continue;
15377
15378 case OMP_CLAUSE_IF:
15379 case OMP_CLAUSE_NUM_THREADS:
15380 case OMP_CLAUSE_NUM_TEAMS:
15381 case OMP_CLAUSE_THREAD_LIMIT:
15382 case OMP_CLAUSE_DEFAULT:
15383 case OMP_CLAUSE_UNTIED:
15384 case OMP_CLAUSE_COLLAPSE:
15385 case OMP_CLAUSE_FINAL:
15386 case OMP_CLAUSE_DEVICE:
15387 case OMP_CLAUSE_DIST_SCHEDULE:
15388 case OMP_CLAUSE_PARALLEL:
15389 case OMP_CLAUSE_FOR:
15390 case OMP_CLAUSE_SECTIONS:
15391 case OMP_CLAUSE_TASKGROUP:
15392 case OMP_CLAUSE_PROC_BIND:
15393 case OMP_CLAUSE_DEVICE_TYPE:
15394 case OMP_CLAUSE_PRIORITY:
15395 case OMP_CLAUSE_GRAINSIZE:
15396 case OMP_CLAUSE_NUM_TASKS:
15397 case OMP_CLAUSE_THREADS:
15398 case OMP_CLAUSE_SIMD:
15399 case OMP_CLAUSE_HINT:
15400 case OMP_CLAUSE_FILTER:
15401 case OMP_CLAUSE_DEFAULTMAP:
15402 case OMP_CLAUSE_BIND:
15403 case OMP_CLAUSE_NUM_GANGS:
15404 case OMP_CLAUSE_NUM_WORKERS:
15405 case OMP_CLAUSE_VECTOR_LENGTH:
15406 case OMP_CLAUSE_ASYNC:
15407 case OMP_CLAUSE_WAIT:
15408 case OMP_CLAUSE_AUTO:
15409 case OMP_CLAUSE_INDEPENDENT:
15410 case OMP_CLAUSE_SEQ:
15411 case OMP_CLAUSE_GANG:
15412 case OMP_CLAUSE_WORKER:
15413 case OMP_CLAUSE_VECTOR:
15414 case OMP_CLAUSE_TILE:
15415 case OMP_CLAUSE_IF_PRESENT:
15416 case OMP_CLAUSE_FINALIZE:
15417 case OMP_CLAUSE_NOHOST:
15418 pc = &OMP_CLAUSE_CHAIN (c);
15419 continue;
15420
15421 case OMP_CLAUSE_MERGEABLE:
15422 mergeable_seen = true;
15423 pc = &OMP_CLAUSE_CHAIN (c);
15424 continue;
15425
15426 case OMP_CLAUSE_NOGROUP:
15427 nogroup_seen = pc;
15428 pc = &OMP_CLAUSE_CHAIN (c);
15429 continue;
15430
15431 case OMP_CLAUSE_SCHEDULE:
15432 schedule_clause = c;
15433 pc = &OMP_CLAUSE_CHAIN (c);
15434 continue;
15435
15436 case OMP_CLAUSE_ORDERED:
15437 ordered_clause = c;
15438 if (order_clause)
15439 {
15440 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15441 "%<order%> clause must not be used together "
15442 "with %<ordered%>");
15443 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15444 order_clause = NULL;
15445 }
15446 pc = &OMP_CLAUSE_CHAIN (c);
15447 continue;
15448
15449 case OMP_CLAUSE_SAFELEN:
15450 safelen = c;
15451 pc = &OMP_CLAUSE_CHAIN (c);
15452 continue;
15453 case OMP_CLAUSE_SIMDLEN:
15454 simdlen = c;
15455 pc = &OMP_CLAUSE_CHAIN (c);
15456 continue;
15457
15458 case OMP_CLAUSE_INBRANCH:
15459 case OMP_CLAUSE_NOTINBRANCH:
15460 if (branch_seen)
15461 {
15462 error_at (OMP_CLAUSE_LOCATION (c),
15463 "%<inbranch%> clause is incompatible with "
15464 "%<notinbranch%>");
15465 remove = true;
15466 break;
15467 }
15468 branch_seen = true;
15469 pc = &OMP_CLAUSE_CHAIN (c);
15470 continue;
15471
15472 case OMP_CLAUSE_INCLUSIVE:
15473 case OMP_CLAUSE_EXCLUSIVE:
15474 need_complete = true;
15475 need_implicitly_determined = true;
15476 t = OMP_CLAUSE_DECL (c);
15477 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15478 {
15479 error_at (OMP_CLAUSE_LOCATION (c),
15480 "%qE is not a variable in clause %qs", t,
15481 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15482 remove = true;
15483 }
15484 break;
15485
15486 default:
15487 gcc_unreachable ();
15488 }
15489
15490 if (!remove)
15491 {
15492 t = OMP_CLAUSE_DECL (c);
15493
15494 if (need_complete)
15495 {
15496 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15497 if (t == error_mark_node)
15498 remove = true;
15499 }
15500
15501 if (need_implicitly_determined)
15502 {
15503 const char *share_name = NULL;
15504
15505 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15506 share_name = "threadprivate";
15507 else switch (c_omp_predetermined_sharing (t))
15508 {
15509 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15510 break;
15511 case OMP_CLAUSE_DEFAULT_SHARED:
15512 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15513 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15514 && c_omp_predefined_variable (t))
15515 /* The __func__ variable and similar function-local
15516 predefined variables may be listed in a shared or
15517 firstprivate clause. */
15518 break;
15519 share_name = "shared";
15520 break;
15521 case OMP_CLAUSE_DEFAULT_PRIVATE:
15522 share_name = "private";
15523 break;
15524 default:
15525 gcc_unreachable ();
15526 }
15527 if (share_name)
15528 {
15529 error_at (OMP_CLAUSE_LOCATION (c),
15530 "%qE is predetermined %qs for %qs",
15531 t, share_name,
15532 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15533 remove = true;
15534 }
15535 else if (TREE_READONLY (t)
15536 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15537 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15538 {
15539 error_at (OMP_CLAUSE_LOCATION (c),
15540 "%<const%> qualified %qE may appear only in "
15541 "%<shared%> or %<firstprivate%> clauses", t);
15542 remove = true;
15543 }
15544 }
15545 }
15546
15547 if (remove)
15548 *pc = OMP_CLAUSE_CHAIN (c);
15549 else
15550 pc = &OMP_CLAUSE_CHAIN (c);
15551 }
15552
15553 if (simdlen
15554 && safelen
15555 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15556 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15557 {
15558 error_at (OMP_CLAUSE_LOCATION (simdlen),
15559 "%<simdlen%> clause value is bigger than "
15560 "%<safelen%> clause value");
15561 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15562 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15563 }
15564
15565 if (ordered_clause
15566 && schedule_clause
15567 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15568 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15569 {
15570 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15571 "%<nonmonotonic%> schedule modifier specified together "
15572 "with %<ordered%> clause");
15573 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15574 = (enum omp_clause_schedule_kind)
15575 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15576 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15577 }
15578
15579 if (reduction_seen < 0 && ordered_clause)
15580 {
15581 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15582 "%qs clause specified together with %<inscan%> "
15583 "%<reduction%> clause", "ordered");
15584 reduction_seen = -2;
15585 }
15586
15587 if (reduction_seen < 0 && schedule_clause)
15588 {
15589 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15590 "%qs clause specified together with %<inscan%> "
15591 "%<reduction%> clause", "schedule");
15592 reduction_seen = -2;
15593 }
15594
15595 if (linear_variable_step_check
15596 || reduction_seen == -2
15597 || allocate_seen
15598 || target_in_reduction_seen)
15599 for (pc = &clauses, c = clauses; c ; c = *pc)
15600 {
15601 bool remove = false;
15602 if (allocate_seen)
15603 switch (OMP_CLAUSE_CODE (c))
15604 {
15605 case OMP_CLAUSE_REDUCTION:
15606 case OMP_CLAUSE_IN_REDUCTION:
15607 case OMP_CLAUSE_TASK_REDUCTION:
15608 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
15609 {
15610 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
15611 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15612 t = TREE_OPERAND (t, 0);
15613 if (TREE_CODE (t) == ADDR_EXPR
15614 || TREE_CODE (t) == INDIRECT_REF)
15615 t = TREE_OPERAND (t, 0);
15616 if (DECL_P (t))
15617 bitmap_clear_bit (&aligned_head, DECL_UID (t));
15618 break;
15619 }
15620 /* FALLTHRU */
15621 case OMP_CLAUSE_PRIVATE:
15622 case OMP_CLAUSE_FIRSTPRIVATE:
15623 case OMP_CLAUSE_LASTPRIVATE:
15624 case OMP_CLAUSE_LINEAR:
15625 if (DECL_P (OMP_CLAUSE_DECL (c)))
15626 bitmap_clear_bit (&aligned_head,
15627 DECL_UID (OMP_CLAUSE_DECL (c)));
15628 break;
15629 default:
15630 break;
15631 }
15632 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15633 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15634 && !bitmap_bit_p (&map_head,
15635 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15636 {
15637 error_at (OMP_CLAUSE_LOCATION (c),
15638 "%<linear%> clause step is a parameter %qD not "
15639 "specified in %<uniform%> clause",
15640 OMP_CLAUSE_LINEAR_STEP (c));
15641 remove = true;
15642 }
15643 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15644 && reduction_seen == -2)
15645 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15646 if (target_in_reduction_seen
15647 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
15648 {
15649 tree t = OMP_CLAUSE_DECL (c);
15650 while (handled_component_p (t)
15651 || TREE_CODE (t) == INDIRECT_REF
15652 || TREE_CODE (t) == ADDR_EXPR
15653 || TREE_CODE (t) == MEM_REF
15654 || TREE_CODE (t) == NON_LVALUE_EXPR)
15655 t = TREE_OPERAND (t, 0);
15656 if (DECL_P (t)
15657 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15658 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15659 }
15660
15661 if (remove)
15662 *pc = OMP_CLAUSE_CHAIN (c);
15663 else
15664 pc = &OMP_CLAUSE_CHAIN (c);
15665 }
15666
15667 if (allocate_seen)
15668 for (pc = &clauses, c = clauses; c ; c = *pc)
15669 {
15670 bool remove = false;
15671 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
15672 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
15673 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
15674 {
15675 error_at (OMP_CLAUSE_LOCATION (c),
15676 "%qD specified in %<allocate%> clause but not in "
15677 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
15678 remove = true;
15679 }
15680 if (remove)
15681 *pc = OMP_CLAUSE_CHAIN (c);
15682 else
15683 pc = &OMP_CLAUSE_CHAIN (c);
15684 }
15685
15686 if (nogroup_seen && reduction_seen)
15687 {
15688 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15689 "%<nogroup%> clause must not be used together with "
15690 "%<reduction%> clause");
15691 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15692 }
15693
15694 if (detach_seen)
15695 {
15696 if (mergeable_seen)
15697 {
15698 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
15699 "%<detach%> clause must not be used together with "
15700 "%<mergeable%> clause");
15701 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
15702 }
15703 else
15704 {
15705 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
15706
15707 for (pc = &clauses, c = clauses; c ; c = *pc)
15708 {
15709 bool remove = false;
15710 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15711 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15712 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
15713 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
15714 && OMP_CLAUSE_DECL (c) == detach_decl)
15715 {
15716 error_at (OMP_CLAUSE_LOCATION (c),
15717 "the event handle of a %<detach%> clause "
15718 "should not be in a data-sharing clause");
15719 remove = true;
15720 }
15721 if (remove)
15722 *pc = OMP_CLAUSE_CHAIN (c);
15723 else
15724 pc = &OMP_CLAUSE_CHAIN (c);
15725 }
15726 }
15727 }
15728
15729 bitmap_obstack_release (NULL);
15730 return clauses;
15731 }
15732
15733 /* Return code to initialize DST with a copy constructor from SRC.
15734 C doesn't have copy constructors nor assignment operators, only for
15735 _Atomic vars we need to perform __atomic_load from src into a temporary
15736 followed by __atomic_store of the temporary to dst. */
15737
15738 tree
c_omp_clause_copy_ctor(tree clause,tree dst,tree src)15739 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15740 {
15741 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15742 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15743
15744 location_t loc = OMP_CLAUSE_LOCATION (clause);
15745 tree type = TREE_TYPE (dst);
15746 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15747 tree tmp = create_tmp_var (nonatomic_type);
15748 tree tmp_addr = build_fold_addr_expr (tmp);
15749 TREE_ADDRESSABLE (tmp) = 1;
15750 suppress_warning (tmp);
15751 tree src_addr = build_fold_addr_expr (src);
15752 tree dst_addr = build_fold_addr_expr (dst);
15753 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15754 vec<tree, va_gc> *params;
15755 /* Expansion of a generic atomic load may require an addition
15756 element, so allocate enough to prevent a resize. */
15757 vec_alloc (params, 4);
15758
15759 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
15760 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15761 params->quick_push (src_addr);
15762 params->quick_push (tmp_addr);
15763 params->quick_push (seq_cst);
15764 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15765
15766 vec_alloc (params, 4);
15767
15768 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
15769 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15770 params->quick_push (dst_addr);
15771 params->quick_push (tmp_addr);
15772 params->quick_push (seq_cst);
15773 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15774 return build2 (COMPOUND_EXPR, void_type_node, load, store);
15775 }
15776
15777 /* Create a transaction node. */
15778
15779 tree
c_finish_transaction(location_t loc,tree block,int flags)15780 c_finish_transaction (location_t loc, tree block, int flags)
15781 {
15782 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15783 if (flags & TM_STMT_ATTR_OUTER)
15784 TRANSACTION_EXPR_OUTER (stmt) = 1;
15785 if (flags & TM_STMT_ATTR_RELAXED)
15786 TRANSACTION_EXPR_RELAXED (stmt) = 1;
15787 return add_stmt (stmt);
15788 }
15789
15790 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15791 down to the element type of an array. If ORIG_QUAL_TYPE is not
15792 NULL, then it should be used as the qualified type
15793 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15794 preserve information about the typedef name from which an array
15795 type was derived). */
15796
15797 tree
c_build_qualified_type(tree type,int type_quals,tree orig_qual_type,size_t orig_qual_indirect)15798 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15799 size_t orig_qual_indirect)
15800 {
15801 if (type == error_mark_node)
15802 return type;
15803
15804 if (TREE_CODE (type) == ARRAY_TYPE)
15805 {
15806 tree t;
15807 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15808 type_quals, orig_qual_type,
15809 orig_qual_indirect - 1);
15810
15811 /* See if we already have an identically qualified type. */
15812 if (orig_qual_type && orig_qual_indirect == 0)
15813 t = orig_qual_type;
15814 else
15815 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15816 {
15817 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15818 && TYPE_NAME (t) == TYPE_NAME (type)
15819 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15820 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15821 TYPE_ATTRIBUTES (type)))
15822 break;
15823 }
15824 if (!t)
15825 {
15826 tree domain = TYPE_DOMAIN (type);
15827
15828 t = build_variant_type_copy (type);
15829 TREE_TYPE (t) = element_type;
15830
15831 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15832 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15833 SET_TYPE_STRUCTURAL_EQUALITY (t);
15834 else if (TYPE_CANONICAL (element_type) != element_type
15835 || (domain && TYPE_CANONICAL (domain) != domain))
15836 {
15837 tree unqualified_canon
15838 = build_array_type (TYPE_CANONICAL (element_type),
15839 domain? TYPE_CANONICAL (domain)
15840 : NULL_TREE);
15841 if (TYPE_REVERSE_STORAGE_ORDER (type))
15842 {
15843 unqualified_canon
15844 = build_distinct_type_copy (unqualified_canon);
15845 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15846 }
15847 TYPE_CANONICAL (t)
15848 = c_build_qualified_type (unqualified_canon, type_quals);
15849 }
15850 else
15851 TYPE_CANONICAL (t) = t;
15852 }
15853 return t;
15854 }
15855
15856 /* A restrict-qualified pointer type must be a pointer to object or
15857 incomplete type. Note that the use of POINTER_TYPE_P also allows
15858 REFERENCE_TYPEs, which is appropriate for C++. */
15859 if ((type_quals & TYPE_QUAL_RESTRICT)
15860 && (!POINTER_TYPE_P (type)
15861 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15862 {
15863 error ("invalid use of %<restrict%>");
15864 type_quals &= ~TYPE_QUAL_RESTRICT;
15865 }
15866
15867 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15868 ? orig_qual_type
15869 : build_qualified_type (type, type_quals));
15870 /* A variant type does not inherit the list of incomplete vars from the
15871 type main variant. */
15872 if ((RECORD_OR_UNION_TYPE_P (var_type)
15873 || TREE_CODE (var_type) == ENUMERAL_TYPE)
15874 && TYPE_MAIN_VARIANT (var_type) != var_type)
15875 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15876 return var_type;
15877 }
15878
15879 /* Build a VA_ARG_EXPR for the C parser. */
15880
15881 tree
c_build_va_arg(location_t loc1,tree expr,location_t loc2,tree type)15882 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15883 {
15884 if (error_operand_p (type))
15885 return error_mark_node;
15886 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15887 order because it takes the address of the expression. */
15888 else if (handled_component_p (expr)
15889 && reverse_storage_order_for_component_p (expr))
15890 {
15891 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15892 return error_mark_node;
15893 }
15894 else if (!COMPLETE_TYPE_P (type))
15895 {
15896 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15897 "type %qT", type);
15898 return error_mark_node;
15899 }
15900 else if (TREE_CODE (type) == FUNCTION_TYPE)
15901 {
15902 error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
15903 type);
15904 return error_mark_node;
15905 }
15906 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15907 warning_at (loc2, OPT_Wc___compat,
15908 "C++ requires promoted type, not enum type, in %<va_arg%>");
15909 return build_va_arg (loc2, expr, type);
15910 }
15911
15912 /* Return truthvalue of whether T1 is the same tree structure as T2.
15913 Return 1 if they are the same. Return false if they are different. */
15914
15915 bool
c_tree_equal(tree t1,tree t2)15916 c_tree_equal (tree t1, tree t2)
15917 {
15918 enum tree_code code1, code2;
15919
15920 if (t1 == t2)
15921 return true;
15922 if (!t1 || !t2)
15923 return false;
15924
15925 for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
15926 code1 = TREE_CODE (t1))
15927 t1 = TREE_OPERAND (t1, 0);
15928 for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
15929 code2 = TREE_CODE (t2))
15930 t2 = TREE_OPERAND (t2, 0);
15931
15932 /* They might have become equal now. */
15933 if (t1 == t2)
15934 return true;
15935
15936 if (code1 != code2)
15937 return false;
15938
15939 if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
15940 return false;
15941
15942 switch (code1)
15943 {
15944 case INTEGER_CST:
15945 return wi::to_wide (t1) == wi::to_wide (t2);
15946
15947 case REAL_CST:
15948 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15949
15950 case STRING_CST:
15951 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15952 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15953 TREE_STRING_LENGTH (t1));
15954
15955 case FIXED_CST:
15956 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15957 TREE_FIXED_CST (t2));
15958
15959 case COMPLEX_CST:
15960 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15961 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15962
15963 case VECTOR_CST:
15964 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15965
15966 case CONSTRUCTOR:
15967 /* We need to do this when determining whether or not two
15968 non-type pointer to member function template arguments
15969 are the same. */
15970 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15971 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15972 return false;
15973 {
15974 tree field, value;
15975 unsigned int i;
15976 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15977 {
15978 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15979 if (!c_tree_equal (field, elt2->index)
15980 || !c_tree_equal (value, elt2->value))
15981 return false;
15982 }
15983 }
15984 return true;
15985
15986 case TREE_LIST:
15987 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15988 return false;
15989 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15990 return false;
15991 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15992
15993 case SAVE_EXPR:
15994 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15995
15996 case CALL_EXPR:
15997 {
15998 tree arg1, arg2;
15999 call_expr_arg_iterator iter1, iter2;
16000 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16001 return false;
16002 for (arg1 = first_call_expr_arg (t1, &iter1),
16003 arg2 = first_call_expr_arg (t2, &iter2);
16004 arg1 && arg2;
16005 arg1 = next_call_expr_arg (&iter1),
16006 arg2 = next_call_expr_arg (&iter2))
16007 if (!c_tree_equal (arg1, arg2))
16008 return false;
16009 if (arg1 || arg2)
16010 return false;
16011 return true;
16012 }
16013
16014 case TARGET_EXPR:
16015 {
16016 tree o1 = TREE_OPERAND (t1, 0);
16017 tree o2 = TREE_OPERAND (t2, 0);
16018
16019 /* Special case: if either target is an unallocated VAR_DECL,
16020 it means that it's going to be unified with whatever the
16021 TARGET_EXPR is really supposed to initialize, so treat it
16022 as being equivalent to anything. */
16023 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16024 && !DECL_RTL_SET_P (o1))
16025 /*Nop*/;
16026 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16027 && !DECL_RTL_SET_P (o2))
16028 /*Nop*/;
16029 else if (!c_tree_equal (o1, o2))
16030 return false;
16031
16032 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16033 }
16034
16035 case COMPONENT_REF:
16036 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16037 return false;
16038 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16039
16040 case PARM_DECL:
16041 case VAR_DECL:
16042 case CONST_DECL:
16043 case FIELD_DECL:
16044 case FUNCTION_DECL:
16045 case IDENTIFIER_NODE:
16046 case SSA_NAME:
16047 return false;
16048
16049 case TREE_VEC:
16050 {
16051 unsigned ix;
16052 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16053 return false;
16054 for (ix = TREE_VEC_LENGTH (t1); ix--;)
16055 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16056 TREE_VEC_ELT (t2, ix)))
16057 return false;
16058 return true;
16059 }
16060
16061 CASE_CONVERT:
16062 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16063 return false;
16064 break;
16065
16066 default:
16067 break;
16068 }
16069
16070 switch (TREE_CODE_CLASS (code1))
16071 {
16072 case tcc_unary:
16073 case tcc_binary:
16074 case tcc_comparison:
16075 case tcc_expression:
16076 case tcc_vl_exp:
16077 case tcc_reference:
16078 case tcc_statement:
16079 {
16080 int i, n = TREE_OPERAND_LENGTH (t1);
16081
16082 switch (code1)
16083 {
16084 case PREINCREMENT_EXPR:
16085 case PREDECREMENT_EXPR:
16086 case POSTINCREMENT_EXPR:
16087 case POSTDECREMENT_EXPR:
16088 n = 1;
16089 break;
16090 case ARRAY_REF:
16091 n = 2;
16092 break;
16093 default:
16094 break;
16095 }
16096
16097 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16098 && n != TREE_OPERAND_LENGTH (t2))
16099 return false;
16100
16101 for (i = 0; i < n; ++i)
16102 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16103 return false;
16104
16105 return true;
16106 }
16107
16108 case tcc_type:
16109 return comptypes (t1, t2);
16110 default:
16111 gcc_unreachable ();
16112 }
16113 }
16114
16115 /* Returns true when the function declaration FNDECL is implicit,
16116 introduced as a result of a call to an otherwise undeclared
16117 function, and false otherwise. */
16118
16119 bool
c_decl_implicit(const_tree fndecl)16120 c_decl_implicit (const_tree fndecl)
16121 {
16122 return C_DECL_IMPLICIT (fndecl);
16123 }
16124