1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "memmodel.h"
30 #include "target.h"
31 #include "function.h"
32 #include "bitmap.h"
33 #include "c-tree.h"
34 #include "gimple-expr.h"
35 #include "predict.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "langhooks.h"
41 #include "c-lang.h"
42 #include "intl.h"
43 #include "tree-iterator.h"
44 #include "gimplify.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "asan.h"
55
56 /* Possible cases of implicit bad conversions. Used to select
57 diagnostic messages in convert_for_assignment. */
58 enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_return
63 };
64
65 /* The level of nesting inside "__alignof__". */
66 int in_alignof;
67
68 /* The level of nesting inside "sizeof". */
69 int in_sizeof;
70
71 /* The level of nesting inside "typeof". */
72 int in_typeof;
73
74 /* The argument of last parsed sizeof expression, only to be tested
75 if expr.original_code == SIZEOF_EXPR. */
76 tree c_last_sizeof_arg;
77 location_t c_last_sizeof_loc;
78
79 /* Nonzero if we might need to print a "missing braces around
80 initializer" message within this initializer. */
81 static int found_missing_braces;
82
83 static int require_constant_value;
84 static int require_constant_elements;
85
86 static bool null_pointer_constant_p (const_tree);
87 static tree qualify_type (tree, tree);
88 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90 static int comp_target_types (location_t, tree, tree);
91 static int function_types_compatible_p (const_tree, const_tree, bool *,
92 bool *);
93 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
94 static tree lookup_field (tree, tree);
95 static int convert_arguments (location_t, vec<location_t>, tree,
96 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
97 tree);
98 static tree pointer_diff (location_t, tree, tree, tree *);
99 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
100 enum impl_conv, bool, tree, tree, int,
101 int = 0);
102 static tree valid_compound_expr_initializer (tree, tree);
103 static void push_string (const char *);
104 static void push_member_name (tree);
105 static int spelling_length (void);
106 static char *print_spelling (char *);
107 static void warning_init (location_t, int, const char *);
108 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
109 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
110 bool, struct obstack *);
111 static void output_pending_init_elements (int, struct obstack *);
112 static bool set_designator (location_t, bool, struct obstack *);
113 static void push_range_stack (tree, struct obstack *);
114 static void add_pending_init (location_t, tree, tree, tree, bool,
115 struct obstack *);
116 static void set_nonincremental_init (struct obstack *);
117 static void set_nonincremental_init_from_string (tree, struct obstack *);
118 static tree find_init_member (tree, struct obstack *);
119 static void readonly_warning (tree, enum lvalue_use);
120 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
121 static void record_maybe_used_decl (tree);
122 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
123
124 /* Return true if EXP is a null pointer constant, false otherwise. */
125
126 static bool
null_pointer_constant_p(const_tree expr)127 null_pointer_constant_p (const_tree expr)
128 {
129 /* This should really operate on c_expr structures, but they aren't
130 yet available everywhere required. */
131 tree type = TREE_TYPE (expr);
132 return (TREE_CODE (expr) == INTEGER_CST
133 && !TREE_OVERFLOW (expr)
134 && integer_zerop (expr)
135 && (INTEGRAL_TYPE_P (type)
136 || (TREE_CODE (type) == POINTER_TYPE
137 && VOID_TYPE_P (TREE_TYPE (type))
138 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
139 }
140
141 /* EXPR may appear in an unevaluated part of an integer constant
142 expression, but not in an evaluated part. Wrap it in a
143 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
144 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
145
146 static tree
note_integer_operands(tree expr)147 note_integer_operands (tree expr)
148 {
149 tree ret;
150 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
151 {
152 ret = copy_node (expr);
153 TREE_OVERFLOW (ret) = 1;
154 }
155 else
156 {
157 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
158 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
159 }
160 return ret;
161 }
162
163 /* Having checked whether EXPR may appear in an unevaluated part of an
164 integer constant expression and found that it may, remove any
165 C_MAYBE_CONST_EXPR noting this fact and return the resulting
166 expression. */
167
168 static inline tree
remove_c_maybe_const_expr(tree expr)169 remove_c_maybe_const_expr (tree expr)
170 {
171 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
172 return C_MAYBE_CONST_EXPR_EXPR (expr);
173 else
174 return expr;
175 }
176
177 /* This is a cache to hold if two types are compatible or not. */
178
179 struct tagged_tu_seen_cache {
180 const struct tagged_tu_seen_cache * next;
181 const_tree t1;
182 const_tree t2;
183 /* The return value of tagged_types_tu_compatible_p if we had seen
184 these two types already. */
185 int val;
186 };
187
188 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
189 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
190
191 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
192 does not have an incomplete type. (That includes void types.)
193 LOC is the location of the use. */
194
195 tree
require_complete_type(location_t loc,tree value)196 require_complete_type (location_t loc, tree value)
197 {
198 tree type = TREE_TYPE (value);
199
200 if (error_operand_p (value))
201 return error_mark_node;
202
203 /* First, detect a valid value with a complete type. */
204 if (COMPLETE_TYPE_P (type))
205 return value;
206
207 c_incomplete_type_error (loc, value, type);
208 return error_mark_node;
209 }
210
211 /* Print an error message for invalid use of an incomplete type.
212 VALUE is the expression that was used (or 0 if that isn't known)
213 and TYPE is the type that was invalid. LOC is the location for
214 the error. */
215
216 void
c_incomplete_type_error(location_t loc,const_tree value,const_tree type)217 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
218 {
219 /* Avoid duplicate error message. */
220 if (TREE_CODE (type) == ERROR_MARK)
221 return;
222
223 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
224 error_at (loc, "%qD has an incomplete type %qT", value, type);
225 else
226 {
227 retry:
228 /* We must print an error message. Be clever about what it says. */
229
230 switch (TREE_CODE (type))
231 {
232 case RECORD_TYPE:
233 case UNION_TYPE:
234 case ENUMERAL_TYPE:
235 break;
236
237 case VOID_TYPE:
238 error_at (loc, "invalid use of void expression");
239 return;
240
241 case ARRAY_TYPE:
242 if (TYPE_DOMAIN (type))
243 {
244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 {
246 error_at (loc, "invalid use of flexible array member");
247 return;
248 }
249 type = TREE_TYPE (type);
250 goto retry;
251 }
252 error_at (loc, "invalid use of array with unspecified bounds");
253 return;
254
255 default:
256 gcc_unreachable ();
257 }
258
259 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
260 error_at (loc, "invalid use of undefined type %qT", type);
261 else
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
263 error_at (loc, "invalid use of incomplete typedef %qT", type);
264 }
265 }
266
267 /* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
269
270 tree
c_type_promotes_to(tree type)271 c_type_promotes_to (tree type)
272 {
273 tree ret = NULL_TREE;
274
275 if (TYPE_MAIN_VARIANT (type) == float_type_node)
276 ret = double_type_node;
277 else if (c_promoting_integer_type_p (type))
278 {
279 /* Preserve unsignedness if not really getting any wider. */
280 if (TYPE_UNSIGNED (type)
281 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
282 ret = unsigned_type_node;
283 else
284 ret = integer_type_node;
285 }
286
287 if (ret != NULL_TREE)
288 return (TYPE_ATOMIC (type)
289 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
290 : ret);
291
292 return type;
293 }
294
295 /* Return true if between two named address spaces, whether there is a superset
296 named address space that encompasses both address spaces. If there is a
297 superset, return which address space is the superset. */
298
299 static bool
addr_space_superset(addr_space_t as1,addr_space_t as2,addr_space_t * common)300 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
301 {
302 if (as1 == as2)
303 {
304 *common = as1;
305 return true;
306 }
307 else if (targetm.addr_space.subset_p (as1, as2))
308 {
309 *common = as2;
310 return true;
311 }
312 else if (targetm.addr_space.subset_p (as2, as1))
313 {
314 *common = as1;
315 return true;
316 }
317 else
318 return false;
319 }
320
321 /* Return a variant of TYPE which has all the type qualifiers of LIKE
322 as well as those of TYPE. */
323
324 static tree
qualify_type(tree type,tree like)325 qualify_type (tree type, tree like)
326 {
327 addr_space_t as_type = TYPE_ADDR_SPACE (type);
328 addr_space_t as_like = TYPE_ADDR_SPACE (like);
329 addr_space_t as_common;
330
331 /* If the two named address spaces are different, determine the common
332 superset address space. If there isn't one, raise an error. */
333 if (!addr_space_superset (as_type, as_like, &as_common))
334 {
335 as_common = as_type;
336 error ("%qT and %qT are in disjoint named address spaces",
337 type, like);
338 }
339
340 return c_build_qualified_type (type,
341 TYPE_QUALS_NO_ADDR_SPACE (type)
342 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
343 | ENCODE_QUAL_ADDR_SPACE (as_common));
344 }
345
346 /* Return true iff the given tree T is a variable length array. */
347
348 bool
c_vla_type_p(const_tree t)349 c_vla_type_p (const_tree t)
350 {
351 if (TREE_CODE (t) == ARRAY_TYPE
352 && C_TYPE_VARIABLE_SIZE (t))
353 return true;
354 return false;
355 }
356
357 /* If NTYPE is a type of a non-variadic function with a prototype
358 and OTYPE is a type of a function without a prototype and ATTRS
359 contains attribute format, diagnosess and removes it from ATTRS.
360 Returns the result of build_type_attribute_variant of NTYPE and
361 the (possibly) modified ATTRS. */
362
363 static tree
build_functype_attribute_variant(tree ntype,tree otype,tree attrs)364 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
365 {
366 if (!prototype_p (otype)
367 && prototype_p (ntype)
368 && lookup_attribute ("format", attrs))
369 {
370 warning_at (input_location, OPT_Wattributes,
371 "%qs attribute cannot be applied to a function that "
372 "does not take variable arguments", "format");
373 attrs = remove_attribute ("format", attrs);
374 }
375 return build_type_attribute_variant (ntype, attrs);
376
377 }
378 /* Return the composite type of two compatible types.
379
380 We assume that comptypes has already been done and returned
381 nonzero; if that isn't so, this may crash. In particular, we
382 assume that qualifiers match. */
383
384 tree
composite_type(tree t1,tree t2)385 composite_type (tree t1, tree t2)
386 {
387 enum tree_code code1;
388 enum tree_code code2;
389 tree attributes;
390
391 /* Save time if the two types are the same. */
392
393 if (t1 == t2) return t1;
394
395 /* If one type is nonsense, use the other. */
396 if (t1 == error_mark_node)
397 return t2;
398 if (t2 == error_mark_node)
399 return t1;
400
401 code1 = TREE_CODE (t1);
402 code2 = TREE_CODE (t2);
403
404 /* Merge the attributes. */
405 attributes = targetm.merge_type_attributes (t1, t2);
406
407 /* If one is an enumerated type and the other is the compatible
408 integer type, the composite type might be either of the two
409 (DR#013 question 3). For consistency, use the enumerated type as
410 the composite type. */
411
412 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
413 return t1;
414 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
415 return t2;
416
417 gcc_assert (code1 == code2);
418
419 switch (code1)
420 {
421 case POINTER_TYPE:
422 /* For two pointers, do this recursively on the target type. */
423 {
424 tree pointed_to_1 = TREE_TYPE (t1);
425 tree pointed_to_2 = TREE_TYPE (t2);
426 tree target = composite_type (pointed_to_1, pointed_to_2);
427 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
428 t1 = build_type_attribute_variant (t1, attributes);
429 return qualify_type (t1, t2);
430 }
431
432 case ARRAY_TYPE:
433 {
434 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
435 int quals;
436 tree unqual_elt;
437 tree d1 = TYPE_DOMAIN (t1);
438 tree d2 = TYPE_DOMAIN (t2);
439 bool d1_variable, d2_variable;
440 bool d1_zero, d2_zero;
441 bool t1_complete, t2_complete;
442
443 /* We should not have any type quals on arrays at all. */
444 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
445 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
446
447 t1_complete = COMPLETE_TYPE_P (t1);
448 t2_complete = COMPLETE_TYPE_P (t2);
449
450 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
451 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
452
453 d1_variable = (!d1_zero
454 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
455 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
456 d2_variable = (!d2_zero
457 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
458 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
459 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
460 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
461
462 /* Save space: see if the result is identical to one of the args. */
463 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
464 && (d2_variable || d2_zero || !d1_variable))
465 return build_type_attribute_variant (t1, attributes);
466 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
467 && (d1_variable || d1_zero || !d2_variable))
468 return build_type_attribute_variant (t2, attributes);
469
470 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
471 return build_type_attribute_variant (t1, attributes);
472 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
473 return build_type_attribute_variant (t2, attributes);
474
475 /* Merge the element types, and have a size if either arg has
476 one. We may have qualifiers on the element types. To set
477 up TYPE_MAIN_VARIANT correctly, we need to form the
478 composite of the unqualified types and add the qualifiers
479 back at the end. */
480 quals = TYPE_QUALS (strip_array_types (elt));
481 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
482 t1 = build_array_type (unqual_elt,
483 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
484 && (d2_variable
485 || d2_zero
486 || !d1_variable))
487 ? t1
488 : t2));
489 /* Ensure a composite type involving a zero-length array type
490 is a zero-length type not an incomplete type. */
491 if (d1_zero && d2_zero
492 && (t1_complete || t2_complete)
493 && !COMPLETE_TYPE_P (t1))
494 {
495 TYPE_SIZE (t1) = bitsize_zero_node;
496 TYPE_SIZE_UNIT (t1) = size_zero_node;
497 }
498 t1 = c_build_qualified_type (t1, quals);
499 return build_type_attribute_variant (t1, attributes);
500 }
501
502 case ENUMERAL_TYPE:
503 case RECORD_TYPE:
504 case UNION_TYPE:
505 if (attributes != NULL)
506 {
507 /* Try harder not to create a new aggregate type. */
508 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
509 return t1;
510 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
511 return t2;
512 }
513 return build_type_attribute_variant (t1, attributes);
514
515 case FUNCTION_TYPE:
516 /* Function types: prefer the one that specified arg types.
517 If both do, merge the arg types. Also merge the return types. */
518 {
519 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
520 tree p1 = TYPE_ARG_TYPES (t1);
521 tree p2 = TYPE_ARG_TYPES (t2);
522 int len;
523 tree newargs, n;
524 int i;
525
526 /* Save space: see if the result is identical to one of the args. */
527 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
528 return build_functype_attribute_variant (t1, t2, attributes);
529 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
530 return build_functype_attribute_variant (t2, t1, attributes);
531
532 /* Simple way if one arg fails to specify argument types. */
533 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
534 {
535 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
536 t1 = build_type_attribute_variant (t1, attributes);
537 return qualify_type (t1, t2);
538 }
539 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
540 {
541 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
542 t1 = build_type_attribute_variant (t1, attributes);
543 return qualify_type (t1, t2);
544 }
545
546 /* If both args specify argument types, we must merge the two
547 lists, argument by argument. */
548
549 for (len = 0, newargs = p1;
550 newargs && newargs != void_list_node;
551 len++, newargs = TREE_CHAIN (newargs))
552 ;
553
554 for (i = 0; i < len; i++)
555 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
556
557 n = newargs;
558
559 for (; p1 && p1 != void_list_node;
560 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
561 {
562 /* A null type means arg type is not specified.
563 Take whatever the other function type has. */
564 if (TREE_VALUE (p1) == NULL_TREE)
565 {
566 TREE_VALUE (n) = TREE_VALUE (p2);
567 goto parm_done;
568 }
569 if (TREE_VALUE (p2) == NULL_TREE)
570 {
571 TREE_VALUE (n) = TREE_VALUE (p1);
572 goto parm_done;
573 }
574
575 /* Given wait (union {union wait *u; int *i} *)
576 and wait (union wait *),
577 prefer union wait * as type of parm. */
578 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
579 && TREE_VALUE (p1) != TREE_VALUE (p2))
580 {
581 tree memb;
582 tree mv2 = TREE_VALUE (p2);
583 if (mv2 && mv2 != error_mark_node
584 && TREE_CODE (mv2) != ARRAY_TYPE)
585 mv2 = TYPE_MAIN_VARIANT (mv2);
586 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
587 memb; memb = DECL_CHAIN (memb))
588 {
589 tree mv3 = TREE_TYPE (memb);
590 if (mv3 && mv3 != error_mark_node
591 && TREE_CODE (mv3) != ARRAY_TYPE)
592 mv3 = TYPE_MAIN_VARIANT (mv3);
593 if (comptypes (mv3, mv2))
594 {
595 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
596 TREE_VALUE (p2));
597 pedwarn (input_location, OPT_Wpedantic,
598 "function types not truly compatible in ISO C");
599 goto parm_done;
600 }
601 }
602 }
603 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
604 && TREE_VALUE (p2) != TREE_VALUE (p1))
605 {
606 tree memb;
607 tree mv1 = TREE_VALUE (p1);
608 if (mv1 && mv1 != error_mark_node
609 && TREE_CODE (mv1) != ARRAY_TYPE)
610 mv1 = TYPE_MAIN_VARIANT (mv1);
611 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
612 memb; memb = DECL_CHAIN (memb))
613 {
614 tree mv3 = TREE_TYPE (memb);
615 if (mv3 && mv3 != error_mark_node
616 && TREE_CODE (mv3) != ARRAY_TYPE)
617 mv3 = TYPE_MAIN_VARIANT (mv3);
618 if (comptypes (mv3, mv1))
619 {
620 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
621 TREE_VALUE (p1));
622 pedwarn (input_location, OPT_Wpedantic,
623 "function types not truly compatible in ISO C");
624 goto parm_done;
625 }
626 }
627 }
628 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
629 parm_done: ;
630 }
631
632 t1 = build_function_type (valtype, newargs);
633 t1 = qualify_type (t1, t2);
634 }
635 /* FALLTHRU */
636
637 default:
638 return build_type_attribute_variant (t1, attributes);
639 }
640
641 }
642
643 /* Return the type of a conditional expression between pointers to
644 possibly differently qualified versions of compatible types.
645
646 We assume that comp_target_types has already been done and returned
647 nonzero; if that isn't so, this may crash. */
648
649 static tree
common_pointer_type(tree t1,tree t2)650 common_pointer_type (tree t1, tree t2)
651 {
652 tree attributes;
653 tree pointed_to_1, mv1;
654 tree pointed_to_2, mv2;
655 tree target;
656 unsigned target_quals;
657 addr_space_t as1, as2, as_common;
658 int quals1, quals2;
659
660 /* Save time if the two types are the same. */
661
662 if (t1 == t2) return t1;
663
664 /* If one type is nonsense, use the other. */
665 if (t1 == error_mark_node)
666 return t2;
667 if (t2 == error_mark_node)
668 return t1;
669
670 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
671 && TREE_CODE (t2) == POINTER_TYPE);
672
673 /* Merge the attributes. */
674 attributes = targetm.merge_type_attributes (t1, t2);
675
676 /* Find the composite type of the target types, and combine the
677 qualifiers of the two types' targets. Do not lose qualifiers on
678 array element types by taking the TYPE_MAIN_VARIANT. */
679 mv1 = pointed_to_1 = TREE_TYPE (t1);
680 mv2 = pointed_to_2 = TREE_TYPE (t2);
681 if (TREE_CODE (mv1) != ARRAY_TYPE)
682 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
683 if (TREE_CODE (mv2) != ARRAY_TYPE)
684 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
685 target = composite_type (mv1, mv2);
686
687 /* Strip array types to get correct qualifier for pointers to arrays */
688 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
689 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
690
691 /* For function types do not merge const qualifiers, but drop them
692 if used inconsistently. The middle-end uses these to mark const
693 and noreturn functions. */
694 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
695 target_quals = (quals1 & quals2);
696 else
697 target_quals = (quals1 | quals2);
698
699 /* If the two named address spaces are different, determine the common
700 superset address space. This is guaranteed to exist due to the
701 assumption that comp_target_type returned non-zero. */
702 as1 = TYPE_ADDR_SPACE (pointed_to_1);
703 as2 = TYPE_ADDR_SPACE (pointed_to_2);
704 if (!addr_space_superset (as1, as2, &as_common))
705 gcc_unreachable ();
706
707 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
708
709 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
710 return build_type_attribute_variant (t1, attributes);
711 }
712
713 /* Return the common type for two arithmetic types under the usual
714 arithmetic conversions. The default conversions have already been
715 applied, and enumerated types converted to their compatible integer
716 types. The resulting type is unqualified and has no attributes.
717
718 This is the type for the result of most arithmetic operations
719 if the operands have the given two types. */
720
721 static tree
c_common_type(tree t1,tree t2)722 c_common_type (tree t1, tree t2)
723 {
724 enum tree_code code1;
725 enum tree_code code2;
726
727 /* If one type is nonsense, use the other. */
728 if (t1 == error_mark_node)
729 return t2;
730 if (t2 == error_mark_node)
731 return t1;
732
733 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
734 t1 = TYPE_MAIN_VARIANT (t1);
735
736 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
737 t2 = TYPE_MAIN_VARIANT (t2);
738
739 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
740 t1 = build_type_attribute_variant (t1, NULL_TREE);
741
742 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
743 t2 = build_type_attribute_variant (t2, NULL_TREE);
744
745 /* Save time if the two types are the same. */
746
747 if (t1 == t2) return t1;
748
749 code1 = TREE_CODE (t1);
750 code2 = TREE_CODE (t2);
751
752 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
753 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
754 || code1 == INTEGER_TYPE);
755 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
756 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
757 || code2 == INTEGER_TYPE);
758
759 /* When one operand is a decimal float type, the other operand cannot be
760 a generic float type or a complex type. We also disallow vector types
761 here. */
762 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
763 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
764 {
765 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
766 {
767 error ("cannot mix operands of decimal floating and vector types");
768 return error_mark_node;
769 }
770 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
771 {
772 error ("cannot mix operands of decimal floating and complex types");
773 return error_mark_node;
774 }
775 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
776 {
777 error ("cannot mix operands of decimal floating "
778 "and other floating types");
779 return error_mark_node;
780 }
781 }
782
783 /* If one type is a vector type, return that type. (How the usual
784 arithmetic conversions apply to the vector types extension is not
785 precisely specified.) */
786 if (code1 == VECTOR_TYPE)
787 return t1;
788
789 if (code2 == VECTOR_TYPE)
790 return t2;
791
792 /* If one type is complex, form the common type of the non-complex
793 components, then make that complex. Use T1 or T2 if it is the
794 required type. */
795 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
796 {
797 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
798 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
799 tree subtype = c_common_type (subtype1, subtype2);
800
801 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
802 return t1;
803 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
804 return t2;
805 else
806 return build_complex_type (subtype);
807 }
808
809 /* If only one is real, use it as the result. */
810
811 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
812 return t1;
813
814 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
815 return t2;
816
817 /* If both are real and either are decimal floating point types, use
818 the decimal floating point type with the greater precision. */
819
820 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
821 {
822 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
823 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
824 return dfloat128_type_node;
825 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
826 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
827 return dfloat64_type_node;
828 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
829 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
830 return dfloat32_type_node;
831 }
832
833 /* Deal with fixed-point types. */
834 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
835 {
836 unsigned int unsignedp = 0, satp = 0;
837 scalar_mode m1, m2;
838 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
839
840 m1 = SCALAR_TYPE_MODE (t1);
841 m2 = SCALAR_TYPE_MODE (t2);
842
843 /* If one input type is saturating, the result type is saturating. */
844 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
845 satp = 1;
846
847 /* If both fixed-point types are unsigned, the result type is unsigned.
848 When mixing fixed-point and integer types, follow the sign of the
849 fixed-point type.
850 Otherwise, the result type is signed. */
851 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
852 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
853 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
854 && TYPE_UNSIGNED (t1))
855 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
856 && TYPE_UNSIGNED (t2)))
857 unsignedp = 1;
858
859 /* The result type is signed. */
860 if (unsignedp == 0)
861 {
862 /* If the input type is unsigned, we need to convert to the
863 signed type. */
864 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
865 {
866 enum mode_class mclass = (enum mode_class) 0;
867 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
868 mclass = MODE_FRACT;
869 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
870 mclass = MODE_ACCUM;
871 else
872 gcc_unreachable ();
873 m1 = as_a <scalar_mode>
874 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
875 }
876 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
877 {
878 enum mode_class mclass = (enum mode_class) 0;
879 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
880 mclass = MODE_FRACT;
881 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
882 mclass = MODE_ACCUM;
883 else
884 gcc_unreachable ();
885 m2 = as_a <scalar_mode>
886 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
887 }
888 }
889
890 if (code1 == FIXED_POINT_TYPE)
891 {
892 fbit1 = GET_MODE_FBIT (m1);
893 ibit1 = GET_MODE_IBIT (m1);
894 }
895 else
896 {
897 fbit1 = 0;
898 /* Signed integers need to subtract one sign bit. */
899 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
900 }
901
902 if (code2 == FIXED_POINT_TYPE)
903 {
904 fbit2 = GET_MODE_FBIT (m2);
905 ibit2 = GET_MODE_IBIT (m2);
906 }
907 else
908 {
909 fbit2 = 0;
910 /* Signed integers need to subtract one sign bit. */
911 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
912 }
913
914 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
915 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
916 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
917 satp);
918 }
919
920 /* Both real or both integers; use the one with greater precision. */
921
922 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
923 return t1;
924 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
925 return t2;
926
927 /* Same precision. Prefer long longs to longs to ints when the
928 same precision, following the C99 rules on integer type rank
929 (which are equivalent to the C90 rules for C90 types). */
930
931 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
932 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
933 return long_long_unsigned_type_node;
934
935 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
936 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
937 {
938 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
939 return long_long_unsigned_type_node;
940 else
941 return long_long_integer_type_node;
942 }
943
944 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
945 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
946 return long_unsigned_type_node;
947
948 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
949 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
950 {
951 /* But preserve unsignedness from the other type,
952 since long cannot hold all the values of an unsigned int. */
953 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
954 return long_unsigned_type_node;
955 else
956 return long_integer_type_node;
957 }
958
959 /* For floating types of the same TYPE_PRECISION (which we here
960 assume means either the same set of values, or sets of values
961 neither a subset of the other, with behavior being undefined in
962 the latter case), follow the rules from TS 18661-3: prefer
963 interchange types _FloatN, then standard types long double,
964 double, float, then extended types _FloatNx. For extended types,
965 check them starting with _Float128x as that seems most consistent
966 in spirit with preferring long double to double; for interchange
967 types, also check in that order for consistency although it's not
968 possible for more than one of them to have the same
969 precision. */
970 tree mv1 = TYPE_MAIN_VARIANT (t1);
971 tree mv2 = TYPE_MAIN_VARIANT (t2);
972
973 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
974 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
975 return FLOATN_TYPE_NODE (i);
976
977 /* Likewise, prefer long double to double even if same size. */
978 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
979 return long_double_type_node;
980
981 /* Likewise, prefer double to float even if same size.
982 We got a couple of embedded targets with 32 bit doubles, and the
983 pdp11 might have 64 bit floats. */
984 if (mv1 == double_type_node || mv2 == double_type_node)
985 return double_type_node;
986
987 if (mv1 == float_type_node || mv2 == float_type_node)
988 return float_type_node;
989
990 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
991 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
992 return FLOATNX_TYPE_NODE (i);
993
994 /* Otherwise prefer the unsigned one. */
995
996 if (TYPE_UNSIGNED (t1))
997 return t1;
998 else
999 return t2;
1000 }
1001
1002 /* Wrapper around c_common_type that is used by c-common.c and other
1003 front end optimizations that remove promotions. ENUMERAL_TYPEs
1004 are allowed here and are converted to their compatible integer types.
1005 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1006 preferably a non-Boolean type as the common type. */
1007 tree
common_type(tree t1,tree t2)1008 common_type (tree t1, tree t2)
1009 {
1010 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1011 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1012 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1013 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1014
1015 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1016 if (TREE_CODE (t1) == BOOLEAN_TYPE
1017 && TREE_CODE (t2) == BOOLEAN_TYPE)
1018 return boolean_type_node;
1019
1020 /* If either type is BOOLEAN_TYPE, then return the other. */
1021 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1022 return t2;
1023 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1024 return t1;
1025
1026 return c_common_type (t1, t2);
1027 }
1028
1029 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1030 or various other operations. Return 2 if they are compatible
1031 but a warning may be needed if you use them together. */
1032
1033 int
comptypes(tree type1,tree type2)1034 comptypes (tree type1, tree type2)
1035 {
1036 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1037 int val;
1038
1039 val = comptypes_internal (type1, type2, NULL, NULL);
1040 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1041
1042 return val;
1043 }
1044
1045 /* Like comptypes, but if it returns non-zero because enum and int are
1046 compatible, it sets *ENUM_AND_INT_P to true. */
1047
1048 static int
comptypes_check_enum_int(tree type1,tree type2,bool * enum_and_int_p)1049 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1050 {
1051 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1052 int val;
1053
1054 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1055 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1056
1057 return val;
1058 }
1059
1060 /* Like comptypes, but if it returns nonzero for different types, it
1061 sets *DIFFERENT_TYPES_P to true. */
1062
1063 int
comptypes_check_different_types(tree type1,tree type2,bool * different_types_p)1064 comptypes_check_different_types (tree type1, tree type2,
1065 bool *different_types_p)
1066 {
1067 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1068 int val;
1069
1070 val = comptypes_internal (type1, type2, NULL, different_types_p);
1071 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1072
1073 return val;
1074 }
1075
1076 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1077 or various other operations. Return 2 if they are compatible
1078 but a warning may be needed if you use them together. If
1079 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1080 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1081 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1082 NULL, and the types are compatible but different enough not to be
1083 permitted in C11 typedef redeclarations, then this sets
1084 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1085 false, but may or may not be set if the types are incompatible.
1086 This differs from comptypes, in that we don't free the seen
1087 types. */
1088
1089 static int
comptypes_internal(const_tree type1,const_tree type2,bool * enum_and_int_p,bool * different_types_p)1090 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1091 bool *different_types_p)
1092 {
1093 const_tree t1 = type1;
1094 const_tree t2 = type2;
1095 int attrval, val;
1096
1097 /* Suppress errors caused by previously reported errors. */
1098
1099 if (t1 == t2 || !t1 || !t2
1100 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1101 return 1;
1102
1103 /* Enumerated types are compatible with integer types, but this is
1104 not transitive: two enumerated types in the same translation unit
1105 are compatible with each other only if they are the same type. */
1106
1107 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1108 {
1109 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1110 if (TREE_CODE (t2) != VOID_TYPE)
1111 {
1112 if (enum_and_int_p != NULL)
1113 *enum_and_int_p = true;
1114 if (different_types_p != NULL)
1115 *different_types_p = true;
1116 }
1117 }
1118 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1119 {
1120 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1121 if (TREE_CODE (t1) != VOID_TYPE)
1122 {
1123 if (enum_and_int_p != NULL)
1124 *enum_and_int_p = true;
1125 if (different_types_p != NULL)
1126 *different_types_p = true;
1127 }
1128 }
1129
1130 if (t1 == t2)
1131 return 1;
1132
1133 /* Different classes of types can't be compatible. */
1134
1135 if (TREE_CODE (t1) != TREE_CODE (t2))
1136 return 0;
1137
1138 /* Qualifiers must match. C99 6.7.3p9 */
1139
1140 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1141 return 0;
1142
1143 /* Allow for two different type nodes which have essentially the same
1144 definition. Note that we already checked for equality of the type
1145 qualifiers (just above). */
1146
1147 if (TREE_CODE (t1) != ARRAY_TYPE
1148 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1149 return 1;
1150
1151 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1152 if (!(attrval = comp_type_attributes (t1, t2)))
1153 return 0;
1154
1155 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1156 val = 0;
1157
1158 switch (TREE_CODE (t1))
1159 {
1160 case INTEGER_TYPE:
1161 case FIXED_POINT_TYPE:
1162 case REAL_TYPE:
1163 /* With these nodes, we can't determine type equivalence by
1164 looking at what is stored in the nodes themselves, because
1165 two nodes might have different TYPE_MAIN_VARIANTs but still
1166 represent the same type. For example, wchar_t and int could
1167 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1168 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1169 and are distinct types. On the other hand, int and the
1170 following typedef
1171
1172 typedef int INT __attribute((may_alias));
1173
1174 have identical properties, different TYPE_MAIN_VARIANTs, but
1175 represent the same type. The canonical type system keeps
1176 track of equivalence in this case, so we fall back on it. */
1177 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1178
1179 case POINTER_TYPE:
1180 /* Do not remove mode information. */
1181 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1182 break;
1183 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1184 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1185 enum_and_int_p, different_types_p));
1186 break;
1187
1188 case FUNCTION_TYPE:
1189 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1190 different_types_p);
1191 break;
1192
1193 case ARRAY_TYPE:
1194 {
1195 tree d1 = TYPE_DOMAIN (t1);
1196 tree d2 = TYPE_DOMAIN (t2);
1197 bool d1_variable, d2_variable;
1198 bool d1_zero, d2_zero;
1199 val = 1;
1200
1201 /* Target types must match incl. qualifiers. */
1202 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1203 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1204 enum_and_int_p,
1205 different_types_p)) == 0)
1206 return 0;
1207
1208 if (different_types_p != NULL
1209 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1210 *different_types_p = true;
1211 /* Sizes must match unless one is missing or variable. */
1212 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1213 break;
1214
1215 d1_zero = !TYPE_MAX_VALUE (d1);
1216 d2_zero = !TYPE_MAX_VALUE (d2);
1217
1218 d1_variable = (!d1_zero
1219 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1220 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1221 d2_variable = (!d2_zero
1222 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1223 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1224 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1225 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1226
1227 if (different_types_p != NULL
1228 && d1_variable != d2_variable)
1229 *different_types_p = true;
1230 if (d1_variable || d2_variable)
1231 break;
1232 if (d1_zero && d2_zero)
1233 break;
1234 if (d1_zero || d2_zero
1235 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1236 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1237 val = 0;
1238
1239 break;
1240 }
1241
1242 case ENUMERAL_TYPE:
1243 case RECORD_TYPE:
1244 case UNION_TYPE:
1245 if (val != 1 && !same_translation_unit_p (t1, t2))
1246 {
1247 tree a1 = TYPE_ATTRIBUTES (t1);
1248 tree a2 = TYPE_ATTRIBUTES (t2);
1249
1250 if (! attribute_list_contained (a1, a2)
1251 && ! attribute_list_contained (a2, a1))
1252 break;
1253
1254 if (attrval != 2)
1255 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1256 different_types_p);
1257 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1258 different_types_p);
1259 }
1260 break;
1261
1262 case VECTOR_TYPE:
1263 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1264 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1265 enum_and_int_p, different_types_p));
1266 break;
1267
1268 default:
1269 break;
1270 }
1271 return attrval == 2 && val == 1 ? 2 : val;
1272 }
1273
1274 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1275 their qualifiers, except for named address spaces. If the pointers point to
1276 different named addresses, then we must determine if one address space is a
1277 subset of the other. */
1278
1279 static int
comp_target_types(location_t location,tree ttl,tree ttr)1280 comp_target_types (location_t location, tree ttl, tree ttr)
1281 {
1282 int val;
1283 int val_ped;
1284 tree mvl = TREE_TYPE (ttl);
1285 tree mvr = TREE_TYPE (ttr);
1286 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1287 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1288 addr_space_t as_common;
1289 bool enum_and_int_p;
1290
1291 /* Fail if pointers point to incompatible address spaces. */
1292 if (!addr_space_superset (asl, asr, &as_common))
1293 return 0;
1294
1295 /* For pedantic record result of comptypes on arrays before losing
1296 qualifiers on the element type below. */
1297 val_ped = 1;
1298
1299 if (TREE_CODE (mvl) == ARRAY_TYPE
1300 && TREE_CODE (mvr) == ARRAY_TYPE)
1301 val_ped = comptypes (mvl, mvr);
1302
1303 /* Qualifiers on element types of array types that are
1304 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1305
1306 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1307 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1308 : TYPE_MAIN_VARIANT (mvl));
1309
1310 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1311 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1312 : TYPE_MAIN_VARIANT (mvr));
1313
1314 enum_and_int_p = false;
1315 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1316
1317 if (val == 1 && val_ped != 1)
1318 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1319 "are incompatible in ISO C");
1320
1321 if (val == 2)
1322 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1323
1324 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1325 warning_at (location, OPT_Wc___compat,
1326 "pointer target types incompatible in C++");
1327
1328 return val;
1329 }
1330
1331 /* Subroutines of `comptypes'. */
1332
1333 /* Determine whether two trees derive from the same translation unit.
1334 If the CONTEXT chain ends in a null, that tree's context is still
1335 being parsed, so if two trees have context chains ending in null,
1336 they're in the same translation unit. */
1337
1338 bool
same_translation_unit_p(const_tree t1,const_tree t2)1339 same_translation_unit_p (const_tree t1, const_tree t2)
1340 {
1341 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1342 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1343 {
1344 case tcc_declaration:
1345 t1 = DECL_CONTEXT (t1); break;
1346 case tcc_type:
1347 t1 = TYPE_CONTEXT (t1); break;
1348 case tcc_exceptional:
1349 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1350 default: gcc_unreachable ();
1351 }
1352
1353 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1354 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1355 {
1356 case tcc_declaration:
1357 t2 = DECL_CONTEXT (t2); break;
1358 case tcc_type:
1359 t2 = TYPE_CONTEXT (t2); break;
1360 case tcc_exceptional:
1361 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1362 default: gcc_unreachable ();
1363 }
1364
1365 return t1 == t2;
1366 }
1367
1368 /* Allocate the seen two types, assuming that they are compatible. */
1369
1370 static struct tagged_tu_seen_cache *
alloc_tagged_tu_seen_cache(const_tree t1,const_tree t2)1371 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1372 {
1373 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1374 tu->next = tagged_tu_seen_base;
1375 tu->t1 = t1;
1376 tu->t2 = t2;
1377
1378 tagged_tu_seen_base = tu;
1379
1380 /* The C standard says that two structures in different translation
1381 units are compatible with each other only if the types of their
1382 fields are compatible (among other things). We assume that they
1383 are compatible until proven otherwise when building the cache.
1384 An example where this can occur is:
1385 struct a
1386 {
1387 struct a *next;
1388 };
1389 If we are comparing this against a similar struct in another TU,
1390 and did not assume they were compatible, we end up with an infinite
1391 loop. */
1392 tu->val = 1;
1393 return tu;
1394 }
1395
1396 /* Free the seen types until we get to TU_TIL. */
1397
1398 static void
free_all_tagged_tu_seen_up_to(const struct tagged_tu_seen_cache * tu_til)1399 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1400 {
1401 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1402 while (tu != tu_til)
1403 {
1404 const struct tagged_tu_seen_cache *const tu1
1405 = (const struct tagged_tu_seen_cache *) tu;
1406 tu = tu1->next;
1407 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1408 }
1409 tagged_tu_seen_base = tu_til;
1410 }
1411
1412 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1413 compatible. If the two types are not the same (which has been
1414 checked earlier), this can only happen when multiple translation
1415 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1416 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1417 comptypes_internal. */
1418
1419 static int
tagged_types_tu_compatible_p(const_tree t1,const_tree t2,bool * enum_and_int_p,bool * different_types_p)1420 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1421 bool *enum_and_int_p, bool *different_types_p)
1422 {
1423 tree s1, s2;
1424 bool needs_warning = false;
1425
1426 /* We have to verify that the tags of the types are the same. This
1427 is harder than it looks because this may be a typedef, so we have
1428 to go look at the original type. It may even be a typedef of a
1429 typedef...
1430 In the case of compiler-created builtin structs the TYPE_DECL
1431 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1432 while (TYPE_NAME (t1)
1433 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1434 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1435 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1436
1437 while (TYPE_NAME (t2)
1438 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1439 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1440 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1441
1442 /* C90 didn't have the requirement that the two tags be the same. */
1443 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1444 return 0;
1445
1446 /* C90 didn't say what happened if one or both of the types were
1447 incomplete; we choose to follow C99 rules here, which is that they
1448 are compatible. */
1449 if (TYPE_SIZE (t1) == NULL
1450 || TYPE_SIZE (t2) == NULL)
1451 return 1;
1452
1453 {
1454 const struct tagged_tu_seen_cache * tts_i;
1455 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1456 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1457 return tts_i->val;
1458 }
1459
1460 switch (TREE_CODE (t1))
1461 {
1462 case ENUMERAL_TYPE:
1463 {
1464 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1465 /* Speed up the case where the type values are in the same order. */
1466 tree tv1 = TYPE_VALUES (t1);
1467 tree tv2 = TYPE_VALUES (t2);
1468
1469 if (tv1 == tv2)
1470 {
1471 return 1;
1472 }
1473
1474 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1475 {
1476 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1477 break;
1478 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1479 {
1480 tu->val = 0;
1481 return 0;
1482 }
1483 }
1484
1485 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1486 {
1487 return 1;
1488 }
1489 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1490 {
1491 tu->val = 0;
1492 return 0;
1493 }
1494
1495 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1496 {
1497 tu->val = 0;
1498 return 0;
1499 }
1500
1501 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1502 {
1503 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1504 if (s2 == NULL
1505 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1506 {
1507 tu->val = 0;
1508 return 0;
1509 }
1510 }
1511 return 1;
1512 }
1513
1514 case UNION_TYPE:
1515 {
1516 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1517 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1518 {
1519 tu->val = 0;
1520 return 0;
1521 }
1522
1523 /* Speed up the common case where the fields are in the same order. */
1524 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1525 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1526 {
1527 int result;
1528
1529 if (DECL_NAME (s1) != DECL_NAME (s2))
1530 break;
1531 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1532 enum_and_int_p, different_types_p);
1533
1534 if (result != 1 && !DECL_NAME (s1))
1535 break;
1536 if (result == 0)
1537 {
1538 tu->val = 0;
1539 return 0;
1540 }
1541 if (result == 2)
1542 needs_warning = true;
1543
1544 if (TREE_CODE (s1) == FIELD_DECL
1545 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1546 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1547 {
1548 tu->val = 0;
1549 return 0;
1550 }
1551 }
1552 if (!s1 && !s2)
1553 {
1554 tu->val = needs_warning ? 2 : 1;
1555 return tu->val;
1556 }
1557
1558 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1559 {
1560 bool ok = false;
1561
1562 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1563 if (DECL_NAME (s1) == DECL_NAME (s2))
1564 {
1565 int result;
1566
1567 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1568 enum_and_int_p,
1569 different_types_p);
1570
1571 if (result != 1 && !DECL_NAME (s1))
1572 continue;
1573 if (result == 0)
1574 {
1575 tu->val = 0;
1576 return 0;
1577 }
1578 if (result == 2)
1579 needs_warning = true;
1580
1581 if (TREE_CODE (s1) == FIELD_DECL
1582 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1583 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1584 break;
1585
1586 ok = true;
1587 break;
1588 }
1589 if (!ok)
1590 {
1591 tu->val = 0;
1592 return 0;
1593 }
1594 }
1595 tu->val = needs_warning ? 2 : 10;
1596 return tu->val;
1597 }
1598
1599 case RECORD_TYPE:
1600 {
1601 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1602
1603 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1604 s1 && s2;
1605 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1606 {
1607 int result;
1608 if (TREE_CODE (s1) != TREE_CODE (s2)
1609 || DECL_NAME (s1) != DECL_NAME (s2))
1610 break;
1611 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1612 enum_and_int_p, different_types_p);
1613 if (result == 0)
1614 break;
1615 if (result == 2)
1616 needs_warning = true;
1617
1618 if (TREE_CODE (s1) == FIELD_DECL
1619 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1620 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1621 break;
1622 }
1623 if (s1 && s2)
1624 tu->val = 0;
1625 else
1626 tu->val = needs_warning ? 2 : 1;
1627 return tu->val;
1628 }
1629
1630 default:
1631 gcc_unreachable ();
1632 }
1633 }
1634
1635 /* Return 1 if two function types F1 and F2 are compatible.
1636 If either type specifies no argument types,
1637 the other must specify a fixed number of self-promoting arg types.
1638 Otherwise, if one type specifies only the number of arguments,
1639 the other must specify that number of self-promoting arg types.
1640 Otherwise, the argument types must match.
1641 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1642
1643 static int
function_types_compatible_p(const_tree f1,const_tree f2,bool * enum_and_int_p,bool * different_types_p)1644 function_types_compatible_p (const_tree f1, const_tree f2,
1645 bool *enum_and_int_p, bool *different_types_p)
1646 {
1647 tree args1, args2;
1648 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1649 int val = 1;
1650 int val1;
1651 tree ret1, ret2;
1652
1653 ret1 = TREE_TYPE (f1);
1654 ret2 = TREE_TYPE (f2);
1655
1656 /* 'volatile' qualifiers on a function's return type used to mean
1657 the function is noreturn. */
1658 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1659 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1660 if (TYPE_VOLATILE (ret1))
1661 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1662 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1663 if (TYPE_VOLATILE (ret2))
1664 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1665 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1666 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1667 if (val == 0)
1668 return 0;
1669
1670 args1 = TYPE_ARG_TYPES (f1);
1671 args2 = TYPE_ARG_TYPES (f2);
1672
1673 if (different_types_p != NULL
1674 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1675 *different_types_p = true;
1676
1677 /* An unspecified parmlist matches any specified parmlist
1678 whose argument types don't need default promotions. */
1679
1680 if (args1 == NULL_TREE)
1681 {
1682 if (!self_promoting_args_p (args2))
1683 return 0;
1684 /* If one of these types comes from a non-prototype fn definition,
1685 compare that with the other type's arglist.
1686 If they don't match, ask for a warning (but no error). */
1687 if (TYPE_ACTUAL_ARG_TYPES (f1)
1688 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1689 enum_and_int_p, different_types_p) != 1)
1690 val = 2;
1691 return val;
1692 }
1693 if (args2 == NULL_TREE)
1694 {
1695 if (!self_promoting_args_p (args1))
1696 return 0;
1697 if (TYPE_ACTUAL_ARG_TYPES (f2)
1698 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1699 enum_and_int_p, different_types_p) != 1)
1700 val = 2;
1701 return val;
1702 }
1703
1704 /* Both types have argument lists: compare them and propagate results. */
1705 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1706 different_types_p);
1707 return val1 != 1 ? val1 : val;
1708 }
1709
1710 /* Check two lists of types for compatibility, returning 0 for
1711 incompatible, 1 for compatible, or 2 for compatible with
1712 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1713 comptypes_internal. */
1714
1715 static int
type_lists_compatible_p(const_tree args1,const_tree args2,bool * enum_and_int_p,bool * different_types_p)1716 type_lists_compatible_p (const_tree args1, const_tree args2,
1717 bool *enum_and_int_p, bool *different_types_p)
1718 {
1719 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1720 int val = 1;
1721 int newval = 0;
1722
1723 while (1)
1724 {
1725 tree a1, mv1, a2, mv2;
1726 if (args1 == NULL_TREE && args2 == NULL_TREE)
1727 return val;
1728 /* If one list is shorter than the other,
1729 they fail to match. */
1730 if (args1 == NULL_TREE || args2 == NULL_TREE)
1731 return 0;
1732 mv1 = a1 = TREE_VALUE (args1);
1733 mv2 = a2 = TREE_VALUE (args2);
1734 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1735 mv1 = (TYPE_ATOMIC (mv1)
1736 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1737 TYPE_QUAL_ATOMIC)
1738 : TYPE_MAIN_VARIANT (mv1));
1739 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1740 mv2 = (TYPE_ATOMIC (mv2)
1741 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1742 TYPE_QUAL_ATOMIC)
1743 : TYPE_MAIN_VARIANT (mv2));
1744 /* A null pointer instead of a type
1745 means there is supposed to be an argument
1746 but nothing is specified about what type it has.
1747 So match anything that self-promotes. */
1748 if (different_types_p != NULL
1749 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1750 *different_types_p = true;
1751 if (a1 == NULL_TREE)
1752 {
1753 if (c_type_promotes_to (a2) != a2)
1754 return 0;
1755 }
1756 else if (a2 == NULL_TREE)
1757 {
1758 if (c_type_promotes_to (a1) != a1)
1759 return 0;
1760 }
1761 /* If one of the lists has an error marker, ignore this arg. */
1762 else if (TREE_CODE (a1) == ERROR_MARK
1763 || TREE_CODE (a2) == ERROR_MARK)
1764 ;
1765 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1766 different_types_p)))
1767 {
1768 if (different_types_p != NULL)
1769 *different_types_p = true;
1770 /* Allow wait (union {union wait *u; int *i} *)
1771 and wait (union wait *) to be compatible. */
1772 if (TREE_CODE (a1) == UNION_TYPE
1773 && (TYPE_NAME (a1) == NULL_TREE
1774 || TYPE_TRANSPARENT_AGGR (a1))
1775 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1776 && tree_int_cst_equal (TYPE_SIZE (a1),
1777 TYPE_SIZE (a2)))
1778 {
1779 tree memb;
1780 for (memb = TYPE_FIELDS (a1);
1781 memb; memb = DECL_CHAIN (memb))
1782 {
1783 tree mv3 = TREE_TYPE (memb);
1784 if (mv3 && mv3 != error_mark_node
1785 && TREE_CODE (mv3) != ARRAY_TYPE)
1786 mv3 = (TYPE_ATOMIC (mv3)
1787 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1788 TYPE_QUAL_ATOMIC)
1789 : TYPE_MAIN_VARIANT (mv3));
1790 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1791 different_types_p))
1792 break;
1793 }
1794 if (memb == NULL_TREE)
1795 return 0;
1796 }
1797 else if (TREE_CODE (a2) == UNION_TYPE
1798 && (TYPE_NAME (a2) == NULL_TREE
1799 || TYPE_TRANSPARENT_AGGR (a2))
1800 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1801 && tree_int_cst_equal (TYPE_SIZE (a2),
1802 TYPE_SIZE (a1)))
1803 {
1804 tree memb;
1805 for (memb = TYPE_FIELDS (a2);
1806 memb; memb = DECL_CHAIN (memb))
1807 {
1808 tree mv3 = TREE_TYPE (memb);
1809 if (mv3 && mv3 != error_mark_node
1810 && TREE_CODE (mv3) != ARRAY_TYPE)
1811 mv3 = (TYPE_ATOMIC (mv3)
1812 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1813 TYPE_QUAL_ATOMIC)
1814 : TYPE_MAIN_VARIANT (mv3));
1815 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1816 different_types_p))
1817 break;
1818 }
1819 if (memb == NULL_TREE)
1820 return 0;
1821 }
1822 else
1823 return 0;
1824 }
1825
1826 /* comptypes said ok, but record if it said to warn. */
1827 if (newval > val)
1828 val = newval;
1829
1830 args1 = TREE_CHAIN (args1);
1831 args2 = TREE_CHAIN (args2);
1832 }
1833 }
1834
1835 /* Compute the size to increment a pointer by. When a function type or void
1836 type or incomplete type is passed, size_one_node is returned.
1837 This function does not emit any diagnostics; the caller is responsible
1838 for that. */
1839
1840 static tree
c_size_in_bytes(const_tree type)1841 c_size_in_bytes (const_tree type)
1842 {
1843 enum tree_code code = TREE_CODE (type);
1844
1845 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1846 || !COMPLETE_TYPE_P (type))
1847 return size_one_node;
1848
1849 /* Convert in case a char is more than one unit. */
1850 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1851 size_int (TYPE_PRECISION (char_type_node)
1852 / BITS_PER_UNIT));
1853 }
1854
1855 /* Return either DECL or its known constant value (if it has one). */
1856
1857 tree
decl_constant_value_1(tree decl,bool in_init)1858 decl_constant_value_1 (tree decl, bool in_init)
1859 {
1860 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1861 TREE_CODE (decl) != PARM_DECL
1862 && !TREE_THIS_VOLATILE (decl)
1863 && TREE_READONLY (decl)
1864 && DECL_INITIAL (decl) != NULL_TREE
1865 && !error_operand_p (DECL_INITIAL (decl))
1866 /* This is invalid if initial value is not constant.
1867 If it has either a function call, a memory reference,
1868 or a variable, then re-evaluating it could give different results. */
1869 && TREE_CONSTANT (DECL_INITIAL (decl))
1870 /* Check for cases where this is sub-optimal, even though valid. */
1871 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1872 return DECL_INITIAL (decl);
1873 return decl;
1874 }
1875
1876 /* Return either DECL or its known constant value (if it has one).
1877 Like the above, but always return decl outside of functions. */
1878
1879 tree
decl_constant_value(tree decl)1880 decl_constant_value (tree decl)
1881 {
1882 /* Don't change a variable array bound or initial value to a constant
1883 in a place where a variable is invalid. */
1884 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1885 }
1886
1887 /* Convert the array expression EXP to a pointer. */
1888 static tree
array_to_pointer_conversion(location_t loc,tree exp)1889 array_to_pointer_conversion (location_t loc, tree exp)
1890 {
1891 tree orig_exp = exp;
1892 tree type = TREE_TYPE (exp);
1893 tree adr;
1894 tree restype = TREE_TYPE (type);
1895 tree ptrtype;
1896
1897 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1898
1899 STRIP_TYPE_NOPS (exp);
1900
1901 if (TREE_NO_WARNING (orig_exp))
1902 TREE_NO_WARNING (exp) = 1;
1903
1904 ptrtype = build_pointer_type (restype);
1905
1906 if (INDIRECT_REF_P (exp))
1907 return convert (ptrtype, TREE_OPERAND (exp, 0));
1908
1909 /* In C++ array compound literals are temporary objects unless they are
1910 const or appear in namespace scope, so they are destroyed too soon
1911 to use them for much of anything (c++/53220). */
1912 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1913 {
1914 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1915 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1916 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1917 "converting an array compound literal to a pointer "
1918 "is ill-formed in C++");
1919 }
1920
1921 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1922 return convert (ptrtype, adr);
1923 }
1924
1925 /* Convert the function expression EXP to a pointer. */
1926 static tree
function_to_pointer_conversion(location_t loc,tree exp)1927 function_to_pointer_conversion (location_t loc, tree exp)
1928 {
1929 tree orig_exp = exp;
1930
1931 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1932
1933 STRIP_TYPE_NOPS (exp);
1934
1935 if (TREE_NO_WARNING (orig_exp))
1936 TREE_NO_WARNING (exp) = 1;
1937
1938 return build_unary_op (loc, ADDR_EXPR, exp, false);
1939 }
1940
1941 /* Mark EXP as read, not just set, for set but not used -Wunused
1942 warning purposes. */
1943
1944 void
mark_exp_read(tree exp)1945 mark_exp_read (tree exp)
1946 {
1947 switch (TREE_CODE (exp))
1948 {
1949 case VAR_DECL:
1950 case PARM_DECL:
1951 DECL_READ_P (exp) = 1;
1952 break;
1953 case ARRAY_REF:
1954 case COMPONENT_REF:
1955 case MODIFY_EXPR:
1956 case REALPART_EXPR:
1957 case IMAGPART_EXPR:
1958 CASE_CONVERT:
1959 case ADDR_EXPR:
1960 case VIEW_CONVERT_EXPR:
1961 mark_exp_read (TREE_OPERAND (exp, 0));
1962 break;
1963 case COMPOUND_EXPR:
1964 /* Pattern match what build_atomic_assign produces with modifycode
1965 NOP_EXPR. */
1966 if (VAR_P (TREE_OPERAND (exp, 1))
1967 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1968 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1969 {
1970 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1971 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1972 if (TREE_CODE (t1) == TARGET_EXPR
1973 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1974 && TREE_CODE (t2) == CALL_EXPR)
1975 {
1976 tree fndecl = get_callee_fndecl (t2);
1977 tree arg = NULL_TREE;
1978 if (fndecl
1979 && TREE_CODE (fndecl) == FUNCTION_DECL
1980 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1981 && call_expr_nargs (t2) >= 2)
1982 switch (DECL_FUNCTION_CODE (fndecl))
1983 {
1984 case BUILT_IN_ATOMIC_STORE:
1985 arg = CALL_EXPR_ARG (t2, 1);
1986 break;
1987 case BUILT_IN_ATOMIC_STORE_1:
1988 case BUILT_IN_ATOMIC_STORE_2:
1989 case BUILT_IN_ATOMIC_STORE_4:
1990 case BUILT_IN_ATOMIC_STORE_8:
1991 case BUILT_IN_ATOMIC_STORE_16:
1992 arg = CALL_EXPR_ARG (t2, 0);
1993 break;
1994 default:
1995 break;
1996 }
1997 if (arg)
1998 {
1999 STRIP_NOPS (arg);
2000 if (TREE_CODE (arg) == ADDR_EXPR
2001 && DECL_P (TREE_OPERAND (arg, 0))
2002 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2003 mark_exp_read (TREE_OPERAND (arg, 0));
2004 }
2005 }
2006 }
2007 /* FALLTHRU */
2008 case C_MAYBE_CONST_EXPR:
2009 mark_exp_read (TREE_OPERAND (exp, 1));
2010 break;
2011 default:
2012 break;
2013 }
2014 }
2015
2016 /* Perform the default conversion of arrays and functions to pointers.
2017 Return the result of converting EXP. For any other expression, just
2018 return EXP.
2019
2020 LOC is the location of the expression. */
2021
2022 struct c_expr
default_function_array_conversion(location_t loc,struct c_expr exp)2023 default_function_array_conversion (location_t loc, struct c_expr exp)
2024 {
2025 tree orig_exp = exp.value;
2026 tree type = TREE_TYPE (exp.value);
2027 enum tree_code code = TREE_CODE (type);
2028
2029 switch (code)
2030 {
2031 case ARRAY_TYPE:
2032 {
2033 bool not_lvalue = false;
2034 bool lvalue_array_p;
2035
2036 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2037 || CONVERT_EXPR_P (exp.value))
2038 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2039 {
2040 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2041 not_lvalue = true;
2042 exp.value = TREE_OPERAND (exp.value, 0);
2043 }
2044
2045 if (TREE_NO_WARNING (orig_exp))
2046 TREE_NO_WARNING (exp.value) = 1;
2047
2048 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2049 if (!flag_isoc99 && !lvalue_array_p)
2050 {
2051 /* Before C99, non-lvalue arrays do not decay to pointers.
2052 Normally, using such an array would be invalid; but it can
2053 be used correctly inside sizeof or as a statement expression.
2054 Thus, do not give an error here; an error will result later. */
2055 return exp;
2056 }
2057
2058 exp.value = array_to_pointer_conversion (loc, exp.value);
2059 }
2060 break;
2061 case FUNCTION_TYPE:
2062 exp.value = function_to_pointer_conversion (loc, exp.value);
2063 break;
2064 default:
2065 break;
2066 }
2067
2068 return exp;
2069 }
2070
2071 struct c_expr
default_function_array_read_conversion(location_t loc,struct c_expr exp)2072 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2073 {
2074 mark_exp_read (exp.value);
2075 return default_function_array_conversion (loc, exp);
2076 }
2077
2078 /* Return whether EXPR should be treated as an atomic lvalue for the
2079 purposes of load and store handling. */
2080
2081 static bool
really_atomic_lvalue(tree expr)2082 really_atomic_lvalue (tree expr)
2083 {
2084 if (error_operand_p (expr))
2085 return false;
2086 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2087 return false;
2088 if (!lvalue_p (expr))
2089 return false;
2090
2091 /* Ignore _Atomic on register variables, since their addresses can't
2092 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2093 sequences wouldn't work. Ignore _Atomic on structures containing
2094 bit-fields, since accessing elements of atomic structures or
2095 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2096 it's undefined at translation time or execution time, and the
2097 normal atomic sequences again wouldn't work. */
2098 while (handled_component_p (expr))
2099 {
2100 if (TREE_CODE (expr) == COMPONENT_REF
2101 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2102 return false;
2103 expr = TREE_OPERAND (expr, 0);
2104 }
2105 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2106 return false;
2107 return true;
2108 }
2109
2110 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2111 including converting functions and arrays to pointers if CONVERT_P.
2112 If READ_P, also mark the expression as having been read. */
2113
2114 struct c_expr
convert_lvalue_to_rvalue(location_t loc,struct c_expr exp,bool convert_p,bool read_p)2115 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2116 bool convert_p, bool read_p)
2117 {
2118 if (read_p)
2119 mark_exp_read (exp.value);
2120 if (convert_p)
2121 exp = default_function_array_conversion (loc, exp);
2122 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2123 exp.value = require_complete_type (loc, exp.value);
2124 if (really_atomic_lvalue (exp.value))
2125 {
2126 vec<tree, va_gc> *params;
2127 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2128 tree expr_type = TREE_TYPE (exp.value);
2129 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2130 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2131
2132 gcc_assert (TYPE_ATOMIC (expr_type));
2133
2134 /* Expansion of a generic atomic load may require an addition
2135 element, so allocate enough to prevent a resize. */
2136 vec_alloc (params, 4);
2137
2138 /* Remove the qualifiers for the rest of the expressions and
2139 create the VAL temp variable to hold the RHS. */
2140 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2141 tmp = create_tmp_var_raw (nonatomic_type);
2142 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2143 TREE_ADDRESSABLE (tmp) = 1;
2144 TREE_NO_WARNING (tmp) = 1;
2145
2146 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2147 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2148 params->quick_push (expr_addr);
2149 params->quick_push (tmp_addr);
2150 params->quick_push (seq_cst);
2151 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2152
2153 /* EXPR is always read. */
2154 mark_exp_read (exp.value);
2155
2156 /* Return tmp which contains the value loaded. */
2157 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2158 NULL_TREE, NULL_TREE);
2159 }
2160 return exp;
2161 }
2162
2163 /* EXP is an expression of integer type. Apply the integer promotions
2164 to it and return the promoted value. */
2165
2166 tree
perform_integral_promotions(tree exp)2167 perform_integral_promotions (tree exp)
2168 {
2169 tree type = TREE_TYPE (exp);
2170 enum tree_code code = TREE_CODE (type);
2171
2172 gcc_assert (INTEGRAL_TYPE_P (type));
2173
2174 /* Normally convert enums to int,
2175 but convert wide enums to something wider. */
2176 if (code == ENUMERAL_TYPE)
2177 {
2178 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2179 TYPE_PRECISION (integer_type_node)),
2180 ((TYPE_PRECISION (type)
2181 >= TYPE_PRECISION (integer_type_node))
2182 && TYPE_UNSIGNED (type)));
2183
2184 return convert (type, exp);
2185 }
2186
2187 /* ??? This should no longer be needed now bit-fields have their
2188 proper types. */
2189 if (TREE_CODE (exp) == COMPONENT_REF
2190 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2191 /* If it's thinner than an int, promote it like a
2192 c_promoting_integer_type_p, otherwise leave it alone. */
2193 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2194 TYPE_PRECISION (integer_type_node)) < 0)
2195 return convert (integer_type_node, exp);
2196
2197 if (c_promoting_integer_type_p (type))
2198 {
2199 /* Preserve unsignedness if not really getting any wider. */
2200 if (TYPE_UNSIGNED (type)
2201 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2202 return convert (unsigned_type_node, exp);
2203
2204 return convert (integer_type_node, exp);
2205 }
2206
2207 return exp;
2208 }
2209
2210
2211 /* Perform default promotions for C data used in expressions.
2212 Enumeral types or short or char are converted to int.
2213 In addition, manifest constants symbols are replaced by their values. */
2214
2215 tree
default_conversion(tree exp)2216 default_conversion (tree exp)
2217 {
2218 tree orig_exp;
2219 tree type = TREE_TYPE (exp);
2220 enum tree_code code = TREE_CODE (type);
2221 tree promoted_type;
2222
2223 mark_exp_read (exp);
2224
2225 /* Functions and arrays have been converted during parsing. */
2226 gcc_assert (code != FUNCTION_TYPE);
2227 if (code == ARRAY_TYPE)
2228 return exp;
2229
2230 /* Constants can be used directly unless they're not loadable. */
2231 if (TREE_CODE (exp) == CONST_DECL)
2232 exp = DECL_INITIAL (exp);
2233
2234 /* Strip no-op conversions. */
2235 orig_exp = exp;
2236 STRIP_TYPE_NOPS (exp);
2237
2238 if (TREE_NO_WARNING (orig_exp))
2239 TREE_NO_WARNING (exp) = 1;
2240
2241 if (code == VOID_TYPE)
2242 {
2243 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2244 "void value not ignored as it ought to be");
2245 return error_mark_node;
2246 }
2247
2248 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2249 if (exp == error_mark_node)
2250 return error_mark_node;
2251
2252 promoted_type = targetm.promoted_type (type);
2253 if (promoted_type)
2254 return convert (promoted_type, exp);
2255
2256 if (INTEGRAL_TYPE_P (type))
2257 return perform_integral_promotions (exp);
2258
2259 return exp;
2260 }
2261
2262 /* Look up COMPONENT in a structure or union TYPE.
2263
2264 If the component name is not found, returns NULL_TREE. Otherwise,
2265 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2266 stepping down the chain to the component, which is in the last
2267 TREE_VALUE of the list. Normally the list is of length one, but if
2268 the component is embedded within (nested) anonymous structures or
2269 unions, the list steps down the chain to the component. */
2270
2271 static tree
lookup_field(tree type,tree component)2272 lookup_field (tree type, tree component)
2273 {
2274 tree field;
2275
2276 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2277 to the field elements. Use a binary search on this array to quickly
2278 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2279 will always be set for structures which have many elements.
2280
2281 Duplicate field checking replaces duplicates with NULL_TREE so
2282 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2283 case just iterate using DECL_CHAIN. */
2284
2285 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2286 && !seen_error ())
2287 {
2288 int bot, top, half;
2289 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2290
2291 field = TYPE_FIELDS (type);
2292 bot = 0;
2293 top = TYPE_LANG_SPECIFIC (type)->s->len;
2294 while (top - bot > 1)
2295 {
2296 half = (top - bot + 1) >> 1;
2297 field = field_array[bot+half];
2298
2299 if (DECL_NAME (field) == NULL_TREE)
2300 {
2301 /* Step through all anon unions in linear fashion. */
2302 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2303 {
2304 field = field_array[bot++];
2305 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2306 {
2307 tree anon = lookup_field (TREE_TYPE (field), component);
2308
2309 if (anon)
2310 return tree_cons (NULL_TREE, field, anon);
2311
2312 /* The Plan 9 compiler permits referring
2313 directly to an anonymous struct/union field
2314 using a typedef name. */
2315 if (flag_plan9_extensions
2316 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2317 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2318 == TYPE_DECL)
2319 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2320 == component))
2321 break;
2322 }
2323 }
2324
2325 /* Entire record is only anon unions. */
2326 if (bot > top)
2327 return NULL_TREE;
2328
2329 /* Restart the binary search, with new lower bound. */
2330 continue;
2331 }
2332
2333 if (DECL_NAME (field) == component)
2334 break;
2335 if (DECL_NAME (field) < component)
2336 bot += half;
2337 else
2338 top = bot + half;
2339 }
2340
2341 if (DECL_NAME (field_array[bot]) == component)
2342 field = field_array[bot];
2343 else if (DECL_NAME (field) != component)
2344 return NULL_TREE;
2345 }
2346 else
2347 {
2348 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2349 {
2350 if (DECL_NAME (field) == NULL_TREE
2351 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2352 {
2353 tree anon = lookup_field (TREE_TYPE (field), component);
2354
2355 if (anon)
2356 return tree_cons (NULL_TREE, field, anon);
2357
2358 /* The Plan 9 compiler permits referring directly to an
2359 anonymous struct/union field using a typedef
2360 name. */
2361 if (flag_plan9_extensions
2362 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2363 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2364 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2365 == component))
2366 break;
2367 }
2368
2369 if (DECL_NAME (field) == component)
2370 break;
2371 }
2372
2373 if (field == NULL_TREE)
2374 return NULL_TREE;
2375 }
2376
2377 return tree_cons (NULL_TREE, field, NULL_TREE);
2378 }
2379
2380 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2381
2382 static void
lookup_field_fuzzy_find_candidates(tree type,tree component,vec<tree> * candidates)2383 lookup_field_fuzzy_find_candidates (tree type, tree component,
2384 vec<tree> *candidates)
2385 {
2386 tree field;
2387 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2388 {
2389 if (DECL_NAME (field) == NULL_TREE
2390 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2391 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2392 candidates);
2393
2394 if (DECL_NAME (field))
2395 candidates->safe_push (DECL_NAME (field));
2396 }
2397 }
2398
2399 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2400 rather than returning a TREE_LIST for an exact match. */
2401
2402 static tree
lookup_field_fuzzy(tree type,tree component)2403 lookup_field_fuzzy (tree type, tree component)
2404 {
2405 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2406
2407 /* First, gather a list of candidates. */
2408 auto_vec <tree> candidates;
2409
2410 lookup_field_fuzzy_find_candidates (type, component,
2411 &candidates);
2412
2413 return find_closest_identifier (component, &candidates);
2414 }
2415
2416 /* Support function for build_component_ref's error-handling.
2417
2418 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2419 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2420
2421 static bool
should_suggest_deref_p(tree datum_type)2422 should_suggest_deref_p (tree datum_type)
2423 {
2424 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2425 allows "." for ptrs; we could be handling a failed attempt
2426 to access a property. */
2427 if (c_dialect_objc ())
2428 return false;
2429
2430 /* Only suggest it for pointers... */
2431 if (TREE_CODE (datum_type) != POINTER_TYPE)
2432 return false;
2433
2434 /* ...to structs/unions. */
2435 tree underlying_type = TREE_TYPE (datum_type);
2436 enum tree_code code = TREE_CODE (underlying_type);
2437 if (code == RECORD_TYPE || code == UNION_TYPE)
2438 return true;
2439 else
2440 return false;
2441 }
2442
2443 /* Make an expression to refer to the COMPONENT field of structure or
2444 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2445 location of the COMPONENT_REF. COMPONENT_LOC is the location
2446 of COMPONENT. */
2447
2448 tree
build_component_ref(location_t loc,tree datum,tree component,location_t component_loc)2449 build_component_ref (location_t loc, tree datum, tree component,
2450 location_t component_loc)
2451 {
2452 tree type = TREE_TYPE (datum);
2453 enum tree_code code = TREE_CODE (type);
2454 tree field = NULL;
2455 tree ref;
2456 bool datum_lvalue = lvalue_p (datum);
2457
2458 if (!objc_is_public (datum, component))
2459 return error_mark_node;
2460
2461 /* Detect Objective-C property syntax object.property. */
2462 if (c_dialect_objc ()
2463 && (ref = objc_maybe_build_component_ref (datum, component)))
2464 return ref;
2465
2466 /* See if there is a field or component with name COMPONENT. */
2467
2468 if (code == RECORD_TYPE || code == UNION_TYPE)
2469 {
2470 if (!COMPLETE_TYPE_P (type))
2471 {
2472 c_incomplete_type_error (loc, NULL_TREE, type);
2473 return error_mark_node;
2474 }
2475
2476 field = lookup_field (type, component);
2477
2478 if (!field)
2479 {
2480 tree guessed_id = lookup_field_fuzzy (type, component);
2481 if (guessed_id)
2482 {
2483 /* Attempt to provide a fixit replacement hint, if
2484 we have a valid range for the component. */
2485 location_t reported_loc
2486 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2487 gcc_rich_location rich_loc (reported_loc);
2488 if (component_loc != UNKNOWN_LOCATION)
2489 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2490 error_at (&rich_loc,
2491 "%qT has no member named %qE; did you mean %qE?",
2492 type, component, guessed_id);
2493 }
2494 else
2495 error_at (loc, "%qT has no member named %qE", type, component);
2496 return error_mark_node;
2497 }
2498
2499 /* Accessing elements of atomic structures or unions is undefined
2500 behavior (C11 6.5.2.3#5). */
2501 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2502 {
2503 if (code == RECORD_TYPE)
2504 warning_at (loc, 0, "accessing a member %qE of an atomic "
2505 "structure %qE", component, datum);
2506 else
2507 warning_at (loc, 0, "accessing a member %qE of an atomic "
2508 "union %qE", component, datum);
2509 }
2510
2511 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2512 This might be better solved in future the way the C++ front
2513 end does it - by giving the anonymous entities each a
2514 separate name and type, and then have build_component_ref
2515 recursively call itself. We can't do that here. */
2516 do
2517 {
2518 tree subdatum = TREE_VALUE (field);
2519 int quals;
2520 tree subtype;
2521 bool use_datum_quals;
2522
2523 if (TREE_TYPE (subdatum) == error_mark_node)
2524 return error_mark_node;
2525
2526 /* If this is an rvalue, it does not have qualifiers in C
2527 standard terms and we must avoid propagating such
2528 qualifiers down to a non-lvalue array that is then
2529 converted to a pointer. */
2530 use_datum_quals = (datum_lvalue
2531 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2532
2533 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2534 if (use_datum_quals)
2535 quals |= TYPE_QUALS (TREE_TYPE (datum));
2536 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2537
2538 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2539 NULL_TREE);
2540 SET_EXPR_LOCATION (ref, loc);
2541 if (TREE_READONLY (subdatum)
2542 || (use_datum_quals && TREE_READONLY (datum)))
2543 TREE_READONLY (ref) = 1;
2544 if (TREE_THIS_VOLATILE (subdatum)
2545 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2546 TREE_THIS_VOLATILE (ref) = 1;
2547
2548 if (TREE_DEPRECATED (subdatum))
2549 warn_deprecated_use (subdatum, NULL_TREE);
2550
2551 datum = ref;
2552
2553 field = TREE_CHAIN (field);
2554 }
2555 while (field);
2556
2557 return ref;
2558 }
2559 else if (should_suggest_deref_p (type))
2560 {
2561 /* Special-case the error message for "ptr.field" for the case
2562 where the user has confused "." vs "->". */
2563 rich_location richloc (line_table, loc);
2564 /* "loc" should be the "." token. */
2565 richloc.add_fixit_replace ("->");
2566 error_at (&richloc,
2567 "%qE is a pointer; did you mean to use %<->%>?",
2568 datum);
2569 return error_mark_node;
2570 }
2571 else if (code != ERROR_MARK)
2572 error_at (loc,
2573 "request for member %qE in something not a structure or union",
2574 component);
2575
2576 return error_mark_node;
2577 }
2578
2579 /* Given an expression PTR for a pointer, return an expression
2580 for the value pointed to.
2581 ERRORSTRING is the name of the operator to appear in error messages.
2582
2583 LOC is the location to use for the generated tree. */
2584
2585 tree
build_indirect_ref(location_t loc,tree ptr,ref_operator errstring)2586 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2587 {
2588 tree pointer = default_conversion (ptr);
2589 tree type = TREE_TYPE (pointer);
2590 tree ref;
2591
2592 if (TREE_CODE (type) == POINTER_TYPE)
2593 {
2594 if (CONVERT_EXPR_P (pointer)
2595 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2596 {
2597 /* If a warning is issued, mark it to avoid duplicates from
2598 the backend. This only needs to be done at
2599 warn_strict_aliasing > 2. */
2600 if (warn_strict_aliasing > 2)
2601 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2602 type, TREE_OPERAND (pointer, 0)))
2603 TREE_NO_WARNING (pointer) = 1;
2604 }
2605
2606 if (TREE_CODE (pointer) == ADDR_EXPR
2607 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2608 == TREE_TYPE (type)))
2609 {
2610 ref = TREE_OPERAND (pointer, 0);
2611 protected_set_expr_location (ref, loc);
2612 return ref;
2613 }
2614 else
2615 {
2616 tree t = TREE_TYPE (type);
2617
2618 ref = build1 (INDIRECT_REF, t, pointer);
2619
2620 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2621 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2622
2623 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2624 so that we get the proper error message if the result is used
2625 to assign to. Also, &* is supposed to be a no-op.
2626 And ANSI C seems to specify that the type of the result
2627 should be the const type. */
2628 /* A de-reference of a pointer to const is not a const. It is valid
2629 to change it via some other pointer. */
2630 TREE_READONLY (ref) = TYPE_READONLY (t);
2631 TREE_SIDE_EFFECTS (ref)
2632 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2633 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2634 protected_set_expr_location (ref, loc);
2635 return ref;
2636 }
2637 }
2638 else if (TREE_CODE (pointer) != ERROR_MARK)
2639 invalid_indirection_error (loc, type, errstring);
2640
2641 return error_mark_node;
2642 }
2643
2644 /* This handles expressions of the form "a[i]", which denotes
2645 an array reference.
2646
2647 This is logically equivalent in C to *(a+i), but we may do it differently.
2648 If A is a variable or a member, we generate a primitive ARRAY_REF.
2649 This avoids forcing the array out of registers, and can work on
2650 arrays that are not lvalues (for example, members of structures returned
2651 by functions).
2652
2653 For vector types, allow vector[i] but not i[vector], and create
2654 *(((type*)&vectortype) + i) for the expression.
2655
2656 LOC is the location to use for the returned expression. */
2657
2658 tree
build_array_ref(location_t loc,tree array,tree index)2659 build_array_ref (location_t loc, tree array, tree index)
2660 {
2661 tree ret;
2662 bool swapped = false;
2663 if (TREE_TYPE (array) == error_mark_node
2664 || TREE_TYPE (index) == error_mark_node)
2665 return error_mark_node;
2666
2667 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2668 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2669 /* Allow vector[index] but not index[vector]. */
2670 && !gnu_vector_type_p (TREE_TYPE (array)))
2671 {
2672 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2673 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2674 {
2675 error_at (loc,
2676 "subscripted value is neither array nor pointer nor vector");
2677
2678 return error_mark_node;
2679 }
2680 std::swap (array, index);
2681 swapped = true;
2682 }
2683
2684 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2685 {
2686 error_at (loc, "array subscript is not an integer");
2687 return error_mark_node;
2688 }
2689
2690 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2691 {
2692 error_at (loc, "subscripted value is pointer to function");
2693 return error_mark_node;
2694 }
2695
2696 /* ??? Existing practice has been to warn only when the char
2697 index is syntactically the index, not for char[array]. */
2698 if (!swapped)
2699 warn_array_subscript_with_type_char (loc, index);
2700
2701 /* Apply default promotions *after* noticing character types. */
2702 index = default_conversion (index);
2703 if (index == error_mark_node)
2704 return error_mark_node;
2705
2706 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2707
2708 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2709 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2710
2711 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2712 {
2713 tree rval, type;
2714
2715 /* An array that is indexed by a non-constant
2716 cannot be stored in a register; we must be able to do
2717 address arithmetic on its address.
2718 Likewise an array of elements of variable size. */
2719 if (TREE_CODE (index) != INTEGER_CST
2720 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2721 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2722 {
2723 if (!c_mark_addressable (array, true))
2724 return error_mark_node;
2725 }
2726 /* An array that is indexed by a constant value which is not within
2727 the array bounds cannot be stored in a register either; because we
2728 would get a crash in store_bit_field/extract_bit_field when trying
2729 to access a non-existent part of the register. */
2730 if (TREE_CODE (index) == INTEGER_CST
2731 && TYPE_DOMAIN (TREE_TYPE (array))
2732 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2733 {
2734 if (!c_mark_addressable (array))
2735 return error_mark_node;
2736 }
2737
2738 if ((pedantic || warn_c90_c99_compat)
2739 && ! was_vector)
2740 {
2741 tree foo = array;
2742 while (TREE_CODE (foo) == COMPONENT_REF)
2743 foo = TREE_OPERAND (foo, 0);
2744 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2745 pedwarn (loc, OPT_Wpedantic,
2746 "ISO C forbids subscripting %<register%> array");
2747 else if (!lvalue_p (foo))
2748 pedwarn_c90 (loc, OPT_Wpedantic,
2749 "ISO C90 forbids subscripting non-lvalue "
2750 "array");
2751 }
2752
2753 type = TREE_TYPE (TREE_TYPE (array));
2754 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2755 /* Array ref is const/volatile if the array elements are
2756 or if the array is. */
2757 TREE_READONLY (rval)
2758 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2759 | TREE_READONLY (array));
2760 TREE_SIDE_EFFECTS (rval)
2761 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2762 | TREE_SIDE_EFFECTS (array));
2763 TREE_THIS_VOLATILE (rval)
2764 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2765 /* This was added by rms on 16 Nov 91.
2766 It fixes vol struct foo *a; a->elts[1]
2767 in an inline function.
2768 Hope it doesn't break something else. */
2769 | TREE_THIS_VOLATILE (array));
2770 ret = require_complete_type (loc, rval);
2771 protected_set_expr_location (ret, loc);
2772 if (non_lvalue)
2773 ret = non_lvalue_loc (loc, ret);
2774 return ret;
2775 }
2776 else
2777 {
2778 tree ar = default_conversion (array);
2779
2780 if (ar == error_mark_node)
2781 return ar;
2782
2783 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2784 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2785
2786 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2787 index, false),
2788 RO_ARRAY_INDEXING);
2789 if (non_lvalue)
2790 ret = non_lvalue_loc (loc, ret);
2791 return ret;
2792 }
2793 }
2794
2795 /* Build an external reference to identifier ID. FUN indicates
2796 whether this will be used for a function call. LOC is the source
2797 location of the identifier. This sets *TYPE to the type of the
2798 identifier, which is not the same as the type of the returned value
2799 for CONST_DECLs defined as enum constants. If the type of the
2800 identifier is not available, *TYPE is set to NULL. */
2801 tree
build_external_ref(location_t loc,tree id,bool fun,tree * type)2802 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2803 {
2804 tree ref;
2805 tree decl = lookup_name (id);
2806
2807 /* In Objective-C, an instance variable (ivar) may be preferred to
2808 whatever lookup_name() found. */
2809 decl = objc_lookup_ivar (decl, id);
2810
2811 *type = NULL;
2812 if (decl && decl != error_mark_node)
2813 {
2814 ref = decl;
2815 *type = TREE_TYPE (ref);
2816 }
2817 else if (fun)
2818 /* Implicit function declaration. */
2819 ref = implicitly_declare (loc, id);
2820 else if (decl == error_mark_node)
2821 /* Don't complain about something that's already been
2822 complained about. */
2823 return error_mark_node;
2824 else
2825 {
2826 undeclared_variable (loc, id);
2827 return error_mark_node;
2828 }
2829
2830 if (TREE_TYPE (ref) == error_mark_node)
2831 return error_mark_node;
2832
2833 if (TREE_DEPRECATED (ref))
2834 warn_deprecated_use (ref, NULL_TREE);
2835
2836 /* Recursive call does not count as usage. */
2837 if (ref != current_function_decl)
2838 {
2839 TREE_USED (ref) = 1;
2840 }
2841
2842 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2843 {
2844 if (!in_sizeof && !in_typeof)
2845 C_DECL_USED (ref) = 1;
2846 else if (DECL_INITIAL (ref) == NULL_TREE
2847 && DECL_EXTERNAL (ref)
2848 && !TREE_PUBLIC (ref))
2849 record_maybe_used_decl (ref);
2850 }
2851
2852 if (TREE_CODE (ref) == CONST_DECL)
2853 {
2854 used_types_insert (TREE_TYPE (ref));
2855
2856 if (warn_cxx_compat
2857 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2858 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2859 {
2860 warning_at (loc, OPT_Wc___compat,
2861 ("enum constant defined in struct or union "
2862 "is not visible in C++"));
2863 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2864 }
2865
2866 ref = DECL_INITIAL (ref);
2867 TREE_CONSTANT (ref) = 1;
2868 }
2869 else if (current_function_decl != NULL_TREE
2870 && !DECL_FILE_SCOPE_P (current_function_decl)
2871 && (VAR_OR_FUNCTION_DECL_P (ref)
2872 || TREE_CODE (ref) == PARM_DECL))
2873 {
2874 tree context = decl_function_context (ref);
2875
2876 if (context != NULL_TREE && context != current_function_decl)
2877 DECL_NONLOCAL (ref) = 1;
2878 }
2879 /* C99 6.7.4p3: An inline definition of a function with external
2880 linkage ... shall not contain a reference to an identifier with
2881 internal linkage. */
2882 else if (current_function_decl != NULL_TREE
2883 && DECL_DECLARED_INLINE_P (current_function_decl)
2884 && DECL_EXTERNAL (current_function_decl)
2885 && VAR_OR_FUNCTION_DECL_P (ref)
2886 && (!VAR_P (ref) || TREE_STATIC (ref))
2887 && ! TREE_PUBLIC (ref)
2888 && DECL_CONTEXT (ref) != current_function_decl)
2889 record_inline_static (loc, current_function_decl, ref,
2890 csi_internal);
2891
2892 return ref;
2893 }
2894
2895 /* Record details of decls possibly used inside sizeof or typeof. */
2896 struct maybe_used_decl
2897 {
2898 /* The decl. */
2899 tree decl;
2900 /* The level seen at (in_sizeof + in_typeof). */
2901 int level;
2902 /* The next one at this level or above, or NULL. */
2903 struct maybe_used_decl *next;
2904 };
2905
2906 static struct maybe_used_decl *maybe_used_decls;
2907
2908 /* Record that DECL, an undefined static function reference seen
2909 inside sizeof or typeof, might be used if the operand of sizeof is
2910 a VLA type or the operand of typeof is a variably modified
2911 type. */
2912
2913 static void
record_maybe_used_decl(tree decl)2914 record_maybe_used_decl (tree decl)
2915 {
2916 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2917 t->decl = decl;
2918 t->level = in_sizeof + in_typeof;
2919 t->next = maybe_used_decls;
2920 maybe_used_decls = t;
2921 }
2922
2923 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2924 USED is false, just discard them. If it is true, mark them used
2925 (if no longer inside sizeof or typeof) or move them to the next
2926 level up (if still inside sizeof or typeof). */
2927
2928 void
pop_maybe_used(bool used)2929 pop_maybe_used (bool used)
2930 {
2931 struct maybe_used_decl *p = maybe_used_decls;
2932 int cur_level = in_sizeof + in_typeof;
2933 while (p && p->level > cur_level)
2934 {
2935 if (used)
2936 {
2937 if (cur_level == 0)
2938 C_DECL_USED (p->decl) = 1;
2939 else
2940 p->level = cur_level;
2941 }
2942 p = p->next;
2943 }
2944 if (!used || cur_level == 0)
2945 maybe_used_decls = p;
2946 }
2947
2948 /* Return the result of sizeof applied to EXPR. */
2949
2950 struct c_expr
c_expr_sizeof_expr(location_t loc,struct c_expr expr)2951 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2952 {
2953 struct c_expr ret;
2954 if (expr.value == error_mark_node)
2955 {
2956 ret.value = error_mark_node;
2957 ret.original_code = ERROR_MARK;
2958 ret.original_type = NULL;
2959 pop_maybe_used (false);
2960 }
2961 else
2962 {
2963 bool expr_const_operands = true;
2964
2965 if (TREE_CODE (expr.value) == PARM_DECL
2966 && C_ARRAY_PARAMETER (expr.value))
2967 {
2968 auto_diagnostic_group d;
2969 if (warning_at (loc, OPT_Wsizeof_array_argument,
2970 "%<sizeof%> on array function parameter %qE will "
2971 "return size of %qT", expr.value,
2972 TREE_TYPE (expr.value)))
2973 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2974 }
2975 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2976 &expr_const_operands);
2977 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2978 c_last_sizeof_arg = expr.value;
2979 c_last_sizeof_loc = loc;
2980 ret.original_code = SIZEOF_EXPR;
2981 ret.original_type = NULL;
2982 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2983 {
2984 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2985 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2986 folded_expr, ret.value);
2987 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2988 SET_EXPR_LOCATION (ret.value, loc);
2989 }
2990 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2991 }
2992 return ret;
2993 }
2994
2995 /* Return the result of sizeof applied to T, a structure for the type
2996 name passed to sizeof (rather than the type itself). LOC is the
2997 location of the original expression. */
2998
2999 struct c_expr
c_expr_sizeof_type(location_t loc,struct c_type_name * t)3000 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3001 {
3002 tree type;
3003 struct c_expr ret;
3004 tree type_expr = NULL_TREE;
3005 bool type_expr_const = true;
3006 type = groktypename (t, &type_expr, &type_expr_const);
3007 ret.value = c_sizeof (loc, type);
3008 c_last_sizeof_arg = type;
3009 c_last_sizeof_loc = loc;
3010 ret.original_code = SIZEOF_EXPR;
3011 ret.original_type = NULL;
3012 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3013 && c_vla_type_p (type))
3014 {
3015 /* If the type is a [*] array, it is a VLA but is represented as
3016 having a size of zero. In such a case we must ensure that
3017 the result of sizeof does not get folded to a constant by
3018 c_fully_fold, because if the size is evaluated the result is
3019 not constant and so constraints on zero or negative size
3020 arrays must not be applied when this sizeof call is inside
3021 another array declarator. */
3022 if (!type_expr)
3023 type_expr = integer_zero_node;
3024 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3025 type_expr, ret.value);
3026 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3027 }
3028 pop_maybe_used (type != error_mark_node
3029 ? C_TYPE_VARIABLE_SIZE (type) : false);
3030 return ret;
3031 }
3032
3033 /* Build a function call to function FUNCTION with parameters PARAMS.
3034 The function call is at LOC.
3035 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3036 TREE_VALUE of each node is a parameter-expression.
3037 FUNCTION's data type may be a function type or a pointer-to-function. */
3038
3039 tree
build_function_call(location_t loc,tree function,tree params)3040 build_function_call (location_t loc, tree function, tree params)
3041 {
3042 vec<tree, va_gc> *v;
3043 tree ret;
3044
3045 vec_alloc (v, list_length (params));
3046 for (; params; params = TREE_CHAIN (params))
3047 v->quick_push (TREE_VALUE (params));
3048 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3049 vec_free (v);
3050 return ret;
3051 }
3052
3053 /* Give a note about the location of the declaration of DECL. */
3054
3055 static void
inform_declaration(tree decl)3056 inform_declaration (tree decl)
3057 {
3058 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
3059 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3060 }
3061
3062 /* Build a function call to function FUNCTION with parameters PARAMS.
3063 If FUNCTION is the result of resolving an overloaded target built-in,
3064 ORIG_FUNDECL is the original function decl, otherwise it is null.
3065 ORIGTYPES, if not NULL, is a vector of types; each element is
3066 either NULL or the original type of the corresponding element in
3067 PARAMS. The original type may differ from TREE_TYPE of the
3068 parameter for enums. FUNCTION's data type may be a function type
3069 or pointer-to-function. This function changes the elements of
3070 PARAMS. */
3071
3072 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)3073 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3074 tree function, vec<tree, va_gc> *params,
3075 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3076 {
3077 tree fntype, fundecl = NULL_TREE;
3078 tree name = NULL_TREE, result;
3079 tree tem;
3080 int nargs;
3081 tree *argarray;
3082
3083
3084 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3085 STRIP_TYPE_NOPS (function);
3086
3087 /* Convert anything with function type to a pointer-to-function. */
3088 if (TREE_CODE (function) == FUNCTION_DECL)
3089 {
3090 name = DECL_NAME (function);
3091
3092 if (flag_tm)
3093 tm_malloc_replacement (function);
3094 fundecl = function;
3095 if (!orig_fundecl)
3096 orig_fundecl = fundecl;
3097 /* Atomic functions have type checking/casting already done. They are
3098 often rewritten and don't match the original parameter list. */
3099 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3100 origtypes = NULL;
3101 }
3102 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3103 function = function_to_pointer_conversion (loc, function);
3104
3105 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3106 expressions, like those used for ObjC messenger dispatches. */
3107 if (params && !params->is_empty ())
3108 function = objc_rewrite_function_call (function, (*params)[0]);
3109
3110 function = c_fully_fold (function, false, NULL);
3111
3112 fntype = TREE_TYPE (function);
3113
3114 if (TREE_CODE (fntype) == ERROR_MARK)
3115 return error_mark_node;
3116
3117 if (!(TREE_CODE (fntype) == POINTER_TYPE
3118 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3119 {
3120 if (!flag_diagnostics_show_caret)
3121 error_at (loc,
3122 "called object %qE is not a function or function pointer",
3123 function);
3124 else if (DECL_P (function))
3125 {
3126 error_at (loc,
3127 "called object %qD is not a function or function pointer",
3128 function);
3129 inform_declaration (function);
3130 }
3131 else
3132 error_at (loc,
3133 "called object is not a function or function pointer");
3134 return error_mark_node;
3135 }
3136
3137 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3138 current_function_returns_abnormally = 1;
3139
3140 /* fntype now gets the type of function pointed to. */
3141 fntype = TREE_TYPE (fntype);
3142
3143 /* Convert the parameters to the types declared in the
3144 function prototype, or apply default promotions. */
3145
3146 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3147 origtypes, function, fundecl);
3148 if (nargs < 0)
3149 return error_mark_node;
3150
3151 /* Check that the function is called through a compatible prototype.
3152 If it is not, warn. */
3153 if (CONVERT_EXPR_P (function)
3154 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3155 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3156 && !comptypes (fntype, TREE_TYPE (tem)))
3157 {
3158 tree return_type = TREE_TYPE (fntype);
3159
3160 /* This situation leads to run-time undefined behavior. We can't,
3161 therefore, simply error unless we can prove that all possible
3162 executions of the program must execute the code. */
3163 warning_at (loc, 0, "function called through a non-compatible type");
3164
3165 if (VOID_TYPE_P (return_type)
3166 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3167 pedwarn (loc, 0,
3168 "function with qualified void return type called");
3169 }
3170
3171 argarray = vec_safe_address (params);
3172
3173 /* Check that arguments to builtin functions match the expectations. */
3174 if (fundecl
3175 && fndecl_built_in_p (fundecl)
3176 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3177 orig_fundecl, nargs, argarray))
3178 return error_mark_node;
3179
3180 /* Check that the arguments to the function are valid. */
3181 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3182 nargs, argarray, &arg_loc);
3183
3184 if (name != NULL_TREE
3185 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3186 {
3187 if (require_constant_value)
3188 result
3189 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3190 function, nargs, argarray);
3191 else
3192 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3193 function, nargs, argarray);
3194 if (TREE_CODE (result) == NOP_EXPR
3195 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3196 STRIP_TYPE_NOPS (result);
3197 }
3198 else
3199 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3200 function, nargs, argarray);
3201 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3202 later. */
3203 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3204 TREE_NO_WARNING (result) = 1;
3205
3206 /* In this improbable scenario, a nested function returns a VM type.
3207 Create a TARGET_EXPR so that the call always has a LHS, much as
3208 what the C++ FE does for functions returning non-PODs. */
3209 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3210 {
3211 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3212 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3213 NULL_TREE, NULL_TREE);
3214 }
3215
3216 if (VOID_TYPE_P (TREE_TYPE (result)))
3217 {
3218 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3219 pedwarn (loc, 0,
3220 "function with qualified void return type called");
3221 return result;
3222 }
3223 return require_complete_type (loc, result);
3224 }
3225
3226 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3227
3228 tree
c_build_function_call_vec(location_t loc,vec<location_t> arg_loc,tree function,vec<tree,va_gc> * params,vec<tree,va_gc> * origtypes)3229 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3230 tree function, vec<tree, va_gc> *params,
3231 vec<tree, va_gc> *origtypes)
3232 {
3233 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3234 STRIP_TYPE_NOPS (function);
3235
3236 /* Convert anything with function type to a pointer-to-function. */
3237 if (TREE_CODE (function) == FUNCTION_DECL)
3238 {
3239 /* Implement type-directed function overloading for builtins.
3240 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3241 handle all the type checking. The result is a complete expression
3242 that implements this function call. */
3243 tree tem = resolve_overloaded_builtin (loc, function, params);
3244 if (tem)
3245 return tem;
3246 }
3247 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3248 }
3249
3250 /* Helper for convert_arguments called to convert the VALue of argument
3251 number ARGNUM from ORIGTYPE to the corresponding parameter number
3252 PARMNUM and TYPE.
3253 PLOC is the location where the conversion is being performed.
3254 FUNCTION and FUNDECL are the same as in convert_arguments.
3255 VALTYPE is the original type of VAL before the conversion and,
3256 for EXCESS_PRECISION_EXPR, the operand of the expression.
3257 NPC is true if VAL represents the null pointer constant (VAL itself
3258 will have been folded to an integer constant).
3259 RNAME is the same as FUNCTION except in Objective C when it's
3260 the function selector.
3261 EXCESS_PRECISION is true when VAL was originally represented
3262 as EXCESS_PRECISION_EXPR.
3263 WARNOPT is the same as in convert_for_assignment. */
3264
3265 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)3266 convert_argument (location_t ploc, tree function, tree fundecl,
3267 tree type, tree origtype, tree val, tree valtype,
3268 bool npc, tree rname, int parmnum, int argnum,
3269 bool excess_precision, int warnopt)
3270 {
3271 /* Formal parm type is specified by a function prototype. */
3272
3273 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3274 {
3275 error_at (ploc, "type of formal parameter %d is incomplete",
3276 parmnum + 1);
3277 return val;
3278 }
3279
3280 /* Optionally warn about conversions that differ from the default
3281 conversions. */
3282 if (warn_traditional_conversion || warn_traditional)
3283 {
3284 unsigned int formal_prec = TYPE_PRECISION (type);
3285
3286 if (INTEGRAL_TYPE_P (type)
3287 && TREE_CODE (valtype) == REAL_TYPE)
3288 warning_at (ploc, OPT_Wtraditional_conversion,
3289 "passing argument %d of %qE as integer rather "
3290 "than floating due to prototype",
3291 argnum, rname);
3292 if (INTEGRAL_TYPE_P (type)
3293 && TREE_CODE (valtype) == COMPLEX_TYPE)
3294 warning_at (ploc, OPT_Wtraditional_conversion,
3295 "passing argument %d of %qE as integer rather "
3296 "than complex due to prototype",
3297 argnum, rname);
3298 else if (TREE_CODE (type) == COMPLEX_TYPE
3299 && TREE_CODE (valtype) == REAL_TYPE)
3300 warning_at (ploc, OPT_Wtraditional_conversion,
3301 "passing argument %d of %qE as complex rather "
3302 "than floating due to prototype",
3303 argnum, rname);
3304 else if (TREE_CODE (type) == REAL_TYPE
3305 && INTEGRAL_TYPE_P (valtype))
3306 warning_at (ploc, OPT_Wtraditional_conversion,
3307 "passing argument %d of %qE as floating rather "
3308 "than integer due to prototype",
3309 argnum, rname);
3310 else if (TREE_CODE (type) == COMPLEX_TYPE
3311 && INTEGRAL_TYPE_P (valtype))
3312 warning_at (ploc, OPT_Wtraditional_conversion,
3313 "passing argument %d of %qE as complex rather "
3314 "than integer due to prototype",
3315 argnum, rname);
3316 else if (TREE_CODE (type) == REAL_TYPE
3317 && TREE_CODE (valtype) == COMPLEX_TYPE)
3318 warning_at (ploc, OPT_Wtraditional_conversion,
3319 "passing argument %d of %qE as floating rather "
3320 "than complex due to prototype",
3321 argnum, rname);
3322 /* ??? At some point, messages should be written about
3323 conversions between complex types, but that's too messy
3324 to do now. */
3325 else if (TREE_CODE (type) == REAL_TYPE
3326 && TREE_CODE (valtype) == REAL_TYPE)
3327 {
3328 /* Warn if any argument is passed as `float',
3329 since without a prototype it would be `double'. */
3330 if (formal_prec == TYPE_PRECISION (float_type_node)
3331 && type != dfloat32_type_node)
3332 warning_at (ploc, 0,
3333 "passing argument %d of %qE as %<float%> "
3334 "rather than %<double%> due to prototype",
3335 argnum, rname);
3336
3337 /* Warn if mismatch between argument and prototype
3338 for decimal float types. Warn of conversions with
3339 binary float types and of precision narrowing due to
3340 prototype. */
3341 else if (type != valtype
3342 && (type == dfloat32_type_node
3343 || type == dfloat64_type_node
3344 || type == dfloat128_type_node
3345 || valtype == dfloat32_type_node
3346 || valtype == dfloat64_type_node
3347 || valtype == dfloat128_type_node)
3348 && (formal_prec
3349 <= TYPE_PRECISION (valtype)
3350 || (type == dfloat128_type_node
3351 && (valtype
3352 != dfloat64_type_node
3353 && (valtype
3354 != dfloat32_type_node)))
3355 || (type == dfloat64_type_node
3356 && (valtype
3357 != dfloat32_type_node))))
3358 warning_at (ploc, 0,
3359 "passing argument %d of %qE as %qT "
3360 "rather than %qT due to prototype",
3361 argnum, rname, type, valtype);
3362
3363 }
3364 /* Detect integer changing in width or signedness.
3365 These warnings are only activated with
3366 -Wtraditional-conversion, not with -Wtraditional. */
3367 else if (warn_traditional_conversion
3368 && INTEGRAL_TYPE_P (type)
3369 && INTEGRAL_TYPE_P (valtype))
3370 {
3371 tree would_have_been = default_conversion (val);
3372 tree type1 = TREE_TYPE (would_have_been);
3373
3374 if (val == error_mark_node)
3375 /* VAL could have been of incomplete type. */;
3376 else if (TREE_CODE (type) == ENUMERAL_TYPE
3377 && (TYPE_MAIN_VARIANT (type)
3378 == TYPE_MAIN_VARIANT (valtype)))
3379 /* No warning if function asks for enum
3380 and the actual arg is that enum type. */
3381 ;
3382 else if (formal_prec != TYPE_PRECISION (type1))
3383 warning_at (ploc, OPT_Wtraditional_conversion,
3384 "passing argument %d of %qE "
3385 "with different width due to prototype",
3386 argnum, rname);
3387 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3388 ;
3389 /* Don't complain if the formal parameter type
3390 is an enum, because we can't tell now whether
3391 the value was an enum--even the same enum. */
3392 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3393 ;
3394 else if (TREE_CODE (val) == INTEGER_CST
3395 && int_fits_type_p (val, type))
3396 /* Change in signedness doesn't matter
3397 if a constant value is unaffected. */
3398 ;
3399 /* If the value is extended from a narrower
3400 unsigned type, it doesn't matter whether we
3401 pass it as signed or unsigned; the value
3402 certainly is the same either way. */
3403 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3404 && TYPE_UNSIGNED (valtype))
3405 ;
3406 else if (TYPE_UNSIGNED (type))
3407 warning_at (ploc, OPT_Wtraditional_conversion,
3408 "passing argument %d of %qE "
3409 "as unsigned due to prototype",
3410 argnum, rname);
3411 else
3412 warning_at (ploc, OPT_Wtraditional_conversion,
3413 "passing argument %d of %qE "
3414 "as signed due to prototype",
3415 argnum, rname);
3416 }
3417 }
3418
3419 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3420 sake of better warnings from convert_and_check. */
3421 if (excess_precision)
3422 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3423
3424 tree parmval = convert_for_assignment (ploc, ploc, type,
3425 val, origtype, ic_argpass,
3426 npc, fundecl, function,
3427 parmnum + 1, warnopt);
3428
3429 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3430 && INTEGRAL_TYPE_P (type)
3431 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3432 parmval = default_conversion (parmval);
3433
3434 return parmval;
3435 }
3436
3437 /* Convert the argument expressions in the vector VALUES
3438 to the types in the list TYPELIST.
3439
3440 If TYPELIST is exhausted, or when an element has NULL as its type,
3441 perform the default conversions.
3442
3443 ORIGTYPES is the original types of the expressions in VALUES. This
3444 holds the type of enum values which have been converted to integral
3445 types. It may be NULL.
3446
3447 FUNCTION is a tree for the called function. It is used only for
3448 error messages, where it is formatted with %qE.
3449
3450 This is also where warnings about wrong number of args are generated.
3451
3452 ARG_LOC are locations of function arguments (if any).
3453
3454 Returns the actual number of arguments processed (which may be less
3455 than the length of VALUES in some error situations), or -1 on
3456 failure. */
3457
3458 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)3459 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3460 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3461 tree function, tree fundecl)
3462 {
3463 unsigned int parmnum;
3464 bool error_args = false;
3465 const bool type_generic = fundecl
3466 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3467 bool type_generic_remove_excess_precision = false;
3468 bool type_generic_overflow_p = false;
3469 tree selector;
3470
3471 /* Change pointer to function to the function itself for
3472 diagnostics. */
3473 if (TREE_CODE (function) == ADDR_EXPR
3474 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3475 function = TREE_OPERAND (function, 0);
3476
3477 /* Handle an ObjC selector specially for diagnostics. */
3478 selector = objc_message_selector ();
3479
3480 /* For a call to a built-in function declared without a prototype,
3481 set to the built-in function's argument list. */
3482 tree builtin_typelist = NULL_TREE;
3483
3484 /* For type-generic built-in functions, determine whether excess
3485 precision should be removed (classification) or not
3486 (comparison). */
3487 if (fundecl
3488 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3489 {
3490 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3491 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3492 {
3493 /* For a call to a built-in function declared without a prototype
3494 use the types of the parameters of the internal built-in to
3495 match those of the arguments to. */
3496 if (tree bdecl = builtin_decl_explicit (code))
3497 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3498 }
3499
3500 /* For type-generic built-in functions, determine whether excess
3501 precision should be removed (classification) or not
3502 (comparison). */
3503 if (type_generic)
3504 switch (code)
3505 {
3506 case BUILT_IN_ISFINITE:
3507 case BUILT_IN_ISINF:
3508 case BUILT_IN_ISINF_SIGN:
3509 case BUILT_IN_ISNAN:
3510 case BUILT_IN_ISNORMAL:
3511 case BUILT_IN_FPCLASSIFY:
3512 type_generic_remove_excess_precision = true;
3513 break;
3514
3515 case BUILT_IN_ADD_OVERFLOW_P:
3516 case BUILT_IN_SUB_OVERFLOW_P:
3517 case BUILT_IN_MUL_OVERFLOW_P:
3518 /* The last argument of these type-generic builtins
3519 should not be promoted. */
3520 type_generic_overflow_p = true;
3521 break;
3522
3523 default:
3524 break;
3525 }
3526 }
3527
3528 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3529 individual converted arguments. */
3530
3531 tree typetail, builtin_typetail, val;
3532 for (typetail = typelist,
3533 builtin_typetail = builtin_typelist,
3534 parmnum = 0;
3535 values && values->iterate (parmnum, &val);
3536 ++parmnum)
3537 {
3538 /* The type of the function parameter (if it was declared with one). */
3539 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3540 /* The type of the built-in function parameter (if the function
3541 is a built-in). Used to detect type incompatibilities in
3542 calls to built-ins declared without a prototype. */
3543 tree builtin_type = (builtin_typetail
3544 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3545 /* The original type of the argument being passed to the function. */
3546 tree valtype = TREE_TYPE (val);
3547 /* The called function (or function selector in Objective C). */
3548 tree rname = function;
3549 int argnum = parmnum + 1;
3550 const char *invalid_func_diag;
3551 /* Set for EXCESS_PRECISION_EXPR arguments. */
3552 bool excess_precision = false;
3553 /* The value of the argument after conversion to the type
3554 of the function parameter it is passed to. */
3555 tree parmval;
3556 /* Some __atomic_* builtins have additional hidden argument at
3557 position 0. */
3558 location_t ploc
3559 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3560 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3561 : input_location;
3562
3563 if (type == void_type_node)
3564 {
3565 if (selector)
3566 error_at (loc, "too many arguments to method %qE", selector);
3567 else
3568 error_at (loc, "too many arguments to function %qE", function);
3569 inform_declaration (fundecl);
3570 return error_args ? -1 : (int) parmnum;
3571 }
3572
3573 if (builtin_type == void_type_node)
3574 {
3575 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3576 "too many arguments to built-in function %qE "
3577 "expecting %d", function, parmnum))
3578 inform_declaration (fundecl);
3579 builtin_typetail = NULL_TREE;
3580 }
3581
3582 if (selector && argnum > 2)
3583 {
3584 rname = selector;
3585 argnum -= 2;
3586 }
3587
3588 /* Determine if VAL is a null pointer constant before folding it. */
3589 bool npc = null_pointer_constant_p (val);
3590
3591 /* If there is excess precision and a prototype, convert once to
3592 the required type rather than converting via the semantic
3593 type. Likewise without a prototype a float value represented
3594 as long double should be converted once to double. But for
3595 type-generic classification functions excess precision must
3596 be removed here. */
3597 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3598 && (type || !type_generic || !type_generic_remove_excess_precision))
3599 {
3600 val = TREE_OPERAND (val, 0);
3601 excess_precision = true;
3602 }
3603 val = c_fully_fold (val, false, NULL);
3604 STRIP_TYPE_NOPS (val);
3605
3606 val = require_complete_type (ploc, val);
3607
3608 /* Some floating-point arguments must be promoted to double when
3609 no type is specified by a prototype. This applies to
3610 arguments of type float, and to architecture-specific types
3611 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3612 bool promote_float_arg = false;
3613 if (type == NULL_TREE
3614 && TREE_CODE (valtype) == REAL_TYPE
3615 && (TYPE_PRECISION (valtype)
3616 <= TYPE_PRECISION (double_type_node))
3617 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3618 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3619 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3620 {
3621 /* Promote this argument, unless it has a _FloatN or
3622 _FloatNx type. */
3623 promote_float_arg = true;
3624 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3625 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3626 {
3627 promote_float_arg = false;
3628 break;
3629 }
3630 }
3631
3632 if (type != NULL_TREE)
3633 {
3634 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3635 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3636 val, valtype, npc, rname, parmnum, argnum,
3637 excess_precision, 0);
3638 }
3639 else if (promote_float_arg)
3640 {
3641 if (type_generic)
3642 parmval = val;
3643 else
3644 {
3645 /* Convert `float' to `double'. */
3646 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3647 warning_at (ploc, OPT_Wdouble_promotion,
3648 "implicit conversion from %qT to %qT when passing "
3649 "argument to function",
3650 valtype, double_type_node);
3651 parmval = convert (double_type_node, val);
3652 }
3653 }
3654 else if ((excess_precision && !type_generic)
3655 || (type_generic_overflow_p && parmnum == 2))
3656 /* A "double" argument with excess precision being passed
3657 without a prototype or in variable arguments.
3658 The last argument of __builtin_*_overflow_p should not be
3659 promoted. */
3660 parmval = convert (valtype, val);
3661 else if ((invalid_func_diag =
3662 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3663 {
3664 error (invalid_func_diag);
3665 return -1;
3666 }
3667 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3668 {
3669 return -1;
3670 }
3671 else
3672 /* Convert `short' and `char' to full-size `int'. */
3673 parmval = default_conversion (val);
3674
3675 (*values)[parmnum] = parmval;
3676 if (parmval == error_mark_node)
3677 error_args = true;
3678
3679 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3680 {
3681 /* For a call to a built-in function declared without a prototype,
3682 perform the conversions from the argument to the expected type
3683 but issue warnings rather than errors for any mismatches.
3684 Ignore the converted argument and use the PARMVAL obtained
3685 above by applying default conversions instead. */
3686 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3687 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3688 val, valtype, npc, rname, parmnum, argnum,
3689 excess_precision,
3690 OPT_Wbuiltin_declaration_mismatch);
3691 }
3692
3693 if (typetail)
3694 typetail = TREE_CHAIN (typetail);
3695
3696 if (builtin_typetail)
3697 builtin_typetail = TREE_CHAIN (builtin_typetail);
3698 }
3699
3700 gcc_assert (parmnum == vec_safe_length (values));
3701
3702 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3703 {
3704 error_at (loc, "too few arguments to function %qE", function);
3705 inform_declaration (fundecl);
3706 return -1;
3707 }
3708
3709 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3710 {
3711 unsigned nargs = parmnum;
3712 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3713 ++nargs;
3714
3715 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3716 "too few arguments to built-in function %qE "
3717 "expecting %u", function, nargs - 1))
3718 inform_declaration (fundecl);
3719 }
3720
3721 return error_args ? -1 : (int) parmnum;
3722 }
3723
3724 /* This is the entry point used by the parser to build unary operators
3725 in the input. CODE, a tree_code, specifies the unary operator, and
3726 ARG is the operand. For unary plus, the C parser currently uses
3727 CONVERT_EXPR for code.
3728
3729 LOC is the location to use for the tree generated.
3730 */
3731
3732 struct c_expr
parser_build_unary_op(location_t loc,enum tree_code code,struct c_expr arg)3733 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3734 {
3735 struct c_expr result;
3736
3737 result.original_code = code;
3738 result.original_type = NULL;
3739
3740 if (reject_gcc_builtin (arg.value))
3741 {
3742 result.value = error_mark_node;
3743 }
3744 else
3745 {
3746 result.value = build_unary_op (loc, code, arg.value, false);
3747
3748 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3749 overflow_warning (loc, result.value, arg.value);
3750 }
3751
3752 /* We are typically called when parsing a prefix token at LOC acting on
3753 ARG. Reflect this by updating the source range of the result to
3754 start at LOC and end at the end of ARG. */
3755 set_c_expr_source_range (&result,
3756 loc, arg.get_finish ());
3757
3758 return result;
3759 }
3760
3761 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3762
3763 static bool
char_type_p(tree type)3764 char_type_p (tree type)
3765 {
3766 return (type == char_type_node
3767 || type == unsigned_char_type_node
3768 || type == signed_char_type_node
3769 || type == char16_type_node
3770 || type == char32_type_node);
3771 }
3772
3773 /* This is the entry point used by the parser to build binary operators
3774 in the input. CODE, a tree_code, specifies the binary operator, and
3775 ARG1 and ARG2 are the operands. In addition to constructing the
3776 expression, we check for operands that were written with other binary
3777 operators in a way that is likely to confuse the user.
3778
3779 LOCATION is the location of the binary operator. */
3780
3781 struct c_expr
parser_build_binary_op(location_t location,enum tree_code code,struct c_expr arg1,struct c_expr arg2)3782 parser_build_binary_op (location_t location, enum tree_code code,
3783 struct c_expr arg1, struct c_expr arg2)
3784 {
3785 struct c_expr result;
3786
3787 enum tree_code code1 = arg1.original_code;
3788 enum tree_code code2 = arg2.original_code;
3789 tree type1 = (arg1.original_type
3790 ? arg1.original_type
3791 : TREE_TYPE (arg1.value));
3792 tree type2 = (arg2.original_type
3793 ? arg2.original_type
3794 : TREE_TYPE (arg2.value));
3795
3796 result.value = build_binary_op (location, code,
3797 arg1.value, arg2.value, true);
3798 result.original_code = code;
3799 result.original_type = NULL;
3800
3801 if (TREE_CODE (result.value) == ERROR_MARK)
3802 {
3803 set_c_expr_source_range (&result,
3804 arg1.get_start (),
3805 arg2.get_finish ());
3806 return result;
3807 }
3808
3809 if (location != UNKNOWN_LOCATION)
3810 protected_set_expr_location (result.value, location);
3811
3812 set_c_expr_source_range (&result,
3813 arg1.get_start (),
3814 arg2.get_finish ());
3815
3816 /* Check for cases such as x+y<<z which users are likely
3817 to misinterpret. */
3818 if (warn_parentheses)
3819 warn_about_parentheses (location, code, code1, arg1.value, code2,
3820 arg2.value);
3821
3822 if (warn_logical_op)
3823 warn_logical_operator (location, code, TREE_TYPE (result.value),
3824 code1, arg1.value, code2, arg2.value);
3825
3826 if (warn_tautological_compare)
3827 {
3828 tree lhs = arg1.value;
3829 tree rhs = arg2.value;
3830 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3831 {
3832 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3833 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3834 lhs = NULL_TREE;
3835 else
3836 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3837 }
3838 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3839 {
3840 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3841 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3842 rhs = NULL_TREE;
3843 else
3844 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3845 }
3846 if (lhs != NULL_TREE && rhs != NULL_TREE)
3847 warn_tautological_cmp (location, code, lhs, rhs);
3848 }
3849
3850 if (warn_logical_not_paren
3851 && TREE_CODE_CLASS (code) == tcc_comparison
3852 && code1 == TRUTH_NOT_EXPR
3853 && code2 != TRUTH_NOT_EXPR
3854 /* Avoid warning for !!x == y. */
3855 && (TREE_CODE (arg1.value) != NE_EXPR
3856 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3857 {
3858 /* Avoid warning for !b == y where b has _Bool type. */
3859 tree t = integer_zero_node;
3860 if (TREE_CODE (arg1.value) == EQ_EXPR
3861 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3862 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3863 {
3864 t = TREE_OPERAND (arg1.value, 0);
3865 do
3866 {
3867 if (TREE_TYPE (t) != integer_type_node)
3868 break;
3869 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3870 t = C_MAYBE_CONST_EXPR_EXPR (t);
3871 else if (CONVERT_EXPR_P (t))
3872 t = TREE_OPERAND (t, 0);
3873 else
3874 break;
3875 }
3876 while (1);
3877 }
3878 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3879 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3880 }
3881
3882 /* Warn about comparisons against string literals, with the exception
3883 of testing for equality or inequality of a string literal with NULL. */
3884 if (code == EQ_EXPR || code == NE_EXPR)
3885 {
3886 if ((code1 == STRING_CST
3887 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3888 || (code2 == STRING_CST
3889 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3890 warning_at (location, OPT_Waddress,
3891 "comparison with string literal results in unspecified behavior");
3892 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3893 if (POINTER_TYPE_P (type1)
3894 && null_pointer_constant_p (arg2.value)
3895 && char_type_p (type2))
3896 {
3897 auto_diagnostic_group d;
3898 if (warning_at (location, OPT_Wpointer_compare,
3899 "comparison between pointer and zero character "
3900 "constant"))
3901 inform (arg1.get_start (),
3902 "did you mean to dereference the pointer?");
3903 }
3904 else if (POINTER_TYPE_P (type2)
3905 && null_pointer_constant_p (arg1.value)
3906 && char_type_p (type1))
3907 {
3908 auto_diagnostic_group d;
3909 if (warning_at (location, OPT_Wpointer_compare,
3910 "comparison between pointer and zero character "
3911 "constant"))
3912 inform (arg2.get_start (),
3913 "did you mean to dereference the pointer?");
3914 }
3915 }
3916 else if (TREE_CODE_CLASS (code) == tcc_comparison
3917 && (code1 == STRING_CST || code2 == STRING_CST))
3918 warning_at (location, OPT_Waddress,
3919 "comparison with string literal results in unspecified behavior");
3920
3921 if (TREE_OVERFLOW_P (result.value)
3922 && !TREE_OVERFLOW_P (arg1.value)
3923 && !TREE_OVERFLOW_P (arg2.value))
3924 overflow_warning (location, result.value);
3925
3926 /* Warn about comparisons of different enum types. */
3927 if (warn_enum_compare
3928 && TREE_CODE_CLASS (code) == tcc_comparison
3929 && TREE_CODE (type1) == ENUMERAL_TYPE
3930 && TREE_CODE (type2) == ENUMERAL_TYPE
3931 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3932 warning_at (location, OPT_Wenum_compare,
3933 "comparison between %qT and %qT",
3934 type1, type2);
3935
3936 return result;
3937 }
3938
3939 /* Return a tree for the difference of pointers OP0 and OP1.
3940 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3941 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3942
3943 static tree
pointer_diff(location_t loc,tree op0,tree op1,tree * instrument_expr)3944 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3945 {
3946 tree restype = ptrdiff_type_node;
3947 tree result, inttype;
3948
3949 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3950 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3951 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3952 tree orig_op0 = op0;
3953 tree orig_op1 = op1;
3954
3955 /* If the operands point into different address spaces, we need to
3956 explicitly convert them to pointers into the common address space
3957 before we can subtract the numerical address values. */
3958 if (as0 != as1)
3959 {
3960 addr_space_t as_common;
3961 tree common_type;
3962
3963 /* Determine the common superset address space. This is guaranteed
3964 to exist because the caller verified that comp_target_types
3965 returned non-zero. */
3966 if (!addr_space_superset (as0, as1, &as_common))
3967 gcc_unreachable ();
3968
3969 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3970 op0 = convert (common_type, op0);
3971 op1 = convert (common_type, op1);
3972 }
3973
3974 /* Determine integer type result of the subtraction. This will usually
3975 be the same as the result type (ptrdiff_t), but may need to be a wider
3976 type if pointers for the address space are wider than ptrdiff_t. */
3977 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3978 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3979 else
3980 inttype = restype;
3981
3982 if (TREE_CODE (target_type) == VOID_TYPE)
3983 pedwarn (loc, OPT_Wpointer_arith,
3984 "pointer of type %<void *%> used in subtraction");
3985 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3986 pedwarn (loc, OPT_Wpointer_arith,
3987 "pointer to a function used in subtraction");
3988
3989 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3990 {
3991 gcc_assert (current_function_decl != NULL_TREE);
3992
3993 op0 = save_expr (op0);
3994 op1 = save_expr (op1);
3995
3996 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3997 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3998 }
3999
4000 /* First do the subtraction, then build the divide operator
4001 and only convert at the very end.
4002 Do not do default conversions in case restype is a short type. */
4003
4004 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4005 pointers. If some platform cannot provide that, or has a larger
4006 ptrdiff_type to support differences larger than half the address
4007 space, cast the pointers to some larger integer type and do the
4008 computations in that type. */
4009 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4010 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4011 convert (inttype, op1), false);
4012 else
4013 {
4014 /* Cast away qualifiers. */
4015 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4016 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4017 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4018 }
4019
4020 /* This generates an error if op1 is pointer to incomplete type. */
4021 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4022 error_at (loc, "arithmetic on pointer to an incomplete type");
4023 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4024 TREE_TYPE (TREE_TYPE (orig_op0))))
4025 verify_type_context (loc, TCTX_POINTER_ARITH,
4026 TREE_TYPE (TREE_TYPE (orig_op1)));
4027
4028 op1 = c_size_in_bytes (target_type);
4029
4030 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4031 error_at (loc, "arithmetic on pointer to an empty aggregate");
4032
4033 /* Divide by the size, in easiest possible way. */
4034 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4035 op0, convert (inttype, op1));
4036
4037 /* Convert to final result type if necessary. */
4038 return convert (restype, result);
4039 }
4040
4041 /* Expand atomic compound assignments into an appropriate sequence as
4042 specified by the C11 standard section 6.5.16.2.
4043
4044 _Atomic T1 E1
4045 T2 E2
4046 E1 op= E2
4047
4048 This sequence is used for all types for which these operations are
4049 supported.
4050
4051 In addition, built-in versions of the 'fe' prefixed routines may
4052 need to be invoked for floating point (real, complex or vector) when
4053 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4054
4055 T1 newval;
4056 T1 old;
4057 T1 *addr
4058 T2 val
4059 fenv_t fenv
4060
4061 addr = &E1;
4062 val = (E2);
4063 __atomic_load (addr, &old, SEQ_CST);
4064 feholdexcept (&fenv);
4065 loop:
4066 newval = old op val;
4067 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4068 SEQ_CST))
4069 goto done;
4070 feclearexcept (FE_ALL_EXCEPT);
4071 goto loop:
4072 done:
4073 feupdateenv (&fenv);
4074
4075 The compiler will issue the __atomic_fetch_* built-in when possible,
4076 otherwise it will generate the generic form of the atomic operations.
4077 This requires temp(s) and has their address taken. The atomic processing
4078 is smart enough to figure out when the size of an object can utilize
4079 a lock-free version, and convert the built-in call to the appropriate
4080 lock-free routine. The optimizers will then dispose of any temps that
4081 are no longer required, and lock-free implementations are utilized as
4082 long as there is target support for the required size.
4083
4084 If the operator is NOP_EXPR, then this is a simple assignment, and
4085 an __atomic_store is issued to perform the assignment rather than
4086 the above loop. */
4087
4088 /* Build an atomic assignment at LOC, expanding into the proper
4089 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4090 the result of the operation, unless RETURN_OLD_P, in which case
4091 return the old value of LHS (this is only for postincrement and
4092 postdecrement). */
4093
4094 static tree
build_atomic_assign(location_t loc,tree lhs,enum tree_code modifycode,tree rhs,bool return_old_p)4095 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4096 tree rhs, bool return_old_p)
4097 {
4098 tree fndecl, func_call;
4099 vec<tree, va_gc> *params;
4100 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4101 tree old, old_addr;
4102 tree compound_stmt = NULL_TREE;
4103 tree stmt, goto_stmt;
4104 tree loop_label, loop_decl, done_label, done_decl;
4105
4106 tree lhs_type = TREE_TYPE (lhs);
4107 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4108 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4109 tree rhs_semantic_type = TREE_TYPE (rhs);
4110 tree nonatomic_rhs_semantic_type;
4111 tree rhs_type;
4112
4113 gcc_assert (TYPE_ATOMIC (lhs_type));
4114
4115 if (return_old_p)
4116 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4117
4118 /* Allocate enough vector items for a compare_exchange. */
4119 vec_alloc (params, 6);
4120
4121 /* Create a compound statement to hold the sequence of statements
4122 with a loop. */
4123 if (modifycode != NOP_EXPR)
4124 {
4125 compound_stmt = c_begin_compound_stmt (false);
4126
4127 /* For consistency with build_modify_expr on non-_Atomic,
4128 mark the lhs as read. Also, it would be very hard to match
4129 such expressions in mark_exp_read. */
4130 mark_exp_read (lhs);
4131 }
4132
4133 /* Remove any excess precision (which is only present here in the
4134 case of compound assignments). */
4135 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4136 {
4137 gcc_assert (modifycode != NOP_EXPR);
4138 rhs = TREE_OPERAND (rhs, 0);
4139 }
4140 rhs_type = TREE_TYPE (rhs);
4141
4142 /* Fold the RHS if it hasn't already been folded. */
4143 if (modifycode != NOP_EXPR)
4144 rhs = c_fully_fold (rhs, false, NULL);
4145
4146 /* Remove the qualifiers for the rest of the expressions and create
4147 the VAL temp variable to hold the RHS. */
4148 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4149 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4150 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4151 TYPE_UNQUALIFIED);
4152 val = create_tmp_var_raw (nonatomic_rhs_type);
4153 TREE_ADDRESSABLE (val) = 1;
4154 TREE_NO_WARNING (val) = 1;
4155 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4156 NULL_TREE);
4157 TREE_SIDE_EFFECTS (rhs) = 1;
4158 SET_EXPR_LOCATION (rhs, loc);
4159 if (modifycode != NOP_EXPR)
4160 add_stmt (rhs);
4161
4162 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4163 an atomic_store. */
4164 if (modifycode == NOP_EXPR)
4165 {
4166 compound_stmt = rhs;
4167 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4168 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4169 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4170 params->quick_push (lhs_addr);
4171 params->quick_push (rhs);
4172 params->quick_push (seq_cst);
4173 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4174
4175 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4176 compound_stmt, func_call);
4177
4178 /* VAL is the value which was stored, return a COMPOUND_STMT of
4179 the statement and that value. */
4180 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4181 }
4182
4183 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4184 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4185 isn't applicable for such builtins. ??? Do we want to handle enums? */
4186 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4187 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4188 {
4189 built_in_function fncode;
4190 switch (modifycode)
4191 {
4192 case PLUS_EXPR:
4193 case POINTER_PLUS_EXPR:
4194 fncode = (return_old_p
4195 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4196 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4197 break;
4198 case MINUS_EXPR:
4199 fncode = (return_old_p
4200 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4201 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4202 break;
4203 case BIT_AND_EXPR:
4204 fncode = (return_old_p
4205 ? BUILT_IN_ATOMIC_FETCH_AND_N
4206 : BUILT_IN_ATOMIC_AND_FETCH_N);
4207 break;
4208 case BIT_IOR_EXPR:
4209 fncode = (return_old_p
4210 ? BUILT_IN_ATOMIC_FETCH_OR_N
4211 : BUILT_IN_ATOMIC_OR_FETCH_N);
4212 break;
4213 case BIT_XOR_EXPR:
4214 fncode = (return_old_p
4215 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4216 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4217 break;
4218 default:
4219 goto cas_loop;
4220 }
4221
4222 /* We can only use "_1" through "_16" variants of the atomic fetch
4223 built-ins. */
4224 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4225 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4226 goto cas_loop;
4227
4228 /* If this is a pointer type, we need to multiply by the size of
4229 the pointer target type. */
4230 if (POINTER_TYPE_P (lhs_type))
4231 {
4232 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4233 /* ??? This would introduce -Wdiscarded-qualifiers
4234 warning: __atomic_fetch_* expect volatile void *
4235 type as the first argument. (Assignments between
4236 atomic and non-atomic objects are OK.) */
4237 || TYPE_RESTRICT (lhs_type))
4238 goto cas_loop;
4239 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4240 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4241 convert (ptrdiff_type_node, rhs),
4242 convert (ptrdiff_type_node, sz));
4243 }
4244
4245 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4246 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4247 fndecl = builtin_decl_explicit (fncode);
4248 params->quick_push (lhs_addr);
4249 params->quick_push (rhs);
4250 params->quick_push (seq_cst);
4251 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4252
4253 newval = create_tmp_var_raw (nonatomic_lhs_type);
4254 TREE_ADDRESSABLE (newval) = 1;
4255 TREE_NO_WARNING (newval) = 1;
4256 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4257 NULL_TREE, NULL_TREE);
4258 SET_EXPR_LOCATION (rhs, loc);
4259 add_stmt (rhs);
4260
4261 /* Finish the compound statement. */
4262 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4263
4264 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4265 the statement and that value. */
4266 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4267 }
4268
4269 cas_loop:
4270 /* Create the variables and labels required for the op= form. */
4271 old = create_tmp_var_raw (nonatomic_lhs_type);
4272 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4273 TREE_ADDRESSABLE (old) = 1;
4274 TREE_NO_WARNING (old) = 1;
4275
4276 newval = create_tmp_var_raw (nonatomic_lhs_type);
4277 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4278 TREE_ADDRESSABLE (newval) = 1;
4279 TREE_NO_WARNING (newval) = 1;
4280
4281 loop_decl = create_artificial_label (loc);
4282 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4283
4284 done_decl = create_artificial_label (loc);
4285 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4286
4287 /* __atomic_load (addr, &old, SEQ_CST). */
4288 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4289 params->quick_push (lhs_addr);
4290 params->quick_push (old_addr);
4291 params->quick_push (seq_cst);
4292 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4293 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4294 NULL_TREE);
4295 add_stmt (old);
4296 params->truncate (0);
4297
4298 /* Create the expressions for floating-point environment
4299 manipulation, if required. */
4300 bool need_fenv = (flag_trapping_math
4301 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4302 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4303 if (need_fenv)
4304 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4305
4306 if (hold_call)
4307 add_stmt (hold_call);
4308
4309 /* loop: */
4310 add_stmt (loop_label);
4311
4312 /* newval = old + val; */
4313 if (rhs_type != rhs_semantic_type)
4314 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4315 rhs = build_binary_op (loc, modifycode, old, val, true);
4316 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4317 {
4318 tree eptype = TREE_TYPE (rhs);
4319 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4320 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4321 }
4322 else
4323 rhs = c_fully_fold (rhs, false, NULL);
4324 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4325 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4326 NULL_TREE, 0);
4327 if (rhs != error_mark_node)
4328 {
4329 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4330 NULL_TREE);
4331 SET_EXPR_LOCATION (rhs, loc);
4332 add_stmt (rhs);
4333 }
4334
4335 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4336 goto done; */
4337 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4338 params->quick_push (lhs_addr);
4339 params->quick_push (old_addr);
4340 params->quick_push (newval_addr);
4341 params->quick_push (integer_zero_node);
4342 params->quick_push (seq_cst);
4343 params->quick_push (seq_cst);
4344 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4345
4346 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4347 SET_EXPR_LOCATION (goto_stmt, loc);
4348
4349 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4350 SET_EXPR_LOCATION (stmt, loc);
4351 add_stmt (stmt);
4352
4353 if (clear_call)
4354 add_stmt (clear_call);
4355
4356 /* goto loop; */
4357 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4358 SET_EXPR_LOCATION (goto_stmt, loc);
4359 add_stmt (goto_stmt);
4360
4361 /* done: */
4362 add_stmt (done_label);
4363
4364 if (update_call)
4365 add_stmt (update_call);
4366
4367 /* Finish the compound statement. */
4368 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4369
4370 /* NEWVAL is the value that was successfully stored, return a
4371 COMPOUND_EXPR of the statement and the appropriate value. */
4372 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4373 return_old_p ? old : newval);
4374 }
4375
4376 /* Construct and perhaps optimize a tree representation
4377 for a unary operation. CODE, a tree_code, specifies the operation
4378 and XARG is the operand.
4379 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4380 promotions (such as from short to int).
4381 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4382 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4383 to pointers in C99.
4384
4385 LOCATION is the location of the operator. */
4386
4387 tree
build_unary_op(location_t location,enum tree_code code,tree xarg,bool noconvert)4388 build_unary_op (location_t location, enum tree_code code, tree xarg,
4389 bool noconvert)
4390 {
4391 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4392 tree arg = xarg;
4393 tree argtype = NULL_TREE;
4394 enum tree_code typecode;
4395 tree val;
4396 tree ret = error_mark_node;
4397 tree eptype = NULL_TREE;
4398 const char *invalid_op_diag;
4399 bool int_operands;
4400
4401 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4402 if (int_operands)
4403 arg = remove_c_maybe_const_expr (arg);
4404
4405 if (code != ADDR_EXPR)
4406 arg = require_complete_type (location, arg);
4407
4408 typecode = TREE_CODE (TREE_TYPE (arg));
4409 if (typecode == ERROR_MARK)
4410 return error_mark_node;
4411 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4412 typecode = INTEGER_TYPE;
4413
4414 if ((invalid_op_diag
4415 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4416 {
4417 error_at (location, invalid_op_diag);
4418 return error_mark_node;
4419 }
4420
4421 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4422 {
4423 eptype = TREE_TYPE (arg);
4424 arg = TREE_OPERAND (arg, 0);
4425 }
4426
4427 switch (code)
4428 {
4429 case CONVERT_EXPR:
4430 /* This is used for unary plus, because a CONVERT_EXPR
4431 is enough to prevent anybody from looking inside for
4432 associativity, but won't generate any code. */
4433 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4434 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4435 || gnu_vector_type_p (TREE_TYPE (arg))))
4436 {
4437 error_at (location, "wrong type argument to unary plus");
4438 return error_mark_node;
4439 }
4440 else if (!noconvert)
4441 arg = default_conversion (arg);
4442 arg = non_lvalue_loc (location, arg);
4443 break;
4444
4445 case NEGATE_EXPR:
4446 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4447 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4448 || gnu_vector_type_p (TREE_TYPE (arg))))
4449 {
4450 error_at (location, "wrong type argument to unary minus");
4451 return error_mark_node;
4452 }
4453 else if (!noconvert)
4454 arg = default_conversion (arg);
4455 break;
4456
4457 case BIT_NOT_EXPR:
4458 /* ~ works on integer types and non float vectors. */
4459 if (typecode == INTEGER_TYPE
4460 || (gnu_vector_type_p (TREE_TYPE (arg))
4461 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4462 {
4463 tree e = arg;
4464
4465 /* Warn if the expression has boolean value. */
4466 while (TREE_CODE (e) == COMPOUND_EXPR)
4467 e = TREE_OPERAND (e, 1);
4468
4469 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4470 || truth_value_p (TREE_CODE (e))))
4471 {
4472 auto_diagnostic_group d;
4473 if (warning_at (location, OPT_Wbool_operation,
4474 "%<~%> on a boolean expression"))
4475 {
4476 gcc_rich_location richloc (location);
4477 richloc.add_fixit_insert_before (location, "!");
4478 inform (&richloc, "did you mean to use logical not?");
4479 }
4480 }
4481 if (!noconvert)
4482 arg = default_conversion (arg);
4483 }
4484 else if (typecode == COMPLEX_TYPE)
4485 {
4486 code = CONJ_EXPR;
4487 pedwarn (location, OPT_Wpedantic,
4488 "ISO C does not support %<~%> for complex conjugation");
4489 if (!noconvert)
4490 arg = default_conversion (arg);
4491 }
4492 else
4493 {
4494 error_at (location, "wrong type argument to bit-complement");
4495 return error_mark_node;
4496 }
4497 break;
4498
4499 case ABS_EXPR:
4500 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4501 {
4502 error_at (location, "wrong type argument to abs");
4503 return error_mark_node;
4504 }
4505 else if (!noconvert)
4506 arg = default_conversion (arg);
4507 break;
4508
4509 case ABSU_EXPR:
4510 if (!(typecode == INTEGER_TYPE))
4511 {
4512 error_at (location, "wrong type argument to absu");
4513 return error_mark_node;
4514 }
4515 else if (!noconvert)
4516 arg = default_conversion (arg);
4517 break;
4518
4519 case CONJ_EXPR:
4520 /* Conjugating a real value is a no-op, but allow it anyway. */
4521 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4522 || typecode == COMPLEX_TYPE))
4523 {
4524 error_at (location, "wrong type argument to conjugation");
4525 return error_mark_node;
4526 }
4527 else if (!noconvert)
4528 arg = default_conversion (arg);
4529 break;
4530
4531 case TRUTH_NOT_EXPR:
4532 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4533 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4534 && typecode != COMPLEX_TYPE)
4535 {
4536 error_at (location,
4537 "wrong type argument to unary exclamation mark");
4538 return error_mark_node;
4539 }
4540 if (int_operands)
4541 {
4542 arg = c_objc_common_truthvalue_conversion (location, xarg);
4543 arg = remove_c_maybe_const_expr (arg);
4544 }
4545 else
4546 arg = c_objc_common_truthvalue_conversion (location, arg);
4547 ret = invert_truthvalue_loc (location, arg);
4548 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4549 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4550 location = EXPR_LOCATION (ret);
4551 goto return_build_unary_op;
4552
4553 case REALPART_EXPR:
4554 case IMAGPART_EXPR:
4555 ret = build_real_imag_expr (location, code, arg);
4556 if (ret == error_mark_node)
4557 return error_mark_node;
4558 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4559 eptype = TREE_TYPE (eptype);
4560 goto return_build_unary_op;
4561
4562 case PREINCREMENT_EXPR:
4563 case POSTINCREMENT_EXPR:
4564 case PREDECREMENT_EXPR:
4565 case POSTDECREMENT_EXPR:
4566
4567 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4568 {
4569 tree inner = build_unary_op (location, code,
4570 C_MAYBE_CONST_EXPR_EXPR (arg),
4571 noconvert);
4572 if (inner == error_mark_node)
4573 return error_mark_node;
4574 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4575 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4576 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4577 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4578 goto return_build_unary_op;
4579 }
4580
4581 /* Complain about anything that is not a true lvalue. In
4582 Objective-C, skip this check for property_refs. */
4583 if (!objc_is_property_ref (arg)
4584 && !lvalue_or_else (location,
4585 arg, ((code == PREINCREMENT_EXPR
4586 || code == POSTINCREMENT_EXPR)
4587 ? lv_increment
4588 : lv_decrement)))
4589 return error_mark_node;
4590
4591 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4592 {
4593 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4594 warning_at (location, OPT_Wc___compat,
4595 "increment of enumeration value is invalid in C++");
4596 else
4597 warning_at (location, OPT_Wc___compat,
4598 "decrement of enumeration value is invalid in C++");
4599 }
4600
4601 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4602 {
4603 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4604 warning_at (location, OPT_Wbool_operation,
4605 "increment of a boolean expression");
4606 else
4607 warning_at (location, OPT_Wbool_operation,
4608 "decrement of a boolean expression");
4609 }
4610
4611 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4612 arg = c_fully_fold (arg, false, NULL, true);
4613
4614 bool atomic_op;
4615 atomic_op = really_atomic_lvalue (arg);
4616
4617 /* Increment or decrement the real part of the value,
4618 and don't change the imaginary part. */
4619 if (typecode == COMPLEX_TYPE)
4620 {
4621 tree real, imag;
4622
4623 pedwarn (location, OPT_Wpedantic,
4624 "ISO C does not support %<++%> and %<--%> on complex types");
4625
4626 if (!atomic_op)
4627 {
4628 arg = stabilize_reference (arg);
4629 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4630 true);
4631 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4632 true);
4633 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4634 if (real == error_mark_node || imag == error_mark_node)
4635 return error_mark_node;
4636 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4637 real, imag);
4638 goto return_build_unary_op;
4639 }
4640 }
4641
4642 /* Report invalid types. */
4643
4644 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4645 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4646 && typecode != COMPLEX_TYPE
4647 && !gnu_vector_type_p (TREE_TYPE (arg)))
4648 {
4649 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4650 error_at (location, "wrong type argument to increment");
4651 else
4652 error_at (location, "wrong type argument to decrement");
4653
4654 return error_mark_node;
4655 }
4656
4657 {
4658 tree inc;
4659
4660 argtype = TREE_TYPE (arg);
4661
4662 /* Compute the increment. */
4663
4664 if (typecode == POINTER_TYPE)
4665 {
4666 /* If pointer target is an incomplete type,
4667 we just cannot know how to do the arithmetic. */
4668 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4669 {
4670 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4671 error_at (location,
4672 "increment of pointer to an incomplete type %qT",
4673 TREE_TYPE (argtype));
4674 else
4675 error_at (location,
4676 "decrement of pointer to an incomplete type %qT",
4677 TREE_TYPE (argtype));
4678 }
4679 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4680 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4681 {
4682 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4683 pedwarn (location, OPT_Wpointer_arith,
4684 "wrong type argument to increment");
4685 else
4686 pedwarn (location, OPT_Wpointer_arith,
4687 "wrong type argument to decrement");
4688 }
4689 else
4690 verify_type_context (location, TCTX_POINTER_ARITH,
4691 TREE_TYPE (argtype));
4692
4693 inc = c_size_in_bytes (TREE_TYPE (argtype));
4694 inc = convert_to_ptrofftype_loc (location, inc);
4695 }
4696 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4697 {
4698 /* For signed fract types, we invert ++ to -- or
4699 -- to ++, and change inc from 1 to -1, because
4700 it is not possible to represent 1 in signed fract constants.
4701 For unsigned fract types, the result always overflows and
4702 we get an undefined (original) or the maximum value. */
4703 if (code == PREINCREMENT_EXPR)
4704 code = PREDECREMENT_EXPR;
4705 else if (code == PREDECREMENT_EXPR)
4706 code = PREINCREMENT_EXPR;
4707 else if (code == POSTINCREMENT_EXPR)
4708 code = POSTDECREMENT_EXPR;
4709 else /* code == POSTDECREMENT_EXPR */
4710 code = POSTINCREMENT_EXPR;
4711
4712 inc = integer_minus_one_node;
4713 inc = convert (argtype, inc);
4714 }
4715 else
4716 {
4717 inc = VECTOR_TYPE_P (argtype)
4718 ? build_one_cst (argtype)
4719 : integer_one_node;
4720 inc = convert (argtype, inc);
4721 }
4722
4723 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4724 need to ask Objective-C to build the increment or decrement
4725 expression for it. */
4726 if (objc_is_property_ref (arg))
4727 return objc_build_incr_expr_for_property_ref (location, code,
4728 arg, inc);
4729
4730 /* Report a read-only lvalue. */
4731 if (TYPE_READONLY (argtype))
4732 {
4733 readonly_error (location, arg,
4734 ((code == PREINCREMENT_EXPR
4735 || code == POSTINCREMENT_EXPR)
4736 ? lv_increment : lv_decrement));
4737 return error_mark_node;
4738 }
4739 else if (TREE_READONLY (arg))
4740 readonly_warning (arg,
4741 ((code == PREINCREMENT_EXPR
4742 || code == POSTINCREMENT_EXPR)
4743 ? lv_increment : lv_decrement));
4744
4745 /* If the argument is atomic, use the special code sequences for
4746 atomic compound assignment. */
4747 if (atomic_op)
4748 {
4749 arg = stabilize_reference (arg);
4750 ret = build_atomic_assign (location, arg,
4751 ((code == PREINCREMENT_EXPR
4752 || code == POSTINCREMENT_EXPR)
4753 ? PLUS_EXPR
4754 : MINUS_EXPR),
4755 (FRACT_MODE_P (TYPE_MODE (argtype))
4756 ? inc
4757 : integer_one_node),
4758 (code == POSTINCREMENT_EXPR
4759 || code == POSTDECREMENT_EXPR));
4760 goto return_build_unary_op;
4761 }
4762
4763 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4764 val = boolean_increment (code, arg);
4765 else
4766 val = build2 (code, TREE_TYPE (arg), arg, inc);
4767 TREE_SIDE_EFFECTS (val) = 1;
4768 if (TREE_CODE (val) != code)
4769 TREE_NO_WARNING (val) = 1;
4770 ret = val;
4771 goto return_build_unary_op;
4772 }
4773
4774 case ADDR_EXPR:
4775 /* Note that this operation never does default_conversion. */
4776
4777 /* The operand of unary '&' must be an lvalue (which excludes
4778 expressions of type void), or, in C99, the result of a [] or
4779 unary '*' operator. */
4780 if (VOID_TYPE_P (TREE_TYPE (arg))
4781 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4782 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4783 pedwarn (location, 0, "taking address of expression of type %<void%>");
4784
4785 /* Let &* cancel out to simplify resulting code. */
4786 if (INDIRECT_REF_P (arg))
4787 {
4788 /* Don't let this be an lvalue. */
4789 if (lvalue_p (TREE_OPERAND (arg, 0)))
4790 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4791 ret = TREE_OPERAND (arg, 0);
4792 goto return_build_unary_op;
4793 }
4794
4795 /* Anything not already handled and not a true memory reference
4796 or a non-lvalue array is an error. */
4797 if (typecode != FUNCTION_TYPE && !noconvert
4798 && !lvalue_or_else (location, arg, lv_addressof))
4799 return error_mark_node;
4800
4801 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4802 folding later. */
4803 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4804 {
4805 tree inner = build_unary_op (location, code,
4806 C_MAYBE_CONST_EXPR_EXPR (arg),
4807 noconvert);
4808 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4809 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4810 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4811 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4812 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4813 goto return_build_unary_op;
4814 }
4815
4816 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4817 argtype = TREE_TYPE (arg);
4818
4819 /* If the lvalue is const or volatile, merge that into the type
4820 to which the address will point. This is only needed
4821 for function types. */
4822 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4823 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4824 && TREE_CODE (argtype) == FUNCTION_TYPE)
4825 {
4826 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4827 int quals = orig_quals;
4828
4829 if (TREE_READONLY (arg))
4830 quals |= TYPE_QUAL_CONST;
4831 if (TREE_THIS_VOLATILE (arg))
4832 quals |= TYPE_QUAL_VOLATILE;
4833
4834 argtype = c_build_qualified_type (argtype, quals);
4835 }
4836
4837 switch (TREE_CODE (arg))
4838 {
4839 case COMPONENT_REF:
4840 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4841 {
4842 error_at (location, "cannot take address of bit-field %qD",
4843 TREE_OPERAND (arg, 1));
4844 return error_mark_node;
4845 }
4846
4847 /* fall through */
4848
4849 case ARRAY_REF:
4850 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4851 {
4852 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4853 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4854 {
4855 error_at (location, "cannot take address of scalar with "
4856 "reverse storage order");
4857 return error_mark_node;
4858 }
4859
4860 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4861 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4862 warning_at (location, OPT_Wscalar_storage_order,
4863 "address of array with reverse scalar storage "
4864 "order requested");
4865 }
4866
4867 default:
4868 break;
4869 }
4870
4871 if (!c_mark_addressable (arg))
4872 return error_mark_node;
4873
4874 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4875 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4876
4877 argtype = build_pointer_type (argtype);
4878
4879 /* ??? Cope with user tricks that amount to offsetof. Delete this
4880 when we have proper support for integer constant expressions. */
4881 val = get_base_address (arg);
4882 if (val && INDIRECT_REF_P (val)
4883 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4884 {
4885 ret = fold_offsetof (arg, argtype);
4886 goto return_build_unary_op;
4887 }
4888
4889 val = build1 (ADDR_EXPR, argtype, arg);
4890
4891 ret = val;
4892 goto return_build_unary_op;
4893
4894 default:
4895 gcc_unreachable ();
4896 }
4897
4898 if (argtype == NULL_TREE)
4899 argtype = TREE_TYPE (arg);
4900 if (TREE_CODE (arg) == INTEGER_CST)
4901 ret = (require_constant_value
4902 ? fold_build1_initializer_loc (location, code, argtype, arg)
4903 : fold_build1_loc (location, code, argtype, arg));
4904 else
4905 ret = build1 (code, argtype, arg);
4906 return_build_unary_op:
4907 gcc_assert (ret != error_mark_node);
4908 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4909 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4910 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4911 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4912 ret = note_integer_operands (ret);
4913 if (eptype)
4914 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4915 protected_set_expr_location (ret, location);
4916 return ret;
4917 }
4918
4919 /* Return nonzero if REF is an lvalue valid for this language.
4920 Lvalues can be assigned, unless their type has TYPE_READONLY.
4921 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4922
4923 bool
lvalue_p(const_tree ref)4924 lvalue_p (const_tree ref)
4925 {
4926 const enum tree_code code = TREE_CODE (ref);
4927
4928 switch (code)
4929 {
4930 case REALPART_EXPR:
4931 case IMAGPART_EXPR:
4932 case COMPONENT_REF:
4933 return lvalue_p (TREE_OPERAND (ref, 0));
4934
4935 case C_MAYBE_CONST_EXPR:
4936 return lvalue_p (TREE_OPERAND (ref, 1));
4937
4938 case COMPOUND_LITERAL_EXPR:
4939 case STRING_CST:
4940 return true;
4941
4942 case INDIRECT_REF:
4943 case ARRAY_REF:
4944 case VAR_DECL:
4945 case PARM_DECL:
4946 case RESULT_DECL:
4947 case ERROR_MARK:
4948 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4949 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4950
4951 case BIND_EXPR:
4952 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4953
4954 default:
4955 return false;
4956 }
4957 }
4958
4959 /* Give a warning for storing in something that is read-only in GCC
4960 terms but not const in ISO C terms. */
4961
4962 static void
readonly_warning(tree arg,enum lvalue_use use)4963 readonly_warning (tree arg, enum lvalue_use use)
4964 {
4965 switch (use)
4966 {
4967 case lv_assign:
4968 warning (0, "assignment of read-only location %qE", arg);
4969 break;
4970 case lv_increment:
4971 warning (0, "increment of read-only location %qE", arg);
4972 break;
4973 case lv_decrement:
4974 warning (0, "decrement of read-only location %qE", arg);
4975 break;
4976 default:
4977 gcc_unreachable ();
4978 }
4979 return;
4980 }
4981
4982
4983 /* Return nonzero if REF is an lvalue valid for this language;
4984 otherwise, print an error message and return zero. USE says
4985 how the lvalue is being used and so selects the error message.
4986 LOCATION is the location at which any error should be reported. */
4987
4988 static int
lvalue_or_else(location_t loc,const_tree ref,enum lvalue_use use)4989 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4990 {
4991 int win = lvalue_p (ref);
4992
4993 if (!win)
4994 lvalue_error (loc, use);
4995
4996 return win;
4997 }
4998
4999 /* Mark EXP saying that we need to be able to take the
5000 address of it; it should not be allocated in a register.
5001 Returns true if successful. ARRAY_REF_P is true if this
5002 is for ARRAY_REF construction - in that case we don't want
5003 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5004 it is fine to use ARRAY_REFs for vector subscripts on vector
5005 register variables. */
5006
5007 bool
c_mark_addressable(tree exp,bool array_ref_p)5008 c_mark_addressable (tree exp, bool array_ref_p)
5009 {
5010 tree x = exp;
5011
5012 while (1)
5013 switch (TREE_CODE (x))
5014 {
5015 case VIEW_CONVERT_EXPR:
5016 if (array_ref_p
5017 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5018 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5019 return true;
5020 /* FALLTHRU */
5021 case COMPONENT_REF:
5022 case ADDR_EXPR:
5023 case ARRAY_REF:
5024 case REALPART_EXPR:
5025 case IMAGPART_EXPR:
5026 x = TREE_OPERAND (x, 0);
5027 break;
5028
5029 case COMPOUND_LITERAL_EXPR:
5030 TREE_ADDRESSABLE (x) = 1;
5031 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5032 return true;
5033
5034 case CONSTRUCTOR:
5035 TREE_ADDRESSABLE (x) = 1;
5036 return true;
5037
5038 case VAR_DECL:
5039 case CONST_DECL:
5040 case PARM_DECL:
5041 case RESULT_DECL:
5042 if (C_DECL_REGISTER (x)
5043 && DECL_NONLOCAL (x))
5044 {
5045 if (TREE_PUBLIC (x) || is_global_var (x))
5046 {
5047 error
5048 ("global register variable %qD used in nested function", x);
5049 return false;
5050 }
5051 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5052 }
5053 else if (C_DECL_REGISTER (x))
5054 {
5055 if (TREE_PUBLIC (x) || is_global_var (x))
5056 error ("address of global register variable %qD requested", x);
5057 else
5058 error ("address of register variable %qD requested", x);
5059 return false;
5060 }
5061
5062 /* FALLTHRU */
5063 case FUNCTION_DECL:
5064 TREE_ADDRESSABLE (x) = 1;
5065 /* FALLTHRU */
5066 default:
5067 return true;
5068 }
5069 }
5070
5071 /* Convert EXPR to TYPE, warning about conversion problems with
5072 constants. SEMANTIC_TYPE is the type this conversion would use
5073 without excess precision. If SEMANTIC_TYPE is NULL, this function
5074 is equivalent to convert_and_check. This function is a wrapper that
5075 handles conversions that may be different than
5076 the usual ones because of excess precision. */
5077
5078 static tree
ep_convert_and_check(location_t loc,tree type,tree expr,tree semantic_type)5079 ep_convert_and_check (location_t loc, tree type, tree expr,
5080 tree semantic_type)
5081 {
5082 if (TREE_TYPE (expr) == type)
5083 return expr;
5084
5085 /* For C11, integer conversions may have results with excess
5086 precision. */
5087 if (flag_isoc11 || !semantic_type)
5088 return convert_and_check (loc, type, expr);
5089
5090 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5091 && TREE_TYPE (expr) != semantic_type)
5092 {
5093 /* For integers, we need to check the real conversion, not
5094 the conversion to the excess precision type. */
5095 expr = convert_and_check (loc, semantic_type, expr);
5096 }
5097 /* Result type is the excess precision type, which should be
5098 large enough, so do not check. */
5099 return convert (type, expr);
5100 }
5101
5102 /* If EXPR refers to a built-in declared without a prototype returns
5103 the actual type of the built-in and, if non-null, set *BLTIN to
5104 a pointer to the built-in. Otherwise return the type of EXPR
5105 and clear *BLTIN if non-null. */
5106
5107 static tree
5108 type_or_builtin_type (tree expr, tree *bltin = NULL)
5109 {
5110 tree dummy;
5111 if (!bltin)
5112 bltin = &dummy;
5113
5114 *bltin = NULL_TREE;
5115
5116 tree type = TREE_TYPE (expr);
5117 if (TREE_CODE (expr) != ADDR_EXPR)
5118 return type;
5119
5120 tree oper = TREE_OPERAND (expr, 0);
5121 if (!DECL_P (oper)
5122 || TREE_CODE (oper) != FUNCTION_DECL
5123 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5124 return type;
5125
5126 built_in_function code = DECL_FUNCTION_CODE (oper);
5127 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5128 return type;
5129
5130 if ((*bltin = builtin_decl_implicit (code)))
5131 type = build_pointer_type (TREE_TYPE (*bltin));
5132
5133 return type;
5134 }
5135
5136 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5137 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5138 if folded to an integer constant then the unselected half may
5139 contain arbitrary operations not normally permitted in constant
5140 expressions. Set the location of the expression to LOC. */
5141
5142 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)5143 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5144 tree op1, tree op1_original_type, location_t op1_loc,
5145 tree op2, tree op2_original_type, location_t op2_loc)
5146 {
5147 tree type1;
5148 tree type2;
5149 enum tree_code code1;
5150 enum tree_code code2;
5151 tree result_type = NULL;
5152 tree semantic_result_type = NULL;
5153 tree orig_op1 = op1, orig_op2 = op2;
5154 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5155 bool ifexp_int_operands;
5156 tree ret;
5157
5158 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5159 if (op1_int_operands)
5160 op1 = remove_c_maybe_const_expr (op1);
5161 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5162 if (op2_int_operands)
5163 op2 = remove_c_maybe_const_expr (op2);
5164 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5165 if (ifexp_int_operands)
5166 ifexp = remove_c_maybe_const_expr (ifexp);
5167
5168 /* Promote both alternatives. */
5169
5170 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5171 op1 = default_conversion (op1);
5172 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5173 op2 = default_conversion (op2);
5174
5175 if (TREE_CODE (ifexp) == ERROR_MARK
5176 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5177 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5178 return error_mark_node;
5179
5180 tree bltin1 = NULL_TREE;
5181 tree bltin2 = NULL_TREE;
5182 type1 = type_or_builtin_type (op1, &bltin1);
5183 code1 = TREE_CODE (type1);
5184 type2 = type_or_builtin_type (op2, &bltin2);
5185 code2 = TREE_CODE (type2);
5186
5187 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5188 return error_mark_node;
5189
5190 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5191 return error_mark_node;
5192
5193 /* C90 does not permit non-lvalue arrays in conditional expressions.
5194 In C99 they will be pointers by now. */
5195 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5196 {
5197 error_at (colon_loc, "non-lvalue array in conditional expression");
5198 return error_mark_node;
5199 }
5200
5201 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5202 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5203 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5204 || code1 == COMPLEX_TYPE)
5205 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5206 || code2 == COMPLEX_TYPE))
5207 {
5208 semantic_result_type = c_common_type (type1, type2);
5209 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5210 {
5211 op1 = TREE_OPERAND (op1, 0);
5212 type1 = TREE_TYPE (op1);
5213 gcc_assert (TREE_CODE (type1) == code1);
5214 }
5215 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5216 {
5217 op2 = TREE_OPERAND (op2, 0);
5218 type2 = TREE_TYPE (op2);
5219 gcc_assert (TREE_CODE (type2) == code2);
5220 }
5221 }
5222
5223 if (warn_cxx_compat)
5224 {
5225 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5226 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5227
5228 if (TREE_CODE (t1) == ENUMERAL_TYPE
5229 && TREE_CODE (t2) == ENUMERAL_TYPE
5230 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5231 warning_at (colon_loc, OPT_Wc___compat,
5232 ("different enum types in conditional is "
5233 "invalid in C++: %qT vs %qT"),
5234 t1, t2);
5235 }
5236
5237 /* Quickly detect the usual case where op1 and op2 have the same type
5238 after promotion. */
5239 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5240 {
5241 if (type1 == type2)
5242 result_type = type1;
5243 else
5244 result_type = TYPE_MAIN_VARIANT (type1);
5245 }
5246 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5247 || code1 == COMPLEX_TYPE)
5248 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5249 || code2 == COMPLEX_TYPE))
5250 {
5251 /* In C11, a conditional expression between a floating-point
5252 type and an integer type should convert the integer type to
5253 the evaluation format of the floating-point type, with
5254 possible excess precision. */
5255 tree eptype1 = type1;
5256 tree eptype2 = type2;
5257 if (flag_isoc11)
5258 {
5259 tree eptype;
5260 if (ANY_INTEGRAL_TYPE_P (type1)
5261 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5262 {
5263 eptype2 = eptype;
5264 if (!semantic_result_type)
5265 semantic_result_type = c_common_type (type1, type2);
5266 }
5267 else if (ANY_INTEGRAL_TYPE_P (type2)
5268 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5269 {
5270 eptype1 = eptype;
5271 if (!semantic_result_type)
5272 semantic_result_type = c_common_type (type1, type2);
5273 }
5274 }
5275 result_type = c_common_type (eptype1, eptype2);
5276 if (result_type == error_mark_node)
5277 return error_mark_node;
5278 do_warn_double_promotion (result_type, type1, type2,
5279 "implicit conversion from %qT to %qT to "
5280 "match other result of conditional",
5281 colon_loc);
5282
5283 /* If -Wsign-compare, warn here if type1 and type2 have
5284 different signedness. We'll promote the signed to unsigned
5285 and later code won't know it used to be different.
5286 Do this check on the original types, so that explicit casts
5287 will be considered, but default promotions won't. */
5288 if (c_inhibit_evaluation_warnings == 0)
5289 {
5290 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5291 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5292
5293 if (unsigned_op1 ^ unsigned_op2)
5294 {
5295 bool ovf;
5296
5297 /* Do not warn if the result type is signed, since the
5298 signed type will only be chosen if it can represent
5299 all the values of the unsigned type. */
5300 if (!TYPE_UNSIGNED (result_type))
5301 /* OK */;
5302 else
5303 {
5304 bool op1_maybe_const = true;
5305 bool op2_maybe_const = true;
5306
5307 /* Do not warn if the signed quantity is an
5308 unsuffixed integer literal (or some static
5309 constant expression involving such literals) and
5310 it is non-negative. This warning requires the
5311 operands to be folded for best results, so do
5312 that folding in this case even without
5313 warn_sign_compare to avoid warning options
5314 possibly affecting code generation. */
5315 c_inhibit_evaluation_warnings
5316 += (ifexp == truthvalue_false_node);
5317 op1 = c_fully_fold (op1, require_constant_value,
5318 &op1_maybe_const);
5319 c_inhibit_evaluation_warnings
5320 -= (ifexp == truthvalue_false_node);
5321
5322 c_inhibit_evaluation_warnings
5323 += (ifexp == truthvalue_true_node);
5324 op2 = c_fully_fold (op2, require_constant_value,
5325 &op2_maybe_const);
5326 c_inhibit_evaluation_warnings
5327 -= (ifexp == truthvalue_true_node);
5328
5329 if (warn_sign_compare)
5330 {
5331 if ((unsigned_op2
5332 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5333 || (unsigned_op1
5334 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5335 /* OK */;
5336 else if (unsigned_op2)
5337 warning_at (op1_loc, OPT_Wsign_compare,
5338 "operand of %<?:%> changes signedness from "
5339 "%qT to %qT due to unsignedness of other "
5340 "operand", TREE_TYPE (orig_op1),
5341 TREE_TYPE (orig_op2));
5342 else
5343 warning_at (op2_loc, OPT_Wsign_compare,
5344 "operand of %<?:%> changes signedness from "
5345 "%qT to %qT due to unsignedness of other "
5346 "operand", TREE_TYPE (orig_op2),
5347 TREE_TYPE (orig_op1));
5348 }
5349 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5350 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5351 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5352 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5353 }
5354 }
5355 }
5356 }
5357 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5358 {
5359 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5360 pedwarn (colon_loc, OPT_Wpedantic,
5361 "ISO C forbids conditional expr with only one void side");
5362 result_type = void_type_node;
5363 }
5364 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5365 {
5366 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5367 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5368 addr_space_t as_common;
5369
5370 if (comp_target_types (colon_loc, type1, type2))
5371 result_type = common_pointer_type (type1, type2);
5372 else if (null_pointer_constant_p (orig_op1))
5373 result_type = type2;
5374 else if (null_pointer_constant_p (orig_op2))
5375 result_type = type1;
5376 else if (!addr_space_superset (as1, as2, &as_common))
5377 {
5378 error_at (colon_loc, "pointers to disjoint address spaces "
5379 "used in conditional expression");
5380 return error_mark_node;
5381 }
5382 else if (VOID_TYPE_P (TREE_TYPE (type1))
5383 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5384 {
5385 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5386 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5387 & ~TYPE_QUALS (TREE_TYPE (type1))))
5388 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5389 "pointer to array loses qualifier "
5390 "in conditional expression");
5391
5392 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5393 pedwarn (colon_loc, OPT_Wpedantic,
5394 "ISO C forbids conditional expr between "
5395 "%<void *%> and function pointer");
5396 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5397 TREE_TYPE (type2)));
5398 }
5399 else if (VOID_TYPE_P (TREE_TYPE (type2))
5400 && !TYPE_ATOMIC (TREE_TYPE (type2)))
5401 {
5402 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5403 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5404 & ~TYPE_QUALS (TREE_TYPE (type2))))
5405 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5406 "pointer to array loses qualifier "
5407 "in conditional expression");
5408
5409 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5410 pedwarn (colon_loc, OPT_Wpedantic,
5411 "ISO C forbids conditional expr between "
5412 "%<void *%> and function pointer");
5413 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5414 TREE_TYPE (type1)));
5415 }
5416 /* Objective-C pointer comparisons are a bit more lenient. */
5417 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5418 result_type = objc_common_type (type1, type2);
5419 else
5420 {
5421 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5422 if (bltin1 && bltin2)
5423 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5424 "pointer type mismatch between %qT and %qT "
5425 "of %qD and %qD in conditional expression",
5426 type1, type2, bltin1, bltin2);
5427 else
5428 pedwarn (colon_loc, 0,
5429 "pointer type mismatch in conditional expression");
5430 result_type = build_pointer_type
5431 (build_qualified_type (void_type_node, qual));
5432 }
5433 }
5434 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5435 {
5436 if (!null_pointer_constant_p (orig_op2))
5437 pedwarn (colon_loc, 0,
5438 "pointer/integer type mismatch in conditional expression");
5439 else
5440 {
5441 op2 = null_pointer_node;
5442 }
5443 result_type = type1;
5444 }
5445 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5446 {
5447 if (!null_pointer_constant_p (orig_op1))
5448 pedwarn (colon_loc, 0,
5449 "pointer/integer type mismatch in conditional expression");
5450 else
5451 {
5452 op1 = null_pointer_node;
5453 }
5454 result_type = type2;
5455 }
5456
5457 if (!result_type)
5458 {
5459 if (flag_cond_mismatch)
5460 result_type = void_type_node;
5461 else
5462 {
5463 error_at (colon_loc, "type mismatch in conditional expression");
5464 return error_mark_node;
5465 }
5466 }
5467
5468 /* Merge const and volatile flags of the incoming types. */
5469 result_type
5470 = build_type_variant (result_type,
5471 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5472 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5473
5474 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5475 semantic_result_type);
5476 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5477 semantic_result_type);
5478
5479 if (ifexp_bcp && ifexp == truthvalue_true_node)
5480 {
5481 op2_int_operands = true;
5482 op1 = c_fully_fold (op1, require_constant_value, NULL);
5483 }
5484 if (ifexp_bcp && ifexp == truthvalue_false_node)
5485 {
5486 op1_int_operands = true;
5487 op2 = c_fully_fold (op2, require_constant_value, NULL);
5488 }
5489 int_const = int_operands = (ifexp_int_operands
5490 && op1_int_operands
5491 && op2_int_operands);
5492 if (int_operands)
5493 {
5494 int_const = ((ifexp == truthvalue_true_node
5495 && TREE_CODE (orig_op1) == INTEGER_CST
5496 && !TREE_OVERFLOW (orig_op1))
5497 || (ifexp == truthvalue_false_node
5498 && TREE_CODE (orig_op2) == INTEGER_CST
5499 && !TREE_OVERFLOW (orig_op2)));
5500 }
5501
5502 /* Need to convert condition operand into a vector mask. */
5503 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5504 {
5505 tree vectype = TREE_TYPE (ifexp);
5506 tree elem_type = TREE_TYPE (vectype);
5507 tree zero = build_int_cst (elem_type, 0);
5508 tree zero_vec = build_vector_from_val (vectype, zero);
5509 tree cmp_type = truth_type_for (vectype);
5510 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5511 }
5512
5513 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5514 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5515 else
5516 {
5517 if (int_operands)
5518 {
5519 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5520 nested inside of the expression. */
5521 op1 = c_fully_fold (op1, false, NULL);
5522 op2 = c_fully_fold (op2, false, NULL);
5523 }
5524 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5525 if (int_operands)
5526 ret = note_integer_operands (ret);
5527 }
5528 if (semantic_result_type)
5529 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5530
5531 protected_set_expr_location (ret, colon_loc);
5532
5533 /* If the OP1 and OP2 are the same and don't have side-effects,
5534 warn here, because the COND_EXPR will be turned into OP1. */
5535 if (warn_duplicated_branches
5536 && TREE_CODE (ret) == COND_EXPR
5537 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5538 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5539 "this condition has identical branches");
5540
5541 return ret;
5542 }
5543
5544 /* Return a compound expression that performs two expressions and
5545 returns the value of the second of them.
5546
5547 LOC is the location of the COMPOUND_EXPR. */
5548
5549 tree
build_compound_expr(location_t loc,tree expr1,tree expr2)5550 build_compound_expr (location_t loc, tree expr1, tree expr2)
5551 {
5552 bool expr1_int_operands, expr2_int_operands;
5553 tree eptype = NULL_TREE;
5554 tree ret;
5555
5556 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5557 if (expr1_int_operands)
5558 expr1 = remove_c_maybe_const_expr (expr1);
5559 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5560 if (expr2_int_operands)
5561 expr2 = remove_c_maybe_const_expr (expr2);
5562
5563 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5564 expr1 = TREE_OPERAND (expr1, 0);
5565 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5566 {
5567 eptype = TREE_TYPE (expr2);
5568 expr2 = TREE_OPERAND (expr2, 0);
5569 }
5570
5571 if (!TREE_SIDE_EFFECTS (expr1))
5572 {
5573 /* The left-hand operand of a comma expression is like an expression
5574 statement: with -Wunused, we should warn if it doesn't have
5575 any side-effects, unless it was explicitly cast to (void). */
5576 if (warn_unused_value)
5577 {
5578 if (VOID_TYPE_P (TREE_TYPE (expr1))
5579 && CONVERT_EXPR_P (expr1))
5580 ; /* (void) a, b */
5581 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5582 && TREE_CODE (expr1) == COMPOUND_EXPR
5583 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5584 ; /* (void) a, (void) b, c */
5585 else
5586 warning_at (loc, OPT_Wunused_value,
5587 "left-hand operand of comma expression has no effect");
5588 }
5589 }
5590 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5591 && warn_unused_value)
5592 {
5593 tree r = expr1;
5594 location_t cloc = loc;
5595 while (TREE_CODE (r) == COMPOUND_EXPR)
5596 {
5597 if (EXPR_HAS_LOCATION (r))
5598 cloc = EXPR_LOCATION (r);
5599 r = TREE_OPERAND (r, 1);
5600 }
5601 if (!TREE_SIDE_EFFECTS (r)
5602 && !VOID_TYPE_P (TREE_TYPE (r))
5603 && !CONVERT_EXPR_P (r))
5604 warning_at (cloc, OPT_Wunused_value,
5605 "right-hand operand of comma expression has no effect");
5606 }
5607
5608 /* With -Wunused, we should also warn if the left-hand operand does have
5609 side-effects, but computes a value which is not used. For example, in
5610 `foo() + bar(), baz()' the result of the `+' operator is not used,
5611 so we should issue a warning. */
5612 else if (warn_unused_value)
5613 warn_if_unused_value (expr1, loc);
5614
5615 if (expr2 == error_mark_node)
5616 return error_mark_node;
5617
5618 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5619
5620 if (flag_isoc99
5621 && expr1_int_operands
5622 && expr2_int_operands)
5623 ret = note_integer_operands (ret);
5624
5625 if (eptype)
5626 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5627
5628 protected_set_expr_location (ret, loc);
5629 return ret;
5630 }
5631
5632 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5633 which we are casting. OTYPE is the type of the expression being
5634 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5635 of the cast. -Wcast-qual appeared on the command line. Named
5636 address space qualifiers are not handled here, because they result
5637 in different warnings. */
5638
5639 static void
handle_warn_cast_qual(location_t loc,tree type,tree otype)5640 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5641 {
5642 tree in_type = type;
5643 tree in_otype = otype;
5644 int added = 0;
5645 int discarded = 0;
5646 bool is_const;
5647
5648 /* Check that the qualifiers on IN_TYPE are a superset of the
5649 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5650 nodes is uninteresting and we stop as soon as we hit a
5651 non-POINTER_TYPE node on either type. */
5652 do
5653 {
5654 in_otype = TREE_TYPE (in_otype);
5655 in_type = TREE_TYPE (in_type);
5656
5657 /* GNU C allows cv-qualified function types. 'const' means the
5658 function is very pure, 'volatile' means it can't return. We
5659 need to warn when such qualifiers are added, not when they're
5660 taken away. */
5661 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5662 && TREE_CODE (in_type) == FUNCTION_TYPE)
5663 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5664 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5665 else
5666 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5667 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5668 }
5669 while (TREE_CODE (in_type) == POINTER_TYPE
5670 && TREE_CODE (in_otype) == POINTER_TYPE);
5671
5672 if (added)
5673 warning_at (loc, OPT_Wcast_qual,
5674 "cast adds %q#v qualifier to function type", added);
5675
5676 if (discarded)
5677 /* There are qualifiers present in IN_OTYPE that are not present
5678 in IN_TYPE. */
5679 warning_at (loc, OPT_Wcast_qual,
5680 "cast discards %qv qualifier from pointer target type",
5681 discarded);
5682
5683 if (added || discarded)
5684 return;
5685
5686 /* A cast from **T to const **T is unsafe, because it can cause a
5687 const value to be changed with no additional warning. We only
5688 issue this warning if T is the same on both sides, and we only
5689 issue the warning if there are the same number of pointers on
5690 both sides, as otherwise the cast is clearly unsafe anyhow. A
5691 cast is unsafe when a qualifier is added at one level and const
5692 is not present at all outer levels.
5693
5694 To issue this warning, we check at each level whether the cast
5695 adds new qualifiers not already seen. We don't need to special
5696 case function types, as they won't have the same
5697 TYPE_MAIN_VARIANT. */
5698
5699 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5700 return;
5701 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5702 return;
5703
5704 in_type = type;
5705 in_otype = otype;
5706 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5707 do
5708 {
5709 in_type = TREE_TYPE (in_type);
5710 in_otype = TREE_TYPE (in_otype);
5711 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5712 && !is_const)
5713 {
5714 warning_at (loc, OPT_Wcast_qual,
5715 "to be safe all intermediate pointers in cast from "
5716 "%qT to %qT must be %<const%> qualified",
5717 otype, type);
5718 break;
5719 }
5720 if (is_const)
5721 is_const = TYPE_READONLY (in_type);
5722 }
5723 while (TREE_CODE (in_type) == POINTER_TYPE);
5724 }
5725
5726 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5727
5728 static bool
c_safe_arg_type_equiv_p(tree t1,tree t2)5729 c_safe_arg_type_equiv_p (tree t1, tree t2)
5730 {
5731 t1 = TYPE_MAIN_VARIANT (t1);
5732 t2 = TYPE_MAIN_VARIANT (t2);
5733
5734 if (TREE_CODE (t1) == POINTER_TYPE
5735 && TREE_CODE (t2) == POINTER_TYPE)
5736 return true;
5737
5738 /* The signedness of the parameter matters only when an integral
5739 type smaller than int is promoted to int, otherwise only the
5740 precision of the parameter matters.
5741 This check should make sure that the callee does not see
5742 undefined values in argument registers. */
5743 if (INTEGRAL_TYPE_P (t1)
5744 && INTEGRAL_TYPE_P (t2)
5745 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5746 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5747 || !targetm.calls.promote_prototypes (NULL_TREE)
5748 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5749 return true;
5750
5751 return comptypes (t1, t2);
5752 }
5753
5754 /* Check if a type cast between two function types can be considered safe. */
5755
5756 static bool
c_safe_function_type_cast_p(tree t1,tree t2)5757 c_safe_function_type_cast_p (tree t1, tree t2)
5758 {
5759 if (TREE_TYPE (t1) == void_type_node &&
5760 TYPE_ARG_TYPES (t1) == void_list_node)
5761 return true;
5762
5763 if (TREE_TYPE (t2) == void_type_node &&
5764 TYPE_ARG_TYPES (t2) == void_list_node)
5765 return true;
5766
5767 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5768 return false;
5769
5770 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5771 t1 && t2;
5772 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5773 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5774 return false;
5775
5776 return true;
5777 }
5778
5779 /* Build an expression representing a cast to type TYPE of expression EXPR.
5780 LOC is the location of the cast-- typically the open paren of the cast. */
5781
5782 tree
build_c_cast(location_t loc,tree type,tree expr)5783 build_c_cast (location_t loc, tree type, tree expr)
5784 {
5785 tree value;
5786
5787 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5788
5789 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5790 expr = TREE_OPERAND (expr, 0);
5791
5792 value = expr;
5793 if (int_operands)
5794 value = remove_c_maybe_const_expr (value);
5795
5796 if (type == error_mark_node || expr == error_mark_node)
5797 return error_mark_node;
5798
5799 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5800 only in <protocol> qualifications. But when constructing cast expressions,
5801 the protocols do matter and must be kept around. */
5802 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5803 return build1 (NOP_EXPR, type, expr);
5804
5805 type = TYPE_MAIN_VARIANT (type);
5806
5807 if (TREE_CODE (type) == ARRAY_TYPE)
5808 {
5809 error_at (loc, "cast specifies array type");
5810 return error_mark_node;
5811 }
5812
5813 if (TREE_CODE (type) == FUNCTION_TYPE)
5814 {
5815 error_at (loc, "cast specifies function type");
5816 return error_mark_node;
5817 }
5818
5819 if (!VOID_TYPE_P (type))
5820 {
5821 value = require_complete_type (loc, value);
5822 if (value == error_mark_node)
5823 return error_mark_node;
5824 }
5825
5826 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5827 {
5828 if (RECORD_OR_UNION_TYPE_P (type))
5829 pedwarn (loc, OPT_Wpedantic,
5830 "ISO C forbids casting nonscalar to the same type");
5831
5832 /* Convert to remove any qualifiers from VALUE's type. */
5833 value = convert (type, value);
5834 }
5835 else if (TREE_CODE (type) == UNION_TYPE)
5836 {
5837 tree field;
5838
5839 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5840 if (TREE_TYPE (field) != error_mark_node
5841 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5842 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5843 break;
5844
5845 if (field)
5846 {
5847 tree t;
5848 bool maybe_const = true;
5849
5850 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5851 t = c_fully_fold (value, false, &maybe_const);
5852 t = build_constructor_single (type, field, t);
5853 if (!maybe_const)
5854 t = c_wrap_maybe_const (t, true);
5855 t = digest_init (loc, type, t,
5856 NULL_TREE, false, true, 0);
5857 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5858 return t;
5859 }
5860 error_at (loc, "cast to union type from type not present in union");
5861 return error_mark_node;
5862 }
5863 else
5864 {
5865 tree otype, ovalue;
5866
5867 if (type == void_type_node)
5868 {
5869 tree t = build1 (CONVERT_EXPR, type, value);
5870 SET_EXPR_LOCATION (t, loc);
5871 return t;
5872 }
5873
5874 otype = TREE_TYPE (value);
5875
5876 /* Optionally warn about potentially worrisome casts. */
5877 if (warn_cast_qual
5878 && TREE_CODE (type) == POINTER_TYPE
5879 && TREE_CODE (otype) == POINTER_TYPE)
5880 handle_warn_cast_qual (loc, type, otype);
5881
5882 /* Warn about conversions between pointers to disjoint
5883 address spaces. */
5884 if (TREE_CODE (type) == POINTER_TYPE
5885 && TREE_CODE (otype) == POINTER_TYPE
5886 && !null_pointer_constant_p (value))
5887 {
5888 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5889 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5890 addr_space_t as_common;
5891
5892 if (!addr_space_superset (as_to, as_from, &as_common))
5893 {
5894 if (ADDR_SPACE_GENERIC_P (as_from))
5895 warning_at (loc, 0, "cast to %s address space pointer "
5896 "from disjoint generic address space pointer",
5897 c_addr_space_name (as_to));
5898
5899 else if (ADDR_SPACE_GENERIC_P (as_to))
5900 warning_at (loc, 0, "cast to generic address space pointer "
5901 "from disjoint %s address space pointer",
5902 c_addr_space_name (as_from));
5903
5904 else
5905 warning_at (loc, 0, "cast to %s address space pointer "
5906 "from disjoint %s address space pointer",
5907 c_addr_space_name (as_to),
5908 c_addr_space_name (as_from));
5909 }
5910 }
5911
5912 /* Warn about possible alignment problems. */
5913 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5914 && TREE_CODE (type) == POINTER_TYPE
5915 && TREE_CODE (otype) == POINTER_TYPE
5916 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5917 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5918 /* Don't warn about opaque types, where the actual alignment
5919 restriction is unknown. */
5920 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5921 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5922 && min_align_of_type (TREE_TYPE (type))
5923 > min_align_of_type (TREE_TYPE (otype)))
5924 warning_at (loc, OPT_Wcast_align,
5925 "cast increases required alignment of target type");
5926
5927 if (TREE_CODE (type) == INTEGER_TYPE
5928 && TREE_CODE (otype) == POINTER_TYPE
5929 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5930 /* Unlike conversion of integers to pointers, where the
5931 warning is disabled for converting constants because
5932 of cases such as SIG_*, warn about converting constant
5933 pointers to integers. In some cases it may cause unwanted
5934 sign extension, and a warning is appropriate. */
5935 warning_at (loc, OPT_Wpointer_to_int_cast,
5936 "cast from pointer to integer of different size");
5937
5938 if (TREE_CODE (value) == CALL_EXPR
5939 && TREE_CODE (type) != TREE_CODE (otype))
5940 warning_at (loc, OPT_Wbad_function_cast,
5941 "cast from function call of type %qT "
5942 "to non-matching type %qT", otype, type);
5943
5944 if (TREE_CODE (type) == POINTER_TYPE
5945 && TREE_CODE (otype) == INTEGER_TYPE
5946 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5947 /* Don't warn about converting any constant. */
5948 && !TREE_CONSTANT (value))
5949 warning_at (loc,
5950 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5951 "of different size");
5952
5953 if (warn_strict_aliasing <= 2)
5954 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
5955
5956 /* If pedantic, warn for conversions between function and object
5957 pointer types, except for converting a null pointer constant
5958 to function pointer type. */
5959 if (pedantic
5960 && TREE_CODE (type) == POINTER_TYPE
5961 && TREE_CODE (otype) == POINTER_TYPE
5962 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5963 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5964 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5965 "conversion of function pointer to object pointer type");
5966
5967 if (pedantic
5968 && TREE_CODE (type) == POINTER_TYPE
5969 && TREE_CODE (otype) == POINTER_TYPE
5970 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5971 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5972 && !null_pointer_constant_p (value))
5973 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5974 "conversion of object pointer to function pointer type");
5975
5976 if (TREE_CODE (type) == POINTER_TYPE
5977 && TREE_CODE (otype) == POINTER_TYPE
5978 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5979 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5980 && !c_safe_function_type_cast_p (TREE_TYPE (type),
5981 TREE_TYPE (otype)))
5982 warning_at (loc, OPT_Wcast_function_type,
5983 "cast between incompatible function types"
5984 " from %qT to %qT", otype, type);
5985
5986 ovalue = value;
5987 value = convert (type, value);
5988
5989 /* Ignore any integer overflow caused by the cast. */
5990 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5991 {
5992 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5993 {
5994 if (!TREE_OVERFLOW (value))
5995 {
5996 /* Avoid clobbering a shared constant. */
5997 value = copy_node (value);
5998 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5999 }
6000 }
6001 else if (TREE_OVERFLOW (value))
6002 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6003 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6004 }
6005 }
6006
6007 /* Don't let a cast be an lvalue. */
6008 if (lvalue_p (value))
6009 value = non_lvalue_loc (loc, value);
6010
6011 /* Don't allow the results of casting to floating-point or complex
6012 types be confused with actual constants, or casts involving
6013 integer and pointer types other than direct integer-to-integer
6014 and integer-to-pointer be confused with integer constant
6015 expressions and null pointer constants. */
6016 if (TREE_CODE (value) == REAL_CST
6017 || TREE_CODE (value) == COMPLEX_CST
6018 || (TREE_CODE (value) == INTEGER_CST
6019 && !((TREE_CODE (expr) == INTEGER_CST
6020 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6021 || TREE_CODE (expr) == REAL_CST
6022 || TREE_CODE (expr) == COMPLEX_CST)))
6023 value = build1 (NOP_EXPR, type, value);
6024
6025 /* If the expression has integer operands and so can occur in an
6026 unevaluated part of an integer constant expression, ensure the
6027 return value reflects this. */
6028 if (int_operands
6029 && INTEGRAL_TYPE_P (type)
6030 && !EXPR_INT_CONST_OPERANDS (value))
6031 value = note_integer_operands (value);
6032
6033 protected_set_expr_location (value, loc);
6034 return value;
6035 }
6036
6037 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6038 location of the open paren of the cast, or the position of the cast
6039 expr. */
6040 tree
c_cast_expr(location_t loc,struct c_type_name * type_name,tree expr)6041 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6042 {
6043 tree type;
6044 tree type_expr = NULL_TREE;
6045 bool type_expr_const = true;
6046 tree ret;
6047 int saved_wsp = warn_strict_prototypes;
6048
6049 /* This avoids warnings about unprototyped casts on
6050 integers. E.g. "#define SIG_DFL (void(*)())0". */
6051 if (TREE_CODE (expr) == INTEGER_CST)
6052 warn_strict_prototypes = 0;
6053 type = groktypename (type_name, &type_expr, &type_expr_const);
6054 warn_strict_prototypes = saved_wsp;
6055
6056 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6057 && reject_gcc_builtin (expr))
6058 return error_mark_node;
6059
6060 ret = build_c_cast (loc, type, expr);
6061 if (type_expr)
6062 {
6063 bool inner_expr_const = true;
6064 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6065 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6066 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6067 && inner_expr_const);
6068 SET_EXPR_LOCATION (ret, loc);
6069 }
6070
6071 if (!EXPR_HAS_LOCATION (ret))
6072 protected_set_expr_location (ret, loc);
6073
6074 /* C++ does not permits types to be defined in a cast, but it
6075 allows references to incomplete types. */
6076 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6077 warning_at (loc, OPT_Wc___compat,
6078 "defining a type in a cast is invalid in C++");
6079
6080 return ret;
6081 }
6082
6083 /* Build an assignment expression of lvalue LHS from value RHS.
6084 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6085 may differ from TREE_TYPE (LHS) for an enum bitfield.
6086 MODIFYCODE is the code for a binary operator that we use
6087 to combine the old value of LHS with RHS to get the new value.
6088 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6089 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6090 which may differ from TREE_TYPE (RHS) for an enum value.
6091
6092 LOCATION is the location of the MODIFYCODE operator.
6093 RHS_LOC is the location of the RHS. */
6094
6095 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)6096 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6097 enum tree_code modifycode,
6098 location_t rhs_loc, tree rhs, tree rhs_origtype)
6099 {
6100 tree result;
6101 tree newrhs;
6102 tree rhseval = NULL_TREE;
6103 tree lhstype = TREE_TYPE (lhs);
6104 tree olhstype = lhstype;
6105 bool npc;
6106 bool is_atomic_op;
6107
6108 /* Types that aren't fully specified cannot be used in assignments. */
6109 lhs = require_complete_type (location, lhs);
6110
6111 /* Avoid duplicate error messages from operands that had errors. */
6112 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6113 return error_mark_node;
6114
6115 /* Ensure an error for assigning a non-lvalue array to an array in
6116 C90. */
6117 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6118 {
6119 error_at (location, "assignment to expression with array type");
6120 return error_mark_node;
6121 }
6122
6123 /* For ObjC properties, defer this check. */
6124 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6125 return error_mark_node;
6126
6127 is_atomic_op = really_atomic_lvalue (lhs);
6128
6129 newrhs = rhs;
6130
6131 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6132 {
6133 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6134 lhs_origtype, modifycode, rhs_loc, rhs,
6135 rhs_origtype);
6136 if (inner == error_mark_node)
6137 return error_mark_node;
6138 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6139 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6140 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6141 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6142 protected_set_expr_location (result, location);
6143 return result;
6144 }
6145
6146 /* If a binary op has been requested, combine the old LHS value with the RHS
6147 producing the value we should actually store into the LHS. */
6148
6149 if (modifycode != NOP_EXPR)
6150 {
6151 lhs = c_fully_fold (lhs, false, NULL, true);
6152 lhs = stabilize_reference (lhs);
6153
6154 /* Construct the RHS for any non-atomic compound assignemnt. */
6155 if (!is_atomic_op)
6156 {
6157 /* If in LHS op= RHS the RHS has side-effects, ensure they
6158 are preevaluated before the rest of the assignment expression's
6159 side-effects, because RHS could contain e.g. function calls
6160 that modify LHS. */
6161 if (TREE_SIDE_EFFECTS (rhs))
6162 {
6163 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6164 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6165 else
6166 newrhs = save_expr (rhs);
6167 rhseval = newrhs;
6168 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6169 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6170 newrhs);
6171 }
6172 newrhs = build_binary_op (location,
6173 modifycode, lhs, newrhs, true);
6174
6175 /* The original type of the right hand side is no longer
6176 meaningful. */
6177 rhs_origtype = NULL_TREE;
6178 }
6179 }
6180
6181 if (c_dialect_objc ())
6182 {
6183 /* Check if we are modifying an Objective-C property reference;
6184 if so, we need to generate setter calls. */
6185 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6186 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6187 else
6188 result = objc_maybe_build_modify_expr (lhs, newrhs);
6189 if (result)
6190 goto return_result;
6191
6192 /* Else, do the check that we postponed for Objective-C. */
6193 if (!lvalue_or_else (location, lhs, lv_assign))
6194 return error_mark_node;
6195 }
6196
6197 /* Give an error for storing in something that is 'const'. */
6198
6199 if (TYPE_READONLY (lhstype)
6200 || (RECORD_OR_UNION_TYPE_P (lhstype)
6201 && C_TYPE_FIELDS_READONLY (lhstype)))
6202 {
6203 readonly_error (location, lhs, lv_assign);
6204 return error_mark_node;
6205 }
6206 else if (TREE_READONLY (lhs))
6207 readonly_warning (lhs, lv_assign);
6208
6209 /* If storing into a structure or union member,
6210 it has probably been given type `int'.
6211 Compute the type that would go with
6212 the actual amount of storage the member occupies. */
6213
6214 if (TREE_CODE (lhs) == COMPONENT_REF
6215 && (TREE_CODE (lhstype) == INTEGER_TYPE
6216 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6217 || TREE_CODE (lhstype) == REAL_TYPE
6218 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6219 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6220
6221 /* If storing in a field that is in actuality a short or narrower than one,
6222 we must store in the field in its actual type. */
6223
6224 if (lhstype != TREE_TYPE (lhs))
6225 {
6226 lhs = copy_node (lhs);
6227 TREE_TYPE (lhs) = lhstype;
6228 }
6229
6230 /* Issue -Wc++-compat warnings about an assignment to an enum type
6231 when LHS does not have its original type. This happens for,
6232 e.g., an enum bitfield in a struct. */
6233 if (warn_cxx_compat
6234 && lhs_origtype != NULL_TREE
6235 && lhs_origtype != lhstype
6236 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6237 {
6238 tree checktype = (rhs_origtype != NULL_TREE
6239 ? rhs_origtype
6240 : TREE_TYPE (rhs));
6241 if (checktype != error_mark_node
6242 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6243 || (is_atomic_op && modifycode != NOP_EXPR)))
6244 warning_at (location, OPT_Wc___compat,
6245 "enum conversion in assignment is invalid in C++");
6246 }
6247
6248 /* If the lhs is atomic, remove that qualifier. */
6249 if (is_atomic_op)
6250 {
6251 lhstype = build_qualified_type (lhstype,
6252 (TYPE_QUALS (lhstype)
6253 & ~TYPE_QUAL_ATOMIC));
6254 olhstype = build_qualified_type (olhstype,
6255 (TYPE_QUALS (lhstype)
6256 & ~TYPE_QUAL_ATOMIC));
6257 }
6258
6259 /* Convert new value to destination type. Fold it first, then
6260 restore any excess precision information, for the sake of
6261 conversion warnings. */
6262
6263 if (!(is_atomic_op && modifycode != NOP_EXPR))
6264 {
6265 tree rhs_semantic_type = NULL_TREE;
6266 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6267 {
6268 rhs_semantic_type = TREE_TYPE (newrhs);
6269 newrhs = TREE_OPERAND (newrhs, 0);
6270 }
6271 npc = null_pointer_constant_p (newrhs);
6272 newrhs = c_fully_fold (newrhs, false, NULL);
6273 if (rhs_semantic_type)
6274 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6275 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6276 rhs_origtype, ic_assign, npc,
6277 NULL_TREE, NULL_TREE, 0);
6278 if (TREE_CODE (newrhs) == ERROR_MARK)
6279 return error_mark_node;
6280 }
6281
6282 /* Emit ObjC write barrier, if necessary. */
6283 if (c_dialect_objc () && flag_objc_gc)
6284 {
6285 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6286 if (result)
6287 {
6288 protected_set_expr_location (result, location);
6289 goto return_result;
6290 }
6291 }
6292
6293 /* Scan operands. */
6294
6295 if (is_atomic_op)
6296 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6297 else
6298 {
6299 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6300 TREE_SIDE_EFFECTS (result) = 1;
6301 protected_set_expr_location (result, location);
6302 }
6303
6304 /* If we got the LHS in a different type for storing in,
6305 convert the result back to the nominal type of LHS
6306 so that the value we return always has the same type
6307 as the LHS argument. */
6308
6309 if (olhstype == TREE_TYPE (result))
6310 goto return_result;
6311
6312 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6313 rhs_origtype, ic_assign, false, NULL_TREE,
6314 NULL_TREE, 0);
6315 protected_set_expr_location (result, location);
6316
6317 return_result:
6318 if (rhseval)
6319 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6320 return result;
6321 }
6322
6323 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6324 This is used to implement -fplan9-extensions. */
6325
6326 static bool
find_anonymous_field_with_type(tree struct_type,tree type)6327 find_anonymous_field_with_type (tree struct_type, tree type)
6328 {
6329 tree field;
6330 bool found;
6331
6332 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6333 found = false;
6334 for (field = TYPE_FIELDS (struct_type);
6335 field != NULL_TREE;
6336 field = TREE_CHAIN (field))
6337 {
6338 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6339 ? c_build_qualified_type (TREE_TYPE (field),
6340 TYPE_QUAL_ATOMIC)
6341 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6342 if (DECL_NAME (field) == NULL
6343 && comptypes (type, fieldtype))
6344 {
6345 if (found)
6346 return false;
6347 found = true;
6348 }
6349 else if (DECL_NAME (field) == NULL
6350 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6351 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6352 {
6353 if (found)
6354 return false;
6355 found = true;
6356 }
6357 }
6358 return found;
6359 }
6360
6361 /* RHS is an expression whose type is pointer to struct. If there is
6362 an anonymous field in RHS with type TYPE, then return a pointer to
6363 that field in RHS. This is used with -fplan9-extensions. This
6364 returns NULL if no conversion could be found. */
6365
6366 static tree
convert_to_anonymous_field(location_t location,tree type,tree rhs)6367 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6368 {
6369 tree rhs_struct_type, lhs_main_type;
6370 tree field, found_field;
6371 bool found_sub_field;
6372 tree ret;
6373
6374 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6375 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6376 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6377
6378 gcc_assert (POINTER_TYPE_P (type));
6379 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6380 ? c_build_qualified_type (TREE_TYPE (type),
6381 TYPE_QUAL_ATOMIC)
6382 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6383
6384 found_field = NULL_TREE;
6385 found_sub_field = false;
6386 for (field = TYPE_FIELDS (rhs_struct_type);
6387 field != NULL_TREE;
6388 field = TREE_CHAIN (field))
6389 {
6390 if (DECL_NAME (field) != NULL_TREE
6391 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6392 continue;
6393 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6394 ? c_build_qualified_type (TREE_TYPE (field),
6395 TYPE_QUAL_ATOMIC)
6396 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6397 if (comptypes (lhs_main_type, fieldtype))
6398 {
6399 if (found_field != NULL_TREE)
6400 return NULL_TREE;
6401 found_field = field;
6402 }
6403 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6404 lhs_main_type))
6405 {
6406 if (found_field != NULL_TREE)
6407 return NULL_TREE;
6408 found_field = field;
6409 found_sub_field = true;
6410 }
6411 }
6412
6413 if (found_field == NULL_TREE)
6414 return NULL_TREE;
6415
6416 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6417 build_fold_indirect_ref (rhs), found_field,
6418 NULL_TREE);
6419 ret = build_fold_addr_expr_loc (location, ret);
6420
6421 if (found_sub_field)
6422 {
6423 ret = convert_to_anonymous_field (location, type, ret);
6424 gcc_assert (ret != NULL_TREE);
6425 }
6426
6427 return ret;
6428 }
6429
6430 /* Issue an error message for a bad initializer component.
6431 GMSGID identifies the message.
6432 The component name is taken from the spelling stack. */
6433
6434 static void ATTRIBUTE_GCC_DIAG (2,0)
error_init(location_t loc,const char * gmsgid,...)6435 error_init (location_t loc, const char *gmsgid, ...)
6436 {
6437 char *ofwhat;
6438
6439 auto_diagnostic_group d;
6440
6441 /* The gmsgid may be a format string with %< and %>. */
6442 va_list ap;
6443 va_start (ap, gmsgid);
6444 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6445 va_end (ap);
6446
6447 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6448 if (*ofwhat && warned)
6449 inform (loc, "(near initialization for %qs)", ofwhat);
6450 }
6451
6452 /* Issue a pedantic warning for a bad initializer component. OPT is
6453 the option OPT_* (from options.h) controlling this warning or 0 if
6454 it is unconditionally given. GMSGID identifies the message. The
6455 component name is taken from the spelling stack. */
6456
6457 static void ATTRIBUTE_GCC_DIAG (3,0)
pedwarn_init(location_t loc,int opt,const char * gmsgid,...)6458 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6459 {
6460 /* Use the location where a macro was expanded rather than where
6461 it was defined to make sure macros defined in system headers
6462 but used incorrectly elsewhere are diagnosed. */
6463 location_t exploc = expansion_point_location_if_in_system_header (loc);
6464 auto_diagnostic_group d;
6465 va_list ap;
6466 va_start (ap, gmsgid);
6467 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6468 va_end (ap);
6469 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6470 if (*ofwhat && warned)
6471 inform (exploc, "(near initialization for %qs)", ofwhat);
6472 }
6473
6474 /* Issue a warning for a bad initializer component.
6475
6476 OPT is the OPT_W* value corresponding to the warning option that
6477 controls this warning. GMSGID identifies the message. The
6478 component name is taken from the spelling stack. */
6479
6480 static void
warning_init(location_t loc,int opt,const char * gmsgid)6481 warning_init (location_t loc, int opt, const char *gmsgid)
6482 {
6483 char *ofwhat;
6484 bool warned;
6485
6486 auto_diagnostic_group d;
6487
6488 /* Use the location where a macro was expanded rather than where
6489 it was defined to make sure macros defined in system headers
6490 but used incorrectly elsewhere are diagnosed. */
6491 location_t exploc = expansion_point_location_if_in_system_header (loc);
6492
6493 /* The gmsgid may be a format string with %< and %>. */
6494 warned = warning_at (exploc, opt, gmsgid);
6495 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6496 if (*ofwhat && warned)
6497 inform (exploc, "(near initialization for %qs)", ofwhat);
6498 }
6499
6500 /* If TYPE is an array type and EXPR is a parenthesized string
6501 constant, warn if pedantic that EXPR is being used to initialize an
6502 object of type TYPE. */
6503
6504 void
maybe_warn_string_init(location_t loc,tree type,struct c_expr expr)6505 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6506 {
6507 if (pedantic
6508 && TREE_CODE (type) == ARRAY_TYPE
6509 && TREE_CODE (expr.value) == STRING_CST
6510 && expr.original_code != STRING_CST)
6511 pedwarn_init (loc, OPT_Wpedantic,
6512 "array initialized from parenthesized string constant");
6513 }
6514
6515 /* Attempt to locate the parameter with the given index within FNDECL,
6516 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6517
6518 static location_t
get_fndecl_argument_location(tree fndecl,int argnum)6519 get_fndecl_argument_location (tree fndecl, int argnum)
6520 {
6521 int i;
6522 tree param;
6523
6524 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6525 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6526 i < argnum && param;
6527 i++, param = TREE_CHAIN (param))
6528 ;
6529
6530 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6531 return DECL_SOURCE_LOCATION (FNDECL). */
6532 if (param == NULL)
6533 return DECL_SOURCE_LOCATION (fndecl);
6534
6535 return DECL_SOURCE_LOCATION (param);
6536 }
6537
6538 /* Issue a note about a mismatching argument for parameter PARMNUM
6539 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6540 Attempt to issue the note at the pertinent parameter of the decl;
6541 failing that issue it at the location of FUNDECL; failing that
6542 issue it at PLOC. */
6543
6544 static void
inform_for_arg(tree fundecl,location_t ploc,int parmnum,tree expected_type,tree actual_type)6545 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6546 tree expected_type, tree actual_type)
6547 {
6548 location_t loc;
6549 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6550 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6551 else
6552 loc = ploc;
6553
6554 inform (loc,
6555 "expected %qT but argument is of type %qT",
6556 expected_type, actual_type);
6557 }
6558
6559 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6560 function FUNDECL declared without prototype to parameter PARMNUM of
6561 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6562
6563 static void
maybe_warn_builtin_no_proto_arg(location_t loc,tree fundecl,int parmnum,tree parmtype,tree argtype)6564 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6565 tree parmtype, tree argtype)
6566 {
6567 tree_code parmcode = TREE_CODE (parmtype);
6568 tree_code argcode = TREE_CODE (argtype);
6569 tree promoted = c_type_promotes_to (argtype);
6570
6571 /* Avoid warning for enum arguments that promote to an integer type
6572 of the same size/mode. */
6573 if (parmcode == INTEGER_TYPE
6574 && argcode == ENUMERAL_TYPE
6575 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6576 return;
6577
6578 if ((parmcode == argcode
6579 || (parmcode == INTEGER_TYPE
6580 && argcode == ENUMERAL_TYPE))
6581 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6582 return;
6583
6584 /* This diagnoses even signed/unsigned mismatches. Those might be
6585 safe in many cases but GCC may emit suboptimal code for them so
6586 warning on those cases drives efficiency improvements. */
6587 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6588 TYPE_MAIN_VARIANT (promoted) == argtype
6589 ? G_("%qD argument %d type is %qT where %qT is expected "
6590 "in a call to built-in function declared without "
6591 "prototype")
6592 : G_("%qD argument %d promotes to %qT where %qT is expected "
6593 "in a call to built-in function declared without "
6594 "prototype"),
6595 fundecl, parmnum, promoted, parmtype))
6596 inform (DECL_SOURCE_LOCATION (fundecl),
6597 "built-in %qD declared here",
6598 fundecl);
6599 }
6600
6601 /* Convert value RHS to type TYPE as preparation for an assignment to
6602 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6603 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6604 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6605 constant before any folding.
6606 The real work of conversion is done by `convert'.
6607 The purpose of this function is to generate error messages
6608 for assignments that are not allowed in C.
6609 ERRTYPE says whether it is argument passing, assignment,
6610 initialization or return.
6611
6612 In the following example, '~' denotes where EXPR_LOC and '^' where
6613 LOCATION point to:
6614
6615 f (var); [ic_argpass]
6616 ^ ~~~
6617 x = var; [ic_assign]
6618 ^ ~~~;
6619 int x = var; [ic_init]
6620 ^^^
6621 return x; [ic_return]
6622 ^
6623
6624 FUNCTION is a tree for the function being called.
6625 PARMNUM is the number of the argument, for printing in error messages.
6626 WARNOPT may be set to a warning option to issue the corresponding warning
6627 rather than an error for invalid conversions. Used for calls to built-in
6628 functions declared without a prototype. */
6629
6630 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)6631 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6632 tree rhs, tree origtype, enum impl_conv errtype,
6633 bool null_pointer_constant, tree fundecl,
6634 tree function, int parmnum, int warnopt /* = 0 */)
6635 {
6636 enum tree_code codel = TREE_CODE (type);
6637 tree orig_rhs = rhs;
6638 tree rhstype;
6639 enum tree_code coder;
6640 tree rname = NULL_TREE;
6641 bool objc_ok = false;
6642
6643 /* Use the expansion point location to handle cases such as user's
6644 function returning a wrong-type macro defined in a system header. */
6645 location = expansion_point_location_if_in_system_header (location);
6646
6647 if (errtype == ic_argpass)
6648 {
6649 tree selector;
6650 /* Change pointer to function to the function itself for
6651 diagnostics. */
6652 if (TREE_CODE (function) == ADDR_EXPR
6653 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6654 function = TREE_OPERAND (function, 0);
6655
6656 /* Handle an ObjC selector specially for diagnostics. */
6657 selector = objc_message_selector ();
6658 rname = function;
6659 if (selector && parmnum > 2)
6660 {
6661 rname = selector;
6662 parmnum -= 2;
6663 }
6664 }
6665
6666 /* This macro is used to emit diagnostics to ensure that all format
6667 strings are complete sentences, visible to gettext and checked at
6668 compile time. */
6669 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6670 do { \
6671 switch (errtype) \
6672 { \
6673 case ic_argpass: \
6674 { \
6675 auto_diagnostic_group d; \
6676 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6677 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6678 } \
6679 break; \
6680 case ic_assign: \
6681 pedwarn (LOCATION, OPT, AS); \
6682 break; \
6683 case ic_init: \
6684 pedwarn_init (LOCATION, OPT, IN); \
6685 break; \
6686 case ic_return: \
6687 pedwarn (LOCATION, OPT, RE); \
6688 break; \
6689 default: \
6690 gcc_unreachable (); \
6691 } \
6692 } while (0)
6693
6694 /* This macro is used to emit diagnostics to ensure that all format
6695 strings are complete sentences, visible to gettext and checked at
6696 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6697 extra parameter to enumerate qualifiers. */
6698 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6699 do { \
6700 switch (errtype) \
6701 { \
6702 case ic_argpass: \
6703 { \
6704 auto_diagnostic_group d; \
6705 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6706 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6707 } \
6708 break; \
6709 case ic_assign: \
6710 pedwarn (LOCATION, OPT, AS, QUALS); \
6711 break; \
6712 case ic_init: \
6713 pedwarn (LOCATION, OPT, IN, QUALS); \
6714 break; \
6715 case ic_return: \
6716 pedwarn (LOCATION, OPT, RE, QUALS); \
6717 break; \
6718 default: \
6719 gcc_unreachable (); \
6720 } \
6721 } while (0)
6722
6723 /* This macro is used to emit diagnostics to ensure that all format
6724 strings are complete sentences, visible to gettext and checked at
6725 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6726 warning_at instead of pedwarn. */
6727 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6728 do { \
6729 switch (errtype) \
6730 { \
6731 case ic_argpass: \
6732 { \
6733 auto_diagnostic_group d; \
6734 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6735 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6736 } \
6737 break; \
6738 case ic_assign: \
6739 warning_at (LOCATION, OPT, AS, QUALS); \
6740 break; \
6741 case ic_init: \
6742 warning_at (LOCATION, OPT, IN, QUALS); \
6743 break; \
6744 case ic_return: \
6745 warning_at (LOCATION, OPT, RE, QUALS); \
6746 break; \
6747 default: \
6748 gcc_unreachable (); \
6749 } \
6750 } while (0)
6751
6752 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6753 rhs = TREE_OPERAND (rhs, 0);
6754
6755 rhstype = TREE_TYPE (rhs);
6756 coder = TREE_CODE (rhstype);
6757
6758 if (coder == ERROR_MARK)
6759 return error_mark_node;
6760
6761 if (c_dialect_objc ())
6762 {
6763 int parmno;
6764
6765 switch (errtype)
6766 {
6767 case ic_return:
6768 parmno = 0;
6769 break;
6770
6771 case ic_assign:
6772 parmno = -1;
6773 break;
6774
6775 case ic_init:
6776 parmno = -2;
6777 break;
6778
6779 default:
6780 parmno = parmnum;
6781 break;
6782 }
6783
6784 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6785 }
6786
6787 if (warn_cxx_compat)
6788 {
6789 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6790 if (checktype != error_mark_node
6791 && TREE_CODE (type) == ENUMERAL_TYPE
6792 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6793 switch (errtype)
6794 {
6795 case ic_argpass:
6796 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6797 "passing argument %d of %qE is invalid in C++",
6798 parmnum, rname))
6799 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6800 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6801 "expected %qT but argument is of type %qT",
6802 type, rhstype);
6803 break;
6804 case ic_assign:
6805 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6806 "%qT in assignment is invalid in C++", rhstype, type);
6807 break;
6808 case ic_init:
6809 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6810 "%qT to %qT in initialization is invalid in C++",
6811 rhstype, type);
6812 break;
6813 case ic_return:
6814 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6815 "%qT in return is invalid in C++", rhstype, type);
6816 break;
6817 default:
6818 gcc_unreachable ();
6819 }
6820 }
6821
6822 if (warn_enum_conversion)
6823 {
6824 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6825 if (checktype != error_mark_node
6826 && TREE_CODE (checktype) == ENUMERAL_TYPE
6827 && TREE_CODE (type) == ENUMERAL_TYPE
6828 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6829 {
6830 gcc_rich_location loc (location);
6831 warning_at (&loc, OPT_Wenum_conversion,
6832 "implicit conversion from %qT to %qT",
6833 checktype, type);
6834 }
6835 }
6836
6837 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6838 {
6839 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6840 return rhs;
6841 }
6842
6843 if (coder == VOID_TYPE)
6844 {
6845 /* Except for passing an argument to an unprototyped function,
6846 this is a constraint violation. When passing an argument to
6847 an unprototyped function, it is compile-time undefined;
6848 making it a constraint in that case was rejected in
6849 DR#252. */
6850 const char msg[] = "void value not ignored as it ought to be";
6851 if (warnopt)
6852 warning_at (location, warnopt, msg);
6853 else
6854 error_at (location, msg);
6855 return error_mark_node;
6856 }
6857 rhs = require_complete_type (location, rhs);
6858 if (rhs == error_mark_node)
6859 return error_mark_node;
6860
6861 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6862 return error_mark_node;
6863
6864 /* A non-reference type can convert to a reference. This handles
6865 va_start, va_copy and possibly port built-ins. */
6866 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6867 {
6868 if (!lvalue_p (rhs))
6869 {
6870 const char msg[] = "cannot pass rvalue to reference parameter";
6871 if (warnopt)
6872 warning_at (location, warnopt, msg);
6873 else
6874 error_at (location, msg);
6875 return error_mark_node;
6876 }
6877 if (!c_mark_addressable (rhs))
6878 return error_mark_node;
6879 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6880 SET_EXPR_LOCATION (rhs, location);
6881
6882 rhs = convert_for_assignment (location, expr_loc,
6883 build_pointer_type (TREE_TYPE (type)),
6884 rhs, origtype, errtype,
6885 null_pointer_constant, fundecl, function,
6886 parmnum, warnopt);
6887 if (rhs == error_mark_node)
6888 return error_mark_node;
6889
6890 rhs = build1 (NOP_EXPR, type, rhs);
6891 SET_EXPR_LOCATION (rhs, location);
6892 return rhs;
6893 }
6894 /* Some types can interconvert without explicit casts. */
6895 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6896 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6897 return convert (type, rhs);
6898 /* Arithmetic types all interconvert, and enum is treated like int. */
6899 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6900 || codel == FIXED_POINT_TYPE
6901 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6902 || codel == BOOLEAN_TYPE)
6903 && (coder == INTEGER_TYPE || coder == REAL_TYPE
6904 || coder == FIXED_POINT_TYPE
6905 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6906 || coder == BOOLEAN_TYPE))
6907 {
6908 if (warnopt && errtype == ic_argpass)
6909 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
6910 rhstype);
6911
6912 bool save = in_late_binary_op;
6913 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6914 || (coder == REAL_TYPE
6915 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6916 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6917 in_late_binary_op = true;
6918 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6919 ? expr_loc : location, type, orig_rhs);
6920 in_late_binary_op = save;
6921 return ret;
6922 }
6923
6924 /* Aggregates in different TUs might need conversion. */
6925 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6926 && codel == coder
6927 && comptypes (type, rhstype))
6928 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6929 ? expr_loc : location, type, rhs);
6930
6931 /* Conversion to a transparent union or record from its member types.
6932 This applies only to function arguments. */
6933 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6934 && TYPE_TRANSPARENT_AGGR (type))
6935 && errtype == ic_argpass)
6936 {
6937 tree memb, marginal_memb = NULL_TREE;
6938
6939 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6940 {
6941 tree memb_type = TREE_TYPE (memb);
6942
6943 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6944 TYPE_MAIN_VARIANT (rhstype)))
6945 break;
6946
6947 if (TREE_CODE (memb_type) != POINTER_TYPE)
6948 continue;
6949
6950 if (coder == POINTER_TYPE)
6951 {
6952 tree ttl = TREE_TYPE (memb_type);
6953 tree ttr = TREE_TYPE (rhstype);
6954
6955 /* Any non-function converts to a [const][volatile] void *
6956 and vice versa; otherwise, targets must be the same.
6957 Meanwhile, the lhs target must have all the qualifiers of
6958 the rhs. */
6959 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6960 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6961 || comp_target_types (location, memb_type, rhstype))
6962 {
6963 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6964 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6965 /* If this type won't generate any warnings, use it. */
6966 if (lquals == rquals
6967 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6968 && TREE_CODE (ttl) == FUNCTION_TYPE)
6969 ? ((lquals | rquals) == rquals)
6970 : ((lquals | rquals) == lquals)))
6971 break;
6972
6973 /* Keep looking for a better type, but remember this one. */
6974 if (!marginal_memb)
6975 marginal_memb = memb;
6976 }
6977 }
6978
6979 /* Can convert integer zero to any pointer type. */
6980 if (null_pointer_constant)
6981 {
6982 rhs = null_pointer_node;
6983 break;
6984 }
6985 }
6986
6987 if (memb || marginal_memb)
6988 {
6989 if (!memb)
6990 {
6991 /* We have only a marginally acceptable member type;
6992 it needs a warning. */
6993 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6994 tree ttr = TREE_TYPE (rhstype);
6995
6996 /* Const and volatile mean something different for function
6997 types, so the usual warnings are not appropriate. */
6998 if (TREE_CODE (ttr) == FUNCTION_TYPE
6999 && TREE_CODE (ttl) == FUNCTION_TYPE)
7000 {
7001 /* Because const and volatile on functions are
7002 restrictions that say the function will not do
7003 certain things, it is okay to use a const or volatile
7004 function where an ordinary one is wanted, but not
7005 vice-versa. */
7006 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7007 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7008 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7009 OPT_Wdiscarded_qualifiers,
7010 G_("passing argument %d of %qE "
7011 "makes %q#v qualified function "
7012 "pointer from unqualified"),
7013 G_("assignment makes %q#v qualified "
7014 "function pointer from "
7015 "unqualified"),
7016 G_("initialization makes %q#v qualified "
7017 "function pointer from "
7018 "unqualified"),
7019 G_("return makes %q#v qualified function "
7020 "pointer from unqualified"),
7021 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7022 }
7023 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7024 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7025 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7026 OPT_Wdiscarded_qualifiers,
7027 G_("passing argument %d of %qE discards "
7028 "%qv qualifier from pointer target type"),
7029 G_("assignment discards %qv qualifier "
7030 "from pointer target type"),
7031 G_("initialization discards %qv qualifier "
7032 "from pointer target type"),
7033 G_("return discards %qv qualifier from "
7034 "pointer target type"),
7035 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7036
7037 memb = marginal_memb;
7038 }
7039
7040 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7041 pedwarn (location, OPT_Wpedantic,
7042 "ISO C prohibits argument conversion to union type");
7043
7044 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7045 return build_constructor_single (type, memb, rhs);
7046 }
7047 }
7048
7049 /* Conversions among pointers */
7050 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7051 && (coder == codel))
7052 {
7053 /* If RHS refers to a built-in declared without a prototype
7054 BLTIN is the declaration of the built-in with a prototype
7055 and RHSTYPE is set to the actual type of the built-in. */
7056 tree bltin;
7057 rhstype = type_or_builtin_type (rhs, &bltin);
7058
7059 tree ttl = TREE_TYPE (type);
7060 tree ttr = TREE_TYPE (rhstype);
7061 tree mvl = ttl;
7062 tree mvr = ttr;
7063 bool is_opaque_pointer;
7064 int target_cmp = 0; /* Cache comp_target_types () result. */
7065 addr_space_t asl;
7066 addr_space_t asr;
7067
7068 if (TREE_CODE (mvl) != ARRAY_TYPE)
7069 mvl = (TYPE_ATOMIC (mvl)
7070 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7071 TYPE_QUAL_ATOMIC)
7072 : TYPE_MAIN_VARIANT (mvl));
7073 if (TREE_CODE (mvr) != ARRAY_TYPE)
7074 mvr = (TYPE_ATOMIC (mvr)
7075 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7076 TYPE_QUAL_ATOMIC)
7077 : TYPE_MAIN_VARIANT (mvr));
7078 /* Opaque pointers are treated like void pointers. */
7079 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7080
7081 /* The Plan 9 compiler permits a pointer to a struct to be
7082 automatically converted into a pointer to an anonymous field
7083 within the struct. */
7084 if (flag_plan9_extensions
7085 && RECORD_OR_UNION_TYPE_P (mvl)
7086 && RECORD_OR_UNION_TYPE_P (mvr)
7087 && mvl != mvr)
7088 {
7089 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7090 if (new_rhs != NULL_TREE)
7091 {
7092 rhs = new_rhs;
7093 rhstype = TREE_TYPE (rhs);
7094 coder = TREE_CODE (rhstype);
7095 ttr = TREE_TYPE (rhstype);
7096 mvr = TYPE_MAIN_VARIANT (ttr);
7097 }
7098 }
7099
7100 /* C++ does not allow the implicit conversion void* -> T*. However,
7101 for the purpose of reducing the number of false positives, we
7102 tolerate the special case of
7103
7104 int *p = NULL;
7105
7106 where NULL is typically defined in C to be '(void *) 0'. */
7107 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7108 warning_at (errtype == ic_argpass ? expr_loc : location,
7109 OPT_Wc___compat,
7110 "request for implicit conversion "
7111 "from %qT to %qT not permitted in C++", rhstype, type);
7112
7113 /* See if the pointers point to incompatible address spaces. */
7114 asl = TYPE_ADDR_SPACE (ttl);
7115 asr = TYPE_ADDR_SPACE (ttr);
7116 if (!null_pointer_constant_p (rhs)
7117 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7118 {
7119 switch (errtype)
7120 {
7121 case ic_argpass:
7122 {
7123 const char msg[] = G_("passing argument %d of %qE from "
7124 "pointer to non-enclosed address space");
7125 if (warnopt)
7126 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7127 else
7128 error_at (expr_loc, msg, parmnum, rname);
7129 break;
7130 }
7131 case ic_assign:
7132 {
7133 const char msg[] = G_("assignment from pointer to "
7134 "non-enclosed address space");
7135 if (warnopt)
7136 warning_at (location, warnopt, msg);
7137 else
7138 error_at (location, msg);
7139 break;
7140 }
7141 case ic_init:
7142 {
7143 const char msg[] = G_("initialization from pointer to "
7144 "non-enclosed address space");
7145 if (warnopt)
7146 warning_at (location, warnopt, msg);
7147 else
7148 error_at (location, msg);
7149 break;
7150 }
7151 case ic_return:
7152 {
7153 const char msg[] = G_("return from pointer to "
7154 "non-enclosed address space");
7155 if (warnopt)
7156 warning_at (location, warnopt, msg);
7157 else
7158 error_at (location, msg);
7159 break;
7160 }
7161 default:
7162 gcc_unreachable ();
7163 }
7164 return error_mark_node;
7165 }
7166
7167 /* Check if the right-hand side has a format attribute but the
7168 left-hand side doesn't. */
7169 if (warn_suggest_attribute_format
7170 && check_missing_format_attribute (type, rhstype))
7171 {
7172 switch (errtype)
7173 {
7174 case ic_argpass:
7175 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7176 "argument %d of %qE might be "
7177 "a candidate for a format attribute",
7178 parmnum, rname);
7179 break;
7180 case ic_assign:
7181 warning_at (location, OPT_Wsuggest_attribute_format,
7182 "assignment left-hand side might be "
7183 "a candidate for a format attribute");
7184 break;
7185 case ic_init:
7186 warning_at (location, OPT_Wsuggest_attribute_format,
7187 "initialization left-hand side might be "
7188 "a candidate for a format attribute");
7189 break;
7190 case ic_return:
7191 warning_at (location, OPT_Wsuggest_attribute_format,
7192 "return type might be "
7193 "a candidate for a format attribute");
7194 break;
7195 default:
7196 gcc_unreachable ();
7197 }
7198 }
7199
7200 /* Any non-function converts to a [const][volatile] void *
7201 and vice versa; otherwise, targets must be the same.
7202 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7203 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7204 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7205 || (target_cmp = comp_target_types (location, type, rhstype))
7206 || is_opaque_pointer
7207 || ((c_common_unsigned_type (mvl)
7208 == c_common_unsigned_type (mvr))
7209 && (c_common_signed_type (mvl)
7210 == c_common_signed_type (mvr))
7211 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7212 {
7213 /* Warn about loss of qualifers from pointers to arrays with
7214 qualifiers on the element type. */
7215 if (TREE_CODE (ttr) == ARRAY_TYPE)
7216 {
7217 ttr = strip_array_types (ttr);
7218 ttl = strip_array_types (ttl);
7219
7220 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7221 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7222 WARNING_FOR_QUALIFIERS (location, expr_loc,
7223 OPT_Wdiscarded_array_qualifiers,
7224 G_("passing argument %d of %qE discards "
7225 "%qv qualifier from pointer target type"),
7226 G_("assignment discards %qv qualifier "
7227 "from pointer target type"),
7228 G_("initialization discards %qv qualifier "
7229 "from pointer target type"),
7230 G_("return discards %qv qualifier from "
7231 "pointer target type"),
7232 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7233 }
7234 else if (pedantic
7235 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7236 ||
7237 (VOID_TYPE_P (ttr)
7238 && !null_pointer_constant
7239 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7240 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7241 G_("ISO C forbids passing argument %d of "
7242 "%qE between function pointer "
7243 "and %<void *%>"),
7244 G_("ISO C forbids assignment between "
7245 "function pointer and %<void *%>"),
7246 G_("ISO C forbids initialization between "
7247 "function pointer and %<void *%>"),
7248 G_("ISO C forbids return between function "
7249 "pointer and %<void *%>"));
7250 /* Const and volatile mean something different for function types,
7251 so the usual warnings are not appropriate. */
7252 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7253 && TREE_CODE (ttl) != FUNCTION_TYPE)
7254 {
7255 /* Don't warn about loss of qualifier for conversions from
7256 qualified void* to pointers to arrays with corresponding
7257 qualifier on the element type. */
7258 if (!pedantic)
7259 ttl = strip_array_types (ttl);
7260
7261 /* Assignments between atomic and non-atomic objects are OK. */
7262 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7263 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7264 {
7265 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7266 OPT_Wdiscarded_qualifiers,
7267 G_("passing argument %d of %qE discards "
7268 "%qv qualifier from pointer target type"),
7269 G_("assignment discards %qv qualifier "
7270 "from pointer target type"),
7271 G_("initialization discards %qv qualifier "
7272 "from pointer target type"),
7273 G_("return discards %qv qualifier from "
7274 "pointer target type"),
7275 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7276 }
7277 /* If this is not a case of ignoring a mismatch in signedness,
7278 no warning. */
7279 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7280 || target_cmp)
7281 ;
7282 /* If there is a mismatch, do warn. */
7283 else if (warn_pointer_sign)
7284 switch (errtype)
7285 {
7286 case ic_argpass:
7287 {
7288 auto_diagnostic_group d;
7289 range_label_for_type_mismatch rhs_label (rhstype, type);
7290 gcc_rich_location richloc (expr_loc, &rhs_label);
7291 if (pedwarn (&richloc, OPT_Wpointer_sign,
7292 "pointer targets in passing argument %d of "
7293 "%qE differ in signedness", parmnum, rname))
7294 inform_for_arg (fundecl, expr_loc, parmnum, type,
7295 rhstype);
7296 }
7297 break;
7298 case ic_assign:
7299 pedwarn (location, OPT_Wpointer_sign,
7300 "pointer targets in assignment from %qT to %qT "
7301 "differ in signedness", rhstype, type);
7302 break;
7303 case ic_init:
7304 pedwarn_init (location, OPT_Wpointer_sign,
7305 "pointer targets in initialization of %qT "
7306 "from %qT differ in signedness", type,
7307 rhstype);
7308 break;
7309 case ic_return:
7310 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7311 "returning %qT from a function with return type "
7312 "%qT differ in signedness", rhstype, type);
7313 break;
7314 default:
7315 gcc_unreachable ();
7316 }
7317 }
7318 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7319 && TREE_CODE (ttr) == FUNCTION_TYPE)
7320 {
7321 /* Because const and volatile on functions are restrictions
7322 that say the function will not do certain things,
7323 it is okay to use a const or volatile function
7324 where an ordinary one is wanted, but not vice-versa. */
7325 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7326 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7327 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7328 OPT_Wdiscarded_qualifiers,
7329 G_("passing argument %d of %qE makes "
7330 "%q#v qualified function pointer "
7331 "from unqualified"),
7332 G_("assignment makes %q#v qualified function "
7333 "pointer from unqualified"),
7334 G_("initialization makes %q#v qualified "
7335 "function pointer from unqualified"),
7336 G_("return makes %q#v qualified function "
7337 "pointer from unqualified"),
7338 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7339 }
7340 }
7341 /* Avoid warning about the volatile ObjC EH puts on decls. */
7342 else if (!objc_ok)
7343 {
7344 switch (errtype)
7345 {
7346 case ic_argpass:
7347 {
7348 auto_diagnostic_group d;
7349 range_label_for_type_mismatch rhs_label (rhstype, type);
7350 gcc_rich_location richloc (expr_loc, &rhs_label);
7351 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7352 "passing argument %d of %qE from incompatible "
7353 "pointer type", parmnum, rname))
7354 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7355 }
7356 break;
7357 case ic_assign:
7358 if (bltin)
7359 pedwarn (location, OPT_Wincompatible_pointer_types,
7360 "assignment to %qT from pointer to "
7361 "%qD with incompatible type %qT",
7362 type, bltin, rhstype);
7363 else
7364 pedwarn (location, OPT_Wincompatible_pointer_types,
7365 "assignment to %qT from incompatible pointer type %qT",
7366 type, rhstype);
7367 break;
7368 case ic_init:
7369 if (bltin)
7370 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7371 "initialization of %qT from pointer to "
7372 "%qD with incompatible type %qT",
7373 type, bltin, rhstype);
7374 else
7375 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7376 "initialization of %qT from incompatible "
7377 "pointer type %qT",
7378 type, rhstype);
7379 break;
7380 case ic_return:
7381 if (bltin)
7382 pedwarn (location, OPT_Wincompatible_pointer_types,
7383 "returning pointer to %qD of type %qT from "
7384 "a function with incompatible type %qT",
7385 bltin, rhstype, type);
7386 else
7387 pedwarn (location, OPT_Wincompatible_pointer_types,
7388 "returning %qT from a function with incompatible "
7389 "return type %qT", rhstype, type);
7390 break;
7391 default:
7392 gcc_unreachable ();
7393 }
7394 }
7395
7396 /* If RHS isn't an address, check pointer or array of packed
7397 struct or union. */
7398 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7399
7400 return convert (type, rhs);
7401 }
7402 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7403 {
7404 /* ??? This should not be an error when inlining calls to
7405 unprototyped functions. */
7406 const char msg[] = "invalid use of non-lvalue array";
7407 if (warnopt)
7408 warning_at (location, warnopt, msg);
7409 else
7410 error_at (location, msg);
7411 return error_mark_node;
7412 }
7413 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7414 {
7415 /* An explicit constant 0 can convert to a pointer,
7416 or one that results from arithmetic, even including
7417 a cast to integer type. */
7418 if (!null_pointer_constant)
7419 switch (errtype)
7420 {
7421 case ic_argpass:
7422 {
7423 auto_diagnostic_group d;
7424 range_label_for_type_mismatch rhs_label (rhstype, type);
7425 gcc_rich_location richloc (expr_loc, &rhs_label);
7426 if (pedwarn (&richloc, OPT_Wint_conversion,
7427 "passing argument %d of %qE makes pointer from "
7428 "integer without a cast", parmnum, rname))
7429 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7430 }
7431 break;
7432 case ic_assign:
7433 pedwarn (location, OPT_Wint_conversion,
7434 "assignment to %qT from %qT makes pointer from integer "
7435 "without a cast", type, rhstype);
7436 break;
7437 case ic_init:
7438 pedwarn_init (location, OPT_Wint_conversion,
7439 "initialization of %qT from %qT makes pointer from "
7440 "integer without a cast", type, rhstype);
7441 break;
7442 case ic_return:
7443 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7444 "function with return type %qT makes pointer from "
7445 "integer without a cast", rhstype, type);
7446 break;
7447 default:
7448 gcc_unreachable ();
7449 }
7450
7451 return convert (type, rhs);
7452 }
7453 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7454 {
7455 switch (errtype)
7456 {
7457 case ic_argpass:
7458 {
7459 auto_diagnostic_group d;
7460 range_label_for_type_mismatch rhs_label (rhstype, type);
7461 gcc_rich_location richloc (expr_loc, &rhs_label);
7462 if (pedwarn (&richloc, OPT_Wint_conversion,
7463 "passing argument %d of %qE makes integer from "
7464 "pointer without a cast", parmnum, rname))
7465 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7466 }
7467 break;
7468 case ic_assign:
7469 pedwarn (location, OPT_Wint_conversion,
7470 "assignment to %qT from %qT makes integer from pointer "
7471 "without a cast", type, rhstype);
7472 break;
7473 case ic_init:
7474 pedwarn_init (location, OPT_Wint_conversion,
7475 "initialization of %qT from %qT makes integer from "
7476 "pointer without a cast", type, rhstype);
7477 break;
7478 case ic_return:
7479 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7480 "function with return type %qT makes integer from "
7481 "pointer without a cast", rhstype, type);
7482 break;
7483 default:
7484 gcc_unreachable ();
7485 }
7486
7487 return convert (type, rhs);
7488 }
7489 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7490 {
7491 tree ret;
7492 bool save = in_late_binary_op;
7493 in_late_binary_op = true;
7494 ret = convert (type, rhs);
7495 in_late_binary_op = save;
7496 return ret;
7497 }
7498
7499 switch (errtype)
7500 {
7501 case ic_argpass:
7502 {
7503 auto_diagnostic_group d;
7504 range_label_for_type_mismatch rhs_label (rhstype, type);
7505 gcc_rich_location richloc (expr_loc, &rhs_label);
7506 const char msg[] = G_("incompatible type for argument %d of %qE");
7507 if (warnopt)
7508 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7509 else
7510 error_at (&richloc, msg, parmnum, rname);
7511 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7512 }
7513 break;
7514 case ic_assign:
7515 {
7516 const char msg[]
7517 = G_("incompatible types when assigning to type %qT from type %qT");
7518 if (warnopt)
7519 warning_at (expr_loc, 0, msg, type, rhstype);
7520 else
7521 error_at (expr_loc, msg, type, rhstype);
7522 break;
7523 }
7524 case ic_init:
7525 {
7526 const char msg[]
7527 = G_("incompatible types when initializing type %qT using type %qT");
7528 if (warnopt)
7529 warning_at (location, 0, msg, type, rhstype);
7530 else
7531 error_at (location, msg, type, rhstype);
7532 break;
7533 }
7534 case ic_return:
7535 {
7536 const char msg[]
7537 = G_("incompatible types when returning type %qT but %qT was expected");
7538 if (warnopt)
7539 warning_at (location, 0, msg, rhstype, type);
7540 else
7541 error_at (location, msg, rhstype, type);
7542 break;
7543 }
7544 default:
7545 gcc_unreachable ();
7546 }
7547
7548 return error_mark_node;
7549 }
7550
7551 /* If VALUE is a compound expr all of whose expressions are constant, then
7552 return its value. Otherwise, return error_mark_node.
7553
7554 This is for handling COMPOUND_EXPRs as initializer elements
7555 which is allowed with a warning when -pedantic is specified. */
7556
7557 static tree
valid_compound_expr_initializer(tree value,tree endtype)7558 valid_compound_expr_initializer (tree value, tree endtype)
7559 {
7560 if (TREE_CODE (value) == COMPOUND_EXPR)
7561 {
7562 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7563 == error_mark_node)
7564 return error_mark_node;
7565 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7566 endtype);
7567 }
7568 else if (!initializer_constant_valid_p (value, endtype))
7569 return error_mark_node;
7570 else
7571 return value;
7572 }
7573
7574 /* Perform appropriate conversions on the initial value of a variable,
7575 store it in the declaration DECL,
7576 and print any error messages that are appropriate.
7577 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7578 If the init is invalid, store an ERROR_MARK.
7579
7580 INIT_LOC is the location of the initial value. */
7581
7582 void
store_init_value(location_t init_loc,tree decl,tree init,tree origtype)7583 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7584 {
7585 tree value, type;
7586 bool npc = false;
7587
7588 /* If variable's type was invalidly declared, just ignore it. */
7589
7590 type = TREE_TYPE (decl);
7591 if (TREE_CODE (type) == ERROR_MARK)
7592 return;
7593
7594 /* Digest the specified initializer into an expression. */
7595
7596 if (init)
7597 npc = null_pointer_constant_p (init);
7598 value = digest_init (init_loc, type, init, origtype, npc,
7599 true, TREE_STATIC (decl));
7600
7601 /* Store the expression if valid; else report error. */
7602
7603 if (!in_system_header_at (input_location)
7604 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7605 warning (OPT_Wtraditional, "traditional C rejects automatic "
7606 "aggregate initialization");
7607
7608 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7609 DECL_INITIAL (decl) = value;
7610
7611 /* ANSI wants warnings about out-of-range constant initializers. */
7612 STRIP_TYPE_NOPS (value);
7613 if (TREE_STATIC (decl))
7614 constant_expression_warning (value);
7615
7616 /* Check if we need to set array size from compound literal size. */
7617 if (TREE_CODE (type) == ARRAY_TYPE
7618 && TYPE_DOMAIN (type) == NULL_TREE
7619 && value != error_mark_node)
7620 {
7621 tree inside_init = init;
7622
7623 STRIP_TYPE_NOPS (inside_init);
7624 inside_init = fold (inside_init);
7625
7626 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7627 {
7628 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7629
7630 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7631 {
7632 /* For int foo[] = (int [3]){1}; we need to set array size
7633 now since later on array initializer will be just the
7634 brace enclosed list of the compound literal. */
7635 tree etype = strip_array_types (TREE_TYPE (decl));
7636 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7637 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7638 layout_type (type);
7639 layout_decl (cldecl, 0);
7640 TREE_TYPE (decl)
7641 = c_build_qualified_type (type, TYPE_QUALS (etype));
7642 }
7643 }
7644 }
7645 }
7646
7647 /* Methods for storing and printing names for error messages. */
7648
7649 /* Implement a spelling stack that allows components of a name to be pushed
7650 and popped. Each element on the stack is this structure. */
7651
7652 struct spelling
7653 {
7654 int kind;
7655 union
7656 {
7657 unsigned HOST_WIDE_INT i;
7658 const char *s;
7659 } u;
7660 };
7661
7662 #define SPELLING_STRING 1
7663 #define SPELLING_MEMBER 2
7664 #define SPELLING_BOUNDS 3
7665
7666 static struct spelling *spelling; /* Next stack element (unused). */
7667 static struct spelling *spelling_base; /* Spelling stack base. */
7668 static int spelling_size; /* Size of the spelling stack. */
7669
7670 /* Macros to save and restore the spelling stack around push_... functions.
7671 Alternative to SAVE_SPELLING_STACK. */
7672
7673 #define SPELLING_DEPTH() (spelling - spelling_base)
7674 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7675
7676 /* Push an element on the spelling stack with type KIND and assign VALUE
7677 to MEMBER. */
7678
7679 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7680 { \
7681 int depth = SPELLING_DEPTH (); \
7682 \
7683 if (depth >= spelling_size) \
7684 { \
7685 spelling_size += 10; \
7686 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7687 spelling_size); \
7688 RESTORE_SPELLING_DEPTH (depth); \
7689 } \
7690 \
7691 spelling->kind = (KIND); \
7692 spelling->MEMBER = (VALUE); \
7693 spelling++; \
7694 }
7695
7696 /* Push STRING on the stack. Printed literally. */
7697
7698 static void
push_string(const char * string)7699 push_string (const char *string)
7700 {
7701 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7702 }
7703
7704 /* Push a member name on the stack. Printed as '.' STRING. */
7705
7706 static void
push_member_name(tree decl)7707 push_member_name (tree decl)
7708 {
7709 const char *const string
7710 = (DECL_NAME (decl)
7711 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7712 : _("<anonymous>"));
7713 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7714 }
7715
7716 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7717
7718 static void
push_array_bounds(unsigned HOST_WIDE_INT bounds)7719 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7720 {
7721 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7722 }
7723
7724 /* Compute the maximum size in bytes of the printed spelling. */
7725
7726 static int
spelling_length(void)7727 spelling_length (void)
7728 {
7729 int size = 0;
7730 struct spelling *p;
7731
7732 for (p = spelling_base; p < spelling; p++)
7733 {
7734 if (p->kind == SPELLING_BOUNDS)
7735 size += 25;
7736 else
7737 size += strlen (p->u.s) + 1;
7738 }
7739
7740 return size;
7741 }
7742
7743 /* Print the spelling to BUFFER and return it. */
7744
7745 static char *
print_spelling(char * buffer)7746 print_spelling (char *buffer)
7747 {
7748 char *d = buffer;
7749 struct spelling *p;
7750
7751 for (p = spelling_base; p < spelling; p++)
7752 if (p->kind == SPELLING_BOUNDS)
7753 {
7754 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7755 d += strlen (d);
7756 }
7757 else
7758 {
7759 const char *s;
7760 if (p->kind == SPELLING_MEMBER)
7761 *d++ = '.';
7762 for (s = p->u.s; (*d = *s++); d++)
7763 ;
7764 }
7765 *d++ = '\0';
7766 return buffer;
7767 }
7768
7769 /* Digest the parser output INIT as an initializer for type TYPE.
7770 Return a C expression of type TYPE to represent the initial value.
7771
7772 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7773
7774 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7775
7776 If INIT is a string constant, STRICT_STRING is true if it is
7777 unparenthesized or we should not warn here for it being parenthesized.
7778 For other types of INIT, STRICT_STRING is not used.
7779
7780 INIT_LOC is the location of the INIT.
7781
7782 REQUIRE_CONSTANT requests an error if non-constant initializers or
7783 elements are seen. */
7784
7785 static tree
digest_init(location_t init_loc,tree type,tree init,tree origtype,bool null_pointer_constant,bool strict_string,int require_constant)7786 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7787 bool null_pointer_constant, bool strict_string,
7788 int require_constant)
7789 {
7790 enum tree_code code = TREE_CODE (type);
7791 tree inside_init = init;
7792 tree semantic_type = NULL_TREE;
7793 bool maybe_const = true;
7794
7795 if (type == error_mark_node
7796 || !init
7797 || error_operand_p (init))
7798 return error_mark_node;
7799
7800 STRIP_TYPE_NOPS (inside_init);
7801
7802 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7803 {
7804 semantic_type = TREE_TYPE (inside_init);
7805 inside_init = TREE_OPERAND (inside_init, 0);
7806 }
7807 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7808
7809 /* Initialization of an array of chars from a string constant
7810 optionally enclosed in braces. */
7811
7812 if (code == ARRAY_TYPE && inside_init
7813 && TREE_CODE (inside_init) == STRING_CST)
7814 {
7815 tree typ1
7816 = (TYPE_ATOMIC (TREE_TYPE (type))
7817 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7818 TYPE_QUAL_ATOMIC)
7819 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7820 /* Note that an array could be both an array of character type
7821 and an array of wchar_t if wchar_t is signed char or unsigned
7822 char. */
7823 bool char_array = (typ1 == char_type_node
7824 || typ1 == signed_char_type_node
7825 || typ1 == unsigned_char_type_node);
7826 bool wchar_array = !!comptypes (typ1, wchar_type_node);
7827 bool char16_array = !!comptypes (typ1, char16_type_node);
7828 bool char32_array = !!comptypes (typ1, char32_type_node);
7829
7830 if (char_array || wchar_array || char16_array || char32_array)
7831 {
7832 struct c_expr expr;
7833 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7834 bool incompat_string_cst = false;
7835 expr.value = inside_init;
7836 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7837 expr.original_type = NULL;
7838 maybe_warn_string_init (init_loc, type, expr);
7839
7840 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7841 pedwarn_init (init_loc, OPT_Wpedantic,
7842 "initialization of a flexible array member");
7843
7844 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7845 TYPE_MAIN_VARIANT (type)))
7846 return inside_init;
7847
7848 if (char_array)
7849 {
7850 if (typ2 != char_type_node)
7851 incompat_string_cst = true;
7852 }
7853 else if (!comptypes (typ1, typ2))
7854 incompat_string_cst = true;
7855
7856 if (incompat_string_cst)
7857 {
7858 error_init (init_loc, "cannot initialize array of %qT from "
7859 "a string literal with type array of %qT",
7860 typ1, typ2);
7861 return error_mark_node;
7862 }
7863
7864 if (TYPE_DOMAIN (type) != NULL_TREE
7865 && TYPE_SIZE (type) != NULL_TREE
7866 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7867 {
7868 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7869 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
7870
7871 /* Subtract the size of a single (possibly wide) character
7872 because it's ok to ignore the terminating null char
7873 that is counted in the length of the constant. */
7874 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
7875 pedwarn_init (init_loc, 0,
7876 ("initializer-string for array of %qT "
7877 "is too long"), typ1);
7878 else if (warn_cxx_compat
7879 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7880 warning_at (init_loc, OPT_Wc___compat,
7881 ("initializer-string for array of %qT "
7882 "is too long for C++"), typ1);
7883 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7884 {
7885 unsigned HOST_WIDE_INT size
7886 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7887 const char *p = TREE_STRING_POINTER (inside_init);
7888
7889 inside_init = build_string (size, p);
7890 }
7891 }
7892
7893 TREE_TYPE (inside_init) = type;
7894 return inside_init;
7895 }
7896 else if (INTEGRAL_TYPE_P (typ1))
7897 {
7898 error_init (init_loc, "array of inappropriate type initialized "
7899 "from string constant");
7900 return error_mark_node;
7901 }
7902 }
7903
7904 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7905 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7906 below and handle as a constructor. */
7907 if (code == VECTOR_TYPE
7908 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7909 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7910 && TREE_CONSTANT (inside_init))
7911 {
7912 if (TREE_CODE (inside_init) == VECTOR_CST
7913 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7914 TYPE_MAIN_VARIANT (type)))
7915 return inside_init;
7916
7917 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7918 {
7919 unsigned HOST_WIDE_INT ix;
7920 tree value;
7921 bool constant_p = true;
7922
7923 /* Iterate through elements and check if all constructor
7924 elements are *_CSTs. */
7925 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7926 if (!CONSTANT_CLASS_P (value))
7927 {
7928 constant_p = false;
7929 break;
7930 }
7931
7932 if (constant_p)
7933 return build_vector_from_ctor (type,
7934 CONSTRUCTOR_ELTS (inside_init));
7935 }
7936 }
7937
7938 if (warn_sequence_point)
7939 verify_sequence_points (inside_init);
7940
7941 /* Any type can be initialized
7942 from an expression of the same type, optionally with braces. */
7943
7944 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7945 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7946 TYPE_MAIN_VARIANT (type))
7947 || (code == ARRAY_TYPE
7948 && comptypes (TREE_TYPE (inside_init), type))
7949 || (gnu_vector_type_p (type)
7950 && comptypes (TREE_TYPE (inside_init), type))
7951 || (code == POINTER_TYPE
7952 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7953 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7954 TREE_TYPE (type)))))
7955 {
7956 if (code == POINTER_TYPE)
7957 {
7958 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7959 {
7960 if (TREE_CODE (inside_init) == STRING_CST
7961 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7962 inside_init = array_to_pointer_conversion
7963 (init_loc, inside_init);
7964 else
7965 {
7966 error_init (init_loc, "invalid use of non-lvalue array");
7967 return error_mark_node;
7968 }
7969 }
7970 }
7971
7972 if (code == VECTOR_TYPE)
7973 /* Although the types are compatible, we may require a
7974 conversion. */
7975 inside_init = convert (type, inside_init);
7976
7977 if (require_constant
7978 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7979 {
7980 /* As an extension, allow initializing objects with static storage
7981 duration with compound literals (which are then treated just as
7982 the brace enclosed list they contain). Also allow this for
7983 vectors, as we can only assign them with compound literals. */
7984 if (flag_isoc99 && code != VECTOR_TYPE)
7985 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7986 "is not constant");
7987 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7988 inside_init = DECL_INITIAL (decl);
7989 }
7990
7991 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7992 && TREE_CODE (inside_init) != CONSTRUCTOR)
7993 {
7994 error_init (init_loc, "array initialized from non-constant array "
7995 "expression");
7996 return error_mark_node;
7997 }
7998
7999 /* Compound expressions can only occur here if -Wpedantic or
8000 -pedantic-errors is specified. In the later case, we always want
8001 an error. In the former case, we simply want a warning. */
8002 if (require_constant && pedantic
8003 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8004 {
8005 inside_init
8006 = valid_compound_expr_initializer (inside_init,
8007 TREE_TYPE (inside_init));
8008 if (inside_init == error_mark_node)
8009 error_init (init_loc, "initializer element is not constant");
8010 else
8011 pedwarn_init (init_loc, OPT_Wpedantic,
8012 "initializer element is not constant");
8013 if (flag_pedantic_errors)
8014 inside_init = error_mark_node;
8015 }
8016 else if (require_constant
8017 && !initializer_constant_valid_p (inside_init,
8018 TREE_TYPE (inside_init)))
8019 {
8020 error_init (init_loc, "initializer element is not constant");
8021 inside_init = error_mark_node;
8022 }
8023 else if (require_constant && !maybe_const)
8024 pedwarn_init (init_loc, OPT_Wpedantic,
8025 "initializer element is not a constant expression");
8026
8027 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8028 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8029 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8030 type, inside_init, origtype,
8031 ic_init, null_pointer_constant,
8032 NULL_TREE, NULL_TREE, 0);
8033 return inside_init;
8034 }
8035
8036 /* Handle scalar types, including conversions. */
8037
8038 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8039 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8040 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8041 {
8042 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8043 && (TREE_CODE (init) == STRING_CST
8044 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8045 inside_init = init = array_to_pointer_conversion (init_loc, init);
8046 if (semantic_type)
8047 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8048 inside_init);
8049 inside_init
8050 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8051 inside_init, origtype, ic_init,
8052 null_pointer_constant, NULL_TREE, NULL_TREE,
8053 0);
8054
8055 /* Check to see if we have already given an error message. */
8056 if (inside_init == error_mark_node)
8057 ;
8058 else if (require_constant && !TREE_CONSTANT (inside_init))
8059 {
8060 error_init (init_loc, "initializer element is not constant");
8061 inside_init = error_mark_node;
8062 }
8063 else if (require_constant
8064 && !initializer_constant_valid_p (inside_init,
8065 TREE_TYPE (inside_init)))
8066 {
8067 error_init (init_loc, "initializer element is not computable at "
8068 "load time");
8069 inside_init = error_mark_node;
8070 }
8071 else if (require_constant && !maybe_const)
8072 pedwarn_init (init_loc, OPT_Wpedantic,
8073 "initializer element is not a constant expression");
8074
8075 return inside_init;
8076 }
8077
8078 /* Come here only for records and arrays. */
8079
8080 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8081 {
8082 error_init (init_loc, "variable-sized object may not be initialized");
8083 return error_mark_node;
8084 }
8085
8086 error_init (init_loc, "invalid initializer");
8087 return error_mark_node;
8088 }
8089
8090 /* Handle initializers that use braces. */
8091
8092 /* Type of object we are accumulating a constructor for.
8093 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8094 static tree constructor_type;
8095
8096 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8097 left to fill. */
8098 static tree constructor_fields;
8099
8100 /* For an ARRAY_TYPE, this is the specified index
8101 at which to store the next element we get. */
8102 static tree constructor_index;
8103
8104 /* For an ARRAY_TYPE, this is the maximum index. */
8105 static tree constructor_max_index;
8106
8107 /* For a RECORD_TYPE, this is the first field not yet written out. */
8108 static tree constructor_unfilled_fields;
8109
8110 /* For an ARRAY_TYPE, this is the index of the first element
8111 not yet written out. */
8112 static tree constructor_unfilled_index;
8113
8114 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8115 This is so we can generate gaps between fields, when appropriate. */
8116 static tree constructor_bit_index;
8117
8118 /* If we are saving up the elements rather than allocating them,
8119 this is the list of elements so far (in reverse order,
8120 most recent first). */
8121 static vec<constructor_elt, va_gc> *constructor_elements;
8122
8123 /* 1 if constructor should be incrementally stored into a constructor chain,
8124 0 if all the elements should be kept in AVL tree. */
8125 static int constructor_incremental;
8126
8127 /* 1 if so far this constructor's elements are all compile-time constants. */
8128 static int constructor_constant;
8129
8130 /* 1 if so far this constructor's elements are all valid address constants. */
8131 static int constructor_simple;
8132
8133 /* 1 if this constructor has an element that cannot be part of a
8134 constant expression. */
8135 static int constructor_nonconst;
8136
8137 /* 1 if this constructor is erroneous so far. */
8138 static int constructor_erroneous;
8139
8140 /* 1 if this constructor is the universal zero initializer { 0 }. */
8141 static int constructor_zeroinit;
8142
8143 /* Structure for managing pending initializer elements, organized as an
8144 AVL tree. */
8145
8146 struct init_node
8147 {
8148 struct init_node *left, *right;
8149 struct init_node *parent;
8150 int balance;
8151 tree purpose;
8152 tree value;
8153 tree origtype;
8154 };
8155
8156 /* Tree of pending elements at this constructor level.
8157 These are elements encountered out of order
8158 which belong at places we haven't reached yet in actually
8159 writing the output.
8160 Will never hold tree nodes across GC runs. */
8161 static struct init_node *constructor_pending_elts;
8162
8163 /* The SPELLING_DEPTH of this constructor. */
8164 static int constructor_depth;
8165
8166 /* DECL node for which an initializer is being read.
8167 0 means we are reading a constructor expression
8168 such as (struct foo) {...}. */
8169 static tree constructor_decl;
8170
8171 /* Nonzero if this is an initializer for a top-level decl. */
8172 static int constructor_top_level;
8173
8174 /* Nonzero if there were any member designators in this initializer. */
8175 static int constructor_designated;
8176
8177 /* Nesting depth of designator list. */
8178 static int designator_depth;
8179
8180 /* Nonzero if there were diagnosed errors in this designator list. */
8181 static int designator_erroneous;
8182
8183
8184 /* This stack has a level for each implicit or explicit level of
8185 structuring in the initializer, including the outermost one. It
8186 saves the values of most of the variables above. */
8187
8188 struct constructor_range_stack;
8189
8190 struct constructor_stack
8191 {
8192 struct constructor_stack *next;
8193 tree type;
8194 tree fields;
8195 tree index;
8196 tree max_index;
8197 tree unfilled_index;
8198 tree unfilled_fields;
8199 tree bit_index;
8200 vec<constructor_elt, va_gc> *elements;
8201 struct init_node *pending_elts;
8202 int offset;
8203 int depth;
8204 /* If value nonzero, this value should replace the entire
8205 constructor at this level. */
8206 struct c_expr replacement_value;
8207 struct constructor_range_stack *range_stack;
8208 char constant;
8209 char simple;
8210 char nonconst;
8211 char implicit;
8212 char erroneous;
8213 char outer;
8214 char incremental;
8215 char designated;
8216 int designator_depth;
8217 };
8218
8219 static struct constructor_stack *constructor_stack;
8220
8221 /* This stack represents designators from some range designator up to
8222 the last designator in the list. */
8223
8224 struct constructor_range_stack
8225 {
8226 struct constructor_range_stack *next, *prev;
8227 struct constructor_stack *stack;
8228 tree range_start;
8229 tree index;
8230 tree range_end;
8231 tree fields;
8232 };
8233
8234 static struct constructor_range_stack *constructor_range_stack;
8235
8236 /* This stack records separate initializers that are nested.
8237 Nested initializers can't happen in ANSI C, but GNU C allows them
8238 in cases like { ... (struct foo) { ... } ... }. */
8239
8240 struct initializer_stack
8241 {
8242 struct initializer_stack *next;
8243 tree decl;
8244 struct constructor_stack *constructor_stack;
8245 struct constructor_range_stack *constructor_range_stack;
8246 vec<constructor_elt, va_gc> *elements;
8247 struct spelling *spelling;
8248 struct spelling *spelling_base;
8249 int spelling_size;
8250 char top_level;
8251 char require_constant_value;
8252 char require_constant_elements;
8253 rich_location *missing_brace_richloc;
8254 };
8255
8256 static struct initializer_stack *initializer_stack;
8257
8258 /* Prepare to parse and output the initializer for variable DECL. */
8259
8260 void
start_init(tree decl,tree asmspec_tree ATTRIBUTE_UNUSED,int top_level,rich_location * richloc)8261 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8262 rich_location *richloc)
8263 {
8264 const char *locus;
8265 struct initializer_stack *p = XNEW (struct initializer_stack);
8266
8267 p->decl = constructor_decl;
8268 p->require_constant_value = require_constant_value;
8269 p->require_constant_elements = require_constant_elements;
8270 p->constructor_stack = constructor_stack;
8271 p->constructor_range_stack = constructor_range_stack;
8272 p->elements = constructor_elements;
8273 p->spelling = spelling;
8274 p->spelling_base = spelling_base;
8275 p->spelling_size = spelling_size;
8276 p->top_level = constructor_top_level;
8277 p->next = initializer_stack;
8278 p->missing_brace_richloc = richloc;
8279 initializer_stack = p;
8280
8281 constructor_decl = decl;
8282 constructor_designated = 0;
8283 constructor_top_level = top_level;
8284
8285 if (decl != NULL_TREE && decl != error_mark_node)
8286 {
8287 require_constant_value = TREE_STATIC (decl);
8288 require_constant_elements
8289 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8290 /* For a scalar, you can always use any value to initialize,
8291 even within braces. */
8292 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8293 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8294 }
8295 else
8296 {
8297 require_constant_value = 0;
8298 require_constant_elements = 0;
8299 locus = _("(anonymous)");
8300 }
8301
8302 constructor_stack = 0;
8303 constructor_range_stack = 0;
8304
8305 found_missing_braces = 0;
8306
8307 spelling_base = 0;
8308 spelling_size = 0;
8309 RESTORE_SPELLING_DEPTH (0);
8310
8311 if (locus)
8312 push_string (locus);
8313 }
8314
8315 void
finish_init(void)8316 finish_init (void)
8317 {
8318 struct initializer_stack *p = initializer_stack;
8319
8320 /* Free the whole constructor stack of this initializer. */
8321 while (constructor_stack)
8322 {
8323 struct constructor_stack *q = constructor_stack;
8324 constructor_stack = q->next;
8325 free (q);
8326 }
8327
8328 gcc_assert (!constructor_range_stack);
8329
8330 /* Pop back to the data of the outer initializer (if any). */
8331 free (spelling_base);
8332
8333 constructor_decl = p->decl;
8334 require_constant_value = p->require_constant_value;
8335 require_constant_elements = p->require_constant_elements;
8336 constructor_stack = p->constructor_stack;
8337 constructor_range_stack = p->constructor_range_stack;
8338 constructor_elements = p->elements;
8339 spelling = p->spelling;
8340 spelling_base = p->spelling_base;
8341 spelling_size = p->spelling_size;
8342 constructor_top_level = p->top_level;
8343 initializer_stack = p->next;
8344 free (p);
8345 }
8346
8347 /* Call here when we see the initializer is surrounded by braces.
8348 This is instead of a call to push_init_level;
8349 it is matched by a call to pop_init_level.
8350
8351 TYPE is the type to initialize, for a constructor expression.
8352 For an initializer for a decl, TYPE is zero. */
8353
8354 void
really_start_incremental_init(tree type)8355 really_start_incremental_init (tree type)
8356 {
8357 struct constructor_stack *p = XNEW (struct constructor_stack);
8358
8359 if (type == NULL_TREE)
8360 type = TREE_TYPE (constructor_decl);
8361
8362 if (VECTOR_TYPE_P (type)
8363 && TYPE_VECTOR_OPAQUE (type))
8364 error ("opaque vector types cannot be initialized");
8365
8366 p->type = constructor_type;
8367 p->fields = constructor_fields;
8368 p->index = constructor_index;
8369 p->max_index = constructor_max_index;
8370 p->unfilled_index = constructor_unfilled_index;
8371 p->unfilled_fields = constructor_unfilled_fields;
8372 p->bit_index = constructor_bit_index;
8373 p->elements = constructor_elements;
8374 p->constant = constructor_constant;
8375 p->simple = constructor_simple;
8376 p->nonconst = constructor_nonconst;
8377 p->erroneous = constructor_erroneous;
8378 p->pending_elts = constructor_pending_elts;
8379 p->depth = constructor_depth;
8380 p->replacement_value.value = 0;
8381 p->replacement_value.original_code = ERROR_MARK;
8382 p->replacement_value.original_type = NULL;
8383 p->implicit = 0;
8384 p->range_stack = 0;
8385 p->outer = 0;
8386 p->incremental = constructor_incremental;
8387 p->designated = constructor_designated;
8388 p->designator_depth = designator_depth;
8389 p->next = 0;
8390 constructor_stack = p;
8391
8392 constructor_constant = 1;
8393 constructor_simple = 1;
8394 constructor_nonconst = 0;
8395 constructor_depth = SPELLING_DEPTH ();
8396 constructor_elements = NULL;
8397 constructor_pending_elts = 0;
8398 constructor_type = type;
8399 constructor_incremental = 1;
8400 constructor_designated = 0;
8401 constructor_zeroinit = 1;
8402 designator_depth = 0;
8403 designator_erroneous = 0;
8404
8405 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8406 {
8407 constructor_fields = TYPE_FIELDS (constructor_type);
8408 /* Skip any nameless bit fields at the beginning. */
8409 while (constructor_fields != NULL_TREE
8410 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8411 constructor_fields = DECL_CHAIN (constructor_fields);
8412
8413 constructor_unfilled_fields = constructor_fields;
8414 constructor_bit_index = bitsize_zero_node;
8415 }
8416 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8417 {
8418 if (TYPE_DOMAIN (constructor_type))
8419 {
8420 constructor_max_index
8421 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8422
8423 /* Detect non-empty initializations of zero-length arrays. */
8424 if (constructor_max_index == NULL_TREE
8425 && TYPE_SIZE (constructor_type))
8426 constructor_max_index = integer_minus_one_node;
8427
8428 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8429 to initialize VLAs will cause a proper error; avoid tree
8430 checking errors as well by setting a safe value. */
8431 if (constructor_max_index
8432 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8433 constructor_max_index = integer_minus_one_node;
8434
8435 constructor_index
8436 = convert (bitsizetype,
8437 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8438 }
8439 else
8440 {
8441 constructor_index = bitsize_zero_node;
8442 constructor_max_index = NULL_TREE;
8443 }
8444
8445 constructor_unfilled_index = constructor_index;
8446 }
8447 else if (gnu_vector_type_p (constructor_type))
8448 {
8449 /* Vectors are like simple fixed-size arrays. */
8450 constructor_max_index =
8451 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8452 constructor_index = bitsize_zero_node;
8453 constructor_unfilled_index = constructor_index;
8454 }
8455 else
8456 {
8457 /* Handle the case of int x = {5}; */
8458 constructor_fields = constructor_type;
8459 constructor_unfilled_fields = constructor_type;
8460 }
8461 }
8462
8463 extern location_t last_init_list_comma;
8464
8465 /* Called when we see an open brace for a nested initializer. Finish
8466 off any pending levels with implicit braces. */
8467 void
finish_implicit_inits(location_t loc,struct obstack * braced_init_obstack)8468 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8469 {
8470 while (constructor_stack->implicit)
8471 {
8472 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8473 && constructor_fields == NULL_TREE)
8474 process_init_element (input_location,
8475 pop_init_level (loc, 1, braced_init_obstack,
8476 last_init_list_comma),
8477 true, braced_init_obstack);
8478 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8479 && constructor_max_index
8480 && tree_int_cst_lt (constructor_max_index,
8481 constructor_index))
8482 process_init_element (input_location,
8483 pop_init_level (loc, 1, braced_init_obstack,
8484 last_init_list_comma),
8485 true, braced_init_obstack);
8486 else
8487 break;
8488 }
8489 }
8490
8491 /* Push down into a subobject, for initialization.
8492 If this is for an explicit set of braces, IMPLICIT is 0.
8493 If it is because the next element belongs at a lower level,
8494 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8495
8496 void
push_init_level(location_t loc,int implicit,struct obstack * braced_init_obstack)8497 push_init_level (location_t loc, int implicit,
8498 struct obstack *braced_init_obstack)
8499 {
8500 struct constructor_stack *p;
8501 tree value = NULL_TREE;
8502
8503 /* Unless this is an explicit brace, we need to preserve previous
8504 content if any. */
8505 if (implicit)
8506 {
8507 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8508 value = find_init_member (constructor_fields, braced_init_obstack);
8509 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8510 value = find_init_member (constructor_index, braced_init_obstack);
8511 }
8512
8513 p = XNEW (struct constructor_stack);
8514 p->type = constructor_type;
8515 p->fields = constructor_fields;
8516 p->index = constructor_index;
8517 p->max_index = constructor_max_index;
8518 p->unfilled_index = constructor_unfilled_index;
8519 p->unfilled_fields = constructor_unfilled_fields;
8520 p->bit_index = constructor_bit_index;
8521 p->elements = constructor_elements;
8522 p->constant = constructor_constant;
8523 p->simple = constructor_simple;
8524 p->nonconst = constructor_nonconst;
8525 p->erroneous = constructor_erroneous;
8526 p->pending_elts = constructor_pending_elts;
8527 p->depth = constructor_depth;
8528 p->replacement_value.value = NULL_TREE;
8529 p->replacement_value.original_code = ERROR_MARK;
8530 p->replacement_value.original_type = NULL;
8531 p->implicit = implicit;
8532 p->outer = 0;
8533 p->incremental = constructor_incremental;
8534 p->designated = constructor_designated;
8535 p->designator_depth = designator_depth;
8536 p->next = constructor_stack;
8537 p->range_stack = 0;
8538 constructor_stack = p;
8539
8540 constructor_constant = 1;
8541 constructor_simple = 1;
8542 constructor_nonconst = 0;
8543 constructor_depth = SPELLING_DEPTH ();
8544 constructor_elements = NULL;
8545 constructor_incremental = 1;
8546 constructor_designated = 0;
8547 constructor_pending_elts = 0;
8548 if (!implicit)
8549 {
8550 p->range_stack = constructor_range_stack;
8551 constructor_range_stack = 0;
8552 designator_depth = 0;
8553 designator_erroneous = 0;
8554 }
8555
8556 /* Don't die if an entire brace-pair level is superfluous
8557 in the containing level. */
8558 if (constructor_type == NULL_TREE)
8559 ;
8560 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8561 {
8562 /* Don't die if there are extra init elts at the end. */
8563 if (constructor_fields == NULL_TREE)
8564 constructor_type = NULL_TREE;
8565 else
8566 {
8567 constructor_type = TREE_TYPE (constructor_fields);
8568 push_member_name (constructor_fields);
8569 constructor_depth++;
8570 }
8571 /* If upper initializer is designated, then mark this as
8572 designated too to prevent bogus warnings. */
8573 constructor_designated = p->designated;
8574 }
8575 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8576 {
8577 constructor_type = TREE_TYPE (constructor_type);
8578 push_array_bounds (tree_to_uhwi (constructor_index));
8579 constructor_depth++;
8580 }
8581
8582 if (constructor_type == NULL_TREE)
8583 {
8584 error_init (loc, "extra brace group at end of initializer");
8585 constructor_fields = NULL_TREE;
8586 constructor_unfilled_fields = NULL_TREE;
8587 return;
8588 }
8589
8590 if (value && TREE_CODE (value) == CONSTRUCTOR)
8591 {
8592 constructor_constant = TREE_CONSTANT (value);
8593 constructor_simple = TREE_STATIC (value);
8594 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8595 constructor_elements = CONSTRUCTOR_ELTS (value);
8596 if (!vec_safe_is_empty (constructor_elements)
8597 && (TREE_CODE (constructor_type) == RECORD_TYPE
8598 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8599 set_nonincremental_init (braced_init_obstack);
8600 }
8601
8602 if (implicit == 1)
8603 {
8604 found_missing_braces = 1;
8605 if (initializer_stack->missing_brace_richloc)
8606 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8607 (loc, "{");
8608 }
8609
8610 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8611 {
8612 constructor_fields = TYPE_FIELDS (constructor_type);
8613 /* Skip any nameless bit fields at the beginning. */
8614 while (constructor_fields != NULL_TREE
8615 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8616 constructor_fields = DECL_CHAIN (constructor_fields);
8617
8618 constructor_unfilled_fields = constructor_fields;
8619 constructor_bit_index = bitsize_zero_node;
8620 }
8621 else if (gnu_vector_type_p (constructor_type))
8622 {
8623 /* Vectors are like simple fixed-size arrays. */
8624 constructor_max_index =
8625 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8626 constructor_index = bitsize_int (0);
8627 constructor_unfilled_index = constructor_index;
8628 }
8629 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8630 {
8631 if (TYPE_DOMAIN (constructor_type))
8632 {
8633 constructor_max_index
8634 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8635
8636 /* Detect non-empty initializations of zero-length arrays. */
8637 if (constructor_max_index == NULL_TREE
8638 && TYPE_SIZE (constructor_type))
8639 constructor_max_index = integer_minus_one_node;
8640
8641 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8642 to initialize VLAs will cause a proper error; avoid tree
8643 checking errors as well by setting a safe value. */
8644 if (constructor_max_index
8645 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8646 constructor_max_index = integer_minus_one_node;
8647
8648 constructor_index
8649 = convert (bitsizetype,
8650 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8651 }
8652 else
8653 constructor_index = bitsize_zero_node;
8654
8655 constructor_unfilled_index = constructor_index;
8656 if (value && TREE_CODE (value) == STRING_CST)
8657 {
8658 /* We need to split the char/wchar array into individual
8659 characters, so that we don't have to special case it
8660 everywhere. */
8661 set_nonincremental_init_from_string (value, braced_init_obstack);
8662 }
8663 }
8664 else
8665 {
8666 if (constructor_type != error_mark_node)
8667 warning_init (input_location, 0, "braces around scalar initializer");
8668 constructor_fields = constructor_type;
8669 constructor_unfilled_fields = constructor_type;
8670 }
8671 }
8672
8673 /* At the end of an implicit or explicit brace level,
8674 finish up that level of constructor. If a single expression
8675 with redundant braces initialized that level, return the
8676 c_expr structure for that expression. Otherwise, the original_code
8677 element is set to ERROR_MARK.
8678 If we were outputting the elements as they are read, return 0 as the value
8679 from inner levels (process_init_element ignores that),
8680 but return error_mark_node as the value from the outermost level
8681 (that's what we want to put in DECL_INITIAL).
8682 Otherwise, return a CONSTRUCTOR expression as the value. */
8683
8684 struct c_expr
pop_init_level(location_t loc,int implicit,struct obstack * braced_init_obstack,location_t insert_before)8685 pop_init_level (location_t loc, int implicit,
8686 struct obstack *braced_init_obstack,
8687 location_t insert_before)
8688 {
8689 struct constructor_stack *p;
8690 struct c_expr ret;
8691 ret.value = NULL_TREE;
8692 ret.original_code = ERROR_MARK;
8693 ret.original_type = NULL;
8694
8695 if (implicit == 0)
8696 {
8697 /* When we come to an explicit close brace,
8698 pop any inner levels that didn't have explicit braces. */
8699 while (constructor_stack->implicit)
8700 process_init_element (input_location,
8701 pop_init_level (loc, 1, braced_init_obstack,
8702 insert_before),
8703 true, braced_init_obstack);
8704 gcc_assert (!constructor_range_stack);
8705 }
8706 else
8707 if (initializer_stack->missing_brace_richloc)
8708 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8709 (insert_before, "}");
8710
8711 /* Now output all pending elements. */
8712 constructor_incremental = 1;
8713 output_pending_init_elements (1, braced_init_obstack);
8714
8715 p = constructor_stack;
8716
8717 /* Error for initializing a flexible array member, or a zero-length
8718 array member in an inappropriate context. */
8719 if (constructor_type && constructor_fields
8720 && TREE_CODE (constructor_type) == ARRAY_TYPE
8721 && TYPE_DOMAIN (constructor_type)
8722 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8723 {
8724 /* Silently discard empty initializations. The parser will
8725 already have pedwarned for empty brackets. */
8726 if (integer_zerop (constructor_unfilled_index))
8727 constructor_type = NULL_TREE;
8728 else
8729 {
8730 gcc_assert (!TYPE_SIZE (constructor_type));
8731
8732 if (constructor_depth > 2)
8733 error_init (loc, "initialization of flexible array member in a nested context");
8734 else
8735 pedwarn_init (loc, OPT_Wpedantic,
8736 "initialization of a flexible array member");
8737
8738 /* We have already issued an error message for the existence
8739 of a flexible array member not at the end of the structure.
8740 Discard the initializer so that we do not die later. */
8741 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8742 constructor_type = NULL_TREE;
8743 }
8744 }
8745
8746 switch (vec_safe_length (constructor_elements))
8747 {
8748 case 0:
8749 /* Initialization with { } counts as zeroinit. */
8750 constructor_zeroinit = 1;
8751 break;
8752 case 1:
8753 /* This might be zeroinit as well. */
8754 if (integer_zerop ((*constructor_elements)[0].value))
8755 constructor_zeroinit = 1;
8756 break;
8757 default:
8758 /* If the constructor has more than one element, it can't be { 0 }. */
8759 constructor_zeroinit = 0;
8760 break;
8761 }
8762
8763 /* Warn when some structs are initialized with direct aggregation. */
8764 if (!implicit && found_missing_braces && warn_missing_braces
8765 && !constructor_zeroinit)
8766 {
8767 gcc_assert (initializer_stack->missing_brace_richloc);
8768 warning_at (initializer_stack->missing_brace_richloc,
8769 OPT_Wmissing_braces,
8770 "missing braces around initializer");
8771 }
8772
8773 /* Warn when some struct elements are implicitly initialized to zero. */
8774 if (warn_missing_field_initializers
8775 && constructor_type
8776 && TREE_CODE (constructor_type) == RECORD_TYPE
8777 && constructor_unfilled_fields)
8778 {
8779 /* Do not warn for flexible array members or zero-length arrays. */
8780 while (constructor_unfilled_fields
8781 && (!DECL_SIZE (constructor_unfilled_fields)
8782 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8783 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8784
8785 if (constructor_unfilled_fields
8786 /* Do not warn if this level of the initializer uses member
8787 designators; it is likely to be deliberate. */
8788 && !constructor_designated
8789 /* Do not warn about initializing with { 0 } or with { }. */
8790 && !constructor_zeroinit)
8791 {
8792 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8793 "missing initializer for field %qD of %qT",
8794 constructor_unfilled_fields,
8795 constructor_type))
8796 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8797 "%qD declared here", constructor_unfilled_fields);
8798 }
8799 }
8800
8801 /* Pad out the end of the structure. */
8802 if (p->replacement_value.value)
8803 /* If this closes a superfluous brace pair,
8804 just pass out the element between them. */
8805 ret = p->replacement_value;
8806 else if (constructor_type == NULL_TREE)
8807 ;
8808 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8809 && TREE_CODE (constructor_type) != ARRAY_TYPE
8810 && !gnu_vector_type_p (constructor_type))
8811 {
8812 /* A nonincremental scalar initializer--just return
8813 the element, after verifying there is just one. */
8814 if (vec_safe_is_empty (constructor_elements))
8815 {
8816 if (!constructor_erroneous && constructor_type != error_mark_node)
8817 error_init (loc, "empty scalar initializer");
8818 ret.value = error_mark_node;
8819 }
8820 else if (vec_safe_length (constructor_elements) != 1)
8821 {
8822 error_init (loc, "extra elements in scalar initializer");
8823 ret.value = (*constructor_elements)[0].value;
8824 }
8825 else
8826 ret.value = (*constructor_elements)[0].value;
8827 }
8828 else
8829 {
8830 if (constructor_erroneous)
8831 ret.value = error_mark_node;
8832 else
8833 {
8834 ret.value = build_constructor (constructor_type,
8835 constructor_elements);
8836 if (constructor_constant)
8837 TREE_CONSTANT (ret.value) = 1;
8838 if (constructor_constant && constructor_simple)
8839 TREE_STATIC (ret.value) = 1;
8840 if (constructor_nonconst)
8841 CONSTRUCTOR_NON_CONST (ret.value) = 1;
8842 }
8843 }
8844
8845 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8846 {
8847 if (constructor_nonconst)
8848 ret.original_code = C_MAYBE_CONST_EXPR;
8849 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8850 ret.original_code = ERROR_MARK;
8851 }
8852
8853 constructor_type = p->type;
8854 constructor_fields = p->fields;
8855 constructor_index = p->index;
8856 constructor_max_index = p->max_index;
8857 constructor_unfilled_index = p->unfilled_index;
8858 constructor_unfilled_fields = p->unfilled_fields;
8859 constructor_bit_index = p->bit_index;
8860 constructor_elements = p->elements;
8861 constructor_constant = p->constant;
8862 constructor_simple = p->simple;
8863 constructor_nonconst = p->nonconst;
8864 constructor_erroneous = p->erroneous;
8865 constructor_incremental = p->incremental;
8866 constructor_designated = p->designated;
8867 designator_depth = p->designator_depth;
8868 constructor_pending_elts = p->pending_elts;
8869 constructor_depth = p->depth;
8870 if (!p->implicit)
8871 constructor_range_stack = p->range_stack;
8872 RESTORE_SPELLING_DEPTH (constructor_depth);
8873
8874 constructor_stack = p->next;
8875 free (p);
8876
8877 if (ret.value == NULL_TREE && constructor_stack == 0)
8878 ret.value = error_mark_node;
8879 return ret;
8880 }
8881
8882 /* Common handling for both array range and field name designators.
8883 ARRAY argument is nonzero for array ranges. Returns false for success. */
8884
8885 static bool
set_designator(location_t loc,bool array,struct obstack * braced_init_obstack)8886 set_designator (location_t loc, bool array,
8887 struct obstack *braced_init_obstack)
8888 {
8889 tree subtype;
8890 enum tree_code subcode;
8891
8892 /* Don't die if an entire brace-pair level is superfluous
8893 in the containing level, or for an erroneous type. */
8894 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
8895 return true;
8896
8897 /* If there were errors in this designator list already, bail out
8898 silently. */
8899 if (designator_erroneous)
8900 return true;
8901
8902 /* Likewise for an initializer for a variable-size type. Those are
8903 diagnosed in digest_init. */
8904 if (COMPLETE_TYPE_P (constructor_type)
8905 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
8906 return true;
8907
8908 if (!designator_depth)
8909 {
8910 gcc_assert (!constructor_range_stack);
8911
8912 /* Designator list starts at the level of closest explicit
8913 braces. */
8914 while (constructor_stack->implicit)
8915 process_init_element (input_location,
8916 pop_init_level (loc, 1, braced_init_obstack,
8917 last_init_list_comma),
8918 true, braced_init_obstack);
8919 constructor_designated = 1;
8920 return false;
8921 }
8922
8923 switch (TREE_CODE (constructor_type))
8924 {
8925 case RECORD_TYPE:
8926 case UNION_TYPE:
8927 subtype = TREE_TYPE (constructor_fields);
8928 if (subtype != error_mark_node)
8929 subtype = TYPE_MAIN_VARIANT (subtype);
8930 break;
8931 case ARRAY_TYPE:
8932 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8933 break;
8934 default:
8935 gcc_unreachable ();
8936 }
8937
8938 subcode = TREE_CODE (subtype);
8939 if (array && subcode != ARRAY_TYPE)
8940 {
8941 error_init (loc, "array index in non-array initializer");
8942 return true;
8943 }
8944 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8945 {
8946 error_init (loc, "field name not in record or union initializer");
8947 return true;
8948 }
8949
8950 constructor_designated = 1;
8951 finish_implicit_inits (loc, braced_init_obstack);
8952 push_init_level (loc, 2, braced_init_obstack);
8953 return false;
8954 }
8955
8956 /* If there are range designators in designator list, push a new designator
8957 to constructor_range_stack. RANGE_END is end of such stack range or
8958 NULL_TREE if there is no range designator at this level. */
8959
8960 static void
push_range_stack(tree range_end,struct obstack * braced_init_obstack)8961 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8962 {
8963 struct constructor_range_stack *p;
8964
8965 p = (struct constructor_range_stack *)
8966 obstack_alloc (braced_init_obstack,
8967 sizeof (struct constructor_range_stack));
8968 p->prev = constructor_range_stack;
8969 p->next = 0;
8970 p->fields = constructor_fields;
8971 p->range_start = constructor_index;
8972 p->index = constructor_index;
8973 p->stack = constructor_stack;
8974 p->range_end = range_end;
8975 if (constructor_range_stack)
8976 constructor_range_stack->next = p;
8977 constructor_range_stack = p;
8978 }
8979
8980 /* Within an array initializer, specify the next index to be initialized.
8981 FIRST is that index. If LAST is nonzero, then initialize a range
8982 of indices, running from FIRST through LAST. */
8983
8984 void
set_init_index(location_t loc,tree first,tree last,struct obstack * braced_init_obstack)8985 set_init_index (location_t loc, tree first, tree last,
8986 struct obstack *braced_init_obstack)
8987 {
8988 if (set_designator (loc, true, braced_init_obstack))
8989 return;
8990
8991 designator_erroneous = 1;
8992
8993 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8994 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8995 {
8996 error_init (loc, "array index in initializer not of integer type");
8997 return;
8998 }
8999
9000 if (TREE_CODE (first) != INTEGER_CST)
9001 {
9002 first = c_fully_fold (first, false, NULL);
9003 if (TREE_CODE (first) == INTEGER_CST)
9004 pedwarn_init (loc, OPT_Wpedantic,
9005 "array index in initializer is not "
9006 "an integer constant expression");
9007 }
9008
9009 if (last && TREE_CODE (last) != INTEGER_CST)
9010 {
9011 last = c_fully_fold (last, false, NULL);
9012 if (TREE_CODE (last) == INTEGER_CST)
9013 pedwarn_init (loc, OPT_Wpedantic,
9014 "array index in initializer is not "
9015 "an integer constant expression");
9016 }
9017
9018 if (TREE_CODE (first) != INTEGER_CST)
9019 error_init (loc, "nonconstant array index in initializer");
9020 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9021 error_init (loc, "nonconstant array index in initializer");
9022 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9023 error_init (loc, "array index in non-array initializer");
9024 else if (tree_int_cst_sgn (first) == -1)
9025 error_init (loc, "array index in initializer exceeds array bounds");
9026 else if (constructor_max_index
9027 && tree_int_cst_lt (constructor_max_index, first))
9028 error_init (loc, "array index in initializer exceeds array bounds");
9029 else
9030 {
9031 constant_expression_warning (first);
9032 if (last)
9033 constant_expression_warning (last);
9034 constructor_index = convert (bitsizetype, first);
9035 if (tree_int_cst_lt (constructor_index, first))
9036 {
9037 constructor_index = copy_node (constructor_index);
9038 TREE_OVERFLOW (constructor_index) = 1;
9039 }
9040
9041 if (last)
9042 {
9043 if (tree_int_cst_equal (first, last))
9044 last = NULL_TREE;
9045 else if (tree_int_cst_lt (last, first))
9046 {
9047 error_init (loc, "empty index range in initializer");
9048 last = NULL_TREE;
9049 }
9050 else
9051 {
9052 last = convert (bitsizetype, last);
9053 if (constructor_max_index != NULL_TREE
9054 && tree_int_cst_lt (constructor_max_index, last))
9055 {
9056 error_init (loc, "array index range in initializer exceeds "
9057 "array bounds");
9058 last = NULL_TREE;
9059 }
9060 }
9061 }
9062
9063 designator_depth++;
9064 designator_erroneous = 0;
9065 if (constructor_range_stack || last)
9066 push_range_stack (last, braced_init_obstack);
9067 }
9068 }
9069
9070 /* Within a struct initializer, specify the next field to be initialized. */
9071
9072 void
set_init_label(location_t loc,tree fieldname,location_t fieldname_loc,struct obstack * braced_init_obstack)9073 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9074 struct obstack *braced_init_obstack)
9075 {
9076 tree field;
9077
9078 if (set_designator (loc, false, braced_init_obstack))
9079 return;
9080
9081 designator_erroneous = 1;
9082
9083 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9084 {
9085 error_init (loc, "field name not in record or union initializer");
9086 return;
9087 }
9088
9089 field = lookup_field (constructor_type, fieldname);
9090
9091 if (field == NULL_TREE)
9092 {
9093 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9094 if (guessed_id)
9095 {
9096 gcc_rich_location rich_loc (fieldname_loc);
9097 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9098 error_at (&rich_loc,
9099 "%qT has no member named %qE; did you mean %qE?",
9100 constructor_type, fieldname, guessed_id);
9101 }
9102 else
9103 error_at (fieldname_loc, "%qT has no member named %qE",
9104 constructor_type, fieldname);
9105 }
9106 else
9107 do
9108 {
9109 constructor_fields = TREE_VALUE (field);
9110 designator_depth++;
9111 designator_erroneous = 0;
9112 if (constructor_range_stack)
9113 push_range_stack (NULL_TREE, braced_init_obstack);
9114 field = TREE_CHAIN (field);
9115 if (field)
9116 {
9117 if (set_designator (loc, false, braced_init_obstack))
9118 return;
9119 }
9120 }
9121 while (field != NULL_TREE);
9122 }
9123
9124 /* Add a new initializer to the tree of pending initializers. PURPOSE
9125 identifies the initializer, either array index or field in a structure.
9126 VALUE is the value of that index or field. If ORIGTYPE is not
9127 NULL_TREE, it is the original type of VALUE.
9128
9129 IMPLICIT is true if value comes from pop_init_level (1),
9130 the new initializer has been merged with the existing one
9131 and thus no warnings should be emitted about overriding an
9132 existing initializer. */
9133
9134 static void
add_pending_init(location_t loc,tree purpose,tree value,tree origtype,bool implicit,struct obstack * braced_init_obstack)9135 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9136 bool implicit, struct obstack *braced_init_obstack)
9137 {
9138 struct init_node *p, **q, *r;
9139
9140 q = &constructor_pending_elts;
9141 p = 0;
9142
9143 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9144 {
9145 while (*q != 0)
9146 {
9147 p = *q;
9148 if (tree_int_cst_lt (purpose, p->purpose))
9149 q = &p->left;
9150 else if (tree_int_cst_lt (p->purpose, purpose))
9151 q = &p->right;
9152 else
9153 {
9154 if (!implicit)
9155 {
9156 if (TREE_SIDE_EFFECTS (p->value))
9157 warning_init (loc, OPT_Woverride_init_side_effects,
9158 "initialized field with side-effects "
9159 "overwritten");
9160 else if (warn_override_init)
9161 warning_init (loc, OPT_Woverride_init,
9162 "initialized field overwritten");
9163 }
9164 p->value = value;
9165 p->origtype = origtype;
9166 return;
9167 }
9168 }
9169 }
9170 else
9171 {
9172 tree bitpos;
9173
9174 bitpos = bit_position (purpose);
9175 while (*q != NULL)
9176 {
9177 p = *q;
9178 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9179 q = &p->left;
9180 else if (p->purpose != purpose)
9181 q = &p->right;
9182 else
9183 {
9184 if (!implicit)
9185 {
9186 if (TREE_SIDE_EFFECTS (p->value))
9187 warning_init (loc, OPT_Woverride_init_side_effects,
9188 "initialized field with side-effects "
9189 "overwritten");
9190 else if (warn_override_init)
9191 warning_init (loc, OPT_Woverride_init,
9192 "initialized field overwritten");
9193 }
9194 p->value = value;
9195 p->origtype = origtype;
9196 return;
9197 }
9198 }
9199 }
9200
9201 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9202 sizeof (struct init_node));
9203 r->purpose = purpose;
9204 r->value = value;
9205 r->origtype = origtype;
9206
9207 *q = r;
9208 r->parent = p;
9209 r->left = 0;
9210 r->right = 0;
9211 r->balance = 0;
9212
9213 while (p)
9214 {
9215 struct init_node *s;
9216
9217 if (r == p->left)
9218 {
9219 if (p->balance == 0)
9220 p->balance = -1;
9221 else if (p->balance < 0)
9222 {
9223 if (r->balance < 0)
9224 {
9225 /* L rotation. */
9226 p->left = r->right;
9227 if (p->left)
9228 p->left->parent = p;
9229 r->right = p;
9230
9231 p->balance = 0;
9232 r->balance = 0;
9233
9234 s = p->parent;
9235 p->parent = r;
9236 r->parent = s;
9237 if (s)
9238 {
9239 if (s->left == p)
9240 s->left = r;
9241 else
9242 s->right = r;
9243 }
9244 else
9245 constructor_pending_elts = r;
9246 }
9247 else
9248 {
9249 /* LR rotation. */
9250 struct init_node *t = r->right;
9251
9252 r->right = t->left;
9253 if (r->right)
9254 r->right->parent = r;
9255 t->left = r;
9256
9257 p->left = t->right;
9258 if (p->left)
9259 p->left->parent = p;
9260 t->right = p;
9261
9262 p->balance = t->balance < 0;
9263 r->balance = -(t->balance > 0);
9264 t->balance = 0;
9265
9266 s = p->parent;
9267 p->parent = t;
9268 r->parent = t;
9269 t->parent = s;
9270 if (s)
9271 {
9272 if (s->left == p)
9273 s->left = t;
9274 else
9275 s->right = t;
9276 }
9277 else
9278 constructor_pending_elts = t;
9279 }
9280 break;
9281 }
9282 else
9283 {
9284 /* p->balance == +1; growth of left side balances the node. */
9285 p->balance = 0;
9286 break;
9287 }
9288 }
9289 else /* r == p->right */
9290 {
9291 if (p->balance == 0)
9292 /* Growth propagation from right side. */
9293 p->balance++;
9294 else if (p->balance > 0)
9295 {
9296 if (r->balance > 0)
9297 {
9298 /* R rotation. */
9299 p->right = r->left;
9300 if (p->right)
9301 p->right->parent = p;
9302 r->left = p;
9303
9304 p->balance = 0;
9305 r->balance = 0;
9306
9307 s = p->parent;
9308 p->parent = r;
9309 r->parent = s;
9310 if (s)
9311 {
9312 if (s->left == p)
9313 s->left = r;
9314 else
9315 s->right = r;
9316 }
9317 else
9318 constructor_pending_elts = r;
9319 }
9320 else /* r->balance == -1 */
9321 {
9322 /* RL rotation */
9323 struct init_node *t = r->left;
9324
9325 r->left = t->right;
9326 if (r->left)
9327 r->left->parent = r;
9328 t->right = r;
9329
9330 p->right = t->left;
9331 if (p->right)
9332 p->right->parent = p;
9333 t->left = p;
9334
9335 r->balance = (t->balance < 0);
9336 p->balance = -(t->balance > 0);
9337 t->balance = 0;
9338
9339 s = p->parent;
9340 p->parent = t;
9341 r->parent = t;
9342 t->parent = s;
9343 if (s)
9344 {
9345 if (s->left == p)
9346 s->left = t;
9347 else
9348 s->right = t;
9349 }
9350 else
9351 constructor_pending_elts = t;
9352 }
9353 break;
9354 }
9355 else
9356 {
9357 /* p->balance == -1; growth of right side balances the node. */
9358 p->balance = 0;
9359 break;
9360 }
9361 }
9362
9363 r = p;
9364 p = p->parent;
9365 }
9366 }
9367
9368 /* Build AVL tree from a sorted chain. */
9369
9370 static void
set_nonincremental_init(struct obstack * braced_init_obstack)9371 set_nonincremental_init (struct obstack * braced_init_obstack)
9372 {
9373 unsigned HOST_WIDE_INT ix;
9374 tree index, value;
9375
9376 if (TREE_CODE (constructor_type) != RECORD_TYPE
9377 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9378 return;
9379
9380 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9381 add_pending_init (input_location, index, value, NULL_TREE, true,
9382 braced_init_obstack);
9383 constructor_elements = NULL;
9384 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9385 {
9386 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9387 /* Skip any nameless bit fields at the beginning. */
9388 while (constructor_unfilled_fields != NULL_TREE
9389 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9390 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9391
9392 }
9393 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9394 {
9395 if (TYPE_DOMAIN (constructor_type))
9396 constructor_unfilled_index
9397 = convert (bitsizetype,
9398 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9399 else
9400 constructor_unfilled_index = bitsize_zero_node;
9401 }
9402 constructor_incremental = 0;
9403 }
9404
9405 /* Build AVL tree from a string constant. */
9406
9407 static void
set_nonincremental_init_from_string(tree str,struct obstack * braced_init_obstack)9408 set_nonincremental_init_from_string (tree str,
9409 struct obstack * braced_init_obstack)
9410 {
9411 tree value, purpose, type;
9412 HOST_WIDE_INT val[2];
9413 const char *p, *end;
9414 int byte, wchar_bytes, charwidth, bitpos;
9415
9416 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9417
9418 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9419 charwidth = TYPE_PRECISION (char_type_node);
9420 gcc_assert ((size_t) wchar_bytes * charwidth
9421 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9422 type = TREE_TYPE (constructor_type);
9423 p = TREE_STRING_POINTER (str);
9424 end = p + TREE_STRING_LENGTH (str);
9425
9426 for (purpose = bitsize_zero_node;
9427 p < end
9428 && !(constructor_max_index
9429 && tree_int_cst_lt (constructor_max_index, purpose));
9430 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9431 {
9432 if (wchar_bytes == 1)
9433 {
9434 val[0] = (unsigned char) *p++;
9435 val[1] = 0;
9436 }
9437 else
9438 {
9439 val[1] = 0;
9440 val[0] = 0;
9441 for (byte = 0; byte < wchar_bytes; byte++)
9442 {
9443 if (BYTES_BIG_ENDIAN)
9444 bitpos = (wchar_bytes - byte - 1) * charwidth;
9445 else
9446 bitpos = byte * charwidth;
9447 val[bitpos / HOST_BITS_PER_WIDE_INT]
9448 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9449 << (bitpos % HOST_BITS_PER_WIDE_INT);
9450 }
9451 }
9452
9453 if (!TYPE_UNSIGNED (type))
9454 {
9455 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9456 if (bitpos < HOST_BITS_PER_WIDE_INT)
9457 {
9458 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9459 {
9460 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9461 val[1] = -1;
9462 }
9463 }
9464 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9465 {
9466 if (val[0] < 0)
9467 val[1] = -1;
9468 }
9469 else if (val[1] & (HOST_WIDE_INT_1
9470 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9471 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9472 }
9473
9474 value = wide_int_to_tree (type,
9475 wide_int::from_array (val, 2,
9476 HOST_BITS_PER_WIDE_INT * 2));
9477 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9478 braced_init_obstack);
9479 }
9480
9481 constructor_incremental = 0;
9482 }
9483
9484 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9485 not initialized yet. */
9486
9487 static tree
find_init_member(tree field,struct obstack * braced_init_obstack)9488 find_init_member (tree field, struct obstack * braced_init_obstack)
9489 {
9490 struct init_node *p;
9491
9492 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9493 {
9494 if (constructor_incremental
9495 && tree_int_cst_lt (field, constructor_unfilled_index))
9496 set_nonincremental_init (braced_init_obstack);
9497
9498 p = constructor_pending_elts;
9499 while (p)
9500 {
9501 if (tree_int_cst_lt (field, p->purpose))
9502 p = p->left;
9503 else if (tree_int_cst_lt (p->purpose, field))
9504 p = p->right;
9505 else
9506 return p->value;
9507 }
9508 }
9509 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9510 {
9511 tree bitpos = bit_position (field);
9512
9513 if (constructor_incremental
9514 && (!constructor_unfilled_fields
9515 || tree_int_cst_lt (bitpos,
9516 bit_position (constructor_unfilled_fields))))
9517 set_nonincremental_init (braced_init_obstack);
9518
9519 p = constructor_pending_elts;
9520 while (p)
9521 {
9522 if (field == p->purpose)
9523 return p->value;
9524 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9525 p = p->left;
9526 else
9527 p = p->right;
9528 }
9529 }
9530 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9531 {
9532 if (!vec_safe_is_empty (constructor_elements)
9533 && (constructor_elements->last ().index == field))
9534 return constructor_elements->last ().value;
9535 }
9536 return NULL_TREE;
9537 }
9538
9539 /* "Output" the next constructor element.
9540 At top level, really output it to assembler code now.
9541 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9542 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9543 TYPE is the data type that the containing data type wants here.
9544 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9545 If VALUE is a string constant, STRICT_STRING is true if it is
9546 unparenthesized or we should not warn here for it being parenthesized.
9547 For other types of VALUE, STRICT_STRING is not used.
9548
9549 PENDING if true means output pending elements that belong
9550 right after this element. (PENDING is normally true;
9551 it is false while outputting pending elements, to avoid recursion.)
9552
9553 IMPLICIT is true if value comes from pop_init_level (1),
9554 the new initializer has been merged with the existing one
9555 and thus no warnings should be emitted about overriding an
9556 existing initializer. */
9557
9558 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)9559 output_init_element (location_t loc, tree value, tree origtype,
9560 bool strict_string, tree type, tree field, bool pending,
9561 bool implicit, struct obstack * braced_init_obstack)
9562 {
9563 tree semantic_type = NULL_TREE;
9564 bool maybe_const = true;
9565 bool npc;
9566
9567 if (type == error_mark_node || value == error_mark_node)
9568 {
9569 constructor_erroneous = 1;
9570 return;
9571 }
9572 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9573 && (TREE_CODE (value) == STRING_CST
9574 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9575 && !(TREE_CODE (value) == STRING_CST
9576 && TREE_CODE (type) == ARRAY_TYPE
9577 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9578 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9579 TYPE_MAIN_VARIANT (type)))
9580 value = array_to_pointer_conversion (input_location, value);
9581
9582 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9583 && require_constant_value && pending)
9584 {
9585 /* As an extension, allow initializing objects with static storage
9586 duration with compound literals (which are then treated just as
9587 the brace enclosed list they contain). */
9588 if (flag_isoc99)
9589 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9590 "constant");
9591 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9592 value = DECL_INITIAL (decl);
9593 }
9594
9595 npc = null_pointer_constant_p (value);
9596 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9597 {
9598 semantic_type = TREE_TYPE (value);
9599 value = TREE_OPERAND (value, 0);
9600 }
9601 value = c_fully_fold (value, require_constant_value, &maybe_const);
9602
9603 if (value == error_mark_node)
9604 constructor_erroneous = 1;
9605 else if (!TREE_CONSTANT (value))
9606 constructor_constant = 0;
9607 else if (!initializer_constant_valid_p (value,
9608 TREE_TYPE (value),
9609 AGGREGATE_TYPE_P (constructor_type)
9610 && TYPE_REVERSE_STORAGE_ORDER
9611 (constructor_type))
9612 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9613 && DECL_C_BIT_FIELD (field)
9614 && TREE_CODE (value) != INTEGER_CST))
9615 constructor_simple = 0;
9616 if (!maybe_const)
9617 constructor_nonconst = 1;
9618
9619 /* Digest the initializer and issue any errors about incompatible
9620 types before issuing errors about non-constant initializers. */
9621 tree new_value = value;
9622 if (semantic_type)
9623 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9624 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9625 require_constant_value);
9626 if (new_value == error_mark_node)
9627 {
9628 constructor_erroneous = 1;
9629 return;
9630 }
9631 if (require_constant_value || require_constant_elements)
9632 constant_expression_warning (new_value);
9633
9634 /* Proceed to check the constness of the original initializer. */
9635 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9636 {
9637 if (require_constant_value)
9638 {
9639 error_init (loc, "initializer element is not constant");
9640 value = error_mark_node;
9641 }
9642 else if (require_constant_elements)
9643 pedwarn (loc, OPT_Wpedantic,
9644 "initializer element is not computable at load time");
9645 }
9646 else if (!maybe_const
9647 && (require_constant_value || require_constant_elements))
9648 pedwarn_init (loc, OPT_Wpedantic,
9649 "initializer element is not a constant expression");
9650
9651 /* Issue -Wc++-compat warnings about initializing a bitfield with
9652 enum type. */
9653 if (warn_cxx_compat
9654 && field != NULL_TREE
9655 && TREE_CODE (field) == FIELD_DECL
9656 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9657 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9658 != TYPE_MAIN_VARIANT (type))
9659 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9660 {
9661 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9662 if (checktype != error_mark_node
9663 && (TYPE_MAIN_VARIANT (checktype)
9664 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9665 warning_init (loc, OPT_Wc___compat,
9666 "enum conversion in initialization is invalid in C++");
9667 }
9668
9669 /* If this field is empty and does not have side effects (and is not at
9670 the end of structure), don't do anything other than checking the
9671 initializer. */
9672 if (field
9673 && (TREE_TYPE (field) == error_mark_node
9674 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9675 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9676 && !TREE_SIDE_EFFECTS (new_value)
9677 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9678 || DECL_CHAIN (field)))))
9679 return;
9680
9681 /* Finally, set VALUE to the initializer value digested above. */
9682 value = new_value;
9683
9684 /* If this element doesn't come next in sequence,
9685 put it on constructor_pending_elts. */
9686 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9687 && (!constructor_incremental
9688 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9689 {
9690 if (constructor_incremental
9691 && tree_int_cst_lt (field, constructor_unfilled_index))
9692 set_nonincremental_init (braced_init_obstack);
9693
9694 add_pending_init (loc, field, value, origtype, implicit,
9695 braced_init_obstack);
9696 return;
9697 }
9698 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9699 && (!constructor_incremental
9700 || field != constructor_unfilled_fields))
9701 {
9702 /* We do this for records but not for unions. In a union,
9703 no matter which field is specified, it can be initialized
9704 right away since it starts at the beginning of the union. */
9705 if (constructor_incremental)
9706 {
9707 if (!constructor_unfilled_fields)
9708 set_nonincremental_init (braced_init_obstack);
9709 else
9710 {
9711 tree bitpos, unfillpos;
9712
9713 bitpos = bit_position (field);
9714 unfillpos = bit_position (constructor_unfilled_fields);
9715
9716 if (tree_int_cst_lt (bitpos, unfillpos))
9717 set_nonincremental_init (braced_init_obstack);
9718 }
9719 }
9720
9721 add_pending_init (loc, field, value, origtype, implicit,
9722 braced_init_obstack);
9723 return;
9724 }
9725 else if (TREE_CODE (constructor_type) == UNION_TYPE
9726 && !vec_safe_is_empty (constructor_elements))
9727 {
9728 if (!implicit)
9729 {
9730 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9731 warning_init (loc, OPT_Woverride_init_side_effects,
9732 "initialized field with side-effects overwritten");
9733 else if (warn_override_init)
9734 warning_init (loc, OPT_Woverride_init,
9735 "initialized field overwritten");
9736 }
9737
9738 /* We can have just one union field set. */
9739 constructor_elements = NULL;
9740 }
9741
9742 /* Otherwise, output this element either to
9743 constructor_elements or to the assembler file. */
9744
9745 constructor_elt celt = {field, value};
9746 vec_safe_push (constructor_elements, celt);
9747
9748 /* Advance the variable that indicates sequential elements output. */
9749 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9750 constructor_unfilled_index
9751 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9752 bitsize_one_node);
9753 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9754 {
9755 constructor_unfilled_fields
9756 = DECL_CHAIN (constructor_unfilled_fields);
9757
9758 /* Skip any nameless bit fields. */
9759 while (constructor_unfilled_fields != NULL_TREE
9760 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9761 constructor_unfilled_fields =
9762 DECL_CHAIN (constructor_unfilled_fields);
9763 }
9764 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9765 constructor_unfilled_fields = NULL_TREE;
9766
9767 /* Now output any pending elements which have become next. */
9768 if (pending)
9769 output_pending_init_elements (0, braced_init_obstack);
9770 }
9771
9772 /* For two FIELD_DECLs in the same chain, return -1 if field1
9773 comes before field2, 1 if field1 comes after field2 and
9774 0 if field1 == field2. */
9775
9776 static int
init_field_decl_cmp(tree field1,tree field2)9777 init_field_decl_cmp (tree field1, tree field2)
9778 {
9779 if (field1 == field2)
9780 return 0;
9781
9782 tree bitpos1 = bit_position (field1);
9783 tree bitpos2 = bit_position (field2);
9784 if (tree_int_cst_equal (bitpos1, bitpos2))
9785 {
9786 /* If one of the fields has non-zero bitsize, then that
9787 field must be the last one in a sequence of zero
9788 sized fields, fields after it will have bigger
9789 bit_position. */
9790 if (TREE_TYPE (field1) != error_mark_node
9791 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9792 && integer_nonzerop (TREE_TYPE (field1)))
9793 return 1;
9794 if (TREE_TYPE (field2) != error_mark_node
9795 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9796 && integer_nonzerop (TREE_TYPE (field2)))
9797 return -1;
9798 /* Otherwise, fallback to DECL_CHAIN walk to find out
9799 which field comes earlier. Walk chains of both
9800 fields, so that if field1 and field2 are close to each
9801 other in either order, it is found soon even for large
9802 sequences of zero sized fields. */
9803 tree f1 = field1, f2 = field2;
9804 while (1)
9805 {
9806 f1 = DECL_CHAIN (f1);
9807 f2 = DECL_CHAIN (f2);
9808 if (f1 == NULL_TREE)
9809 {
9810 gcc_assert (f2);
9811 return 1;
9812 }
9813 if (f2 == NULL_TREE)
9814 return -1;
9815 if (f1 == field2)
9816 return -1;
9817 if (f2 == field1)
9818 return 1;
9819 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9820 return 1;
9821 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9822 return -1;
9823 }
9824 }
9825 else if (tree_int_cst_lt (bitpos1, bitpos2))
9826 return -1;
9827 else
9828 return 1;
9829 }
9830
9831 /* Output any pending elements which have become next.
9832 As we output elements, constructor_unfilled_{fields,index}
9833 advances, which may cause other elements to become next;
9834 if so, they too are output.
9835
9836 If ALL is 0, we return when there are
9837 no more pending elements to output now.
9838
9839 If ALL is 1, we output space as necessary so that
9840 we can output all the pending elements. */
9841 static void
output_pending_init_elements(int all,struct obstack * braced_init_obstack)9842 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9843 {
9844 struct init_node *elt = constructor_pending_elts;
9845 tree next;
9846
9847 retry:
9848
9849 /* Look through the whole pending tree.
9850 If we find an element that should be output now,
9851 output it. Otherwise, set NEXT to the element
9852 that comes first among those still pending. */
9853
9854 next = NULL_TREE;
9855 while (elt)
9856 {
9857 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9858 {
9859 if (tree_int_cst_equal (elt->purpose,
9860 constructor_unfilled_index))
9861 output_init_element (input_location, elt->value, elt->origtype,
9862 true, TREE_TYPE (constructor_type),
9863 constructor_unfilled_index, false, false,
9864 braced_init_obstack);
9865 else if (tree_int_cst_lt (constructor_unfilled_index,
9866 elt->purpose))
9867 {
9868 /* Advance to the next smaller node. */
9869 if (elt->left)
9870 elt = elt->left;
9871 else
9872 {
9873 /* We have reached the smallest node bigger than the
9874 current unfilled index. Fill the space first. */
9875 next = elt->purpose;
9876 break;
9877 }
9878 }
9879 else
9880 {
9881 /* Advance to the next bigger node. */
9882 if (elt->right)
9883 elt = elt->right;
9884 else
9885 {
9886 /* We have reached the biggest node in a subtree. Find
9887 the parent of it, which is the next bigger node. */
9888 while (elt->parent && elt->parent->right == elt)
9889 elt = elt->parent;
9890 elt = elt->parent;
9891 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9892 elt->purpose))
9893 {
9894 next = elt->purpose;
9895 break;
9896 }
9897 }
9898 }
9899 }
9900 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9901 {
9902 /* If the current record is complete we are done. */
9903 if (constructor_unfilled_fields == NULL_TREE)
9904 break;
9905
9906 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9907 elt->purpose);
9908 if (cmp == 0)
9909 output_init_element (input_location, elt->value, elt->origtype,
9910 true, TREE_TYPE (elt->purpose),
9911 elt->purpose, false, false,
9912 braced_init_obstack);
9913 else if (cmp < 0)
9914 {
9915 /* Advance to the next smaller node. */
9916 if (elt->left)
9917 elt = elt->left;
9918 else
9919 {
9920 /* We have reached the smallest node bigger than the
9921 current unfilled field. Fill the space first. */
9922 next = elt->purpose;
9923 break;
9924 }
9925 }
9926 else
9927 {
9928 /* Advance to the next bigger node. */
9929 if (elt->right)
9930 elt = elt->right;
9931 else
9932 {
9933 /* We have reached the biggest node in a subtree. Find
9934 the parent of it, which is the next bigger node. */
9935 while (elt->parent && elt->parent->right == elt)
9936 elt = elt->parent;
9937 elt = elt->parent;
9938 if (elt
9939 && init_field_decl_cmp (constructor_unfilled_fields,
9940 elt->purpose) < 0)
9941 {
9942 next = elt->purpose;
9943 break;
9944 }
9945 }
9946 }
9947 }
9948 }
9949
9950 /* Ordinarily return, but not if we want to output all
9951 and there are elements left. */
9952 if (!(all && next != NULL_TREE))
9953 return;
9954
9955 /* If it's not incremental, just skip over the gap, so that after
9956 jumping to retry we will output the next successive element. */
9957 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9958 constructor_unfilled_fields = next;
9959 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9960 constructor_unfilled_index = next;
9961
9962 /* ELT now points to the node in the pending tree with the next
9963 initializer to output. */
9964 goto retry;
9965 }
9966
9967 /* Expression VALUE coincides with the start of type TYPE in a braced
9968 initializer. Return true if we should treat VALUE as initializing
9969 the first element of TYPE, false if we should treat it as initializing
9970 TYPE as a whole.
9971
9972 If the initializer is clearly invalid, the question becomes:
9973 which choice gives the best error message? */
9974
9975 static bool
initialize_elementwise_p(tree type,tree value)9976 initialize_elementwise_p (tree type, tree value)
9977 {
9978 if (type == error_mark_node || value == error_mark_node)
9979 return false;
9980
9981 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
9982
9983 tree value_type = TREE_TYPE (value);
9984 if (value_type == error_mark_node)
9985 return false;
9986
9987 /* GNU vectors can be initialized elementwise. However, treat any
9988 kind of vector value as initializing the vector type as a whole,
9989 regardless of whether the value is a GNU vector. Such initializers
9990 are valid if and only if they would have been valid in a non-braced
9991 initializer like:
9992
9993 TYPE foo = VALUE;
9994
9995 so recursing into the vector type would be at best confusing or at
9996 worst wrong. For example, when -flax-vector-conversions is in effect,
9997 it's possible to initialize a V8HI from a V4SI, even though the vectors
9998 have different element types and different numbers of elements. */
9999 if (gnu_vector_type_p (type))
10000 return !VECTOR_TYPE_P (value_type);
10001
10002 if (AGGREGATE_TYPE_P (type))
10003 return type != TYPE_MAIN_VARIANT (value_type);
10004
10005 return false;
10006 }
10007
10008 /* Add one non-braced element to the current constructor level.
10009 This adjusts the current position within the constructor's type.
10010 This may also start or terminate implicit levels
10011 to handle a partly-braced initializer.
10012
10013 Once this has found the correct level for the new element,
10014 it calls output_init_element.
10015
10016 IMPLICIT is true if value comes from pop_init_level (1),
10017 the new initializer has been merged with the existing one
10018 and thus no warnings should be emitted about overriding an
10019 existing initializer. */
10020
10021 void
process_init_element(location_t loc,struct c_expr value,bool implicit,struct obstack * braced_init_obstack)10022 process_init_element (location_t loc, struct c_expr value, bool implicit,
10023 struct obstack * braced_init_obstack)
10024 {
10025 tree orig_value = value.value;
10026 int string_flag
10027 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10028 bool strict_string = value.original_code == STRING_CST;
10029 bool was_designated = designator_depth != 0;
10030
10031 designator_depth = 0;
10032 designator_erroneous = 0;
10033
10034 if (!implicit && value.value && !integer_zerop (value.value))
10035 constructor_zeroinit = 0;
10036
10037 /* Handle superfluous braces around string cst as in
10038 char x[] = {"foo"}; */
10039 if (string_flag
10040 && constructor_type
10041 && !was_designated
10042 && TREE_CODE (constructor_type) == ARRAY_TYPE
10043 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10044 && integer_zerop (constructor_unfilled_index))
10045 {
10046 if (constructor_stack->replacement_value.value)
10047 error_init (loc, "excess elements in %<char%> array initializer");
10048 constructor_stack->replacement_value = value;
10049 return;
10050 }
10051
10052 if (constructor_stack->replacement_value.value != NULL_TREE)
10053 {
10054 error_init (loc, "excess elements in struct initializer");
10055 return;
10056 }
10057
10058 /* Ignore elements of a brace group if it is entirely superfluous
10059 and has already been diagnosed, or if the type is erroneous. */
10060 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10061 return;
10062
10063 /* Ignore elements of an initializer for a variable-size type.
10064 Those are diagnosed in digest_init. */
10065 if (COMPLETE_TYPE_P (constructor_type)
10066 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10067 return;
10068
10069 if (!implicit && warn_designated_init && !was_designated
10070 && TREE_CODE (constructor_type) == RECORD_TYPE
10071 && lookup_attribute ("designated_init",
10072 TYPE_ATTRIBUTES (constructor_type)))
10073 warning_init (loc,
10074 OPT_Wdesignated_init,
10075 "positional initialization of field "
10076 "in %<struct%> declared with %<designated_init%> attribute");
10077
10078 /* If we've exhausted any levels that didn't have braces,
10079 pop them now. */
10080 while (constructor_stack->implicit)
10081 {
10082 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10083 && constructor_fields == NULL_TREE)
10084 process_init_element (loc,
10085 pop_init_level (loc, 1, braced_init_obstack,
10086 last_init_list_comma),
10087 true, braced_init_obstack);
10088 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10089 || gnu_vector_type_p (constructor_type))
10090 && constructor_max_index
10091 && tree_int_cst_lt (constructor_max_index,
10092 constructor_index))
10093 process_init_element (loc,
10094 pop_init_level (loc, 1, braced_init_obstack,
10095 last_init_list_comma),
10096 true, braced_init_obstack);
10097 else
10098 break;
10099 }
10100
10101 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10102 if (constructor_range_stack)
10103 {
10104 /* If value is a compound literal and we'll be just using its
10105 content, don't put it into a SAVE_EXPR. */
10106 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10107 || !require_constant_value)
10108 {
10109 tree semantic_type = NULL_TREE;
10110 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10111 {
10112 semantic_type = TREE_TYPE (value.value);
10113 value.value = TREE_OPERAND (value.value, 0);
10114 }
10115 value.value = save_expr (value.value);
10116 if (semantic_type)
10117 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10118 value.value);
10119 }
10120 }
10121
10122 while (1)
10123 {
10124 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10125 {
10126 tree fieldtype;
10127 enum tree_code fieldcode;
10128
10129 if (constructor_fields == NULL_TREE)
10130 {
10131 pedwarn_init (loc, 0, "excess elements in struct initializer");
10132 break;
10133 }
10134
10135 fieldtype = TREE_TYPE (constructor_fields);
10136 if (fieldtype != error_mark_node)
10137 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10138 fieldcode = TREE_CODE (fieldtype);
10139
10140 /* Error for non-static initialization of a flexible array member. */
10141 if (fieldcode == ARRAY_TYPE
10142 && !require_constant_value
10143 && TYPE_SIZE (fieldtype) == NULL_TREE
10144 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10145 {
10146 error_init (loc, "non-static initialization of a flexible "
10147 "array member");
10148 break;
10149 }
10150
10151 /* Error for initialization of a flexible array member with
10152 a string constant if the structure is in an array. E.g.:
10153 struct S { int x; char y[]; };
10154 struct S s[] = { { 1, "foo" } };
10155 is invalid. */
10156 if (string_flag
10157 && fieldcode == ARRAY_TYPE
10158 && constructor_depth > 1
10159 && TYPE_SIZE (fieldtype) == NULL_TREE
10160 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10161 {
10162 bool in_array_p = false;
10163 for (struct constructor_stack *p = constructor_stack;
10164 p && p->type; p = p->next)
10165 if (TREE_CODE (p->type) == ARRAY_TYPE)
10166 {
10167 in_array_p = true;
10168 break;
10169 }
10170 if (in_array_p)
10171 {
10172 error_init (loc, "initialization of flexible array "
10173 "member in a nested context");
10174 break;
10175 }
10176 }
10177
10178 /* Accept a string constant to initialize a subarray. */
10179 if (value.value != NULL_TREE
10180 && fieldcode == ARRAY_TYPE
10181 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10182 && string_flag)
10183 value.value = orig_value;
10184 /* Otherwise, if we have come to a subaggregate,
10185 and we don't have an element of its type, push into it. */
10186 else if (value.value != NULL_TREE
10187 && initialize_elementwise_p (fieldtype, value.value))
10188 {
10189 push_init_level (loc, 1, braced_init_obstack);
10190 continue;
10191 }
10192
10193 if (value.value)
10194 {
10195 push_member_name (constructor_fields);
10196 output_init_element (loc, value.value, value.original_type,
10197 strict_string, fieldtype,
10198 constructor_fields, true, implicit,
10199 braced_init_obstack);
10200 RESTORE_SPELLING_DEPTH (constructor_depth);
10201 }
10202 else
10203 /* Do the bookkeeping for an element that was
10204 directly output as a constructor. */
10205 {
10206 /* For a record, keep track of end position of last field. */
10207 if (DECL_SIZE (constructor_fields))
10208 constructor_bit_index
10209 = size_binop_loc (input_location, PLUS_EXPR,
10210 bit_position (constructor_fields),
10211 DECL_SIZE (constructor_fields));
10212
10213 /* If the current field was the first one not yet written out,
10214 it isn't now, so update. */
10215 if (constructor_unfilled_fields == constructor_fields)
10216 {
10217 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10218 /* Skip any nameless bit fields. */
10219 while (constructor_unfilled_fields != 0
10220 && (DECL_UNNAMED_BIT_FIELD
10221 (constructor_unfilled_fields)))
10222 constructor_unfilled_fields =
10223 DECL_CHAIN (constructor_unfilled_fields);
10224 }
10225 }
10226
10227 constructor_fields = DECL_CHAIN (constructor_fields);
10228 /* Skip any nameless bit fields at the beginning. */
10229 while (constructor_fields != NULL_TREE
10230 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10231 constructor_fields = DECL_CHAIN (constructor_fields);
10232 }
10233 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10234 {
10235 tree fieldtype;
10236 enum tree_code fieldcode;
10237
10238 if (constructor_fields == NULL_TREE)
10239 {
10240 pedwarn_init (loc, 0,
10241 "excess elements in union initializer");
10242 break;
10243 }
10244
10245 fieldtype = TREE_TYPE (constructor_fields);
10246 if (fieldtype != error_mark_node)
10247 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10248 fieldcode = TREE_CODE (fieldtype);
10249
10250 /* Warn that traditional C rejects initialization of unions.
10251 We skip the warning if the value is zero. This is done
10252 under the assumption that the zero initializer in user
10253 code appears conditioned on e.g. __STDC__ to avoid
10254 "missing initializer" warnings and relies on default
10255 initialization to zero in the traditional C case.
10256 We also skip the warning if the initializer is designated,
10257 again on the assumption that this must be conditional on
10258 __STDC__ anyway (and we've already complained about the
10259 member-designator already). */
10260 if (!in_system_header_at (input_location) && !constructor_designated
10261 && !(value.value && (integer_zerop (value.value)
10262 || real_zerop (value.value))))
10263 warning (OPT_Wtraditional, "traditional C rejects initialization "
10264 "of unions");
10265
10266 /* Accept a string constant to initialize a subarray. */
10267 if (value.value != NULL_TREE
10268 && fieldcode == ARRAY_TYPE
10269 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10270 && string_flag)
10271 value.value = orig_value;
10272 /* Otherwise, if we have come to a subaggregate,
10273 and we don't have an element of its type, push into it. */
10274 else if (value.value != NULL_TREE
10275 && initialize_elementwise_p (fieldtype, value.value))
10276 {
10277 push_init_level (loc, 1, braced_init_obstack);
10278 continue;
10279 }
10280
10281 if (value.value)
10282 {
10283 push_member_name (constructor_fields);
10284 output_init_element (loc, value.value, value.original_type,
10285 strict_string, fieldtype,
10286 constructor_fields, true, implicit,
10287 braced_init_obstack);
10288 RESTORE_SPELLING_DEPTH (constructor_depth);
10289 }
10290 else
10291 /* Do the bookkeeping for an element that was
10292 directly output as a constructor. */
10293 {
10294 constructor_bit_index = DECL_SIZE (constructor_fields);
10295 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10296 }
10297
10298 constructor_fields = NULL_TREE;
10299 }
10300 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10301 {
10302 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10303 enum tree_code eltcode = TREE_CODE (elttype);
10304
10305 /* Accept a string constant to initialize a subarray. */
10306 if (value.value != NULL_TREE
10307 && eltcode == ARRAY_TYPE
10308 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10309 && string_flag)
10310 value.value = orig_value;
10311 /* Otherwise, if we have come to a subaggregate,
10312 and we don't have an element of its type, push into it. */
10313 else if (value.value != NULL_TREE
10314 && initialize_elementwise_p (elttype, value.value))
10315 {
10316 push_init_level (loc, 1, braced_init_obstack);
10317 continue;
10318 }
10319
10320 if (constructor_max_index != NULL_TREE
10321 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10322 || integer_all_onesp (constructor_max_index)))
10323 {
10324 pedwarn_init (loc, 0,
10325 "excess elements in array initializer");
10326 break;
10327 }
10328
10329 /* Now output the actual element. */
10330 if (value.value)
10331 {
10332 push_array_bounds (tree_to_uhwi (constructor_index));
10333 output_init_element (loc, value.value, value.original_type,
10334 strict_string, elttype,
10335 constructor_index, true, implicit,
10336 braced_init_obstack);
10337 RESTORE_SPELLING_DEPTH (constructor_depth);
10338 }
10339
10340 constructor_index
10341 = size_binop_loc (input_location, PLUS_EXPR,
10342 constructor_index, bitsize_one_node);
10343
10344 if (!value.value)
10345 /* If we are doing the bookkeeping for an element that was
10346 directly output as a constructor, we must update
10347 constructor_unfilled_index. */
10348 constructor_unfilled_index = constructor_index;
10349 }
10350 else if (gnu_vector_type_p (constructor_type))
10351 {
10352 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10353
10354 /* Do a basic check of initializer size. Note that vectors
10355 always have a fixed size derived from their type. */
10356 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10357 {
10358 pedwarn_init (loc, 0,
10359 "excess elements in vector initializer");
10360 break;
10361 }
10362
10363 /* Now output the actual element. */
10364 if (value.value)
10365 {
10366 if (TREE_CODE (value.value) == VECTOR_CST)
10367 elttype = TYPE_MAIN_VARIANT (constructor_type);
10368 output_init_element (loc, value.value, value.original_type,
10369 strict_string, elttype,
10370 constructor_index, true, implicit,
10371 braced_init_obstack);
10372 }
10373
10374 constructor_index
10375 = size_binop_loc (input_location,
10376 PLUS_EXPR, constructor_index, bitsize_one_node);
10377
10378 if (!value.value)
10379 /* If we are doing the bookkeeping for an element that was
10380 directly output as a constructor, we must update
10381 constructor_unfilled_index. */
10382 constructor_unfilled_index = constructor_index;
10383 }
10384
10385 /* Handle the sole element allowed in a braced initializer
10386 for a scalar variable. */
10387 else if (constructor_type != error_mark_node
10388 && constructor_fields == NULL_TREE)
10389 {
10390 pedwarn_init (loc, 0,
10391 "excess elements in scalar initializer");
10392 break;
10393 }
10394 else
10395 {
10396 if (value.value)
10397 output_init_element (loc, value.value, value.original_type,
10398 strict_string, constructor_type,
10399 NULL_TREE, true, implicit,
10400 braced_init_obstack);
10401 constructor_fields = NULL_TREE;
10402 }
10403
10404 /* Handle range initializers either at this level or anywhere higher
10405 in the designator stack. */
10406 if (constructor_range_stack)
10407 {
10408 struct constructor_range_stack *p, *range_stack;
10409 int finish = 0;
10410
10411 range_stack = constructor_range_stack;
10412 constructor_range_stack = 0;
10413 while (constructor_stack != range_stack->stack)
10414 {
10415 gcc_assert (constructor_stack->implicit);
10416 process_init_element (loc,
10417 pop_init_level (loc, 1,
10418 braced_init_obstack,
10419 last_init_list_comma),
10420 true, braced_init_obstack);
10421 }
10422 for (p = range_stack;
10423 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10424 p = p->prev)
10425 {
10426 gcc_assert (constructor_stack->implicit);
10427 process_init_element (loc,
10428 pop_init_level (loc, 1,
10429 braced_init_obstack,
10430 last_init_list_comma),
10431 true, braced_init_obstack);
10432 }
10433
10434 p->index = size_binop_loc (input_location,
10435 PLUS_EXPR, p->index, bitsize_one_node);
10436 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10437 finish = 1;
10438
10439 while (1)
10440 {
10441 constructor_index = p->index;
10442 constructor_fields = p->fields;
10443 if (finish && p->range_end && p->index == p->range_start)
10444 {
10445 finish = 0;
10446 p->prev = 0;
10447 }
10448 p = p->next;
10449 if (!p)
10450 break;
10451 finish_implicit_inits (loc, braced_init_obstack);
10452 push_init_level (loc, 2, braced_init_obstack);
10453 p->stack = constructor_stack;
10454 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10455 p->index = p->range_start;
10456 }
10457
10458 if (!finish)
10459 constructor_range_stack = range_stack;
10460 continue;
10461 }
10462
10463 break;
10464 }
10465
10466 constructor_range_stack = 0;
10467 }
10468
10469 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10470 (guaranteed to be 'volatile' or null) and ARGS (represented using
10471 an ASM_EXPR node). */
10472 tree
build_asm_stmt(bool is_volatile,tree args)10473 build_asm_stmt (bool is_volatile, tree args)
10474 {
10475 if (is_volatile)
10476 ASM_VOLATILE_P (args) = 1;
10477 return add_stmt (args);
10478 }
10479
10480 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10481 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10482 SIMPLE indicates whether there was anything at all after the
10483 string in the asm expression -- asm("blah") and asm("blah" : )
10484 are subtly different. We use a ASM_EXPR node to represent this.
10485 LOC is the location of the asm, and IS_INLINE says whether this
10486 is asm inline. */
10487 tree
build_asm_expr(location_t loc,tree string,tree outputs,tree inputs,tree clobbers,tree labels,bool simple,bool is_inline)10488 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10489 tree clobbers, tree labels, bool simple, bool is_inline)
10490 {
10491 tree tail;
10492 tree args;
10493 int i;
10494 const char *constraint;
10495 const char **oconstraints;
10496 bool allows_mem, allows_reg, is_inout;
10497 int ninputs, noutputs;
10498
10499 ninputs = list_length (inputs);
10500 noutputs = list_length (outputs);
10501 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10502
10503 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10504
10505 /* Remove output conversions that change the type but not the mode. */
10506 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10507 {
10508 tree output = TREE_VALUE (tail);
10509
10510 output = c_fully_fold (output, false, NULL, true);
10511
10512 /* ??? Really, this should not be here. Users should be using a
10513 proper lvalue, dammit. But there's a long history of using casts
10514 in the output operands. In cases like longlong.h, this becomes a
10515 primitive form of typechecking -- if the cast can be removed, then
10516 the output operand had a type of the proper width; otherwise we'll
10517 get an error. Gross, but ... */
10518 STRIP_NOPS (output);
10519
10520 if (!lvalue_or_else (loc, output, lv_asm))
10521 output = error_mark_node;
10522
10523 if (output != error_mark_node
10524 && (TREE_READONLY (output)
10525 || TYPE_READONLY (TREE_TYPE (output))
10526 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10527 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10528 readonly_error (loc, output, lv_asm);
10529
10530 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10531 oconstraints[i] = constraint;
10532
10533 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10534 &allows_mem, &allows_reg, &is_inout))
10535 {
10536 /* If the operand is going to end up in memory,
10537 mark it addressable. */
10538 if (!allows_reg && !c_mark_addressable (output))
10539 output = error_mark_node;
10540 if (!(!allows_reg && allows_mem)
10541 && output != error_mark_node
10542 && VOID_TYPE_P (TREE_TYPE (output)))
10543 {
10544 error_at (loc, "invalid use of void expression");
10545 output = error_mark_node;
10546 }
10547 }
10548 else
10549 output = error_mark_node;
10550
10551 TREE_VALUE (tail) = output;
10552 }
10553
10554 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10555 {
10556 tree input;
10557
10558 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10559 input = TREE_VALUE (tail);
10560
10561 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10562 oconstraints, &allows_mem, &allows_reg))
10563 {
10564 /* If the operand is going to end up in memory,
10565 mark it addressable. */
10566 if (!allows_reg && allows_mem)
10567 {
10568 input = c_fully_fold (input, false, NULL, true);
10569
10570 /* Strip the nops as we allow this case. FIXME, this really
10571 should be rejected or made deprecated. */
10572 STRIP_NOPS (input);
10573 if (!c_mark_addressable (input))
10574 input = error_mark_node;
10575 }
10576 else
10577 {
10578 struct c_expr expr;
10579 memset (&expr, 0, sizeof (expr));
10580 expr.value = input;
10581 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10582 input = c_fully_fold (expr.value, false, NULL);
10583
10584 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10585 {
10586 error_at (loc, "invalid use of void expression");
10587 input = error_mark_node;
10588 }
10589 }
10590 }
10591 else
10592 input = error_mark_node;
10593
10594 TREE_VALUE (tail) = input;
10595 }
10596
10597 /* ASMs with labels cannot have outputs. This should have been
10598 enforced by the parser. */
10599 gcc_assert (outputs == NULL || labels == NULL);
10600
10601 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10602
10603 /* asm statements without outputs, including simple ones, are treated
10604 as volatile. */
10605 ASM_INPUT_P (args) = simple;
10606 ASM_VOLATILE_P (args) = (noutputs == 0);
10607 ASM_INLINE_P (args) = is_inline;
10608
10609 return args;
10610 }
10611
10612 /* Generate a goto statement to LABEL. LOC is the location of the
10613 GOTO. */
10614
10615 tree
c_finish_goto_label(location_t loc,tree label)10616 c_finish_goto_label (location_t loc, tree label)
10617 {
10618 tree decl = lookup_label_for_goto (loc, label);
10619 if (!decl)
10620 return NULL_TREE;
10621 TREE_USED (decl) = 1;
10622 {
10623 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10624 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10625 SET_EXPR_LOCATION (t, loc);
10626 return add_stmt (t);
10627 }
10628 }
10629
10630 /* Generate a computed goto statement to EXPR. LOC is the location of
10631 the GOTO. */
10632
10633 tree
c_finish_goto_ptr(location_t loc,tree expr)10634 c_finish_goto_ptr (location_t loc, tree expr)
10635 {
10636 tree t;
10637 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10638 expr = c_fully_fold (expr, false, NULL);
10639 expr = convert (ptr_type_node, expr);
10640 t = build1 (GOTO_EXPR, void_type_node, expr);
10641 SET_EXPR_LOCATION (t, loc);
10642 return add_stmt (t);
10643 }
10644
10645 /* Generate a C `return' statement. RETVAL is the expression for what
10646 to return, or a null pointer for `return;' with no value. LOC is
10647 the location of the return statement, or the location of the expression,
10648 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10649 is the original type of RETVAL. */
10650
10651 tree
c_finish_return(location_t loc,tree retval,tree origtype)10652 c_finish_return (location_t loc, tree retval, tree origtype)
10653 {
10654 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10655 bool no_warning = false;
10656 bool npc = false;
10657
10658 /* Use the expansion point to handle cases such as returning NULL
10659 in a function returning void. */
10660 location_t xloc = expansion_point_location_if_in_system_header (loc);
10661
10662 if (TREE_THIS_VOLATILE (current_function_decl))
10663 warning_at (xloc, 0,
10664 "function declared %<noreturn%> has a %<return%> statement");
10665
10666 if (retval)
10667 {
10668 tree semantic_type = NULL_TREE;
10669 npc = null_pointer_constant_p (retval);
10670 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10671 {
10672 semantic_type = TREE_TYPE (retval);
10673 retval = TREE_OPERAND (retval, 0);
10674 }
10675 retval = c_fully_fold (retval, false, NULL);
10676 if (semantic_type
10677 && valtype != NULL_TREE
10678 && TREE_CODE (valtype) != VOID_TYPE)
10679 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10680 }
10681
10682 if (!retval)
10683 {
10684 current_function_returns_null = 1;
10685 if ((warn_return_type >= 0 || flag_isoc99)
10686 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10687 {
10688 bool warned_here;
10689 if (flag_isoc99)
10690 warned_here = pedwarn
10691 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10692 "%<return%> with no value, in function returning non-void");
10693 else
10694 warned_here = warning_at
10695 (loc, OPT_Wreturn_type,
10696 "%<return%> with no value, in function returning non-void");
10697 no_warning = true;
10698 if (warned_here)
10699 inform (DECL_SOURCE_LOCATION (current_function_decl),
10700 "declared here");
10701 }
10702 }
10703 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10704 {
10705 current_function_returns_null = 1;
10706 bool warned_here;
10707 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10708 warned_here = pedwarn
10709 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10710 "%<return%> with a value, in function returning void");
10711 else
10712 warned_here = pedwarn
10713 (xloc, OPT_Wpedantic, "ISO C forbids "
10714 "%<return%> with expression, in function returning void");
10715 if (warned_here)
10716 inform (DECL_SOURCE_LOCATION (current_function_decl),
10717 "declared here");
10718 }
10719 else
10720 {
10721 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10722 retval, origtype, ic_return,
10723 npc, NULL_TREE, NULL_TREE, 0);
10724 tree res = DECL_RESULT (current_function_decl);
10725 tree inner;
10726 bool save;
10727
10728 current_function_returns_value = 1;
10729 if (t == error_mark_node)
10730 return NULL_TREE;
10731
10732 save = in_late_binary_op;
10733 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10734 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10735 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10736 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10737 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10738 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10739 in_late_binary_op = true;
10740 inner = t = convert (TREE_TYPE (res), t);
10741 in_late_binary_op = save;
10742
10743 /* Strip any conversions, additions, and subtractions, and see if
10744 we are returning the address of a local variable. Warn if so. */
10745 while (1)
10746 {
10747 switch (TREE_CODE (inner))
10748 {
10749 CASE_CONVERT:
10750 case NON_LVALUE_EXPR:
10751 case PLUS_EXPR:
10752 case POINTER_PLUS_EXPR:
10753 inner = TREE_OPERAND (inner, 0);
10754 continue;
10755
10756 case MINUS_EXPR:
10757 /* If the second operand of the MINUS_EXPR has a pointer
10758 type (or is converted from it), this may be valid, so
10759 don't give a warning. */
10760 {
10761 tree op1 = TREE_OPERAND (inner, 1);
10762
10763 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10764 && (CONVERT_EXPR_P (op1)
10765 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10766 op1 = TREE_OPERAND (op1, 0);
10767
10768 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10769 break;
10770
10771 inner = TREE_OPERAND (inner, 0);
10772 continue;
10773 }
10774
10775 case ADDR_EXPR:
10776 inner = TREE_OPERAND (inner, 0);
10777
10778 while (REFERENCE_CLASS_P (inner)
10779 && !INDIRECT_REF_P (inner))
10780 inner = TREE_OPERAND (inner, 0);
10781
10782 if (DECL_P (inner)
10783 && !DECL_EXTERNAL (inner)
10784 && !TREE_STATIC (inner)
10785 && DECL_CONTEXT (inner) == current_function_decl
10786 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
10787 {
10788 if (TREE_CODE (inner) == LABEL_DECL)
10789 warning_at (loc, OPT_Wreturn_local_addr,
10790 "function returns address of label");
10791 else
10792 {
10793 warning_at (loc, OPT_Wreturn_local_addr,
10794 "function returns address of local variable");
10795 tree zero = build_zero_cst (TREE_TYPE (res));
10796 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10797 }
10798 }
10799 break;
10800
10801 default:
10802 break;
10803 }
10804
10805 break;
10806 }
10807
10808 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10809 SET_EXPR_LOCATION (retval, loc);
10810
10811 if (warn_sequence_point)
10812 verify_sequence_points (retval);
10813 }
10814
10815 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10816 TREE_NO_WARNING (ret_stmt) |= no_warning;
10817 return add_stmt (ret_stmt);
10818 }
10819
10820 struct c_switch {
10821 /* The SWITCH_EXPR being built. */
10822 tree switch_expr;
10823
10824 /* The original type of the testing expression, i.e. before the
10825 default conversion is applied. */
10826 tree orig_type;
10827
10828 /* A splay-tree mapping the low element of a case range to the high
10829 element, or NULL_TREE if there is no high element. Used to
10830 determine whether or not a new case label duplicates an old case
10831 label. We need a tree, rather than simply a hash table, because
10832 of the GNU case range extension. */
10833 splay_tree cases;
10834
10835 /* The bindings at the point of the switch. This is used for
10836 warnings crossing decls when branching to a case label. */
10837 struct c_spot_bindings *bindings;
10838
10839 /* The next node on the stack. */
10840 struct c_switch *next;
10841
10842 /* Remember whether the controlling expression had boolean type
10843 before integer promotions for the sake of -Wswitch-bool. */
10844 bool bool_cond_p;
10845 };
10846
10847 /* A stack of the currently active switch statements. The innermost
10848 switch statement is on the top of the stack. There is no need to
10849 mark the stack for garbage collection because it is only active
10850 during the processing of the body of a function, and we never
10851 collect at that point. */
10852
10853 struct c_switch *c_switch_stack;
10854
10855 /* Start a C switch statement, testing expression EXP. Return the new
10856 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10857 SWITCH_COND_LOC is the location of the switch's condition.
10858 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10859
10860 tree
c_start_case(location_t switch_loc,location_t switch_cond_loc,tree exp,bool explicit_cast_p)10861 c_start_case (location_t switch_loc,
10862 location_t switch_cond_loc,
10863 tree exp, bool explicit_cast_p)
10864 {
10865 tree orig_type = error_mark_node;
10866 bool bool_cond_p = false;
10867 struct c_switch *cs;
10868
10869 if (exp != error_mark_node)
10870 {
10871 orig_type = TREE_TYPE (exp);
10872
10873 if (!INTEGRAL_TYPE_P (orig_type))
10874 {
10875 if (orig_type != error_mark_node)
10876 {
10877 error_at (switch_cond_loc, "switch quantity not an integer");
10878 orig_type = error_mark_node;
10879 }
10880 exp = integer_zero_node;
10881 }
10882 else
10883 {
10884 tree type = TYPE_MAIN_VARIANT (orig_type);
10885 tree e = exp;
10886
10887 /* Warn if the condition has boolean value. */
10888 while (TREE_CODE (e) == COMPOUND_EXPR)
10889 e = TREE_OPERAND (e, 1);
10890
10891 if ((TREE_CODE (type) == BOOLEAN_TYPE
10892 || truth_value_p (TREE_CODE (e)))
10893 /* Explicit cast to int suppresses this warning. */
10894 && !(TREE_CODE (type) == INTEGER_TYPE
10895 && explicit_cast_p))
10896 bool_cond_p = true;
10897
10898 if (!in_system_header_at (input_location)
10899 && (type == long_integer_type_node
10900 || type == long_unsigned_type_node))
10901 warning_at (switch_cond_loc,
10902 OPT_Wtraditional, "%<long%> switch expression not "
10903 "converted to %<int%> in ISO C");
10904
10905 exp = c_fully_fold (exp, false, NULL);
10906 exp = default_conversion (exp);
10907
10908 if (warn_sequence_point)
10909 verify_sequence_points (exp);
10910 }
10911 }
10912
10913 /* Add this new SWITCH_EXPR to the stack. */
10914 cs = XNEW (struct c_switch);
10915 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10916 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10917 cs->orig_type = orig_type;
10918 cs->cases = splay_tree_new (case_compare, NULL, NULL);
10919 cs->bindings = c_get_switch_bindings ();
10920 cs->bool_cond_p = bool_cond_p;
10921 cs->next = c_switch_stack;
10922 c_switch_stack = cs;
10923
10924 return add_stmt (cs->switch_expr);
10925 }
10926
10927 /* Process a case label at location LOC. */
10928
10929 tree
do_case(location_t loc,tree low_value,tree high_value)10930 do_case (location_t loc, tree low_value, tree high_value)
10931 {
10932 tree label = NULL_TREE;
10933
10934 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10935 {
10936 low_value = c_fully_fold (low_value, false, NULL);
10937 if (TREE_CODE (low_value) == INTEGER_CST)
10938 pedwarn (loc, OPT_Wpedantic,
10939 "case label is not an integer constant expression");
10940 }
10941
10942 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10943 {
10944 high_value = c_fully_fold (high_value, false, NULL);
10945 if (TREE_CODE (high_value) == INTEGER_CST)
10946 pedwarn (input_location, OPT_Wpedantic,
10947 "case label is not an integer constant expression");
10948 }
10949
10950 if (c_switch_stack == NULL)
10951 {
10952 if (low_value)
10953 error_at (loc, "case label not within a switch statement");
10954 else
10955 error_at (loc, "%<default%> label not within a switch statement");
10956 return NULL_TREE;
10957 }
10958
10959 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10960 EXPR_LOCATION (c_switch_stack->switch_expr),
10961 loc))
10962 return NULL_TREE;
10963
10964 label = c_add_case_label (loc, c_switch_stack->cases,
10965 SWITCH_COND (c_switch_stack->switch_expr),
10966 low_value, high_value);
10967 if (label == error_mark_node)
10968 label = NULL_TREE;
10969 return label;
10970 }
10971
10972 /* Finish the switch statement. TYPE is the original type of the
10973 controlling expression of the switch, or NULL_TREE. */
10974
10975 void
c_finish_case(tree body,tree type)10976 c_finish_case (tree body, tree type)
10977 {
10978 struct c_switch *cs = c_switch_stack;
10979 location_t switch_location;
10980
10981 SWITCH_BODY (cs->switch_expr) = body;
10982
10983 /* Emit warnings as needed. */
10984 switch_location = EXPR_LOCATION (cs->switch_expr);
10985 c_do_switch_warnings (cs->cases, switch_location,
10986 type ? type : TREE_TYPE (cs->switch_expr),
10987 SWITCH_COND (cs->switch_expr), cs->bool_cond_p);
10988 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10989 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10990
10991 /* Pop the stack. */
10992 c_switch_stack = cs->next;
10993 splay_tree_delete (cs->cases);
10994 c_release_switch_bindings (cs->bindings);
10995 XDELETE (cs);
10996 }
10997
10998 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10999 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11000 may be null. */
11001
11002 void
c_finish_if_stmt(location_t if_locus,tree cond,tree then_block,tree else_block)11003 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11004 tree else_block)
11005 {
11006 tree stmt;
11007
11008 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11009 SET_EXPR_LOCATION (stmt, if_locus);
11010 add_stmt (stmt);
11011 }
11012
11013 /* Emit a general-purpose loop construct. START_LOCUS is the location of
11014 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
11015 is false for DO loops. INCR is the FOR increment expression. BODY is
11016 the statement controlled by the loop. BLAB is the break label. CLAB is
11017 the continue label. Everything is allowed to be NULL.
11018 COND_LOCUS is the location of the loop condition, INCR_LOCUS is the
11019 location of the FOR increment expression. */
11020
11021 void
c_finish_loop(location_t start_locus,location_t cond_locus,tree cond,location_t incr_locus,tree incr,tree body,tree blab,tree clab,bool cond_is_first)11022 c_finish_loop (location_t start_locus, location_t cond_locus, tree cond,
11023 location_t incr_locus, tree incr, tree body, tree blab,
11024 tree clab, bool cond_is_first)
11025 {
11026 tree entry = NULL, exit = NULL, t;
11027
11028 /* If the condition is zero don't generate a loop construct. */
11029 if (cond && integer_zerop (cond))
11030 {
11031 if (cond_is_first)
11032 {
11033 t = build_and_jump (&blab);
11034 SET_EXPR_LOCATION (t, start_locus);
11035 add_stmt (t);
11036 }
11037 }
11038 else
11039 {
11040 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
11041
11042 /* If we have an exit condition, then we build an IF with gotos either
11043 out of the loop, or to the top of it. If there's no exit condition,
11044 then we just build a jump back to the top. */
11045 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
11046
11047 if (cond && !integer_nonzerop (cond))
11048 {
11049 /* Canonicalize the loop condition to the end. This means
11050 generating a branch to the loop condition. Reuse the
11051 continue label, if possible. */
11052 if (cond_is_first)
11053 {
11054 if (incr || !clab)
11055 {
11056 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
11057 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
11058 }
11059 else
11060 t = build1 (GOTO_EXPR, void_type_node, clab);
11061 SET_EXPR_LOCATION (t, start_locus);
11062 add_stmt (t);
11063 }
11064
11065 t = build_and_jump (&blab);
11066 exit = fold_build3_loc (cond_is_first ? start_locus : input_location,
11067 COND_EXPR, void_type_node, cond, exit, t);
11068 }
11069 else
11070 {
11071 /* For the backward-goto's location of an unconditional loop
11072 use the beginning of the body, or, if there is none, the
11073 top of the loop. */
11074 location_t loc = EXPR_LOCATION (expr_first (body));
11075 if (loc == UNKNOWN_LOCATION)
11076 loc = start_locus;
11077 SET_EXPR_LOCATION (exit, loc);
11078 }
11079
11080 add_stmt (top);
11081 }
11082
11083 if (body)
11084 add_stmt (body);
11085 if (clab)
11086 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
11087 if (incr)
11088 {
11089 if (MAY_HAVE_DEBUG_MARKER_STMTS && incr_locus != UNKNOWN_LOCATION)
11090 {
11091 t = build0 (DEBUG_BEGIN_STMT, void_type_node);
11092 SET_EXPR_LOCATION (t, incr_locus);
11093 add_stmt (t);
11094 }
11095 add_stmt (incr);
11096 }
11097 if (entry)
11098 add_stmt (entry);
11099 if (MAY_HAVE_DEBUG_MARKER_STMTS && cond_locus != UNKNOWN_LOCATION)
11100 {
11101 t = build0 (DEBUG_BEGIN_STMT, void_type_node);
11102 SET_EXPR_LOCATION (t, cond_locus);
11103 add_stmt (t);
11104 }
11105 if (exit)
11106 add_stmt (exit);
11107 if (blab)
11108 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
11109 }
11110
11111 tree
c_finish_bc_stmt(location_t loc,tree * label_p,bool is_break)11112 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
11113 {
11114 bool skip;
11115 tree label = *label_p;
11116
11117 /* In switch statements break is sometimes stylistically used after
11118 a return statement. This can lead to spurious warnings about
11119 control reaching the end of a non-void function when it is
11120 inlined. Note that we are calling block_may_fallthru with
11121 language specific tree nodes; this works because
11122 block_may_fallthru returns true when given something it does not
11123 understand. */
11124 skip = !block_may_fallthru (cur_stmt_list);
11125
11126 if (!label)
11127 {
11128 if (!skip)
11129 *label_p = label = create_artificial_label (loc);
11130 }
11131 else if (TREE_CODE (label) == LABEL_DECL)
11132 ;
11133 else switch (TREE_INT_CST_LOW (label))
11134 {
11135 case 0:
11136 if (is_break)
11137 error_at (loc, "break statement not within loop or switch");
11138 else
11139 error_at (loc, "continue statement not within a loop");
11140 return NULL_TREE;
11141
11142 case 1:
11143 gcc_assert (is_break);
11144 error_at (loc, "break statement used with OpenMP for loop");
11145 return NULL_TREE;
11146
11147 case 2:
11148 if (is_break)
11149 error ("break statement within %<#pragma simd%> loop body");
11150 else
11151 error ("continue statement within %<#pragma simd%> loop body");
11152 return NULL_TREE;
11153
11154 default:
11155 gcc_unreachable ();
11156 }
11157
11158 if (skip)
11159 return NULL_TREE;
11160
11161 if (!is_break)
11162 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
11163
11164 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
11165 }
11166
11167 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11168
11169 static void
emit_side_effect_warnings(location_t loc,tree expr)11170 emit_side_effect_warnings (location_t loc, tree expr)
11171 {
11172 if (expr == error_mark_node)
11173 ;
11174 else if (!TREE_SIDE_EFFECTS (expr))
11175 {
11176 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
11177 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11178 }
11179 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11180 {
11181 tree r = expr;
11182 location_t cloc = loc;
11183 while (TREE_CODE (r) == COMPOUND_EXPR)
11184 {
11185 if (EXPR_HAS_LOCATION (r))
11186 cloc = EXPR_LOCATION (r);
11187 r = TREE_OPERAND (r, 1);
11188 }
11189 if (!TREE_SIDE_EFFECTS (r)
11190 && !VOID_TYPE_P (TREE_TYPE (r))
11191 && !CONVERT_EXPR_P (r)
11192 && !TREE_NO_WARNING (r)
11193 && !TREE_NO_WARNING (expr))
11194 warning_at (cloc, OPT_Wunused_value,
11195 "right-hand operand of comma expression has no effect");
11196 }
11197 else
11198 warn_if_unused_value (expr, loc);
11199 }
11200
11201 /* Process an expression as if it were a complete statement. Emit
11202 diagnostics, but do not call ADD_STMT. LOC is the location of the
11203 statement. */
11204
11205 tree
c_process_expr_stmt(location_t loc,tree expr)11206 c_process_expr_stmt (location_t loc, tree expr)
11207 {
11208 tree exprv;
11209
11210 if (!expr)
11211 return NULL_TREE;
11212
11213 expr = c_fully_fold (expr, false, NULL);
11214
11215 if (warn_sequence_point)
11216 verify_sequence_points (expr);
11217
11218 if (TREE_TYPE (expr) != error_mark_node
11219 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11220 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11221 error_at (loc, "expression statement has incomplete type");
11222
11223 /* If we're not processing a statement expression, warn about unused values.
11224 Warnings for statement expressions will be emitted later, once we figure
11225 out which is the result. */
11226 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11227 && warn_unused_value)
11228 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11229
11230 exprv = expr;
11231 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11232 exprv = TREE_OPERAND (exprv, 1);
11233 while (CONVERT_EXPR_P (exprv))
11234 exprv = TREE_OPERAND (exprv, 0);
11235 if (DECL_P (exprv)
11236 || handled_component_p (exprv)
11237 || TREE_CODE (exprv) == ADDR_EXPR)
11238 mark_exp_read (exprv);
11239
11240 /* If the expression is not of a type to which we cannot assign a line
11241 number, wrap the thing in a no-op NOP_EXPR. */
11242 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11243 {
11244 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11245 SET_EXPR_LOCATION (expr, loc);
11246 }
11247
11248 return expr;
11249 }
11250
11251 /* Emit an expression as a statement. LOC is the location of the
11252 expression. */
11253
11254 tree
c_finish_expr_stmt(location_t loc,tree expr)11255 c_finish_expr_stmt (location_t loc, tree expr)
11256 {
11257 if (expr)
11258 return add_stmt (c_process_expr_stmt (loc, expr));
11259 else
11260 return NULL;
11261 }
11262
11263 /* Do the opposite and emit a statement as an expression. To begin,
11264 create a new binding level and return it. */
11265
11266 tree
c_begin_stmt_expr(void)11267 c_begin_stmt_expr (void)
11268 {
11269 tree ret;
11270
11271 /* We must force a BLOCK for this level so that, if it is not expanded
11272 later, there is a way to turn off the entire subtree of blocks that
11273 are contained in it. */
11274 keep_next_level ();
11275 ret = c_begin_compound_stmt (true);
11276
11277 c_bindings_start_stmt_expr (c_switch_stack == NULL
11278 ? NULL
11279 : c_switch_stack->bindings);
11280
11281 /* Mark the current statement list as belonging to a statement list. */
11282 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11283
11284 return ret;
11285 }
11286
11287 /* LOC is the location of the compound statement to which this body
11288 belongs. */
11289
11290 tree
c_finish_stmt_expr(location_t loc,tree body)11291 c_finish_stmt_expr (location_t loc, tree body)
11292 {
11293 tree last, type, tmp, val;
11294 tree *last_p;
11295
11296 body = c_end_compound_stmt (loc, body, true);
11297
11298 c_bindings_end_stmt_expr (c_switch_stack == NULL
11299 ? NULL
11300 : c_switch_stack->bindings);
11301
11302 /* Locate the last statement in BODY. See c_end_compound_stmt
11303 about always returning a BIND_EXPR. */
11304 last_p = &BIND_EXPR_BODY (body);
11305 last = BIND_EXPR_BODY (body);
11306
11307 continue_searching:
11308 if (TREE_CODE (last) == STATEMENT_LIST)
11309 {
11310 tree_stmt_iterator l = tsi_last (last);
11311
11312 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11313 tsi_prev (&l);
11314
11315 /* This can happen with degenerate cases like ({ }). No value. */
11316 if (tsi_end_p (l))
11317 return body;
11318
11319 /* If we're supposed to generate side effects warnings, process
11320 all of the statements except the last. */
11321 if (warn_unused_value)
11322 {
11323 for (tree_stmt_iterator i = tsi_start (last);
11324 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11325 {
11326 location_t tloc;
11327 tree t = tsi_stmt (i);
11328
11329 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11330 emit_side_effect_warnings (tloc, t);
11331 }
11332 }
11333 last_p = tsi_stmt_ptr (l);
11334 last = *last_p;
11335 }
11336
11337 /* If the end of the list is exception related, then the list was split
11338 by a call to push_cleanup. Continue searching. */
11339 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11340 || TREE_CODE (last) == TRY_CATCH_EXPR)
11341 {
11342 last_p = &TREE_OPERAND (last, 0);
11343 last = *last_p;
11344 goto continue_searching;
11345 }
11346
11347 if (last == error_mark_node)
11348 return last;
11349
11350 /* In the case that the BIND_EXPR is not necessary, return the
11351 expression out from inside it. */
11352 if ((last == BIND_EXPR_BODY (body)
11353 /* Skip nested debug stmts. */
11354 || last == expr_first (BIND_EXPR_BODY (body)))
11355 && BIND_EXPR_VARS (body) == NULL)
11356 {
11357 /* Even if this looks constant, do not allow it in a constant
11358 expression. */
11359 last = c_wrap_maybe_const (last, true);
11360 /* Do not warn if the return value of a statement expression is
11361 unused. */
11362 TREE_NO_WARNING (last) = 1;
11363 return last;
11364 }
11365
11366 /* Extract the type of said expression. */
11367 type = TREE_TYPE (last);
11368
11369 /* If we're not returning a value at all, then the BIND_EXPR that
11370 we already have is a fine expression to return. */
11371 if (!type || VOID_TYPE_P (type))
11372 return body;
11373
11374 /* Now that we've located the expression containing the value, it seems
11375 silly to make voidify_wrapper_expr repeat the process. Create a
11376 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11377 tmp = create_tmp_var_raw (type);
11378
11379 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11380 tree_expr_nonnegative_p giving up immediately. */
11381 val = last;
11382 if (TREE_CODE (val) == NOP_EXPR
11383 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11384 val = TREE_OPERAND (val, 0);
11385
11386 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11387 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11388
11389 {
11390 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11391 SET_EXPR_LOCATION (t, loc);
11392 return t;
11393 }
11394 }
11395
11396 /* Begin and end compound statements. This is as simple as pushing
11397 and popping new statement lists from the tree. */
11398
11399 tree
c_begin_compound_stmt(bool do_scope)11400 c_begin_compound_stmt (bool do_scope)
11401 {
11402 tree stmt = push_stmt_list ();
11403 if (do_scope)
11404 push_scope ();
11405 return stmt;
11406 }
11407
11408 /* End a compound statement. STMT is the statement. LOC is the
11409 location of the compound statement-- this is usually the location
11410 of the opening brace. */
11411
11412 tree
c_end_compound_stmt(location_t loc,tree stmt,bool do_scope)11413 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11414 {
11415 tree block = NULL;
11416
11417 if (do_scope)
11418 {
11419 if (c_dialect_objc ())
11420 objc_clear_super_receiver ();
11421 block = pop_scope ();
11422 }
11423
11424 stmt = pop_stmt_list (stmt);
11425 stmt = c_build_bind_expr (loc, block, stmt);
11426
11427 /* If this compound statement is nested immediately inside a statement
11428 expression, then force a BIND_EXPR to be created. Otherwise we'll
11429 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11430 STATEMENT_LISTs merge, and thus we can lose track of what statement
11431 was really last. */
11432 if (building_stmt_list_p ()
11433 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11434 && TREE_CODE (stmt) != BIND_EXPR)
11435 {
11436 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11437 TREE_SIDE_EFFECTS (stmt) = 1;
11438 SET_EXPR_LOCATION (stmt, loc);
11439 }
11440
11441 return stmt;
11442 }
11443
11444 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11445 when the current scope is exited. EH_ONLY is true when this is not
11446 meant to apply to normal control flow transfer. */
11447
11448 void
push_cleanup(tree decl,tree cleanup,bool eh_only)11449 push_cleanup (tree decl, tree cleanup, bool eh_only)
11450 {
11451 enum tree_code code;
11452 tree stmt, list;
11453 bool stmt_expr;
11454
11455 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11456 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11457 add_stmt (stmt);
11458 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11459 list = push_stmt_list ();
11460 TREE_OPERAND (stmt, 0) = list;
11461 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11462 }
11463
11464 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11465 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11466
11467 static tree
build_vec_cmp(tree_code code,tree type,tree arg0,tree arg1)11468 build_vec_cmp (tree_code code, tree type,
11469 tree arg0, tree arg1)
11470 {
11471 tree zero_vec = build_zero_cst (type);
11472 tree minus_one_vec = build_minus_one_cst (type);
11473 tree cmp_type = truth_type_for (type);
11474 tree cmp = build2 (code, cmp_type, arg0, arg1);
11475 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11476 }
11477
11478 /* Build a binary-operation expression without default conversions.
11479 CODE is the kind of expression to build.
11480 LOCATION is the operator's location.
11481 This function differs from `build' in several ways:
11482 the data type of the result is computed and recorded in it,
11483 warnings are generated if arg data types are invalid,
11484 special handling for addition and subtraction of pointers is known,
11485 and some optimization is done (operations on narrow ints
11486 are done in the narrower type when that gives the same result).
11487 Constant folding is also done before the result is returned.
11488
11489 Note that the operands will never have enumeral types, or function
11490 or array types, because either they will have the default conversions
11491 performed or they have both just been converted to some other type in which
11492 the arithmetic is to be done. */
11493
11494 tree
build_binary_op(location_t location,enum tree_code code,tree orig_op0,tree orig_op1,bool convert_p)11495 build_binary_op (location_t location, enum tree_code code,
11496 tree orig_op0, tree orig_op1, bool convert_p)
11497 {
11498 tree type0, type1, orig_type0, orig_type1;
11499 tree eptype;
11500 enum tree_code code0, code1;
11501 tree op0, op1;
11502 tree ret = error_mark_node;
11503 const char *invalid_op_diag;
11504 bool op0_int_operands, op1_int_operands;
11505 bool int_const, int_const_or_overflow, int_operands;
11506
11507 /* Expression code to give to the expression when it is built.
11508 Normally this is CODE, which is what the caller asked for,
11509 but in some special cases we change it. */
11510 enum tree_code resultcode = code;
11511
11512 /* Data type in which the computation is to be performed.
11513 In the simplest cases this is the common type of the arguments. */
11514 tree result_type = NULL;
11515
11516 /* When the computation is in excess precision, the type of the
11517 final EXCESS_PRECISION_EXPR. */
11518 tree semantic_result_type = NULL;
11519
11520 /* Nonzero means operands have already been type-converted
11521 in whatever way is necessary.
11522 Zero means they need to be converted to RESULT_TYPE. */
11523 int converted = 0;
11524
11525 /* Nonzero means create the expression with this type, rather than
11526 RESULT_TYPE. */
11527 tree build_type = NULL_TREE;
11528
11529 /* Nonzero means after finally constructing the expression
11530 convert it to this type. */
11531 tree final_type = NULL_TREE;
11532
11533 /* Nonzero if this is an operation like MIN or MAX which can
11534 safely be computed in short if both args are promoted shorts.
11535 Also implies COMMON.
11536 -1 indicates a bitwise operation; this makes a difference
11537 in the exact conditions for when it is safe to do the operation
11538 in a narrower mode. */
11539 int shorten = 0;
11540
11541 /* Nonzero if this is a comparison operation;
11542 if both args are promoted shorts, compare the original shorts.
11543 Also implies COMMON. */
11544 int short_compare = 0;
11545
11546 /* Nonzero if this is a right-shift operation, which can be computed on the
11547 original short and then promoted if the operand is a promoted short. */
11548 int short_shift = 0;
11549
11550 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11551 int common = 0;
11552
11553 /* True means types are compatible as far as ObjC is concerned. */
11554 bool objc_ok;
11555
11556 /* True means this is an arithmetic operation that may need excess
11557 precision. */
11558 bool may_need_excess_precision;
11559
11560 /* True means this is a boolean operation that converts both its
11561 operands to truth-values. */
11562 bool boolean_op = false;
11563
11564 /* Remember whether we're doing / or %. */
11565 bool doing_div_or_mod = false;
11566
11567 /* Remember whether we're doing << or >>. */
11568 bool doing_shift = false;
11569
11570 /* Tree holding instrumentation expression. */
11571 tree instrument_expr = NULL;
11572
11573 if (location == UNKNOWN_LOCATION)
11574 location = input_location;
11575
11576 op0 = orig_op0;
11577 op1 = orig_op1;
11578
11579 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11580 if (op0_int_operands)
11581 op0 = remove_c_maybe_const_expr (op0);
11582 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11583 if (op1_int_operands)
11584 op1 = remove_c_maybe_const_expr (op1);
11585 int_operands = (op0_int_operands && op1_int_operands);
11586 if (int_operands)
11587 {
11588 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11589 && TREE_CODE (orig_op1) == INTEGER_CST);
11590 int_const = (int_const_or_overflow
11591 && !TREE_OVERFLOW (orig_op0)
11592 && !TREE_OVERFLOW (orig_op1));
11593 }
11594 else
11595 int_const = int_const_or_overflow = false;
11596
11597 /* Do not apply default conversion in mixed vector/scalar expression. */
11598 if (convert_p
11599 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11600 {
11601 op0 = default_conversion (op0);
11602 op1 = default_conversion (op1);
11603 }
11604
11605 orig_type0 = type0 = TREE_TYPE (op0);
11606
11607 orig_type1 = type1 = TREE_TYPE (op1);
11608
11609 /* The expression codes of the data types of the arguments tell us
11610 whether the arguments are integers, floating, pointers, etc. */
11611 code0 = TREE_CODE (type0);
11612 code1 = TREE_CODE (type1);
11613
11614 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11615 STRIP_TYPE_NOPS (op0);
11616 STRIP_TYPE_NOPS (op1);
11617
11618 /* If an error was already reported for one of the arguments,
11619 avoid reporting another error. */
11620
11621 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11622 return error_mark_node;
11623
11624 if (code0 == POINTER_TYPE
11625 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11626 return error_mark_node;
11627
11628 if (code1 == POINTER_TYPE
11629 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11630 return error_mark_node;
11631
11632 if ((invalid_op_diag
11633 = targetm.invalid_binary_op (code, type0, type1)))
11634 {
11635 error_at (location, invalid_op_diag);
11636 return error_mark_node;
11637 }
11638
11639 switch (code)
11640 {
11641 case PLUS_EXPR:
11642 case MINUS_EXPR:
11643 case MULT_EXPR:
11644 case TRUNC_DIV_EXPR:
11645 case CEIL_DIV_EXPR:
11646 case FLOOR_DIV_EXPR:
11647 case ROUND_DIV_EXPR:
11648 case EXACT_DIV_EXPR:
11649 may_need_excess_precision = true;
11650 break;
11651
11652 case EQ_EXPR:
11653 case NE_EXPR:
11654 case LE_EXPR:
11655 case GE_EXPR:
11656 case LT_EXPR:
11657 case GT_EXPR:
11658 /* Excess precision for implicit conversions of integers to
11659 floating point in C11 and later. */
11660 may_need_excess_precision = (flag_isoc11
11661 && (ANY_INTEGRAL_TYPE_P (type0)
11662 || ANY_INTEGRAL_TYPE_P (type1)));
11663 break;
11664
11665 default:
11666 may_need_excess_precision = false;
11667 break;
11668 }
11669 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11670 {
11671 op0 = TREE_OPERAND (op0, 0);
11672 type0 = TREE_TYPE (op0);
11673 }
11674 else if (may_need_excess_precision
11675 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11676 {
11677 type0 = eptype;
11678 op0 = convert (eptype, op0);
11679 }
11680 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11681 {
11682 op1 = TREE_OPERAND (op1, 0);
11683 type1 = TREE_TYPE (op1);
11684 }
11685 else if (may_need_excess_precision
11686 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11687 {
11688 type1 = eptype;
11689 op1 = convert (eptype, op1);
11690 }
11691
11692 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11693
11694 /* In case when one of the operands of the binary operation is
11695 a vector and another is a scalar -- convert scalar to vector. */
11696 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11697 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11698 {
11699 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11700 true);
11701
11702 switch (convert_flag)
11703 {
11704 case stv_error:
11705 return error_mark_node;
11706 case stv_firstarg:
11707 {
11708 bool maybe_const = true;
11709 tree sc;
11710 sc = c_fully_fold (op0, false, &maybe_const);
11711 sc = save_expr (sc);
11712 sc = convert (TREE_TYPE (type1), sc);
11713 op0 = build_vector_from_val (type1, sc);
11714 if (!maybe_const)
11715 op0 = c_wrap_maybe_const (op0, true);
11716 orig_type0 = type0 = TREE_TYPE (op0);
11717 code0 = TREE_CODE (type0);
11718 converted = 1;
11719 break;
11720 }
11721 case stv_secondarg:
11722 {
11723 bool maybe_const = true;
11724 tree sc;
11725 sc = c_fully_fold (op1, false, &maybe_const);
11726 sc = save_expr (sc);
11727 sc = convert (TREE_TYPE (type0), sc);
11728 op1 = build_vector_from_val (type0, sc);
11729 if (!maybe_const)
11730 op1 = c_wrap_maybe_const (op1, true);
11731 orig_type1 = type1 = TREE_TYPE (op1);
11732 code1 = TREE_CODE (type1);
11733 converted = 1;
11734 break;
11735 }
11736 default:
11737 break;
11738 }
11739 }
11740
11741 switch (code)
11742 {
11743 case PLUS_EXPR:
11744 /* Handle the pointer + int case. */
11745 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11746 {
11747 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11748 goto return_build_binary_op;
11749 }
11750 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11751 {
11752 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11753 goto return_build_binary_op;
11754 }
11755 else
11756 common = 1;
11757 break;
11758
11759 case MINUS_EXPR:
11760 /* Subtraction of two similar pointers.
11761 We must subtract them as integers, then divide by object size. */
11762 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11763 && comp_target_types (location, type0, type1))
11764 {
11765 ret = pointer_diff (location, op0, op1, &instrument_expr);
11766 goto return_build_binary_op;
11767 }
11768 /* Handle pointer minus int. Just like pointer plus int. */
11769 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11770 {
11771 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11772 goto return_build_binary_op;
11773 }
11774 else
11775 common = 1;
11776 break;
11777
11778 case MULT_EXPR:
11779 common = 1;
11780 break;
11781
11782 case TRUNC_DIV_EXPR:
11783 case CEIL_DIV_EXPR:
11784 case FLOOR_DIV_EXPR:
11785 case ROUND_DIV_EXPR:
11786 case EXACT_DIV_EXPR:
11787 doing_div_or_mod = true;
11788 warn_for_div_by_zero (location, op1);
11789
11790 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11791 || code0 == FIXED_POINT_TYPE
11792 || code0 == COMPLEX_TYPE
11793 || gnu_vector_type_p (type0))
11794 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11795 || code1 == FIXED_POINT_TYPE
11796 || code1 == COMPLEX_TYPE
11797 || gnu_vector_type_p (type1)))
11798 {
11799 enum tree_code tcode0 = code0, tcode1 = code1;
11800
11801 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11802 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11803 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11804 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11805
11806 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11807 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11808 resultcode = RDIV_EXPR;
11809 else
11810 /* Although it would be tempting to shorten always here, that
11811 loses on some targets, since the modulo instruction is
11812 undefined if the quotient can't be represented in the
11813 computation mode. We shorten only if unsigned or if
11814 dividing by something we know != -1. */
11815 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11816 || (TREE_CODE (op1) == INTEGER_CST
11817 && !integer_all_onesp (op1)));
11818 common = 1;
11819 }
11820 break;
11821
11822 case BIT_AND_EXPR:
11823 case BIT_IOR_EXPR:
11824 case BIT_XOR_EXPR:
11825 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11826 shorten = -1;
11827 /* Allow vector types which are not floating point types. */
11828 else if (gnu_vector_type_p (type0)
11829 && gnu_vector_type_p (type1)
11830 && !VECTOR_FLOAT_TYPE_P (type0)
11831 && !VECTOR_FLOAT_TYPE_P (type1))
11832 common = 1;
11833 break;
11834
11835 case TRUNC_MOD_EXPR:
11836 case FLOOR_MOD_EXPR:
11837 doing_div_or_mod = true;
11838 warn_for_div_by_zero (location, op1);
11839
11840 if (gnu_vector_type_p (type0)
11841 && gnu_vector_type_p (type1)
11842 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11843 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11844 common = 1;
11845 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11846 {
11847 /* Although it would be tempting to shorten always here, that loses
11848 on some targets, since the modulo instruction is undefined if the
11849 quotient can't be represented in the computation mode. We shorten
11850 only if unsigned or if dividing by something we know != -1. */
11851 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11852 || (TREE_CODE (op1) == INTEGER_CST
11853 && !integer_all_onesp (op1)));
11854 common = 1;
11855 }
11856 break;
11857
11858 case TRUTH_ANDIF_EXPR:
11859 case TRUTH_ORIF_EXPR:
11860 case TRUTH_AND_EXPR:
11861 case TRUTH_OR_EXPR:
11862 case TRUTH_XOR_EXPR:
11863 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11864 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11865 || code0 == FIXED_POINT_TYPE)
11866 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11867 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11868 || code1 == FIXED_POINT_TYPE))
11869 {
11870 /* Result of these operations is always an int,
11871 but that does not mean the operands should be
11872 converted to ints! */
11873 result_type = integer_type_node;
11874 if (op0_int_operands)
11875 {
11876 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11877 op0 = remove_c_maybe_const_expr (op0);
11878 }
11879 else
11880 op0 = c_objc_common_truthvalue_conversion (location, op0);
11881 if (op1_int_operands)
11882 {
11883 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11884 op1 = remove_c_maybe_const_expr (op1);
11885 }
11886 else
11887 op1 = c_objc_common_truthvalue_conversion (location, op1);
11888 converted = 1;
11889 boolean_op = true;
11890 }
11891 if (code == TRUTH_ANDIF_EXPR)
11892 {
11893 int_const_or_overflow = (int_operands
11894 && TREE_CODE (orig_op0) == INTEGER_CST
11895 && (op0 == truthvalue_false_node
11896 || TREE_CODE (orig_op1) == INTEGER_CST));
11897 int_const = (int_const_or_overflow
11898 && !TREE_OVERFLOW (orig_op0)
11899 && (op0 == truthvalue_false_node
11900 || !TREE_OVERFLOW (orig_op1)));
11901 }
11902 else if (code == TRUTH_ORIF_EXPR)
11903 {
11904 int_const_or_overflow = (int_operands
11905 && TREE_CODE (orig_op0) == INTEGER_CST
11906 && (op0 == truthvalue_true_node
11907 || TREE_CODE (orig_op1) == INTEGER_CST));
11908 int_const = (int_const_or_overflow
11909 && !TREE_OVERFLOW (orig_op0)
11910 && (op0 == truthvalue_true_node
11911 || !TREE_OVERFLOW (orig_op1)));
11912 }
11913 break;
11914
11915 /* Shift operations: result has same type as first operand;
11916 always convert second operand to int.
11917 Also set SHORT_SHIFT if shifting rightward. */
11918
11919 case RSHIFT_EXPR:
11920 if (gnu_vector_type_p (type0)
11921 && gnu_vector_type_p (type1)
11922 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11923 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11924 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11925 TYPE_VECTOR_SUBPARTS (type1)))
11926 {
11927 result_type = type0;
11928 converted = 1;
11929 }
11930 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11931 || (gnu_vector_type_p (type0)
11932 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11933 && code1 == INTEGER_TYPE)
11934 {
11935 doing_shift = true;
11936 if (TREE_CODE (op1) == INTEGER_CST)
11937 {
11938 if (tree_int_cst_sgn (op1) < 0)
11939 {
11940 int_const = false;
11941 if (c_inhibit_evaluation_warnings == 0)
11942 warning_at (location, OPT_Wshift_count_negative,
11943 "right shift count is negative");
11944 }
11945 else if (code0 == VECTOR_TYPE)
11946 {
11947 if (compare_tree_int (op1,
11948 TYPE_PRECISION (TREE_TYPE (type0)))
11949 >= 0)
11950 {
11951 int_const = false;
11952 if (c_inhibit_evaluation_warnings == 0)
11953 warning_at (location, OPT_Wshift_count_overflow,
11954 "right shift count >= width of vector element");
11955 }
11956 }
11957 else
11958 {
11959 if (!integer_zerop (op1))
11960 short_shift = 1;
11961
11962 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11963 {
11964 int_const = false;
11965 if (c_inhibit_evaluation_warnings == 0)
11966 warning_at (location, OPT_Wshift_count_overflow,
11967 "right shift count >= width of type");
11968 }
11969 }
11970 }
11971
11972 /* Use the type of the value to be shifted. */
11973 result_type = type0;
11974 /* Avoid converting op1 to result_type later. */
11975 converted = 1;
11976 }
11977 break;
11978
11979 case LSHIFT_EXPR:
11980 if (gnu_vector_type_p (type0)
11981 && gnu_vector_type_p (type1)
11982 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11983 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11984 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11985 TYPE_VECTOR_SUBPARTS (type1)))
11986 {
11987 result_type = type0;
11988 converted = 1;
11989 }
11990 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11991 || (gnu_vector_type_p (type0)
11992 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11993 && code1 == INTEGER_TYPE)
11994 {
11995 doing_shift = true;
11996 if (TREE_CODE (op0) == INTEGER_CST
11997 && tree_int_cst_sgn (op0) < 0)
11998 {
11999 /* Don't reject a left shift of a negative value in a context
12000 where a constant expression is needed in C90. */
12001 if (flag_isoc99)
12002 int_const = false;
12003 if (c_inhibit_evaluation_warnings == 0)
12004 warning_at (location, OPT_Wshift_negative_value,
12005 "left shift of negative value");
12006 }
12007 if (TREE_CODE (op1) == INTEGER_CST)
12008 {
12009 if (tree_int_cst_sgn (op1) < 0)
12010 {
12011 int_const = false;
12012 if (c_inhibit_evaluation_warnings == 0)
12013 warning_at (location, OPT_Wshift_count_negative,
12014 "left shift count is negative");
12015 }
12016 else if (code0 == VECTOR_TYPE)
12017 {
12018 if (compare_tree_int (op1,
12019 TYPE_PRECISION (TREE_TYPE (type0)))
12020 >= 0)
12021 {
12022 int_const = false;
12023 if (c_inhibit_evaluation_warnings == 0)
12024 warning_at (location, OPT_Wshift_count_overflow,
12025 "left shift count >= width of vector element");
12026 }
12027 }
12028 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12029 {
12030 int_const = false;
12031 if (c_inhibit_evaluation_warnings == 0)
12032 warning_at (location, OPT_Wshift_count_overflow,
12033 "left shift count >= width of type");
12034 }
12035 else if (TREE_CODE (op0) == INTEGER_CST
12036 && maybe_warn_shift_overflow (location, op0, op1)
12037 && flag_isoc99)
12038 int_const = false;
12039 }
12040
12041 /* Use the type of the value to be shifted. */
12042 result_type = type0;
12043 /* Avoid converting op1 to result_type later. */
12044 converted = 1;
12045 }
12046 break;
12047
12048 case EQ_EXPR:
12049 case NE_EXPR:
12050 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12051 {
12052 tree intt;
12053 if (!vector_types_compatible_elements_p (type0, type1))
12054 {
12055 error_at (location, "comparing vectors with different "
12056 "element types");
12057 return error_mark_node;
12058 }
12059
12060 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12061 TYPE_VECTOR_SUBPARTS (type1)))
12062 {
12063 error_at (location, "comparing vectors with different "
12064 "number of elements");
12065 return error_mark_node;
12066 }
12067
12068 /* It's not precisely specified how the usual arithmetic
12069 conversions apply to the vector types. Here, we use
12070 the unsigned type if one of the operands is signed and
12071 the other one is unsigned. */
12072 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12073 {
12074 if (!TYPE_UNSIGNED (type0))
12075 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12076 else
12077 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12078 warning_at (location, OPT_Wsign_compare, "comparison between "
12079 "types %qT and %qT", type0, type1);
12080 }
12081
12082 /* Always construct signed integer vector type. */
12083 intt = c_common_type_for_size (GET_MODE_BITSIZE
12084 (SCALAR_TYPE_MODE
12085 (TREE_TYPE (type0))), 0);
12086 if (!intt)
12087 {
12088 error_at (location, "could not find an integer type "
12089 "of the same size as %qT",
12090 TREE_TYPE (type0));
12091 return error_mark_node;
12092 }
12093 result_type = build_opaque_vector_type (intt,
12094 TYPE_VECTOR_SUBPARTS (type0));
12095 converted = 1;
12096 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12097 goto return_build_binary_op;
12098 }
12099 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12100 warning_at (location,
12101 OPT_Wfloat_equal,
12102 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12103 /* Result of comparison is always int,
12104 but don't convert the args to int! */
12105 build_type = integer_type_node;
12106 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12107 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12108 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12109 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12110 short_compare = 1;
12111 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12112 {
12113 if (TREE_CODE (op0) == ADDR_EXPR
12114 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
12115 && !from_macro_expansion_at (location))
12116 {
12117 if (code == EQ_EXPR)
12118 warning_at (location,
12119 OPT_Waddress,
12120 "the comparison will always evaluate as %<false%> "
12121 "for the address of %qD will never be NULL",
12122 TREE_OPERAND (op0, 0));
12123 else
12124 warning_at (location,
12125 OPT_Waddress,
12126 "the comparison will always evaluate as %<true%> "
12127 "for the address of %qD will never be NULL",
12128 TREE_OPERAND (op0, 0));
12129 }
12130 result_type = type0;
12131 }
12132 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12133 {
12134 if (TREE_CODE (op1) == ADDR_EXPR
12135 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
12136 && !from_macro_expansion_at (location))
12137 {
12138 if (code == EQ_EXPR)
12139 warning_at (location,
12140 OPT_Waddress,
12141 "the comparison will always evaluate as %<false%> "
12142 "for the address of %qD will never be NULL",
12143 TREE_OPERAND (op1, 0));
12144 else
12145 warning_at (location,
12146 OPT_Waddress,
12147 "the comparison will always evaluate as %<true%> "
12148 "for the address of %qD will never be NULL",
12149 TREE_OPERAND (op1, 0));
12150 }
12151 result_type = type1;
12152 }
12153 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12154 {
12155 tree tt0 = TREE_TYPE (type0);
12156 tree tt1 = TREE_TYPE (type1);
12157 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12158 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12159 addr_space_t as_common = ADDR_SPACE_GENERIC;
12160
12161 /* Anything compares with void *. void * compares with anything.
12162 Otherwise, the targets must be compatible
12163 and both must be object or both incomplete. */
12164 if (comp_target_types (location, type0, type1))
12165 result_type = common_pointer_type (type0, type1);
12166 else if (!addr_space_superset (as0, as1, &as_common))
12167 {
12168 error_at (location, "comparison of pointers to "
12169 "disjoint address spaces");
12170 return error_mark_node;
12171 }
12172 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12173 {
12174 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12175 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12176 "comparison of %<void *%> with function pointer");
12177 }
12178 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12179 {
12180 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12181 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12182 "comparison of %<void *%> with function pointer");
12183 }
12184 else
12185 /* Avoid warning about the volatile ObjC EH puts on decls. */
12186 if (!objc_ok)
12187 pedwarn (location, 0,
12188 "comparison of distinct pointer types lacks a cast");
12189
12190 if (result_type == NULL_TREE)
12191 {
12192 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12193 result_type = build_pointer_type
12194 (build_qualified_type (void_type_node, qual));
12195 }
12196 }
12197 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12198 {
12199 result_type = type0;
12200 pedwarn (location, 0, "comparison between pointer and integer");
12201 }
12202 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12203 {
12204 result_type = type1;
12205 pedwarn (location, 0, "comparison between pointer and integer");
12206 }
12207 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12208 || truth_value_p (TREE_CODE (orig_op0)))
12209 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12210 || truth_value_p (TREE_CODE (orig_op1))))
12211 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12212 break;
12213
12214 case LE_EXPR:
12215 case GE_EXPR:
12216 case LT_EXPR:
12217 case GT_EXPR:
12218 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12219 {
12220 tree intt;
12221 if (!vector_types_compatible_elements_p (type0, type1))
12222 {
12223 error_at (location, "comparing vectors with different "
12224 "element types");
12225 return error_mark_node;
12226 }
12227
12228 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12229 TYPE_VECTOR_SUBPARTS (type1)))
12230 {
12231 error_at (location, "comparing vectors with different "
12232 "number of elements");
12233 return error_mark_node;
12234 }
12235
12236 /* It's not precisely specified how the usual arithmetic
12237 conversions apply to the vector types. Here, we use
12238 the unsigned type if one of the operands is signed and
12239 the other one is unsigned. */
12240 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12241 {
12242 if (!TYPE_UNSIGNED (type0))
12243 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12244 else
12245 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12246 warning_at (location, OPT_Wsign_compare, "comparison between "
12247 "types %qT and %qT", type0, type1);
12248 }
12249
12250 /* Always construct signed integer vector type. */
12251 intt = c_common_type_for_size (GET_MODE_BITSIZE
12252 (SCALAR_TYPE_MODE
12253 (TREE_TYPE (type0))), 0);
12254 if (!intt)
12255 {
12256 error_at (location, "could not find an integer type "
12257 "of the same size as %qT",
12258 TREE_TYPE (type0));
12259 return error_mark_node;
12260 }
12261 result_type = build_opaque_vector_type (intt,
12262 TYPE_VECTOR_SUBPARTS (type0));
12263 converted = 1;
12264 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12265 goto return_build_binary_op;
12266 }
12267 build_type = integer_type_node;
12268 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12269 || code0 == FIXED_POINT_TYPE)
12270 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12271 || code1 == FIXED_POINT_TYPE))
12272 short_compare = 1;
12273 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12274 {
12275 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12276 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12277 addr_space_t as_common;
12278
12279 if (comp_target_types (location, type0, type1))
12280 {
12281 result_type = common_pointer_type (type0, type1);
12282 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12283 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12284 pedwarn (location, 0,
12285 "comparison of complete and incomplete pointers");
12286 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12287 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12288 "ordered comparisons of pointers to functions");
12289 else if (null_pointer_constant_p (orig_op0)
12290 || null_pointer_constant_p (orig_op1))
12291 warning_at (location, OPT_Wextra,
12292 "ordered comparison of pointer with null pointer");
12293
12294 }
12295 else if (!addr_space_superset (as0, as1, &as_common))
12296 {
12297 error_at (location, "comparison of pointers to "
12298 "disjoint address spaces");
12299 return error_mark_node;
12300 }
12301 else
12302 {
12303 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12304 result_type = build_pointer_type
12305 (build_qualified_type (void_type_node, qual));
12306 pedwarn (location, 0,
12307 "comparison of distinct pointer types lacks a cast");
12308 }
12309 }
12310 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12311 {
12312 result_type = type0;
12313 if (pedantic)
12314 pedwarn (location, OPT_Wpedantic,
12315 "ordered comparison of pointer with integer zero");
12316 else if (extra_warnings)
12317 warning_at (location, OPT_Wextra,
12318 "ordered comparison of pointer with integer zero");
12319 }
12320 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12321 {
12322 result_type = type1;
12323 if (pedantic)
12324 pedwarn (location, OPT_Wpedantic,
12325 "ordered comparison of pointer with integer zero");
12326 else if (extra_warnings)
12327 warning_at (location, OPT_Wextra,
12328 "ordered comparison of pointer with integer zero");
12329 }
12330 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12331 {
12332 result_type = type0;
12333 pedwarn (location, 0, "comparison between pointer and integer");
12334 }
12335 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12336 {
12337 result_type = type1;
12338 pedwarn (location, 0, "comparison between pointer and integer");
12339 }
12340
12341 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12342 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12343 {
12344 op0 = save_expr (op0);
12345 op1 = save_expr (op1);
12346
12347 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12348 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12349 }
12350
12351 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12352 || truth_value_p (TREE_CODE (orig_op0)))
12353 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12354 || truth_value_p (TREE_CODE (orig_op1))))
12355 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12356 break;
12357
12358 default:
12359 gcc_unreachable ();
12360 }
12361
12362 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12363 return error_mark_node;
12364
12365 if (gnu_vector_type_p (type0)
12366 && gnu_vector_type_p (type1)
12367 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12368 || !vector_types_compatible_elements_p (type0, type1)))
12369 {
12370 gcc_rich_location richloc (location);
12371 maybe_range_label_for_tree_type_mismatch
12372 label_for_op0 (orig_op0, orig_op1),
12373 label_for_op1 (orig_op1, orig_op0);
12374 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12375 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12376 binary_op_error (&richloc, code, type0, type1);
12377 return error_mark_node;
12378 }
12379
12380 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12381 || code0 == FIXED_POINT_TYPE
12382 || gnu_vector_type_p (type0))
12383 &&
12384 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12385 || code1 == FIXED_POINT_TYPE
12386 || gnu_vector_type_p (type1)))
12387 {
12388 bool first_complex = (code0 == COMPLEX_TYPE);
12389 bool second_complex = (code1 == COMPLEX_TYPE);
12390 int none_complex = (!first_complex && !second_complex);
12391
12392 if (shorten || common || short_compare)
12393 {
12394 result_type = c_common_type (type0, type1);
12395 do_warn_double_promotion (result_type, type0, type1,
12396 "implicit conversion from %qT to %qT "
12397 "to match other operand of binary "
12398 "expression",
12399 location);
12400 if (result_type == error_mark_node)
12401 return error_mark_node;
12402 }
12403
12404 if (first_complex != second_complex
12405 && (code == PLUS_EXPR
12406 || code == MINUS_EXPR
12407 || code == MULT_EXPR
12408 || (code == TRUNC_DIV_EXPR && first_complex))
12409 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12410 && flag_signed_zeros)
12411 {
12412 /* An operation on mixed real/complex operands must be
12413 handled specially, but the language-independent code can
12414 more easily optimize the plain complex arithmetic if
12415 -fno-signed-zeros. */
12416 tree real_type = TREE_TYPE (result_type);
12417 tree real, imag;
12418 if (type0 != orig_type0 || type1 != orig_type1)
12419 {
12420 gcc_assert (may_need_excess_precision && common);
12421 semantic_result_type = c_common_type (orig_type0, orig_type1);
12422 }
12423 if (first_complex)
12424 {
12425 if (TREE_TYPE (op0) != result_type)
12426 op0 = convert_and_check (location, result_type, op0);
12427 if (TREE_TYPE (op1) != real_type)
12428 op1 = convert_and_check (location, real_type, op1);
12429 }
12430 else
12431 {
12432 if (TREE_TYPE (op0) != real_type)
12433 op0 = convert_and_check (location, real_type, op0);
12434 if (TREE_TYPE (op1) != result_type)
12435 op1 = convert_and_check (location, result_type, op1);
12436 }
12437 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12438 return error_mark_node;
12439 if (first_complex)
12440 {
12441 op0 = save_expr (op0);
12442 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12443 op0, true);
12444 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12445 op0, true);
12446 switch (code)
12447 {
12448 case MULT_EXPR:
12449 case TRUNC_DIV_EXPR:
12450 op1 = save_expr (op1);
12451 imag = build2 (resultcode, real_type, imag, op1);
12452 /* Fall through. */
12453 case PLUS_EXPR:
12454 case MINUS_EXPR:
12455 real = build2 (resultcode, real_type, real, op1);
12456 break;
12457 default:
12458 gcc_unreachable();
12459 }
12460 }
12461 else
12462 {
12463 op1 = save_expr (op1);
12464 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12465 op1, true);
12466 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12467 op1, true);
12468 switch (code)
12469 {
12470 case MULT_EXPR:
12471 op0 = save_expr (op0);
12472 imag = build2 (resultcode, real_type, op0, imag);
12473 /* Fall through. */
12474 case PLUS_EXPR:
12475 real = build2 (resultcode, real_type, op0, real);
12476 break;
12477 case MINUS_EXPR:
12478 real = build2 (resultcode, real_type, op0, real);
12479 imag = build1 (NEGATE_EXPR, real_type, imag);
12480 break;
12481 default:
12482 gcc_unreachable();
12483 }
12484 }
12485 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12486 goto return_build_binary_op;
12487 }
12488
12489 /* For certain operations (which identify themselves by shorten != 0)
12490 if both args were extended from the same smaller type,
12491 do the arithmetic in that type and then extend.
12492
12493 shorten !=0 and !=1 indicates a bitwise operation.
12494 For them, this optimization is safe only if
12495 both args are zero-extended or both are sign-extended.
12496 Otherwise, we might change the result.
12497 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12498 but calculated in (unsigned short) it would be (unsigned short)-1. */
12499
12500 if (shorten && none_complex)
12501 {
12502 final_type = result_type;
12503 result_type = shorten_binary_op (result_type, op0, op1,
12504 shorten == -1);
12505 }
12506
12507 /* Shifts can be shortened if shifting right. */
12508
12509 if (short_shift)
12510 {
12511 int unsigned_arg;
12512 tree arg0 = get_narrower (op0, &unsigned_arg);
12513
12514 final_type = result_type;
12515
12516 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12517 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12518
12519 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12520 && tree_int_cst_sgn (op1) > 0
12521 /* We can shorten only if the shift count is less than the
12522 number of bits in the smaller type size. */
12523 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12524 /* We cannot drop an unsigned shift after sign-extension. */
12525 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12526 {
12527 /* Do an unsigned shift if the operand was zero-extended. */
12528 result_type
12529 = c_common_signed_or_unsigned_type (unsigned_arg,
12530 TREE_TYPE (arg0));
12531 /* Convert value-to-be-shifted to that type. */
12532 if (TREE_TYPE (op0) != result_type)
12533 op0 = convert (result_type, op0);
12534 converted = 1;
12535 }
12536 }
12537
12538 /* Comparison operations are shortened too but differently.
12539 They identify themselves by setting short_compare = 1. */
12540
12541 if (short_compare)
12542 {
12543 /* Don't write &op0, etc., because that would prevent op0
12544 from being kept in a register.
12545 Instead, make copies of the our local variables and
12546 pass the copies by reference, then copy them back afterward. */
12547 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12548 enum tree_code xresultcode = resultcode;
12549 tree val
12550 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12551 &xresultcode);
12552
12553 if (val != NULL_TREE)
12554 {
12555 ret = val;
12556 goto return_build_binary_op;
12557 }
12558
12559 op0 = xop0, op1 = xop1;
12560 converted = 1;
12561 resultcode = xresultcode;
12562
12563 if (c_inhibit_evaluation_warnings == 0)
12564 {
12565 bool op0_maybe_const = true;
12566 bool op1_maybe_const = true;
12567 tree orig_op0_folded, orig_op1_folded;
12568
12569 if (in_late_binary_op)
12570 {
12571 orig_op0_folded = orig_op0;
12572 orig_op1_folded = orig_op1;
12573 }
12574 else
12575 {
12576 /* Fold for the sake of possible warnings, as in
12577 build_conditional_expr. This requires the
12578 "original" values to be folded, not just op0 and
12579 op1. */
12580 c_inhibit_evaluation_warnings++;
12581 op0 = c_fully_fold (op0, require_constant_value,
12582 &op0_maybe_const);
12583 op1 = c_fully_fold (op1, require_constant_value,
12584 &op1_maybe_const);
12585 c_inhibit_evaluation_warnings--;
12586 orig_op0_folded = c_fully_fold (orig_op0,
12587 require_constant_value,
12588 NULL);
12589 orig_op1_folded = c_fully_fold (orig_op1,
12590 require_constant_value,
12591 NULL);
12592 }
12593
12594 if (warn_sign_compare)
12595 warn_for_sign_compare (location, orig_op0_folded,
12596 orig_op1_folded, op0, op1,
12597 result_type, resultcode);
12598 if (!in_late_binary_op && !int_operands)
12599 {
12600 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12601 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12602 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12603 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12604 }
12605 }
12606 }
12607 }
12608
12609 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12610 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12611 Then the expression will be built.
12612 It will be given type FINAL_TYPE if that is nonzero;
12613 otherwise, it will be given type RESULT_TYPE. */
12614
12615 if (!result_type)
12616 {
12617 /* Favor showing any expression locations that are available. */
12618 op_location_t oploc (location, UNKNOWN_LOCATION);
12619 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12620 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12621 return error_mark_node;
12622 }
12623
12624 if (build_type == NULL_TREE)
12625 {
12626 build_type = result_type;
12627 if ((type0 != orig_type0 || type1 != orig_type1)
12628 && !boolean_op)
12629 {
12630 gcc_assert (may_need_excess_precision && common);
12631 semantic_result_type = c_common_type (orig_type0, orig_type1);
12632 }
12633 }
12634
12635 if (!converted)
12636 {
12637 op0 = ep_convert_and_check (location, result_type, op0,
12638 semantic_result_type);
12639 op1 = ep_convert_and_check (location, result_type, op1,
12640 semantic_result_type);
12641
12642 /* This can happen if one operand has a vector type, and the other
12643 has a different type. */
12644 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12645 return error_mark_node;
12646 }
12647
12648 if (sanitize_flags_p ((SANITIZE_SHIFT
12649 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12650 && current_function_decl != NULL_TREE
12651 && (doing_div_or_mod || doing_shift)
12652 && !require_constant_value)
12653 {
12654 /* OP0 and/or OP1 might have side-effects. */
12655 op0 = save_expr (op0);
12656 op1 = save_expr (op1);
12657 op0 = c_fully_fold (op0, false, NULL);
12658 op1 = c_fully_fold (op1, false, NULL);
12659 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12660 | SANITIZE_FLOAT_DIVIDE))))
12661 instrument_expr = ubsan_instrument_division (location, op0, op1);
12662 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12663 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12664 }
12665
12666 /* Treat expressions in initializers specially as they can't trap. */
12667 if (int_const_or_overflow)
12668 ret = (require_constant_value
12669 ? fold_build2_initializer_loc (location, resultcode, build_type,
12670 op0, op1)
12671 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12672 else
12673 ret = build2 (resultcode, build_type, op0, op1);
12674 if (final_type != NULL_TREE)
12675 ret = convert (final_type, ret);
12676
12677 return_build_binary_op:
12678 gcc_assert (ret != error_mark_node);
12679 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12680 ret = (int_operands
12681 ? note_integer_operands (ret)
12682 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12683 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12684 && !in_late_binary_op)
12685 ret = note_integer_operands (ret);
12686 protected_set_expr_location (ret, location);
12687
12688 if (instrument_expr != NULL)
12689 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12690 instrument_expr, ret);
12691
12692 if (semantic_result_type)
12693 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12694 semantic_result_type, ret);
12695
12696 return ret;
12697 }
12698
12699
12700 /* Convert EXPR to be a truth-value, validating its type for this
12701 purpose. LOCATION is the source location for the expression. */
12702
12703 tree
c_objc_common_truthvalue_conversion(location_t location,tree expr)12704 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12705 {
12706 bool int_const, int_operands;
12707
12708 switch (TREE_CODE (TREE_TYPE (expr)))
12709 {
12710 case ARRAY_TYPE:
12711 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12712 return error_mark_node;
12713
12714 case RECORD_TYPE:
12715 error_at (location, "used struct type value where scalar is required");
12716 return error_mark_node;
12717
12718 case UNION_TYPE:
12719 error_at (location, "used union type value where scalar is required");
12720 return error_mark_node;
12721
12722 case VOID_TYPE:
12723 error_at (location, "void value not ignored as it ought to be");
12724 return error_mark_node;
12725
12726 case POINTER_TYPE:
12727 if (reject_gcc_builtin (expr))
12728 return error_mark_node;
12729 break;
12730
12731 case FUNCTION_TYPE:
12732 gcc_unreachable ();
12733
12734 case VECTOR_TYPE:
12735 error_at (location, "used vector type where scalar is required");
12736 return error_mark_node;
12737
12738 default:
12739 break;
12740 }
12741
12742 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12743 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12744 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12745 {
12746 expr = remove_c_maybe_const_expr (expr);
12747 expr = build2 (NE_EXPR, integer_type_node, expr,
12748 convert (TREE_TYPE (expr), integer_zero_node));
12749 expr = note_integer_operands (expr);
12750 }
12751 else
12752 /* ??? Should we also give an error for vectors rather than leaving
12753 those to give errors later? */
12754 expr = c_common_truthvalue_conversion (location, expr);
12755
12756 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12757 {
12758 if (TREE_OVERFLOW (expr))
12759 return expr;
12760 else
12761 return note_integer_operands (expr);
12762 }
12763 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12764 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12765 return expr;
12766 }
12767
12768
12769 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12770 required. */
12771
12772 tree
c_expr_to_decl(tree expr,bool * tc ATTRIBUTE_UNUSED,bool * se)12773 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12774 {
12775 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12776 {
12777 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12778 /* Executing a compound literal inside a function reinitializes
12779 it. */
12780 if (!TREE_STATIC (decl))
12781 *se = true;
12782 return decl;
12783 }
12784 else
12785 return expr;
12786 }
12787
12788 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12789 statement. LOC is the location of the construct. */
12790
12791 tree
c_finish_omp_construct(location_t loc,enum tree_code code,tree body,tree clauses)12792 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12793 tree clauses)
12794 {
12795 body = c_end_compound_stmt (loc, body, true);
12796
12797 tree stmt = make_node (code);
12798 TREE_TYPE (stmt) = void_type_node;
12799 OMP_BODY (stmt) = body;
12800 OMP_CLAUSES (stmt) = clauses;
12801 SET_EXPR_LOCATION (stmt, loc);
12802
12803 return add_stmt (stmt);
12804 }
12805
12806 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12807 statement. LOC is the location of the OACC_DATA. */
12808
12809 tree
c_finish_oacc_data(location_t loc,tree clauses,tree block)12810 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12811 {
12812 tree stmt;
12813
12814 block = c_end_compound_stmt (loc, block, true);
12815
12816 stmt = make_node (OACC_DATA);
12817 TREE_TYPE (stmt) = void_type_node;
12818 OACC_DATA_CLAUSES (stmt) = clauses;
12819 OACC_DATA_BODY (stmt) = block;
12820 SET_EXPR_LOCATION (stmt, loc);
12821
12822 return add_stmt (stmt);
12823 }
12824
12825 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12826 statement. LOC is the location of the OACC_HOST_DATA. */
12827
12828 tree
c_finish_oacc_host_data(location_t loc,tree clauses,tree block)12829 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12830 {
12831 tree stmt;
12832
12833 block = c_end_compound_stmt (loc, block, true);
12834
12835 stmt = make_node (OACC_HOST_DATA);
12836 TREE_TYPE (stmt) = void_type_node;
12837 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12838 OACC_HOST_DATA_BODY (stmt) = block;
12839 SET_EXPR_LOCATION (stmt, loc);
12840
12841 return add_stmt (stmt);
12842 }
12843
12844 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12845
12846 tree
c_begin_omp_parallel(void)12847 c_begin_omp_parallel (void)
12848 {
12849 tree block;
12850
12851 keep_next_level ();
12852 block = c_begin_compound_stmt (true);
12853
12854 return block;
12855 }
12856
12857 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12858 statement. LOC is the location of the OMP_PARALLEL. */
12859
12860 tree
c_finish_omp_parallel(location_t loc,tree clauses,tree block)12861 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12862 {
12863 tree stmt;
12864
12865 block = c_end_compound_stmt (loc, block, true);
12866
12867 stmt = make_node (OMP_PARALLEL);
12868 TREE_TYPE (stmt) = void_type_node;
12869 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12870 OMP_PARALLEL_BODY (stmt) = block;
12871 SET_EXPR_LOCATION (stmt, loc);
12872
12873 return add_stmt (stmt);
12874 }
12875
12876 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12877
12878 tree
c_begin_omp_task(void)12879 c_begin_omp_task (void)
12880 {
12881 tree block;
12882
12883 keep_next_level ();
12884 block = c_begin_compound_stmt (true);
12885
12886 return block;
12887 }
12888
12889 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12890 statement. LOC is the location of the #pragma. */
12891
12892 tree
c_finish_omp_task(location_t loc,tree clauses,tree block)12893 c_finish_omp_task (location_t loc, tree clauses, tree block)
12894 {
12895 tree stmt;
12896
12897 block = c_end_compound_stmt (loc, block, true);
12898
12899 stmt = make_node (OMP_TASK);
12900 TREE_TYPE (stmt) = void_type_node;
12901 OMP_TASK_CLAUSES (stmt) = clauses;
12902 OMP_TASK_BODY (stmt) = block;
12903 SET_EXPR_LOCATION (stmt, loc);
12904
12905 return add_stmt (stmt);
12906 }
12907
12908 /* Generate GOMP_cancel call for #pragma omp cancel. */
12909
12910 void
c_finish_omp_cancel(location_t loc,tree clauses)12911 c_finish_omp_cancel (location_t loc, tree clauses)
12912 {
12913 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12914 int mask = 0;
12915 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12916 mask = 1;
12917 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12918 mask = 2;
12919 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12920 mask = 4;
12921 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12922 mask = 8;
12923 else
12924 {
12925 error_at (loc, "%<#pragma omp cancel%> must specify one of "
12926 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12927 "clauses");
12928 return;
12929 }
12930 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12931 if (ifc != NULL_TREE)
12932 {
12933 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
12934 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
12935 error_at (OMP_CLAUSE_LOCATION (ifc),
12936 "expected %<cancel%> %<if%> clause modifier");
12937 else
12938 {
12939 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
12940 if (ifc2 != NULL_TREE)
12941 {
12942 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
12943 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
12944 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
12945 error_at (OMP_CLAUSE_LOCATION (ifc2),
12946 "expected %<cancel%> %<if%> clause modifier");
12947 }
12948 }
12949
12950 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12951 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12952 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12953 build_zero_cst (type));
12954 }
12955 else
12956 ifc = boolean_true_node;
12957 tree stmt = build_call_expr_loc (loc, fn, 2,
12958 build_int_cst (integer_type_node, mask),
12959 ifc);
12960 add_stmt (stmt);
12961 }
12962
12963 /* Generate GOMP_cancellation_point call for
12964 #pragma omp cancellation point. */
12965
12966 void
c_finish_omp_cancellation_point(location_t loc,tree clauses)12967 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12968 {
12969 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12970 int mask = 0;
12971 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12972 mask = 1;
12973 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12974 mask = 2;
12975 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12976 mask = 4;
12977 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12978 mask = 8;
12979 else
12980 {
12981 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12982 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12983 "clauses");
12984 return;
12985 }
12986 tree stmt = build_call_expr_loc (loc, fn, 1,
12987 build_int_cst (integer_type_node, mask));
12988 add_stmt (stmt);
12989 }
12990
12991 /* Helper function for handle_omp_array_sections. Called recursively
12992 to handle multiple array-section-subscripts. C is the clause,
12993 T current expression (initially OMP_CLAUSE_DECL), which is either
12994 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12995 expression if specified, TREE_VALUE length expression if specified,
12996 TREE_CHAIN is what it has been specified after, or some decl.
12997 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12998 set to true if any of the array-section-subscript could have length
12999 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13000 first array-section-subscript which is known not to have length
13001 of one. Given say:
13002 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13003 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13004 all are or may have length of 1, array-section-subscript [:2] is the
13005 first one known not to have length 1. For array-section-subscript
13006 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13007 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13008 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13009 case though, as some lengths could be zero. */
13010
13011 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)13012 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13013 bool &maybe_zero_len, unsigned int &first_non_one,
13014 enum c_omp_region_type ort)
13015 {
13016 tree ret, low_bound, length, type;
13017 if (TREE_CODE (t) != TREE_LIST)
13018 {
13019 if (error_operand_p (t))
13020 return error_mark_node;
13021 ret = t;
13022 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13023 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13024 {
13025 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13026 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13027 return error_mark_node;
13028 }
13029 if (TREE_CODE (t) == COMPONENT_REF
13030 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13031 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13032 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13033 {
13034 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13035 {
13036 error_at (OMP_CLAUSE_LOCATION (c),
13037 "bit-field %qE in %qs clause",
13038 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13039 return error_mark_node;
13040 }
13041 while (TREE_CODE (t) == COMPONENT_REF)
13042 {
13043 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13044 {
13045 error_at (OMP_CLAUSE_LOCATION (c),
13046 "%qE is a member of a union", t);
13047 return error_mark_node;
13048 }
13049 t = TREE_OPERAND (t, 0);
13050 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13051 {
13052 if (maybe_ne (mem_ref_offset (t), 0))
13053 error_at (OMP_CLAUSE_LOCATION (c),
13054 "cannot dereference %qE in %qs clause", t,
13055 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13056 else
13057 t = TREE_OPERAND (t, 0);
13058 }
13059 }
13060 }
13061 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13062 {
13063 if (DECL_P (t))
13064 error_at (OMP_CLAUSE_LOCATION (c),
13065 "%qD is not a variable in %qs clause", t,
13066 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13067 else
13068 error_at (OMP_CLAUSE_LOCATION (c),
13069 "%qE is not a variable in %qs clause", t,
13070 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13071 return error_mark_node;
13072 }
13073 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13074 && TYPE_ATOMIC (TREE_TYPE (t)))
13075 {
13076 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13077 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13078 return error_mark_node;
13079 }
13080 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13081 && VAR_P (t)
13082 && DECL_THREAD_LOCAL_P (t))
13083 {
13084 error_at (OMP_CLAUSE_LOCATION (c),
13085 "%qD is threadprivate variable in %qs clause", t,
13086 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13087 return error_mark_node;
13088 }
13089 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13090 && TYPE_ATOMIC (TREE_TYPE (t))
13091 && POINTER_TYPE_P (TREE_TYPE (t)))
13092 {
13093 /* If the array section is pointer based and the pointer
13094 itself is _Atomic qualified, we need to atomically load
13095 the pointer. */
13096 c_expr expr;
13097 memset (&expr, 0, sizeof (expr));
13098 expr.value = ret;
13099 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13100 expr, false, false);
13101 ret = expr.value;
13102 }
13103 return ret;
13104 }
13105
13106 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13107 maybe_zero_len, first_non_one, ort);
13108 if (ret == error_mark_node || ret == NULL_TREE)
13109 return ret;
13110
13111 type = TREE_TYPE (ret);
13112 low_bound = TREE_PURPOSE (t);
13113 length = TREE_VALUE (t);
13114
13115 if (low_bound == error_mark_node || length == error_mark_node)
13116 return error_mark_node;
13117
13118 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13119 {
13120 error_at (OMP_CLAUSE_LOCATION (c),
13121 "low bound %qE of array section does not have integral type",
13122 low_bound);
13123 return error_mark_node;
13124 }
13125 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13126 {
13127 error_at (OMP_CLAUSE_LOCATION (c),
13128 "length %qE of array section does not have integral type",
13129 length);
13130 return error_mark_node;
13131 }
13132 if (low_bound
13133 && TREE_CODE (low_bound) == INTEGER_CST
13134 && TYPE_PRECISION (TREE_TYPE (low_bound))
13135 > TYPE_PRECISION (sizetype))
13136 low_bound = fold_convert (sizetype, low_bound);
13137 if (length
13138 && TREE_CODE (length) == INTEGER_CST
13139 && TYPE_PRECISION (TREE_TYPE (length))
13140 > TYPE_PRECISION (sizetype))
13141 length = fold_convert (sizetype, length);
13142 if (low_bound == NULL_TREE)
13143 low_bound = integer_zero_node;
13144 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13145 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13146 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13147 {
13148 if (length != integer_one_node)
13149 {
13150 error_at (OMP_CLAUSE_LOCATION (c),
13151 "expected single pointer in %qs clause",
13152 c_omp_map_clause_name (c, ort == C_ORT_ACC));
13153 return error_mark_node;
13154 }
13155 }
13156 if (length != NULL_TREE)
13157 {
13158 if (!integer_nonzerop (length))
13159 {
13160 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13161 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13162 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13163 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13164 {
13165 if (integer_zerop (length))
13166 {
13167 error_at (OMP_CLAUSE_LOCATION (c),
13168 "zero length array section in %qs clause",
13169 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13170 return error_mark_node;
13171 }
13172 }
13173 else
13174 maybe_zero_len = true;
13175 }
13176 if (first_non_one == types.length ()
13177 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13178 first_non_one++;
13179 }
13180 if (TREE_CODE (type) == ARRAY_TYPE)
13181 {
13182 if (length == NULL_TREE
13183 && (TYPE_DOMAIN (type) == NULL_TREE
13184 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13185 {
13186 error_at (OMP_CLAUSE_LOCATION (c),
13187 "for unknown bound array type length expression must "
13188 "be specified");
13189 return error_mark_node;
13190 }
13191 if (TREE_CODE (low_bound) == INTEGER_CST
13192 && tree_int_cst_sgn (low_bound) == -1)
13193 {
13194 error_at (OMP_CLAUSE_LOCATION (c),
13195 "negative low bound in array section in %qs clause",
13196 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13197 return error_mark_node;
13198 }
13199 if (length != NULL_TREE
13200 && TREE_CODE (length) == INTEGER_CST
13201 && tree_int_cst_sgn (length) == -1)
13202 {
13203 error_at (OMP_CLAUSE_LOCATION (c),
13204 "negative length in array section in %qs clause",
13205 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13206 return error_mark_node;
13207 }
13208 if (TYPE_DOMAIN (type)
13209 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13210 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13211 == INTEGER_CST)
13212 {
13213 tree size
13214 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13215 size = size_binop (PLUS_EXPR, size, size_one_node);
13216 if (TREE_CODE (low_bound) == INTEGER_CST)
13217 {
13218 if (tree_int_cst_lt (size, low_bound))
13219 {
13220 error_at (OMP_CLAUSE_LOCATION (c),
13221 "low bound %qE above array section size "
13222 "in %qs clause", low_bound,
13223 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13224 return error_mark_node;
13225 }
13226 if (tree_int_cst_equal (size, low_bound))
13227 {
13228 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13229 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13230 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13231 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13232 {
13233 error_at (OMP_CLAUSE_LOCATION (c),
13234 "zero length array section in %qs clause",
13235 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13236 return error_mark_node;
13237 }
13238 maybe_zero_len = true;
13239 }
13240 else if (length == NULL_TREE
13241 && first_non_one == types.length ()
13242 && tree_int_cst_equal
13243 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13244 low_bound))
13245 first_non_one++;
13246 }
13247 else if (length == NULL_TREE)
13248 {
13249 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13250 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13251 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13252 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13253 maybe_zero_len = true;
13254 if (first_non_one == types.length ())
13255 first_non_one++;
13256 }
13257 if (length && TREE_CODE (length) == INTEGER_CST)
13258 {
13259 if (tree_int_cst_lt (size, length))
13260 {
13261 error_at (OMP_CLAUSE_LOCATION (c),
13262 "length %qE above array section size "
13263 "in %qs clause", length,
13264 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13265 return error_mark_node;
13266 }
13267 if (TREE_CODE (low_bound) == INTEGER_CST)
13268 {
13269 tree lbpluslen
13270 = size_binop (PLUS_EXPR,
13271 fold_convert (sizetype, low_bound),
13272 fold_convert (sizetype, length));
13273 if (TREE_CODE (lbpluslen) == INTEGER_CST
13274 && tree_int_cst_lt (size, lbpluslen))
13275 {
13276 error_at (OMP_CLAUSE_LOCATION (c),
13277 "high bound %qE above array section size "
13278 "in %qs clause", lbpluslen,
13279 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13280 return error_mark_node;
13281 }
13282 }
13283 }
13284 }
13285 else if (length == NULL_TREE)
13286 {
13287 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13288 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13289 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13290 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13291 maybe_zero_len = true;
13292 if (first_non_one == types.length ())
13293 first_non_one++;
13294 }
13295
13296 /* For [lb:] we will need to evaluate lb more than once. */
13297 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13298 {
13299 tree lb = save_expr (low_bound);
13300 if (lb != low_bound)
13301 {
13302 TREE_PURPOSE (t) = lb;
13303 low_bound = lb;
13304 }
13305 }
13306 }
13307 else if (TREE_CODE (type) == POINTER_TYPE)
13308 {
13309 if (length == NULL_TREE)
13310 {
13311 error_at (OMP_CLAUSE_LOCATION (c),
13312 "for pointer type length expression must be specified");
13313 return error_mark_node;
13314 }
13315 if (length != NULL_TREE
13316 && TREE_CODE (length) == INTEGER_CST
13317 && tree_int_cst_sgn (length) == -1)
13318 {
13319 error_at (OMP_CLAUSE_LOCATION (c),
13320 "negative length in array section in %qs clause",
13321 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13322 return error_mark_node;
13323 }
13324 /* If there is a pointer type anywhere but in the very first
13325 array-section-subscript, the array section can't be contiguous. */
13326 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13327 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13328 {
13329 error_at (OMP_CLAUSE_LOCATION (c),
13330 "array section is not contiguous in %qs clause",
13331 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13332 return error_mark_node;
13333 }
13334 }
13335 else
13336 {
13337 error_at (OMP_CLAUSE_LOCATION (c),
13338 "%qE does not have pointer or array type", ret);
13339 return error_mark_node;
13340 }
13341 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13342 types.safe_push (TREE_TYPE (ret));
13343 /* We will need to evaluate lb more than once. */
13344 tree lb = save_expr (low_bound);
13345 if (lb != low_bound)
13346 {
13347 TREE_PURPOSE (t) = lb;
13348 low_bound = lb;
13349 }
13350 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13351 return ret;
13352 }
13353
13354 /* Handle array sections for clause C. */
13355
13356 static bool
handle_omp_array_sections(tree c,enum c_omp_region_type ort)13357 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13358 {
13359 bool maybe_zero_len = false;
13360 unsigned int first_non_one = 0;
13361 auto_vec<tree, 10> types;
13362 tree *tp = &OMP_CLAUSE_DECL (c);
13363 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13364 && TREE_CODE (*tp) == TREE_LIST
13365 && TREE_PURPOSE (*tp)
13366 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13367 tp = &TREE_VALUE (*tp);
13368 tree first = handle_omp_array_sections_1 (c, *tp, types,
13369 maybe_zero_len, first_non_one,
13370 ort);
13371 if (first == error_mark_node)
13372 return true;
13373 if (first == NULL_TREE)
13374 return false;
13375 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13376 {
13377 tree t = *tp;
13378 tree tem = NULL_TREE;
13379 /* Need to evaluate side effects in the length expressions
13380 if any. */
13381 while (TREE_CODE (t) == TREE_LIST)
13382 {
13383 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13384 {
13385 if (tem == NULL_TREE)
13386 tem = TREE_VALUE (t);
13387 else
13388 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13389 TREE_VALUE (t), tem);
13390 }
13391 t = TREE_CHAIN (t);
13392 }
13393 if (tem)
13394 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13395 first = c_fully_fold (first, false, NULL, true);
13396 *tp = first;
13397 }
13398 else
13399 {
13400 unsigned int num = types.length (), i;
13401 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13402 tree condition = NULL_TREE;
13403
13404 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13405 maybe_zero_len = true;
13406
13407 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13408 t = TREE_CHAIN (t))
13409 {
13410 tree low_bound = TREE_PURPOSE (t);
13411 tree length = TREE_VALUE (t);
13412
13413 i--;
13414 if (low_bound
13415 && TREE_CODE (low_bound) == INTEGER_CST
13416 && TYPE_PRECISION (TREE_TYPE (low_bound))
13417 > TYPE_PRECISION (sizetype))
13418 low_bound = fold_convert (sizetype, low_bound);
13419 if (length
13420 && TREE_CODE (length) == INTEGER_CST
13421 && TYPE_PRECISION (TREE_TYPE (length))
13422 > TYPE_PRECISION (sizetype))
13423 length = fold_convert (sizetype, length);
13424 if (low_bound == NULL_TREE)
13425 low_bound = integer_zero_node;
13426 if (!maybe_zero_len && i > first_non_one)
13427 {
13428 if (integer_nonzerop (low_bound))
13429 goto do_warn_noncontiguous;
13430 if (length != NULL_TREE
13431 && TREE_CODE (length) == INTEGER_CST
13432 && TYPE_DOMAIN (types[i])
13433 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13434 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13435 == INTEGER_CST)
13436 {
13437 tree size;
13438 size = size_binop (PLUS_EXPR,
13439 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13440 size_one_node);
13441 if (!tree_int_cst_equal (length, size))
13442 {
13443 do_warn_noncontiguous:
13444 error_at (OMP_CLAUSE_LOCATION (c),
13445 "array section is not contiguous in %qs "
13446 "clause",
13447 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13448 return true;
13449 }
13450 }
13451 if (length != NULL_TREE
13452 && TREE_SIDE_EFFECTS (length))
13453 {
13454 if (side_effects == NULL_TREE)
13455 side_effects = length;
13456 else
13457 side_effects = build2 (COMPOUND_EXPR,
13458 TREE_TYPE (side_effects),
13459 length, side_effects);
13460 }
13461 }
13462 else
13463 {
13464 tree l;
13465
13466 if (i > first_non_one
13467 && ((length && integer_nonzerop (length))
13468 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13469 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13470 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13471 continue;
13472 if (length)
13473 l = fold_convert (sizetype, length);
13474 else
13475 {
13476 l = size_binop (PLUS_EXPR,
13477 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13478 size_one_node);
13479 l = size_binop (MINUS_EXPR, l,
13480 fold_convert (sizetype, low_bound));
13481 }
13482 if (i > first_non_one)
13483 {
13484 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13485 size_zero_node);
13486 if (condition == NULL_TREE)
13487 condition = l;
13488 else
13489 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13490 l, condition);
13491 }
13492 else if (size == NULL_TREE)
13493 {
13494 size = size_in_bytes (TREE_TYPE (types[i]));
13495 tree eltype = TREE_TYPE (types[num - 1]);
13496 while (TREE_CODE (eltype) == ARRAY_TYPE)
13497 eltype = TREE_TYPE (eltype);
13498 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13499 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13500 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13501 {
13502 if (integer_zerop (size)
13503 || integer_zerop (size_in_bytes (eltype)))
13504 {
13505 error_at (OMP_CLAUSE_LOCATION (c),
13506 "zero length array section in %qs clause",
13507 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13508 return error_mark_node;
13509 }
13510 size = size_binop (EXACT_DIV_EXPR, size,
13511 size_in_bytes (eltype));
13512 }
13513 size = size_binop (MULT_EXPR, size, l);
13514 if (condition)
13515 size = fold_build3 (COND_EXPR, sizetype, condition,
13516 size, size_zero_node);
13517 }
13518 else
13519 size = size_binop (MULT_EXPR, size, l);
13520 }
13521 }
13522 if (side_effects)
13523 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13524 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13525 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13526 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13527 {
13528 size = size_binop (MINUS_EXPR, size, size_one_node);
13529 size = c_fully_fold (size, false, NULL);
13530 size = save_expr (size);
13531 tree index_type = build_index_type (size);
13532 tree eltype = TREE_TYPE (first);
13533 while (TREE_CODE (eltype) == ARRAY_TYPE)
13534 eltype = TREE_TYPE (eltype);
13535 tree type = build_array_type (eltype, index_type);
13536 tree ptype = build_pointer_type (eltype);
13537 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13538 t = build_fold_addr_expr (t);
13539 tree t2 = build_fold_addr_expr (first);
13540 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13541 ptrdiff_type_node, t2);
13542 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13543 ptrdiff_type_node, t2,
13544 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13545 ptrdiff_type_node, t));
13546 t2 = c_fully_fold (t2, false, NULL);
13547 if (tree_fits_shwi_p (t2))
13548 t = build2 (MEM_REF, type, t,
13549 build_int_cst (ptype, tree_to_shwi (t2)));
13550 else
13551 {
13552 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13553 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13554 TREE_TYPE (t), t, t2);
13555 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13556 }
13557 OMP_CLAUSE_DECL (c) = t;
13558 return false;
13559 }
13560 first = c_fully_fold (first, false, NULL);
13561 OMP_CLAUSE_DECL (c) = first;
13562 if (size)
13563 size = c_fully_fold (size, false, NULL);
13564 OMP_CLAUSE_SIZE (c) = size;
13565 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13566 || (TREE_CODE (t) == COMPONENT_REF
13567 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13568 return false;
13569 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13570 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
13571 switch (OMP_CLAUSE_MAP_KIND (c))
13572 {
13573 case GOMP_MAP_ALLOC:
13574 case GOMP_MAP_IF_PRESENT:
13575 case GOMP_MAP_TO:
13576 case GOMP_MAP_FROM:
13577 case GOMP_MAP_TOFROM:
13578 case GOMP_MAP_ALWAYS_TO:
13579 case GOMP_MAP_ALWAYS_FROM:
13580 case GOMP_MAP_ALWAYS_TOFROM:
13581 case GOMP_MAP_RELEASE:
13582 case GOMP_MAP_DELETE:
13583 case GOMP_MAP_FORCE_TO:
13584 case GOMP_MAP_FORCE_FROM:
13585 case GOMP_MAP_FORCE_TOFROM:
13586 case GOMP_MAP_FORCE_PRESENT:
13587 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13588 break;
13589 default:
13590 break;
13591 }
13592 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13593 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13594 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13595 else if (TREE_CODE (t) == COMPONENT_REF)
13596 {
13597 gomp_map_kind k = (ort == C_ORT_ACC) ? GOMP_MAP_ATTACH_DETACH
13598 : GOMP_MAP_ALWAYS_POINTER;
13599 OMP_CLAUSE_SET_MAP_KIND (c2, k);
13600 }
13601 else
13602 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13603 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13604 && !c_mark_addressable (t))
13605 return false;
13606 OMP_CLAUSE_DECL (c2) = t;
13607 t = build_fold_addr_expr (first);
13608 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13609 tree ptr = OMP_CLAUSE_DECL (c2);
13610 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13611 ptr = build_fold_addr_expr (ptr);
13612 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13613 ptrdiff_type_node, t,
13614 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13615 ptrdiff_type_node, ptr));
13616 t = c_fully_fold (t, false, NULL);
13617 OMP_CLAUSE_SIZE (c2) = t;
13618 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13619 OMP_CLAUSE_CHAIN (c) = c2;
13620 }
13621 return false;
13622 }
13623
13624 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13625 an inline call. But, remap
13626 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13627 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13628
13629 static tree
c_clone_omp_udr(tree stmt,tree omp_decl1,tree omp_decl2,tree decl,tree placeholder)13630 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13631 tree decl, tree placeholder)
13632 {
13633 copy_body_data id;
13634 hash_map<tree, tree> decl_map;
13635
13636 decl_map.put (omp_decl1, placeholder);
13637 decl_map.put (omp_decl2, decl);
13638 memset (&id, 0, sizeof (id));
13639 id.src_fn = DECL_CONTEXT (omp_decl1);
13640 id.dst_fn = current_function_decl;
13641 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13642 id.decl_map = &decl_map;
13643
13644 id.copy_decl = copy_decl_no_change;
13645 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13646 id.transform_new_cfg = true;
13647 id.transform_return_to_modify = false;
13648 id.transform_lang_insert_block = NULL;
13649 id.eh_lp_nr = 0;
13650 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13651 return stmt;
13652 }
13653
13654 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13655 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13656
13657 static tree
c_find_omp_placeholder_r(tree * tp,int *,void * data)13658 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13659 {
13660 if (*tp == (tree) data)
13661 return *tp;
13662 return NULL_TREE;
13663 }
13664
13665 /* Similarly, but also walk aggregate fields. */
13666
13667 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13668
13669 static tree
c_find_omp_var_r(tree * tp,int *,void * data)13670 c_find_omp_var_r (tree *tp, int *, void *data)
13671 {
13672 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13673 return *tp;
13674 if (RECORD_OR_UNION_TYPE_P (*tp))
13675 {
13676 tree field;
13677 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13678
13679 for (field = TYPE_FIELDS (*tp); field;
13680 field = DECL_CHAIN (field))
13681 if (TREE_CODE (field) == FIELD_DECL)
13682 {
13683 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13684 c_find_omp_var_r, data, pset);
13685 if (ret)
13686 return ret;
13687 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13688 if (ret)
13689 return ret;
13690 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13691 pset);
13692 if (ret)
13693 return ret;
13694 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13695 if (ret)
13696 return ret;
13697 }
13698 }
13699 else if (INTEGRAL_TYPE_P (*tp))
13700 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13701 ((struct c_find_omp_var_s *) data)->pset);
13702 return NULL_TREE;
13703 }
13704
13705 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13706 and clauses containing them should be removed. */
13707
13708 static bool
c_omp_finish_iterators(tree iter)13709 c_omp_finish_iterators (tree iter)
13710 {
13711 bool ret = false;
13712 for (tree it = iter; it; it = TREE_CHAIN (it))
13713 {
13714 tree var = TREE_VEC_ELT (it, 0);
13715 tree begin = TREE_VEC_ELT (it, 1);
13716 tree end = TREE_VEC_ELT (it, 2);
13717 tree step = TREE_VEC_ELT (it, 3);
13718 tree orig_step;
13719 tree type = TREE_TYPE (var);
13720 location_t loc = DECL_SOURCE_LOCATION (var);
13721 if (type == error_mark_node)
13722 {
13723 ret = true;
13724 continue;
13725 }
13726 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13727 {
13728 error_at (loc, "iterator %qD has neither integral nor pointer type",
13729 var);
13730 ret = true;
13731 continue;
13732 }
13733 else if (TYPE_ATOMIC (type))
13734 {
13735 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13736 ret = true;
13737 continue;
13738 }
13739 else if (TYPE_READONLY (type))
13740 {
13741 error_at (loc, "iterator %qD has const qualified type", var);
13742 ret = true;
13743 continue;
13744 }
13745 else if (step == error_mark_node
13746 || TREE_TYPE (step) == error_mark_node)
13747 {
13748 ret = true;
13749 continue;
13750 }
13751 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13752 {
13753 error_at (EXPR_LOC_OR_LOC (step, loc),
13754 "iterator step with non-integral type");
13755 ret = true;
13756 continue;
13757 }
13758 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13759 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13760 orig_step = save_expr (c_fully_fold (step, false, NULL));
13761 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13762 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13763 if (POINTER_TYPE_P (type))
13764 {
13765 begin = save_expr (begin);
13766 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13767 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13768 fold_convert (sizetype, step),
13769 fold_convert (sizetype, begin));
13770 step = fold_convert (ssizetype, step);
13771 }
13772 if (integer_zerop (step))
13773 {
13774 error_at (loc, "iterator %qD has zero step", var);
13775 ret = true;
13776 continue;
13777 }
13778
13779 if (begin == error_mark_node
13780 || end == error_mark_node
13781 || step == error_mark_node
13782 || orig_step == error_mark_node)
13783 {
13784 ret = true;
13785 continue;
13786 }
13787 hash_set<tree> pset;
13788 tree it2;
13789 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13790 {
13791 tree var2 = TREE_VEC_ELT (it2, 0);
13792 tree begin2 = TREE_VEC_ELT (it2, 1);
13793 tree end2 = TREE_VEC_ELT (it2, 2);
13794 tree step2 = TREE_VEC_ELT (it2, 3);
13795 tree type2 = TREE_TYPE (var2);
13796 location_t loc2 = DECL_SOURCE_LOCATION (var2);
13797 struct c_find_omp_var_s data = { var, &pset };
13798 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13799 {
13800 error_at (loc2,
13801 "type of iterator %qD refers to outer iterator %qD",
13802 var2, var);
13803 break;
13804 }
13805 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13806 {
13807 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13808 "begin expression refers to outer iterator %qD", var);
13809 break;
13810 }
13811 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13812 {
13813 error_at (EXPR_LOC_OR_LOC (end2, loc2),
13814 "end expression refers to outer iterator %qD", var);
13815 break;
13816 }
13817 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13818 {
13819 error_at (EXPR_LOC_OR_LOC (step2, loc2),
13820 "step expression refers to outer iterator %qD", var);
13821 break;
13822 }
13823 }
13824 if (it2)
13825 {
13826 ret = true;
13827 continue;
13828 }
13829 TREE_VEC_ELT (it, 1) = begin;
13830 TREE_VEC_ELT (it, 2) = end;
13831 TREE_VEC_ELT (it, 3) = step;
13832 TREE_VEC_ELT (it, 4) = orig_step;
13833 }
13834 return ret;
13835 }
13836
13837 /* Ensure that pointers are used in OpenACC attach and detach clauses.
13838 Return true if an error has been detected. */
13839
13840 static bool
c_oacc_check_attachments(tree c)13841 c_oacc_check_attachments (tree c)
13842 {
13843 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13844 return false;
13845
13846 /* OpenACC attach / detach clauses must be pointers. */
13847 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13848 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
13849 {
13850 tree t = OMP_CLAUSE_DECL (c);
13851
13852 while (TREE_CODE (t) == TREE_LIST)
13853 t = TREE_CHAIN (t);
13854
13855 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13856 {
13857 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
13858 c_omp_map_clause_name (c, true));
13859 return true;
13860 }
13861 }
13862
13863 return false;
13864 }
13865
13866 /* For all elements of CLAUSES, validate them against their constraints.
13867 Remove any elements from the list that are invalid. */
13868
13869 tree
c_finish_omp_clauses(tree clauses,enum c_omp_region_type ort)13870 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13871 {
13872 bitmap_head generic_head, firstprivate_head, lastprivate_head;
13873 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
13874 tree c, t, type, *pc;
13875 tree simdlen = NULL_TREE, safelen = NULL_TREE;
13876 bool branch_seen = false;
13877 bool copyprivate_seen = false;
13878 bool linear_variable_step_check = false;
13879 tree *nowait_clause = NULL;
13880 tree ordered_clause = NULL_TREE;
13881 tree schedule_clause = NULL_TREE;
13882 bool oacc_async = false;
13883 tree last_iterators = NULL_TREE;
13884 bool last_iterators_remove = false;
13885 tree *nogroup_seen = NULL;
13886 tree *order_clause = NULL;
13887 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
13888 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
13889 int reduction_seen = 0;
13890
13891 bitmap_obstack_initialize (NULL);
13892 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13893 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13894 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13895 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13896 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
13897 bitmap_initialize (&map_head, &bitmap_default_obstack);
13898 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13899 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
13900 instead. */
13901 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13902
13903 if (ort & C_ORT_ACC)
13904 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13905 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13906 {
13907 oacc_async = true;
13908 break;
13909 }
13910
13911 for (pc = &clauses, c = clauses; c ; c = *pc)
13912 {
13913 bool remove = false;
13914 bool need_complete = false;
13915 bool need_implicitly_determined = false;
13916
13917 switch (OMP_CLAUSE_CODE (c))
13918 {
13919 case OMP_CLAUSE_SHARED:
13920 need_implicitly_determined = true;
13921 goto check_dup_generic;
13922
13923 case OMP_CLAUSE_PRIVATE:
13924 need_complete = true;
13925 need_implicitly_determined = true;
13926 goto check_dup_generic;
13927
13928 case OMP_CLAUSE_REDUCTION:
13929 if (reduction_seen == 0)
13930 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
13931 else if (reduction_seen != -2
13932 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
13933 ? -1 : 1))
13934 {
13935 error_at (OMP_CLAUSE_LOCATION (c),
13936 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
13937 "on the same construct");
13938 reduction_seen = -2;
13939 }
13940 /* FALLTHRU */
13941 case OMP_CLAUSE_IN_REDUCTION:
13942 case OMP_CLAUSE_TASK_REDUCTION:
13943 need_implicitly_determined = true;
13944 t = OMP_CLAUSE_DECL (c);
13945 if (TREE_CODE (t) == TREE_LIST)
13946 {
13947 if (handle_omp_array_sections (c, ort))
13948 {
13949 remove = true;
13950 break;
13951 }
13952
13953 t = OMP_CLAUSE_DECL (c);
13954 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13955 && OMP_CLAUSE_REDUCTION_INSCAN (c))
13956 {
13957 error_at (OMP_CLAUSE_LOCATION (c),
13958 "%<inscan%> %<reduction%> clause with array "
13959 "section");
13960 remove = true;
13961 break;
13962 }
13963 }
13964 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13965 if (t == error_mark_node)
13966 {
13967 remove = true;
13968 break;
13969 }
13970 if (oacc_async)
13971 c_mark_addressable (t);
13972 type = TREE_TYPE (t);
13973 if (TREE_CODE (t) == MEM_REF)
13974 type = TREE_TYPE (type);
13975 if (TREE_CODE (type) == ARRAY_TYPE)
13976 {
13977 tree oatype = type;
13978 gcc_assert (TREE_CODE (t) != MEM_REF);
13979 while (TREE_CODE (type) == ARRAY_TYPE)
13980 type = TREE_TYPE (type);
13981 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13982 {
13983 error_at (OMP_CLAUSE_LOCATION (c),
13984 "%qD in %<reduction%> clause is a zero size array",
13985 t);
13986 remove = true;
13987 break;
13988 }
13989 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13990 TYPE_SIZE_UNIT (type));
13991 if (integer_zerop (size))
13992 {
13993 error_at (OMP_CLAUSE_LOCATION (c),
13994 "%qD in %<reduction%> clause is a zero size array",
13995 t);
13996 remove = true;
13997 break;
13998 }
13999 size = size_binop (MINUS_EXPR, size, size_one_node);
14000 size = save_expr (size);
14001 tree index_type = build_index_type (size);
14002 tree atype = build_array_type (type, index_type);
14003 tree ptype = build_pointer_type (type);
14004 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14005 t = build_fold_addr_expr (t);
14006 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14007 OMP_CLAUSE_DECL (c) = t;
14008 }
14009 if (TYPE_ATOMIC (type))
14010 {
14011 error_at (OMP_CLAUSE_LOCATION (c),
14012 "%<_Atomic%> %qE in %<reduction%> clause", t);
14013 remove = true;
14014 break;
14015 }
14016 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14017 || OMP_CLAUSE_REDUCTION_TASK (c))
14018 {
14019 /* Disallow zero sized or potentially zero sized task
14020 reductions. */
14021 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14022 {
14023 error_at (OMP_CLAUSE_LOCATION (c),
14024 "zero sized type %qT in %qs clause", type,
14025 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14026 remove = true;
14027 break;
14028 }
14029 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14030 {
14031 error_at (OMP_CLAUSE_LOCATION (c),
14032 "variable sized type %qT in %qs clause", type,
14033 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14034 remove = true;
14035 break;
14036 }
14037 }
14038 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14039 && (FLOAT_TYPE_P (type)
14040 || TREE_CODE (type) == COMPLEX_TYPE))
14041 {
14042 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14043 const char *r_name = NULL;
14044
14045 switch (r_code)
14046 {
14047 case PLUS_EXPR:
14048 case MULT_EXPR:
14049 case MINUS_EXPR:
14050 break;
14051 case MIN_EXPR:
14052 if (TREE_CODE (type) == COMPLEX_TYPE)
14053 r_name = "min";
14054 break;
14055 case MAX_EXPR:
14056 if (TREE_CODE (type) == COMPLEX_TYPE)
14057 r_name = "max";
14058 break;
14059 case BIT_AND_EXPR:
14060 r_name = "&";
14061 break;
14062 case BIT_XOR_EXPR:
14063 r_name = "^";
14064 break;
14065 case BIT_IOR_EXPR:
14066 r_name = "|";
14067 break;
14068 case TRUTH_ANDIF_EXPR:
14069 if (FLOAT_TYPE_P (type))
14070 r_name = "&&";
14071 break;
14072 case TRUTH_ORIF_EXPR:
14073 if (FLOAT_TYPE_P (type))
14074 r_name = "||";
14075 break;
14076 default:
14077 gcc_unreachable ();
14078 }
14079 if (r_name)
14080 {
14081 error_at (OMP_CLAUSE_LOCATION (c),
14082 "%qE has invalid type for %<reduction(%s)%>",
14083 t, r_name);
14084 remove = true;
14085 break;
14086 }
14087 }
14088 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14089 {
14090 error_at (OMP_CLAUSE_LOCATION (c),
14091 "user defined reduction not found for %qE", t);
14092 remove = true;
14093 break;
14094 }
14095 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14096 {
14097 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14098 type = TYPE_MAIN_VARIANT (type);
14099 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14100 VAR_DECL, NULL_TREE, type);
14101 tree decl_placeholder = NULL_TREE;
14102 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14103 DECL_ARTIFICIAL (placeholder) = 1;
14104 DECL_IGNORED_P (placeholder) = 1;
14105 if (TREE_CODE (t) == MEM_REF)
14106 {
14107 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14108 VAR_DECL, NULL_TREE, type);
14109 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14110 DECL_ARTIFICIAL (decl_placeholder) = 1;
14111 DECL_IGNORED_P (decl_placeholder) = 1;
14112 }
14113 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14114 c_mark_addressable (placeholder);
14115 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14116 c_mark_addressable (decl_placeholder ? decl_placeholder
14117 : OMP_CLAUSE_DECL (c));
14118 OMP_CLAUSE_REDUCTION_MERGE (c)
14119 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14120 TREE_VEC_ELT (list, 0),
14121 TREE_VEC_ELT (list, 1),
14122 decl_placeholder ? decl_placeholder
14123 : OMP_CLAUSE_DECL (c), placeholder);
14124 OMP_CLAUSE_REDUCTION_MERGE (c)
14125 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14126 void_type_node, NULL_TREE,
14127 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14128 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14129 if (TREE_VEC_LENGTH (list) == 6)
14130 {
14131 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14132 c_mark_addressable (decl_placeholder ? decl_placeholder
14133 : OMP_CLAUSE_DECL (c));
14134 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14135 c_mark_addressable (placeholder);
14136 tree init = TREE_VEC_ELT (list, 5);
14137 if (init == error_mark_node)
14138 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14139 OMP_CLAUSE_REDUCTION_INIT (c)
14140 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14141 TREE_VEC_ELT (list, 3),
14142 decl_placeholder ? decl_placeholder
14143 : OMP_CLAUSE_DECL (c), placeholder);
14144 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14145 {
14146 tree v = decl_placeholder ? decl_placeholder : t;
14147 OMP_CLAUSE_REDUCTION_INIT (c)
14148 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14149 OMP_CLAUSE_REDUCTION_INIT (c));
14150 }
14151 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14152 c_find_omp_placeholder_r,
14153 placeholder, NULL))
14154 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14155 }
14156 else
14157 {
14158 tree init;
14159 tree v = decl_placeholder ? decl_placeholder : t;
14160 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14161 init = build_constructor (TREE_TYPE (v), NULL);
14162 else
14163 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14164 OMP_CLAUSE_REDUCTION_INIT (c)
14165 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14166 }
14167 OMP_CLAUSE_REDUCTION_INIT (c)
14168 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14169 void_type_node, NULL_TREE,
14170 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14171 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14172 }
14173 if (TREE_CODE (t) == MEM_REF)
14174 {
14175 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14176 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14177 != INTEGER_CST)
14178 {
14179 sorry ("variable length element type in array "
14180 "%<reduction%> clause");
14181 remove = true;
14182 break;
14183 }
14184 t = TREE_OPERAND (t, 0);
14185 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14186 t = TREE_OPERAND (t, 0);
14187 if (TREE_CODE (t) == ADDR_EXPR)
14188 t = TREE_OPERAND (t, 0);
14189 }
14190 goto check_dup_generic_t;
14191
14192 case OMP_CLAUSE_COPYPRIVATE:
14193 copyprivate_seen = true;
14194 if (nowait_clause)
14195 {
14196 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14197 "%<nowait%> clause must not be used together "
14198 "with %<copyprivate%>");
14199 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14200 nowait_clause = NULL;
14201 }
14202 goto check_dup_generic;
14203
14204 case OMP_CLAUSE_COPYIN:
14205 t = OMP_CLAUSE_DECL (c);
14206 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14207 {
14208 error_at (OMP_CLAUSE_LOCATION (c),
14209 "%qE must be %<threadprivate%> for %<copyin%>", t);
14210 remove = true;
14211 break;
14212 }
14213 goto check_dup_generic;
14214
14215 case OMP_CLAUSE_LINEAR:
14216 if (ort != C_ORT_OMP_DECLARE_SIMD)
14217 need_implicitly_determined = true;
14218 t = OMP_CLAUSE_DECL (c);
14219 if (ort != C_ORT_OMP_DECLARE_SIMD
14220 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
14221 {
14222 error_at (OMP_CLAUSE_LOCATION (c),
14223 "modifier should not be specified in %<linear%> "
14224 "clause on %<simd%> or %<for%> constructs");
14225 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14226 }
14227 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14228 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14229 {
14230 error_at (OMP_CLAUSE_LOCATION (c),
14231 "linear clause applied to non-integral non-pointer "
14232 "variable with type %qT", TREE_TYPE (t));
14233 remove = true;
14234 break;
14235 }
14236 if (TYPE_ATOMIC (TREE_TYPE (t)))
14237 {
14238 error_at (OMP_CLAUSE_LOCATION (c),
14239 "%<_Atomic%> %qD in %<linear%> clause", t);
14240 remove = true;
14241 break;
14242 }
14243 if (ort == C_ORT_OMP_DECLARE_SIMD)
14244 {
14245 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14246 if (TREE_CODE (s) == PARM_DECL)
14247 {
14248 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14249 /* map_head bitmap is used as uniform_head if
14250 declare_simd. */
14251 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14252 linear_variable_step_check = true;
14253 goto check_dup_generic;
14254 }
14255 if (TREE_CODE (s) != INTEGER_CST)
14256 {
14257 error_at (OMP_CLAUSE_LOCATION (c),
14258 "%<linear%> clause step %qE is neither constant "
14259 "nor a parameter", s);
14260 remove = true;
14261 break;
14262 }
14263 }
14264 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14265 {
14266 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14267 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14268 OMP_CLAUSE_DECL (c), s);
14269 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14270 sizetype, fold_convert (sizetype, s),
14271 fold_convert
14272 (sizetype, OMP_CLAUSE_DECL (c)));
14273 if (s == error_mark_node)
14274 s = size_one_node;
14275 OMP_CLAUSE_LINEAR_STEP (c) = s;
14276 }
14277 else
14278 OMP_CLAUSE_LINEAR_STEP (c)
14279 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14280 goto check_dup_generic;
14281
14282 check_dup_generic:
14283 t = OMP_CLAUSE_DECL (c);
14284 check_dup_generic_t:
14285 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14286 {
14287 error_at (OMP_CLAUSE_LOCATION (c),
14288 "%qE is not a variable in clause %qs", t,
14289 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14290 remove = true;
14291 }
14292 else if ((ort == C_ORT_ACC
14293 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14294 || (ort == C_ORT_OMP
14295 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14296 || (OMP_CLAUSE_CODE (c)
14297 == OMP_CLAUSE_USE_DEVICE_ADDR))))
14298 {
14299 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14300 {
14301 error_at (OMP_CLAUSE_LOCATION (c),
14302 ort == C_ORT_ACC
14303 ? "%qD appears more than once in reduction clauses"
14304 : "%qD appears more than once in data clauses",
14305 t);
14306 remove = true;
14307 }
14308 else
14309 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14310 }
14311 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14312 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14313 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14314 {
14315 error_at (OMP_CLAUSE_LOCATION (c),
14316 "%qE appears more than once in data clauses", t);
14317 remove = true;
14318 }
14319 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14320 && bitmap_bit_p (&map_head, DECL_UID (t)))
14321 {
14322 if (ort == C_ORT_ACC)
14323 error_at (OMP_CLAUSE_LOCATION (c),
14324 "%qD appears more than once in data clauses", t);
14325 else
14326 error_at (OMP_CLAUSE_LOCATION (c),
14327 "%qD appears both in data and map clauses", t);
14328 remove = true;
14329 }
14330 else
14331 bitmap_set_bit (&generic_head, DECL_UID (t));
14332 break;
14333
14334 case OMP_CLAUSE_FIRSTPRIVATE:
14335 t = OMP_CLAUSE_DECL (c);
14336 need_complete = true;
14337 need_implicitly_determined = true;
14338 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14339 {
14340 error_at (OMP_CLAUSE_LOCATION (c),
14341 "%qE is not a variable in clause %<firstprivate%>", t);
14342 remove = true;
14343 }
14344 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14345 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14346 {
14347 error_at (OMP_CLAUSE_LOCATION (c),
14348 "%qE appears more than once in data clauses", t);
14349 remove = true;
14350 }
14351 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14352 {
14353 if (ort == C_ORT_ACC)
14354 error_at (OMP_CLAUSE_LOCATION (c),
14355 "%qD appears more than once in data clauses", t);
14356 else
14357 error_at (OMP_CLAUSE_LOCATION (c),
14358 "%qD appears both in data and map clauses", t);
14359 remove = true;
14360 }
14361 else
14362 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14363 break;
14364
14365 case OMP_CLAUSE_LASTPRIVATE:
14366 t = OMP_CLAUSE_DECL (c);
14367 need_complete = true;
14368 need_implicitly_determined = true;
14369 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14370 {
14371 error_at (OMP_CLAUSE_LOCATION (c),
14372 "%qE is not a variable in clause %<lastprivate%>", t);
14373 remove = true;
14374 }
14375 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14376 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14377 {
14378 error_at (OMP_CLAUSE_LOCATION (c),
14379 "%qE appears more than once in data clauses", t);
14380 remove = true;
14381 }
14382 else
14383 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14384 break;
14385
14386 case OMP_CLAUSE_ALIGNED:
14387 t = OMP_CLAUSE_DECL (c);
14388 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14389 {
14390 error_at (OMP_CLAUSE_LOCATION (c),
14391 "%qE is not a variable in %<aligned%> clause", t);
14392 remove = true;
14393 }
14394 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14395 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14396 {
14397 error_at (OMP_CLAUSE_LOCATION (c),
14398 "%qE in %<aligned%> clause is neither a pointer nor "
14399 "an array", t);
14400 remove = true;
14401 }
14402 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14403 {
14404 error_at (OMP_CLAUSE_LOCATION (c),
14405 "%<_Atomic%> %qD in %<aligned%> clause", t);
14406 remove = true;
14407 break;
14408 }
14409 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14410 {
14411 error_at (OMP_CLAUSE_LOCATION (c),
14412 "%qE appears more than once in %<aligned%> clauses",
14413 t);
14414 remove = true;
14415 }
14416 else
14417 bitmap_set_bit (&aligned_head, DECL_UID (t));
14418 break;
14419
14420 case OMP_CLAUSE_NONTEMPORAL:
14421 t = OMP_CLAUSE_DECL (c);
14422 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14423 {
14424 error_at (OMP_CLAUSE_LOCATION (c),
14425 "%qE is not a variable in %<nontemporal%> clause", t);
14426 remove = true;
14427 }
14428 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14429 {
14430 error_at (OMP_CLAUSE_LOCATION (c),
14431 "%qE appears more than once in %<nontemporal%> "
14432 "clauses", t);
14433 remove = true;
14434 }
14435 else
14436 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14437 break;
14438
14439 case OMP_CLAUSE_DEPEND:
14440 t = OMP_CLAUSE_DECL (c);
14441 if (t == NULL_TREE)
14442 {
14443 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14444 == OMP_CLAUSE_DEPEND_SOURCE);
14445 break;
14446 }
14447 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14448 {
14449 gcc_assert (TREE_CODE (t) == TREE_LIST);
14450 for (; t; t = TREE_CHAIN (t))
14451 {
14452 tree decl = TREE_VALUE (t);
14453 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14454 {
14455 tree offset = TREE_PURPOSE (t);
14456 bool neg = wi::neg_p (wi::to_wide (offset));
14457 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14458 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14459 neg ? MINUS_EXPR : PLUS_EXPR,
14460 decl, offset);
14461 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14462 sizetype,
14463 fold_convert (sizetype, t2),
14464 fold_convert (sizetype, decl));
14465 if (t2 == error_mark_node)
14466 {
14467 remove = true;
14468 break;
14469 }
14470 TREE_PURPOSE (t) = t2;
14471 }
14472 }
14473 break;
14474 }
14475 if (TREE_CODE (t) == TREE_LIST
14476 && TREE_PURPOSE (t)
14477 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14478 {
14479 if (TREE_PURPOSE (t) != last_iterators)
14480 last_iterators_remove
14481 = c_omp_finish_iterators (TREE_PURPOSE (t));
14482 last_iterators = TREE_PURPOSE (t);
14483 t = TREE_VALUE (t);
14484 if (last_iterators_remove)
14485 t = error_mark_node;
14486 }
14487 else
14488 last_iterators = NULL_TREE;
14489 if (TREE_CODE (t) == TREE_LIST)
14490 {
14491 if (handle_omp_array_sections (c, ort))
14492 remove = true;
14493 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14494 {
14495 error_at (OMP_CLAUSE_LOCATION (c),
14496 "%<depend%> clause with %<depobj%> dependence "
14497 "type on array section");
14498 remove = true;
14499 }
14500 break;
14501 }
14502 if (t == error_mark_node)
14503 remove = true;
14504 else if (!lvalue_p (t))
14505 {
14506 error_at (OMP_CLAUSE_LOCATION (c),
14507 "%qE is not lvalue expression nor array section in "
14508 "%<depend%> clause", t);
14509 remove = true;
14510 }
14511 else if (TREE_CODE (t) == COMPONENT_REF
14512 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14513 {
14514 error_at (OMP_CLAUSE_LOCATION (c),
14515 "bit-field %qE in %qs clause", t, "depend");
14516 remove = true;
14517 }
14518 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14519 {
14520 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14521 {
14522 error_at (OMP_CLAUSE_LOCATION (c),
14523 "%qE does not have %<omp_depend_t%> type in "
14524 "%<depend%> clause with %<depobj%> dependence "
14525 "type", t);
14526 remove = true;
14527 }
14528 }
14529 else if (c_omp_depend_t_p (TREE_TYPE (t)))
14530 {
14531 error_at (OMP_CLAUSE_LOCATION (c),
14532 "%qE should not have %<omp_depend_t%> type in "
14533 "%<depend%> clause with dependence type other than "
14534 "%<depobj%>", t);
14535 remove = true;
14536 }
14537 if (!remove)
14538 {
14539 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14540 t, false);
14541 if (addr == error_mark_node)
14542 remove = true;
14543 else
14544 {
14545 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14546 RO_UNARY_STAR);
14547 if (t == error_mark_node)
14548 remove = true;
14549 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14550 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14551 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14552 == TREE_VEC))
14553 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14554 else
14555 OMP_CLAUSE_DECL (c) = t;
14556 }
14557 }
14558 break;
14559
14560 case OMP_CLAUSE_MAP:
14561 case OMP_CLAUSE_TO:
14562 case OMP_CLAUSE_FROM:
14563 case OMP_CLAUSE__CACHE_:
14564 t = OMP_CLAUSE_DECL (c);
14565 if (TREE_CODE (t) == TREE_LIST)
14566 {
14567 if (handle_omp_array_sections (c, ort))
14568 remove = true;
14569 else
14570 {
14571 t = OMP_CLAUSE_DECL (c);
14572 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14573 {
14574 error_at (OMP_CLAUSE_LOCATION (c),
14575 "array section does not have mappable type "
14576 "in %qs clause",
14577 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14578 remove = true;
14579 }
14580 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14581 {
14582 error_at (OMP_CLAUSE_LOCATION (c),
14583 "%<_Atomic%> %qE in %qs clause", t,
14584 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14585 remove = true;
14586 }
14587 while (TREE_CODE (t) == ARRAY_REF)
14588 t = TREE_OPERAND (t, 0);
14589 if (TREE_CODE (t) == COMPONENT_REF
14590 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14591 {
14592 while (TREE_CODE (t) == COMPONENT_REF)
14593 t = TREE_OPERAND (t, 0);
14594 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14595 break;
14596 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14597 {
14598 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14599 error_at (OMP_CLAUSE_LOCATION (c),
14600 "%qD appears more than once in motion "
14601 "clauses", t);
14602 else if (ort == C_ORT_ACC)
14603 error_at (OMP_CLAUSE_LOCATION (c),
14604 "%qD appears more than once in data "
14605 "clauses", t);
14606 else
14607 error_at (OMP_CLAUSE_LOCATION (c),
14608 "%qD appears more than once in map "
14609 "clauses", t);
14610 remove = true;
14611 }
14612 else
14613 {
14614 bitmap_set_bit (&map_head, DECL_UID (t));
14615 bitmap_set_bit (&map_field_head, DECL_UID (t));
14616 }
14617 }
14618 }
14619 if (c_oacc_check_attachments (c))
14620 remove = true;
14621 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14622 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14623 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14624 /* In this case, we have a single array element which is a
14625 pointer, and we already set OMP_CLAUSE_SIZE in
14626 handle_omp_array_sections above. For attach/detach clauses,
14627 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
14628 here. */
14629 OMP_CLAUSE_SIZE (c) = size_zero_node;
14630 break;
14631 }
14632 if (t == error_mark_node)
14633 {
14634 remove = true;
14635 break;
14636 }
14637 /* OpenACC attach / detach clauses must be pointers. */
14638 if (c_oacc_check_attachments (c))
14639 {
14640 remove = true;
14641 break;
14642 }
14643 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14644 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14645 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14646 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
14647 bias) to zero here, so it is not set erroneously to the pointer
14648 size later on in gimplify.c. */
14649 OMP_CLAUSE_SIZE (c) = size_zero_node;
14650 if (TREE_CODE (t) == COMPONENT_REF
14651 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14652 {
14653 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14654 {
14655 error_at (OMP_CLAUSE_LOCATION (c),
14656 "bit-field %qE in %qs clause",
14657 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14658 remove = true;
14659 }
14660 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14661 {
14662 error_at (OMP_CLAUSE_LOCATION (c),
14663 "%qE does not have a mappable type in %qs clause",
14664 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14665 remove = true;
14666 }
14667 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14668 {
14669 error_at (OMP_CLAUSE_LOCATION (c),
14670 "%<_Atomic%> %qE in %qs clause", t,
14671 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14672 remove = true;
14673 }
14674 while (TREE_CODE (t) == COMPONENT_REF)
14675 {
14676 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14677 == UNION_TYPE)
14678 {
14679 error_at (OMP_CLAUSE_LOCATION (c),
14680 "%qE is a member of a union", t);
14681 remove = true;
14682 break;
14683 }
14684 t = TREE_OPERAND (t, 0);
14685 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
14686 {
14687 if (maybe_ne (mem_ref_offset (t), 0))
14688 error_at (OMP_CLAUSE_LOCATION (c),
14689 "cannot dereference %qE in %qs clause", t,
14690 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14691 else
14692 t = TREE_OPERAND (t, 0);
14693 }
14694 }
14695 if (remove)
14696 break;
14697 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14698 {
14699 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14700 break;
14701 }
14702 }
14703 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14704 {
14705 error_at (OMP_CLAUSE_LOCATION (c),
14706 "%qE is not a variable in %qs clause", t,
14707 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14708 remove = true;
14709 }
14710 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14711 {
14712 error_at (OMP_CLAUSE_LOCATION (c),
14713 "%qD is threadprivate variable in %qs clause", t,
14714 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14715 remove = true;
14716 }
14717 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14718 || (OMP_CLAUSE_MAP_KIND (c)
14719 != GOMP_MAP_FIRSTPRIVATE_POINTER))
14720 && !c_mark_addressable (t))
14721 remove = true;
14722 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14723 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
14724 || (OMP_CLAUSE_MAP_KIND (c)
14725 == GOMP_MAP_FIRSTPRIVATE_POINTER)
14726 || (OMP_CLAUSE_MAP_KIND (c)
14727 == GOMP_MAP_FORCE_DEVICEPTR)))
14728 && t == OMP_CLAUSE_DECL (c)
14729 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14730 {
14731 error_at (OMP_CLAUSE_LOCATION (c),
14732 "%qD does not have a mappable type in %qs clause", t,
14733 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14734 remove = true;
14735 }
14736 else if (TREE_TYPE (t) == error_mark_node)
14737 remove = true;
14738 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14739 {
14740 error_at (OMP_CLAUSE_LOCATION (c),
14741 "%<_Atomic%> %qE in %qs clause", t,
14742 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14743 remove = true;
14744 }
14745 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14746 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14747 {
14748 if (bitmap_bit_p (&generic_head, DECL_UID (t))
14749 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14750 {
14751 error_at (OMP_CLAUSE_LOCATION (c),
14752 "%qD appears more than once in data clauses", t);
14753 remove = true;
14754 }
14755 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14756 {
14757 if (ort == C_ORT_ACC)
14758 error_at (OMP_CLAUSE_LOCATION (c),
14759 "%qD appears more than once in data clauses", t);
14760 else
14761 error_at (OMP_CLAUSE_LOCATION (c),
14762 "%qD appears both in data and map clauses", t);
14763 remove = true;
14764 }
14765 else
14766 bitmap_set_bit (&generic_head, DECL_UID (t));
14767 }
14768 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14769 {
14770 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14771 error_at (OMP_CLAUSE_LOCATION (c),
14772 "%qD appears more than once in motion clauses", t);
14773 else if (ort == C_ORT_ACC)
14774 error_at (OMP_CLAUSE_LOCATION (c),
14775 "%qD appears more than once in data clauses", t);
14776 else
14777 error_at (OMP_CLAUSE_LOCATION (c),
14778 "%qD appears more than once in map clauses", t);
14779 remove = true;
14780 }
14781 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14782 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14783 {
14784 if (ort == C_ORT_ACC)
14785 error_at (OMP_CLAUSE_LOCATION (c),
14786 "%qD appears more than once in data clauses", t);
14787 else
14788 error_at (OMP_CLAUSE_LOCATION (c),
14789 "%qD appears both in data and map clauses", t);
14790 remove = true;
14791 }
14792 else
14793 {
14794 bitmap_set_bit (&map_head, DECL_UID (t));
14795 if (t != OMP_CLAUSE_DECL (c)
14796 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14797 bitmap_set_bit (&map_field_head, DECL_UID (t));
14798 }
14799 break;
14800
14801 case OMP_CLAUSE_TO_DECLARE:
14802 case OMP_CLAUSE_LINK:
14803 t = OMP_CLAUSE_DECL (c);
14804 if (TREE_CODE (t) == FUNCTION_DECL
14805 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14806 ;
14807 else if (!VAR_P (t))
14808 {
14809 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14810 error_at (OMP_CLAUSE_LOCATION (c),
14811 "%qE is neither a variable nor a function name in "
14812 "clause %qs", t,
14813 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14814 else
14815 error_at (OMP_CLAUSE_LOCATION (c),
14816 "%qE is not a variable in clause %qs", t,
14817 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14818 remove = true;
14819 }
14820 else if (DECL_THREAD_LOCAL_P (t))
14821 {
14822 error_at (OMP_CLAUSE_LOCATION (c),
14823 "%qD is threadprivate variable in %qs clause", t,
14824 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14825 remove = true;
14826 }
14827 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14828 {
14829 error_at (OMP_CLAUSE_LOCATION (c),
14830 "%qD does not have a mappable type in %qs clause", t,
14831 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14832 remove = true;
14833 }
14834 if (remove)
14835 break;
14836 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
14837 {
14838 error_at (OMP_CLAUSE_LOCATION (c),
14839 "%qE appears more than once on the same "
14840 "%<declare target%> directive", t);
14841 remove = true;
14842 }
14843 else
14844 bitmap_set_bit (&generic_head, DECL_UID (t));
14845 break;
14846
14847 case OMP_CLAUSE_UNIFORM:
14848 t = OMP_CLAUSE_DECL (c);
14849 if (TREE_CODE (t) != PARM_DECL)
14850 {
14851 if (DECL_P (t))
14852 error_at (OMP_CLAUSE_LOCATION (c),
14853 "%qD is not an argument in %<uniform%> clause", t);
14854 else
14855 error_at (OMP_CLAUSE_LOCATION (c),
14856 "%qE is not an argument in %<uniform%> clause", t);
14857 remove = true;
14858 break;
14859 }
14860 /* map_head bitmap is used as uniform_head if declare_simd. */
14861 bitmap_set_bit (&map_head, DECL_UID (t));
14862 goto check_dup_generic;
14863
14864 case OMP_CLAUSE_IS_DEVICE_PTR:
14865 case OMP_CLAUSE_USE_DEVICE_PTR:
14866 t = OMP_CLAUSE_DECL (c);
14867 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14868 {
14869 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14870 && ort == C_ORT_OMP)
14871 {
14872 error_at (OMP_CLAUSE_LOCATION (c),
14873 "%qs variable is not a pointer",
14874 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14875 remove = true;
14876 }
14877 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14878 {
14879 error_at (OMP_CLAUSE_LOCATION (c),
14880 "%qs variable is neither a pointer nor an array",
14881 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14882 remove = true;
14883 }
14884 }
14885 goto check_dup_generic;
14886
14887 case OMP_CLAUSE_USE_DEVICE_ADDR:
14888 t = OMP_CLAUSE_DECL (c);
14889 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14890 c_mark_addressable (t);
14891 goto check_dup_generic;
14892
14893 case OMP_CLAUSE_NOWAIT:
14894 if (copyprivate_seen)
14895 {
14896 error_at (OMP_CLAUSE_LOCATION (c),
14897 "%<nowait%> clause must not be used together "
14898 "with %<copyprivate%>");
14899 remove = true;
14900 break;
14901 }
14902 nowait_clause = pc;
14903 pc = &OMP_CLAUSE_CHAIN (c);
14904 continue;
14905
14906 case OMP_CLAUSE_ORDER:
14907 if (ordered_clause)
14908 {
14909 error_at (OMP_CLAUSE_LOCATION (c),
14910 "%<order%> clause must not be used together "
14911 "with %<ordered%>");
14912 remove = true;
14913 break;
14914 }
14915 else if (order_clause)
14916 {
14917 /* Silently remove duplicates. */
14918 remove = true;
14919 break;
14920 }
14921 order_clause = pc;
14922 pc = &OMP_CLAUSE_CHAIN (c);
14923 continue;
14924
14925 case OMP_CLAUSE_IF:
14926 case OMP_CLAUSE_NUM_THREADS:
14927 case OMP_CLAUSE_NUM_TEAMS:
14928 case OMP_CLAUSE_THREAD_LIMIT:
14929 case OMP_CLAUSE_DEFAULT:
14930 case OMP_CLAUSE_UNTIED:
14931 case OMP_CLAUSE_COLLAPSE:
14932 case OMP_CLAUSE_FINAL:
14933 case OMP_CLAUSE_MERGEABLE:
14934 case OMP_CLAUSE_DEVICE:
14935 case OMP_CLAUSE_DIST_SCHEDULE:
14936 case OMP_CLAUSE_PARALLEL:
14937 case OMP_CLAUSE_FOR:
14938 case OMP_CLAUSE_SECTIONS:
14939 case OMP_CLAUSE_TASKGROUP:
14940 case OMP_CLAUSE_PROC_BIND:
14941 case OMP_CLAUSE_DEVICE_TYPE:
14942 case OMP_CLAUSE_PRIORITY:
14943 case OMP_CLAUSE_GRAINSIZE:
14944 case OMP_CLAUSE_NUM_TASKS:
14945 case OMP_CLAUSE_THREADS:
14946 case OMP_CLAUSE_SIMD:
14947 case OMP_CLAUSE_HINT:
14948 case OMP_CLAUSE_DEFAULTMAP:
14949 case OMP_CLAUSE_BIND:
14950 case OMP_CLAUSE_NUM_GANGS:
14951 case OMP_CLAUSE_NUM_WORKERS:
14952 case OMP_CLAUSE_VECTOR_LENGTH:
14953 case OMP_CLAUSE_ASYNC:
14954 case OMP_CLAUSE_WAIT:
14955 case OMP_CLAUSE_AUTO:
14956 case OMP_CLAUSE_INDEPENDENT:
14957 case OMP_CLAUSE_SEQ:
14958 case OMP_CLAUSE_GANG:
14959 case OMP_CLAUSE_WORKER:
14960 case OMP_CLAUSE_VECTOR:
14961 case OMP_CLAUSE_TILE:
14962 case OMP_CLAUSE_IF_PRESENT:
14963 case OMP_CLAUSE_FINALIZE:
14964 pc = &OMP_CLAUSE_CHAIN (c);
14965 continue;
14966
14967 case OMP_CLAUSE_NOGROUP:
14968 nogroup_seen = pc;
14969 pc = &OMP_CLAUSE_CHAIN (c);
14970 continue;
14971
14972 case OMP_CLAUSE_SCHEDULE:
14973 schedule_clause = c;
14974 pc = &OMP_CLAUSE_CHAIN (c);
14975 continue;
14976
14977 case OMP_CLAUSE_ORDERED:
14978 ordered_clause = c;
14979 if (order_clause)
14980 {
14981 error_at (OMP_CLAUSE_LOCATION (*order_clause),
14982 "%<order%> clause must not be used together "
14983 "with %<ordered%>");
14984 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
14985 order_clause = NULL;
14986 }
14987 pc = &OMP_CLAUSE_CHAIN (c);
14988 continue;
14989
14990 case OMP_CLAUSE_SAFELEN:
14991 safelen = c;
14992 pc = &OMP_CLAUSE_CHAIN (c);
14993 continue;
14994 case OMP_CLAUSE_SIMDLEN:
14995 simdlen = c;
14996 pc = &OMP_CLAUSE_CHAIN (c);
14997 continue;
14998
14999 case OMP_CLAUSE_INBRANCH:
15000 case OMP_CLAUSE_NOTINBRANCH:
15001 if (branch_seen)
15002 {
15003 error_at (OMP_CLAUSE_LOCATION (c),
15004 "%<inbranch%> clause is incompatible with "
15005 "%<notinbranch%>");
15006 remove = true;
15007 break;
15008 }
15009 branch_seen = true;
15010 pc = &OMP_CLAUSE_CHAIN (c);
15011 continue;
15012
15013 case OMP_CLAUSE_INCLUSIVE:
15014 case OMP_CLAUSE_EXCLUSIVE:
15015 need_complete = true;
15016 need_implicitly_determined = true;
15017 t = OMP_CLAUSE_DECL (c);
15018 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15019 {
15020 error_at (OMP_CLAUSE_LOCATION (c),
15021 "%qE is not a variable in clause %qs", t,
15022 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15023 remove = true;
15024 }
15025 break;
15026
15027 default:
15028 gcc_unreachable ();
15029 }
15030
15031 if (!remove)
15032 {
15033 t = OMP_CLAUSE_DECL (c);
15034
15035 if (need_complete)
15036 {
15037 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15038 if (t == error_mark_node)
15039 remove = true;
15040 }
15041
15042 if (need_implicitly_determined)
15043 {
15044 const char *share_name = NULL;
15045
15046 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15047 share_name = "threadprivate";
15048 else switch (c_omp_predetermined_sharing (t))
15049 {
15050 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15051 break;
15052 case OMP_CLAUSE_DEFAULT_SHARED:
15053 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15054 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15055 && c_omp_predefined_variable (t))
15056 /* The __func__ variable and similar function-local
15057 predefined variables may be listed in a shared or
15058 firstprivate clause. */
15059 break;
15060 share_name = "shared";
15061 break;
15062 case OMP_CLAUSE_DEFAULT_PRIVATE:
15063 share_name = "private";
15064 break;
15065 default:
15066 gcc_unreachable ();
15067 }
15068 if (share_name)
15069 {
15070 error_at (OMP_CLAUSE_LOCATION (c),
15071 "%qE is predetermined %qs for %qs",
15072 t, share_name,
15073 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15074 remove = true;
15075 }
15076 else if (TREE_READONLY (t)
15077 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15078 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15079 {
15080 error_at (OMP_CLAUSE_LOCATION (c),
15081 "%<const%> qualified %qE may appear only in "
15082 "%<shared%> or %<firstprivate%> clauses", t);
15083 remove = true;
15084 }
15085 }
15086 }
15087
15088 if (remove)
15089 *pc = OMP_CLAUSE_CHAIN (c);
15090 else
15091 pc = &OMP_CLAUSE_CHAIN (c);
15092 }
15093
15094 if (simdlen
15095 && safelen
15096 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15097 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15098 {
15099 error_at (OMP_CLAUSE_LOCATION (simdlen),
15100 "%<simdlen%> clause value is bigger than "
15101 "%<safelen%> clause value");
15102 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15103 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15104 }
15105
15106 if (ordered_clause
15107 && schedule_clause
15108 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15109 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15110 {
15111 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15112 "%<nonmonotonic%> schedule modifier specified together "
15113 "with %<ordered%> clause");
15114 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15115 = (enum omp_clause_schedule_kind)
15116 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15117 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15118 }
15119
15120 if (reduction_seen < 0 && ordered_clause)
15121 {
15122 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15123 "%qs clause specified together with %<inscan%> "
15124 "%<reduction%> clause", "ordered");
15125 reduction_seen = -2;
15126 }
15127
15128 if (reduction_seen < 0 && schedule_clause)
15129 {
15130 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15131 "%qs clause specified together with %<inscan%> "
15132 "%<reduction%> clause", "schedule");
15133 reduction_seen = -2;
15134 }
15135
15136 if (linear_variable_step_check || reduction_seen == -2)
15137 for (pc = &clauses, c = clauses; c ; c = *pc)
15138 {
15139 bool remove = false;
15140 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15141 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15142 && !bitmap_bit_p (&map_head,
15143 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15144 {
15145 error_at (OMP_CLAUSE_LOCATION (c),
15146 "%<linear%> clause step is a parameter %qD not "
15147 "specified in %<uniform%> clause",
15148 OMP_CLAUSE_LINEAR_STEP (c));
15149 remove = true;
15150 }
15151 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
15152 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15153
15154 if (remove)
15155 *pc = OMP_CLAUSE_CHAIN (c);
15156 else
15157 pc = &OMP_CLAUSE_CHAIN (c);
15158 }
15159
15160 if (nogroup_seen && reduction_seen)
15161 {
15162 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15163 "%<nogroup%> clause must not be used together with "
15164 "%<reduction%> clause");
15165 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15166 }
15167
15168 bitmap_obstack_release (NULL);
15169 return clauses;
15170 }
15171
15172 /* Return code to initialize DST with a copy constructor from SRC.
15173 C doesn't have copy constructors nor assignment operators, only for
15174 _Atomic vars we need to perform __atomic_load from src into a temporary
15175 followed by __atomic_store of the temporary to dst. */
15176
15177 tree
c_omp_clause_copy_ctor(tree clause,tree dst,tree src)15178 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15179 {
15180 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15181 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15182
15183 location_t loc = OMP_CLAUSE_LOCATION (clause);
15184 tree type = TREE_TYPE (dst);
15185 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15186 tree tmp = create_tmp_var (nonatomic_type);
15187 tree tmp_addr = build_fold_addr_expr (tmp);
15188 TREE_ADDRESSABLE (tmp) = 1;
15189 TREE_NO_WARNING (tmp) = 1;
15190 tree src_addr = build_fold_addr_expr (src);
15191 tree dst_addr = build_fold_addr_expr (dst);
15192 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15193 vec<tree, va_gc> *params;
15194 /* Expansion of a generic atomic load may require an addition
15195 element, so allocate enough to prevent a resize. */
15196 vec_alloc (params, 4);
15197
15198 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
15199 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15200 params->quick_push (src_addr);
15201 params->quick_push (tmp_addr);
15202 params->quick_push (seq_cst);
15203 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15204
15205 vec_alloc (params, 4);
15206
15207 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
15208 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15209 params->quick_push (dst_addr);
15210 params->quick_push (tmp_addr);
15211 params->quick_push (seq_cst);
15212 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15213 return build2 (COMPOUND_EXPR, void_type_node, load, store);
15214 }
15215
15216 /* Create a transaction node. */
15217
15218 tree
c_finish_transaction(location_t loc,tree block,int flags)15219 c_finish_transaction (location_t loc, tree block, int flags)
15220 {
15221 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15222 if (flags & TM_STMT_ATTR_OUTER)
15223 TRANSACTION_EXPR_OUTER (stmt) = 1;
15224 if (flags & TM_STMT_ATTR_RELAXED)
15225 TRANSACTION_EXPR_RELAXED (stmt) = 1;
15226 return add_stmt (stmt);
15227 }
15228
15229 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15230 down to the element type of an array. If ORIG_QUAL_TYPE is not
15231 NULL, then it should be used as the qualified type
15232 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15233 preserve information about the typedef name from which an array
15234 type was derived). */
15235
15236 tree
c_build_qualified_type(tree type,int type_quals,tree orig_qual_type,size_t orig_qual_indirect)15237 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15238 size_t orig_qual_indirect)
15239 {
15240 if (type == error_mark_node)
15241 return type;
15242
15243 if (TREE_CODE (type) == ARRAY_TYPE)
15244 {
15245 tree t;
15246 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15247 type_quals, orig_qual_type,
15248 orig_qual_indirect - 1);
15249
15250 /* See if we already have an identically qualified type. */
15251 if (orig_qual_type && orig_qual_indirect == 0)
15252 t = orig_qual_type;
15253 else
15254 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15255 {
15256 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15257 && TYPE_NAME (t) == TYPE_NAME (type)
15258 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15259 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15260 TYPE_ATTRIBUTES (type)))
15261 break;
15262 }
15263 if (!t)
15264 {
15265 tree domain = TYPE_DOMAIN (type);
15266
15267 t = build_variant_type_copy (type);
15268 TREE_TYPE (t) = element_type;
15269
15270 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15271 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15272 SET_TYPE_STRUCTURAL_EQUALITY (t);
15273 else if (TYPE_CANONICAL (element_type) != element_type
15274 || (domain && TYPE_CANONICAL (domain) != domain))
15275 {
15276 tree unqualified_canon
15277 = build_array_type (TYPE_CANONICAL (element_type),
15278 domain? TYPE_CANONICAL (domain)
15279 : NULL_TREE);
15280 if (TYPE_REVERSE_STORAGE_ORDER (type))
15281 {
15282 unqualified_canon
15283 = build_distinct_type_copy (unqualified_canon);
15284 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15285 }
15286 TYPE_CANONICAL (t)
15287 = c_build_qualified_type (unqualified_canon, type_quals);
15288 }
15289 else
15290 TYPE_CANONICAL (t) = t;
15291 }
15292 return t;
15293 }
15294
15295 /* A restrict-qualified pointer type must be a pointer to object or
15296 incomplete type. Note that the use of POINTER_TYPE_P also allows
15297 REFERENCE_TYPEs, which is appropriate for C++. */
15298 if ((type_quals & TYPE_QUAL_RESTRICT)
15299 && (!POINTER_TYPE_P (type)
15300 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15301 {
15302 error ("invalid use of %<restrict%>");
15303 type_quals &= ~TYPE_QUAL_RESTRICT;
15304 }
15305
15306 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15307 ? orig_qual_type
15308 : build_qualified_type (type, type_quals));
15309 /* A variant type does not inherit the list of incomplete vars from the
15310 type main variant. */
15311 if ((RECORD_OR_UNION_TYPE_P (var_type)
15312 || TREE_CODE (var_type) == ENUMERAL_TYPE)
15313 && TYPE_MAIN_VARIANT (var_type) != var_type)
15314 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15315 return var_type;
15316 }
15317
15318 /* Build a VA_ARG_EXPR for the C parser. */
15319
15320 tree
c_build_va_arg(location_t loc1,tree expr,location_t loc2,tree type)15321 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15322 {
15323 if (error_operand_p (type))
15324 return error_mark_node;
15325 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15326 order because it takes the address of the expression. */
15327 else if (handled_component_p (expr)
15328 && reverse_storage_order_for_component_p (expr))
15329 {
15330 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15331 return error_mark_node;
15332 }
15333 else if (!COMPLETE_TYPE_P (type))
15334 {
15335 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15336 "type %qT", type);
15337 return error_mark_node;
15338 }
15339 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15340 warning_at (loc2, OPT_Wc___compat,
15341 "C++ requires promoted type, not enum type, in %<va_arg%>");
15342 return build_va_arg (loc2, expr, type);
15343 }
15344
15345 /* Return truthvalue of whether T1 is the same tree structure as T2.
15346 Return 1 if they are the same. Return false if they are different. */
15347
15348 bool
c_tree_equal(tree t1,tree t2)15349 c_tree_equal (tree t1, tree t2)
15350 {
15351 enum tree_code code1, code2;
15352
15353 if (t1 == t2)
15354 return true;
15355 if (!t1 || !t2)
15356 return false;
15357
15358 for (code1 = TREE_CODE (t1);
15359 CONVERT_EXPR_CODE_P (code1)
15360 || code1 == NON_LVALUE_EXPR;
15361 code1 = TREE_CODE (t1))
15362 t1 = TREE_OPERAND (t1, 0);
15363 for (code2 = TREE_CODE (t2);
15364 CONVERT_EXPR_CODE_P (code2)
15365 || code2 == NON_LVALUE_EXPR;
15366 code2 = TREE_CODE (t2))
15367 t2 = TREE_OPERAND (t2, 0);
15368
15369 /* They might have become equal now. */
15370 if (t1 == t2)
15371 return true;
15372
15373 if (code1 != code2)
15374 return false;
15375
15376 switch (code1)
15377 {
15378 case INTEGER_CST:
15379 return wi::to_wide (t1) == wi::to_wide (t2);
15380
15381 case REAL_CST:
15382 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15383
15384 case STRING_CST:
15385 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15386 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15387 TREE_STRING_LENGTH (t1));
15388
15389 case FIXED_CST:
15390 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15391 TREE_FIXED_CST (t2));
15392
15393 case COMPLEX_CST:
15394 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15395 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15396
15397 case VECTOR_CST:
15398 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15399
15400 case CONSTRUCTOR:
15401 /* We need to do this when determining whether or not two
15402 non-type pointer to member function template arguments
15403 are the same. */
15404 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15405 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15406 return false;
15407 {
15408 tree field, value;
15409 unsigned int i;
15410 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15411 {
15412 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15413 if (!c_tree_equal (field, elt2->index)
15414 || !c_tree_equal (value, elt2->value))
15415 return false;
15416 }
15417 }
15418 return true;
15419
15420 case TREE_LIST:
15421 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15422 return false;
15423 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15424 return false;
15425 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15426
15427 case SAVE_EXPR:
15428 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15429
15430 case CALL_EXPR:
15431 {
15432 tree arg1, arg2;
15433 call_expr_arg_iterator iter1, iter2;
15434 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15435 return false;
15436 for (arg1 = first_call_expr_arg (t1, &iter1),
15437 arg2 = first_call_expr_arg (t2, &iter2);
15438 arg1 && arg2;
15439 arg1 = next_call_expr_arg (&iter1),
15440 arg2 = next_call_expr_arg (&iter2))
15441 if (!c_tree_equal (arg1, arg2))
15442 return false;
15443 if (arg1 || arg2)
15444 return false;
15445 return true;
15446 }
15447
15448 case TARGET_EXPR:
15449 {
15450 tree o1 = TREE_OPERAND (t1, 0);
15451 tree o2 = TREE_OPERAND (t2, 0);
15452
15453 /* Special case: if either target is an unallocated VAR_DECL,
15454 it means that it's going to be unified with whatever the
15455 TARGET_EXPR is really supposed to initialize, so treat it
15456 as being equivalent to anything. */
15457 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
15458 && !DECL_RTL_SET_P (o1))
15459 /*Nop*/;
15460 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
15461 && !DECL_RTL_SET_P (o2))
15462 /*Nop*/;
15463 else if (!c_tree_equal (o1, o2))
15464 return false;
15465
15466 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15467 }
15468
15469 case COMPONENT_REF:
15470 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15471 return false;
15472 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15473
15474 case PARM_DECL:
15475 case VAR_DECL:
15476 case CONST_DECL:
15477 case FIELD_DECL:
15478 case FUNCTION_DECL:
15479 case IDENTIFIER_NODE:
15480 case SSA_NAME:
15481 return false;
15482
15483 case TREE_VEC:
15484 {
15485 unsigned ix;
15486 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15487 return false;
15488 for (ix = TREE_VEC_LENGTH (t1); ix--;)
15489 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15490 TREE_VEC_ELT (t2, ix)))
15491 return false;
15492 return true;
15493 }
15494
15495 default:
15496 break;
15497 }
15498
15499 switch (TREE_CODE_CLASS (code1))
15500 {
15501 case tcc_unary:
15502 case tcc_binary:
15503 case tcc_comparison:
15504 case tcc_expression:
15505 case tcc_vl_exp:
15506 case tcc_reference:
15507 case tcc_statement:
15508 {
15509 int i, n = TREE_OPERAND_LENGTH (t1);
15510
15511 switch (code1)
15512 {
15513 case PREINCREMENT_EXPR:
15514 case PREDECREMENT_EXPR:
15515 case POSTINCREMENT_EXPR:
15516 case POSTDECREMENT_EXPR:
15517 n = 1;
15518 break;
15519 case ARRAY_REF:
15520 n = 2;
15521 break;
15522 default:
15523 break;
15524 }
15525
15526 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15527 && n != TREE_OPERAND_LENGTH (t2))
15528 return false;
15529
15530 for (i = 0; i < n; ++i)
15531 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15532 return false;
15533
15534 return true;
15535 }
15536
15537 case tcc_type:
15538 return comptypes (t1, t2);
15539 default:
15540 gcc_unreachable ();
15541 }
15542 /* We can get here with --disable-checking. */
15543 return false;
15544 }
15545
15546 /* Returns true when the function declaration FNDECL is implicit,
15547 introduced as a result of a call to an otherwise undeclared
15548 function, and false otherwise. */
15549
15550 bool
c_decl_implicit(const_tree fndecl)15551 c_decl_implicit (const_tree fndecl)
15552 {
15553 return C_DECL_IMPLICIT (fndecl);
15554 }
15555